Changeset 807


Ignore:
Timestamp:
Feb 24, 2024 6:31:54 AM (2 months ago)
Author:
anonymous
Message:

Minor improvements. Add Validator::IPAddress() method.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/bin/module_maker/module.cli.php

    r612 r807  
    207207        $default = $col[4];
    208208
    209         if (mb_strpos($default, '0000') !== false || '0' == $default) {
     209        if (is_null($default) || mb_strpos($default, '0000') !== false || '0' == $default) {
    210210            $default = '';
    211211        }
  • trunk/bin/module_maker/validation.cli.php

    r655 r807  
    292292            }
    293293            $negative_ok = $unsigned ? 'false' : 'true';
    294             $o[] = "\$fv->isDecimal('$field', $max_dig, $max_dec, false, sprintf(_(\"%s must be a number with a maximum of %d integer digits and %d fractional digits, e.g., {EX}.\"), _(\"$title\"), ${max_dig}-${max_dec}, $max_dec), MSG_ERR, __FILE__, __LINE__);";
     294            $o[] = "\$fv->isDecimal('$field', $max_dig, $max_dec, false, sprintf(_(\"%s must be a number with a maximum of %d integer digits and %d fractional digits, e.g., {EX}.\"), _(\"$title\"), {$max_dig}-{$max_dec}, $max_dec), MSG_ERR, __FILE__, __LINE__);";
    295295            break;
    296296
  • trunk/lib/FormValidator.inc.php

    r722 r807  
    694694    public function fileUploadSize($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null)
    695695    {
    696         if (Validator::fileUploadSize($form_name, LOG_NOTICE, $file, $line)) {
     696        if (Validator::fileUploadSize(LOG_NOTICE, $file, $line)) {
    697697            return true;
    698698        } else {
  • trunk/lib/Utilities.inc.php

    r806 r807  
    2525 */
    2626
     27require_once dirname(__FILE__) . '/App.inc.php';
    2728
    2829/**
     
    186187    $app =& App::getInstance();
    187188
     189    $indent = trim($indent, ' ') . ' ';
     190
    188191    $indent_str = str_repeat($indent, $depth);
    189192    $output = '';
    190193    if (is_array($var)) {
    191194        foreach ($var as $k=>$v) {
    192             $k = ucfirst(mb_strtolower(str_replace(array('_', '  '), ' ', $k)));
     195            $k = ucfirst(mb_strtolower(str_replace(['_', '  '], ' ', $k)));
    193196            if (is_array($v)) {
    194197                $output .= sprintf("\n%s%s:\n%s\n", $indent_str, $k, fancyDump($v, $indent, $depth+1));
     
    200203        $output .= sprintf("%s%s\n", $indent_str, $var);
    201204    }
    202     return preg_replace(['/^[ \t]+$/' . $app->getParam('preg_u'), '/\n\n+/' . $app->getParam('preg_u'), '/^(?:\S( ))?(?:\S( ))?(?:\S( ))?(?:\S( ))?(?:\S( ))?(?:\S( ))?(?:\S( ))?(?:\S( ))?(\S )/m' . $app->getParam('preg_u')], ['', "\n", '$1$1$2$2$3$3$4$4$5$5$6$6$7$7$8$8$9'], $output);
     205
     206    return preg_replace([
     207        '/^[ \t]+$/' . $app->getParam('preg_u'),
     208        '/\n\n+/' . $app->getParam('preg_u'),
     209        sprintf('/^(?:%1$s( ))?(?:%1$s( ))?(?:%1$s( ))?(?:%1$s( ))?(?:%1$s( ))?(?:%1$s( ))?(?:%1$s( ))?(?:%1$s( ))?(%1$s )/m%2$s', preg_quote(trim($indent, ' '), '/'), $app->getParam('preg_u')),
     210    ], [
     211        '',
     212        "\n",
     213        '$1$1$2$2$3$3$4$4$5$5$6$6$7$7$8$8$9'
     214    ], $output);
    203215}
    204216
     
    15161528
    15171529    $addr = canonicalIPAddr(trim($_SERVER['REMOTE_ADDR']));
    1518     return $dolookup && $addr ? gethostbyaddr($addr) : $addr;
     1530    if (false !== filter_var($addr, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 | FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
     1531        return $dolookup && '' != $addr ? gethostbyaddr($addr) : $addr;
     1532    }
     1533
     1534    return '';
    15191535}
    15201536
  • trunk/lib/Validator.inc.php

    r799 r807  
    520520    *
    521521    * @param  string $form_name The input data to validate.
    522     * @param  const  $type  A LOG_* constant (see App->logMsg())
    523     * @param  string $file  Filename to log (usually __FILE__)
    524     * @param  int    $line  Line number to log (usually __LINE__)
    525     * @return bool   True if no errors found, false otherwise.
     522    * @param  const  $type      A LOG_* constant (see App->logMsg())
     523    * @param  string $file      Filename to log (usually __FILE__)
     524    * @param  int    $line      Line number to log (usually __LINE__)
     525    * @return bool              True if no errors found, false otherwise.
    526526    */
    527527    static public function fileUploaded($form_name, $type=LOG_DEBUG, $file=null, $line=null)
     
    555555    *
    556556    * @access   public
    557     * @param    string $form_name The input data to validate.
    558     * @param  const  $type  A LOG_* constant (see App->logMsg())
    559     * @param  string $file  Filename to log (usually __FILE__)
    560     * @param  int    $line  Line number to log (usually __LINE__)
    561     * @return   bool   True if no errors found, false otherwise.
     557    * @param    const  $type  A LOG_* constant (see App->logMsg())
     558    * @param    string $file  Filename to log (usually __FILE__)
     559    * @param    int    $line  Line number to log (usually __LINE__)
     560    * @return   bool          True if no errors found, false otherwise.
    562561    * @author   Quinn Comendant <quinn@strangecode.com>
    563562    * @version  1.0
    564563    * @since    20 Aug 2014 14:44:23
    565564    */
    566     static public function fileUploadSize($form_name, $type=LOG_DEBUG, $file=null, $line=null)
     565    static public function fileUploadSize($type=LOG_DEBUG, $file=null, $line=null)
    567566    {
    568567        $app =& App::getInstance();
     
    575574    }
    576575
     576    /*
     577    *
     578    *
     579    * @access   public
     580    * @param
     581    * @return
     582    * @author   Quinn Comendant <quinn@strangecode.com>
     583    * @since    09 Feb 2024 21:03:57
     584    */
     585    public static function IPAddress($val, $flags=null, $type=LOG_DEBUG, $file=null, $line=null)
     586    {
     587        $app =& App::getInstance();
     588        if (is_string($val) && '' === trim($val)) {
     589            // Don't be too bothered about empty strings.
     590            return true;
     591        }
     592
     593        if (!filter_var($val, FILTER_VALIDATE_IP, $flags)) {
     594            $app->logMsg(sprintf('%s (line %s) failed: %s', __METHOD__, __LINE__, getDump($val)), $type, $file, $line);
     595            return false;
     596        } else {
     597            return true;
     598        }
     599    }
     600
    577601} // THE END
    578602
Note: See TracChangeset for help on using the changeset viewer.