battles/file_db_func.php
2018-01-28 18:40:49 +02:00

87 lines
1.9 KiB
PHP

<?
ini_set("display_errors", 1);
error_reporting(E_ALL ^ E_NOTICE);
if(!@$start_dir) { $start_dir = '/tmp/backup/battle/'; }
function select_ff($name, $key = '*') {
global $start_dir;
if(file_exists($start_dir.$name)) {
$lines = file($start_dir.$name);
foreach($lines as $k => $v) {
$kv = explode('@=||=@', $v);
if($key == '*') {
$arr[$kv[0]] = $kv[1];
} elseif ($kv[0] == $key) {
$arr = $kv[1];
}
}
return $arr;
} else {
return false;
}
}
function select_line_to_file($name, $query, $key = '*') {
global $start_dir;
$r = mysql_fetch_assoc(mysql_query($query));
if($key != '*') {
if(file_exists($start_dir.$name)) {
$oldl = ($start_dir.$name);
foreach($oldl as $k => $v) {
$kv = explode('@=||=@', $v);
$oldv[$kv[0]]=$kv[1];
}
}
}
$nf = fopen($start_dir.$name, 'w');
if($key != '*') {
$found = false;
foreach($oldv as $k => $v) {
if($k == $key) {
$found = true;
$v = $r[$k];
}
fputs($nf, $k."@=||=@".$v."@=||=@\n");
}
if($found == false) {
fwrite($nf,$key."@=||=@".$r[$key]."@=||=@\n");
}
} else {
foreach($r as $k => $v) {
fwrite($nf,$k."@=||=@".$v."@=||=@\n");
}
}
fclose($nf);
return $r;
}
function update_file($name, $key, $val, $inc = false) {
global $start_dir;
$ex = file_exists($start_dir.$name);
if($ex) {
$oldl = ($start_dir.$name);
foreach($oldl as $k => $v) {
$kv = explode('@=||=@', $v);
$oldv[$kv[0]] = $kv[1];
}
}
if($ex || $inc == false) {
$nf = fopen($start_dir.$name, 'w');
$found = false;
if(is_array($oldv))
foreach($oldv as $k => $v) {
if($k == $key) {
$found = true;
$v = $inc == false ? $val:$v+$val;
}
fputs($nf,$k."@=||=@".$v."@=||=@\n");
}
if($found == false) {
fwrite($nf,$key."@=||=@".$val."@=||=@\n");
}
fclose($nf);
return "OK";
}
return false;
}
?>