plugin.lib.php 22 KB

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