plugin.lib.php 22 KB

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