2015-09-17 03:16:07 +08:00
|
|
|
<?php
|
|
|
|
require_once( 'database.php' );
|
2015-12-31 23:13:48 +08:00
|
|
|
|
2016-05-06 03:33:28 +08:00
|
|
|
class Server {
|
2018-01-18 03:22:04 +08:00
|
|
|
private $defaults = array(
|
|
|
|
'Id' => null,
|
|
|
|
'Name' => '',
|
|
|
|
'Hostname' => '',
|
|
|
|
'zmaudit' => 1,
|
|
|
|
'zmstats' => 1,
|
|
|
|
'zmtrigger' => 0,
|
|
|
|
);
|
2016-05-06 03:33:28 +08:00
|
|
|
public function __construct( $IdOrRow = NULL ) {
|
|
|
|
$row = NULL;
|
|
|
|
if ( $IdOrRow ) {
|
2016-12-09 04:58:00 +08:00
|
|
|
if ( is_integer( $IdOrRow ) or ctype_digit( $IdOrRow ) ) {
|
2016-05-06 03:33:28 +08:00
|
|
|
$row = dbFetchOne( 'SELECT * FROM Servers WHERE Id=?', NULL, array( $IdOrRow ) );
|
|
|
|
if ( ! $row ) {
|
|
|
|
Error("Unable to load Server record for Id=" . $IdOrRow );
|
|
|
|
}
|
|
|
|
} elseif ( is_array( $IdOrRow ) ) {
|
|
|
|
$row = $IdOrRow;
|
|
|
|
}
|
|
|
|
} # end if isset($IdOrRow)
|
|
|
|
if ( $row ) {
|
|
|
|
foreach ($row as $k => $v) {
|
|
|
|
$this->{$k} = $v;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->{'Name'} = '';
|
|
|
|
$this->{'Hostname'} = '';
|
2015-09-18 03:34:26 +08:00
|
|
|
}
|
2016-05-06 03:33:28 +08:00
|
|
|
}
|
2018-01-14 04:15:14 +08:00
|
|
|
public static function find_all( $parameters = null, $options = null ) {
|
|
|
|
$filters = array();
|
|
|
|
$sql = 'SELECT * FROM Servers ';
|
|
|
|
$values = array();
|
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
if ( $options and isset($options['order']) ) {
|
|
|
|
$sql .= ' ORDER BY ' . $options['order'];
|
|
|
|
}
|
|
|
|
$result = dbQuery($sql, $values);
|
|
|
|
$results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Server');
|
|
|
|
foreach ( $results as $row => $obj ) {
|
|
|
|
$filters[] = $obj;
|
|
|
|
}
|
|
|
|
return $filters;
|
|
|
|
}
|
2015-09-18 03:34:26 +08:00
|
|
|
|
2015-12-02 04:16:07 +08:00
|
|
|
public function Url() {
|
2016-01-04 00:55:53 +08:00
|
|
|
if ( $this->Id() ) {
|
|
|
|
return ZM_BASE_PROTOCOL . '://'. $this->Hostname();
|
|
|
|
} else {
|
2018-02-13 03:44:58 +08:00
|
|
|
return ZM_BASE_PROTOCOL . '://'. $_SERVER['SERVER_NAME'];
|
2016-01-05 02:46:05 +08:00
|
|
|
return '';
|
2016-01-04 00:55:53 +08:00
|
|
|
}
|
2015-09-18 03:34:26 +08:00
|
|
|
}
|
|
|
|
public function Hostname() {
|
2015-10-03 02:22:19 +08:00
|
|
|
if ( isset( $this->{'Hostname'} ) and ( $this->{'Hostname'} != '' ) ) {
|
|
|
|
return $this->{'Hostname'};
|
2015-09-18 04:06:47 +08:00
|
|
|
}
|
2015-10-03 02:22:19 +08:00
|
|
|
return $this->{'Name'};
|
2015-09-17 03:16:07 +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 {
|
|
|
|
if ( array_key_exists( $fn, $this->defaults ) ) {
|
|
|
|
return $this->defaults{$fn};
|
|
|
|
} else {
|
|
|
|
$backTrace = debug_backtrace();
|
|
|
|
$file = $backTrace[1]['file'];
|
|
|
|
$line = $backTrace[1]['line'];
|
|
|
|
Warning( "Unknown function call Server->$fn from $file:$line" );
|
|
|
|
}
|
2015-12-02 04:16:07 +08:00
|
|
|
}
|
2016-05-06 03:33:28 +08:00
|
|
|
}
|
2018-01-18 03:22:04 +08:00
|
|
|
|
2016-05-06 03:33:28 +08:00
|
|
|
public static function find( $parameters = array(), $limit = NULL ) {
|
|
|
|
$sql = 'SELECT * FROM Servers';
|
|
|
|
$values = array();
|
|
|
|
if ( sizeof($parameters) ) {
|
|
|
|
$sql .= ' WHERE ' . implode( ' AND ', array_map(
|
|
|
|
function($v){ return $v.'=?'; },
|
|
|
|
array_keys( $parameters )
|
|
|
|
) );
|
|
|
|
$values = array_values( $parameters );
|
|
|
|
}
|
2016-12-09 04:58:00 +08:00
|
|
|
if ( is_integer( $limit ) or ctype_digit( $limit ) ) {
|
2016-12-09 02:37:23 +08:00
|
|
|
$sql .= ' LIMIT ' . $limit;
|
|
|
|
} else {
|
|
|
|
$backTrace = debug_backtrace();
|
|
|
|
$file = $backTrace[1]['file'];
|
|
|
|
$line = $backTrace[1]['line'];
|
|
|
|
Error("Invalid value for limit($limit) passed to Server::find from $file:$line");
|
|
|
|
return;
|
|
|
|
}
|
2016-05-06 03:33:28 +08:00
|
|
|
$results = dbFetchAll( $sql, NULL, $values );
|
|
|
|
if ( $results ) {
|
|
|
|
return array_map( function($id){ return new Server($id); }, $results );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function find_one( $parameters = array() ) {
|
|
|
|
$results = Server::find( $parameters, 1 );
|
|
|
|
if ( ! sizeof( $results ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return $results[0];
|
|
|
|
}
|
|
|
|
|
2015-09-17 03:16:07 +08:00
|
|
|
}
|
|
|
|
?>
|