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

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

https://bitbucket.org/MaheshDhaduk/androidmobiles
PHP | 346 lines | 252 code | 70 blank | 24 comment | 63 complexity | 605fdcbe8486bb9a4a73f8613331c15a MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /*
  3. Plugin Name: Redirection
  4. Plugin URI: http://urbangiraffe.com/plugins/redirection/
  5. Description: Manage all your 301 redirects and monitor 404 errors
  6. Version: 2.2.10
  7. Author: John Godley
  8. Author URI: http://urbangiraffe.com
  9. ============================================================================================================
  10. This software is provided "as is" and any express or implied warranties, including, but not limited to, the
  11. implied warranties of merchantibility and fitness for a particular purpose are disclaimed. In no event shall
  12. the copyright owner or contributors be liable for any direct, indirect, incidental, special, exemplary, or
  13. consequential damages(including, but not limited to, procurement of substitute goods or services; loss of
  14. use, data, or profits; or business interruption) however caused and on any theory of liability, whether in
  15. contract, strict liability, or tort(including negligence or otherwise) arising in any way out of the use of
  16. this software, even if advised of the possibility of such damage.
  17. For full license details see license.txt
  18. ============================================================================================================
  19. */
  20. include dirname( __FILE__ ).'/plugin.php';
  21. include dirname( __FILE__ ).'/models/redirect.php';
  22. include dirname( __FILE__ ).'/models/match.php';
  23. include dirname( __FILE__ ).'/models/log.php';
  24. include dirname( __FILE__ ).'/models/group.php';
  25. include dirname( __FILE__ ).'/models/module.php';
  26. include dirname( __FILE__ ).'/models/action.php';
  27. include dirname( __FILE__ ).'/models/monitor.php';
  28. include dirname( __FILE__ ).'/modules/wordpress.php';
  29. include dirname( __FILE__ ).'/modules/404.php';
  30. define( 'REDIRECTION_VERSION', '2.2' );
  31. if ( class_exists( 'Redirection' ) )
  32. return;
  33. class Redirection extends Redirection_Plugin {
  34. var $hasMatched = false;
  35. function Redirection() {
  36. $this->register_plugin( 'redirection', __FILE__ );
  37. if ( is_admin() ) {
  38. $this->add_action( 'admin_menu' );
  39. $this->add_action( 'load-tools_page_redirection', 'redirection_head' );
  40. $this->add_action( 'init', 'inject' );
  41. $this->register_activation( __FILE__ );
  42. $this->register_plugin_settings( __FILE__ );
  43. // Ajax functions
  44. if ( defined( 'DOING_AJAX' ) ) {
  45. include_once dirname( __FILE__ ).'/ajax.php';
  46. $this->ajax = new RedirectionAjax();
  47. }
  48. }
  49. else {
  50. $this->update();
  51. // Create a WordPress exporter and let it handle the load
  52. $this->wp = new WordPress_Module();
  53. $this->wp->start();
  54. $this->error = new Error404_Module();
  55. $this->error->start();
  56. }
  57. $this->monitor = new Red_Monitor( $this->get_options() );
  58. }
  59. function update() {
  60. $version = get_option( 'redirection_version' );
  61. if ( $version != REDIRECTION_VERSION ) {
  62. include_once dirname( __FILE__ ).'/models/database.php';
  63. $db = new RE_Database();
  64. return $db->upgrade( $version, REDIRECTION_VERSION );
  65. }
  66. return true;
  67. }
  68. function activate() {
  69. if ( $this->update() === false ) {
  70. $db = new RE_Database();
  71. $db->remove( $version, REDIRECTION_VERSION );
  72. exit();
  73. }
  74. }
  75. function plugin_settings( $links ) {
  76. $settings_link = '<a href="tools.php?page='.basename( __FILE__ ).'">'.__( 'Settings', 'redirection' ).'</a>';
  77. array_unshift( $links, $settings_link );
  78. return $links;
  79. }
  80. function version() {
  81. $plugin_data = implode( '', file( __FILE__ ) );
  82. if ( preg_match( '|Version:(.*)|i', $plugin_data, $version ) )
  83. return trim( $version[1] );
  84. return '';
  85. }
  86. function redirection_head() {
  87. wp_enqueue_script( 'redirection', plugin_dir_url( __FILE__ ).'js/redirection.js', array( 'jquery-form', 'jquery-ui-sortable' ), $this->version() );
  88. wp_enqueue_style( 'redirection', plugin_dir_url( __FILE__ ).'admin.css', $this->version() );
  89. wp_localize_script( 'redirection', 'Redirectioni10n', array(
  90. 'please_wait' => __( 'Please wait...', 'redirection' ),
  91. 'type' => 1,
  92. 'progress' => '<img src="'.plugin_dir_url( __FILE__ ).'/images/progress.gif" alt="loading" width="50" height="16"/>',
  93. 'are_you_sure' => __( 'Are you sure?', 'redirection' ),
  94. 'none_select' => __( 'No items have been selected', 'redirection' )
  95. ) );
  96. }
  97. function admin_menu() {
  98. add_management_page( __( "Redirection", 'redirection' ), __( "Redirection", 'redirection' ), "administrator", basename( __FILE__ ), array( &$this, "admin_screen" ) );
  99. }
  100. function admin_screen() {
  101. $this->update();
  102. // Decide what to do
  103. $sub = isset( $_GET['sub'] ) ? $_GET['sub'] : '';
  104. $options = $this->get_options();
  105. if ( isset($_GET['sub']) ) {
  106. if ( $_GET['sub'] == 'log' )
  107. return $this->admin_screen_log();
  108. elseif ( $_GET['sub'] == 'options' )
  109. return $this->admin_screen_options();
  110. elseif ( $_GET['sub'] == 'process' )
  111. return $this->admin_screen_process();
  112. elseif ( $_GET['sub'] == 'groups' )
  113. return $this->admin_groups( isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0 );
  114. elseif ( $_GET['sub'] == 'modules' )
  115. return $this->admin_screen_modules();
  116. elseif ( $_GET['sub'] == 'support' )
  117. return $this->render_admin('support');
  118. }
  119. return $this->admin_redirects( isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0 );
  120. }
  121. function admin_screen_modules() {
  122. if ( isset( $_POST['create'] ) && check_admin_referer( 'redirection-module_add' ) ) {
  123. $data = stripslashes_deep( $_POST );
  124. if ( ( $module = Red_Module::create( $data ) ) ) {
  125. $moduleid = 0;
  126. if ( isset( $_POST['module'] ) )
  127. $moduleid = intval( $_POST['module'] );
  128. $this->render_message( __( 'Your module was successfully created', 'redirection' ) );
  129. Red_Module::flush( $moduleid );
  130. }
  131. else
  132. $this->render_error( __( 'Your module was not created - did you provide a name?', 'redirection' ) );
  133. }
  134. $options = $this->get_options();
  135. $this->render_admin( 'module_list', array( 'modules' => Red_Module::get_all(), 'module_types' => Red_Module::get_types(), 'token' => $options['token'] ) );
  136. }
  137. function get_options() {
  138. $options = get_option( 'redirection_options' );
  139. if ( $options === false )
  140. $options = array();
  141. $defaults = array (
  142. 'lookup' => 'http://urbangiraffe.com/map/?from=redirection&amp;ip=',
  143. 'support' => false,
  144. 'log_redirections' => true,
  145. 'log_404s' => true,
  146. 'expire' => 0,
  147. 'token' => '',
  148. 'monitor_new_posts' => false,
  149. 'monitor_post' => 0,
  150. 'auto_target' => '',
  151. );
  152. foreach ( $defaults AS $key => $value ) {
  153. if ( !isset( $options[$key] ) )
  154. $options[$key] = $value;
  155. }
  156. if ( $options['lookup'] == 'http://geomaplookup.cinnamonthoughts.org/?ip=' || $options['lookup'] == 'http://geomaplookup.net/?ip=' )
  157. $options['lookup'] = 'http://urbangiraffe.com/map/?from=redirection&amp;ip=';
  158. return $options;
  159. }
  160. function inject() {
  161. $options = $this->get_options();
  162. if ( isset($_GET['token'] ) && isset( $_GET['page'] ) && isset( $_GET['sub'] ) && $_GET['token'] == $options['token'] && $_GET['page'] == 'redirection.php' && in_array( $_GET['sub'], array( 'rss', 'xml', 'csv', 'apache' ) ) ) {
  163. include dirname( __FILE__ ).'/models/file_io.php';
  164. $exporter = new Red_FileIO;
  165. if ( $exporter->export( $_GET['sub'] ) )
  166. die();
  167. }
  168. }
  169. function admin_screen_options() {
  170. if ( isset( $_POST['update'] ) && check_admin_referer( 'redirection-update_options' ) ) {
  171. $options['lookup'] = stripslashes( $_POST['lookup'] );
  172. $options['monitor_post'] = stripslashes( $_POST['monitor_post'] );
  173. // $options['monitor_category'] = stripslashes( $_POST['monitor_category'] );
  174. $options['auto_target'] = stripslashes( $_POST['auto_target'] );
  175. $options['support'] = isset( $_POST['support'] ) ? true : false;
  176. $options['log_redirections'] = (bool) @ $_POST['log_redirections'];
  177. $options['log_404s'] = (bool) @ $_POST['log_404s'];
  178. $options['monitor_new_posts'] = isset( $_POST['monitor_new_posts'] ) ? true : false;
  179. $options['expire'] = intval( $_POST['expire'] );
  180. $options['token'] = stripslashes( $_POST['token'] );
  181. if ( trim( $options['token'] ) == '' )
  182. $options['token'] = md5( uniqid() );
  183. update_option( 'redirection_options', $options );
  184. $this->render_message( __( 'Your options were updated', 'redirection' ) );
  185. }
  186. elseif ( isset( $_POST['delete'] ) && check_admin_referer( 'redirection-delete_plugin' ) ) {
  187. include dirname( __FILE__ ).'/models/database.php';
  188. $db = new RE_Database;
  189. $db->remove( __FILE__ );
  190. $this->render_message( __( 'Redirection data has been deleted and the plugin disabled', 'redirection' ) );
  191. return;
  192. }
  193. elseif ( isset( $_POST['import'] ) && check_admin_referer( 'redirection-import' ) ) {
  194. include dirname( __FILE__ ).'/models/file_io.php';
  195. $importer = new Red_FileIO;
  196. $count = $importer->import( $_POST['group'], $_FILES['upload'] );
  197. if ( $count > 0 )
  198. $this->render_message( sprintf( _n( '%d redirection was successfully imported','%d redirections were successfully imported', $count, 'redirection' ), $count ) );
  199. else
  200. $this->render_message( __( 'No items were imported', 'redirection' ) );
  201. }
  202. $groups = Red_Group::get_for_select();
  203. $this->render_admin( 'options', array( 'options' => $this->get_options(), 'groups' => $groups ) );
  204. }
  205. function admin_screen_log() {
  206. include dirname( __FILE__ ).'/models/pager.php';
  207. if ( isset( $_POST['deleteall'] ) && check_admin_referer( 'redirection-process_logs' ) ) {
  208. if ( isset( $_GET['module'] ) )
  209. RE_Log::delete_all( array( 'module_id' => intval( $_GET['module'] ) ), new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'created', 'DESC', 'log' ) );
  210. else if (isset($_GET['group']))
  211. RE_Log::delete_all( array( 'group_id' => intval( $_GET['group'] ) ), new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'created', 'DESC', 'log' ) );
  212. else
  213. RE_Log::delete_all( array(), new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'created', 'DESC', 'log' ) );
  214. $this->render_message( __( 'Your logs have been deleted', 'redirection' ) );
  215. }
  216. $pager = new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'created', 'DESC', 'log' );
  217. if ( isset( $_GET['module'] ) )
  218. $logs = RE_Log::get_by_module( $pager, intval( $_GET['module'] ) );
  219. else if (isset($_GET['group']))
  220. $logs = RE_Log::get_by_group( $pager, intval( $_GET['group'] ) );
  221. else if (isset($_GET['redirect']))
  222. $logs = RE_Log::get_by_redirect( $pager, intval( $_GET['redirect'] ) );
  223. else
  224. $logs = RE_Log::get( $pager );
  225. $options = $this->get_options();
  226. $this->render_admin( 'log', array( 'logs' => $logs, 'pager' => $pager, 'lookup' => $options['lookup'] ) );
  227. }
  228. function admin_groups( $module ) {
  229. include dirname( __FILE__ ).'/models/pager.php';
  230. if ( isset( $_POST['add'] ) && check_admin_referer( 'redirection-add_group' ) ) {
  231. if ( Red_Group::create( stripslashes_deep( $_POST ) ) ) {
  232. $this->render_message( __( 'Your group was added successfully', 'redirection' ) );
  233. Red_Module::flush( $module );
  234. }
  235. else
  236. $this->render_error( __( 'Please specify a group name', 'redirection' ) );
  237. }
  238. if ( $module == 0 )
  239. $module = Red_Module::get_first_id();
  240. $pager = new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'position', 'ASC' );
  241. $items = Red_Group::get_all( $module, $pager );
  242. $this->render_admin( 'group_list', array( 'groups' => $items, 'pager' => $pager, 'modules' => Red_Module::get_for_select(), 'module' => Red_Module::get( $module ) ) );
  243. }
  244. function admin_redirects( $group ) {
  245. include dirname( __FILE__ ).'/models/pager.php';
  246. if ( $group == 0 )
  247. $group = Red_Group::get_first_id();
  248. $pager = new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'position', 'ASC' );
  249. $items = Red_Item::get_by_group( $group, $pager );
  250. $this->render_admin( 'item_list', array( 'items' => $items, 'pager' => $pager, 'group' => Red_Group::get( $group ), 'groups' => Red_Group::get_for_select(), 'date_format' => get_option( 'date_format' ) ) );
  251. }
  252. function setMatched( $match ) {
  253. $this->hasMatched = $match;
  254. }
  255. function hasMatched() {
  256. return $this->hasMatched;
  257. }
  258. function locales() {
  259. $locales = array();
  260. if ( file_exists( dirname( __FILE__ ).'/readme.txt' ) ) {
  261. $readme = file_get_contents( dirname( __FILE__ ).'/readme.txt' );
  262. $start = strpos( $readme, __( 'Redirection is available in' ) );
  263. $end = strpos( $readme, '==', $start );
  264. if ( $start !== false && $end !== false ) {
  265. if ( preg_match_all( '/^\* (.*?) by (.*?)/m', substr( $readme, $start, $end ), $matches ) > 0 ) {
  266. $locales = $matches[1];
  267. }
  268. }
  269. sort( $locales );
  270. }
  271. return $locales;
  272. }
  273. }
  274. // Instantiate the plugin
  275. $redirection = new Redirection;