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(); } } }
/** * Delete existing SMS message * * @param string response_type - The response type. * * @return Array */ public function _del_email_msg($response_type) { if ($_POST) { $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('message_id.*', 'required', 'numeric'); if ($post->validate()) { $email_id = $post->twitter_id; $email = new Message_Model($email_id); if ($email->loaded == true) { $email->delete(); } else { //email id doesn't exist in DB //TODO i18nize the string $this->error_messages .= "Email ID does not exist."; $this->ret_value = 1; } } else { //TODO i18nize the string $this->error_messages .= "Email ID is required."; $this->ret_value = 1; } } else { $this->ret_value = 3; } return $this->api_actions->_response($this->ret_value, $response_type); }
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 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(); } } }
public function index() { $this->template->content = new View('task'); $this->template->title = 'SMS Task Queue'; $this->template->content->message = Message_Model::get_oldest_available_message(); // If there are no messages, pass "null" if (count($this->template->content->message) == 0) { $this->template->content->message = NULL; } }
public function test($do = NULL) { $this->template->content = new View('welcome'); $this->template->title = 'Welcome'; switch ($do) { case 'add_sms': Message_Model::add_message('1112223333', 'This is a test message ' . rand(1, 1000)); break; default: die('Do Required'); } Debug_Toolbar::render(TRUE); }
public function get_oldest_available_message($lock = 1) { // Get all active categories $messages = array(); foreach (ORM::factory('message')->where('status', '0')->orderby('received', 'ASC')->limit(1)->find_all() as $msg) { // Create a list of all categories $messages[$msg->id] = array('number' => $msg->number, 'sms' => $msg->sms, 'translation' => $msg->translation, 'notes' => $msg->notes, 'received' => $msg->received, 'updated' => $msg->updated); if ($lock == 1) { Message_Model::lock_message($msg->id); } } return $messages; }
public function index() { $this->template->content = new View('feed'); $this->template->title = 'Feed'; $limit = '0,20'; if (isset($_POST['limit'])) { $limit = $_POST['limit']; } $messages = Message_Model::get_translated_messages($limit); $info = array('notes' => 'This is an RSS feed of the translated SMS messages.'); $items = array(); foreach ($messages as $id => $msg) { $items[] = array('title' => string_manipulation::xmlentities($msg['sms']), 'link' => 'http://localhost:8888/SMSTurks/feed/' . $id, 'description' => string_manipulation::xmlentities($msg['translation']), 'author' => $msg['number'], 'pubDate' => date('D, d M Y H:i:s O', strtotime($msg['received']))); } $this->template->content->display = feed::create($info, $items); }
public function process_post($post) { $post = new Validation($post); $post->add_rules('form', 'required'); if ($post->validate()) { switch ($post->form) { case 'update_translation': $post->add_rules('id', 'numeric'); if ($post->validate()) { Message_Model::update_translation($post->id, $post->translation); } break; } } else { echo 'FAILED VALIDATION'; } }
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(); } } }
/** * 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) { $service = ORM::factory('service')->where('service_name', 'Email')->find(); if (!$service->loaded) { return; } if (empty($messages) or !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_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 $attachments) { foreach ($attachments as $attachment) { $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 = $attachment[0]; $media->media_medium = $attachment[1]; $media->media_thumb = $attachment[2]; $media->media_date = date("Y-m-d H:i:s", time()); $media->save(); } } // 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(); } } // 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')); // Action::message_email_add - Email Received! Event::run('ushahidi_action.message_email_add', $email); } } }
/** * 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_check = ORM::factory('reporter')->where('service_id', $service->id)->where('service_account', $tweet->{'from_user'})->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 = $tweet->{'from_user'}; $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; } 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; $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(); } } }
/** * 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(); } } } }
public function index() { $birth = 1263427200; // 2010-01-14 - We'll move in 3 hour increments from here $cache = Cache::instance(); $last_message_date = $cache->get('georss_parser'); if ($last_message_date == NULL) { $last_message_date = $birth; $cache->set('georss_parser', $birth, array('georss'), 0); } //echo $last_message_date; $settings = ORM::factory('settings', 1); $sms_rss = $settings->georss_feed . "&only_phone=1&limit=50," . $this->items; //."&uptots=".$last_message_date; $curl_handle = curl_init(); curl_setopt($curl_handle, CURLOPT_URL, $sms_rss); curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); // Timeout curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); // Set curl to store data in variable instead of print $buffer = curl_exec($curl_handle); curl_close($curl_handle); // Parse Feed URL using SimplePIE $feed_data = $this->_simplepie($buffer); if (count($feed_data->get_items(0, $this->items)) == 0) { $cache->set('georss_parser', $last_message_date + 3600, array('georss'), 0); //exit; } // Cycle through feed data $i = 0; foreach ($feed_data->get_items(0, $this->items) as $feed_data_item) { $service_messageid = $feed_data_item->get_item_tags('http://www.w3.org/2005/Atom', 'id'); $service_messageid = str_replace("http://4636.ushahidi.com/person.php?id=", "", trim($service_messageid[0]['data'])); $date = $feed_data_item->get_item_tags('http://www.w3.org/2005/Atom', 'updated'); $date = date("Y-m-d H:i:s", strtotime(trim($date[0]['data']))); $phone = $feed_data_item->get_item_tags('http://www.w3.org/2005/Atom', 'phone'); $phone = intval($phone[0]['data']); $category = $feed_data_item->get_item_tags('http://www.w3.org/2005/Atom', 'categorization'); $category = trim($category[0]['data']); $message_sms = $feed_data_item->get_item_tags('http://www.w3.org/2005/Atom', 'sms'); $message_sms = trim($message_sms[0]['data']); $message_notes = $feed_data_item->get_item_tags('http://www.w3.org/2005/Atom', 'notes'); $message_notes = trim($message_notes[0]['data']); $message_detail = $message_notes . "\n~~~~~~~~~~~~~~~~~\n"; $message_detail .= "Category: " . $category; $latitude = $feed_data_item->get_latitude(); $longitude = $feed_data_item->get_longitude(); $location_name = $feed_data_item->get_item_tags('http://www.w3.org/2005/Atom', 'city'); $location_name = trim($location_name[0]['data']); // Okay now we have everything we need // Step 1. Does this message have a phone number? if ($phone) { // Step 2. Has this particular message been saved before?? $exists = ORM::factory('message')->where('service_messageid', $service_messageid)->where('message_from', $phone)->find(); if (!$exists->loaded) { $parent_id = 0; // Step 3. Make sure this phone number is not in our database $reporter = ORM::factory('reporter')->where('service_id', 1)->where('service_account', $phone)->find(); if (!$reporter->loaded) { $reporter->service_id = 1; // 1 - SMS (See Service Table) $reporter->level_id = 3; // 3 - Untrusted (See Level Table) $reporter->service_account = $phone; $reporter->reporter_date = $date; $reporter->save(); } else { // Find previous message and use it as parent $parent = ORM::factory('message')->where('reporter_id', $reporter->id)->where('message_type', '1')->where('parent_id', '0')->where('message_trash', '0')->orderby('message_date', 'desc')->find(); if ($parent->loaded) { $parent_id = $parent->id; $parent->message_reply = 1; $parent->save($parent->id); } } // Step 4. If this message has a location, save it! $location_id = 0; if ($latitude && $longitude) { $location = ORM::factory('location')->where('latitude', $latitude)->where('longitude', $longitude)->find(); if (!$location->loaded) { $location = new Location_Model(); if ($location_name) { $location->location_name = $location_name; } else { $location->location_name = "Unknown"; } $location->latitude = $latitude; $location->longitude = $longitude; $location->location_date = date("Y-m-d H:i:s", time()); $location->save(); $location_id = $location->id; } } // Save Message $message = new Message_Model(); $message->parent_id = $parent_id; $message->incident_id = 0; $message->location_id = $location_id; $message->user_id = 0; $message->reporter_id = $reporter->id; $message->message_from = $phone; $message->message_to = null; $message->message = $message_sms; $message->message_detail = $message_detail; $message->message_type = 1; // Inbox $message->message_date = $date; $message->service_messageid = $service_messageid; $message->save(); $i++; } } } if ($i == 0) { $cache->set('georss_parser', $last_message_date + 3600, array('georss'), 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; }
/** * Create a report and assign it to one or more categories and set verification */ public function __response_create_report($vars) { $categories = array(); if (isset($vars['add_category'])) { $categories = $vars['add_category']; } $verify = 0; if (isset($vars['verify'])) { $verify = (int) $vars['verify']; } $approve = 0; if (isset($vars['approve'])) { $approve = (int) $vars['approve']; } // Grab the location_id or create one if we can $location_id = 0; if (isset($this->data->location_id)) { $location_id = $this->data->location_id; } elseif (isset($this->data->latitude) and isset($this->data->longitude)) { $location_name = map::reverse_geocode($this->data->latitude, $this->data->longitude); // In case our location name is too long, chop off the end $location_name = substr_replace($location_name, '', 250); $location_data = (object) array('location_name' => $location_name, 'latitude' => $this->data->latitude, 'longitude' => $this->data->longitude); $location = new Location_Model(); reports::save_location($location_data, $location); $location_id = $location->id; } // We can only create reports if we have location. if ($location_id == FALSE or $location_id == 0) { return false; } // Save Incident $incident = new Incident_Model(); $incident->location_id = $location_id; $incident->incident_title = $vars['report_title']; $incident->incident_description = $this->data->message; $incident->incident_date = $this->data->message_date; $incident->incident_active = $approve; $incident->incident_verified = $verify; $incident->incident_dateadd = date("Y-m-d H:i:s", time()); $incident->save(); // Conflicted.. do I run report add here? Potential to create a mess with action triggers? //Event::run('ushahidi_action.report_add', $incident); $incident_id = $incident->id; foreach ($categories as $category_id) { // Assign Category Incident_Category_Model::assign_category_to_incident($incident_id, $category_id); } // Link message with incident? if (isset($this->data->message) and isset($this->data->id)) { $message = new Message_Model($this->data->id); $message->incident_id = $incident_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) { 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; }
/** * 系统消息统计 * @return <type> */ public function counts() { $messageModel = new Message_Model(); $res = $messageModel->getNewNoticeNum($this->uid); //获取新通知条数 if ($res) { $noticeNum = (int) $res; } else { $noticeNum = 0; } $aboutmeNum = $this->feedModel->aboutMeNewCount(); // 取得新的消息的统计 $smsNum = User_Model::instance()->get_sms_count($this->uid); $this->send_response(200, array('message_count' => $noticeNum, 'aboutme_count' => $aboutmeNum, 'sms_count' => $smsNum)); }
/** * 报名活动 */ public function create($id = 0) { $data = $this->get_data(); $aid = (int) $id; $apply_type = (int) $data['type']; $webRequest = (int) $data['web']; if ($webRequest != 2) { $webRequest = 0; } if ($this->get_method() != 'POST') { $this->send_response(405, NULL, '请求的方法不存在'); } if ($id == 0) { $this->send_response(400, NULL, '400501:活动id为空'); } if ($apply_type < Kohana::config('activity.apply_type.join') || $apply_type > Kohana::config('activity.apply_type.interest')) { $this->send_response(400, NULL, '400508:活动报名类型非法'); } $activity = $this->model->getActivityInfo($aid); if (!$activity) { $this->send_response(400, NULL, '400502:活动不存在'); } if ($activity['end_time'] < time()) { $this->send_response(400, NULL, '活动已结束'); } $permit = $this->_check_activity_view_permission($activity, $this->user_id); if (!$permit) { $this->send_response(400, NULL, '400510:无权限报名活动'); } if ($webRequest > 0) { $types = array_flip(Kohana::config('activity.type')); $typeId = $types[$activity['type']]; $typeNames = Kohana::config('activity.typeName'); $type = $typeNames[$typeId]; $view = new View('activity/details'); $view->webRequest = $webRequest; $view->type = $type; $user = array(); $user['id'] = floatval($activity['creator_id']); $userInfo = sns::getuser($activity['creator_id']); $user['name'] = $userInfo['realname']; $user['mobile'] = $userInfo['mobile']; $activity['user'] = $user; unset($user); $organizer = array(); $organizerList = $this->model->getActivityOrganizer($aid); $organizerIdList = array(); foreach ($organizerList as $value) { $user = array(); $user['id'] = floatval($value['uid']); $userInfo = sns::getuser($value['uid']); $user['name'] = $userInfo['realname']; $user['avatar'] = sns::getavatar($value['uid']); $user['mobile'] = $userInfo['mobile']; $organizer[] = $user; $organizerIdList[] = $user['id']; unset($userInfo); unset($user); } $activity['organizer'] = $organizer; $view->activity = $activity; $view->apply_type = $apply_type; } $userModel = new User_Model(); $nowTime = time(); $feedModel = new Feed_Model(); //获取用户是否已经参与了报名 $applyResult = $this->model->getActivityApplyType($aid, $this->user_id); if ($applyResult) { $tab = $userModel->getTag($this->user_id, 15, $aid); if ($applyResult == $apply_type) { if ($webRequest > 0) { $view->render(true); exit; } $this->send_response(200); } else { if ($applyResult == Kohana::config('activity.apply_type.not_join') && !$tab) { $userModel->insertTag($this->user_id, 15, $aid); } else { if ($apply_type == Kohana::config('activity.apply_type.not_join')) { //$userModel->deleteTag($this->user_id, 15, $aid); } } } $activityMember = array('apply_type' => $apply_type, 'apply_time' => $nowTime); $grade = $this->model->getMemberGrade($aid, $this->user_id); if ($grade == Kohana::config('activity.grade.manager') && $apply_type != Kohana::config('activity.apply_type.join')) { $activityMember['grade'] = Kohana::config('activity.group.normal'); } $result = $this->model->updateApplyActivity($aid, $this->user_id, $activityMember); $this->_add_feed_comment($activity, $applyResult, $apply_type, $this->user_id); $feedModel->addTab($aid, $activity['title'], 15, $this->user_id); if ($webRequest > 0) { $view->render(true); exit; } $this->send_response(200); } $activityMember['aid'] = $aid; $activityMember['uid'] = $this->user_id; $activityMember['apply_type'] = $apply_type; $activityMember['apply_time'] = $nowTime; $activityMember['grade'] = Kohana::config('activity.grade.normal'); $this->model->applyActivity($activityMember); $userModel->insertTag($this->user_id, 15, $aid); $messageModel = new Message_Model(); $appid = Kohana::config('uap.app.action'); $tplid = Kohana::config('noticetpl.actionInvite.id'); while (true) { $inviteMsg = $messageModel->getNoticeInfo(array('uid' => $this->user_id, 'appid' => $appid, 'tplid' => $tplid, 'objid' => $aid, 'status' => 0)); if ($inviteMsg) { //修改系统消息模板 $invite_uid = $inviteMsg['authorid']; $nid = $inviteMsg['id']; $messageModel->putChangeTplByid($this->user_id, $nid, $apply_type); $this->model->setInviteStatus($aid, $invite_uid, $this->user_id); } else { break; } } if ($apply_type == Kohana::config('activity.apply_type.not_join')) { if ($webRequest > 0) { $view->render(true); exit; } $this->send_response(200); } $this->_add_feed_comment($activity, 0, $apply_type, $this->user_id); $feedModel->addTab($aid, $activity['title'], 15, $this->user_id); if ($webRequest > 0) { $view->render(true); exit; } $this->send_response(200); }
/** * 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; }
/** * Spam / Unspam existing email message * * @return Array */ public function _spam_twitter_msg() { $ret_val = 0; // Initialize a 0 return value; successful execution if ($_POST) { $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('message_id', 'required', 'numeric'); if ($post->validate()) { $twitter_id = $post->message_id; $twitter = new Message_Model($twitter_id); if ($twitter->loaded == true) { if ($twitter->message_level == '1') { $twitter->message_level = '99'; } else { $twitter->message_level = '1'; } $twitter->save(); } else { //twitter id doesn't exist in DB //TODO i18nize the string $this->error_messages .= "Twitter ID does not exist."; $this->ret_value = 1; } } else { //TODO i18nize the string $this->error_messages .= "Twitter ID is required."; $ret_value = 1; } } else { $ret_value = 3; } $this->response_data = $this->response($ret_value); }
/** * 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(); } } } }
/** * 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; }
/** * Create a report and assign it to one or more categories and set verification */ public function __response_create_report($vars) { $categories = array(); if (isset($vars['add_category'])) { $categories = $vars['add_category']; } $verify = 0; if (isset($vars['verify'])) { $verify = (int) $vars['verify']; } $approve = 0; if (isset($vars['approve'])) { $approve = (int) $vars['approve']; } // Grab the location_id or create one if we can $location_id = 0; if (isset($this->data->location_id)) { $location_id = $this->data->location_id; } elseif (isset($this->data->latitude) and isset($this->data->longitude)) { $location_name = map::reverse_geocode($this->data->latitude, $this->data->longitude); // In case our location name is too long, chop off the end $location_name = substr_replace($location_name, '', 250); $location_data = (object) array('location_name' => $location_name, 'latitude' => $this->data->latitude, 'longitude' => $this->data->longitude); $location = new Location_Model(); reports::save_location($location_data, $location); $location_id = $location->id; } // We can only create reports if we have location. if ($location_id == FALSE or $location_id == 0) { return false; } // Build title // Build title & description // If this is a message if (isset($this->data->message)) { $incident_title = $this->data->message; $incident_description = $this->data->message; $incident_date = $this->data->message_date; // If we're got more message detail, make that the description if (!empty($message->message_detail)) { $incident_description = $this->data->message_detail; } } elseif (isset($this->data->item_title)) { $incident_title = html::strip_tags(html_entity_decode(html_entity_decode($this->data->item_title, ENT_QUOTES))); $incident_description = html::clean(html_entity_decode($this->data->item_description, ENT_QUOTES)); $incident_date = $this->data->item_date; } // Override title from action options if (!empty($vars['report_title'])) { $incident_title = $vars['report_title']; } // Save Incident $incident = new Incident_Model(); $incident->location_id = $location_id; $incident->incident_title = $incident_title; $incident->incident_description = $incident_description; $incident->incident_date = $incident_date; $incident->incident_active = $approve; $incident->incident_verified = $verify; $incident->incident_dateadd = date("Y-m-d H:i:s", time()); $incident->save(); // Conflicted.. do I run report add here? Potential to create a mess with action triggers? //Event::run('ushahidi_action.report_add', $incident); // Save media if (isset($this->data->item_title)) { $news = new Media_Model(); $news->location_id = $incident->location_id; $news->incident_id = $incident->id; $news->media_type = 4; // News $news->media_link = $this->data->item_link; $news->media_date = $this->data->item_date; $news->save(); } $incident_id = $incident->id; foreach ($categories as $category_id) { // Assign Category Incident_Category_Model::assign_category_to_incident($incident_id, $category_id); } // Link message with incident? if (isset($this->data->message) and isset($this->data->id)) { $message = new Message_Model($this->data->id); $message->incident_id = $incident_id; $message->save(); } elseif (isset($this->data->item_title) and isset($this->data->id)) { $item = new Feed_Item_Model($this->data->id); $item->incident_id = $incident_id; $item->save(); } return TRUE; }