[28834] | 1 | <?php |
---|
| 2 | defined('PHPWG_ROOT_PATH') or die('Hacking attempt!'); |
---|
| 3 | |
---|
| 4 | class BatchDownloader_maintain extends PluginMaintain |
---|
| 5 | { |
---|
| 6 | private $table_download_sets; |
---|
| 7 | private $table_download_sets_images; |
---|
| 8 | private $table_image_sizes; |
---|
| 9 | |
---|
| 10 | private $default_conf = array( |
---|
| 11 | 'groups' => array(), |
---|
| 12 | 'level' => 0, |
---|
| 13 | 'what' => array('categories','specials','collections'), |
---|
| 14 | 'photo_size' => 'original', |
---|
| 15 | 'multisize' => true, |
---|
| 16 | 'archive_prefix' => 'piwigo', |
---|
| 17 | 'archive_timeout' => 48, /* hours */ |
---|
| 18 | 'max_elements' => 500, |
---|
| 19 | 'max_size' => 100, /* MB */ |
---|
| 20 | 'last_clean' => 0, |
---|
| 21 | 'one_archive' => false, |
---|
| 22 | 'force_pclzip' => false, |
---|
| 23 | 'direct' => false, |
---|
| 24 | ); |
---|
| 25 | |
---|
| 26 | function __construct($id) |
---|
| 27 | { |
---|
| 28 | global $prefixeTable; |
---|
| 29 | |
---|
| 30 | parent::__construct($id); |
---|
| 31 | $this->table_download_sets = $prefixeTable . 'download_sets'; |
---|
| 32 | $this->table_download_sets_images = $prefixeTable . 'download_sets_images'; |
---|
| 33 | $this->table_image_sizes = $prefixeTable . 'image_sizes'; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | function install($plugin_version, &$errors=array()) |
---|
| 37 | { |
---|
| 38 | global $conf, $prefixeTable; |
---|
| 39 | |
---|
| 40 | // configuration |
---|
| 41 | if (empty($conf['batch_download'])) |
---|
| 42 | { |
---|
| 43 | $this->default_conf['last_clean'] = time(); |
---|
| 44 | |
---|
| 45 | conf_update_param('batch_download', $this->default_conf, true); |
---|
| 46 | conf_update_param('batch_download_comment', null, true); |
---|
| 47 | } |
---|
| 48 | else |
---|
| 49 | { |
---|
| 50 | $new_conf = safe_unserialize($conf['batch_download']); |
---|
| 51 | |
---|
| 52 | if (!isset($new_conf['what'])) |
---|
| 53 | { |
---|
| 54 | $new_conf['what'] = array('categories','specials','collections'); |
---|
| 55 | } |
---|
| 56 | if (!isset($new_conf['one_archive'])) |
---|
| 57 | { |
---|
| 58 | $new_conf['one_archive'] = false; |
---|
| 59 | $new_conf['force_pclzip'] = isset($conf['batch_download_force_pclzip']) && $conf['batch_download_force_pclzip']; |
---|
| 60 | $new_conf['direct'] = isset($conf['batch_download_direct']) && $conf['batch_download_direct']; |
---|
| 61 | } |
---|
| 62 | if (!isset($new_conf['multisize'])) |
---|
| 63 | { |
---|
| 64 | $new_conf['multisize'] = true; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | conf_update_param('batch_download', $new_conf, true); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | // archives directory |
---|
| 71 | if (!file_exists(PHPWG_ROOT_PATH . $conf['data_location'] . 'download_archives/')) |
---|
| 72 | { |
---|
| 73 | mkgetdir(PHPWG_ROOT_PATH . $conf['data_location'] . 'download_archives/', MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | // create tables |
---|
| 77 | $query = ' |
---|
| 78 | CREATE TABLE IF NOT EXISTS `' . $this->table_download_sets . '` ( |
---|
| 79 | `id` mediumint(8) NOT NULL AUTO_INCREMENT, |
---|
| 80 | `user_id` smallint(5) NOT NULL, |
---|
| 81 | `date_creation` datetime NOT NULL, |
---|
| 82 | `type` varchar(16) NOT NULL, |
---|
| 83 | `type_id` varchar(64) NOT NULL, |
---|
| 84 | `size` varchar(16) NOT NULL DEFAULT "original", |
---|
| 85 | `nb_zip` smallint(3) NOT NULL DEFAULT 0, |
---|
| 86 | `last_zip` smallint(3) NOT NULL DEFAULT 0, |
---|
| 87 | `nb_images` mediumint(8) NOT NULL DEFAULT 0, |
---|
| 88 | `total_size` int(10) NOT NULL DEFAULT 0, |
---|
| 89 | `status` enum("new","ready","download","done") NOT NULL DEFAULT "new", |
---|
| 90 | PRIMARY KEY (`id`) |
---|
| 91 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 |
---|
| 92 | ;'; |
---|
| 93 | pwg_query($query); |
---|
| 94 | |
---|
| 95 | $query = ' |
---|
| 96 | CREATE TABLE IF NOT EXISTS `' . $this->table_download_sets_images . '` ( |
---|
| 97 | `set_id` mediumint(8) NOT NULL, |
---|
| 98 | `image_id` mediumint(8) NOT NULL, |
---|
| 99 | `zip` smallint(5) NOT NULL DEFAULT 0, |
---|
| 100 | UNIQUE KEY `UNIQUE` (`set_id`,`image_id`) |
---|
| 101 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
---|
| 102 | ;'; |
---|
| 103 | pwg_query($query); |
---|
| 104 | |
---|
| 105 | $query = ' |
---|
| 106 | CREATE TABLE IF NOT EXISTS `' . $this->table_image_sizes . '` ( |
---|
| 107 | `image_id` mediumint(8) NOT NULL, |
---|
| 108 | `type` varchar(16) NOT NULL, |
---|
| 109 | `width` smallint(9) NOT NULL, |
---|
| 110 | `height` smallint(9) NOT NULL, |
---|
| 111 | `filesize` mediumint(9) NOT NULL, |
---|
| 112 | `filemtime` int(16) NOT NULL, |
---|
| 113 | PRIMARY KEY (`image_id`) |
---|
| 114 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
---|
| 115 | ;'; |
---|
| 116 | pwg_query($query); |
---|
| 117 | |
---|
| 118 | // add a "size" column to download_sets |
---|
| 119 | $result = pwg_query('SHOW COLUMNS FROM `' . $this->table_download_sets . '` LIKE "size";'); |
---|
| 120 | if (!pwg_db_num_rows($result)) |
---|
| 121 | { |
---|
| 122 | pwg_query('ALTER TABLE `' . $this->table_download_sets . '` ADD `size` varchar(16) NOT NULL DEFAULT "original";'); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | // add "ready" status |
---|
| 126 | pwg_query('ALTER TABLE `' . $this->table_download_sets . '` CHANGE `status` `status` enum("new","ready","download","done") NOT NULL DEFAULT "new";'); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | function update($old_version, $new_version, &$errors=array()) |
---|
| 130 | { |
---|
| 131 | $this->install($new_version, $errors); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | function uninstall() |
---|
| 135 | { |
---|
| 136 | global $conf; |
---|
| 137 | |
---|
| 138 | conf_delete_param('batch_download'); |
---|
| 139 | |
---|
| 140 | pwg_query('DROP TABLE `' . $this->table_download_sets . '`;'); |
---|
| 141 | pwg_query('DROP TABLE `' . $this->table_download_sets_images . '`;'); |
---|
| 142 | |
---|
| 143 | self::rrmdir(PHPWG_ROOT_PATH . $conf['data_location'] . 'download_archives/'); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | static function rrmdir($dir) |
---|
| 147 | { |
---|
| 148 | if (!is_dir($dir)) |
---|
| 149 | { |
---|
| 150 | return false; |
---|
| 151 | } |
---|
| 152 | $dir = rtrim($dir, '/'); |
---|
| 153 | $objects = scandir($dir); |
---|
| 154 | $return = true; |
---|
| 155 | |
---|
| 156 | foreach ($objects as $object) |
---|
| 157 | { |
---|
| 158 | if ($object !== '.' && $object !== '..') |
---|
| 159 | { |
---|
| 160 | $path = $dir.'/'.$object; |
---|
| 161 | if (filetype($path) == 'dir') |
---|
| 162 | { |
---|
| 163 | $return = $return && self::rrmdir($path); |
---|
| 164 | } |
---|
| 165 | else |
---|
| 166 | { |
---|
| 167 | $return = $return && @unlink($path); |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | return $return && @rmdir($dir); |
---|
| 173 | } |
---|
| 174 | } |
---|