PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/kennethreitz-archive/wordpress-skeleton
PHP | 408 lines | 302 code | 78 blank | 28 comment | 82 complexity | 13eb4034268ad50585f34dee8210cdff MD5 | raw file
  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.1.25
  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.1.16' );
  31. class Redirection extends Redirection_Plugin {
  32. var $hasMatched = false;
  33. function Redirection() {
  34. $this->register_plugin('redirection', __FILE__);
  35. if ( is_admin() ) {
  36. $this->add_action( 'admin_menu' );
  37. $this->add_action( 'admin_head' );
  38. $this->add_action( 'wp_print_scripts' );
  39. $this->add_action( 'wp_print_styles' );
  40. $this->add_action( 'admin_head', 'wp_print_styles' );
  41. $this->add_action( 'init', 'inject' );
  42. $this->add_filter( 'contextual_help', 'contextual_help', 10, 2 );
  43. $this->add_action( 'admin_footer' );
  44. $this->add_filter( 'print_scripts_array' );
  45. $this->register_plugin_settings( __FILE__ );
  46. // Ajax functions
  47. if ( defined( 'DOING_AJAX' ) ) {
  48. include_once dirname( __FILE__ ).'/ajax.php';
  49. $this->ajax = new RedirectionAjax();
  50. }
  51. }
  52. else {
  53. $this->update();
  54. // Create a WordPress exporter and let it handle the load
  55. $this->wp = new WordPress_Module();
  56. $this->wp->start();
  57. $this->error = new Error404_Module();
  58. $this->error->start();
  59. }
  60. $this->monitor = new Red_Monitor($this->get_options());
  61. }
  62. function print_scripts_array( $scripts ) {
  63. $farb = array_search( 'farbtastic', $scripts );
  64. if ( $farb && isset( $_GET['page'] ) && $_GET['page'] == 'redirection.php' )
  65. unset( $scripts[$farb] );
  66. return $scripts;
  67. }
  68. function plugin_settings( $links ) {
  69. $settings_link = '<a href="tools.php?page='.basename( __FILE__ ).'">'.__('Settings', 'redirection').'</a>';
  70. array_unshift( $links, $settings_link );
  71. return $links;
  72. }
  73. function contextual_help( $help, $screen ) {
  74. if ( $screen == 'tools_page_redirection' ) {
  75. $help .= '<h5>' . __( 'Redirection Help' ) . '</h5><div class="metabox-prefs">';
  76. $help .= '<a href="http://urbangiraffe.com/plugins/redirection/">'.__( 'Redirection Documentation', 'redirection' ).'</a><br/>';
  77. $help .= '<a href="http://urbangiraffe.com/support/forum/redirection">'.__( 'Redirection Support Forum', 'redirection' ).'</a><br/>';
  78. $help .= '<a href="http://urbangiraffe.com/tracker/projects/redirection/issues?set_filter=1&amp;tracker_id=1">'.__( 'Redirection Bug Tracker', 'redirection' ).'</a><br/>';
  79. $help .= '<a href="http://urbangiraffe.com/plugins/redirection/faq/">'.__( 'Redirection FAQ', 'redirection' ).'</a><br/>';
  80. $help .= __( 'Please read the documentation and FAQ, and check the bug tracker, before asking a question.', 'redirection' );
  81. $help .= '</div>';
  82. }
  83. return $help;
  84. }
  85. function is_25() {
  86. global $wp_version;
  87. if ( version_compare( '2.5', $wp_version ) <= 0 )
  88. return true;
  89. return false;
  90. }
  91. function submenu( $inwrap = false ) {
  92. // Decide what to do
  93. $sub = isset( $_GET['sub'] ) ? $_GET['sub'] : '';
  94. $url = explode( '&', $_SERVER['REQUEST_URI'] );
  95. $url = $url[0];
  96. if ( !$this->is_25() && $inwrap == false )
  97. $this->render_admin( 'submenu', array( 'url' => $url, 'sub' => $sub, 'class' => 'id="subsubmenu"' ) );
  98. elseif ( $this->is_25() && $inwrap == true )
  99. $this->render_admin( 'submenu', array( 'url' => $url, 'sub' => $sub, 'class' => 'class="subsubsub"', 'trail' => ' | ' ) );
  100. return $sub;
  101. }
  102. function version() {
  103. $plugin_data = implode( '', file( __FILE__ ) );
  104. if ( preg_match( '|Version:(.*)|i', $plugin_data, $version ) )
  105. return trim( $version[1] );
  106. return '';
  107. }
  108. function wp_print_scripts() {
  109. if ( strpos( $_SERVER['REQUEST_URI'], 'redirection.php' ) ) {
  110. if (!function_exists ('wp_print_styles')) {
  111. wp_deregister_script ('jquery');
  112. wp_enqueue_script( 'jquery', $this->url ().'/2.3/jquery.js', array(), $this->version () );
  113. wp_enqueue_script( 'jquery-ui-core', $this->url ().'/2.3/ui.core.js', array('jquery'), $this->version () );
  114. wp_enqueue_script( 'jquery-ui-sortable', $this->url ().'/2.3/ui.sortable.js', array('jquery-ui-core'), $this->version () );
  115. }
  116. wp_enqueue_script( 'redirection', $this->url().'/js/redirection.js', array('jquery-form', 'jquery-ui-sortable' ), $this->version() );
  117. }
  118. }
  119. function wp_print_styles() {
  120. if ( strpos( $_SERVER['REQUEST_URI'], 'redirection.php' ) )
  121. echo '<link rel="stylesheet" href="'.$this->url().'/admin.css" type="text/css" media="screen" title="no title" charset="utf-8"/>';
  122. }
  123. function admin_head() {
  124. $sub = isset($_GET['sub']) ? $_GET['sub'] : '';
  125. if ( isset($_GET['page']) && $_GET['page'] == 'redirection.php' )
  126. $this->render_admin( 'head', array( 'type' => $sub == '' ? '301' : $sub ) );
  127. }
  128. function admin_menu() {
  129. add_management_page( __( "Redirection", 'redirection' ), __( "Redirection", 'redirection' ), "administrator", basename( __FILE__ ), array( &$this, "admin_screen" ) );
  130. }
  131. function update() {
  132. $version = get_option( 'redirection_version' );
  133. if ( $version != REDIRECTION_VERSION ) {
  134. include_once dirname( __FILE__ ).'/models/database.php';
  135. $db = new RE_Database();
  136. $db->upgrade( $version, REDIRECTION_VERSION );
  137. }
  138. }
  139. function admin_screen() {
  140. $this->update();
  141. $sub = $this->submenu();
  142. $options = $this->get_options();
  143. if ( isset($_GET['sub']) ) {
  144. if ( $_GET['sub'] == 'log' )
  145. return $this->admin_screen_log();
  146. elseif ( $_GET['sub'] == 'options' )
  147. return $this->admin_screen_options();
  148. elseif ( $_GET['sub'] == 'process' )
  149. return $this->admin_screen_process();
  150. elseif ( $_GET['sub'] == 'groups' )
  151. return $this->admin_groups( isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0);
  152. elseif ( $_GET['sub'] == 'modules' )
  153. return $this->admin_screen_modules();
  154. elseif ( $_GET['sub'] == 'support' )
  155. return $this->render_admin('support');
  156. }
  157. return $this->admin_redirects(isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0);
  158. }
  159. function admin_screen_modules() {
  160. if ( isset( $_POST['create'] ) && check_admin_referer( 'redirection-module_add' ) ) {
  161. $data = stripslashes_deep( $_POST );
  162. if ( ( $module = Red_Module::create( $data ) ) ) {
  163. $moduleid = 0;
  164. if ( isset($_POST['module']))
  165. $moduleid = intval( $_POST['module'] );
  166. $this->render_message( __( 'Your module was successfully created', 'redirection' ) );
  167. Red_Module::flush( $moduleid );
  168. }
  169. else
  170. $this->render_error( __( 'Your module was not created - did you provide a name?', 'redirection' ) );
  171. }
  172. $options = $this->get_options();
  173. $this->render_admin( 'module_list', array( 'modules' => Red_Module::get_all(), 'module_types' => Red_Module::get_types(), 'token' => $options['token'] ) );
  174. }
  175. function get_options() {
  176. $options = get_option( 'redirection_options' );
  177. if ( $options === false )
  178. $options = array();
  179. $defaults = array (
  180. 'lookup' => 'http://urbangiraffe.com/map/?from=redirection&amp;ip=',
  181. 'support' => false,
  182. 'log_redirections' => true,
  183. 'log_404s' => true,
  184. 'expire' => 0,
  185. 'token' => '',
  186. 'monitor_new_posts' => false,
  187. 'monitor_post' => 0
  188. );
  189. foreach ( $defaults AS $key => $value ){
  190. if ( !isset( $options[$key] ) )
  191. $options[$key] = $value;
  192. }
  193. if ($options['lookup'] == 'http://geomaplookup.cinnamonthoughts.org/?ip=' || $options['lookup'] == 'http://geomaplookup.net/?ip=')
  194. $options['lookup'] = 'http://urbangiraffe.com/map/?from=redirection&amp;ip=';
  195. return $options;
  196. }
  197. function inject() {
  198. $options = $this->get_options();
  199. 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' ) ) ) {
  200. include dirname( __FILE__ ).'/models/file_io.php';
  201. $exporter = new Red_FileIO;
  202. if ( $exporter->export( $_GET['sub'] ) )
  203. die();
  204. }
  205. }
  206. function admin_screen_options() {
  207. if ( isset( $_POST['update'] ) && check_admin_referer( 'redirection-update_options' ) ) {
  208. $options['lookup'] = stripslashes( $_POST['lookup'] );
  209. $options['monitor_post'] = stripslashes( $_POST['monitor_post'] );
  210. $options['monitor_category'] = stripslashes( $_POST['monitor_category'] );
  211. $options['auto_target'] = stripslashes( $_POST['auto_target'] );
  212. $options['support'] = isset( $_POST['support'] ) ? true : false;
  213. $options['log_redirections'] = (bool) @ $_POST['log_redirections'];
  214. $options['log_404s'] = (bool) @ $_POST['log_404s'];
  215. $options['monitor_new_posts'] = isset( $_POST['monitor_new_posts'] ) ? true : false;
  216. $options['expire'] = intval( $_POST['expire'] );
  217. $options['token'] = stripslashes( $_POST['token'] );
  218. if ( trim( $options['token'] ) == '' )
  219. $options['token'] = md5( uniqid() );
  220. update_option( 'redirection_options', $options );
  221. $this->render_message( __( 'Your options were updated', 'redirection' ) );
  222. }
  223. elseif ( isset( $_POST['delete'] ) && check_admin_referer( 'redirection-delete_plugin' ) ) {
  224. include dirname( __FILE__ ).'/models/database.php';
  225. $db = new RE_Database;
  226. $db->remove( __FILE__ );
  227. $this->render_message( __( 'Redirection data has been deleted and the plugin disabled', 'redirection' ) );
  228. return;
  229. }
  230. elseif ( isset( $_POST['import'] ) && check_admin_referer( 'redirection-import' ) ) {
  231. include dirname( __FILE__ ).'/models/file_io.php';
  232. $importer = new Red_FileIO;
  233. $count = $importer->import( $_POST['group'], $_FILES['upload'] );
  234. if ( $count > 0 )
  235. $this->render_message( sprintf( __ngettext( '%d redirection was successfully imported','%d redirections were successfully imported', $count, 'redirection' ), $count ) );
  236. else
  237. $this->render_message( __( 'No items were imported', 'redirection' ) );
  238. }
  239. $groups = Red_Group::get_for_select();
  240. $this->render_admin( 'options', array( 'options' => $this->get_options(), 'groups' => $groups ) );
  241. }
  242. function admin_screen_log() {
  243. include dirname( __FILE__ ).'/models/pager.php';
  244. if ( isset( $_POST['deleteall'] ) && check_admin_referer( 'redirection-process_logs' ) ) {
  245. if ( isset( $_GET['module'] ) )
  246. RE_Log::delete_all( array( 'module_id' => intval( $_GET['module'] ) ), new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'created', 'DESC', 'log' ) );
  247. else if (isset($_GET['group']))
  248. RE_Log::delete_all( array( 'group_id' => intval( $_GET['group'] ) ), new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'created', 'DESC', 'log' ) );
  249. else
  250. RE_Log::delete_all( array(), new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'created', 'DESC', 'log' ) );
  251. $this->render_message( __( 'Your logs have been deleted', 'redirection' ) );
  252. }
  253. $pager = new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'created', 'DESC', 'log' );
  254. if ( isset( $_GET['module'] ) )
  255. $logs = RE_Log::get_by_module( $pager, intval( $_GET['module'] ) );
  256. else if (isset($_GET['group']))
  257. $logs = RE_Log::get_by_group( $pager, intval( $_GET['group'] ) );
  258. else if (isset($_GET['redirect']))
  259. $logs = RE_Log::get_by_redirect( $pager, intval( $_GET['redirect'] ) );
  260. else
  261. $logs = RE_Log::get( $pager );
  262. $options = $this->get_options();
  263. $this->render_admin( 'log', array( 'logs' => $logs, 'pager' => $pager, 'lookup' => $options['lookup'] ) );
  264. }
  265. function admin_groups($module) {
  266. include dirname( __FILE__ ).'/models/pager.php';
  267. if (isset( $_POST['add'] ) && check_admin_referer( 'redirection-add_group' ) ) {
  268. if ( Red_Group::create(stripslashes_deep( $_POST ) ) ) {
  269. $this->render_message( __( 'Your group was added successfully', 'redirection' ) );
  270. Red_Module::flush( $module );
  271. }
  272. else
  273. $this->render_error( __( 'Please specify a group name', 'redirection' ) );
  274. }
  275. if ( $module == 0 )
  276. $module = Red_Module::get_first_id();
  277. $pager = new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'position', 'ASC' );
  278. $items = Red_Group::get_all( $module, $pager );
  279. $this->render_admin( 'group_list', array( 'groups' => $items, 'pager' => $pager, 'modules' => Red_Module::get_for_select(), 'module' => Red_Module::get( $module ) ) );
  280. }
  281. function admin_redirects( $group ) {
  282. include dirname( __FILE__ ).'/models/pager.php';
  283. if ( $group == 0 )
  284. $group = Red_Group::get_first_id();
  285. $pager = new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'position', 'ASC' );
  286. $items = Red_Item::get_by_group( $group, $pager );
  287. $this->render_admin( 'item_list', array( 'items' => $items, 'modules' => Red_Group::get_for_select(), 'pager' => $pager, 'group' => Red_Group::get( $group ), 'groups' => Red_Group::get_for_select(), 'date_format' => get_option('date_format')) );
  288. }
  289. /**
  290. * Displays the nice animated support logo
  291. *
  292. * @return void
  293. **/
  294. function admin_footer() {
  295. if ( isset($_GET['page']) && $_GET['page'] == basename( __FILE__ ) ) {
  296. $options = $this->get_options();
  297. if ( !$options['support'] ) {
  298. ?>
  299. <script type="text/javascript" charset="utf-8">
  300. jQuery(function() {
  301. jQuery('#support-annoy').animate( { opacity: 0.2, backgroundColor: 'red' } ).animate( { opacity: 1, backgroundColor: 'yellow' });
  302. });
  303. </script>
  304. <?php
  305. }
  306. }
  307. }
  308. function setMatched( $match ) {
  309. $this->hasMatched = $match;
  310. }
  311. function hasMatched() {
  312. return $this->hasMatched;
  313. }
  314. function locales() {
  315. $locales = array();
  316. $readme = @file_get_contents( dirname( __FILE__ ).'/readme.txt' );
  317. if ( $readme ) {
  318. if ( preg_match_all( '/^\* (.*?) by \[(.*?)\]\((.*?)\)/m', $readme, $matches ) ) {
  319. foreach ( $matches[1] AS $pos => $match ) {
  320. $locales[$match] = '<a href="'.$matches[3][$pos].'">'.$matches[2][$pos].'</a>';
  321. }
  322. }
  323. }
  324. ksort( $locales );
  325. return $locales;
  326. }
  327. }
  328. // Instantiate the plugin
  329. $redirection = new Redirection;