[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; |
---|
| 195 | require $full_entry.'/_define.php'; |
---|
[2997] | 196 | $this->all_modules[$entry] =& $this->modules[$entry]; |
---|
[0] | 197 | $this->id = null; |
---|
| 198 | $this->mroot = null; |
---|
| 199 | } |
---|
| 200 | else |
---|
| 201 | { |
---|
[2954] | 202 | if (file_exists($full_entry.'/_define.php')) { |
---|
[2997] | 203 | $this->id = $entry; |
---|
| 204 | $this->mroot = $full_entry; |
---|
[2954] | 205 | $this->disabled_mode=true; |
---|
| 206 | require $full_entry.'/_define.php'; |
---|
| 207 | $this->disabled_mode=false; |
---|
| 208 | $this->disabled[$entry] = $this->disabled_meta; |
---|
[2997] | 209 | $this->all_modules[$entry] =& $this->disabled[$entry]; |
---|
| 210 | $this->id = null; |
---|
| 211 | $this->mroot = null; |
---|
[2954] | 212 | } |
---|
[0] | 213 | } |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | $d->close(); |
---|
| 217 | } |
---|
[2997] | 218 | $this->checkDependencies(); |
---|
[0] | 219 | # Sort plugins |
---|
| 220 | uasort($this->modules,array($this,'sortModules')); |
---|
[2566] | 221 | |
---|
[0] | 222 | foreach ($this->modules as $id => $m) |
---|
| 223 | { |
---|
[2918] | 224 | # Load translation and _prepend |
---|
[0] | 225 | if (file_exists($m['root'].'/_prepend.php')) |
---|
| 226 | { |
---|
[2095] | 227 | $r = $this->loadModuleFile($m['root'].'/_prepend.php'); |
---|
[2566] | 228 | |
---|
[0] | 229 | # If _prepend.php file returns null (ie. it has a void return statement) |
---|
| 230 | if (is_null($r)) { |
---|
[2945] | 231 | $ignored[] = $id; |
---|
[0] | 232 | continue; |
---|
| 233 | } |
---|
| 234 | unset($r); |
---|
| 235 | } |
---|
[2566] | 236 | |
---|
[0] | 237 | $this->loadModuleL10N($id,$lang,'main'); |
---|
| 238 | if ($ns == 'admin') { |
---|
| 239 | $this->loadModuleL10Nresources($id,$lang); |
---|
[2708] | 240 | $this->core->adminurl->register('admin.plugin.'.$id,'plugin.php',array('p'=>$id)); |
---|
[0] | 241 | } |
---|
[2918] | 242 | } |
---|
[3108] | 243 | |
---|
| 244 | // Give opportunity to do something before loading context (admin,public,xmlrpc) files |
---|
| 245 | $this->core->callBehavior('coreBeforeLoadingNsFiles',$this->core,$this,$lang); |
---|
| 246 | |
---|
[2918] | 247 | foreach ($this->modules as $id => $m) |
---|
| 248 | { |
---|
[2945] | 249 | # If _prepend.php file returns null (ie. it has a void return statement) |
---|
| 250 | if (in_array($id,$ignored)) { |
---|
| 251 | continue; |
---|
| 252 | } |
---|
[2918] | 253 | # Load ns_file |
---|
[0] | 254 | $this->loadNsFile($id,$ns); |
---|
| 255 | } |
---|
| 256 | } |
---|
[2566] | 257 | |
---|
[0] | 258 | public function requireDefine($dir,$id) |
---|
| 259 | { |
---|
| 260 | if (file_exists($dir.'/_define.php')) { |
---|
| 261 | $this->id = $id; |
---|
| 262 | require $dir.'/_define.php'; |
---|
| 263 | $this->id = null; |
---|
| 264 | } |
---|
[2566] | 265 | } |
---|
| 266 | |
---|
[0] | 267 | /** |
---|
| 268 | This method registers a module in modules list. You should use this to |
---|
| 269 | register a new module. |
---|
[2566] | 270 | |
---|
[0] | 271 | <var>$permissions</var> is a comma separated list of permissions for your |
---|
| 272 | module. If <var>$permissions</var> is null, only super admin has access to |
---|
| 273 | this module. |
---|
[2566] | 274 | |
---|
[0] | 275 | <var>$priority</var> is an integer. Modules are sorted by priority and name. |
---|
| 276 | Lowest priority comes first. |
---|
[2566] | 277 | |
---|
[0] | 278 | @param name <b>string</b> Module name |
---|
| 279 | @param desc <b>string</b> Module description |
---|
| 280 | @param author <b>string</b> Module author name |
---|
| 281 | @param version <b>string</b> Module version |
---|
[2566] | 282 | @param properties <b>array</b> extra properties |
---|
[2492] | 283 | (currently available keys : permissions, priority, type) |
---|
[0] | 284 | */ |
---|
[464] | 285 | public function registerModule($name,$desc,$author,$version, $properties = array()) |
---|
[0] | 286 | { |
---|
[2954] | 287 | if ($this->disabled_mode) { |
---|
| 288 | $this->disabled_meta = array_merge( |
---|
| 289 | $properties, |
---|
| 290 | array( |
---|
| 291 | 'root' => $this->mroot, |
---|
| 292 | 'name' => $name, |
---|
| 293 | 'desc' => $desc, |
---|
| 294 | 'author' => $author, |
---|
| 295 | 'version' => $version, |
---|
[2997] | 296 | 'enabled' => false, |
---|
[2954] | 297 | 'root_writable' => is_writable($this->mroot) |
---|
| 298 | ) |
---|
| 299 | ); |
---|
| 300 | return; |
---|
| 301 | } |
---|
[2492] | 302 | # Fallback to legacy registerModule parameters |
---|
[464] | 303 | if (!is_array($properties)) { |
---|
| 304 | $args = func_get_args(); |
---|
| 305 | $properties = array(); |
---|
| 306 | if (isset($args[4])) { |
---|
| 307 | $properties['permissions']=$args[4]; |
---|
| 308 | } |
---|
| 309 | if (isset($args[5])) { |
---|
| 310 | $properties['priority']= (integer)$args[5]; |
---|
| 311 | } |
---|
| 312 | } |
---|
[2492] | 313 | |
---|
| 314 | # Default module properties |
---|
[464] | 315 | $properties = array_merge( |
---|
| 316 | array( |
---|
| 317 | 'permissions' => null, |
---|
[2239] | 318 | 'priority' => 1000, |
---|
| 319 | 'standalone_config' => false, |
---|
[2954] | 320 | 'type' => null, |
---|
[2997] | 321 | 'enabled' => true, |
---|
[2954] | 322 | 'requires' => array() |
---|
[464] | 323 | ), $properties |
---|
| 324 | ); |
---|
[2239] | 325 | |
---|
[2492] | 326 | # Check module type |
---|
| 327 | if (self::$type !== null && $properties['type'] !== null && $properties['type'] != self::$type) { |
---|
[2239] | 328 | $this->errors[] = sprintf( |
---|
| 329 | __('Module "%s" has type "%s" that mismatch required module type "%s".'), |
---|
| 330 | '<strong>'.html::escapeHTML($name).'</strong>', |
---|
| 331 | '<em>'.html::escapeHTML($properties['type']).'</em>', |
---|
| 332 | '<em>'.html::escapeHTML(self::$type).'</em>' |
---|
| 333 | ); |
---|
| 334 | return; |
---|
| 335 | } |
---|
| 336 | |
---|
[2492] | 337 | # Check module perms on admin side |
---|
[464] | 338 | $permissions = $properties['permissions']; |
---|
[0] | 339 | if ($this->ns == 'admin') { |
---|
| 340 | if ($permissions == '' && !$this->core->auth->isSuperAdmin()) { |
---|
| 341 | return; |
---|
| 342 | } elseif (!$this->core->auth->check($permissions,$this->core->blog->id)) { |
---|
| 343 | return; |
---|
| 344 | } |
---|
| 345 | } |
---|
[2566] | 346 | |
---|
[2492] | 347 | # Check module install on multiple path |
---|
[0] | 348 | if ($this->id) { |
---|
| 349 | $module_exists = array_key_exists($name,$this->modules_names); |
---|
| 350 | $module_overwrite = $module_exists ? version_compare($this->modules_names[$name],$version,'<') : false; |
---|
| 351 | if (!$module_exists || ($module_exists && $module_overwrite)) { |
---|
| 352 | $this->modules_names[$name] = $version; |
---|
[464] | 353 | $this->modules[$this->id] = array_merge( |
---|
| 354 | $properties, |
---|
| 355 | array( |
---|
| 356 | 'root' => $this->mroot, |
---|
| 357 | 'name' => $name, |
---|
| 358 | 'desc' => $desc, |
---|
| 359 | 'author' => $author, |
---|
| 360 | 'version' => $version, |
---|
| 361 | 'root_writable' => is_writable($this->mroot) |
---|
| 362 | ) |
---|
[0] | 363 | ); |
---|
| 364 | } |
---|
| 365 | else { |
---|
| 366 | $path1 = path::real($this->moduleInfo($name,'root')); |
---|
| 367 | $path2 = path::real($this->mroot); |
---|
| 368 | $this->errors[] = sprintf( |
---|
[2239] | 369 | __('Module "%s" is installed twice in "%s" and "%s".'), |
---|
[0] | 370 | '<strong>'.$name.'</strong>', |
---|
| 371 | '<em>'.$path1.'</em>', |
---|
| 372 | '<em>'.$path2.'</em>' |
---|
| 373 | ); |
---|
| 374 | } |
---|
| 375 | } |
---|
| 376 | } |
---|
[2566] | 377 | |
---|
[0] | 378 | public function resetModulesList() |
---|
| 379 | { |
---|
| 380 | $this->modules = array(); |
---|
| 381 | $this->modules_names = array(); |
---|
[2239] | 382 | $this->errors = array(); |
---|
[2566] | 383 | } |
---|
| 384 | |
---|
[0] | 385 | public static function installPackage($zip_file,dcModules &$modules) |
---|
| 386 | { |
---|
| 387 | $zip = new fileUnzip($zip_file); |
---|
[2245] | 388 | $zip->getList(false,'#(^|/)(__MACOSX|\.svn|\.hg|\.git|\.DS_Store|\.directory|Thumbs\.db)(/|$)#'); |
---|
[2566] | 389 | |
---|
[0] | 390 | $zip_root_dir = $zip->getRootDir(); |
---|
| 391 | $define = ''; |
---|
| 392 | if ($zip_root_dir != false) { |
---|
| 393 | $target = dirname($zip_file); |
---|
| 394 | $destination = $target.'/'.$zip_root_dir; |
---|
| 395 | $define = $zip_root_dir.'/_define.php'; |
---|
| 396 | $has_define = $zip->hasFile($define); |
---|
| 397 | } else { |
---|
| 398 | $target = dirname($zip_file).'/'.preg_replace('/\.([^.]+)$/','',basename($zip_file)); |
---|
| 399 | $destination = $target; |
---|
| 400 | $define = '_define.php'; |
---|
| 401 | $has_define = $zip->hasFile($define); |
---|
| 402 | } |
---|
[2566] | 403 | |
---|
[0] | 404 | if ($zip->isEmpty()) { |
---|
| 405 | $zip->close(); |
---|
| 406 | unlink($zip_file); |
---|
| 407 | throw new Exception(__('Empty module zip file.')); |
---|
| 408 | } |
---|
[2566] | 409 | |
---|
[0] | 410 | if (!$has_define) { |
---|
| 411 | $zip->close(); |
---|
| 412 | unlink($zip_file); |
---|
| 413 | throw new Exception(__('The zip file does not appear to be a valid Dotclear module.')); |
---|
| 414 | } |
---|
[2566] | 415 | |
---|
[0] | 416 | $ret_code = 1; |
---|
[2566] | 417 | |
---|
[2239] | 418 | if (!is_dir($destination)) |
---|
| 419 | { |
---|
| 420 | try { |
---|
| 421 | files::makeDir($destination,true); |
---|
[2566] | 422 | |
---|
[2239] | 423 | $sandbox = clone $modules; |
---|
| 424 | $zip->unzip($define, $target.'/_define.php'); |
---|
[2566] | 425 | |
---|
[2239] | 426 | $sandbox->resetModulesList(); |
---|
| 427 | $sandbox->requireDefine($target,basename($destination)); |
---|
| 428 | unlink($target.'/_define.php'); |
---|
[2566] | 429 | |
---|
[2239] | 430 | $new_errors = $sandbox->getErrors(); |
---|
| 431 | if (!empty($new_errors)) { |
---|
| 432 | $new_errors = is_array($new_errors) ? implode(" \n",$new_errors) : $new_errors; |
---|
| 433 | throw new Exception($new_errors); |
---|
| 434 | } |
---|
[2566] | 435 | |
---|
[2239] | 436 | files::deltree($destination); |
---|
| 437 | } |
---|
| 438 | catch(Exception $e) |
---|
| 439 | { |
---|
| 440 | $zip->close(); |
---|
| 441 | unlink($zip_file); |
---|
| 442 | files::deltree($destination); |
---|
[2566] | 443 | throw new Exception($e->getMessage()); |
---|
[2239] | 444 | } |
---|
| 445 | } |
---|
| 446 | else |
---|
[0] | 447 | { |
---|
| 448 | # test for update |
---|
| 449 | $sandbox = clone $modules; |
---|
| 450 | $zip->unzip($define, $target.'/_define.php'); |
---|
[2566] | 451 | |
---|
[0] | 452 | $sandbox->resetModulesList(); |
---|
| 453 | $sandbox->requireDefine($target,basename($destination)); |
---|
| 454 | unlink($target.'/_define.php'); |
---|
| 455 | $new_modules = $sandbox->getModules(); |
---|
[2566] | 456 | |
---|
[0] | 457 | if (!empty($new_modules)) |
---|
| 458 | { |
---|
| 459 | $tmp = array_keys($new_modules); |
---|
| 460 | $id = $tmp[0]; |
---|
| 461 | $cur_module = $modules->getModules($id); |
---|
[2432] | 462 | if (!empty($cur_module) && (defined('DC_DEV') && DC_DEV === true || dcUtils::versionsCompare($new_modules[$id]['version'], $cur_module['version'], '>', true))) |
---|
[0] | 463 | { |
---|
| 464 | # delete old module |
---|
| 465 | if (!files::deltree($destination)) { |
---|
| 466 | throw new Exception(__('An error occurred during module deletion.')); |
---|
| 467 | } |
---|
| 468 | $ret_code = 2; |
---|
| 469 | } |
---|
| 470 | else |
---|
| 471 | { |
---|
| 472 | $zip->close(); |
---|
| 473 | unlink($zip_file); |
---|
[2566] | 474 | throw new Exception(sprintf(__('Unable to upgrade "%s". (older or same version)'),basename($destination))); |
---|
[0] | 475 | } |
---|
| 476 | } |
---|
| 477 | else |
---|
| 478 | { |
---|
| 479 | $zip->close(); |
---|
| 480 | unlink($zip_file); |
---|
[2566] | 481 | throw new Exception(sprintf(__('Unable to read new _define.php file'))); |
---|
[0] | 482 | } |
---|
| 483 | } |
---|
| 484 | $zip->unzipAll($target); |
---|
| 485 | $zip->close(); |
---|
| 486 | unlink($zip_file); |
---|
| 487 | return $ret_code; |
---|
| 488 | } |
---|
[2566] | 489 | |
---|
[0] | 490 | /** |
---|
| 491 | This method installs all modules having a _install file. |
---|
[2566] | 492 | |
---|
[0] | 493 | @see dcModules::installModule |
---|
| 494 | */ |
---|
| 495 | public function installModules() |
---|
| 496 | { |
---|
| 497 | $res = array('success'=>array(),'failure'=>array()); |
---|
| 498 | foreach ($this->modules as $id => &$m) |
---|
| 499 | { |
---|
| 500 | $i = $this->installModule($id,$msg); |
---|
| 501 | if ($i === true) { |
---|
| 502 | $res['success'][$id] = true; |
---|
| 503 | } elseif ($i === false) { |
---|
| 504 | $res['failure'][$id] = $msg; |
---|
| 505 | } |
---|
| 506 | } |
---|
[2566] | 507 | |
---|
[0] | 508 | return $res; |
---|
| 509 | } |
---|
[2566] | 510 | |
---|
[0] | 511 | /** |
---|
| 512 | This method installs module with ID <var>$id</var> and having a _install |
---|
| 513 | file. This file should throw exception on failure or true if it installs |
---|
| 514 | successfully. |
---|
[2566] | 515 | |
---|
[0] | 516 | <var>$msg</var> is an out parameter that handle installer message. |
---|
[2566] | 517 | |
---|
[0] | 518 | @param id <b>string</b> Module ID |
---|
| 519 | @param msg <b>string</b> Module installer message |
---|
| 520 | @return <b>boolean</b> |
---|
| 521 | */ |
---|
| 522 | public function installModule($id,&$msg) |
---|
| 523 | { |
---|
| 524 | try { |
---|
| 525 | $i = $this->loadModuleFile($this->modules[$id]['root'].'/_install.php'); |
---|
| 526 | if ($i === true) { |
---|
| 527 | return true; |
---|
| 528 | } |
---|
| 529 | } catch (Exception $e) { |
---|
| 530 | $msg = $e->getMessage(); |
---|
| 531 | return false; |
---|
| 532 | } |
---|
[2566] | 533 | |
---|
[0] | 534 | return null; |
---|
| 535 | } |
---|
[2566] | 536 | |
---|
[0] | 537 | public function deleteModule($id,$disabled=false) |
---|
| 538 | { |
---|
| 539 | if ($disabled) { |
---|
| 540 | $p =& $this->disabled; |
---|
| 541 | } else { |
---|
| 542 | $p =& $this->modules; |
---|
| 543 | } |
---|
[2566] | 544 | |
---|
[0] | 545 | if (!isset($p[$id])) { |
---|
| 546 | throw new Exception(__('No such module.')); |
---|
| 547 | } |
---|
[2566] | 548 | |
---|
[0] | 549 | if (!files::deltree($p[$id]['root'])) { |
---|
| 550 | throw new Exception(__('Cannot remove module files')); |
---|
| 551 | } |
---|
| 552 | } |
---|
[2566] | 553 | |
---|
[0] | 554 | public function deactivateModule($id) |
---|
| 555 | { |
---|
| 556 | if (!isset($this->modules[$id])) { |
---|
| 557 | throw new Exception(__('No such module.')); |
---|
| 558 | } |
---|
[2566] | 559 | |
---|
[0] | 560 | if (!$this->modules[$id]['root_writable']) { |
---|
| 561 | throw new Exception(__('Cannot deactivate plugin.')); |
---|
| 562 | } |
---|
[2566] | 563 | |
---|
[0] | 564 | if (@file_put_contents($this->modules[$id]['root'].'/_disabled','')) { |
---|
| 565 | throw new Exception(__('Cannot deactivate plugin.')); |
---|
| 566 | } |
---|
| 567 | } |
---|
[2566] | 568 | |
---|
[0] | 569 | public function activateModule($id) |
---|
| 570 | { |
---|
| 571 | if (!isset($this->disabled[$id])) { |
---|
| 572 | throw new Exception(__('No such module.')); |
---|
| 573 | } |
---|
[2566] | 574 | |
---|
[0] | 575 | if (!$this->disabled[$id]['root_writable']) { |
---|
| 576 | throw new Exception(__('Cannot activate plugin.')); |
---|
| 577 | } |
---|
[2566] | 578 | |
---|
[0] | 579 | if (@unlink($this->disabled[$id]['root'].'/_disabled') === false) { |
---|
| 580 | throw new Exception(__('Cannot activate plugin.')); |
---|
| 581 | } |
---|
| 582 | } |
---|
[2566] | 583 | |
---|
[0] | 584 | /** |
---|
| 585 | This method will search for file <var>$file</var> in language |
---|
| 586 | <var>$lang</var> for module <var>$id</var>. |
---|
[2566] | 587 | |
---|
[0] | 588 | <var>$file</var> should not have any extension. |
---|
[2566] | 589 | |
---|
[0] | 590 | @param id <b>string</b> Module ID |
---|
| 591 | @param lang <b>string</b> Language code |
---|
| 592 | @param file <b>string</b> File name (without extension) |
---|
| 593 | */ |
---|
| 594 | public function loadModuleL10N($id,$lang,$file) |
---|
| 595 | { |
---|
| 596 | if (!$lang || !isset($this->modules[$id])) { |
---|
| 597 | return; |
---|
| 598 | } |
---|
[2566] | 599 | |
---|
[0] | 600 | $lfile = $this->modules[$id]['root'].'/locales/%s/%s'; |
---|
| 601 | if (l10n::set(sprintf($lfile,$lang,$file)) === false && $lang != 'en') { |
---|
| 602 | l10n::set(sprintf($lfile,'en',$file)); |
---|
| 603 | } |
---|
| 604 | } |
---|
[2566] | 605 | |
---|
[0] | 606 | public function loadModuleL10Nresources($id,$lang) |
---|
| 607 | { |
---|
| 608 | if (!$lang || !isset($this->modules[$id])) { |
---|
| 609 | return; |
---|
| 610 | } |
---|
[2566] | 611 | |
---|
[0] | 612 | $f = l10n::getFilePath($this->modules[$id]['root'].'/locales','resources.php',$lang); |
---|
| 613 | if ($f) { |
---|
| 614 | $this->loadModuleFile($f); |
---|
| 615 | } |
---|
| 616 | } |
---|
[2566] | 617 | |
---|
[0] | 618 | /** |
---|
| 619 | Returns all modules associative array or only one module if <var>$id</var> |
---|
| 620 | is present. |
---|
[2566] | 621 | |
---|
[0] | 622 | @param id <b>string</b> Optionnal module ID |
---|
| 623 | @return <b>array</b> |
---|
| 624 | */ |
---|
| 625 | public function getModules($id=null) |
---|
| 626 | { |
---|
| 627 | if ($id && isset($this->modules[$id])) { |
---|
| 628 | return $this->modules[$id]; |
---|
| 629 | } |
---|
| 630 | return $this->modules; |
---|
| 631 | } |
---|
[2566] | 632 | |
---|
[0] | 633 | /** |
---|
| 634 | Returns true if the module with ID <var>$id</var> exists. |
---|
[2566] | 635 | |
---|
[0] | 636 | @param id <b>string</b> Module ID |
---|
| 637 | @return <b>boolean</b> |
---|
| 638 | */ |
---|
| 639 | public function moduleExists($id) |
---|
| 640 | { |
---|
| 641 | return isset($this->modules[$id]); |
---|
| 642 | } |
---|
[2566] | 643 | |
---|
[0] | 644 | /** |
---|
| 645 | Returns all disabled modules in an array |
---|
[2566] | 646 | |
---|
[0] | 647 | @return <b>array</b> |
---|
| 648 | */ |
---|
| 649 | public function getDisabledModules() |
---|
| 650 | { |
---|
| 651 | return $this->disabled; |
---|
| 652 | } |
---|
[2566] | 653 | |
---|
[0] | 654 | /** |
---|
| 655 | Returns root path for module with ID <var>$id</var>. |
---|
[2566] | 656 | |
---|
[0] | 657 | @param id <b>string</b> Module ID |
---|
| 658 | @return <b>string</b> |
---|
| 659 | */ |
---|
| 660 | public function moduleRoot($id) |
---|
| 661 | { |
---|
| 662 | return $this->moduleInfo($id,'root'); |
---|
| 663 | } |
---|
[2566] | 664 | |
---|
[0] | 665 | /** |
---|
| 666 | Returns a module information that could be: |
---|
| 667 | - root |
---|
| 668 | - name |
---|
| 669 | - desc |
---|
| 670 | - author |
---|
| 671 | - version |
---|
| 672 | - permissions |
---|
| 673 | - priority |
---|
[2566] | 674 | |
---|
[0] | 675 | @param id <b>string</b> Module ID |
---|
| 676 | @param info <b>string</b> Information to retrieve |
---|
| 677 | @return <b>string</b> |
---|
| 678 | */ |
---|
| 679 | public function moduleInfo($id,$info) |
---|
| 680 | { |
---|
| 681 | return isset($this->modules[$id][$info]) ? $this->modules[$id][$info] : null; |
---|
| 682 | } |
---|
[2566] | 683 | |
---|
[0] | 684 | /** |
---|
| 685 | Loads namespace <var>$ns</var> specific files for all modules. |
---|
[2566] | 686 | |
---|
[0] | 687 | @param ns <b>string</b> Namespace name |
---|
| 688 | */ |
---|
| 689 | public function loadNsFiles($ns=null) |
---|
| 690 | { |
---|
| 691 | foreach ($this->modules as $k => $v) { |
---|
| 692 | $this->loadNsFile($k,$ns); |
---|
| 693 | } |
---|
| 694 | } |
---|
[2566] | 695 | |
---|
[0] | 696 | /** |
---|
| 697 | Loads namespace <var>$ns</var> specific file for module with ID |
---|
| 698 | <var>$id</var> |
---|
[2566] | 699 | |
---|
[0] | 700 | @param id <b>string</b> Module ID |
---|
| 701 | @param ns <b>string</b> Namespace name |
---|
| 702 | */ |
---|
| 703 | public function loadNsFile($id,$ns=null) |
---|
| 704 | { |
---|
| 705 | switch ($ns) { |
---|
| 706 | case 'admin': |
---|
| 707 | $this->loadModuleFile($this->modules[$id]['root'].'/_admin.php'); |
---|
| 708 | break; |
---|
| 709 | case 'public': |
---|
| 710 | $this->loadModuleFile($this->modules[$id]['root'].'/_public.php'); |
---|
| 711 | break; |
---|
| 712 | case 'xmlrpc': |
---|
| 713 | $this->loadModuleFile($this->modules[$id]['root'].'/_xmlrpc.php'); |
---|
| 714 | break; |
---|
| 715 | } |
---|
| 716 | } |
---|
[2566] | 717 | |
---|
[0] | 718 | public function getErrors() |
---|
| 719 | { |
---|
| 720 | return $this->errors; |
---|
| 721 | } |
---|
[2566] | 722 | |
---|
[0] | 723 | protected function loadModuleFile($________) |
---|
| 724 | { |
---|
| 725 | if (!file_exists($________)) { |
---|
| 726 | return; |
---|
| 727 | } |
---|
[2566] | 728 | |
---|
[0] | 729 | self::$_k = array_keys($GLOBALS); |
---|
[2566] | 730 | |
---|
[0] | 731 | foreach (self::$_k as self::$_n) { |
---|
| 732 | if (!in_array(self::$_n,self::$superglobals)) { |
---|
| 733 | global ${self::$_n}; |
---|
| 734 | } |
---|
| 735 | } |
---|
[2566] | 736 | |
---|
[0] | 737 | return require $________; |
---|
| 738 | } |
---|
[2566] | 739 | |
---|
[0] | 740 | private function sortModules($a,$b) |
---|
| 741 | { |
---|
| 742 | if ($a['priority'] == $b['priority']) { |
---|
| 743 | return strcasecmp($a['name'],$b['name']); |
---|
| 744 | } |
---|
[2566] | 745 | |
---|
[0] | 746 | return ($a['priority'] < $b['priority']) ? -1 : 1; |
---|
| 747 | } |
---|
| 748 | } |
---|