cvs: php4 /pear/Schedule At.php

From: Date: Tue, 09 Jan 2001 19:29:40 +0000
Subject: cvs: php4 /pear/Schedule At.php
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-3520@lists.php.net to get a copy of this message
cmv Tue Jan 9 11:29:40 2001 EDT Added files: /php4/pear/Schedule At.php Log: Schedule_AT class: interface to the Unix "at" command PR: Submitted by: Reviewed by: Obtained from: Index: php4/pear/Schedule/At.php +++ php4/pear/Schedule/At.php <?php /* vim: set ts=4 sw=4: */ // +----------------------------------------------------------------------+ // | PHP version 4.0 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2001 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Colin Viebrock <colin@easydns.com> | // +----------------------------------------------------------------------+ // // $Id: At.php,v 1.1 2001/01/09 19:29:39 cmv Exp $ // // Interface to the UNIX "at" program /** * Class to interface to the UNIX "at" program * * @author Colin Viebrock <colin@easydns.com> */ class Schedule_At { var $AT_PROG = '/usr/bin/at'; var $error = false; var $runtime = false; var $job = false; var $lastexec = ''; /** * Constructor: instantiates the class. * */ function Schedule_At() { $this->_reset(); } /** * Adds an at command * This makes an "at" job, where $cmd is the shell command to run * and $timespec describes when the function should run. See the * at man page for a description of the spec. * * $queue is an optional 1 character string [a-zA-Z] that can define * which queue to put the job in. * * If $mail is set, then an email will be sent when the job runs, * even if the job doesn't output anything. The mail gets sent to * the user running the script (probably the webserver, i.e. * nobody@localhost). * * The add() method returns false on error (in which case, check * $at->error for the message), or the job number on success. * On succes, $at->runtime is also set with the timestamp corresponding * to when the job will run. * * @param $cmd shell command * @param $timespec time when command should run, formatted accoring to the spec for at * @param $queue optional at queue specifier * @param $mail optional flag to specify whether to send email */ function add($cmd, $timespec, $queue = false, $mail = false ) { echo "***"; $this->_reset(); if ($queue && !preg_match('/^[a-zA-Z]{1,1}$/', $queue) ) { $this->error = "Invalid queue specification"; return false; } $cmd = escapeShellCmd($cmd); $exec = sprintf("echo \"%s\" | %s %s %s %s 2>&1", addslashes($cmd), $this->AT_PROG, ($queue ? '-q '.$queue : ''), ($mail ? '-m' : ''), $timespec ); $result = $this->_doexec($exec); if (preg_match('/garbled time/i', $result) ) { $this->error = "Garbled time"; return false; } if (preg_match('/job (\d+) at (.*)/i', $result, $m) ) { $this->runtime = $this->_parsedate($m[2]); $this->job = $m[1]; return $this->job; } else { $this->error = $result; return false; } } /** * Shows commands in the at queue * * This returns an array listing all queued jobs. The array's keys * are the job numbers, and each entry is itself an associative array * listing the runtime (timestamp) and queue (char). * * You can optionally provide a queue character to only list the jobs * in that queue. * * @param $queue optional queue specifier */ function show($queue = false) { $this->_reset(); if ($queue && !preg_match('/^[a-zA-Z]{1,1}$/', $queue) ) { $this->error = "Invalid queue specification"; return false; } $exec = sprintf("%s -l %s", $this->AT_PROG, ($queue ? '-q '.$queue : '') ); $result = $this->_doexec($exec); $lines = explode("\n", $result); $return = array(); foreach($lines as $line) { if (trim($line)) { list($job, $day, $time, $queue) = preg_split('/\s+/', trim($line) ); $return[$job] = array( 'runtime' => $this->_parsedate($day.' '.$time), 'queue' => $queue ); } } return $return; } /** * Remove job from the at queue * * This removes jobs from the queue. Returns false if the job doesn't * exist or on failure, or true on success. * * @param $job job to remove */ function remove($job = false) { $this->_reset(); if (!$job) { $this->error = "No job specified"; return false; } $queue = $this->show(); if (!isset($queue[$job]) ) { $this->error = "Job " . $job . " doesn't exist"; return false; } $exec = sprintf("%s -d %s", $this->AT_PROG, $job ); $this->_doexec($exec); /* this is required since the shell command doesn't return anything on success */ $queue = $this->show(); return !isset($queue[$job]); } /** * PRIVATE: Reset class * */ function _reset() { $this->error = false; $this->runtime = false; $this->job = false; $this->lastexec = ''; } /** * PRIVATE: Parse date string returned from shell command * * @param $str date string to parse */ function _parsedate($str) { if (preg_match('/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})/i', $str, $m) ) { return mktime($m[4], $m[5], 0, $m[2], $m[3], $m[1]); } else { return false; } } /** * PRIVATE: Run a shell command * * @param $cmd command to run */ function _doexec($cmd) { $this->lastexec = $cmd; return $cmd; } } ?>

« previous php.cvs (#3520) next »