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