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

Db health check/ layouts fix #608

Merged
merged 5 commits into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions scripts/check_dir_permission.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

# script to change permission of file

# File variable to store location

FILE="$1"

if [[ ! -e "${FILE}" ]]; then
# creating directory...
mkdir -p "${FILE}"
# write permission of other and group of file
chmod 770 "${FILE}"
chown -R bluecherry:bluecherry "${FILE}"
elif [[ ! -d "${FILE}" ]]; then
echo "FILE already exists but is not a directory"
fi

# find out if file has write permission or not
#[ -w $FILE ] && W="Write = yes" || W="Write = No"
[ -w $FILE ] && W=w || W='-'

# find out if file has excute permission or not
[ -x $FILE ] && X=x || X='-'

# find out if file has read permission or not
[ -r $FILE ] && R=r || R='-'

#echo "$FILE permissions"
echo "-$W$R$X"
69 changes: 58 additions & 11 deletions www/ajax/devices.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public function getData()
$this->ipCameras->arr = array();
$this->ipCameras->ok = Array();
$this->ipCameras->disabled = Array();

$this->getCards();
$this->getIpCameras();


$this->setView('ajax.devices');
$devices = new StdClass();

Expand All @@ -30,8 +30,13 @@ public function getData()

$this->view->devices = $devices;

if ($this->reqJSON()) {
$this->Makejson();
die();
}

if ($this->reqXML()) {
$this->MakeXML();
$this->MakeXML();
die();
}
}
Expand Down Expand Up @@ -73,14 +78,15 @@ private function getCards()
{
$cards = data::query("SELECT card_id FROM AvailableSources GROUP BY card_id");
$this->info['total_devices'] = 0;
if (!$cards) { return false; }
else {
foreach ($cards as $key => $card){
$this->cards[$card['card_id']] = new card($card['card_id']);
$this->info['total_devices'] += count($this->cards[$card['card_id']]->cameras);
}

}
if (!$cards) {
return false;
} else {
foreach ($cards as $key => $card){
$this->cards[$card['card_id']] = new card($card['card_id']);
$this->info['total_devices'] += count($this->cards[$card['card_id']]->cameras);
}

}
}
private function getIpCameras(){
$info = data::query("SELECT * FROM Devices WHERE protocol in ('IP-RTSP', 'IP-MJPEG', 'IP')");
Expand Down Expand Up @@ -141,7 +147,48 @@ public function MakeXML(){
header('Content-type: text/xml');
print $xml;
}

public function MakeJSON() {
$this_user = new user('id', $_SESSION['id']);
$devices = array();
if (!empty($this->cards)) {
foreach ($this->cards as $card_id => $card) {
$devices = array_merge($devices, $card->cameras);
}
}
if (!empty($this->ipCameras->arr))
$devices = array_merge($devices, $this->ipCameras->arr);

$json_data = array('devices' => array());

$short_props = array('protocol', 'device_name', 'resolutionX', 'resolutionY');
$block_props = array('rtsp_password', 'rtsp_username');

foreach ($devices as $device) {
if (isset($device->info['id']) && !$this_user->camPermission($device->info['id']))
continue;

$device_data = array();
if (!empty($device->info['id']))
$device_data['id'] = $device->info['id'];

foreach ($device->info as $prop => $val) {
if (isset($_GET['short']) && !in_array($prop, $short_props))
continue;

if (in_array($prop, $block_props) || is_array($prop) || is_array($val))
continue;

$device_data[$prop] = $val;
}

$json_data['devices'][] = $device_data;
}

header('Content-type: application/json');
echo json_encode($json_data, JSON_PRETTY_PRINT);
}


}


171 changes: 94 additions & 77 deletions www/ajax/events/eventsIndex.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
<?php

