plugin.lib.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. <?php
  2. /* See license terms in /license.txt */
  3. /**
  4. * Class AppPlugin
  5. */
  6. class AppPlugin
  7. {
  8. public $plugin_regions = array(
  9. 'main_top',
  10. 'main_bottom',
  11. 'login_top',
  12. 'login_bottom',
  13. 'menu_top',
  14. 'menu_bottom',
  15. 'content_top',
  16. 'content_bottom',
  17. 'header_main',
  18. 'header_center',
  19. 'header_left',
  20. 'header_right',
  21. 'pre_footer',
  22. 'footer_left',
  23. 'footer_center',
  24. 'footer_right',
  25. 'menu_administrator',
  26. 'course_tool_plugin'
  27. );
  28. public $installedPluginListName = array();
  29. public $installedPluginListObject = array();
  30. /**
  31. * Constructor
  32. */
  33. public function __construct()
  34. {
  35. }
  36. /**
  37. * Read plugin from path
  38. * @return array
  39. */
  40. public function read_plugins_from_path()
  41. {
  42. /* We scan the plugin directory. Each folder is a potential plugin. */
  43. $pluginPath = api_get_path(SYS_PLUGIN_PATH);
  44. $plugins = array();
  45. $handle = @opendir($pluginPath);
  46. while (false !== ($file = readdir($handle))) {
  47. if ($file != '.' && $file != '..' && is_dir(api_get_path(SYS_PLUGIN_PATH).$file)) {
  48. $plugins[] = $file;
  49. }
  50. }
  51. @closedir($handle);
  52. sort($plugins);
  53. return $plugins;
  54. }
  55. /**
  56. * @return array
  57. */
  58. public function get_installed_plugins_by_region()
  59. {
  60. $plugins = array();
  61. /* We retrieve all the active plugins. */
  62. $result = api_get_settings('Plugins');
  63. if (!empty($result)) {
  64. foreach ($result as $row) {
  65. $plugins[$row['variable']][] = $row['selected_value'];
  66. }
  67. }
  68. return $plugins;
  69. }
  70. /**
  71. * @return array
  72. */
  73. public function getInstalledPluginListName()
  74. {
  75. if (empty($this->installedPluginListName)) {
  76. $this->installedPluginListName = $this->get_installed_plugins();
  77. }
  78. return $this->installedPluginListName;
  79. }
  80. /**
  81. * @return array List of Plugin
  82. */
  83. public function getInstalledPluginListObject()
  84. {
  85. if (empty($this->installedPluginListObject)) {
  86. $this->setInstalledPluginListObject();
  87. }
  88. return $this->installedPluginListObject;
  89. }
  90. /**
  91. * @return array
  92. */
  93. public function setInstalledPluginListObject()
  94. {
  95. $pluginListName = $this->getInstalledPluginListName();
  96. $pluginList = array();
  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 = array();
  113. $plugins = api_get_settings_params(
  114. array(
  115. "variable = ? AND selected_value = ? AND category = ? " => array('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. null,
  145. null,
  146. null,
  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. array('category = ? AND access_url = ? AND subkey = ? ' => array('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 = array();
  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 string $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 = array();
  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 = array();
  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. static $plugin_data = array();
  360. if (isset($plugin_data[$plugin_name]) && $forced == false) {
  361. return $plugin_data[$plugin_name];
  362. } else {
  363. $plugin_file = api_get_path(SYS_PLUGIN_PATH)."$plugin_name/plugin.php";
  364. $plugin_info = array();
  365. if (file_exists($plugin_file)) {
  366. require $plugin_file;
  367. }
  368. // Extra options
  369. $plugin_settings = api_get_settings_params(
  370. array(
  371. "subkey = ? AND category = ? AND type = ? " => array($plugin_name, 'Plugins', 'setting')
  372. )
  373. );
  374. $settings_filtered = array();
  375. foreach ($plugin_settings as $item) {
  376. $settings_filtered[$item['variable']] = $item['selected_value'];
  377. }
  378. $plugin_info['settings'] = $settings_filtered;
  379. $plugin_data[$plugin_name] = $plugin_info;
  380. return $plugin_info;
  381. }
  382. }
  383. /**
  384. * Get the template list
  385. * @param string $pluginName
  386. *
  387. * @return bool
  388. */
  389. public function get_templates_list($pluginName)
  390. {
  391. $plugin_info = $this->getPluginInfo($pluginName);
  392. if (isset($plugin_info) && isset($plugin_info['templates'])) {
  393. return $plugin_info['templates'];
  394. } else {
  395. return false;
  396. }
  397. }
  398. /**
  399. * Remove all regions of an specific plugin
  400. */
  401. public function remove_all_regions($plugin)
  402. {
  403. $access_url_id = api_get_current_access_url_id();
  404. if (!empty($plugin)) {
  405. api_delete_settings_params(
  406. array(
  407. 'category = ? AND type = ? AND access_url = ? AND subkey = ? ' => array('Plugins', 'region', $access_url_id, $plugin)
  408. )
  409. );
  410. }
  411. }
  412. /**
  413. * Add a plugin to a region
  414. * @param string $plugin
  415. * @param string $region
  416. */
  417. public function add_to_region($plugin, $region)
  418. {
  419. $access_url_id = api_get_current_access_url_id();
  420. api_add_setting(
  421. $plugin,
  422. $region,
  423. $plugin,
  424. 'region',
  425. 'Plugins',
  426. $plugin,
  427. null,
  428. null,
  429. null,
  430. $access_url_id,
  431. 1
  432. );
  433. }
  434. /**
  435. * @param int $courseId
  436. */
  437. public function install_course_plugins($courseId)
  438. {
  439. $pluginList = $this->getInstalledPluginListObject();
  440. if (!empty($pluginList)) {
  441. /** @var Plugin $obj */
  442. foreach ($pluginList as $obj) {
  443. $pluginName = $obj->get_name();
  444. $plugin_path = api_get_path(SYS_PLUGIN_PATH).$pluginName.'/plugin.php';
  445. if (file_exists($plugin_path)) {
  446. require $plugin_path;
  447. if (isset($plugin_info) && isset($plugin_info['plugin_class']) && $obj->isCoursePlugin) {
  448. $obj->course_install($courseId);
  449. }
  450. }
  451. }
  452. }
  453. }
  454. /**
  455. * Add the course settings to the course settings form
  456. * @param FormValidator $form
  457. */
  458. public function add_course_settings_form($form)
  459. {
  460. $pluginList = $this->getInstalledPluginListObject();
  461. /** @var Plugin $obj */
  462. foreach ($pluginList as $obj) {
  463. $plugin_name = $obj->get_name();
  464. $pluginTitle = $obj->get_title();
  465. if (!empty($obj->course_settings)) {
  466. if (is_file(api_get_path(SYS_CODE_PATH).'img/icons/'.ICON_SIZE_SMALL.'/'.$plugin_name.'.png')) {
  467. $icon = Display::return_icon(
  468. $plugin_name.'.png',
  469. Security::remove_XSS($pluginTitle),
  470. '',
  471. ICON_SIZE_SMALL
  472. );
  473. } else {
  474. $icon = Display::return_icon(
  475. 'plugins.png',
  476. Security::remove_XSS($pluginTitle),
  477. '',
  478. ICON_SIZE_SMALL
  479. );
  480. }
  481. $form->addHtml('<div class="panel panel-default">');
  482. $form->addHtml('
  483. <div class="panel-heading" role="tab" id="heading-' . $plugin_name.'-settings">
  484. <h4 class="panel-title">
  485. <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">
  486. ');
  487. $form->addHtml($icon.' '.$pluginTitle);
  488. $form->addHtml('
  489. </a>
  490. </h4>
  491. </div>
  492. ');
  493. $form->addHtml('
  494. <div id="collapse-' . $plugin_name.'-settings" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading-'.$plugin_name.'-settings">
  495. <div class="panel-body">
  496. ');
  497. $groups = array();
  498. foreach ($obj->course_settings as $setting) {
  499. if ($obj->validateCourseSetting($setting['name']) === false) {
  500. continue;
  501. }
  502. if ($setting['type'] != 'checkbox') {
  503. $form->addElement($setting['type'], $setting['name'], $obj->get_lang($setting['name']));
  504. } else {
  505. $element = & $form->createElement(
  506. $setting['type'],
  507. $setting['name'],
  508. '',
  509. $obj->get_lang($setting['name'])
  510. );
  511. if (isset($setting['init_value']) && $setting['init_value'] == 1) {
  512. $element->setChecked(true);
  513. }
  514. $form->addElement($element);
  515. if (isset($setting['group'])) {
  516. $groups[$setting['group']][] = $element;
  517. }
  518. }
  519. }
  520. foreach ($groups as $k => $v) {
  521. $form->addGroup($groups[$k], $k, array($obj->get_lang($k)));
  522. }
  523. $form->addButtonSave(get_lang('SaveSettings'));
  524. $form->addHtml('
  525. </div>
  526. </div>
  527. ');
  528. $form->addHtml('</div>');
  529. }
  530. }
  531. }
  532. /**
  533. * Get all course settings from all installed plugins.
  534. * @return array
  535. */
  536. public function getAllPluginCourseSettings()
  537. {
  538. $pluginList = $this->getInstalledPluginListObject();
  539. /** @var Plugin $obj */
  540. $courseSettings = array();
  541. if (!empty($pluginList)) {
  542. foreach ($pluginList as $obj) {
  543. $pluginCourseSetting = $obj->getCourseSettings();
  544. $courseSettings = array_merge($courseSettings, $pluginCourseSetting);
  545. }
  546. }
  547. return $courseSettings;
  548. }
  549. /**
  550. * When saving the plugin values in the course settings, check whether
  551. * a callback method should be called and send it the updated settings
  552. * @param array $values The new settings the user just saved
  553. * @return void
  554. */
  555. public function saveCourseSettingsHook($values)
  556. {
  557. $pluginList = $this->getInstalledPluginListObject();
  558. /** @var Plugin $obj */
  559. foreach ($pluginList as $obj) {
  560. $settings = $obj->getCourseSettings();
  561. $subValues = array();
  562. if (!empty($settings)) {
  563. foreach ($settings as $v) {
  564. if (isset($values[$v])) {
  565. $subValues[$v] = $values[$v];
  566. }
  567. }
  568. }
  569. if (!empty($subValues)) {
  570. $obj->course_settings_updated($subValues);
  571. }
  572. }
  573. }
  574. /**
  575. * Get first SMS plugin name
  576. * @return string|boolean
  577. */
  578. public function getSMSPluginName()
  579. {
  580. $installedPluginsList = $this->getInstalledPluginListObject();
  581. foreach ($installedPluginsList as $installedPlugin) {
  582. if ($installedPlugin->isMailPlugin) {
  583. return get_class($installedPlugin);
  584. }
  585. }
  586. return false;
  587. }
  588. /**
  589. * @return SmsPluginLibraryInterface
  590. */
  591. public function getSMSPluginLibrary()
  592. {
  593. $className = $this->getSMSPluginName();
  594. $className = str_replace('Plugin', '', $className);
  595. if (class_exists($className)) {
  596. return new $className;
  597. }
  598. return false;
  599. }
  600. }