1<?php
2/**
3 * DokuWiki Plugin log (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Adrian Lang <lang@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15
16function log_get_log_page($plugin, $id, $needspage = false) {
17    global $conf;
18    if (strpos($id, $conf['start']) !== strlen($id) - strlen($conf['start'])) {
19        // Force log file in ns:log for ids like ns
20        $id .= ':';
21    }
22    $logpage = getNS($id) . ':' . $plugin->getConf('logpage');
23    if (!page_exists($logpage)) {
24        if (!$needspage) {
25            return $logpage;
26        }
27        if (auth_quickaclcheck($logpage) < AUTH_CREATE) {
28            throw new Exception($plugin->getLang('e_not_writable'));
29        }
30
31        global $conf;
32
33        $locale = DOKU_PLUGIN . 'log/lang/' . $conf['lang'] . '/log.txt';
34        if (!file_exists($locale)) {
35            $locale = DOKU_PLUGIN . 'log/lang/en/log.txt';
36        }
37
38        $caption = useHeading('content') ? p_get_first_heading($id,true) : $id;
39
40        saveWikiText($logpage,
41                     str_replace(array('@@CAPTION@@', '@@ID@@'),
42                                 array($caption, $id),
43                                 file_get_contents($locale)),
44                     $plugin->getLang('created_summary'));
45    }
46    return $logpage;
47}
48
49function log_add_log_entry($plugin) {
50    global $ID;
51    global $USERINFO;
52    if (!checkSecurityToken() || !isset($_POST['log_text'])) {
53        return;
54    }
55    $text = $_POST['log_text'];
56    $log_id = log_get_log_page($plugin, $ID, true);
57    if (auth_quickaclcheck($log_id) < AUTH_EDIT) {
58        throw new Exception($plugin->getLang('e_not_writable'));
59    }
60    $log_text = rawWiki($log_id);
61    $str = preg_split('/(\n {2,}[-*] *)/', $log_text, 2, PREG_SPLIT_DELIM_CAPTURE);
62    if (count($str) === 1) {
63        $str = array($log_text, "\n  * ", '');
64    }
65    list($pre, $lstart, $post) = $str;
66    $log_text = $pre . $lstart . dformat() . ' ';
67    if (!isset($_SERVER['REMOTE_USER'])) {
68        $log_text .= '//' . clientIP(true) . '//';
69    } else {
70        if ($plugin->getConf('userpage') !== '') {
71            $log_text .= '[[' . sprintf($plugin->getConf('userpage'), $_SERVER['REMOTE_USER']) . '|' . $USERINFO['name'] . ']]';
72        } else {
73            $log_text .= '//' . $USERINFO['name'] . '//';
74        }
75    }
76    $log_text .= ': ' . $text;
77    if ($post !== '') {
78        $log_text .= $lstart . $post;
79    }
80
81    // save the log page (reset ID to avoid problems with plugins that
82    // intercept IO_WIKIPAGE_WRITE but rely on $ID
83    $oldid = $ID;
84    $ID = $log_id;
85    saveWikiText($log_id, $log_text, $plugin->getLang('summary'));
86    $ID = $oldid;
87}
88