Exemplo n.º 1
0
 function index()
 {
     if (isset($_GET['key'])) {
         $frontlinesms_key = $_GET['key'];
     }
     if (isset($_GET['s'])) {
         $message_from = $_GET['s'];
         // Remove non-numeric characters from string
         $message_from = ereg_replace("[^0-9]", "", $message_from);
     }
     if (isset($_GET['m'])) {
         $message_description = $_GET['m'];
     }
     if (!empty($frontlinesms_key) && !empty($message_from) && !empty($message_description)) {
         // Is this a valid FrontlineSMS Key?
         $keycheck = ORM::factory('settings', 1)->where('frontlinesms_key', $frontlinesms_key)->find();
         if ($keycheck->loaded == true) {
             $services = new Service_Model();
             $service = $services->where('service_name', 'SMS')->find();
             if (!$service) {
                 return;
             }
             $reporter_check = ORM::factory('reporter')->where('service_id', $service->id)->where('service_account', $message_from)->find();
             if ($reporter_check->loaded == true) {
                 $reporter_id = $reporter_check->id;
             } else {
                 // get default reporter level (Untrusted)
                 $levels = new Level_Model();
                 $default_level = $levels->where('level_weight', 0)->find();
                 $reporter = new Reporter_Model();
                 $reporter->service_id = $service->id;
                 $reporter->service_userid = null;
                 $reporter->service_account = $message_from;
                 $reporter->reporter_level = $default_level;
                 $reporter->reporter_first = null;
                 $reporter->reporter_last = null;
                 $reporter->reporter_email = null;
                 $reporter->reporter_phone = null;
                 $reporter->reporter_ip = null;
                 $reporter->reporter_date = date('Y-m-d');
                 $reporter->save();
                 $reporter_id = $reporter->id;
             }
             // Save Message
             $message = new Message_Model();
             $message->parent_id = 0;
             $message->incident_id = 0;
             $message->user_id = 0;
             $message->reporter_id = $reporter_id;
             $message->message_from = $message_from;
             $message->message_to = null;
             $message->message = $message_description;
             $message->message_type = 1;
             // Inbox
             $message->message_date = date("Y-m-d H:i:s", time());
             $message->service_messageid = null;
             $message->save();
         }
     }
 }
Exemplo n.º 2
0
 /**
  * Adds email to the database and saves the sender as a new
  * Reporter if they don't already exist
  * @param string $messages
  */
 private function add_email($messages)
 {
     $services = new Service_Model();
     $service = $services->where('service_name', 'Email')->find();
     if (!$service) {
         return;
     }
     if (empty($messages) || !is_array($messages)) {
         return;
     }
     foreach ($messages as $message) {
         $reporter = null;
         $reporter_check = ORM::factory('reporter')->where('service_id', $service->id)->where('service_account', $message['email'])->find();
         if ($reporter_check->loaded == true) {
             $reporter_id = $reporter_check->id;
             $reporter = ORM::factory('reporter')->find($reporter_id);
         } else {
             // Add new reporter
             $names = explode(' ', $message['from'], 2);
             $last_name = '';
             if (count($names) == 2) {
                 $last_name = $names[1];
             }
             // get default reporter level (Untrusted)
             $levels = new Level_Model();
             $default_level = $levels->where('level_weight', 0)->find();
             $reporter = new Reporter_Model();
             $reporter->service_id = $service->id;
             $reporter->service_userid = null;
             $reporter->service_account = $message['email'];
             $reporter->reporter_level = $default_level;
             $reporter->reporter_first = $names[0];
             $reporter->reporter_last = $last_name;
             $reporter->reporter_email = null;
             $reporter->reporter_phone = null;
             $reporter->reporter_ip = null;
             $reporter->reporter_date = date('Y-m-d');
             $reporter->save();
             $reporter_id = $reporter->id;
         }
         if ($reporter->reporter_level > 1) {
             // Save Email as Message
             $email = new Message_Model();
             $email->parent_id = 0;
             $email->incident_id = 0;
             $email->user_id = 0;
             $email->reporter_id = $reporter_id;
             $email->message_from = $message['from'];
             $email->message_to = null;
             $email->message = $message['subject'];
             $email->message_detail = $message['body'];
             $email->message_type = 1;
             // Inbox
             $email->message_date = $message['date'];
             $email->service_messageid = null;
             $email->save();
         }
     }
 }
Exemplo n.º 3
0
 function index()
 {
     if (isset($_GET['key'])) {
         $frontlinesms_key = $_GET['key'];
     }
     if (isset($_GET['s'])) {
         $message_from = $_GET['s'];
         // Remove non-numeric characters from string
         $message_from = ereg_replace("[^0-9]", "", $message_from);
     }
     if (isset($_GET['m'])) {
         $message_description = $_GET['m'];
     }
     if (!empty($frontlinesms_key) && !empty($message_from) && !empty($message_description)) {
         // Is this a valid FrontlineSMS Key?
         $keycheck = ORM::factory('settings', 1)->where('frontlinesms_key', $frontlinesms_key)->find();
         if ($keycheck->loaded == TRUE) {
             $services = new Service_Model();
             $service = $services->where('service_name', 'SMS')->find();
             if (!$service) {
                 return;
             }
             $reporter = ORM::factory('reporter')->where('service_id', $service->id)->where('service_account', $message_from)->find();
             if (!$reporter->loaded == TRUE) {
                 // get default reporter level (Untrusted)
                 $level = ORM::factory('level')->where('level_weight', 0)->find();
                 $reporter->service_id = $service->id;
                 $reporter->level_id = $level->id;
                 $reporter->service_userid = null;
                 $reporter->service_account = $message_from;
                 $reporter->reporter_first = null;
                 $reporter->reporter_last = null;
                 $reporter->reporter_email = null;
                 $reporter->reporter_phone = null;
                 $reporter->reporter_ip = null;
                 $reporter->reporter_date = date('Y-m-d');
                 $reporter->save();
             }
             // Save Message
             $message = new Message_Model();
             $message->parent_id = 0;
             $message->incident_id = 0;
             $message->user_id = 0;
             $message->reporter_id = $reporter->id;
             $message->message_from = $message_from;
             $message->message_to = null;
             $message->message = $message_description;
             $message->message_type = 1;
             // Inbox
             $message->message_date = date("Y-m-d H:i:s", time());
             $message->service_messageid = null;
             $message->save();
             // Notify Admin Of New Email Message
             $send = notifications::notify_admins("[" . Kohana::config('settings.site_name') . "] " . Kohana::lang('notifications.admin_new_sms.subject'), Kohana::lang('notifications.admin_new_sms.message'));
         }
     }
 }
Exemplo n.º 4
0
 /**
  * Adds email to the database and saves the sender as a new
  * Reporter if they don't already exist
  * @param string $messages
  */
 private function add_email($messages)
 {
     $services = new Service_Model();
     $service = $services->where('service_name', 'Email')->find();
     if (!$service) {
         return;
     }
     if (empty($messages) || !is_array($messages)) {
         return;
     }
     foreach ($messages as $message) {
         $reporter = ORM::factory('reporter')->where('service_id', $service->id)->where('service_account', $message['email'])->find();
         if (!$reporter->loaded == true) {
             // Add new reporter
             $names = explode(' ', $message['from'], 2);
             $last_name = '';
             if (count($names) == 2) {
                 $last_name = $names[1];
             }
             // get default reporter level (Untrusted)
             $level = ORM::factory('level')->where('level_weight', 0)->find();
             $reporter->service_id = $service->id;
             $reporter->level_id = $level->id;
             $reporter->service_userid = null;
             $reporter->service_account = $message['email'];
             $reporter->reporter_first = $names[0];
             $reporter->reporter_last = $last_name;
             $reporter->reporter_email = $message['email'];
             $reporter->reporter_phone = null;
             $reporter->reporter_ip = null;
             $reporter->reporter_date = date('Y-m-d');
             $reporter->save();
         }
         if ($reporter->level_id > 1 && count(ORM::factory('message')->where('service_messageid', $message['message_id'])->find_all()) == 0) {
             // Save Email as Message
             $email = new Message_Model();
             $email->parent_id = 0;
             $email->incident_id = 0;
             $email->user_id = 0;
             $email->reporter_id = $reporter->id;
             $email->message_from = $message['from'];
             $email->message_to = null;
             $email->message = $message['subject'];
             $email->message_detail = $message['body'];
             $email->message_type = 1;
             // Inbox
             $email->message_date = $message['date'];
             $email->service_messageid = $message['message_id'];
             $email->save();
             // Notify Admin Of New Email Message
             $send = notifications::notify_admins("[" . Kohana::config('settings.site_name') . "] " . Kohana::lang('notifications.admin_new_email.subject'), Kohana::lang('notifications.admin_new_email.message'));
         }
     }
 }
