-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.php
231 lines (207 loc) · 10.2 KB
/
models.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
require_once('config.php');
mysql_connect($databaseserver, $databaseuser, $databasepassword);
mysql_select_db($databasename) or die ("Unable to select database!");
function load_user(){
if ($_COOKIE and array_key_exists("validuser", $_COOKIE))
return new DBUser($_COOKIE["validuser"]);
else
return new SessionUser();
}
function log_event($message, $to){
$log = fopen($to, 'a');
fwrite($log, date("D j M Y - G:i:s")."\n\t".$message."\n");
fclose($log);
}
function escape($s){
$res = str_replace('"', '\"', $s);
return $res;
}
function run_sql($query){
global $dblog;
log_event($query, $dblog);
$tmp = mysql_query($query) or die ("Error in query:". $query." ".mysql_error());
return $tmp;
}
class User {
public $channels;
public $name;
public function __construct(){
$this->load_channels();
}
public function update_channel(){
if ($_GET) $this->setChannel($_GET["channel"], $_GET["to"] == "on");
}
public function update_show(){
if ($_GET) $this->setShow($_GET["show"], $_GET["rating"]);
}
public function getTimeFlag($period) {
return $period != "tomorrow" ? 2 : 1;
}
public function update_groups(){
if ($_GET) $this->setGroup($_GET["period"], $_GET["to"]);
}
public function setGroup($group, $to){ }
public function getShowInstance($start, $end, $name){
$channellist = array();
foreach ($this->channels as $channel) {
if ($channel["default?"]) array_push($channellist, $channel["ChannelName"]);
}
$channellist = implode('", "', $channellist);
$query = 'SELECT showname, starttime, channelname, endtime, discription FROM tvshowinstance';
$query = $query.' WHERE channelname IN ("'.$channellist.'") AND '.$start.' AND '.$end.' AND "'.escape($name).'"= showname ';
$query = $query.' ORDER BY starttime;';
$result = run_sql($query);
$answer = array();
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)) {
$data = array("Show Name"=>$row[0], "Start Time"=>strtotime($row[1]), "End Time"=>strtotime($row[3]), "Channel Name"=>$row[2], "Description"=>$row[4]);
array_push($answer, $data);
}
mysql_free_result($result);
}
return $answer;
}
}
class DBUser extends User{
public function __construct($name){
$this->name = $name;
parent::__construct();
}
public function setGroup($group, $to){
$mapping = array('now'=>'onnow', 'soon'=>'soon', 'later'=>'today', 'tomorrow'=>'tomorrow');
run_sql('UPDATE user SET '.$mapping[$group].' = '.( $to == 'on'? 1 : 0 ).' WHERE username = "'.escape($this->name).'" ;');
}
public function getTimeFlag($period) {
$mapping = array('now'=>'onnow', 'soon'=>'soon', 'later'=>'today', 'tomorrow'=>'tomorrow');
$result = run_sql('SELECT '.$mapping[$period].' FROM user WHERE username="'.escape($this->name).'" ;');
if (mysql_num_rows($result) > 0){
$row = mysql_fetch_row($result);
return $row[0] == '1' ? 2 : 1;
}
else {
return $period != "tomorrow" ? 2 : 1;
}
}
public function setShow($name, $rating){
if ($rating){
$result = run_sql('SELECT * FROM tvshowrating WHERE username="'.escape($this->name).'" AND showname="'.escape($name).'";');
if (mysql_num_rows($result) > 0 )
run_sql('UPDATE tvshowrating SET rating='.escape($rating).', lastset=NOW() WHERE username="'.escape($this->name).'" AND showname="'.escape($name).'";');
else
run_sql('INSERT tvshowrating SET rating='.escape($rating).', lastset=NOW(), username="'.escape($this->name).'", showname="'.escape($name).'";');
}
else{
run_sql('DELETE FROM tvshowrating WHERE username="'.escape($this->name).'" AND showname="'.escape($name).'";');
}
}
public function getShows($start, $end, $minrating, $null){
$channellist = array();
foreach ($this->channels as $channel) {
if ($channel["default?"]) array_push($channellist, $channel["ChannelName"]);
}
$channellist = implode('", "', $channellist);
$query = 'SELECT tvshowinstance.showname, starttime, channelname, endtime, rating ';
$query = $query.' FROM tvshowinstance LEFT JOIN tvshowrating ON tvshowinstance.showname = tvshowrating.showname AND username="'.escape($this->name).'" ';
$query = $query.' WHERE channelname IN ("'.$channellist.'") AND '.$start.' AND '.$end.' ';
if ($null)
$query = $query.' AND ( '.escape($minrating).' <= rating OR rating IS NULL ) ';
else
$query = $query.' AND '.escape($minrating).' <= rating ';
$query = $query.' ORDER BY starttime, endtime, channelname;';
$result = run_sql($query);
$answer = array();
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)) {
$data = array("Show Name"=>$row[0], "Start Time"=>strtotime($row[1]), "End Time"=>strtotime($row[3]), "Channel Name"=>$row[2], "Rating"=>$row[4]);
$data["HTML Name"] = htmlentities($data["Show Name"], ENT_QUOTES);
$data["URL Name"] = urlencode($data["Show Name"]);
if (!$data["Rating"])
$data["Rating"] = 0;
array_push($answer, $data);
}
mysql_free_result($result);
}
return $answer;
}
public function load_channels(){
$query = 'SELECT channel.channelName, state ';
$query = $query.' FROM channel LEFT JOIN userchannels ON channel.channelname = userchannels.channelname AND username = "'.escape($this->name).'" ';
$query = $query.' WHERE storeddays > 0 ORDER BY channel.channelname';
$result = run_sql($query);
$this->channels = array();
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result))
array_push($this->channels, array("ChannelName"=>$row[0], "default?"=>$row[1]));
mysql_free_result($result);
}
}
public function setChannel($ChannelName, $state){
$result = run_sql('SELECT * FROM userchannels WHERE username="'.escape($this->name).'" AND channelname="'.escape($ChannelName).'";');
if (mysql_num_rows($result) > 0)
run_sql('UPDATE userchannels SET set_on=NOW(), state="'.escape($state).'" WHERE username = "'.escape($this->name).'" AND channelname = "'.escape($ChannelName).'";');
else
run_sql('INSERT userchannels SET set_on=NOW(), state="'.escape($state).'", username = "'.escape($this->name).'", channelname = "'.escape($ChannelName).'";');
}
}
class SessionUser extends User{
public function load_channels(){
if($_SESSION['channels']){
$this->channels = $_SESSION['channels'];
}
else{
$this->channels = array();
$result = run_sql('SELECT channelName, standard, storeddays FROM channel WHERE storeddays > 0 ORDER BY channelname;');
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)) {
$data = array("ChannelName"=>$row[0], "default?"=>$row[1]);
array_push($this->channels, $data);
}
mysql_free_result($result);
}
$_SESSION['channels'] = $this->channels;
$_SESSION['shows'] = array();
}
}
public function getShows($start, $end, $minrating, $null){
$channellist = array();
foreach ($this->channels as $channel) {
if ($channel["default?"]) array_push($channellist, $channel["ChannelName"]);
}
$channellist = implode('", "', $channellist);
$query = 'SELECT showname, starttime, channelname, endtime FROM tvshowinstance';
$query = $query.' WHERE channelname IN ("'.$channellist.'") AND '.$start.' AND '.$end.' ';
$query = $query.' ORDER BY starttime;';
$result = run_sql($query);
$answer = array();
if ($null){
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)) {
$data = array("Show Name"=>$row[0], "Start Time"=>strtotime($row[1]), "End Time"=>strtotime($row[3]), "Channel Name"=>$row[2]);
$data["HTML Name"] = htmlentities($data["Show Name"], ENT_QUOTES);
$data["URL Name"] = urlencode($data["Show Name"]);
$data["Rating"] = $_SESSION['shows'][$data['Show Name']];
if (!$data["Rating"])
$data["Rating"] = 0;
if($data["Rating"] >= $minrating || (!$data["Rating"] && $null))
array_push($answer, $data);
}
mysql_free_result($result);
}
}
return $answer;
}
public function setChannel($ChannelName, $state){
foreach($_SESSION['channels'] as $key=>$info){
if ($info['ChannelName'] == $ChannelName)
$_SESSION['channels'][$key]['default?'] = $state;
}
}
public function setShow($name, $rating){
$name = str_replace("\'", "'", $name);
echo $name."<br />".$rating;
$_SESSION['shows'][$name] = $rating;
echo "<br />".$_SESSION['shows'][$name];
}
}
?>