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 self::log2file($message, $level, $canonical); 83 84 /** 85 * If test, we throw an error 86 */ 87 if (defined('DOKU_UNITTEST') 88 && ($level >= self::LVL_MSG_WARNING) 89 ) { 90 throw new \RuntimeException(PluginUtility::$PLUGIN_NAME . " - " . $message); 91 } 92 } 93 94 /** 95 * Print log to a file 96 * 97 * Adapted from {@link dbglog} 98 * Note: {@link dbg()} dbg print to the web page 99 * 100 * @param string $msg 101 * @param int $logLevel 102 * @param null $canonical 103 */ 104 static function log2file($msg, $logLevel = self::LVL_MSG_INFO, $canonical = null) 105 { 106 107 if (defined('DOKU_UNITTEST') || $logLevel >= self::LVL_MSG_WARNING) { 108 109 $prefix = PluginUtility::$PLUGIN_NAME; 110 if (!empty($canonical)) { 111 $prefix .= ' - ' . $canonical; 112 } 113 $msg = $prefix . ' - ' . $msg; 114 115 global $INPUT; 116 global $conf; 117 118 $id = PluginUtility::getPageId(); 119 120 $file = $conf['cachedir'] . '/debug.log'; 121 $fh = fopen($file, 'a'); 122 if ($fh) { 123 $sep = " - "; 124 fwrite($fh, date('c') . $sep . self::LVL_NAME[$logLevel] . $sep . $msg . $sep . $INPUT->server->str('REMOTE_ADDR') . $sep . $id . "\n"); 125 fclose($fh); 126 } 127 } 128 129 } 130 131 /** 132 * @param $message 133 * @param $level 134 * @param $canonical 135 * @param bool $withIconURL 136 */ 137 public static function log2FrontEnd($message, $level, $canonical="support", $withIconURL = true) 138 { 139 /** 140 * If we are not in the console 141 * and not in test 142 * we test that the message comes in the front end 143 * (example {@link \plugin_combo_frontmatter_test} 144 */ 145 $isCLI = (php_sapi_name() == 'cli'); 146 $print = true; 147 if ($isCLI) { 148 if (!defined('DOKU_UNITTEST')) { 149 $print = false; 150 } 151 } 152 if ($print) { 153 $htmlMsg = PluginUtility::getUrl("", PluginUtility::$PLUGIN_NAME, $withIconURL); 154 if ($canonical != null) { 155 $htmlMsg = PluginUtility::getUrl($canonical, ucfirst(str_replace(":", " ", $canonical))); 156 } 157 158 /** 159 * Adding page - context information 160 * We are not creating the page 161 * direction from {@link Page::createRequestedPageFromEnvironment()} 162 * because it throws an error message when the environment 163 * is not good, creating a recursive call. 164 */ 165 $id = PluginUtility::getPageId(); 166 if ($id!=null) { 167 $page = Page::createPageFromId($id); 168 if ($page != null) { 169 $htmlMsg .= " - " . $page->getAnchorLink(); 170 } 171 } 172 173 /** 174 * 175 */ 176 $htmlMsg .= " - " . $message; 177 if ($level > self::LVL_MSG_DEBUG) { 178 $dokuWikiLevel = self::LVL_TO_MSG_LEVEL[$level]; 179 msg($htmlMsg, $dokuWikiLevel, '', '', MSG_USERS_ONLY); 180 } 181 } 182 } 183 184 /** 185 * Log a message to the browser console 186 * @param $message 187 */ 188 public static function log2BrowserConsole($message) 189 { 190 // TODO 191 } 192} 193