Log::irc()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 15
rs 8.8571
cc 6
eloc 9
nc 6
nop 1
1
<?php
2
/* zLibrary
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Affero General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Affero General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Affero General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
class Log
19
{
20
21
	public function __construct()
22
	{
23
		trigger_error('The class "log" may only be invoked statically.', E_USER_ERROR);
24
	}
25
26
	public static function log($text)
0 ignored issues
show
Best Practice introduced by
Using PHP4-style constructors that are named like the class is not recommend; better use the more explicit __construct method.
Loading history...
27
	{
28
		global $logfile;
29
		if (!file_exists($logfile) && !is_writable(dirname($logfile)))
30
			return; // Can't create the file
31
32
		if (is_writable($logfile))
33
			error_log(date("Ymd H:i:s") . " $text \n", 3, $logfile);
34
	}
35
36
	/*
37
	   Mapped by Eggdrop to log into #esc
38
	 */
39
	public static function irc($text)
40
	{
41
		global $ircLogFile, $ircLogFrom;
42
43
		$from = isset($ircLogFrom) ? $ircLogFrom : "";
44
45
		if (!isset($ircLogFile) || $ircLogFile == "")
46
			return;
47
48
		$text = self::addIRCColors($text);
49
		if (!is_writable($ircLogFile) && !is_writable(dirname($ircLogFile)))
50
			return;
51
52
		error_log("\n${from}$text\n", 3, $ircLogFile);
53
	}
54
55
56
	public static function ircAdmin($text)
57
	{
58
		global $ircAdminLogFile, $ircLogFrom;
59
60
		$from = isset($ircLogFrom) ? $ircLogFrom : "";
61
62
		if (!isset($ircAdminLogFile) || $ircAdminLogFile == "")
63
			return;
64
65
		$text = self::addIRCColors($text);
66
67
		if (!is_writable($ircAdminLogFile) && !is_writable(dirname($ircAdminLogFile)))
68
			return; // Can't create the file
69
70
		error_log("\n${from}$text\n", 3, $ircAdminLogFile);
71
	}
72
73
	public static $colors = array(
74
		"|r|" => "\x0305", // red
75
		"|g|" => "\x0303", // green
76
		"|w|" => "\x0300", // white
77
		"|b|" => "\x0302", // blue
78
		"|blk|" => "\x0301", // black
79
		"|c|" => "\x0310", // cyan
80
		"|y|" => "\x0308", // yellow
81
		"|o|" => "\x0307", // orange
82
		"|n|" => "\x03", // reset
83
	);
84
85
	/**
86
	 * @param string $msg
87
	 * @return string
88
	**/
89
	public static function addIRCColors($msg)
90
	{
91
		foreach (self::$colors as $color => $value)
92
			$msg = str_replace($color, $value, $msg);
93
94
		return $msg;
95
	}
96
97
	/**
98
	 * @param string $msg
99
	 * @return string
100
	**/
101
	public static function stripIRCColors($msg)
102
	{
103
		foreach (self::$colors as $color => $value)
104
			$msg = str_replace($color, "", $msg);
105
106
		return $msg;
107
	}
108
109
}