1<?php
2/**
3 * DokuWiki Plugin showpageafterlogin (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Sam B. <sam@s-blu.de>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12class action_plugin_showpageafterlogin extends DokuWiki_Action_Plugin
13{
14
15    /**
16     * Registers a callback function for ACTION_SHOW_REDIRECT to get notified after a login/logout happened
17     *
18     * @param Doku_Event_Handler $controller DokuWiki's event controller object
19     * @return void
20     */
21    public function register(Doku_Event_Handler $controller)
22    {
23        $controller->register_hook('ACTION_SHOW_REDIRECT', 'BEFORE', $this, 'handle_redirect');
24    }
25
26    /**
27     * Change the Page ID for the redirect after login to the configurated value.
28     *
29     * Checks if a maximum Count is configured and if the calling user is within this count
30     *
31     * @param Doku_Event $event event object by reference
32     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
33     *                           handler was registered]
34     * @return void
35     */
36    public function handle_redirect(Doku_Event $event)
37    {
38        global $INPUT;
39        global $INFO;
40
41        if ($INPUT->server->has('REMOTE_USER') && $event->data['preact'] == 'redirect') {
42            $pageid = $this->getConf('page_after_login');
43            $displayCountConfig = $this->getConf('login_display_count');
44
45
46            if ($displayCountConfig == 0) {
47
48                $event->data['id'] = $pageid;
49
50            } else if ($displayCountConfig > 0) {
51
52                $displayCount = showpageafterlogin_read_json();
53                $username = $INFO['client'];
54
55                if ($username && !isset($displayCount[$username])) {
56                    $displayCount[$username] = 0;
57                }
58
59                if ($displayCount[$username] <= $displayCountConfig) {
60                    $displayCount[$username]++;
61                    $event->data['id'] = $pageid;
62                    showpageafterlogin_save_json($displayCount);
63                }
64            }
65        }
66    }
67}
68
69function showpageafterlogin_read_json()
70{
71    $json = new JSON(JSON_LOOSE_TYPE);
72    $file = @file_get_contents(DOKU_PLUGIN . "showpageafterlogin/showpageafterlogincount.json");
73    if (!$file) return Array();
74    return $json->decode($file);
75}
76
77// Save the statistics into the JSON file
78function showpageafterlogin_save_json($data)
79{
80    $json = new JSON();
81    $json = $json->encode($data);
82    file_put_contents(DOKU_PLUGIN . "showpageafterlogin/showpageafterlogincount.json", $json);
83}
84// vim:ts=4:sw=4:et:
85