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(); } } }
/** * 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(); } } }
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')); } } }
/** * 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')); } } }
/** * 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(); } } }
/** * 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; }
/** * 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; }
/** * 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(); } } } }
/** * 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; }
/** * 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(); } } } }
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(); } } }
/** * 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; }
/** * 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; }
/** * 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(); } } } }