Location: PHPKode > projects > Webgenerator-X content management system > wgx_rc1.5/WG-X/class.Validator.php
<?
/*
* This file is part of Webgenerator-X,
* an object oriented website management engine working an top of
* Apache/PHP4/MySQL.
* http://www.webgenerator-x.com
* @2001 REGNI Giorgio
* hide@address.com
*
* Webgenerator-X is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Webgenerator-X is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*************************************************************************/
/*
	WG-X Validator

	A class for validating common data from forms
	@2001 REGNI Giorgio
*/

class A_Validator
{
	
  function A_Validator ()
    {
      return;
    }
	
  // strip slashes,remove spaces at start and end
  function clean($string)
    {
      return	stripslashes( trim( $string ));
    }
	
  // trim,stripslashes(=clean) and then addslashes
  function make_dbrecord( $string)
    {
      return addslashes( $this->clean( $string) );
    }

  // transform any http:// in a text to <a href=...
  function urltolink( $text )
    {
      return eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])",
			   "<A HREF=\"\\1://\\2\\3\" TARGET=\"_blank\">\\1://\\2\\3</A>",$text);
    }
	
  // all text between [] will be bold
  function makebold( $text )
    {
      return eregi_replace("\[([^]]*)\]", "<b>\\1</b>", $text);
    }
	
  // this function checks $login for only lettres,numbers,at least 4 letters and less than 20 plus start by a letter
  function is_login($login)
    {
      return ereg("^[a-zA-Z][a-zA-Z0-9]{3,19}", $login);
    }
	
  // return true if valid email adress
  function is_email ($address) { 
    return (ereg("^[-!#$%&'*+\\./0-9=?A-Z^_`a-z{|}~]+". 
		 '@'. 
		 "[-!#$%&'*+\\/0-9=?A-Z^_`a-z{|}~]+\.". 
		 "[-!#$%&'*+\\./0-9=?A-Z^_`a-z{|}~]+$", 
		 $address)); 
  }
        
  //  retourne true if there's only alphanumerics caracters
  function is_alpha( $chaine ) {
    return (ereg("^[A-Za-z0-9]+$" , $chaine ));
  }


}	// End class

$VALIDATOR = new A_Validator();

// validation utility
function checks_email( $email )
{
  global $VALIDATOR;
	
  eval( TRANSLATE("BadEmail", "\$stringBadEmail" ));

  if ($VALIDATOR->is_email( $email))
    return "1";
  else
    return $stringBadEmail;
}

function checks_login( $login )
{
  global $VALIDATOR;
	
  eval( TRANSLATE("BadLogin", "\$stringBadLogin" ));
	
  if ($VALIDATOR->is_login( $login))
    return "1";
  else
    return $stringBadLogin;
}

function checks_passwords( $pass1,$pass2 )
{
  global $VALIDATOR;
	
  eval( TRANSLATE("BadPassword", "\$stringBadPassword" ));
  eval( TRANSLATE("BadPasswordDifferent", "\$stringBadPasswordDifferent" ));
	
  if ($VALIDATOR->is_login( $pass1))
    {
      if ($pass1==$pass2)
	return "1";
      else
	return $stringBadPasswordDifferent;
    }
  else
    return $stringBadPassword;
}

// giving a text variable (output from database) and format as char, this function will return the text to display
// example: format = T (text): nl2(br), underlines urls etc...
// example: format = H (html): strip some unwanted tags
// used by the forum
function Format_Text( $intext, $format = 'T')
{
  global $VALIDATOR;
	
  if ($format == 'T' or $format == 't')
    {
      $outtext = $VALIDATOR->makebold( $VALIDATOR->urltolink ( nl2br ( strip_tags( $intext))));
    }
  else
    {
      $outtext = $intext;
    }
	
  return $outtext;
}

/*
Ex: dir1 = /a/b/c
    dir2 = a.php
	return /a/b/c/a.php
	
	always uses / as separator because it works under windows and unix
*/
function concat_dirs( $dir1,$dir2)
{
  $trailer = substr($dir1,-1);
  $header  = substr($dir2,0,1);

  if ($header=='/' or $header=="\\")
    {
      $dir2 = substr( $dir2,1,strlen($dir2)-1);
    }
	
  if ($trailer=='/' or $trailer=="\\")
    {
      $dir1 = substr( $dir1,0,strlen($dir1)-1);
    }
	
  return $dir1.'/'.$dir2;
}

