Passed
Push — master ( beab7a...440203 )
by alexandr
14:48
created

Logger::log()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * RooCMS - Open Source Free Content Managment System
4
 * @copyright © 2010-2020 alexandr Belov aka alex Roosso. All rights reserved.
5
 * @author    alex Roosso <[email protected]>
6
 * @link      http://www.roocms.com
7
 * @license   http://www.gnu.org/licenses/gpl-3.0.html
8
 *
9
 * You should have received a copy of the GNU General Public License v3
10
 * along with this program.  If not, see http://www.gnu.org/licenses/
11
 */
12
13
14
//#########################################################
15
// Anti Hack
16
//---------------------------------------------------------
17
if(!defined('RooCMS')) {
18
	die('Access Denied');
19
}
20
//#########################################################
21
22
23
class Logger {
24
25
	# stock
26
	private	$log = [];
27
28
29
	
30
	/**
31
	 * Logger constructor.
32
	 */
33
	public function __construct() {
34
		# register handler for logs
35
		register_shutdown_function(array($this,'save'));
36
	}
37
38
	/**
39
	 * Log error
40
	 *
41
	 * @param      $subj
42
	 * @param bool $save - on/off write error in db
43
	 */
44
	public function error($subj, $save=true) {
45
		$_SESSION['error'][] = $subj;
46
		if($save) {
47
			$this->log($subj, "error");
48
		}
49
	}
50
51
52
	/**
53
	 * Log info
54
	 *
55
	 * @param      $subj
56
	 * @param bool $save - on/off write notice in db
57
	 */
58
	public function info($subj, $save=true) {
59
		$_SESSION['info'][] = $subj;
60
		if($save) {
61
			$this->log($subj, "info");
62
		}
63
	}
64
65
66
	/**
67
	 * Add msg to log
68
	 *
69
	 * @param        $subj
70
	 * @param string $type
71
	 */
72
	public function log($subj, $type="log") {
73
74
		# check type msg
75
		if($type != "info" && $type != "error") {
76
			$type="log";
77
		}
78
79
		$this->log[] = array("subj" => $subj, "type"=>$type);
80
	}
81
82
83
	/**
84
	 * Save log into database
85
	 */
86
	public function save() {
87
88
		global $db, $roocms, $parse;
89
90
		if(!empty($this->log)) {
91
92
			$dump = [];
93
			$uid = (isset($_SESSION['uid'])) ? $_SESSION['uid'] : 0 ;
94
95
			foreach($this->log AS $value) {
96
				$dump[] = "('".$uid."', '".$value["subj"]."', '".$value["type"]."', '".time()."', '".$roocms->userip."')";
97
			}
98
99
			# insert log msg in to db
100
			$db->query("INSERT INTO ".LOG_TABLE." (uid, message, type_log, date_log, user_ip) VALUES ".implode(", ", $dump));
101
		}
102
103
		# Close connection to DB (recommended)
104
		$db->close();
105
	}
106
}
107