PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_akeeba/akeeba/utils/inihelper.php

https://gitlab.com/endomorphosis/OLAAaction
PHP | 380 lines | 279 code | 36 blank | 65 comment | 36 complexity | fa6898561bd2ba1dbfff5b15b4858d41 MD5 | raw file
  1. <?php
  2. /**
  3. * Akeeba Engine
  4. * The modular PHP5 site backup engine
  5. * @copyright Copyright (c)2009-2011 Nicholas K. Dionysopoulos
  6. * @license GNU GPL version 3 or, at your option, any later version
  7. * @package akeebaengine
  8. * @version $Id: inihelper.php 409 2011-01-24 09:30:22Z nikosdion $
  9. */
  10. // Protection against direct access
  11. defined('AKEEBAENGINE') or die('Restricted access');
  12. /**
  13. * A class to load INI files describing the various Akeeba engines and GUI definitions,
  14. * along with their parameters.
  15. */
  16. class AEUtilInihelper {
  17. /** Do not allow object instances */
  18. private function __construct() {}
  19. /**
  20. * Returns a hash list of Akeeba engines and their data. Each entry has the engine
  21. * name as key and contains two arrays, under the 'information' and 'parameters' keys.
  22. * @param string $engine_type The engine type to return information for
  23. * @return array
  24. */
  25. public static function getEnginesList($engine_type)
  26. {
  27. // This is a static cache which persists between subsequent calls, but not
  28. // between successive page loads.
  29. static $engine_list = array();
  30. // Try to serve cached data first
  31. if(isset($engine_list[$engine_type])) return $engine_list[$engine_type];
  32. // Find absolute path to normal and plugins directories
  33. $ds = DIRECTORY_SEPARATOR;
  34. $path_list = array(
  35. AEFactory::getAkeebaRoot().$ds.'engines'.$ds.$engine_type,
  36. AEFactory::getAkeebaRoot().$ds.'plugins'.$ds.'engines'.$ds.$engine_type
  37. );
  38. // Initialize the array where we store our data
  39. $engine_list[$engine_type] = array();
  40. // Loop for the paths where engines can befound
  41. foreach($path_list as $path)
  42. {
  43. if(is_dir($path))
  44. {
  45. if(is_readable($path))
  46. {
  47. if( $handle = @opendir($path) )
  48. {
  49. while(false !== $filename = @readdir($handle))
  50. {
  51. if( (strtolower(substr($filename, -4)) == '.ini') && @is_file($path.$ds.$filename) )
  52. {
  53. $information = array();
  54. $parameters = array();
  55. AEUtilINI::parseEngineINI($path.$ds.$filename, $information, $parameters);
  56. $engine_name = substr($filename, 0, strlen($filename) - 4);
  57. $engine_list[$engine_type][$engine_name] = array(
  58. 'information' => $information,
  59. 'parameters' => $parameters
  60. );
  61. }
  62. } // while readdir
  63. @closedir($handle);
  64. } // if opendir
  65. } // if readable
  66. } // if is_dir
  67. }
  68. return $engine_list[$engine_type];
  69. }
  70. /**
  71. * Parses the GUI INI files and returns an array of groups and their data
  72. * @return array
  73. */
  74. public static function getGUIGroups()
  75. {
  76. // This is a static cache which persists between subsequent calls, but not
  77. // between successive page loads.
  78. static $gui_list = array();
  79. // Try to serve cached data first
  80. if(!empty($gui_list) && is_array($qui_list) )
  81. {
  82. if(count($gui_list) > 0) return $gui_list;
  83. }
  84. // Find absolute path to normal and plugins directories
  85. $ds = DIRECTORY_SEPARATOR;
  86. $path_list = array(
  87. AEFactory::getAkeebaRoot().$ds.'core'
  88. );
  89. // Initialize the array where we store our data
  90. $gui_list = array();
  91. // Loop for the paths where engines can be found
  92. foreach($path_list as $path)
  93. {
  94. if(is_dir($path))
  95. {
  96. if(is_readable($path))
  97. {
  98. if( $handle = @opendir($path) )
  99. {
  100. // Store INI names in temp array because we'll sort based on filename (GUI order IS IMPORTANT!!)
  101. $allINIs = array();
  102. while(false !== $filename = @readdir($handle))
  103. {
  104. if( (strtolower(substr($filename, -4)) == '.ini') && @is_file($path.$ds.$filename) )
  105. {
  106. $allINIs[] = $path.$ds.$filename;
  107. }
  108. } // while readdir
  109. @closedir($handle);
  110. if(!empty($allINIs))
  111. {
  112. // Sort GUI files alphabetically
  113. asort($allINIs);
  114. // Include each GUI def file
  115. foreach($allINIs as $filename)
  116. {
  117. $information = array();
  118. $parameters = array();
  119. AEUtilINI::parseInterfaceINI($filename, $information, $parameters);
  120. // This effectively skips non-GUI INIs (e.g. the scripting INI)
  121. if(!empty($information['description']))
  122. {
  123. $group_name = substr(basename($filename), 0, - 4);
  124. $gui_list[$group_name] = array(
  125. 'information' => $information,
  126. 'parameters' => $parameters
  127. );
  128. }
  129. }
  130. }
  131. } // if opendir
  132. } // if readable
  133. } // if is_dir
  134. }
  135. // @todo Push stack filter settings to the 03.filters section
  136. $path_list = array(
  137. AEFactory::getAkeebaRoot().$ds.'platform'.$ds.'filters'.$ds.'stack',
  138. AEFactory::getAkeebaRoot().$ds.'filters'.$ds.'stack',
  139. AEFactory::getAkeebaRoot().$ds.'plugins'.$ds.'filters'.$ds.'stack'
  140. );
  141. // Loop for the paths where optional filters can be found
  142. foreach($path_list as $path)
  143. {
  144. if(is_dir($path))
  145. {
  146. if(is_readable($path))
  147. {
  148. if( $handle = @opendir($path) )
  149. {
  150. // Store INI names in temp array because we'll sort based on filename (GUI order IS IMPORTANT!!)
  151. $allINIs = array();
  152. while(false !== $filename = @readdir($handle))
  153. {
  154. if( (strtolower(substr($filename, -4)) == '.ini') && @is_file($path.$ds.$filename) )
  155. {
  156. $allINIs[] = $path.$ds.$filename;
  157. }
  158. } // while readdir
  159. @closedir($handle);
  160. if(!empty($allINIs))
  161. {
  162. // Sort filter files alphabetically
  163. asort($allINIs);
  164. // Include each filter def file
  165. foreach($allINIs as $filename)
  166. {
  167. $information = array();
  168. $parameters = array();
  169. AEUtilINI::parseInterfaceINI($filename, $information, $parameters);
  170. if(!empty($gui_list['03.filters']['parameters'])) {
  171. $gui_list['03.filters']['parameters'][] = array(
  172. 'title' => '',
  173. 'description' => '',
  174. 'type' => 'separator',
  175. 'default' => ''
  176. );
  177. }
  178. $gui_list['03.filters']['parameters'] = array_merge( $gui_list['03.filters']['parameters'], $parameters );
  179. }
  180. }
  181. } // if opendir
  182. } // if readable
  183. } // if is_dir
  184. }
  185. return $gui_list;
  186. }
  187. public static function getInstallerList()
  188. {
  189. // This is a static cache which persists between subsequent calls, but not
  190. // between successive page loads.
  191. static $installer_list = array();
  192. // Try to serve cached data first
  193. if(!empty($installer_list) && is_array($installer_list))
  194. {
  195. if(count($installer_list) > 0) return $installer_list;
  196. }
  197. // Find absolute path to normal and plugins directories
  198. $ds = DIRECTORY_SEPARATOR;
  199. $path_list = array(
  200. AEPlatform::get_installer_images_path()
  201. );
  202. // Initialize the array where we store our data
  203. $installer_list = array();
  204. // Loop for the paths where engines can be found
  205. foreach($path_list as $path)
  206. {
  207. if(is_dir($path))
  208. {
  209. if(is_readable($path))
  210. {
  211. if( $handle = @opendir($path) )
  212. {
  213. while(false !== $filename = @readdir($handle))
  214. {
  215. if( (strtolower(substr($filename, -4)) == '.ini') && @is_file($path.$ds.$filename) )
  216. {
  217. $data = AEUtilINI::parse_ini_file($path.$ds.$filename, true);
  218. foreach($data as $key => $values)
  219. {
  220. $installer_list[$key] = array();
  221. foreach($values as $key2 => $value)
  222. {
  223. $installer_list[$key][$key2]=$value;
  224. }
  225. }
  226. }
  227. } // while readdir
  228. @closedir($handle);
  229. } // if opendir
  230. } // if readable
  231. } // if is_dir
  232. }
  233. return $installer_list;
  234. }
  235. /**
  236. * Returns the JSON representation of the GUI definition and the associated values
  237. * @return string
  238. */
  239. public static function getJsonGuiDefinition()
  240. {
  241. // Initialize the array which will be converted to JSON representation
  242. $json_array = array(
  243. 'engines' => array(),
  244. 'installers' => array(),
  245. 'gui' => array()
  246. );
  247. // Get a reference to the configuration
  248. $configuration =& AEFactory::getConfiguration();
  249. // Get data for all engines
  250. $engine_types = array('archiver','dump','scan','writer','proc');
  251. foreach($engine_types as $type)
  252. {
  253. $engines = self::getEnginesList($type);
  254. foreach($engines as $engine_name => $engine_data)
  255. {
  256. // Translate information
  257. foreach($engine_data['information'] as $key => $value)
  258. {
  259. switch($key)
  260. {
  261. case 'title':
  262. case 'description':
  263. $value = AEPlatform::translate($value);
  264. break;
  265. }
  266. $json_array['engines'][$type][$engine_name]['information'][$key] = $value;
  267. }
  268. // Process parameters
  269. $parameters = array();
  270. foreach($engine_data['parameters'] as $param_key => $param)
  271. {
  272. $param['default'] = $configuration->get( $param_key, $param['default'], false );
  273. foreach($param as $option_key => $option_value )
  274. {
  275. // Translate title, description, enumkeys
  276. switch($option_key)
  277. {
  278. case 'title':
  279. case 'description':
  280. case 'labelempty':
  281. case 'labelnotempty':
  282. $param[$option_key] = AEPlatform::translate($option_value);
  283. break;
  284. case 'enumkeys':
  285. $enumkeys = explode('|', $option_value);
  286. $new_keys = array();
  287. foreach($enumkeys as $old_key)
  288. {
  289. $new_keys[] = AEPlatform::translate($old_key);
  290. }
  291. $param[$option_key] = implode('|', $new_keys);
  292. break;
  293. default:
  294. }
  295. }
  296. $parameters[$param_key] = $param;
  297. }
  298. // Add processed parameters
  299. $json_array['engines'][$type][$engine_name]['parameters'] = $parameters;
  300. }
  301. }
  302. // Get data for GUI elements
  303. $json_array['gui'] = array();
  304. $groupdefs = self::getGUIGroups();
  305. foreach($groupdefs as $group_ini => $definition)
  306. {
  307. $group_name = AEPlatform::translate($definition['information']['description']);
  308. if( empty($group_name) ) continue; // Skip no-name groups
  309. $parameters = array();
  310. foreach($definition['parameters'] as $param_key => $param)
  311. {
  312. $param['default'] = $configuration->get( $param_key, $param['default'], FALSE );
  313. foreach($param as $option_key => $option_value )
  314. {
  315. // Translate title, description, enumkeys
  316. switch($option_key)
  317. {
  318. case 'title':
  319. case 'description':
  320. $param[$option_key] = AEPlatform::translate($option_value);
  321. break;
  322. case 'enumkeys':
  323. $enumkeys = explode('|', $option_value);
  324. $new_keys = array();
  325. foreach($enumkeys as $old_key)
  326. {
  327. $new_keys[] = AEPlatform::translate($old_key);
  328. }
  329. $param[$option_key] = implode('|', $new_keys);
  330. break;
  331. default:
  332. }
  333. }
  334. $parameters[$param_key] = $param;
  335. }
  336. $json_array['gui'][$group_name] = $parameters;
  337. }
  338. // Get data for the installers
  339. $json_array['installers'] = self::getInstallerList();
  340. $json = json_encode($json_array);
  341. return $json;
  342. }
  343. }
  344. ?>