PageRenderTime 43ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/application/controllers/admin/settings/api.php

https://github.com/yamamoto123/Ushahidi_Web
PHP | 354 lines | 229 code | 53 blank | 72 comment | 27 complexity | 9f88f0762f1224c21fa86554d0a0c624 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access allowed.');
  2. /**
  3. * This controller is used to manage API logging
  4. *
  5. * PHP version 5
  6. * LICENSE: This source file is subject to LGPL license
  7. * that is available through the world-wide-web at the following URI:
  8. * http://www.gnu.org/copyleft/lesser.html
  9. * @author Ushahidi Team <team@ushahidi.com>
  10. * @package Ushahidi - http://source.ushahididev.com
  11. * @subpackage Admin
  12. * @copyright Ushahidi - http://www.ushahidi.com
  13. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
  14. */
  15. class Api_Controller extends Admin_Controller {
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. $this->template->this_page = 'settings';
  20. if ( ! admin::permissions($this->user, "manage"))
  21. {
  22. url::redirect(url::site().'admin/dashboard');
  23. }
  24. }
  25. /**
  26. * API Logging settings
  27. */
  28. public function index()
  29. {
  30. $this->template->content = new View('admin/api');
  31. // Set up and initialize form field names
  32. $form = array
  33. (
  34. 'api_default_record_limit' => '',
  35. 'api_max_record_limit' => '',
  36. 'api_max_requests_per_ip_address' => '',
  37. 'api_max_requests_quota_basis' => ''
  38. );
  39. // Copy the form as errors, so the errors will be stored with keys
  40. // corresponding to the form field names
  41. $errors = $form;
  42. $form_error = FALSE;
  43. $form_saved = FALSE;
  44. // Check if the form has been submitted, if so setup validation
  45. if ($_POST)
  46. {
  47. $post = new Validation($_POST);
  48. // Add some filters
  49. $post->pre_filter('trim', TRUE);
  50. // Add validation rules
  51. // All values must be positive values; no (-ve) values are allowed
  52. $post->add_rules('api_default_record_limit', 'required', 'numeric', 'length[1,20]')
  53. ->add_rules('api_max_record_limit', 'numeric', 'length[0,20]')
  54. ->add_rules('api_max_requests_per_ip_address', 'depends_on[api_max_requests_quota_basis]', 'numeric', 'length[0,20]')
  55. ->add_rules('api_max_requests_quota_basis', 'depends_on[api_max_requests_per_ip_address]', 'numeric', 'between[0,1]');
  56. // Test to see if rule checks have beens satisfied
  57. if ($post->validate() AND $post->action == 's')
  58. {
  59. // Check if the maximum record limit is less than the default
  60. if (isset($post->api_max_record_limit) AND strlen($post->api_max_record_limit > 0))
  61. {
  62. if ((int) $post->api_default_record_limit > (int) $post->api_max_record_limit)
  63. {
  64. $errors[] = Kohana::lang('ui_admin.api_invalid_max_record_limit');
  65. $form_error = TRUE;
  66. }
  67. }
  68. // Proceed with saving if there's no form error
  69. if ( ! $form_error)
  70. {
  71. // Everything is valid
  72. $api_settings = new Api_Settings_Model(1);
  73. $api_settings->default_record_limit = ((int) $post->api_default_record_limit > 0)
  74. ? $post->api_default_record_limit
  75. : (int) Kohana::config('settings.items_per_api_request');
  76. $api_settings->max_record_limit = $post->api_max_record_limit;
  77. $api_settings->max_requests_per_ip_address = $post->api_max_requests_per_ip_address;
  78. // Only set the quota basis if the max. no of API requests per IP has been specified
  79. $api_settings->max_requests_quota_basis = ((int) $post->api_max_requests_per_ip_address > 0)
  80. ? $post->api_max_requests_quota_basis
  81. : NULL;
  82. $api_settings->modification_date = date("Y-m-d H:i:s", time());
  83. $api_settings->save();
  84. $form_saved = TRUE;
  85. // Repopulate the form fields
  86. $form = arr::overwrite($form, $post->as_array());
  87. }
  88. }
  89. // There are validation errors
  90. else
  91. {
  92. // Re-populate the form fields
  93. $form = arr::overwrite($form, $post->as_array());
  94. // Populate the error fields if any
  95. $errors = arr::overwrite($errors, $post->errors('api_settings'));
  96. $form_error = TRUE;
  97. }
  98. }
  99. else
  100. {
  101. // Retrieve current settings
  102. $api_settings = ORM::factory('api_settings', 1);
  103. $form = array
  104. (
  105. 'api_default_record_limit' => ((int) $api_settings->default_record_limit > 0)
  106. ? $api_settings->default_record_limit
  107. : Kohana::config('settings.items_per_api_request'),
  108. 'api_max_record_limit' => $api_settings->max_record_limit,
  109. 'api_max_requests_per_ip_address' => $api_settings->max_requests_per_ip_address,
  110. 'api_max_requests_quota_basis' => $api_settings->max_requests_quota_basis
  111. );
  112. }
  113. // Set the form data
  114. $this->template->content->form = $form;
  115. // Set the form errors
  116. $this->template->content->errors = $errors;
  117. // Set the status of the form
  118. $this->template->content->form_error = $form_error;
  119. $this->template->content->form_saved = $form_saved;
  120. // API request quota options (per day, month)
  121. $this->template->content->max_requests_quota_array = array(
  122. '' => '-- Select --',
  123. '0' => Kohana::lang('ui_main.day'),
  124. '1' => Kohana::lang('ui_main.month')
  125. );
  126. // Javascript header
  127. $this->template->js = new View('admin/api_js');
  128. }
  129. /**
  130. * Displays the API logs
  131. */
  132. public function log()
  133. {
  134. $this->template->content = new View('admin/apilogs');
  135. $this->template->content->this_page='apilogs';
  136. $this->template->content->title = Kohana::lang('ui_main.api_logs');
  137. $form_error = FALSE;
  138. $form_saved = FALSE;
  139. $form_action = "";
  140. // Check if the form has been submitted
  141. if ($_POST)
  142. {
  143. $post = Validation::factory($_POST);
  144. // Add some filters
  145. $post->pre_filter('trim', TRUE);
  146. // Add some rules
  147. $post->add_rules('action', 'required', 'alpha', 'length[1,1]');
  148. $post->add_rules('api_log_id.*', 'required', 'numeric');
  149. // Validate the submitted data against the validation rules
  150. if ($post->validate())
  151. {
  152. if ($post->action == 'd') // Delete action
  153. {
  154. foreach ($post->api_log_id as $item)
  155. {
  156. $update = new Api_Log_Model($item);
  157. if ($update->loaded == true)
  158. {
  159. $update->delete();
  160. }
  161. }
  162. $form_action = "DELETED";
  163. }
  164. elseif ($post->action == 'x') // Delete all logs action
  165. {
  166. ORM::factory('api_log')->delete_all();
  167. $form_action = "DELETED";
  168. }
  169. elseif ($post->action == 'b')
  170. {
  171. foreach ($post->api_log_id as $item)
  172. {
  173. $log_item = new Api_Log_Model($item);
  174. if ($log_item->loaded == true)
  175. {
  176. // Get the IP Address associated with the specified api_log id
  177. $log_ip_address = $log_item->api_ipaddress;
  178. // Check if the IP address has already been banned
  179. $banned_count = ORM::factory('api_banned')
  180. ->where('banned_ipaddress', $log_ip_address)
  181. ->count_all();
  182. if ($banned_count == 0)
  183. {
  184. // Add the IP to the list of banned addresses
  185. $api_banned = new Api_Banned_Model();
  186. $api_banned->banned_ipaddress = $log_ip_address;
  187. $api_banned->banned_date = date('Y-m-d H:i:s', time());
  188. $api_banned->save();
  189. }
  190. }
  191. }
  192. $form_action = "BANNED";
  193. }
  194. $form_saved = TRUE;
  195. }
  196. else
  197. {
  198. $form_error = TRUE;
  199. }
  200. }
  201. // END form submission check
  202. // Set up pagination
  203. $pagination = new Pagination(array(
  204. 'query_string' => 'page',
  205. 'items_per_page' => (int)Kohana::config('settings.items_per_page_admin'),
  206. 'total_items' => ORM::factory('api_log')->count_all()
  207. ));
  208. // Fetch the api logs and page them
  209. $api_logs = $this->db->query('
  210. SELECT al.id, al.api_task, ab.id AS ban_id, al.api_parameters, al.api_records, al.api_ipaddress, al.api_date
  211. FROM '.$this->table_prefix.'api_log al
  212. LEFT JOIN '.$this->table_prefix.'api_banned AS ab ON (ab.banned_ipaddress = al.api_ipaddress)
  213. ORDER BY al.api_date DESC
  214. LIMIT ' . $pagination->sql_offset. ', '.$this->items_per_page
  215. );
  216. /*
  217. $api_logs = ORM::factory('api_log')
  218. ->orderby('api_date', 'asc')
  219. ->find_all($this->items_per_page, $pagination->sql_offset);
  220. */
  221. // Set the total no. of items
  222. $this->template->content->total_items = ORM::factory('api_log')->count_all();
  223. // Set the form action
  224. $this->template->content->form_action = $form_action;
  225. $this->template->content->form_error = $form_error;
  226. $this->template->content->form_saved = $form_saved;
  227. $this->template->content->api_logs = $api_logs;
  228. $this->template->content->pagination = $pagination;
  229. // Javascript header
  230. $this->template->js = new View('admin/apilogs_js');
  231. }
  232. /**
  233. * Displays the list of IP addresses that have been banned from access the API
  234. */
  235. public function apibanned()
  236. {
  237. $this->template->content = new View('admin/api_banned');
  238. $this->template->content->this_page = 'apibanned';
  239. $this->template->content->title = Kohana::lang('ui_main.api_banned');
  240. $form_error = FALSE;
  241. $form_saved = FALSE;
  242. $form_action = "";
  243. // Check if the form has been submitted
  244. if ($_POST)
  245. {
  246. $post = Validation::factory($_POST);
  247. // Add some filters
  248. $post->pre_filter('trim', TRUE);
  249. // Add some validation rules
  250. $post->add_rules('action', 'required', 'alpha', 'length[1,1]');
  251. $post->add_rules('api_banned_id.*', 'required', 'numeric');
  252. // Validate the submitted data against the validatieon rules
  253. if ($post->validate())
  254. {
  255. if ($post->action == 'd') // Uban action
  256. {
  257. foreach ($post->api_banned_id as $item)
  258. {
  259. $update = new Api_Banned_Model($item);
  260. if ($update->loaded == true)
  261. {
  262. $update->delete();
  263. }
  264. }
  265. $form_action = "UNBANNED";
  266. }
  267. elseif ($post->action == 'x') // Unban all IP addresses
  268. {
  269. ORM::factory('api_banned')->delete_all();
  270. $form_action = "UNBANNED";
  271. }
  272. $form_saved = TRUE;
  273. }
  274. else // Validation failed
  275. {
  276. $form_error = TRUE;
  277. }
  278. }
  279. // END form submission check
  280. // Set up pagination
  281. $pagination = new Pagination(array(
  282. 'query_string' => 'page',
  283. 'items_per_page' => $this->items_per_page,
  284. 'total_items' => ORM::factory('api_banned')->count_all()
  285. ));
  286. // Fetch all the IP addresses banned from accessing the API
  287. $api_bans = ORM::factory('api_banned')
  288. ->orderby('banned_date', 'desc')
  289. ->find_all($this->items_per_page, $pagination->sql_offset);
  290. // Set the total no. of items
  291. $this->template->content->total_items = ORM::factory('api_banned')->count_all();
  292. // Set the form action
  293. $this->template->content->form_action = $form_action;
  294. $this->template->content->form_error = $form_error;
  295. $this->template->content->form_saved = $form_saved;
  296. $this->template->content->api_bans = $api_bans;
  297. $this->template->content->pagination = $pagination;
  298. // Javascript header
  299. $this->template->js = new View('admin/api_banned_js');
  300. }
  301. }