2014-08-30 13:23:38 +02:00
< ? php
2014-08-31 03:29:56 +02:00
/**
* Userhomepage plugin main file
* Previous authors : James GuanFeng Lin , Mikhail I . Izmestev , Daniel Stonier
* @ author : Simon Delage < simon . geekitude @ gmail . com >
* @ license : CC Attribution - Share Alike 3.0 Unported < http :// creativecommons . org / licenses / by - sa / 3.0 />
*/
2014-08-30 17:07:19 +02:00
2014-08-31 04:10:39 +02:00
// must be run within Dokuwiki
2014-08-30 13:23:38 +02:00
if ( ! defined ( 'DOKU_INC' )) die ();
if ( ! defined ( 'DOKU_PLUGIN' )) define ( 'DOKU_PLUGIN' , DOKU_INC . 'lib/plugins/' );
2014-08-31 04:10:39 +02:00
2014-08-30 13:23:38 +02:00
require_once ( DOKU_PLUGIN . 'action.php' );
require_once ( DOKU_PLUGIN . '/acl/admin.php' );
2014-09-02 02:55:38 +02:00
2014-08-31 04:10:39 +02:00
class action_plugin_userhomepage extends DokuWiki_Action_Plugin {
2014-09-07 11:51:44 +02:00
2014-08-31 04:10:39 +02:00
function register ( & $controller ) {
2014-09-07 09:20:39 +02:00
$controller -> register_hook ( 'AUTH_LOGIN_CHECK' , 'AFTER' , $this , 'init' , array ());
2014-08-31 04:10:39 +02:00
$controller -> register_hook ( 'ACTION_ACT_PREPROCESS' , 'BEFORE' , $this , 'redirect' , array ());
2014-09-03 01:45:56 +02:00
}
2014-09-07 11:51:44 +02:00
2014-09-07 09:20:39 +02:00
function init ( & $event , $param ) {
global $conf ;
global $INFO ;
2014-09-07 16:46:00 +02:00
// CREATE PRIVATE NAMESPACE START PAGE TEMPLATES IF NEEDED
if (( $this -> getConf ( 'create_private_ns' )) && ( ! file_exists ( DOKU_INC . $this -> getConf ( 'templates_path' ) . '/userhomepage_private.txt' ))) {
2014-09-08 17:31:36 +02:00
// If old template exists, use it as source to create userhomepage_private.txt in templates_path
if ( file_exists ( DOKU_INC . $this -> getConf ( 'templatepath' ))) {
$source_private = DOKU_INC . $this -> getConf ( 'templatepath' );
} else {
2014-09-09 23:47:30 +02:00
$source_private = DOKU_INC . 'lib/plugins/userhomepage/lang/' . $conf [ 'lang' ] . '/userhomepage_private.default' ;
2014-09-08 17:31:36 +02:00
}
if ( ! copy ( $source_private , DOKU_INC . $this -> getConf ( 'templates_path' ) . '/userhomepage_private.txt' )) {
2014-09-03 03:05:30 +02:00
// echo ' An error occured while attempting to copy userhomepage_private.default to '.DOKU_INC.$this->getConf('templates_path').'/userhomepage_private.txt';
2014-09-03 02:52:22 +02:00
// } else {
// echo ' Successfully copied private template.';
2014-09-03 01:45:56 +02:00
}
}
2014-09-07 16:46:00 +02:00
// CREATE PUBLIC PAGE TEMPLATES IF NEEDED
2014-09-07 17:59:07 +02:00
if (( $this -> getConf ( 'create_public_page' )) && ( ! file_exists ( DOKU_INC . $this -> getConf ( 'templates_path' ) . '/userhomepage_public.txt' ))) {
2014-09-09 23:47:30 +02:00
if ( ! copy ( DOKU_INC . 'lib/plugins/userhomepage/lang/' . $conf [ 'lang' ] . '/userhomepage_public.default' , DOKU_INC . $this -> getConf ( 'templates_path' ) . '/userhomepage_public.txt' )) {
2014-09-03 03:05:30 +02:00
// echo ' An error occured while attempting to copy userhomepage_public.default to '.DOKU_INC.$this->getConf('templates_path').'/userhomepage_public.txt';
2014-09-03 02:52:22 +02:00
// } else {
// echo ' Successfully copied public template.';
2014-09-03 01:45:56 +02:00
}
}
2014-09-07 09:20:39 +02:00
// TARGETS
if ( $this -> getConf ( 'group_by_name' )) {
// private:s:simon or private:s:simon_delage
$this -> private_ns = cleanID ( $this -> getConf ( 'users_namespace' ) . ':' . substr ( $this -> privateNamespace (), 0 , 1 ) . ':' . $this -> privateNamespace ());
} else {
// private:simon or private:simon_delage
$this -> private_ns = cleanID ( $this -> getConf ( 'users_namespace' ) . ':' . $this -> privateNamespace ());
}
2014-09-07 11:51:44 +02:00
// ...:start.txt or ...:simon_delage.txt
2014-09-07 12:15:24 +02:00
$this -> private_page = $this -> private_ns . ':' . $this -> privateStart ();
2014-09-07 09:20:39 +02:00
// user:simon.txt
$this -> public_page = cleanID ( $this -> getConf ( 'public_pages_ns' ) . ':' . $_SERVER [ 'REMOTE_USER' ]);
// ACL
2014-09-07 11:14:20 +02:00
$acl = new admin_plugin_acl ();
2014-09-07 11:36:44 +02:00
// For private namespace
if (( $this -> getConf ( 'create_private_ns' )) && ( $this -> getConf ( 'set_permissions' ))) {
2014-09-07 09:20:39 +02:00
// If use_name_string is enabled, we can't use ACL wildcard
if ( $this -> getConf ( 'use_name_string' )) {
2014-09-07 11:51:44 +02:00
$ns = $this -> private_ns . ':*' ;
$acl -> _acl_add ( $ns , $INFO [ 'userinfo' ][ 'name' ], AUTH_DELETE );
} else {
$acl -> _acl_add ( cleanID ( $this -> getConf ( 'users_namespace' )) . ':%USER%:*' , '%USER%' , AUTH_DELETE );
}
2014-09-07 11:14:20 +02:00
$acl -> _acl_add ( cleanID ( $this -> getConf ( 'users_namespace' )) . ':*' , '@ALL' , ( int ) $this -> getConf ( 'set_permissions_others' ));
$acl -> _acl_add ( cleanID ( $this -> getConf ( 'users_namespace' )) . ':*' , '@user' , ( int ) $this -> getConf ( 'set_permissions_others' ));
2014-09-07 11:51:44 +02:00
}
2014-09-07 09:20:39 +02:00
// For public user pages
2014-09-07 11:51:44 +02:00
if (( $this -> getConf ( 'create_public_page' )) && ( $this -> getConf ( 'set_permissions_public' ))) {
2014-09-07 11:14:20 +02:00
$acl -> _acl_add ( cleanID ( $this -> getConf ( 'public_pages_ns' )) . ':%USER%' , '%USER%' , AUTH_EDIT );
2014-09-07 09:20:39 +02:00
$acl -> _acl_add ( cleanID ( $this -> getConf ( 'public_pages_ns' )) . ':*' , '@ALL' , AUTH_READ );
$acl -> _acl_add ( cleanID ( $this -> getConf ( 'public_pages_ns' )) . ':*' , '@user' , AUTH_READ );
}
// Some lines in conf/acl.auth.php file have probably been duplicated so let's read the file
$lines = file ( DOKU_INC . 'conf/acl.auth.php' );
// And only keep unique lines (OK, we loose an empty comment line...)
$lines = array_unique ( $lines );
// Write things back to conf/acl.auth.php
file_put_contents ( DOKU_INC . 'conf/acl.auth.php' , implode ( $lines ));
2014-08-31 04:10:39 +02:00
}
2014-09-07 11:51:44 +02:00
2014-08-31 04:10:39 +02:00
function redirect ( & $event , $param ) {
global $conf ;
global $INFO ;
2014-09-02 02:05:54 +02:00
$created = array ();
// If user just logged in
2014-08-31 04:10:39 +02:00
if (( $_SERVER [ 'REMOTE_USER' ] != null ) && ( $_REQUEST [ 'do' ] == 'login' )) {
2014-09-02 02:05:54 +02:00
// if private page doesn't exists, create it (from template)
if ( $this -> getConf ( 'create_private_ns' ) && ! page_exists ( $this -> private_page ) && ! checklock ( $this -> private_page ) && ! checkwordblock ()) {
2014-09-07 15:01:34 +02:00
// Target private start page template
2014-09-03 02:52:22 +02:00
$this -> private_page_template = DOKU_INC . $this -> getConf ( 'templates_path' ) . '/userhomepage_private.txt' ;
2014-09-02 02:05:54 +02:00
// Create private page
lock ( $this -> private_page );
2014-09-07 15:01:34 +02:00
saveWikiText ( $this -> private_page , $this -> applyTemplate ( 'private' ), $lang [ 'created' ]);
2014-09-02 02:05:54 +02:00
unlock ( $this -> private_page );
// Note that we created private page
$created [ 'private' ] = true ;
2014-08-31 04:10:39 +02:00
}
2014-09-02 02:05:54 +02:00
// Public page?
// If public page doesn't exists, create it (from template)
if ( $this -> getConf ( 'create_public_page' ) && ! page_exists ( $this -> public_page ) && ! checklock ( $this -> public_page ) && ! checkwordblock ()) {
2014-09-07 15:01:34 +02:00
// Target public page template
2014-09-03 02:52:22 +02:00
$this -> public_page_template = DOKU_INC . $this -> getConf ( 'templates_path' ) . '/userhomepage_public.txt' ;
2014-09-02 02:55:38 +02:00
// Create public page
lock ( $this -> public_page );
2014-09-07 15:01:34 +02:00
saveWikiText ( $this -> public_page , $this -> applyTemplate ( 'public' ), $lang [ 'created' ]);
2014-09-02 02:55:38 +02:00
unlock ( $this -> public_page );
2014-09-02 02:05:54 +02:00
// Note that we created public page
$created [ 'public' ] = true ;
2014-09-02 02:55:38 +02:00
}
2014-08-31 05:33:25 +02:00
// If Translation plugin is active, determine if we're at wikistart
if ( ! plugin_isdisabled ( 'translation' )) {
foreach ( explode ( ' ' , $conf [ 'plugin' ][ 'translation' ][ 'translations' ]) as $lang ){
if ( getID () === $lang . ':' . $conf [ 'start' ]) {
$wikistart = true ;
break ;
}
}
}
2014-09-02 02:05:54 +02:00
// If Public page was just created, redirect to it and edit
if ( $created [ 'public' ]) {
send_redirect ( wl ( $this -> public_page , 'do=edit' , false , '&' ));
// Else if private start page was just created and edit option is set, redirect to it and edit
} elseif (( $created [ 'private' ]) && ( $this -> getConf ( 'edit_before_create' ))) {
send_redirect ( wl ( $this -> private_page , 'do=edit' , false , '&' ));
// Else if the user was not at a specific page (beside wiki start) and private page exists, redirect to it.
} elseif ((( $_REQUEST [ 'id' ] == $conf [ 'start' ]) || ( ! isset ( $_REQUEST [ 'id' ])) || ( $wikistart )) && ( page_exists ( $this -> private_page ))) {
send_redirect ( wl ( $this -> private_page ));
2014-08-30 13:23:38 +02:00
}
}
2014-08-31 04:10:39 +02:00
}
2014-09-07 11:51:44 +02:00
2014-09-01 18:37:07 +02:00
function privateNamespace () {
2014-08-31 04:10:39 +02:00
if ( $this -> getConf ( 'use_name_string' )) {
global $INFO ;
2014-09-07 09:20:39 +02:00
$raw_string = cleanID ( $INFO [ 'userinfo' ][ 'name' ]);
2014-08-31 04:10:39 +02:00
// simon_delage
return $raw_string ;
} else {
// simon
return strtolower ( $_SERVER [ 'REMOTE_USER' ]);
2014-08-30 13:23:38 +02:00
}
2014-08-31 04:10:39 +02:00
}
2014-09-07 11:51:44 +02:00
2014-09-07 12:15:24 +02:00
function privateStart () {
2014-09-07 09:20:39 +02:00
if ( $this -> getConf ( 'use_start_page' )) {
2014-09-02 02:55:38 +02:00
global $conf ;
2014-09-07 09:20:39 +02:00
return cleanID ( $conf [ 'start' ]);
2014-09-02 02:55:38 +02:00
} else {
2014-09-07 09:20:39 +02:00
return $this -> privateNamespace ();
2014-09-03 03:50:59 +02:00
}
2014-09-02 02:55:38 +02:00
}
2014-09-07 11:51:44 +02:00
2014-09-07 15:01:34 +02:00
function applyTemplate ( $type ) {
2014-09-01 18:37:07 +02:00
global $INFO ;
2014-09-06 20:39:56 +02:00
global $lang ;
2014-09-07 09:20:39 +02:00
if ( $type == 'private' ) {
2014-09-07 11:51:44 +02:00
$content = io_readFile ( $this -> private_page_template , false );
} elseif ( $type == 'public' ) {
$content = io_readFile ( $this -> public_page_template , false );
}
2014-09-09 21:51:02 +02:00
$content = str_replace ( '@TARGETPRIVATENS@' , $this -> private_ns , $content );
$content = str_replace ( '@TARGETPUBLICPAGE@' , $this -> public_page , $content );
$content = str_replace ( '@TARGETPUBLICNS@' , cleanID ( $this -> getConf ( 'public_pages_ns' )), $content );
// Improved template process to use standard replacement patterns from https://www.dokuwiki.org/namespace_templates based on code proposed by Christian Nancy
2014-09-09 08:38:50 +02:00
// Build a fake data structure for the parser
$data = array ( 'tpl' => $content , 'id' => $this -> private_page );
2014-09-07 11:51:44 +02:00
// Use the built-in parser
$content = parsePageTemplate ( $data );
2014-09-01 18:37:07 +02:00
return $content ;
}
2014-09-07 11:51:44 +02:00
2014-08-30 13:23:38 +02:00
}