Changeset 23821


Ignore:
Timestamp:
Jul 7, 2013, 1:28:12 PM (11 years ago)
Author:
mistic100
Message:

bug:2898 make some updates methods static + factorize code from plugins, themes & languages

Location:
trunk/admin/include
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/include/functions.php

    r23272 r23821  
    25282528  return $sub_dirs;
    25292529}
     2530
     2531/**
     2532 * recursively delete a directory
     2533 * @param string $path
     2534 * @param string $trash_path, try to move the directory to this path if it cannot be delete
     2535 */
     2536function deltree($path, $trash_path=null)
     2537{
     2538  if (is_dir($path))
     2539  {
     2540    $fh = opendir($path);
     2541    while ($file = readdir($fh))
     2542    {
     2543      if ($file != '.' and $file != '..')
     2544      {
     2545        $pathfile = $path . '/' . $file;
     2546        if (is_dir($pathfile))
     2547        {
     2548          deltree($pathfile, $trash_path);
     2549        }
     2550        else
     2551        {
     2552          @unlink($pathfile);
     2553        }
     2554      }
     2555    }
     2556    closedir($fh);
     2557   
     2558    if (@rmdir($path))
     2559    {
     2560      return true;
     2561    }
     2562    elseif (!empty($trash_path))
     2563    {
     2564      if (!is_dir($trash_path))
     2565      {
     2566        @mkgetdir($trash_path, MKGETDIR_RECURSIVE|MKGETDIR_DIE_ON_ERROR|MKGETDIR_PROTECT_HTACCESS);
     2567      }
     2568      while ($r = $trash_path . '/' . md5(uniqid(rand(), true)))
     2569      {
     2570        if (!is_dir($r))
     2571        {
     2572          @rename($path, $r);
     2573          break;
     2574        }
     2575      }
     2576    }
     2577    else
     2578    {
     2579      return false;
     2580    }
     2581  }
     2582}
     2583
    25302584?>
  • trunk/admin/include/languages.class.php

    r20449 r23821  
    113113        pwg_query($query);
    114114
    115         if (!$this->deltree(PHPWG_ROOT_PATH.'language/'.$language_id))
    116         {
    117           $this->send_to_trash(PHPWG_ROOT_PATH.'language/'.$language_id);
    118         }
     115        deltree(PHPWG_ROOT_PATH.'language/'.$language_id, PHPWG_ROOT_PATH.'language/trash');
    119116        break;
    120117
     
    382379                    elseif (is_dir($path))
    383380                    {
    384                       if (!$this->deltree($path))
    385                       {
    386                         $this->send_to_trash($path);
    387                       }
     381                      deltree($path, PHPWG_ROOT_PATH.'language/trash');
    388382                    }
    389383                  }
     
    407401
    408402  /**
    409    * delete $path directory
    410    * @param string - path
    411    */
    412   function deltree($path)
    413   {
    414     if (is_dir($path))
    415     {
    416       $fh = opendir($path);
    417       while ($file = readdir($fh))
    418       {
    419         if ($file != '.' and $file != '..')
    420         {
    421           $pathfile = $path . '/' . $file;
    422           if (is_dir($pathfile))
    423           {
    424             $this->deltree($pathfile);
    425           }
    426           else
    427           {
    428             @unlink($pathfile);
    429           }
    430         }
    431       }
    432       closedir($fh);
    433       return @rmdir($path);
    434     }
    435   }
    436 
    437   /**
    438    * send $path to trash directory
    439    * @param string - path
    440    */
    441   function send_to_trash($path)
    442   {
    443     $trash_path = PHPWG_ROOT_PATH . 'language/trash';
    444     if (!is_dir($trash_path))
    445     {
    446       @mkdir($trash_path);
    447       $file = @fopen($trash_path . '/.htaccess', 'w');
    448       @fwrite($file, 'deny from all');
    449       @fclose($file);
    450     }
    451     while ($r = $trash_path . '/' . md5(uniqid(rand(), true)))
    452     {
    453       if (!is_dir($r))
    454       {
    455         @rename($path, $r);
    456         break;
    457       }
    458     }
    459   }
    460 
    461   /**
    462403   * Sort functions
    463404   */
  • trunk/admin/include/plugins.class.php

    r20449 r23821  
    167167          break;
    168168        }
    169         if (!$this->deltree(PHPWG_PLUGINS_PATH . $plugin_id))
    170         {
    171           $this->send_to_trash(PHPWG_PLUGINS_PATH . $plugin_id);
    172         }
     169        deltree(PHPWG_PLUGINS_PATH . $plugin_id, PHPWG_PLUGINS_PATH . 'trash');
    173170        break;
    174171    }
     
    515512                  elseif (is_dir($path))
    516513                  {
    517                     if (!$this->deltree($path))
    518                     {
    519                       $this->send_to_trash($path);
    520                     }
     514                    deltree($path, PHPWG_PLUGINS_PATH . 'trash');
    521515                  }
    522516                }
     
    554548    return $merged_extensions;
    555549  }
    556  
    557   /**
    558    * delete $path directory
    559    * @param string - path
    560    */
    561   function deltree($path)
    562   {
    563     if (is_dir($path))
    564     {
    565       $fh = opendir($path);
    566       while ($file = readdir($fh))
    567       {
    568         if ($file != '.' and $file != '..')
    569         {
    570           $pathfile = $path . '/' . $file;
    571           if (is_dir($pathfile))
    572           {
    573             $this->deltree($pathfile);
    574           }
    575           else
    576           {
    577             @unlink($pathfile);
    578           }
    579         }
    580       }
    581       closedir($fh);
    582       return @rmdir($path);
    583     }
    584   }
    585 
    586   /**
    587    * send $path to trash directory
    588    * @param string - path
    589    */
    590   function send_to_trash($path)
    591   {
    592     $trash_path = PHPWG_PLUGINS_PATH . 'trash';
    593     if (!is_dir($trash_path))
    594     {
    595       @mkdir($trash_path);
    596       $file = @fopen($trash_path . '/.htaccess', 'w');
    597       @fwrite($file, 'deny from all');
    598       @fclose($file);
    599     }
    600     while ($r = $trash_path . '/' . md5(uniqid(rand(), true)))
    601     {
    602       if (!is_dir($r))
    603       {
    604         @rename($path, $r);
    605         break;
    606       }
    607     }
    608   }
    609550
    610551  /**
  • trunk/admin/include/themes.class.php

    r23248 r23821  
    222222        }
    223223
    224         if (!$this->deltree(PHPWG_THEMES_PATH.$theme_id))
    225         {
    226           $this->send_to_trash(PHPWG_THEMES_PATH.$theme_id);
    227         }
     224        deltree(PHPWG_THEMES_PATH.$theme_id, PHPWG_THEMES_PATH . 'trash');
    228225        break;
    229226
     
    642639                  elseif (is_dir($path))
    643640                  {
    644                     if (!$this->deltree($path))
    645                     {
    646                       $this->send_to_trash($path);
    647                     }
     641                    deltree($path, PHPWG_THEMES_PATH . 'trash');
    648642                  }
    649643                }
     
    665659
    666660  /**
    667    * delete $path directory
    668    * @param string - path
    669    */
    670   function deltree($path)
    671   {
    672     if (is_dir($path))
    673     {
    674       $fh = opendir($path);
    675       while ($file = readdir($fh))
    676       {
    677         if ($file != '.' and $file != '..')
    678         {
    679           $pathfile = $path . '/' . $file;
    680           if (is_dir($pathfile))
    681           {
    682             $this->deltree($pathfile);
    683           }
    684           else
    685           {
    686             @unlink($pathfile);
    687           }
    688         }
    689       }
    690       closedir($fh);
    691       return @rmdir($path);
    692     }
    693   }
    694 
    695   /**
    696    * send $path to trash directory
    697    * @param string - path
    698    */
    699   function send_to_trash($path)
    700   {
    701     $trash_path = PHPWG_THEMES_PATH . 'trash';
    702     if (!is_dir($trash_path))
    703     {
    704       @mkdir($trash_path);
    705       $file = @fopen($trash_path . '/.htaccess', 'w');
    706       @fwrite($file, 'deny from all');
    707       @fclose($file);
    708     }
    709     while ($r = $trash_path . '/' . md5(uniqid(rand(), true)))
    710     {
    711       if (!is_dir($r))
    712       {
    713         @rename($path, $r);
    714         break;
    715       }
    716     }
    717   }
    718 
    719   /**
    720661   * Sort functions
    721662   */
  • trunk/admin/include/updates.class.php

    r16179 r23821  
    3434  }
    3535
    36   function check_piwigo_upgrade()
     36  static function check_piwigo_upgrade()
    3737  {
    3838    $_SESSION['need_update'] = null;
     
    243243  }
    244244
    245   function deltree($path, $move_to_trash=false)
    246   {
    247     if (is_dir($path))
    248     {
    249       $fh = opendir($path);
    250       while ($file = readdir($fh))
    251       {
    252         if ($file != '.' and $file != '..')
    253         {
    254           $pathfile = $path . '/' . $file;
    255           if (is_dir($pathfile))
    256           {
    257             self::deltree($pathfile, $move_to_trash);
    258           }
    259           else
    260           {
    261             @unlink($pathfile);
    262           }
    263         }
    264       }
    265       closedir($fh);
    266       if (@rmdir($path))
    267       {
    268         return true;
    269       }
    270       elseif ($move_to_trash)
    271       {
    272         $trash = PHPWG_ROOT_PATH.'_trash';
    273         if (!is_dir($trash))
    274         {
    275           @mkgetdir($trash);
    276         }
    277         return @rename($path, $trash . '/'.md5(uniqid(rand(), true)));
    278       }
    279       else
    280       {
    281         return false;
    282       }
    283     }
    284   }
    285 
    286   function process_obsolete_list($file)
     245  static function process_obsolete_list($file)
    287246  {
    288247    if (file_exists(PHPWG_ROOT_PATH.$file)
     
    300259        elseif (is_dir($path))
    301260        {
    302           self::deltree($path, true);
    303         }
    304       }
    305     }
    306   }
    307 
    308   function dump_database($include_history=false)
     261          deltree($path, PHPWG_ROOT_PATH.'_trash');
     262        }
     263      }
     264    }
     265  }
     266
     267  static function dump_database($include_history=false)
    309268  {
    310269    global $page, $conf, $cfgBase;
     
    351310
    352311      @readfile($backupFile);
    353       self::deltree(PHPWG_ROOT_PATH.$conf['data_location'].'update');
     312      deltree(PHPWG_ROOT_PATH.$conf['data_location'].'update');
    354313      exit();
    355314    }
     
    360319  }
    361320
    362   function upgrade_to($upgrade_to, &$step, $check_current_version=true)
     321  static function upgrade_to($upgrade_to, &$step, $check_current_version=true)
    363322  {
    364323    global $page, $conf, $template;
     
    450409          {
    451410            self::process_obsolete_list($obsolete_list);
    452             self::deltree(PHPWG_ROOT_PATH.$conf['data_location'].'update');
     411            deltree(PHPWG_ROOT_PATH.$conf['data_location'].'update');
    453412            invalidate_user_cache(true);
    454413            $template->delete_compiled_templates();
     
    478437        else
    479438        {
    480           self::deltree(PHPWG_ROOT_PATH.$conf['data_location'].'update');
     439          deltree(PHPWG_ROOT_PATH.$conf['data_location'].'update');
    481440          array_push($page['errors'], l10n('An error has occured during upgrade.'));
    482441        }
Note: See TracChangeset for help on using the changeset viewer.