PageRenderTime 28ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/blueprintmrk/bladencountyrecords
PHP | 409 lines | 288 code | 90 blank | 31 comment | 72 complexity | 24f8d4df307a1dd74cd44f48605d198a MD5 | raw file
  1. <?php
  2. /**
  3. * Redirection
  4. *
  5. * @package Redirection
  6. * @author John Godley
  7. * @copyright Copyright( C) John Godley
  8. **/
  9. /*
  10. ============================================================================================================
  11. This software is provided "as is" and any express or implied warranties, including, but not limited to, the
  12. implied warranties of merchantibility and fitness for a particular purpose are disclaimed. In no event shall
  13. the copyright owner or contributors be liable for any direct, indirect, incidental, special, exemplary, or
  14. consequential damages( including, but not limited to, procurement of substitute goods or services; loss of
  15. use, data, or profits; or business interruption) however caused and on any theory of liability, whether in
  16. contract, strict liability, or tort( including negligence or otherwise) arising in any way out of the use of
  17. this software, even if advised of the possibility of such damage.
  18. For full license details see license.txt
  19. ============================================================================================================ */
  20. class Red_Item {
  21. var $id = null;
  22. var $url = null;
  23. var $regex = false;
  24. var $action_data = null;
  25. var $last_access = null;
  26. var $last_count = 0;
  27. var $tracking = true;
  28. function Red_Item( $values, $type = '', $match = '' ) {
  29. if ( is_object( $values ) ) {
  30. foreach ( $values AS $key => $value ) {
  31. $this->$key = $value;
  32. }
  33. if ( $this->match_type ) {
  34. $this->match = Red_Match::create( $this->match_type, $this->action_data);
  35. $this->match->id = $this->id;
  36. $this->match->action_code = $this->action_code;
  37. }
  38. if ( $this->action_type ) {
  39. $this->action = Red_Action::create( $this->action_type, $this->action_code);
  40. $this->match->action = $this->action;
  41. }
  42. else
  43. $this->action = Red_Action::create( 'nothing', 0 );
  44. if ( $this->last_access == '0000-00-00 00:00:00' )
  45. $this->last_access = 0;
  46. else
  47. $this->last_access = mysql2date( 'U', $this->last_access);
  48. }
  49. else {
  50. $this->url = $values;
  51. $this->type = $type;
  52. $this->match = $match;
  53. }
  54. }
  55. function get_all_for_module( $module ) {
  56. global $wpdb;
  57. $sql = "SELECT @redirection_items.*,@redirection_groups.tracking FROM @redirection_items INNER JOIN @redirection_groups ON @redirection_groups.id=@redirection_items.group_id AND @redirection_groups.status='enabled' AND @redirection_groups.module_id='$module' WHERE @redirection_items.status='enabled' ORDER BY @redirection_groups.position,@redirection_items.position";
  58. $sql = str_replace( '@', $wpdb->prefix, $sql );
  59. $rows = $wpdb->get_results( $sql );
  60. $items = array();
  61. if ( count( $rows) > 0 ) {
  62. foreach ( $rows AS $row ) {
  63. $items[] = new Red_Item( $row );
  64. }
  65. }
  66. return $items;
  67. }
  68. function exists( $url ) {
  69. global $wpdb;
  70. if ( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$wpdb->prefix}redirection_items WHERE url=%s", $url ) ) > 0 )
  71. return true;
  72. return false;
  73. }
  74. function get_for_url( $url, $type ) {
  75. global $wpdb;
  76. $sql = $wpdb->prepare( "SELECT @redirection_items.*,@redirection_groups.tracking,@redirection_groups.position AS group_pos,@redirection_modules.id AS module_id FROM @redirection_items INNER JOIN @redirection_groups ON @redirection_groups.id=@redirection_items.group_id AND @redirection_groups.status='enabled' INNER JOIN @redirection_modules ON @redirection_modules.id=@redirection_groups.module_id AND @redirection_modules.type=%s WHERE( @redirection_items.regex=1 OR @redirection_items.url=%s)", $type, $url );
  77. $sql = str_replace( '@', $wpdb->prefix, $sql);
  78. $rows = $wpdb->get_results( $sql ) ;
  79. $items = array();
  80. if ( count( $rows ) > 0 ) {
  81. foreach ( $rows AS $row ) {
  82. $items[$row->group_pos.'.'.$row->position] = new Red_Item( $row );
  83. }
  84. }
  85. // Sort it in PHP
  86. ksort( $items );
  87. $items = array_values( $items );
  88. return $items;
  89. }
  90. function get_by_module( &$pager, $module ) {
  91. global $wpdb;
  92. $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM {$wpdb->prefix}redirection_items INNER JOIN {$wpdb->prefix}redirection_groups ON {$wpdb->prefix}redirection_groups.id={$wpdb->prefix}redirection_items.group_id";
  93. $sql .= $pager->to_limits( "{$wpdb->prefix}redirection_groups.module_id=".$module, array( 'url', 'action_data' ));
  94. $rows = $wpdb->get_results( $sql );
  95. $pager->set_total( $wpdb->get_var( "SELECT FOUND_ROWS()" ));
  96. $items = array();
  97. if ( count( $rows) > 0)
  98. {
  99. foreach( $rows AS $row)
  100. $items[] = new Red_Item( $row);
  101. }
  102. return $items;
  103. }
  104. /**
  105. * Get redirection items in a group
  106. */
  107. function get_by_group( $group, &$pager ) {
  108. global $wpdb;
  109. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT SQL_CALC_FOUND_ROWS * FROM {$wpdb->prefix}redirection_items ".$pager->to_limits( 'group_id='.$group, array( 'url', 'action_data' ) ) ) );
  110. $pager->set_total( $wpdb->get_var( "SELECT FOUND_ROWS()" ));
  111. $items = array();
  112. if ( count( $rows ) > 0 ) {
  113. foreach ( $rows AS $row ) {
  114. $items[] = new Red_Item( $row );
  115. }
  116. }
  117. return $items;
  118. }
  119. function get_by_id( $id ) {
  120. global $wpdb;
  121. $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}redirection_items WHERE id=%d", $id ) );
  122. if ( $row )
  123. return new Red_Item( $row );
  124. return false;
  125. }
  126. function auto_generate() {
  127. global $redirection;
  128. $options = $redirection->get_options();
  129. $id = time();
  130. $url = $options['auto_target'];
  131. $url = str_replace( '$dec$', $id, $url );
  132. $url = str_replace( '$hex$', sprintf( '%x', $id), $url );
  133. return $url;
  134. }
  135. function create( $details ) {
  136. global $wpdb;
  137. // Auto generate URLs
  138. if ( $details['source'] == '' )
  139. $details['source'] = Red_Item::auto_generate();
  140. if ( $details['target'] == '' )
  141. $details['target'] = Red_Item::auto_generate();
  142. // Make sure we don't redirect to ourself
  143. if ( $details['source'] == $details['target'] )
  144. $details['target'] .= '-1';
  145. $matcher = Red_Match::create( $details['match'] );
  146. $group_id = intval( $details['group'] );
  147. if ( $group_id > 0 && $matcher ) {
  148. $regex = ( isset( $details['regex']) && $details['regex'] != false) ? true : false;
  149. $position = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$wpdb->prefix}redirection_items WHERE group_id=%d", $group_id ) );
  150. $action = $details['red_action'];
  151. $action_code = 0;
  152. if ( $action == 'url' || $action == 'random' )
  153. $action_code = 301;
  154. elseif ( $action == 'error' )
  155. $action_code = 404;
  156. if ( isset( $details['action_code'] ) )
  157. $action_code = intval( $details['action_code'] );
  158. $data = array(
  159. 'url' => Red_Item::sanitize_url( $details['source'], $regex),
  160. 'action_type' => $details['red_action'],
  161. 'regex' => $regex,
  162. 'position' => $position,
  163. 'match_type' => $details['match'],
  164. 'action_data' => $matcher->data( $details ),
  165. 'action_code' => $action_code,
  166. 'last_access' => 0,
  167. 'group_id' => $group_id
  168. );
  169. $wpdb->insert( $wpdb->prefix.'redirection_items', $data );
  170. $group = Red_Group::get( $group_id );
  171. Red_Module::flush( $group->module_id );
  172. return Red_Item::get_by_id( $wpdb->insert_id );
  173. }
  174. return false;
  175. }
  176. function delete_by_group( $group ) {
  177. global $wpdb;
  178. RE_Log::delete_for_group( $group);
  179. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_items WHERE group_id=%d", $group ) );
  180. $group = Red_Group::get( $group_id );
  181. Red_Module::flush( $group->module_id );
  182. }
  183. function delete( $id ) {
  184. global $wpdb;
  185. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_items WHERE id=%d", $id ) );
  186. RE_Log::delete_for_id( $id );
  187. // Reorder all elements
  188. $rows = $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}redirection_items ORDER BY position" );
  189. if ( count( $rows) > 0 ) {
  190. foreach ( $rows AS $pos => $row ) {
  191. $wpdb->update( $wpdb->prefix.'redirection_items', array( 'position' => $pos ), array( 'id' => $row->id ) );
  192. }
  193. }
  194. }
  195. function sanitize_url( $url, $regex ) {
  196. // Make sure that the old URL is relative
  197. $url = preg_replace( '@^https?://(.*?)/@', '/', $url );
  198. $url = preg_replace( '@^https?://(.*?)$@', '/', $url );
  199. if ( substr( $url, 0, 1) != '/' && $regex == false )
  200. $url = '/'.$url;
  201. return $url;
  202. }
  203. function update( $details ) {
  204. if ( strlen( $details['old'] ) > 0 ) {
  205. global $wpdb;
  206. $this->regex = isset( $details['regex'] ) ? true : false;
  207. $this->url = $this->sanitize_url( $details['old'], $this->regex );
  208. $this->title = $details['title'];
  209. $data = $this->match->data( $details );
  210. $this->action_code = 0;
  211. if ( isset( $details['action_code'] ) )
  212. $this->action_code = intval( $details['action_code'] );
  213. if ( isset( $details['group_id'] ) )
  214. $this->group_id = intval( $details['group_id'] );
  215. // Save this
  216. global $wpdb;
  217. $wpdb->update( $wpdb->prefix.'redirection_items', array( 'url' => $this->url, 'regex' => $this->regex, 'action_code' => $this->action_code, 'action_data' => $data, 'group_id' => $this->group_id, 'title' => $this->title ), array( 'id' => $this->id ) );
  218. $group = Red_Group::get( $this->group_id );
  219. if ( $group )
  220. Red_Module::flush( $group->module_id );
  221. }
  222. }
  223. function save_order( $items, $start ) {
  224. global $wpdb;
  225. foreach ( $items AS $pos => $id ) {
  226. $wpdb->update( $wpdb->prefix.'redirection_items', array( 'position' => $pos + $start ), array( 'id' => $id ) );
  227. }
  228. $item = Red_Item::get_by_id( $id );
  229. $group = Red_Group::get( $item->group_id );
  230. if ( $group )
  231. Red_Module::flush( $group->module_id );
  232. }
  233. function matches( $url ) {
  234. $this->url = str_replace( ' ', '%20', $this->url );
  235. $matches = false;
  236. // Check if we match the URL
  237. if ( ( $this->regex == false && ( $this->url == $url || $this->url == rtrim( $url, '/' ) || $this->url == urldecode( $url ) ) ) ||( $this->regex == true && @preg_match( '@'.str_replace( '@', '\\@', $this->url).'@', $url, $matches) > 0) ||( $this->regex == true && @preg_match( '@'.str_replace( '@', '\\@', $this->url).'@', urldecode( $url ), $matches) > 0) ) {
  238. // Check if our match wants this URL
  239. $target = $this->match->get_target( $url, $this->url, $this->regex);
  240. if ( $target ) {
  241. $target = $this->replaceSpecialTags( $target );
  242. $this->visit( $url, $target );
  243. if ( $this->status == 'enabled' )
  244. return $this->action->process_before( $this->action_code, $target );
  245. }
  246. }
  247. return false;
  248. }
  249. function replaceSpecialTags( $target ) {
  250. if ( is_numeric( $target ) )
  251. $target = get_permalink( $target );
  252. else {
  253. $user = wp_get_current_user();
  254. if ( !empty( $user ) ) {
  255. $target = str_replace( '%userid%', $user->ID, $target );
  256. $target = str_replace( '%userlogin%', isset( $user->user_login ) ? $user->user_login : '', $target );
  257. $target = str_replace( '%userurl%', isset( $user->user_url ) ? $user->user_url : '', $target );
  258. }
  259. }
  260. return $target;
  261. }
  262. function visit( $url, $target ) {
  263. if ( $this->tracking && $this->id ) {
  264. global $wpdb, $redirection;
  265. // Update the counters
  266. $count = $this->last_count + 1;
  267. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}redirection_items SET last_count=%d, last_access=NOW() WHERE id=%d", $count, $this->id ) );
  268. if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
  269. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  270. elseif ( isset( $_SERVER['REMOTE_ADDR'] ) )
  271. $ip = $_SERVER['REMOTE_ADDR'];
  272. $options = $redirection->get_options();
  273. if ( $options['log_redirections'])
  274. $log = RE_Log::create( $url, $target, $_SERVER['HTTP_USER_AGENT'], $ip, isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '', $this->id, $this->module_id, $this->group_id);
  275. }
  276. }
  277. function reset() {
  278. global $wpdb;
  279. $this->last_count = 0;
  280. $this->last_access = '0000-00-00 00:00:00';
  281. $wpdb->update( $wpdb->prefix.'redirection_items', array( 'last_count' => 0, 'last_access' => $this->last_access ), array( 'id' => $this->id ) );
  282. RE_Log::delete_for_id( $this->id );
  283. }
  284. function show_url( $url ) {
  285. return implode( '&#8203;/', explode( '/', $url ) );
  286. }
  287. function move_to( $group ) {
  288. global $wpdb;
  289. $wpdb->update( $wpdb->prefix.'redirection_items', array( 'group_id' => $group ), array( 'id' => $this->id ) );
  290. }
  291. function toggle_status() {
  292. global $wpdb;
  293. $this->status = ( $this->status == 'enabled' ) ? 'disabled' : 'enabled';
  294. $wpdb->update( $wpdb->prefix.'redirection_items', array( 'status' => $this->status ), array( 'id' => $this->id ) );
  295. }
  296. function actions( $action = '' ) {
  297. $actions = array(
  298. 'url' => __( 'Redirect to URL', 'redirection' ),
  299. 'random' => __( 'Redirect to random post', 'redirection' ),
  300. 'pass' => __( 'Pass-through', 'redirection' ),
  301. 'error' => __( 'Error (404)', 'redirection' ),
  302. 'nothing' => __( 'Do nothing', 'redirection' ),
  303. );
  304. if ( $action )
  305. return $actions[$action];
  306. return $actions;
  307. }
  308. function match_name() {
  309. return $this->match->match_name();
  310. }
  311. function type() {
  312. if ( ( $this->action_type == 'url' || $this->action_type == 'error' || $this->action_type == 'random' ) && $this->action_code > 0 )
  313. return $this->action_code;
  314. else if ( $this->action_type == 'pass' )
  315. return 'pass';
  316. return '&mdash;';
  317. }
  318. }