Exemplo n.º 5
0
 /**
  * Adds hash tweets in JSON format to the database and saves the sender as a new
  * Reporter if they don't already exist
  * @param string $data - Twitter JSON results
  */
 private function add_hash_tweets($data)
 {
     $services = new Service_Model();
     $service = $services->where('service_name', 'Twitter')->find();
     if (!$service) {
         return;
     }
     $tweets = json_decode($data, false);
     if (!$tweets) {
         return;
     }
     if (isset($tweets->{'error'})) {
         return;
     }
     $tweet_results = $tweets->{'results'};
     foreach ($tweet_results as $tweet) {
         $reporter = ORM::factory('reporter')->where('service_id', $service->id)->where('service_account', $tweet->{'from_user'})->find();
         if (!$reporter->loaded) {
             // get default reporter level (Untrusted)
             $level = ORM::factory('level')->where('level_weight', 0)->find();
             $reporter->service_id = $service->id;
             $reporter->level_id = $level->id;
             $reporter->service_userid = null;
             $reporter->service_account = $tweet->{'from_user'};
             $reporter->reporter_first = null;
             $reporter->reporter_last = null;
             $reporter->reporter_email = null;
             $reporter->reporter_phone = null;
             $reporter->reporter_ip = null;
             $reporter->reporter_date = date('Y-m-d');
             $reporter->save();
         }
         if ($reporter->level_id > 1 && count(ORM::factory('message')->where('service_messageid', $tweet->{'id'})->find_all()) == 0) {
             // Save Tweet as Message
             $message = new Message_Model();
             $message->parent_id = 0;
             $message->incident_id = 0;
             $message->user_id = 0;
             $message->reporter_id = $reporter->id;
             $message->message_from = $tweet->{'from_user'};
             $message->message_to = null;
             $message->message = $tweet->{'text'};
             $message->message_type = 1;
             // Inbox
             $tweet_date = date("Y-m-d H:i:s", strtotime($tweet->{'created_at'}));
             $message->message_date = $tweet_date;
             $message->service_messageid = $tweet->{'id'};
             $message->save();
         }
     }
 }
Exemplo n.º 6
0
 /**
  * Adds hash tweets in JSON format to the database and saves the sender as a new
  * Reporter if they don't already exist
  * @param string $data - Twitter JSON results
  */
 private function add_hash_tweets($data)
 {
     if ($this->_lock()) {
         return false;
     }
     $services = new Service_Model();
     $service = $services->where('service_name', 'Twitter')->find();
     if (!$service) {
         $this->_unlock();
         return false;
     }
     $tweets = json_decode($data, false);
     if (!$tweets) {
         $this->_unlock();
         return false;
     }
     if (isset($tweets->{'error'})) {
         $this->_unlock();
         return false;
     }
     $tweet_results = $tweets->{'results'};
     foreach ($tweet_results as $tweet) {
         $reporter = ORM::factory('reporter')->where('service_id', $service->id)->where('service_account', $tweet->{'from_user'})->find();
         if (!$reporter->loaded) {
             // get default reporter level (Untrusted)
             $level = ORM::factory('level')->where('level_weight', 0)->find();
             $reporter->service_id = $service->id;
             $reporter->level_id = $level->id;
             $reporter->service_account = $tweet->{'from_user'};
             $reporter->reporter_first = null;
             $reporter->reporter_last = null;
             $reporter->reporter_email = null;
             $reporter->reporter_phone = null;
             $reporter->reporter_ip = null;
             $reporter->reporter_date = date('Y-m-d');
             $reporter->save();
         }
         if ($reporter->level_id > 1 && count(ORM::factory("message")->where("service_messageid = '" . $tweet->{'id_str'} . "'")->find_all()) == 0) {
             // Grab geo data if it exists from the tweet
             $tweet_lat = null;
             $tweet_lon = null;
             if ($tweet->{'geo'} != null) {
                 $tweet_lat = $tweet->{'geo'}->coordinates[0];
                 $tweet_lon = $tweet->{'geo'}->coordinates[1];
             }
             // Save Tweet as Message
             $message = new Message_Model();
             $message->parent_id = 0;
             $message->incident_id = 0;
             $message->user_id = 0;
             $message->reporter_id = $reporter->id;
             $message->message_from = $tweet->{'from_user'};
             $message->message_to = null;
             $message->message = $tweet->{'text'};
             $message->message_type = 1;
             // Inbox
             $tweet_date = date("Y-m-d H:i:s", strtotime($tweet->{'created_at'}));
             $message->message_date = $tweet_date;
             $message->service_messageid = $tweet->{'id_str'};
             $message->latitude = $tweet_lat;
             $message->longitude = $tweet_lon;
             $message->save();
             // Action::message_twitter_add - Twitter Message Received!
             Event::run('ushahidi_action.message_twitter_add', $message);
             // Auto-Create A Report if Reporter is Trusted
             $reporter_weight = $reporter->level->level_weight;
             $reporter_location = $reporter->location;
             if ($reporter_weight > 0 and $reporter_location) {
                 $incident_title = text::limit_chars($message->message, 50, "...", false);
                 // Create Incident
                 $incident = new Incident_Model();
                 $incident->location_id = $reporter_location->id;
                 $incident->incident_title = $incident_title;
                 $incident->incident_description = $message->message;
                 $incident->incident_date = $tweet_date;
                 $incident->incident_dateadd = date("Y-m-d H:i:s", time());
                 $incident->incident_active = 1;
                 if ($reporter_weight == 2) {
                     $incident->incident_verified = 1;
                 }
                 $incident->save();
                 // Update Message with Incident ID
                 $message->incident_id = $incident->id;
                 $message->save();
                 // Save Incident Category
                 $trusted_categories = ORM::factory("category")->where("category_trusted", 1)->find();
                 if ($trusted_categories->loaded) {
                     $incident_category = new Incident_Category_Model();
                     $incident_category->incident_id = $incident->id;
                     $incident_category->category_id = $trusted_categories->id;
                     $incident_category->save();
                 }
             }
         }
     }
     $this->_unlock();
     return true;
 }
