Exemplo n.º 1
0
 /**
  * Retrieves Clickatell Balance using Clickatell Library
  */
 function smsbalance()
 {
     $this->template = "";
     $this->auto_render = FALSE;
     $settings = new Settings_Model(1);
     if ($settings->loaded == true) {
         $clickatell_api = $settings->clickatell_api;
         $clickatell_username = $settings->clickatell_username;
         $clickatell_password = $settings->clickatell_password;
         $mysms = new Clickatell();
         $mysms->api_id = $clickatell_api;
         $mysms->user = $clickatell_username;
         $mysms->password = $clickatell_password;
         $mysms->use_ssl = false;
         $mysms->sms();
         // echo $mysms->session;
         echo $mysms->getbalance();
     }
 }
Exemplo n.º 2
0
 private function _send_mobile_alert($alert_mobile, $alert_lon, $alert_lat, $alert_radius)
 {
     // For Mobile Alerts, Confirmation Code
     // Should be 6 distinct characters
     $alert_code = text::random('distinct', 8);
     $settings = ORM::factory('settings', 1);
     if (!$settings->loaded) {
         return FALSE;
     }
     // Get SMS Numbers
     if (!empty($settings->sms_no3)) {
         $sms_from = $settings->sms_no3;
     } elseif (!empty($settings->sms_no2)) {
         $sms_from = $settings->sms_no2;
     } elseif (!empty($settings->sms_no1)) {
         $sms_from = $settings->sms_no1;
     } else {
         $sms_from = "000";
         // User needs to set up an SMS number
     }
     $sms = new Clickatell();
     $sms->api_id = $settings->clickatell_api;
     $sms->user = $settings->clickatell_username;
     $sms->password = $settings->clickatell_password;
     $sms->use_ssl = false;
     $sms->sms();
     $message = "Your alerts confirmation code\n\t\t\t\tis: " . $alert_code . " This code is NOT case sensitive";
     if ($sms->send($alert_mobile, $sms_from, $message) == "OK") {
         $alert = ORM::factory('alert');
         $alert->alert_type = self::MOBILE_ALERT;
         $alert->alert_recipient = $alert_mobile;
         $alert->alert_code = $alert_code;
         $alert->alert_lon = $alert_lon;
         $alert->alert_lat = $alert_lat;
         $alert->alert_radius = $alert_radius;
         $alert->save();
         return TRUE;
     }
     return FALSE;
 }
