plugin.lib.php 20 KB

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