-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.php
executable file
·112 lines (105 loc) · 2.36 KB
/
common.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
function boolToString($a_val)
{
if($a_val == "true" || $a_val == "1" || $a_val == "yes") return "true";
if($a_val == "false" || $a_val == "0" || $a_val == "no") return "false";
// no bool value, numbers can be returned directly
if(is_numeric($a_val)) return $a_val;
// no bool value, at least quote the string then.
return "'" . $a_val . "'";
}
/** stores the given settings of the given user */
function storeSettings($a_user, $a_settings)
{
$filepath = "usersettings/" . $a_user . ".settings";
$pfile = fopen($filepath, "w+");
if(FALSE == $pfile)
{
return -1;
}
rewind($pfile);
foreach($a_settings AS $thekey=>$thevalue)
{
$theline = "$thekey:$thevalue\r\n";
fwrite($pfile, $theline);
}
fclose($pfile);
return 0;
}
function checkSession()
{
return(($_SESSION['validUser'] == true) && isset($_SESSION['userName']));
}
/* loads the settings of the given user */
function loadSettings($a_user)
{
$filepath = "usersettings/" . $a_user . ".settings";
if(!is_readable($filepath))
{
return null;
}
$pfile = fopen($filepath, "r+");
if(FALSE == $pfile)
{
return null;
}
rewind($pfile);
while (!feof($pfile))
{
$line = fgets($pfile);
if(0 != strlen($line))
{
$tmp = explode(':', $line);
$settings[$tmp[0]] = substr($tmp[1], 0, strlen($tmp[1])-2); // -2 because of trailing CR_LF
}
}
return($settings);
}
/* loads the walks for the given user */
function loadWalks($a_user)
{
$filepath = "usersettings/" . $a_user . ".walks";
if(!is_readable($filepath))
{
return null;
}
$pfile = fopen($filepath, "r");
if(FALSE == $pfile)
{
return null;
}
rewind($pfile);
$walks = array();
while (!feof($pfile))
{
$line = fgets($pfile);
if(0 != strlen($line))
{
$tmp = explode(' ', $line);
$walks[strtoupper($tmp[0])] = substr($tmp[1], 0, strlen($tmp[1])-2); // -2 because of trailing CR_LF
}
}
return $walks;
}
function editWalk($a_user, $a_id, $a_action)
{
switch($a_action)
{
case 'walked':
$filepath = "usersettings/" . $a_user . ".walks";
$pfile = fopen($filepath, "a");
if(FALSE == $pfile)
{
return -1;
}
$today = date('Y-m-d');
$theline = strtoupper($a_id) . " " . $today . "\r\n";
fwrite($pfile, $theline);
fclose($pfile);
return 0;
default:
return -1;
break;
}
}
?>