diff --git a/README.md b/README.md
index bb8a0f4..18ebfdb 100644
--- a/README.md
+++ b/README.md
@@ -1,20 +1,16 @@
-event
-=====
+XaroRSA Event Module additions:
-Setup actions to occur when a feed goes above, below or is equal a set value. Set another feed or send an email.
+Twilio SMS function
+Pre-Mute function
-Prowl, NMA, Curl, Twitter and Email methods native to application.
+Twilio:
+ How to use:
+ 1: Sign up for Twilio trail or full account at www.Twilio.com
+ 2: Enter ACCOUNT SID and AUTH TOKEN into event Settings.
+ 3: Look under Numbers tab in Twilio account page. They allocate a number to use. Enter number into "FromNumber" when creating new event in format of +12345124124(This works, not sure about brackets and dashes)
+ 4: On trail Twilio accounts, number you want to send to must be verified. Do this via Twilio page.
+ 5: Once number is verified, enter number into "ToNumber" tab when creating new event.
-Email currently is biased towards gmail SMTP server.
-
-Requirements
-
-Run the next command at your (linux) shell to ensure that PHP5 scripts can run:
-`sudo apt-get install php5-mcrypt php5-curl`
-
-NMA - Notify my android
-
-NMA is a message API for sending the events to your android phone or tablet.
-You need to register at: https://www.notifymyandroid.com/ and make an API key.
-Second you have to enter the API key at the event settings (at emoncms).
-Beware that you have a treshold of only 5 messages a day with a trial account!
+Pre-mute:
+ Set a pre-mute time before sending notification. After pre-mute time is reached, regular post-mute function runs eg. 15seconds pre-mute, after 15seconds, every 30seconds post-mute if alarm remains active.
+ Note: only works when values are continuously posted. Will not automatic "check" after time-outs are reached.
\ No newline at end of file
diff --git a/event_controller.php b/event_controller.php
index 6139bd9..6c7b7c0 100644
--- a/event_controller.php
+++ b/event_controller.php
@@ -38,9 +38,16 @@ function event_controller()
$priority = get('priority');
$message = get('message');
- $event->add($userid,$eventfeed,$eventtype,$eventvalue,$action,$setfeed,$setemail,$setvalue,$callcurl,$message,$mutetime,$priority);
+ //Twilio added XaroRSA
+ $fromNumber = get('fromNumber');
+ $toNumber = get('toNumber');
+ $premute = get('premute');
+
+ $event->add($userid,$eventfeed,$eventtype,$eventvalue,$action,$setfeed,$setemail,$setvalue,$callcurl,$message,$mutetime,$priority,$fromNumber,$toNumber,$premute);
+
$result = "Event added";
}
+
if ($route->action == 'edit' && $session['write'])
{
$eventid = intval(get('eventid'));
@@ -55,8 +62,13 @@ function event_controller()
$mutetime = get('mutetime');
$priority = get('priority');
$message = get('message');
+
+ //Twilio added XaroRSA
+ $fromNumber = get('fromNumber');
+ $toNumber = get('toNumber');
+ $premute = get('premute');
- $event->update($userid,$eventid,$eventfeed,$eventtype,$eventvalue,$action,$setfeed,$setemail,$setvalue,$callcurl,$message,$mutetime,$priority);
+ $event->update($userid,$eventid,$eventfeed,$eventtype,$eventvalue,$action,$setfeed,$setemail,$setvalue,$callcurl,$message,$mutetime,$priority,$fromNumber,$toNumber,$premute);
$result = "Event updated";
}
@@ -111,8 +123,12 @@ function event_controller()
$consumersecret = post('consumersecret');
$usertoken = post('usertoken');
$usersecret = post('usersecret');
+
+ //Added XaroRSA
+ $sid = post('sid');
+ $token = post('token');
- $result = $event->set_settings($session['userid'],$prowlkey,$consumerkey,$consumersecret,$usertoken,$usersecret,$smtpserver,$smtpuser,$smtppassword,$smtpport,$nmakey);
+ $result = $event->set_settings($session['userid'],$prowlkey,$consumerkey,$consumersecret,$usertoken,$usersecret,$smtpserver,$smtpuser,$smtppassword,$smtpport,$nmakey, $sid, $token);
}
else if ($session['write'])
diff --git a/event_list.php b/event_list.php
index d40c328..f4a3d1b 100644
--- a/event_list.php
+++ b/event_list.php
@@ -1,5 +1,6 @@
Event
Setup actions to occur when a feed goes above, below or is equal to a specified value, or becomes inactive. Send an email or set another feed to a specified value.
+Add multiple reciepts for email and sms(Twilio) by seperating with ; example: John@hotmail.com;Doe@gmail.com || 2712341234;98769873.
-
-
No event notifications created
-
To add an event based notification:
-
1) Select the feed you wish to be notified about from the drop down menu
-
2) Select whether you want to be notifed if the feed goes above, below or equals the value specified, or is inactive.
-
3) Enter a value
-
+
+
No event notifications created
+
To add an event based notification:
+
1) Select the feed you wish to be notified about from the drop down menu
+
2) Select whether you want to be notifed if the feed goes above, below or equals the value specified, or is inactive.
+
3) Enter a value
+
-
+
if
get_field($item['eventfeed'],'name'); ?>
-
- ";
- if ($item['eventtype']==1) echo "<";
- if ($item['eventtype']==2) echo "==";
- if ($item['eventtype']==3) echo "inactive";
- if ($item['eventtype']==4) echo "updated";
- if ($item['eventtype']==5) echo "inc by";
- if ($item['eventtype']==6) echo "dec by";
- if ($item['eventtype']==7) echo "manual update";
+ ";
+ if ($item['eventtype']==1) echo "<";
+ if ($item['eventtype']==2) echo "==";
+ if ($item['eventtype']==3) echo "inactive";
+ if ($item['eventtype']==4) echo "updated";
+ if ($item['eventtype']==5) echo "inc by";
+ if ($item['eventtype']==6) echo "dec by";
+ if ($item['eventtype']==7) echo "manual update";
?>
+
-
- get_field($item['eventfeed'],'value')>$item['eventvalue'])) $state = true;
- if ($item['eventtype']==1 && ($feed->get_field($item['eventfeed'],'value')<$item['eventvalue'])) $state = true;
- if ($item['eventtype']==2 && ($feed->get_field($item['eventfeed'],'value')==$item['eventvalue'])) $state = true;
- if ($item['eventtype']==3 && (((time()-strtotime($feed->get_field($item['eventfeed'],'time')))/3600)>24)) $state = true;
- // Perhaps a n/a state?
- //if ($item['eventtype']==4) $state = false;
- if ($state == true) echo 'TRUE '; else echo 'FALSE ';
- ?>
-
-
- get_field($item['eventfeed'],'value')>$item['eventvalue'])) $state = true;
+ if ($item['eventtype']==1 && ($feed->get_field($item['eventfeed'],'value')<$item['eventvalue'])) $state = true;
+ if ($item['eventtype']==2 && ($feed->get_field($item['eventfeed'],'value')==$item['eventvalue'])) $state = true;
+ if ($item['eventtype']==3 && (((time()-strtotime($feed->get_field($item['eventfeed'],'time')))/3600)>24)) $state = true;
+ // Perhaps a n/a state?
+ //if ($item['eventtype']==4) $state = false;
+ if ($state == true) echo 'TRUE '; else echo 'FALSE ';
+ ?>
+
+ Last true: ".date("Y-n-j H:i:s", $item['lasttime'])."";
+ if ($item['premute']!=0){
+ echo "First time: ".date("Y-n-j H:i:s", $item['firsttime'])." ";}
+
+ echo "Last true: ".date("Y-n-j H:i:s", $item['lasttime'])." ";
}
- ?>
-
-
-
+ ?>
-
get_field($item['setfeed'],'name'); ?>
@@ -87,8 +89,29 @@
+
+
+
+
+
+
+
+ ";
+ }
+ }
+ else echo $item['toNumber'] ;
+ }
+ ?>
+
+
+ secs
secs
+
Edit
@@ -134,7 +160,8 @@
is
-
+
+
more than
increases by
less than
@@ -151,13 +178,14 @@
:
-
+
send email
set feed
call url
tweet
send prowl
send nma
+ send sms
@@ -181,7 +209,7 @@
priority
-
+
Very Low
Moderate
Normal
@@ -195,23 +223,89 @@
-
- No mute
+
+ Pre-Mute
+
+ No pre-mute
5 secs
15 secs
30 secs
1 min
5 min
10 min
+ 15 min
+ 20 min
+ 25 min
30 min
- 1 hour
- 3 hour
+ 35 min
+ 40 min
+ 45 min
+ 50 min
+ 55 min
+ 1 hour
+ 1 hour 10mins
+ 1 hour 20mins
+ 1 hour 30mins
+ 1 hour 40mins
+ 1 hour 50mins
+ 2 hour
+ 2 hour 10mins
+ 2 hour 20mins
+ 2 hour 30mins
+ 2 hour 40mins
+ 2 hour 50mins
+ 3 hour
+ 4 hour
6 hour
12 hour
24 hour
-
- Add
+
+
+ Post-Mute
+
+ No post-mute
+5 secs
+ 15 secs
+ 30 secs
+ 1 min
+ 5 min
+ 10 min
+ 15 min
+ 20 min
+ 25 min
+ 30 min
+ 35 min
+ 40 min
+ 45 min
+ 50 min
+ 55 min
+ 1 hour
+ 1 hour 10mins
+ 1 hour 20mins
+ 1 hour 30mins
+ 1 hour 40mins
+ 1 hour 50mins
+ 2 hour
+ 2 hour 10mins
+ 2 hour 20mins
+ 2 hour 30mins
+ 2 hour 40mins
+ 2 hour 50mins
+ 3 hour
+ 4 hour
+ 6 hour
+ 12 hour
+ 24 hour
+
+
+
+
+
+
+
+
+ Add
Edit
@@ -273,6 +367,12 @@
$("#action").val($(this).attr("action"));
$("#message").val($(this).attr("message"));
$("#mutetime").val($(this).attr("mutetime"));
+
+ //Added XaroRSA
+ $("#premute").val($(this).attr("premute"));
+ $("#fromNumber").val($(this).attr("fromNumber"));
+ $("#toNumber").val($(this).attr("toNumber"));
+
$("#eventtype").change();
$("#action").change();
return false;
@@ -291,13 +391,13 @@
});
$("#action").change(function() {
- if ($(this).val() == 0) { $("#not-email").show(); $("#not-curl").hide(); $("#not-feed").hide(); $("#not-value").hide(); $("#not-message").show(); $("#not-priority").hide();}
- if ($(this).val() == 1) { $("#not-email").hide(); $("#not-curl").hide(); $("#not-feed").show(); $("#not-value").show(); $("#not-message").hide(); $("#not-priority").hide();}
- if ($(this).val() == 2) { $("#not-email").hide(); $("#not-curl").show(); $("#not-feed").hide(); $("#not-value").hide(); $("#not-message").hide(); $("#not-priority").hide();}
- if ($(this).val() == 3) { $("#not-email").hide(); $("#not-curl").hide(); $("#not-feed").hide(); $("#not-value").hide(); $("#not-message").show(); $("#not-priority").hide();}
- if ($(this).val() == 4) { $("#not-email").hide(); $("#not-curl").hide(); $("#not-feed").hide(); $("#not-value").hide(); $("#not-message").show(); $("#not-priority").show();}
- if ($(this).val() == 5) { $("#not-email").hide(); $("#not-curl").hide(); $("#not-feed").hide(); $("#not-value").hide(); $("#not-message").show(); $("#not-priority").show();}
-
+ if ($(this).val() == 0) { $("#not-email").show(); $("#not-curl").hide(); $("#not-feed").hide(); $("#not-value").hide(); $("#not-message").show(); $("#not-priority").hide();$("#not-twilio").hide();}
+ if ($(this).val() == 1) { $("#not-email").hide(); $("#not-curl").hide(); $("#not-feed").show(); $("#not-value").show(); $("#not-message").hide(); $("#not-priority").hide();$("#not-twilio").hide();}
+ if ($(this).val() == 2) { $("#not-email").hide(); $("#not-curl").show(); $("#not-feed").hide(); $("#not-value").hide(); $("#not-message").hide(); $("#not-priority").hide();$("#not-twilio").hide();}
+ if ($(this).val() == 3) { $("#not-email").hide(); $("#not-curl").hide(); $("#not-feed").hide(); $("#not-value").hide(); $("#not-message").show(); $("#not-priority").hide();$("#not-twilio").hide();}
+ if ($(this).val() == 4) { $("#not-email").hide(); $("#not-curl").hide(); $("#not-feed").hide(); $("#not-value").hide(); $("#not-message").show(); $("#not-priority").show();$("#not-twilio").hide();}
+ if ($(this).val() == 5) { $("#not-email").hide(); $("#not-curl").hide(); $("#not-feed").hide(); $("#not-value").hide(); $("#not-message").show(); $("#not-priority").show();$("#not-twilio").hide();}
+ if ($(this).val() == 6) { $("#not-email").hide(); $("#not-curl").hide(); $("#not-feed").hide(); $("#not-value").hide(); $("#not-message").show(); $("#not-twilio").show(); $("#not-priority").show();$("#not-twilio").show();}
});
jQuery(document).ready(function(){
diff --git a/event_menu.php b/event_menu.php
index 756b4ce..3c18574 100644
--- a/event_menu.php
+++ b/event_menu.php
@@ -1,3 +1,7 @@
"Event", 'path'=>"event" , 'session'=>"write", 'order' => 10 );
+ $domain = "messages";
+ bindtextdomain($domain, "Modules/event/locale");
+ bind_textdomain_codeset($domain, 'UTF-8');
+
+ $menu_left[] = array('name'=> dgettext($domain, "Events"), 'path'=>"event/view" , 'session'=>"write", 'order' => 2 );
\ No newline at end of file
diff --git a/event_model.php b/event_model.php
index e3357b9..929c50d 100644
--- a/event_model.php
+++ b/event_model.php
@@ -29,15 +29,24 @@ public function __construct($mysqli,$redis)
$this->mysqli->query(UPDATE table SET field = '$value' WHERE `field` = '$value');
*/
+
+ //Added XaroRSA
+ public function set_firsttime($userid,$id,$time)
+ {
+ $this->mysqli->query("UPDATE event SET `firsttime` = '$time' WHERE `userid` = '$userid' AND `id` = '$id' ");
+ }
public function set_lasttime($userid,$id,$time)
{
$this->mysqli->query("UPDATE event SET `lasttime` = '$time' WHERE `userid` = '$userid' AND `id` = '$id' ");
}
- public function update($userid,$id,$eventfeed,$eventtype,$eventvalue,$action,$setfeed,$setemail,$setvalue,$callcurl,$message,$mutetime,$priority)
+ public function update($userid,$id,$eventfeed,$eventtype,$eventvalue,$action,$setfeed,$setemail,$setvalue,$callcurl,$message,$mutetime,$priority,$fromNumber,$toNumber,
+ $premute)
{
- $sql = "UPDATE emoncms.event SET eventfeed = $eventfeed, eventtype = $eventtype, eventvalue = $eventvalue, action = $action, setfeed = $setfeed, setemail = '$setemail', setvalue = $setvalue, callcurl = '$callcurl', mutetime = $mutetime, priority = $priority, message = '$message' WHERE `userid` = '$userid' AND `id` = '$id' ";
+ $sql = "UPDATE emoncms.event SET eventfeed = $eventfeed, eventtype = $eventtype, eventvalue = $eventvalue, action = $action, setfeed = $setfeed, setemail = '$setemail',
+ setvalue = $setvalue, callcurl = '$callcurl', mutetime = $mutetime, priority = $priority, message = '$message', fromNumber = '$fromNumber', toNumber = '$toNumber',
+ premute = '$premute' WHERE `userid` = '$userid' AND `id` = '$id' ";
error_log('Mysql Query: ' + $sql);
$result = $this->mysqli->query($sql);
if (!$result){
@@ -45,9 +54,13 @@ public function update($userid,$id,$eventfeed,$eventtype,$eventvalue,$action,$se
}
}
- public function add($userid,$eventfeed,$eventtype,$eventvalue,$action,$setfeed,$setemail,$setvalue,$callcurl,$message,$mutetime,$priority)
+ public function add($userid,$eventfeed,$eventtype,$eventvalue,$action,$setfeed,$setemail,$setvalue,$callcurl,$message,$mutetime,$priority,$fromNumber,$toNumber,$premute)
{
- $sql = "INSERT INTO event (`userid`,`eventfeed`, `eventtype`, `eventvalue`, `action`, `setfeed`, `setemail`, `setvalue`, `lasttime`, `callcurl`, `mutetime`, `priority`, `message`, `disabled`) VALUES ('$userid','$eventfeed','$eventtype','$eventvalue','$action','$setfeed','$setemail','$setvalue','0','$callcurl','$mutetime','$priority','$message','0')";
+ $sql = "INSERT INTO event (`userid`,`eventfeed`, `eventtype`, `eventvalue`, `action`, `setfeed`, `setemail`, `setvalue`, `lasttime`, `callcurl`, `mutetime`, `priority`,
+ `message`, `disabled`, `fromNumber`, `toNumber`, `firsttime`, `premute` , `firstoccurence`)
+ VALUES
+ ('$userid','$eventfeed','$eventtype','$eventvalue','$action','$setfeed','$setemail','$setvalue','0','$callcurl','$mutetime','$priority',
+ '$message','0', '$fromNumber', '$toNumber' , '0' ,'$premute' , '0')";
error_log('Mysql Query: ' + $sql);
$result = $this->mysqli->query($sql);
if (!$result){
@@ -73,7 +86,7 @@ public function eventlist($userid)
// Set all event settings in one save
- public function set_settings($userid,$prowlkey,$consumerkey,$consumersecret,$usertoken,$usersecret,$smtpserver,$smtpuser,$smtppassword,$smtpport,$nmakey)
+ public function set_settings($userid,$prowlkey,$consumerkey,$consumersecret,$usertoken,$usersecret,$smtpserver,$smtpuser,$smtppassword,$smtpport,$nmakey,$sid,$token)
{
$result = $this->mysqli->query("SELECT userid FROM event_settings WHERE `userid` = '$userid'");
$row = $result->fetch_array();
@@ -84,9 +97,10 @@ public function set_settings($userid,$prowlkey,$consumerkey,$consumersecret,$use
}
else
{
- $this->mysqli->query("UPDATE event_settings SET prowlkey = '$prowlkey', consumerkey = '$consumerkey', consumersecret = '$consumersecret', usertoken = '$usertoken', usersecret = '$usersecret', smtpserver = '$smtpserver', smtpuser = '$smtpuser', smtppassword = '$smtppassword', smtpport = '$smtpport', nmakey = '$nmakey' WHERE userid='$userid'");
+ $this->mysqli->query("UPDATE event_settings SET prowlkey = '$prowlkey', consumerkey = '$consumerkey', consumersecret = '$consumersecret', usertoken = '$usertoken', usersecret = '$usersecret', smtpserver = '$smtpserver', smtpuser = '$smtpuser', smtppassword = '$smtppassword', smtpport = '$smtpport', nmakey = '$nmakey', sid = '$sid', token = '$token' WHERE userid='$userid'");
}
}
+
public function set_status($userid, $id, $status)
{
$this->mysqli->query("UPDATE event SET disabled = '$status' WHERE userid='$userid' and id = $id");
@@ -121,6 +135,14 @@ public function get_user_nma($userid) {
$row = $result->fetch_array();
return $row;
}
+
+ //Added XaroRSA
+ public function get_user_twilio($userid) {
+ $result = $this->mysqli->query("SELECT sid, token FROM event_settings WHERE `userid` = '$userid'");
+ $row = $result->fetch_array();
+ return $row;
+ }
+
public function test($userid,$id,$feedid)
{
@@ -135,7 +157,6 @@ public function test($userid,$id,$feedid)
}
-
public function check_feed_event($feedid,$updatetime,$feedtime,$value,$row=NULL,$test=false) {
global $user,$session,$feed;
@@ -149,13 +170,16 @@ public function check_feed_event($feedid,$updatetime,$feedtime,$value,$row=NULL,
}
$result = $this->mysqli->query($sqlFeed);
-
+
// check type
while ($row = $result->fetch_array()) {
if ($row['lasttime']+$row['mutetime'] > time() && !$test) {
+ echo "post-mute active";
+ echo "\ntime remaining:" . (($row['lasttime'] + $row['mutetime'])-time())." ";
continue;
}
+
if ($test){
$sendAlert = 1;
}else{
@@ -241,183 +265,228 @@ public function check_feed_event($feedid,$updatetime,$feedtime,$value,$row=NULL,
}
// event type
- if ($sendAlert == 1) {
- switch($row['action']) {
- case 0:
- // email
- require_once(realpath(dirname(__FILE__)).'/../event/scripts/phpmailer/class.phpmailer.php');
- $smtp = $this->get_settings($userid);
-
- $mail = new PHPMailer();
-
- $mail->IsSMTP(); // telling the class to use SMTP
- $mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
- // 1 = errors and messages
- // 2 = messages only
- $mail->SMTPAuth = true; // enable SMTP authentication
- if ($smtp['smtpport'] == 465)
- {$mail->SMTPSecure = "ssl";} // sets the prefix to the server
-
- $mail->Host = $smtp['smtpserver']; // sets GMAIL as the SMTP server
- $mail->Port = $smtp['smtpport']; // set the SMTP port for the GMAIL server
- $mail->Username = $smtp['smtpuser']; // GMAIL username
- $salt = $user->get_salt($userid);
-
- $mail->Password = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $salt, base64_decode($smtp['smtppassword']), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))); // GMAIL password
-
- $address = $smtp['smtpuser'];
- $mail->SetFrom($address, 'emoncms');
-
- //$mail->AddReplyTo("user2@gmail.com', 'First Last");
-
-
- $mail->Subject = $message;
- //$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
-
- $mail->MsgHTML($message);
-
- $dest = $address;
- if ($row['setemail'] != ''){
- $dest = $row['setemail'];
- }
- // Allows multiple recipients for the event email. Seperate by semi-colon ;
- if (strpos($dest,';') !== false) {
- $addresses = explode(';', $dest);
- foreach ($addresses as &$addressee) {
- $mail->AddAddress($addressee, "emoncms");
- }
- }
- else {
- $mail->AddAddress($dest, "emoncms");
- }
-
-
- //$mail->AddAttachment("images/phpmailer.gif"); // attachment
- //$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
-
- if(!$mail->Send()) {
- echo "Mailer Error: " . $mail->ErrorInfo;
- error_log("Mailer Error: " . $mail->ErrorInfo);
- } else {
- echo "Message sent!";
- error_log("Message sent");
- }
-
- break;
- case 1:
- // set feed
- $setfeed = $row['setfeed'];
- $setvalue = $row['setvalue'];
-
- $this->redis->hMset("feed:lastvalue:$setfeed", array('value' => $setvalue, 'time' => $updatetime));
- // $this->mysqli->query("UPDATE feeds SET value = '$setvalue', time = '$updatetime' WHERE id='$setfeed'");
- break;
- case 2:
- // call url
- $explodedUrl = preg_split('/[?]+/', $row['callcurl'],-1);
- if (count($explodedUrl) > 1){
- $explodedUrl[1] = str_replace(' ', '%20', str_replace('{value}', $value, str_replace('{feed}', $feedData->name, $explodedUrl[1])));
- }
- $ch = curl_init();
- $body = $explodedUrl[0] . '?' . $explodedUrl[1];
- // set URL and other appropriate options
- curl_setopt($ch, CURLOPT_URL, $body);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
- curl_setopt($ch, CURLOPT_TIMEOUT, 1);
-
- // grab URL and pass it to the browser
- if(curl_exec($ch) === false){
- error_log("Curl Error:".curl_error($ch));
- }
- // close cURL resource, and free up system resources
- curl_close($ch);
- error_log("Curl Log:".$body);
-
-
- break;
- case 3:
- // Twitter
- require_once(realpath(dirname(__FILE__)).'/../event/scripts/twitter/twitter-api-php/TwitterAPIExchange.php');
- $twitter = $this->get_user_twitter($userid);
-
- // Twitter disallow duplicate tweets within an unspecified and variable time per account
- // so add the feed time to make each tweet unique.
- $message = $message.' at '.date("H:i:s", $feedtime);;
-
- // Set the OAauth values
- $settings = array(
- 'oauth_access_token' => $twitter['usertoken'],
- 'oauth_access_token_secret' => $twitter['usersecret'],
- 'consumer_key' => $twitter['consumerkey'],
- 'consumer_secret' => $twitter['consumersecret']
- );
-
- // Make the API call
- $url = 'https://api.twitter.com/1.1/statuses/update.json';
- $requestMethod = 'POST';
- $postfields = array(
- 'status' => $message );
- $tweet = new TwitterAPIExchange($settings);
- echo $tweet->buildOauth($url, $requestMethod)
- ->setPostfields($postfields)
- ->performRequest();
- break;
- case 4:
- // Prowl
- require_once realpath(dirname(__FILE__)).'/scripts/prowlphp/ProwlConnector.class.php';
- require_once realpath(dirname(__FILE__)).'/scripts/prowlphp/ProwlMessage.class.php';
- require_once realpath(dirname(__FILE__)).'/scripts/prowlphp/ProwlResponse.class.php';
- $prowl = $this->get_user_prowl($userid);
-
- $oProwl = new ProwlConnector();
- $oMsg = new ProwlMessage();
-
- $oProwl->setIsPostRequest(true);
- $oMsg->setPriority($row['priority']);
-
- $oMsg->addApiKey($prowl['prowlkey']);
-
- $oMsg->setEvent($message);
-
-
- // These are optional:
- $message = 'event at '.date("Y-m-d H:i:s",time());
- $oMsg->setDescription($message);
- $oMsg->setApplication('emoncms');
-
- $oResponse = $oProwl->push($oMsg);
-
- if ($oResponse->isError()) {
- error_log("Prowl error:".$oResponse->getErrorAsString());
- }
-
- break;
- case 5:
- // NMA
- require_once realpath(dirname(__FILE__)).'/scripts/nma/nmaApi.class.php';
-
- $nmakey = $this->get_user_nma($userid);
-
- $nma = new nmaApi(array('apikey' => $nmakey['nmakey']));
-
- $priority = $row['priority'];
-
- if($nma->verify()){
- $nma->notify('EmonCMS '.$message, 'EmonCMS', $message, $priority);
- }
-
-
- break;
- }
- // update the lasttime called
- if(!$test){
- $this->mysqli->query("UPDATE event SET lasttime = '".time()."' WHERE id='".$row['id']."'");
- }
-
-
- }
- }
- }
+ if ($sendAlert == 1) {
+
+ if ($row['firstoccurence'] == 0 && !$test && $row['premute']!=0){
+ //save time to firstTime, set firstoccurance to 1
+ $this->mysqli->query("UPDATE event SET firsttime = '".time()."' WHERE id='".$row['id']."'");
+ $this->mysqli->query("UPDATE event SET firstoccurence = '1' WHERE id='".$row['id']."'");
+ echo "First occurrence set = 1\nFirst Time updated\n";
+ }
+ else if ($row['firstoccurence'] == 1 && $row['firsttime'] + $row['premute'] < Time() || $test || $row['premute']==0){
+
+ echo "Sending notifications!\n";
+
+ switch($row['action']) {
+ case 0:
+ // email
+ require_once(realpath(dirname(__FILE__)).'/../event/scripts/phpmailer/class.phpmailer.php');
+ $smtp = $this->get_settings($userid);
+
+ $mail = new PHPMailer();
+
+ $mail->IsSMTP(); // telling the class to use SMTP
+ $mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
+ // 1 = errors and messages
+ // 2 = messages only
+ $mail->SMTPAuth = true; // enable SMTP authentication
+ if ($smtp['smtpport'] == 465)
+ {$mail->SMTPSecure = "ssl";} // sets the prefix to the server
+
+ $mail->Host = $smtp['smtpserver']; // sets GMAIL as the SMTP server
+ $mail->Port = $smtp['smtpport']; // set the SMTP port for the GMAIL server
+ $mail->Username = $smtp['smtpuser']; // GMAIL username
+ $salt = $user->get_salt($userid);
+
+ $mail->Password = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $salt, base64_decode($smtp['smtppassword']), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))); // GMAIL password
+
+ $address = $smtp['smtpuser'];
+ $mail->SetFrom($address, 'emoncms');
+
+ //$mail->AddReplyTo("user2@gmail.com', 'First Last");
+
+
+ $mail->Subject = $message;
+ //$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
+
+ $mail->MsgHTML($message);
+
+ $dest = $address;
+ if ($row['setemail'] != ''){
+ $dest = $row['setemail'];
+ }
+ // Allows multiple recipients for the event email. Seperate by semi-colon ;
+ if (strpos($dest,';') !== false) {
+ $addresses = explode(';', $dest);
+ foreach ($addresses as &$addressee) {
+ $mail->AddAddress($addressee, "emoncms");
+ }
+ }
+ else {
+ $mail->AddAddress($dest, "emoncms");
+ }
+
+
+ //$mail->AddAttachment("images/phpmailer.gif"); // attachment
+ //$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
+
+ if(!$mail->Send()) {
+ echo "Mailer Error: " . $mail->ErrorInfo;
+ error_log("Mailer Error: " . $mail->ErrorInfo);
+ } else {
+ echo "Message sent!";
+ error_log("Message sent");
+ }
+
+ break;
+ case 1:
+ // set feed
+ $setfeed = $row['setfeed'];
+ $setvalue = $row['setvalue'];
+
+ $this->redis->hMset("feed:lastvalue:$setfeed", array('value' => $setvalue, 'time' => $updatetime));
+ // $this->mysqli->query("UPDATE feeds SET value = '$setvalue', time = '$updatetime' WHERE id='$setfeed'");
+ break;
+ case 2:
+ // call url
+ $explodedUrl = preg_split('/[?]+/', $row['callcurl'],-1);
+ if (count($explodedUrl) > 1){
+ $explodedUrl[1] = str_replace(' ', '%20', str_replace('{value}', $value, str_replace('{feed}', $feedData->name, $explodedUrl[1])));
+ }
+ $ch = curl_init();
+ $body = $explodedUrl[0] . '?' . $explodedUrl[1];
+ // set URL and other appropriate options
+ curl_setopt($ch, CURLOPT_URL, $body);
+ curl_setopt($ch, CURLOPT_HEADER, 0);
+ curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
+ curl_setopt($ch, CURLOPT_TIMEOUT, 1);
+
+ // grab URL and pass it to the browser
+ if(curl_exec($ch) === false){
+ error_log("Curl Error:".curl_error($ch));
+ }
+ // close cURL resource, and free up system resources
+ curl_close($ch);
+ error_log("Curl Log:".$body);
+
+
+ break;
+ case 3:
+ // Twitter
+ require_once(realpath(dirname(__FILE__)).'/../event/scripts/twitter/twitter-api-php/TwitterAPIExchange.php');
+ $twitter = $this->get_user_twitter($userid);
+
+ // Twitter disallow duplicate tweets within an unspecified and variable time per account
+ // so add the feed time to make each tweet unique.
+ $message = $message.' at '.date("H:i:s", $feedtime);;
+
+ // Set the OAauth values
+ $settings = array(
+ 'oauth_access_token' => $twitter['usertoken'],
+ 'oauth_access_token_secret' => $twitter['usersecret'],
+ 'consumer_key' => $twitter['consumerkey'],
+ 'consumer_secret' => $twitter['consumersecret']
+ );
+
+ // Make the API call
+ $url = 'https://api.twitter.com/1.1/statuses/update.json';
+ $requestMethod = 'POST';
+ $postfields = array(
+ 'status' => $message );
+ $tweet = new TwitterAPIExchange($settings);
+ echo $tweet->buildOauth($url, $requestMethod)
+ ->setPostfields($postfields)
+ ->performRequest();
+ break;
+ case 4:
+ // Prowl
+ require_once realpath(dirname(__FILE__)).'/scripts/prowlphp/ProwlConnector.class.php';
+ require_once realpath(dirname(__FILE__)).'/scripts/prowlphp/ProwlMessage.class.php';
+ require_once realpath(dirname(__FILE__)).'/scripts/prowlphp/ProwlResponse.class.php';
+ $prowl = $this->get_user_prowl($userid);
+
+ $oProwl = new ProwlConnector();
+ $oMsg = new ProwlMessage();
+
+ $oProwl->setIsPostRequest(true);
+ $oMsg->setPriority($row['priority']);
+
+ $oMsg->addApiKey($prowl['prowlkey']);
+
+ $oMsg->setEvent($message);
+
+
+ // These are optional:
+ $message = 'event at '.date("Y-m-d H:i:s",time());
+ $oMsg->setDescription($message);
+ $oMsg->setApplication('emoncms');
+
+ $oResponse = $oProwl->push($oMsg);
+
+ if ($oResponse->isError()) {
+ error_log("Prowl error:".$oResponse->getErrorAsString());
+ }
+
+ break;
+ case 5:
+ // NMA
+ require_once realpath(dirname(__FILE__)).'/scripts/nma/nmaApi.class.php';
+
+ $nmakey = $this->get_user_nma($userid);
+
+ $nma = new nmaApi(array('apikey' => $nmakey['nmakey']));
+
+ $priority = $row['priority'];
+
+ if($nma->verify()){
+ $nma->notify('EmonCMS '.$message, 'EmonCMS', $message, $priority);
+ }
+
+ break;
+
+ case 6:
+ // Twilio
+ require_once realpath(dirname(__FILE__)).'/scripts/twilio/Services/Twilio.php';
+
+ $twiliokeys = $this->get_user_twilio($userid);
+
+ $sid = $twiliokeys['sid']; // Your Account SID from www.twilio.com/user/account
+ $token = $twiliokeys['token']; // Your Auth Token from www.twilio.com/user/account
+ $From = $row['fromNumber']; // From a valid Twilio number
+ $To = $row['toNumber']; // Text this number
+
+ $client = new Services_Twilio($sid, $token);
+
+ // Allows multiple recipients for the Twilio Sms. Seperate by semi-colon ;
+ if (strpos($To,';') !== false) {
+ $ToNumberArray = explode(';', $To);
+ foreach ($ToNumberArray as &$singleToNumbers) {
+ $messageTwilio = $client->account->messages->sendMessage($From,$singleToNumbers,$message);
+ }
+ }
+ else {
+ $messageTwilio = $client->account->messages->sendMessage($From,$To,$message);
+ }
+
+
+ break;
+ }
+ // update the lasttime called
+ if(!$test){
+ $this->mysqli->query("UPDATE event SET lasttime = '".time()."' WHERE id='".$row['id']."'");
+ echo "\nUpdating Last Time value\n";
+ }
+
+ }else
+ {
+ echo "Active Alarm\nPre-mute time active\nFirst time=" . date("Y-m-d h:i:sa",$row['firsttime']) . "\npremute = ". $row['premute'];
+ echo "\ntime remaining:" . (($row['firsttime'] + $row['premute'])-time())." ";
+ }
+ }else {
+ $this->mysqli->query("UPDATE event SET firstoccurence = '0' WHERE id='".$row['id']."'");
+ $this->mysqli->query("UPDATE event SET firsttime = '0' WHERE id='".$row['id']."'");
+ echo "Set Alarm Status = 0\nSet First Occurrence = 0\nSet First Time value =0\nNo active triggers\n";
+ }
+ }
+ }
}
-
diff --git a/event_schema.php b/event_schema.php
index 3fd1ab2..704d4c4 100644
--- a/event_schema.php
+++ b/event_schema.php
@@ -11,7 +11,10 @@
'usertoken' => array('type' => 'varchar(60)'),
'usersecret' => array('type' => 'varchar(60)'),
'prowlkey' => array('type' => 'varchar(60)'),
- 'nmakey' => array('type' => 'varchar(60)')
+ 'nmakey' => array('type' => 'varchar(60)'),
+ 'sid' => array('type' => 'varchar(60)'),
+ 'token' => array('type' => 'varchar(60)'),
+
);
$schema['event'] = array(
@@ -29,6 +32,11 @@
'mutetime' => array('type' => 'int(11)'),
'priority' => array('type' => 'int(3)', 'default'=>0),
'message' => array('type' => 'text'),
- 'disabled' => array('type' => 'int(1)')
+ 'disabled' => array('type' => 'int(1)'),
+ 'fromNumber' => array('type' => 'text'),
+ 'toNumber' => array('type' => 'text'),
+ 'firsttime' => array('type' => 'int(11)'),
+ 'premute' => array('type' => 'int(11)'),
+ 'firstoccurence' => array('type' => 'int(11)'),
);
diff --git a/event_settings_view.php b/event_settings_view.php
index 04e1fe1..7f2d1fb 100644
--- a/event_settings_view.php
+++ b/event_settings_view.php
@@ -6,7 +6,7 @@