1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Pete Prodoehl <pete@2xlnetworks.com> and Jean Marc Massou <massou@gmail.com>
5 */
6
7require_once(DOKU_PLUGIN . 'action.php');
8
9class action_plugin_randompage extends Dokuwiki_Action_Plugin
10{
11	/**
12	 * Register its handlers with the dokuwiki's event controller
13	 */
14	function register(Doku_Event_Handler $controller)
15	{
16		$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'init', 'header');
17	}
18
19	function init(Doku_Event $event, $args)
20	{
21		// Catch the good request
22		if ($_REQUEST['do'] == 'randompage' || $_REQUEST['do'] == 'nsrandompage') {
23			if ($args == 'header') {
24				$this->action_randompage($event, $args);
25			}
26		}
27	}
28
29
30	function action_randompage($event, $args)
31	{
32		global $conf;
33		global $ID;
34		global $INFO;
35
36		$data = array();
37		$dir = $conf['savedir'];
38
39		$data = file($dir . '/index/page.idx');
40
41		//if current page is in
42		function isCurNS($value)
43		{
44			global $INFO;
45			return stripos($value, $INFO['namespace']) === 0 ? true : false;
46		}
47
48		if ($INFO['namespace'] != null && $_REQUEST['do'] == 'nsrandompage') {
49			$data = array_filter($data, "isCurNS");
50		}
51
52		//We loops through ten random page...
53		$i = 1;
54		while ($i <= 10 & $i <> "ok") :
55			//echo $i;
56			$i++;
57
58			$id = rtrim($data[array_rand($data)]);
59			$testACL = auth_aclcheck($id, $_SERVER['REMOTE_USER'], $USERINFO['grps']);
60
61			if (($testACL > 1) and (file_exists(wikiFN($id)))) {
62				$i = "ok";
63			}
64
65		endwhile;
66
67		if ($testACL < 1) {
68			$id = $ID;
69		}
70
71		send_redirect(wl($id, '', true, '&'));
72		exit();
73	}
74
75	//Function from Php manual to get a random number in a Array
76	function array_rand($array, $lim = 1)
77	{
78		mt_srand((float) microtime() * 1000000);
79		for ($a = 0; $a <= $lim; $a++) {
80			$num[] = mt_srand(0, count($array) - 1);
81		}
82		return @$num;
83	}
84}
85