Example #1
0
 /**
  * Logs API requests
  * If @param task_library_found == FALSE the no. of records returned is 0
  *
  * @param bool $task_library_found
  */
 private function _log_api_request($task_library_found)
 {
     // Log the API request
     $this->api_logger = new Api_Log_Model();
     $this->api_logger->api_task = strtolower($this->task_name);
     $this->api_logger->api_parameters = $this->api_parameters;
     $this->api_logger->api_ipaddress = $this->request_ip_address;
     $this->api_logger->api_records = $task_library_found ? $this->api_object->get_record_count() : 0;
     $this->api_logger->api_date = date('Y-m-d H:i:s', time());
     $this->api_logger->save();
 }
Example #2
0
 /**
  * Displays the API logs
  */
 public function log()
 {
     $this->template->content = new View('admin/settings/api/logs');
     $this->template->content->this_page = 'apilogs';
     $this->template->content->title = Kohana::lang('ui_main.api_logs');
     $form_error = FALSE;
     $form_saved = FALSE;
     $form_action = "";
     // Check if the form has been submitted
     if ($_POST) {
         $post = Validation::factory($_POST);
         // Add some filters
         $post->pre_filter('trim', TRUE);
         // Add some rules
         $post->add_rules('action', 'required', 'alpha', 'length[1,1]');
         $post->add_rules('api_log_id.*', 'required', 'numeric');
         // Validate the submitted data against the validation rules
         if ($post->validate()) {
             if ($post->action == 'd') {
                 foreach ($post->api_log_id as $item) {
                     $update = new Api_Log_Model($item);
                     if ($update->loaded == true) {
                         $update->delete();
                     }
                 }
                 $form_action = "DELETED";
             } elseif ($post->action == 'x') {
                 ORM::factory('api_log')->delete_all();
                 $form_action = "DELETED";
             } elseif ($post->action == 'b') {
                 foreach ($post->api_log_id as $item) {
                     $log_item = new Api_Log_Model($item);
                     if ($log_item->loaded == true) {
                         // Get the IP Address associated with the specified api_log id
                         $log_ip_address = $log_item->api_ipaddress;
                         // Check if the IP address has already been banned
                         $banned_count = ORM::factory('api_banned')->where('banned_ipaddress', $log_ip_address)->count_all();
                         if ($banned_count == 0) {
                             // Add the IP to the list of banned addresses
                             $api_banned = new Api_Banned_Model();
                             $api_banned->banned_ipaddress = $log_ip_address;
                             $api_banned->banned_date = date('Y-m-d H:i:s', time());
                             $api_banned->save();
                         }
                     }
                 }
                 $form_action = "BANNED";
             }
             $form_saved = TRUE;
         } else {
             $form_error = TRUE;
         }
     }
     // END form submission check
     // Set up pagination
     $pagination = new Pagination(array('query_string' => 'page', 'items_per_page' => (int) Kohana::config('settings.items_per_page_admin'), 'total_items' => ORM::factory('api_log')->count_all()));
     // Fetch the api logs and page them
     $api_logs = $this->db->query('
             SELECT al.id, al.api_task, ab.id AS ban_id, al.api_parameters, al.api_records, al.api_ipaddress, al.api_date 
             FROM ' . $this->table_prefix . 'api_log al
             LEFT JOIN ' . $this->table_prefix . 'api_banned AS ab ON (ab.banned_ipaddress = al.api_ipaddress)
             ORDER BY al.api_date DESC
             LIMIT ' . $pagination->sql_offset . ', ' . $this->items_per_page);
     /*    
     $api_logs = ORM::factory('api_log')
                     ->orderby('api_date', 'asc')
                     ->find_all($this->items_per_page, $pagination->sql_offset);
     */
     // Set the total no. of items
     $this->template->content->total_items = ORM::factory('api_log')->count_all();
     // Set the form action
     $this->template->content->form_action = $form_action;
     $this->template->content->form_error = $form_error;
     $this->template->content->form_saved = $form_saved;
     $this->template->content->api_logs = $api_logs;
     $this->template->content->pagination = $pagination;
     // Javascript header
     $this->template->js = new View('admin/settings/api/logs_js');
 }