<?php
/*************************
  Coppermine Photo Gallery
  ************************
  Copyright (c) 2003-2006 Coppermine Dev Team
  v1.1 originally written by Gregory DEMAR

  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.

  ********************************************

  Coppermine version: 1.4.8
  $Source$
  $Revision: 3116 $
  $Author: gaugau $
  $Date: 2006-06-08 00:11:54 +0200 (Do, 08 Jun 2006) $

**********************************************/

if (!defined('IN_COPPERMINE')) { die('Not in Coppermine...');}

// Add plugin_install action
$thisplugin->add_action('plugin_install','friendly_urls_install');

// Add plugin_uninstall action
$thisplugin->add_action('plugin_uninstall','friendly_urls_uninstall');

// Add plugin_configure action
$thisplugin->add_action('plugin_configure','friendly_urls_configure');

// Add plugin_cleanup action
$thisplugin->add_action('plugin_cleanup','friendly_urls_cleanup');

// Add page_html filter
$thisplugin->add_filter('page_html','friendly_urls_convert');

/**
 * Convert urls to search-engine friendly (SEF) urls
 */
function friendly_urls_convert(&$html) {

	// Profile links
	$html = preg_replace('/profile\.php\?uid=([0-9]+)/i','user-$1.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastupby(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)uid=([0-9]+)(\&|\&amp;)page=([0-9]+)/i','user-$4-last-uploads-cat-$2-page-$6.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastupby(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)uid=([0-9]+)(\&|\&amp;)page=([0-9]+)/i','user-$4-last-uploads-album-$2-page-$6.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastupby(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)page=([0-9]+)(\&|\&amp;)uid=([0-9]+)/i','user-$6-last-uploads-cat-$2-page-$4.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastupby(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)page=([0-9]+)(\&|\&amp;)uid=([0-9]+)/i','user-$6-last-uploads-album-$2-page-$4.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastupby(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)uid=([0-9]+)/i','user-$4-last-uploads-cat-$2.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastupby(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)uid=([0-9]+)/i','user-$4-last-uploads-album-$2.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastupby(\&|\&amp;)uid=([0-9]+)/i','user-$2-last-uploaded.html',$html);
	$html = preg_replace('/displayimage\.php\?album=lastupby(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)pos=([0-9]+)(\&|\&amp;)uid=([0-9]+)/i','user-$6-last-uploaded-pics-cat-$2-pos-$4.html',$html);
	$html = preg_replace('/displayimage\.php\?album=lastupby(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)pos=([0-9]+)(\&|\&amp;)uid=([0-9]+)/i','user-$6-last-uploaded-pics-album-$2-pos-$4.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastcomby(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)uid=([0-9]+)(\&|\&amp;)page=([0-9]+)/i','user-$4-last-comments-cat-$2-page-$6.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastcomby(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)uid=([0-9]+)(\&|\&amp;)page=([0-9]+)/i','user-$4-last-comments-album-$2-page-$6.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastcomby(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)page=([0-9]+)(\&|\&amp;)uid=([0-9]+)/i','user-$6-last-comments-cat-$2-page-$4.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastcomby(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)page=([0-9]+)(\&|\&amp;)uid=([0-9]+)/i','user-$6-last-comments-album-$2-page-$4.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastcomby(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)uid=([0-9]+)/i','user-$4-last-comments-cat-$2.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastcomby(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)uid=([0-9]+)/i','user-$4-last-comments-album-$2.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastcomby(\&|\&amp;)uid=([0-9]+)/i','user-$2-last-comment.html',$html);
	$html = preg_replace('/displayimage\.php\?album=lastcomby(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)pos=([0-9]+)(\&|\&amp;)uid=([0-9]+)/i','user-$6-last-comment-pics-cat-$2-pos-$4.html',$html);
	$html = preg_replace('/displayimage\.php\?album=lastcomby(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)pos=([0-9]+)(\&|\&amp;)uid=([0-9]+)/i','user-$6-last-comment-pics-album-$2-pos-$4.html',$html);
	$html = preg_replace('/profile\.php\?op=edit_profile/i','my-profile.html',$html);

	// Category links
	$html = preg_replace('/index\.php\?cat=([0-9]+)(\&|\&amp;)page=([0-9]+)/i','cat-$1-page-$3.html',$html);
	$html = preg_replace('/index\.php\?cat=([0-9]+)/i','cat-$1.html',$html);

	// Slideshow links (from sef_urls)
	$html = preg_replace('/displayimage\.php\?album=([a-z]+)(\&|\&amp;)cat=([\-0-9]+)(\&|\&amp;)uid=([0-9]+)(\&|\&amp;)pid=([0-9]+)(\&|\&amp;)slideshow=([0-9]+)/i','slideshow-album-$1-cat-$3-user-$5-pid-$7-delay-$9.html',$html);
	$html = preg_replace('/displayimage\.php\?album=([a-z]+)(\&|\&amp;)cat=([\-0-9]+)(\&|\&amp;)pid=([0-9]+)(\&|\&amp;)slideshow=([0-9]+)/i','slideshow-album-$1-cat-$3-pid-$5-delay-$7.html',$html);
	$html = preg_replace('/displayimage\.php\?album=([a-z0-9]+)(\&|\&amp;)pid=([0-9]+)(\&|\&amp;)slideshow=([0-9]+)/i','slideshow-album-$1-pid-$3-delay-$5.html',$html);

	// Thumbnail random files
	$html = preg_replace('/displayimage\.php\?album=random(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)pos=-([0-9]+)/i','random-pics-cat-$2-pos-$4.html',$html);
	$html = preg_replace('/displayimage\.php\?album=random(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)pos=-([0-9]+)/i','random-pics-album-$2-pos-$4.html',$html);

	// Thumbnail last additions
	$html = preg_replace('/displayimage\.php\?album=lastup(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)pos=([0-9]+)/i','last-uploads-pics-cat-$2-pos-$4.html',$html);
	$html = preg_replace('/displayimage\.php\?album=lastup(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)pos=([0-9]+)/i','last-uploads-pics-album-$2-pos-$4.html',$html);

	// Thumbnail most viewed
	$html = preg_replace('/displayimage\.php\?album=topn(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)pos=([0-9]+)/i','most-viewed-pics-cat-$2-pos-$4.html',$html);
	$html = preg_replace('/displayimage\.php\?album=topn(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)pos=([0-9]+)/i','most-viewed-pics-album-$2-pos-$4.html',$html);

	// Thumbnail top rated
	$html = preg_replace('/displayimage\.php\?album=toprated(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)pos=([0-9]+)/i','top-rated-pics-cat-$2-pos-$4.html',$html);
	$html = preg_replace('/displayimage\.php\?album=toprated(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)pos=([0-9]+)/i','top-rated-pics-album-$2-pos-$4.html',$html);

	// Thumbnail last comments
	$html = preg_replace('/displayimage\.php\?album=lastcom(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)pos=([0-9]+)/i','last-comments-pics-cat-$2-pos-$4.html',$html);
	$html = preg_replace('/displayimage\.php\?album=lastcom(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)pos=([0-9]+)/i','last-comments-pics-album-$2-pos-$4.html',$html);

	// Thumbnail last viewed
	$html = preg_replace('/displayimage\.php\?album=lasthits(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)pos=([0-9]+)/i','last-viewed-pics-cat-$2-pos-$4.html',$html);
	$html = preg_replace('/displayimage\.php\?album=lasthits(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)pos=([0-9]+)/i','last-viewed-pics-album-$2-pos-$4.html',$html);

	// Last uploads
	$html = preg_replace('/thumbnails\.php\?album=lastup(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)page=([0-9]+)/i','last-uploads-cat-$2-page-$4.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastup(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)page=([0-9]+)/i','last-uploads-album-$2-page-$4.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastup(\&|\&amp;)cat=([0-9]+)/i','last-uploads-cat-$2.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastup(\&|\&amp;)cat=-([0-9]+)/i','last-uploads-album-$2.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastup/i','last-uploads.html',$html);

	// Most viewed
	$html = preg_replace('/thumbnails\.php\?album=topn(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)page=([0-9]+)/i','most-viewed-cat-$2-page-$4.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=topn(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)page=([0-9]+)/i','most-viewed-album-$2-page-$4.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=topn(\&|\&amp;)cat=([0-9]+)/i','most-viewed-cat-$2.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=topn(\&|\&amp;)cat=-([0-9]+)/i','most-viewed-album-$2.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=topn/i','most-viewed.html',$html);

	// Top rated
	$html = preg_replace('/thumbnails\.php\?album=toprated(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)page=([0-9]+)/i','top-rated-cat-$2-page-$4.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=toprated(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)page=([0-9]+)/i','top-rated-album-$2-page-$4.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=toprated(\&|\&amp;)cat=([0-9]+)/i','top-rated-cat-$2.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=toprated(\&|\&amp;)cat=-([0-9]+)/i','top-rated-album-$2.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=toprated/i','top-rated.html',$html);

	// Last comments
	$html = preg_replace('/thumbnails\.php\?album=lastcom(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)page=([0-9]+)/i','last-comments-cat-$2-page-$4.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastcom(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)page=([0-9]+)/i','last-comments-album-$2-page-$44.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastcom(\&|\&amp;)cat=([0-9]+)/i','last-comments-cat-$2.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastcom(\&|\&amp;)cat=-([0-9]+)/i','last-comments-album-$2.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lastcom/i','last-comments.html',$html);

	// Last viewed
	$html = preg_replace('/thumbnails\.php\?album=lasthits(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)page=([0-9]+)/i','last-viewed-cat-$2-page-$4.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lasthits(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)page=([0-9]+)/i','last-viewed-album-$2-page-$4.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lasthits(\&|\&amp;)cat=([0-9]+)/i','last-viewed-cat-$2.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lasthits(\&|\&amp;)cat=-([0-9]+)/i','last-viewed-album-$2.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=lasthits/i','last-viewed.html',$html);

	// My Favorites
	$html = preg_replace('/thumbnails\.php\?album=favpics/i','my-favorite-pictures.html',$html);

	// Album links with pages and sort
	$html = preg_replace('/thumbnails\.php\?album=([0-9]+)(\&|\&amp;)page=([0-9]+)(\&|\&amp;)sort=ta/i','album-$1-page-$3-sort-title-ascending.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=([0-9]+)(\&|\&amp;)page=([0-9]+)(\&|\&amp;)sort=td/i','album-$1-page-$3-sort-title-descending.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=([0-9]+)(\&|\&amp;)page=([0-9]+)(\&|\&amp;)sort=na/i','album-$1-page-$3-sort-name-ascending.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=([0-9]+)(\&|\&amp;)page=([0-9]+)(\&|\&amp;)sort=nd/i','album-$1-page-$3-sort-name-descending.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=([0-9]+)(\&|\&amp;)page=([0-9]+)(\&|\&amp;)sort=da/i','album-$1-page-$3-sort-date-ascending.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=([0-9]+)(\&|\&amp;)page=([0-9]+)(\&|\&amp;)sort=dd/i','album-$1-page-$3-sort-date-descending.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=([0-9]+)(\&|\&amp;)page=([0-9]+)(\&|\&amp;)sort=pa/i','album-$1-page-$3-sort-position-ascending.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=([0-9]+)(\&|\&amp;)page=([0-9]+)(\&|\&amp;)sort=pd/i','album-$1-page-$3-sort-position-descending.html',$html);

	// Album links
	$html = preg_replace('/thumbnails\.php\?album=([0-9]+)(\&|\&amp;)page=([0-9]+)/i','album-$1-page-$3.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=([0-9]+)/i','album-$1.html',$html);

	// Display image link
	$html = preg_replace('/displayimage\.php\?album=([0-9]+)(\&|\&amp;)pos=([0-9]+)/i','display-image-album-$1-pos-$3.html',$html);

	// Image direct link
	$html = preg_replace('/displayimage\.php\?pos=-([0-9]+)/i','picture-$1.html',$html);

	// Picture rate link
	$html = preg_replace('/ratepic\.php\?pic=([0-9]+)(\&|\&amp;)rate=([0-9]+)/i','rate-image-$1-rate-$3.html',$html);

	// Report picture link
	$html = preg_replace('/report_file\.php\?album=([0-9]+)(\&|\&amp;)pid=([0-9]+)(\&|\&amp;)pos=([0-9]+)/i','report-image-album-$1-pic-$3-pos-$5.html',$html);
	$html = preg_replace('/report_file\.php\?album=([a-z]+)(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)pid=([0-9]+)(\&|\&amp;)pos=([0-9]+)/i','report-image-album-$1-cat-$3-pic-$5-pos-$7.html',$html);
	$html = preg_replace('/report_file\.php\?album=([a-z]+)(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)pid=([0-9]+)(\&|\&amp;)pos=([0-9]+)/i','report-image-album-$1-catn-$3-pic-$5-pos-$7.html',$html);
	$html = preg_replace('/report_file\.php\?pid=([0-9]+)(\&|\&amp;)msg_id=([0-9]+)(\&|\&amp;)what=comment/i','report-comment-pid-$1-msgid-$3.html',$html);

	// E-Card picture link
	$html = preg_replace('/ecard\.php\?album=([0-9]+)(\&|\&amp;)pid=([0-9]+)(\&|\&amp;)pos=([0-9]+)/i','ecard-image-album-$1-pic-$3-pos-$5.html',$html);
	$html = preg_replace('/ecard\.php\?album=([a-z]+)(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)pid=([0-9]+)(\&|\&amp;)pos=([0-9]+)/i','ecard-image-album-$1-cat-$3-pic-$5-pos-$7.html',$html);
	$html = preg_replace('/ecard\.php\?album=([a-z]+)(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)pid=([0-9]+)(\&|\&amp;)pos=([0-9]+)/i','ecard-image-album-$1-catn-$3-pic-$5-pos-$7.html',$html);

	// Home
	$html = preg_replace('/index\.php/i','index.html',$html);

	// FAQ
	$html = preg_replace('/faq\.php/i','faq.html',$html);

	// Register
	$html = preg_replace('/register\.php/i','register.html',$html);

	// Users
	$html = preg_replace('/usermgr\.php/i','users.html',$html);

	// Search links
	$html = preg_replace('/search\.php/i','search.html',$html);
	$html = preg_replace('/thumbnails\.php\?album=search(\&|\&amp;)search=([a-z0-9]*)\"/i','search-$2.html"',$html);
	$html = preg_replace('/thumbnails\.php\?album=search(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)page=([0-9]+)/i','search-cat-$2-page-$4.html"',$html);
	$html = preg_replace('/thumbnails\.php\?album=search(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)page=([0-9]+)/i','search-album-$2-page-$4.html"',$html);
	$html = preg_replace('/thumbnails\.php\?album=search(\&|\&amp;)cat=([0-9]+)(\&|\&amp;)page=([0-9]+)/i','search-cat-$2-page-$4.html"',$html);
	$html = preg_replace('/thumbnails\.php\?album=search(\&|\&amp;)cat=-([0-9]+)(\&|\&amp;)page=([0-9]+)/i','search-album-$2-page-$4.html"',$html);

	// Upload
	$html = preg_replace('/upload\.php/i','upload.html',$html);

	// Login
	$html = preg_replace('/login\.php/i','login.html',$html);

	// Logout
	$html = preg_replace('/logout\.php/i','logout.html',$html);
	
	// Lost Password
	$html = preg_replace('/forgot_passwd\.php/i','forgot-password.html',$html);
	
	// Album Manager
	$html = preg_replace('/albmgr\.php/i','album-manager.html',$html);

	// Modify Album
	$html = preg_replace('/modifyalb\.php\?album=([0-9]+)/i','modify-album-$1.html',$html);
	$html = preg_replace('/modifyalb\.php/i','modify-album.html',$html);

	// Edit Pictures
	$html = preg_replace('/editpics\.php\?album=([0-9]+)/i','edit-pictures-album-$1.html',$html);
	$html = preg_replace('/editOnePic\.php\?id=([0-9]+)(\&|\&amp;)what=picture/i','edit-picture-$1.html',$html);

	// Picture Manager
	$html = preg_replace('/picmgr\.php/i','picture-manager.html',$html);

	// Delete links
	$html = preg_replace('/delete\.php\?msg_id=([0-9]+)(\&|\&amp;)what=comment/i','delete-comment-msgid-$1.html',$html);
	$html = preg_replace('/delete\.php\?id=([0-9]+)(\&|\&amp;)what=album/i','delete-album-$1.html',$html);
	$html = preg_replace('/delete\.php\?id=([0-9]+)(\&|\&amp;)what=picture/i','delete-picture-$1.html',$html);

	/*
    $html = preg_replace('/thumbnails\.php\?album=search(\&|\&amp;)search=([^"]+)/i','thumbnails-search-$2.html',$html);
    $html = preg_replace('/displayimage\.php\?album=([a-z0-9]+)(\&|\&amp;)cat=([\-0-9]+)(\&|\&amp;)pos=([\-0-9]+)/i','displayimage-$1-$3-$5.html',$html);
	*/

    return $html;
}

