<?php
/**
 * SEF module for Joomla!
 *
 * @author      $Author: michal $
 * @copyright   ARTIO s.r.o., http://www.artio.cz
 * @package     JoomSEF
 * @version     $Name$, ($Revision: 4994 $, $Date: 2005-11-03 20:50:05 +0100 (??t, 03 XI 2005) $)
 */

// Security check to ensure this file is being included by a parent file.
if (!defined('_VALID_MOS')) die('Direct Access to this location is not allowed.');

function com_install()
{
    global $database, $mosConfig_absolute_path;

    // Change SB menu icon.
	$database->setQuery("SELECT id FROM #__components WHERE admin_menu_link = 'option=com_sef'");
	$id = $database->loadResult();

	// Set new admin menu image.
	$database->setQuery("UPDATE #__components " .
                        "SET admin_menu_img  = '../administrator/components/com_sef/images/icon.png'" .
                        "WHERE id='$id'");
	$database->query();
	
	// Install JoomSEF MetaBot.
	if (!is_dir($mosConfig_absolute_path.'/mambots/system')) {
	    @mkdir($mosConfig_absolute_path.'/mambots/system', null, true);
	}
	rename($mosConfig_absolute_path.'/administrator/components/com_sef/bots/joomsef_metabot.php', $mosConfig_absolute_path.'/mambots/system/joomsef_metabot.php');
	rename($mosConfig_absolute_path.'/administrator/components/com_sef/bots/joomsef_metabot.xml.tmp', $mosConfig_absolute_path.'/mambots/system/joomsef_metabot.xml');
	@rmdir($mosConfig_absolute_path.'/administrator/components/com_sef/bots');
	$database->setQuery("INSERT INTO #__mambots
	                    (id, name, element, folder, access, ordering, published, iscore, client_id, checked_out, checked_out_time, params)
	                    VALUES ('', 'ARTIO JoomSEF MetaBot', 'joomsef_metabot', 'system', 0, 0, 1, 0, 0, 0, '0000-00-00 00:00:00', 'rewrite_keywords=1\nprefer_joomsef_title=1')");
	$database->query();
	
    ob_start();

    echo '<div class="quote" style="text-align: center;">';
    echo '<h1>ARTIO JoomSEF installed succesfully!</h1>';
    echo '<h3>You must first edit the configuration, enable it and save before it will become active.</h3>';
    $readdocs = '<p class="message">Please scroll down and read the documentation.<br/>There is still extra configuration that you need to complete for ';
    if (!(strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') === false)) {
        echo $readdocs.'IIS.</p>';
    }
    else {
        $htaccess = "
DirectoryIndex index.php
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} ^(/component/option,com) [NC,OR]
RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|/[^.]*)$  [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php
	";

        //if (substr(PHP_OS, 0, 3) == 'WIN') {
        //	echo '<p class="error">Found apache on windows, .htaccess is an illegal filename for this system.<br/>You must complete the rest of the configuration manually.</p>';
        //	echo $readdocs."the apache .htaccess file.</p>";
        //}
        //else{
        echo '<p style="text-align: center;">Checking for .htaccess in Joomla! root...<br />';
        if(!file_exists($GLOBALS['mosConfig_absolute_path'].DIRECTORY_SEPARATOR.'.htaccess')){
            echo 'not found.</p>';
            $file = fopen($GLOBALS['mosConfig_absolute_path'].DIRECTORY_SEPARATOR.'.htaccess', 'w+');
            if(!$file){
                echo '<p style="text-align: center;" class="error">Unable to create .htaccess file in your Joomla! root. Please create this file yourself and add the following lines:<br/><pre>'.$htaccess.'</pre>';
            }
            else{
                fwrite($file, $htaccess);
                fclose($file);
                echo "Successfully created .htaccess file in your Joomla! root with the following content:<br/><pre>$htaccess</pre>";
            }
            echo "Please check that the RewriteBase directive path is set correctly and matches your configuration.";
        }
        else {
            echo '<span class="error">Found existing .htaccess in Joomla! root.</span></p>';
            echo $readdocs.'the apache .htaccess file</p>';
        }
        echo '</div>';
    }

    include($GLOBALS['mosConfig_absolute_path'].'/administrator/components/com_sef/readme.inc.html');

    $output = ob_get_contents();
    ob_end_clean();

    return $output;
}
?>
