diff options
author | Tom Needham <needham.thomas@gmail.com> | 2012-03-31 22:44:50 +0000 |
---|---|---|
committer | Tom Needham <needham.thomas@gmail.com> | 2012-03-31 22:44:50 +0000 |
commit | f7d8a8c5717ec3630f5f73905399c14c1f72aa72 (patch) | |
tree | 37b4e5ab862398ae0e4452b8ec6195747a80f84a /lib | |
parent | d20eea9761238e0569f538c6f8b1bb553068bf7b (diff) | |
parent | 4e327295c65b25fc5d6ceec5a8242eecf57b94e2 (diff) | |
download | nextcloud-server-f7d8a8c5717ec3630f5f73905399c14c1f72aa72.tar.gz nextcloud-server-f7d8a8c5717ec3630f5f73905399c14c1f72aa72.zip |
Merge branch 'master' into migration
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/app.php | 91 | ||||
-rw-r--r-- | lib/base.php | 73 | ||||
-rw-r--r-- | lib/db.php | 3 | ||||
-rw-r--r-- | lib/eventsource.php | 3 | ||||
-rw-r--r-- | lib/filecache.php | 47 | ||||
-rwxr-xr-x | lib/helper.php | 15 | ||||
-rw-r--r-- | lib/installer.php | 39 | ||||
-rw-r--r-- | lib/log.php | 71 | ||||
-rw-r--r-- | lib/log/owncloud.php | 79 | ||||
-rw-r--r-- | lib/log/syslog.php | 37 | ||||
-rw-r--r-- | lib/util.php | 2 |
11 files changed, 329 insertions, 131 deletions
diff --git a/lib/app.php b/lib/app.php index 3daf539aa21..6c882963a02 100755 --- a/lib/app.php +++ b/lib/app.php @@ -34,16 +34,20 @@ class OC_App{ static private $settingsForms = array(); static private $adminForms = array(); static private $personalForms = array(); + static private $appInfo = array(); /** * @brief loads all apps + * @param array $types * @returns true/false * * This function walks through the owncloud directory and loads all apps * it can find. A directory contains an app if the file /appinfo/app.php * exists. + * + * if $types is set, only apps of those types will be loaded */ - public static function loadApps(){ + public static function loadApps($types=null){ // Did we allready load everything? if( self::$init ){ return true; @@ -51,13 +55,15 @@ class OC_App{ // Our very own core apps are hardcoded foreach( array('files', 'settings') as $app ){ - require( $app.'/appinfo/app.php' ); + if(is_null($types) or self::isType($app,$types)){ + require( $app.'/appinfo/app.php' ); + } } // The rest comes here - $apps = OC_Appconfig::getApps(); + $apps = self::getEnabledApps(); foreach( $apps as $app ){ - if( self::isEnabled( $app )){ + if(is_null($types) or self::isType($app,$types)){ if(is_file(OC::$APPSROOT.'/apps/'.$app.'/appinfo/app.php')){ require( $app.'/appinfo/app.php' ); } @@ -71,6 +77,41 @@ class OC_App{ } /** + * check if an app is of a sepcific type + * @param string $app + * @param string/array $types + */ + public static function isType($app,$types){ + if(is_string($types)){ + $types=array($types); + } + $appData=self::getAppInfo($app); + if(!isset($appData['types'])){ + return false; + } + $appTypes=$appData['types']; + foreach($types as $type){ + if(array_search($type,$appTypes)!==false){ + return true; + } + } + return false; + } + + /** + * get all enabled apps + */ + public static function getEnabledApps(){ + $apps=array(); + $query = OC_DB::prepare( 'SELECT appid FROM *PREFIX*appconfig WHERE configkey = "enabled" AND configvalue="yes"' ); + $query->execute(); + while($row=$query->fetchRow()){ + $apps[]=$row['appid']; + } + return $apps; + } + + /** * @brief checks whether or not an app is enabled * @param $app app * @returns true/false @@ -265,24 +306,36 @@ class OC_App{ /** * @brief Read app metadata from the info.xml file * @param string $appid id of the app or the path of the info.xml file + * @param boolean path (optional) * @returns array */ - public static function getAppInfo($appid){ - if(is_file($appid)){ + public static function getAppInfo($appid,$path=false){ + if($path){ $file=$appid; }else{ - $file=OC::$APPSROOT.'/apps/'.$appid.'/appinfo/info.xml'; - if(!is_file($file)){ - return array(); + if(isset(self::$appInfo[$appid])){ + return self::$appInfo[$appid]; } + $file=OC::$APPSROOT.'/apps/'.$appid.'/appinfo/info.xml'; } $data=array(); $content=file_get_contents($file); + if(!$content){ + return; + } $xml = new SimpleXMLElement($content); $data['info']=array(); foreach($xml->children() as $child){ - $data[$child->getName()]=(string)$child; + if($child->getName()=='types'){ + $data['types']=array(); + foreach($child->children() as $type){ + $data['types'][]=$type->getName(); + } + }else{ + $data[$child->getName()]=(string)$child; + } } + self::$appInfo[$appid]=$data; return $data; } @@ -381,9 +434,8 @@ class OC_App{ */ public static function updateApps(){ // The rest comes here - $apps = OC_Appconfig::getApps(); - foreach( $apps as $app ){ - $installedVersion=OC_Appconfig::getValue($app,'installed_version'); + $versions = self::getAppVersions(); + foreach( $versions as $app=>$installedVersion ){ $appInfo=OC_App::getAppInfo($app); if (isset($appInfo['version'])) { $currentVersion=$appInfo['version']; @@ -396,6 +448,19 @@ class OC_App{ } /** + * get the installed version of all papps + */ + public static function getAppVersions(){ + $versions=array(); + $query = OC_DB::prepare( 'SELECT appid, configvalue FROM *PREFIX*appconfig WHERE configkey = "installed_version"' ); + $result = $query->execute(); + while($row = $result->fetchRow()){ + $versions[$row['appid']]=$row['configvalue']; + } + return $versions; + } + + /** * update the database for the app and call the update script * @param string appid */ diff --git a/lib/base.php b/lib/base.php index b07ac5af416..22f7f4ea486 100644 --- a/lib/base.php +++ b/lib/base.php @@ -229,6 +229,39 @@ class OC{ } } + public static function initTemplateEngine() { + // if the formfactor is not yet autodetected do the autodetection now. For possible forfactors check the detectFormfactor documentation + if(!isset($_SESSION['formfactor'])){ + $_SESSION['formfactor']=OC::detectFormfactor(); + } + // allow manual override via GET parameter + if(isset($_GET['formfactor'])){ + $_SESSION['formfactor']=$_GET['formfactor']; + } + + // Add the stuff we need always + OC_Util::addScript( "jquery-1.6.4.min" ); + OC_Util::addScript( "jquery-ui-1.8.16.custom.min" ); + OC_Util::addScript( "jquery-showpassword" ); + OC_Util::addScript( "jquery.infieldlabel.min" ); + OC_Util::addScript( "jquery-tipsy" ); + OC_Util::addScript( "oc-dialogs" ); + OC_Util::addScript( "js" ); + OC_Util::addScript( "eventsource" ); + OC_Util::addScript( "config" ); + //OC_Util::addScript( "multiselect" ); + OC_Util::addScript('search','result'); + OC_Util::addStyle( "styles" ); + OC_Util::addStyle( "multiselect" ); + OC_Util::addStyle( "jquery-ui-1.8.16.custom" ); + OC_Util::addStyle( "jquery-tipsy" ); + } + + public static function initSession() { + ini_set('session.cookie_httponly','1;'); + session_start(); + } + public static function init(){ // register autoloader spl_autoload_register(array('OC','autoload')); @@ -270,37 +303,10 @@ class OC{ self::checkInstalled(); self::checkSSL(); - self::checkUpgrade(); - ini_set('session.cookie_httponly','1;'); - session_start(); - - // if the formfactor is not yet autodetected do the autodetection now. For possible forfactors check the detectFormfactor documentation - if(!isset($_SESSION['formfactor'])){ - $_SESSION['formfactor']=OC::detectFormfactor(); - } - // allow manual override via GET parameter - if(isset($_GET['formfactor'])){ - $_SESSION['formfactor']=$_GET['formfactor']; - } - - - // Add the stuff we need always - OC_Util::addScript( "jquery-1.6.4.min" ); - OC_Util::addScript( "jquery-ui-1.8.16.custom.min" ); - OC_Util::addScript( "jquery-showpassword" ); - OC_Util::addScript( "jquery.infieldlabel.min" ); - OC_Util::addScript( "jquery-tipsy" ); - OC_Util::addScript( "oc-dialogs" ); - OC_Util::addScript( "js" ); - OC_Util::addScript( "eventsource" ); - OC_Util::addScript( "config" ); - //OC_Util::addScript( "multiselect" ); - OC_Util::addScript('search','result'); - OC_Util::addStyle( "styles" ); - OC_Util::addStyle( "multiselect" ); - OC_Util::addStyle( "jquery-ui-1.8.16.custom" ); - OC_Util::addStyle( "jquery-tipsy" ); + self::initSession(); + self::initTemplateEngine(); + self::checkUpgrade(); $errors=OC_Util::checkServer(); if(count($errors)>0) { @@ -333,8 +339,13 @@ class OC{ // Load Apps // This includes plugins for users and filesystems as well global $RUNTIME_NOAPPS; + global $RUNTIME_APPTYPES; if(!$RUNTIME_NOAPPS ){ - OC_App::loadApps(); + if($RUNTIME_APPTYPES){ + OC_App::loadApps($RUNTIME_APPTYPES); + }else{ + OC_App::loadApps(); + } } //make sure temporary files are cleaned up diff --git a/lib/db.php b/lib/db.php index bfcff678699..a7b7ae75da0 100644 --- a/lib/db.php +++ b/lib/db.php @@ -318,9 +318,6 @@ class OC_DB { // Make changes and save them to an in-memory file $file2 = 'static://db_scheme'; - if($file2 == ''){ - die('could not create tempfile in get_temp_dir() - aborting'); - } $content = str_replace( '*dbname*', $CONFIG_DBNAME, $content ); $content = str_replace( '*dbprefix*', $CONFIG_DBTABLEPREFIX, $content ); if( $CONFIG_DBTYPE == 'pgsql' ){ //mysql support it too but sqlite doesn't diff --git a/lib/eventsource.php b/lib/eventsource.php index 523f72403c3..cf10660b94c 100644 --- a/lib/eventsource.php +++ b/lib/eventsource.php @@ -32,6 +32,7 @@ class OC_EventSource{ private $fallBackId=0; public function __construct(){ + @ob_end_clean(); header('Cache-Control: no-cache'); $this->fallback=isset($_GET['fallback']) and $_GET['fallback']=='true'; if($this->fallback){ @@ -58,7 +59,7 @@ class OC_EventSource{ $type=null; } if($this->fallback){ - $response='<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack('.$this->fallBackId.',"'.$type.'","'.json_encode($data).'")</script>'.PHP_EOL; + $response='<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack('.$this->fallBackId.',"'.$type.'",'.json_encode($data).')</script>'.PHP_EOL; echo $response; }else{ if($type){ diff --git a/lib/filecache.php b/lib/filecache.php index 280a9929db0..4a4183cbdb5 100644 --- a/lib/filecache.php +++ b/lib/filecache.php @@ -59,8 +59,8 @@ class OC_FileCache{ $root=''; } $path=$root.$path; - $query=OC_DB::prepare('SELECT ctime,mtime,mimetype,size,encrypted,versioned,writable FROM *PREFIX*fscache WHERE path=?'); - $result=$query->execute(array($path))->fetchRow(); + $query=OC_DB::prepare('SELECT ctime,mtime,mimetype,size,encrypted,versioned,writable FROM *PREFIX*fscache WHERE path_hash=?'); + $result=$query->execute(array(md5($path)))->fetchRow(); if(is_array($result)){ return $result; }else{ @@ -111,8 +111,8 @@ class OC_FileCache{ } $mimePart=dirname($data['mimetype']); $user=OC_User::getUser(); - $query=OC_DB::prepare('INSERT INTO *PREFIX*fscache(parent, name, path, size, mtime, ctime, mimetype, mimepart,user,writable,encrypted,versioned) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)'); - $result=$query->execute(array($parent,basename($path),$path,$data['size'],$data['mtime'],$data['ctime'],$data['mimetype'],$mimePart,$user,$data['writable'],$data['encrypted'],$data['versioned'])); + $query=OC_DB::prepare('INSERT INTO *PREFIX*fscache(parent, name, path, path_hash, size, mtime, ctime, mimetype, mimepart,user,writable,encrypted,versioned) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)'); + $result=$query->execute(array($parent,basename($path),$path,md5($path),$data['size'],$data['mtime'],$data['ctime'],$data['mimetype'],$mimePart,$user,$data['writable'],$data['encrypted'],$data['versioned'])); if(OC_DB::isError($result)){ OC_Log::write('files','error while writing file('.$path.') to cache',OC_Log::ERROR); } @@ -162,8 +162,8 @@ class OC_FileCache{ $oldPath=$root.$oldPath; $newPath=$root.$newPath; $newParent=self::getParentId($newPath); - $query=OC_DB::prepare('UPDATE *PREFIX*fscache SET parent=? ,name=?, path=? WHERE path=?'); - $query->execute(array($newParent,basename($newPath),$newPath,$oldPath)); + $query=OC_DB::prepare('UPDATE *PREFIX*fscache SET parent=? ,name=?, path=?, path_hash=? WHERE path_hash=?'); + $query->execute(array($newParent,basename($newPath),$newPath,md5($newPath),md5($oldPath))); } /** @@ -285,12 +285,12 @@ class OC_FileCache{ * @return int */ private static function getFileId($path){ - $query=OC_DB::prepare('SELECT id FROM *PREFIX*fscache WHERE path=?'); + $query=OC_DB::prepare('SELECT id FROM *PREFIX*fscache WHERE path_hash=?'); if(OC_DB::isError($query)){ OC_Log::write('files','error while getting file id of '.$path,OC_Log::ERROR); return -1; } - $result=$query->execute(array($path)); + $result=$query->execute(array(md5($path))); if(OC_DB::isError($result)){ OC_Log::write('files','error while getting file id of '.$path,OC_Log::ERROR); return -1; @@ -367,8 +367,8 @@ class OC_FileCache{ } } $path=$root.$path; - $query=OC_DB::prepare('SELECT ctime,mtime,mimetype,size,encrypted,versioned,writable FROM *PREFIX*fscache WHERE path=?'); - $result=$query->execute(array($path))->fetchRow(); + $query=OC_DB::prepare('SELECT ctime,mtime,mimetype,size,encrypted,versioned,writable FROM *PREFIX*fscache WHERE path_hash=?'); + $result=$query->execute(array(md5($path)))->fetchRow(); if(is_array($result)){ if(isset(self::$savedData[$path])){ $result=array_merge($result,self::$savedData[$path]); @@ -389,8 +389,8 @@ class OC_FileCache{ } } $path=$root.$path; - $query=OC_DB::prepare('SELECT size FROM *PREFIX*fscache WHERE path=?'); - $result=$query->execute(array($path)); + $query=OC_DB::prepare('SELECT size FROM *PREFIX*fscache WHERE path_hash=?'); + $result=$query->execute(array(md5($path))); if($row=$result->fetchRow()){ return $row['size']; }else{//file not in cache @@ -469,6 +469,10 @@ class OC_FileCache{ * @param string root (optionak) */ public static function scan($path,$eventSource=false,&$count=0,$root=''){ + if($eventSource){ + $eventSource->send('scanning',array('file'=>$path,'count'=>$count)); + } + $lastSend=$count; if(!$root){ $view=OC_Filesystem::getView(); }else{ @@ -482,13 +486,14 @@ class OC_FileCache{ if($filename != '.' and $filename != '..'){ $file=$path.'/'.$filename; if($view->is_dir($file.'/')){ - if($eventSource){ - $eventSource->send('scanning',array('file'=>$file,'count'=>$count)); - } self::scan($file,$eventSource,$count,$root); }else{ $totalSize+=self::scanFile($file,$root); $count++; + if($count>$lastSend+25 and $eventSource){ + $lastSend=$count; + $eventSource->send('scanning',array('file'=>$path,'count'=>$count)); + } } } } @@ -579,8 +584,8 @@ class OC_FileCache{ $mtime=$view->filemtime($path); $isDir=$view->is_dir($path); $path=$root.$path; - $query=OC_DB::prepare('SELECT mtime FROM *PREFIX*fscache WHERE path=?'); - $result=$query->execute(array($path)); + $query=OC_DB::prepare('SELECT mtime FROM *PREFIX*fscache WHERE path_hash=?'); + $result=$query->execute(array(md5($path))); if($row=$result->fetchRow()){ $cachedMTime=$row['mtime']; return ($mtime>$cachedMTime); @@ -637,6 +642,14 @@ class OC_FileCache{ self::fileSystemWatcherWrite(array('path'=>$path),$root); } } + + /** + * clean old pre-path_hash entries + */ + public static function clean(){ + $query=OC_DB::prepare('DELETE FROM *PREFIX*fscache WHERE LENGTH(path_hash)<30'); + $query->execute(); + } } //watch for changes and try to keep the cache up to date diff --git a/lib/helper.php b/lib/helper.php index 66f31d929be..efff00c2fe6 100755 --- a/lib/helper.php +++ b/lib/helper.php @@ -432,6 +432,19 @@ class OC_Helper { self::$tmpFiles[]=$file; return $file; } + + /** + * create a temporary folder with an unique filename + * @return string + * + * temporary files are automatically cleaned up after the script is finished + */ + public static function tmpFolder(){ + $path=get_temp_dir().'/'.md5(time().rand()); + mkdir($path); + self::$tmpFiles[]=$path; + return $path.'/'; + } /** * remove all files created by self::tmpFile @@ -439,7 +452,7 @@ class OC_Helper { public static function cleanTmp(){ foreach(self::$tmpFiles as $file){ if(file_exists($file)){ - unlink($file); + self::rmdirr($file); } } } diff --git a/lib/installer.php b/lib/installer.php index 2a9676998f6..38e17130e3c 100644 --- a/lib/installer.php +++ b/lib/installer.php @@ -62,7 +62,7 @@ class OC_Installer{ //download the file if necesary if($data['source']=='http'){ - $path=OC_Helper::tmpFile('.zip'); + $path=OC_Helper::tmpFile(); if(!isset($data['href'])){ OC_Log::write('core','No href specified when installing app from http',OC_Log::ERROR); return false; @@ -76,14 +76,24 @@ class OC_Installer{ $path=$data['path']; } + //detect the archive type + $mime=OC_Helper::getMimeType($path); + if($mime=='application/zip'){ + rename($path,$path.'.zip'); + $path.='.zip'; + }elseif($mime=='application/x-gzip'){ + rename($path,$path.'.tgz'); + $path.='.tgz'; + }else{ + OC_Log::write('core','Archives of type '.$mime.' are not supported',OC_Log::ERROR); + return false; + } + //extract the archive in a temporary folder - $extractDir=tempnam(get_temp_dir(),'oc_installer_uncompressed_'); - unlink($extractDir); + $extractDir=OC_Helper::tmpFolder(); mkdir($extractDir); - $zip = new ZipArchive; - if($zip->open($path)===true){ - $zip->extractTo($extractDir); - $zip->close(); + if($archive=OC_Archive::open($path)){ + $archive->extract($extractDir); } else { OC_Log::write('core','Failed to open archive when installing app',OC_Log::ERROR); OC_Helper::rmdirr($extractDir); @@ -95,6 +105,17 @@ class OC_Installer{ //load the info.xml file of the app if(!is_file($extractDir.'/appinfo/info.xml')){ + //try to find it in a subdir + $dh=opendir($extractDir); + while($folder=readdir($dh)){ + if(substr($folder,0,1)!='.' and is_dir($extractDir.'/'.$folder)){ + if(is_file($extractDir.'/'.$folder.'/appinfo/info.xml')){ + $extractDir.='/'.$folder; + } + } + } + } + if(!is_file($extractDir.'/appinfo/info.xml')){ OC_Log::write('core','App does not provide an info.xml file',OC_Log::ERROR); OC_Helper::rmdirr($extractDir); if($data['source']=='http'){ @@ -102,7 +123,7 @@ class OC_Installer{ } return false; } - $info=OC_App::getAppInfo($extractDir.'/appinfo/info.xml'); + $info=OC_App::getAppInfo($extractDir.'/appinfo/info.xml',true); $basedir=OC::$APPSROOT.'/apps/'.$info['id']; //check if an app with the same id is already installed @@ -275,7 +296,7 @@ class OC_Installer{ if(is_file(OC::$APPSROOT."/apps/$app/appinfo/install.php")){ include(OC::$APPSROOT."/apps/$app/appinfo/install.php"); } - $info=OC_App::getAppInfo(OC::$APPSROOT."/apps/$app/appinfo/info.xml"); + $info=OC_App::getAppInfo($app); OC_Appconfig::setValue($app,'installed_version',$info['version']); return $info; } diff --git a/lib/log.php b/lib/log.php index 4e450a027f5..8bb2839be66 100644 --- a/lib/log.php +++ b/lib/log.php @@ -1,78 +1,39 @@ <?php /** - * ownCloud - * - * @author Robin Appelman - * @copyright 2012 Robin Appelman icewind1991@gmail.com - * - * 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/>. - * + * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. */ /** - *logging utilities + * logging utilities * - * Log is saved at data/owncloud.log (on default) + * Log is saved by default at data/owncloud.log using OC_Log_Owncloud. + * Selecting other backend is done with a config option 'log_type'. */ -class OC_Log{ +class OC_Log { const DEBUG=0; const INFO=1; const WARN=2; const ERROR=3; const FATAL=4; + static protected $class = null; + /** * write a message in the log * @param string $app * @param string $message * @param int level */ - public static function write($app,$message,$level){ - $minLevel=OC_Config::getValue( "loglevel", 2 ); - if($level>=$minLevel){ - $datadir=OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' ); - $logFile=OC_Config::getValue( "logfile", $datadir.'/owncloud.log' ); - $entry=array('app'=>$app,'message'=>$message,'level'=>$level,'time'=>time()); - $fh=fopen($logFile,'a'); - fwrite($fh,json_encode($entry)."\n"); - fclose($fh); - } - } - - /** - * get entries from the log in reverse chronological order - * @param int limit - * @param int offset - * @return array - */ - public static function getEntries($limit=50,$offset=0){ - $datadir=OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' ); - $logFile=OC_Config::getValue( "logfile", $datadir.'/owncloud.log' ); - $entries=array(); - if(!file_exists($logFile)){ - return array(); - } - $contents=file($logFile); - if(!$contents){//error while reading log - return array(); - } - $end=max(count($contents)-$offset-1,0); - $start=max($end-$limit,0); - for($i=$end;$i>$start;$i--){ - $entries[]=json_decode($contents[$i]); + public static function write($app, $message, $level) { + if (!self::$class) { + self::$class = 'OC_Log_'.ucfirst(OC_Config::getValue('log_type', 'owncloud')); + call_user_func(array(self::$class, 'init')); } - return $entries; + $log_class=self::$class; + $log_class::write($app, $message, $level); } } diff --git a/lib/log/owncloud.php b/lib/log/owncloud.php new file mode 100644 index 00000000000..5e143205563 --- /dev/null +++ b/lib/log/owncloud.php @@ -0,0 +1,79 @@ +<?php +/** + * ownCloud + * + * @author Robin Appelman + * @copyright 2012 Robin Appelman icewind1991@gmail.com + * + * 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/>. + * + */ + +/** + * logging utilities + * + * Log is saved at data/owncloud.log (on default) + */ + +class OC_Log_Owncloud { + static protected $logFile; + + /** + * Init class data + */ + public static function init() { + $datadir=OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' ); + self::$logFile=OC_Config::getValue( "logfile", $datadir.'/owncloud.log' ); + } + + /** + * write a message in the log + * @param string $app + * @param string $message + * @param int level + */ + public static function write($app, $message, $level) { + $minLevel=OC_Config::getValue( "loglevel", 2 ); + if($level>=$minLevel){ + $entry=array('app'=>$app, 'message'=>$message, 'level'=>$level,'time'=>time()); + $fh=fopen(self::$logFile, 'a'); + fwrite($fh, json_encode($entry)."\n"); + fclose($fh); + } + } + + /** + * get entries from the log in reverse chronological order + * @param int limit + * @param int offset + * @return array + */ + public static function getEntries($limit=50, $offset=0){ + self::init(); + $entries=array(); + if(!file_exists(self::$logFile)) { + return array(); + } + $contents=file($logFile); + if(!$contents) {//error while reading log + return array(); + } + $end=max(count($contents)-$offset-1, 0); + $start=max($end-$limit,0); + for($i=$end;$i>$start;$i--) { + $entries[]=json_decode($contents[$i]); + } + return $entries; + } +} diff --git a/lib/log/syslog.php b/lib/log/syslog.php new file mode 100644 index 00000000000..d1fb28d8b0a --- /dev/null +++ b/lib/log/syslog.php @@ -0,0 +1,37 @@ +<?php +/** + * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +class OC_Log_Syslog { + static protected $levels = array( + OC_Log::DEBUG => LOG_DEBUG, + OC_Log::INFO => LOG_INFO, + OC_Log::WARN => LOG_WARNING, + OC_Log::ERROR => LOG_ERR, + OC_Log::FATAL => LOG_CRIT, + ); + + /** + * Init class data + */ + public static function init() { + openlog('ownCloud', LOG_PID | LOG_CONS, LOG_USER); + // Close at shutdown + register_shutdown_function('closelog'); + } + + /** + * write a message in the log + * @param string $app + * @param string $message + * @param int level + */ + public static function write($app, $message, $level) { + $syslog_level = self::$levels[$level]; + syslog($syslog_level, '{'.$app.'} '.$message); + } +} diff --git a/lib/util.php b/lib/util.php index fa5b3daaab6..529b6d958fb 100644 --- a/lib/util.php +++ b/lib/util.php @@ -66,7 +66,7 @@ class OC_Util { * @return array */ public static function getVersion(){ - return array(3,00,3); + return array(3,00,4); } /** |