/src/S_confedit.php

https://github.com/Mithgol/Nephtsys · PHP · 343 lines · 321 code · 12 blank · 10 comment · 110 complexity · 56e6f737efd102a5b3f495d3b1feecb7 MD5 · raw file

  1. <?php
  2. /*\
  3. / This is a part of PhFiTo (aka PHP Fido Tosser)
  4. // Copyright (c) Alex Kocharin, 2:50/13
  5. ///
  6. /// This program is distributed under GNU GPL v2
  7. /// See docs/license for details
  8. //
  9. / $Id: S_confedit.php,v 1.5 2011/01/08 12:21:09 kocharin Exp $
  10. \*/
  11. if (!function_exists('xinclude')) die();
  12. function EditConfigCmd($cmds)
  13. {
  14. foreach($cmds as $cmd) {
  15. if ($cmd !== '' && $cmd{0} == '+') {
  16. $cmd = substr($cmd,1);
  17. $arr = explode("=", $cmd, 2);
  18. if (sizeof($arr) == 2) {
  19. list($key, $value) = $arr;
  20. } else {
  21. $key = $arr[0];
  22. $value = '';
  23. }
  24. Config_AddItem($key, $value);
  25. } else if ($cmd !== '' && $cmd{0} == '-') {
  26. $cmd = substr($cmd,1);
  27. $arr = explode("=", $cmd, 2);
  28. if (sizeof($arr) == 2) {
  29. list($key, $value) = $arr;
  30. } else {
  31. $key = $arr[0];
  32. $value = '';
  33. }
  34. $len = strlen($key)-1;
  35. while($len > 0 &&
  36. ord($key{$len}) >= ord('0') && ord($key{$len}) <= ord('9')
  37. ) $len--;
  38. if ($len != strlen($key)-1) {
  39. $num = substr($key, $len+1);
  40. } else {
  41. $num = false;
  42. }
  43. $key = substr($key, 0, $len+1);
  44. if ($key === '') continue;
  45. if ($num === false) {
  46. Config_DelItem($key);
  47. } else {
  48. Config_DelItem($key, $num);
  49. }
  50. } else if ($cmd !== '' && $cmd{0} == '[') {
  51. $cmd = substr($cmd,1);
  52. $arr = explode("]", $cmd, 2);
  53. if (sizeof($arr) == 2) {
  54. list($link, $param) = $arr;
  55. } else {
  56. continue;
  57. }
  58. if ($link === '') continue;
  59. $arr = explode("=", $param, 2);
  60. if (sizeof($arr) == 2) {
  61. list($key, $value) = $arr;
  62. } else {
  63. $key = $arr[0];
  64. $value = '';
  65. }
  66. if ($key === '') continue;
  67. LinkSetVar($link, $key, $value);
  68. } else {
  69. $arr = explode("=", $cmd, 2);
  70. if (sizeof($arr) == 2) {
  71. list($key, $value) = $arr;
  72. } else {
  73. $key = $arr[0];
  74. $value = '';
  75. }
  76. $len = strlen($key)-1;
  77. while($len > 0 &&
  78. ord($key{$len}) >= ord('0') && ord($key{$len}) <= ord('9')
  79. ) $len--;
  80. if ($len != strlen($key)-1) {
  81. $num = substr($key, $len+1);
  82. } else {
  83. $num = 0;
  84. }
  85. $key = substr($key, 0, $len+1);
  86. if ($key === '') continue;
  87. Config_ModItem($key, $value, $num);
  88. }
  89. }
  90. }
  91. function Config_AddItem($key,$value)
  92. {
  93. GLOBAL $CONFIG;
  94. $lokey = strtolower($key);
  95. if (isset($CONFIG->markers[$lokey])) {
  96. list($filenum,$line) = $CONFIG->markers[$lokey];
  97. $file = $CONFIG->_state_files[$filenum];
  98. $line--;
  99. } elseif (isset($CONFIG->markers['all'])) {
  100. list($filenum,$line) = $CONFIG->markers['all'];
  101. $file = $CONFIG->_state_files[$filenum];
  102. $line--;
  103. } else if (isset($CONFIG->Vars[$lokey])) {
  104. $num='';
  105. while (isset($CONFIG->Vars[$lokey.($num+1)])) $num++;
  106. list($filenum,$line) = $CONFIG->_state_cache[$lokey.$num];
  107. $file = $CONFIG->_state_files[$filenum];
  108. } else {
  109. $file = $CONFIG->_mainfile;
  110. $line = -1;
  111. }
  112. $num='';
  113. while (isset($CONFIG->Vars[$lokey.$num])) $num++;
  114. if (!isset($filenum)) {
  115. $filenum = $CONFIG->_get_state_files_num($file);
  116. }
  117. if ($line == -1) {
  118. $res = AppendLine($file,$key.' '.$value);
  119. $line = $res;
  120. } else {
  121. $res = InsertLine($file,$key.' '.$value,$line);
  122. }
  123. if ($res !== false) {
  124. $s=sizeof($CONFIG->_state_cache);
  125. foreach(array_keys($CONFIG->_state_cache) as $k) {
  126. $state =& $CONFIG->_state_cache[$k];
  127. if ($state[0] == $filenum) {
  128. if ($state[1] > $line) {
  129. $state[1]++;
  130. }
  131. }
  132. }
  133. foreach(array_keys($CONFIG->markers) as $k) {
  134. $state =& $CONFIG->markers[$k];
  135. if ($state[0] == $filenum) {
  136. if ($state[1] > $line) {
  137. $state[1]++;
  138. }
  139. }
  140. }
  141. $CONFIG->Vars[$lokey.$num] = $value;
  142. $CONFIG->_state_cache[$lokey.$num] = array($filenum, $line);
  143. }
  144. }
  145. function Config_DelItem($key,$num = 0)
  146. {
  147. GLOBAL $CONFIG;
  148. $key = strtolower($key);
  149. if ($num == 0) $num = '';
  150. if (isset($CONFIG->_state_cache[$key.$num])) {
  151. list($filenum,$line) = $CONFIG->_state_cache[$key.$num];
  152. $s=sizeof($CONFIG->_state_cache);
  153. foreach(array_keys($CONFIG->_state_cache) as $k) {
  154. $state =& $CONFIG->_state_cache[$k];
  155. if ($state[0] == $filenum) {
  156. if ($state[1] > $line) {
  157. $state[1]--;
  158. }
  159. }
  160. }
  161. foreach(array_keys($CONFIG->markers) as $k) {
  162. $state =& $CONFIG->markers[$k];
  163. if ($state[0] == $filenum) {
  164. if ($state[1] > $line) {
  165. $state[1]--;
  166. }
  167. }
  168. }
  169. $file = $CONFIG->_state_files[$filenum];
  170. while(isset($CONFIG->Vars[$key.($num+1)])) {
  171. $CONFIG->Vars[$key.$num] = $CONFIG->Vars[$key.($num+1)];
  172. $CONFIG->_state_cache[$key.$num] = $CONFIG->_state_cache[$key.($num+1)];
  173. $num++;
  174. }
  175. unset($CONFIG->Vars[$key.$num]);
  176. unset($CONFIG->_state_cache[$key.$num]);
  177. return RemoveLine($file,$line-1);
  178. } else {
  179. return false;
  180. }
  181. }
  182. function Config_ModItem($key,$newvalue,$num)
  183. {
  184. GLOBAL $CONFIG;
  185. $key = strtolower($key);
  186. if ($num == 0) $num = '';
  187. if (isset($CONFIG->_state_cache[$key.$num])) {
  188. list($filenum,$line) = $CONFIG->_state_cache[$key.$num];
  189. $file = $CONFIG->_state_files[$filenum];
  190. $line--;
  191. $lines = @file($file);
  192. if (!isset($lines[$line])) return false;
  193. $s = $lines[$line];
  194. $p = 0;
  195. $sa = array(" ","\t");
  196. while(isset($s{$p}) && in_array($s{$p},$sa)) $p++;
  197. while(isset($s{$p}) && !in_array($s{$p},$sa)) $p++;
  198. while(isset($s{$p}) && in_array($s{$p},$sa)) $p++;
  199. $s = substr($s,0,$p).$newvalue."\n";
  200. $lines[$line] = $s;
  201. if ($f = fopen($file,'w')) {
  202. fwrite($f,join("",$lines));
  203. fclose($f);
  204. $CONFIG->Vars[$key.$num] = $newvalue;
  205. return true;
  206. } else {
  207. return false;
  208. }
  209. } else {
  210. return false;
  211. }
  212. }
  213. function LinkSetVar($link,$key,$newvalue)
  214. {
  215. # print "------------- $link $key $newvalue\n";
  216. GLOBAL $CONFIG;
  217. if (!isset($CONFIG->Links->Vals[$link])) return 0;
  218. $arr = isset($CONFIG->Links->Vals_loc[$link][$key])?$CONFIG->Links->Vals_loc[$link][$key]:$CONFIG->Links->Vals_defloc[$link];
  219. list($file,$line,$exists) = $arr;
  220. if (!is_writable($file)) return 0;
  221. if ($f = @fopen($file,'r')):
  222. $l = 0;
  223. $lines = array();
  224. while ($l = fgets($f,1024)) $lines[] = chop($l);
  225. fclose($f);
  226. if ($exists):
  227. $cl = $lines[$line-1];
  228. $key = strtok($cl,' ');
  229. $cl = $key.' '.$newvalue;
  230. $lines[$line-1] = $cl;
  231. endif;
  232. while((trim($lines[$line-2])=='') && ($line>1)) $line--;
  233. if ($f = @fopen($file,'w')):
  234. for($i=0,$s=sizeof($lines);$i<$s;$i++):
  235. if (!$exists && ($i==$line-1)) fwrite($f,$key.' '.$newvalue."\n");
  236. fwrite($f,$lines[$i]."\n");
  237. endfor;
  238. fclose($f);
  239. $CONFIG->Links->Vals[$link][$k=strtolower($key)] = $newvalue;
  240. $CONFIG->Links->Vals_loc[$link][$k] = array($file,$line,true);
  241. if (!$exists):
  242. reset($CONFIG->Links->Vals);
  243. while(list($link,$def) = each($CONFIG->Links->Vals_loc)):
  244. reset($def);
  245. $chg = 0;
  246. while(list($key,$arr) = each($def)):
  247. if ((strcmp($arr[0],$file)==0) && ($arr[1]>$line)):
  248. $def[$key] = array($arr[0],$arr[1]+1,$arr[2]);
  249. $chg = 1;
  250. endif;
  251. endwhile;
  252. if ($chg) $CONFIG->Links->Vals_loc[$link] = $def;
  253. endwhile;
  254. reset($CONFIG->Links->Vals_defloc);
  255. while(list($link,$arr) = each($CONFIG->Links->Vals_defloc)):
  256. if ((strcmp($arr[0],$file)==0) && ($arr[1]>=$line)):
  257. $arr[1]++;
  258. $CONFIG->Links->Vals_defloc[$link] = $arr;
  259. endif;
  260. endwhile;
  261. endif;
  262. return 1;
  263. else:
  264. return 0;
  265. endif;
  266. else:
  267. return 0;
  268. endif;
  269. }
  270. function AppendLine($tofile,$line)
  271. {
  272. $result = 1;
  273. if (is_file($tofile) && ($f = @fopen($tofile,'r+'))):
  274. while(fgets($f,10240)) $result++;
  275. fseek($f,-1,2);
  276. $last = fread($f,1);
  277. if (ord($last) != 10) fwrite($f,"\n");
  278. fwrite($f,$line."\n");
  279. fclose($f);
  280. return $result;
  281. elseif ($f = @fopen($tofile,'a')):
  282. fwrite($f,$line."\n");
  283. fclose($f);
  284. return $result;
  285. else:
  286. return false;
  287. endif;
  288. }
  289. function InsertLine($tofile,$line,$num)
  290. {
  291. $lines = @file($tofile);
  292. $s = sizeof($lines);
  293. $lines[$s-1] = rtrim($lines[$s-1],"\n")."\n";
  294. for($i=$s;$i>$num;$i--) {
  295. $lines[$i] = $lines[$i-1];
  296. }
  297. $lines[$num] = $line."\n";
  298. if ($f = fopen($tofile,'w')) {
  299. fwrite($f,join("",$lines));
  300. fclose($f);
  301. } else {
  302. return false;
  303. }
  304. }
  305. function RemoveLine($tofile,$num)
  306. {
  307. $lines = @file($tofile);
  308. $s = sizeof($lines);
  309. if ($num>$s) $num = $s;
  310. for($i=$num;$i<$s-1;$i++) {
  311. $lines[$i] = $lines[$i+1];
  312. }
  313. unset($lines[$s-1]);
  314. if ($f = fopen($tofile,'w')) {
  315. fwrite($f,join("",$lines));
  316. fclose($f);
  317. } else {
  318. return false;
  319. }
  320. }
  321. function ReplaceLine($tofile,$line,$num)
  322. {
  323. $lines = @file($tofile);
  324. $lines[$num] = $line;
  325. if ($f = fopen($tofile,'w')) {
  326. fwrite($f,join("",$lines));
  327. fclose($f);
  328. } else {
  329. return false;
  330. }
  331. }