PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/wp-content/plugins/redirection/models/log.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 388 lines | 303 code | 85 blank | 0 comment | 29 complexity | 9ede765f5527d5d7ea2ac38f8b8a7482 MD5 | raw file
  1. <?php
  2. class RE_Log {
  3. public $id;
  4. public $created;
  5. public $url;
  6. public $agent;
  7. public $referrer;
  8. public $ip;
  9. public $redirection_id;
  10. function __construct( $values ) {
  11. foreach ( $values as $key => $value ) {
  12. $this->$key = $value;
  13. }
  14. $this->created = mysql2date( 'U', $this->created );
  15. $this->url = stripslashes( $this->url );
  16. }
  17. static function get_by_id( $id ) {
  18. global $wpdb;
  19. $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_logs WHERE id=%d", $id ) );
  20. if ( $row ) {
  21. return new RE_Log( $row );
  22. }
  23. return false;
  24. }
  25. static function create( $url, $target, $agent, $ip, $referrer, $extra = array() ) {
  26. global $wpdb, $redirection;
  27. $insert = array(
  28. 'url' => urldecode( $url ),
  29. 'created' => current_time( 'mysql' ),
  30. 'ip' => substr( $ip, 0, 45 ),
  31. );
  32. if ( ! empty( $agent ) ) {
  33. $insert['agent'] = $agent;
  34. }
  35. if ( ! empty( $referrer ) ) {
  36. $insert['referrer'] = $referrer;
  37. }
  38. $insert['sent_to'] = $target;
  39. $insert['redirection_id'] = isset( $extra['redirect_id'] ) ? $extra['redirect_id'] : 0;
  40. $insert['group_id'] = isset( $extra['group_id'] ) ? $extra['group_id'] : 0;
  41. $insert = apply_filters( 'redirection_log_data', $insert );
  42. if ( $insert ) {
  43. do_action( 'redirection_log', $insert );
  44. $wpdb->insert( $wpdb->prefix . 'redirection_logs', $insert );
  45. }
  46. return $wpdb->insert_id;
  47. }
  48. static function show_url( $url ) {
  49. return $url;
  50. }
  51. static function delete( $id ) {
  52. global $wpdb;
  53. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE id=%d", $id ) );
  54. }
  55. static function delete_for_id( $id ) {
  56. global $wpdb;
  57. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE redirection_id=%d", $id ) );
  58. }
  59. static function delete_for_group( $id ) {
  60. global $wpdb;
  61. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE group_id=%d", $id ) );
  62. }
  63. static function delete_all( $filter_by = [] ) {
  64. global $wpdb;
  65. $where = [];
  66. if ( isset( $filter_by['url'] ) ) {
  67. $where[] = $wpdb->prepare( 'url LIKE %s', '%' . $wpdb->esc_like( $filter_by['url'] ) . '%' );
  68. } elseif ( isset( $filter_by['ip'] ) ) {
  69. $where[] = $wpdb->prepare( 'ip=%s', $filter_by['ip'] );
  70. }
  71. $where_cond = '';
  72. if ( count( $where ) > 0 ) {
  73. $where_cond = ' WHERE ' . implode( ' AND ', $where );
  74. }
  75. $wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_logs" . $where_cond );
  76. }
  77. static function export_to_csv() {
  78. global $wpdb;
  79. $filename = 'redirection-log-' . date_i18n( get_option( 'date_format' ) ) . '.csv';
  80. header( 'Content-Type: text/csv' );
  81. header( 'Cache-Control: no-cache, must-revalidate' );
  82. header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
  83. header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
  84. $stdout = fopen( 'php://output', 'w' );
  85. fputcsv( $stdout, array( 'date', 'source', 'target', 'ip', 'referrer', 'agent' ) );
  86. $total_items = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_logs" );
  87. $exported = 0;
  88. while ( $exported < $total_items ) {
  89. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_logs LIMIT %d,%d", $exported, 100 ) );
  90. $exported += count( $rows );
  91. foreach ( $rows as $row ) {
  92. $csv = array(
  93. $row->created,
  94. $row->url,
  95. $row->sent_to,
  96. $row->ip,
  97. $row->referrer,
  98. $row->agent,
  99. );
  100. fputcsv( $stdout, $csv );
  101. }
  102. if ( count( $rows ) < 100 ) {
  103. break;
  104. }
  105. }
  106. }
  107. public function to_json() {
  108. return array(
  109. 'sent_to' => $this->sent_to,
  110. 'ip' => $this->ip,
  111. );
  112. }
  113. }
  114. class RE_404 {
  115. public $id;
  116. public $created;
  117. public $url;
  118. public $agent;
  119. public $referrer;
  120. public $ip;
  121. function __construct( $values ) {
  122. foreach ( $values as $key => $value ) {
  123. $this->$key = $value;
  124. }
  125. $this->created = mysql2date( 'U', $this->created );
  126. }
  127. static function get_by_id( $id ) {
  128. global $wpdb;
  129. $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_404 WHERE id=%d", $id ) );
  130. if ( $row ) {
  131. return new RE_404( $row );
  132. }
  133. return false;
  134. }
  135. static function create( $url, $agent, $ip, $referrer ) {
  136. global $wpdb, $redirection;
  137. $insert = array(
  138. 'url' => substr( urldecode( $url ), 0, 255 ),
  139. 'created' => current_time( 'mysql' ),
  140. 'ip' => substr( $ip, 0, 45 ),
  141. );
  142. if ( ! empty( $agent ) ) {
  143. $insert['agent'] = substr( $agent, 0, 255 );
  144. }
  145. if ( ! empty( $referrer ) ) {
  146. $insert['referrer'] = substr( $referrer, 0, 255 );
  147. }
  148. $insert = apply_filters( 'redirection_404_data', $insert );
  149. if ( $insert ) {
  150. $wpdb->insert( $wpdb->prefix . 'redirection_404', $insert );
  151. if ( $wpdb->insert_id ) {
  152. return $wpdb->insert_id;
  153. }
  154. }
  155. return false;
  156. }
  157. static function delete( $id ) {
  158. global $wpdb;
  159. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_404 WHERE id=%d", $id ) );
  160. }
  161. static function delete_all( $filter_by, $filter_value ) {
  162. global $wpdb;
  163. $where = [];
  164. if ( $filter_by === 'url-exact' ) {
  165. $where[] = $wpdb->prepare( 'url=%s', $filter_value );
  166. } elseif ( $filter_by === 'url' ) {
  167. $where[] = $wpdb->prepare( 'url LIKE %s', '%' . $wpdb->esc_like( $filter_value ) . '%' );
  168. } elseif ( $filter_by === 'ip' ) {
  169. $where[] = $wpdb->prepare( 'ip=%s', $filter_value );
  170. }
  171. $where_cond = '';
  172. if ( count( $where ) > 0 ) {
  173. $where_cond = ' WHERE ' . implode( ' AND ', $where );
  174. }
  175. $wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_404" . $where_cond );
  176. }
  177. static function export_to_csv() {
  178. global $wpdb;
  179. $filename = 'redirection-404-' . date_i18n( get_option( 'date_format' ) ) . '.csv';
  180. header( 'Content-Type: text/csv' );
  181. header( 'Cache-Control: no-cache, must-revalidate' );
  182. header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
  183. header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
  184. $stdout = fopen( 'php://output', 'w' );
  185. fputcsv( $stdout, array( 'date', 'source', 'ip', 'referrer', 'useragent' ) );
  186. $total_items = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_404" );
  187. $exported = 0;
  188. while ( $exported < $total_items ) {
  189. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_404 LIMIT %d,%d", $exported, 100 ) );
  190. $exported += count( $rows );
  191. foreach ( $rows as $row ) {
  192. $csv = array(
  193. $row->created,
  194. $row->url,
  195. $row->ip,
  196. $row->referrer,
  197. $row->agent,
  198. );
  199. fputcsv( $stdout, $csv );
  200. }
  201. if ( count( $rows ) < 100 ) {
  202. break;
  203. }
  204. }
  205. }
  206. public function to_json() {
  207. return array(
  208. 'ip' => $this->ip,
  209. );
  210. }
  211. }
  212. class RE_Filter_Log {
  213. static public function get( $table, $construct, array $params ) {
  214. global $wpdb;
  215. $query = self::get_query( $params );
  216. $rows = $wpdb->get_results(
  217. "SELECT * FROM {$wpdb->prefix}$table {$query['where']}" . $wpdb->prepare( ' ORDER BY ' . $query['orderby'] . ' ' . $query['direction'] . ' LIMIT %d,%d', $query['offset'], $query['limit'] )
  218. );
  219. $total_items = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}$table " . $query['where'] );
  220. $items = array();
  221. foreach ( $rows as $row ) {
  222. $item = new $construct( $row );
  223. $items[] = array_merge( $item->to_json(), array(
  224. 'id' => intval( $item->id, 10 ),
  225. 'created' => date_i18n( get_option( 'date_format' ), $item->created ),
  226. 'created_time' => gmdate( get_option( 'time_format' ), $item->created ),
  227. 'url' => $item->url,
  228. 'agent' => $item->agent,
  229. 'referrer' => $item->referrer,
  230. ) );
  231. }
  232. return array(
  233. 'items' => $items,
  234. 'total' => intval( $total_items, 10 ),
  235. );
  236. }
  237. static public function get_grouped( $table, $group, array $params ) {
  238. global $wpdb;
  239. $query = self::get_query( $params );
  240. if ( ! in_array( $group, array( 'ip', 'url' ), true ) ) {
  241. $group = 'url';
  242. }
  243. $sql = $wpdb->prepare( "SELECT COUNT(*) as count,$group FROM {$wpdb->prefix}$table " . $query['where'] . ' GROUP BY ' . $group . ' ORDER BY count ' . $query['direction'] . ', ' . $group . ' LIMIT %d,%d', $query['offset'], $query['limit'] );
  244. $rows = $wpdb->get_results( $sql );
  245. $total_items = $wpdb->get_var( "SELECT COUNT(DISTINCT $group) FROM {$wpdb->prefix}$table" );
  246. foreach ( $rows as $row ) {
  247. $row->count = intval( $row->count, 10 );
  248. $row->id = isset( $row->url ) ? $row->url : $row->ip;
  249. }
  250. return array(
  251. 'items' => $rows,
  252. 'total' => intval( $total_items, 10 ),
  253. );
  254. }
  255. static private function get_query( array $params ) {
  256. global $wpdb;
  257. $query = array(
  258. 'orderby' => 'id',
  259. 'direction' => 'DESC',
  260. 'limit' => RED_DEFAULT_PER_PAGE,
  261. 'offset' => 0,
  262. 'where' => '',
  263. );
  264. if ( isset( $params['orderby'] ) && in_array( $params['orderby'], array( 'ip', 'url' ), true ) ) {
  265. $query['orderby'] = $params['orderby'];
  266. }
  267. if ( isset( $params['direction'] ) && in_array( $params['direction'], array( 'asc', 'desc' ), true ) ) {
  268. $query['direction'] = strtoupper( $params['direction'] );
  269. }
  270. if ( isset( $params['filterBy'] ) && is_array( $params['filterBy'] ) ) {
  271. if ( isset( $params['filterBy']['ip'] ) ) {
  272. $ip = @inet_pton( trim( $params['filterBy']['ip'] ) );
  273. if ( $ip !== false ) {
  274. $ip = @inet_ntop( $ip ); // Convert back to string
  275. $query['where'] = $wpdb->prepare( 'WHERE ip=%s', $ip );
  276. } else {
  277. $query['where'] = $wpdb->prepare( 'WHERE ip LIKE %s', '%' . $wpdb->esc_like( trim( $params['filterBy']['ip'] ) ) . '%' );
  278. }
  279. } elseif ( isset( $params['filterBy']['url-exact'] ) ) {
  280. $query['where'] = $wpdb->prepare( 'WHERE url=%s', $params['filterBy']['url-exact'] );
  281. } elseif ( isset( $params['filterBy']['referrer'] ) ) {
  282. $query['where'] = $wpdb->prepare( 'WHERE referrer LIKE %s', '%' . $wpdb->esc_like( trim( $params['filterBy']['referrer'] ) ) . '%' );
  283. } elseif ( isset( $params['filterBy']['agent'] ) ) {
  284. $query['where'] = $wpdb->prepare( 'WHERE agent LIKE %s', '%' . $wpdb->esc_like( trim( $params['filterBy']['agent'] ) ) . '%' );
  285. } elseif ( isset( $params['filterBy']['target'] ) ) {
  286. $query['where'] = $wpdb->prepare( 'WHERE sent_to LIKE %s', '%' . $wpdb->esc_like( trim( $params['filterBy']['target'] ) ) . '%' );
  287. } elseif ( isset( $params['filterBy']['url'] ) ) {
  288. $query['where'] = $wpdb->prepare( 'WHERE url LIKE %s', '%' . $wpdb->esc_like( trim( $params['filterBy']['url'] ) ) . '%' );
  289. }
  290. }
  291. if ( isset( $params['per_page'] ) ) {
  292. $query['limit'] = intval( $params['per_page'], 10 );
  293. $query['limit'] = min( RED_MAX_PER_PAGE, $query['limit'] );
  294. $query['limit'] = max( 5, $query['limit'] );
  295. }
  296. if ( isset( $params['page'] ) ) {
  297. $query['offset'] = intval( $params['page'], 10 );
  298. $query['offset'] = max( 0, $query['offset'] );
  299. $query['offset'] *= $query['limit'];
  300. }
  301. return $query;
  302. }
  303. }