TUTOS PHP Cross Reference Groupware Applications

Source: /php/admin/crontab_maker.php - 177 lines - 5459 bytes - Summary - Text - Print

Description: Copyright 2009 - 2013 by Gero Kohnert

   1  <?php
   2  /**
   3   *  Copyright 2009 - 2013 by Gero Kohnert
   4   *
   5   *  This program is free software; you can redistribute it and/or modify it
   6   *  under the terms of the GNU General Public License as published by the
   7   *  Free Software Foundation; version 2 of the License.
   8   *
   9   * create/fill crontab for the apache user (if allowed and possible)
  10   *
  11   * @modulegroup admin
  12   * @module crontab_maker
  13   * @package admin
  14   */
  15  $tutos['files'][__FILE__] = '$Rev: 930 $';
  16  
  17  $tutos['base'] = "../..";
  18  ini_set("include_path","..");
  19  include_once  'webelements.p3';
  20  include_once  'permission.p3';
  21  
  22  check_user();
  23  loadmodules('admin','customize');
  24  loadlayout();
  25  
  26  class CrontabMaker extends layout {
  27  
  28      /**
  29       * check if a command is available
  30       */
  31      function check_cmd ($c) {
  32          $r = '';
  33          $rt = '';
  34          $x = '';
  35          @exec('which '.$c,$x,$rt);
  36  
  37          if ($rt != 0) return $r;
  38  
  39          foreach ($x as $l) {
  40              $r .= $l;
  41          }
  42          return $r;
  43      }
  44  
  45      Function info() {
  46          global $tutos,$msg,$info;
  47  
  48          $r = '';
  49  
  50          if ($cmd = $this->check_cmd('php')) {
  51              $c = $cmd;
  52              $cmd = '*/5 * * * * cd '.dirname(dirname($_SERVER['SCRIPT_FILENAME']));
  53              $cmd .= '; '.$c .' check.php -dbnr '.$_SESSION['dbnr'];
  54          } elseif($cmd = $this->check_cmd('lynx')) {
  55              $cmd = '*/5 * * * * '.$cmd;
  56              $cmd .= ' -accept_all_cookies -dump '.getBaseUrl(true).'check.php?dbnr='.$_SESSION['dbnr'];
  57          } elseif($cmd = $this->check_cmd('wget')) {
  58              $cmd = '*/5 * * * * '.$cmd;
  59              $cmd .= ' -T 10 -O /dev/null --no-check-certificate '.getBaseUrl(true).'check.php?dbnr='.$_SESSION['dbnr'];
  60          } else {
  61              $r = '<div class="message">No suitable programm found (php/lynx/wget)</div>';
  62              return $r;
  63          }
  64  
  65          $cmd .= ' >> '.normalize_url(dirname($_SERVER['SCRIPT_FILENAME']).'/'.$tutos[errlog]);
  66          $cmd .= ' 2>&1';
  67  
  68          $r .= '<form method="POST">';
  69          $r .= $this->DataTableStart();
  70          $r .= "<tr>\n";
  71          $r .= " <th colspan=\"4\">Crontab</th>\n";
  72          $r .= "</tr>\n";
  73  
  74          $r .= '<tr>';
  75          $r .= '<td colspan="4">';
  76          $r .= 'In order to enable cyclic (every 5 minutes) checks you should add the following line to your crontab.<br />';
  77          $r .= '<b>'.$cmd.'</b>';
  78          $r .= '</td>';
  79          $r .= '</tr>';
  80  
  81  
  82          $rt = '';
  83          $x = array();
  84          @exec('crontab -l',$x,$rt);
  85          /**
  86             if ($rt != 0) {
  87             $r .= '<div class="message">Sorry ! Unable to find/run crontab on your system.('.$rt.')<br />';
  88             foreach ($x as $l) {
  89             $r .= $l."<br />";
  90             }
  91             $r .= '</div>';
  92             $r .= $this->DataTableEnd();
  93             return $r;
  94             }
  95          */
  96          $orig = '';
  97          $found = false;
  98          foreach ($x as $l) {
  99              if (empty($l)) continue;
 100              if (!$found && ($l == $cmd)) {
 101                  $r = '<div class="info">I already found a suitable entry.</div>'.$r;
 102                  $found = true;
 103              }
 104              $orig .= $l."\n";
 105          }
 106          if (!$found) {
 107              $r .= '<tr><td colspan="4">I have already added the line in the form below (please submit to enable).</td></tr>';
 108              $orig .= "#\n";
 109              $orig .= "# Call TUTOS check script every 5 minutes \n";
 110              $orig .= "# (automatically added on ".Date("d.M.Y H:i:s T").")\n";
 111              $orig .= $cmd."\n";
 112          }
 113  
 114  
 115          $r .= "<tr><td colspan=\"4\">\n";
 116          $r .= '<textarea rows="20" cols="'.(10+strlen($cmd)).'" name="crontab">';
 117          $r .= $orig;
 118          $r .= '</textarea>';
 119          $r .= "</td></tr>\n";
 120          $r .= '<tr><td colspan="4">comments start with an #.</br>Your are free to disable/remove other entries.<br />There will be <b>NO</b> backup.</td></tr>';
 121          $r .= "<tr>\n";
 122          $r .= submit_reset(0,1,1,1,1,0);
 123          $r .= "</tr>\n";
 124          $r .= $this->DataTableEnd();
 125          $r .= hiddenFormElements();
 126          $r .= $this->getHidden();
 127          $r .= $this->markBadFields();
 128          $r .= '</form>';
 129  
 130  
 131          return $r;
 132      }
 133  
 134      function prepare() {
 135          global $tutos,$msg,$info;
 136  
 137          $this->name = 'Crontab';
 138  
 139          if ( ! $this->user->isAdmin() ) {
 140              $msg = addLine($msg,$this->error("Only admins are allowed to see this"));
 141              $this->stop = true;
 142              return;
 143          }
 144          if (isset($_POST['crontab'])) {
 145              // install crontab
 146              $tmpname = tempnam ($tutos[sessionpath], "cron");
 147              $fd = file_put_contents($tmpname,preg_replace("#\r\n#m","\n",$_POST['crontab']));
 148              if ($fd === false) {
 149                  $msg = addLine($msg,$this->error('could not open/create '.$tmpname));
 150                  $this->stop = true;
 151                  return;
 152              }
 153              $info .= 'INSTALLED NEW CRONTAB '.$tmpname;
 154              $rt = '';
 155              $x = '';
 156              @exec('crontab '.$tmpname,$x,$rt);
 157              if ($rt != 0) {
 158                  $msg .= $this->error('could not run crontab with  '.$tmpname);
 159                  $this->stop = true;
 160                  return;
 161              }
 162              foreach ($x as $l) {
 163                  $info .= $l.'<br />';
 164              }
 165              unlink($tmpname);
 166          }
 167      }
 168  }
 169  
 170  $l = new CrontabMaker($current_user);
 171  $l->display();
 172  $dbconn->Close();
 173  ?>
 174  <!--
 175  SVN Info  $Id: product_show.php 908 2013-01-27 18:44:05Z gokohnert $
 176  $Author: gokohnert $
 177  -->

title

Description

title

Description

title

Description

title

title

Body