Exemplo n.º 7
0
 /**
  * Displays all reports.
  */
 public function index()
 {
     // Cacheable Controller
     $this->is_cachable = TRUE;
     $this->template->header->this_page = 'reports';
     $this->template->content = new View('reports');
     $this->themes->js = new View('reports_js');
     // Store any exisitng URL parameters
     $this->themes->js->url_params = json_encode($_GET);
     // Enable the map
     $this->themes->map_enabled = TRUE;
     // Set the latitude and longitude
     $this->themes->js->latitude = Kohana::config('settings.default_lat');
     $this->themes->js->longitude = Kohana::config('settings.default_lon');
     $this->themes->js->default_map = Kohana::config('settings.default_map');
     $this->themes->js->default_zoom = Kohana::config('settings.default_zoom');
     // Load the alert radius view
     $alert_radius_view = new View('alert_radius_view');
     $alert_radius_view->show_usage_info = FALSE;
     $alert_radius_view->enable_find_location = FALSE;
     $alert_radius_view->css_class = "rb_location-radius";
     $this->template->content->alert_radius_view = $alert_radius_view;
     // Get locale
     $l = Kohana::config('locale.language.0');
     // Get the report listing view
     $report_listing_view = $this->_get_report_listing_view($l);
     // Set the view
     $this->template->content->report_listing_view = $report_listing_view;
     // Load the category
     $category_id = (isset($_GET['c']) and intval($_GET['c']) > 0) ? intval($_GET['c']) : 0;
     $category = ORM::factory('category', $category_id);
     if ($category->loaded) {
         $translated_title = Category_Lang_Model::category_title($category_id, $l);
         // Set the category title
         $this->template->content->category_title = $translated_title ? $translated_title : $category->category_title;
     } else {
         $this->template->content->category_title = "";
     }
     // Collect report stats
     $this->template->content->report_stats = new View('reports_stats');
     // Total Reports
     $total_reports = Incident_Model::get_total_reports(TRUE);
     // Get the date of the oldest report
     $oldest_timestamp = Incident_Model::get_oldest_report_timestamp();
     // Get the date of the latest report
     $latest_timestamp = Incident_Model::get_latest_report_timestamp();
     // Round the number of days up to the nearest full day
     $days_since = ceil((time() - $oldest_timestamp) / 86400);
     $avg_reports_per_day = $days_since < 1 ? $total_reports : round($total_reports / $days_since, 2);
     // Percent Verified
     $total_verified = Incident_Model::get_total_reports_by_verified(TRUE);
     $percent_verified = $total_reports == 0 ? '-' : round($total_verified / $total_reports * 100, 2) . '%';
     // Category tree view
     $this->template->content->category_tree_view = category::get_category_tree_view();
     // Additional view content
     $this->template->content->oldest_timestamp = $oldest_timestamp;
     $this->template->content->latest_timestamp = $latest_timestamp;
     $this->template->content->report_stats->total_reports = $total_reports;
     $this->template->content->report_stats->avg_reports_per_day = $avg_reports_per_day;
     $this->template->content->report_stats->percent_verified = $percent_verified;
     $this->template->content->services = Service_Model::get_array();
     $this->template->header->header_block = $this->themes->header_block();
 }
Exemplo n.º 8
0
 /**
  * Adds hash tweets in JSON format to the database and saves the sender as a new
  * Reporter if they don't already exist
  * @param string $data - Twitter JSON results
  */
 private function add_hash_tweets($data)
 {
     $services = new Service_Model();
     $service = $services->where('service_name', 'Twitter')->find();
     if (!$service) {
         return;
     }
     // HACK: Make twitter IDs be strings so 32bit php doesn't choke on them - Nigel McNie
     $data = preg_replace('/"id":(\\d+)/', '"id":"$1"', $data);
     $tweets = json_decode($data, false);
     if (!$tweets) {
         return;
     }
     if (isset($tweets->{'error'})) {
         return;
     }
     $tweet_results = $tweets->{'results'};
     foreach ($tweet_results as $tweet) {
         // Skip over duplicate tweets
         $tweet_hash = $this->tweet_hash($tweet->text);
         if ($this->is_tweet_registered($tweet_hash)) {
             continue;
         }
         $reporter = ORM::factory('reporter')->where('service_id', $service->id)->where('service_account', $tweet->{'from_user'})->find();
         if (!$reporter->loaded) {
             // get default reporter level (Untrusted)
             $level = ORM::factory('level')->where('level_weight', 0)->find();
             $reporter->service_id = $service->id;
             $reporter->level_id = $level->id;
             $reporter->service_userid = null;
             $reporter->service_account = $tweet->{'from_user'};
             $reporter->reporter_first = null;
             $reporter->reporter_last = null;
             $reporter->reporter_email = null;
             $reporter->reporter_phone = null;
             $reporter->reporter_ip = null;
             $reporter->reporter_date = date('Y-m-d');
             $reporter->save();
         }
         if ($reporter->level_id > 1 && count(ORM::factory('message')->where('service_messageid', $tweet->{'id'})->find_all()) == 0) {
             // Save Tweet as Message
             $message = new Message_Model();
             $message->parent_id = 0;
             $message->incident_id = 0;
             $message->user_id = 0;
             $message->reporter_id = $reporter->id;
             $message->message_from = $tweet->{'from_user'};
             $message->message_to = null;
             $message->message = $tweet->{'text'};
             $message->message_type = 1;
             // Inbox
             $tweet_date = date("Y-m-d H:i:s", strtotime($tweet->{'created_at'}));
             $message->message_date = $tweet_date;
             $message->service_messageid = $tweet->{'id'};
             $message->save();
             // Mark this tweet as received for the duplicate checker
             $this->register_tweet($message->id, $tweet_hash);
             // Action::message_twitter_add - Twitter Message Received!
             Event::run('ushahidi_action.message_twitter_add', $message);
         }
         // Auto-Create A Report if Reporter is Trusted
         $reporter_weight = $reporter->level->level_weight;
         $reporter_location = $reporter->location;
         if ($reporter_weight > 0 and $reporter_location) {
             $incident_title = text::limit_chars($message->message, 50, "...", false);
             // Create Incident
             $incident = new Incident_Model();
             $incident->location_id = $reporter_location->id;
             $incident->incident_title = $incident_title;
             $incident->incident_description = $message->message;
             $incident->incident_date = $tweet_date;
             $incident->incident_dateadd = date("Y-m-d H:i:s", time());
             $incident->incident_active = 1;
             if ($reporter_weight == 2) {
                 $incident->incident_verified = 1;
             }
             $incident->save();
             // Update Message with Incident ID
             $message->incident_id = $incident->id;
             $message->save();
             // Save Incident Category
             $trusted_categories = ORM::factory("category")->where("category_trusted", 1)->find();
             if ($trusted_categories->loaded) {
                 $incident_category = new Incident_Category_Model();
                 $incident_category->incident_id = $incident->id;
                 $incident_category->category_id = $trusted_categories->id;
                 $incident_category->save();
             }
         }
     }
 }
