PageRenderTime 431ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/system/expressionengine/models/tools_model.php

https://bitbucket.org/tdevonshire/hoolux
PHP | 700 lines | 373 code | 135 blank | 192 comment | 44 complexity | 5c855f1f9ce46027e8bf5e7060eca58c 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 Tools Model
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Core
  19. * @category Model
  20. * @author EllisLab Dev Team
  21. * @link http://ellislab.com
  22. */
  23. class Tools_model extends CI_Model {
  24. /**
  25. * Get Search and Replace Options
  26. *
  27. * This method populates the dropdown field on the search and replace form
  28. *
  29. * @return array
  30. */
  31. public function get_search_replace_options()
  32. {
  33. $options = array();
  34. // site prefs
  35. foreach ($this->session->userdata('assigned_sites') as $site_id => $site_label)
  36. {
  37. $prefs["site_preferences_{$site_id}"] = $site_label;
  38. }
  39. $options['preferences'] = array('name' => $this->lang->line('site_preferences'), 'choices' => $prefs);
  40. // entry titles
  41. $options['title'] = array('name' => $this->lang->line('channel_entry_title'));
  42. // channel fields
  43. $this->db->select('fg.group_name, cf.field_id, cf.field_label, s.site_label');
  44. $this->db->from('field_groups AS fg');
  45. $this->db->join('sites AS s', 's.site_id = fg.site_id');
  46. $this->db->join('channel_fields AS cf', 'cf.group_id = fg.group_id');
  47. if ($this->config->item('multiple_sites_enabled') !== 'y')
  48. {
  49. $this->db->where('cf.site_id', 1);
  50. }
  51. $this->db->order_by('s.site_label, fg.group_id, cf.field_label');
  52. $query = $this->db->get();
  53. $site = '';
  54. $fields = array();
  55. foreach($query->result() as $row)
  56. {
  57. if ($this->config->item('multiple_sites_enabled') == 'y')
  58. {
  59. $fields["field_id_{$row->field_id}"] = $row->site_label.' - '.$row->field_label.' ('.$row->group_name.')';
  60. }
  61. else
  62. {
  63. $fields["field_id_{$row->field_id}"] = $row->field_label.' ('.$row->group_name.')';
  64. }
  65. }
  66. $options['channel_fields'] = array('name' => $this->lang->line('channel_fields'), 'choices' => $fields);
  67. // ALL templates
  68. $options['template_data'] = array('name' => $this->lang->line('templates'));
  69. // template groups
  70. $this->db->select('group_id, group_name, site_label');
  71. $this->db->from('template_groups');
  72. $this->db->join('sites', 'sites.site_id = template_groups.site_id');
  73. if ($this->config->item('multiple_sites_enabled') !== 'y')
  74. {
  75. $this->db->where('sites.site_id', 1);
  76. }
  77. $this->db->order_by('site_label, group_name');
  78. $query = $this->db->get();
  79. $templates = array();
  80. foreach ($query->result() as $row)
  81. {
  82. if ($this->config->item('multiple_sites_enabled') == 'y')
  83. {
  84. $templates["template_{$row->group_id}"] = $row->site_label.' - '.$row->group_name;
  85. }
  86. else
  87. {
  88. $templates["template_{$row->group_id}"] = $row->group_name;
  89. }
  90. }
  91. $options['template_groups'] = array('name' => $this->lang->line('template_groups'), 'choices' => $templates);
  92. return $options;
  93. }
  94. // --------------------------------------------------------------------
  95. /**
  96. * Get Control Panel Log
  97. *
  98. * @return mixed
  99. */
  100. public function get_cp_log($limit = 50, $offset = 0, $order = array())
  101. {
  102. $this->db->select('cp_log.*, sites.site_id, sites.site_label');
  103. $this->db->from('cp_log');
  104. $this->db->join('sites', 'sites.site_id=cp_log.site_id');
  105. if (is_array($order) && count($order) > 0)
  106. {
  107. foreach ($order as $key => $val)
  108. {
  109. $this->db->order_by($key, $val);
  110. }
  111. }
  112. else
  113. {
  114. $this->db->order_by('act_date', 'desc');
  115. }
  116. $this->db->limit($limit, $offset);
  117. return $this->db->get();
  118. }
  119. // --------------------------------------------------------------------
  120. /**
  121. * Get Search Log
  122. *
  123. * @return mixed
  124. */
  125. public function get_search_log($limit = 50, $offset = 0, $order = array())
  126. {
  127. $this->db->select('search_log.*, sites.site_id, sites.site_label');
  128. $this->db->from('search_log');
  129. $this->db->join('sites', 'sites.site_id=search_log.site_id');
  130. if (is_array($order) && count($order) > 0)
  131. {
  132. foreach ($order as $key => $val)
  133. {
  134. $this->db->order_by($key, $val);
  135. }
  136. }
  137. else
  138. {
  139. $this->db->order_by('search_date', 'desc');
  140. }
  141. $this->db->limit($limit, $offset);
  142. return $this->db->get();
  143. }
  144. // --------------------------------------------------------------------
  145. /**
  146. * Get Throttle Log
  147. *
  148. * @param int maximum page loads
  149. * @param int lockout time
  150. * @return mixed
  151. */
  152. public function get_throttle_log($max_page_loads = 10, $lockout_time = 30, $limit = 50, $offset = 0, $order = array())
  153. {
  154. $this->db->select('ip_address, hits, locked_out, last_activity');
  155. $this->db->from('throttle');
  156. $this->db->where('(hits >= "'.$max_page_loads.'" OR (locked_out = "y" AND last_activity > "'.$lockout_time.'"))', NULL, FALSE);
  157. if (is_array($order) && count($order) > 0)
  158. {
  159. foreach ($order as $key => $val)
  160. {
  161. $this->db->order_by($key, $val);
  162. }
  163. }
  164. else
  165. {
  166. $this->db->order_by('ip_address', 'desc');
  167. }
  168. $this->db->limit($limit, $offset);
  169. return $this->db->get();
  170. }
  171. // --------------------------------------------------------------------
  172. /**
  173. * Get items in the Developer Log
  174. *
  175. * @param int $limit Query limit
  176. * @param int $offset Query offset
  177. * @param array $order Array of fields to order by
  178. * e.g. 'field1' => 'asc', 'field2' => 'desc'
  179. * @return DB result object
  180. */
  181. public function get_developer_log($limit = 50, $offset = 0, $order = array())
  182. {
  183. // Apply custom ordering if it is set
  184. if (is_array($order) && count($order) > 0)
  185. {
  186. foreach ($order as $key => $val)
  187. {
  188. $this->db->order_by($key, $val);
  189. }
  190. }
  191. // Otherwise, order by latest log item
  192. else
  193. {
  194. $this->db->order_by('timestamp', 'desc');
  195. }
  196. $this->db->limit($limit, $offset);
  197. return $this->db->get('developer_log');
  198. }
  199. // --------------------------------------------------------------------
  200. /**
  201. * Returns number of unviewed items in the developer log to display in
  202. * a notice on the CP home screen
  203. *
  204. * @return int Number of unviewed developer logs
  205. */
  206. public function count_unviewed_developer_logs()
  207. {
  208. $this->db->where('viewed', 'n');
  209. return $this->db->count_all_results('developer_log');
  210. }
  211. // --------------------------------------------------------------------
  212. /**
  213. * Marks developer logs as viewed
  214. *
  215. * Given an array of logs formatted for datatables, updates each record
  216. * with associated log_id and sets its 'viewed' field to 'y'
  217. *
  218. * @param array $logs Array of existing logs to mark as viewed
  219. * @return void
  220. */
  221. public function mark_developer_logs_as_viewed($logs)
  222. {
  223. $log_ids = array();
  224. // Build an array of log IDs
  225. foreach ($logs as $log)
  226. {
  227. // Don't take on any more logs than we have to
  228. if ($log['viewed'] == 'n')
  229. {
  230. $log_ids[] = $log['log_id']['data'];
  231. }
  232. }
  233. // Set 'viewed' to 'y' where the ID exists in the $log_ids array
  234. if (count($log_ids))
  235. {
  236. $this->db->where_in('log_id', $log_ids);
  237. $this->db->update('developer_log', array('viewed' => 'y'));
  238. }
  239. }
  240. // --------------------------------------------------------------------
  241. /**
  242. * Delete logs
  243. *
  244. * Given a table name, clears out its contents. Given a table name and
  245. * array of log IDs, deletes only those rows from table.
  246. *
  247. * @param string $table Table name
  248. * @param string $id_field Name of ID field in table
  249. * @param array $log_ids IDs of rows to delete
  250. * @return void
  251. */
  252. public function delete_logs($table = NULL, $id_field = NULL, $log_ids = array())
  253. {
  254. // Can't do anything without the table name
  255. if (empty($table))
  256. {
  257. return FALSE;
  258. }
  259. // If no log IDs were passed, clear out the table
  260. if (empty($log_ids))
  261. {
  262. $this->db->empty_table($table);
  263. }
  264. // Otherwise, delete WHERE IN the array of log IDs
  265. else
  266. {
  267. $this->db->where_in($id_field, $log_ids);
  268. $this->db->delete($table);
  269. }
  270. }
  271. // --------------------------------------------------------------------
  272. /**
  273. * Blacklist IP addresses
  274. *
  275. * @param mixed list of ips
  276. * @return int inserted count
  277. */
  278. public function blacklist_ips($naughty = array())
  279. {
  280. // Get all previously blacklisted ips
  281. $this->db->select('blacklisted_value');
  282. $query = $this->db->get_where('blacklisted', array('blacklisted_type' => 'ip'));
  283. // Merge old and new
  284. if ($query->num_rows() > 0)
  285. {
  286. $naughty = array_merge($naughty, explode('|', $query->row('blacklisted_value')));
  287. }
  288. // Clear the old data
  289. $this->db->where('blacklisted_type', 'ip');
  290. $this->db->delete('blacklisted');
  291. // And put the new data back in
  292. $data = array( 'blacklisted_type' => 'ip',
  293. 'blacklisted_value' => implode("|", array_unique($naughty)));
  294. $this->db->insert('blacklisted', $data);
  295. return $this->db->affected_rows();
  296. }
  297. // --------------------------------------------------------------------
  298. /**
  299. * Get Email Logs
  300. *
  301. * @param int
  302. * @return mixed
  303. */
  304. public function get_email_logs($group_id = FALSE, $limit = 50, $offset = 0, $order = array())
  305. {
  306. $this->db->select('cache_id, member_id, member_name, recipient_name, cache_date, subject');
  307. $this->db->from('email_console_cache');
  308. if (is_array($order) && count($order) > 0)
  309. {
  310. foreach ($order as $key => $val)
  311. {
  312. $this->db->order_by($key, $val);
  313. }
  314. }
  315. else
  316. {
  317. $this->db->order_by('cache_id', 'desc');
  318. }
  319. $this->db->limit($limit, $offset);
  320. return $this->db->get();
  321. }
  322. // --------------------------------------------------------------------
  323. /**
  324. * Get Language Filelist
  325. *
  326. * Returns an array of language files
  327. *
  328. * @return array
  329. */
  330. public function get_language_filelist($language_directory = 'english')
  331. {
  332. $this->load->helper('file');
  333. $path = APPPATH.'language/'.$language_directory;
  334. $ext_len = strlen('.php');
  335. $filename_end = '_lang.php';
  336. $filename_end_len = strlen($filename_end);
  337. $languages = array();
  338. $language_files = get_filenames($path);
  339. foreach ($language_files as $file)
  340. {
  341. if ($file == 'email_data.php')
  342. {
  343. continue;
  344. }
  345. if (substr($file, -$filename_end_len) && substr($file, -$ext_len) == '.php')
  346. {
  347. $languages[] = $file;
  348. }
  349. }
  350. sort($languages);
  351. return $languages;
  352. }
  353. // --------------------------------------------------------------------
  354. /**
  355. * Get Language List
  356. *
  357. * Returns an array of language variables in the file
  358. *
  359. * @param string the language file to return
  360. * @param string the folder to save the new file to
  361. * @return array
  362. */
  363. public function get_language_list($language_file = '', $dest_folder = 'translations')
  364. {
  365. if ($language_file == '')
  366. {
  367. show_error('no_lang_file');
  368. }
  369. $language_file = $this->security->sanitize_filename($language_file);
  370. $source_dir = APPPATH.'language/english/';
  371. $dest_dir = APPPATH.$dest_folder.'/';
  372. if ( ! file_exists($source_dir.$language_file))
  373. {
  374. show_error(lang('no_lang_keys'));
  375. }
  376. require($source_dir.$language_file);
  377. $M = $lang;
  378. unset($lang);
  379. if (file_exists($dest_dir.$language_file))
  380. {
  381. require($dest_dir.$language_file);
  382. }
  383. else
  384. {
  385. $lang = $M;
  386. }
  387. $lang_list = array();
  388. foreach ($M as $key => $val)
  389. {
  390. if ($key != '')
  391. {
  392. $trans = ( ! isset($lang[$key])) ? '' : $lang[$key];
  393. $lang_list[$key]['original'] = $val;
  394. $lang_list[$key]['trans'] = $trans;
  395. }
  396. }
  397. return $lang_list;
  398. }
  399. // --------------------------------------------------------------------
  400. /**
  401. * Delete Upload Preferences
  402. *
  403. * @deprecated Deprecated since 2.2
  404. * @param int
  405. * @return string
  406. */
  407. public function delete_upload_preferences($id = '')
  408. {
  409. $this->load->library('logger');
  410. $this->logger->deprecated('2.2', 'File_upload_preferences_model::delete_upload_preferences()');
  411. $this->load->model('file_upload_preferences_model');
  412. return $this->file_upload_preferences_model->delete_upload_preferences($id);
  413. }
  414. // --------------------------------------------------------------------
  415. /**
  416. * Get Upload Preferences
  417. *
  418. * @deprecated Deprecated since 2.2
  419. * @param int
  420. * @return mixed
  421. */
  422. public function get_upload_preferences($group_id = NULL, $id = NULL)
  423. {
  424. $this->load->library('logger');
  425. $this->logger->deprecated('2.2', 'File_upload_preferences_model::get_file_upload_preferences()');
  426. $this->load->model('file_upload_preferences_model');
  427. return $this->file_upload_preferences_model->get_upload_preferences($group_id, $id);
  428. }
  429. // --------------------------------------------------------------------
  430. /**
  431. * Get Files
  432. *
  433. * @deprecated Deprecated since 2.2
  434. * @param int
  435. * @return mixed
  436. */
  437. public function get_files($directories = array(), $allowed_types = array(), $full_server_path = '', $hide_sensitive_data = FALSE, $get_dimensions = FALSE, $files_array = array())
  438. {
  439. $this->load->library('logger');
  440. $this->logger->deprecated('2.2', 'File_model::get_raw_files()');
  441. $this->load->model('file_model');
  442. return $this->file_model->get_raw_files($directories, $allowed_types, $full_server_path, $hide_sensitive_data, $get_dimensions, $files_array);
  443. }
  444. // --------------------------------------------------------------------
  445. /**
  446. * Get Image Properties
  447. *
  448. * Fetches image width, height, and type
  449. *
  450. * @return string filepath
  451. */
  452. public function image_properties($file)
  453. {
  454. if (function_exists('getimagesize'))
  455. {
  456. if ( ! $D = @getimagesize($file))
  457. {
  458. return FALSE;
  459. }
  460. $this->width = $D['0'];
  461. $this->height = $D['1'];
  462. $this->imgtype = $D['2'];
  463. return $D;
  464. }
  465. else
  466. {
  467. return FALSE;
  468. }
  469. }
  470. // --------------------------------------------------------------------
  471. /**
  472. * Get SQL Info
  473. *
  474. * Fetches various stats for the database
  475. *
  476. * @return array
  477. */
  478. public function get_sql_info()
  479. {
  480. $this->load->helper('number');
  481. $info = array();
  482. // database type
  483. $info['database_type'] = $this->db->dbdriver;
  484. // db version
  485. $info['sql_version'] = $this->db->version();
  486. // db records and size
  487. $query = $this->db->query("SHOW TABLE STATUS FROM `{$this->db->database}`");
  488. $totsize = 0;
  489. $records = 0;
  490. $prefix_len = strlen($this->db->dbprefix);
  491. foreach ($query->result_array() as $row)
  492. {
  493. if (strncmp($row['Name'], $this->db->dbprefix, $prefix_len) != 0)
  494. {
  495. continue;
  496. }
  497. $totsize += $row['Data_length'] + $row['Index_length'];
  498. $records += $row['Rows'];
  499. }
  500. $info['records'] = $records;
  501. $info['size'] = byte_format($totsize);
  502. // db uptime
  503. $query = $this->db->query("SHOW STATUS");
  504. $uptime = '';
  505. $queries = '';
  506. // We need this a bit later
  507. $res = $query->result_array();
  508. foreach ($res as $key => $val)
  509. {
  510. foreach ($val as $v)
  511. {
  512. if (strncasecmp($v, 'uptime', 6) == 0)
  513. {
  514. $uptime = $key;
  515. }
  516. if (strncasecmp($v, 'questions', 9) == 0)
  517. {
  518. $queries = $key;
  519. }
  520. }
  521. }
  522. $info['database_uptime'] = $this->localize->format_timespan($res[$uptime]['Value']);
  523. $info['total_queries'] = number_format($query->result_array[$queries]['Value']);
  524. return $info;
  525. }
  526. // --------------------------------------------------------------------
  527. /**
  528. * Get Table Status
  529. *
  530. * Runs a STATUS query on the database, returns query object
  531. *
  532. * @return object
  533. */
  534. public function get_table_status()
  535. {
  536. $this->load->helper('number');
  537. $status = array();
  538. $query = $this->db->query("SHOW TABLE STATUS FROM `{$this->db->database}`");
  539. $i = 0;
  540. $records = 0;
  541. $tables = 0;
  542. $totsize = 0;
  543. $prefix_len = strlen($this->db->dbprefix);
  544. foreach ($query->result() as $row)
  545. {
  546. if (strncmp($row->Name, $this->db->dbprefix, $prefix_len) != 0)
  547. {
  548. continue;
  549. }
  550. $len = $row->Data_length + $row->Index_length;
  551. $status[$i]['name'] = $row->Name;
  552. $status[$i]['rows'] = $row->Rows;
  553. $status[$i]['size'] = byte_format($len);
  554. $status[$i]['browse_link'] = BASE.AMP.'C=tools_data'.AMP.'M=sql_run_query'.AMP.'thequery='.rawurlencode(base64_encode('SELECT * FROM `'.$row->Name.'`'));
  555. $records += $row->Rows;
  556. $totsize += $len;
  557. $tables++;
  558. $i++;
  559. }
  560. return array('status' => $status, 'records' => $records, 'total_size' => byte_format($totsize), 'tables' => $tables);
  561. }
  562. // --------------------------------------------------------------------
  563. }
  564. /* End of file tools_model.php */
  565. /* Location: ./system/expressionengine/models/tools_model.php */