plugin.lib.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. <?php
  2. /* See license terms in /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Class AppPlugin.
  6. */
  7. class AppPlugin
  8. {
  9. public $plugin_regions = [
  10. 'main_top',
  11. 'main_bottom',
  12. 'login_top',
  13. 'login_bottom',
  14. 'menu_top',
  15. 'menu_bottom',
  16. 'content_top',
  17. 'content_bottom',
  18. 'header_main',
  19. 'header_center',
  20. 'header_left',
  21. 'header_right',
  22. 'pre_footer',
  23. 'footer_left',
  24. 'footer_center',
  25. 'footer_right',
  26. 'menu_administrator',
  27. 'course_tool_plugin',
  28. ];
  29. public $installedPluginListName = [];
  30. public $installedPluginListObject = [];
  31. private static $instance;
  32. /**
  33. * Constructor.
  34. */
  35. public function __construct()
  36. {
  37. }
  38. /**
  39. * @return AppPlugin
  40. */
  41. public static function getInstance()
  42. {
  43. if (!isset(self::$instance)) {
  44. self::$instance = new self();
  45. }
  46. return self::$instance;
  47. }
  48. /**
  49. * Read plugin from path.
  50. *
  51. * @return array
  52. */
  53. public function read_plugins_from_path()
  54. {
  55. /* We scan the plugin directory. Each folder is a potential plugin. */
  56. $pluginPath = api_get_path(SYS_PLUGIN_PATH);
  57. $plugins = [];
  58. $handle = @opendir($pluginPath);
  59. while (false !== ($file = readdir($handle))) {
  60. if ($file != '.' && $file != '..' && is_dir(api_get_path(SYS_PLUGIN_PATH).$file)) {
  61. $plugins[] = $file;
  62. }
  63. }
  64. @closedir($handle);
  65. sort($plugins);
  66. return $plugins;
  67. }
  68. /**
  69. * @return array
  70. */
  71. public function getInstalledPluginListName()
  72. {
  73. if (empty($this->installedPluginListName)) {
  74. $this->installedPluginListName = $this->getInstalledPlugins();
  75. }
  76. return $this->installedPluginListName;
  77. }
  78. /**
  79. * @return array List of Plugin
  80. */
  81. public function getInstalledPluginListObject()
  82. {
  83. if (empty($this->installedPluginListObject)) {
  84. $this->setInstalledPluginListObject();
  85. }
  86. return $this->installedPluginListObject;
  87. }
  88. public function setInstalledPluginListObject()
  89. {
  90. $pluginListName = $this->getInstalledPluginListName();
  91. $pluginList = [];
  92. if (!empty($pluginListName)) {
  93. foreach ($pluginListName as $pluginName) {
  94. $pluginInfo = $this->getPluginInfo($pluginName, true);
  95. if (isset($pluginInfo['plugin_class'])) {
  96. $pluginList[] = $pluginInfo['plugin_class']::create();
  97. }
  98. }
  99. }
  100. $this->installedPluginListObject = $pluginList;
  101. }
  102. /**
  103. * @param string $plugin
  104. *
  105. * @return bool
  106. */
  107. public function isInstalled($plugin)
  108. {
  109. $list = self::getInstalledPlugins(false);
  110. return in_array($plugin, $list);
  111. }
  112. /**
  113. * @deprecated
  114. */
  115. public function get_installed_plugins($fromDatabase = true)
  116. {
  117. return $this->getInstalledPlugins($fromDatabase);
  118. }
  119. /**
  120. * @param bool $fromDatabase
  121. *
  122. * @return array
  123. */
  124. public function getInstalledPlugins($fromDatabase = true)
  125. {
  126. static $installedPlugins = null;
  127. if ($fromDatabase === false) {
  128. if (is_array($installedPlugins)) {
  129. return $installedPlugins;
  130. }
  131. }
  132. if ($fromDatabase || $installedPlugins === null) {
  133. $installedPlugins = [];
  134. $plugins = api_get_settings_params(
  135. [
  136. 'variable = ? AND selected_value = ? AND category = ? ' => ['status', 'installed', 'Plugins'],
  137. ]
  138. );
  139. if (!empty($plugins)) {
  140. foreach ($plugins as $row) {
  141. $installedPlugins[$row['subkey']] = true;
  142. }
  143. $installedPlugins = array_keys($installedPlugins);
  144. }
  145. }
  146. return $installedPlugins;
  147. }
  148. /**
  149. * @param string $pluginName
  150. * @param int $urlId
  151. */
  152. public function install($pluginName, $urlId = null)
  153. {
  154. $urlId = (int) $urlId;
  155. if (empty($urlId)) {
  156. $urlId = api_get_current_access_url_id();
  157. }
  158. api_add_setting(
  159. 'installed',
  160. 'status',
  161. $pluginName,
  162. 'setting',
  163. 'Plugins',
  164. $pluginName,
  165. '',
  166. '',
  167. '',
  168. $urlId,
  169. 1
  170. );
  171. $pluginPath = api_get_path(SYS_PLUGIN_PATH).$pluginName.'/install.php';
  172. if (is_file($pluginPath) && is_readable($pluginPath)) {
  173. // Execute the install procedure.
  174. require $pluginPath;
  175. }
  176. }
  177. /**
  178. * @param string $pluginName
  179. * @param int $urlId
  180. */
  181. public function uninstall($pluginName, $urlId = null)
  182. {
  183. $urlId = (int) $urlId;
  184. if (empty($urlId)) {
  185. $urlId = api_get_current_access_url_id();
  186. }
  187. // First call the custom uninstall to allow full access to global settings
  188. $pluginPath = api_get_path(SYS_PLUGIN_PATH).$pluginName.'/uninstall.php';
  189. if (is_file($pluginPath) && is_readable($pluginPath)) {
  190. // Execute the uninstall procedure.
  191. require $pluginPath;
  192. }
  193. // Second remove all remaining global settings
  194. api_delete_settings_params(
  195. ['category = ? AND access_url = ? AND subkey = ? ' => ['Plugins', $urlId, $pluginName]]
  196. );
  197. }
  198. /**
  199. * @param string $pluginName
  200. *
  201. * @return array
  202. */
  203. public function get_areas_by_plugin($pluginName)
  204. {
  205. $result = api_get_settings('Plugins');
  206. $areas = [];
  207. foreach ($result as $row) {
  208. if ($pluginName == $row['selected_value']) {
  209. $areas[] = $row['variable'];
  210. }
  211. }
  212. return $areas;
  213. }
  214. /**
  215. * @param string $pluginName
  216. *
  217. * @return bool
  218. */
  219. public function is_valid_plugin($pluginName)
  220. {
  221. if (is_dir(api_get_path(SYS_PLUGIN_PATH).$pluginName)) {
  222. if (is_file(api_get_path(SYS_PLUGIN_PATH).$pluginName.'/index.php')) {
  223. return true;
  224. }
  225. }
  226. return false;
  227. }
  228. /**
  229. * @return array
  230. */
  231. public function getPluginRegions()
  232. {
  233. sort($this->plugin_regions);
  234. return $this->plugin_regions;
  235. }
  236. /**
  237. * @param array $pluginRegionList
  238. * @param string $region
  239. * @param Twig_Environment $template
  240. * @param bool $forced
  241. *
  242. * @return string|null
  243. */
  244. public function loadRegion($pluginName, $region, $template, $forced = false)
  245. {
  246. if ($region == 'course_tool_plugin') {
  247. return '';
  248. }
  249. ob_start();
  250. $this->getAllPluginContentsByRegion($pluginName, $region, $template, $forced);
  251. $content = ob_get_contents();
  252. ob_end_clean();
  253. return $content;
  254. }
  255. /**
  256. * Loads the translation files inside a plugin if exists.
  257. * It loads by default english see the hello world plugin.
  258. *
  259. * @param string $plugin_name
  260. *
  261. * @todo add caching
  262. */
  263. public function load_plugin_lang_variables($plugin_name)
  264. {
  265. $language_interface = api_get_interface_language();
  266. $root = api_get_path(SYS_PLUGIN_PATH);
  267. $strings = null;
  268. // 1. Loading english if exists
  269. $english_path = $root.$plugin_name.'/lang/english.php';
  270. if (is_readable($english_path)) {
  271. include $english_path;
  272. foreach ($strings as $key => $string) {
  273. $GLOBALS[$key] = $string;
  274. }
  275. }
  276. // 2. Loading the system language
  277. if ($language_interface != 'english') {
  278. $path = $root.$plugin_name."/lang/$language_interface.php";
  279. if (is_readable($path)) {
  280. include $path;
  281. if (!empty($strings)) {
  282. foreach ($strings as $key => $string) {
  283. $GLOBALS[$key] = $string;
  284. }
  285. }
  286. } else {
  287. /*$interfaceLanguageId = api_get_language_id($language_interface);
  288. $interfaceLanguageInfo = api_get_language_info($interfaceLanguageId);
  289. $languageParentId = intval($interfaceLanguageInfo['parent_id']);
  290. if ($languageParentId > 0) {
  291. $languageParentInfo = api_get_language_info($languageParentId);
  292. $languageParentFolder = $languageParentInfo['dokeos_folder'];
  293. $parentPath = "{$root}{$plugin_name}/lang/{$languageParentFolder}.php";
  294. if (is_readable($parentPath)) {
  295. include $parentPath;
  296. if (!empty($strings)) {
  297. foreach ($strings as $key => $string) {
  298. $this->strings[$key] = $string;
  299. }
  300. }
  301. }
  302. }*/
  303. }
  304. }
  305. }
  306. /**
  307. * @param array $_plugins
  308. * @param string $region
  309. * @param Twig_Environment $template
  310. * @param bool $forced
  311. *
  312. * @return bool
  313. *
  314. * @todo improve this function
  315. */
  316. public function getAllPluginContentsByRegion($plugin_name, $region, $template, $forced = false)
  317. {
  318. // The plugin_info variable is available inside the plugin index
  319. $plugin_info = $this->getPluginInfo($plugin_name, $forced);
  320. // We also know where the plugin is
  321. $plugin_info['current_region'] = $region;
  322. // Loading the plugin/XXX/index.php file
  323. $plugin_file = api_get_path(SYS_PLUGIN_PATH)."$plugin_name/index.php";
  324. if (file_exists($plugin_file)) {
  325. //Loading the lang variables of the plugin if exists
  326. self::load_plugin_lang_variables($plugin_name);
  327. // Printing the plugin index.php file
  328. require $plugin_file;
  329. // If the variable $_template is set we assign those values to be accessible in Twig
  330. if (isset($_template)) {
  331. $_template['plugin_info'] = $plugin_info;
  332. } else {
  333. $_template = [];
  334. $_template['plugin_info'] = $plugin_info;
  335. }
  336. // Setting the plugin info available in the template if exists.
  337. //$template->addGlobal($plugin_name, $_template);
  338. // Loading the Twig template plugin files if exists
  339. $templateList = [];
  340. if (isset($plugin_info) && isset($plugin_info['templates'])) {
  341. $templateList = $plugin_info['templates'];
  342. }
  343. if (!empty($templateList)) {
  344. foreach ($templateList as $pluginTemplate) {
  345. if (!empty($pluginTemplate)) {
  346. $templatePluginFile = "$plugin_name/$pluginTemplate"; // for twig
  347. //$template->render($templatePluginFile, []);
  348. }
  349. }
  350. }
  351. }
  352. return true;
  353. }
  354. /**
  355. * Loads plugin info.
  356. *
  357. * @staticvar array $plugin_data
  358. *
  359. * @param string $pluginName
  360. * @param bool $forced load from DB or from the static array
  361. *
  362. * @return array
  363. *
  364. * @todo filter setting_form
  365. */
  366. public function getPluginInfo($pluginName, $forced = false)
  367. {
  368. //$pluginData = Session::read('plugin_data');
  369. if (0) {
  370. //if (isset($pluginData[$pluginName]) && $forced == false) {
  371. return $pluginData[$pluginName];
  372. } else {
  373. $plugin_file = api_get_path(SYS_PLUGIN_PATH)."$pluginName/plugin.php";
  374. $plugin_info = [];
  375. if (file_exists($plugin_file)) {
  376. require $plugin_file;
  377. }
  378. // @todo check if settings are already added
  379. // Extra options
  380. $plugin_settings = api_get_settings_params(
  381. [
  382. 'subkey = ? AND category = ? AND type = ? AND access_url = ?' => [
  383. $pluginName,
  384. 'Plugins',
  385. 'setting',
  386. api_get_current_access_url_id(),
  387. ],
  388. ]
  389. );
  390. $settings_filtered = [];
  391. foreach ($plugin_settings as $item) {
  392. if (!empty($item['selected_value'])) {
  393. //if (unserialize($item['selected_value']) !== false) {
  394. //$item['selected_value'] = unserialize($item['selected_value']);
  395. //}
  396. }
  397. $settings_filtered[$item['variable']] = $item['selected_value'];
  398. }
  399. $plugin_info['settings'] = $settings_filtered;
  400. $pluginData[$pluginName] = $plugin_info;
  401. //Session::write('plugin_data', $pluginData);
  402. return $plugin_info;
  403. }
  404. }
  405. /**
  406. * Get the template list.
  407. *
  408. * @param string $pluginName
  409. *
  410. * @return bool
  411. */
  412. public function get_templates_list($pluginName)
  413. {
  414. $plugin_info = $this->getPluginInfo($pluginName);
  415. if (isset($plugin_info) && isset($plugin_info['templates'])) {
  416. return $plugin_info['templates'];
  417. }
  418. return false;
  419. }
  420. /**
  421. * Remove all regions of an specific plugin.
  422. *
  423. * @param string $plugin
  424. */
  425. public function removeAllRegions($plugin)
  426. {
  427. if (!empty($plugin)) {
  428. api_delete_settings_params(
  429. [
  430. 'category = ? AND type = ? AND access_url = ? AND subkey = ? ' => [
  431. 'Plugins',
  432. 'region',
  433. api_get_current_access_url_id(),
  434. $plugin,
  435. ],
  436. ]
  437. );
  438. }
  439. }
  440. /**
  441. * Add a plugin to a region.
  442. *
  443. * @param string $plugin
  444. * @param string $region
  445. */
  446. public function add_to_region($plugin, $region)
  447. {
  448. api_add_setting(
  449. $plugin,
  450. $region,
  451. $plugin,
  452. 'region',
  453. 'Plugins',
  454. $plugin,
  455. '',
  456. '',
  457. '',
  458. api_get_current_access_url_id(),
  459. 1
  460. );
  461. }
  462. /**
  463. * @param int $courseId
  464. */
  465. public function install_course_plugins($courseId)
  466. {
  467. $pluginList = $this->getInstalledPluginListObject();
  468. if (!empty($pluginList)) {
  469. /** @var Plugin $obj */
  470. foreach ($pluginList as $obj) {
  471. $pluginName = $obj->get_name();
  472. $plugin_path = api_get_path(SYS_PLUGIN_PATH).$pluginName.'/plugin.php';
  473. if (file_exists($plugin_path)) {
  474. require $plugin_path;
  475. if (isset($plugin_info) && isset($plugin_info['plugin_class']) && $obj->isCoursePlugin) {
  476. $obj->course_install($courseId);
  477. }
  478. }
  479. }
  480. }
  481. }
  482. /**
  483. * Trigger for Plugin::doWhenDeleting[Item] functions.
  484. *
  485. * @param string $itemType
  486. * @param int $itemId
  487. */
  488. public function performActionsWhenDeletingItem($itemType, $itemId)
  489. {
  490. $pluginList = $this->getInstalledPluginListObject();
  491. if (empty($pluginList)) {
  492. return;
  493. }
  494. /** @var Plugin $pluginObj */
  495. foreach ($pluginList as $pluginObj) {
  496. switch ($itemType) {
  497. case 'course':
  498. $pluginObj->doWhenDeletingCourse($itemId);
  499. break;
  500. case 'session':
  501. $pluginObj->doWhenDeletingSession($itemId);
  502. break;
  503. case 'user':
  504. $pluginObj->doWhenDeletingUser($itemId);
  505. break;
  506. }
  507. }
  508. }
  509. /**
  510. * Add the course settings to the course settings form.
  511. *
  512. * @param FormValidator $form
  513. */
  514. public function add_course_settings_form($form)
  515. {
  516. $pluginList = $this->getInstalledPluginListObject();
  517. /** @var Plugin $obj */
  518. foreach ($pluginList as $obj) {
  519. $pluginName = $obj->get_name();
  520. $pluginTitle = $obj->get_title();
  521. if (!empty($obj->course_settings)) {
  522. if (is_file(api_get_path(SYS_CODE_PATH).'img/icons/'.ICON_SIZE_SMALL.'/'.$pluginName.'.png')) {
  523. $icon = Display::return_icon(
  524. $pluginName.'.png',
  525. Security::remove_XSS($pluginTitle),
  526. '',
  527. ICON_SIZE_SMALL
  528. );
  529. } else {
  530. $icon = Display::return_icon(
  531. 'plugins.png',
  532. Security::remove_XSS($pluginTitle),
  533. '',
  534. ICON_SIZE_SMALL
  535. );
  536. }
  537. $form->addHtml('<div class="panel panel-default">');
  538. $form->addHtml('
  539. <div class="panel-heading" role="tab" id="heading-'.$pluginName.'-settings">
  540. <h4 class="panel-title">
  541. <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse-'.$pluginName.'-settings" aria-expanded="false" aria-controls="collapse-'.$pluginName.'-settings">
  542. ');
  543. $form->addHtml($icon.' '.$pluginTitle);
  544. $form->addHtml('
  545. </a>
  546. </h4>
  547. </div>
  548. ');
  549. $form->addHtml('
  550. <div id="collapse-'.$pluginName.'-settings" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading-'.$pluginName.'-settings">
  551. <div class="panel-body">
  552. ');
  553. $groups = [];
  554. foreach ($obj->course_settings as $setting) {
  555. if ($obj->validateCourseSetting($setting['name']) === false) {
  556. continue;
  557. }
  558. if ($setting['type'] != 'checkbox') {
  559. $form->addElement($setting['type'], $setting['name'], $obj->get_lang($setting['name']));
  560. } else {
  561. $element = &$form->createElement(
  562. $setting['type'],
  563. $setting['name'],
  564. '',
  565. $obj->get_lang($setting['name'])
  566. );
  567. if (isset($setting['init_value']) && $setting['init_value'] == 1) {
  568. $element->setChecked(true);
  569. }
  570. $form->addElement($element);
  571. if (isset($setting['group'])) {
  572. $groups[$setting['group']][] = $element;
  573. }
  574. }
  575. }
  576. foreach ($groups as $k => $v) {
  577. $form->addGroup($groups[$k], $k, [$obj->get_lang($k)]);
  578. }
  579. $form->addButtonSave(get_lang('Save settings'));
  580. $form->addHtml('
  581. </div>
  582. </div>
  583. ');
  584. $form->addHtml('</div>');
  585. }
  586. }
  587. }
  588. /**
  589. * Get all course settings from all installed plugins.
  590. *
  591. * @return array
  592. */
  593. public function getAllPluginCourseSettings()
  594. {
  595. $pluginList = $this->getInstalledPluginListObject();
  596. /** @var Plugin $obj */
  597. $courseSettings = [];
  598. if (!empty($pluginList)) {
  599. foreach ($pluginList as $obj) {
  600. $pluginCourseSetting = $obj->getCourseSettings();
  601. $courseSettings = array_merge($courseSettings, $pluginCourseSetting);
  602. }
  603. }
  604. return $courseSettings;
  605. }
  606. /**
  607. * When saving the plugin values in the course settings, check whether
  608. * a callback method should be called and send it the updated settings.
  609. *
  610. * @param array $values The new settings the user just saved
  611. */
  612. public function saveCourseSettingsHook($values)
  613. {
  614. $pluginList = $this->getInstalledPluginListObject();
  615. /** @var Plugin $obj */
  616. foreach ($pluginList as $obj) {
  617. $settings = $obj->getCourseSettings();
  618. $subValues = [];
  619. if (!empty($settings)) {
  620. foreach ($settings as $v) {
  621. if (isset($values[$v])) {
  622. $subValues[$v] = $values[$v];
  623. }
  624. }
  625. }
  626. if (!empty($subValues)) {
  627. $obj->course_settings_updated($subValues);
  628. }
  629. }
  630. }
  631. /**
  632. * Get first SMS plugin name.
  633. *
  634. * @return string|bool
  635. */
  636. public function getSMSPluginName()
  637. {
  638. $installedPluginsList = $this->getInstalledPluginListObject();
  639. foreach ($installedPluginsList as $installedPlugin) {
  640. if ($installedPlugin->isMailPlugin) {
  641. return get_class($installedPlugin);
  642. }
  643. }
  644. return false;
  645. }
  646. /**
  647. * @return SmsPluginLibraryInterface
  648. */
  649. public function getSMSPluginLibrary()
  650. {
  651. $className = $this->getSMSPluginName();
  652. $className = str_replace('Plugin', '', $className);
  653. if (class_exists($className)) {
  654. return new $className();
  655. }
  656. return false;
  657. }
  658. /**
  659. * @param array $pluginRegionList
  660. * @param string $pluginRegion
  661. * @param Twig_Environment $twig
  662. */
  663. public function setPluginRegion($pluginRegionList, $pluginRegion, $twig)
  664. {
  665. $regionContent = $this->loadRegion(
  666. $pluginRegionList,
  667. $pluginRegion,
  668. $twig,
  669. true //$this->force_plugin_load
  670. );
  671. //$twig->addGlobal('plugin_'.$pluginRegion, $regionContent);
  672. }
  673. }