plugin.lib.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /* See license terms in /license.txt */
  3. class AppPlugin {
  4. var $plugin_regions = array (
  5. // 'loginpage_main',
  6. 'login_top',
  7. 'login_bottom',
  8. 'menu_top',
  9. 'menu_bottom',
  10. /* 'campushomepage_main',
  11. 'campushomepage_menu',
  12. 'mycourses_main',
  13. 'mycourses_menu',*/
  14. 'content_top',
  15. 'content_bottom',
  16. 'header_main',
  17. 'header_center',
  18. 'header_left',
  19. 'header_right',
  20. //'footer',
  21. 'footer_left',
  22. 'footer_center',
  23. 'footer_right',
  24. 'course_tool_plugin'
  25. );
  26. function __construct() {
  27. }
  28. function read_plugins_from_path() {
  29. /* We scan the plugin directory. Each folder is a potential plugin. */
  30. $pluginpath = api_get_path(SYS_PLUGIN_PATH);
  31. $possible_plugins = array();
  32. $handle = @opendir($pluginpath);
  33. while (false !== ($file = readdir($handle))) {
  34. if ($file != '.' && $file != '..' && is_dir(api_get_path(SYS_PLUGIN_PATH).$file)) {
  35. $possible_plugins[] = $file;
  36. }
  37. }
  38. @closedir($handle);
  39. sort($possible_plugins);
  40. return $possible_plugins;
  41. }
  42. function get_installed_plugins_by_region(){
  43. $used_plugins = array();
  44. /* We retrieve all the active plugins. */
  45. $result = api_get_settings('Plugins');
  46. foreach ($result as $row) {
  47. $used_plugins[$row['variable']][] = $row['selected_value'];
  48. }
  49. return $used_plugins;
  50. }
  51. function get_installed_plugins() {
  52. $installed_plugins = array();
  53. $plugin_array = api_get_settings_params(array("variable = ? AND selected_value = ? AND category = ? " =>
  54. array('status', 'installed', 'Plugins')));
  55. if (!empty($plugin_array)) {
  56. foreach ($plugin_array as $row) {
  57. $installed_plugins[$row['subkey']] = true;
  58. }
  59. $installed_plugins = array_keys($installed_plugins);
  60. }
  61. return $installed_plugins;
  62. }
  63. function install($plugin_name, $access_url_id = null) {
  64. if (empty($access_url_id)) {
  65. $access_url_id = api_get_current_access_url_id();
  66. } else {
  67. $access_url_id = intval($access_url_id);
  68. }
  69. api_add_setting('installed', 'status', $plugin_name, 'setting', 'Plugins', $plugin_name, null, null, null, $access_url_id, 1);
  70. //api_add_setting($plugin, $area, $plugin, null, 'Plugins', $plugin, null, null, null, $_configuration['access_url'], 1);
  71. $pluginpath = api_get_path(SYS_PLUGIN_PATH).$plugin_name.'/install.php';
  72. if (is_file($pluginpath) && is_readable($pluginpath)) {
  73. //execute the install procedure
  74. require $pluginpath;
  75. }
  76. }
  77. function uninstall($plugin_name, $access_url_id = null) {
  78. if (empty($access_url_id)) {
  79. $access_url_id = api_get_current_access_url_id();
  80. } else {
  81. $access_url_id = intval($access_url_id);
  82. }
  83. api_delete_settings_params(array('category = ? AND access_url = ? AND subkey = ? ' =>
  84. array('Plugins', $access_url_id, $plugin_name)));
  85. $pluginpath = api_get_path(SYS_PLUGIN_PATH).$plugin_name.'/uninstall.php';
  86. if (is_file($pluginpath) && is_readable($pluginpath)) {
  87. //execute the uninstall procedure
  88. require $pluginpath;
  89. }
  90. }
  91. function get_areas_by_plugin($plugin_name) {
  92. $result = api_get_settings('Plugins');
  93. $areas = array();
  94. foreach ($result as $row) {
  95. if ($plugin_name == $row['selected_value']) {
  96. $areas[] = $row['variable'];
  97. }
  98. }
  99. return $areas;
  100. }
  101. function is_valid_plugin_location($location) {
  102. return in_array($location, $this->plugin_list);
  103. }
  104. function is_valid_plugin($plugin_name) {
  105. if (is_dir(api_get_path(SYS_PLUGIN_PATH).$plugin_name)) {
  106. if (is_file(api_get_path(SYS_PLUGIN_PATH).$plugin_name.'/index.php')) {
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. function get_plugin_regions() {
  113. sort($this->plugin_regions);
  114. return $this->plugin_regions;
  115. }
  116. function load_region($region, $main_template) {
  117. ob_start();
  118. $this->get_all_plugin_contents_by_region($region, $main_template);
  119. $content = ob_get_contents();
  120. ob_end_clean();
  121. return $content;
  122. }
  123. /**
  124. * Loads the translation files inside a plugin if exists. It loads by default english see the hello world plugin
  125. *
  126. * @todo add caching
  127. * @param string $plugin_name
  128. */
  129. function load_plugin_lang_variables($plugin_name) {
  130. global $language_interface;
  131. $root = api_get_path(SYS_PLUGIN_PATH);
  132. //1. Loading english if exists
  133. $english_path = $root.$plugin_name."/lang/english.php";
  134. if (is_readable($english_path)) {
  135. include $english_path;
  136. foreach ($strings as $key => $string) {
  137. //$$key = $string;
  138. $GLOBALS[$key] = $string;
  139. }
  140. }
  141. //2. Loading the system language
  142. if ($language_interface != 'english') {
  143. $path = $root.$plugin_name."/lang/$language_interface.php";
  144. if (is_readable($path)) {
  145. include $path;
  146. if (!empty($strings)) {
  147. foreach ($strings as $key => $string) {
  148. //$$key = $string;
  149. $GLOBALS[$key] = $string;
  150. }
  151. }
  152. }
  153. }
  154. }
  155. /**
  156. *
  157. *
  158. * @param string $block
  159. * @param obj template obj
  160. * @todo improve this function
  161. */
  162. function get_all_plugin_contents_by_region($region, $template) {
  163. global $_plugins;
  164. if (isset($_plugins[$region]) && is_array($_plugins[$region])) {
  165. //if (1) {
  166. foreach ($_plugins[$region] as $plugin_name) {
  167. //Load the plugin information
  168. //The plugin_info variable is available inside the plugin index
  169. $plugin_info = $this->get_plugin_info($plugin_name);
  170. //We also know where the plugin is
  171. $plugin_info['current_region'] = $region;
  172. // Loading the plugin/XXX/index.php file
  173. $plugin_file = api_get_path(SYS_PLUGIN_PATH)."$plugin_name/index.php";
  174. if (file_exists($plugin_file)) {
  175. //Loading the lang variables of the plugin if exists
  176. self::load_plugin_lang_variables($plugin_name);
  177. //Printing the plugin index.php file
  178. require $plugin_file;
  179. //If the variable $_template is set we assign those values to be accesible in Twig
  180. if (isset($_template)) {
  181. $_template['plugin_info'] = $plugin_info;
  182. } else {
  183. $_template = array();
  184. $_template['plugin_info'] = $plugin_info;
  185. }
  186. //Setting the plugin info available in the template if exists
  187. $template->assign($plugin_name, $_template);
  188. //Loading the Twig template plugin files if exists
  189. $template_list = array();
  190. if (isset($plugin_info) && isset($plugin_info['templates'])) {
  191. $template_list = $plugin_info['templates'];
  192. }
  193. if (!empty($template_list)) {
  194. foreach ($template_list as $plugin_tpl) {
  195. if (!empty($plugin_tpl)) {
  196. //$template_plugin_file = api_get_path(SYS_PLUGIN_PATH)."$plugin_name/$plugin_tpl"; //for smarty
  197. $template_plugin_file = "$plugin_name/$plugin_tpl"; // for twig
  198. $template->display($template_plugin_file);
  199. }
  200. }
  201. }
  202. }
  203. }
  204. }
  205. return true;
  206. }
  207. /**
  208. *
  209. * Loads plugin info
  210. * @staticvar array $plugin_data
  211. * @param string plugin name
  212. * @param bool load from DB or from the static array
  213. * @todo filter setting_form
  214. * @return array
  215. */
  216. function get_plugin_info($plugin_name, $forced = false) {
  217. static $plugin_data = array();
  218. if (isset($plugin_data[$plugin_name]) && $forced == false) {
  219. return $plugin_data[$plugin_name];
  220. } else {
  221. $plugin_file = api_get_path(SYS_PLUGIN_PATH)."$plugin_name/plugin.php";
  222. $plugin_info = array();
  223. if (file_exists($plugin_file)) {
  224. require $plugin_file;
  225. }
  226. //extra options
  227. $plugin_settings = api_get_settings_params(array("subkey = ? AND category = ? AND type = ? " =>
  228. array($plugin_name, 'Plugins','setting')));
  229. $settings_filtered = array();
  230. foreach ($plugin_settings as $item) {
  231. $settings_filtered[$item['variable']] = $item['selected_value'];
  232. }
  233. $plugin_info['settings'] = $settings_filtered;
  234. $plugin_data[$plugin_name] = $plugin_info;
  235. return $plugin_info;
  236. }
  237. }
  238. /*
  239. * Get the template list
  240. */
  241. function get_templates_list($plugin_name) {
  242. $plugin_info = $this->get_plugin_info($plugin_name);
  243. if (isset($plugin_info) && isset($plugin_info['templates'])) {
  244. return $plugin_info['templates'];
  245. } else {
  246. return false;
  247. }
  248. }
  249. /* *
  250. * Remove all regions of an specific plugin
  251. */
  252. function remove_all_regions($plugin) {
  253. $access_url_id = api_get_current_access_url_id();
  254. if (!empty($plugin)) {
  255. api_delete_settings_params(array('category = ? AND type = ? AND access_url = ? AND subkey = ? ' =>
  256. array('Plugins', 'region', $access_url_id, $plugin)));
  257. }
  258. }
  259. /*
  260. * Add a plugin to a region
  261. */
  262. function add_to_region($plugin, $region) {
  263. $access_url_id = api_get_current_access_url_id();
  264. api_add_setting($plugin, $region, $plugin, 'region', 'Plugins', $plugin, null, null, null, $access_url_id, 1);
  265. }
  266. }