class eventsIndex extends Controller {

public function __construct()
{
parent::__construct();
$this->chAccess('backup');
$this->chAccess('backup');
}

public function getData()
Expand All @@ -17,22 +17,29 @@ public function getData()

$events_portion = 5000;

$query = "SELECT EventsCam.*, Media.size AS media_size, Devices.device_name, ((Media.size>0 OR Media.end=0) AND Media.filepath!='') AS media_available ".
"FROM EventsCam LEFT JOIN Media ON (EventsCam.media_id=Media.id) LEFT JOIN Devices ON (EventsCam.device_id=Devices.id) ".
"WHERE ";
$query = "SELECT EventsCam.*, Media.size AS media_size, Media.start, Media.end, Devices.device_name, ((Media.size>0 OR Media.end=0) AND Media.filepath!='') AS media_available, ".
"IFNULL(TIMESTAMPDIFF(SECOND, Media.start, Media.end), 0) AS video_duration ".
"FROM EventsCam ".
"LEFT JOIN Media ON (EventsCam.media_id=Media.id) ".
"LEFT JOIN Devices ON (EventsCam.device_id=Devices.id) ".
"WHERE ";


if (isset($_GET['startDate']))
$query .= "EventsCam.time >= ".((int)$_GET['startDate'])." AND ";
$query .= "EventsCam.time >= " . ((int)$_GET['startDate']) . " AND ";
if (isset($_GET['endDate']))
$query .= "EventsCam.time <= ".((int)$_GET['endDate'])." AND ";
$query .= "EventsCam.time <= " . ((int)$_GET['endDate']) . " AND ";
if (isset($_GET['beforeId']))
$query .= "EventsCam.id < ".((int)$_GET['beforeId'])." AND ";
$query .= "EventsCam.id < " . ((int)$_GET['beforeId']) . " AND ";
if (isset($_GET['afterId']))
$query .= "EventsCam.id > ".((int)$_GET['afterId'])." AND ";
$query .= "EventsCam.id > " . ((int)$_GET['afterId']) . " AND ";
if (isset($_GET['id']))
$query .= "EventsCam.id = ".((int)$_GET['id'])." AND ";
$query .= "EventsCam.id = " . ((int)$_GET['id']) . " AND ";

if (empty($current_user->data[0]['access_device_list'])){
$current_user->data[0]['access_device_list'] = '-1';
$current_user->data[0]['access_device_list'] = '-1';
}

$query .= "EventsCam.device_id NOT IN ({$current_user->data[0]['access_device_list']}) ";
$query .= "ORDER BY EventsCam.id DESC ";

Expand All @@ -46,83 +53,93 @@ public function getData()


# Output header for this feed
print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
print "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n";
print " <title type=\"text\">Bluecherry Events for " .
$_SERVER["SERVER_NAME"] . "</title>\n";

if (isset($events) && $events) {
$curevent = current($events);
$lastupdate = date(DATE_ATOM, $curevent['time']);
} else {
$lastupdate = date(DATE_ATOM);
}
header('Content-Type: application/json');

print " <updated>" . $lastupdate . "</updated>\n";
$data = array();

# XXX Need to generate this per-server
print " <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>\n";
$data['title'] = 'Bluecherry Events for ' . $_SERVER['SERVER_NAME'];

print " <link rel=\"self\" type=\"application/atom+xml\" " .
"href=\"http://" . $_SERVER["SERVER_NAME"] . "/events/\"/>\n";
print " <generator uri=\"http://www.bluecherrydvr.com/atom.html\" version=\"1.0\">\n";
print " BluecherryDVR Events Atom Generator\n";
print " </generator>\n";

# Output one events portion per query with limit/offset
//for ($portions = 0; $portions < $portions_num; $portions++) {
$offset = 0;
while (true) {

$limit = min($requested_limit, $events_portion);

if ($limit < 0)
$limit = $events_portion;
if (isset($events) && $events) {
$curevent = current($events);
$lastupdate = date(DATE_ATOM, $curevent['time']);
} else {
$lastupdate = date(DATE_ATOM);
}

$events = data::query($query." LIMIT ".$limit." OFFSET ".$offset);
$data['updated'] = $lastupdate;

# Output one item for each event
foreach ($events as $item) {
if (!$current_user->camPermission($item['device_id']))
continue;
// XXX Need to generate this per-server
$data['id'] = 'urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6';

print " <entry>\n";
print " <id raw=\"".$item['id']."\">http://".$_SERVER['SERVER_NAME']."/events/?id=".$item['id']."</id>\n";
print " <title>" . $item['level_id'] . ": " . $item['type_id'] .
" event on device " . $item['device_name'] . "</title>\n";
print " <published>" . date(DATE_ATOM, $item['time']) .
"</published>\n";
$data['link'] = array(
'rel' => 'self',
'type' => 'application/json',
'href' => 'http://' . $_SERVER['SERVER_NAME'] . '/events/'
);

/* If updated exists and is empty, the event is on-going */
if (!empty($item['length'])) {
print " <updated>";
if ($item['length'] > 0) {
print date(DATE_ATOM, $item['time'] +
$item['length']);
}
print "</updated>\n";
}
$data['generator'] = array(
'uri' => 'http://www.bluecherrydvr.com/atom.html',
'version' => '1.0',
'content' => 'BluecherryDVR Events Atom Generator'
);

print " <category scheme=\"http://www.bluecherrydvr.com/atom.html\" " .
"term=\"" . $item['device_id'] . "/" . $item['level_id'] . "/" .
$item['type_id'] . "\"/>\n";
$entries = array();

if (!empty($item['media_id']) && $item['media_available']) {
print " <content media_id=\"".$item['media_id']."\" media_size=\"".$item['media_size']."\">";
print (!empty($_SERVER['HTTPS']) ? "https" : "http")."://".$_SERVER['HTTP_HOST']."/media/request.php?id=".$item['media_id'];
print "</content>\n";
}
$offset = 0;
while (true) {
$limit = min($requested_limit, $events_portion);

if ($limit < 0) {
$limit = $events_portion;
}

$events = data::query($query . ' LIMIT ' . $limit . ' OFFSET ' . $offset);

foreach ($events as $item) {
if (!$current_user->camPermission($item['device_id'])) {
continue;
}

$entry = array(
'id' => 'http://' . $_SERVER['SERVER_NAME'] . '/events/?id=' . $item['id'],
'title' => $item['level_id'] . ': ' . $item['type_id'] . ' event on device ' . $item['device_name'],
'published' => date(DATE_ATOM, $item['time'])
);

if (!empty($item['length'])) {
if ($item['length'] > 0) {
$entry['updated'] = date(DATE_ATOM, $item['time'] + $item['length']);
}
}

$entry['category'] = array(
'scheme' => 'http://www.bluecherrydvr.com/atom.html',
'term' => $item['device_id'] . '/' . $item['level_id'] . '/' . $item['type_id']
);

if (!empty($item['media_id']) && $item['media_available']) {
$entry['content'] = array(
'media_id' => $item['media_id'],
'media_size' => $item['media_size'],
'media_duration' => $item['media_duration'],
'content' => (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/media/request.php?id=' . $item['media_id']
);
}

$entries[] = $entry;
}

if (count($events) < $events_portion) {
break;
}

$offset += $events_portion;
}

print " </entry>\n";
}
$data['entry'] = $entries;

if (count($events) < $events_portion)
break;
echo json_encode($data, JSON_PRETTY_PRINT);

$offset += $events_portion;
}
# Close it out
print "</feed>\n";
}

public function postData()
Expand Down
Loading