#!/usr/bin/php <?php $DIAG = 2; $LOGPATH = "/tmp/webcams.log"; $host = "127.0.0.1"; $user = "root"; $pass = ""; $db = "zm"; $directory = "/var/cache/zoneminder/webcams/"; $dblastid = "0"; $mainpull = "0"; $nextrotate = "10"; $nextpull = "60"; $autoadjustrefresh = FALSE; $ms = mysql_pconnect($host, $user, $pass); if ( !$ms ) { echo ""; } mysql_select_db($db); //connection complete. chdir ( $directory ); if ($DIAG > 0) { $LogFile = fopen($LOGPATH, 'a') or die("can't open file"); }; if ($DIAG > 0) { fwrite($LogFile, "Starting Logfile at : " . time(). " ...\n"); }; function RefreshAllImages() { global $directory, $dblastid, $nextpull,$DIAG,$LogFile; $q = "SELECT * from WebCams where Enabled='1'"; $r = mysql_query($q); if (!$r) { echo ("Problems\r"); } else { for ($rows = 0; $rows < mysql_num_rows($r); $rows++) { $filenamepath = $directory . "originals/" . mysql_result($r,$rows,'Monitor') . mysql_result($r,$rows,'ID') . ".jpg"; $filename = str_replace($directory."originals/","",$filenamepath); $filetime = filemtime($filenamepath); if ($DIAG >= 2) { fwrite($LogFile, "Pulling File : " . $filenamepath . "\n"); }; // old command $cmd = "wget -q -t 2 -T 5 \"" . mysql_result($r,$rows,'URL') . "\" -O \"" . $filenamepath . "\""; $cmd = "curl -q -m 10 -R --retry 1 -s -f --url \"" . mysql_result($r,$rows,'URL') . "\" -o \"" . $filenamepath . "\""; if ($DIAG >= 2) { fwrite($LogFile, "Using Command : " . $cmd . "\n"); }; exec($cmd); clearstatcache(TRUE, $filenamepath); if ( (filesize($filenamepath)!="0") && ($filetime != filemtime($filenamepath)) ) { if ($DIAG >= 2) { fwrite($LogFile, "Converting File : " . filectime($filenamepath) . $filenamepath . "\n"); }; $cmd = "convert " . $filenamepath . " -resize 640x480\! -pointsize 20 -fill yellow -draw 'text 10, 20 \"" . mysql_result($r,$rows,'Name') . "\"' " . $directory . "resized/" . $filename; exec($cmd); touch ( $directory . "resized/" . $filename, filemtime($filenamepath) ); }; }; $dblastid = mysql_result($r,$rows-1,'ID'); }; if ($DIAG >= 2) { fwrite($LogFile, "Finished Pulling All Files!\n"); }; $nextpull = (time() + 60); }; function RefreshImages() { global $directory, $dblastid, $nextpull, $autoadjustrefresh, $DIAG, $LogFile; clearstatcache(); if ($DIAG >= 2) { fwrite($LogFile, "Refreshing Images...\n"); }; $q = "SELECT * from WebCams where Enabled='1'"; $r = mysql_query($q); for ($rows = 0; $rows < mysql_num_rows($r); $rows++) { $start = mysql_result($r,$rows,'Start'); $stop = mysql_result($r,$rows,'Stop'); if ($DIAG >= 2) { fwrite($LogFile, "Start : $start. Stop : $stop. Time : " . date("H:i:s") . ".\n"); }; if ( ( ($start < $stop) && ($start < date("H:i:s")) && ($stop > date("H:i:s")) ) || ( ($start > $stop) && ( (($start < date("H:i:s")) && ($stop < date("H:i:s"))) || (($start > date("H:i:s")) && ($stop > date("H:i:s"))) ) ) ) { if ($DIAG >= 2) { fwrite($LogFile, "In timeframe running checks...\n"); }; $row_filename = mysql_result($r,$rows,'Monitor').mysql_result($r,$rows,'ID').".jpg"; $row_id = mysql_result($r,$rows,'ID'); $row_refresh = time() - mysql_result($r,$rows,'Refresh'); $filenamepath = $directory."originals/".$row_filename; $filectime = filectime($filenamepath); $filemtime = filemtime($filenamepath); if ( (($filemtime > time() - 3600) && ($filectime < $row_refresh)) || (!file_exists($filenamepath)) ) { if ($DIAG >= 2) { fwrite($LogFile, "Pulling New File : " . filectime($filenamepath) . $filenamepath . "\n"); }; // old command $cmd = "wget -q -t 2 -T 5 \"" . mysql_result($r,$rows,'URL') . "\" -O \"" . $filenamepath . "\""; $cmd = "curl -q -m 10 -R --retry 1 -s -f --url \"" . mysql_result($r,$rows,'URL') . "\" -o \"" . $filenamepath . "\""; if ($DIAG >= 2) { fwrite($LogFile, "Using Command : " . $cmd . "\n"); }; exec($cmd); clearstatcache(TRUE, $filenamepath); if (( filemtime($filenamepath) > $filemtime ) && (filesize($filenamepath)!="0") ) { if ( $autoadjustrefresh == TRUE ) { if ($DIAG >= 2) { fwrite($LogFile, "Adjusting refresh time.\n Calculating difference...\n"); }; $timedifference = filemtime($filenamepath) - $filemtime; if ($DIAG >= 2) { fwrite($LogFile, "Difference : " . $timedifference . "\n"); }; if ( ($timedifference > (mysql_result($r,$rows,'Refresh')+30) || $timedifference < (mysql_result($r,$rows,'Refresh')-30)) && ($timedifference < 900)) { if ($DIAG >= 2) { fwrite($LogFile, "Updating Database Refresh timer.\n"); }; $q2="UPDATE WebCams set Refresh='" . ($timedifference) . "' WHERE ID='$row_id'"; $r2 = mysql_query($q2); }; }; if ($DIAG >= 2) { fwrite($LogFile, "Attempting resize : " . filemtime($filenamepath) . $filenamepath . "\n"); }; $cmd = "convert $filenamepath -resize 640x480\! -pointsize 20 -fill yellow -draw 'text 10, 20 \"" . mysql_result($r,$rows,'Name') . "\"' " . $directory . "resized/" . $row_filename; exec($cmd); touch ( $directory . "resized/" . $row_filename, filemtime($filenamepath) ); } else { if ($DIAG >= 2){ fwrite($LogFile, "$row_filename Not Newer : " . filemtime($filenamepath) . " <= " . $filemtime . " Ignoring\n"); }; }; }; }; }; $nextpull = (time() + 60); }; function RotateImages() { global $directory, $nextrotate,$DIAG,$LogFile; $newimageset = FALSE; $imagefound = FALSE; clearstatcache(); $q = "SELECT distinct(Monitor) from WebCams"; $r = mysql_query($q); for ($rows = 0; $rows < mysql_num_rows($r); $rows++) { if ($DIAG >= 1){ fwrite($LogFile, "Rotating Monitor " . mysql_result($r,$rows,'Monitor') . "\n"); }; if ($DIAG >= 1){ fwrite($LogFile, "Checking for New Files ...\n"); }; $newimageset = FALSE; $imagefound = FALSE; if (mysql_result($r,$rows,'Monitor') != 'C'){ foreach (glob($directory."resized/".mysql_result($r,$rows,'Monitor')."*.jpg") as $filenamepath) { if ($DIAG >= 1){ fwrite($LogFile, " " . $filenamepath ); }; if (filectime($filenamepath) > (time()-15) ) { if ($DIAG >= 1){ fwrite($LogFile, "\n New file Found linking ... \n"); }; exec( "ln -sf " . $filenamepath . " " . $directory . "rotate" . mysql_result($r,$rows,'Monitor') . ".jpg" ); $newimageset = TRUE; }; }; }; if ($DIAG >= 1){ fwrite($LogFile, "Finished lookin for newer files.\n"); }; if ( $newimageset == FALSE) { $rotatefile = $directory."rotate".mysql_result($r,$rows,'Monitor').".jpg"; if (file_exists($rotatefile) ) { if ($DIAG >= 1){ fwrite($LogFile, "\nLooking for Link File : $rotatefile ...\n"); }; $currentfile = readlink($rotatefile); if ($DIAG >= 1){ fwrite($LogFile, "Current : " . $currentfile . "\n"); }; foreach (glob($directory."resized/".mysql_result($r,$rows,'Monitor')."*.jpg") as $filenamepath) { if ( $filenamepath == $currentfile ) { if($DIAG >= 1){ fwrite($LogFile, "Found Correct entry.\n"); }; $imagefound = TRUE; } elseif (($imagefound) && (!$newimageset)) { if ($DIAG >= 1){ fwrite($LogFile, "Checking next Image...\n"); }; if (filectime($filenamepath) > (time() - 1800) ) { if ($DIAG >= 1){ fwrite($LogFile, "Next Suitable Image " . $filenamepath . " Linking...\n"); }; exec( "ln -sf " . $filenamepath . " " . $directory . "rotate" . mysql_result($r,$rows,'Monitor') . ".jpg" ); $newimageset = TRUE; }; }; }; }; }; if ($newimageset == FALSE) { if ($DIAG >= 1){ fwrite($LogFile, "Image still not set, Falling back to load first suitable\n"); }; foreach (glob($directory."resized/".mysql_result($r,$rows,'Monitor')."*.jpg") as $filenamepath) { if ($DIAG >= 1){ fwrite($LogFile, "Checking File " . $filenamepath . "\n"); }; if (filectime($filenamepath) > (time() - 1800) && ($newimageset == FALSE) ) { exec( "ln -sf " . $filenamepath . " " . $directory . "rotate" . mysql_result($r,$rows,'Monitor') . ".jpg" ); $newimageset = TRUE; }; }; }; }; $nextrotate = (time() + 10); }; $pulled = 0; for (;;) { if (time() > $nextpull) { if ($pulled == 0 or $pulled == 60) { RefreshAllImages(); $pulled = 1; } else { RefreshImages(); $pulled++; }; }; if (time() > $nextrotate) { RotateImages(); }; sleep(2); }; ?>Save the file then chmod +x ~/webcams2.php
Labels: Webcams, xlib_shm, Zoneminder