-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctrl.php
121 lines (94 loc) · 4.1 KB
/
ctrl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/*
The MIT License (MIT)
Copyright (c) 2014 University of Waterloo Amateur Radio Club
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
require_once('classes/RecordingsSQLite3.class.php');
require_once('config.php');
/** Intialize objects **/
$recordings = new RecordingsSQLite3();
if (!isset($argv[0])) die('Sorry, you are not permitted to execute this file.' . PHP_EOL);
if (!isset($argv[1])) die('Usage: php ctrl.php (add-recording|add-all-recordings|clear|add-talkgroup|add-radio)' . PHP_EOL);
switch($argv[1])
{
case 'add-recording':
if (!isset($argv[2])) die('Must provided the filename of a recording to add.' . PHP_EOL);
$recordings->addRecording($argv[2]);
break;
case 'add-all-recordings':
echo 'This will add all recordings in ' . RECORDINGS_PATH . ' and my include duplicates. Do you want to continue (Y/n)?' . PHP_EOL;
$stdin = fopen('php://stdin', 'r');
$answer = fgets($stdin, 2);
$answer = rtrim($answer);
fclose($stdin);
if ($answer == 'Y' || $answer == 'y')
{
echo 'Adding all recordings...';
$files = Array();
if ($handle = opendir(RECORDINGS_PATH))
{
while (false !== ($entry = readdir($handle)))
{
if (is_dir($entry)) continue;
$str_date = preg_replace('/.*(\d{4})-(\d{2})-(\d{2})-(\d{2}):?(\d{2}):?(\d{2}).*/', '$1-$2-$3 $4:$5:$6', $entry);
// Get the time of the recording
// https://www.simonholywell.com/post/2013/12/convert-utc-to-local-time.html
$utcDate = DateTime::createFromFormat(
'Y-m-d H:i:s',
$str_date,
new DateTimeZone('UTC')
);
if (!$utcDate) continue;
$files[] = Array(
'file' => $entry,
'timestamp' => $utcDate->getTimestamp()
);
}// End of while
}// End of if
usort($files, function($a, $b) {
return $a['timestamp'] > $b['timestamp'];
});
foreach ($files as $file)
{
echo 'Adding: ' . $file['file'] . PHP_EOL;
$recordings->addRecording($file['file']);
}// End of foreach
}// End of if
break;
case 'clear':
echo 'This will add all recordings in ' . RECORDINGS_PATH . ' and my include duplicates. Do you want to continue (Y/n)?' . PHP_EOL;
$stdin = fopen('php://stdin', 'r');
$answer = fgets($stdin, 2);
$answer = rtrim($answer);
fclose($stdin);
if ($answer == 'Y' || $answer == 'y')
{
unlink(SQLITE_DATABASE);
}// End of if
break;
case 'add-talkgroup':
if (!isset($argv[2])) die('Please specify talkgroup ID.' . PHP_EOL);
if (!isset($argv[3])) die('Please specify talkgroup name.' . PHP_EOL);
$recordings->addTalkgroup($argv[2], $argv[3]);
break;
case 'add-radio':
if (!isset($argv[2])) die('Please specify radio ID.' . PHP_EOL);
if (!isset($argv[3])) die('Please specify radio name.' . PHP_EOL);
$recordings->addRadio($argv[2], $argv[3]);
break;
}// End of switch