Example #1
0
    /**
     * validation of attributes value
     *
     * @param string $validation_type
     * @param string $attribute
     * @param string $value
     * @return boolean
     */
    public function validation($validation_type, $attribute, $value)
    {
        switch ($validation_type) {
            /* please dont' use boolean, it's not a good idea in PHP :) */
            case 'boolean':
                if (is_bool($value)) {
                    $this->setValid($attribute, true);
                    return true;
                } else {
                    $this->setValid($attribute, false);
                    return false;
                }
                break;
            case 'int':
            case 'decimal':
            case 'numeric':
                if (is_numeric($value)) {
                    $this->setValid($attribute, true);
                    return true;
                } else {
                    $this->setValid($attribute, false);
                    return false;
                }
                break;
            case 'string':
            case 'text':
            case 'serialized':
            case 'xml':
                $value = trim($value);
                if ($value != '') {
                    $this->setValid($attribute, true);
                    return true;
                } else {
                    if ($this->_metaData[$attribute]['required'] == true) {
                        $this->setValid($attribute, false);
                        /*
                        ($this->_metaData[$attribute]['label'] == '') ? $label = $attribute: $label = $this->_metaData[$attribute]['label'];
                        msg("$label is required","error", 0);
                        */
                        return false;
                    }
                }
            case 'xhtml':
                //don't do any validation if Tidy is not installed
                if (!function_exists('tidy_get_status')) {
                    return true;
                }
                //msg($_GET['request']);
                //msg($value);
                $tidy_content = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>test</title></head><body>' . $value . '</body></html>';
                // Specify configuration
                $config = array('show-warnings' => true, 'doctype' => 'transitional', 'indent' => true, 'output-xhtml' => true, 'wrap' => 200);
                // Tidy
                $tidy = new tidy();
                $tidy->parseString($tidy_content, $config, 'utf8');
                //$tidy->cleanRepair();
                //$tidy->diagnose();
                // get result
                $result_status = tidy_get_status($tidy);
                $result_message = tidy_get_error_buffer($tidy);
                if ($result_status > 1) {
                    $error = $result_message;
                } else {
                    if ($result_status > 0) {
                        msg("Tidy warning: {$result_message}", "error", 2);
                    }
                }
                if ($error != '') {
                    msg($error, 'error');
                    $this->setValid($attribute, false);
                    return false;
                } else {
                    $this->setValid($attribute, true);
                    return true;
                }
                break;
            case 'datetime':
                //$this->setValid($attribute, true);
                return true;
                break;
            case 'date':
                // ISO date
                $regex = "/^\\d{4}-\\d{1,2}-\\d{1,2}\$/";
                if (preg_match($regex, $value, $matches)) {
                    $this->setValid($attribute, true);
                    return true;
                } else {
                    $this->setValid($attribute, false);
                    return false;
                }
                break;
            case 'email':
                $regex = '/^([*+!.&#$|\'\\%\\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\\.)+[0-9a-z]{2,32})$/i';
                if (preg_match($regex, $value, $matches)) {
                    $this->setValid($attribute, true);
                    return true;
                } else {
                    msg(I18N_ERROR_ENTER_VALID_EMAIL, 'error');
                    $this->setValid($attribute, false);
                    return false;
                }
                break;
            case 'url':
                $regex = '/^(http:\\/\\/|ftp:\\/\\/)/i';
                if (preg_match($regex, $value, $matches)) {
                    $this->setValid($attribute, true);
                    return true;
                } else {
                    msg(I18N_ERROR_WRONG_URL, "error", 2);
                    $this->setValid($attribute, false);
                    return false;
                }
                break;
            case 'decimal':
                $this->setValid($attribute, true);
                return true;
                break;
            case 'product_code':
                /*
                 * be aware of "_", in SQL LIKE (escape it, or don't use it) 
                 */
                if (preg_match('/^[0-9a-zA-Z-]*$/', $pc)) {
                    $this->setValid($attribute, true);
                    return true;
                } else {
                    msg(I18N_ERROR_INVALID_PRODUCT_CODE, 'error', 2);
                    $this->setValid($attribute, false);
                    return false;
                }
                break;
            default:
                $this->setValid($attribute, true);
                return true;
                break;
        }
    }
<?php

