Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- error_reporting(E_ALL);
- ini_set('display_errors', 1);
- // Usage: emailhandler.php?from=<sender>&body=<body>[&to=<device>]
- // CREATE TABLE inbox(sender VARCHAR(255), rcpt VARCHAR(255), body VARCHAR(255));
- require_once '../incl/Config.php';
- require_once '../incl/DBi.php';
- require_once '../incl/User.php';
- require_once '../incl/Utils.php';
- $db = DB::getInstance();
- date_default_timezone_set('Europe/Minsk');
- $dateiname = "log.txt";
- $handler = fOpen($dateiname , "w+");
- fWrite($handler , json_encode($_REQUEST)." ".date('d.m.y h:i:s'));
- fClose($handler);
- // ?from=79219353989&body=lala
- if(isSet($_GET['from']) && isSet($_GET['body']) && !empty($_GET['from']) && !empty($_GET['body'])){
- $from = $db->real_escape_string($_GET['from']);
- $body = trim($db->real_escape_string($_GET['body']));
- $body = str_replace(array('%0a','%0d'),'',$body);
- $sql= "SELECT `email`.id as id, `email`.gateway_id, `email`.user_id as user_id, `email`.ctext as ctext, `email_response`.type as response_type, `email`.order_nr, `email`.employee_nr FROM `email` LEFT JOIN `email_response` ON `email`.id = `email_response`.email_id WHERE `email`.`fon_nr` = '$from' AND `email`.state = 'Отправлено' AND `email`.stype='основное' ORDER BY id desc LIMIT 1";
- $email = $db->fetchOne($sql);
- if($email) {
- if($email['response_type'] == null) {
- $result = intVal($body);
- if($result > 0 && $result < 6) {
- $type = 'Correct answer';
- $pEmailCount = 1;
- if($email['ctext'] != '') {
- $prices = Utils::getPrices();
- $pEmailCount = Utils::countEmail($email['ctext']);
- $toPay = $pEmailCount * floatVal($prices['email_price']);
- if(User::changeBalance(-$toPay,$email['user_id'])) {
- $sql = "INSERT INTO `email`
- (`gateway_id`, `user_id`,`stype`,`email_amount`,`text`,`fon_nr`,`state`,`source`,`order_nr`,`employee_nr`,`sent`) VALUES
- ($email[gateway_id], '$email[user_id]','finished','$pEmailCount','$email[ctext]','$from','process','site','$email[order_nr]','$email[employee_nr]',NOW()+1)";
- if(!$db->query($sql)) {
- $dateiname = "log2.txt";
- $handler = fOpen($dateiname , "w+");
- fWrite($handler , $sql);
- fClose($handler);
- } else {
- $sql = "UPDATE `email` SET `sent_last` = NOW()+1 WHERE `gateway_id` = '$email[id]'";
- $db->query($sql);
- $db->query("UPDATE `gateways` SET `email_per_day_actual` = `email_per_day_actual`+1 WHERE `gateway_id` = $email[gateway_id]");
- }
- }
- echo 'ok';
- }
- } else {
- $type = 'Incorrect naswer';
- $result = 0;
- }
- $sql = "INSERT INTO `email_response` (`email_id`,`text`,`fon_nr`,`for_user`,`type`,`result`)
- VALUES ('$email[id]','$body','$from','$email[user_id]','$type','$result')";
- $db->query($sql);
- echo 'Saved in email_response: '.$type;
- } else {
- $sql = "INSERT INTO `email_response` (`text`,`fon_nr`,`for_user`,`type`)
- VALUES ('$body','$from','$email[user_id]','Duplicate')";
- $db->query($sql);
- echo 'Duplicate';
- }
- } else {
- $sql = "INSERT INTO `inbox` VALUES('$from', '$from', '$body')";
- $db->query($sql);
- }
- } else {
- echo 'parameters missing!';
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement