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

/cp/expressionengine/modules/ip_to_nation/mcp.ip_to_nation.php

https://bitbucket.org/sbeuken/artelux
PHP | 432 lines | 263 code | 84 blank | 85 comment | 21 complexity | 28d25eaa4387a976be049b06daaa392c MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author EllisLab Dev Team
  7. * @copyright Copyright (c) 2003 - 2012, EllisLab, Inc.
  8. * @license http://ellislab.com/expressionengine/user-guide/license.html
  9. * @link http://ellislab.com
  10. * @since Version 2.0
  11. * @filesource
  12. */
  13. // --------------------------------------------------------------------------
  14. /**
  15. * ExpressionEngine IP to Nation Module
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Modules
  19. * @category Modules
  20. * @author EllisLab Dev Team
  21. * @link http://ellislab.com
  22. */
  23. class Ip_to_nation_mcp {
  24. /**
  25. * Constructor
  26. */
  27. function __construct()
  28. {
  29. $this->load->helper('array');
  30. $this->load->model('ip_to_nation_data', 'ip_data');
  31. $this->base_url = BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=ip_to_nation';
  32. if ($this->cp->allowed_group('can_moderate_comments', 'can_edit_all_comments', 'can_delete_all_comments'))
  33. {
  34. $this->cp->set_right_nav(array(
  35. 'update_ips' => $this->base_url.AMP.'method=update_data'
  36. ));
  37. }
  38. }
  39. // ----------------------------------------------------------------------
  40. /**
  41. * Nation Home Page
  42. */
  43. function index()
  44. {
  45. $countries = $this->_country_names();
  46. $ip = '';
  47. $country = '';
  48. $error = FALSE;
  49. if (isset($_POST['ip']))
  50. {
  51. $ip_address = trim($_POST['ip']);
  52. if ($this->input->valid_ip($ip_address))
  53. {
  54. $ip = $ip_address;
  55. $c_code = $this->ip_data->find($ip);
  56. $country = element($c_code, $countries, '');
  57. if ($c_code === FALSE)
  58. {
  59. $error = lang('ip_not_found');
  60. }
  61. }
  62. else
  63. {
  64. $error = lang('ip_not_valid');
  65. }
  66. }
  67. $data = compact('ip', 'country', 'error');
  68. $this->view->cp_page_title = lang('ip_to_nation_module_name');
  69. return $this->load->view('index', $data, TRUE);
  70. }
  71. // ----------------------------------------------------------------------
  72. /**
  73. * Ban list table
  74. */
  75. function banlist()
  76. {
  77. $countries = $this->_country_names();
  78. $query = $this->db->get('ip2nation_countries')->result();
  79. $status = array();
  80. foreach ($query as $row)
  81. {
  82. $status[$row->code] = $row->banned;
  83. }
  84. $country_list = array();
  85. foreach ($countries as $key => $val)
  86. {
  87. // Don't show countries for which we lack IP information
  88. if (isset($status[$key]))
  89. {
  90. $country_list[$key] = array(
  91. 'code' => $key,
  92. 'name' => $val,
  93. 'status' => ($status[$key] == 'y')
  94. );
  95. }
  96. }
  97. $this->cp->set_breadcrumb(
  98. $this->base_url,
  99. lang('ip_to_nation_module_name')
  100. );
  101. $data = compact('country_list');
  102. $this->view->cp_page_title = lang('banlist');
  103. return $this->load->view('banlist', $data, TRUE);
  104. }
  105. // ----------------------------------------------------------------------
  106. /**
  107. * Update Ban List
  108. */
  109. function update()
  110. {
  111. $countries = $this->_country_names();
  112. // remove unknowns and 'n's
  113. $ban = array_intersect_key($_POST, $countries);
  114. $ban = preg_grep('/y/', $ban);
  115. // ban them
  116. $this->ip_data->ban(array_keys($ban));
  117. $this->session->set_flashdata('message_success', lang('banlist_updated'));
  118. $this->functions->redirect($this->base_url.AMP.'method=index');
  119. }
  120. // ----------------------------------------------------------------------
  121. /**
  122. * Update data
  123. */
  124. function update_data()
  125. {
  126. $this->cp->set_breadcrumb($this->base_url, lang('ip_to_nation_module_name'));
  127. $last_update = $this->config->item('ip2nation_db_date');
  128. $cache_files = $this->_cache_files('csv');
  129. // clear out stale data before we start
  130. if ( ! empty($cache_files))
  131. {
  132. foreach ($cache_files as $file)
  133. {
  134. unlink($file);
  135. }
  136. }
  137. // check again, if we can't clear them, the user will
  138. // have to do something about it or we end up killing
  139. // their database in the next step.
  140. if (count($this->_cache_files('csv')))
  141. {
  142. $this->session->set_flashdata('message_failure', lang('@todo cache full'));
  143. $this->functions->redirect($this->base_url.AMP.'method=index');
  144. }
  145. // look for data files that they may have
  146. // uploaded manually
  147. $data_files = $this->_cache_files('zip,gz');
  148. $data = array(
  149. 'update_data_provider' => str_replace(
  150. '%d',
  151. $this->cp->masked_url('http://www.maxmind.com/app/geolite'),
  152. lang('update_data_provider')
  153. ),
  154. 'last_update' => ($last_update) ? $this->localize->set_human_time($last_update) : FALSE
  155. );
  156. $this->cp->add_js_script('fp_module', 'ip_to_nation');
  157. $this->javascript->set_global(array(
  158. 'ip2n' => array(
  159. 'run_script' => 'update',
  160. 'base_url' => str_replace(AMP, '&', $this->base_url),
  161. 'steps' => array('download_data', 'extract_data', 'insert_data'),
  162. 'lang' => array(
  163. 'ip_db_updating' => lang('ip_db_updating'),
  164. 'ip_db_failed' => lang('ip_db_failed')
  165. )
  166. )
  167. ));
  168. $this->view->cp_page_title = lang('update_ips');
  169. return $this->load->view('import', $data, TRUE);
  170. }
  171. // ----------------------------------------------------------------------
  172. /**
  173. * Download new data files
  174. */
  175. function download_data()
  176. {
  177. if ( ! AJAX_REQUEST)
  178. {
  179. show_error(lang('unauthorized_access'));
  180. }
  181. $cache_path = $this->_cache_path();
  182. $valid_response = TRUE;
  183. $out_files = array();
  184. // download
  185. $files = array(
  186. 'http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip',
  187. 'http://geolite.maxmind.com/download/geoip/database/GeoIPv6.csv.gz'
  188. );
  189. foreach ($files as $file)
  190. {
  191. $out_fh = fopen($cache_path.basename($file), "w");
  192. $out_files[] = $cache_path.basename($file);
  193. $timeout = 5;
  194. $ch = curl_init();
  195. curl_setopt($ch, CURLOPT_URL, $file);
  196. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  197. curl_setopt($ch, CURLOPT_FILE, $out_fh);
  198. curl_exec($ch);
  199. $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  200. curl_close($ch);
  201. if ($http_status != '200')
  202. {
  203. $valid_response = FALSE;
  204. $response_code[] = $http_status;
  205. }
  206. }
  207. if ( ! $valid_response)
  208. {
  209. // cleanup
  210. array_map('unlink', $out_files);
  211. $msg = (in_array('403', $response_code)) ? 'ip_db_connection_403' : 'ip_db_connection_error';
  212. $this->output->send_ajax_response(array(
  213. 'error' => lang($msg)
  214. ));
  215. }
  216. $this->output->send_ajax_response(array(
  217. 'success' => lang('ip_db_downloaded')
  218. ));
  219. }
  220. // ----------------------------------------------------------------------
  221. /**
  222. * Extract all data files
  223. */
  224. function extract_data()
  225. {
  226. if ( ! AJAX_REQUEST)
  227. {
  228. show_error(lang('unauthorized_access'));
  229. }
  230. $cache_files = $this->_cache_files('zip, gz');
  231. foreach ($cache_files as $file)
  232. {
  233. $filename = basename($file);
  234. $ext = end(explode('.', $filename));
  235. $fn = '_extract_'.$ext;
  236. $this->$fn($filename);
  237. }
  238. $this->output->send_ajax_response(array(
  239. 'success' => lang('ip_db_unpacked')
  240. ));
  241. }
  242. // ----------------------------------------------------------------------
  243. function insert_data()
  244. {
  245. if ( ! AJAX_REQUEST)
  246. {
  247. show_error(lang('unauthorized_access'));
  248. }
  249. $files = $this->_cache_files('csv');
  250. $this->ip_data->load($files);
  251. // cleanup
  252. array_map('unlink', $this->_cache_files('csv,gz,zip'));
  253. $this->config->_update_config(array('ip2nation_db_date' => $this->localize->now));
  254. $this->output->send_ajax_response(array(
  255. 'success' => lang('ip_db_updated')
  256. ));
  257. }
  258. // ----------------------------------------------------------------------
  259. function _cache_files($ext)
  260. {
  261. $ext = '{'.str_replace(' ', '', $ext).'}';
  262. $path = $this->_cache_path();
  263. return glob($path.'*.'.$ext, GLOB_BRACE);
  264. }
  265. // ----------------------------------------------------------------------
  266. function _cache_path()
  267. {
  268. $cache_path = $this->config->item('cache_path');
  269. if (empty($cache_path))
  270. {
  271. $cache_path = APPPATH.'cache/';
  272. }
  273. $cache_path .= 'ip2nation/';
  274. if ( ! is_dir($cache_path))
  275. {
  276. mkdir($cache_path, DIR_WRITE_MODE);
  277. @chmod($cache_path, DIR_WRITE_MODE);
  278. }
  279. return $cache_path;
  280. }
  281. // ----------------------------------------------------------------------
  282. /**
  283. * Extract gz file
  284. */
  285. private function _extract_gz($source)
  286. {
  287. $cache_path = $this->_cache_path();
  288. ob_start();
  289. readgzfile($cache_path.$source);
  290. $file_contents = ob_get_contents();
  291. ob_end_clean();
  292. $outname = str_replace('.gz', '', $source);
  293. file_put_contents($cache_path.$outname, $file_contents);
  294. @chmod($cache_path.$outname, FILE_WRITE_MODE);
  295. }
  296. // ----------------------------------------------------------------------
  297. /**
  298. * Extract zip archive
  299. *
  300. * Extracts into same directory as the source
  301. */
  302. private function _extract_zip($source)
  303. {
  304. $cache_path = $this->_cache_path();
  305. // unzip
  306. $zip = zip_open($cache_path.$source);
  307. if (is_resource($zip))
  308. {
  309. while ($zip_entry = zip_read($zip))
  310. {
  311. $outfile = $cache_path.zip_entry_name($zip_entry);
  312. $fp = fopen($outfile, "w");
  313. if (zip_entry_open($zip, $zip_entry, "r"))
  314. {
  315. $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
  316. fwrite($fp, "$buf");
  317. zip_entry_close($zip_entry);
  318. }
  319. fclose($fp);
  320. @chmod($outfile, FILE_WRITE_MODE);
  321. }
  322. zip_close($zip);
  323. }
  324. }
  325. // ----------------------------------------------------------------------
  326. /**
  327. * Grab the country name file
  328. */
  329. function _country_names()
  330. {
  331. if ( ! include(APPPATH.'config/countries.php'))
  332. {
  333. show_error(lang('countryfile_missing'));
  334. }
  335. return $countries;
  336. }
  337. // ----------------------------------------------------------------------
  338. /**
  339. * Easier superobject access
  340. */
  341. function __get($key)
  342. {
  343. return get_instance()->$key;
  344. }
  345. }
  346. // END CLASS
  347. /* End of file mcp.ip_to_nation.php */
  348. /* Location: ./system/expressionengine/modules/ip_to_nation/mcp.ip_to_nation.php */