plugin.lib.php 19 KB

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