Ignore:
Timestamp:
Feb 19, 2014 2:28:00 AM (10 years ago)
Author:
anonymous
Message:

Merged in changes from trunk to finish Eli's branch.

Location:
branches/eli_branch
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eli_branch

  • branches/eli_branch/lib/Utilities.inc.php

    r439 r467  
    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/>.
     
    3333 * @param  bool  $var_dump Use var_dump instead of print_r.
    3434 */
    35 function dump($var, $display=false, $var_dump=false)
    36 {
     35function dump($var, $display=false, $var_dump=false, $file='', $line='')
     36{
     37    if (defined('_CLI')) {
     38        echo "\n$file $line\n";
     39    } else {
     40        echo $display ? "\n<br /><strong>$file $line</strong>\n<br /><pre>\n" : "\n\n\n<!--\n$file $line\n";
     41    }
     42    if ($var_dump) {
     43        var_dump($var);
     44    } else {
     45        print_r($var);
     46    }
    3747    if (defined('_CLI')) {
    3848        echo "\n";
    39     } else {       
    40         echo $display ? "\n<br /><pre>\n" : "\n\n\n<!--\n";
    41     }
    42     if ($var_dump) {
    43         var_dump($var);
    44     } else {
    45         print_r($var);
    46     }
    47     if (defined('_CLI')) {
    48         echo "\n";
    49     } else {       
     49    } else {
    5050        echo $display ?  "\n</pre><br />\n" : "\n-->\n\n\n";
    5151    }
    5252}
    5353
    54 /**
    55  * Return dump as variable.
    56  *
    57  * @param  mixed $var           Variable to dump.
    58  * @param  bool  $serialize     Remove line-endings. Useful for logging variables.
    59  * @return string Dump of var.
    60  */
     54/*
     55* Log a PHP variable to javascript console. Relies on getDump(), below.
     56*
     57* @access   public
     58* @param    mixed   $var      The variable to dump.
     59* @param    string  $prefix   A short note to print before the output to make identifying output easier.
     60* @param    string  $file     The value of __FILE__.
     61* @param    string  $line     The value of __LINE__.
     62* @return   null
     63* @author   Quinn Comendant <quinn@strangecode.com>
     64*/
     65function jsDump($var, $prefix='jsDump', $file='-', $line='-')
     66{
     67    if (!empty($var)) {
     68        ?>
     69        <script type="text/javascript" charset="utf-8">
     70        /* <![CDATA[ */
     71        window.console && console.log('<?php printf('%s: %s (on line %s of %s)', $prefix, str_replace("'", "\\'", getDump($var, true)), $line, $file); ?>');
     72        /* ]]> */
     73        </script>
     74        <?php
     75    }
     76}
     77
     78/*
     79* Return a string version of any variable, optionally serialized on one line.
     80*
     81* @access   public
     82* @param    mixed   $var        The variable to dump.
     83* @param    bool    $serialize  If true, remove line-endings. Useful for logging variables.
     84* @return   string              The dumped variable.
     85* @author   Quinn Comendant <quinn@strangecode.com>
     86*/
    6187function getDump($var, $serialize=false)
    6288{
     
    6591    $d = ob_get_contents();
    6692    ob_end_clean();
    67     return $serialize ? preg_replace('/\s+/m', '', $d) : $d;
     93    return $serialize ? preg_replace('/\s+/m', ' ', $d) : $d;
    6894}
    6995
     
    180206{
    181207    $words = preg_split('/[^\w]/', $search, -1, PREG_SPLIT_NO_EMPTY);
    182    
     208
    183209    $search = array();
    184210    $replace = array();
    185    
     211
    186212    foreach ($words as $w) {
    187213        if ('' != trim($w)) {
     
    266292
    267293/**
    268  * Turns "a really long string" into "a rea...string"
     294 * Truncates "a really long string" into a string of specified length
     295 * at the beginning: "
long string"
     296 * at the middle: "a rea
string"
     297 * or at the end: "a really
".
     298 *
     299 * The regular expressions below first match and replace the string to the specified length and position,
     300 * and secondly they remove any whitespace from around the delimiter (to avoid "this 
 " from happening).
    269301 *
    270302 * @access  public
     
    272304 * @param   int     $len    Maximum string length.
    273305 * @param   string  $where  Where to cut the string. One of: 'start', 'middle', or 'end'.
    274  * @return  string          Truncated output string
     306 * @return  string          Truncated output string.
    275307 * @author  Quinn Comendant <quinn@strangecode.com>
    276308 * @since   29 Mar 2006 13:48:49
    277309 */
    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);
     310function truncate($str, $len=50, $where='end', $delim='
')
     311{
     312    $dlen = mb_strlen($delim);
     313    if ($len <= $dlen || mb_strlen($str) <= $dlen) {
     314        return substr($str, 0, $len);
     315    }
     316    $part1 = floor(($len - $dlen) / 2);
     317    $part2 = ceil(($len - $dlen) / 2);
    285318    switch ($where) {
    286319    case 'start' :
    287         return preg_replace(array(sprintf('/^.{4,}(.{%s})$/sU', $part1 + $part2), '/\s*\.{3,}\s*/sU'), array($delim . '$1', $delim), $str);
    288         break;
     320        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);
     321
     322    case 'middle' :
     323        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);
     324
     325    case 'end' :
    289326    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;
     327        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);
    296328    }
    297329}
     
    307339* @since    06 Dec 2008 17:04:01
    308340*/
    309 if (!function_exists('mb_ucfirst')) {   
     341if (!function_exists('mb_ucfirst')) {
    310342    function mb_ucfirst($string)
    311343    {
     
    326358* @since    20 Jan 2013 12:33:26
    327359*/
    328 if (!function_exists('mb_strtr')) {   
     360if (!function_exists('mb_strtr')) {
    329361    function mb_strtr($string, $from, $to)
    330362    {
     
    367399/*
    368400* Returns a human readable amount of time for the given amount of seconds.
    369 * 
     401*
    370402* 45 seconds
    371403* 12 minutes
     
    374406* 1 week
    375407* 4 months
    376 * 
     408*
    377409* Months are calculated using the real number of days in a year: 365.2422 / 12.
    378410*
     
    400432        'century' => array(3155692608, _("century"), _("centuries")),
    401433    );
    402    
     434
    403435    // Max unit to calculate.
    404436    $max_unit = isset($units[$max_unit]) ? $max_unit : 'year';
     
    416448    }
    417449    $final_time = sprintf($format, $final_time);
    418     return sprintf('%s %s', $final_time, (1 == $final_time ? $units[$final_unit][1] : $units[$final_unit][2]));   
     450    return sprintf('%s %s', $final_time, (1 == $final_time ? $units[$final_unit][1] : $units[$final_unit][2]));
    419451}
    420452
     
    430462{
    431463    $app =& App::getInstance();
    432    
     464
    433465    foreach (explode(PATH_SEPARATOR, get_include_path()) as $path) {
    434466        $fullpath = $path . DIRECTORY_SEPARATOR . $file;
     
    680712 * escaped.  This is useful for putting values coming in from posted
    681713 * checkboxes into a SET column of a database.
    682  * 
     714 *
    683715 *
    684716 * @param  array $in      Array to convert.
     
    688720{
    689721    $db =& DB::getInstance();
    690    
     722
    691723    if (is_array($in) && !empty($in)) {
    692724        return join($separator, array_map(array($db, 'escapeString'), $in));
     
    839871{
    840872    $app =& App::getInstance();
    841    
     873
    842874    if ('' == trim($val)) {
    843875        $app->logMsg(sprintf('Cannot add signature to an empty string.', null), LOG_INFO, __FILE__, __LINE__);
     
    848880        $salt = $app->getParam('signing_key');
    849881    }
    850    
     882
    851883    // TODO: consider using more bits-per-character, such as done with:
    852884    // http://www.php.net/manual/en/function.sha1.php#86239
     
    916948{
    917949    $app =& App::getInstance();
    918    
     950
    919951    $add_members = '/usr/lib/mailman/bin/add_members';
    920952    /// FIXME: checking of executable is disabled.
     
    948980{
    949981    $app =& App::getInstance();
    950    
     982
    951983    $remove_members = '/usr/lib/mailman/bin/remove_members';
    952984    /// FIXME: checking of executable is disabled.
Note: See TracChangeset for help on using the changeset viewer.