Changeset 722 for trunk/lib


Ignore:
Timestamp:
May 3, 2020 9:58:17 PM (4 years ago)
Author:
anonymous
Message:

Refactor URLSlug() and cleanFileName(). Add simplifyAccents().

Location:
trunk/lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/FormValidator.inc.php

    r685 r722  
    6767        'notice' => ' sc-msg-notice ',
    6868        'success' => ' sc-msg-success ',
     69        'use_raise_msg' => false,
    6970    );
    7071
     
    151152    public function addError($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null)
    152153    {
     154        $app =& App::getInstance();
     155
     156        if (true === $this->getParam('use_raise_msg')) {
     157            $app->raiseMsg($msg, $type, $file, $line);
     158        }
     159
    153160        $this->errors[] = array(
    154161            'name' => $form_name,
  • trunk/lib/Utilities.inc.php

    r718 r722  
    520520}
    521521
    522 /*
    523 * Converts a string into a URL-safe slug, removing spaces and non word characters.
    524 *
    525 * @access   public
    526 * @param    string  $str    String to convert.
    527 * @return   string          URL-safe slug.
    528 * @author   Quinn Comendant <quinn@strangecode.com>
    529 * @version  1.0
    530 * @since    18 Aug 2014 12:54:29
    531 */
    532 function URLSlug($str)
    533 {
    534     $slug = preg_replace(array('/\W+/u', '/^-+|-+$/u'), array('-', ''), $str);
    535     $slug = strtolower($slug);
    536     return $slug;
    537 }
    538 
    539522/**
    540523 * Return a human readable disk space measurement. Input value measured in bytes.
     
    623606}
    624607
    625 /**
    626  * Removes non-latin characters from file name, using htmlentities to convert known weirdos into regular squares.
     608/*
     609* Converts strange characters into ASCII using a htmlentities hack. If a character does not have a specific rule, it will remain as its entity name, e.g., `5¢` becomes `5&cent;` which becomes `5cent`.
     610*
     611* @access   public
     612* @param    string  $str    Input string of text containing accents.
     613* @return   string          String with accented characters converted to ASCII equivalents.
     614* @author   Quinn Comendant <quinn@strangecode.com>
     615* @since    30 Apr 2020 21:29:16
     616*/
     617function simplifyAccents($str)
     618{
     619    $app =& App::getInstance();
     620
     621    return preg_replace([
     622        '/&amp;(?=[\w\d#]{1,10};)/ui',
     623        '/&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml|caron);/ui',
     624        '/&(?:ndash|mdash|horbar);/ui',
     625        '/&(?:nbsp);/ui',
     626        '/&(?:bdquo|ldquo|ldquor|lsquo|lsquor|rdquo|rdquor|rsquo|rsquor|sbquo|lsaquo|rsaquo);/ui',
     627        '/&(?:amp);/ui', // This replacement must come after matching all other entities.
     628        '/[&;]+/u',
     629    ], [
     630        '&',
     631        '$1',
     632        '-',
     633        ' ',
     634        '',
     635        'and',
     636        '',
     637    ], htmlentities($str, ENT_NOQUOTES | ENT_IGNORE, $app->getParam('character_set')));
     638}
     639
     640/*
     641* Converts a string into a URL-safe slug, removing spaces and non word characters.
     642*
     643* @access   public
     644* @param    string  $str    String to convert.
     645* @return   string          URL-safe slug.
     646* @author   Quinn Comendant <quinn@strangecode.com>
     647* @version  1.0
     648* @since    18 Aug 2014 12:54:29
     649*/
     650function URLSlug($str)
     651{
     652    return strtolower(urlencode(preg_replace(['/[-\s–—.:;?!@#=+_\/\\\]+|(?:&nbsp;|&#160;|&ndash;|&#8211;|&mdash;|&#8212;|%c2%a0|%e2%80%93|%e2%80%9)+/u', '/-+/u', '/[^\w-]+/u', '/^-+|-+$/u'], ['-', '-', '', ''], simplifyAccents($str))));
     653}
     654
     655/**
     656 * Converts a string of text into a safe file name by removing non-ASCII characters and non-word characters.
    627657 *
    628658 * @access  public
     
    634664    $app =& App::getInstance();
    635665
    636     $file_name = preg_replace(array(
    637         '/&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml|caron);/ui',
    638         '/&(?:amp);/ui',
    639         '/[&;]+/u',
    640         '/[^a-zA-Z0-9()@._=+-]+/u',
    641         '/^_+|_+$/u'
    642     ), array(
    643         '$1',
    644         'and',
    645         '',
    646         '_',
    647         ''
    648     ), htmlentities($file_name, ENT_NOQUOTES | ENT_IGNORE, $app->getParam('character_set')));
     666    $file_name = preg_replace(['/[^a-zA-Z0-9()@._=+-]+/u', '/^_+|_+$/u'], ['_', ''], simplifyAccents($file_name));
    649667    return mb_substr($file_name, 0, 250);
    650668}
Note: See TracChangeset for help on using the changeset viewer.