/**
 * Configure plugin for install
 */
function friendly_urls_configure($action) {
    global $thisplugin;

    if ($action===1) {
        $code = implode('',file($thisplugin->fullpath.'/htaccess.txt'));
        echo <<< EOT

    <form action="{$_SERVER['REQUEST_URI']}" method="post">
        <p>
            You already have a .htaccess file in your root Coppermine folder.<br />
            Is it ok to overwrite it?
        </p>
        <div style="margin:25;">
        <table border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td><input type="radio" name="create" value="1" /></td>
                <td>Yes</td>
            </tr>
            <tr>
                <td><input type="radio" name="create" checked="checked" value="0" /></td>
                <td>No</td>
            </tr>
        </table>
        </div>
        <span>
           <input type="submit" name="submit" value="Submit" /> &nbsp;&nbsp;&nbsp;
            <input type="button" name="cancel" onClick="window.location='pluginmgr.php';" value="Cancel" />
        </span>
        <p>&nbsp;</p>
        <p style="color:red;font-weight:bold;">STOP! READ THE FOLLOWING!</p>
        <p>
            If you don not want your .htaccess file to be overwritten, you will have to insert the following code:
        </p>
        <div align="right">
            <a class="link" href="{$thisplugin->fullpath}/htaccess.txt" target="_blank">Open in a seperate window</a>
        </div>
        <pre style="border:1;border-color:black;background-color:white;font-family:fixedsys;">
            $code
        </pre>
    </form>
EOT;
    }
}

