/application/libraries/zetro_manager.php

https://gitlab.com/zetrosoft/pos-accounting · PHP · 491 lines · 385 code · 69 blank · 37 comment · 96 complexity · 174c92cd02afd9957bd3c4b94492bfce MD5 · raw file

  1. <?php
  2. class zetro_manager{
  3. var $CRLF;
  4. var $bi_a;
  5. var $filePATH;
  6. function __construct (){ //zetro_manager(){
  7. $info = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  8. $this->crlf = ( strpos( strtolower( $info ), "windows", 0 ) === false ) ? "\n" : "\r\n" ;
  9. unset( $this->bi_a );
  10. }
  11. function parse_ini_file($filename){
  12. unset( $this->bi_a );
  13. // Allocate the result array
  14. $res = array();
  15. // Does the file exists and can we read it?
  16. if(file_exists($filename) && is_readable($filename))
  17. {
  18. // In the beggining we are not in a section
  19. $section = "";
  20. // Open the file
  21. $fd = @fopen($filename,"r");
  22. // Read each line
  23. while(!feof($fd))
  24. {
  25. // Read the line and trim it
  26. $line = trim(@fgets($fd, 4096 ));
  27. $len = strlen($line);
  28. // Only process non-blank lines
  29. if($len != 0)
  30. {
  31. // Only process non-comment lines
  32. if($line[0] != ';')
  33. {
  34. // Found a section?
  35. if( ( $line[0] == '[') && ($line[$len-1] == ']' ) )
  36. {
  37. // Get section name
  38. $section = substr($line,1,$len-2);
  39. // Check if the section is already included in result array
  40. if(!isset($res[$section]))
  41. {
  42. // If not included create it
  43. $res[ $section ] = array();
  44. }
  45. }
  46. // Check for entries
  47. $pos = strpos($line,'|'); //'original =
  48. // Found an entry
  49. if( $pos != false )
  50. {
  51. // get name of entry and [Joao Borges] delete any blank spaces (begin and end)
  52. $name = trim( substr( $line, 0, $pos ) );
  53. // get value of entry and [Joao Borges] delete blank spaces again
  54. $value = trim( substr( $line, $pos+1, $len - $pos - 1 ) );
  55. $value = stripslashes( $value );
  56. // follows some sort of inizialization for entries not including text
  57. if ( empty( $value ) ) $value = "" ;
  58. // syntax must be strictly followed !
  59. if ( strlen( $name ) > 0 ) {
  60. // Store entry if we are inside a section
  61. ( strlen( $section ) > 0 )? $res[$section][$name] = $value:$res[$name] = $value;
  62. }
  63. }
  64. }
  65. }
  66. }
  67. // Close the file
  68. @fclose($fd);
  69. }
  70. return $res;
  71. }
  72. function show_content( $path )
  73. {
  74. $this->filePATH = $path ;
  75. $INIarray = $this->parse_ini_file( $path );
  76. $fileCONTENTS = "" ;
  77. $c1 = 0 ;
  78. if ( is_array( $INIarray ) )
  79. {
  80. foreach ( $INIarray as $i => $a )
  81. {
  82. $c2 = 0 ;
  83. if ( is_array( $a ) )
  84. {
  85. foreach ( $a as $n => $value )
  86. {
  87. if ( $c2 == 0 ) $fileCONTENTS .= "[".($i)."]<br/>".("$this->CRLF$n")."=".($value)."<br/>$this->CRLF";
  88. else if ( strlen( $value ) != 0 ) $fileCONTENTS .= ($n)."=".($value)."<br/>$this->CRLF";
  89. $c2++;
  90. }
  91. $fileCONTENTS .= "<br/>$this->CRLF" ;
  92. }
  93. }
  94. $fileCONTENTS = substr( $fileCONTENTS, 0, strlen( $fileCONTENTS ) - ( 5 + strlen( "<br/>" ) ) );
  95. }
  96. echo "<code>$fileCONTENTS</code>" ;
  97. }
  98. function save_content( $path )
  99. {
  100. $fileCONTENTS = "" ;
  101. $c1 = 0 ;
  102. if ( is_array( $this->bi_a ) )
  103. {
  104. foreach ( $this->bi_a as $i => $a)
  105. {
  106. $c2 = 0 ;
  107. if ( is_array( $a ) )
  108. {
  109. foreach ( $a as $n => $value )
  110. {
  111. if ( $c2 == 0 ) $fileCONTENTS .= "[$i]$this->crlf$n|$value$this->crlf";
  112. else if ( strlen( $value ) != 0 ) $fileCONTENTS .= "$n|$value$this->crlf";
  113. $c2++;
  114. }
  115. $fileCONTENTS .= $this->crlf ;
  116. }
  117. }
  118. $hFile = @fopen( $path, "w+" );
  119. @fwrite( $hFile, $fileCONTENTS );
  120. @fclose( $hFile );
  121. unset( $this->bi_a );
  122. }
  123. }
  124. function get_content_array( $path )
  125. {
  126. $this->filePATH = $path ;
  127. $INIarray = $this->parse_ini_file( $path );
  128. return $INIarray ;
  129. }
  130. //////////////////////////////////////////////////////////
  131. function find_content( $path, $keyNAME, $entryNAME )
  132. {
  133. $this->filePATH = $path ;
  134. $INIarray = $this->parse_ini_file( $path );
  135. if ( is_array( $INIarray ) )
  136. {
  137. foreach ( $INIarray as $i => $a )
  138. {
  139. if ( is_array( $a ) )
  140. {
  141. foreach ( $a as $n => $value )
  142. {
  143. if ( strcmp( $i, $keyNAME ) == 0 && strcmp( $n, $entryNAME ) == 0 )
  144. {
  145. return true ;
  146. }
  147. }
  148. }
  149. }
  150. }
  151. return false ;
  152. }
  153. //////////////////////////////////////////////////////////
  154. function get_content( $path, $keyNAME, $entryNAME )
  155. {
  156. $this->filePATH = $path ;
  157. $INIarray = $this->parse_ini_file( $path );
  158. if ( is_array( $INIarray ) )
  159. {
  160. foreach ( $INIarray as $i => $a )
  161. {
  162. if ( is_array( $a ) )
  163. {
  164. foreach ( $a as $n => $value )
  165. {
  166. if ( strcmp( $i, $keyNAME ) == 0 && strcmp( $n, $entryNAME ) == 0 )
  167. {
  168. // [Ulrich Zdebel] Bugfix: doublequotes were not correctly managed
  169. return (stripslashes( $value )) ;
  170. }
  171. }
  172. }
  173. }
  174. }
  175. return "" ;
  176. }
  177. //////////////////////////////////////////////////////////
  178. function add_content( $path, $keyNAME, $entryNAME='', $entryVALUE='' )
  179. {
  180. if ( $this->find_content( $path, $keyNAME, $entryNAME ) )
  181. {
  182. $this->edit_content( $path, $keyNAME, $entryNAME, $entryVALUE ) ;
  183. return ;
  184. }
  185. $this->filePATH = $path ;
  186. $INIarray = $this->parse_ini_file( $path );
  187. $this->bi_a = array();
  188. $bKEYfound = false ;
  189. $bKEYadded = false ;
  190. if ( is_array( $INIarray ) )
  191. {
  192. foreach ($INIarray as $i => $a)
  193. {
  194. if ( is_array( $a ) )
  195. {
  196. foreach ($a as $n => $value)
  197. {
  198. if ( strcmp( $i, $keyNAME ) == 0 ) $bKEYfound = true ;
  199. $this->bi_a[$i][$n] = $a[$n] ;
  200. }
  201. if ( $bKEYfound )
  202. {
  203. $this->bi_a[$i][$entryNAME] = $entryVALUE ;
  204. $bKEYfound = false ;
  205. $bKEYadded = true ;
  206. }
  207. }
  208. }
  209. }
  210. if ( !$bKEYadded ) $this->bi_a[$keyNAME][$entryNAME] = $entryVALUE ;
  211. $this->save_content( $path );
  212. }
  213. //////////////////////////////////////////////////////////
  214. function edit_content( $path, $keyNAME, $entryNAME, $entryVALUE )
  215. {
  216. $this->filePATH = $path ;
  217. $INIarray = $this->parse_ini_file( $path );
  218. $this->bi_a = array();
  219. if ( is_array( $INIarray ) )
  220. {
  221. foreach ($INIarray as $i => $a)
  222. {
  223. if ( is_array( $a ) )
  224. {
  225. foreach ($a as $n => $value)
  226. {
  227. if ( strcmp( $i, $keyNAME ) == 0 && strcmp( $n, $entryNAME ) == 0 )
  228. {
  229. $this->bi_a[$i][$n] = $entryVALUE ;
  230. }
  231. else $this->bi_a[$i][$n] = $a[$n] ;
  232. }
  233. }
  234. }
  235. }
  236. $this->save_content( $path );
  237. }
  238. //////////////////////////////////////////////////////////
  239. function del_content($keyNAME,$entryNAME,$path )
  240. {
  241. $this->filePATH = $path ;
  242. $INIarray = $this->parse_ini_file( $path );
  243. $this->bi_a = array();
  244. if ( is_array( $INIarray ) )
  245. {
  246. foreach ($INIarray as $i => $a)
  247. {
  248. if ( is_array( $a ) )
  249. {
  250. foreach ($a as $n => $value)
  251. {
  252. if ( strcmp( $i, ($keyNAME) ) == 0 &&
  253. strcmp( $n, ($entryNAME) ) == 0 )
  254. {
  255. // don't do anything !
  256. } else $this->bi_a[$i][$n] = $a[$n] ;
  257. echo strcmp( $i, ($keyNAME) );//echo"akan di hapus";
  258. }
  259. }
  260. }
  261. }
  262. $this->save_content( $path );
  263. }
  264. //////////////////////////////////////////////////////////
  265. function del_field( $path, $keyNAME )
  266. {
  267. $fileCONTENTS = "" ;
  268. $this->filePATH = $path ;
  269. $INIarray = $this->parse_ini_file( $path );
  270. if ( is_array( $INIarray ) )
  271. {
  272. foreach ($INIarray as $i => $a)
  273. {
  274. $c2 = 0 ;
  275. if ( is_array( $a ) )
  276. {
  277. foreach ($a as $n => $value)
  278. {
  279. if (strcmp( $i, $keyNAME ) != 0 )
  280. {
  281. if ( $c2 == 0 ) $fileCONTENTS .= "[$i]$this->crlf$n|$value$this->crlf";
  282. else if ( strlen( $value ) != 0 ) $fileCONTENTS .= "$n|$value$this->crlf";
  283. $c2++;
  284. }
  285. }
  286. $fileCONTENTS .= $this->crlf ;
  287. }
  288. }
  289. }
  290. $hFile = @fopen( $path, "w+" );
  291. @fwrite( $hFile, $fileCONTENTS );
  292. @fclose( $hFile );
  293. }
  294. //////////////////////////////////////////////////////////
  295. function del_all_field( $path )
  296. {
  297. $fileCONTENTS = "" ;
  298. $this->filePATH = $path ;
  299. $INIarray = $this->parse_ini_file( $path );
  300. if ( is_array( $INIarray ) )
  301. {
  302. foreach ($INIarray as $i => $a)
  303. {
  304. $c2 = 0 ;
  305. if ( is_array( $a ) )
  306. {
  307. foreach ($a as $n => $value)
  308. {
  309. if ( $c2 == 0 ) $fileCONTENTS .= "[$i]$this->crlf";
  310. $c2++ ;
  311. }
  312. $fileCONTENTS .= $this->crlf ;
  313. }
  314. }
  315. }
  316. $hFile = @fopen( $path, "w+" );
  317. @fwrite( $hFile, $fileCONTENTS );
  318. @fclose( $hFile );
  319. }
  320. //////////////////////////////////////////////////////////
  321. function Count($keyName,$path){
  322. $this->filePATH = $path ;
  323. $INIarray = $this->parse_ini_file( $path );
  324. if ( is_array( $INIarray ) )
  325. {
  326. $c2 = 0;
  327. foreach ($INIarray as $i => $a)
  328. { if ( is_array( $a ) )
  329. {foreach ($a as $n => $value){
  330. if($i==($keyName)){$c2++;}}}
  331. }
  332. }
  333. return $c2;
  334. }
  335. //if (is_array($a)){$c2++;}
  336. //////////////////////////////////////////////////////////
  337. function rContent($keyName,$entryName,$path)
  338. {
  339. $entry_val=$this->get_content($path,($keyName),($entryName));
  340. return $entry_val;
  341. }
  342. /////////////////////////////////////////////////////////
  343. function field( $keyNAME,$path ){
  344. $this->filePATH = $path ;
  345. $INIarray = $this->parse_ini_file( $path );
  346. $fileCONTENTS = "" ;
  347. $c1 = 0 ;
  348. if ( is_array( $INIarray ) )
  349. {
  350. foreach ( $INIarray as $i => $a )
  351. {
  352. $c2 = 0 ;
  353. if ( is_array( $a ) )
  354. {
  355. foreach ( $a as $n => $value )
  356. {
  357. if(($i)==$keyNAME){
  358. if ( $c2 == 0 ) $fileCONTENTS .= "[".($i)."]<br/>".("$this->CRLF$n")."=".($value)."<br/>$this->CRLF";
  359. else if ( strlen( $value ) != 0 ) $fileCONTENTS .= ($n)."=".($value)."<br/>$this->CRLF";
  360. $c2++;
  361. }
  362. }
  363. $fileCONTENTS .= "$this->CRLF" ;
  364. }
  365. }
  366. $fileCONTENTS = substr( $fileCONTENTS, 0, strlen( $fileCONTENTS ) - ( 5 + strlen( "<br/>" ) ) );
  367. }
  368. return $fileCONTENTS;
  369. }
  370. function show_field( $path )
  371. {
  372. $this->filePATH = $path ;
  373. $INIarray = $this->parse_ini_file( $path );
  374. $fileCONTENTS = "" ;
  375. $c1 = 0 ;
  376. if ( is_array( $INIarray ) )
  377. {
  378. foreach ( $INIarray as $i => $a )
  379. {
  380. $c2 = 0 ;
  381. if ( is_array( $a ) )
  382. {
  383. foreach ( $a as $n => $value )
  384. {
  385. if ( $c2 == 0 ) $fileCONTENTS .= ($i).",";
  386. $c2++;
  387. }
  388. $fileCONTENTS .= "$this->CRLF" ;
  389. }
  390. }
  391. $fileCONTENTS = substr( $fileCONTENTS, 0, strlen( $fileCONTENTS )-1/* - ( 5 + strlen( "<br/>" ) )*/ );
  392. }
  393. echo $fileCONTENTS ;
  394. }
  395. function ReadSec($keyNAME,$path,$cari='S',$loop='',$select=''){
  396. $data=explode(",",$this->field($keyNAME,$path));
  397. $nfile=$path;
  398. if ($cari=='S'){
  399. //for ($i=0;$i<=count($data)-1;$i++){
  400. $xx=trim($data[$loop]);
  401. $readSec="<option value='$xx'";
  402. if($xx==$select){$readSec .=" selected";}
  403. $readSec .=">".$this->rContent($keyNAME,$xx,$nfile)."</option>\n";
  404. // }
  405. }else if ($cari=='L'){
  406. //for ($i=0;$i<=count($data)-1;$i++){
  407. $xx=trim($data[$loop]);
  408. if($xx==$select){$readSec .=" selected";}
  409. $readSec=$this->rContent($keyNAME,"$xx",$nfile)."\n";
  410. // }
  411. }else if($cari!='S'){
  412. while($pos=current($data)){
  413. if($pos==$cari){
  414. $xx=$data[key($data)];
  415. $readSec=$this->rContent($keyNAME,"$xx",$nfile);
  416. }
  417. next($data);
  418. }
  419. }
  420. return $readSec;
  421. }
  422. }//end class