Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server path prefix #2152

Merged
merged 23 commits into from Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
90e4c26
Use Server->Url() more,, moving the logic into Server->Url()
Jul 9, 2018
34299af
Add PathPrefix to Servers Table
Jul 9, 2018
661876b
Use Server->Url() more,, moving the logic into Server->Url()
Jul 9, 2018
62f45b4
Add PathPrefix and use it in Url. Make Url() smarter so it can do mo…
Jul 9, 2018
d051342
Add PathPrefix
Jul 9, 2018
7ef43a0
Merge ../ZoneMinder.master into server_path_prefix
Jul 9, 2018
acab621
bump version for db update
Jul 9, 2018
3d1fe72
Merge ../ZoneMinder.master into server_path_prefix
Jul 9, 2018
ebe55cf
Include new Server.js
Jul 9, 2018
87aa146
remove debug output from updating navbar
Jul 9, 2018
45f6eaf
Add the current serverId to the available javascript variables
Jul 9, 2018
ee017a1
Add PathPrefix to language
Jul 9, 2018
51cfd9b
Use Server.js object for generating url to images
Jul 9, 2018
e98578b
Use Server.js objects for Servers array populated by json
Jul 9, 2018
0b4d76f
Add PathPrefix column to the server list
Jul 9, 2018
c2c7b75
check for ZM_SERVER_ID being defined
Jul 9, 2018
6e5472d
Merge branch 'master' into server_path_prefix
Aug 22, 2018
c5f7fb7
Merge branch 'master' into server_path_prefix
connortechnology Nov 22, 2018
f8b2ff5
rework from Url() to PathToIndex(), PathToZMS(), UrlToIndex() and Url…
Nov 27, 2018
2b48b09
Add entries for PathToIndex and PathToZMS
Nov 28, 2018
cd13dda
fix use of instead of
Nov 28, 2018
a89dd83
Update to use object instead of db row
Nov 28, 2018
1e915e9
Merge branch 'master' into server_path_prefix
Nov 28, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions db/zm_create.sql.in
Expand Up @@ -557,6 +557,7 @@ DROP TABLE IF EXISTS `Servers`;
CREATE TABLE `Servers` (
`Id` int(10) unsigned NOT NULL auto_increment,
`Hostname` TEXT,
`PathPrefix` TEXT,
`Name` varchar(64) NOT NULL default '',
`State_Id` int(10) unsigned,
`Status` enum('Unknown','NotRunning','Running') NOT NULL default 'Unknown',
Expand Down
15 changes: 15 additions & 0 deletions db/zm_update-1.31.47.sql
@@ -0,0 +1,15 @@
--
-- Add Prefix column to Storage
--

SET @s = (SELECT IF(
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = DATABASE()
AND table_name = 'Servers'
AND column_name = 'PathPrefix'
) > 0,
"SELECT 'Column PathPrefix already exists in Servers'",
"ALTER TABLE Servers ADD `PathPrefix` TEXT AFTER `Hostname`"
));

