16 define(
'SCHEDULED_TASK_MESSAGE_TYPE_COMPLETED', 
'common.completed');
 
   17 define(
'SCHEDULED_TASK_MESSAGE_TYPE_ERROR', 
'common.error');
 
   18 define(
'SCHEDULED_TASK_MESSAGE_TYPE_WARNING', 
'common.warning');
 
   19 define(
'SCHEDULED_TASK_MESSAGE_TYPE_NOTICE', 
'common.notice');
 
   20 define(
'SCHEDULED_TASK_EXECUTION_LOG_DIR', 
'scheduledTaskLogs');
 
   36    function __construct($email = 
'', $contactName = 
'') {
 
   37       if (!$email || !$contactName) {
 
   39          $site = $siteDao->getSite(); 
 
   40          $email = $site->getLocalizedContactEmail();
 
   41          $contactName = $site->getLocalizedContactName();
 
   44       $this->_contactEmail = $email;
 
   45       $this->_contactName = $contactName;
 
   55       import(
'lib.pkp.classes.mail.Mail');
 
   68       while(($arg = $task->getChildByName(
'arg', $index)) != 
null) {
 
   69          array_push($args, $arg->getValue());
 
   86       $lastRunTime = $taskDao->getLastRunTime($className);
 
   89       $dayOfWeek = $frequency->getAttribute(
'dayofweek');
 
   90       if (isset($dayOfWeek)) {
 
   91          $isValid = self::_isInRange($dayOfWeek, (
int)date(
'w'), $lastRunTime, 
'day', strtotime(
'-1 week'), strtotime(
'-1 day'));
 
   96          $month = $frequency->getAttribute(
'month');
 
   98             $isValid = self::_isInRange($month, (
int)date(
'n'), $lastRunTime, 
'month', strtotime(
'-1 year'), strtotime(
'-1 month'));
 
  104          $day = $frequency->getAttribute(
'day');
 
  106             $isValid = self::_isInRange($day, (
int)date(
'j'), $lastRunTime, 
'day', strtotime(
'-1 month'), strtotime(
'-1 day'));
 
  112          $hour = $frequency->getAttribute(
'hour');
 
  114             $isValid = self::_isInRange($hour, (
int)date(
'G'), $lastRunTime, 
'hour', strtotime(
'-1 day'), strtotime(
'-1 hour'));
 
  120          $minute = $frequency->getAttribute(
'minute');
 
  121          if (isset($minute)) {
 
  122             $isValid = self::_isInRange($minute, (
int)date(
'i'), $lastRunTime, 
'min', strtotime(
'-1 hour'), strtotime(
'-1 minute'));
 
  139       $reportErrorOnly = 
Config::getVar(
'general', 
'scheduled_tasks_report_error_only', 
true);
 
  141       if (!$result || !$reportErrorOnly) {
 
  142          $message = $this->
getMessage($executionLogFile);
 
  146             $type = SCHEDULED_TASK_MESSAGE_TYPE_COMPLETED;
 
  149             $type = SCHEDULED_TASK_MESSAGE_TYPE_ERROR;
 
  152          $subject = $name . 
' - ' . $id . 
' - ' . __($type);
 
  153          return $this->_sendEmail($message, $subject);
 
  165       if (!$executionLogFile) {
 
  166          return __(
'admin.scheduledTask.noLog');
 
  170       $router = $request->getRouter();
 
  171       $downloadLogUrl = $router->url($request, 
'index', 
'admin', 
'downloadScheduledTaskLogFile', 
null, array(
'file' => basename($executionLogFile)));
 
  172       return __(
'admin.scheduledTask.downloadLog', array(
 
  173          'url' => $downloadLogUrl,
 
  185       import(
'lib.pkp.classes.file.PrivateFileManager');
 
  188       $fileMgr->rmtree($fileMgr->getBasePath() . DIRECTORY_SEPARATOR . SCHEDULED_TASK_EXECUTION_LOG_DIR);   
 
  196       import(
'lib.pkp.classes.file.PrivateFileManager');
 
  199       $fileMgr->downloadByPath($fileMgr->getBasePath() . DIRECTORY_SEPARATOR . SCHEDULED_TASK_EXECUTION_LOG_DIR . DIRECTORY_SEPARATOR . $file); 
 
  212    private function _sendEmail($message, $subject) {
 
  214       $mail->addRecipient($this->_contactEmail, $this->_contactName);
 
  215       $mail->setSubject($subject);
 
  216       $mail->setBody($message);
 
  218       return $mail->send();
 
  231    private static function _isInRange($rangeStr, $currentValue, $lastTimestamp, $timeCompareStr, $passTimestamp, $blockTimestamp) {
 
  233       $rangeArray = explode(
',', $rangeStr);
 
  236       if ($lastTimestamp > $blockTimestamp) {
 
  241       if ($passTimestamp > $lastTimestamp) {
 
  245       for ($i = 0, $count = 
count($rangeArray); !$isValid && ($i < $count); $i++) {
 
  246          if ($rangeArray[$i] == 
'*') {
 
  250          } 
if (is_numeric($rangeArray[$i])) {
 
  252             $isValid = ($currentValue == (int)$rangeArray[$i]);
 
  254          } 
else if (preg_match(
'/^(\d*)\-(\d*)$/', $rangeArray[$i], $matches)) {
 
  256             $isValid = self::_isInNumericRange($currentValue, (
int)$matches[1], (
int)$matches[2]);
 
  258          } 
else if (preg_match(
'/^(.+)\/(\d+)$/', $rangeArray[$i], $matches)) {
 
  260             $skipRangeStr = $matches[1];
 
  261             $skipFactor = (int)$matches[2];
 
  263             if ($skipRangeStr == 
'*') {
 
  266             } 
else if (preg_match(
'/^(\d*)\-(\d*)$/', $skipRangeStr, $matches)) {
 
  267                $isValid = self::_isInNumericRange($currentValue, (
int)$matches[1], (
int)$matches[2]);
 
  272                $isValid = (strtotime(
"-$skipFactor $timeCompareStr") > $lastTimestamp);
 
  287    private static function _isInNumericRange($value, $min, $max) {
 
  288       return ($value >= $min && $value <= $max);