/common/site/configuration.php

https://github.com/dkullmann/vielerets · PHP · 507 lines · 377 code · 44 blank · 86 comment · 61 complexity · bdcb90f984dc40f2e7fdc11323747dca MD5 · raw file

  1. <?php
  2. // Copyright (C) 2003-2010 National Association of REALTORS(R)
  3. //
  4. // All rights reserved.
  5. //
  6. // Permission is hereby granted, free of charge, to any person
  7. // obtaining a copy of this software and associated documentation
  8. // files (the "Software"), to deal in the Software without
  9. // restriction, including without limitation the rights to use, copy,
  10. // modify, merge, publish, distribute, and/or sell copies of the
  11. // Software, and to permit persons to whom the Software is furnished
  12. // to do so, provided that the above copyright notice(s) and this
  13. // permission notice appear in all copies of the Software and that
  14. // both the above copyright notice(s) and this permission notice
  15. // appear in supporting documentation.
  16. class Configuration
  17. {
  18. var $contents;
  19. var $valueIndex;
  20. var $variableIndex;
  21. var $fileName;
  22. function Configuration($fileName = null)
  23. {
  24. if ($fileName != null)
  25. {
  26. $this->fileName = $fileName;
  27. if (file_exists($fileName))
  28. {
  29. $this->contents = file($fileName);
  30. }
  31. $this->buildIndex();
  32. }
  33. }
  34. function buildIndex()
  35. {
  36. if ($this->contents != null)
  37. {
  38. //print("BUILD INDEX\r\n");
  39. $this->valueIndex = null;
  40. $searchKey = 'define("';
  41. foreach ($this->contents as $line_num => $line)
  42. {
  43. $pos = strpos($line, $searchKey);
  44. if ($pos === false)
  45. {
  46. }
  47. else
  48. {
  49. //
  50. // find comma
  51. //
  52. $pos = strpos($line, ',');
  53. $source = substr($line, 0, $pos);
  54. $source = trim($source);
  55. //
  56. // find left paren
  57. //
  58. $pos = strpos($source, '(');
  59. $source = substr($source, $pos + 1, strlen($source));
  60. $source = trim($source);
  61. //
  62. // remove single and double quotes
  63. //
  64. $source = trim($source, "\x22\x27");
  65. //
  66. // enter into index
  67. //
  68. $this->valueIndex[$source] = $line_num;
  69. }
  70. }
  71. //print_r($this->valueIndex);
  72. //print("VALUE\r\n");
  73. $source = null;
  74. $this->variableIndex = null;
  75. $searchKey = '=array(';
  76. foreach ($this->contents as $line_num => $line)
  77. {
  78. $pos = strpos($line, $searchKey);
  79. if ($pos === false)
  80. {
  81. }
  82. else
  83. {
  84. //
  85. // find equals
  86. //
  87. $pos = strpos($line, '=');
  88. $source = substr($line, 0, $pos);
  89. $source = trim($source);
  90. //
  91. // remove $
  92. //
  93. $pos = strpos($source, '$');
  94. $source = substr($source, $pos + 1, strlen($source));
  95. $source = trim($source);
  96. //
  97. // enter into index
  98. //
  99. $this->variableIndex[$source] = $line_num;
  100. }
  101. }
  102. //print_r($this->variableIndex);
  103. //print("VARIABLE\r\n");
  104. }
  105. }
  106. function getName()
  107. {
  108. return baseName($this->fileName);
  109. }
  110. function getListValue($key)
  111. {
  112. $list = null;
  113. $temp = explode(',', $this->getValue($key));
  114. foreach ($temp as $num => $item)
  115. {
  116. $list[$item] = true;
  117. }
  118. return $list;
  119. }
  120. function setBooleanFromArg($name, $vars)
  121. {
  122. $this->setValue($name, booleanFromArg($name,$vars));
  123. }
  124. function getBooleanValue($key)
  125. {
  126. $value = $this->getValue($key);
  127. if (is_bool($value))
  128. {
  129. return $value;
  130. }
  131. if (is_string($value))
  132. {
  133. $value = strtolower($value);
  134. if ($value == 'true')
  135. {
  136. return true;
  137. }
  138. if ($value == '1')
  139. {
  140. return true;
  141. }
  142. return false;
  143. }
  144. settype($value, 'boolean');
  145. return $value;
  146. }
  147. function getValue($key)
  148. {
  149. if ($this->valueIndex == null)
  150. {
  151. return null;
  152. }
  153. if (!array_key_exists($key, $this->valueIndex))
  154. {
  155. return null;
  156. }
  157. $line = $this->valueIndex[$key];
  158. if ($line === false)
  159. {
  160. return null;
  161. }
  162. //
  163. // get the line
  164. //
  165. $source = $this->contents[$line];
  166. //
  167. // find comma
  168. //
  169. $first = strpos($source, ',');
  170. $temp = substr($source, $first + 1, strlen($source));
  171. $temp = trim($temp);
  172. //
  173. // find right paren
  174. //
  175. $first = strrpos($temp, ')');
  176. $temp = substr($temp, 0, $first);
  177. $temp = trim($temp);
  178. //
  179. // remove escaped $
  180. //
  181. $pos = strpos($temp, "\\$");
  182. if ($pos === false)
  183. {
  184. }
  185. else
  186. {
  187. $piece = substr($temp, 0, $pos);
  188. $piece .= substr($temp, $pos + 1, strlen($temp));
  189. $temp = $piece;
  190. }
  191. //
  192. // remove single and double quotes
  193. //
  194. return trim($temp, "\x22\x27");
  195. }
  196. function setBooleanValue($name, $val)
  197. {
  198. if (is_bool($val))
  199. {
  200. $value = $val;
  201. }
  202. if (is_string($val))
  203. {
  204. $check = strtolower($val);
  205. if ($check == 'true')
  206. {
  207. $value = true;
  208. }
  209. if ($check == '1')
  210. {
  211. $value = true;
  212. }
  213. $value = false;
  214. }
  215. $this->setValue($name, $value);
  216. }
  217. function setValue($key,
  218. $val)
  219. {
  220. $line = false;
  221. if (array_key_exists($key, $this->valueIndex))
  222. {
  223. $line = $this->valueIndex[$key];
  224. }
  225. //
  226. // value not defined yet
  227. //
  228. if ($line === false)
  229. {
  230. $line = sizeof($this->contents) - 1;
  231. $this->contents[$line + 1] = "\n";
  232. $this->contents[] = '?>';
  233. }
  234. //
  235. // identify BINARY
  236. //
  237. $binary = false;
  238. if (is_bool($val))
  239. {
  240. $binary = true;
  241. }
  242. else
  243. {
  244. if (is_numeric($val))
  245. {
  246. $binary = false;
  247. }
  248. else
  249. {
  250. if ($val == 'true')
  251. {
  252. $binary = true;
  253. $val = true;
  254. }
  255. else
  256. {
  257. if ($val == 'false')
  258. {
  259. $binary = true;
  260. $val = false;
  261. }
  262. }
  263. }
  264. }
  265. //
  266. // generate output
  267. //
  268. if ($binary)
  269. {
  270. if ($val === false)
  271. {
  272. $this->contents[$line] = "define(\x22" .
  273. $key .
  274. "\x22,false);\n";
  275. }
  276. else
  277. {
  278. if ($val == 0)
  279. {
  280. $this->contents[$line] = "define(\x22" .
  281. $key .
  282. "\x22,\x220\x22);\n";
  283. }
  284. else
  285. {
  286. $this->contents[$line] = "define(\x22" .
  287. $key .
  288. "\x22,true);\n";
  289. }
  290. }
  291. }
  292. else
  293. {
  294. if (!is_numeric($val))
  295. {
  296. //
  297. // convert NULL
  298. //
  299. if ($val == 'NULL')
  300. {
  301. $val = '';
  302. }
  303. $pos = strpos($val, '$');
  304. if ($pos === false)
  305. {
  306. }
  307. else
  308. {
  309. $piece = substr($val, 0, $pos);
  310. $piece .= "\\$";
  311. $piece .= substr($val, $pos + 1, strlen($val));
  312. $val = $piece;
  313. }
  314. }
  315. $this->contents[$line] = "define(\x22" .
  316. $key .
  317. "\x22,\x22" .
  318. $val .
  319. "\x22);" .
  320. "\n";
  321. }
  322. $this->buildIndex();
  323. }
  324. function removeVariable($key)
  325. {
  326. $this->setVariable('$' . $key, null);
  327. }
  328. function setVariable($key,
  329. $val)
  330. {
  331. $key = $this->ensureLegalVariableName($key);
  332. //
  333. // if this is an array, convert to a string
  334. //
  335. if (is_array($val))
  336. {
  337. $setting = '$' . $key . '=array(';
  338. foreach ($val as $a_key => $value)
  339. {
  340. $setting .= '"' .
  341. $a_key .
  342. '"=>"' .
  343. $value .
  344. '",';
  345. }
  346. $setting = substr($setting, 0, strlen($setting) - 1);
  347. $setting .= ');';
  348. }
  349. else
  350. {
  351. $setting = $val;
  352. }
  353. //
  354. // write value
  355. //
  356. $lookup = true;
  357. if ($this->variableIndex == null)
  358. {
  359. $lookup = false;
  360. }
  361. else
  362. {
  363. if (!array_key_exists($key, $this->variableIndex))
  364. {
  365. $lookup = false;
  366. }
  367. }
  368. $line = false;
  369. if ($lookup)
  370. {
  371. $line = $this->variableIndex[$key];
  372. }
  373. if ($line === false)
  374. {
  375. if ($setting != null)
  376. {
  377. $this->contents[sizeof($this->contents) - 1] = $setting .
  378. "\n";
  379. $this->contents[] = '?>';
  380. }
  381. }
  382. else
  383. {
  384. if ($setting != null)
  385. {
  386. $this->contents[$line] = $setting . "\n";
  387. }
  388. else
  389. {
  390. $this->contents[$line] = null;
  391. }
  392. }
  393. $this->buildIndex();
  394. }
  395. function ensureLegalVariableName($key)
  396. {
  397. $offset = 0;
  398. $check = substr($key, $offset, 1);
  399. if ($check == '$')
  400. {
  401. ++$offset;
  402. $check = substr($key, $offset, 1);
  403. }
  404. if (is_numeric($check))
  405. {
  406. if ($offset == 1)
  407. {
  408. return 'x' .
  409. substr($key, 1, strlen($key));
  410. }
  411. else
  412. {
  413. return 'x' . substr($key, 0, strlen($key));
  414. }
  415. }
  416. return substr($key, 1, strlen($key));
  417. }
  418. function getVariable($key)
  419. {
  420. if ($this->variableIndex == null)
  421. {
  422. return null;
  423. }
  424. $key = $this->ensureLegalVariableName('$' . $key);
  425. //echo "<XMP>Get $key</XMP>";
  426. if (!array_key_exists($key, $this->variableIndex))
  427. {
  428. return null;
  429. }
  430. $line = $this->variableIndex[$key];
  431. if ($line === false)
  432. {
  433. return null;
  434. }
  435. // include_once($this->fileName);
  436. @include($this->fileName);
  437. // include($this->fileName);
  438. //echo "<XMP>FOUND $key LINE $line FILE $this->fileName</XMP>";
  439. return $$key;
  440. }
  441. function write($fileName)
  442. {
  443. $fp = fopen($fileName, 'w');
  444. foreach ($this->contents as $line_num => $line)
  445. {
  446. if ($line != null)
  447. {
  448. fwrite($fp, $line);
  449. }
  450. }
  451. fclose($fp);
  452. }
  453. /*
  454. function dump()
  455. {
  456. foreach ($this->contents as $line_num => $line)
  457. {
  458. echo "Line #<b>{$line_num}</b> : " . $line . "<br/>\n";
  459. }
  460. }
  461. */
  462. }
  463. ?>