public function check_feed_event($feedid, $updatetime, $feedtime, $value, $row = NULL, $test = false) { global $user, $session, $feed; $userid = $session['userid']; $sqlFeed = "SELECT * FROM event WHERE `userid` = '{$userid}'"; if ($test) { $sqlFeed = $sqlFeed . " and id = {$feedid}"; } else { $sqlFeed = $sqlFeed . " and (`disabled` <> 1 or `disabled` IS NULL) and (eventfeed = {$feedid} or eventtype=3)"; } $result = $this->mysqli->query($sqlFeed); // check type while ($row = $result->fetch_array()) { if ($row['lasttime'] + $row['mutetime'] > time() && !$test) { continue; } if ($test) { $sendAlert = 1; } else { $sendAlert = 0; switch ($row['eventtype']) { case 0: // more than if ($value > $row['eventvalue']) { $sendAlert = 1; } break; case 1: // less than if ($value < $row['eventvalue']) { $sendAlert = 1; } break; case 2: // equal to if ($value == $row['eventvalue']) { $sendAlert = 1; } break; case 3: // inactive // not sure this can be called as no feed updated //if (((time()-$row['lasttime'])/3600)>24) {} $feedData = $feed->get($row['eventfeed']); //error_log("Feeddata: " .$feedData->time); $t = time() - strtotime($feedData['time']); //error_log("t: " .$t); if ($t > $row['eventvalue']) { $sendAlert = 1; } break; case 4: // updated $sendAlert = 1; break; case 5: // increased by $feedname = 'feed_' . $feedid; $resultprev = $this->mysqli->query("SELECT * FROM {$feedname} ORDER BY `time` DESC LIMIT 1,1"); $rowprev = $resultprev->fetch_array(); //echo "INC == ".$value." > ".$rowprev['data']."+".$row['eventvalue']; if ($value > $rowprev['data'] + $row['eventvalue']) { $sendAlert = 1; } break; case 6: // decreased by $feedname = 'feed_' . $feedid; $resultprev = $this->mysqli->query("SELECT * FROM {$feedname} ORDER BY `time` DESC LIMIT 1,1"); $rowprev = $resultprev->fetch_array(); //echo "DEC == ".$value."<". $rowprev['data']."-".$row['eventvalue']; if ($value < $rowprev['data'] - $row['eventvalue']) { $sendAlert = 1; } break; case 7: // manual update // Check if event.lasttime is less than feed.time $feedData = $feed->get($feedid); if ($feedData['time'] > $row['lasttime']) { $sendAlert = 1; } } } $feedData = $feed->get($row['eventfeed']); $message = $row['message']; $message = str_replace('{feed}', $feedData['name'], $message); $message = str_replace('{value}', $value, $message); $message = htmlspecialchars($message); if (empty($message)) { $message = "No message body"; } if ($test) { $message = 'TEST - ' . $message; } // event type if ($sendAlert == 1) { switch ($row['action']) { case 0: // email require_once realpath(dirname(__FILE__)) . '/../event/scripts/phpmailer/class.phpmailer.php'; require_once realpath(dirname(__FILE__)) . '/../event/scripts/phpmailer/class.smtp.php'; $smtp = $this->get_settings($userid); $mail = new PHPMailer(); $mail->IsSMTP(); // telling the class to use SMTP $mail->SMTPDebug = 0; // SMTP debug information (for testing) // 0 No output // 1 Commands // 2 Data and commands // 3 As 2 plus connection status // 4 Low-level data output $mail->SMTPAuth = true; // enable SMTP authentication if ($smtp['smtpport'] == 587) { $mail->SMTPSecure = "tls"; } else { 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("*****@*****.**', '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: // MQTT require_once realpath(dirname(__FILE__)) . '/scripts/mqtt/phpMQTT.php'; $mqttSettings = $this->get_user_mqtt($userid); $salt = $user->get_salt($userid); $mqtttopic = $row['mqtttopic']; $mqtttopic = str_replace('{feed}', $feedData['name'], $mqtttopic); $mqtttopic = str_replace('{value}', $value, $mqtttopic); $mqtttopic = htmlspecialchars($mqtttopic); $mqttqos = $row['mqttqos']; if (empty($mqttqos)) { $mqttqos = 0; } // setup connection $mqtt = new phpMQTT($mqttSettings['mqttbrokerip'], $mqttSettings['mqttbrokerport'], "emoncms"); if (empty($mqttSettings['mqttusername'])) { $mqttConnected = $mqtt->connect(); } else { $mqttusername = $mqttSettings['mqttusername']; $mqttpassword = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $salt, base64_decode($mqttSettings['mqttpassword']), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))); $mqttConnected = $mqtt->connect(false, NULL, $mqttusername, $mqttpassword); } if ($mqttConnected) { $mqtt->publish($mqtttopic, $message, $mqttqos); } else { error_log("MQTT connection failed"); } $mqtt->close(); break; } // update the lasttime called if (!$test) { $this->mysqli->query("UPDATE event SET lasttime = '" . time() . "' WHERE id='" . $row['id'] . "'"); } } } }
<?php /** * Example File * @author Mario Mueller <*****@*****.**> * @version 0.3.2 */ require_once 'ProwlConnector.class.php'; require_once 'ProwlMessage.class.php'; require_once 'ProwlResponse.class.php'; $oProwl = new ProwlConnector(); $oMsg = new ProwlMessage(); // If you have one: // $oProwl->setProviderKey('MY_PROVIDER_KEY'); try { $oProwl->setIsPostRequest(true); $oMsg->setPriority(0); // You can ADD up to 5 api keys // This is a Test Key, please use your own. $oMsg->addApiKey('e0bf09a4cc20ae0bcd63b30b19031ef59a458634'); $oMsg->setEvent('My Event!'); // These are optional: $oMsg->setDescription('My Event description.'); $oMsg->setApplication('My Custom App Name.'); $oResponse = $oProwl->push($oMsg); if ($oResponse->isError()) { print $oResponse->getErrorAsString(); } else { print "Message sent." . PHP_EOL; print "You have " . $oResponse->getRemaining() . " Messages left." . PHP_EOL; print "Your counter will be resetted on " . date('Y-m-d H:i:s', $oResponse->getResetDate());