2011-03-02 01:20:16 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2011-03-13 19:25:34 +03:00
|
|
|
* ownCloud
|
|
|
|
*
|
|
|
|
* @author Frank Karlitschek
|
|
|
|
* @author Jakob Sack
|
|
|
|
* @copyright 2010 Frank Karlitschek karlitschek@kde.org
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library 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 AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* The following SQL statement is just a help for developers and will not be
|
|
|
|
* executed!
|
|
|
|
*
|
2011-04-16 12:41:41 +04:00
|
|
|
* CREATE TABLE `log` (
|
|
|
|
* `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
|
2011-05-15 17:03:12 +04:00
|
|
|
* `moment` DATETIME NOT NULL ,
|
2011-03-13 19:25:34 +03:00
|
|
|
* `appid` VARCHAR( 255 ) NOT NULL ,
|
2011-04-16 12:41:41 +04:00
|
|
|
* `user` VARCHAR( 255 ) NOT NULL ,
|
|
|
|
* `action` VARCHAR( 255 ) NOT NULL ,
|
|
|
|
* `info` TEXT NOT NULL
|
|
|
|
* ) ENGINE = MYISAM ;
|
2011-03-13 19:25:34 +03:00
|
|
|
*
|
|
|
|
*/
|
2011-03-02 01:20:16 +03:00
|
|
|
|
|
|
|
/**
|
2011-03-13 19:25:34 +03:00
|
|
|
* This class is for logging
|
2011-03-02 01:20:16 +03:00
|
|
|
*/
|
2011-07-29 23:36:03 +04:00
|
|
|
class OC_Log {
|
2011-03-02 01:20:16 +03:00
|
|
|
/**
|
2011-03-13 19:25:34 +03:00
|
|
|
* @brief adds an entry to the log
|
|
|
|
* @param $appid id of the app
|
|
|
|
* @param $subject username
|
|
|
|
* @param $predicate action
|
|
|
|
* @param $object = null; additional information
|
|
|
|
* @returns true/false
|
2011-03-02 01:20:16 +03:00
|
|
|
*
|
2011-03-13 19:25:34 +03:00
|
|
|
* This function adds another entry to the log database
|
2011-03-02 01:20:16 +03:00
|
|
|
*/
|
2011-04-22 01:03:45 +04:00
|
|
|
public static function add( $appid, $subject, $predicate, $object = ' ' ){
|
2011-07-28 00:44:51 +04:00
|
|
|
$query=OC_DB::prepare("INSERT INTO `*PREFIX*log`(moment,appid,user,action,info) VALUES(NOW(),?,?,?,?)");
|
2011-04-28 19:40:51 +04:00
|
|
|
$result=$query->execute(array($appid,$subject,$predicate,$object));
|
|
|
|
// Die if we have an error
|
|
|
|
if( PEAR::isError($result)) {
|
|
|
|
$entry = 'DB Error: "'.$result->getMessage().'"<br />';
|
|
|
|
$entry .= 'Offending command was: '.$query.'<br />';
|
|
|
|
error_log( $entry );
|
|
|
|
die( $entry );
|
|
|
|
}
|
2011-03-13 19:25:34 +03:00
|
|
|
return true;
|
|
|
|
}
|
2011-03-02 01:20:16 +03:00
|
|
|
|
|
|
|
/**
|
2011-03-13 19:25:34 +03:00
|
|
|
* @brief Fetches log entries
|
|
|
|
* @param $filter = array(); array with filter options
|
|
|
|
* @returns array with entries
|
|
|
|
*
|
|
|
|
* This function fetches the log entries according to the filter options
|
|
|
|
* passed.
|
2011-03-02 01:20:16 +03:00
|
|
|
*
|
2011-03-13 19:25:34 +03:00
|
|
|
* $filter is an associative array.
|
|
|
|
* The following keys are optional:
|
|
|
|
* - from: all entries after this date
|
|
|
|
* - until: all entries until this date
|
|
|
|
* - user: username (default: current user)
|
|
|
|
* - app: only entries for this app
|
2011-03-02 01:20:16 +03:00
|
|
|
*/
|
2011-03-13 19:25:34 +03:00
|
|
|
public static function get( $filter = array()){
|
2011-07-28 00:44:51 +04:00
|
|
|
$queryString='SELECT * FROM `*PREFIX*log` WHERE 1=1 ORDER BY moment DESC';
|
2011-04-22 01:03:45 +04:00
|
|
|
$params=array();
|
|
|
|
if(isset($filter['from'])){
|
2011-05-15 17:03:12 +04:00
|
|
|
$queryString.='AND moment>? ';
|
2011-04-22 01:03:45 +04:00
|
|
|
array_push($params,$filter('from'));
|
|
|
|
}
|
|
|
|
if(isset($filter['until'])){
|
2011-05-15 17:03:12 +04:00
|
|
|
$queryString.='AND moment<? ';
|
2011-04-22 01:03:45 +04:00
|
|
|
array_push($params,$filter('until'));
|
|
|
|
}
|
|
|
|
if(isset($filter['user'])){
|
|
|
|
$queryString.='AND user=? ';
|
|
|
|
array_push($params,$filter('user'));
|
|
|
|
}
|
|
|
|
if(isset($filter['app'])){
|
|
|
|
$queryString.='AND appid=? ';
|
|
|
|
array_push($params,$filter('app'));
|
|
|
|
}
|
|
|
|
$query=OC_DB::prepare($queryString);
|
2011-04-28 19:40:51 +04:00
|
|
|
$result=$query->execute($params)->fetchAll();
|
2011-05-15 17:03:12 +04:00
|
|
|
if(count($result)>0 and is_numeric($result[0]['moment'])){
|
2011-04-28 19:40:51 +04:00
|
|
|
foreach($result as &$row){
|
2011-07-29 23:36:03 +04:00
|
|
|
$row['moment']=OC_Util::formatDate($row['moment']);
|
2011-04-28 19:40:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
2011-04-22 01:03:45 +04:00
|
|
|
|
2011-03-02 01:20:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-13 19:25:34 +03:00
|
|
|
* @brief removes log entries
|
|
|
|
* @param $date delete entries older than this date
|
|
|
|
* @returns true/false
|
|
|
|
*
|
|
|
|
* This function deletes all entries that are older than $date.
|
2011-03-02 01:20:16 +03:00
|
|
|
*/
|
2011-03-13 19:25:34 +03:00
|
|
|
public static function deleteBefore( $date ){
|
2011-07-28 00:44:51 +04:00
|
|
|
$query=OC_DB::prepare("DELETE FROM `*PREFIX*log` WHERE moment<?");
|
2011-04-22 01:03:45 +04:00
|
|
|
$query->execute(array($date));
|
2011-03-13 19:25:34 +03:00
|
|
|
return true;
|
2011-03-02 01:20:16 +03:00
|
|
|
}
|
2011-06-07 23:48:21 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief removes all log entries
|
|
|
|
* @returns true/false
|
|
|
|
*
|
|
|
|
* This function deletes all log entries.
|
|
|
|
*/
|
|
|
|
public static function deleteAll(){
|
2011-07-28 00:44:51 +04:00
|
|
|
$query=OC_DB::prepare("DELETE FROM `*PREFIX*log`");
|
2011-06-07 23:48:21 +04:00
|
|
|
$query->execute();
|
|
|
|
return true;
|
|
|
|
}
|
2011-04-22 01:03:45 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief filter an array of log entries on action
|
|
|
|
* @param array $logs the log entries to filter
|
|
|
|
* @param array $actions an array of actions to filter for
|
|
|
|
* @returns array
|
|
|
|
*/
|
|
|
|
public static function filterAction($logs,$actions){
|
|
|
|
$filteredLogs=array();
|
|
|
|
foreach($logs as $log){
|
|
|
|
if(array_search($log['action'],$actions)!==false){
|
|
|
|
$filteredLogs[]=$log;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $filteredLogs;
|
|
|
|
}
|
2011-03-02 01:20:16 +03:00
|
|
|
}
|