00001 <?php
00002
00003 # header ('Content-type: text/plain');
00004 header ('Content-type: text/xml');
00005
00006 include "tag.php";
00007 include "def.php";
00008 include "db.php";
00009 include "check.php";
00010
00011
00012 function stopSession ($mac, $id, $ses_id)
00013 {
00014
00015 $debug = fopen("/tmp/dvr.log", "a");
00016 fwrite($debug, "session.php: cmd: \n");
00017 fwrite($debug, $cmd."\n");
00018 fclose($debug);
00019
00020 $mac_orig = $mac;
00021
00022 if (($mac = checkMac ($mac)) == -1)
00023 {
00024 return error ("Error: Wrong MAC=" . $mac_orig, "recorder");
00025 }
00026
00027 $query = "SELECT * FROM cameras WHERE mac='" . $mac . "' AND del=0";
00028
00029 $query_resp = @mysql_query ($query, $id);
00030 $num = @mysql_numrows ($query_resp);
00031
00032 if ($num < 1)
00033 {
00034 return error ("Error: Camera with MAC='" . $mac_orig . "' is not found", "recorder");
00035 }
00036
00037 $query = "SELECT * FROM sessions WHERE mac='" . $mac . "' AND id='" . $ses_id . "' AND del=0";
00038
00039 $query_resp = @mysql_query ($query, $id);
00040 $num = @mysql_numrows ($query_resp);
00041
00042 if ($num < 1)
00043 {
00044 return error ("Error: Database has no info about the session with id='" . $ses_id . "' (camera MAC='". $mac_orig . "')", "recorder");
00045 }
00046
00047 $stop_stream = @mysql_result ($query_resp, $i, "stop_stream");
00048 if ($stop_stream != 0)
00049 {
00050 return error ("Error: This session seems to be already stopped. Reason: stop_stream field already set as " . $stop_stream, "recorder");
00051 }
00052
00053 $pid = @mysql_result ($query_resp, $i, "strm_pid");
00054 if (!posix_kill ($pid, 1))
00055 {
00056
00057
00058 }
00059
00060 $stop_stream = gettimeofday();
00061 $stop_stream = $stop_stream["sec"];
00062
00063 $log_file = @mysql_result ($query_resp, $i, "log_file");
00064 $status = shell_exec("cat $log_file | tr -s '\r' '\n' | tail -n 2 | tr -s '\n' '\t' ");
00065
00066 $query = "UPDATE sessions SET stop_stream='" . $stop_stream . "', status='" . $status . "' WHERE mac='" . $mac ;
00067 $query .= "' AND id='" . $ses_id . "' AND del=0";
00068 $query_resp = @mysql_query ($query, $id);
00069
00070 $ret = formClosedTag ("OK", "stop", "") . "\n";
00071 $ret = formClosedTag ($ret, "recorder", "\n");
00072
00073 unlink ($log_file);
00074
00075 return $ret;
00076 }
00077
00078
00079 function startSession ($mac, $id, $strm_rtsp_ip, $strm_ip, $strm_port, $fps, $width, $height, $max_file_size, $max_frame_size, $tmp)
00080 {
00081
00082 $DROOT = $_SERVER['DOCUMENT_ROOT'];
00083 $mac_orig = $mac;
00084
00085 if (($mac = checkMac ($mac)) == -1)
00086 {
00087 return error ("Error: Wrong MAC=" . $mac_orig, "recorder");
00088 }
00089
00090 $query = "SELECT * FROM cameras WHERE mac='" . $mac . "' AND del=0";
00091
00092 $query_resp = mysql_query ($query, $id);
00093 $num = mysql_numrows ($query_resp);
00094
00095 if ($num < 1)
00096 {
00097 return error ("Error: Camera with MAC='" . $mac . "' is not found", "recorder");
00098 }
00099
00100 $ip1 = @mysql_result ($query_resp, $i, "ip1");
00101 $ip2 = @mysql_result ($query_resp, $i, "ip2");
00102 $place = @mysql_result ($query_resp, $i, "place");
00103
00104 if (! $strm_rtsp_ip)
00105 {
00106 return error ("Error: May be you use old camera firmware; Wrong strm_rtsp_ip=" . $strm_rtsp_ip, "recorder");
00107 }
00108
00109 if (! $strm_ip)
00110 {
00111 return error ("Error: Wrong strm_ip=" . $strm_ip, "recorder");
00112 }
00113
00114 if (! $strm_port)
00115 {
00116 return error ("Error: Wrong strm_port=" . $strm_port, "recorder");
00117 }
00118
00119 if (! $fps)
00120 {
00121 return error ("Error: Wrong fps=" . $fps, "recorder");
00122 }
00123
00124
00125
00126 $start = gettimeofday();
00127 $start_sec = $start["sec"];
00128 $start_usec = $start["usec"];
00129 $start_stream = $start_sec;
00130 $stop_stream = 0;
00131
00132 $log_file = log_dir . "/" . $mac . "_" . $start_sec . "_" . $start_usec . ".log" ;
00133
00134 $cmd = "nohup " . eo ." -o -u rtsp://" . $strm_rtsp_ip . "/track1 -f " . $fps ;
00135
00136 if ($skip) $cmd .= " -k " . $skip;
00137 if ($max_file_size) $cmd .= " -m " . $max_file_size;
00138 if ($max_frame_size) $cmd .= " -F " . $max_frame_size;
00139 $cmd .= " > /dev/null 2>" . $log_file . " & echo $! ";
00140
00141
00142
00143 $ses_data_dir=$DROOT.url_base;
00144 if (! file_exists ("$ses_data_dir"))
00145 {
00146 if (! @mkdir ("$ses_data_dir"))
00147 {
00148
00149 return error ("Error: Can't create directory '" . $ses_data_dir . "'!"."document_ROOT=".$DROOT, "recorder");
00150 }
00151 }
00152
00153 $debug = fopen("/tmp/dvr.log", "a");
00154 fwrite($debug, "session.php: cmd: \n");
00155 fwrite($debug, $cmd."\n");
00156 fclose($debug);
00157
00158
00159 $ses_data_dir .= "/camera_" . $mac;
00160 if (! file_exists ("$ses_data_dir"))
00161 {
00162 if (! @mkdir ("$ses_data_dir"))
00163 {
00164 return error ("Error: Can't create directory '" . $ses_data_dir . "'!", "recorder");
00165 }
00166 }
00167
00168 $ses_data_dir .= "/" . $start_stream;
00169 if (! file_exists ("$ses_data_dir"))
00170 {
00171 if (! @mkdir ("$ses_data_dir"))
00172 {
00173 return error ("Error: Can't create directory '" . $ses_data_dir . "'!", "recorder");
00174 }
00175 }
00176
00177 if (! @chdir ("$ses_data_dir"))
00178 {
00179 return error ("Error: Can't cdir to directory '" . $ses_data_dir . "'!", "recorder");
00180 }
00181
00182
00183 escapeshellcmd ($cmd);
00184 $pid = trim (shell_exec ($cmd));
00185
00186 $query = "INSERT INTO sessions ";
00187 $query .= " (mac, strm_pid, ip1, ip2, place, start_stream, fps, skip, max_file_size, max_frame_size, strm_ip, strm_port, width, height, tmp, log_file, del)";
00188 $query .= " VALUES ('$mac', '$pid','$ip1', '$ip2', '$place', '$start_stream', '$fps', '$skip', '$max_file_size', '$max_frame_size', '$strm_ip', '$strm_port', '$width', '$height', '$tmp', '$log_file', '0')";
00189 $query_resp = mysql_query ($query, $id);
00190
00191 $ret = mysql_insert_id ($id);
00192 $ret = formClosedTag ($ret, "id", "") . "\n";
00193 $ret = formClosedTag ($ret, "session", "\n") . "\n";
00194 $ret = formClosedTag ($ret, "recorder", "\n");
00195
00196
00197 return $ret;
00198 }
00199
00200
00201
00202 function printFileNameList ($mac, $start_stream)
00203 {
00204 $ses_data_dir = work_dir . "/camera_" . $mac . "/" . $start_stream;
00205
00206
00207 if (! @chdir ("$ses_data_dir"))
00208 {
00209 return error ("Error: Can't cdir to directory '" . $ses_data_dir . "'!", "files") . "\n";
00210 }
00211
00212
00213 if ($handle = opendir($ses_data_dir))
00214 {
00215 while (false !== ($file = readdir($handle)))
00216 {
00217 if (! is_dir ($file))
00218 {
00219 $url = url_base . "/camera_" . $mac ."/" . $start_stream . "/" . $file;
00220 $size = stat ($file);
00221 $size = $size[size];
00222 $ret_ = '';
00223 $ret_ .= formClosedTag ($file, "name", "") . "\n";
00224 $ret_ .= formClosedTag ($size, "size", "") . "\n";
00225 $ret_ .= formClosedTag ($url, "url", "") . "\n";
00226 $ret .= formClosedTag ($ret_, "file", "\n") . "\n";
00227 }
00228 }
00229
00230 closedir($handle);
00231 }
00232
00233 $ret = formClosedTag ($ret, "files", "\n") . "\n";
00234
00235 return $ret;
00236 }
00237
00238
00239 function listSession ($mac, $id, $ses_id, $min_start_time, $max_start_time, $next_start_time, $prev_start_time, $print_serv_info)
00240 {
00241 $mac_orig = $mac;
00242 # Insert here check values function!!!
00243
00244 if (!$mac)
00245 {
00246 return error ("Error: Wrong MAC='" . $mac_orig . "'", "recorder");
00247 }
00248
00249 if (($mac = checkMac ($mac)) == -1)
00250 {
00251 return error ("Error: Wrong MAC='" . $mac_orig . "'", "recorder");
00252 }
00253
00254
00255 $query_mac = " mac='" . $mac . "' AND ";
00256
00257 if ($ses_id)
00258 {
00259 $query_id = " id='" . $ses_id . "' AND ";
00260 }
00261
00262 if ($min_start_time)
00263 {
00264 if ($next_start_time || $prev_start_time)
00265 {
00266 return error ("Error: Can not process 'next_start_time' or 'prev_start_time' with 'min_start_time' in the same request!", "recorder");
00267 }
00268
00269 $query_min = " start_stream>='" . $min_start_time . "' AND ";
00270 }
00271
00272 if ($max_start_time)
00273 {
00274 if ($next_start_time || $prev_start_time)
00275 {
00276 return error ("Error: Can not process 'next_start_time' or 'prev_start_time' with 'max_start_time' in the same request!", "recorder");
00277 }
00278
00279 $query_max = " start_stream<='" . $max_start_time . "' AND ";
00280 }
00281
00282
00283 if ($next_start_time)
00284 {
00285 if ($prev_start_time)
00286 {
00287 return error ("Error: Can not process 'next_start_time' with 'prev_start_time' in the same request!", "recorder");
00288 }
00289
00290 $query_next = " start_stream>'" . $next_start_time . "' AND ";
00291 $query_limit = " LIMIT 1 ";
00292 }
00293
00294 if ($prev_start_time)
00295 {
00296 if ($next_start_time)
00297 {
00298 return error ("Error: Can not process 'next_start_time' with 'prev_start_time' in the same request!", "recorder");
00299 }
00300
00301 $query_prev = " start_stream<'" . $prev_start_time . "' AND ";
00302 $query_limit = " LIMIT 1 ";
00303 $query_order = " DESC ";
00304 }
00305
00306 $query = "SELECT * FROM sessions WHERE ";
00307 $query .= $query_mac . $query_id . $query_min . $query_max . $query_next . $query_prev;
00308 $query .= " del=0 ORDER BY start_stream " . $query_order . $query_limit;
00309
00310 $query_resp = mysql_query ($query, $id);
00311 $num = @mysql_numrows ($query_resp);
00312
00313 if ($num < 1)
00314 {
00315 return error ("Error: Database does not have any items satisfying the query!", "recorder");
00316 }
00317 $ret = '';
00318 $i = 0;
00319 while ($i < $num)
00320 {
00321 $ret_ = '';
00322 $ret_ .= formClosedTag (mysql_result ($query_resp, $i, "id"), "id", "") . "\n";
00323 if ($print_serv_info == 1)
00324 {
00325 $ret_ .= formClosedTag (mysql_result ($query_resp, $i, "strm_pid"), "pid", "") . "\n";
00326 $mac_ = getDottedMAC (mysql_result ($query_resp, $i, "mac"));
00327 $ret_ .= formClosedTag ($mac_, "camera_mac", "") . "\n";
00328 }
00329 $ret_ .= formClosedTag (mysql_result ($query_resp, $i, "ip1"), "ip1", "") . "\n";
00330 $ret_ .= formClosedTag (mysql_result ($query_resp, $i, "ip2"), "ip2", "") . "\n";
00331 $ret_ .= formClosedTag (mysql_result ($query_resp, $i, "place"), "place", "") . "\n";
00332 $ret_ .= formClosedTag (mysql_result ($query_resp, $i, "start_stream"), "start_stream", "") . "\n";
00333 $ret_ .= formClosedTag (mysql_result ($query_resp, $i, "stop_stream"), "stop_stream", "") . "\n";
00334 $ret_ .= formClosedTag (mysql_result ($query_resp, $i, "fps"), "fps", "") . "\n";
00335 $ret_ .= formClosedTag (mysql_result ($query_resp, $i, "width"), "width", "") . "\n";
00336 $ret_ .= formClosedTag (mysql_result ($query_resp, $i, "height"), "height", "") . "\n";
00337 $ret_ .= formClosedTag (mysql_result ($query_resp, $i, "tmp"), "tmp", "") . "\n";
00338
00339 $ret_ .= printFileNameList (mysql_result ($query_resp, $i, "mac"), mysql_result ($query_resp, $i, "start_stream"));
00340
00341 $status = mysql_result ($query_resp, $i, "status");
00342 if (!$status)
00343 {
00344 $log_file = @mysql_result ($query_resp, $i, "log_file");
00345 $status = shell_exec("cat $log_file | tr -s '\r' '\n' | tail -n 2 | tr -s '\n' '\t' ");
00346 }
00347 $ret_ .= formClosedTag ($status, "status", "") . "\n";
00348
00349 if ($print_serv_info == 1)
00350 {
00351 $ret_ .= formClosedTag (mysql_result ($query_resp, $i, "skip"), "skip", "") . "\n";
00352 $ret_ .= formClosedTag (mysql_result ($query_resp, $i, "max_file_size"), "max_file_size", "") . "\n";
00353 $ret_ .= formClosedTag (mysql_result ($query_resp, $i, "max_frame_size"), "max_frame_size", "") . "\n";
00354
00355 $ret_ .= formClosedTag (mysql_result ($query_resp, $i, "strm_ip"), "strm_ip", "") . "\n";
00356 $ret_ .= formClosedTag (mysql_result ($query_resp, $i, "strm_port"), "strm_port", "") . "\n";
00357 }
00358
00359 $ret_ = formClosedTag ($ret_, "session", "\n") . "\n";
00360 $ret .= $ret_;
00361 $i++;
00362 }
00363
00364 $ret = formClosedTag ($ret, "recorder", "\n") . "\n";
00365 return $ret;
00366 }
00367
00368
00369
00370
00371
00372 $debug = fopen("/tmp/dvr.log", "a");
00373 fwrite($debug, "session.php: \n");
00374 fwrite($debug, $_SERVER['QUERY_STRING']."\n");
00375 fclose($debug);
00376
00377 switch ($_GET[cmd])
00378 {
00379 case "start":
00380 case "stop":
00381 case "list":
00382 break;
00383 default :
00384 echo error ("Error: Unknown command '" . $_GET[cmd] . "' ", "recorder");
00385 return 0;
00386 break;
00387 }
00388
00389 $id = connectToServer (server, user, password);
00390 if (!$id)
00391 {
00392 echo error ("Error: " . mysql_error(), "recorder");
00393 return 0;
00394 }
00395
00396 if (!openDB (database, $id))
00397 {
00398 echo error ("Error: " . mysql_error($id), "recorder");
00399 closeDB ($id);
00400 return 0;
00401 }
00402
00403 switch ($_GET[cmd])
00404 {
00405 case "list":
00406
00407 $ret = listSession ($_GET[mac], $id, $_GET[ses_id], $_GET[min_start_time], $_GET[max_start_time], $_GET[next_start_time], $_GET[prev_start_time], $_GET[print_serv_info]);
00408 break;
00409 case "start":
00410 $ret = startSession ($_GET[mac], $id, $_GET[strm_rtsp_ip], $_GET[strm_ip], $_GET[strm_port], $_GET[fps], $_GET[width], $_GET[height], $_GET[max_file_size], $_GET[max_frame_size], $_GET[tmp]);
00411 break;
00412 case "stop":
00413 $ret = stopSession ($_GET[mac], $id, $_GET[ses_id]);
00414 break;
00415 default :
00416 $ret = error ("Error: Unknown command \"$_GET[cmd]\"", "recorder");
00417 break;
00418 }
00419
00420 closeDB ($id);
00421
00422 echo $ret
00423
00424 ?>
00425