/2011/components/com_gantry/core/gantryini.class.php

https://bitbucket.org/elijahvsjesus/tandava · PHP · 345 lines · 263 code · 46 blank · 36 comment · 98 complexity · 6e97ff4dc41e493f8482553834b6a6e9 MD5 · raw file

  1. <?php
  2. /**
  3. * @package gantry
  4. * @subpackage core
  5. * @version 3.1.15 July 15, 2011
  6. * @author RocketTheme http://www.rockettheme.com
  7. * @copyright Copyright (C) 2007 - 2011 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. $output .= $key . $prefix . "=" . $value . "\n";
  99. }
  100. }
  101. }
  102. $output = substr($output, 1, strlen($output));
  103. if (file_put_contents($file, $output)) return true;
  104. else return false;
  105. }
  106. else if ($remove_empty){
  107. if (file_exists($file) && is_writeable($file)){
  108. unlink($file);
  109. }
  110. }
  111. }
  112. function &_readFile( $data, $process_sections = false )
  113. {
  114. static $inistocache;
  115. if (!isset( $inistocache )) {
  116. $inistocache = array();
  117. }
  118. if (is_string($data))
  119. {
  120. $lines = explode("\n", $data);
  121. $hash = md5($data);
  122. }
  123. else
  124. {
  125. if (is_array($data)) {
  126. $lines = $data;
  127. } else {
  128. $lines = array ();
  129. }
  130. $hash = md5(implode("\n",$lines));
  131. }
  132. if(array_key_exists($hash, $inistocache)) {
  133. return $inistocache[$hash];
  134. }
  135. $obj = new stdClass();
  136. $sec_name = '';
  137. $unparsed = 0;
  138. if (!$lines) {
  139. return $obj;
  140. }
  141. foreach ($lines as $line)
  142. {
  143. // ignore comments
  144. if ($line && $line{0} == ';') {
  145. continue;
  146. }
  147. $line = trim($line);
  148. if ($line == '') {
  149. continue;
  150. }
  151. $lineLen = strlen($line);
  152. if ($line && $line{0} == '[' && $line{$lineLen-1} == ']')
  153. {
  154. $sec_name = substr($line, 1, $lineLen - 2);
  155. if ($process_sections) {
  156. $obj-> $sec_name = new stdClass();
  157. }
  158. }
  159. else
  160. {
  161. if ($pos = strpos($line, '='))
  162. {
  163. $property = trim(substr($line, 0, $pos));
  164. // property is assumed to be ascii
  165. if ($property && $property{0} == '"')
  166. {
  167. $propLen = strlen( $property );
  168. if ($property{$propLen-1} == '"') {
  169. $property = stripcslashes(substr($property, 1, $propLen - 2));
  170. }
  171. }
  172. // AJE: 2006-11-06 Fixes problem where you want leading spaces
  173. // for some parameters, eg, class suffix
  174. // $value = trim(substr($line, $pos +1));
  175. $value = substr($line, $pos +1);
  176. if (strpos($value, '|') !== false && preg_match('#(?<!\\\)\|#', $value))
  177. {
  178. $newlines = explode('\n', $value);
  179. $values = array();
  180. foreach($newlines as $newlinekey=>$newline) {
  181. // Explode the value if it is serialized as an arry of value1|value2|value3
  182. $parts = preg_split('/(?<!\\\)\|/', $newline);
  183. $array = (strcmp($parts[0], $newline) === 0) ? false : true;
  184. $parts = str_replace('\|', '|', $parts);
  185. foreach ($parts as $key => $value)
  186. {
  187. if ($value == 'false') {
  188. $value = false;
  189. }
  190. else if ($value == 'true') {
  191. $value = true;
  192. }
  193. else if ($value && $value{0} == '"')
  194. {
  195. $valueLen = strlen( $value );
  196. if ($value{$valueLen-1} == '"') {
  197. $value = stripcslashes(substr($value, 1, $valueLen - 2));
  198. }
  199. }
  200. if(!isset($values[$newlinekey])) $values[$newlinekey] = array();
  201. $values[$newlinekey][] = str_replace('\n', "\n", $value);
  202. }
  203. if (!$array) {
  204. $values[$newlinekey] = $values[$newlinekey][0];
  205. }
  206. }
  207. if ($process_sections)
  208. {
  209. if ($sec_name != '') {
  210. $obj->$sec_name->$property = $values[$newlinekey];
  211. } else {
  212. $obj->$property = $values[$newlinekey];
  213. }
  214. }
  215. else
  216. {
  217. $obj->$property = $values[$newlinekey];
  218. }
  219. }
  220. else
  221. {
  222. //unescape the \|
  223. $value = str_replace('\|', '|', $value);
  224. if ($value == 'false') {
  225. $value = false;
  226. }
  227. else if ($value == 'true') {
  228. $value = true;
  229. }
  230. else if ($value && $value{0} == '"')
  231. {
  232. $valueLen = strlen( $value );
  233. if ($value{$valueLen-1} == '"') {
  234. $value = stripcslashes(substr($value, 1, $valueLen - 2));
  235. }
  236. }
  237. if ($process_sections)
  238. {
  239. $value = str_replace('\n', "\n", $value);
  240. if ($sec_name != '') {
  241. $obj->$sec_name->$property = $value;
  242. } else {
  243. $obj->$property = $value;
  244. }
  245. }
  246. else
  247. {
  248. $obj->$property = str_replace('\n', "\n", $value);
  249. }
  250. }
  251. }
  252. else
  253. {
  254. if ($line && $line{0} == ';') {
  255. continue;
  256. }
  257. if ($process_sections)
  258. {
  259. $property = '__invalid'.$unparsed ++.'__';
  260. if ($process_sections)
  261. {
  262. if ($sec_name != '') {
  263. $obj->$sec_name->$property = trim($line);
  264. } else {
  265. $obj->$property = trim($line);
  266. }
  267. }
  268. else
  269. {
  270. $obj->$property = trim($line);
  271. }
  272. }
  273. }
  274. }
  275. }
  276. $inistocache[$hash] = clone($obj);
  277. return $obj;
  278. }
  279. function _deleteKey($merged, $data) {
  280. foreach($data as $title => $customs) {
  281. foreach($customs as $key => $content) {
  282. $merged[$title][$key] = array();
  283. }
  284. }
  285. return $merged;
  286. }
  287. function _array_merge_replace_recursive( &$array1, &$array2) {
  288. $merged = $array1;
  289. foreach($array2 as $key => $value) {
  290. if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
  291. $merged[$key] = GantryINI::_array_merge_replace_recursive($merged[$key], $value);
  292. }
  293. else {
  294. $merged[$key] = $value;
  295. }
  296. }
  297. return $merged;
  298. }
  299. }