<?php

# PLUGIN PREVIEW BY TEXTPATTERN.INFO

/**
* Plugin:      abl_robots_txt
* Version:     0.1
* URL:         http://www-blaser-it.ch/
* Description: Simple robots.txt editor
* Year:        2011
* MD5:         d3fc6b08e5ee8ca3e1d9df4faa856ba5
*
* Author:      Andreas Blaser
*
* License:     GPL v2
*
*/
    // init
    
if (@txpinterface == 'admin') {
        
$myevent 'abl_robots_txt';
        
$mytab 'robots.txt';
        
// Set the privilege levels for our new event
        
add_privs($myevent'1,2');
        
// Add a new tab under 'extensions' associated with our event
        
register_tab('extensions'$myevent$mytab);
        
// 'abl_robots_txt_gui' will be called to handle the new event
        
register_callback($myevent '_gui'$myevent);
    }
    
// main
    
function abl_robots_txt_gui($event$step) {
        
// get or process data
        
$result abl_robots_txt_data($event$step);
        
// create gui
        
pagetop("Edit robots.txt"$result['pagetop_msg']);
        
$links abl_robots_txt_links();
        echo 
'<div style="text-align:left; margin:0pt auto; width:790px; float:none;">
<div style="float:left; width:250px; margin-right:20px;">
<h3>robots.txt Help</h3>
<h4>User-agent</h4>
<ul class="plain-list">
<li><code>User-agent: *</code><br />Matches all User-Agents.</li>
<li><code>User-agent: BadBot</code><br />Matches User-agent "BadBot".</li>
</ul>
<h4>Disallow</h4>
<ul class="plain-list">
<li><code>Disallow:</code><br />Allow all Files and Directories.</li>
<li><code>Disallow: /</code><br />Disallow all Files and Directories.</li>
<li><code>Disallow: /help</code><br />Disallow help.html and all files in Directory "help".</li>
<li><code>Disallow: /help/</code><br />Allow help.html but disallow all files in Directory "help".</li>
</ul>
<h4>XML Sitemap</h4>
<ul class="plain-list">
<li><code>Sitemap: http://www.example.com/sitemap.xml</code><br />XML-Sitemap location.</li>
</ul>
<h4>Useful Resources</h4>
<ul class="plain-list">
<li><a href="http://www.robotstxt.org/" target="_blank">robotstxt.org</a></li>
<li><a href="http://en.wikipedia.org/wiki/Robots_exclusion_standard" target="_blank">wikipedia</a></li>
<li><a href="http://www.google.com/support/webmasters/bin/answer.py?hl=en&amp;answer=156449" target="_blank">Google</a></li>
$links '</ul>
</div>
</div>' 
n;
        echo 
'<div style="float: left; width: 520px;">' .
            
form(
            
graf(text_area($event '_editor''300''500'$result['content']).'<br />' .
                
fInput('submit'$event '_submit''Save''publish') .
                
eInput($event) . sInput($event '_save'),
                
' style="float:left; width: 550px; text-align:left;"')
            ) . 
.
        
'</div>' n;
        echo 
'<div style="float:none; clear:both; display:block; width:100%; height:0;"></div>' n;
        echo 
'<div style="text-align:center;"><p>' abl_robots_txt_identify($event) . '</p></div>' n;
    }

    
// get additional links from txp_links
    
function abl_robots_txt_links() {
        
//$sort = get_pref('link_sort_column', 'name');
        
$sort 'linksort';
        
$dir get_pref('link_sort_dir''asc');
        
$dir = ($dir == 'desc') ? 'desc' 'asc';
        
$criteria 'category = "robots_txt"';
        
$sort_sql $sort ' ' $dir;
        
$rs safe_rows_start('*, unix_timestamp(date) as uDate''txp_link',
                              
"$criteria order by $sort_sql limit 0, 10");
        
$links '';
        if (
$rs) {
            while (
$row nextRow($rs)) {
                
extract($row);
                
$links .= '<li><a href="' $url '" target="_blank" title="' $description '">' $linkname '</a></li>' n;
            }
        }
        return 
$links;
    }

    
// read & write robots.txt
    
function abl_robots_txt_data($event$step) {
        
$fn $GLOBALS['prefs']['path_to_site'] . '/robots.txt';
        
$robots_exists file_exists($fn);
        
$content '';
        
$pagetop_msg '';
        if (
ps($event '_submit')) {
            
// create or update file
            
$content trim(ps($event '_editor'));
            if (
$content != '') {
                
$ret file_put_contents($fn$content);
                if (
$ret === false) {
                    if (
$robots_exists) {
                        
$pagetop_msg "Error! 'robots.txt' NOT updated!";
                    } else {
                        
$pagetop_msg "Error! 'robots.txt' NOT created!";
                    }
                } else {
                    if (
$robots_exists) {
                        
$pagetop_msg "'robots.txt' updated.";
                    } else {
                        
$pagetop_msg "'robots.txt' created.";
                    }
                }
            } else  {
                if (
$robots_exists) {
                    
$ret unlink($fn);
                    if (!
unlink($fn)) {
                        
// unlink failed, try to empty that file
                        
$ret file_put_contents($fn"");
                        if (
$ret === false) {
                            
$pagetop_msg "Error! Could NOT delete or clear 'robots.txt'.";
                        } else {
                            
$pagetop_msg "File 'robots.txt' cleared.";
                        }
                    }
                }
            }
        } else {
            
// read file if it exists
            
if ($robots_exists$content file_get_contents($fn);
            if (
$content === false) {
                
$pagetop_msg "Error! Could not retrieve 'robots.txt'.";
                
$content "";
            }
        }
        return array(
"content" => $content"pagetop_msg" => $pagetop_msg);
    }

    
// identity
    
function abl_robots_txt_identify($event) {
        global 
$plugins_ver;
        return 
$event ' &#183; ' $plugins_ver[$event];
    }