Dotclear

source: inc/core/class.dc.modules.php @ 3333:11107ba2fc59

Revision 3333:11107ba2fc59, 19.0 KB checked in by franck <carnet.franck.paul@…>, 9 years ago (diff)

Cope with settings URLs for modules (defined in _define.php).
The settings URLs are displayed on the plugins maganement page, and at the bottom of each plugin main page if any (index.php).

The URLs are set in _define.php, as a new property using this schema:

'settings' => array( <list of URLs> )

With:

<list of URLs> = '<type>' => '<location>', …
<type> = 'self' (own plugin page), 'blog' (in blog parameters page) or 'pref' (in user preferences page)
<location> = (empty) or #<tab>[.<id>] with <tab> = id of the corresponding tab, and <id> = id of fieldset, h4, h5, field, … of first corresponding field

The list of URLs are displayed in the order defined in the array above.

Examples:

Antispam plugin:

'settings' => array(

'self' => ,
'blog' => '#params.antispam_params'

)

self → for main settings of the plugin on its own page (index.php)
blog → for secondary settings in the blog parameters

Tags plugin:

'settings' => array(

'pref' => '#user-options.tags_prefs'

)

pref → for tags list format in user preferences

Maintenance plugin:

'settings' => array(

'self' => '#settings'

)

self → "settings" tab of its own page (index.php)

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

Sites map