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

https://gitlab.com/iamgraeme/royalmile · PHP · 244 lines · 185 code · 59 blank · 0 comment · 17 complexity · 31bcfbc98182c65b43321d86a8b160cd 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. return false;
  23. }
  24. static function create( $url, $target, $agent, $ip, $referrer, $extra = array()) {
  25. global $wpdb, $redirection;
  26. $insert = array(
  27. 'url' => urldecode( $url ),
  28. 'created' => current_time( 'mysql' ),
  29. 'ip' => $ip,
  30. );
  31. if ( ! empty( $agent ) )
  32. $insert['agent'] = $agent;
  33. if ( ! empty( $referrer ) )
  34. $insert['referrer'] = $referrer;
  35. $insert['sent_to'] = $target;
  36. $insert['redirection_id'] = isset( $extra['redirect_id'] ) ? $extra['redirect_id'] : 0;
  37. $insert['group_id'] = isset( $extra['group_id'] ) ? $extra['group_id'] : 0;
  38. $insert = apply_filters( 'redirection_log_data', $insert );
  39. do_action( 'redirection_log', $insert );
  40. $wpdb->insert( $wpdb->prefix.'redirection_logs', $insert );
  41. }
  42. static function show_url( $url ) {
  43. return implode( '&#8203;/', explode( '/', substr( $url, 0, 80 ) ) ).( strlen( $url ) > 80 ? '...' : '' );
  44. }
  45. static function delete( $id ) {
  46. global $wpdb;
  47. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE id=%d", $id ) );
  48. }
  49. static function delete_for_id( $id ) {
  50. global $wpdb;
  51. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE redirection_id=%d", $id ) );
  52. }
  53. static function delete_for_group( $id ) {
  54. global $wpdb;
  55. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE group_id=%d", $id ) );
  56. }
  57. static function delete_all( $type = 'all', $id = 0 ) {
  58. global $wpdb;
  59. $where = array();
  60. if ( $type === 'module' )
  61. $where[] = $wpdb->prepare( 'module_id=%d', $id );
  62. elseif ( $type === 'group' )
  63. $where[] = $wpdb->prepare( 'group_id=%d AND redirection_id IS NOT NULL', $id );
  64. elseif ( $type === 'redirect' )
  65. $where[] = $wpdb->prepare( 'redirection_id=%d', $id );
  66. if ( isset( $_REQUEST['s'] ) )
  67. $where[] = $wpdb->prepare( 'url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' );
  68. $where_cond = '';
  69. if ( count( $where ) > 0 )
  70. $where_cond = ' WHERE '.implode( ' AND ', $where );
  71. $wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_logs ".$where_cond );
  72. }
  73. static function export_to_csv() {
  74. global $wpdb;
  75. $filename = 'redirection-log-'.date_i18n( get_option( 'date_format' ) ).'.csv';
  76. header( 'Content-Type: text/csv' );
  77. header( 'Cache-Control: no-cache, must-revalidate' );
  78. header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
  79. header( 'Content-Disposition: attachment; filename="'.$filename.'"' );
  80. $stdout = fopen( 'php://output', 'w' );
  81. fputcsv( $stdout, array( 'date', 'source', 'target', 'ip', 'referrer', 'agent' ) );
  82. $extra = '';
  83. $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_logs";
  84. if ( isset( $_REQUEST['s'] ) )
  85. $extra = $wpdb->prepare( ' WHERE url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' );
  86. $total_items = $wpdb->get_var( $sql.$extra );
  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. class RE_404 {
  108. public $id;
  109. public $created;
  110. public $url;
  111. public $agent;
  112. public $referrer;
  113. public $ip;
  114. function __construct( $values ) {
  115. foreach ( $values as $key => $value ) {
  116. $this->$key = $value;
  117. }
  118. $this->created = mysql2date( 'U', $this->created );
  119. }
  120. static function get_by_id( $id ) {
  121. global $wpdb;
  122. $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_404 WHERE id=%d", $id ) );
  123. if ( $row )
  124. return new RE_404( $row );
  125. return false;
  126. }
  127. static function create( $url, $agent, $ip, $referrer ) {
  128. global $wpdb, $redirection;
  129. $insert = array(
  130. 'url' => urldecode( $url ),
  131. 'created' => current_time( 'mysql' ),
  132. 'ip' => ip2long( $ip ),
  133. );
  134. if ( ! empty( $agent ) )
  135. $insert['agent'] = $agent;
  136. if ( ! empty( $referrer ) )
  137. $insert['referrer'] = $referrer;
  138. $wpdb->insert( $wpdb->prefix.'redirection_404', $insert );
  139. }
  140. static function delete( $id ) {
  141. global $wpdb;
  142. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_404 WHERE id=%d", $id ) );
  143. }
  144. static function delete_all() {
  145. global $wpdb;
  146. $where = array();
  147. if ( isset( $_REQUEST['s'] ) )
  148. $where[] = $wpdb->prepare( 'url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' );
  149. $where_cond = '';
  150. if ( count( $where ) > 0 )
  151. $where_cond = ' WHERE '.implode( ' AND ', $where );
  152. $wpdb->query( "DELETE FROM {$wpdb->prefix}redirection_404 ".$where_cond );
  153. }
  154. static function export_to_csv() {
  155. global $wpdb;
  156. $filename = 'redirection-log-'.date_i18n( get_option( 'date_format' ) ).'.csv';
  157. header( 'Content-Type: text/csv' );
  158. header( 'Cache-Control: no-cache, must-revalidate' );
  159. header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
  160. header( 'Content-Disposition: attachment; filename="'.$filename.'"' );
  161. $stdout = fopen( 'php://output', 'w' );
  162. fputcsv( $stdout, array( 'date', 'source', 'ip', 'referrer' ) );
  163. $extra = '';
  164. $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_404";
  165. if ( isset( $_REQUEST['s'] ) )
  166. $extra = $wpdb->prepare( ' WHERE url LIKE %s', '%'.$wpdb->esc_like( $_REQUEST['s'] ).'%' );
  167. $total_items = $wpdb->get_var( $sql.$extra );
  168. $exported = 0;
  169. while ( $exported < $total_items ) {
  170. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_404 LIMIT %d,%d", $exported, 100 ) );
  171. $exported += count( $rows );
  172. foreach ( $rows as $row ) {
  173. $csv = array(
  174. $row->created,
  175. $row->url,
  176. long2ip( $row->ip ),
  177. $row->referrer,
  178. );
  179. fputcsv( $stdout, $csv );
  180. }
  181. if ( count( $rows ) < 100 )
  182. break;
  183. }
  184. }
  185. }