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
|
|
|
|
*/
|
|
|
|
public $components = array('Paginator', 'RequestHandler');
|
|
|
|
|
2015-12-20 06:44:46 +08:00
|
|
|
|
|
|
|
public function beforeFilter() {
|
|
|
|
parent::beforeFilter();
|
|
|
|
$canView = $this->Session->Read('monitorPermission');
|
|
|
|
if ($canView =='None')
|
|
|
|
{
|
|
|
|
throw new UnauthorizedException(__('Insufficient Privileges'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-23 11:44:56 +08:00
|
|
|
/**
|
|
|
|
* index method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function index() {
|
|
|
|
$this->Monitor->recursive = 0;
|
2015-12-20 06:44:46 +08:00
|
|
|
$allowedMonitors=preg_split ('@,@', $this->Session->Read('allowedMonitors'),NULL, PREG_SPLIT_NO_EMPTY);
|
|
|
|
|
|
|
|
if (!empty($allowedMonitors))
|
|
|
|
{
|
|
|
|
$options = array('conditions'=>array('Monitor.Id'=> $allowedMonitors));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$options='';
|
|
|
|
}
|
|
|
|
$monitors = $this->Monitor->find('all',$options);
|
2014-04-23 11:44:56 +08:00
|
|
|
$this->set(array(
|
|
|
|
'monitors' => $monitors,
|
|
|
|
'_serialize' => array('monitors')
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* view method
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @param string $id
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function view($id = null) {
|
2014-04-23 11:56:40 +08:00
|
|
|
$this->Monitor->recursive = 0;
|
2014-04-23 11:44:56 +08:00
|
|
|
if (!$this->Monitor->exists($id)) {
|
|
|
|
throw new NotFoundException(__('Invalid monitor'));
|
|
|
|
}
|
2015-12-20 06:44:46 +08:00
|
|
|
$allowedMonitors=preg_split ('@,@', $this->Session->Read('allowedMonitors'),NULL, PREG_SPLIT_NO_EMPTY);
|
|
|
|
if (!empty($allowedMonitors))
|
|
|
|
{
|
|
|
|
$restricted = array('Monitor.' . $this->Monitor->primaryKey => $allowedMonitors);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$restricted = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$options = array('conditions' => array(
|
|
|
|
array('Monitor.' . $this->Monitor->primaryKey => $id),
|
|
|
|
$restricted
|
|
|
|
)
|
|
|
|
);
|
2014-04-23 11:56:40 +08:00
|
|
|
$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
|
|
|
|
*/
|
|
|
|
public function add() {
|
|
|
|
if ($this->request->is('post')) {
|
2015-12-20 06:44:46 +08:00
|
|
|
|
|
|
|
if ($this->Session->Read('systemPermission') != 'Edit')
|
|
|
|
{
|
|
|
|
throw new UnauthotizedException(__('Insufficient privileges'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-23 11:44:56 +08:00
|
|
|
$this->Monitor->create();
|
|
|
|
if ($this->Monitor->save($this->request->data)) {
|
2015-06-11 10:58:58 +08:00
|
|
|
$this->daemonControl($this->Monitor->id, 'start', $this->request->data);
|
2014-04-23 11:44:56 +08:00
|
|
|
return $this->flash(__('The monitor has been saved.'), array('action' => 'index'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* edit method
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @param string $id
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function edit($id = null) {
|
2014-04-25 11:38:51 +08:00
|
|
|
$this->Monitor->id = $id;
|
|
|
|
|
2014-04-23 11:44:56 +08:00
|
|
|
if (!$this->Monitor->exists($id)) {
|
|
|
|
throw new NotFoundException(__('Invalid monitor'));
|
|
|
|
}
|
2015-12-20 06:44:46 +08:00
|
|
|
if ($this->Session->Read('systemPermission') != 'Edit')
|
|
|
|
{
|
|
|
|
throw new UnauthorizedException(__('Insufficient privileges'));
|
|
|
|
return;
|
|
|
|
}
|
2014-04-25 11:38:51 +08:00
|
|
|
if ($this->Monitor->save($this->request->data)) {
|
|
|
|
$message = 'Saved';
|
2014-04-23 11:44:56 +08:00
|
|
|
} else {
|
2014-04-25 11:38:51 +08:00
|
|
|
$message = 'Error';
|
2014-04-23 11:44:56 +08:00
|
|
|
}
|
2014-04-25 11:38:51 +08:00
|
|
|
|
|
|
|
$this->set(array(
|
2014-04-26 02:20:23 +08:00
|
|
|
'message' => $message,
|
2014-04-25 11:38:51 +08:00
|
|
|
'_serialize' => array('message')
|
|
|
|
));
|
2015-12-20 07:36:38 +08:00
|
|
|
// - restart this monitor after change
|
2015-12-20 06:44:46 +08:00
|
|
|
$this->daemonControl($this->Monitor->id, 'restart', $this->request->data);
|
2014-04-23 11:44:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* delete method
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @param string $id
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function delete($id = null) {
|
|
|
|
$this->Monitor->id = $id;
|
|
|
|
if (!$this->Monitor->exists()) {
|
|
|
|
throw new NotFoundException(__('Invalid monitor'));
|
|
|
|
}
|
2015-12-20 06:44:46 +08:00
|
|
|
if ($this->Session->Read('systemPermission') != 'Edit')
|
|
|
|
{
|
|
|
|
throw new UnauthorizedException(__('Insufficient privileges'));
|
|
|
|
return;
|
|
|
|
}
|
2014-04-23 11:44:56 +08:00
|
|
|
$this->request->allowMethod('post', 'delete');
|
2015-06-11 10:58:58 +08:00
|
|
|
|
|
|
|
$this->daemonControl($this->Monitor->id, 'stop');
|
|
|
|
|
2014-04-23 11:44:56 +08:00
|
|
|
if ($this->Monitor->delete()) {
|
|
|
|
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'));
|
|
|
|
}
|
2014-08-25 21:08:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function sourceTypes() {
|
|
|
|
$sourceTypes = $this->Monitor->query("describe Monitors Type;");
|
|
|
|
|
|
|
|
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')
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2015-06-11 10:58:58 +08:00
|
|
|
// 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'];
|
|
|
|
|
|
|
|
if (!$this->Monitor->exists($id)) {
|
|
|
|
throw new NotFoundException(__('Invalid monitor'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
|
if ($monitor[0]['Type'] == 'Local') {
|
|
|
|
$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");
|
|
|
|
$status = exec( $command );
|
|
|
|
|
|
|
|
// 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'),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function daemonControl($id, $command, $monitor=null, $daemon=null) {
|
|
|
|
$args = '';
|
|
|
|
$daemons = array();
|
|
|
|
|
|
|
|
if (!$monitor) {
|
|
|
|
// Need to see if it is local or remote
|
|
|
|
$monitor = $this->Monitor->find('first', array(
|
|
|
|
'fields' => array('Type', 'Function'),
|
|
|
|
'conditions' => array('Id' => $id)
|
|
|
|
));
|
|
|
|
$monitor = $monitor['Monitor'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($monitor['Type'] == 'Local') {
|
|
|
|
$args = "-d " . $monitor['Device'];
|
|
|
|
} else {
|
|
|
|
$args = "-m " . $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($monitor['Function'] == 'Monitor') {
|
|
|
|
array_push($daemons, 'zmc');
|
|
|
|
} else {
|
|
|
|
array_push($daemons, 'zmc', 'zma');
|
|
|
|
}
|
|
|
|
|
|
|
|
$zm_path_bin = Configure::read('ZM_PATH_BIN');
|
|
|
|
|
|
|
|
foreach ($daemons as $daemon) {
|
|
|
|
$shellcmd = escapeshellcmd("$zm_path_bin/zmdc.pl $command $daemon $args");
|
|
|
|
$status = exec( $shellcmd );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-25 21:08:56 +08:00
|
|
|
}
|
|
|
|
|