1<?php
2/**
3 * Copyright (c) 2020. ComboStrap, Inc. and its affiliates. All Rights Reserved.
4 *
5 * This source code is licensed under the GPL license found in the
6 * COPYING  file in the root directory of this source tree.
7 *
8 * @license  GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html)
9 * @author   ComboStrap <support@combostrap.com>
10 *
11 */
12
13namespace ComboStrap;
14
15require_once(__DIR__ . '/PluginUtility.php');
16
17class LogUtility
18{
19
20    /**
21     * Constant for the function {@link msg()}
22     * -1 = error, 0 = info, 1 = success, 2 = notify
23     * (Not even in order of importance)
24     */
25    const LVL_MSG_ERROR = 4; //-1;
26    const LVL_MSG_WARNING = 3; //2;
27    const LVL_MSG_SUCCESS = 2; //1;
28    const LVL_MSG_INFO = 1; //0;
29    const LVL_MSG_DEBUG = 0; //3;
30
31
32    /**
33     * Id level to name
34     */
35    const LVL_NAME = array(
36        0 => "debug",
37        1 => "info",
38        3 => "warning",
39        2 => "success",
40        4 => "error"
41    );
42
43    /**
44     * Id level to name
45     * {@link msg()} constant
46     */
47    const LVL_TO_MSG_LEVEL = array(
48        0 => 3,
49        1 => 0,
50        2 => 1,
51        3 => 2,
52        4 => -1
53    );
54
55
56    const LOGLEVEL_URI_QUERY_PROPERTY = "loglevel";
57
58    /**
59     * Send a message to a manager and log it
60     * Fail if in test
61     * @param string $message
62     * @param int $level - the level see LVL constant
63     * @param string $canonical - the canonical
64     */
65    public static function msg($message, $level = self::LVL_MSG_ERROR, $canonical = "support")
66    {
67
68        /**
69         * Log to frontend
70         */
71        self::log2FrontEnd($message, $level, $canonical, true);
72
73        /**
74         * Log level passed for a page (only for file used)
75         * to not allow an attacker to see all errors in frontend
76         */
77        global $INPUT;
78        $loglevelProp = $INPUT->str(self::LOGLEVEL_URI_QUERY_PROPERTY, null);
79        if (!empty($loglevelProp)) {
80            $level = $loglevelProp;
81        }
82        /**
83         * TODO: Make it a configuration ?
84         */
85        if($level >= self::LVL_MSG_WARNING) {
86            self::log2file($message, $level, $canonical);
87        }
88
89        /**
90         * If test, we throw an error
91         */
92        if (defined('DOKU_UNITTEST')
93            && ($level >= self::LVL_MSG_WARNING)
94        ) {
95            throw new LogException(PluginUtility::$PLUGIN_NAME . " - " . $message);
96        }
97    }
98
99    /**
100     * Print log to a  file
101     *
102     * Adapted from {@link dbglog}
103     * Note: {@link dbg()} dbg print to the web page
104     *
105     * @param string $msg
106     * @param int $logLevel
107     * @param null $canonical
108     */
109    static function log2file($msg, $logLevel = self::LVL_MSG_INFO, $canonical = null)
110    {
111
112        if (defined('DOKU_UNITTEST') || $logLevel >= self::LVL_MSG_WARNING) {
113
114            $prefix = PluginUtility::$PLUGIN_NAME;
115            if (!empty($canonical)) {
116                $prefix .= ' - ' . $canonical;
117            }
118            $msg = $prefix . ' - ' . $msg;
119
120            global $INPUT;
121            global $conf;
122
123            $id = PluginUtility::getPageId();
124
125            $file = $conf['cachedir'] . '/debug.log';
126            $fh = fopen($file, 'a');
127            if ($fh) {
128                $sep = " - ";
129                fwrite($fh, date('c') . $sep . self::LVL_NAME[$logLevel] . $sep . $msg . $sep . $INPUT->server->str('REMOTE_ADDR') . $sep . $id . "\n");
130                fclose($fh);
131            }
132        }
133
134    }
135
136    /**
137     * @param $message
138     * @param $level
139     * @param $canonical
140     * @param bool $withIconURL
141     */
142    public static function log2FrontEnd($message, $level, $canonical="support", $withIconURL = true)
143    {
144        /**
145         * If we are not in the console
146         * and not in test
147         * we test that the message comes in the front end
148         * (example {@link \plugin_combo_frontmatter_test}
149         */
150        $isCLI = (php_sapi_name() == 'cli');
151        $print = true;
152        if ($isCLI) {
153            if (!defined('DOKU_UNITTEST')) {
154                $print = false;
155            }
156        }
157        if ($print) {
158            $htmlMsg = PluginUtility::getUrl("", PluginUtility::$PLUGIN_NAME, $withIconURL);
159            if ($canonical != null) {
160                $htmlMsg = PluginUtility::getUrl($canonical, ucfirst(str_replace(":", " ", $canonical)));
161            }
162
163            /**
164             * Adding page - context information
165             * We are not creating the page
166             * direction from {@link Page::createRequestedPageFromEnvironment()}
167             * because it throws an error message when the environment
168             * is not good, creating a recursive call.
169             */
170            $id = PluginUtility::getPageId();
171            if ($id!=null) {
172                $page = Page::createPageFromId($id);
173                if ($page != null) {
174                    $htmlMsg .= " - " . $page->getAnchorLink();
175                }
176            }
177
178            /**
179             *
180             */
181            $htmlMsg .= " - " . $message;
182            if ($level > self::LVL_MSG_DEBUG) {
183                $dokuWikiLevel = self::LVL_TO_MSG_LEVEL[$level];
184                msg($htmlMsg, $dokuWikiLevel, '', '', MSG_USERS_ONLY);
185            }
186        }
187    }
188
189    /**
190     * Log a message to the browser console
191     * @param $message
192     */
193    public static function log2BrowserConsole($message)
194    {
195        // TODO
196    }
197}
198