Hello ,
I am trying to write a plugin that will allow any user (logged in or not) to send a feedback of a wiki page to the Creator of the page.
I am able to retrieve the mail ID of the Creator when the user is logged in so far (i am using global $auth, p_get_metadata($id), getUserData("username"). However, when i want i want to retrieve the information of the Creator of a page when the user is not logged in, the $auth global variable shows null.
Actually, i installed the plugin feedback (
https://www.dokuwiki.org/plugin:feedback) and changed the file action.php. Below is the code :
<?php
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
class action_plugin_feedback extends DokuWiki_Action_Plugin {
/**
* Registers a callback function for a given event
*
* @param Doku_Event_Handler $controller DokuWiki's event controller object
* @return void
*/
public function register(Doku_Event_Handler $controller) {
$controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'handle_start');
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax');
}
/**
* Add some info about the current page to the info array if feedback can be given
*
* @param Doku_Event $event event object by reference
* @param mixed $param [the parameters passed as fifth argument to register_hook() when this
* handler was registered]
* @return void
*/
public function handle_start(Doku_Event &$event, $param) {
global $ID;
global $JSINFO;
global $ACT;
// only on show
if($ACT != 'show') return;
// allow anonymous feedback?
if(!$_SERVER['REMOTE_USER'] && !$this->getConf('allowanon')) return;
// any contact defined?
if(!$this->getFeedbackContact($ID)) return;
$JSINFO['plugin_feedback'] = true;
}
/**
* [Custom event handler which performs action]
*
* @param Doku_Event $event event object by reference
* @param mixed $param [the parameters passed as fifth argument to register_hook() when this
* handler was registered]
* @return void
*/
public function handle_ajax(Doku_Event &$event, $param) {
// our event?
if($event->data != 'plugin_feedback') return;
$event->preventDefault();
$event->stopPropagation();
// allow anonymous feedback?
if(!$_SERVER['REMOTE_USER'] && !$this->getConf('allowanon')) {
http_status(400);
die('no anonymous access');
}
// get submitted data
global $INPUT;
$id = $INPUT->str('id');
$feedback = $INPUT->str('feedback');
// get the responsible contact
$contact = $this->getFeedbackContact($id);
if(!$contact) {
http_status(400);
die('no contact defined');
}
// get info on user
$user = null;
if($_SERVER['REMOTE_USER']) {
/** @var DokuWiki_Auth_Plugin $auth */
global $auth;
$user = $auth->getUserData($_SERVER['REMOTE_USER']);
if(!$user) $user = null;
}
// send the mail
$pageUrl = $this->getFullLinkPage($id); // My code
$mailer = new Mailer();
$mailer->to($contact);
if($user) $mailer->setHeader('Reply-To', $user['mail']);
$mailer->subject($this->getLang('subject'));
$mailer->setBody(
io_readFile($this->localFN('mail')),
array('PAGE' => $id, 'FEEDBACK' => $feedback, 'PAGEURL' => $pageUrl) // My code : added PAGEURL
);
$mailer->send();
header('Content-Type: text/html; charset=utf-8');
echo $this->getLang('thanks');
}
/**
* My code
* Get the responsible contact for givven ID
* @param $id
* @return false|string
*/
public function getFeedbackContact($id) {
//$conf = confToHash(DOKU_CONF . 'plugin_feedback.conf');
// get info on user
$user = null;
if($_SERVER['REMOTE_USER']) {
global $auth;
$user = $auth->getUserData($_SERVER['REMOTE_USER']);
//var_dump($user);
if(!$user) $user = null;
}
//var_dump( $auth );
$pageInfo = p_get_metadata( $id );
//var_dump($pageInfo);
//retrieve page creator mail adress
if( $pageInfo && $auth )
{
$userdata = $auth->getUserData($pageInfo['user']);
//var_dump($userdata);
if( $userdata )
$userMail = $userdata['mail'];
}
if( $userMail ) return $userMail;
return false;
}
/**
* prints or returns the action link
*
* Alternatively you can add the plugin_feedback class to any object in the DOM and it will be used
* for triggering the feedback dialog
*
* @param bool $return
* @return string
*/
public function tpl($return = false) {
global $ID;
global $ACT;
// only on show
if($ACT != 'show') return;
// allow anonymous feedback?
if(!$_SERVER['REMOTE_USER'] && !$this->getConf('allowanon')) return;
// any contact defined?
//var_dump($this->getFeedbackContact($ID));echo "ici";
if(!$this->getFeedbackContact($ID)) return;
$html = '<a href="#" class="plugin_feedback">' . $this->getLang('feedback') . '</a>';
if($return) return $html;
echo $html;
return '';
}
/**
* My code
* This function return the full link to the page concerned by the feedback
* @param $id (page)
* @return string
*/
public function getFullLinkPage($id){
$link = wl($id,'',true);
return $link;
}
}
// vim:ts=4:sw=4:et:
In the code when the user is not logged in then the link "send feedback" is not visible because the function getFeedbackContact($id) returns false. With the var_dump($auth) i saw that $auth = null.
Thanks for helping me, as i need this only to finish my plugin.
Awaiting for an early response.
Thank you
Help53