dmBridge PHP API
DMString.php
00001 <?php
00002 #
00003 # dmBridge: a data access framework for CONTENTdm(R)
00004 #
00005 # Copyright © 2009, 2010, 2011 Board of Regents of the Nevada System of Higher
00006 # Education, on behalf of the University of Nevada, Las Vegas
00007 #
00008 
00015 abstract class DMString {
00016 
00022    public static function camelCaseToUnderscore($str) {
00023       return strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $str));
00024    }
00025 
00036    public static function clean($str) {
00037       // Plan A: iconv; this is documented as working, but returns an empty
00038       // string for me. Should be harmless to try, at least.
00039       if (function_exists('iconv')) {
00040          $tmp = @iconv("UTF-8", "UTF-8//IGNORE", $str);
00041          if (strlen($tmp)) {
00042             return $tmp;
00043          }
00044       }
00045       // Plan B: strip out any non-ASCII character
00046       return filter_var(
00047          $str,
00048          FILTER_UNSAFE_RAW,
00049          FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
00050    }
00051 
00064    public static function paranoid($str, $allowed=array()) {
00065       $allow = null;
00066       if (!empty($allowed)) {
00067          foreach ($allowed as $value) {
00068             $allow .= "\\$value";
00069          }
00070       }
00071       $cleaned = preg_replace("/[^{$allow}a-zA-Z0-9]/", '', $str);
00072       return $cleaned;
00073    }
00074 
00083    public static function highlight($term, $str,
00084          $class = "dmHighlightedTerm") {
00085       return str_ireplace($term,
00086             '<span class="' . $class . '">' . $term . '</span>',
00087             $str);
00088    }
00089 
00099    public static function hyperlink($str, DOMDocument $dxml = null) {
00100       $protocols = array("http", "https", "ftp", "gopher");
00101 
00102       $tmp = explode(' ', $str);
00103       if ($dxml) {
00104          $fragment = $dxml->createDocumentFragment();
00105          foreach ($tmp as &$word) {
00106             $match = false;
00107             foreach ($protocols as $proto) {
00108                if ($proto . "://" == substr($word, 0, strlen($proto . "://"))) {
00109                   $fragment->appendXML(
00110                         sprintf('<a href="%s">%s</a>', $word, $word)
00111                            . " ");
00112                   $match = true;
00113                   break;
00114                }
00115             }
00116             if (!$match) {
00117                $fragment->appendXML($word . " ");
00118             }
00119          }
00120          return $fragment;
00121       } else {
00122          foreach ($tmp as &$word) {
00123             foreach ($protocols as $proto) {
00124                if ($proto . "://" == substr($word, 0, strlen($proto . "://"))) {
00125                   $word = sprintf('<a href="%s">%s</a>', $word, $word);
00126                   break;
00127                }
00128             }
00129          }
00130          return implode(" ", $tmp);
00131       }
00132    }
00133 
00142    public static function truncate($str, $max_words) {
00143       $text_array = explode(' ', $str);
00144       if (count($text_array) > $max_words && $max_words > 0) {
00145          $str = implode(' ', array_slice($text_array, 0, (int) $max_words))
00146             . '...';
00147       }
00148       return $str;
00149    }
00150 
00156    public static function isUTF8($str) {
00157       // Plan A: iconv
00158       if (function_exists('iconv')) {
00159          return ($str == @iconv("UTF-8", "UTF-8", $str));
00160       }
00161       // Plan B: UTF-8 to Code Point Array Converter
00162       require_once(dirname(__FILE__) . "/../libraries/php-utf8/utf8.inc.php");
00163       return utf8toUnicode($str);
00164    }
00165 
00174    public static function isValidHostname($str) {
00175       // Total domain can only be from 1 to 255 characters, inclusive
00176       if ((strlen($str) < 1) || (strlen($str) > 255)) {
00177           return false;
00178       }
00179       // Check if domain is IP, possibly enclosed in square brackets.
00180       if (preg_match('/^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])'
00181          .'(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}$/'
00182          ,$str) ||
00183           preg_match('/^\[(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])'
00184          .'(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}\]$/'
00185          ,$str)) {
00186           return true;
00187       } else {
00188           $arrDomainPortion = explode('.', $str);
00189           if (sizeof($arrDomainPortion) < 2) {
00190               return false; // Not enough parts to domain
00191           }
00192           for ($i = 0, $max = sizeof($arrDomainPortion); $i < $max; $i++) {
00193               // Each portion must be between 1 and 63 characters, inclusive
00194                if ((strlen($arrDomainPortion[$i]) < 1)
00195                      || (strlen($arrDomainPortion[$i]) > 63)) {
00196                   return false;
00197               }
00198               if (!preg_match('/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|'
00199                  .'([A-Za-z0-9]+))$/', $arrDomainPortion[$i])) {
00200                   return false;
00201               }
00202           }
00203       }
00204       return true;
00205    }
00206 
00213    public static function isValidIPv4Address(
00214          $str, $allow_ranges = false) {
00215       if ($str != '::1') { // localhost
00216          if (!is_numeric(str_replace('.', '', $str))) return false;
00217          $a = explode('.', $str);
00218          if (sizeof($a) > 4) return false;
00219          if (!$allow_ranges && sizeof($a) < 4) return false;
00220          foreach ($a as $octet) {
00221             if ($octet > 255) return false;
00222          }
00223       }
00224       return true;
00225    }
00226 
00233    public static function isValidPathname($str, $check_exists = false) {
00234       $legal_chars = array('.', '_', '-', '/', '\\');
00235       if ($str == DMString::paranoid($str, $legal_chars)) {
00236          if ($check_exists) {
00237             return file_exists($str);
00238          }
00239          return true;
00240       }
00241       return false;
00242    }
00243 
00250    public static function underscoreToCamelCase(
00251          $str, $capitalize_first_char = false) {
00252       $words = explode('_', strtolower($str));
00253       $return = '';
00254       $count = count($words);
00255       for ($i = 0; $i < $count; $i++) {
00256          if ($i == 0 && !$capitalize_first_char) {
00257             $return .= trim($words[$i]);
00258          }
00259          else $return .= ucfirst(trim($words[$i]));
00260       }
00261       return $return;
00262    }
00263 
00273    public static function websafe($str, $encoding = "UTF-8") {
00274       return htmlspecialchars(strip_tags($str), ENT_QUOTES, $encoding);
00275    }
00276 
00282    public static function xmlentities($str) {
00283       return str_replace(array('&', '"', "'", '<', '>'),
00284          array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'),
00285          $str);
00286    }
00287 
00288 }
 All Data Structures Functions Variables