Browse Source

Added basic Log class (#143)

Tobias Reich 10 years ago
parent
commit
f479511454
3 changed files with 64 additions and 1 deletions
  1. 13 0
      php/database/log_table.sql
  2. 14 1
      php/modules/Database.php
  3. 37 0
      php/modules/Log.php

+ 13 - 0
php/database/log_table.sql

@@ -0,0 +1,13 @@
+# Dump of table lychee_log
+# Version 2.5
+# ------------------------------------------------------------
+
+CREATE TABLE IF NOT EXISTS `lychee_log` (
+  `id` int(11) NOT NULL AUTO_INCREMENT,
+  `time` int(11) NOT NULL,
+  `type` varchar(11) NOT NULL,
+  `function` varchar(100) NOT NULL,
+  `line` int(11) NOT NULL,
+  `text` TEXT DEFAULT '',
+  PRIMARY KEY (`id`)
+) ENGINE=MyISAM DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

+ 14 - 1
php/modules/Database.php

@@ -29,7 +29,7 @@ class Database extends Module {
 			if (!Database::createDatabase($database, $name)) exit('Error: Could not create database!');
 			if (!Database::createDatabase($database, $name)) exit('Error: Could not create database!');
 
 
 		# Check tables
 		# Check tables
-		if (!$database->query('SELECT * FROM lychee_photos, lychee_albums, lychee_settings LIMIT 0;'))
+		if (!$database->query('SELECT * FROM lychee_photos, lychee_albums, lychee_settings, lychee_log LIMIT 0;'))
 			if (!Database::createTables($database)) exit('Error: Could not create tables!');
 			if (!Database::createTables($database)) exit('Error: Could not create tables!');
 
 
 		return $database;
 		return $database;
@@ -119,6 +119,19 @@ if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
 		# Check dependencies
 		# Check dependencies
 		Module::dependencies(isset($database));
 		Module::dependencies(isset($database));
 
 
+		# Create log
+		if (!$database->query('SELECT * FROM lychee_log LIMIT 0;')) {
+
+			# Read file
+			$file	= __DIR__ . '/../database/log_table.sql';
+			$query	= @file_get_contents($file);
+
+			# Create table
+			if (!isset($query)||$query===false) return false;
+			if (!$database->query($query)) return false;
+
+		}
+
 		# Create settings
 		# Create settings
 		if (!$database->query('SELECT * FROM lychee_settings LIMIT 0;')) {
 		if (!$database->query('SELECT * FROM lychee_settings LIMIT 0;')) {
 
 

+ 37 - 0
php/modules/Log.php

@@ -0,0 +1,37 @@
+<?php
+
+###
+# @name			Log Module
+# @author		Tobias Reich
+# @copyright	2014 by Tobias Reich
+###
+
+if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
+
+class Log extends Module {
+
+	public static function error($database, $function, $line, $text = '') {
+
+		# Check dependencies
+		Module::dependencies(isset($database, $function, $line, $text));
+
+		# Get time
+		$sysstamp = time();
+
+		# Escape
+		$function	= mysqli_real_escape_string($database, $function);
+		$line		= mysqli_real_escape_string($database, $line);
+		$text		= mysqli_real_escape_string($database, $text);
+
+		# Save in database
+		$query	= "INSERT INTO lychee_log (time, type, function, line, text) VALUES ('$sysstamp', 'error', '$function', '$line', '$text');";
+		$result	= $database->query($query);
+
+		if (!$result) return false;
+		return $database->insert_id;
+
+	}
+
+}
+
+?>