64) return false; $fields = preg_split('/\s+/', trim($spec), -1, PREG_SPLIT_NO_EMPTY); if (count($fields) != 5) return false; // minute, hour, day of month, month, day of week $ranges = array(array(0, 59), array(0, 23), array(1, 31), array(1, 12), array(0, 7)); foreach ($fields as $i => $field) { list($lo, $hi) = $ranges[$i]; foreach (explode(',', $field) as $item) { if ($item === '') return false; // A step may follow either a range or a star: */5, 0-30/10. $step = null; if (strpos($item, '/') !== false) { $parts = explode('/', $item); if (count($parts) != 2) return false; $item = $parts[0]; $step = $parts[1]; if (!preg_match('/^\d+$/', $step) or (intval($step) < 1)) return false; // cron rejects a step after a plain number: 5/10 is not 5-59/10. if (($item !== '*') and (strpos($item, '-') === false)) return false; } if ($item === '*') continue; if (strpos($item, '-') !== false) { $parts = explode('-', $item); if (count($parts) != 2) return false; if (!preg_match('/^\d+$/', $parts[0]) or !preg_match('/^\d+$/', $parts[1])) return false; $from = intval($parts[0]); $to = intval($parts[1]); if (($from < $lo) or ($to > $hi) or ($from > $to)) return false; continue; } if (!preg_match('/^\d+$/', $item)) return false; $value = intval($item); if (($value < $lo) or ($value > $hi)) return false; } } return true; } // Is $command one we are willing to put in a crontab line? // // The command itself is not inspected: a scheduled job exists to run things, // and deciding which of them are wise is not this function's business. What it // does enforce is that the command stays on its own line and means in cron // what it means to the person who typed it. // // A newline would end the entry and start another, so a command could smuggle // in a schedule of its own. A bare '%' is where cron stops the command and // begins feeding the rest to it as standard input, which silently truncates // anything after it -- cron's own escape is '\%', which is accepted here. function cronCommandIsValid($command) { if (!is_string($command)) return false; $command = trim($command); if ($command === '') return false; if (preg_match('/[\r\n]/', $command)) return false; // Every % must be escaped. Count unescaped ones by removing the escaped // pairs first, which also stops a trailing backslash from hiding one. if (strpos(str_replace('\\%', '', str_replace('\\\\', '', $command)), '%') !== false) return false; // A lone trailing backslash escapes the space before our "# ZoneMinder" marker, which then becomes arguments. if (preg_match('/(? array('pipe', 'w'), 2 => array('pipe', 'w'), ); $pipes = array(); // C locale so the "no crontab for" message below is the one we match. $process = @proc_open('crontab -l', $descriptors, $pipes, null, array_merge(getenv(), array('LC_ALL'=>'C'))); if (!is_resource($process)) { \ZM\Error('Unable to run crontab to read the current crontab'); return false; } $out = stream_get_contents($pipes[1]); $err = stream_get_contents($pipes[2]); fclose($pipes[1]); fclose($pipes[2]); $status = proc_close($process); if ($status === 0) return rtrim($out, "\n"); // The normal answer for a user who has no crontab yet; BSD crontab prefixes "crontab: ". if (($status === 1) and preg_match('/^(?:crontab: )?no crontab for /', $err)) return ''; \ZM\Error('crontab -l failed (status '.$status.'), not changing the crontab: '.trim($err)); return false; } // Replace the web user's crontab with $content. Returns true on success. // // Written through a pipe rather than a shell redirect so the content is never // subject to quoting, and so an empty crontab can be installed without the // shell turning it into something else. function cronWrite($content) { $descriptors = array( 0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'), ); $pipes = array(); $process = @proc_open('crontab -', $descriptors, $pipes); if (!is_resource($process)) { \ZM\Error('Unable to run crontab to install scheduled actions'); return false; } fwrite($pipes[0], $content); fclose($pipes[0]); $out = stream_get_contents($pipes[1]); $err = stream_get_contents($pipes[2]); fclose($pipes[1]); fclose($pipes[2]); $status = proc_close($process); if ($status !== 0) { \ZM\Error('crontab refused the scheduled actions (status '.$status.'): '.trim($err.' '.$out)); return false; } return true; } // Rebuild the ZoneMinder block from everything in the database that cron runs: // scheduled monitor actions and cron jobs alike. // // Called whenever either is saved or deleted, so the crontab is a projection // of the tables rather than a second place to keep the truth. The whole block // is regenerated each time, which is why cronSpliceBlock has to leave // everything around it alone. // // Returns an array of the lines installed, or false if the crontab could not // be written. function cronRebuild() { require_once('MonitorAction.php'); require_once('CronJob.php'); $lines = array(); $skipped_remote = 0; foreach (ZM\CronJob::find(array('Enabled'=>1), array('order'=>'Id')) as $job) { if (!$job->belongsOnThisServer()) { $skipped_remote++; continue; } $line = $job->cronLine(); if ($line) $lines[] = $line; } foreach (ZM\MonitorAction::find(array('TriggerOn'=>'Schedule', 'Enabled'=>1), array('order'=>'Id')) as $action) { $line = $action->cronLine(); if (!$line) continue; // cron runs zmcontrol.pl here, on this machine, so an action driving a // monitor that belongs to another server would reach nothing. The web ui // proxies such a command through the API instead; cron has no such route, // so these are left out and counted rather than written and silently // ineffective. // A deleted or missing monitor on either end leaves nothing for the line to drive. $target = $action->TargetMonitor(); $source = $action->Monitor(); if (!$target or $target->Deleted() or !$source or $source->Deleted()) continue; if (defined('ZM_SERVER_ID') and ZM_SERVER_ID and $target->ServerId() and ($target->ServerId() != ZM_SERVER_ID)) { $skipped_remote++; continue; } $lines[] = $line; } if ($skipped_remote) { \ZM\Warning($skipped_remote.' scheduled item(s) belong to another server '. 'and were not written to this server\'s crontab'); } $existing = cronRead(); if ($existing === false) return false; if (!cronWrite(cronSpliceBlock($existing, $lines))) return false; \ZM\Debug('Installed '.count($lines).' scheduled item(s) into the crontab'); return $lines; } // The name this had when scheduled actions were the only thing in the block. function cronRebuildScheduledActions() { return cronRebuild(); } // --- Saying it in English --------------------------------------------------- // // A cron spec is the wrong thing to show an operator, so the editor offers // "Every at