Exemplo n.º 3
0
 public function index()
 {
     $config = kohana::config('alerts');
     $settings = kohana::config('settings');
     $site_name = $settings['site_name'];
     $unsubscribe_message = Kohana::lang('alerts.unsubscribe') . url::site() . 'alerts/unsubscribe/';
     $settings = NULL;
     $sms_from = NULL;
     $clickatell = NULL;
     $db = new Database();
     $incidents = $db->query("SELECT incident.id, incident_title, \n\t\t\t\t\t\t\t\t incident_description, incident_verified, \n\t\t\t\t\t\t\t\t location.latitude, location.longitude, alert_sent.incident_id\n\t\t\t\t\t\t\t\t FROM incident INNER JOIN location ON incident.location_id = location.id\n\t\t\t\t\t\t\t\t LEFT OUTER JOIN alert_sent ON incident.id = alert_sent.incident_id");
     foreach ($incidents as $incident) {
         if ($incident->incident_id != NULL) {
             continue;
         }
         $verified = (int) $incident->incident_verified;
         if ($verified) {
             $latitude = (double) $incident->latitude;
             $longitude = (double) $incident->longitude;
             $proximity = new Proximity($latitude, $longitude);
             $alertees = $this->_get_alertees($proximity);
             foreach ($alertees as $alertee) {
                 $alert_type = (int) $alertee->alert_type;
                 if ($alert_type == 1) {
                     if ($settings == null) {
                         $settings = ORM::factory('settings', 1);
                         if ($settings->loaded == true) {
                             // Get SMS Numbers
                             if (!empty($settings->sms_no3)) {
                                 $sms_from = $settings->sms_no3;
                             } elseif (!empty($settings->sms_no2)) {
                                 $sms_from = $settings->sms_no2;
                             } elseif (!empty($settings->sms_no1)) {
                                 $sms_from = $settings->sms_no1;
                             } else {
                                 $sms_from = "000";
                             }
                             // User needs to set up an SMS number
                         }
                         $clickatell = new Clickatell();
                         $clickatell->api_id = $settings->clickatell_api;
                         $clickatell->user = $settings->clickatell_username;
                         $clickatell->password = $settings->clickatell_password;
                         $clickatell->use_ssl = false;
                         $clickatell->sms();
                     }
                     $message = $incident->incident_description;
                     if ($clickatell->send($alertee->alert_recipient, $sms_from, $message) == "OK") {
                         $alert = ORM::factory('alert_sent');
                         $alert->alert_id = $alertee->id;
                         $alert->incident_id = $incident->id;
                         $alert->alert_date = date("Y-m-d H:i:s");
                         $alert->save();
                     }
                 } elseif ($alert_type == 2) {
                     $to = $alertee->alert_recipient;
                     $from = $config['alerts_email'];
                     $subject = "[{$site_name}] " . $incident->incident_title;
                     $message = $incident->incident_description . "<p>" . $unsubscribe_message . $alertee->alert_code . "</p>";
                     if (email::send($to, $from, $subject, $message, TRUE) == 1) {
                         $alert = ORM::factory('alert_sent');
                         $alert->alert_id = $alertee->id;
                         $alert->incident_id = $incident->id;
                         $alert->alert_date = date("Y-m-d H:i:s");
                         $alert->save();
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 4
0
 /**
  * Send A New Message Using Clickatell Library
  */
 function send()
 {
     $this->template = "";
     $this->auto_render = FALSE;
     // setup and initialize form field names
     $form = array('to_id' => '', 'message' => '');
     //  Copy the form as errors, so the errors will be stored with keys
     //  corresponding to the form field names
     $errors = $form;
     $form_error = FALSE;
     // check, has the form been submitted, if so, setup validation
     if ($_POST) {
         // Instantiate Validation, use $post, so we don't overwrite $_POST
         // fields with our own things
         $post = new Validation($_POST);
         // Add some filters
         $post->pre_filter('trim', TRUE);
         // Add some rules, the input field, followed by a list of checks, carried out in order
         $post->add_rules('to_id', 'required', 'numeric');
         $post->add_rules('message', 'required', 'length[1,160]');
         // Test to see if things passed the rule checks
         if ($post->validate()) {
             // Yes! everything is valid
             $reply_to = ORM::factory('message', $post->to_id);
             if ($reply_to->loaded == true) {
                 // Yes! Replyto Exists
                 // This is the message we're replying to
                 $sms_to = intval($reply_to->message_from);
                 // Load Users Settings
                 $settings = new Settings_Model(1);
                 if ($settings->loaded == true) {
                     // Get SMS Numbers
                     if (!empty($settings->sms_no3)) {
                         $sms_from = $settings->sms_no3;
                     } elseif (!empty($settings->sms_no2)) {
                         $sms_from = $settings->sms_no2;
                     } elseif (!empty($settings->sms_no1)) {
                         $sms_from = $settings->sms_no1;
                     } else {
                         $sms_from = "000";
                         // User needs to set up an SMS number
                     }
                     // Create Clickatell Object
                     $mysms = new Clickatell();
                     $mysms->api_id = $settings->clickatell_api;
                     $mysms->user = $settings->clickatell_username;
                     $mysms->password = $settings->clickatell_password;
                     $mysms->use_ssl = false;
                     $mysms->sms();
                     $send_me = $mysms->send($sms_to, $sms_from, $post->message);
                     // Message Went Through??
                     if ($send_me == "OK") {
                         $newmessage = ORM::factory('message');
                         $newmessage->parent_id = $post->to_id;
                         // The parent message
                         $newmessage->message_from = $sms_from;
                         $newmessage->message_to = $sms_to;
                         $newmessage->message = $post->message;
                         $newmessage->message_type = 2;
                         // This is an outgoing message
                         $newmessage->reporter_id = $reply_to->reporter_id;
                         $newmessage->message_date = date("Y-m-d H:i:s", time());
                         $newmessage->save();
                         echo json_encode(array("status" => "sent", "message" => Kohana::lang('ui_admin.message_sent')));
                     } else {
                         echo json_encode(array("status" => "error", "message" => Kohana::lang('ui_admin.error') . " - " . $send_me));
                     }
                 } else {
                     echo json_encode(array("status" => "error", "message" => Kohana::lang('ui_admin.error') . Kohana::lang('ui_admin.check_sms_settings')));
                 }
             } else {
                 echo json_encode(array("status" => "error", "message" => Kohana::lang('ui_admin.error') . Kohana::lang('ui_admin.check_number')));
             }
         } else {
             // populate the error fields, if any
             $errors = arr::overwrite($errors, $post->errors('messages'));
             echo json_encode(array("status" => "error", "message" => Kohana::lang('ui_admin.error') . Kohana::lang('ui_admin.check_message_valid')));
         }
     }
 }
Exemplo n.º 5
0
 public function index()
 {
     $db = new Database();
     $incidents = $db->query("SELECT distinct incident.id, incident_title, incident_verified, location.latitude, location.longitude \n\t\t\t\t\t\t\t\t\tFROM location, incident, alert_sent \n\t\t\t\t\t\t\t\t\tWHERE incident.location_id = location.id AND incident.id != alert_sent.incident_id");
     $settings = null;
     $sms_from = null;
     $clickatell = null;
     foreach ($incidents as $incident) {
         $verified = (int) $incident->incident_verified;
         if ($verified) {
             $latitude = (double) $incident->latitude;
             $longitude = (double) $incident->longitude;
             $proximity = new Proximity($latitude, $longitude);
             $alertees = $this->_get_alertees($proximity);
             foreach ($alertees as $alertee) {
                 $alert_type = (int) $alertee->alert_type;
                 if ($alert_type == 1) {
                     if ($settings == null) {
                         $settings = ORM::factory('settings', 1);
                         if ($settings->loaded == true) {
                             // Get SMS Numbers
                             if (!empty($settings->sms_no3)) {
                                 $sms_from = $settings->sms_no3;
                             } elseif (!empty($settings->sms_no2)) {
                                 $sms_from = $settings->sms_no2;
                             } elseif (!empty($settings->sms_no1)) {
                                 $sms_from = $settings->sms_no1;
                             } else {
                                 $sms_from = "000";
                             }
                             // User needs to set up an SMS number
                         }
                         $clickatell = new Clickatell();
                         $clickatell->api_id = $settings->clickatell_api;
                         $clickatell->user = $settings->clickatell_username;
                         $clickatell->password = $settings->clickatell_password;
                         $clickatell->use_ssl = false;
                         $clickatell->sms();
                     }
                     //XXX: Fix the outgoing message!
                     $message = $incident->incident_title . " occured near you!";
                     echo "{$message}<br/>";
                     if ($clickatell->send($alertee->alert_alert_recipient, $sms_from, $message) == "OK") {
                         $alert = ORM::factory('alert_sent');
                         $alert->alert_id = $alertee->id;
                         $alert->incident_id = $incident->id;
                         $alert->alert_date = date("Y-m-d H:i:s");
                         $alert->save();
                     }
                 } elseif ($alert_type == 2) {
                     //XXX: Setup correct 'from' address and message
                     $to = $alertee->alert_recipient;
                     $from = '*****@*****.**';
                     $subject = 'Ushahidi alert!';
                     $message = $incident->incident_title . " occured near you!";
                     if (email::send($to, $from, $subject, $message, TRUE) == 1) {
                         $alert = ORM::factory('alert_sent');
                         $alert->alert_id = $alertee->id;
                         $alert->incident_id = $incident->id;
                         $alert->alert_date = date("Y-m-d H:i:s");
                         $alert->save();
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 6
0
 public function index()
 {
     // Create new session
     $this->session->create();
     $this->template->header->this_page = 'alerts';
     $this->template->content = new View('alerts');
     // Display news feeds?
     $this->template->content->allow_feed = Kohana::config('settings.allow_feed');
     // Retrieve default country, latitude, longitude
     $default_country = Kohana::config('settings.default_country');
     // Retrieve Country Cities
     $this->template->content->cities = $this->_get_cities($default_country);
     // setup and initialize form field names
     $form = array('alert_mobile' => '', 'alert_mobile_yes' => '', 'alert_email' => '', 'alert_email_yes' => '', 'alert_lat' => '', 'alert_lon' => '');
     // copy the form as errors, so the errors will be stored with keys
     // corresponding to the form field names
     $errors = $form;
     $form_error = FALSE;
     $form_saved = FALSE;
     // check, has the form been submitted, if so, setup validation
     if ($_POST) {
         // Instantiate Validation, use $post, so we don't overwrite $_POST fields with our own things
         $post = new Validation($_POST);
         //  Add some filters
         $post->pre_filter('trim', TRUE);
         // Add some rules, the input field, followed by a list of checks, carried out in order
         if (!empty($_POST['alert_mobile']) || isset($_POST['alert_mobile_yes'])) {
             $post->add_rules('alert_mobile', 'required', 'numeric', 'length[6,20]');
         }
         if (!empty($_POST['alert_email']) || isset($_POST['alert_email_yes'])) {
             $post->add_rules('alert_email', 'required', 'email', 'length[3,64]');
         }
         if (empty($_POST['alert_email']) && empty($_POST['alert_mobile'])) {
             $post->add_error('alert_mobile', 'one_required');
             $post->add_error('alert_email', 'one_required');
         }
         $post->add_rules('alert_lat', 'required', 'between[-90,90]');
         // Validate for maximum and minimum latitude values
         $post->add_rules('alert_lon', 'required', 'between[-180,180]');
         // Validate for maximum and minimum longitude values
         // Add a callback, to validate the mobile phone/email (See the methods below)
         $post->add_callbacks('alert_mobile', array($this, 'mobile_check'));
         $post->add_callbacks('alert_email', array($this, 'email_check'));
         // Test to see if things passed the rule checks
         if ($post->validate()) {
             // Yes! everything is valid
             // Save alert and send out confirmation code
             $email_confirmation_saved = FALSE;
             $sms_confirmation_saved = FALSE;
             if (!empty($post->alert_mobile)) {
                 $alert_code = $this->_mk_code();
                 $settings = ORM::factory('settings', 1);
                 if ($settings->loaded == true) {
                     // Get SMS Numbers
                     if (!empty($settings->sms_no3)) {
                         $sms_from = $settings->sms_no3;
                     } elseif (!empty($settings->sms_no2)) {
                         $sms_from = $settings->sms_no2;
                     } elseif (!empty($settings->sms_no1)) {
                         $sms_from = $settings->sms_no1;
                     } else {
                         $sms_from = "000";
                         // User needs to set up an SMS number
                     }
                     $sms = new Clickatell();
                     $sms->api_id = $settings->clickatell_api;
                     $sms->user = $settings->clickatell_username;
                     $sms->password = $settings->clickatell_password;
                     $sms->use_ssl = false;
                     $sms->sms();
                     $message = "Your alerts confirmation code\n\t\t\t\t\t\t\t\tis: " . $alert_code . " This code is NOT case sensitive";
                     if ($sms->send($post->alert_mobile, $sms_from, $message) == "OK") {
                         $alert = ORM::factory('alert');
                         $alert->alert_type = self::MOBILE_ALERT;
                         $alert->alert_recipient = $post->alert_mobile;
                         $alert->alert_code = $alert_code;
                         $alert->alert_lon = $post->alert_lon;
                         $alert->alert_lat = $post->alert_lat;
                         $alert->save();
                         if ($alert->saved == TRUE) {
                             $sms_confirmation_saved = TRUE;
                         }
                     }
                 }
             }
             if (!empty($post->alert_email)) {
                 $alert_code = $this->_mk_code();
                 //Send verification email
                 $config = kohana::config('alerts');
                 $settings = kohana::config('settings');
                 $to = $post->alert_email;
                 $from = $config['alerts_email'];
                 $subject = $settings['site_name'] . ' alerts - verification';
                 $message = 'Please follow ' . url::base() . 'alerts/verify/' . $alert_code . ' to confirm your alert request';
                 if (email::send($to, $from, $subject, $message, TRUE) == 1) {
                     $alert = ORM::factory('alert');
                     $alert->alert_type = self::EMAIL_ALERT;
                     $alert->alert_recipient = $post->alert_email;
                     $alert->alert_code = $alert_code;
                     $alert->alert_lon = $post->alert_lon;
                     $alert->alert_lat = $post->alert_lat;
                     $alert->save();
                     if ($alert->saved == TRUE) {
                         $email_confirmation_saved = TRUE;
                     }
                 }
             }
             $this->session->set('alert_mobile', $post->alert_mobile);
             $this->session->set('alert_email', $post->alert_email);
             $this->session->set('sms_confirmation_saved', $sms_confirmation_saved);
             $this->session->set('email_confirmation_saved', $email_confirmation_saved);
             url::redirect('alerts/confirm');
         } else {
             // repopulate the form fields
             $form = arr::overwrite($form, $post->as_array());
             // populate the error fields, if any
             $errors = arr::overwrite($errors, $post->errors('alerts'));
             $form_error = TRUE;
         }
     } else {
         $form['alert_lat'] = Kohana::config('settings.default_lat');
         $form['alert_lon'] = Kohana::config('settings.default_lon');
     }
     $this->template->content->form = $form;
     $this->template->content->errors = $errors;
     $this->template->content->form_error = $form_error;
     $this->template->content->form_saved = $form_saved;
     // Javascript Header
     $this->template->header->map_enabled = TRUE;
     $this->template->header->js = new View('alerts_js');
     $this->template->header->js->default_map = Kohana::config('settings.default_map');
     $this->template->header->js->default_zoom = Kohana::config('settings.default_zoom');
     $this->template->header->js->latitude = $form['alert_lat'];
     $this->template->header->js->longitude = $form['alert_lon'];
 }
Exemplo n.º 7
0
    public function index()
    {
        $settings = ORM::factory('settings')->find(1);
        $site_name = $settings->site_name;
        $alerts_email = $settings->alerts_email;
        $unsubscribe_message = Kohana::lang('alerts.unsubscribe') . url::site() . 'alerts/unsubscribe/';
        $sms_from = NULL;
        $clickatell = NULL;
        $miles = FALSE;
        // Change to True to calculate distances in Miles
        $max_recipients = 20;
        // Limit each script execution to 50 recipients
        if ($settings->email_smtp == 1) {
            Kohana::config_set('email.driver', 'smtp');
            Kohana::config_set('email.options', array('hostname' => $settings->alerts_host, 'port' => $settings->alerts_port, 'username' => $settings->alerts_username, 'password' => $settings->alerts_password));
        }
        $db = new Database();
        /* Find All Alerts with the following parameters
        		- incident_active = 1 -- An approved incident
        		- incident_alert_status = 1 -- Incident has been tagged for sending
        		
        		Incident Alert Statuses
        		  - 0, Incident has not been tagged for sending. Ensures old incidents are not sent out as alerts
        		  - 1, Incident has been tagged for sending by updating it with 'approved' or 'verified'
        		  - 2, Incident has been tagged as sent. No need to resend again
        		*/
        // 1 - Retrieve All the Incidents that haven't been sent (Process only 1 per script execution)
        $incidents = $db->query("\n\t\t\t\t\t\t\t\t SELECT incident.id, incident_title, \n\t\t\t\t\t\t\t\t incident_description, incident_verified, \n\t\t\t\t\t\t\t\t location.latitude, location.longitude\n\t\t\t\t\t\t\t\t FROM incident\n\t\t\t\t\t\t\t\t INNER JOIN location on location.id = incident.location_id\n\t\t\t\t\t\t\t\t WHERE incident.incident_active=1 AND incident.incident_alert_status = 1 LIMIT 1\n\t\t\t\t\t\t\t\t");
        foreach ($incidents as $incident) {
            $latitude = (double) $incident->latitude;
            $longitude = (double) $incident->longitude;
            // 2 - Retrieve All Qualifying Alertees Based on Distance and Make sure they haven't received this alert,
            // 	   Return Alertees if they haven't requested a Category OR Return Alertees if they specifically asked
            // 	   for this incident region and specific category.
            $distance_type = $miles ? "" : " * 1.609344";
            $alertees = $db->query('
									SELECT DISTINCT alert.*, ((ACOS(SIN(' . $latitude . ' * PI() / 180) * 
									SIN(`alert`.`alert_lat` * PI() / 180) + COS(' . $latitude . ' * PI() / 180) * 
									COS(`alert`.`alert_lat` * PI() / 180) * COS((' . $longitude . ' - `alert`.`alert_lon`)
									 * PI() / 180)) * 180 / PI()) * 60 * 1.1515 ' . $distance_type . ') AS distance
									FROM alert 
									WHERE  alert.alert_confirmed = 1
									HAVING distance <= alert_radius
									');
            $i = 0;
            foreach ($alertees as $alertee) {
                // 3 - Has this incident alert been sent to this alertee?
                $alert_sent = ORM::factory('alert_sent')->where('alert_id', $alertee->id)->where('incident_id', $incident->id)->find();
                $requested_category = true;
                $alert_categories = ORM::factory('alert_category')->select("alert_category.category_id")->where('alert_id', $alertee->id)->find_all();
                // The Alertee requested specific categories for the incident.
                // Make sure the categories are part of this incident.
                $requested_category = alert::requested_category($alert_categories, $incident->id);
                // Alert has not been sent and the alertee requested the incident with the category
                if ($alert_sent->loaded == FALSE && $requested_category) {
                    // 4 - Get Alert Type. 1=SMS, 2=EMAIL
                    $alert_type = (int) $alertee->alert_type;
                    if ($alert_type == 1) {
                        // Save this first
                        $alert = ORM::factory('alert_sent');
                        $alert->alert_id = $alertee->id;
                        $alert->incident_id = $incident->id;
                        $alert->alert_date = date("Y-m-d H:i:s");
                        $alert->save();
                        // Get SMS Numbers
                        if (!empty($settings->sms_no3)) {
                            $sms_from = $settings->sms_no3;
                        } elseif (!empty($settings->sms_no2)) {
                            $sms_from = $settings->sms_no2;
                        } elseif (!empty($settings->sms_no1)) {
                            $sms_from = $settings->sms_no1;
                        } else {
                            $sms_from = "000";
                        }
                        // Admin needs to set up an SMS number
                        $clickatell = new Clickatell();
                        $clickatell->api_id = $settings->clickatell_api;
                        $clickatell->user = $settings->clickatell_username;
                        $clickatell->password = $settings->clickatell_password;
                        $clickatell->use_ssl = false;
                        $clickatell->sms();
                        $message = text::limit_chars($incident->incident_description, 150, "...");
                        //++ We won't verify for now if sms was sent
                        // Leaves too much room for duplicates to be sent out
                        $clickatell->send($alertee->alert_recipient, $sms_from, $message);
                    } elseif ($alert_type == 2) {
                        // Save this first
                        $alert = ORM::factory('alert_sent');
                        $alert->alert_id = $alertee->id;
                        $alert->incident_id = $incident->id;
                        $alert->alert_date = date("Y-m-d H:i:s");
                        $alert->save();
                        //for some reason, mail function complains about bad parameters
                        // in the function so i'm disallowing these characters in the
                        // subject field to allow the mail function to work.
                        $disallowed_chars = array("(", ")", "[", "]", "-");
                        $to = $alertee->alert_recipient;
                        $from = $alerts_email;
                        $subject = trim(str_replace($disallowed_chars, "", $site_name) . ": " . str_replace($disallowed_chars, "", $incident->incident_title));
                        $message = $incident->incident_description . "<p>" . url::base() . "reports/view/" . $incident->id . "</p>" . "<p>" . $unsubscribe_message . "?c=" . $alertee->alert_code . "</p>";
                        //++ We won't verify for now if email was sent
                        // Leaves too much room for duplicates to be sent out
                        email::send($to, $from, $subject, $message, TRUE);
                    }
                    sleep(1);
                    $i++;
                }
                if ($i == $max_recipients) {
                    exit;
                }
            }
            // End For Each Loop
            // Update Incident - All Alerts Have Been Sent!
            $update_incident = ORM::factory('incident', $incident->id);
            if ($update_incident->loaded) {
                $update_incident->incident_alert_status = 2;
                $update_incident->save();
            }
        }
    }
Exemplo n.º 8
0
 public function index()
 {
     $db = new Database();
     $incidents = $db->query("SELECT incident.id, incident_title,\n\t\t\t\t\t\t\t\t incident_description,\n\t\t\t\t\t\t\t     incident_verified, location.latitude, \n\t\t\t\t\t\t\t\t location.longitude FROM incident INNER JOIN location \n\t\t\t\t\t\t\t\t ON incident.location_id = location.id");
     $alerts_sent = $db->query("SELECT incident_id FROM alert_sent");
     $alerts_sent_ids = array();
     foreach ($alerts_sent as $alert_sent) {
         array_push($alerts_sent_ids, $alert_sent->incident_id);
     }
     $config = kohana::config('alerts');
     $settings = null;
     $sms_from = null;
     $clickatell = null;
     foreach ($incidents as $incident) {
         if (in_array($incident->id, $alerts_sent_ids)) {
             continue;
         }
         $verified = (int) $incident->incident_verified;
         if ($verified) {
             $latitude = (double) $incident->latitude;
             $longitude = (double) $incident->longitude;
             $proximity = new Proximity($latitude, $longitude);
             $alertees = $this->_get_alertees($proximity);
             foreach ($alertees as $alertee) {
                 $alert_type = (int) $alertee->alert_type;
                 if ($alert_type == 1) {
                     if ($settings == null) {
                         $settings = ORM::factory('settings', 1);
                         if ($settings->loaded == true) {
                             // Get SMS Numbers
                             if (!empty($settings->sms_no3)) {
                                 $sms_from = $settings->sms_no3;
                             } elseif (!empty($settings->sms_no2)) {
                                 $sms_from = $settings->sms_no2;
                             } elseif (!empty($settings->sms_no1)) {
                                 $sms_from = $settings->sms_no1;
                             } else {
                                 $sms_from = "000";
                             }
                             // User needs to set up an SMS number
                         }
                         $clickatell = new Clickatell();
                         $clickatell->api_id = $settings->clickatell_api;
                         $clickatell->user = $settings->clickatell_username;
                         $clickatell->password = $settings->clickatell_password;
                         $clickatell->use_ssl = false;
                         $clickatell->sms();
                     }
                     //XXX: Fix the outgoing message!
                     $message = $incident->incident_description;
                     if ($clickatell->send($alertee->alert_recipient, $sms_from, $message) == "OK") {
                         $alert = ORM::factory('alert_sent');
                         $alert->alert_id = $alertee->id;
                         $alert->incident_id = $incident->id;
                         $alert->alert_date = date("Y-m-d H:i:s");
                         $alert->save();
                     }
                 } elseif ($alert_type == 2) {
                     //XXX: Setup correct 'from' address and message
                     $to = $alertee->alert_recipient;
                     $from = $config['alerts_email'];
                     $subject = $incident->incident_title;
                     $message = $incident->incident_description;
                     if (email::send($to, $from, $subject, $message, TRUE) == 1) {
                         $alert = ORM::factory('alert_sent');
                         $alert->alert_id = $alertee->id;
                         $alert->incident_id = $incident->id;
                         $alert->alert_date = date("Y-m-d H:i:s");
                         $alert->save();
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 9
0
 public function index()
 {
     $settings = kohana::config('settings');
     $site_name = $settings['site_name'];
     $alerts_email = $settings['alerts_email'];
     $unsubscribe_message = Kohana::lang('alerts.unsubscribe') . url::site() . 'alerts/unsubscribe/';
     $database_settings = kohana::config('database');
     //around line 33
     $this->table_prefix = $database_settings['default']['table_prefix'];
     //around line 34
     $settings = NULL;
     $sms_from = NULL;
     $clickatell = NULL;
     $db = new Database();
     /* Find All Alerts with the following parameters
     		- incident_active = 1 -- An approved incident
     		- incident_alert_status = 1 -- Incident has been tagged for sending
     		
     		Incident Alert Statuses
     		  - 0, Incident has not been tagged for sending. Ensures old incidents are not sent out as alerts
     		  - 1, Incident has been tagged for sending by updating it with 'approved' or 'verified'
     		  - 2, Incident has been tagged as sent. No need to resend again
     		*/
     $incidents = $db->query("SELECT i.id, incident_title, \n\t\t\t\t\t\t\t\t incident_description, incident_verified, \n\t\t\t\t\t\t\t\t l.latitude, l.longitude, a.alert_id, a.incident_id\n\t\t\t\t\t\t\t\t FROM " . $this->table_prefix . "incident AS i INNER JOIN " . $this->table_prefix . "location AS l ON i.location_id = l.id\n\t\t\t\t\t\t\t\t LEFT OUTER JOIN " . $this->table_prefix . "alert_sent AS a ON i.id = a.incident_id WHERE\n\t\t\t\t\t\t\t\t i.incident_active=1 AND i.incident_archived = 0 AND i.incident_alert_status = 1 ");
     foreach ($incidents as $incident) {
         $latitude = (double) $incident->latitude;
         $longitude = (double) $incident->longitude;
         // Get all alertees
         $alertees = ORM::factory('alert')->where('alert_confirmed', '1')->find_all();
         foreach ($alertees as $alertee) {
             // Has this alert been sent to this alertee?
             if ($alertee->id == $incident->alert_id) {
                 continue;
             }
             $alert_radius = (int) $alertee->alert_radius;
             $alert_type = (int) $alertee->alert_type;
             $latitude2 = (double) $alertee->alert_lat;
             $longitude2 = (double) $alertee->alert_lon;
             $distance = (string) new Distance($latitude, $longitude, $latitude2, $longitude2);
             // If the calculated distance between the incident and the alert fits...
             if ($distance <= $alert_radius) {
                 if ($alert_type == 1) {
                     if ($settings == null) {
                         $settings = ORM::factory('settings', 1);
                         if ($settings->loaded == true) {
                             // Get SMS Numbers
                             if (!empty($settings->sms_no3)) {
                                 $sms_from = $settings->sms_no3;
                             } elseif (!empty($settings->sms_no2)) {
                                 $sms_from = $settings->sms_no2;
                             } elseif (!empty($settings->sms_no1)) {
                                 $sms_from = $settings->sms_no1;
                             } else {
                                 $sms_from = "000";
                             }
                             // Admin needs to set up an SMS number
                         }
                         $clickatell = new Clickatell();
                         $clickatell->api_id = $settings->clickatell_api;
                         $clickatell->user = $settings->clickatell_username;
                         $clickatell->password = $settings->clickatell_password;
                         $clickatell->use_ssl = false;
                         $clickatell->sms();
                     }
                     $message = $incident->incident_description;
                     // If Clickatell Is Set Up
                     if ($clickatell->send($alertee->alert_recipient, $sms_from, $message) == "OK") {
                         $alert = ORM::factory('alert_sent');
                         $alert->alert_id = $alertee->id;
                         $alert->incident_id = $incident->id;
                         $alert->alert_date = date("Y-m-d H:i:s");
                         $alert->save();
                     }
                 } elseif ($alert_type == 2) {
                     $to = $alertee->alert_recipient;
                     $from = $alerts_email;
                     $subject = "[{$site_name}] " . $incident->incident_title;
                     $message = $incident->incident_description . "<p>" . $unsubscribe_message . $alertee->alert_code . "</p>";
                     if (email::send($to, $from, $subject, $message, TRUE) == 1) {
                         $alert = ORM::factory('alert_sent');
                         $alert->alert_id = $alertee->id;
                         $alert->incident_id = $incident->id;
                         $alert->alert_date = date("Y-m-d H:i:s");
                         $alert->save();
                     }
                 }
             }
         }
         // End For Each Loop
         // Update Incident - All Alerts Have Been Sent!
         $update_incident = ORM::factory('incident', $incident->id);
         if ($update_incident->loaded) {
             $update_incident->incident_alert_status = 2;
             $update_incident->save();
         }
     }
 }
Exemplo n.º 10
0
    public function index()
    {
        $config = kohana::config('alerts');
        $settings = kohana::config('settings');
        $site_name = $settings['site_name'];
        $unsubscribe_message = Kohana::lang('alerts.unsubscribe') . url::site() . 'alerts/unsubscribe/';
        $settings = NULL;
        $sms_from = NULL;
        $clickatell = NULL;
        $miles = FALSE;
        // Change to True to calculate distances in Miles
        $max_recipients = 20;
        // Limit each script execution to 50 recipients
        $db = new Database();
        $incidents = $db->query("SELECT incident.id, incident_title, \n\t\t\t\t\t\t\t\t incident_description, incident_verified, \n\t\t\t\t\t\t\t\t location.latitude, location.longitude, alert_sent.incident_id\n\t\t\t\t\t\t\t\t FROM incident INNER JOIN location ON incident.location_id = location.id\n\t\t\t\t\t\t\t\t LEFT OUTER JOIN alert_sent ON incident.id = alert_sent.incident_id");
        foreach ($incidents as $incident) {
            $latitude = (double) $incident->latitude;
            $longitude = (double) $incident->longitude;
            // 2 - Retrieve All Qualifying Alertees Based on Distance and Make sure they haven't received this alert
            $distance_type = $miles ? "" : " * 1.609344";
            $alertees = $db->query('SELECT DISTINCT alert.*, ((ACOS(SIN(' . $latitude . ' * PI() / 180) * 
									SIN(`alert`.`alert_lat` * PI() / 180) + COS(' . $latitude . ' * PI() / 180) * 
									COS(`alert`.`alert_lat` * PI() / 180) * COS((' . $longitude . ' - `alert`.`alert_lon`)
									 * PI() / 180)) * 180 / PI()) * 60 * 1.1515 ' . $distance_type . ') AS distance
									FROM alert WHERE alert.alert_confirmed = 1 
									HAVING distance <= alert_radius ');
            if ($incident->incident_id != NULL) {
                continue;
            }
            $verified = (int) $incident->incident_verified;
            if ($verified) {
                $latitude = (double) $incident->latitude;
                $longitude = (double) $incident->longitude;
                $proximity = new Proximity($latitude, $longitude);
                $alertees = $this->_get_alertees($proximity);
                foreach ($alertees as $alertee) {
                    $alert_type = (int) $alertee->alert_type;
                    if ($alert_type == 1) {
                        if ($settings == null) {
                            $settings = ORM::factory('settings', 1);
                            if ($settings->loaded == true) {
                                // Get SMS Numbers
                                if (!empty($settings->sms_no3)) {
                                    $sms_from = $settings->sms_no3;
                                } elseif (!empty($settings->sms_no2)) {
                                    $sms_from = $settings->sms_no2;
                                } elseif (!empty($settings->sms_no1)) {
                                    $sms_from = $settings->sms_no1;
                                } else {
                                    $sms_from = "000";
                                }
                                // User needs to set up an SMS number
                            }
                            $clickatell = new Clickatell();
                            $clickatell->api_id = $settings->clickatell_api;
                            $clickatell->user = $settings->clickatell_username;
                            $clickatell->password = $settings->clickatell_password;
                            $clickatell->use_ssl = false;
                            $clickatell->sms();
                        }
                        $message = $incident->incident_description;
                        if ($clickatell->send($alertee->alert_recipient, $sms_from, $message) == "OK") {
                            $alert = ORM::factory('alert_sent');
                            $alert->alert_id = $alertee->id;
                            $alert->incident_id = $incident->id;
                            $alert->alert_date = date("Y-m-d H:i:s");
                            $alert->save();
                        }
                    } elseif ($alert_type == 2) {
                        $disallowed_chars = array("(", ")", "[", "]");
                        $to = $alertee->alert_recipient;
                        $from = $config['alerts_email'];
                        $subject = trim(str_replace($disallowed_chars, "", $site_name) . ": " . str_replace($disallowed_chars, "", $incident->incident_title));
                        $message = $incident->incident_description . "<p>" . url::base() . "reports/view/" . $incident->id . "</p>" . "<p>" . $unsubscribe_message . $alertee->alert_code . "</p>";
                        if (email::send($to, $from, $subject, $message, TRUE) == 1) {
                            $alert = ORM::factory('alert_sent');
                            $alert->alert_id = $alertee->id;
                            $alert->incident_id = $incident->id;
                            $alert->alert_date = date("Y-m-d H:i:s");
                            $alert->save();
                        }
                    }
                }
            }
        }
    }