2015-12-16 00:12:21 +08:00
|
|
|
<?php
|
|
|
|
require_once( 'database.php' );
|
2017-10-11 01:39:17 +08:00
|
|
|
|
2017-10-11 03:11:59 +08:00
|
|
|
#$storage_cache = array();
|
2015-12-16 00:12:21 +08:00
|
|
|
class Storage {
|
2016-09-21 00:16:49 +08:00
|
|
|
public function __construct( $IdOrRow = NULL ) {
|
|
|
|
$row = NULL;
|
|
|
|
if ( $IdOrRow ) {
|
|
|
|
if ( is_integer( $IdOrRow ) or is_numeric( $IdOrRow ) ) {
|
2017-10-11 01:39:17 +08:00
|
|
|
|
2017-10-11 03:11:59 +08:00
|
|
|
#if ( isset( $storage_cache[$IdOrRow] ) ) {
|
|
|
|
#Warning("using cached object for $dOrRow");
|
|
|
|
#return $storage_cache[$IdOrRow];
|
|
|
|
#} else {
|
|
|
|
#Warning("Not using cached object for $dOrRow");
|
2016-09-21 00:16:49 +08:00
|
|
|
$row = dbFetchOne( 'SELECT * FROM Storage WHERE Id=?', NULL, array( $IdOrRow ) );
|
|
|
|
if ( ! $row ) {
|
|
|
|
Error("Unable to load Storage record for Id=" . $IdOrRow );
|
|
|
|
}
|
|
|
|
} elseif ( is_array( $IdOrRow ) ) {
|
|
|
|
$row = $IdOrRow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $row ) {
|
|
|
|
foreach ($row as $k => $v) {
|
|
|
|
$this->{$k} = $v;
|
|
|
|
}
|
2017-10-11 03:11:59 +08:00
|
|
|
#$storage_cache[$IdOrRow] = $this;
|
2016-09-21 00:16:49 +08:00
|
|
|
} else {
|
|
|
|
$this->{'Name'} = '';
|
|
|
|
$this->{'Path'} = '';
|
2017-10-12 22:32:48 +08:00
|
|
|
$this->{'Type'} = 'local';
|
2015-12-16 00:12:21 +08:00
|
|
|
}
|
2016-09-21 00:16:49 +08:00
|
|
|
}
|
2015-12-16 00:12:21 +08:00
|
|
|
|
2016-09-21 00:16:49 +08:00
|
|
|
public function Path() {
|
|
|
|
if ( isset( $this->{'Path'} ) and ( $this->{'Path'} != '' ) ) {
|
|
|
|
return $this->{'Path'};
|
|
|
|
} else if ( ! isset($this->{'Id'}) ) {
|
2016-09-22 04:00:23 +08:00
|
|
|
$path = ZM_DIR_EVENTS;
|
|
|
|
if ( $path[0] != '/' ) {
|
|
|
|
$this->{'Path'} = ZM_PATH_WEB.'/'.ZM_DIR_EVENTS;
|
|
|
|
} else {
|
|
|
|
$this->{'Path'} = ZM_DIR_EVENTS;
|
|
|
|
}
|
|
|
|
return $this->{'Path'};
|
|
|
|
|
2015-12-16 00:12:21 +08:00
|
|
|
}
|
2016-09-21 00:16:49 +08:00
|
|
|
return $this->{'Name'};
|
|
|
|
}
|
|
|
|
public function Name() {
|
|
|
|
if ( isset( $this->{'Name'} ) and ( $this->{'Name'} != '' ) ) {
|
|
|
|
return $this->{'Name'};
|
|
|
|
} else if ( ! isset($this->{'Id'}) ) {
|
|
|
|
return 'Default';
|
|
|
|
}
|
|
|
|
return $this->{'Name'};
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __call( $fn, array $args= NULL){
|
2017-10-18 01:09:14 +08:00
|
|
|
if ( count( $args ) ) {
|
|
|
|
$this->{$fn} = $args[0];
|
|
|
|
}
|
|
|
|
if ( array_key_exists( $fn, $this ) ) {
|
2016-09-21 00:16:49 +08:00
|
|
|
return $this->{$fn};
|
2017-10-18 01:09:14 +08:00
|
|
|
|
|
|
|
$backTrace = debug_backtrace();
|
|
|
|
$file = $backTrace[1]['file'];
|
|
|
|
$line = $backTrace[1]['line'];
|
|
|
|
Warning( "Unknown function call Storage->$fn from $file:$line" );
|
2016-09-21 00:16:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public static function find_all() {
|
|
|
|
$storage_areas = array();
|
|
|
|
$result = dbQuery( 'SELECT * FROM Storage ORDER BY Name');
|
|
|
|
$results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Storage' );
|
|
|
|
foreach ( $results as $row => $obj ) {
|
|
|
|
$storage_areas[] = $obj;
|
2017-10-11 02:06:21 +08:00
|
|
|
$storage_cache[$obj->Id()] = $obj;
|
2016-09-21 00:16:49 +08:00
|
|
|
}
|
|
|
|
return $storage_areas;
|
|
|
|
}
|
|
|
|
public function disk_usage_percent() {
|
|
|
|
$path = $this->Path();
|
2017-01-31 06:24:41 +08:00
|
|
|
if ( ! $path ) {
|
|
|
|
Warning("Storage::disk_usage_percent: path is empty");
|
|
|
|
return 0;
|
|
|
|
} else if ( ! file_exists( $path ) ) {
|
|
|
|
Warning("Storage::disk_usage_percent: path $path does not exist");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-10 22:38:13 +08:00
|
|
|
$total = $this->disk_total_space();
|
2016-09-21 00:16:49 +08:00
|
|
|
if ( ! $total ) {
|
|
|
|
Error("disk_total_space returned false for " . $path );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$free = disk_free_space( $path );
|
|
|
|
if ( ! $free ) {
|
|
|
|
Error("disk_free_space returned false for " . $path );
|
2016-02-03 22:43:02 +08:00
|
|
|
}
|
2016-09-21 00:16:49 +08:00
|
|
|
$usage = round(($total - $free) / $total * 100);
|
|
|
|
return $usage;
|
|
|
|
}
|
2017-10-10 22:38:13 +08:00
|
|
|
public function disk_total_space() {
|
|
|
|
if ( ! array_key_exists( 'disk_total_space', $this ) ) {
|
|
|
|
$this->{'disk_total_space'} = disk_total_space( $this->Path() );
|
|
|
|
}
|
|
|
|
return $this->{'disk_total_space'};
|
|
|
|
}
|
|
|
|
public function disk_used_space() {
|
|
|
|
# This isn't a function like this in php, so we have to add up the space used in each event.
|
2017-11-23 23:29:05 +08:00
|
|
|
if ( ! array_key_exists( 'DiskSpace', $this ) ) {
|
|
|
|
$used = $this->{'DiskSpace'};
|
|
|
|
if ( ! $used ) {
|
|
|
|
if ( $this->{'Type'} == 's3fs' ) {
|
|
|
|
$used = dbFetchOne('SELECT SUM(DiskSpace) AS DiskSpace FROM Events WHERE StorageId=? AND DiskSpace IS NOT NULL', 'DiskSpace', array($this->Id()) );
|
2017-10-24 02:02:36 +08:00
|
|
|
|
2017-11-23 23:29:05 +08:00
|
|
|
foreach ( Event::find_all( array( 'StorageId'=>$this->Id(), 'DiskSpace'=>null ) ) as $Event ) {
|
|
|
|
$Event->Storage( $this ); // Prevent further db hit
|
|
|
|
$used += $Event->DiskSpace();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$path = $this->Path();
|
|
|
|
$used = disk_total_space( $path ) - disk_free_space( $path );;
|
|
|
|
}
|
|
|
|
} # end if
|
|
|
|
$this->{'DiskSpace'} = $used;
|
2017-10-10 22:38:13 +08:00
|
|
|
}
|
2017-10-12 02:10:53 +08:00
|
|
|
|
2017-11-23 23:29:05 +08:00
|
|
|
return $this->{'DiskSpace'};
|
2017-10-10 22:38:13 +08:00
|
|
|
}
|
2015-12-16 00:12:21 +08:00
|
|
|
}
|
|
|
|
?>
|