Exemplo n.º 9
0
 function reporters()
 {
     $this->template->content = new View('admin/reporters');
     $this->template->content->title = 'Reporters';
     // setup and initialize form field names
     $form = array('reporter_id' => '', 'service_id' => '', 'service_userid' => '', 'service_account' => '', 'reporter_level' => '', 'reporter_first' => '', 'reporter_last' => '', 'reporter_email' => '', 'reporter_phone' => '', 'reporter_ip' => '', 'reporter_date' => '');
     //  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;
     $form_action = "";
     // 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 = Validation::factory($_POST);
         //  Add some filters
         $post->pre_filter('trim', TRUE);
         if ($post->action == 'a') {
             // Add some rules, the input field, followed by a list of checks, carried out in order
             $post->add_rules('service_id', 'required');
             // we also require either service_userid or service_account, not necessarily both
         }
         // Test to see if things passed the rule checks
         if ($post->validate()) {
             $reporter_id = $post->reporter_id;
             $reporter = new Reporter_Model($reporter_id);
             if ($post->action == 'd') {
                 $level->delete($level_id);
                 $form_saved = TRUE;
                 $form_action = "DELETED";
             } else {
                 if ($post->action == 'a') {
                     // SAVE Reporter
                     $reporter->service_id = $post->service_id;
                     /*$reporter->service_userid = $post->service_userid;
                     		$reporter->service_account = $post->service_account;*/
                     $reporter->reporter_level = $post->reporter_level;
                     /*$reporter->reporter_first = $post->reporter_first;
                     		$reporter->reporter_last = $post->reporter_last;
                     		$reporter->reporter_email = $post->reporter_email;
                     		$reporter->reporter_phone = $post->reporter_phone;
                     		$reporter->reporter_ip = $post->reporter_ip;
                     		$reporter->reporter_date = $post->reporter_date;*/
                     $reporter->save();
                     $form_saved = TRUE;
                     $form_action = "ADDED/EDITED";
                 }
             }
         } else {
             // repopulate the form fields
             $form = arr::overwrite($form, $post->as_array());
             // populate the error fields, if any
             $errors = arr::overwrite($errors, $post->errors('level'));
             $form_error = TRUE;
         }
     }
     // Pagination
     $pagination = new Pagination(array('query_string' => 'page', 'items_per_page' => (int) Kohana::config('settings.items_per_page_admin'), 'total_items' => ORM::factory('reporter')->count_all()));
     $reporters = ORM::factory('reporter')->orderby('reporter_first', 'asc')->find_all((int) Kohana::config('settings.items_per_page_admin'), $pagination->sql_offset);
     $this->template->content->form = $form;
     $this->template->content->errors = $errors;
     $this->template->content->form_error = $form_error;
     $this->template->content->form_saved = $form_saved;
     $this->template->content->form_action = $form_action;
     $this->template->content->pagination = $pagination;
     $this->template->content->total_items = $pagination->total_items;
     $this->template->content->reporters = $reporters;
     // Level and Service Arrays
     $this->template->content->level_array = Level_Model::get_array();
     $this->template->content->service_array = Service_Model::get_array();
 }
Exemplo n.º 10
0
 /**
  * ADD The SMS Message Using Default Provider
  * @param from mixed  The source/sender address
  * @param message mixed  The text content of the message
  * @param to mixed  Optional... 'which number the message was sent to'
  */
 public static function add($from = NULL, $message = NULL, $to = NULL)
 {
     $from = preg_replace("#[^0-9]#", "", $from);
     $to = preg_replace("#[^0-9]#", "", $to);
     if (!$from or !$message) {
         return "Missing Sender and/or Message";
     }
     //Filters to allow modification of the values from the SMS gateway
     Event::run('ushahidi_filter.message_sms_from', $from);
     Event::run('ushahidi_filter.message_sms', $message);
     $services = new Service_Model();
     $service = $services->where('service_name', 'SMS')->find();
     if (!$service) {
         return false;
     }
     $reporter = ORM::factory('reporter')->where('service_id', $service->id)->where('service_account', $from)->find();
     if (!$reporter->loaded == TRUE) {
         // get default reporter level (Untrusted)
         $level = ORM::factory('level')->where('level_weight', 0)->find();
         $reporter->service_id = $service->id;
         $reporter->level_id = $level->id;
         $reporter->service_userid = null;
         $reporter->service_account = $from;
         $reporter->reporter_first = null;
         $reporter->reporter_last = null;
         $reporter->reporter_email = null;
         $reporter->reporter_phone = null;
         $reporter->reporter_ip = null;
         $reporter->reporter_date = date('Y-m-d');
         $reporter->save();
     }
     // Save Message
     $sms = new Message_Model();
     $sms->parent_id = 0;
     $sms->incident_id = 0;
     $sms->user_id = 0;
     $sms->reporter_id = $reporter->id;
     $sms->message_from = $from;
     $sms->message_to = $to;
     $sms->message = $message;
     $sms->message_type = 1;
     // Inbox
     $sms->message_date = date("Y-m-d H:i:s", time());
     $sms->service_messageid = null;
     $sms->save();
     // Notify Admin Of New Email Message
     $send = notifications::notify_admins("[" . Kohana::config('settings.site_name') . "] " . Kohana::lang('notifications.admin_new_sms.subject'), Kohana::lang('notifications.admin_new_sms.message'));
     // Action::message_sms_add - SMS Received!
     Event::run('ushahidi_action.message_sms_add', $sms);
     // Auto-Create A Report if Reporter is Trusted
     $reporter_weight = $reporter->level->level_weight;
     $reporter_location = $reporter->location;
     if ($reporter_weight > 0 and $reporter_location) {
         $incident_title = text::limit_chars($message, 50, "...", false);
         // Create Incident
         $incident = new Incident_Model();
         $incident->location_id = $reporter_location->id;
         $incident->incident_title = $incident_title;
         $incident->incident_description = $message;
         $incident->incident_date = $sms->message_date;
         $incident->incident_dateadd = date("Y-m-d H:i:s", time());
         $incident->incident_active = 1;
         if ($reporter_weight == 2) {
             $incident->incident_verified = 1;
         }
         $incident->save();
         // Update Message with Incident ID
         $sms->incident_id = $incident->id;
         $sms->save();
         // Save Incident Category
         $trusted_categories = ORM::factory("category")->where("category_trusted", 1)->find();
         if ($trusted_categories->loaded) {
             $incident_category = new Incident_Category_Model();
             $incident_category->incident_id = $incident->id;
             $incident_category->category_id = $trusted_categories->id;
             $incident_category->save();
         }
     }
     // Add clickable report back feature.
     // Change delimiter to whatever is needed for sending the text aka #
     $delimiter = "#";
     $token = strtok($message, $delimiter);
     $i = 0;
     while ($token !== false) {
         $str[$i] = $token;
         $token = strtok($delimiter);
         $i++;
     }
     // Redirection for mysql server
     $php_db = "ranjoat_Ushahidi_Web";
     $myphp_db = 'ranjoat_Ushahidi_Web';
     // Change these variables to the working database
     $addr = "127.0.0.1";
     $login = "******";
     $passwd = "0258";
     if (strstr($str[0], "{$delimiter}.stop")) {
         // connect to database and find/match sms number in list of sms alerts numbers
         $db = mysql_connect($addr, $login, $passwd);
         if (!$db) {
             die('Could not connect: ' . mysql_error());
         }
         mysql_select_db($php_db, $db);
         //when matched begin process to remove that number from the table
         mysql_query("DELETE FROM {$myphp_db}.`alert` WHERE `alert`.`alert_recipient` = `{$from}`");
         mysql_close($db);
     } else {
         if (strstr($str[0], "{$delimiter}.report")) {
             if ($i == 1 && $str[0] !== false) {
                 // When the user does not how to use the #report function
                 // Add clickable report back feature.
                 if (strstr($str[0], "{$delimiter}.report")) {
                     $message = "Format for #report is: #report/# where the # is the incident id or #report/#location/keyword where location is the city and keyword used in the search.";
                     // Edit the parameters in sms::send to work with main deployment
                     //sms::send($to, $from, $message);
                     sms::send($to, $from, $message);
                 }
             }
         }
     }
     // For matching specific cases where the user knows the report ID
     if ($i == 2 && $str[1] !== false) {
         $db = mysql_connect($addr, $login, $passwd);
         if (!$db) {
             die('Could not connect: ' . mysql_error());
         }
         mysql_select_db($php_db, $db);
         $new = $str[1];
         $result = mysql_query("SELECT `incident`.`id`, `incident`.`incident_description` FROM `incident` WHERE `incident`.`id` = {$str['1']}");
         $message = mysql_fetch_row($result);
         sms::send($to, $from, $message[1]);
         mysql_free_result($result);
         mysql_close($db);
     }
     return TRUE;
 }
