2015-09-17 03:16:07 +08:00
|
|
|
<?php
|
2018-07-10 00:09:57 +08:00
|
|
|
require_once('database.php');
|
2015-12-31 23:13:48 +08:00
|
|
|
|
2018-09-15 21:42:59 +08:00
|
|
|
$server_cache = array();
|
|
|
|
|
2016-05-06 03:33:28 +08:00
|
|
|
class Server {
|
2018-01-18 03:22:04 +08:00
|
|
|
private $defaults = array(
|
2018-07-10 00:09:57 +08:00
|
|
|
'Id' => null,
|
|
|
|
'Name' => '',
|
2018-11-22 23:04:33 +08:00
|
|
|
'Protocol' => '',
|
2018-07-10 00:09:57 +08:00
|
|
|
'Hostname' => '',
|
2018-11-22 23:04:33 +08:00
|
|
|
'Port' => null,
|
2018-07-10 00:09:57 +08:00
|
|
|
'PathPrefix' => '/zm',
|
|
|
|
'zmaudit' => 1,
|
|
|
|
'zmstats' => 1,
|
|
|
|
'zmtrigger' => 0,
|
2018-01-18 03:22:04 +08:00
|
|
|
);
|
2018-07-10 00:09:57 +08:00
|
|
|
|
|
|
|
public function __construct($IdOrRow = NULL) {
|
2018-11-22 23:04:33 +08:00
|
|
|
global $server_cache;
|
2016-05-06 03:33:28 +08:00
|
|
|
$row = NULL;
|
|
|
|
if ( $IdOrRow ) {
|
2018-07-10 00:09:57 +08:00
|
|
|
if ( is_integer($IdOrRow) or ctype_digit($IdOrRow) ) {
|
|
|
|
$row = dbFetchOne('SELECT * FROM Servers WHERE Id=?', NULL, array($IdOrRow));
|
|
|
|
if ( !$row ) {
|
|
|
|
Error('Unable to load Server record for Id='.$IdOrRow);
|
2016-05-06 03:33:28 +08:00
|
|
|
}
|
2018-07-10 00:09:57 +08:00
|
|
|
} elseif ( is_array($IdOrRow) ) {
|
2016-05-06 03:33:28 +08:00
|
|
|
$row = $IdOrRow;
|
|
|
|
}
|
|
|
|
} # end if isset($IdOrRow)
|
|
|
|
if ( $row ) {
|
|
|
|
foreach ($row as $k => $v) {
|
|
|
|
$this->{$k} = $v;
|
|
|
|
}
|
2018-09-15 21:42:59 +08:00
|
|
|
$server_cache[$row['Id']] = $this;
|
2016-05-06 03:33:28 +08:00
|
|
|
} else {
|
2018-07-10 00:09:57 +08:00
|
|
|
# Set defaults
|
|
|
|
foreach ( $this->defaults as $k => $v ) $this->{$k} = $v;
|
2015-09-18 03:34:26 +08:00
|
|
|
}
|
2016-05-06 03:33:28 +08:00
|
|
|
}
|
2018-07-10 00:09:57 +08:00
|
|
|
|
2018-11-22 23:04:33 +08:00
|
|
|
public function Hostname( $new = null ) {
|
|
|
|
if ( $new != null )
|
|
|
|
$this->{'Hostname'} = $new;
|
2018-01-14 04:15:14 +08:00
|
|
|
|
2018-11-22 23:04:33 +08:00
|
|
|
if ( isset( $this->{'Hostname'}) and ( $this->{'Hostname'} != '' ) ) {
|
|
|
|
return $this->{'Hostname'};
|
|
|
|
} else if ( $this->Id() ) {
|
|
|
|
return $this->{'Name'};
|
2018-01-14 04:15:14 +08:00
|
|
|
}
|
2018-11-22 23:04:33 +08:00
|
|
|
return $_SERVER['SERVER_NAME'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function Protocol( $new = null ) {
|
|
|
|
if ( $new != null )
|
|
|
|
$this->{'Protocol'} = $new;
|
|
|
|
|
|
|
|
if ( isset($this->{'Protocol'}) and ( $this->{'Protocol'} != '' ) ) {
|
|
|
|
return $this->{'Protocol'};
|
2018-01-14 04:15:14 +08:00
|
|
|
}
|
2018-11-22 23:04:33 +08:00
|
|
|
return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? 'https' : 'http';
|
2018-01-14 04:15:14 +08:00
|
|
|
}
|
2015-09-18 03:34:26 +08:00
|
|
|
|
2018-11-22 23:04:33 +08:00
|
|
|
public function Port( $new = '' ) {
|
|
|
|
if ( $new != '' )
|
|
|
|
$this->{'Port'} = $new;
|
|
|
|
|
|
|
|
if ( isset($this->{'Port'}) and $this->{'Port'} ) {
|
|
|
|
return $this->{'Port'};
|
|
|
|
}
|
|
|
|
return $_SERVER['SERVER_PORT'];
|
2018-07-10 00:09:57 +08:00
|
|
|
}
|
|
|
|
|
2018-10-08 21:55:47 +08:00
|
|
|
public function Url( $port = null ) {
|
2018-11-22 23:04:33 +08:00
|
|
|
$url = $this->Protocol().'://';
|
2016-01-04 00:55:53 +08:00
|
|
|
if ( $this->Id() ) {
|
2018-10-08 21:55:47 +08:00
|
|
|
$url .= $this->Hostname();
|
2016-01-04 00:55:53 +08:00
|
|
|
} else {
|
2018-10-08 21:55:47 +08:00
|
|
|
$url .= $_SERVER['SERVER_NAME'];
|
2016-01-04 00:55:53 +08:00
|
|
|
}
|
2018-10-08 21:55:47 +08:00
|
|
|
if ( $port ) {
|
|
|
|
$url .= ':'.$port;
|
2018-10-19 22:59:16 +08:00
|
|
|
} else {
|
2018-11-22 23:04:33 +08:00
|
|
|
$url .= ':'.$this->Port();
|
2018-07-10 00:09:57 +08:00
|
|
|
}
|
2018-11-22 23:04:33 +08:00
|
|
|
$url .= $this->PathPrefix();
|
2018-10-08 21:55:47 +08:00
|
|
|
return $url;
|
2015-09-18 03:34:26 +08:00
|
|
|
}
|
2018-11-22 23:04:33 +08:00
|
|
|
|
|
|
|
public function PathPrefix( $new = null ) {
|
|
|
|
if ( $new != null )
|
|
|
|
$this->{'PathPrefix'} = $new;
|
|
|
|
|
|
|
|
if ( isset($this->{'PathPrefix'}) and $this->{'PathPrefix'} ) {
|
|
|
|
return $this->{'PathPrefix'};
|
|
|
|
}
|
|
|
|
return $_SERVER['PHP_SELF'];
|
2018-07-10 00:09:57 +08:00
|
|
|
}
|
|
|
|
|
2018-01-18 03:22:04 +08:00
|
|
|
public function __call($fn, array $args){
|
|
|
|
if ( count($args) ) {
|
|
|
|
$this->{$fn} = $args[0];
|
|
|
|
}
|
|
|
|
if ( array_key_exists($fn, $this) ) {
|
2016-05-06 03:33:28 +08:00
|
|
|
return $this->{$fn};
|
2018-01-18 03:22:04 +08:00
|
|
|
} else {
|
2018-07-10 00:09:57 +08:00
|
|
|
if ( array_key_exists($fn, $this->defaults) ) {
|
2018-01-18 03:22:04 +08:00
|
|
|
return $this->defaults{$fn};
|
|
|
|
} else {
|
|
|
|
$backTrace = debug_backtrace();
|
|
|
|
$file = $backTrace[1]['file'];
|
|
|
|
$line = $backTrace[1]['line'];
|
2018-07-10 00:09:57 +08:00
|
|
|
Warning("Unknown function call Server->$fn from $file:$line");
|
2018-01-18 03:22:04 +08:00
|
|
|
}
|
2015-12-02 04:16:07 +08:00
|
|
|
}
|
2016-05-06 03:33:28 +08:00
|
|
|
}
|
2018-09-08 04:31:11 +08:00
|
|
|
public static function find( $parameters = null, $options = null ) {
|
|
|
|
$filters = array();
|
|
|
|
$sql = 'SELECT * FROM Servers ';
|
2016-05-06 03:33:28 +08:00
|
|
|
$values = array();
|
2018-09-08 04:31:11 +08:00
|
|
|
|
|
|
|
if ( $parameters ) {
|
|
|
|
$fields = array();
|
|
|
|
$sql .= 'WHERE ';
|
|
|
|
foreach ( $parameters as $field => $value ) {
|
|
|
|
if ( $value == null ) {
|
|
|
|
$fields[] = $field.' IS NULL';
|
|
|
|
} else if ( is_array( $value ) ) {
|
|
|
|
$func = function(){return '?';};
|
|
|
|
$fields[] = $field.' IN ('.implode(',', array_map( $func, $value ) ). ')';
|
|
|
|
$values += $value;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$fields[] = $field.'=?';
|
|
|
|
$values[] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$sql .= implode(' AND ', $fields );
|
2018-07-10 00:09:57 +08:00
|
|
|
}
|
2018-09-08 04:31:11 +08:00
|
|
|
if ( $options ) {
|
|
|
|
if ( isset($options['order']) ) {
|
|
|
|
$sql .= ' ORDER BY ' . $options['order'];
|
|
|
|
}
|
|
|
|
if ( isset($options['limit']) ) {
|
|
|
|
if ( is_integer($options['limit']) or ctype_digit($options['limit']) ) {
|
2018-09-15 21:42:59 +08:00
|
|
|
$sql .= ' LIMIT ' . $options['limit'];
|
2018-09-08 04:31:11 +08:00
|
|
|
} else {
|
|
|
|
$backTrace = debug_backtrace();
|
|
|
|
$file = $backTrace[1]['file'];
|
|
|
|
$line = $backTrace[1]['line'];
|
2018-09-15 21:42:59 +08:00
|
|
|
Error("Invalid value for limit(".$options['limit'].") passed to Server::find from $file:$line");
|
2018-09-10 01:56:29 +08:00
|
|
|
return array();
|
2018-09-08 04:31:11 +08:00
|
|
|
}
|
|
|
|
}
|
2016-05-06 03:33:28 +08:00
|
|
|
}
|
|
|
|
$results = dbFetchAll( $sql, NULL, $values );
|
|
|
|
if ( $results ) {
|
2018-07-10 00:09:57 +08:00
|
|
|
return array_map(function($id){ return new Server($id); }, $results);
|
2016-05-06 03:33:28 +08:00
|
|
|
}
|
2018-09-10 01:56:29 +08:00
|
|
|
return array();
|
2016-05-06 03:33:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function find_one( $parameters = array() ) {
|
2018-09-15 21:42:59 +08:00
|
|
|
global $server_cache;
|
|
|
|
if (
|
|
|
|
( count($parameters) == 1 ) and
|
|
|
|
isset($parameters['Id']) and
|
|
|
|
isset($server_cache[$parameters['Id']]) ) {
|
|
|
|
return $server_cache[$parameters['Id']];
|
|
|
|
}
|
2018-09-08 04:31:11 +08:00
|
|
|
$results = Server::find( $parameters, array('limit'=>1) );
|
|
|
|
if ( ! sizeof($results) ) {
|
2016-05-06 03:33:28 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
return $results[0];
|
|
|
|
}
|
|
|
|
|
2018-07-10 00:09:57 +08:00
|
|
|
} # end class Server
|
2015-09-17 03:16:07 +08:00
|
|
|
?>
|