<?php
//AUTHOR James Lin
//Version 3.0
 
if (!defined('DOKU_INC')) die();
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
 
require_once (DOKU_PLUGIN . 'action.php');
require_once (DOKU_PLUGIN . '/acl/admin.php');
 
class action_plugin_userhomepage extends DokuWiki_Action_Plugin
{
        private $userHomePagePath = '';
        private $userHomePageTemplate = '';
	private $home_wiki_page = '';
	private $home_wiki_ns = '';
	private $doku_page_path = '';
	private $home_page_template = '';

	function init()
	{
		if($_SERVER['REMOTE_USER']!=null)
                {
                        $this->doku_page_path = DOKU_INC . 'data/pages';
                        $this->home_page_template = DOKU_INC . $this->getConf('templatepath');
                        if ($this->getConf('group_by_name'))
                        {
                                // people:g:gme']li8600
                                $this->home_wiki_ns = $this->getConf('users_namespace').':'.strtolower(substr($this->homeNamespace(), 0, 1)).':'. $this->homeNamespace();
                        }
                        else
                        {
                                // people:gli8600
                                $this->home_wiki_ns = $this->getConf('users_namespace').':'. $this->homeNamespace();
                        }
                        // people:gli8600:start.txt
                        $this->home_wiki_page= $this->home_wiki_ns . ':' . $this->homePage();
                }	
	}
	
	//draws a home link, used by calls from main.php in template folder
	function homeButton()
	{
		$this->init();
		if ($_SERVER['REMOTE_USER']!=null)
		{
			echo '<form class="button btn_show" method="post" action="doku.php?id='.$this->home_wiki_page.'"><input class="button" type="submit" value="Home"/></form>';
		}
	}
	
	//draws a home button, used by calls from main.php in template folder
	function homeLink()
	{
		$this->init();
		if ($_SERVER['REMOTE_USER']!=null)
		{
			echo '<a href="doku.php?id='.$this->home_wiki_page.'">Home</a>';
		}
	}

	function homeNamespace() {
	    if ( $this->getConf('use_name_string')) {
	        global $INFO;
                $raw_string = $INFO['userinfo']['name'];
		//james_lin
	        return str_replace(" ","_",strtolower($raw_string)); 
	    } else {
		//gli8600
                return $_SERVER['REMOTE_USER'];
	    };
	}

	function homePage() {
	    if ( $this->getConf('use_start_page')) {
	        global $conf;
                return $conf['start'];
	    } else {
	        return $this->homeNamespace();
	    };
	}

        function redirect(&$event, $param)
        {
            global $conf;
	    global $INFO;
 
            if (($_SERVER['REMOTE_USER']!=null)&&($_REQUEST['do']=='login'))
            {
		$this->init();
		// /var/www/localhost/htdocs/doku/data/pages/people/gli8600
		$home_path= $this->doku_page_path.'/'.str_replace(":","/",$this->home_wiki_ns);
		// /var/www/localhost/htdocs/doku/data/pages/people/gli8600/start.txt
		$home_file= $home_path.'/'.$this->homePage().'.txt';
		// user daniel_stonier
                $folders = explode(':', $this->home_wiki_ns);
                //check if page not exists, create it
                if (!file_exists($home_file))
                {
                    //check if folder exists, if not, create it
		    $path = $this->doku_page_path;
                    foreach($folders as $folder)
                    {
                        $path .= '/'.$folder;
                        if (!is_dir($path)) { mkdir($path); }
                    }
                    //writes the user info to page
                    $content = file_get_contents($this->home_page_template);
                    $handle = fopen($home_file, 'w');
	            $name = $INFO['userinfo']['name']; 
		    $user = $_SERVER['REMOTE_USER'];
                    $content = str_replace('@NAME@',$name,$content);
                    $content = str_replace('@USER@',$user,$content);
                    fwrite($handle, $content);
                    fclose($handle);
		    // set acl's if requested
                    if ( $this->getConf('set_permissions') == 1 )
                    {
                        $acl = new admin_plugin_acl();
			$acl->_acl_add($this->home_wiki_ns.':*', '@ALL', (int)$this->getConf('set_permissions_others'));
                        $acl->_acl_add($this->home_wiki_ns.':*', $_SERVER['REMOTE_USER'], AUTH_DELETE);
                    }
                }
		// if the user was at a specific page, then don't redirect to personal page.
		if (($_REQUEST['id']=="start")||(!isset($_REQUEST['id'])))
		{
	                header('Location: doku.php?id='.$this->home_wiki_page);
        	        die(); // Don't let doku take over from here
		}
            }
        }
 
        function getInfo()
        {
                return  array
                (
                        'name' => 'User Home Page',
                        'email' => 'guanfenglin@gmail.com',
                        'date' => '04/12/2008',
                        'author' => 'James GuanFeng Lin',
                        'desc' => 'auto redirects users to <create> their homepage',
                        'url' => ''
                );
        }
 
        function register(&$controller)
        {
                $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'redirect',array());
        }
}
?>
