Last active
August 29, 2015 14:24
-
-
Save davidegironi/c21493554d28395e5482 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Copyright (c) 2015 Davide Gironi | |
// | |
// Please refer to LICENSE file for licensing information. | |
// Xively trigger service for sending alert mail | |
// | |
// Installation and usage: | |
// Upload this script to your website | |
// es: example.com/xivelytrig/xivelytrig.php | |
// Set the configuration parameters above, according to your needs. | |
// Go to your xively feed, create a trigger for a channel. | |
// Point the trigger post URL to this script. | |
// es: (send both mail and sms) | |
// example.com/xivelytrig/xivelytrig.php?sign=123456&feed=your_feed_name&channel=your_channel_name&alarm=your_alarm_text&[email protected]&mobile=000000000000 | |
// es: (send mail) | |
// example.com/xivelytrig/xivelytrig.php?sign=123456&feed=your_feed_name&channel=your_channel_name&alarm=youralarm_text&[email protected] | |
header('Content-Type: text/plain; charset=utf-8'); | |
// Configuration | |
// -------------------------------------------- | |
// timezone configuration | |
date_default_timezone_set('Europe/Rome'); | |
// validation token | |
$sign = "123456"; | |
// mail config | |
$mail_from = "[email protected]"; | |
// betamax config | |
$betamax_url = "www.yourbetamaxprovider.com"; | |
$betamax_username = "______"; | |
$betamax_password = "______"; | |
$betamax_from = "______"; | |
// sms max message enabled | |
$sms_maxenabler = true; | |
// sms max temp folder | |
$sms_maxfolder = "./temp"; | |
// set the sms time to purge sms temporary folder and enable the send of a message | |
$sms_maxtime = 43200; // means max 1 message per feed/channel/number every 12 hours (43200 seconds) | |
// -------------------------------------------- | |
try { | |
//validate input | |
if($sign != $_GET['sign']) { | |
throw new RuntimeException('Error: Invalid signature.'); | |
} | |
if(empty($_GET['feed'])) { | |
throw new RuntimeException('Error: Invalid feed.'); | |
} | |
if(empty($_GET['channel'])) { | |
throw new RuntimeException('Error: Invalid channel.'); | |
} | |
if(empty($_GET['address'])) { | |
throw new RuntimeException('Error: Invalid address.'); | |
} | |
if(empty($_GET['alarm'])) { | |
throw new RuntimeException('Error: Invalid alarm.'); | |
} | |
//decode message from xively | |
//$data = json_decode(substr(urldecode(file_get_contents('php://input')), 5), true); | |
//$json_string = json_encode($data); | |
$smssent = false; | |
$smssentable = false; | |
$mailsent = false; | |
//try to send email | |
$to = $_GET['address']; | |
$subject = $_GET['feed'] . '/' . $_GET['channel'] . ' xively alert on date ' . date('Y-m-d H:i:s'); | |
$message = "Alert!\r\n\r\nAlarm \"" . $_GET['alarm']. "\" trigger is fired on feed \"". $_GET['feed'] . "\" for channel \"" . $_GET['channel'] . "\"."; | |
$headers = 'From: ' . $mail_from . "\r\n" . 'X-Mailer: PHP/' . phpversion(); | |
if(mail($to, $subject, $message, $headers)) | |
$mailsent = true; | |
//delete temporary sms limiter files | |
if($sms_maxenabler) { | |
foreach (glob($sms_maxfolder."/*") as $file) { | |
if (filemtime($file) < time() - $sms_maxtime) { | |
unlink($file); | |
} | |
} | |
} | |
//try to send sms | |
if(!empty($_GET['mobile'])) { | |
//check for max sms send limits | |
$trysmssend = true; | |
if($sms_maxenabler) { | |
$trysmssend = false; | |
$sms_file = $sms_maxfolder . '/' . $_GET['feed'] . '-' . $_GET['channel'] . '-' . $_GET['mobile'] . '.txt'; | |
if(!file_exists($sms_file)) { | |
$smssentable = true; | |
$fp = fopen($sms_file,"wb"); | |
fwrite($fp, ""); | |
fclose($fp); | |
$trysmssend = true; | |
} | |
} | |
if($trysmssend) { | |
//send sms by betamax service | |
$curl = curl_init(); | |
curl_setopt_array($curl, array( | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_URL => 'https://' . $betamax_url . '/myaccount/sendsms.php?username=' . $betamax_username . '&password=' . $betamax_password . '&from=' . $betamax_from . '&to=' . $_GET['mobile'] . '&text=' . urlencode('alarm ' . $_GET['alarm'] . ' on ' . $_GET['feed'] . '/' . $_GET['channel']), | |
CURLOPT_CONNECTTIMEOUT => 0, | |
CURLOPT_TIMEOUT => 30, | |
CURLOPT_SSL_VERIFYPEER => false | |
)); | |
$resp = curl_exec($curl); | |
curl_close($curl); | |
$respxml = simplexml_load_string(htmlspecialchars_decode($resp)); | |
if($respxml->result == "1") | |
$smssent = true; | |
} | |
} | |
//output results | |
if(!$mailsent) | |
echo "Error sending mail. "; | |
else | |
echo "Mail sent successfully. "; | |
if(!empty($_GET['mobile'])) { | |
if(!$smssent) | |
{ | |
if($smssentable) | |
echo "Error sending sms. "; | |
else | |
echo "SMS does not need to be sent. "; | |
} | |
else | |
echo "SMS sent successfully. "; | |
} | |
} catch (RuntimeException $e) { | |
echo $e->getMessage(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment