| 1 | <?php |
|---|
| 2 | # ***** BEGIN LICENSE BLOCK ***** |
|---|
| 3 | # This file is part of daInstaller, a plugin for DotClear2. |
|---|
| 4 | # Copyright (c) 2008-2011 Tomtom, Pep and contributors, for DotAddict.org. |
|---|
| 5 | # All rights reserved. |
|---|
| 6 | # |
|---|
| 7 | # ***** END LICENSE BLOCK ***** |
|---|
| 8 | |
|---|
| 9 | class daInstaller |
|---|
| 10 | { |
|---|
| 11 | protected $core; |
|---|
| 12 | |
|---|
| 13 | # Set via plugin's settings |
|---|
| 14 | protected $themes_xml; |
|---|
| 15 | protected $plugins_xml; |
|---|
| 16 | |
|---|
| 17 | # Raw datas |
|---|
| 18 | protected $modules; |
|---|
| 19 | |
|---|
| 20 | /** |
|---|
| 21 | * "Look, Ma ! That's what we call a constructor." |
|---|
| 22 | * Nothing more to say about it for now. :-) |
|---|
| 23 | * |
|---|
| 24 | */ |
|---|
| 25 | public function __construct($core) |
|---|
| 26 | { |
|---|
| 27 | $this->core = $core; |
|---|
| 28 | |
|---|
| 29 | # Settings compatibility test |
|---|
| 30 | $s = !version_compare(DC_VERSION,'2.2-x','<') ? $core->blog->settings->dainstaller : $core->blog->settings; |
|---|
| 31 | $this->themes_xml = $s->dainstaller_themes_xml; |
|---|
| 32 | $this->plugins_xml = $s->dainstaller_plugins_xml; |
|---|
| 33 | $this->modules = array( |
|---|
| 34 | 'plugins' => array( |
|---|
| 35 | 'new' => array(), |
|---|
| 36 | 'update' => array() |
|---|
| 37 | ), |
|---|
| 38 | 'themes' => array( |
|---|
| 39 | 'new' => array(), |
|---|
| 40 | 'update' => array() |
|---|
| 41 | ) |
|---|
| 42 | ); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * Check new/updated plugins availability |
|---|
| 47 | * Plugins already installed but marked as disabled will be ignored. |
|---|
| 48 | * Results of this method are stored as two subarrays 'new' and 'update' in $modules['plugins']. |
|---|
| 49 | * |
|---|
| 50 | * @param boolean $force Forces datas refresh if true (default to false) |
|---|
| 51 | * |
|---|
| 52 | * @return boolean Reports operatio status : |
|---|
| 53 | * - true if operation was successful, |
|---|
| 54 | * - false otherwise, you may check internal error trace. |
|---|
| 55 | * |
|---|
| 56 | */ |
|---|
| 57 | protected function checkPlugins($force = false) |
|---|
| 58 | { |
|---|
| 59 | if (!$this->plugins_xml) { |
|---|
| 60 | return false; |
|---|
| 61 | } |
|---|
| 62 | if (($parser = daModulesReader::quickParse($this->plugins_xml,DC_TPL_CACHE,$force)) === false) { |
|---|
| 63 | return false; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | $raw_datas = $parser->getModules(); |
|---|
| 67 | |
|---|
| 68 | uasort($raw_datas,array('self','sort')); |
|---|
| 69 | |
|---|
| 70 | # On se débarasse des plugins désactivés. |
|---|
| 71 | $skipped = array_keys($this->core->plugins->getDisabledModules()); |
|---|
| 72 | foreach ($skipped as $p_id) { |
|---|
| 73 | if (isset($raw_datas[$p_id])) { |
|---|
| 74 | unset($raw_datas[$p_id]); |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | # On vérifie les mises à jour |
|---|
| 79 | $updates = array(); |
|---|
| 80 | $current = $this->core->plugins->getModules(); |
|---|
| 81 | foreach ($current as $p_id => $p_infos) { |
|---|
| 82 | if (isset($raw_datas[$p_id])) { |
|---|
| 83 | if (self::da_version_compare($raw_datas[$p_id]['version'],$p_infos['version'],'>')) { |
|---|
| 84 | $updates[$p_id] = $raw_datas[$p_id]; |
|---|
| 85 | $updates[$p_id]['root'] = $p_infos['root']; |
|---|
| 86 | $updates[$p_id]['root_writable'] = $p_infos['root_writable']; |
|---|
| 87 | $updates[$p_id]['current_version'] = $p_infos['version']; |
|---|
| 88 | } |
|---|
| 89 | unset($raw_datas[$p_id]); |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | $this->modules['plugins'] = array( |
|---|
| 94 | 'new' => $raw_datas, |
|---|
| 95 | 'update' => $updates |
|---|
| 96 | ); |
|---|
| 97 | |
|---|
| 98 | return true; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | /** |
|---|
| 102 | * Check new/updated themes availability |
|---|
| 103 | * Results of this method are stored as two subarrays 'new' and 'update' in $modules['themes']. |
|---|
| 104 | * |
|---|
| 105 | * @param boolean $force Forces datas refresh if true (default to false) |
|---|
| 106 | * |
|---|
| 107 | * @return boolean Reports operatio status : |
|---|
| 108 | * - true if operation was successful, |
|---|
| 109 | * - false otherwise, you may check internal error trace. |
|---|
| 110 | * |
|---|
| 111 | */ |
|---|
| 112 | protected function checkThemes($force = false) |
|---|
| 113 | { |
|---|
| 114 | if (!$this->themes_xml) { |
|---|
| 115 | return false; |
|---|
| 116 | } |
|---|
| 117 | if (($parser = daModulesReader::quickParse($this->themes_xml,DC_TPL_CACHE,$force)) === false) { |
|---|
| 118 | return false; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | $raw_datas = $parser->getModules(); |
|---|
| 122 | |
|---|
| 123 | uasort($raw_datas,array('self','sort')); |
|---|
| 124 | |
|---|
| 125 | # On vérifie les mises à jour |
|---|
| 126 | $updates = array(); |
|---|
| 127 | $core_themes = new dcModules($this->core); |
|---|
| 128 | $core_themes->loadModules($this->core->blog->themes_path,null); |
|---|
| 129 | $current = $core_themes->getModules(); |
|---|
| 130 | foreach ($current as $p_id => $p_infos) { |
|---|
| 131 | if (isset($raw_datas[$p_id])) { |
|---|
| 132 | if (self::da_version_compare($raw_datas[$p_id]['version'],$p_infos['version'],'>')) { |
|---|
| 133 | $updates[$p_id] = $raw_datas[$p_id]; |
|---|
| 134 | $updates[$p_id]['root'] = $p_infos['root']; |
|---|
| 135 | $updates[$p_id]['root_writable'] = $p_infos['root_writable']; |
|---|
| 136 | $updates[$p_id]['current_version'] = $p_infos['version']; |
|---|
| 137 | } |
|---|
| 138 | unset($raw_datas[$p_id]); |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | $this->modules['themes'] = array( |
|---|
| 143 | 'new' => $raw_datas, |
|---|
| 144 | 'update' => $updates |
|---|
| 145 | ); |
|---|
| 146 | |
|---|
| 147 | return true; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | /** |
|---|
| 151 | * Get informations about available new//updated themes/plugins. |
|---|
| 152 | * Probably the first method to invoke after instanciation. |
|---|
| 153 | * |
|---|
| 154 | * @param boolean $force Forces datas refresh if true (default to false) |
|---|
| 155 | * @param boolean $verbose Display exceptions when exists (default to false) |
|---|
| 156 | * |
|---|
| 157 | * @return boolean Returns if the check is completed or not |
|---|
| 158 | * |
|---|
| 159 | */ |
|---|
| 160 | public function check($force = false, $verbose = false) |
|---|
| 161 | { |
|---|
| 162 | $ret = true; |
|---|
| 163 | |
|---|
| 164 | try { |
|---|
| 165 | if (!$this->checkPlugins($force)) { |
|---|
| 166 | $ret = false; |
|---|
| 167 | } |
|---|
| 168 | if (!$this->checkThemes($force)) { |
|---|
| 169 | $ret = false; |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | catch (daModuleParserException $e) { |
|---|
| 173 | if ($verbose) { |
|---|
| 174 | $this->core->error->add(sprintf( |
|---|
| 175 | __('Oops, something went wrong with %s, we are working on it. %s'), |
|---|
| 176 | sprintf('<a href="%1$s">%2$s</a>','http://dotaddict.org',__('dotaddict.org website')), |
|---|
| 177 | sprintf('<a href="%1$s">%2$s</a>','http://dotclear.org/donate',__('Wanna help too?')) |
|---|
| 178 | )); |
|---|
| 179 | } |
|---|
| 180 | $ret = false; |
|---|
| 181 | } |
|---|
| 182 | catch (Exception $e) { |
|---|
| 183 | # Probablement à compléter. |
|---|
| 184 | $ret = false; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | return $ret; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | /** |
|---|
| 191 | * Retrieve a specific module list |
|---|
| 192 | * |
|---|
| 193 | * @param string $type The type of modules wanted ('plugins' or 'themes') |
|---|
| 194 | * @param boolean $update Flag to choose between new or updated modules (default to false - new modules list) |
|---|
| 195 | * |
|---|
| 196 | * @return mixed The matching modules entries as an array, if any. Or a boolean set to false. |
|---|
| 197 | * |
|---|
| 198 | */ |
|---|
| 199 | public function getModules($type, $update = false) |
|---|
| 200 | { |
|---|
| 201 | $type = ($type == 'themes') ? 'themes' : 'plugins'; |
|---|
| 202 | $what = ($update) ? 'update' : 'new'; |
|---|
| 203 | if (isset($this->modules[$type][$what])) { |
|---|
| 204 | return $this->modules[$type][$what]; |
|---|
| 205 | } |
|---|
| 206 | return false; |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | /** |
|---|
| 210 | * Search a string in module id, label and description. |
|---|
| 211 | * Search is case-insensitive and can be apply to available themes or plugins |
|---|
| 212 | * |
|---|
| 213 | * @param string $search The search string |
|---|
| 214 | * @param string $type The type of targeted modules ('plugins' or 'themes') |
|---|
| 215 | * |
|---|
| 216 | * @return array An array of matching modules entries |
|---|
| 217 | * |
|---|
| 218 | */ |
|---|
| 219 | public function search($search,$type = 'plugins') |
|---|
| 220 | { |
|---|
| 221 | $type = ($type == 'themes') ? 'themes' : 'plugins'; |
|---|
| 222 | $result = array(); |
|---|
| 223 | |
|---|
| 224 | foreach ($this->modules[$type]['new'] as $module) |
|---|
| 225 | { |
|---|
| 226 | if ( preg_match('/'.$search.'/i',$module['id']) || |
|---|
| 227 | preg_match('/'.$search.'/i',$module['label']) || |
|---|
| 228 | preg_match('/'.$search.'/i',$module['desc'])) |
|---|
| 229 | { |
|---|
| 230 | $result[] = $module; |
|---|
| 231 | } |
|---|
| 232 | } |
|---|
| 233 | return $result; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | /** |
|---|
| 237 | * Helper method to fetch and install a ZIP package. |
|---|
| 238 | * |
|---|
| 239 | * @param string $url Source file URL |
|---|
| 240 | * @param string $dest Target file destination |
|---|
| 241 | * @param dcModules $coreModules Target modules stack |
|---|
| 242 | * |
|---|
| 243 | * @return integer Basic operation status code : |
|---|
| 244 | * - 1 : everything's all right, |
|---|
| 245 | * - 2 : tempfile couldn't be deleted. |
|---|
| 246 | * |
|---|
| 247 | */ |
|---|
| 248 | public function processPackage($url,$dest,dcModules $coreModules) |
|---|
| 249 | { |
|---|
| 250 | try { |
|---|
| 251 | $client = netHttp::initClient($url,$path); |
|---|
| 252 | $client->setUserAgent(self::getUserAgent()); |
|---|
| 253 | $client->useGzip(false); |
|---|
| 254 | $client->setPersistReferers(false); |
|---|
| 255 | $client->setOutput($dest); |
|---|
| 256 | $client->get($path); |
|---|
| 257 | } |
|---|
| 258 | catch (Exception $e) { |
|---|
| 259 | throw new Exception(__('An error occurred while downloading the file.')); |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | unset($client); |
|---|
| 263 | $ret_code = dcModules::installPackage($dest,$coreModules); |
|---|
| 264 | |
|---|
| 265 | return $ret_code; |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | /** |
|---|
| 269 | * Helper method to get user agent according to DC and daInstaller version |
|---|
| 270 | * |
|---|
| 271 | * @return string daInstaller user agent |
|---|
| 272 | * |
|---|
| 273 | */ |
|---|
| 274 | public static function getUserAgent() |
|---|
| 275 | { |
|---|
| 276 | $m_version = $GLOBALS['core']->plugins->moduleInfo('daInstaller','version'); |
|---|
| 277 | return sprintf('daInstaller/%s (Dotclear/%s)',$m_version,DC_VERSION); |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | /** |
|---|
| 281 | * Helper method to compare correctly version. |
|---|
| 282 | * |
|---|
| 283 | * @param string $v1 Version of first module |
|---|
| 284 | * @param string $v2 Version of second module |
|---|
| 285 | * @param string $op Operator |
|---|
| 286 | * |
|---|
| 287 | * @return boolean True if test of version is correct according to operator |
|---|
| 288 | * |
|---|
| 289 | */ |
|---|
| 290 | private static function da_version_compare($v1,$v2,$op) |
|---|
| 291 | { |
|---|
| 292 | $v1 = preg_replace('!-r(\d+)$!','-p$1',$v1); |
|---|
| 293 | $v2 = preg_replace('!-r(\d+)$!','-p$1',$v2); |
|---|
| 294 | return version_compare($v1,$v2,$op); |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | /** |
|---|
| 298 | * Helper method to sort module list. |
|---|
| 299 | * |
|---|
| 300 | * @param array $a First module |
|---|
| 301 | * @param array $b Second module |
|---|
| 302 | * |
|---|
| 303 | * @return integer |
|---|
| 304 | * |
|---|
| 305 | */ |
|---|
| 306 | private static function sort($a,$b) |
|---|
| 307 | { |
|---|
| 308 | $c = strtolower($a['id']); |
|---|
| 309 | $d = strtolower($b['id']); |
|---|
| 310 | if ($c == $d) { |
|---|
| 311 | return 0; |
|---|
| 312 | } |
|---|
| 313 | return ($c < $d) ? -1 : 1; |
|---|
| 314 | } |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | ?> |
|---|