[0] | 1 | <?php |
---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
| 3 | # |
---|
| 4 | # This file is part of Dotclear 2. |
---|
| 5 | # |
---|
[1179] | 6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
---|
[0] | 7 | # Licensed under the GPL version 2.0 license. |
---|
| 8 | # See LICENSE file or |
---|
| 9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
| 10 | # |
---|
| 11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
| 12 | if (!defined('DC_RC_PATH')) { return; } |
---|
| 13 | |
---|
| 14 | /** |
---|
| 15 | @ingroup DC_CORE |
---|
| 16 | @brief Modules handler |
---|
| 17 | |
---|
[2566] | 18 | Provides an object to handle modules (themes or plugins). |
---|
[0] | 19 | */ |
---|
| 20 | class dcModules |
---|
| 21 | { |
---|
| 22 | protected $path; |
---|
| 23 | protected $ns; |
---|
| 24 | protected $modules = array(); |
---|
| 25 | protected $disabled = array(); |
---|
| 26 | protected $errors = array(); |
---|
| 27 | protected $modules_names = array(); |
---|
[2997] | 28 | protected $all_modules = array(); |
---|
[2954] | 29 | protected $disabled_mode = false; |
---|
| 30 | protected $disabled_meta = array(); |
---|
[3066] | 31 | protected $to_disable = array(); |
---|
[2566] | 32 | |
---|
[0] | 33 | protected $id; |
---|
| 34 | protected $mroot; |
---|
[2566] | 35 | |
---|
[0] | 36 | # Inclusion variables |
---|
| 37 | protected static $superglobals = array('GLOBALS','_SERVER','_GET','_POST','_COOKIE','_FILES','_ENV','_REQUEST','_SESSION'); |
---|
| 38 | protected static $_k; |
---|
| 39 | protected static $_n; |
---|
[2239] | 40 | |
---|
[2492] | 41 | protected static $type = null; |
---|
[2566] | 42 | |
---|
[0] | 43 | public $core; ///< <b>dcCore</b> dcCore instance |
---|
[2566] | 44 | |
---|
[0] | 45 | /** |
---|
| 46 | Object constructor. |
---|
[2566] | 47 | |
---|
[0] | 48 | @param core <b>dcCore</b> dcCore instance |
---|
| 49 | */ |
---|
| 50 | public function __construct($core) |
---|
| 51 | { |
---|
| 52 | $this->core =& $core; |
---|
| 53 | } |
---|
[2566] | 54 | |
---|
[3007] | 55 | /** |
---|
| 56 | * Checks all modules dependencies |
---|
| 57 | * Fills in the following information in module : |
---|
| 58 | * * cannot_enable : list reasons why module cannot be enabled. Not set if module can be enabled |
---|
| 59 | * * cannot_disable : list reasons why module cannot be disabled. Not set if module can be disabled |
---|
| 60 | * * implies : reverse dependencies |
---|
| 61 | * @return array list of enabled modules with unmet dependencies, and that must be disabled. |
---|
| 62 | */ |
---|
[3067] | 63 | public function checkDependencies() |
---|
| 64 | { |
---|
[3135] | 65 | $dc_version = preg_replace('/\-dev$/','',DC_VERSION); |
---|
[3066] | 66 | $this->to_disable = array(); |
---|
[2997] | 67 | foreach ($this->all_modules as $k => &$m) { |
---|
| 68 | if (isset($m['requires'])) { |
---|
[3007] | 69 | $missing = array(); |
---|
[2997] | 70 | foreach ($m['requires'] as &$dep) { |
---|
| 71 | if (!is_array($dep)) { |
---|
| 72 | $dep = array($dep); |
---|
| 73 | } |
---|
[3007] | 74 | // grab missing dependencies |
---|
[3135] | 75 | if (!isset($this->all_modules[$dep[0]]) && ($dep[0] != 'core')) { |
---|
[2997] | 76 | // module not present |
---|
[3135] | 77 | $missing[$dep[0]] = sprintf(__("Requires %s module which is not installed"), $dep[0]); |
---|
| 78 | } elseif ((count($dep) > 1) && |
---|
| 79 | version_compare(($dep[0] == 'core' ? $dc_version : $this->all_modules[$dep[0]]['version']),$dep[1]) == -1) |
---|
| 80 | { |
---|
[2997] | 81 | // module present, but version missing |
---|
[3135] | 82 | if ($dep[0] == 'core') { |
---|
| 83 | $missing[$dep[0]] = sprintf(__("Requires Dotclear version %s, but version %s is installed"), |
---|
| 84 | $dep[1],$dc_version); |
---|
| 85 | } else { |
---|
| 86 | $missing[$dep[0]] = sprintf(__("Requires %s module version %s, but version %s is installed"), |
---|
| 87 | $dep[0],$dep[1],$this->all_modules[$dep[0]]['version']); |
---|
| 88 | } |
---|
| 89 | } elseif (($dep[0] != 'core') && !$this->all_modules[$dep[0]]['enabled']) { |
---|
[3007] | 90 | // module disabled |
---|
[3135] | 91 | $missing[$dep[0]] = sprintf(__("Requires %s module which is disabled"), $dep[0]); |
---|
[2997] | 92 | } |
---|
[3007] | 93 | $this->all_modules[$dep[0]]['implies'][]=$k; |
---|
| 94 | } |
---|
| 95 | if (count($missing)) { |
---|
| 96 | $m['cannot_enable']=$missing; |
---|
| 97 | if ($m['enabled']) { |
---|
[3066] | 98 | $this->to_disable[]=array('name' => $k,'reason'=> $missing); |
---|
[2997] | 99 | } |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | } |
---|
[3007] | 103 | // Check modules that cannot be disabled |
---|
| 104 | foreach ($this->modules as $k => &$m) { |
---|
| 105 | if (isset($m['implies']) && $m['enabled']) { |
---|
| 106 | foreach ($m['implies'] as $im) { |
---|
| 107 | if (isset($this->all_modules[$im]) && $this->all_modules[$im]['enabled']) { |
---|
| 108 | $m['cannot_disable'][]=$im; |
---|
| 109 | } |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | } |
---|
[3066] | 113 | } |
---|
| 114 | |
---|
| 115 | /** |
---|
| 116 | * Checks all modules dependencies, and disable unmet dependencies |
---|
| 117 | * @param string $redir_url URL to redirect if modules are to disable |
---|
[3067] | 118 | * @return boolean, true if a redirection has been performed |
---|
[3066] | 119 | */ |
---|
[3067] | 120 | public function disableDepModules($redir_url) |
---|
| 121 | { |
---|
[3066] | 122 | if (isset($_GET['dep'])) { |
---|
| 123 | // Avoid infinite redirects |
---|
| 124 | return false; |
---|
| 125 | } |
---|
| 126 | $reason = array(); |
---|
| 127 | foreach ($this->to_disable as $module) { |
---|
[3067] | 128 | try{ |
---|
| 129 | $this->deactivateModule($module['name']); |
---|
| 130 | $reason[] = sprintf("<li>%s : %s</li>",$module['name'],join(',',$module['reason'])); |
---|
| 131 | } catch (Exception $e) { |
---|
| 132 | } |
---|
[3066] | 133 | } |
---|
| 134 | if (count($reason)) { |
---|
| 135 | $message = sprintf ("<p>%s</p><ul>%s</ul>", |
---|
[3067] | 136 | __('The following extensions have been disabled :'), |
---|
[3066] | 137 | join('',$reason) |
---|
| 138 | ); |
---|
[3067] | 139 | dcPage::addWarningNotice($message,array('divtag' => true,'with_ts' => false)); |
---|
| 140 | $url = $redir_url.(strpos($redir_url,"?") ? '&' : '?').'dep=1'; |
---|
[3066] | 141 | http::redirect($url); |
---|
| 142 | return true; |
---|
| 143 | } |
---|
| 144 | return false; |
---|
[2997] | 145 | } |
---|
| 146 | |
---|
[0] | 147 | /** |
---|
| 148 | Loads modules. <var>$path</var> could be a separated list of paths |
---|
| 149 | (path separator depends on your OS). |
---|
[2566] | 150 | |
---|
[0] | 151 | <var>$ns</var> indicates if an additionnal file needs to be loaded on plugin |
---|
| 152 | load, value could be: |
---|
| 153 | - admin (loads module's _admin.php) |
---|
| 154 | - public (loads module's _public.php) |
---|
| 155 | - xmlrpc (loads module's _xmlrpc.php) |
---|
[2566] | 156 | |
---|
[0] | 157 | <var>$lang</var> indicates if we need to load a lang file on plugin |
---|
| 158 | loading. |
---|
| 159 | */ |
---|
| 160 | public function loadModules($path,$ns=null,$lang=null) |
---|
| 161 | { |
---|
| 162 | $this->path = explode(PATH_SEPARATOR,$path); |
---|
| 163 | $this->ns = $ns; |
---|
[2566] | 164 | |
---|
[43] | 165 | $disabled = isset($_SESSION['sess_safe_mode']) && $_SESSION['sess_safe_mode']; |
---|
| 166 | $disabled = $disabled && !get_parent_class($this) ? true : false; |
---|
[2566] | 167 | |
---|
[2945] | 168 | $ignored = array(); |
---|
| 169 | |
---|
[0] | 170 | foreach ($this->path as $root) |
---|
| 171 | { |
---|
| 172 | if (!is_dir($root) || !is_readable($root)) { |
---|
| 173 | continue; |
---|
| 174 | } |
---|
[2566] | 175 | |
---|
[0] | 176 | if (substr($root,-1) != '/') { |
---|
| 177 | $root .= '/'; |
---|
| 178 | } |
---|
[2566] | 179 | |
---|
[0] | 180 | if (($d = @dir($root)) === false) { |
---|
| 181 | continue; |
---|
| 182 | } |
---|
[2566] | 183 | |
---|
[0] | 184 | while (($entry = $d->read()) !== false) |
---|
| 185 | { |
---|
[1196] | 186 | $full_entry = $root.$entry; |
---|
[2566] | 187 | |
---|
[0] | 188 | if ($entry != '.' && $entry != '..' && is_dir($full_entry) |
---|
| 189 | && file_exists($full_entry.'/_define.php')) |
---|
| 190 | { |
---|
[43] | 191 | if (!file_exists($full_entry.'/_disabled') && !$disabled) |
---|
[0] | 192 | { |
---|
| 193 | $this->id = $entry; |
---|
| 194 | $this->mroot = $full_entry; |
---|
[3395] | 195 | ob_start(); |
---|
[0] | 196 | require $full_entry.'/_define.php'; |
---|
[3395] | 197 | ob_end_clean(); |
---|
[2997] | 198 | $this->all_modules[$entry] =& $this->modules[$entry]; |
---|
[0] | 199 | $this->id = null; |
---|
| 200 | $this->mroot = null; |
---|
| 201 | } |
---|
| 202 | else |
---|
| 203 | { |
---|
[2954] | 204 | if (file_exists($full_entry.'/_define.php')) { |
---|
[2997] | 205 | $this->id = $entry; |
---|
| 206 | $this->mroot = $full_entry; |
---|
[3395] | 207 | $this->disabled_mode = true; |
---|
| 208 | ob_start(); |
---|
[2954] | 209 | require $full_entry.'/_define.php'; |
---|
[3395] | 210 | ob_end_clean(); |
---|
| 211 | $this->disabled_mode = false; |
---|
[2954] | 212 | $this->disabled[$entry] = $this->disabled_meta; |
---|
[2997] | 213 | $this->all_modules[$entry] =& $this->disabled[$entry]; |
---|
| 214 | $this->id = null; |
---|
| 215 | $this->mroot = null; |
---|
[2954] | 216 | } |
---|
[0] | 217 | } |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | $d->close(); |
---|
| 221 | } |
---|
[2997] | 222 | $this->checkDependencies(); |
---|
[0] | 223 | # Sort plugins |
---|
| 224 | uasort($this->modules,array($this,'sortModules')); |
---|
[2566] | 225 | |
---|
[0] | 226 | foreach ($this->modules as $id => $m) |
---|
| 227 | { |
---|
[2918] | 228 | # Load translation and _prepend |
---|
[0] | 229 | if (file_exists($m['root'].'/_prepend.php')) |
---|
| 230 | { |
---|
[2095] | 231 | $r = $this->loadModuleFile($m['root'].'/_prepend.php'); |
---|
[2566] | 232 | |
---|
[0] | 233 | # If _prepend.php file returns null (ie. it has a void return statement) |
---|
| 234 | if (is_null($r)) { |
---|
[2945] | 235 | $ignored[] = $id; |
---|
[0] | 236 | continue; |
---|
| 237 | } |
---|
| 238 | unset($r); |
---|
| 239 | } |
---|
[2566] | 240 | |
---|
[0] | 241 | $this->loadModuleL10N($id,$lang,'main'); |
---|
| 242 | if ($ns == 'admin') { |
---|
| 243 | $this->loadModuleL10Nresources($id,$lang); |
---|
[2708] | 244 | $this->core->adminurl->register('admin.plugin.'.$id,'plugin.php',array('p'=>$id)); |
---|
[0] | 245 | } |
---|
[2918] | 246 | } |
---|
[3108] | 247 | |
---|
| 248 | // Give opportunity to do something before loading context (admin,public,xmlrpc) files |
---|
| 249 | $this->core->callBehavior('coreBeforeLoadingNsFiles',$this->core,$this,$lang); |
---|
| 250 | |
---|
[2918] | 251 | foreach ($this->modules as $id => $m) |
---|
| 252 | { |
---|
[2945] | 253 | # If _prepend.php file returns null (ie. it has a void return statement) |
---|
| 254 | if (in_array($id,$ignored)) { |
---|
| 255 | continue; |
---|
| 256 | } |
---|
[2918] | 257 | # Load ns_file |
---|
[0] | 258 | $this->loadNsFile($id,$ns); |
---|
| 259 | } |
---|
| 260 | } |
---|
[2566] | 261 | |
---|
[0] | 262 | public function requireDefine($dir,$id) |
---|
| 263 | { |
---|
| 264 | if (file_exists($dir.'/_define.php')) { |
---|
| 265 | $this->id = $id; |
---|
[3395] | 266 | ob_start(); |
---|
[0] | 267 | require $dir.'/_define.php'; |
---|
[3395] | 268 | ob_end_clean(); |
---|
[0] | 269 | $this->id = null; |
---|
| 270 | } |
---|
[2566] | 271 | } |
---|
| 272 | |
---|
[0] | 273 | /** |
---|
| 274 | This method registers a module in modules list. You should use this to |
---|
| 275 | register a new module. |
---|
[2566] | 276 | |
---|
[0] | 277 | <var>$permissions</var> is a comma separated list of permissions for your |
---|
| 278 | module. If <var>$permissions</var> is null, only super admin has access to |
---|
| 279 | this module. |
---|
[2566] | 280 | |
---|
[0] | 281 | <var>$priority</var> is an integer. Modules are sorted by priority and name. |
---|
| 282 | Lowest priority comes first. |
---|
[2566] | 283 | |
---|
[0] | 284 | @param name <b>string</b> Module name |
---|
| 285 | @param desc <b>string</b> Module description |
---|
| 286 | @param author <b>string</b> Module author name |
---|
| 287 | @param version <b>string</b> Module version |
---|
[2566] | 288 | @param properties <b>array</b> extra properties |
---|
[2492] | 289 | (currently available keys : permissions, priority, type) |
---|
[0] | 290 | */ |
---|
[3333] | 291 | public function registerModule($name,$desc,$author,$version,$properties = array()) |
---|
[0] | 292 | { |
---|
[2954] | 293 | if ($this->disabled_mode) { |
---|
| 294 | $this->disabled_meta = array_merge( |
---|
| 295 | $properties, |
---|
| 296 | array( |
---|
| 297 | 'root' => $this->mroot, |
---|
| 298 | 'name' => $name, |
---|
| 299 | 'desc' => $desc, |
---|
| 300 | 'author' => $author, |
---|
| 301 | 'version' => $version, |
---|
[2997] | 302 | 'enabled' => false, |
---|
[2954] | 303 | 'root_writable' => is_writable($this->mroot) |
---|
| 304 | ) |
---|
| 305 | ); |
---|
| 306 | return; |
---|
| 307 | } |
---|
[2492] | 308 | # Fallback to legacy registerModule parameters |
---|
[464] | 309 | if (!is_array($properties)) { |
---|
| 310 | $args = func_get_args(); |
---|
| 311 | $properties = array(); |
---|
| 312 | if (isset($args[4])) { |
---|
| 313 | $properties['permissions']=$args[4]; |
---|
| 314 | } |
---|
| 315 | if (isset($args[5])) { |
---|
| 316 | $properties['priority']= (integer)$args[5]; |
---|
| 317 | } |
---|
| 318 | } |
---|
[2492] | 319 | |
---|
| 320 | # Default module properties |
---|
[464] | 321 | $properties = array_merge( |
---|
| 322 | array( |
---|
| 323 | 'permissions' => null, |
---|
[2239] | 324 | 'priority' => 1000, |
---|
| 325 | 'standalone_config' => false, |
---|
[2954] | 326 | 'type' => null, |
---|
[2997] | 327 | 'enabled' => true, |
---|
[3333] | 328 | 'requires' => array(), |
---|
| 329 | 'settings' => array() |
---|
[464] | 330 | ), $properties |
---|
| 331 | ); |
---|
[2239] | 332 | |
---|
[2492] | 333 | # Check module type |
---|
| 334 | if (self::$type !== null && $properties['type'] !== null && $properties['type'] != self::$type) { |
---|
[2239] | 335 | $this->errors[] = sprintf( |
---|
| 336 | __('Module "%s" has type "%s" that mismatch required module type "%s".'), |
---|
| 337 | '<strong>'.html::escapeHTML($name).'</strong>', |
---|
| 338 | '<em>'.html::escapeHTML($properties['type']).'</em>', |
---|
| 339 | '<em>'.html::escapeHTML(self::$type).'</em>' |
---|
| 340 | ); |
---|
| 341 | return; |
---|
| 342 | } |
---|
| 343 | |
---|
[2492] | 344 | # Check module perms on admin side |
---|
[464] | 345 | $permissions = $properties['permissions']; |
---|
[0] | 346 | if ($this->ns == 'admin') { |
---|
| 347 | if ($permissions == '' && !$this->core->auth->isSuperAdmin()) { |
---|
| 348 | return; |
---|
| 349 | } elseif (!$this->core->auth->check($permissions,$this->core->blog->id)) { |
---|
| 350 | return; |
---|
| 351 | } |
---|
| 352 | } |
---|
[2566] | 353 | |
---|
[2492] | 354 | # Check module install on multiple path |
---|
[0] | 355 | if ($this->id) { |
---|
| 356 | $module_exists = array_key_exists($name,$this->modules_names); |
---|
| 357 | $module_overwrite = $module_exists ? version_compare($this->modules_names[$name],$version,'<') : false; |
---|
| 358 | if (!$module_exists || ($module_exists && $module_overwrite)) { |
---|
| 359 | $this->modules_names[$name] = $version; |
---|
[464] | 360 | $this->modules[$this->id] = array_merge( |
---|
| 361 | $properties, |
---|
| 362 | array( |
---|
| 363 | 'root' => $this->mroot, |
---|
| 364 | 'name' => $name, |
---|
| 365 | 'desc' => $desc, |
---|
| 366 | 'author' => $author, |
---|
| 367 | 'version' => $version, |
---|
| 368 | 'root_writable' => is_writable($this->mroot) |
---|
| 369 | ) |
---|
[0] | 370 | ); |
---|
| 371 | } |
---|
| 372 | else { |
---|
| 373 | $path1 = path::real($this->moduleInfo($name,'root')); |
---|
| 374 | $path2 = path::real($this->mroot); |
---|
| 375 | $this->errors[] = sprintf( |
---|
[2239] | 376 | __('Module "%s" is installed twice in "%s" and "%s".'), |
---|
[0] | 377 | '<strong>'.$name.'</strong>', |
---|
| 378 | '<em>'.$path1.'</em>', |
---|
| 379 | '<em>'.$path2.'</em>' |
---|
| 380 | ); |
---|
| 381 | } |
---|
| 382 | } |
---|
| 383 | } |
---|
[2566] | 384 | |
---|
[0] | 385 | public function resetModulesList() |
---|
| 386 | { |
---|
| 387 | $this->modules = array(); |
---|
| 388 | $this->modules_names = array(); |
---|
[2239] | 389 | $this->errors = array(); |
---|
[2566] | 390 | } |
---|
| 391 | |
---|
[0] | 392 | public static function installPackage($zip_file,dcModules &$modules) |
---|
| 393 | { |
---|
| 394 | $zip = new fileUnzip($zip_file); |
---|
[2245] | 395 | $zip->getList(false,'#(^|/)(__MACOSX|\.svn|\.hg|\.git|\.DS_Store|\.directory|Thumbs\.db)(/|$)#'); |
---|
[2566] | 396 | |
---|
[0] | 397 | $zip_root_dir = $zip->getRootDir(); |
---|
| 398 | $define = ''; |
---|
| 399 | if ($zip_root_dir != false) { |
---|
| 400 | $target = dirname($zip_file); |
---|
| 401 | $destination = $target.'/'.$zip_root_dir; |
---|
| 402 | $define = $zip_root_dir.'/_define.php'; |
---|
| 403 | $has_define = $zip->hasFile($define); |
---|
| 404 | } else { |
---|
| 405 | $target = dirname($zip_file).'/'.preg_replace('/\.([^.]+)$/','',basename($zip_file)); |
---|
| 406 | $destination = $target; |
---|
| 407 | $define = '_define.php'; |
---|
| 408 | $has_define = $zip->hasFile($define); |
---|
| 409 | } |
---|
[2566] | 410 | |
---|
[0] | 411 | if ($zip->isEmpty()) { |
---|
| 412 | $zip->close(); |
---|
| 413 | unlink($zip_file); |
---|
| 414 | throw new Exception(__('Empty module zip file.')); |
---|
| 415 | } |
---|
[2566] | 416 | |
---|
[0] | 417 | if (!$has_define) { |
---|
| 418 | $zip->close(); |
---|
| 419 | unlink($zip_file); |
---|
| 420 | throw new Exception(__('The zip file does not appear to be a valid Dotclear module.')); |
---|
| 421 | } |
---|
[2566] | 422 | |
---|
[0] | 423 | $ret_code = 1; |
---|
[2566] | 424 | |
---|
[2239] | 425 | if (!is_dir($destination)) |
---|
| 426 | { |
---|
| 427 | try { |
---|
| 428 | files::makeDir($destination,true); |
---|
[2566] | 429 | |
---|
[2239] | 430 | $sandbox = clone $modules; |
---|
| 431 | $zip->unzip($define, $target.'/_define.php'); |
---|
[2566] | 432 | |
---|
[2239] | 433 | $sandbox->resetModulesList(); |
---|
| 434 | $sandbox->requireDefine($target,basename($destination)); |
---|
| 435 | unlink($target.'/_define.php'); |
---|
[2566] | 436 | |
---|
[2239] | 437 | $new_errors = $sandbox->getErrors(); |
---|
| 438 | if (!empty($new_errors)) { |
---|
| 439 | $new_errors = is_array($new_errors) ? implode(" \n",$new_errors) : $new_errors; |
---|
| 440 | throw new Exception($new_errors); |
---|
| 441 | } |
---|
[2566] | 442 | |
---|
[2239] | 443 | files::deltree($destination); |
---|
| 444 | } |
---|
| 445 | catch(Exception $e) |
---|
| 446 | { |
---|
| 447 | $zip->close(); |
---|
| 448 | unlink($zip_file); |
---|
| 449 | files::deltree($destination); |
---|
[2566] | 450 | throw new Exception($e->getMessage()); |
---|
[2239] | 451 | } |
---|
| 452 | } |
---|
| 453 | else |
---|
[0] | 454 | { |
---|
| 455 | # test for update |
---|
| 456 | $sandbox = clone $modules; |
---|
| 457 | $zip->unzip($define, $target.'/_define.php'); |
---|
[2566] | 458 | |
---|
[0] | 459 | $sandbox->resetModulesList(); |
---|
| 460 | $sandbox->requireDefine($target,basename($destination)); |
---|
| 461 | unlink($target.'/_define.php'); |
---|
| 462 | $new_modules = $sandbox->getModules(); |
---|
[2566] | 463 | |
---|
[0] | 464 | if (!empty($new_modules)) |
---|
| 465 | { |
---|
| 466 | $tmp = array_keys($new_modules); |
---|
| 467 | $id = $tmp[0]; |
---|
| 468 | $cur_module = $modules->getModules($id); |
---|
[2432] | 469 | if (!empty($cur_module) && (defined('DC_DEV') && DC_DEV === true || dcUtils::versionsCompare($new_modules[$id]['version'], $cur_module['version'], '>', true))) |
---|
[0] | 470 | { |
---|
| 471 | # delete old module |
---|
| 472 | if (!files::deltree($destination)) { |
---|
| 473 | throw new Exception(__('An error occurred during module deletion.')); |
---|
| 474 | } |
---|
| 475 | $ret_code = 2; |
---|
| 476 | } |
---|
| 477 | else |
---|
| 478 | { |
---|
| 479 | $zip->close(); |
---|
| 480 | unlink($zip_file); |
---|
[2566] | 481 | throw new Exception(sprintf(__('Unable to upgrade "%s". (older or same version)'),basename($destination))); |
---|
[0] | 482 | } |
---|
| 483 | } |
---|
| 484 | else |
---|
| 485 | { |
---|
| 486 | $zip->close(); |
---|
| 487 | unlink($zip_file); |
---|
[2566] | 488 | throw new Exception(sprintf(__('Unable to read new _define.php file'))); |
---|
[0] | 489 | } |
---|
| 490 | } |
---|
| 491 | $zip->unzipAll($target); |
---|
| 492 | $zip->close(); |
---|
| 493 | unlink($zip_file); |
---|
| 494 | return $ret_code; |
---|
| 495 | } |
---|
[2566] | 496 | |
---|
[0] | 497 | /** |
---|
| 498 | This method installs all modules having a _install file. |
---|
[2566] | 499 | |
---|
[0] | 500 | @see dcModules::installModule |
---|
| 501 | */ |
---|
| 502 | public function installModules() |
---|
| 503 | { |
---|
| 504 | $res = array('success'=>array(),'failure'=>array()); |
---|
| 505 | foreach ($this->modules as $id => &$m) |
---|
| 506 | { |
---|
| 507 | $i = $this->installModule($id,$msg); |
---|
| 508 | if ($i === true) { |
---|
| 509 | $res['success'][$id] = true; |
---|
| 510 | } elseif ($i === false) { |
---|
| 511 | $res['failure'][$id] = $msg; |
---|
| 512 | } |
---|
| 513 | } |
---|
[2566] | 514 | |
---|
[0] | 515 | return $res; |
---|
| 516 | } |
---|
[2566] | 517 | |
---|
[0] | 518 | /** |
---|
| 519 | This method installs module with ID <var>$id</var> and having a _install |
---|
| 520 | file. This file should throw exception on failure or true if it installs |
---|
| 521 | successfully. |
---|
[2566] | 522 | |
---|
[0] | 523 | <var>$msg</var> is an out parameter that handle installer message. |
---|
[2566] | 524 | |
---|
[0] | 525 | @param id <b>string</b> Module ID |
---|
| 526 | @param msg <b>string</b> Module installer message |
---|
| 527 | @return <b>boolean</b> |
---|
| 528 | */ |
---|
| 529 | public function installModule($id,&$msg) |
---|
| 530 | { |
---|
| 531 | try { |
---|
| 532 | $i = $this->loadModuleFile($this->modules[$id]['root'].'/_install.php'); |
---|
| 533 | if ($i === true) { |
---|
| 534 | return true; |
---|
| 535 | } |
---|
| 536 | } catch (Exception $e) { |
---|
| 537 | $msg = $e->getMessage(); |
---|
| 538 | return false; |
---|
| 539 | } |
---|
[2566] | 540 | |
---|
[0] | 541 | return null; |
---|
| 542 | } |
---|
[2566] | 543 | |
---|
[0] | 544 | public function deleteModule($id,$disabled=false) |
---|
| 545 | { |
---|
| 546 | if ($disabled) { |
---|
| 547 | $p =& $this->disabled; |
---|
| 548 | } else { |
---|
| 549 | $p =& $this->modules; |
---|
| 550 | } |
---|
[2566] | 551 | |
---|
[0] | 552 | if (!isset($p[$id])) { |
---|
| 553 | throw new Exception(__('No such module.')); |
---|
| 554 | } |
---|
[2566] | 555 | |
---|
[0] | 556 | if (!files::deltree($p[$id]['root'])) { |
---|
| 557 | throw new Exception(__('Cannot remove module files')); |
---|
| 558 | } |
---|
| 559 | } |
---|
[2566] | 560 | |
---|
[0] | 561 | public function deactivateModule($id) |
---|
| 562 | { |
---|
| 563 | if (!isset($this->modules[$id])) { |
---|
| 564 | throw new Exception(__('No such module.')); |
---|
| 565 | } |
---|
[2566] | 566 | |
---|
[0] | 567 | if (!$this->modules[$id]['root_writable']) { |
---|
| 568 | throw new Exception(__('Cannot deactivate plugin.')); |
---|
| 569 | } |
---|
[2566] | 570 | |
---|
[0] | 571 | if (@file_put_contents($this->modules[$id]['root'].'/_disabled','')) { |
---|
| 572 | throw new Exception(__('Cannot deactivate plugin.')); |
---|
| 573 | } |
---|
| 574 | } |
---|
[2566] | 575 | |
---|
[0] | 576 | public function activateModule($id) |
---|
| 577 | { |
---|
| 578 | if (!isset($this->disabled[$id])) { |
---|
| 579 | throw new Exception(__('No such module.')); |
---|
| 580 | } |
---|
[2566] | 581 | |
---|
[0] | 582 | if (!$this->disabled[$id]['root_writable']) { |
---|
| 583 | throw new Exception(__('Cannot activate plugin.')); |
---|
| 584 | } |
---|
[2566] | 585 | |
---|
[0] | 586 | if (@unlink($this->disabled[$id]['root'].'/_disabled') === false) { |
---|
| 587 | throw new Exception(__('Cannot activate plugin.')); |
---|
| 588 | } |
---|
| 589 | } |
---|
[2566] | 590 | |
---|
[0] | 591 | /** |
---|
| 592 | This method will search for file <var>$file</var> in language |
---|
| 593 | <var>$lang</var> for module <var>$id</var>. |
---|
[2566] | 594 | |
---|
[0] | 595 | <var>$file</var> should not have any extension. |
---|
[2566] | 596 | |
---|
[0] | 597 | @param id <b>string</b> Module ID |
---|
| 598 | @param lang <b>string</b> Language code |
---|
| 599 | @param file <b>string</b> File name (without extension) |
---|
| 600 | */ |
---|
| 601 | public function loadModuleL10N($id,$lang,$file) |
---|
| 602 | { |
---|
| 603 | if (!$lang || !isset($this->modules[$id])) { |
---|
| 604 | return; |
---|
| 605 | } |
---|
[2566] | 606 | |
---|
[0] | 607 | $lfile = $this->modules[$id]['root'].'/locales/%s/%s'; |
---|
| 608 | if (l10n::set(sprintf($lfile,$lang,$file)) === false && $lang != 'en') { |
---|
| 609 | l10n::set(sprintf($lfile,'en',$file)); |
---|
| 610 | } |
---|
| 611 | } |
---|
[2566] | 612 | |
---|
[0] | 613 | public function loadModuleL10Nresources($id,$lang) |
---|
| 614 | { |
---|
| 615 | if (!$lang || !isset($this->modules[$id])) { |
---|
| 616 | return; |
---|
| 617 | } |
---|
[2566] | 618 | |
---|
[0] | 619 | $f = l10n::getFilePath($this->modules[$id]['root'].'/locales','resources.php',$lang); |
---|
| 620 | if ($f) { |
---|
| 621 | $this->loadModuleFile($f); |
---|
| 622 | } |
---|
| 623 | } |
---|
[2566] | 624 | |
---|
[0] | 625 | /** |
---|
| 626 | Returns all modules associative array or only one module if <var>$id</var> |
---|
| 627 | is present. |
---|
[2566] | 628 | |
---|
[0] | 629 | @param id <b>string</b> Optionnal module ID |
---|
| 630 | @return <b>array</b> |
---|
| 631 | */ |
---|
| 632 | public function getModules($id=null) |
---|
| 633 | { |
---|
| 634 | if ($id && isset($this->modules[$id])) { |
---|
| 635 | return $this->modules[$id]; |
---|
| 636 | } |
---|
| 637 | return $this->modules; |
---|
| 638 | } |
---|
[2566] | 639 | |
---|
[0] | 640 | /** |
---|
| 641 | Returns true if the module with ID <var>$id</var> exists. |
---|
[2566] | 642 | |
---|
[0] | 643 | @param id <b>string</b> Module ID |
---|
| 644 | @return <b>boolean</b> |
---|
| 645 | */ |
---|
| 646 | public function moduleExists($id) |
---|
| 647 | { |
---|
| 648 | return isset($this->modules[$id]); |
---|
| 649 | } |
---|
[2566] | 650 | |
---|
[0] | 651 | /** |
---|
| 652 | Returns all disabled modules in an array |
---|
[2566] | 653 | |
---|
[0] | 654 | @return <b>array</b> |
---|
| 655 | */ |
---|
| 656 | public function getDisabledModules() |
---|
| 657 | { |
---|
| 658 | return $this->disabled; |
---|
| 659 | } |
---|
[2566] | 660 | |
---|
[0] | 661 | /** |
---|
| 662 | Returns root path for module with ID <var>$id</var>. |
---|
[2566] | 663 | |
---|
[0] | 664 | @param id <b>string</b> Module ID |
---|
| 665 | @return <b>string</b> |
---|
| 666 | */ |
---|
| 667 | public function moduleRoot($id) |
---|
| 668 | { |
---|
| 669 | return $this->moduleInfo($id,'root'); |
---|
| 670 | } |
---|
[2566] | 671 | |
---|
[0] | 672 | /** |
---|
| 673 | Returns a module information that could be: |
---|
| 674 | - root |
---|
| 675 | - name |
---|
| 676 | - desc |
---|
| 677 | - author |
---|
| 678 | - version |
---|
| 679 | - permissions |
---|
| 680 | - priority |
---|
[2566] | 681 | |
---|
[0] | 682 | @param id <b>string</b> Module ID |
---|
| 683 | @param info <b>string</b> Information to retrieve |
---|
| 684 | @return <b>string</b> |
---|
| 685 | */ |
---|
| 686 | public function moduleInfo($id,$info) |
---|
| 687 | { |
---|
| 688 | return isset($this->modules[$id][$info]) ? $this->modules[$id][$info] : null; |
---|
| 689 | } |
---|
[2566] | 690 | |
---|
[0] | 691 | /** |
---|
| 692 | Loads namespace <var>$ns</var> specific files for all modules. |
---|
[2566] | 693 | |
---|
[0] | 694 | @param ns <b>string</b> Namespace name |
---|
| 695 | */ |
---|
| 696 | public function loadNsFiles($ns=null) |
---|
| 697 | { |
---|
| 698 | foreach ($this->modules as $k => $v) { |
---|
| 699 | $this->loadNsFile($k,$ns); |
---|
| 700 | } |
---|
| 701 | } |
---|
[2566] | 702 | |
---|
[0] | 703 | /** |
---|
| 704 | Loads namespace <var>$ns</var> specific file for module with ID |
---|
| 705 | <var>$id</var> |
---|
[2566] | 706 | |
---|
[0] | 707 | @param id <b>string</b> Module ID |
---|
| 708 | @param ns <b>string</b> Namespace name |
---|
| 709 | */ |
---|
| 710 | public function loadNsFile($id,$ns=null) |
---|
| 711 | { |
---|
| 712 | switch ($ns) { |
---|
| 713 | case 'admin': |
---|
| 714 | $this->loadModuleFile($this->modules[$id]['root'].'/_admin.php'); |
---|
| 715 | break; |
---|
| 716 | case 'public': |
---|
| 717 | $this->loadModuleFile($this->modules[$id]['root'].'/_public.php'); |
---|
| 718 | break; |
---|
| 719 | case 'xmlrpc': |
---|
| 720 | $this->loadModuleFile($this->modules[$id]['root'].'/_xmlrpc.php'); |
---|
| 721 | break; |
---|
| 722 | } |
---|
| 723 | } |
---|
[2566] | 724 | |
---|
[0] | 725 | public function getErrors() |
---|
| 726 | { |
---|
| 727 | return $this->errors; |
---|
| 728 | } |
---|
[2566] | 729 | |
---|
[3395] | 730 | protected function loadModuleFile($________,$catch = true) |
---|
[0] | 731 | { |
---|
| 732 | if (!file_exists($________)) { |
---|
| 733 | return; |
---|
| 734 | } |
---|
[2566] | 735 | |
---|
[0] | 736 | self::$_k = array_keys($GLOBALS); |
---|
[2566] | 737 | |
---|
[0] | 738 | foreach (self::$_k as self::$_n) { |
---|
| 739 | if (!in_array(self::$_n,self::$superglobals)) { |
---|
| 740 | global ${self::$_n}; |
---|
| 741 | } |
---|
| 742 | } |
---|
[2566] | 743 | |
---|
[3395] | 744 | if ($catch) { |
---|
| 745 | // Catch ouput to prevents hacked or corrupted modules |
---|
| 746 | ob_start(); |
---|
| 747 | $ret = require $________; |
---|
| 748 | ob_end_clean(); |
---|
| 749 | return $ret; |
---|
| 750 | } |
---|
| 751 | |
---|
[0] | 752 | return require $________; |
---|
| 753 | } |
---|
[2566] | 754 | |
---|
[0] | 755 | private function sortModules($a,$b) |
---|
| 756 | { |
---|
| 757 | if ($a['priority'] == $b['priority']) { |
---|
| 758 | return strcasecmp($a['name'],$b['name']); |
---|
| 759 | } |
---|
[2566] | 760 | |
---|
[0] | 761 | return ($a['priority'] < $b['priority']) ? -1 : 1; |
---|
| 762 | } |
---|
| 763 | } |
---|