]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Core/Plugins.php
db588ca0dc57671483d89d9ce371819bca378335
[galette.git] / galette / lib / Galette / Core / Plugins.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Plugins handling
7 *
8 * PHP version 5
9 *
10 * Copyright © 2009-2014 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Core
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2009-2014 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7 - 2009-03-09
36 */
37
38 namespace Galette\Core;
39
40 use Analog\Analog;
41 use Galette\Common\ClassLoader;
42 use Galette\Core\Preferences;
43
44 /**
45 * Plugins class for galette
46 *
47 * @category Core
48 * @name Plugins
49 * @package Galette
50 * @author Johan Cwiklinski <johan@x-tnd.be>
51 * @copyright 2009-2014 The Galette Team
52 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
53 * @link http://galette.tuxfamily.org
54 * @since Available since 0.7 - 2009-03-09
55 */
56
57 class Plugins
58 {
59 const DISABLED_COMPAT = 0;
60 const DISABLED_MISS = 1;
61 const DISABLED_EXPLICIT = 2;
62
63 protected $path;
64 protected $modules = array();
65 protected $disabled = array();
66
67 protected $id;
68 protected $mroot;
69
70 protected $preferences;
71 protected $autoload = false;
72
73 /**
74 * Register autoloaders for all plugins
75 *
76 * @param string $path could be a separated list of paths
77 * (path separator depends on your OS).
78 *
79 * @return void
80 */
81 public function autoload($path)
82 {
83 $this->path = explode(PATH_SEPARATOR, $path);
84 $this->autoload = true;
85 $this->parseModules();
86 }
87
88 /**
89 * Parse modules in current path
90 *
91 * @return void
92 */
93 protected function parseModules()
94 {
95 foreach ($this->path as $root) {
96 if (!is_dir($root) || !is_readable($root)) {
97 continue;
98 }
99
100 if (substr($root, -1) != '/') {
101 $root .= '/';
102 }
103
104 if (($d = @dir($root)) === false) {
105 continue;
106 }
107
108 while (($entry = $d->read()) !== false) {
109 $full_entry = realpath($root . $entry);
110 if ($entry != '.' && $entry != '..' && is_dir($full_entry)) {
111 $this->id = $entry;
112 $this->mroot = $full_entry;
113 if (!file_exists($full_entry . '/_define.php')
114 || !file_exists($full_entry . '/_routes.php')
115 ) {
116 //plugin is not compatible with that version of galette.
117 Analog::log(
118 'Plugin ' . $entry . ' is missing a _define.php and/or _routes.php ' .
119 'files that are required.',
120 Analog::WARNING
121 );
122 $this->setDisabled(self::DISABLED_MISS);
123 } elseif (!file_exists($full_entry . '/_disabled')) {
124 include $full_entry . '/_define.php';
125 $this->id = null;
126 $this->mroot = null;
127 if ($this->autoload == true) {
128 //set autoloader to PluginName.
129 if (file_exists($full_entry . '/lib') && isset($this->modules[$entry])) {
130 $varname = $entry . 'Loader';
131 $$varname = new ClassLoader(
132 $this->getNamespace($entry),
133 $full_entry . '/lib'
134 );
135 $$varname->register();
136 }
137 }
138 } else {
139 //plugin is not compatible with that version of galette.
140 Analog::log(
141 'Plugin ' . $entry . ' is explicitely disabled',
142 Analog::INFO
143 );
144 $this->setDisabled(self::DISABLED_EXPLICIT);
145 }
146 }
147 }
148 $d->close();
149 }
150 }
151
152 /**
153 * Loads modules.
154 *
155 * @param Preferences $preferences Galette's Preferences
156 * @param string $path could be a separated list of paths
157 * (path separator depends on your OS).
158 * @param string $lang Indicates if we need to load a lang file on plugin
159 * loading.
160 *
161 * @return void
162 */
163 public function loadModules(Preferences $preferences, $path, $lang = null)
164 {
165 $this->preferences = $preferences;
166 $this->path = explode(PATH_SEPARATOR, $path);
167
168 $this->parseModules();
169
170 // Sort plugins
171 uasort($this->modules, array($this, 'sortModules'));
172
173 // Load translation, _prepend and ns_file
174 foreach ($this->modules as $id => $m) {
175 $this->loadModuleL10N($id, $lang);
176 $this->loadSmarties($id);
177 $this->loadEventProviders($id);
178 $this->overridePrefs($id);
179 }
180 }
181
182 /**
183 * This method registers a module in modules list. You should use this to
184 * register a new module.
185 *
186 * <var>$permissions</var> is a comma separated list of permissions for your
187 * module. If <var>$permissions</var> is null, only super admin has access to
188 * this module.
189 *
190 * <var>$priority</var> is an integer. Modules are sorted by priority and name.
191 * Lowest priority comes first.
192 *
193 * @param string $name Module name
194 * @param string $desc Module description
195 * @param string $author Module author name
196 * @param string $version Module version
197 * @param string $compver Galette version compatibility
198 * @param string $route Module route name
199 * @param string $date Module release date
200 * @param string $acls Module routes ACLs
201 * @param integer $priority Module priority
202 *
203 * @return void
204 */
205 public function register(
206 $name,
207 $desc,
208 $author,
209 $version,
210 $compver = null,
211 $route = null,
212 $date = null,
213 $acls = null,
214 $priority = 1000
215 ) {
216 if ($compver === null) {
217 //plugin compatibility missing!
218 Analog::log(
219 'Plugin ' . $name . ' does not contains mandatory version ' .
220 'compatiblity information. Please contact the author.',
221 Analog::ERROR
222 );
223 $this->setDisabled(self::DISABLED_COMPAT);
224 } elseif (version_compare($compver, GALETTE_COMPAT_VERSION, '<')) {
225 //plugin is not compatible with that version of galette.
226 Analog::log(
227 'Plugin ' . $name . ' is known to be compatible with Galette ' .
228 $compver . ' only, but you current installation require a ' .
229 'plugin compatible with at least ' . GALETTE_COMPAT_VERSION,
230 Analog::WARNING
231 );
232 $this->setDisabled(self::DISABLED_COMPAT);
233 } else {
234 if ($this->id) {
235 $release_date = $date;
236 if ($date !== null && $this->autoload === false) {
237 //try to localize release date
238 try {
239 $release_date = new \DateTime($date);
240 $release_date = $release_date->format(__("Y-m-d"));
241 } catch (\Exception $e) {
242 Analog::log(
243 'Unable to localize release date for plugin ' . $name,
244 Analog::WARNING
245 );
246 }
247 }
248
249 $this->modules[$this->id] = array(
250 'root' => $this->mroot,
251 'name' => $name,
252 'desc' => $desc,
253 'author' => $author,
254 'version' => $version,
255 'acls' => $acls,
256 'date' => $release_date,
257 'priority' => $priority === null ?
258 1000 : (integer)$priority,
259 'root_writable' => is_writable($this->mroot),
260 'route' => $route
261 );
262 }
263 }
264 }
265
266 /**
267 * Reset modules list
268 *
269 * @return void
270 */
271 public function resetModulesList()
272 {
273 $this->modules = array();
274 }
275
276 /**
277 * Deactivate specified module
278 *
279 * @param string $id Module's ID
280 *
281 * @return void|exception
282 */
283 public function deactivateModule($id)
284 {
285 if (!isset($this->modules[$id])) {
286 throw new \Exception(_T("No such module."));
287 }
288
289 if (!$this->modules[$id]['root_writable']) {
290 throw new \Exception(_T("Cannot deactivate plugin."));
291 }
292
293 if (@file_put_contents($this->modules[$id]['root'] . '/_disabled', '')) {
294 throw new \Exception(_T("Cannot deactivate plugin."));
295 }
296 }
297
298 /**
299 * Activate specified module
300 *
301 * @param string $id Module's ID
302 *
303 * @return void|exception
304 */
305 public function activateModule($id)
306 {
307 if (!isset($this->disabled[$id])) {
308 throw new \Exception(_T("No such module."));
309 }
310
311 if (!$this->disabled[$id]['root_writable']) {
312 throw new \Exception(_T("Cannot activate plugin."));
313 }
314
315 if (@unlink($this->disabled[$id]['root'] . '/_disabled') === false) {
316 throw new \Exception(_T("Cannot activate plugin."));
317 }
318 }
319
320 /**
321 * This method will search for file <var>$file</var> in language
322 * <var>$lang</var> for module <var>$id</var>.
323 * <var>$file</var> should not have any extension.
324 *
325 * @param string $id Module ID
326 * @param string $language Language code
327 *
328 * @return void
329 */
330 public function loadModuleL10N($id, $language)
331 {
332 global $translator;
333
334 if (!$language || !isset($this->modules[$id])) {
335 return;
336 }
337
338 $domains = [
339 $this->modules[$id]['route']
340 ];
341 foreach ($domains as $domain) {
342 //load translation file for domain
343 $translator->addTranslationFilePattern(
344 'gettext',
345 $this->modules[$id]['root'] . '/lang/',
346 '/%s/LC_MESSAGES/' . $domain . '.mo',
347 $domain
348 );
349
350 //check if a local lang file exists and load it
351 $translator->addTranslationFilePattern(
352 'phparray',
353 $this->modules[$id]['root'] . '/lang/',
354 $domain . '_%s_local_lang.php',
355 $domain
356 );
357 }
358 }
359
360 /**
361 * Loads smarties specific (headers, assigments and so on)
362 *
363 * @param string $id Module ID
364 *
365 * @return void
366 */
367 public function loadSmarties($id)
368 {
369 $f = $this->modules[$id]['root'] . '/_smarties.php';
370 if (file_exists($f)) {
371 include_once $f;
372 if (isset($_tpl_assignments)) {
373 $this->modules[$id]['tpl_assignments'] = $_tpl_assignments;
374 }
375 }
376 }
377
378 /**
379 * Loads event provider
380 *
381 * @param string $id Module ID
382 *
383 * @return void
384 */
385 public function loadEventProviders($id)
386 {
387 global $emitter;
388
389 $providerClassName = '\\' . $this->getNamespace($id) . '\\' . 'PluginEventProvider';
390 if (class_exists($providerClassName)
391 && method_exists($providerClassName, 'provideListeners')
392 ) {
393 $emitter->useListenerProvider(new $providerClassName());
394 }
395 }
396
397 /**
398 * Returns all modules associative array or only one module if <var>$id</var>
399 * is present.
400 *
401 * @param string $id Optionnal module ID
402 *
403 * @return <b>array</b>
404 */
405 public function getModules($id = null)
406 {
407 if ($id && isset($this->modules[$id])) {
408 return $this->modules[$id];
409 }
410 return $this->modules;
411 }
412
413 /**
414 * Returns true if the module with ID <var>$id</var> exists.
415 *
416 * @param string $id Module ID
417 *
418 * @return <b>boolean</b>
419 */
420 public function moduleExists($id)
421 {
422 return isset($this->modules[$id]);
423 }
424
425 /**
426 * Returns all disabled modules in an array
427 *
428 * @return <b>array</b>
429 */
430 public function getDisabledModules()
431 {
432 return $this->disabled;
433 }
434
435 /**
436 * Returns root path for module with ID <var>$id</var>.
437 *
438 * @param string $id Module ID
439 *
440 * @return <b>string</b>
441 */
442 public function moduleRoot($id)
443 {
444 return $this->moduleInfo($id, 'root');
445 }
446
447 /**
448 * Returns a module information that could be:
449 * - root
450 * - name
451 * - desc
452 * - author
453 * - version
454 * - date
455 * - permissions
456 * - priority
457 *
458 * @param string $id Module ID
459 * @param string $info Information to retrieve
460 *
461 * @return module's information
462 */
463 public function moduleInfo($id, $info)
464 {
465 return isset($this->modules[$id][$info]) ? $this->modules[$id][$info] : null;
466 }
467
468 /**
469 * Search and load menu templates from plugins.
470 * Also sets the web path to the plugin with the var "galette_[plugin-name]_path"
471 *
472 * @param Smarty $tpl Smarty template
473 *
474 * @return void
475 */
476 public function getMenus($tpl)
477 {
478 $modules = $this->getModules();
479 foreach (array_keys($this->getModules()) as $r) {
480 $menu_path = $this->getTemplatesPath($r) . '/menu.tpl';
481 if ($tpl->templateExists($menu_path)) {
482 $name2path = strtolower(
483 str_replace(' ', '_', $modules[$r]['name'])
484 );
485 $tpl->assign(
486 'galette_' . $name2path . '_path',
487 'plugins/' . $r . '/'
488 );
489 $tpl->display($menu_path);
490 }
491 }
492 }
493
494 /**
495 * Search and load public menu templates from plugins.
496 * Also sets the web path to the plugin with the var "galette_[plugin-name]_path"
497 *
498 * @param Smarty $tpl Smarty template
499 * @param boolean $public_page Called from a public page
500 *
501 * @return void
502 */
503 public function getPublicMenus($tpl, $public_page = false)
504 {
505 $modules = $this->getModules();
506 foreach (array_keys($this->getModules()) as $r) {
507 $menu_path = $this->getTemplatesPath($r) . '/public_menu.tpl';
508 if ($tpl->templateExists($menu_path)) {
509 $name2path = strtolower(
510 str_replace(' ', '_', $modules[$r]['name'])
511 );
512 $tpl->assign(
513 'galette_' . $name2path . '_path',
514 'plugins/' . $r . '/'
515 );
516 $tpl->assign(
517 'public_page',
518 $public_page
519 );
520 $tpl->display($menu_path);
521 }
522 }
523 }
524
525 /**
526 * Get plugins dashboard entries.
527 *
528 * @param Smarty $tpl Smarty template
529 *
530 * @return void
531 */
532 public function getDashboard($tpl)
533 {
534 $modules = $this->getModules();
535 foreach (array_keys($this->getModules()) as $r) {
536 $dash_path = $this->getTemplatesPath($r) . '/dashboard.tpl';
537 if ($tpl->templateExists($dash_path)) {
538 $name2path = strtolower(
539 str_replace(' ', '_', $modules[$r]['name'])
540 );
541 $tpl->display($dash_path);
542 }
543 }
544 }
545
546 /**
547 * Get plugins single member dashboard entries.
548 *
549 * @param Smarty $tpl Smarty template
550 *
551 * @return void
552 */
553 public function getMemberDashboard($tpl)
554 {
555 $modules = $this->getModules();
556 foreach (array_keys($this->getModules()) as $r) {
557 $dash_path = $this->getTemplatesPath($r) . '/dashboard_member.tpl';
558 if ($tpl->templateExists($dash_path)) {
559 $name2path = strtolower(
560 str_replace(' ', '_', $modules[$r]['name'])
561 );
562 $tpl->display($dash_path);
563 }
564 }
565 }
566
567 /**
568 * Sort modules
569 *
570 * @param array $a A module
571 * @param array $b Another module
572 *
573 * @return 1 if a has the highest priority, -1 otherwise
574 */
575 private function sortModules($a, $b)
576 {
577 if ($a['priority'] == $b['priority']) {
578 return strcasecmp($a['name'], $b['name']);
579 }
580
581 return ($a['priority'] < $b['priority']) ? -1 : 1;
582 }
583
584 /**
585 * Get the templates path for a specified module
586 *
587 * @param string $id Module's ID
588 *
589 * @return Concatenated templates path for requested module
590 */
591 public function getTemplatesPath($id)
592 {
593 return $this->moduleRoot($id) . '/templates/' . $this->preferences->pref_theme;
594 }
595
596 /**
597 * Get the templates path for a specified module name
598 *
599 * @param string $name Module's name
600 *
601 * @return Concatenated templates path for requested module
602 */
603 public function getTemplatesPathFromName($name)
604 {
605 $id = null;
606 foreach (array_keys($this->getModules()) as $r) {
607 $mod = $this->getModules($r);
608 if ($mod['name'] === $name) {
609 return $this->getTemplatesPath($r);
610 }
611 }
612 }
613
614 /**
615 * For each module, returns the headers.tpl full path, if present.
616 *
617 * @return array of headers to include for all modules
618 */
619 public function getTplHeaders()
620 {
621 $_headers = array();
622 foreach (array_keys($this->modules) as $key) {
623 $headers_path = $this->getTemplatesPath($key) . '/headers.tpl';
624 if (file_exists($headers_path)) {
625 $_headers[$key] = $headers_path;
626 }
627 }
628 return $_headers;
629 }
630
631 /**
632 * For each module, return the adh_actions.tpl full path, if present.
633 *
634 * @return array of adherent actions to include on member list for all modules
635 */
636 public function getTplAdhActions()
637 {
638 $_actions = array();
639 foreach (array_keys($this->modules) as $key) {
640 $actions_path = $this->getTemplatesPath($key) . '/adh_actions.tpl';
641 if (file_exists($actions_path)) {
642 $_actions['actions_' . $key] = $actions_path;
643 }
644 }
645 return $_actions;
646 }
647
648 /**
649 * For each module, return the adh_batch_action.tpl full path, if present.
650 *
651 * @return array of adherents batch actions to include on members list
652 * all modules
653 */
654 public function getTplAdhBatchActions()
655 {
656 $_actions = array();
657 foreach (array_keys($this->modules) as $key) {
658 $actions_path = $this->getTemplatesPath($key) . '/adh_batch_action.tpl';
659 if (file_exists($actions_path)) {
660 $_actions['batch_action_' . $key] = $actions_path;
661 }
662 }
663 return $_actions;
664 }
665
666 /**
667 * For each module, return the adh_fiche_action.tpl full path, if present.
668 *
669 * @return array of adherent actions to include on member detailled view for
670 * all modules
671 */
672 public function getTplAdhDetailledActions()
673 {
674 $_actions = array();
675 foreach (array_keys($this->modules) as $key) {
676 $actions_path = $this->getTemplatesPath($key) . '/adh_fiche_action.tpl';
677 if (file_exists($actions_path)) {
678 $_actions['det_actions_' . $key] = $actions_path;
679 }
680 }
681 return $_actions;
682 }
683
684 /**
685 * For each module, gets templates assignements ; and replace some path variables
686 *
687 * @return array of Smarty templates assignement for all modules
688 */
689 public function getTplAssignments()
690 {
691 $_assign = array();
692 foreach ($this->modules as $key => $module) {
693 if (isset($module['tpl_assignments'])) {
694 foreach ($module['tpl_assignments'] as $k => $v) {
695 $v = str_replace(
696 '__plugin_dir__',
697 'plugins/' . $key . '/',
698 $v
699 );
700 $v = str_replace(
701 '__plugin_include_dir__',
702 'plugins/' . $key . '/includes/',
703 $v
704 );
705 $v = str_replace(
706 '__plugin_templates_dir__',
707 'plugins/' . $key . '/templates/' .
708 $this->preferences->pref_theme . '/',
709 $v
710 );
711 $_assign[$k] = $v;
712 }
713 }
714 }
715 return $_assign;
716 }
717
718 /**
719 * Does module needs a database?
720 *
721 * @param string $id Module's ID
722 *
723 * @return boolean
724 */
725 public function needsDatabase($id)
726 {
727 if (isset($this->modules[$id])) {
728 $d = $this->modules[$id]['root'] . '/scripts/';
729 if (file_exists($d)) {
730 return true;
731 } else {
732 return false;
733 }
734 } else {
735 throw new \Exception(_T("Module does not exists!"));
736 }
737 }
738
739 /**
740 * Override preferences from plugin
741 *
742 * @param string $id Module ID
743 *
744 * @return void
745 */
746 public function overridePrefs($id)
747 {
748 $overridables = ['pref_adhesion_form'];
749
750 $f = $this->modules[$id]['root'] . '/_preferences.php';
751 if (file_exists($f)) {
752 include_once $f;
753 if (isset($_preferences)) {
754 foreach ($_preferences as $k => $v) {
755 if (in_array($k, $overridables)) {
756 $this->preferences->$k = $v;
757 }
758 }
759 }
760 }
761 }
762
763 /**
764 * Get plugins routes ACLs
765 *
766 * @return array
767 */
768 public function getAcls()
769 {
770 $acls = [];
771 foreach ($this->modules as $module) {
772 $acls = array_merge($acls, $module['acls']);
773 }
774 return $acls;
775 }
776
777 /**
778 * Retrieve a file that should be publically exposed
779 *
780 * @param int $id Module id
781 * @param string $path File path
782 *
783 * @return string
784 */
785 public function getFile($id, $path)
786 {
787 if (isset($this->modules[$id])) {
788 $file = $this->modules[$id]['root'] . '/webroot/' . $path;
789 if (file_exists($file)) {
790 return $file;
791 } else {
792 throw new \RuntimeException(_T("File not found!"));
793 }
794 } else {
795 throw new \Exception(_T("Module does not exists!"));
796 }
797 }
798
799 /**
800 * Set a module as disabled
801 *
802 * @param integer $cause Cause (one of Plugins::DISABLED_* constants)
803 *
804 * @return void
805 */
806 private function setDisabled($cause)
807 {
808 $this->disabled[$this->id] = array(
809 'root' => $this->mroot,
810 'root_writable' => is_writable($this->mroot),
811 'cause' => $cause
812 );
813 $this->id = null;
814 $this->mroot = null;
815 }
816
817 /**
818 * Get module namespace
819 *
820 * @param integer $id Module ID
821 *
822 * @return string
823 */
824 public function getNamespace($id)
825 {
826 return str_replace(' ', '', $this->modules[$id]['name']);
827 }
828 }