forked from emoncms/event_archived
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_controller.php
152 lines (128 loc) · 5.38 KB
/
event_controller.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
<?php
/*
All Emoncms code is released under the GNU Affero General Public License.
See COPYRIGHT.txt and LICENSE.txt.
---------------------------------------------------------------------
Emoncms - open source energy visualisation
Part of the OpenEnergyMonitor project:
http://openenergymonitor.org
*/
// no direct access
defined('EMONCMS_EXEC') or die('Restricted access');
function event_controller()
{
global $mysqli,$redis,$user, $session, $route, $timestore_adminkey;
global $feed;
include "Modules/feed/feed_model.php";
$feed = new Feed($mysqli,$redis,$timestore_adminkey);
require "Modules/event/event_model.php";
$event = new Event($mysqli,$redis);
$userid = $session['userid'];
if ($route->action == 'add' && $session['write'])
{
$eventfeed = intval(get('eventfeed'));
$eventtype = intval(get('eventtype'));
$eventvalue = floatval(get('eventvalue'));
$action = intval(get('action'));
$setfeed = intval(get('setfeed'));
$setemail = get('setemail');
$setvalue = floatval(get('setvalue'));
$callcurl = get('callcurl');
$mutetime = get('mutetime');
$priority = get('priority');
$message = get('message');
$mqtttopic = get('mqtttopic');
$mqttqos = get('mqttqos');
$event->add($userid,$eventfeed,$eventtype,$eventvalue,$action,$setfeed,$setemail,$setvalue,$callcurl,$message,$mutetime,$priority,$mqtttopic,$mqttqos);
$result = "Event added";
}
if ($route->action == 'edit' && $session['write'])
{
$eventid = intval(get('eventid'));
$eventfeed = intval(get('eventfeed'));
$eventtype = intval(get('eventtype'));
$eventvalue = floatval(get('eventvalue'));
$action = intval(get('action'));
$setfeed = intval(get('setfeed'));
$setemail = get('setemail');
$setvalue = floatval(get('setvalue'));
$callcurl = get('callcurl');
$mutetime = get('mutetime');
$priority = get('priority');
$message = get('message');
$mqtttopic = get('mqtttopic');
$mqttqos = get('mqttqos');
$event->update($userid,$eventid,$eventfeed,$eventtype,$eventvalue,$action,$setfeed,$setemail,$setvalue,$callcurl,$message,$mutetime,$priority,$mqtttopic,$mqttqos);
$result = "Event updated";
}
else if ($route->action == 'delete' && $session['write'])
{
$id = intval(get('id'));
$event->delete($userid,$id);
$result = "Event deleted";
}
else if ($route->action == 'status' && $session['write'])
{
$id = intval(get('id'));
$status = intval(get('status'));
$event->set_status($userid,$id,$status);
$result = "Event deleted";
}
else if ($route->action == 'test' && $session['write'])
{
$id = intval(get('id'));
$feedid = intval(get('feedid'));
$event->test($userid,$id,$feedid);
$result = "Event Test Sent";
}
else if ($route->action == 'settings' && $session['write'])
{
$settings = $event->get_settings($session['userid']);
$result = view("Modules/event/event_settings_view.php", array('settings'=>$settings));
}
//--------------------------------------------------------------------------
// SET TWITTER
// http://yoursite/emoncms/user/settwitter
//--------------------------------------------------------------------------
else if ($route->action == 'savesettings' && $session['write'])
{
// Store userlang in database
$prowlkey = post('prowlkey');
$nmakey = post('nmakey');
$smtpserver = post('smtpserver');
$smtpuser = post('smtpuser');
$salt = $user->get_salt($session['userid']);
$ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC");
$iv = openssl_random_pseudo_bytes($ivlen);
$ciphertext_raw = openssl_encrypt(post('smtppassword'), $cipher, $salt, $options=OPENSSL_RAW_DATA, $iv);
$hmac = hash_hmac('sha256', $ciphertext_raw, $salt, $as_binary=true);
$smtppassword = base64_encode( $iv.$hmac.$ciphertext_raw );
$smtpport = preg_replace('/[^\w\s-]/','',post('smtpport'));
$consumerkey = post('consumerkey');
$consumersecret = post('consumersecret');
$usertoken = post('usertoken');
$usersecret = post('usersecret');
$mqttbrokerip = post('mqttbrokerip');
$mqttbrokerport = post('mqttbrokerport');
$mqttusername = post('mqttusername');
$ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC");
$iv = openssl_random_pseudo_bytes($ivlen);
$ciphertext_raw = openssl_encrypt(post('mqttpassword'), $cipher, $salt, $options=OPENSSL_RAW_DATA, $iv);
$hmac = hash_hmac('sha256', $ciphertext_raw, $salt, $as_binary=true);
$mqttpassword = base64_encode( $iv.$hmac.$ciphertext_raw );
$result = $event->set_settings($session['userid'],$prowlkey,$consumerkey,$consumersecret,$usertoken,$usersecret,$smtpserver,$smtpuser,$smtppassword,$smtpport,$nmakey,$mqttbrokerip,$mqttbrokerport, $mqttusername, $mqttpassword);
if ($result) {
$result = "Event settings updated!";
}
else {
$result = "Error occours while updating settings!";
}
}
else if ($session['write'])
{
$list = $event->eventlist($userid);
$feeds = $feed->get_user_feeds($userid);
$result = view("Modules/event/event_list.php", array('event_list'=>$list, 'feeds'=>$feeds));
}
return array('content'=>$result);
}