Exemplo n.º 11
0
 /**
  * Adds tweets in JSON format to the database and saves the sender as a new
  * Reporter if they don't already exist unless the message is a TWitter Search result
  * @param string $data - Twitter JSON results
  */
 private function add_json_tweets($data)
 {
     $services = new Service_Model();
     $service = $services->where('service_name', 'Twitter')->find();
     if (!$service) {
         return false;
     }
     $tweets = json_decode($data, false);
     if (!$tweets) {
         return false;
     }
     if (array_key_exists('results', $tweets)) {
         $tweets = $tweets->{'results'};
     }
     foreach ($tweets as $tweet) {
         $tweet_user = null;
         if (array_key_exists('user', $tweet)) {
             $tweet_user = $tweet->{'user'};
         }
         //XXX For Twitter Search, should we curl Twitter for a full tweet?
         $reporter = null;
         if ($tweet_user) {
             $reporter_model = new Reporter_Model();
             $reporters = $reporter_model->where('service_id', $service->id)->where('service_userid', $tweet_user->{'id'})->find_all();
             if (count($reporters) < 1) {
                 // Add new reporter
                 $names = explode(' ', $tweet_user->{'name'}, 2);
                 $last_name = '';
                 if (count($names) == 2) {
                     $last_name = $names[1];
                 }
                 // get default reporter level (Untrusted)
                 $levels = new Level_Model();
                 $default_level = $levels->where('level_weight', 0)->find();
                 $reporter = new Reporter_Model();
                 $reporter->service_id = $service->id;
                 $reporter->service_userid = $tweet_user->{'id'};
                 $reporter->service_account = $tweet_user->{'screen_name'};
                 $reporter->reporter_level = $default_level;
                 $reporter->reporter_first = $names[0];
                 $reporter->reporter_last = $last_name;
                 $reporter->reporter_email = null;
                 $reporter->reporter_phone = null;
                 $reporter->reporter_ip = null;
                 $reporter->reporter_date = date('Y-m-d');
                 $reporter->save();
             } else {
                 // reporter already exists
                 $reporter = $reporters[0];
             }
         }
         if (count(ORM::factory('message')->where('service_messageid', $tweet->{'id'})->find_all()) == 0) {
             // Save Tweet as Message
             $message = new Message_Model();
             $message->parent_id = 0;
             $message->incident_id = 0;
             $message->user_id = 0;
             if ($reporter) {
                 $message->reporter_id = $reporter->id;
             } else {
                 $message->reporter_id = 0;
             }
             if ($tweet_user) {
                 $message->message_from = $tweet_user->{'screen_name'};
             } elseif (array_key_exists('from_user', $tweet)) {
                 // Twitter Search tweets
                 $message->message_from = $tweet->{'from_user'};
             }
             $message->message_to = null;
             $message->message = $tweet->{'text'};
             $message->message_detail = null;
             $message->message_type = 1;
             // Inbox
             $tweet_date = date("Y-m-d H:i:s", strtotime($tweet->{'created_at'}));
             $message->message_date = $tweet_date;
             $message->service_messageid = $tweet->{'id'};
             $message->save();
         }
     }
     return true;
 }
Exemplo n.º 12
0
 /**
  * Displays all reports.
  */
 public function index()
 {
     // Cacheable Controller
     $this->is_cachable = TRUE;
     $this->template->header->this_page = 'reports';
     $this->template->content = new View('reports/main');
     $this->themes->js = new View('reports/reports_js');
     $this->template->header->page_title .= Kohana::lang('ui_main.reports') . Kohana::config('settings.title_delimiter');
     // Store any exisitng URL parameters
     $this->themes->js->url_params = json_encode($_GET);
     // Enable the map
     $this->themes->map_enabled = TRUE;
     // Set the latitude and longitude
     $this->themes->js->latitude = Kohana::config('settings.default_lat');
     $this->themes->js->longitude = Kohana::config('settings.default_lon');
     $this->themes->js->default_map = Kohana::config('settings.default_map');
     $this->themes->js->default_zoom = Kohana::config('settings.default_zoom');
     // Get Default Color
     $this->themes->js->default_map_all = $this->template->content->default_map_all = Kohana::config('settings.default_map_all');
     // Get default icon
     $this->themes->js->default_map_all_icon = $this->template->content->default_map_all_icon = '';
     if (Kohana::config('settings.default_map_all_icon_id')) {
         $icon_object = ORM::factory('media')->find(Kohana::config('settings.default_map_all_icon_id'));
         $this->themes->js->default_map_all_icon = $this->template->content->default_map_all_icon = Kohana::config('upload.relative_directory') . "/" . $icon_object->media_thumb;
     }
     // Load the alert radius view
     $alert_radius_view = new View('alerts/radius');
     $alert_radius_view->show_usage_info = FALSE;
     $alert_radius_view->enable_find_location = TRUE;
     $alert_radius_view->css_class = "rb_location-radius";
     $this->template->content->alert_radius_view = $alert_radius_view;
     // Get locale
     $l = Kohana::config('locale.language.0');
     // Get the report listing view
     $report_listing_view = $this->_get_report_listing_view($l);
     // Set the view
     $this->template->content->report_listing_view = $report_listing_view;
     // Load the category
     $category_id = (isset($_GET['c']) and intval($_GET['c']) > 0) ? intval($_GET['c']) : 0;
     $category = ORM::factory('category', $category_id);
     if ($category->loaded) {
         // Set the category title
         $this->template->content->category_title = Category_Lang_Model::category_title($category_id, $l);
     } else {
         $this->template->content->category_title = "";
     }
     // Collect report stats
     $this->template->content->report_stats = new View('reports/stats');
     // Total Reports
     $total_reports = Incident_Model::get_total_reports(TRUE);
     // Get the date of the oldest report
     if (isset($_GET['s']) and !empty($_GET['s']) and intval($_GET['s']) > 0) {
         $oldest_timestamp = intval($_GET['s']);
     } else {
         $oldest_timestamp = Incident_Model::get_oldest_report_timestamp();
     }
     // Get the date of the latest report
     if (isset($_GET['e']) and !empty($_GET['e']) and intval($_GET['e']) > 0) {
         $latest_timestamp = intval($_GET['e']);
     } else {
         $latest_timestamp = Incident_Model::get_latest_report_timestamp();
     }
     // Round the number of days up to the nearest full day
     $days_since = ceil((time() - $oldest_timestamp) / 86400);
     $avg_reports_per_day = $days_since < 1 ? $total_reports : round($total_reports / $days_since, 2);
     // Percent Verified
     $total_verified = Incident_Model::get_total_reports_by_verified(TRUE);
     $percent_verified = $total_reports == 0 ? '-' : round($total_verified / $total_reports * 100, 2) . '%';
     // Category tree view
     $this->template->content->category_tree_view = category::get_category_tree_view();
     // Additional view content
     $this->template->content->custom_forms_filter = new View('reports/submit_custom_forms');
     $this->template->content->custom_forms_filter->disp_custom_fields = customforms::get_custom_form_fields();
     $this->template->content->custom_forms_filter->search_form = TRUE;
     $this->template->content->oldest_timestamp = $oldest_timestamp;
     $this->template->content->latest_timestamp = $latest_timestamp;
     $this->template->content->report_stats->total_reports = $total_reports;
     $this->template->content->report_stats->avg_reports_per_day = $avg_reports_per_day;
     $this->template->content->report_stats->percent_verified = $percent_verified;
     $this->template->content->services = Service_Model::get_array();
 }
