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(); } } }
/** * 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(); } } }
function reporters() { $this->template->content = new View('admin/reporters'); $this->template->content->title = 'Reporters'; // setup and initialize form field names $form = array('reporter_id' => '', 'service_id' => '', 'service_userid' => '', 'service_account' => '', 'reporter_level' => '', 'reporter_first' => '', 'reporter_last' => '', 'reporter_email' => '', 'reporter_phone' => '', 'reporter_ip' => '', 'reporter_date' => ''); // copy the form as errors, so the errors will be stored with keys corresponding to the form field names $errors = $form; $form_error = FALSE; $form_saved = FALSE; $form_action = ""; // check, has the form been submitted, if so, setup validation if ($_POST) { // Instantiate Validation, use $post, so we don't overwrite $_POST fields with our own things $post = Validation::factory($_POST); // Add some filters $post->pre_filter('trim', TRUE); if ($post->action == 'a') { // Add some rules, the input field, followed by a list of checks, carried out in order $post->add_rules('service_id', 'required'); // we also require either service_userid or service_account, not necessarily both } // Test to see if things passed the rule checks if ($post->validate()) { $reporter_id = $post->reporter_id; $reporter = new Reporter_Model($reporter_id); if ($post->action == 'd') { $level->delete($level_id); $form_saved = TRUE; $form_action = "DELETED"; } else { if ($post->action == 'a') { // SAVE Reporter $reporter->service_id = $post->service_id; /*$reporter->service_userid = $post->service_userid; $reporter->service_account = $post->service_account;*/ $reporter->reporter_level = $post->reporter_level; /*$reporter->reporter_first = $post->reporter_first; $reporter->reporter_last = $post->reporter_last; $reporter->reporter_email = $post->reporter_email; $reporter->reporter_phone = $post->reporter_phone; $reporter->reporter_ip = $post->reporter_ip; $reporter->reporter_date = $post->reporter_date;*/ $reporter->save(); $form_saved = TRUE; $form_action = "ADDED/EDITED"; } } } else { // repopulate the form fields $form = arr::overwrite($form, $post->as_array()); // populate the error fields, if any $errors = arr::overwrite($errors, $post->errors('level')); $form_error = TRUE; } } // Pagination $pagination = new Pagination(array('query_string' => 'page', 'items_per_page' => (int) Kohana::config('settings.items_per_page_admin'), 'total_items' => ORM::factory('reporter')->count_all())); $reporters = ORM::factory('reporter')->orderby('reporter_first', 'asc')->find_all((int) Kohana::config('settings.items_per_page_admin'), $pagination->sql_offset); $this->template->content->form = $form; $this->template->content->errors = $errors; $this->template->content->form_error = $form_error; $this->template->content->form_saved = $form_saved; $this->template->content->form_action = $form_action; $this->template->content->pagination = $pagination; $this->template->content->total_items = $pagination->total_items; $this->template->content->reporters = $reporters; // Level and Service Arrays $this->template->content->level_array = Level_Model::get_array(); $this->template->content->service_array = Service_Model::get_array(); }
/** * 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; }
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 reply 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_reply_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; } foreach ($tweets as $tweet) { $tweet_user = $tweet->{'user'}; $reporter = ORM::factory('reporter')->where('service_id', $service->id)->where('service_userid', $tweet_user->{'id'})->find(); if (!$reporter->loaded) { // Add new reporter $names = explode(' ', $tweet_user->{'name'}, 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 = new Reporter_Model(); $reporter->service_id = $service->id; $reporter->level_id = $level->id; $reporter->service_userid = $tweet_user->{'id'}; $reporter->service_account = $tweet_user->{'screen_name'}; $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->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_user->{'screen_name'}; $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(); } } }