/**
 * Display cleanup options for uninstall
 */
function friendly_urls_cleanup($action) {
    if ($action===1) {
        echo <<< EOT
    <form action="{$_SERVER['REQUEST_URI']}" method="post">
        <p>
            Delete the .htaccess file in your Coppermine root? (If this file was created by this plugin,
            It is ok to delete it.)
        </p>
        <div style="margin:25;">
        <table border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td><input type="radio" name="delete" value="1" /></td>
                <td>Yes</td>
            </tr>
            <tr>
                <td><input type="radio" name="delete" checked="checked" value="0" /></td>
                <td>No</td>
            </tr>
        </table>
        </div>
        <span>
           <input type="submit" name="submit" value="Submit" /> &nbsp;&nbsp;&nbsp;
            <input type="button" name="cancel" onClick="window.location='pluginmgr.php';" value="Cancel" />
        </span>
    </form>
EOT;
    }
}

/**
 * Install the plugin
 */
function friendly_urls_install() {
    global $thisplugin;

    $create = @$_POST['create'];

    // There's no .htaccess file or user has clicked 'yes' on the create form
    if (!file_exists('.htaccess') || $create) {
        copy($thisplugin->fullpath.'/htaccess.txt','.htaccess');
        return true;

    // An htaccess file exists; display the configure form
    } elseif (!isset($create)) {
        return 1;

    // User has clicked 'no' on the configure form. Install plugin. Don't create .htaccess file
    } else {
        return true;
    }
}

/**
 * Uninstall the plugin
 */
function friendly_urls_uninstall() {
    global $thisplugin;

    $delete = @$_POST['delete'];

    // There's an .htaccess file and user has clicked 'yes' on the cleanup form; delete the .htaccess file
    if (file_exists('.htaccess') && $delete) {
        unlink('.htaccess');
        return true;

    // An .htaccess file exists; display the cleanup form
    } elseif (file_exists('.htaccess') && !isset($delete)) {
        return 1;

    // User has clicked 'no' on the cleanup form. Uninstall plugin. Don't delete '.htaccess' file
    } else {
        return true;
    }
}
?>