plugin.lib.php 21 KB

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