[ Index ]

PHP Cross Reference of Eventum

title

Body

[close]

/include/ -> class.validation.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 encoding=utf-8: */
   3  // +----------------------------------------------------------------------+
   4  // | Eventum - Issue Tracking System                                      |
   5  // +----------------------------------------------------------------------+
   6  // | Copyright (c) 2003, 2004, 2005, 2006, 2007 MySQL AB                  |
   7  // |                                                                      |
   8  // | This program is free software; you can redistribute it and/or modify |
   9  // | it under the terms of the GNU General Public License as published by |
  10  // | the Free Software Foundation; either version 2 of the License, or    |
  11  // | (at your option) any later version.                                  |
  12  // |                                                                      |
  13  // | This program is distributed in the hope that it will be useful,      |
  14  // | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
  15  // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
  16  // | GNU General Public License for more details.                         |
  17  // |                                                                      |
  18  // | You should have received a copy of the GNU General Public License    |
  19  // | along with this program; if not, write to:                           |
  20  // |                                                                      |
  21  // | Free Software Foundation, Inc.                                       |
  22  // | 59 Temple Place - Suite 330                                          |
  23  // | Boston, MA 02111-1307, USA.                                          |
  24  // +----------------------------------------------------------------------+
  25  // | Authors: João Prado Maia <jpm@mysql.com>                             |
  26  // +----------------------------------------------------------------------+
  27  //
  28  // @(#) $Id: class.validation.php 3246 2007-02-09 09:10:12Z glen $
  29  //
  30  
  31  
  32  /**
  33   * Class to handle form validation in the server-side, duplicating the
  34   * javascript based validation available in most forms, to make sure
  35   * the data integrity is the best possible.
  36   *
  37   * @version 1.0
  38   * @author João Prado Maia <jpm@mysql.com>
  39   */
  40  
  41  class Validation
  42  {
  43      /**
  44       * Method used to check whether a string is totally compromised of
  45       * whitespace characters, such as spaces, tabs or newlines.
  46       *
  47       * @access  public
  48       * @param   string $str The string to check against
  49       * @return  boolean
  50       */
  51      function isWhitespace($str)
  52      {
  53          $str = trim($str);
  54          if (strlen($str) == 0) {
  55              return true;
  56          } else {
  57              return false;
  58          }
  59      }
  60  
  61  
  62      /**
  63       * Method used to check whether an email address is a valid one.
  64       *
  65       * @access  public
  66       * @param   string $str The email address to check against
  67       * @return  boolean
  68       */
  69      function isEmail($str)
  70      {
  71          $valid_chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 
  72                                  'j', 'l', 'k', 'm', 'n', 'o', 'p', 'q', 'r',
  73                                  's', 't', 'u', 'w', 'v', 'x', 'y', 'z',
  74                                  '0', '1', '2', '3', '4', '5', '6', '7', 
  75                                  '8', '9');
  76          $extended_chars = array('.', '+', '_', '-', '@');
  77          $str = strtolower($str);
  78  
  79          // we need at least one @ symbol
  80          if (!strstr($str, '@')) {
  81              return false;
  82          }
  83          // and no more than one @ symbol
  84          if (strpos($str, '@') != strrpos($str, '@')) {
  85              return false;
  86          }
  87          // check for invalid characters in the email address
  88          for ($i = 0; $i < strlen($str); $i++) {
  89              if ((!in_array(substr($str, $i, 1), $valid_chars)) && 
  90                      (!in_array(substr($str, $i, 1), $extended_chars))) {
  91                  return false;
  92              }
  93          }
  94          // email addresses need at least one dot (but also allow for user@localhost addresses)
  95          if ((!strstr($str, '.')) && (substr($str, strrpos($str, '@')) != '@localhost')) {
  96              return false;
  97          }
  98          // no two dots alongside each other
  99          if (strstr($str, '..')) {
 100              return false;
 101          }
 102          // do an extra check for a dot as the last character of an address
 103          array_shift($extended_chars);
 104          if ((substr($str, strlen($str)-1) == '.') && 
 105                  (substr($str, strrpos($str, '@')) != '@localhost.')) {
 106              return false;
 107          }
 108          // the last character cannot be one of the extended ones
 109          if (in_array(substr($str, strlen($str)-1), $extended_chars)) {
 110              return false;
 111          }
 112          return true;
 113      }
 114  
 115  
 116      /**
 117       * Method used to check whether a string has only valid (ASCII) 
 118       * characters.
 119       *
 120       * @access  public
 121       * @param   string $str The string to check against
 122       * @return  boolean
 123       */
 124      function hasValidChars($str)
 125      {
 126          $valid_chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 
 127                                  'j', 'l', 'k', 'm', 'n', 'o', 'p', 'q', 'r',
 128                                  's', 't', 'u', 'w', 'v', 'x', 'y', 'z');
 129  
 130          for ($i = 0; $i < strlen($str); $i++) {
 131              if (!in_array(substr($str, $i, 1), $valid_chars)) {
 132                  return false;
 133              }
 134          }
 135          return true;
 136      }
 137  }
 138  
 139  // benchmarking the included file (aka setup time)
 140  if (APP_BENCHMARK) {
 141      $GLOBALS['bench']->setMarker('Included Validation Class');
 142  }


Generated: Wed Dec 19 21:21:33 2007 Cross-referenced by PHPXref 0.7