plugin.lib.php 19 KB

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