<?php
/**
 * Plugin USERS PAGE AUTOCREATE
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author Vladimir Psyukalov <pedagog@pisem.net>
 */

// must be run within Dokuwiki
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_userspagecreate extends DokuWiki_Action_Plugin {

    /**
     * return some info
     */
    function getInfo(){
        return array(
            'author' => 'Vladimir Psyukalov',
            'email'  => 'pedagog@palitra.info',
            'date'   => '2008-08-07',
            'name'   => 'User page autocreate (v0.1.0)',
            'desc'   => 'User page autocreate after register', 
            'url'    => 'https://www.dokuwiki.org/plugin:userspagecreate',
        );
    }

    /**
     * register the eventhandlers
     */
    function register(&$controller){
        $controller->register_hook('ACTION_ACT_PREPROCESS',
                                   'AFTER',
                                   $this,
                                   'action_act_preprocess_after',
                                   array());

        $controller->register_hook('DOKUWIKI_STARTED',
                                   'BEFORE',
                                   $this,
                                   'dokuwiki_started_before',
                                   array());

        $controller->register_hook('RENDERER_CONTENT_POSTPROCESS',
                                   'AFTER',
                                   $this,
                                   'renderer_content_postprocess_after',
                                   array());
                                 


    }
    function renderer_content_postprocess_after(&$event, $param){
       global $INFO;
       $event->data[1]=preg_replace('{class="\w+" title="'.$INFO['usersnamespace'].':}si','class="wikiuser" title="'.$INFO['usersnamespace'].':',$event->data[1]);
    }
    function dokuwiki_started_before(&$event, $param){
        global $INFO;
        $INFO['usersnamespace']=preg_replace('{^[:/]?(.+?)[:/]?$}','\1',$this->getConf('usersnamespace'));
    }
    
    function action_act_preprocess_after(&$event, $param){
        global $MSG;
        global $INFO;
        global $lang;
        
        if ($event->data=='login' and isset($MSG)) {
            $flagOK=0;
            foreach($MSG as $msg) {
                if (    $msg['lvl']=='success' 
                    and (    $msg['msg']==$lang['regsuccess'] 
                          or $msg['msg']==$lang['regsuccess2'])) {
                    $flagOK=1;
                        }            
        }
        if ($flagOK==1) {
            $INFO['userinfo']['name']=$_POST['fullname'];
            $INFO['userinfo']['mail']=$_POST['email'];
            $_SERVER['REMOTE_USER']=$_POST['login'];
            /* $pageName=':'.$INFO['usersnamespace'].':'.$_SERVER['REMOTE_USER']; */
            $pageName= .$INFO['usersnamespace'].':'.$_SERVER['REMOTE_USER'];

            $data = array($pageName);
            $text = trigger_event('HTML_PAGE_FROMTEMPLATE',$data,'pageTemplate',true);
            if ($text == '') {
                $text = '===== '.$INFO['userinfo']['name'].' =====';
            }
            saveWikiText($pageName,con('',$text,'',1),$lang['created'],'');
            $a=new admin_plugin_acl();
            $a->_acl_add($pageName, $_SERVER['REMOTE_USER'], 2);
        }
        }
    }
}

function mytpl_userhtmllink($user){
  global $INFO;
  global $auth;
  $userdata=$auth->getUserData($user);
  return '<a href="'.wl($INFO['usersnamespace'].':'.$user).'">'.htmlspecialchars($userdata['name']).'</a>';
}

function mytpl_userinfo(){
  global $lang;
  global $INFO;
  if($_SERVER['REMOTE_USER']){
    print $lang['loggedinas'].': '.mytpl_userhtmllink($_SERVER['REMOTE_USER']);
    return true;
  }
  return false;
}

function mytpl_pageinfo(){
  global $conf;
  global $lang;
  global $INFO;
  global $REV;
  global $ID;
  
  // return if we are not allowed to view the page
  if (!auth_quickaclcheck($ID)) { return; }
  
  // prepare date and path
  $fn = $INFO['filepath'];
  if(!$conf['fullpath']){
    if($REV){
      $fn = str_replace(fullpath($conf['olddir']).'/','',$fn);
    }else{
      $fn = str_replace(fullpath($conf['datadir']).'/','',$fn);
    }
  }
  $fn = utf8_decodeFN($fn);
  $date = strftime($conf['dformat'],$INFO['lastmod']);

  // print it
  if($INFO['exists']){
    print $fn;
    print ' &middot; ';
    print $lang['lastmod'];
    print ': ';
    print $date;
    if($INFO['editor']){
      print ' '.$lang['by'].' ';
      print mytpl_userhtmllink($INFO['editor']);
    }else{
      print ' ('.$lang['external_edit'].')';
    }
    if($INFO['locked']){
      print ' &middot; ';
      print $lang['lockedby'];
      print ': ';
      print $INFO['locked'];
    }
    return true;
  }
  return false;
}

/* 
if you want see link to userpage to revisions and recent page then change file html.php 

// string 436 (replace string)
// print (empty($INFO['editor']))?('('.$lang['external_edit'].')'):htmlspecialchars($INFO['editor']);
   print (empty($INFO['editor']))?('('.$lang['external_edit'].')'):mytpl_userhtmllink($INFO['editor']);

// string 480 (replace string)
// print htmlspecialchars($info['user']);
   print mytpl_userhtmllink($info['user']);

// string 575 (replace string)
// print htmlspecialchars($recent['user']);
   print mytpl_userhtmllink($recent['user']);

if you want see link in toolbar in signature change file toolbar.php and use @USERPAGE@ in parameter signature
// string 201 (insert new string)
   $sig = str_replace('@USERPAGE@',$INFO['usersnamespace'].':'.$_SERVER['REMOTE_USER'],$sig);

*/
