[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/felamimail/inc/ -> class.Script.inc.php (source)

   1  <?php
   2  /*
   3   * $Id: class.Script.inc.php 20048 2005-11-29 12:08:24Z lkneschke $
   4   *
   5   * Copyright 2002 Stephen Grier <stephengrier@users.sourceforge.net>
   6   *
   7   * See the inclosed smartsieve-NOTICE file for conditions of use and distribution.
   8   */
   9  
  10  class Script {
  11  
  12      var $name;         /* filename of script. */
  13      var $script;       /* full ascii text of script from server. */
  14      var $size;         /* size of script in bytes. */
  15      var $so;           /* boolean: is it safe to overwrite script?
  16                                              * only safe if we recognise encoding. */
  17      var $mode;         /* basic or advanced. Smartsieve can only read/write basic. */
  18      var $rules;        /* array of sieve rules. */
  19      var $vacation;     /* vacation settings. */
  20      var $pcount;       /* highest priority value in ruleset. */
  21      var $errstr;       /* error text. */
  22  
  23      // class constructor
  24  	function Script ($scriptname) {
  25  
  26      $this->name = $scriptname;
  27      $this->script = '';
  28                  $this->size = 0;
  29                  $this->so = true;
  30                  $this->mode = '';
  31      $this->rules = array();
  32      $this->vacation = array();
  33      $this->pcount = 0;
  34                  $this->errstr = '';
  35  
  36      }
  37      // end contructor
  38  
  39  
  40      // class functions
  41  
  42  
  43      // get sieve script rules for this user
  44  	function retrieveRules ($connection) {
  45          global $_SESSION;
  46          $continuebit = 1;
  47          $sizebit = 2;
  48          $anyofbit = 4;
  49          $keepbit = 8;
  50          $regexbit = 128;
  51   
  52          if (!isset($this->name)){
  53                  $this->errstr = 'retrieveRules: no script name specified';
  54                  return false;
  55          }
  56          if (!is_object($connection)) {
  57      $this->errstr = "retrieveRules: no sieve session open";
  58      return false;
  59          }
  60   
  61          // if script doesn't yet exist, nothing to retrieve. 
  62          // safe to write to this script file.
  63  #LK    if (!AppSession::scriptExists($this->name)) {
  64  #LK        $this->so = true;
  65  #LK        return true;
  66  #LK    }
  67   
  68          $script = $connection->getscript($this->name);
  69   
  70          if ($connection->errstr) {
  71      $this->errstr = 'retrieveRules: failed getting script: ' . $connection->errstr;
  72                  return false;
  73          }
  74   
  75          $lines = array();
  76          $lines = preg_split("/\n/",$script); //,PREG_SPLIT_NO_EMPTY);
  77   
  78          $rules = array();
  79          $vacation = array();
  80          $regexps = array('^ *##PSEUDO','^ *#rule','^ *#vacation','^ *#mode');
  81  
  82          /* first line should be the script size. eg: {123}. */
  83          $line = array_shift($lines);
  84          if (!preg_match("/^\{(\d+)\}$/", $line, $bits)){
  85                  $this->errstr = 'retrieveRules: unexpected value: ' . $line;
  86                  return false;
  87          }
  88          $this->size = $bits[1];
  89  
  90          /* next line should be the recognised encoded head. if not, the script 
  91           * is of an unrecognised format, and we should not overwrite it. */
  92          $line = array_shift($lines);
  93          if (!preg_match("/^# ?Mail(.*)rules for/", $line)){
  94                  $this->errstr = 'retrieveRules: encoding not recognised';
  95                  $this->so = false;
  96                  return false;
  97          }
  98          $this->so = true;
  99   
 100          $line = array_shift($lines);
 101          while (isset($line)){
 102                  foreach ($regexps as $regexp){
 103                          if (preg_match("/$regexp/i",$line)){
 104                                  $line = rtrim($line);
 105                                  if (preg_match(
 106                  "/^ *#rule&&(.*)&&(.*)&&(.*)&&(.*)&&(.*)&&(.*)&&(.*)&&(.*)&&(.*)&&(.*)&&(.*)$/i",                                $line,$bits)){
 107                                          $rule = array();
 108                                          $rule['priority'] = $bits[1];
 109                                          $rule['status'] = $bits[2];
 110                                          $rule['from'] = $bits[3];
 111                                          $rule['to'] = $bits[4];
 112                                          $rule['subject'] = $bits[5];
 113                                          $rule['action'] = $bits[6];
 114                                          $rule['action_arg'] = $bits[7];
 115                  // <crnl>s will be encoded as \\n. undo this.
 116                  $rule['action_arg'] = preg_replace("/\\\\n/","\r\n",$rule['action_arg']);
 117                                          $rule['flg'] = $bits[8];   // bitwise flag
 118                                          $rule['field'] = $bits[9];
 119                                          $rule['field_val'] = $bits[10];
 120                                          $rule['size'] = $bits[11];
 121                                          $rule['continue'] = ($bits[8] & $continuebit);
 122                                          $rule['gthan'] = ($bits[8] & $sizebit); // use 'greater than'
 123                                          $rule['anyof'] = ($bits[8] & $anyofbit);
 124                                          $rule['keep'] = ($bits[8] & $keepbit);
 125                                          $rule['regexp'] = ($bits[8] & $regexbit);
 126                                          $rule['unconditional'] = 0;
 127                                          if (!$rule['from'] && !$rule['to'] && !$rule['subject'] &&
 128                                                  !$rule['field'] && !$rule['size'] && $rule['action'])
 129                                                  $rule['unconditional'] = 1;
 130   
 131                                          array_push($rules,$rule);
 132                  if ($rule['priority'] > $this->pcount)
 133              $this->pcount = $rule['priority'];
 134                                  }
 135                                  if (preg_match("/^ *#vacation&&(.*)&&(.*)&&(.*)&&(.*)/i",$line,$bits)){
 136                                          $vacation['days'] = $bits[1];
 137                                          $vaddresslist = $bits[2];
 138                                          $vaddresslist = preg_replace("/\"|\s/","",$vaddresslist);
 139                                          $vaddresses = array();
 140                                          $vaddresses = preg_split("/,/",$vaddresslist);
 141                                          $vacation['text'] = $bits[3];
 142                  // <crnl>s will be encoded as \\n. undo this.
 143                  $vacation['text'] = preg_replace("/\\\\n/","\r\n",$vacation['text']);
 144                                          $vacation['status'] = $bits[4];
 145                                          $vacation['addresses'] = &$vaddresses;
 146                                  }
 147                                  if (preg_match("/^ *#mode&&(.*)/i",$line,$bits)){
 148                                          if ($bits[1] == 'basic')
 149                                                  $this->mode = 'basic';
 150                                          elseif ($bits[1] == 'advanced')
 151                                                  $this->mode = 'advanced';
 152                                          else
 153                                                  $this->mode = 'unknown';
 154                                  }
 155                          }
 156                  }
 157                  $line = array_shift($lines);
 158          }
 159   
 160          $this->script = $script;
 161          $this->rules = $rules;
 162          $this->vacation = $vacation;
 163   
 164          return true;
 165      }
 166   
 167   
 168      // update and save sieve script
 169  	function updateScript ($connection) 
 170      {
 171          global $_SESSION,$default,$sieve;
 172          
 173          $activerules = 0;
 174          $regexused = 0;
 175          $rejectused = 0;
 176          
 177          $username    = $GLOBALS['egw_info']['user'][account_lid];
 178          $version    = $GLOBALS['egw_info'][apps][felamimail][version];
 179          
 180          //include "$default->lib_dir/version.php";
 181   
 182          if (!is_object($connection)) 
 183          {
 184              $this->errstr = "updateScript: no sieve session open";
 185              return false;
 186          }
 187  
 188          // don't overwrite a file if not created by SmartSieve,
 189          // unless configured to do so.
 190  #LK        if (!$this->so && !$default->allow_write_unrecognised_scripts) {
 191  #LK            $this->errstr = 'updateScript: encoding not recognised: not safe to overwrite ' . $this->name;
 192  #LK            return false;
 193  #LK        }
 194   
 195          // lets generate the main body of the script from our rules
 196   
 197          $newscriptbody = "";
 198          $continue = 1;
 199  
 200          foreach ($this->rules as $rule){
 201              $newruletext = "";
 202   
 203              // don't print this rule if disabled.
 204              if ($rule['status'] != 'ENABLED') {
 205              }
 206              else {
 207   
 208                  $activerules = 1;
 209   
 210                  // conditions
 211   
 212                  $anyall = "allof";
 213                  if ($rule['anyof']) $anyall = "anyof";
 214                  if ($rule['regexp']) {
 215                          $regexused = 1;
 216                  }
 217                  $started = 0;
 218   
 219                  if (!$rule['unconditional']) {
 220                          if (!$continue) $newruletext .= "els";
 221                          $newruletext .= "if " . $anyall . " (";
 222                          if ($rule['from']) {
 223                                  if (preg_match("/^\s*!/", $rule['from'])){
 224                                          $newruletext .= 'not ';
 225                                          $rule['from'] = preg_replace("/^\s*!/","",$rule['from']);
 226                                  }
 227                                  $match = ':contains';
 228                                  if (preg_match("/\*|\?/", $rule['from'])) $match = ':matches';
 229                                  if ($rule['regexp']) $match = ':regex';
 230                                  $newruletext .= "address " . $match . " [\"From\"]";
 231                                  $newruletext .= " \"" . $rule['from'] . "\"";
 232                                  $started = 1;
 233                          }
 234                          if ($rule['to']) {
 235                                  if ($started) $newruletext .= ", ";
 236                                  if (preg_match("/^\s*!/", $rule['to'])){
 237                                          $newruletext .= 'not ';
 238                                          $rule['to'] = preg_replace("/^\s*!/","",$rule['to']);
 239                                  }
 240                                  $match = ':contains';
 241                                  if (preg_match("/\*|\?/", $rule['to'])) $match = ':matches';
 242                                  if ($rule['regexp']) $match = ':regex';
 243                                  $newruletext .= "address " . $match . " [\"To\",\"TO\",\"Cc\",\"CC\"]";
 244                                  $newruletext .= " \"" . $rule['to'] . "\"";
 245                                  $started = 1;
 246                          }
 247                          if ($rule['subject']) {
 248                                  if ($started) $newruletext .= ", ";
 249                                  if (preg_match("/^\s*!/", $rule['subject'])){
 250                                          $newruletext .= 'not ';
 251                                          $rule['subject'] = preg_replace("/^\s*!/","",$rule['subject']);
 252                                  }
 253                                  $match = ':contains';
 254                                  if (preg_match("/\*|\?/", $rule['subject'])) $match = ':matches';
 255                                  if ($rule['regexp']) $match = ':regex';
 256                                  $newruletext .= "header " . $match . " \"subject\"";
 257                                  $newruletext .= " \"" . $rule['subject'] . "\"";
 258                                  $started = 1;
 259                          }
 260                          if ($rule['field'] && $rule['field_val']) {
 261                                  if ($started) $newruletext .= ", ";
 262                                  if (preg_match("/^\s*!/", $rule['field_val'])){
 263                                          $newruletext .= 'not ';
 264                                          $rule['field_val'] = preg_replace("/^\s*!/","",$rule['field_val']);
 265                                  }
 266                                  $match = ':contains';
 267                                  if (preg_match("/\*|\?/", $rule['field_val'])) $match = ':matches';
 268                                  if ($rule['regexp']) $match = ':regex';
 269                                  $newruletext .= "header " . $match . " \"" . $rule['field'] . "\"";
 270                                  $newruletext .= " \"" . $rule['field_val'] . "\"";
 271                                  $started = 1;
 272                          }
 273                          if ($rule['size']) {
 274                                  $xthan = " :under ";
 275                                  if ($rule['gthan']) $xthan = " :over ";
 276                                  if ($started) $newruletext .= ", ";
 277                                  $newruletext .= "size " . $xthan . $rule['size'] . "K";
 278                                  $started = 1;
 279                          }
 280   
 281                  }
 282   
 283                  // actions
 284   
 285                  if (!$rule['unconditional']) $newruletext .= ") {\n\t";
 286  
 287                  if (preg_match("/folder/i",$rule['action'])) {
 288                          $newruletext .= "fileinto \"" . $rule['action_arg'] . "\";";
 289                  }
 290                  if (preg_match("/reject/i",$rule['action'])) {
 291                          $newruletext .= "reject text: \n" . $rule['action_arg'] . "\n.\n;";
 292                          $rejectused = 1;
 293                  }
 294                  if (preg_match("/address/i",$rule['action'])) {
 295                          $newruletext .= "redirect \"" . $rule['action_arg'] . "\";";
 296                  }
 297                  if (preg_match("/discard/i",$rule['action'])) {
 298                          $newruletext .= "discard;";
 299                  }
 300                  if ($rule['keep']) $newruletext .= "\n\tkeep;";
 301                  if (!$rule['unconditional']) $newruletext .= "\n}";
 302   
 303                  $continue = 0;
 304                  if ($rule['continue']) $continue = 1;
 305                  if ($rule['unconditional']) $continue = 1;
 306   
 307                  $newscriptbody .= $newruletext . "\n\n";
 308   
 309              } // end 'if ! ENABLED'
 310          }
 311  
 312          // vacation rule
 313   
 314          if ($this->vacation) {
 315                  $vacation = $this->vacation;
 316      if (!$vacation['days']) $vacation['days'] = $default->vacation_days;
 317      if (!$vacation['text']) $vacation['text'] = $default->vacation_text;
 318      if (!$vacation['status']) $vacation['status'] = 'on';
 319  
 320      // filter out invalid addresses.
 321      $ok_vaddrs = array();
 322      foreach($vacation['addresses'] as $addr){
 323              if ($addr != '' && preg_match("/\@/",$addr)) 
 324          array_push($ok_vaddrs,$addr);
 325      }
 326      $vacation['addresses'] = $ok_vaddrs;
 327  
 328      if (!$vacation['addresses'][0]){
 329                          $defaultaddr = $sieve->user . '@' . $sieve->maildomain;
 330              array_push($vacation['addresses'],$defaultaddr);
 331      }
 332                  if ($vacation['status'] == 'on') {
 333                          $newscriptbody .= "vacation :days " . $vacation['days'] . " :addresses [";
 334                          $first = 1;
 335                          foreach ($vacation['addresses'] as $vaddress) {
 336                                  if (!$first) $newscriptbody .= ", ";
 337                                  $newscriptbody .= "\"" . $vaddress . "\"";
 338                                  $first = 0;
 339                          }
 340                          $newscriptbody .= "] text:\n" . $vacation['text'] . "\n.\n;\n\n";
 341                  }
 342      // update with any changes.
 343      $this->vacation = $vacation;
 344          }
 345   
 346          // generate the script head
 347   
 348          $newscripthead = "";
 349          $newscripthead .= "#Mail filter rules for " . $username . "\n";
 350          $newscripthead .= '#Generated by ' . $username . ' using FeLaMiMail ' . $version . ' ' . date($default->script_date_format);
 351          $newscripthead .= "\n";
 352   
 353          if ($activerules) {
 354                  $newscripthead .= "require [\"fileinto\"";
 355                  if ($regexused) $newscripthead .= ",\"regex\"";
 356                  if ($rejectused) $newscripthead .= ",\"reject\"";
 357                  if ($this->vacation && $this->vacation['status'] == 'on')
 358                          $newscripthead .= ",\"vacation\"";
 359                  $newscripthead .= "];\n\n";
 360          }
 361          else {
 362      // no active rules, but might still have an active vacation rule
 363      if ($this->vacation && $this->vacation['status'] == 'on')
 364              $newscripthead .= "require [\"vacation\"];\n\n";
 365          }
 366      
 367   
 368          // generate the encoded script foot
 369   
 370          $newscriptfoot = "";
 371          $pcount = 1;
 372          $newscriptfoot .= "##PSEUDO script start\n";
 373          foreach ($this->rules as $rule){
 374              // only add rule to foot if status != deleted. this is how we delete a rule.
 375              if ($rule['status'] != 'DELETED') {
 376      // we need to handle \r\n here.
 377      $rule['action_arg'] = preg_replace("/\r\n/","\\n",$rule['action_arg']);
 378      /* reset priority value. note: we only do this
 379       * for compatibility with Websieve. */
 380      $rule['priority'] = $pcount;
 381                  $newscriptfoot .= "#rule&&" . $rule['priority'] . "&&" . $rule['status'] . "&&" . $rule['from'] . "&&" . $rule['to'] . "&&" . $rule['subject'] . "&&" . $rule['action'] .
 382  "&&" . $rule['action_arg'] . "&&" . $rule['flg'] . "&&" . $rule['field'] . "&&" . $rule['field_val'] . "&&" . $rule['size'] . "\n";
 383      $pcount = $pcount+2;
 384              }
 385          }
 386          if ($this->vacation) {
 387                  $vacation = $this->vacation;
 388                  $newscriptfoot .= "#vacation&&" . $vacation['days'] . "&&";
 389                  $first = 1;
 390                  foreach ($vacation['addresses'] as $address) {
 391                          if (!$first) $newscriptfoot .= ", ";
 392                          $newscriptfoot .= "\"" . $address . "\"";
 393                          $first = 0;
 394                  }
 395      $vacation['text'] = preg_replace("/\r\n/","\\n",$vacation['text']);
 396                  $newscriptfoot .= "&&" . $vacation['text'] . "&&" . $vacation['status'] . "\n";
 397          }
 398          $newscriptfoot .= "#mode&&basic\n";
 399   
 400          $newscript = $newscripthead . $newscriptbody . $newscriptfoot;
 401          $this->script = $newscript;
 402  #print "<pre>$newscript</pre>";
 403          $scriptfile = $this->name;
 404          if (!$connection->putscript($scriptfile,$newscript))
 405          {
 406      $this->errstr = 'updateScript: putscript failed: ' . $connection->errstr;
 407                  return false;
 408          }
 409  
 410  #LK    if (!AppSession::isActiveScript($this->name) &&
 411  #LK        ($default->update_activate_script || 
 412  #LK         !$default->allow_multi_scripts ||
 413  #LK         AppSession::getNumScripts() == 0)) {
 414  #LK    if (!$connection->activatescript($this->name)) {
 415  #LK        $this->errstr = 'updateScript: activatescript failed: ' . $connection->errstr;
 416  #LK        return false;
 417  #LK    }
 418  #LK    }
 419  
 420          /* prompt AppSession object to update its script list and active 
 421           * script data in the light of any changes we may have made. */
 422  #LK    if (!AppSession::doListScripts()) {
 423  #LK        $this->errstr = 'updateScript: AppSession::doListScripts failed: ' . AppSession::getError();
 424  #LK        return false;
 425  #LK    }
 426   
 427          return true;
 428      }
 429  
 430  
 431  }
 432  
 433  
 434  ?>


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7