Changeset 454


Ignore:
Timestamp:
Jan 4, 2014 3:25:57 AM (10 years ago)
Author:
anonymous
Message:

Improved the truncate() function.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/css/utilities.inc.css

    r406 r454  
    11/**
    2  * utilities.css 
     2 * utilities.css
    33 *
    44 * CSS used often in Strangecode applications. Do not arbitrarily
     
    4747}
    4848.sc-note:hover { cursor: help; }
     49.sc-ellipsis {
     50    overflow: hidden;
     51    white-space: nowrap;
     52    text-overflow: ellipsis;
     53}
    4954
    5055/* .sc-pkg class wraps enclosing block element around inner floated elements */
  • trunk/lib/Email.inc.php

    r424 r454  
    44 * For details visit the project site: <http://trac.strangecode.com/codebase/>
    55 * Copyright 2001-2012 Strangecode, LLC
    6  * 
     6 *
    77 * This file is part of The Strangecode Codebase.
    88 *
     
    1111 * Free Software Foundation, either version 3 of the License, or (at your option)
    1212 * any later version.
    13  * 
     13 *
    1414 * The Strangecode Codebase is distributed in the hope that it will be useful, but
    1515 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1616 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    1717 * details.
    18  * 
     18 *
    1919 * You should have received a copy of the GNU General Public License along with
    2020 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
     
    2828 * @author  Quinn Comendant <quinn@strangecode.com>
    2929 * @version 1.0
    30  * 
    31  
     30 *
     31
    3232// Example.
    3333$email = new Email(array(
     
    5050}
    5151
    52  * 
     52 *
    5353 */
    5454class Email {
     
    6161        'headers' => null,
    6262        'regex' => null,
    63        
     63
    6464        // A single carriage return (\n) should terminate lines for locally injected mail.
    6565        // A carriage return + line-feed (\r\n) should be used if sending mail directly with SMTP.
    6666        'crlf' => "\n",
    67        
     67
    6868        // RFC 2822 says line length MUST be no more than 998 characters, and SHOULD be no more than 78 characters, excluding the CRLF.
    6969        // http://mailformat.dan.info/body/linelength.html
     
    120120    {
    121121        $app =& App::getInstance();
    122    
     122
    123123        if (isset($params) && is_array($params)) {
    124124            // Enforce valid email addresses.
     
    147147    {
    148148        $app =& App::getInstance();
    149    
     149
    150150        if (isset($this->_params[$param])) {
    151151            return $this->_params[$param];
     
    167167    {
    168168        $app =& App::getInstance();
    169    
     169
    170170        // Load file, using include_path.
    171171        if (!$this->_template = file_get_contents($template, true)) {
     
    191191    {
    192192        $app =& App::getInstance();
    193    
     193
    194194        if ('' == trim($string)) {
    195195            $app->logMsg(sprintf('Empty string provided.', null), LOG_ERR, __FILE__, __LINE__);
     
    215215    {
    216216        $app =& App::getInstance();
    217    
     217
    218218        // Ensure template exists.
    219219        if (!isset($this->_template)) {
     
    285285        $final_body = isset($this->_template_replaced) ? $this->_template_replaced : $this->_template;
    286286        if (false !== $this->getParam('wrap')) {
    287             $final_body = wordwrap($final_body, $this->getParam('line_length'), $this->getParam('crlf'));           
     287            $final_body = wordwrap($final_body, $this->getParam('line_length'), $this->getParam('crlf'));
    288288        }
    289289
     
    320320            $envelope_sender_header = sprintf('-f %s', $envelope_sender_address);
    321321        } else {
    322             $envelope_sender_header = '';           
     322            $envelope_sender_header = '';
    323323        }
    324324
     
    331331        }
    332332
    333         // Send email without 5th parameter if safemode is enabled. 
     333        // Send email without 5th parameter if safemode is enabled.
    334334        if (ini_get('safe_mode')) {
    335335            $ret = mb_send_mail($final_to, $this->_params['subject'], $final_body, $final_headers);
     
    337337            $ret = mb_send_mail($final_to, $this->_params['subject'], $final_body, $final_headers, $envelope_sender_header);
    338338        }
    339        
     339
    340340        // Ensure message was successfully accepted for delivery.
    341341        if ($ret) {
     
    345345            $app->logMsg(sprintf('Email failure with parameters: %s, %s, %s, %s', $final_to, $this->_params['subject'], str_replace("\r\n", '\r\n', $final_headers), $envelope_sender_header), LOG_NOTICE, __FILE__, __LINE__);
    346346            return false;
    347         } 
     347        }
    348348    }
    349349
     
    366366    {
    367367        $app =& App::getInstance();
    368    
     368
    369369        // If an array, check values recursively.
    370370        if (is_array($email)) {
  • trunk/lib/Utilities.inc.php

    r419 r454  
    44 * For details visit the project site: <http://trac.strangecode.com/codebase/>
    55 * Copyright 2001-2012 Strangecode, LLC
    6  * 
     6 *
    77 * This file is part of The Strangecode Codebase.
    88 *
     
    1111 * Free Software Foundation, either version 3 of the License, or (at your option)
    1212 * any later version.
    13  * 
     13 *
    1414 * The Strangecode Codebase is distributed in the hope that it will be useful, but
    1515 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1616 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    1717 * details.
    18  * 
     18 *
    1919 * You should have received a copy of the GNU General Public License along with
    2020 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
     
    3737    if (defined('_CLI')) {
    3838        echo "\n";
    39     } else {       
     39    } else {
    4040        echo $display ? "\n<br /><pre>\n" : "\n\n\n<!--\n";
    4141    }
     
    4747    if (defined('_CLI')) {
    4848        echo "\n";
    49     } else {       
     49    } else {
    5050        echo $display ?  "\n</pre><br />\n" : "\n-->\n\n\n";
    5151    }
     
    180180{
    181181    $words = preg_split('/[^\w]/', $search, -1, PREG_SPLIT_NO_EMPTY);
    182    
     182
    183183    $search = array();
    184184    $replace = array();
    185    
     185
    186186    foreach ($words as $w) {
    187187        if ('' != trim($w)) {
     
    266266
    267267/**
    268  * Turns "a really long string" into "a rea...string"
     268 * Truncates "a really long string" into a string of specified length
     269 * at the beginning: "
long string"
     270 * at the middle: "a rea
string"
     271 * or at the end: "a really
".
     272 *
     273 * The regular expressions below first match and replace the string to the specified length and position,
     274 * and secondly they remove any whitespace from around the delimiter (to avoid "this 
 " from happening).
    269275 *
    270276 * @access  public
     
    272278 * @param   int     $len    Maximum string length.
    273279 * @param   string  $where  Where to cut the string. One of: 'start', 'middle', or 'end'.
    274  * @return  string          Truncated output string
     280 * @return  string          Truncated output string.
    275281 * @author  Quinn Comendant <quinn@strangecode.com>
    276282 * @since   29 Mar 2006 13:48:49
    277283 */
    278 function truncate($str, $len, $where='end', $delim='
')
    279 {
    280     if ($len <= 3 || mb_strlen($str) <= 3) {
    281         return '';
    282     }
    283     $part1 = floor(($len - 3) / 2);
    284     $part2 = ceil(($len - 3) / 2);
     284function truncate($str, $len=50, $where='end', $delim='
')
     285{
     286    $dlen = mb_strlen($delim);
     287    if ($len <= $dlen || mb_strlen($str) <= $dlen) {
     288        return substr($str, 0, $len);
     289    }
     290    $part1 = floor(($len - $dlen) / 2);
     291    $part2 = ceil(($len - $dlen) / 2);
    285292    switch ($where) {
    286293    case 'start' :
    287         return preg_replace(array(sprintf('/^.{4,}(.{%s})$/sU', $part1 + $part2), '/\s*\.{3,}\s*/sU'), array($delim . '$1', $delim), $str);
    288         break;
     294        return preg_replace(array(sprintf('/^.{%s,}(.{%s})$/sU', $dlen + 1, $part1 + $part2), sprintf('/\s*%s{%s,}\s*/sU', preg_quote($delim), $dlen)), array($delim . '$1', $delim), $str);
     295
     296    case 'middle' :
     297        return preg_replace(array(sprintf('/^(.{%s}).{%s,}(.{%s})$/sU', $part1, $dlen + 1, $part2), sprintf('/\s*%s{%s,}\s*/sU', preg_quote($delim), $dlen)), array('$1' . $delim . '$2', $delim), $str);
     298
     299    case 'end' :
    289300    default :
    290     case 'middle' :
    291         return preg_replace(array(sprintf('/^(.{%s}).{4,}(.{%s})$/sU', $part1, $part2), '/\s*\.{3,}\s*/sU'), array('$1' . $delim . '$2', $delim), $str);
    292         break;   
    293     case 'end' :
    294         return preg_replace(array(sprintf('/^(.{%s}).{4,}$/sU', $part1 + $part2), '/\s*\.{3,}\s*/sU'), array('$1' . $delim, $delim), $str);
    295         break;
     301        return preg_replace(array(sprintf('/^(.{%s}).{%s,}$/sU', $part1 + $part2, $dlen + 1), sprintf('/\s*%s{%s,}\s*/sU', preg_quote($delim), $dlen)), array('$1' . $delim, $delim), $str);
    296302    }
    297303}
     
    307313* @since    06 Dec 2008 17:04:01
    308314*/
    309 if (!function_exists('mb_ucfirst')) {   
     315if (!function_exists('mb_ucfirst')) {
    310316    function mb_ucfirst($string)
    311317    {
     
    326332* @since    20 Jan 2013 12:33:26
    327333*/
    328 if (!function_exists('mb_strtr')) {   
     334if (!function_exists('mb_strtr')) {
    329335    function mb_strtr($string, $from, $to)
    330336    {
     
    367373/*
    368374* Returns a human readable amount of time for the given amount of seconds.
    369 * 
     375*
    370376* 45 seconds
    371377* 12 minutes
     
    374380* 1 week
    375381* 4 months
    376 * 
     382*
    377383* Months are calculated using the real number of days in a year: 365.2422 / 12.
    378384*
     
    400406        'century' => array(3155692608, _("century"), _("centuries")),
    401407    );
    402    
     408
    403409    // Max unit to calculate.
    404410    $max_unit = isset($units[$max_unit]) ? $max_unit : 'year';
     
    416422    }
    417423    $final_time = sprintf($format, $final_time);
    418     return sprintf('%s %s', $final_time, (1 == $final_time ? $units[$final_unit][1] : $units[$final_unit][2]));   
     424    return sprintf('%s %s', $final_time, (1 == $final_time ? $units[$final_unit][1] : $units[$final_unit][2]));
    419425}
    420426
     
    430436{
    431437    $app =& App::getInstance();
    432    
     438
    433439    foreach (explode(PATH_SEPARATOR, get_include_path()) as $path) {
    434440        $fullpath = $path . DIRECTORY_SEPARATOR . $file;
     
    680686 * escaped.  This is useful for putting values coming in from posted
    681687 * checkboxes into a SET column of a database.
    682  * 
     688 *
    683689 *
    684690 * @param  array $in      Array to convert.
     
    688694{
    689695    $db =& DB::getInstance();
    690    
     696
    691697    if (is_array($in) && !empty($in)) {
    692698        return join($separator, array_map(array($db, 'escapeString'), $in));
     
    839845{
    840846    $app =& App::getInstance();
    841    
     847
    842848    if ('' == trim($val)) {
    843849        $app->logMsg(sprintf('Cannot add signature to an empty string.', null), LOG_INFO, __FILE__, __LINE__);
     
    848854        $salt = $app->getParam('signing_key');
    849855    }
    850    
     856
    851857    // TODO: consider using more bits-per-character, such as done with:
    852858    // http://www.php.net/manual/en/function.sha1.php#86239
     
    916922{
    917923    $app =& App::getInstance();
    918    
     924
    919925    $add_members = '/usr/lib/mailman/bin/add_members';
    920926    /// FIXME: checking of executable is disabled.
     
    948954{
    949955    $app =& App::getInstance();
    950    
     956
    951957    $remove_members = '/usr/lib/mailman/bin/remove_members';
    952958    /// FIXME: checking of executable is disabled.
Note: See TracChangeset for help on using the changeset viewer.