PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/endomorphosis/OLAAaction
PHP | 249 lines | 190 code | 12 blank | 47 comment | 30 complexity | 2b541c108e8a36b0d351f2fc1d989df9 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: ini.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, without bumping into incompatibilities between
  14. * different PHP versions
  15. */
  16. class AEUtilINI {
  17. /** Do not allow object instances */
  18. private function __construct() {}
  19. /**
  20. * Parse an INI file and return an associative array. Since PHP versions before
  21. * 5.1 are bitches with regards to INI parsing, I use a PHP-only solution to
  22. * overcome this obstacle.
  23. * @param string $file The file to process
  24. * @param bool $process_sections True to also process INI sections
  25. * @return array An associative array of sections, keys and values
  26. */
  27. public static function parse_ini_file( $file, $process_sections, $rawdata = false )
  28. {
  29. if($rawdata)
  30. {
  31. return self::parse_ini_file_php($file, $process_sections, $rawdata);
  32. }
  33. else
  34. {
  35. if( version_compare(PHP_VERSION, '5.1.0', '>=') && (!$rawdata) )
  36. {
  37. if( function_exists('parse_ini_file') )
  38. {
  39. return parse_ini_file($file, $process_sections);
  40. }
  41. else
  42. {
  43. return self::parse_ini_file_php($file, $process_sections);
  44. }
  45. } else {
  46. return self::parse_ini_file_php($file, $process_sections, $rawdata);
  47. }
  48. }
  49. }
  50. /**
  51. * A PHP based INI file parser.
  52. * Thanks to asohn ~at~ aircanopy ~dot~ net for posting this handy function on
  53. * the parse_ini_file page on http://gr.php.net/parse_ini_file
  54. * @param string $file Filename to process
  55. * @param bool $process_sections True to also process INI sections
  56. * @param bool $rawdata If true, the $file contains raw INI data, not a filename
  57. * @return array An associative array of sections, keys and values
  58. */
  59. static function parse_ini_file_php($file, $process_sections = false, $rawdata = false)
  60. {
  61. $process_sections = ($process_sections !== true) ? false : true;
  62. if(!$rawdata)
  63. {
  64. $ini = file($file);
  65. }
  66. else
  67. {
  68. $file = str_replace("\r","",$file);
  69. $ini = explode("\n", $file);
  70. }
  71. if (count($ini) == 0) {return array();}
  72. $sections = array();
  73. $values = array();
  74. $result = array();
  75. $globals = array();
  76. $i = 0;
  77. foreach ($ini as $line) {
  78. $line = trim($line);
  79. $line = str_replace("\t", " ", $line);
  80. // Comments
  81. if (!preg_match('/^[a-zA-Z0-9[]/', $line)) {continue;}
  82. // Sections
  83. if ($line{0} == '[') {
  84. $tmp = explode(']', $line);
  85. $sections[] = trim(substr($tmp[0], 1));
  86. $i++;
  87. continue;
  88. }
  89. // Key-value pair
  90. list($key, $value) = explode('=', $line, 2);
  91. $key = trim($key);
  92. $value = trim($value);
  93. if (strstr($value, ";")) {
  94. $tmp = explode(';', $value);
  95. if (count($tmp) == 2) {
  96. if ((($value{0} != '"') && ($value{0} != "'")) ||
  97. preg_match('/^".*"\s*;/', $value) || preg_match('/^".*;[^"]*$/', $value) ||
  98. preg_match("/^'.*'\s*;/", $value) || preg_match("/^'.*;[^']*$/", $value) ){
  99. $value = $tmp[0];
  100. }
  101. } else {
  102. if ($value{0} == '"') {
  103. $value = preg_replace('/^"(.*)".*/', '$1', $value);
  104. } elseif ($value{0} == "'") {
  105. $value = preg_replace("/^'(.*)'.*/", '$1', $value);
  106. } else {
  107. $value = $tmp[0];
  108. }
  109. }
  110. }
  111. $value = trim($value);
  112. $value = trim($value, "'\"");
  113. if ($i == 0) {
  114. if (substr($line, -1, 2) == '[]') {
  115. $globals[$key][] = $value;
  116. } else {
  117. $globals[$key] = $value;
  118. }
  119. } else {
  120. if (substr($line, -1, 2) == '[]') {
  121. $values[$i-1][$key][] = $value;
  122. } else {
  123. $values[$i-1][$key] = $value;
  124. }
  125. }
  126. }
  127. for($j = 0; $j < $i; $j++) {
  128. if ($process_sections === true) {
  129. if( isset($sections[$j]) && isset($values[$j]) ) $result[$sections[$j]] = $values[$j];
  130. } else {
  131. if( isset($values[$j]) ) $result[] = $values[$j];
  132. }
  133. }
  134. return $result + $globals;
  135. }
  136. /**
  137. * Parses an engine INI file returning two arrays, one with the general information
  138. * of that engine and one with its configuration variables' definitions
  139. * @param string $inifile Absolute path to engine INI file
  140. * @param array $information [out] The engine information hash array
  141. * @param array $parameters [out] The parameters hash array
  142. * @return bool True if the file was loaded
  143. */
  144. public static function parseEngineINI($inifile, &$information, &$parameters)
  145. {
  146. if(!file_exists($inifile)) return false;
  147. $information = array(
  148. 'title' => '',
  149. 'description' => ''
  150. );
  151. $parameters = array();
  152. $inidata = AEUtilINI::parse_ini_file($inifile, true);
  153. foreach($inidata as $section => $data)
  154. {
  155. if(is_array($data))
  156. {
  157. if($section == '_information')
  158. {
  159. // Parse information
  160. foreach($data as $key=>$value)
  161. {
  162. $information[$key] = $value;
  163. }
  164. }
  165. elseif( substr($section,0,1) != '_' )
  166. {
  167. // Parse parameters
  168. $newparam = array(
  169. 'title' => '',
  170. 'description' => '',
  171. 'type' => 'string',
  172. 'default' => ''
  173. );
  174. foreach($data as $key=>$value)
  175. {
  176. $newparam[$key] = $value;
  177. }
  178. $parameters[$section] = $newparam;
  179. }
  180. }
  181. }
  182. return true;
  183. }
  184. /**
  185. * Parses a graphical interface INI file returning two arrays, one with the general
  186. * information of that configuration section and one with its configuration variables'
  187. * definitions.
  188. * @param string $inifile Absolute path to engine INI file
  189. * @param array $information [out] The GUI information hash array
  190. * @param array $parameters [out] The parameters hash array
  191. * @return bool True if the file was loaded
  192. */
  193. public static function parseInterfaceINI($inifile, &$information, &$parameters)
  194. {
  195. if(!file_exists($inifile)) return false;
  196. $information = array(
  197. 'description' => ''
  198. );
  199. $parameters = array();
  200. $inidata = AEUtilINI::parse_ini_file($inifile, true);
  201. foreach($inidata as $section => $data)
  202. {
  203. if(is_array($data))
  204. {
  205. if($section == '_group')
  206. {
  207. // Parse information
  208. foreach($data as $key=>$value)
  209. {
  210. $information[$key] = $value;
  211. }
  212. }
  213. elseif( substr($section,0,1) != '_' )
  214. {
  215. // Parse parameters
  216. $newparam = array(
  217. 'title' => '',
  218. 'description' => '',
  219. 'type' => 'string',
  220. 'default' => ''
  221. );
  222. foreach($data as $key=>$value)
  223. {
  224. $newparam[$key] = $value;
  225. }
  226. $parameters[$section] = $newparam;
  227. }
  228. }
  229. }
  230. return true;
  231. }
  232. }
  233. ?>