<?php
/*
Plugin Name: AskApache RewriteRules Viewer
Plugin URI: https://www.askapache.com/htaccess/rewriterule-viewer-plugin/
Description: Displays the Internal WordPress Rewrite Rules.  <a href="options-general.php?page=aa-rewriterules-viewer.php">Options configuration panel</a>
Version: 3.0
Author: AskApache
Author URI: https://www.askapache.com
*/

/*
== Installation ==

1. Upload aa-rewriterules-viewer.zip to the /wp-content/plugins/ directory
2. Unzip into its own folder /wp-content/plugins/aa-rewriterules-viewer/aa-rewriterules-viewer.php
3. Activate the plugin through the 'Plugins' menu in WordPress by clicking "AskApache RewriteRules Viewer"
4. Go to your Options Panel and open the "AA RewriteRules" submenu.
*/



/*
/--------------------------------------------------------------------\
|                                                                    |
| License: GPL                                                       |
|                                                                    |
| AskApache RewriteRules Viewer - View Wordpress Rewrites            |
| Copyright (C) 2008, AskApache, www.askapache.com                   |
| All rights reserved.                                               |
|                                                                    |
| This program is free software; you can redistribute it and/or      |
| modify it under the terms of the GNU General Public License        |
| as published by the Free Software Foundation; either version 2     |
| of the License, or (at your option) any later version.             |
|                                                                    |
| This program is distributed in the hope that it will be useful,    |
| but WITHOUT ANY WARRANTY; without even the implied warranty of     |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      |
| GNU General Public License for more details.                       |
|                                                                    |
| You should have received a copy of the GNU General Public License  |
| along with this program; if not, write to the                      |
| Free Software Foundation, Inc.                                     |
| 51 Franklin Street, Fifth Floor                                    |
| Boston, MA  02110-1301, USA                                        |
|                                                                    |
\--------------------------------------------------------------------/
*/
?>
<?php

function aa_rewriterules_viewer_options_setup() {
    global 
$aa_RW;
    
$aa_RW=get_option('askapache_rewriterules_viewer');
    
add_options_page($aa_RW['plugin_data']['Name'], 'AA RewriteRules'8basename(__FILE__), 'aa_rewriterules_viewer_page');
}



function 
aa_rewriterules_viewer_page() {
    global 
$is_apache$wp_rewrite$aa_RW;
    
$aa_RW=get_option('askapache_rewriterules_viewer');
    
    
// security
    
if (function_exists('current_user_can')&&!current_user_can(8)||!current_user_can('upload_files'))    wp_die(__("You are not allowed to be here"));

    
$aa_rws=$wp_rewrite->rewrite_rules();
    
$home_path get_home_path();
    
$hta=$home_path '.htaccess';
    
$aa_perm_str=get_option('permalink_structure');
    
    
ob_start();
    
$htas=aa_gett_file($hta);
    
$htal=ob_get_clean();
    
$aa_rl=0;

    foreach (
$aa_rws as $i => $url){
        
$aa_r.="$i == $url\n";
        
$aa_rl++;
    }


    
// status and debug info
    
ob_start();
    echo 
'<pre id="message" class="fade-72B35D" style="width:50%;height:100%;margin:0;float:right;overflow:auto;">';
    if ( !
$wp_rewrite->using_permalinks() ) echo "<strong>&gt;</strong> Not using permalinks\n";
    else echo 
"<strong>&gt;</strong> Permalink: $aa_perm_str\n";
    if (!((!
file_exists$hta ) && is_writable$home_path ) ) || is_writable$hta ) ) ) echo "<strong>&gt;</strong> No .htaccess file found or not writable\n";
    else echo 
"<strong>&gt;</strong> Writable .htaccess file found\n";
    if (! 
got_mod_rewrite() )echo "<strong>&gt;</strong> mod_rewrite not found\n";
    else echo 
"<strong>&gt;</strong> mod_rewrite found\n";
    if ( !
$wp_rewrite->using_index_permalinks() ) echo "<strong>&gt;</strong> Not using pathinfo style index permalinks\n";
    else echo 
"<strong>&gt;</strong> Using pathinfo style index permalinks\n";
    if (!
$wp_rewrite->using_mod_rewrite_permalinks() ) echo "<strong>&gt;</strong> Not using mod_rewrite permalinks";
    else echo 
"<strong>&gt;</strong> Using mod_rewrite permalinks";
    echo 
'</pre>';    
    
$aa_status=ob_get_clean();



    

    
// rewrites
    
ob_start();
    echo 
'<div class="wrap"><h2>'$aa_RW['plugin_data']['Name'].' ' $aa_RW['plugin_data']['Version'] . '</h2><form action="' $_SERVER['REQUEST_URI'] . '" id="sform" method="post">';
    
?>
    <fieldset class="options"><legend>WordPress Generated .htaccess Rules</legend>
    <p><?php echo $aa_status;?><textarea rows="7" style="width: 45%;" name="rules" class="code" id="rules"><?php echo wp_specialchars($wp_rewrite->mod_rewrite_rules()); ?></textarea></p></fieldset><hr style="visibility:hidden;">
    <fieldset class="options"><legend>All WordPress RewriteRules</legend>
    <p><textarea rows="<?php echo $aa_rl;?>" style="width: 98%; height:20em; white-space:pre; overflow:auto;" name="prrules" class="code" id="prrules"><?php echo wp_specialchars($aa_r); ?></textarea></p></fieldset><hr style="visibility:hidden;">
    <fieldset class="options"><legend>Full Contents of your .htaccess file (<a href="templates.php?file=.htaccess">Go Edit</a>)</legend>
    <p><textarea rows="<?php echo $htal;?>" style="width: 98%; height:20em; white-space:pre; overflow:auto;" class="code" name="rrules" id="rrules"><?php echo wp_specialchars($htas); ?></textarea></p></fieldset>
    </form></div>
    <?php 
    $aa_pre_rr
=ob_get_clean();

    
    
$aa_head='<p style="text-align:center;">[ <a href="https://www.askapache.com/htaccess/">.htaccess guide</a> - <a href="https://www.askapache.com/htaccess/rewriterule-viewer-plugin/">'.$aa_RW['plugin_data']['Name'].' <strong>'.$aa_RW['plugin_data']['Version'] . '</strong></a> - <a href="https://www.askapache.com/">Author Home</a> - <a href="http://www.htaccesselite.com/htaccess/">mod_rewrite help forum</a> ]</p><hr style="visibility:hidden;">';
    
        
    
_e($aa_head);
    
_e($aa_pre_rr);

}


function 
aa_gett_file($fn){
    
$lc=0;
    
$handle = @fopen($fn"r");
    if (
$handle) {
        while (!
feof($handle)) {
            
$buffer.= fgets($handle);
            
$lc++;
        }
        
fclose($handle);
    }
    echo 
$lc;
    return 
$buffer;
}


function 
aa_rw_activate(){
    global 
$aa_RW;
    
$aa_RW['plugin_data']=get_plugin_data(__FILE__);
    
update_option('askapache_rewriterules_viewer',$aa_RW);
}

function 
aa_rw_deactivate(){
    
delete_option('askapache_rewriterules_viewer');
}

register_activation_hook(__FILE__'aa_rw_activate');
register_deactivation_hook(__FILE__'aa_rw_deactivate');
add_action('admin_menu''aa_rewriterules_viewer_options_setup');

?>