PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/davidsevcik/ppg_website
PHP | 410 lines | 303 code | 79 blank | 28 comment | 82 complexity | ea5a3e6b36cb292f6439fa4ea436b2a1 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.22
  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.15' );
  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. $_POST = stripslashes_deep( $_POST );
  162. if ( ( $module = Red_Module::create( $_POST ) ) ) {
  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. $_POST = stripslashes_deep( $_POST );
  209. $options['lookup'] = $_POST['lookup'];
  210. $options['monitor_post'] = $_POST['monitor_post'];
  211. $options['monitor_category'] = $_POST['monitor_category'];
  212. $options['auto_target'] = $_POST['auto_target'];
  213. $options['support'] = isset( $_POST['support'] ) ? true : false;
  214. $options['log_redirections'] = (bool) @ $_POST['log_redirections'];
  215. $options['log_404s'] = (bool) @ $_POST['log_404s'];
  216. $options['monitor_new_posts'] = isset( $_POST['monitor_new_posts'] ) ? true : false;
  217. $options['expire'] = intval( $_POST['expire'] );
  218. $options['token'] = $_POST['token'];
  219. if ( trim( $options['token'] ) == '' )
  220. $options['token'] = md5( uniqid() );
  221. update_option( 'redirection_options', $options );
  222. $this->render_message( __( 'Your options were updated', 'redirection' ) );
  223. }
  224. elseif ( isset( $_POST['delete'] ) && check_admin_referer( 'redirection-delete_plugin' ) ) {
  225. include dirname( __FILE__ ).'/models/database.php';
  226. $db = new RE_Database;
  227. $db->remove( __FILE__ );
  228. $this->render_message( __( 'Redirection data has been deleted and the plugin disabled', 'redirection' ) );
  229. return;
  230. }
  231. elseif ( isset( $_POST['import'] ) && check_admin_referer( 'redirection-import' ) ) {
  232. include dirname( __FILE__ ).'/models/file_io.php';
  233. $importer = new Red_FileIO;
  234. $count = $importer->import( $_POST['group'], $_FILES['upload'] );
  235. if ( $count > 0 )
  236. $this->render_message( sprintf( __ngettext( '%d redirection was successfully imported','%d redirections were successfully imported', $count, 'redirection' ), $count ) );
  237. else
  238. $this->render_message( __( 'No items were imported', 'redirection' ) );
  239. }
  240. $groups = Red_Group::get_for_select();
  241. $this->render_admin( 'options', array( 'options' => $this->get_options(), 'groups' => $groups ) );
  242. }
  243. function admin_screen_log() {
  244. include dirname( __FILE__ ).'/models/pager.php';
  245. if ( isset( $_POST['deleteall'] ) && check_admin_referer( 'redirection-process_logs' ) ) {
  246. if ( isset( $_GET['module'] ) )
  247. RE_Log::delete_all( array( 'module_id' => intval( $_GET['module'] ) ), new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'created', 'DESC', 'log' ) );
  248. else if (isset($_GET['group']))
  249. RE_Log::delete_all( array( 'group_id' => intval( $_GET['group'] ) ), new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'created', 'DESC', 'log' ) );
  250. else
  251. RE_Log::delete_all( array(), new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'created', 'DESC', 'log' ) );
  252. $this->render_message( __( 'Your logs have been deleted', 'redirection' ) );
  253. }
  254. $pager = new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'created', 'DESC', 'log' );
  255. if ( isset( $_GET['module'] ) )
  256. $logs = RE_Log::get_by_module( $pager, intval( $_GET['module'] ) );
  257. else if (isset($_GET['group']))
  258. $logs = RE_Log::get_by_group( $pager, intval( $_GET['group'] ) );
  259. else if (isset($_GET['redirect']))
  260. $logs = RE_Log::get_by_redirect( $pager, intval( $_GET['redirect'] ) );
  261. else
  262. $logs = RE_Log::get( $pager );
  263. $options = $this->get_options();
  264. $this->render_admin( 'log', array( 'logs' => $logs, 'pager' => $pager, 'lookup' => $options['lookup'] ) );
  265. }
  266. function admin_groups($module) {
  267. include dirname( __FILE__ ).'/models/pager.php';
  268. if (isset( $_POST['add'] ) && check_admin_referer( 'redirection-add_group' ) ) {
  269. if ( Red_Group::create(stripslashes_deep( $_POST ) ) ) {
  270. $this->render_message( __( 'Your group was added successfully', 'redirection' ) );
  271. Red_Module::flush( $module );
  272. }
  273. else
  274. $this->render_error( __( 'Please specify a group name', 'redirection' ) );
  275. }
  276. if ( $module == 0 )
  277. $module = Red_Module::get_first_id();
  278. $pager = new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'position', 'ASC' );
  279. $items = Red_Group::get_all( $module, $pager );
  280. $this->render_admin( 'group_list', array( 'groups' => $items, 'pager' => $pager, 'modules' => Red_Module::get_for_select(), 'module' => Red_Module::get( $module ) ) );
  281. }
  282. function admin_redirects( $group ) {
  283. include dirname( __FILE__ ).'/models/pager.php';
  284. if ( $group == 0 )
  285. $group = Red_Group::get_first_id();
  286. $pager = new RE_Pager( $_GET, $_SERVER['REQUEST_URI'], 'position', 'ASC' );
  287. $items = Red_Item::get_by_group( $group, $pager );
  288. $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')) );
  289. }
  290. /**
  291. * Displays the nice animated support logo
  292. *
  293. * @return void
  294. **/
  295. function admin_footer() {
  296. if ( isset($_GET['page']) && $_GET['page'] == basename( __FILE__ ) ) {
  297. $options = $this->get_options();
  298. if ( !$options['support'] ) {
  299. ?>
  300. <script type="text/javascript" charset="utf-8">
  301. jQuery(function() {
  302. jQuery('#support-annoy').animate( { opacity: 0.2, backgroundColor: 'red' } ).animate( { opacity: 1, backgroundColor: 'yellow' });
  303. });
  304. </script>
  305. <?php
  306. }
  307. }
  308. }
  309. function setMatched( $match ) {
  310. $this->hasMatched = $match;
  311. }
  312. function hasMatched() {
  313. return $this->hasMatched;
  314. }
  315. function locales() {
  316. $locales = array();
  317. $readme = @file_get_contents( dirname( __FILE__ ).'/readme.txt' );
  318. if ( $readme ) {
  319. if ( preg_match_all( '/^\* (.*?) by \[(.*?)\]\((.*?)\)/m', $readme, $matches ) ) {
  320. foreach ( $matches[1] AS $pos => $match ) {
  321. $locales[$match] = '<a href="'.$matches[3][$pos].'">'.$matches[2][$pos].'</a>';
  322. }
  323. }
  324. }
  325. ksort( $locales );
  326. return $locales;
  327. }
  328. }
  329. // Instantiate the plugin
  330. $redirection = new Redirection;