PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/application/helpers/plugin.php

https://github.com/yamamoto123/Ushahidi_Web
PHP | 292 lines | 177 code | 29 blank | 86 comment | 17 complexity | 1acfd6df8b4e5b4e12ac05476df382a4 MD5 | raw file
  1. <?php
  2. /**
  3. * Plugins helper
  4. *
  5. * @package Ushahidi
  6. * @subpackage Helpers
  7. * @author Ushahidi Team
  8. * @copyright (c) 2008 Ushahidi Team
  9. * @license http://www.ushahidi.com/license.html
  10. */
  11. class plugin_Core {
  12. /**
  13. * @var array
  14. */
  15. protected static $javascripts = array();
  16. /**
  17. * @var array
  18. */
  19. protected static $stylesheets = array();
  20. /**
  21. * @var array
  22. */
  23. protected static $sms_providers = array();
  24. /**
  25. * Adds an array of javascript items to the list of javascript sources
  26. *
  27. * @param array $javascripts
  28. */
  29. public static function add_javascript($javascripts = array())
  30. {
  31. if ( ! is_array($javascripts))
  32. $javascripts = array($javascripts);
  33. foreach ($javascripts as $key => $javascript)
  34. {
  35. self::$javascripts[] = $javascript;
  36. }
  37. }
  38. /**
  39. * Removes a list of javascript items from the list of javascript
  40. * sources
  41. *
  42. * @param array $javascripts
  43. */
  44. public static function remove_javascript($javascripts = array())
  45. {
  46. foreach (self::$javascripts as $key => $javascript)
  47. {
  48. if (in_array($javascript, $javascripts))
  49. unset(self::$javascripts[$key]);
  50. }
  51. }
  52. /**
  53. * Adds a list of stylesheet items to the list of stylesheets for the
  54. * plugin
  55. *
  56. * @param array $stylesheets
  57. */
  58. public static function add_stylesheet($stylesheets = array())
  59. {
  60. if ( ! is_array($stylesheets))
  61. $stylesheets = array($stylesheets);
  62. foreach ($stylesheets as $key => $stylesheet)
  63. {
  64. self::$stylesheets[] = $stylesheet;
  65. }
  66. }
  67. /**
  68. * Adds an SMS provider to the list of available SMS providers
  69. *
  70. * @param array $sms_providers
  71. */
  72. public static function add_sms_provider($sms_providers = array())
  73. {
  74. if ( ! is_array($sms_providers))
  75. $sms_providers = array($sms_providers => $sms_providers);
  76. foreach ($sms_providers as $key => $sms_provider)
  77. {
  78. self::$sms_providers[$key] = $sms_provider;
  79. }
  80. }
  81. /**
  82. * Adds a the stylesheet/javascript to the header of the view file
  83. *
  84. * @param string $type
  85. */
  86. public static function render($type)
  87. {
  88. $files = $type.'s';
  89. $html = '';
  90. foreach (self::$$files as $key => $file)
  91. {
  92. switch ($type)
  93. {
  94. case 'stylesheet':
  95. if (substr_compare($file, '.css', -3, 3, FALSE) !== 0)
  96. {
  97. // Add the javascript suffix
  98. $file .= '.css';
  99. }
  100. $html .= '<link rel="stylesheet" type="text/css" href="'.url::base()."plugins/".$file.'" />';
  101. break;
  102. case 'javascript':
  103. if (substr_compare($file, '.js', -3, 3, FALSE) !== 0)
  104. {
  105. // Add the javascript suffix
  106. $file .= '.js';
  107. }
  108. $html .= '<script type="text/javascript" src="'.url::base()."plugins/".$file.'"></script>';
  109. break;
  110. }
  111. }
  112. return $html;
  113. }
  114. /**
  115. * Rettuns the list of SMS providers
  116. *
  117. * @return array
  118. */
  119. public static function get_sms_providers()
  120. {
  121. return self::$sms_providers;
  122. }
  123. /**
  124. * Load Plugin Information from readme.txt file
  125. *
  126. * @param string plugin name
  127. * @return array
  128. */
  129. public static function meta($plugin = NULL)
  130. {
  131. // Set Default Values
  132. $plugin_headers = array(
  133. "plugin_name" => "name",
  134. "plugin_description" => "description",
  135. "plugin_uri" => "website",
  136. "plugin_author" => "author",
  137. "plugin_version" => "version",
  138. );
  139. // Determine if readme.txt (Case Insensitive) exists
  140. $file = PLUGINPATH.$plugin."/readme.txt";
  141. if ( file::file_exists_i($file) )
  142. {
  143. $fp = fopen( $file, 'r' );
  144. // Pull only the first 8kiB of the file in.
  145. $file_data = fread( $fp, 8192 );
  146. fclose( $fp );
  147. foreach ( $plugin_headers as $field => $regex )
  148. {
  149. preg_match( '/' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, ${$field});
  150. if ( ! empty( ${$field} ) )
  151. {
  152. ${$field} = trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', ${$field}[1] ));
  153. }
  154. else
  155. {
  156. ${$field} = '';
  157. }
  158. }
  159. $file_data = compact( array_keys( $plugin_headers ) );
  160. return $file_data;
  161. }
  162. else
  163. {
  164. return $plugin_headers;
  165. }
  166. }
  167. /**
  168. * Discover Plugin Settings Controller
  169. *
  170. * @param string plugin name
  171. * @return mixed Plugin settings page on success, FALSE otherwise
  172. */
  173. public static function settings($plugin = NULL)
  174. {
  175. // Determine if readme.txt (Case Insensitive) exists
  176. $file = PLUGINPATH.$plugin."/controllers/admin/".$plugin."_settings.php";
  177. if ( file::file_exists_i($file) )
  178. {
  179. return $plugin."_settings";
  180. }
  181. else
  182. {
  183. return FALSE;
  184. }
  185. }
  186. /**
  187. * Delete plugin from Plugin Folder
  188. *
  189. * @param string plugin name/folder
  190. */
  191. public static function delete($folder = NULL)
  192. {
  193. if ($folder)
  194. {
  195. if (is_dir(PLUGINPATH.$folder))
  196. {
  197. // First Delete Files Recursively
  198. $files = scandir(PLUGINPATH.$folder);
  199. array_shift($files); // remove '.' from array
  200. array_shift($files); // remove '..' from array
  201. foreach ($files as $file)
  202. {
  203. $file = PLUGINPATH.$folder."/".$file;
  204. if (is_dir($file))
  205. {
  206. plugin::delete($file);
  207. try
  208. {
  209. rmdir($file);
  210. }
  211. catch (Kohana_Database_Exception $e)
  212. {
  213. // Log exceptions
  214. Kohana::log('error', 'Caught exception: '.$e->getMessage());
  215. }
  216. }
  217. else
  218. {
  219. try
  220. {
  221. unlink($file);
  222. }
  223. catch (Kohana_Database_Exception $e)
  224. {
  225. // Log exceptions
  226. Kohana::log('error', 'Caught exception: '.$e->getMessage());
  227. }
  228. }
  229. }
  230. }
  231. }
  232. }
  233. /**
  234. * Temporarily load a config file.
  235. *
  236. * @param string $name config filename, without extension
  237. * @param boolean $required is the file required?
  238. * @return array
  239. */
  240. public static function config_load($name, $required = TRUE)
  241. {
  242. if (isset(self::$internal_cache['configuration'][$name]))
  243. return self::$internal_cache['configuration'][$name];
  244. // Load matching configs
  245. $configuration = array();
  246. $file = PLUGINPATH.$name.'/config/'.$name.'.php';
  247. if ( file_exists($file) )
  248. {
  249. require $file;
  250. if (isset($config) AND is_array($config))
  251. {
  252. // Merge in configuration
  253. $configuration = array_merge($configuration, $config);
  254. }
  255. }
  256. if ( ! isset(self::$write_cache['configuration']))
  257. {
  258. // Cache has changed
  259. self::$write_cache['configuration'] = TRUE;
  260. }
  261. return self::$internal_cache['configuration'][$name] = $configuration;
  262. }
  263. }