plugin.lib.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. *
  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. $plugin_info = $this->getPluginInfo($pluginName);
  98. if (isset($plugin_info['plugin_class'])) {
  99. $pluginList[] = $plugin_info['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. It loads by default english see the hello world plugin
  243. *
  244. * @param string $plugin_name
  245. *
  246. * @todo add caching
  247. */
  248. public function load_plugin_lang_variables($plugin_name)
  249. {
  250. global $language_interface;
  251. $root = api_get_path(SYS_PLUGIN_PATH);
  252. // 1. Loading english if exists
  253. $english_path = $root.$plugin_name."/lang/english.php";
  254. if (is_readable($english_path)) {
  255. include $english_path;
  256. foreach ($strings as $key => $string) {
  257. $GLOBALS[$key] = $string;
  258. }
  259. }
  260. // 2. Loading the system language
  261. if ($language_interface != 'english') {
  262. $path = $root.$plugin_name."/lang/$language_interface.php";
  263. if (is_readable($path)) {
  264. include $path;
  265. if (!empty($strings)) {
  266. foreach ($strings as $key => $string) {
  267. $GLOBALS[$key] = $string;
  268. }
  269. }
  270. }
  271. }
  272. }
  273. /**
  274. * @param string $region
  275. * @param Template $template
  276. * @param bool $forced
  277. *
  278. * @return bool
  279. *
  280. * @todo improve this function
  281. */
  282. public function get_all_plugin_contents_by_region($region, $template, $forced = false)
  283. {
  284. global $_plugins;
  285. if (isset($_plugins[$region]) && is_array($_plugins[$region])) {
  286. //if (1) {
  287. //Load the plugin information
  288. foreach ($_plugins[$region] as $plugin_name) {
  289. //The plugin_info variable is available inside the plugin index
  290. $plugin_info = $this->getPluginInfo($plugin_name, $forced);
  291. //We also know where the plugin is
  292. $plugin_info['current_region'] = $region;
  293. // Loading the plugin/XXX/index.php file
  294. $plugin_file = api_get_path(SYS_PLUGIN_PATH)."$plugin_name/index.php";
  295. if (file_exists($plugin_file)) {
  296. //Loading the lang variables of the plugin if exists
  297. self::load_plugin_lang_variables($plugin_name);
  298. //Printing the plugin index.php file
  299. require $plugin_file;
  300. //If the variable $_template is set we assign those values to be accesible in Twig
  301. if (isset($_template)) {
  302. $_template['plugin_info'] = $plugin_info;
  303. } else {
  304. $_template = array();
  305. $_template['plugin_info'] = $plugin_info;
  306. }
  307. //Setting the plugin info available in the template if exists
  308. $template->assign($plugin_name, $_template);
  309. //Loading the Twig template plugin files if exists
  310. $template_list = array();
  311. if (isset($plugin_info) && isset($plugin_info['templates'])) {
  312. $template_list = $plugin_info['templates'];
  313. }
  314. if (!empty($template_list)) {
  315. foreach ($template_list as $plugin_tpl) {
  316. if (!empty($plugin_tpl)) {
  317. //$template_plugin_file = api_get_path(SYS_PLUGIN_PATH)."$plugin_name/$plugin_tpl"; //for smarty
  318. $template_plugin_file = "$plugin_name/$plugin_tpl"; // for twig
  319. $template->display($template_plugin_file);
  320. }
  321. }
  322. }
  323. }
  324. }
  325. }
  326. return true;
  327. }
  328. /**
  329. * @param string $plugin_name
  330. * @param bool $forced
  331. *
  332. * @deprecated
  333. */
  334. public function get_plugin_info($plugin_name, $forced = false)
  335. {
  336. return $this->getPluginInfo($plugin_name, $forced);
  337. }
  338. /**
  339. * Loads plugin info
  340. *
  341. * @staticvar array $plugin_data
  342. * @param string $plugin_name
  343. * @param bool $forced load from DB or from the static array
  344. *
  345. * @return array
  346. * @todo filter setting_form
  347. */
  348. public function getPluginInfo($plugin_name, $forced = false)
  349. {
  350. static $plugin_data = array();
  351. if (isset($plugin_data[$plugin_name]) && $forced == false) {
  352. return $plugin_data[$plugin_name];
  353. } else {
  354. $plugin_file = api_get_path(SYS_PLUGIN_PATH)."$plugin_name/plugin.php";
  355. $plugin_info = array();
  356. if (file_exists($plugin_file)) {
  357. require $plugin_file;
  358. }
  359. //extra options
  360. $plugin_settings = api_get_settings_params(
  361. array(
  362. "subkey = ? AND category = ? AND type = ? " => array($plugin_name, 'Plugins','setting')
  363. )
  364. );
  365. $settings_filtered = array();
  366. foreach ($plugin_settings as $item) {
  367. $settings_filtered[$item['variable']] = $item['selected_value'];
  368. }
  369. $plugin_info['settings'] = $settings_filtered;
  370. $plugin_data[$plugin_name] = $plugin_info;
  371. return $plugin_info;
  372. }
  373. }
  374. /**
  375. * Get the template list
  376. * @param string $pluginName
  377. * @return bool
  378. */
  379. public function get_templates_list($pluginName)
  380. {
  381. $plugin_info = $this->getPluginInfo($pluginName);
  382. if (isset($plugin_info) && isset($plugin_info['templates'])) {
  383. return $plugin_info['templates'];
  384. } else {
  385. return false;
  386. }
  387. }
  388. /**
  389. * Remove all regions of an specific plugin
  390. */
  391. public function remove_all_regions($plugin)
  392. {
  393. $access_url_id = api_get_current_access_url_id();
  394. if (!empty($plugin)) {
  395. api_delete_settings_params(
  396. array(
  397. 'category = ? AND type = ? AND access_url = ? AND subkey = ? ' => array('Plugins', 'region', $access_url_id, $plugin)
  398. )
  399. );
  400. }
  401. }
  402. /**
  403. * Add a plugin to a region
  404. * @param string $plugin
  405. * @param string $region
  406. */
  407. public function add_to_region($plugin, $region)
  408. {
  409. $access_url_id = api_get_current_access_url_id();
  410. api_add_setting($plugin, $region, $plugin, 'region', 'Plugins', $plugin, null, null, null, $access_url_id, 1);
  411. }
  412. /**
  413. * @param int $courseId
  414. */
  415. public function install_course_plugins($courseId)
  416. {
  417. $pluginList = $this->getInstalledPluginListObject();
  418. if (!empty($pluginList)) {
  419. /** @var Plugin $obj */
  420. foreach ($pluginList as $obj) {
  421. $pluginName = $obj->get_name();
  422. $plugin_path = api_get_path(SYS_PLUGIN_PATH).$pluginName.'/plugin.php';
  423. if (file_exists($plugin_path)) {
  424. require $plugin_path;
  425. if (isset($plugin_info) && isset($plugin_info['plugin_class']) && $obj->isCoursePlugin) {
  426. $obj->course_install($courseId);
  427. }
  428. }
  429. }
  430. }
  431. }
  432. /**
  433. * @param FormValidator $form
  434. */
  435. public function add_course_settings_form($form)
  436. {
  437. $pluginList = $this->getInstalledPluginListObject();
  438. /** @var Plugin $obj */
  439. foreach ($pluginList as $obj) {
  440. $plugin_name = $obj->get_name();
  441. $pluginTitle = $obj->get_title();
  442. if (!empty($obj->course_settings)) {
  443. if (is_file(api_get_path(SYS_CODE_PATH).'img/icons/'.ICON_SIZE_SMALL.'/'.$plugin_name.'.png')) {
  444. $icon = Display::return_icon(
  445. $plugin_name . '.png',
  446. Security::remove_XSS($pluginTitle),
  447. '',
  448. ICON_SIZE_SMALL
  449. );
  450. } else {
  451. $icon = Display::return_icon(
  452. 'plugins.png',
  453. Security::remove_XSS($pluginTitle),
  454. '',
  455. ICON_SIZE_SMALL
  456. );
  457. }
  458. //$icon = null;
  459. $form->addElement('html', '<div><h3>'.$icon.' '.Security::remove_XSS($pluginTitle).'</h3><div>');
  460. $groups = array();
  461. foreach ($obj->course_settings as $setting) {
  462. if ($setting['type'] != 'checkbox') {
  463. $form->addElement($setting['type'], $setting['name'], $obj->get_lang($setting['name']));
  464. } else {
  465. $element = & $form->createElement($setting['type'], $setting['name'], '', $obj->get_lang($setting['name']));
  466. if ($setting['init_value'] == 1) {
  467. $element->setChecked(true);
  468. }
  469. $groups[$setting['group']][] = $element;
  470. }
  471. }
  472. foreach ($groups as $k => $v) {
  473. $form->addGroup($groups[$k], $k, array($obj->get_lang($k)));
  474. }
  475. $form->addElement('style_submit_button', null, get_lang('SaveSettings'), 'class="save"');
  476. $form->addElement('html', '</div></div>');
  477. }
  478. }
  479. }
  480. /**
  481. * Get all course settings from all installed plugins.
  482. * @return array
  483. */
  484. public function getAllPluginCourseSettings()
  485. {
  486. $pluginList = $this->getInstalledPluginListObject();
  487. /** @var Plugin $obj */
  488. $courseSettings = array();
  489. if (!empty($pluginList)) {
  490. foreach ($pluginList as $obj) {
  491. $pluginCourseSetting = $obj->getCourseSettings();
  492. $courseSettings = array_merge($courseSettings, $pluginCourseSetting);
  493. }
  494. }
  495. return $courseSettings;
  496. }
  497. /**
  498. * When saving the plugin values in the course settings, check whether
  499. * a callback method should be called and send it the updated settings
  500. * @param array $values The new settings the user just saved
  501. * @return void
  502. */
  503. public function saveCourseSettingsHook($values)
  504. {
  505. $pluginList = $this->getInstalledPluginListObject();
  506. /** @var Plugin $obj */
  507. foreach ($pluginList as $obj) {
  508. $settings = $obj->getCourseSettings();
  509. $subValues = array();
  510. if (!empty($settings)) {
  511. foreach ($settings as $v) {
  512. if (isset($values[$v])) {
  513. $subValues[$v] = $values[$v];
  514. }
  515. }
  516. }
  517. if (!empty($subValues)) {
  518. $obj->course_settings_updated($subValues);
  519. }
  520. }
  521. }
  522. /**
  523. * Get first SMS plugin name
  524. * @return string|boolean
  525. */
  526. public function getSMSPluginName() {
  527. $installedPluginsList = $this->getInstalledPluginListObject();
  528. foreach ($installedPluginsList as $installedPlugin) {
  529. if ($installedPlugin->isMailPlugin) {
  530. return get_class($installedPlugin);
  531. }
  532. }
  533. return false;
  534. }
  535. }