function MyAddSlashes($in ) {
  return( get_magic_quotes_gpc() == 1 ? 
          $in : 
          AddSlashes($in) );
}

function MyStripSlashes($in) {
  return( get_magic_quotes_gpc() == 1 ? 
          StripSlashes($in) : 
          $in );
}

/*======================================================================*\
    Function: parse_attrs
    Purpose:  Parse attribute string like id="" size=""
\*======================================================================*/
    function parse_attrs($tag_args)
    {
        /* Tokenize tag attributes. */
        preg_match_all('/(?:"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"       |
                          \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'     | (?>[^"\'=\s]+)
                         )+ |
                         [=]
                        /x', $tag_args, $match);
        $tokens       = $match[0];
        //$var_delims   = array('$', '#', '%');

        $attrs = array();
        /* Parse state:
            0 - expecting attribute name
            1 - expecting '='
            2 - expecting attribute value (not '=') */
        $state = 0;

        foreach ($tokens as $token) {
            switch ($state) {
                case 0:
                    /* If the token is a valid identifier, we set attribute name
                       and go to state 1. */
                    if (preg_match('!^\w+$!', $token)) {
                        $attr_name = $token;
                        $state = 1;
                    } /*else
                        $this->_syntax_error("invalid attribute name - '$token'");*/
                    break;

                case 1:
                    /* If the token is '=', then we go to state 2. */
                    if ($token == '=') {
                        $state = 2;
                    } /*else
                        $this->_syntax_error("expecting '=' after attribute name");*/
                    break;

                case 2:
                    /* If token is not '=', we set the attribute value and go to
                       state 0. */
                    if ($token != '=') {
                        /* We booleanize the token if it's a non-quoted possible
                           boolean value. */
                        if (preg_match('!^(on|yes|true)$!', $token))
                            $token = true;
                        else if (preg_match('!^(off|no|false)$!', $token))
                            $token = false;
                        /* If the token starts with ' or " then it's taken as a "" or '' string and the quotes or single quotes are taken off */
                        else if (($token{0} == '"' || $token[0] == "'") &&
                                 $token{strlen($token)-1} == $token{0})
			  $token = substr($token,1,strlen($token)-2);

                        $attrs[$attr_name] = $token;
                        $state = 0;
                    } /*else
                        $this->_syntax_error("'=' cannot be an attribute value");*/
                    break;
            }
        }

        return $attrs;
    }


/**
* Render simple smarty tags in the text passed as in.
* Mini Smarty parser that for the moment is useful for iimages tag {image }
*/
function render_text($in)
{
  global $IImage;

  $ldq = preg_quote('{', '!');
  $rdq = preg_quote('}', '!');

  /* Gather all image tags. */
  preg_match_all("!{$ldq}image\s*(.*?)\s*{$rdq}!s", $in, $match);
  $template_tags = $match[1];
  unset ($match);
  /* Split content by template tags to obtain non-template content. */
  $text_blocks = preg_split("!{$ldq}image.*?{$rdq}!s", $in);
  

  echo "<pre>";
  print_r($template_tags);
  print_r($text_blocks);
  echo "</pre>";
  // now look at the tags
  $qstr_regexp = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'';
  $rendered_tags = array();
  foreach( $template_tags as $template_tag)
    {
      /* Split tag into two parts: command and the arguments. */
      preg_match('/^(
              (?: ' . $qstr_regexp . ' | (?>[^"\'\s]+))+
              )
                      (?:\s+(.*))?
              /xs', "image $template_tag", $match);

      $tag_command = $match[1];
      $tag_args = parse_attrs(isset($match[2]) ? $match[2] : '');

      unset ($match);
      $rendered_tags[]= $IImage->Generate_HTML($tag_args);
    }

  $out = '';

  /* Interleave the generated contents and text blocks to get the final result. */
  for ($i = 0; $i < count($rendered_tags); $i++) {
    $out .= $text_blocks[$i].$rendered_tags[$i];
  }
  $out .= $text_blocks[$i];

  return $out;
}

?>
Return current item: Webgenerator-X content management system