$html = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title></title>
</head>
<body>
<p>paragraph</p>
</body>
</html>';
$tidy = tidy_parse_string($html);
echo tidy_get_status($tidy);
// status 0 indicates no errors or warnings
$html = '<p>paragraph</i>';
$tidy = tidy_parse_string($html);
echo tidy_get_status($tidy);
// status 1 indicates warnings
$html = '<bogus>test</bogus>';
$tidy = tidy_parse_string($html);
echo tidy_get_status($tidy);
// status 2 indicates error
Example #3
0
 /**
  * Use the HTML tidy PECL extension to use the tidy library in-process,
  * saving the overhead of spawning a new process. Currently written to
  * the PHP 4.3.x version of the extension, may not work on PHP 5.
  *
  * 'pear install tidy' should be able to compile the extension module.
  *
  * @private
  * @static
  */
 function internalTidy($text)
 {
     global $wgTidyConf;
     $fname = 'Parser::internalTidy';
     wfProfileIn($fname);
     tidy_load_config($wgTidyConf);
     tidy_set_encoding('utf8');
     tidy_parse_string($text);
     tidy_clean_repair();
     if (tidy_get_status() == 2) {
         // 2 is magic number for fatal error
         // http://www.php.net/manual/en/function.tidy-get-status.php
         $cleansource = null;
     } else {
         $cleansource = tidy_get_output();
     }
     wfProfileOut($fname);
     return $cleansource;
 }
Example #4
0
 /**
  * Use HTML Tidy to validate the $text
  * Only runs when $config['HTML_Tidy'] is off
  *
  * @param string $text The html content to be checked. Passed by reference
  */
 static function tidyFix(&$text, $ignore_config = false)
 {
     global $config;
     if (!$ignore_config) {
         if (empty($config['HTML_Tidy']) || $config['HTML_Tidy'] == 'off') {
             return true;
         }
     }
     if (!function_exists('tidy_parse_string')) {
         return false;
     }
     $options = array();
     $options['wrap'] = 0;
     //keeps tidy from wrapping... want the least amount of space changing as possible.. could get rid of spaces between words with the str_replaces below
     $options['doctype'] = 'omit';
     //omit, auto, strict, transitional, user
     $options['drop-empty-paras'] = true;
     //drop empty paragraphs
     $options['output-xhtml'] = true;
     //need this so that <br> will be <br/> .. etc
     $options['show-body-only'] = true;
     $options['hide-comments'] = false;
     //$options['anchor-as-name'] = true;		//default is true, but not alwasy availabel. When true, adds an id attribute to anchor; when false, removes the name attribute... poorly designed, but we need it to be true
     //
     //	php4
     //
     if (function_exists('tidy_setopt')) {
         $options['char-encoding'] = 'utf8';
         gp_edit::tidyOptions($options);
         $tidy = tidy_parse_string($text);
         tidy_clean_repair();
         if (tidy_get_status() === 2) {
             // 2 is magic number for fatal error
             // http://www.php.net/manual/en/function.tidy-get-status.php
             $tidyErrors[] = 'Tidy found serious XHTML errors: <br/>' . nl2br(htmlspecialchars(tidy_get_error_buffer($tidy)));
             return false;
         }
         $text = tidy_get_output();
         //
         //	php5
         //
     } else {
         $tidy = tidy_parse_string($text, $options, 'utf8');
         tidy_clean_repair($tidy);
         if (tidy_get_status($tidy) === 2) {
             // 2 is magic number for fatal error
             // http://www.php.net/manual/en/function.tidy-get-status.php
             $tidyErrors[] = 'Tidy found serious XHTML errors: <br/>' . nl2br(htmlspecialchars(tidy_get_error_buffer($tidy)));
             return false;
         }
         $text = tidy_get_output($tidy);
     }
     return true;
 }
Example #5
0
 /**
  * Use HTML Tidy to validate the $text
  * Only runs when $config['HTML_Tidy'] is off
  *
  * @param string $text The html content to be checked. Passed by reference
  */
 public static function tidyFix(&$text, $ignore_config = false)
 {
     global $config;
     if (!$ignore_config) {
         if (empty($config['HTML_Tidy']) || $config['HTML_Tidy'] == 'off') {
             return true;
         }
     }
     if (!function_exists('tidy_parse_string')) {
         return false;
     }
     $options = array();
     $options['wrap'] = 0;
     //keeps tidy from wrapping... want the least amount of space changing as possible.. could get rid of spaces between words with the str_replaces below
     $options['doctype'] = 'omit';
     //omit, auto, strict, transitional, user
     $options['drop-empty-paras'] = true;
     //drop empty paragraphs
     $options['output-xhtml'] = true;
     //need this so that <br> will be <br/> .. etc
     $options['show-body-only'] = true;
     $options['hide-comments'] = false;
     $tidy = tidy_parse_string($text, $options, 'utf8');
     tidy_clean_repair($tidy);
     if (tidy_get_status($tidy) === 2) {
         // 2 is magic number for fatal error
         // http://www.php.net/manual/en/function.tidy-get-status.php
         return false;
     }
     $text = tidy_get_output($tidy);
     return true;
 }