PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/gantry/core/gantryini.class.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 350 lines | 267 code | 47 blank | 36 comment | 98 complexity | de8923b41f3449146931649ad399982c MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  1. <?php
  2. /**
  3. * @package gantry
  4. * @subpackage core
  5. * @version 3.2.22 August 3, 2012
  6. * @author RocketTheme http://www.rockettheme.com
  7. * @copyright Copyright (C) 2007 - 2012 RocketTheme, LLC
  8. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  9. *
  10. * Gantry uses the Joomla Framework (http://www.joomla.org), a GNU/GPLv2 content management system
  11. *
  12. */
  13. defined('GANTRY_VERSION') or die();
  14. /**
  15. * @package gantry
  16. * @subpackage core
  17. */
  18. class GantryINI {
  19. /**
  20. * @param $file
  21. * @param boolean $get_title
  22. * @param boolean $get_item
  23. * @param boolean $get_key
  24. * @return #Ffile_get_contents|array|?
  25. */
  26. function read($file, $get_title = false, $get_item = false, $get_key = false) {
  27. $data = file_get_contents($file);
  28. $inioutject = GantryINI::_readFile($data, true);
  29. $parse = get_object_vars( $inioutject );
  30. $data = array();
  31. foreach($parse as $title => $content) {
  32. foreach($content as $prefix => $value) {
  33. $tmp = explode("_", $prefix);
  34. $key = $tmp[0];
  35. $tmp = array_slice($tmp, 1);
  36. $key_prefix = implode("_", $tmp);
  37. if (!isset($data[$title][$key])) $data[$title][$key] = array();
  38. $data[$title][$key][$key_prefix] = $value;
  39. }
  40. }
  41. if ($get_title && !$get_item && !$get_key) {
  42. if (isset($data[$get_title])) return $data[$get_title];
  43. else return array();
  44. }
  45. if ($get_title && $get_item && !$get_key) {
  46. if (isset($data[$get_title]) && isset($data[$get_title][$get_item])) return $data[$get_title][$get_item];
  47. else return array();
  48. }
  49. if ($get_title && $get_item && $get_key) {
  50. if (isset($data[$get_title]) && isset($data[$get_title][$get_item]) && isset($data[$get_title][$get_item][$get_key]))
  51. return $data[$get_title][$get_item][$get_key];
  52. else return array();
  53. }
  54. return $data;
  55. }
  56. /**
  57. * @param $file
  58. * @param $data
  59. * @param boolean $merge_data
  60. * @param boolean $remove_empty
  61. * @return boolean
  62. */
  63. function write($file, $data, $merge_data = true, $remove_empty = true) {
  64. $ini_array = array();
  65. foreach($data as $title => $content) {
  66. $ini_array[$title] = array();
  67. foreach($content as $prefix => $values) {
  68. $ini_array[$title][$prefix] = array();
  69. foreach($values as $key => $value) {
  70. $ini_array[$title][$prefix][$key] = $value;
  71. }
  72. }
  73. }
  74. $merged = $ini_array;
  75. if ($merge_data || $merge_data === 'delete-key'){
  76. $ini_content = GantryINI::read($file);
  77. $merged = GantryINI::_array_merge_replace_recursive($ini_content, $ini_array);
  78. if ($merge_data === 'delete-key') {
  79. $merged = GantryINI::_deleteKey($merged, $data);
  80. }
  81. }
  82. $isempty = true;
  83. foreach($merged as $title => $content) {
  84. foreach($content as $key => $values) {
  85. foreach($values as $prefix => $value) {
  86. $isempty = false;
  87. break(3);
  88. }
  89. }
  90. }
  91. if (!$isempty) {
  92. $output = "";
  93. foreach($merged as $title => $content) {
  94. $output .= "\n[".$title."]\n";
  95. foreach($content as $key => $values) {
  96. $key = $key . "_";
  97. foreach($values as $prefix => $value) {
  98. $value=preg_replace_callback("/.*/s",array('GantryINI','repquotes'),$value);
  99. $output .= $key . $prefix . '="' . $value . '"'."\n";
  100. }
  101. }
  102. }
  103. $output = substr($output, 1, strlen($output));
  104. if (file_put_contents($file, $output)) return true;
  105. else return false;
  106. }
  107. else if ($remove_empty){
  108. if (file_exists($file) && is_writeable($file)){
  109. unlink($file);
  110. }
  111. }
  112. }
  113. function &_readFile( $data, $process_sections = false )
  114. {
  115. static $inistocache;
  116. if (!isset( $inistocache )) {
  117. $inistocache = array();
  118. }
  119. if (is_string($data))
  120. {
  121. $lines = explode("\n", $data);
  122. $hash = md5($data);
  123. }
  124. else
  125. {
  126. if (is_array($data)) {
  127. $lines = $data;
  128. } else {
  129. $lines = array ();
  130. }
  131. $hash = md5(implode("\n",$lines));
  132. }
  133. if(array_key_exists($hash, $inistocache)) {
  134. return $inistocache[$hash];
  135. }
  136. $obj = new stdClass();
  137. $sec_name = '';
  138. $unparsed = 0;
  139. if (!$lines) {
  140. return $obj;
  141. }
  142. foreach ($lines as $line)
  143. {
  144. // ignore comments
  145. if ($line && $line{0} == ';') {
  146. continue;
  147. }
  148. $line = trim($line);
  149. if ($line == '') {
  150. continue;
  151. }
  152. $lineLen = strlen($line);
  153. if ($line && $line{0} == '[' && $line{$lineLen-1} == ']')
  154. {
  155. $sec_name = substr($line, 1, $lineLen - 2);
  156. if ($process_sections) {
  157. $obj-> $sec_name = new stdClass();
  158. }
  159. }
  160. else
  161. {
  162. if ($pos = strpos($line, '='))
  163. {
  164. $property = trim(substr($line, 0, $pos));
  165. // property is assumed to be ascii
  166. if ($property && $property{0} == '"')
  167. {
  168. $propLen = strlen( $property );
  169. if ($property{$propLen-1} == '"') {
  170. $property = stripcslashes(substr($property, 1, $propLen - 2));
  171. }
  172. }
  173. // AJE: 2006-11-06 Fixes problem where you want leading spaces
  174. // for some parameters, eg, class suffix
  175. // $value = trim(substr($line, $pos +1));
  176. $value = substr($line, $pos +1);
  177. if (strpos($value, '|') !== false && preg_match('#(?<!\\\)\|#', $value))
  178. {
  179. $newlines = explode('\n', $value);
  180. $values = array();
  181. foreach($newlines as $newlinekey=>$newline) {
  182. // Explode the value if it is serialized as an arry of value1|value2|value3
  183. $parts = preg_split('/(?<!\\\)\|/', $newline);
  184. $array = (strcmp($parts[0], $newline) === 0) ? false : true;
  185. $parts = str_replace('\|', '|', $parts);
  186. foreach ($parts as $key => $value)
  187. {
  188. if ($value == 'false') {
  189. $value = false;
  190. }
  191. else if ($value == 'true') {
  192. $value = true;
  193. }
  194. else if ($value && $value{0} == '"')
  195. {
  196. $valueLen = strlen( $value );
  197. if ($value{$valueLen-1} == '"') {
  198. $value = stripcslashes(substr($value, 1, $valueLen - 2));
  199. }
  200. }
  201. if(!isset($values[$newlinekey])) $values[$newlinekey] = array();
  202. $values[$newlinekey][] = str_replace('\n', "\n", $value);
  203. }
  204. if (!$array) {
  205. $values[$newlinekey] = $values[$newlinekey][0];
  206. }
  207. }
  208. if ($process_sections)
  209. {
  210. if ($sec_name != '') {
  211. $obj->$sec_name->$property = $values[$newlinekey];
  212. } else {
  213. $obj->$property = $values[$newlinekey];
  214. }
  215. }
  216. else
  217. {
  218. $obj->$property = $values[$newlinekey];
  219. }
  220. }
  221. else
  222. {
  223. //unescape the \|
  224. $value = str_replace('\|', '|', $value);
  225. if ($value == 'false') {
  226. $value = false;
  227. }
  228. else if ($value == 'true') {
  229. $value = true;
  230. }
  231. else if ($value && $value{0} == '"')
  232. {
  233. $valueLen = strlen( $value );
  234. if ($value{$valueLen-1} == '"') {
  235. $value = stripcslashes(substr($value, 1, $valueLen - 2));
  236. }
  237. }
  238. if ($process_sections)
  239. {
  240. $value = str_replace('\n', "\n", $value);
  241. if ($sec_name != '') {
  242. $obj->$sec_name->$property = $value;
  243. } else {
  244. $obj->$property = $value;
  245. }
  246. }
  247. else
  248. {
  249. $obj->$property = str_replace('\n', "\n", $value);
  250. }
  251. }
  252. }
  253. else
  254. {
  255. if ($line && $line{0} == ';') {
  256. continue;
  257. }
  258. if ($process_sections)
  259. {
  260. $property = '__invalid'.$unparsed ++.'__';
  261. if ($process_sections)
  262. {
  263. if ($sec_name != '') {
  264. $obj->$sec_name->$property = trim($line);
  265. } else {
  266. $obj->$property = trim($line);
  267. }
  268. }
  269. else
  270. {
  271. $obj->$property = trim($line);
  272. }
  273. }
  274. }
  275. }
  276. }
  277. $inistocache[$hash] = clone($obj);
  278. return $obj;
  279. }
  280. function _deleteKey($merged, $data) {
  281. foreach($data as $title => $customs) {
  282. foreach($customs as $key => $content) {
  283. $merged[$title][$key] = array();
  284. }
  285. }
  286. return $merged;
  287. }
  288. function _array_merge_replace_recursive( &$array1, &$array2) {
  289. $merged = $array1;
  290. foreach($array2 as $key => $value) {
  291. if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
  292. $merged[$key] = GantryINI::_array_merge_replace_recursive($merged[$key], $value);
  293. }
  294. else {
  295. $merged[$key] = $value;
  296. }
  297. }
  298. return $merged;
  299. }
  300. function repquotes($matches) {
  301. return preg_replace('/[\'"]/','\\\$0',$matches[0]);
  302. }
  303. }