PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/faxocr/app/external/lib/file_conf.php

https://code.google.com/
PHP | 534 lines | 356 code | 104 blank | 74 comment | 61 complexity | aee9f89e25b5563881e1fc6a49660769 MD5 | raw file
  1. <?php
  2. /*
  3. * Shinsai FaxOCR
  4. *
  5. * Copyright (C) 2009-2011 National Institute of Public Health, Japan.
  6. * All rights Reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. // ===========================================================================
  23. // ??????
  24. // ===========================================================================
  25. require_once "config.php";
  26. require_once "lib/common.php";
  27. //
  28. // ?????????
  29. //
  30. class FileConf {
  31. protected $FileConfObj;
  32. function __construct($file_id)
  33. {
  34. if (FILE_CONF_STORAGE_DEVICE == "filedb") {
  35. $this->FileConfObj = new FileConfFileDb($file_id);
  36. } else {
  37. $this->FileConfObj = new FileConfFile($file_id);
  38. }
  39. }
  40. public function get($item)
  41. {
  42. return $this->FileConfObj->get($item);
  43. }
  44. public function set($item, $value)
  45. {
  46. return $this->FileConfObj->set($item, $value);
  47. }
  48. public function commit()
  49. {
  50. return $this->FileConfObj->commit();
  51. }
  52. public function array_destroy($item)
  53. {
  54. return $this->FileConfObj->array_destroy($item);
  55. }
  56. public function array_set($item, $confs)
  57. {
  58. return $this->FileConfObj->array_set($item, $confs);
  59. }
  60. public function array_getall($item)
  61. {
  62. return $this->FileConfObj->array_getall($item);
  63. }
  64. public function array_commit()
  65. {
  66. return $this->FileConfObj->array_commit();
  67. }
  68. }
  69. //
  70. // ????(file????)?????
  71. //
  72. class FileConfFile {
  73. protected $file_id;
  74. protected $get_confs;
  75. protected $set_confs;
  76. protected $b_exist_get_confs;
  77. protected $set_array_items;
  78. protected $set_array_confs;
  79. function __construct($file_id)
  80. {
  81. $this->file_id = $file_id;
  82. $this->get_confs = array();
  83. $this->set_confs = array();
  84. $this->b_exist_get_conf = false;
  85. $this->set_array_items = array();
  86. $this->set_array_confs = array();
  87. }
  88. //
  89. // ??????????????
  90. //
  91. public function get($item)
  92. {
  93. if (!$this->b_exist_get_confs) {
  94. $conf_path = DST_DIR . $this->file_id . CONF_EXT;
  95. $lines = file($conf_path);
  96. for ($cnt = 0; $cnt < count($lines); $cnt++) {
  97. $line = explode(",", $lines[$cnt]);
  98. $this->get_confs[$line[0]] = trim($line[1]);
  99. }
  100. $this->b_exist_get_confs = true;
  101. }
  102. $result = (isset($this->get_confs[$item])) ? $this->get_confs[$item] : "";
  103. switch ($item) {
  104. case "flag-cc":
  105. case "flag-bc":
  106. case "flag-cb":
  107. case "flag-bb":
  108. case "append_row":
  109. case "append_column":
  110. $result = ($result != "") ? true : false;
  111. break;
  112. }
  113. return $result;
  114. }
  115. //
  116. // ???????????
  117. //
  118. public function set($item, $value)
  119. {
  120. $this->set_confs[$item] = $value;
  121. return NOERR;
  122. }
  123. //
  124. // ????????
  125. //
  126. public function commit()
  127. {
  128. $conf_path = DST_DIR . $this->file_id . CONF_EXT;
  129. $lines = @file($conf_path);
  130. if (!$lines) {
  131. $lines = array();
  132. }
  133. foreach ($this->set_confs as $conf_key => $conf_value) {
  134. $bUpdated = false;
  135. for ($cnt = 0; $cnt < count($lines); $cnt++) {
  136. $line = explode(",", $lines[$cnt]);
  137. if ($conf_key == $line[0]) {
  138. $lines[$cnt] = "{$conf_key},{$conf_value}\n";
  139. $bUpdated = true;
  140. break;
  141. }
  142. }
  143. if (!$bUpdated) {
  144. $lines[$cnt] = "{$conf_key},{$conf_value}\n";
  145. }
  146. }
  147. $fp = fopen($conf_path, "w");
  148. for ($cnt = 0; $cnt < count($lines); $cnt++) {
  149. fwrite($fp, $lines[$cnt]);
  150. }
  151. fclose($fp);
  152. foreach ($this->set_confs as $conf_key => $conf_value) {
  153. $this->get_confs[$conf_key] = $conf_value;
  154. }
  155. $this->set_confs = array();
  156. return NOERR;
  157. }
  158. //
  159. // ???????????
  160. //
  161. public function array_destroy($item)
  162. {
  163. $conf_path = DST_DIR . $this->file_id . ARRAY_CONF_EXT;
  164. if (file_exists($conf_path)) {
  165. $lines = file($conf_path);
  166. } else {
  167. $lines = array();
  168. }
  169. $fp = fopen($conf_path, "w");
  170. for ($cnt = 0; $cnt < count($lines); $cnt++) {
  171. $line = explode(",", $lines[$cnt]);
  172. $items = explode("=", $line[0]);
  173. if ($items[0] != $item) {
  174. fwrite($fp, $lines[$cnt]);
  175. }
  176. }
  177. fclose($fp);
  178. return NOERR;
  179. }
  180. //
  181. // ???conf???????????????
  182. //
  183. public function array_set($item, $confs)
  184. {
  185. $b_exists = false;
  186. if (count($this->set_array_items) > 0) {
  187. foreach ($this->set_array_items as $exists_item) {
  188. if ($exists_item == $item) {
  189. $b_exists = true;
  190. break;
  191. }
  192. }
  193. }
  194. if (!$b_exists) {
  195. $this->set_array_items[] = $item;
  196. }
  197. $this->set_array_confs[$item][] = $confs;
  198. return NOERR;
  199. }
  200. //
  201. // ???conf???????????????
  202. //
  203. public function array_getall($item)
  204. {
  205. $result = array();
  206. $conf_path = DST_DIR . $this->file_id . ARRAY_CONF_EXT;
  207. $lines = @file($conf_path);
  208. if (!$lines) {
  209. $lines = array();
  210. }
  211. $result_cnt = 0;
  212. for ($cnt = 0; $cnt < count($lines); $cnt++) {
  213. $lines[$cnt] = trim($lines[$cnt]);
  214. $line = explode("," , $lines[$cnt]);
  215. $sep_key_value = explode("=", $line[0]);
  216. if ($sep_key_value[0] == $item) {
  217. foreach ($line as $key_value) {
  218. $sep_key_value = explode("=", $key_value);
  219. if ($sep_key_value[0] != $item) {
  220. $result[$result_cnt][$sep_key_value[0]] = $sep_key_value[1];
  221. }
  222. }
  223. $result_cnt++;
  224. }
  225. }
  226. return $result;
  227. }
  228. //
  229. // ????????????
  230. //
  231. public function array_commit()
  232. {
  233. $conf_path = DST_DIR . $this->file_id . ARRAY_CONF_EXT;
  234. $lines = @file($conf_path);
  235. if (!$lines) {
  236. $lines = array();
  237. }
  238. $fp = fopen($conf_path, "w");
  239. foreach ($lines as $line) {
  240. fwrite($fp, $line);
  241. }
  242. foreach ($this->set_array_items as $item) {
  243. for ($cnt = 0; $cnt < count($this->set_array_confs[$item]); $cnt++) {
  244. $confs = $this->set_array_confs[$item][$cnt];
  245. do {
  246. $b_exists = FALSE;
  247. $array_conf_id = genUniqueId();
  248. foreach ($lines as $line) {
  249. $items = explode(",", $line);
  250. $item = explode("=", $items[0]);
  251. if ($item[1] == $array_conf_id) {
  252. $b_exists = TRUE;
  253. break;
  254. }
  255. }
  256. } while ($b_exists);
  257. $line = $item . "=" . $array_conf_id;
  258. foreach ($confs as $key => $value) {
  259. $line .= "," . $key . "=" . $value;
  260. }
  261. $line .= "\n";
  262. fwrite($fp, $line);
  263. }
  264. }
  265. fclose($fp);
  266. $set_array_items = array();
  267. $set_array_confs = array();
  268. return NOERR;
  269. }
  270. }
  271. //
  272. // ????(filedb????)?????
  273. //
  274. class FileConfFileDb
  275. {
  276. protected $file_id;
  277. protected $set_confs;
  278. protected $set_array_items;
  279. protected $set_array_confs;
  280. function __construct($file_id)
  281. {
  282. $this->file_id = $file_id;
  283. $this->set_confs = array();
  284. $set_array_items = array();
  285. $set_array_confs = array();
  286. }
  287. //
  288. // ??????????????
  289. //
  290. public function get($item)
  291. {
  292. $file_db_key = $this->file_id . "-" . $item;
  293. $result = get_string($file_db_key);
  294. if ($result == ERROR) {
  295. $result = "";
  296. }
  297. switch ($item) {
  298. case "flag-cc":
  299. case "flag-bc":
  300. case "flag-cb":
  301. case "flag-bb":
  302. case "append_row":
  303. case "append_column":
  304. break;
  305. $result = ($result != "") ? true : false;
  306. }
  307. return $result;
  308. }
  309. //
  310. // ???????????
  311. //
  312. public function set($item, $value)
  313. {
  314. $this->set_confs[$item] = $value;
  315. return NOERR;
  316. }
  317. //
  318. // ????????
  319. //
  320. public function commit() {
  321. foreach ($this->set_confs as $key => $value) {
  322. $file_db_key = $this->file_id . "-" . $key;
  323. replace_string($file_db_key, $value);
  324. }
  325. $this->set_confs = array();
  326. return NOERR;
  327. }
  328. //
  329. // ???????????
  330. //
  331. public function array_destroy($item)
  332. {
  333. $file_db_list_key = $this->file_id . "-" . $item;
  334. $item_list_str = get_string($file_db_list_key);
  335. if ($item_list_str == ERROR) {
  336. return NOERR;
  337. }
  338. $item_id_list = explode(",", $item_list_str);
  339. for ($cnt = 0; $cnt < count($item_id_list); $cnt++) {
  340. $del_entry($item_id_list[$cnt]);
  341. }
  342. $del_entry($file_db_list_key);
  343. return NOERR;
  344. }
  345. //
  346. // ???conf???????????????
  347. //
  348. public function array_set($item, $confs)
  349. {
  350. $b_exists = false;
  351. if (count($this->set_array_items) > 0) {
  352. foreach ($this->set_array_items as $exists_item) {
  353. if ($exists_item == $item) {
  354. $b_exists = true;
  355. break;
  356. }
  357. }
  358. }
  359. if (!$b_exists) {
  360. $this->set_array_items[] = $item;
  361. }
  362. $this->set_array_confs[$item][] = $confs;
  363. return NOERR;
  364. }
  365. //
  366. // ???conf???????????????
  367. //
  368. public function array_getall($item)
  369. {
  370. $result = array();
  371. $file_db_list_key = $this->file_id . "-" . $item;
  372. $item_list_str = get_string($file_db_list_key);
  373. if ($item_list_str == ERROR) {
  374. return $result;
  375. }
  376. $item_id_list = explode(",", $item_list_str);
  377. $result_cnt = 0;
  378. for ($cnt = 0; $cnt < count($item_id_list); $cnt++) {
  379. $values = get_string($item_id_list[$cnt]);
  380. if ($values != ERROR) {
  381. $line = explode("," , $values);
  382. foreach ($line as $key_value) {
  383. $sep_key_value = explode("=", $key_value);
  384. $result[$result_cnt][$sep_key_value[0]] = $sep_key_value[1];
  385. }
  386. $result_cnt++;
  387. }
  388. }
  389. return $result;
  390. }
  391. //
  392. // ????????????
  393. //
  394. public function array_commit()
  395. {
  396. foreach ($this->set_array_items as $item) {
  397. $file_db_list_key = $this->file_id . "-" . $item;
  398. $item_list_str = get_string($file_db_list_key);
  399. if ($item_list_str == ERROR) {
  400. $item_list_str = "";
  401. }
  402. for ($cnt = 0; $cnt < count($this->set_array_confs[$item]); $cnt++) {
  403. $confs = $this->set_array_confs[$item][$cnt];
  404. do {
  405. $array_conf_id = genUniqueId();
  406. $b_exists = is_exists_key($array_conf_id);
  407. if ($b_exists == ERROR) break;
  408. } while ($b_exists);
  409. if ($b_exists == ERROR) {
  410. return ERROR;
  411. }
  412. $line = "";
  413. foreach ($confs as $key => $value) {
  414. if ($line != "") $line .= ",";
  415. $line .= $key . "=" . $value;
  416. }
  417. replace_string($array_conf_id, $line);
  418. if ($item_list_str != "") {
  419. $item_list_str .= ",";
  420. }
  421. $item_list_str .= $array_conf_id;
  422. }
  423. replace_string($file_db_list_key, $item_list_str);
  424. }
  425. $set_array_items = array();
  426. $set_array_confs = array();
  427. return NOERR;
  428. }
  429. }
  430. ?>