Skip to content

Commit

Permalink
Implémentation du système de logs
Browse files Browse the repository at this point in the history
Voir la méthode slash.php : log($log_info,$log_title,$log_type,$clear).
Utilisation dans les modules $this->slash->log("mon log");
  • Loading branch information
wakdev committed Mar 27, 2013
1 parent 0236654 commit bcf8dfd
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 11 deletions.
31 changes: 31 additions & 0 deletions core/common/constants/logs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* @package SLASH-CMS
* @subpackage Constants
* @internal Constants Logs
* @version logs.php
* @author Julien Veuillet [http://www.wakdev.com]
* @copyright Copyright(C) 2009 - Today. All rights reserved.
* @license GNU/GPL
This program is free software : you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

define("SL_LOG_TYPE_INFO", "INFO");
define("SL_LOG_TYPE_ERROR", "ERROR");
define("SL_LOG_TYPE_DEBUG", "DEBUG");
define("SL_LOG_TYPE_SYSTEM", "SYSTEM");
?>
2 changes: 1 addition & 1 deletion core/common/constants/sl_constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
*/

include ("errors.php");

include ("logs.php");
?>
4 changes: 4 additions & 0 deletions core/config/sl_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class SLConfig {
public $site_path = "/"; //For exemple : "/slashcms/"

public $error_level = SL_DEBUG; //SL_DEBUG (development) | SL_INFO | SL_ERROR (production)

//Logs
public $logs = true;
public $logs_rotation = "week"; //week, day, hour, month
/*
@todo => Move var in database
var $site_name = "SLASH CMS";
Expand Down
75 changes: 75 additions & 0 deletions core/slash.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,81 @@ public function show_fatal_error ($message=null,$code=null) {

}

/**
* Log action
* @param string $log_info information
* @param string $log_title title
* @param string $log_type type
* @param boolean $clear Clear old logs ?
*/
public function log($log_info,$log_title=null,$log_type=SL_LOG_TYPE_INFO,$clear=true){

if (isset($this->properties["logs"]) && $this->properties["logs"] == true) {

$id_user = 0;
$log_url = $_SERVER["REQUEST_URI"];
$log_date = date("Y-m-d H:i:s",time());

if (isset($_SESSION["id_user"])) {$id_user=$_SESSION["id_user"];}
if (!isset($log_info) || $log_info=="") {$log_info="none";}
if ($log_title!== null){$log_info = $log_title." : ".$log_info; }

if ($clear) {$this->clear_log();}

$this->database->setQuery("
INSERT INTO ".$this->db_prefix."logs
(id,log_type,log_url,log_info,id_user,log_date) value
('','".$log_type."','".$log_url."','".$log_info."','".$id_user."','".$log_date."')");
if (!$this->database->execute()) {
$this->show_fatal_error("QUERY_ERROR",$this->database->getError());
}

}

}

/**
* Clear Log : see config $logs_rotation
*/
public function clear_log(){

if (isset($this->properties["logs_rotation"])) {
$log_rot = "-1 week";
switch ($this->properties["logs_rotation"]){
case "month":
$log_rot = "-1 month";
break;
case "week":
$log_rot = "-1 week";
break;
case "day":
$log_rot = "-1 day";
break;
case "hour":
$log_rot = "-1 hour";
break;
}

$expired_date = date('Y-m-d H:i:s', strtotime($log_rot));

$this->database->setQuery("SELECT * FROM ".$this->database_prefix."logs WHERE log_date<'".$expired_date."'");
if (!$this->database->execute()) {
$this->show_fatal_error("QUERY_ERROR",$this->database->getError());
}

$n_expired = $this->database->rowCount();

if( $n_expired > 0 ) {
$this->database->setQuery("DELETE FROM ".$this->db_prefix."logs WHERE log_date<'".$expired_date."'");
if (!$this->database->execute()) {
$this->show_fatal_error("QUERY_ERROR",$this->database->getError());
}else{
$this->log($n_expired." logs clear","SYSTEM",SL_LOG_TYPE_SYSTEM,false);
}
}

}
}

/**
* User admin infos
Expand Down
43 changes: 33 additions & 10 deletions db_slashcms.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- http://www.phpmyadmin.net
--
-- Client: localhost
-- Généré le: Ven 22 Mars 2013 à 12:24
-- Généré le: Mer 27 Mars 2013 à 11:59
-- Version du serveur: 5.5.24-log
-- Version de PHP: 5.4.3

Expand All @@ -17,7 +17,7 @@ SET time_zone = "+00:00";
/*!40101 SET NAMES utf8 */;

--
-- Base de données: `db_slashcms`
-- Base de données: `db_slashcms`
--

-- --------------------------------------------------------
Expand Down Expand Up @@ -85,7 +85,7 @@ CREATE TABLE IF NOT EXISTS `sl_articles` (
--

INSERT INTO `sl_articles` (`id`, `id_user`, `categories`, `title`, `content`, `date`, `enabled`) VALUES
(1, 1, '2', 'L''équipe', '<p>\r\n Du contenu...</p>\r\n', '2013-01-29 13:17:43', 1),
(1, 1, '2', 'L''équipe', '<p>\r\n Du contenu...</p>\r\n', '2013-01-29 13:17:43', 1),
(2, 1, '', 'Lorem ipsum', '<p>\r\n Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse non felis et erat interdum lobortis et non quam. Nulla lectus magna, scelerisque eu facilisis vitae, tristique at metus. Cras lacinia consequat sapien, rutrum lacinia nunc sodales sed. Nullam pellentesque dictum dolor, nec varius orci gravida sit amet. Aenean vulputate, est eget porttitor congue, augue quam dapibus augue, et venenatis mauris arcu sed nunc. Praesent tristique porta augue a tempor. Nullam rhoncus justo in tellus condimentum tincidunt. Fusce nec quam mi. Nunc eu nibh orci, eget sagittis felis. Aenean cursus luctus est, non feugiat quam feugiat vel.</p>\r\n', '2013-02-01 12:31:01', 1),
(3, 1, '2,1', 'Aliquam erat volutpat.', '<p>\r\n Nunc ante augue, imperdiet a euismod at, pharetra et diam. Phasellus tempus urna vitae mauris congue at egestas nibh rutrum. Vestibulum in diam eget neque tincidunt adipiscing. Sed vel facilisis lectus. Phasellus vulputate arcu eget neque pharetra lobortis blandit ligula dapibus. Mauris auctor feugiat vehicula. Sed pulvinar convallis velit id rutrum. Cras magna metus, vehicula at ultrices non, semper euismod magna. Mauris mollis mollis interdum. Proin mattis neque et magna rhoncus posuere. Aenean nec ipsum non ipsum consectetur mollis a ac dolor.</p>\r\n', '2013-02-01 12:31:24', 1),
(4, 1, '', 'Aliquam nec', '<p>\r\n ulla laoreet nisl pellentesque commodo. Maecenas vitae velit tellus, et sagittis nulla. Nulla sollicitudin molestie posuere. Quisque in ligula augue, ut accumsan eros. Praesent nisl metus, aliquam non porttitor sed, euismod in lacus. Suspendisse vel arcu et lorem aliquet egestas. Phasellus vitae ante nec augue rhoncus lacinia. Donec pulvinar accumsan justo, eget volutpat velit elementum at. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Etiam eleifend velit vel risus iaculis tempor. Morbi vel elit orci, sed vulputate elit.</p>\r\n', '2013-02-01 12:31:49', 1),
Expand Down Expand Up @@ -115,7 +115,14 @@ CREATE TABLE IF NOT EXISTS `sl_attachments` (
`position` int(11) unsigned NOT NULL,
`state` tinyint(4) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=13 ;
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=18 ;

--
-- Contenu de la table `sl_attachments`
--

INSERT INTO `sl_attachments` (`id`, `id_user`, `id_module`, `id_element`, `id_field`, `filename`, `position`, `state`) VALUES
(13, 0, 0, 0, '', 'git.txt', 1, 0);

-- --------------------------------------------------------

Expand Down Expand Up @@ -221,6 +228,22 @@ INSERT INTO `sl_lang` (`id`, `name`, `shortname`, `enabled`) VALUES

-- --------------------------------------------------------

--
-- Structure de la table `sl_logs`
--

CREATE TABLE IF NOT EXISTS `sl_logs` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`log_type` varchar(45) NOT NULL,
`log_url` varchar(255) NOT NULL,
`log_info` varchar(255) NOT NULL,
`id_user` int(10) unsigned NOT NULL,
`log_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Structure de la table `sl_menu`
--
Expand All @@ -245,9 +268,9 @@ CREATE TABLE IF NOT EXISTS `sl_menu` (

INSERT INTO `sl_menu` (`id`, `menu_id`, `pri_type`, `sec_type`, `parent`, `position`, `title`, `action`, `home`, `enabled`) VALUES
(1, 0, 1, 'horizontal', 0, 0, 'Principal', '#', 0, 1),
(2, 1, 2, 'url_self', 0, 0, 'Présentation', 'index.php?mod=sl_pages&id=1', 1, 1),
(3, 1, 2, 'url_self', 0, 1, 'Actualités', 'index.php?mod=sl_pages&id=2', 0, 1),
(5, 1, 2, 'url_self', 0, 2, 'Téléchargement', 'index.php?mod=sl_pages&id=3', 0, 1),
(2, 1, 2, 'url_self', 0, 0, 'Présentation', 'index.php?mod=sl_pages&id=1', 1, 1),
(3, 1, 2, 'url_self', 0, 1, 'Actualités', 'index.php?mod=sl_pages&id=2', 0, 1),
(5, 1, 2, 'url_self', 0, 2, 'Téléchargement', 'index.php?mod=sl_pages&id=3', 0, 1),
(6, 1, 2, 'url_self', 0, 4, 'Contact', 'index.php?mod=sl_pages&id=4', 0, 1),
(15, 1, 2, 'url_self', 0, 3, 'Documentation', 'index.php?mod=sl_pages&id=5', 0, 1);

Expand Down Expand Up @@ -341,9 +364,9 @@ CREATE TABLE IF NOT EXISTS `sl_pages` (
--

INSERT INTO `sl_pages` (`id`, `id_user`, `title`, `content`, `date`, `enabled`) VALUES
(1, 1, 'Présentation', '<p>\r\n Slash CMS, est une application web open source qui va vous permettre de cr&eacute;er<br />\r\n et g&eacute;rer votre site internet tr&egrave;s simplement.<br />\r\n <br />\r\n Avec ce syst&egrave;me de gestion de contenu sur internet, vous allez pouvoir mettre a disposition des visiteurs toutes les informations dont vous avez besoin de communiquer.<br />\r\n <br />\r\n Allant de la simple page &agrave; la galerie photo, en passant par des news flash et bien d&#39;autres modules,<br />\r\n vous pourrez afficher toutes vos donn&eacute;es sur votre site, sans avoir la moindre connaissance technique.</p>\r\n', '2013-01-29 13:08:34', 1),
(2, 1, 'Actualités', '<p>\r\n A venir.</p>\r\n', '2013-01-29 13:08:52', 1),
(3, 1, 'Téléchargement', '<p>\r\n A venir.</p>\r\n', '2013-01-29 13:09:06', 1),
(1, 1, 'Présentation', '<p>\r\n Slash CMS, est une application web open source qui va vous permettre de cr&eacute;er<br />\r\n et g&eacute;rer votre site internet tr&egrave;s simplement.<br />\r\n <br />\r\n Avec ce syst&egrave;me de gestion de contenu sur internet, vous allez pouvoir mettre a disposition des visiteurs toutes les informations dont vous avez besoin de communiquer.<br />\r\n <br />\r\n Allant de la simple page &agrave; la galerie photo, en passant par des news flash et bien d&#39;autres modules,<br />\r\n vous pourrez afficher toutes vos donn&eacute;es sur votre site, sans avoir la moindre connaissance technique.</p>\r\n', '2013-01-29 13:08:34', 1),
(2, 1, 'Actualités', '<p>\r\n A venir</p>\r\n', '2013-01-29 13:08:52', 1),
(3, 1, 'Téléchargement', '<p>\r\n A venir.</p>\r\n', '2013-01-29 13:09:06', 1),
(4, 1, 'Contact', '<p>\r\n Vous pouvez me contacter a cette adresse : weneedyou [arobase] slash-cms.com</p>\r\n', '2013-01-29 13:09:23', 1),
(5, 1, 'Documentation', '<p>\r\n A venir.</p>\r\n', '2013-01-29 22:56:12', 1);

Expand Down

1 comment on commit bcf8dfd

@wakdev
Copy link
Owner Author

@wakdev wakdev commented on bcf8dfd Mar 27, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Utilisation dans les modules : $this->slash->("log");
Ou plus complet : $this->slash->("log","titre",CONSTANT TYPE LOG);

Pour plus de lisibilité, je conseil de placer dans le titre le nom du module en cours.
L'activation des logs et paramétrage de la rotation est situé dans le fichier de config.

Please sign in to comment.