PREPARE stmt FROM @s;
EXECUTE stmt;
2 changes: 1 addition & 1 deletion version
@@ -1 +1 @@
1.31.46
1.31.47
40 changes: 14 additions & 26 deletions web/includes/Event.php
Expand Up @@ -189,25 +189,19 @@ public function delete() {

public function getStreamSrc( $args=array(), $querySep='&' ) {

$streamSrc = ZM_BASE_PROTOCOL.'://';
$streamSrc = '';
if ( $this->Storage()->ServerId() ) {
$Server = $this->Storage()->Server();
$streamSrc .= $Server->Hostname();
if ( ZM_MIN_STREAMING_PORT ) {
$streamSrc .= ':'.(ZM_MIN_STREAMING_PORT+$this->{'MonitorId'});
}
} else if ( $this->Monitor()->ServerId() ) {
# Assume that the server that recorded it has it
$Server = $this->Monitor()->Server();
$streamSrc .= $Server->Hostname();
if ( ZM_MIN_STREAMING_PORT ) {
$streamSrc .= ':'.(ZM_MIN_STREAMING_PORT+$this->{'MonitorId'});
}
} else if ( ZM_MIN_STREAMING_PORT ) {
$streamSrc .= $_SERVER['SERVER_NAME'].':'.(ZM_MIN_STREAMING_PORT+$this->{'MonitorId'});
} else {
$streamSrc .= $_SERVER['HTTP_HOST'];
$Server = new Server;
}
$streamSrc .= $Server->Url(
ZM_MIN_STREAMING_PORT ?
ZM_MIN_STREAMING_PORT+$this->{'MonitorId'} :
null);

if ( $this->{'DefaultVideo'} and $args['mode'] != 'jpeg' ) {
$streamSrc .= ( ZM_BASE_PATH != '/' ? ZM_BASE_PATH : '' ).'/index.php';
Expand Down Expand Up @@ -319,25 +313,19 @@ function getThumbnailSrc( $args=array(), $querySep='&' ) {
# The thumbnail is theoretically the image with the most motion.
# We always store at least 1 image when capturing

$streamSrc = ZM_BASE_PROTOCOL.'://';
$streamSrc = '';
if ( $this->Storage()->ServerId() ) {
$Server = $this->Storage()->Server();
$streamSrc .= $Server->Hostname();
if ( ZM_MIN_STREAMING_PORT ) {
$streamSrc .= ':'.(ZM_MIN_STREAMING_PORT+$this->{'MonitorId'});
}
} else if ( $this->Monitor()->ServerId() ) {
# Assume that the server that recorded it has it
$Server = $this->Monitor()->Server();
$streamSrc .= $Server->Hostname();
if ( ZM_MIN_STREAMING_PORT ) {
$streamSrc .= ':'.(ZM_MIN_STREAMING_PORT+$this->{'MonitorId'});
}

} else if ( ZM_MIN_STREAMING_PORT ) {
$streamSrc .= $_SERVER['SERVER_NAME'].':'.(ZM_MIN_STREAMING_PORT+$this->{'MonitorId'});
} else {
$streamSrc .= $_SERVER['HTTP_HOST'];
}
$Server = new Server;
}
$streamSrc .= $Server->Url(
ZM_MIN_STREAMING_PORT ?
ZM_MIN_STREAMING_PORT+$this->{'MonitorId'} :
null);

$streamSrc .= ( ZM_BASE_PATH != '/' ? ZM_BASE_PATH : '' ).'/index.php';
$args['eid'] = $this->{'Id'};
Expand Down
30 changes: 12 additions & 18 deletions web/includes/Monitor.php
Expand Up @@ -197,20 +197,13 @@ public function __call($fn, array $args){
}
}

public function getStreamSrc( $args, $querySep='&' ) {

$streamSrc = ZM_BASE_PROTOCOL.'://';
if ( isset($this->{'ServerId'}) and $this->{'ServerId'} ) {
$Server = new Server( $this->{'ServerId'} );
$streamSrc .= $Server->Hostname();
if ( ZM_MIN_STREAMING_PORT ) {
$streamSrc .= ':'.(ZM_MIN_STREAMING_PORT+$this->{'Id'});
}
} else if ( ZM_MIN_STREAMING_PORT ) {
$streamSrc .= $_SERVER['SERVER_NAME'].':'.(ZM_MIN_STREAMING_PORT+$this->{'Id'});
} else {
$streamSrc .= $_SERVER['HTTP_HOST'];
}
public function getStreamSrc($args, $querySep='&') {

$streamSrc = $this->Server()->Url(
ZM_MIN_STREAMING_PORT ?
ZM_MIN_STREAMING_PORT+$this->{'Id'} :
null);

$streamSrc .= ZM_PATH_ZMS;

$args['monitor'] = $this->{'Id'};
Expand All @@ -232,9 +225,9 @@ public function getStreamSrc( $args, $querySep='&' ) {
$args['rand'] = time();
}

$streamSrc .= '?'.http_build_query( $args,'', $querySep );
$streamSrc .= '?'.http_build_query($args,'', $querySep);

return( $streamSrc );
return $streamSrc;
} // end function getStreamSrc

public function Width($new = null) {
Expand Down Expand Up @@ -489,9 +482,10 @@ public function Source( ) {
$source = preg_replace( '/^.*\//', '', $this->{'Path'} );
} elseif ( $this->{'Type'} == 'Ffmpeg' || $this->{'Type'} == 'Libvlc' || $this->{'Type'} == 'WebSite' ) {
$url_parts = parse_url( $this->{'Path'} );
if ( ZM_WEB_FILTER_SOURCE == "Hostname" ) { # Filter out everything but the hostname
if ( ZM_WEB_FILTER_SOURCE == 'Hostname' ) { # Filter out everything but the hostname
$source = $url_parts['host'];
} elseif ( ZM_WEB_FILTER_SOURCE == "NoCredentials" ) { # Filter out sensitive and common items
} elseif ( ZM_WEB_FILTER_SOURCE == 'NoCredentials' ) {
# Filter out sensitive and common items
unset($url_parts['user']);
unset($url_parts['pass']);
#unset($url_parts['scheme']);
Expand Down
112 changes: 57 additions & 55 deletions web/includes/Server.php
Expand Up @@ -3,22 +3,24 @@

class Server {
private $defaults = array(
'Id' => null,
'Name' => '',
'Hostname' => '',
'zmaudit' => 1,
'zmstats' => 1,
'zmtrigger' => 0,
'Id' => null,
'Name' => '',
'Hostname' => '',
'PathPrefix' => '/zm',
'zmaudit' => 1,
'zmstats' => 1,
'zmtrigger' => 0,
);
public function __construct( $IdOrRow = NULL ) {

public function __construct($IdOrRow = NULL) {
$row = NULL;
if ( $IdOrRow ) {
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 );
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);
}
} elseif ( is_array( $IdOrRow ) ) {
} elseif ( is_array($IdOrRow) ) {
$row = $IdOrRow;
}
} # end if isset($IdOrRow)
Expand All @@ -27,11 +29,12 @@ public function __construct( $IdOrRow = NULL ) {
$this->{$k} = $v;
}
} else {
$this->{'Name'} = '';
$this->{'Hostname'} = '';
# Set defaults
foreach ( $this->defaults as $k => $v ) $this->{$k} = $v;
}
}
public static function find_all( $parameters = null, $options = null ) {

public static function find_all($parameters = null, $options = null) {
$filters = array();
$sql = 'SELECT * FROM Servers ';
$values = array();
Expand All @@ -42,20 +45,20 @@ public static function find_all( $parameters = null, $options = null ) {
foreach ( $parameters as $field => $value ) {
if ( $value == null ) {
$fields[] = $field.' IS NULL';
} else if ( is_array( $value ) ) {
} else if ( is_array($value) ) {
$func = function(){return '?';};
$fields[] = $field.' IN ('.implode(',', array_map( $func, $value ) ). ')';
$fields[] = $field.' IN ('.implode(',', array_map($func, $value) ). ')';
$values += $value;

} else {
$fields[] = $field.'=?';
$values[] = $value;
}
}
$sql .= implode(' AND ', $fields );
$sql .= implode(' AND ', $fields);
}
if ( $options and isset($options['order']) ) {
$sql .= ' ORDER BY ' . $options['order'];
$sql .= ' ORDER BY ' . $options['order'];
}
$result = dbQuery($sql, $values);
$results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Server');
Expand All @@ -65,70 +68,69 @@ public static function find_all( $parameters = null, $options = null ) {
return $filters;
}

public function Url() {
if ( $this->Id() ) {
return ZM_BASE_PROTOCOL . '://'. $this->Hostname();
} else {
return ZM_BASE_PROTOCOL . '://'. $_SERVER['SERVER_NAME'];
return '';
}
}
public function Hostname() {
if ( isset( $this->{'Hostname'} ) and ( $this->{'Hostname'} != '' ) ) {
return $this->{'Hostname'};
}
return $this->{'Name'};
}
public function Url($port=null) {
return ZM_BASE_PROTOCOL.'://'.$this->Hostname().($port ? ':'.$port : '').$this->{'PathPrefix'};
}

public function Hostname() {
if ( isset( $this->{'Hostname'}) and ( $this->{'Hostname'} != '' ) ) {
return $this->{'Hostname'};
} else if ( $this->Id() ) {
return $this->{'Name'};
}
return $_SERVER['SERVER_NAME'];
}

public function __call($fn, array $args){
if ( count($args) ) {
$this->{$fn} = $args[0];
}
if ( array_key_exists($fn, $this) ) {
return $this->{$fn};
} else {
if ( array_key_exists( $fn, $this->defaults ) ) {
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" );
Warning("Unknown function call Server->$fn from $file:$line");
}
}
}

public static function find( $parameters = array(), $limit = NULL ) {
public static function find($parameters = array(), $limit = NULL) {
$sql = 'SELECT * FROM Servers';
$values = array();
if ( sizeof($parameters) ) {
$sql .= ' WHERE ' . implode( ' AND ', array_map(
$sql .= ' WHERE ' . implode(' AND ', array_map(
function($v){ return $v.'=?'; },
array_keys( $parameters )
) );
$values = array_values( $parameters );
array_keys($parameters)
));
$values = array_values($parameters);
}
if ( is_integer($limit) or ctype_digit($limit) ) {
$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;
}
if ( is_integer( $limit ) or ctype_digit( $limit ) ) {
$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;
}
$results = dbFetchAll( $sql, NULL, $values );
$results = dbFetchAll($sql, NULL, $values);
if ( $results ) {
return array_map( function($id){ return new Server($id); }, $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 ) ) {
public static function find_one($parameters = array()) {
$results = Server::find($parameters, 1);
if ( !sizeof($results) ) {
return;
}
return $results[0];
}

}
} # end class Server
?>
12 changes: 12 additions & 0 deletions web/js/Server.js
@@ -0,0 +1,12 @@
class Server {
constructor(json) {
for( var k in json ) {
this[k] = json[k];
}
}
url(port=0){
return location.protocol+'//'+this.Hostname+
(port ? ':'+port : '') +
( ( this.PathPrefix && this.PathPrefix != 'null') ? this.PathPrefix : '');
}
};
1 change: 1 addition & 0 deletions web/lang/en_gb.php
Expand Up @@ -583,6 +583,7 @@
'Parameter' => 'Parameter',
'Password' => 'Password',
'PasswordsDifferent' => 'The new and confirm passwords are different',
'PathPrefix' => 'Path Prefix',
'Paths' => 'Paths',
'Pause' => 'Pause',
'PhoneBW' => 'Phone B/W',
Expand Down
2 changes: 2 additions & 0 deletions web/skins/classic/includes/functions.php
Expand Up @@ -119,6 +119,8 @@ function output_link_if_exists( $files ) {
<script src="skins/<?php echo $skin; ?>/js/chosen/chosen.jquery.min.js"></script>
<script src="skins/<?php echo $skin; ?>/js/dateTimePicker/jquery-ui-timepicker-addon.js"></script>

<script src="<?php echo cache_bust('js/Server.js'); ?>"></script>

<script>
//<![CDATA[
<!--
Expand Down
1 change: 0 additions & 1 deletion web/skins/classic/js/skin.js
Expand Up @@ -204,7 +204,6 @@ if ( currentView != 'none' && currentView != 'login' ) {
}

function setNavBar(data) {
console.log(data);
if ( data.auth ) {
if ( data.auth != auth_hash ) {
// Update authentication token.
Expand Down
1 change: 1 addition & 0 deletions web/skins/classic/js/skin.js.php
Expand Up @@ -31,6 +31,7 @@
var currentView = '<?php echo $view ?>';
var thisUrl = "<?php echo ZM_BASE_URL.$_SERVER['PHP_SELF'] ?>";
var skinPath = "<?php echo ZM_SKIN_PATH ?>";
var serverId = '<?php echo defined('ZM_SERVER_ID') ? ZM_SERVER_ID : '' ?>';

var canEditSystem = <?php echo canEdit('System' )?'true':'false' ?>;
var canViewSystem = <?php echo canView('System' )?'true':'false' ?>;
Expand Down