Просмотр файла system/inc/classes/ini.class.php

Размер файла: 9.74Kb
  1. <?php
  2. /*
  3. * Поиск файлов INI уточняется.
  4. */
  5. function search_ini_file ( $filename, $search_param, $return_section = false )
  6. {
  7. $search_key = (isset($search_param['key'])?$search_param['key']:false);
  8. $search_value = (isset($search_param['value'])?$search_param['value']:false);
  9. if ( !($search_key !==false || $search_value !==false) ){
  10. return false;
  11. }
  12. $retvalue = false;
  13. $handle = fopen($filename, 'r');
  14. if ( ($search_key !== false) && ($search_value !== false) ){
  15. $key_found = false;
  16. $retvalue['key'] = false;
  17. $retvalue['value'] = false;
  18. while( !feof($handle) ) {
  19. $line = trim(fgets($handle, 4096));
  20. if (preg_match("/^\[$search_key\].*?$/s",$line)){
  21. $key_found = true;
  22. $retvalue['key'] = true;
  23. continue;
  24. }
  25. if ($key_found){
  26. if (preg_match("/^\[.*?$/", trim($line))){
  27. break;
  28. }else{
  29. if ($return_section){
  30. if ($line != '') {
  31. list($k, $v) = split("=", $line);
  32. $retvalue[$search_key][$k] = preg_replace("/;.*$/", "", $v);
  33. } } }
  34.  
  35. if (preg_match("/^$search_value\s*?=.*$/", $line)){
  36. $retvalue['value'] = true;
  37. break;
  38. } } }
  39. }elseif ($search_key !== false){
  40. $keyfound = false;
  41. while ( !feof($handle) ){
  42. $line = trim(fgets($handle, 4096));
  43. if (preg_match("/^\[$search_key\].*?$/s",$line)){
  44. $retvalue = true;
  45. if (!$return_section){
  46. break;
  47. }else{
  48. $retvalue = Array();
  49. $keyfound = true;
  50. continue;
  51. } }
  52.  
  53. if ( $keyfound ){
  54. if (preg_match("/^\[.*?$/", trim($line))){
  55. break;
  56. }else{
  57. if ($return_section){
  58. if ($line != ''){
  59. list($k, $v) = split("=", $line);
  60. $retvalue[$search_key][$k] = preg_replace("/;.*$/", "", $v);
  61. } } } } }
  62. }elseif ($search_value !== false){
  63. while ( !feof($handle) ){
  64. $line = trim(fgets($handle, 4096));
  65.  
  66. if (preg_match("/^$search_value\s*?=.*$/", $line)){
  67. $retvalue = true;
  68. if ($return_section){
  69. $retvalue = array();
  70. if ($line != ''){
  71. list($k, $v) = split("=", $line);
  72. $retvalue[$k] = preg_replace("/;.*$/", "", $v);
  73. } }
  74. break;
  75. } } }
  76. fclose($handle);
  77. return $retvalue;
  78. }
  79. abstract class ini {
  80. static function value_encode($str) {
  81. $str = str_replace(array("\r", "\n", "\t"), array('\r', '\n', '\t'), $str);
  82. return htmlentities($str, ENT_QUOTES, 'UTF-8');
  83. }
  84. static function value_decode($str) {
  85. $str = str_replace(array('\r', '\n', '\t'), array("\r", "\n", "\t"), $str);
  86. return html_entity_decode($str, ENT_QUOTES, 'UTF-8');
  87. }
  88. static function openString($string, $sect = false) {
  89. $tmp_file = TMP . '/' . passgen() . '.tmp';
  90. if (!@file_put_contents($tmp_file, $string)) {
  91. return false;
  92. }
  93. $ini = self::read($tmp_file, $sect);
  94. unlink($tmp_file);
  95. return $ini;
  96. }
  97. static function read($file, $sect = false) {
  98. $arr = @parse_ini_file($file, $sect);
  99. if ($arr) {
  100. if ($sect) {
  101. foreach ($arr as $key => $value) {
  102. foreach ($value as $key2 => $value2) {
  103. $arr [$key] [$key2] = self::value_decode($value2);
  104. }
  105. }
  106. } else {
  107. foreach ($arr as $key => $value) {
  108. $arr [$key] = self::value_decode($value);
  109. }
  110. }
  111. }
  112. return $arr;
  113. }
  114. static function save($file, $array, $sect = false) {
  115. $tmp_file = TMP . '/tmp.' . passgen() . '.ini';
  116. $ini = array();
  117. $ini[] = "; SHCMS_ENGINE ini_class";
  118. if ($sect) {
  119. foreach ($array as $key => $value) {
  120. $ini [] = '[' . self::value_encode($key) . ']';
  121. foreach ($value as $key2 => $value2) {
  122. $ini [] = "$key2 = \"" . self::value_encode($value2) . "\";";
  123. }
  124. }
  125. } else {
  126. foreach ($array as $key => $value) {
  127. $ini [] = "$key = \"" . self::value_encode($value) . "\";";
  128. }
  129. }
  130. if (!@file_put_contents($tmp_file, implode("\r\n", $ini))) {
  131. return false;
  132. }
  133. @chmod($tmp_file, filesystem::getChmodToWrite());
  134. if (IS_WINDOWS) {
  135. if (@file_exists($file) && !@unlink($file)) {
  136. return false;
  137. }
  138. }
  139. if (!@rename($tmp_file, $file)) {
  140. return false;
  141. }
  142.  
  143.  
  144. return true;
  145. }
  146.  
  147. }
  148. class Configuration {
  149. const AUTO = 0;
  150. const JSON = 2;
  151. const PHP_INI = 4;
  152. const XML = 16;
  153.  
  154. static private $CONF_EXT_RELATION = array(
  155. 'json' => 2, // JSON
  156. 'ini' => 4, // PHP_INI
  157. 'xml' => 16 // XML
  158. );
  159.  
  160. static private $instances;
  161.  
  162. private $data;
  163.  
  164. static public function objectToArray($obj) {
  165. $arr = (is_object($obj))?
  166. get_object_vars($obj) :
  167. $obj;
  168.  
  169. foreach ($arr as $key => $val) {
  170. $arr[$key] = ((is_array($val)) || (is_object($val)))?
  171. self::objectToArray($val) :
  172. $val;
  173. }
  174.  
  175. return $arr;
  176. }
  177.  
  178. private function __construct($file, $type = Configuration::AUTO) {
  179. if ($type == self::AUTO) {
  180. $type = self::$CONF_EXT_RELATION[pathinfo($file, PATHINFO_EXTENSION)];
  181. }
  182.  
  183. switch($type) {
  184. case self::JSON:
  185. $this->data = json_decode(file_get_contents($file), true);
  186. break;
  187.  
  188. case self::PHP_INI:
  189. $this->data = parse_ini_file($file, true);
  190. break;
  191.  
  192. case self::XML:
  193. $this->data = self::objectToArray(simplexml_load_file($file));
  194. break;
  195. }
  196. }
  197.  
  198. static public function & getInstance($file, $type = Configuration::AUTO) {
  199. if(! isset(self::$instances[$file])) {
  200. self::$instances[$file] = new Configuration($file, $type);
  201. }
  202.  
  203. return self::$instances[$file];
  204. }
  205.  
  206. public function __get($section) {
  207. if ((is_array($this->data)) &&
  208. (array_key_exists($section, $this->data))) {
  209. return $this->data[$section];
  210. }
  211. }
  212.  
  213. public function getAvailableSections() {
  214. return array_keys($this->data);
  215. }
  216. }
  217.  
  218. $configuration = Configuration::getInstance(/*configuration filename*/);
  219. foreach($configuration->getAvailableSections() as $pos => $sectionName) {
  220. var_dump($sectionName);
  221. var_dump($configuration->{$sectionName});
  222. }
  223. function write_php_ini($array, $file)
  224. {
  225. $res = array();
  226. foreach($array as $key => $val)
  227. {
  228. if(is_array($val))
  229. {
  230. $res[] = "[$key]";
  231. foreach($val as $skey => $sval) $res[] = "$skey = ".(is_numeric($sval) ? $sval : '"'.$sval.'"');
  232. }
  233. else $res[] = "$key = ".(is_numeric($val) ? $val : '"'.$val.'"');
  234. }
  235. safefilerewrite($file, implode("\r\n", $res));
  236. }
  237.  
  238. function safefilerewrite($fileName, $dataToSave)
  239. { if ($fp = fopen($fileName, 'w'))
  240. {
  241. $startTime = microtime();
  242. do
  243. { $canWrite = flock($fp, LOCK_EX);
  244. // Если замок не получается спать 0 - 100 миллисекунд, чтобы избежать столкновения и загрузки процессора
  245. if(!$canWrite) usleep(round(rand(0, 100)*1000));
  246. } while ((!$canWrite)and((microtime()-$startTime) < 1000));
  247.  
  248. //Файл был заперт так что теперь мы можем хранить информацию
  249. if ($canWrite)
  250. { fwrite($fp, $dataToSave);
  251. flock($fp, LOCK_UN);
  252. }
  253. fclose($fp);
  254. }
  255.  
  256. }
  257.  
  258. ?>