2014-04-23 11:44:56 +08:00
|
|
|
<?php
|
|
|
|
App::uses('AppController', 'Controller');
|
|
|
|
/**
|
|
|
|
* Monitors Controller
|
|
|
|
*
|
|
|
|
* @property Monitor $Monitor
|
|
|
|
* @property PaginatorComponent $Paginator
|
|
|
|
*/
|
|
|
|
class MonitorsController extends AppController {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Components
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-12-23 01:16:56 +08:00
|
|
|
public $components = array('Paginator', 'RequestHandler');
|
2014-04-23 11:44:56 +08:00
|
|
|
|
2018-01-02 03:43:02 +08:00
|
|
|
public function beforeRender() {
|
|
|
|
$this->set($this->Monitor->enumValues());
|
|
|
|
}
|
2018-07-25 04:41:09 +08:00
|
|
|
|
2017-12-05 04:52:16 +08:00
|
|
|
public function beforeFilter() {
|
|
|
|
parent::beforeFilter();
|
2018-08-08 21:59:46 +08:00
|
|
|
global $user;
|
|
|
|
# We already tested for auth in appController, so we just need to test for specific permission
|
|
|
|
$canView = (!$user) || ($user['Monitors'] != 'None');
|
|
|
|
if ( !$canView ) {
|
2017-12-05 04:52:16 +08:00
|
|
|
throw new UnauthorizedException(__('Insufficient Privileges'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-12-20 06:44:46 +08:00
|
|
|
|
2014-04-23 11:44:56 +08:00
|
|
|
/**
|
|
|
|
* index method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-12-23 01:16:56 +08:00
|
|
|
public function index() {
|
2019-12-21 03:56:01 +08:00
|
|
|
$this->Monitor->recursive = 0;
|
2017-12-05 04:52:16 +08:00
|
|
|
|
2018-07-25 04:41:09 +08:00
|
|
|
if ( $this->request->params['named'] ) {
|
2017-12-23 01:16:56 +08:00
|
|
|
$this->FilterComponent = $this->Components->load('Filter');
|
2019-02-11 13:27:57 +08:00
|
|
|
$conditions = $this->FilterComponent->buildFilter($this->request->params['named']);
|
2017-12-23 01:16:56 +08:00
|
|
|
} else {
|
|
|
|
$conditions = array();
|
|
|
|
}
|
2018-08-08 21:59:46 +08:00
|
|
|
global $user;
|
|
|
|
$allowedMonitors = $user ? preg_split('@,@', $user['MonitorIds'], NULL, PREG_SPLIT_NO_EMPTY) : null;
|
|
|
|
if ( $allowedMonitors ) {
|
2017-11-14 02:27:24 +08:00
|
|
|
$conditions['Monitor.Id' ] = $allowedMonitors;
|
2017-12-23 01:16:56 +08:00
|
|
|
}
|
2017-12-05 04:52:16 +08:00
|
|
|
|
2019-12-21 03:56:01 +08:00
|
|
|
$find_array = array(
|
|
|
|
'conditions' => &$conditions,
|
|
|
|
'contain' => array('Group'),
|
|
|
|
'joins' => array(
|
2017-12-05 04:52:16 +08:00
|
|
|
array(
|
|
|
|
'table' => 'Groups_Monitors',
|
2019-12-23 07:43:53 +08:00
|
|
|
'type' => 'left',
|
2017-12-05 04:52:16 +08:00
|
|
|
'conditions' => array(
|
2019-12-05 11:23:55 +08:00
|
|
|
'Groups_Monitors.MonitorId = Monitor.Id',
|
2017-12-05 04:52:16 +08:00
|
|
|
),
|
|
|
|
),
|
2020-01-16 03:31:05 +08:00
|
|
|
),
|
|
|
|
'group' => '`Monitor`.`Id`',
|
2019-12-21 03:56:01 +08:00
|
|
|
);
|
2017-12-05 04:52:16 +08:00
|
|
|
$monitors = $this->Monitor->find('all',$find_array);
|
2017-11-14 02:27:24 +08:00
|
|
|
$this->set(array(
|
|
|
|
'monitors' => $monitors,
|
|
|
|
'_serialize' => array('monitors')
|
|
|
|
));
|
2017-12-23 01:16:56 +08:00
|
|
|
}
|
2014-04-23 11:44:56 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* view method
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @param string $id
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-12-23 01:16:56 +08:00
|
|
|
public function view($id = null) {
|
|
|
|
$this->Monitor->recursive = 0;
|
2018-07-25 04:41:09 +08:00
|
|
|
if ( !$this->Monitor->exists($id) ) {
|
2017-12-23 01:16:56 +08:00
|
|
|
throw new NotFoundException(__('Invalid monitor'));
|
|
|
|
}
|
2018-08-08 21:59:46 +08:00
|
|
|
global $user;
|
|
|
|
$allowedMonitors = $user ? preg_split('@,@', $user['MonitorIds'], NULL, PREG_SPLIT_NO_EMPTY) : null;
|
|
|
|
if ( $allowedMonitors ) {
|
2017-12-23 01:16:56 +08:00
|
|
|
$restricted = array('Monitor.' . $this->Monitor->primaryKey => $allowedMonitors);
|
|
|
|
} else {
|
|
|
|
$restricted = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$options = array('conditions' => array(
|
|
|
|
array('Monitor.' . $this->Monitor->primaryKey => $id),
|
|
|
|
$restricted
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$monitor = $this->Monitor->find('first', $options);
|
|
|
|
$this->set(array(
|
|
|
|
'monitor' => $monitor,
|
|
|
|
'_serialize' => array('monitor')
|
|
|
|
));
|
|
|
|
}
|
2014-04-23 11:44:56 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* add method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-12-23 01:16:56 +08:00
|
|
|
public function add() {
|
2018-01-02 03:43:02 +08:00
|
|
|
if ( $this->request->is('post') ) {
|
2017-12-23 01:16:56 +08:00
|
|
|
|
2018-08-08 21:59:46 +08:00
|
|
|
global $user;
|
|
|
|
$canAdd = (!$user) || ($user['System'] == 'Edit' );
|
|
|
|
if ( !$canAdd ) {
|
2018-07-25 04:41:09 +08:00
|
|
|
throw new UnauthorizedException(__('Insufficient privileges'));
|
2017-12-23 01:16:56 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->Monitor->create();
|
2019-11-02 20:30:25 +08:00
|
|
|
if ($this->Monitor->save($this->request->data) ) {
|
2017-12-23 01:16:56 +08:00
|
|
|
$this->daemonControl($this->Monitor->id, 'start');
|
2018-01-02 03:43:02 +08:00
|
|
|
//return $this->flash(__('The monitor has been saved.'), array('action' => 'index'));
|
|
|
|
$message = 'Saved';
|
|
|
|
} else {
|
|
|
|
$message = 'Error';
|
2019-11-02 20:30:25 +08:00
|
|
|
// if there is a validation message, use it
|
|
|
|
if (!$this->Monitor->validates()) {
|
|
|
|
$message = $this->Monitor->validationErrors;
|
|
|
|
}
|
2017-12-23 01:16:56 +08:00
|
|
|
}
|
2018-01-02 03:43:02 +08:00
|
|
|
$this->set(array(
|
|
|
|
'message' => $message,
|
|
|
|
'_serialize' => array('message')
|
|
|
|
));
|
2017-12-23 01:16:56 +08:00
|
|
|
}
|
|
|
|
}
|
2014-04-23 11:44:56 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* edit method
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @param string $id
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-12-23 01:16:56 +08:00
|
|
|
public function edit($id = null) {
|
|
|
|
$this->Monitor->id = $id;
|
|
|
|
|
2018-07-25 04:41:09 +08:00
|
|
|
if ( !$this->Monitor->exists($id) ) {
|
2017-12-23 01:16:56 +08:00
|
|
|
throw new NotFoundException(__('Invalid monitor'));
|
|
|
|
}
|
2018-08-08 21:59:46 +08:00
|
|
|
global $user;
|
|
|
|
$canEdit = (!$user) || ($user['Monitors'] == 'Edit');
|
|
|
|
if ( !$canEdit ) {
|
2018-01-26 23:30:29 +08:00
|
|
|
throw new UnauthorizedException(__('Insufficient privileges'));
|
2017-12-23 01:16:56 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-01-26 23:30:29 +08:00
|
|
|
|
2018-01-26 23:39:12 +08:00
|
|
|
$message = '';
|
2018-01-26 23:30:29 +08:00
|
|
|
if ( $this->Monitor->save($this->request->data) ) {
|
2018-01-26 23:39:12 +08:00
|
|
|
$message = 'Saved';
|
2018-01-26 23:30:29 +08:00
|
|
|
$Monitor = $this->Monitor->find('first', array(
|
|
|
|
'fields' => array('Function','ServerId'),
|
|
|
|
'conditions' => array('Id' => $id)
|
|
|
|
))['Monitor'];
|
|
|
|
|
|
|
|
// - restart or stop this monitor after change
|
|
|
|
$func = $Monitor['Function'];
|
|
|
|
// We don't pass the request data as the monitor object because it may be a subset of the full monitor array
|
2018-07-25 04:41:09 +08:00
|
|
|
$this->daemonControl($this->Monitor->id, 'stop');
|
|
|
|
if (
|
|
|
|
( $func != 'None' )
|
|
|
|
and
|
|
|
|
(
|
|
|
|
(!defined('ZM_SERVER_ID'))
|
|
|
|
or
|
|
|
|
($Monitor['ServerId']==ZM_SERVER_ID)
|
|
|
|
)
|
|
|
|
) {
|
2018-11-15 01:54:10 +08:00
|
|
|
if ( !defined('ZM_SERVER_ID')) {
|
2019-04-02 05:21:01 +08:00
|
|
|
ZM\Logger::Debug("Not defined ZM_SERVER_ID");
|
2018-11-15 01:54:10 +08:00
|
|
|
}
|
2018-07-25 04:41:09 +08:00
|
|
|
$this->daemonControl($this->Monitor->id, 'start');
|
2018-01-26 23:30:29 +08:00
|
|
|
}
|
2017-12-23 01:16:56 +08:00
|
|
|
} else {
|
2018-01-26 23:39:12 +08:00
|
|
|
$message = 'Error ' . print_r($this->Monitor->invalidFields(), true);
|
2017-12-23 01:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->set(array(
|
|
|
|
'message' => $message,
|
|
|
|
'_serialize' => array('message')
|
|
|
|
));
|
|
|
|
|
|
|
|
} // end function edit
|
2014-04-23 11:44:56 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* delete method
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @param string $id
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-12-23 01:16:56 +08:00
|
|
|
public function delete($id = null) {
|
|
|
|
$this->Monitor->id = $id;
|
2018-07-25 04:41:09 +08:00
|
|
|
if ( !$this->Monitor->exists() ) {
|
2017-12-23 01:16:56 +08:00
|
|
|
throw new NotFoundException(__('Invalid monitor'));
|
|
|
|
}
|
2018-10-29 23:03:03 +08:00
|
|
|
global $user;
|
|
|
|
$canEdit = (!$user) || ($user['System'] == 'Edit');
|
|
|
|
if ( !$canEdit ) {
|
|
|
|
throw new UnauthorizedException(__('Insufficient privileges'));
|
2017-12-23 01:16:56 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->request->allowMethod('post', 'delete');
|
|
|
|
|
|
|
|
$this->daemonControl($this->Monitor->id, 'stop');
|
|
|
|
|
2018-07-25 04:41:09 +08:00
|
|
|
if ( $this->Monitor->delete() ) {
|
2017-12-23 01:16:56 +08:00
|
|
|
return $this->flash(__('The monitor has been deleted.'), array('action' => 'index'));
|
|
|
|
} else {
|
|
|
|
return $this->flash(__('The monitor could not be deleted. Please, try again.'), array('action' => 'index'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function sourceTypes() {
|
2018-08-08 21:59:46 +08:00
|
|
|
$sourceTypes = $this->Monitor->query('describe Monitors Type;');
|
2017-12-23 01:16:56 +08:00
|
|
|
|
|
|
|
preg_match('/^enum\((.*)\)$/', $sourceTypes[0]['COLUMNS']['Type'], $matches);
|
|
|
|
foreach( explode(',', $matches[1]) as $value ) {
|
|
|
|
$enum[] = trim( $value, "'" );
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->set(array(
|
|
|
|
'sourceTypes' => $enum,
|
|
|
|
'_serialize' => array('sourceTypes')
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// arm/disarm alarms
|
|
|
|
// expected format: http(s):/portal-api-url/monitors/alarm/id:M/command:C.json
|
|
|
|
// where M=monitorId
|
|
|
|
// where C=on|off|status
|
|
|
|
public function alarm() {
|
|
|
|
$id = $this->request->params['named']['id'];
|
2018-07-25 04:41:09 +08:00
|
|
|
if ( !$this->Monitor->exists($id) ) {
|
2017-12-23 01:16:56 +08:00
|
|
|
throw new NotFoundException(__('Invalid monitor'));
|
|
|
|
}
|
|
|
|
|
2019-12-02 01:30:27 +08:00
|
|
|
$cmd = strtolower($this->request->params['named']['command']);
|
2017-12-23 01:16:56 +08:00
|
|
|
switch ($cmd) {
|
|
|
|
case 'on':
|
|
|
|
$q = '-a';
|
|
|
|
$verbose = '-v';
|
|
|
|
break;
|
|
|
|
case 'off':
|
|
|
|
$q = '-c';
|
|
|
|
$verbose = '-v';
|
|
|
|
break;
|
|
|
|
case 'status':
|
|
|
|
$verbose = ''; // zmu has a bug - gives incorrect verbose output in this case
|
|
|
|
$q = '-s';
|
|
|
|
break;
|
2019-12-02 01:30:27 +08:00
|
|
|
default :
|
|
|
|
throw new BadRequestException(__('Invalid command'));
|
2017-12-23 01:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// form auth key based on auth credentials
|
2018-07-25 04:41:09 +08:00
|
|
|
$auth = '';
|
2019-07-18 08:38:58 +08:00
|
|
|
|
2019-12-02 01:30:27 +08:00
|
|
|
if ( ZM_OPT_USE_AUTH ) {
|
|
|
|
global $user;
|
|
|
|
$mToken = $this->request->query('token') ? $this->request->query('token') : $this->request->data('token');;
|
|
|
|
if ( $mToken ) {
|
2019-07-18 08:38:58 +08:00
|
|
|
$auth = ' -T '.$mToken;
|
2019-12-02 01:30:27 +08:00
|
|
|
} else if ( ZM_AUTH_RELAY == 'hashed' ) {
|
|
|
|
$auth = ' -A '.generateAuthHash(ZM_AUTH_HASH_IPS);
|
|
|
|
} else if ( ZM_AUTH_RELAY == 'plain' ) {
|
|
|
|
# Plain requires the plain text password which must either be in request or stored in session
|
|
|
|
$password = $this->request->query('pass') ? $this->request->query('pass') : $this->request->data('pass');;
|
|
|
|
if ( !$password )
|
|
|
|
$password = $this->request->query('password') ? $this->request->query('password') : $this->request->data('password');
|
|
|
|
|
|
|
|
if ( ! $password ) {
|
|
|
|
# during auth the session will have been populated with the plaintext password
|
|
|
|
$stateful = $this->request->query('stateful') ? $this->request->query('stateful') : $this->request->data('stateful');
|
|
|
|
if ( $stateful ) {
|
|
|
|
$password = $_SESSION['password'];
|
|
|
|
}
|
|
|
|
} else if ( $_COOKIE['ZMSESSID'] ) {
|
|
|
|
$password = $_SESSION['password'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$auth = ' -U ' .$user['Username'].' -P '.$password;
|
|
|
|
} else if ( ZM_AUTH_RELAY == 'none' ) {
|
|
|
|
$auth = ' -U ' .$user['Username'];
|
2017-12-23 01:16:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 01:30:27 +08:00
|
|
|
$shellcmd = escapeshellcmd(ZM_PATH_BIN."/zmu $verbose -m$id $q $auth");
|
2017-12-23 01:16:56 +08:00
|
|
|
$status = exec ($shellcmd);
|
|
|
|
|
|
|
|
$this->set(array(
|
|
|
|
'status' => $status,
|
|
|
|
'_serialize' => array('status'),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if a daemon is running for the monitor id
|
|
|
|
public function daemonStatus() {
|
|
|
|
$id = $this->request->params['named']['id'];
|
|
|
|
$daemon = $this->request->params['named']['daemon'];
|
|
|
|
|
2018-07-25 04:41:09 +08:00
|
|
|
if ( !$this->Monitor->exists($id) ) {
|
2017-12-23 01:16:56 +08:00
|
|
|
throw new NotFoundException(__('Invalid monitor'));
|
|
|
|
}
|
|
|
|
|
2019-02-11 13:27:57 +08:00
|
|
|
if (preg_match('/^[a-z]+$/i', $daemon) !== 1) {
|
|
|
|
throw new BadRequestException(__('Invalid command'));
|
|
|
|
}
|
|
|
|
|
2017-12-23 01:16:56 +08:00
|
|
|
$monitor = $this->Monitor->find('first', array(
|
|
|
|
'fields' => array('Id', 'Type', 'Device'),
|
|
|
|
'conditions' => array('Id' => $id)
|
|
|
|
));
|
|
|
|
|
|
|
|
// Clean up the returned array
|
|
|
|
$monitor = Set::extract('/Monitor/.', $monitor);
|
|
|
|
|
|
|
|
// Pass -d for local, otherwise -m
|
2018-08-08 21:59:46 +08:00
|
|
|
if ( $monitor[0]['Type'] == 'Local' ) {
|
2017-12-23 01:16:56 +08:00
|
|
|
$args = '-d '. $monitor[0]['Device'];
|
|
|
|
} else {
|
|
|
|
$args = '-m '. $monitor[0]['Id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the command, and execute it
|
|
|
|
$zm_path_bin = Configure::read('ZM_PATH_BIN');
|
|
|
|
$command = escapeshellcmd("$zm_path_bin/zmdc.pl status $daemon $args");
|
2018-08-08 21:59:46 +08:00
|
|
|
$status = exec($command);
|
2017-12-23 01:16:56 +08:00
|
|
|
|
|
|
|
// If 'not' is present, the daemon is not running, so return false
|
|
|
|
// https://github.com/ZoneMinder/ZoneMinder/issues/799#issuecomment-108996075
|
|
|
|
// Also sending back the status text so we can check if the monitor is in pending
|
|
|
|
// state which means there may be an error
|
|
|
|
$statustext = $status;
|
|
|
|
$status = (strpos($status, 'not')) ? false : true;
|
|
|
|
|
|
|
|
$this->set(array(
|
|
|
|
'status' => $status,
|
|
|
|
'statustext' => $statustext,
|
|
|
|
'_serialize' => array('status','statustext'),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2018-11-15 01:59:44 +08:00
|
|
|
public function daemonControl($id, $command, $daemon=null) {
|
2017-12-23 01:16:56 +08:00
|
|
|
|
2018-11-15 01:54:10 +08:00
|
|
|
// Need to see if it is local or remote
|
|
|
|
$monitor = $this->Monitor->find('first', array(
|
|
|
|
'fields' => array('Type', 'Function', 'Device'),
|
|
|
|
'conditions' => array('Id' => $id)
|
|
|
|
));
|
|
|
|
$monitor = $monitor['Monitor'];
|
2017-12-23 01:16:56 +08:00
|
|
|
|
2018-11-28 22:12:22 +08:00
|
|
|
$daemons = array();
|
2018-11-15 01:59:44 +08:00
|
|
|
if ( ! $daemon ) {
|
|
|
|
if ( $monitor['Function'] == 'Monitor' ) {
|
|
|
|
array_push($daemons, 'zmc');
|
|
|
|
} else {
|
|
|
|
array_push($daemons, 'zmc', 'zma');
|
|
|
|
}
|
2017-12-23 01:16:56 +08:00
|
|
|
} else {
|
2018-11-15 01:59:44 +08:00
|
|
|
array_push($daemons, $daemon);
|
2017-12-23 01:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$zm_path_bin = Configure::read('ZM_PATH_BIN');
|
|
|
|
|
2018-11-15 01:54:10 +08:00
|
|
|
$status_text = '';
|
2018-08-08 21:59:46 +08:00
|
|
|
foreach ( $daemons as $daemon ) {
|
2018-06-07 00:56:33 +08:00
|
|
|
$args = '';
|
2018-08-08 21:59:46 +08:00
|
|
|
if ( $daemon == 'zmc' and $monitor['Type'] == 'Local' ) {
|
2018-06-07 00:56:33 +08:00
|
|
|
$args = '-d ' . $monitor['Device'];
|
2020-03-27 01:57:43 +08:00
|
|
|
} else if ( $daemon == 'zmcontrol.pl' ) {
|
|
|
|
$args = '--id '.$id;
|
2018-06-07 00:56:33 +08:00
|
|
|
} else {
|
|
|
|
$args = '-m ' . $id;
|
|
|
|
}
|
|
|
|
|
2017-12-23 01:16:56 +08:00
|
|
|
$shellcmd = escapeshellcmd("$zm_path_bin/zmdc.pl $command $daemon $args");
|
2019-04-02 05:21:01 +08:00
|
|
|
ZM\Logger::Debug("Command $shellcmd");
|
2018-11-15 01:54:10 +08:00
|
|
|
$status = exec($shellcmd);
|
|
|
|
$status_text .= $status."\n";
|
2017-12-23 01:16:56 +08:00
|
|
|
}
|
2018-11-15 01:54:10 +08:00
|
|
|
$this->set(array(
|
|
|
|
'status' => 'ok',
|
|
|
|
'statustext' => $status_text,
|
|
|
|
'_serialize' => array('status','statustext'),
|
|
|
|
));
|
|
|
|
} // end function daemonControl
|
2017-12-23 01:16:56 +08:00
|
|
|
} // end class MonitorsController
|