Exemplo n.º 13
0
 /**
  * Adds hash tweets in JSON format to the database and saves the sender as a new
  * Reporter if they don't already exist
  * @param string $data - Twitter JSON results
  */
 private function add_hash_tweets($data)
 {
     $services = new Service_Model();
     $service = $services->where('service_name', 'Twitter')->find();
     if (!$service) {
         return;
     }
     $tweets = json_decode($data, false);
     if (!$tweets) {
         return;
     }
     if (isset($tweets->{'error'})) {
         return;
     }
     $tweet_results = $tweets->{'results'};
     foreach ($tweet_results as $tweet) {
         // continue if tweet is RT
         if (preg_match('/^(.+?) *(R|Q)T( |:)*(@[a-zA-Z0-9_]+)( |:)/u', $tweet->{'text'}) === 1) {
             continue;
         }
         $reporter = ORM::factory('reporter')->where('service_id', $service->id)->where('service_account', $tweet->{'from_user'})->find();
         if (!$reporter->loaded) {
             // get default reporter level (Untrusted)
             $level = ORM::factory('level')->where('level_weight', 0)->find();
             $reporter->service_id = $service->id;
             $reporter->level_id = $level->id;
             $reporter->service_userid = null;
             $reporter->service_account = $tweet->{'from_user'};
             $reporter->reporter_first = null;
             $reporter->reporter_last = null;
             $reporter->reporter_email = null;
             $reporter->reporter_phone = null;
             $reporter->reporter_ip = null;
             $reporter->reporter_date = date('Y-m-d');
             $reporter->save();
         }
         if ($reporter->level_id > 1 && count(ORM::factory('message')->where('service_messageid', $tweet->{'id'})->find_all()) == 0) {
             // Save Tweet as Message
             $message = new Message_Model();
             $message->parent_id = 0;
             $message->incident_id = 0;
             $message->user_id = 0;
             $message->reporter_id = $reporter->id;
             $message->message_from = $tweet->{'from_user'};
             $message->message_to = null;
             $message->message = $tweet->{'text'};
             $message->message_type = 1;
             // Inbox
             $tweet_date = date("Y-m-d H:i:s", strtotime($tweet->{'created_at'}));
             $message->message_date = $tweet_date;
             $message->service_messageid = $tweet->{'id'};
             $message->save();
             // Action::message_twitter_add - Twitter Message Received!
             Event::run('ushahidi_action.message_twitter_add', $message);
         }
         // Auto-Create A Report if Reporter is Trusted
         $reporter_weight = $reporter->level->level_weight;
         $reporter_location = $reporter->location;
         if ($reporter_weight > 0 and $reporter_location) {
             $incident_title = text::limit_chars($message->message, 50, "...", false);
             // Create Incident
             $incident = new Incident_Model();
             $incident->location_id = $reporter_location->id;
             $incident->incident_title = $incident_title;
             $incident->incident_description = $message->message;
             $incident->incident_date = $tweet_date;
             $incident->incident_dateadd = date("Y-m-d H:i:s", time());
             $incident->incident_active = 1;
             if ($reporter_weight == 2) {
                 $incident->incident_verified = 1;
             }
             $incident->save();
             // Update Message with Incident ID
             $message->incident_id = $incident->id;
             $message->save();
             // Save Incident Category
             $trusted_categories = ORM::factory("category")->where("category_trusted", 1)->find();
             if ($trusted_categories->loaded) {
                 $incident_category = new Incident_Category_Model();
                 $incident_category->incident_id = $incident->id;
                 $incident_category->category_id = $trusted_categories->id;
                 $incident_category->save();
             }
         }
     }
 }
Exemplo n.º 14
0
 function index()
 {
     if (isset($_GET['key'])) {
         $frontlinesms_key = $_GET['key'];
     }
     if (isset($_GET['s'])) {
         $message_from = $_GET['s'];
         // Remove non-numeric characters from string
         $message_from = ereg_replace("[^0-9]", "", $message_from);
     }
     if (isset($_GET['m'])) {
         $message_description = $_GET['m'];
     }
     if (!empty($frontlinesms_key) && !empty($message_from) && !empty($message_description)) {
         // Is this a valid FrontlineSMS Key?
         $keycheck = ORM::factory('settings', 1)->where('frontlinesms_key', $frontlinesms_key)->find();
         if ($keycheck->loaded == true) {
             $services = new Service_Model();
             $service = $services->where('service_name', 'SMS')->find();
             if (!$service) {
                 return;
             }
             $reporter_check = ORM::factory('reporter')->where('service_id', $service->id)->where('service_account', $message_from)->find();
             if ($reporter_check->loaded == true) {
                 $reporter_id = $reporter_check->id;
             } else {
                 // get default reporter level (Untrusted)
                 $levels = new Level_Model();
                 $default_level = $levels->where('level_weight', 0)->find();
                 $reporter = new Reporter_Model();
                 $reporter->service_id = $service->id;
                 $reporter->service_userid = null;
                 $reporter->service_account = $message_from;
                 $reporter->reporter_level = $default_level;
                 $reporter->reporter_first = null;
                 $reporter->reporter_last = null;
                 $reporter->reporter_email = null;
                 $reporter->reporter_phone = null;
                 $reporter->reporter_ip = null;
                 $reporter->reporter_date = date('Y-m-d');
                 $reporter->save();
                 $reporter_id = $reporter->id;
             }
             $rest = $this->getsms($message_description);
             if ($rest['incidencia'] != '') {
                 if ($coord = $this->gettextaddress($rest['localidad'] . ', mexico')) {
                     // STEP 1: SAVE LOCATION
                     $location = new Location_Model('');
                     $location->location_name = $coord['textaddress'];
                     $location->country_id = 157;
                     //Mexico
                     $location->latitude = $coord['latitude'];
                     $location->longitude = $coord['longitude'];
                     $location->location_date = date("Y-m-d H:i:s", time());
                     $location->save();
                     $locationid = $location->id;
                 } else {
                     $locationid = 0;
                 }
                 // STEP 2: SAVE INCIDENT
                 $incident = new Incident_Model('');
                 $incident->location_id = $locationid;
                 //$incident->locale = $post->locale;
                 $incident->form_id = 1;
                 $incident->user_id = 0;
                 $incident->incident_title = $rest['mensaje'];
                 $incident->incident_description = $rest['mensaje'];
                 $incident->incident_date = date("Y-m-d H:i:s", time());
                 $incident->incident_dateadd = date("Y-m-d H:i:s", time());
                 $incident->incident_mode = 2;
                 // Incident Evaluation Info
                 $incident->incident_active = 0;
                 $incident->incident_verified = 0;
                 $incident->incident_source = null;
                 $incident->incident_information = null;
                 //Save
                 $incident->save();
                 $incidentid = $incident->id;
                 // STEP 3: SAVE CATEGORIES
                 $incident_category = new Incident_Category_Model();
                 $incident_category->incident_id = $incident->id;
                 $incident_category->category_id = $rest['incidencia'];
                 $incident_category->save();
             } else {
                 $incidentid = 0;
             }
             // Save Message
             $message = new Message_Model();
             $message->parent_id = 0;
             $message->incident_id = $incidentid;
             $message->user_id = 0;
             $message->reporter_id = $reporter_id;
             $message->message_from = $message_from;
             $message->message_to = null;
             $message->message = $message_description;
             $message->message_type = 1;
             // Inbox
             $message->message_date = date("Y-m-d H:i:s", time());
             $message->service_messageid = null;
             $message->save();
         }
     }
 }
