2015-12-16 00:12:21 +08:00
|
|
|
<?php
|
2018-04-11 04:05:37 +08:00
|
|
|
require_once('database.php');
|
2017-10-11 01:39:17 +08:00
|
|
|
|
2018-04-11 04:05:37 +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 ) {
|
2018-04-11 04:05:37 +08:00
|
|
|
global $storage_cache;
|
|
|
|
|
2016-09-21 00:16:49 +08:00
|
|
|
$row = NULL;
|
|
|
|
if ( $IdOrRow ) {
|
|
|
|
if ( is_integer( $IdOrRow ) or is_numeric( $IdOrRow ) ) {
|
|
|
|
$row = dbFetchOne( 'SELECT * FROM Storage WHERE Id=?', NULL, array( $IdOrRow ) );
|
|
|
|
if ( ! $row ) {
|
|
|
|
Error("Unable to load Storage record for Id=" . $IdOrRow );
|
|
|
|
}
|
2018-04-24 23:48:35 +08:00
|
|
|
} else if ( is_array($IdOrRow) ) {
|
2016-09-21 00:16:49 +08:00
|
|
|
$row = $IdOrRow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $row ) {
|
|
|
|
foreach ($row as $k => $v) {
|
|
|
|
$this->{$k} = $v;
|
|
|
|
}
|
2018-04-15 22:26:57 +08:00
|
|
|
$storage_cache[$row['Id']] = $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'};
|
|
|
|
}
|
|
|
|
|
2018-09-08 04:31:11 +08:00
|
|
|
public function __call( $fn, array $args= NULL ) {
|
|
|
|
if ( count($args) ) {
|
2017-10-18 01:09:14 +08:00
|
|
|
$this->{$fn} = $args[0];
|
|
|
|
}
|
2018-09-08 04:31:11 +08:00
|
|
|
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
|
|
|
|
2018-09-08 04:31:11 +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
|
|
|
}
|
2018-09-08 04:31:11 +08:00
|
|
|
|
2018-04-11 04:05:37 +08:00
|
|
|
public static function find_one( $parameters = null, $options = null ) {
|
|
|
|
global $storage_cache;
|
|
|
|
if (
|
|
|
|
( count($parameters) == 1 ) and
|
|
|
|
isset($parameters['Id']) and
|
|
|
|
isset($storage_cache[$parameters['Id']]) ) {
|
|
|
|
return $storage_cache[$parameters['Id']];
|
|
|
|
}
|
2018-09-08 04:31:11 +08:00
|
|
|
$results = Storage::find($parameters, $options);
|
2018-04-11 04:05:37 +08:00
|
|
|
if ( count($results) > 1 ) {
|
|
|
|
Error("Storage Returned more than 1");
|
|
|
|
return $results[0];
|
|
|
|
} else if ( count($results) ) {
|
|
|
|
return $results[0];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2018-09-08 04:31:11 +08:00
|
|
|
public static function find( $parameters = null, $options = null ) {
|
2017-12-23 03:38:42 +08:00
|
|
|
$sql = 'SELECT * FROM Storage ';
|
|
|
|
$values = array();
|
|
|
|
|
|
|
|
if ( $parameters ) {
|
|
|
|
$fields = array();
|
|
|
|
$sql .= 'WHERE ';
|
|
|
|
foreach ( $parameters as $field => $value ) {
|
|
|
|
if ( $value == null ) {
|
|
|
|
$fields[] = $field.' IS NULL';
|
2018-09-08 04:31:11 +08:00
|
|
|
} else if ( is_array($value) ) {
|
2017-12-23 03:38:42 +08:00
|
|
|
$func = function(){return '?';};
|
2018-04-18 00:37:30 +08:00
|
|
|
$fields[] = $field.' IN ('.implode(',', array_map($func, $value)). ')';
|
2017-12-23 03:38:42 +08:00
|
|
|
$values += $value;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$fields[] = $field.'=?';
|
|
|
|
$values[] = $value;
|
|
|
|
}
|
|
|
|
}
|
2018-04-18 00:37:30 +08:00
|
|
|
$sql .= implode(' AND ', $fields);
|
2018-09-08 04:31:11 +08:00
|
|
|
} # end if parameters
|
|
|
|
if ( $options ) {
|
|
|
|
if ( isset($options['order']) ) {
|
|
|
|
$sql .= ' ORDER BY ' . $options['order'];
|
|
|
|
} # end if options
|
|
|
|
if ( isset($options['limit']) ) {
|
|
|
|
if ( is_integer($options['limit']) or ctype_digit($options['limit']) ) {
|
|
|
|
$sql .= ' LIMIT ' . $limit;
|
|
|
|
} else {
|
|
|
|
$backTrace = debug_backtrace();
|
|
|
|
$file = $backTrace[1]['file'];
|
|
|
|
$line = $backTrace[1]['line'];
|
|
|
|
Error("Invalid value for limit($limit) passed to Control::find from $file:$line");
|
2018-09-10 01:59:10 +08:00
|
|
|
return array();
|
2018-09-08 04:31:11 +08:00
|
|
|
}
|
|
|
|
} # end if limit
|
|
|
|
} # end if options
|
|
|
|
$storage_areas = array();
|
2017-12-23 03:38:42 +08:00
|
|
|
$result = dbQuery($sql, $values);
|
2018-04-11 04:05:37 +08:00
|
|
|
if ( $result ) {
|
|
|
|
$results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Storage');
|
|
|
|
foreach ( $results as $row => $obj ) {
|
2018-09-08 04:31:11 +08:00
|
|
|
$storage_areas[] = $obj;
|
2018-04-11 04:05:37 +08:00
|
|
|
}
|
2016-09-21 00:16:49 +08:00
|
|
|
}
|
2018-09-08 04:31:11 +08:00
|
|
|
return $storage_areas;
|
|
|
|
} # end find()
|
|
|
|
|
2016-09-21 00:16:49 +08:00
|
|
|
public function disk_usage_percent() {
|
|
|
|
$path = $this->Path();
|
2017-01-31 06:24:41 +08:00
|
|
|
if ( ! $path ) {
|
2018-09-08 04:31:11 +08:00
|
|
|
Warning('Storage::disk_usage_percent: path is empty');
|
2017-01-31 06:24:41 +08:00
|
|
|
return 0;
|
2018-09-08 04:31:11 +08:00
|
|
|
} else if ( ! file_exists($path) ) {
|
2017-01-31 06:24:41 +08:00
|
|
|
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 ) {
|
2018-09-08 04:31:11 +08:00
|
|
|
Error('disk_total_space returned false for ' . $path );
|
2016-09-21 00:16:49 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2017-12-23 01:02:49 +08:00
|
|
|
$used = $this->disk_used_space();
|
|
|
|
$usage = round( ($used / $total) * 100);
|
2018-01-19 04:39:39 +08:00
|
|
|
//Logger::Debug("Used $usage = round( ( $used / $total ) * 100 )");
|
2016-09-21 00:16:49 +08:00
|
|
|
return $usage;
|
|
|
|
}
|
2018-09-08 04:31:11 +08:00
|
|
|
|
2017-10-10 22:38:13 +08:00
|
|
|
public function disk_total_space() {
|
2018-07-12 05:21:44 +08:00
|
|
|
if ( !array_key_exists('disk_total_space', $this) ) {
|
|
|
|
$path = $this->Path();
|
|
|
|
if ( file_exists($path) ) {
|
|
|
|
$this->{'disk_total_space'} = disk_total_space($path);
|
|
|
|
} else {
|
|
|
|
Error("Path $path does not exist.");
|
|
|
|
$this->{'disk_total_space'} = 0;
|
|
|
|
}
|
2017-10-10 22:38:13 +08:00
|
|
|
}
|
|
|
|
return $this->{'disk_total_space'};
|
|
|
|
}
|
2018-04-18 05:14:40 +08:00
|
|
|
|
2017-10-10 22:38:13 +08:00
|
|
|
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.
|
2018-07-12 05:21:44 +08:00
|
|
|
if ( ( !array_key_exists('disk_used_space', $this)) or !$this->{'disk_used_space'} ) {
|
2017-11-27 04:12:35 +08:00
|
|
|
if ( $this->{'Type'} == 's3fs' ) {
|
2018-04-18 05:14:40 +08:00
|
|
|
$this->{'disk_used_space'} = $this->disk_event_space();
|
2017-11-27 04:12:35 +08:00
|
|
|
} else {
|
|
|
|
$path = $this->Path();
|
2018-07-12 05:21:44 +08:00
|
|
|
if ( file_exists($path) ) {
|
|
|
|
$this->{'disk_used_space'} = disk_total_space($path) - disk_free_space($path);
|
|
|
|
} else {
|
|
|
|
Error("Path $path does not exist.");
|
|
|
|
$this->{'disk_used_space'} = 0;
|
|
|
|
}
|
2018-04-18 05:14:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->{'disk_used_space'};
|
|
|
|
} // end function disk_used_space
|
|
|
|
|
|
|
|
public function event_disk_space() {
|
|
|
|
# This isn't a function like this in php, so we have to add up the space used in each event.
|
|
|
|
if ( (! array_key_exists('DiskSpace', $this)) or (!$this->{'DiskSpace'}) ) {
|
|
|
|
$used = dbFetchOne('SELECT SUM(DiskSpace) AS DiskSpace FROM Events WHERE StorageId=? AND DiskSpace IS NOT NULL', 'DiskSpace', array($this->Id()) );
|
|
|
|
|
2018-09-08 04:31:11 +08:00
|
|
|
foreach ( Event::find(array('StorageId'=>$this->Id(), 'DiskSpace'=>null)) as $Event ) {
|
|
|
|
$Event->Storage($this); // Prevent further db hit
|
2018-04-18 05:14:40 +08:00
|
|
|
$used += $Event->DiskSpace();
|
2017-11-27 04:12:35 +08:00
|
|
|
}
|
2017-11-23 23:29:05 +08:00
|
|
|
$this->{'DiskSpace'} = $used;
|
2017-10-10 22:38:13 +08:00
|
|
|
}
|
2017-11-23 23:29:05 +08:00
|
|
|
return $this->{'DiskSpace'};
|
2018-04-18 05:14:40 +08:00
|
|
|
} // end function event_disk_space
|
2018-05-08 05:08:32 +08:00
|
|
|
|
2018-04-18 00:37:30 +08:00
|
|
|
public function Server() {
|
|
|
|
if ( ! array_key_exists('Server',$this) ) {
|
|
|
|
$this->{'Server'}= new Server( $this->{'ServerId'} );
|
|
|
|
}
|
|
|
|
return $this->{'Server'};
|
|
|
|
}
|
2015-12-16 00:12:21 +08:00
|
|
|
}
|
|
|
|
?>
|