-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
144 lines (141 loc) · 4.9 KB
/
index.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
<?php
header("Content-type: text/html; charset=utf-8");
require_once 'HTTP/OAuth/Consumer.php';
require_once 'config/main.php';
$timer = new timer();
//get url parameter and include right file.
$parameter = isset($_GET['p'])?$_GET['p']:'';
logger::log('Start of request: '.$parameter, ALL);
$account = new account();
if(!$account->authenticated)
{
if(!$account->authenticate())
{
includer::includeFiles(array('header.php', 'login.php', 'footer.php'),
array('account' => $account));
} else {
header('Location: /');
}
}
else
{
$parameters = explode('/',$parameter);
switch($parameters[0])
{
case '':
if($account->initTelldusData())
{
$startTime = tub::getStartTime($account->tubTime, $account->telldusData, $account->settings);
includer::includeFiles(array('header.php', 'default.php', 'footer.php'),
array(
'account' => $account,
'startTime' => $startTime
));
}
else
{
logger::log("Telldus integration error. " . $account->telldusData->errorMessage, ERROR);
includer::includeFiles(array('header.php', '500.php', 'footer.php'),
array('httpResponseError' => true));
}
break;
case 'access-token':
if($account->setAccessTokens()) {
header('Location: /');
} else {
includer::includeFiles(array('header.php', 'login.php', 'footer.php'),
array('account' => $account));
}
break;
case 'test':
if($account->initTelldusData()) {
$turnOn = null;
if($_POST) {
require_once config::libDir().'/webtub/cron.php';
$time = 0;
if($_POST['date'] && $_POST['time']) {
$time = strtotime($_POST['date'] . ' ' . $_POST['time']);
}
$temp = floatval(str_replace(',','.',$_POST['temp']));
$cron = new cron(true);
$turnOn = tub::tubOnOrOff($account->telldusData->data['tubTemp'], $account->telldusData->data['airTemp'], $temp, $time, $account->telldusData->data['tubLastChecked'], $account->settings);
}
includer::includeFiles(array('header.php', 'test.php', 'footer.php'),
array('account' => $account, 'turnOn' => $turnOn));
}
break;
case 'post':
$pageExists = false;
switch($parameters[1])
{
case 'settings':
$account->updateSettings($_POST);
header('Location: /');
$pageExists = true;
break;
case 'tubtime':
if(isset($_POST['delete']))
{
$account->deleteTubTime();
}
else
{
$time = 0;
if($_POST['date'] && $_POST['time']) {
$time = strtotime($_POST['date'] . ' ' . $_POST['time']);
} else if($_POST['datetime']){
$time = strtotime(str_replace('T', ' ', $_POST['datetime']));
}
$temp = floatval(str_replace(',','.',$_POST['temp']));
if($temp <= 20 || $temp > 40) {
$temp = false;
}
if($time && $temp) {
$account->updateOrCreateTubTime($time, $temp);
$account->initTelldusData();
if(tub::tubOnOrOff($account->telldusData->data['tubTemp'], $account->telldusData->data['airTemp'], $temp, $time, time(), $account->settings)) {
$account->telldusData->turnOnDevice($account->settings['tubDeviceId']['value']);
logger::log('Successfully turned on device '.$account->settings['tubDeviceId']['value'] . ' when saving tub time', DEBUG);
$tubTime = $account->getTubTime();
tub::markTubTimeAsActivated($tubTime['id']);
}
header('Location: /');
$pageExists = true;
}
}
break;
case 'turn-on':
$account->initTelldusData();
$deviceId = $account->settings['tubDeviceId']['value'];
$account->telldusData->turnOnDevice($deviceId);
header('Location: /');
$pageExists = true;
break;
case 'turn-off':
$account->initTelldusData();
$deviceId = $account->settings['tubDeviceId']['value'];
$account->telldusData->turnOffDevice($deviceId);
header('Location: /');
$pageExists = true;
break;
}
if($pageExists)
{
break;
}
case 'logout':
$account->logout();
header('Location: /');
break;
default:
// Default to File not found
logger::log('File not found: ' . $_SERVER['REQUEST_URI'], WARNING);
header("HTTP/1.0 404 Not Found");
$pageTitle = 'Page not found | ' . webtub::pageTitle;
includer::includeFiles(array('header.php','404.php','footer.php'),
array('httpResponseError' => true, 'pageTitle' => $pageTitle));
break;
}
}
logger::log('Time to render page: '. $timer->endTimer() .' Request: '. $parameter, ALL);
?>