Exemplo n.º 15
0
 /**
  * Send The SMS Message Using Default Provider
  * @param from mixed  The source/sender address
  * @param message mixed  The text content of the message
  * @param to mixed  Optional... 'which number the message was sent to'
  */
 public static function add($from = NULL, $message = NULL, $to = NULL)
 {
     $from = preg_replace("#[^0-9]#", "", $from);
     $to = preg_replace("#[^0-9]#", "", $to);
     if (!$from or !$message) {
         return "Missing Sender and/or Message";
     }
     //Filters to allow modification of the values from the SMS gateway
     Event::run('ushahidi_filter.message_sms_from', $from);
     Event::run('ushahidi_filter.message_sms', $message);
     $services = new Service_Model();
     $service = $services->where('service_name', 'SMS')->find();
     if (!$service) {
         return false;
     }
     $reporter = ORM::factory('reporter')->where('service_id', $service->id)->where('service_account', $from)->find();
     if (!$reporter->loaded == TRUE) {
         // get default reporter level (Untrusted)
         $level = ORM::factory('level')->where('level_weight', 0)->find();
         $reporter->service_id = $service->id;
         $reporter->level_id = $level->id;
         $reporter->service_userid = null;
         $reporter->service_account = $from;
         $reporter->reporter_first = null;
         $reporter->reporter_last = null;
         $reporter->reporter_email = null;
         $reporter->reporter_phone = null;
         $reporter->reporter_ip = null;
         $reporter->reporter_date = date('Y-m-d');
         $reporter->save();
     }
     // Save Message
     $sms = new Message_Model();
     $sms->parent_id = 0;
     $sms->incident_id = 0;
     $sms->user_id = 0;
     $sms->reporter_id = $reporter->id;
     $sms->message_from = $from;
     $sms->message_to = $to;
     $sms->message = $message;
     $sms->message_type = 1;
     // Inbox
     $sms->message_date = date("Y-m-d H:i:s", time());
     $sms->service_messageid = null;
     $sms->save();
     // Notify Admin Of New Email Message
     $send = notifications::notify_admins("[" . Kohana::config('settings.site_name') . "] " . Kohana::lang('notifications.admin_new_sms.subject'), Kohana::lang('notifications.admin_new_sms.message'));
     // Action::message_sms_add - SMS Received!
     Event::run('ushahidi_action.message_sms_add', $sms);
     // Auto-Create A Report if Reporter is Trusted
     $reporter_weight = $reporter->level->level_weight;
     $reporter_location = $reporter->location;
     if ($reporter_weight > 0 and $reporter_location) {
         $incident_title = text::limit_chars($message, 50, "...", false);
         // Create Incident
         $incident = new Incident_Model();
         $incident->location_id = $reporter_location->id;
         $incident->incident_title = $incident_title;
         $incident->incident_description = $message;
         $incident->incident_date = $sms->message_date;
         $incident->incident_dateadd = date("Y-m-d H:i:s", time());
         $incident->incident_active = 1;
         if ($reporter_weight == 2) {
             $incident->incident_verified = 1;
         }
         $incident->save();
         // Update Message with Incident ID
         $sms->incident_id = $incident->id;
         $sms->save();
         // Save Incident Category
         $trusted_categories = ORM::factory("category")->where("category_trusted", 1)->find();
         if ($trusted_categories->loaded) {
             $incident_category = new Incident_Category_Model();
             $incident_category->incident_id = $incident->id;
             $incident_category->category_id = $trusted_categories->id;
             $incident_category->save();
         }
     }
     return TRUE;
 }
Exemplo n.º 16
0
 public function index($service_id = 1)
 {
     $this->template->content = new View('admin/reporters');
     $this->template->content->title = Kohana::lang('ui_admin.reporters');
     $filter = "1=1";
     $search_type = "";
     $keyword = "";
     // Get Search Type (If Any)
     if ($service_id) {
         $search_type = $service_id;
         $filter .= " AND (service_id='" . $service_id . "')";
     } else {
         $search_type = "0";
     }
     // Get Search Keywords (If Any)
     if (isset($_GET['k']) and !empty($_GET['k'])) {
         $keyword = $_GET['k'];
         $filter .= " AND (service_account LIKE'%" . $_GET['k'] . "%')";
     }
     // setup and initialize form field names
     $form = array('reporter_id' => '', 'level_id' => '', 'service_name' => '', 'service_account' => '', 'location_id' => '', 'location_name' => '', 'latitude' => '', 'longitude' => '');
     //  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;
     $form_action = "";
     // 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 = Validation::factory($_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('action', 'required', 'alpha', 'length[1,1]');
         $post->add_rules('reporter_id.*', 'required', 'numeric');
         if ($post->action == 'l') {
             $post->add_rules('level_id', 'required', 'numeric');
         } elseif ($post->action == 'a') {
             $post->add_rules('level_id', 'required', 'numeric');
             // If any location data is provided, require all location parameters
             if ($post->latitude or $post->longitude or $post->location_name) {
                 $post->add_rules('latitude', 'required', 'between[-90,90]');
                 // Validate for maximum and minimum latitude values
                 $post->add_rules('longitude', 'required', 'between[-180,180]');
                 // Validate for maximum and minimum longitude values
                 $post->add_rules('location_name', 'required', 'length[3,200]');
             }
         }
         // Test to see if things passed the rule checks
         if ($post->validate()) {
             if ($post->action == 'd') {
                 foreach ($post->reporter_id as $item) {
                     // Delete Reporters Messages
                     ORM::factory('message')->where('reporter_id', $item)->delete_all();
                     // Delete Reporter
                     $reporter = ORM::factory('reporter')->find($item);
                     $reporter->delete($item);
                 }
                 $form_saved = TRUE;
                 $form_action = strtoupper(Kohana::lang('ui_admin.deleted'));
             } elseif ($post->action == 'l') {
                 foreach ($post->reporter_id as $item) {
                     // Update Reporter Level
                     $reporter = ORM::factory('reporter')->find($item);
                     if ($reporter->loaded) {
                         $reporter->level_id = $post->level_id;
                         $reporter->save();
                     }
                 }
                 $form_saved = TRUE;
                 $form_action = strtoupper(Kohana::lang('ui_admin.modified'));
             } else {
                 if ($post->action == 'a') {
                     foreach ($post->reporter_id as $item) {
                         $reporter = ORM::factory('reporter')->find($item);
                         // SAVE Reporter only if loaded
                         if ($reporter->loaded) {
                             $reporter->level_id = $post->level_id;
                             // SAVE Location if available
                             if ($post->latitude and $post->longitude) {
                                 $location = new Location_Model($post->location_id);
                                 $location->location_name = $post->location_name;
                                 $location->latitude = $post->latitude;
                                 $location->longitude = $post->longitude;
                                 $location->location_date = date("Y-m-d H:i:s", time());
                                 $location->save();
                                 $reporter->location_id = $location->id;
                             }
                             $reporter->save();
                             $form_saved = TRUE;
                             $form_action = strtoupper(Kohana::lang('ui_admin.modified'));
                         }
                     }
                 }
             }
         } else {
             // repopulate the form fields
             $form = arr::overwrite($form, $post->as_array());
             // populate the error fields, if any
             $errors = arr::overwrite($errors, $post->errors('reporters'));
             $form_error = TRUE;
         }
     }
     // Pagination
     $pagination = new Pagination(array('query_string' => 'page', 'items_per_page' => (int) Kohana::config('settings.items_per_page_admin'), 'total_items' => ORM::factory('reporter')->where($filter)->count_all()));
     $reporters = ORM::factory('reporter')->where($filter)->orderby('service_account', 'asc')->find_all((int) Kohana::config('settings.items_per_page_admin'), $pagination->sql_offset);
     $this->template->content->form = $form;
     $this->template->content->errors = $errors;
     $this->template->content->form_error = $form_error;
     $this->template->content->form_saved = $form_saved;
     $this->template->content->form_action = $form_action;
     $this->template->content->pagination = $pagination;
     $this->template->content->total_items = $pagination->total_items;
     $this->template->content->reporters = $reporters;
     $this->template->content->service_id = $service_id;
     $this->template->content->search_type = $search_type;
     $search_type_array = Service_Model::get_array();
     $search_type_array[0] = "All";
     asort($search_type_array);
     $this->template->content->search_type_array = $search_type_array;
     $this->template->content->keyword = $keyword;
     $levels = ORM::factory('level')->orderby('level_weight')->find_all();
     $this->template->content->levels = $levels;
     // Level and Service Arrays
     $this->template->content->level_array = Level_Model::get_array();
     $this->template->content->service_array = Service_Model::get_array();
     // Javascript Header
     $this->template->map_enabled = TRUE;
     $this->template->js = new View('admin/reporters_js');
     $this->template->js->default_map = Kohana::config('settings.default_map');
     $this->template->js->default_zoom = Kohana::config('settings.default_zoom');
     $this->template->js->latitude = Kohana::config('settings.default_lat');
     $this->template->js->longitude = Kohana::config('settings.default_lon');
     $this->template->js->form_error = $form_error;
 }
