Files
EUromat/includes/file.php
2015-01-10 20:07:42 +01:00

162 lines
3.5 KiB
PHP

<?php
/*
* These functions handle file access.
*/
function get_share_id($ip, $saltfile, $visitfile){
$index = crypt($ip, get_salt($saltfile));
$visits = get_visits($index, $visitfile);
if($visits != null and isset($visits['ans'][$index])){
return $index.'-'.sizeof($visits['ans'][$index]);
} else {
return 0;
}
}
function get_answer_string($visitfile, $index, $subindex){
$visits = load_var($visitfile);
if($visits != null){
if(isset($visits['ans']) and isset($visits['ans'][$index]) and isset($visits['ans'][$index][$subindex-1])){
return $visits['ans'][$index][$subindex-1];
}
}
return 0;
}
function add_visit($id, $file, $nocount=false, $ans=''){
$visits = get_visits($id, $file);
if($visits != null){
if($nocount){
$visits['nocount'] = $visits['nocount'] + 1;
} else {
$date = date('Ymd');
if(!isset($visits[$date][$id])){
$visits[$date][$id] = 1;
} else {
$visits[$date][$id] = $visits[$date][$id] + 1;
}
$visits['ans'][$id][] = $ans;
}
set_visits($visits, $file);
} else {
echo '<!-- ERROR konnte Besuche nicht zählen -->';
}
}
function get_visits($id, $file){
if(!file_exists($file)){
$visits[date('Ymd')][$id] = 0;
$visits['nocount'] = 0;
$visits['ans'] = Array();
return $visits;
}
if(is_readable($file)){
$handle = fopen($file, 'r');
$contents = fread($handle, filesize($file));
fclose($handle);
$visits = unserialize($contents);
return $visits;
} else {
echo "<!-- ERROR $file kann nicht gelesen werden! -->";
return null;
}
}
function set_visits($visits, $file){
if(is_writable($file) or (is_writable(dirname($file)) and !file_exists($file))){
$handle = fopen($file, 'w');
if (!fwrite($handle, serialize($visits))) {
echo "<!-- ERROR Kann in die Datei $file nicht schreiben -->";
}
fclose($handle);
} else {
echo "<!-- ERROR $file nicht beschreibbar! -->";
}
}
function get_salt($hashfile){
// a) file does not exist yet
if(!file_exists($hashfile)){
if(is_writable(dirname($hashfile))){
$hash[0] = date('Ymd');
$hash[1] = mt_rand();
$handle = fopen($hashfile, 'w');
if (!fwrite($handle, serialize($hash))) {
echo "<!-- ERROR Kann in die Datei $hashfile nicht schreiben -->";
}
fclose($handle);
} else {
echo "<!-- ERROR Kann Datei $hashfile nicht erstellen! -->";
}
}
if(is_readable($hashfile)){
$handle = fopen($hashfile, 'r');
$contents = fread($handle, filesize($hashfile));
fclose($handle);
$hash = unserialize($contents);
// b) hash is not of today
if($hash[0] != date('Ymd')){
$hash[0] = date('Ymd');
$hash[1] = mt_rand();
$handle = fopen($hashfile, 'w');
if (!fwrite($handle, serialize($hash))) {
echo "<!-- ERROR Kann in die Datei $hashfile nicht schreiben -->";
}
fclose($handle);
}
return $hash[1];
} else {
echo "<!-- ERROR $hashfile kann nicht gelesen werden! -->";
return 1;
}
}
function load_var($file){
if(!file_exists($file)){
return null;
}
if(is_readable($file)){
$handle = fopen($file, 'r');
$contents = fread($handle, filesize($file));
fclose($handle);
$var = unserialize($contents);
return $var;
} else {
echo "<!-- ERROR $file kann nicht gelesen werden! -->";
return null;
}
}
function save_var($file, $var){
if(is_writable($file) or (is_writable(dirname($file)) and !file_exists($file))){
$handle = fopen($file, 'w');
if (!fwrite($handle, serialize($var))) {
echo "<!-- ERROR Kann in die Datei $file nicht schreiben -->";
}
fclose($handle);
} else {
echo "<!-- ERROR $file nicht beschreibbar! -->";
}
}
?>