Exemplo n.º 17
0
 /**
  * Receive SMS's via FrontlineSMS or via Mobile Phone Native Apps
  *
  * @param array request - 
  * @param string response_type - XML or JSON
  */
 public function _sms($request, $response_type)
 {
     $reponse = array();
     // Validate User
     // Should either be authenticated or have app_key
     $username = isset($request['username']) ? $request['username'] : "";
     $password = isset($request['password']) ? $request['password'] : "";
     $app_key = isset($request['key']) ? $request['key'] : "";
     if ($user_id = $this->_login($username, $password) || $this->_chk_key($app_key)) {
         // Process POST
         // setup and initialize form field names
         $form = array('message_from' => '', 'message_description' => '', 'message_date' => '');
         /**
          * Instantiate Validation, 
          * use $post, so we don't overwrite $_POST fields with our 
          * own things
          */
         $post = Validation::factory($request);
         //  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('message_from', 'required', 'numeric', 'length[6,20]');
         $post->add_rules('message_description', 'required', 'length[3,300]');
         $post->add_rules('message_date', 'date_mmddyyyy');
         // Test to see if things passed the rule checks
         if ($post->validate()) {
             // Validates so Save Message
             $services = new Service_Model();
             $service = $services->where('service_name', 'SMS')->find();
             if (!$service) {
                 return;
             }
             $reporter = ORM::factory('reporter')->where('service_id', $service->id)->where('service_account', $post->message_from)->find();
             if (!$reporter->loaded == TRUE) {
                 // get default reporter level (Untrusted)
                 $level = ORM::factory('level')->where('level_weight', 0)->find();
                 $reporter->service_id = $service->id;
                 $reporter->level_id = $level->id;
                 $reporter->service_userid = null;
                 $reporter->service_account = $post->message_from;
                 $reporter->reporter_first = null;
                 $reporter->reporter_last = null;
                 $reporter->reporter_email = null;
                 $reporter->reporter_phone = null;
                 $reporter->reporter_ip = null;
                 $reporter->reporter_date = date('Y-m-d');
                 $reporter->save();
             }
             // Save Message
             $message = new Message_Model();
             $message->parent_id = 0;
             $message->incident_id = 0;
             $message->user_id = $user_id ? $user_id : 0;
             $message->reporter_id = $reporter->id;
             $message->message_from = $post->message_from;
             $message->message_to = null;
             $message->message = $post->message_description;
             $message->message_type = 1;
             // Inbox
             $message->message_date = isset($post->message_date) && !empty($post->message_date) ? $post->message_date : date("Y-m-d H:i:s", time());
             $message->service_messageid = null;
             $message->save();
             // success!
             $reponse = array("payload" => array("domain" => $this->domain, "success" => "true"), "error" => $this->api_actions->_get_error_msg(0));
         } else {
             // Required parameters are missing or invalid
             $reponse = array("payload" => array("domain" => $this->domain, "success" => "false"), "error" => $this->api_actions->_get_error_msg(02));
         }
     } else {
         // Authentication Failed. Invalid User or App Key
         $reponse = array("payload" => array("domain" => $this->domain, "success" => "false"), "error" => $this->api_actions->_get_error_msg(05));
     }
     if ($response_type == 'json') {
         $this->ret_json_or_xml = $this->api_actions->_array_as_JSON($reponse);
     } else {
         $this->ret_json_or_xml = $this->api_actions->_array_as_XML($reponse, array());
     }
     return $ret_json_or_xml;
 }
Exemplo n.º 18
0
 /**
  * Adds email to the database and saves the sender as a new
  * Reporter if they don't already exist
  * @param string $messages
  */
 private function add_email($messages)
 {
     $services = new Service_Model();
     $service = $services->where('service_name', 'Email')->find();
     if (!$service) {
         return;
     }
     if (empty($messages) || !is_array($messages)) {
         return;
     }
     foreach ($messages as $message) {
         $reporter = ORM::factory('reporter')->where('service_id', $service->id)->where('service_account', $message['email'])->find();
         if (!$reporter->loaded == true) {
             // Add new reporter
             $names = explode(' ', $message['from'], 2);
             $last_name = '';
             if (count($names) == 2) {
                 $last_name = $names[1];
             }
             // get default reporter level (Untrusted)
             $level = ORM::factory('level')->where('level_weight', 0)->find();
             $reporter->service_id = $service->id;
             $reporter->level_id = $level->id;
             $reporter->service_userid = null;
             $reporter->service_account = $message['email'];
             $reporter->reporter_first = $names[0];
             $reporter->reporter_last = $last_name;
             $reporter->reporter_email = $message['email'];
             $reporter->reporter_phone = null;
             $reporter->reporter_ip = null;
             $reporter->reporter_date = date('Y-m-d');
             $reporter->save();
         }
         if ($reporter->level_id > 1 && count(ORM::factory('message')->where('service_messageid', $message['message_id'])->find_all()) == 0) {
             // Save Email as Message
             $email = new Message_Model();
             $email->parent_id = 0;
             $email->incident_id = 0;
             $email->user_id = 0;
             $email->reporter_id = $reporter->id;
             $email->message_from = $message['from'];
             $email->message_to = null;
             $email->message = $message['subject'];
             $email->message_detail = $message['body'];
             $email->message_type = 1;
             // Inbox
             $email->message_date = $message['date'];
             $email->service_messageid = $message['message_id'];
             $email->save();
             // Attachments?
             foreach ($message['attachments'] as $attachment) {
                 foreach ($attachment as $key => $value) {
                     $media = new Media_Model();
                     $media->location_id = 0;
                     $media->incident_id = 0;
                     $media->message_id = $email->id;
                     $media->media_type = 1;
                     // Images
                     $media->media_link = $key;
                     $media->media_thumb = $value;
                     $media->media_date = date("Y-m-d H:i:s", time());
                     $media->save();
                 }
             }
             // Notify Admin Of New Email Message
             $send = notifications::notify_admins("[" . Kohana::config('settings.site_name') . "] " . Kohana::lang('notifications.admin_new_email.subject'), Kohana::lang('notifications.admin_new_email.message'));
         }
         // Auto-Create A Report if Reporter is Trusted
         $reporter_weight = $reporter->level->level_weight;
         $reporter_location = $reporter->location;
         if ($reporter_weight > 0 and $reporter_location) {
             // Create Incident
             $incident = new Incident_Model();
             $incident->location_id = $reporter_location->id;
             $incident->incident_title = $message['subject'];
             $incident->incident_description = $message['body'];
             $incident->incident_date = $message['date'];
             $incident->incident_dateadd = date("Y-m-d H:i:s", time());
             $incident->incident_active = 1;
             if ($reporter_weight == 2) {
                 $incident->incident_verified = 1;
             }
             $incident->save();
             // Update Message with Incident ID
             $email->incident_id = $incident->id;
             $email->save();
             // Save Incident Category
             $trusted_categories = ORM::factory("category")->where("category_trusted", 1)->find();
             if ($trusted_categories->loaded) {
                 $incident_category = new Incident_Category_Model();
                 $incident_category->incident_id = $incident->id;
                 $incident_category->category_id = $trusted_categories->id;
                 $incident_category->save();
             }
             // Add Attachments
             $attachments = ORM::factory("media")->where("message_id", $email->id)->find_all();
             foreach ($attachments as $attachment) {
                 $attachment->incident_id = $incident->id;
                 $attachment->save();
             }
         }
     }
 }