/home/coolpkct/www/websites/cake3.cool.rocks/admin/classes/helpers.php
<?php
/**
 * Part of SimpleViewer.net portfolio web site package.
 *
 * @package Showkase
 * @author Jack Hardie {@link http://www.jhardie.com}
 * @copyright Copyright (c) 2012, SimpleViewer Inc.
 */
defined('SK_ACCESS')||die('<h1>403: Forbidden</h1>');
Class Helpers
{
    /**
     * Convert ini-style G, M, kbytes to bytes
     * Note that switch statement drops through without breaks
     *
     * @access private
     * @return integer bytes
     * @param string
     */
    public static function getBytes($val)
    {
      $val = trim($val);
      $last = strtolower($val{strlen($val)-1});
      switch ($last) {
          case 'g':
              $val *= 1024;
          case 'm':
              $val *= 1024;
          case 'k':
              $val *= 1024;
      }
      return $val;
    }
    
    /**
     * return a properly formatted hex color string
     *
     * @return string
     * @param string containing hex color
     * @param integer required length of hex number in characters
     */
    public static function cleanHex($hex, $length = 6)
    {
        $hex = trim(strtolower($hex));
        $hex = ltrim($hex, '#');
        $hex = str_replace('0x', '', $hex);
        return '0x'.str_pad(strtoupper(dechex(hexdec(substr(trim($hex), 0, $length)))), $length, '0', STR_PAD_LEFT);
    }
    
    /**
     * Based on Drupal hexToRgb
     *
     * @return string
     * @param string
     */
    public static function hexToRgb($hex)
    {
        $hex = trim(strtolower($hex));
        $hex = ltrim($hex, '#');
        $hex = str_replace('0x', '', $hex);
        // Convert shorthands like '#abc' to '#aabbcc'.
        if (strlen($hex) == 3) {
          $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
        }
        $c = hexdec($hex);
        return array(
          'red' => $c >> 16 & 0xFF, 
          'green' => $c >> 8 & 0xFF, 
          'blue' => $c & 0xFF,
        );
    }
      
    /**
     * Based on Drupal rgbToHex function
     *
     * @param string
     * @return array (hex,alpha) or ('000000', null) for most malformed input
     */
    public static function rgbaToHex($rgbaString) {
        // allow peculiar spacing with ' *'
        preg_match('/(\d+) *, *(\d+) *, *(\d+) *, *([0-9]*\.?[0-9]+)/', $rgbaString, $rgb);
        array_shift($rgb);
        $alpha = array_pop($rgb);
        $out = 0;
        foreach ($rgb as $k => $v) {
            $out |= $v << (16 - $k * 8);
        }
        return array(strtoupper(str_pad(dechex($out), 6, 0, STR_PAD_LEFT)), $alpha);
    }
     
    /**
     * Get which version of GD is installed, if any.
     *
     * @access private
     * @return string version vector or '0' if GD not installed
     */
    public static function getGdVersion()
    {
      if (! extension_loaded('gd')) { return '0'; }
      // Use the gd_info() function if possible.
      if (function_exists('gd_info'))
      {
        $versionInfo = gd_info();
        preg_match("/[\d\.]+/", $versionInfo['GD Version'], $matches);
        return $matches[0];
      }
      // If phpinfo() is disabled return false...
      if (preg_match('/phpinfo/', ini_get('disable_functions')))
      {
        return '0';
      }
      // ...otherwise use phpinfo().
      ob_start();
      @phpinfo(8);
      $moduleInfo = ob_get_contents();
      ob_end_clean();
      if (preg_match("/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i", $moduleInfo,$matches))
      {
        $gdVersion = $matches[1];
      }
      else
      {
        $gdVersion = '0';
      }
      return $gdVersion;
    }
  
    /**
     * Get maximum byte size to plug into uploader
     *
     * @access public
     * @return integer file size in MBytes
     */
    public static function getMaxUploadMBytes()
    {
      if (POST_MAX_SIZE !== 0) return POST_MAX_SIZE;
      $postMaxSize = (ini_get('post_max_size') == '') ? POST_MAX_SIZE_FALLBACK : ini_get('post_max_size');
      $uploadMaxFilesize = (ini_get('upload_max_filesize') == '') ? UPLOAD_MAX_FILESIZE_FALLBACK : ini_get('upload_max_filesize');
      $postMaxMBytes = intval(self::getBytes($postMaxSize)/pow(2,20));
      $uploadMaxMBytes = intval(self::getBytes($uploadMaxFilesize)/pow(2,20));
      return min($postMaxMBytes, $uploadMaxMBytes);
    }
    
    /**
     * Get max_file_uploads setting
     *
     * @return string
     */
    public static function getMaxFileUploads()
    {
      $maxFileUploads = ini_get('max_file_uploads');
      if (!$maxFileUploads || is_null($maxFileUploads)) return false;
      return (string)$maxFileUploads;
    }
    
    /**
     * Flatten an array http://php.net/manual/en/ref.array.php
     *
     * @param array()
     * @return one-dimensional array
     */ 
    public static function array_flatten($a) {
      foreach ($a as $k=>$v) $a[$k]=(array)$v;
      return call_user_func_array('array_merge',$a);
    }
    
    /**
     * Limit string to a given length including ...
     *
     * @param string
     * @param integer length
     * @return string
     */
    public static function stringLimit($string, $limit)
    {
      return (strlen($string) < $limit) ? $string : substr($string, 0, $limit-4).'…';
    }
    
    /**
     * Limit a string and break on a word boundary
     *
     * @param string
     * @param integer length
     * @return string
     */
    public static function stringLimitOnWord($string, $limit)
    {
      if (strlen($string) < $limit) return $string;
      $lines = explode("\n", wordwrap($string, $limit, "\n", true));
      return $lines[0].'...';
    }
    
    /**
     * Showkase tailored slug for directory names
     *
     * @param string that has already been transliterated
     * @return string may be empty
     */
    public static function slugFolder($folder)
    {
        $folder = preg_replace("#[^a-zA-Z0-9 /~_.\-\\\\]+#", '', $folder);
        $folder = str_replace(array(' ','_','/','\\'), '-', $folder);
        $folder = preg_replace("#\-+#", '-', $folder); // multiple separators
        $folder = trim($folder, '-');
        return strtolower($folder);
    }
    
    /**
     * Calculate a slug from an arbitrary string
     * @link http://stackoverflow.com/questions/2864785/drupal-standard-way-for-creating-a-slug-from-a-string
     * @param string source
     * @param integer maximum length the slug can have.
     * @param string separator
     * @return string representing the slug
     */
    public static function slug($string, $length = -1, $separator = '-')
    {
      $string = self::transliterate($string);
      $string = strtolower($string);
      // replace non alphanumeric and non underscore charachters by separator
      $string = preg_replace('/[^a-z0-9]/i', $separator, $string);
      // replace multiple occurences of separator by one instance
      $string = preg_replace('/'. preg_quote($separator) .'['. preg_quote($separator) .']*/', $separator, $string);
      // cut off to maximum length
      if ($length > -1 && strlen($string) > $length) $string = substr($string, 0, $length);
      // remove separator from start and end of string
      $string = trim($string, $separator);
      return $string;
    }
  
    /**
     * Transliterate a given string.
     *
     * @param $string to transliterate
     * @return string representing the transliterated version of the input string.
     */
    public static function transliterate($string)
    {
      static $charmap;
      if (!$charmap)
      {
        $charmap = array(
          // Decompositions for Latin-1 Supplement
          chr(195) . chr(128) => 'A', chr(195) . chr(129) => 'A',
          chr(195) . chr(130) => 'A', chr(195) . chr(131) => 'A',
          chr(195) . chr(132) => 'A', chr(195) . chr(133) => 'A',
          chr(195) . chr(135) => 'C', chr(195) . chr(136) => 'E',
          chr(195) . chr(137) => 'E', chr(195) . chr(138) => 'E',
          chr(195) . chr(139) => 'E', chr(195) . chr(140) => 'I',
          chr(195) . chr(141) => 'I', chr(195) . chr(142) => 'I',
          chr(195) . chr(143) => 'I', chr(195) . chr(145) => 'N',
          chr(195) . chr(146) => 'O', chr(195) . chr(147) => 'O',
          chr(195) . chr(148) => 'O', chr(195) . chr(149) => 'O',
          chr(195) . chr(150) => 'O', chr(195) . chr(153) => 'U',
          chr(195) . chr(154) => 'U', chr(195) . chr(155) => 'U',
          chr(195) . chr(156) => 'U', chr(195) . chr(157) => 'Y',
          chr(195) . chr(159) => 's', chr(195) . chr(160) => 'a',
          chr(195) . chr(161) => 'a', chr(195) . chr(162) => 'a',
          chr(195) . chr(163) => 'a', chr(195) . chr(164) => 'a',
          chr(195) . chr(165) => 'a', chr(195) . chr(167) => 'c',
          chr(195) . chr(168) => 'e', chr(195) . chr(169) => 'e',
          chr(195) . chr(170) => 'e', chr(195) . chr(171) => 'e',
          chr(195) . chr(172) => 'i', chr(195) . chr(173) => 'i',
          chr(195) . chr(174) => 'i', chr(195) . chr(175) => 'i',
          chr(195) . chr(177) => 'n', chr(195) . chr(178) => 'o',
          chr(195) . chr(179) => 'o', chr(195) . chr(180) => 'o',
          chr(195) . chr(181) => 'o', chr(195) . chr(182) => 'o',
          chr(195) . chr(182) => 'o', chr(195) . chr(185) => 'u',
          chr(195) . chr(186) => 'u', chr(195) . chr(187) => 'u',
          chr(195) . chr(188) => 'u', chr(195) . chr(189) => 'y',
          chr(195) . chr(191) => 'y',
          // Decompositions for Latin Extended-A
          chr(196) . chr(128) => 'A', chr(196) . chr(129) => 'a',
          chr(196) . chr(130) => 'A', chr(196) . chr(131) => 'a',
          chr(196) . chr(132) => 'A', chr(196) . chr(133) => 'a',
          chr(196) . chr(134) => 'C', chr(196) . chr(135) => 'c',
          chr(196) . chr(136) => 'C', chr(196) . chr(137) => 'c',
          chr(196) . chr(138) => 'C', chr(196) . chr(139) => 'c',
          chr(196) . chr(140) => 'C', chr(196) . chr(141) => 'c',
          chr(196) . chr(142) => 'D', chr(196) . chr(143) => 'd',
          chr(196) . chr(144) => 'D', chr(196) . chr(145) => 'd',
          chr(196) . chr(146) => 'E', chr(196) . chr(147) => 'e',
          chr(196) . chr(148) => 'E', chr(196) . chr(149) => 'e',
          chr(196) . chr(150) => 'E', chr(196) . chr(151) => 'e',
          chr(196) . chr(152) => 'E', chr(196) . chr(153) => 'e',
          chr(196) . chr(154) => 'E', chr(196) . chr(155) => 'e',
          chr(196) . chr(156) => 'G', chr(196) . chr(157) => 'g',
          chr(196) . chr(158) => 'G', chr(196) . chr(159) => 'g',
          chr(196) . chr(160) => 'G', chr(196) . chr(161) => 'g',
          chr(196) . chr(162) => 'G', chr(196) . chr(163) => 'g',
          chr(196) . chr(164) => 'H', chr(196) . chr(165) => 'h',
          chr(196) . chr(166) => 'H', chr(196) . chr(167) => 'h',
          chr(196) . chr(168) => 'I', chr(196) . chr(169) => 'i',
          chr(196) . chr(170) => 'I', chr(196) . chr(171) => 'i',
          chr(196) . chr(172) => 'I', chr(196) . chr(173) => 'i',
          chr(196) . chr(174) => 'I', chr(196) . chr(175) => 'i',
          chr(196) . chr(176) => 'I', chr(196) . chr(177) => 'i',
          chr(196) . chr(178) => 'IJ', chr(196) . chr(179) => 'ij',
          chr(196) . chr(180) => 'J', chr(196) . chr(181) => 'j',
          chr(196) . chr(182) => 'K', chr(196) . chr(183) => 'k',
          chr(196) . chr(184) => 'k', chr(196) . chr(185) => 'L',
          chr(196) . chr(186) => 'l', chr(196) . chr(187) => 'L',
          chr(196) . chr(188) => 'l', chr(196) . chr(189) => 'L',
          chr(196) . chr(190) => 'l', chr(196) . chr(191) => 'L',
          chr(197) . chr(128) => 'l', chr(197) . chr(129) => 'L',
          chr(197) . chr(130) => 'l', chr(197) . chr(131) => 'N',
          chr(197) . chr(132) => 'n', chr(197) . chr(133) => 'N',
          chr(197) . chr(134) => 'n', chr(197) . chr(135) => 'N',
          chr(197) . chr(136) => 'n', chr(197) . chr(137) => 'N',
          chr(197) . chr(138) => 'n', chr(197) . chr(139) => 'N',
          chr(197) . chr(140) => 'O', chr(197) . chr(141) => 'o',
          chr(197) . chr(142) => 'O', chr(197) . chr(143) => 'o',
          chr(197) . chr(144) => 'O', chr(197) . chr(145) => 'o',
          chr(197) . chr(146) => 'OE', chr(197) . chr(147) => 'oe',
          chr(197) . chr(148) => 'R', chr(197) . chr(149) => 'r',
          chr(197) . chr(150) => 'R', chr(197) . chr(151) => 'r',
          chr(197) . chr(152) => 'R', chr(197) . chr(153) => 'r',
          chr(197) . chr(154) => 'S', chr(197) . chr(155) => 's',
          chr(197) . chr(156) => 'S', chr(197) . chr(157) => 's',
          chr(197) . chr(158) => 'S', chr(197) . chr(159) => 's',
          chr(197) . chr(160) => 'S', chr(197) . chr(161) => 's',
          chr(197) . chr(162) => 'T', chr(197) . chr(163) => 't',
          chr(197) . chr(164) => 'T', chr(197) . chr(165) => 't',
          chr(197) . chr(166) => 'T', chr(197) . chr(167) => 't',
          chr(197) . chr(168) => 'U', chr(197) . chr(169) => 'u',
          chr(197) . chr(170) => 'U', chr(197) . chr(171) => 'u',
          chr(197) . chr(172) => 'U', chr(197) . chr(173) => 'u',
          chr(197) . chr(174) => 'U', chr(197) . chr(175) => 'u',
          chr(197) . chr(176) => 'U', chr(197) . chr(177) => 'u',
          chr(197) . chr(178) => 'U', chr(197) . chr(179) => 'u',
          chr(197) . chr(180) => 'W', chr(197) . chr(181) => 'w',
          chr(197) . chr(182) => 'Y', chr(197) . chr(183) => 'y',
          chr(197) . chr(184) => 'Y', chr(197) . chr(185) => 'Z',
          chr(197) . chr(186) => 'z', chr(197) . chr(187) => 'Z',
          chr(197) . chr(188) => 'z', chr(197) . chr(189) => 'Z',
          chr(197) . chr(190) => 'z', chr(197) . chr(191) => 's',
          // Euro Sign
          chr(226) . chr(130) . chr(172) => 'E'
        );
      }
      return strtr($string, $charmap);
    }
    /**
     * @param string source
     * @param integer maximum length the slug can have.
     * @param string separator
     * @return boolean string is a slug
     */
    public static function is_slug($str)
    {
      return $str == self::slug($str);
    }
    
    /**
     * Replace file extension
     * Note that returned path may contain a directory separator - be careful with urls
     *
     * @return string new path
     * @param string old path
     * @param string new extension
     * @param array of extensions not to change
     */
    public static function changeFileExtension($path, $extension, array $exclusions)
    {
      $info = pathinfo($path);
      if (in_array($info['extension'], $exclusions)) return $path;
      $baseName = basename($path,'.'.$info['extension']);
      $dirname = ($info['dirname'] == '.') ? '' : $info['dirname'].DIRECTORY_SEPARATOR;
      return
        $dirname.
        basename($path,'.'.$info['extension']).
        '.'.ltrim($extension, '.');
    }
    
    /**
     * Replaces double line-breaks with paragraph elements.
     *
     * A group of regex replaces used to identify text formatted with newlines and
     * replace double line-breaks with HTML paragraph tags. The remaining
     * line-breaks after conversion become <<br />> tags, unless $br is set to '0'
     * or 'false'.
     *
     * @since 0.71
     *
     * * @author Matt Mullenweg {@link http://ma.tt/scripts/autop/}
     *
     * @param string $pee The text which has to be formatted.
     * @param int|bool $br Optional. If set, this will convert all remaining line-breaks after paragraphing. Default true.
     * @return string Text which has been converted into correct paragraph tags.
     */
    public static function wpautop($pee, $br = 1) {
    	if (trim($pee) === '')
    		return '';
    	$pee = $pee . "\n"; // just to make things a little easier, pad the end
    	$pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
    	// Space things out a little
    	$allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|option|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)';
    	$pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
    	$pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
    	$pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
    	if (strpos($pee, '<object') !== false) {
    		$pee = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $pee); // no pee inside object/embed
    		$pee = preg_replace('|\s*</embed>\s*|', '</embed>', $pee);
    	}
    	$pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
    	// make paragraphs, including one at the end
    	$pees = preg_split('/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY);
    	$pee = '';
    	foreach ($pees as $tinkle)
    		$pee .= '<p>' . trim($tinkle, "\n") . "</p>\n";
    	$pee = preg_replace('|<p>\s*</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
    	$pee = preg_replace('!<p>([^<]+)</(div|address|form)>!', "<p>$1</p></$2>", $pee);
    	$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
    	$pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
    	$pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
    	$pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
    	$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
    	$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
    	if ($br) {
    		$pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', array('self', '_autop_newline_preservation_helper'), $pee);
    		$pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
    		$pee = str_replace('<WPPreserveNewline />', "\n", $pee);
    	}
    	$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
    	$pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
    	if (strpos($pee, '<pre') !== false)
    		$pee = preg_replace_callback('!(<pre[^>]*>)(.*?)</pre>!is', 'clean_pre', $pee );
    	$pee = preg_replace("|\n</p>$|", '</p>', $pee);
    
    	return $pee;
    }
    
    /**
     * Newline preservation help function for wpautop
     *
     * @since 3.1.0
     * @access private
     * @param array $matches preg_replace_callback matches array
     * @returns string
     */
    private static function _autop_newline_preservation_helper($matches)
    {
  	  return str_replace("\n", "<WPPreserveNewline />", $matches[0]);
    }
    
    /**
     * Get host name
     *
     * @return string
     */
    public static function getHost()
    {
        return isset($_SERVER['HTTP_HOST'])
            ? $_SERVER['HTTP_HOST']
            : $_SERVER['SERVER_NAME'];
    }
    
    /**
     * rawurlencodes basename part of url
     *
     * @param string url with forward slashes
     * @return string url
     */
    public static function encodeUrlBase($url)
    {
      $urlParts = explode('/', rtrim($url, '/'));
      $folder = rawurlencode(array_pop($urlParts));
      array_push($urlParts, $folder);
      return implode('/', $urlParts).'/';
    }
    
    
    /**
     * Simulates double_encode = true for php pre 5.2.3
     *
     * @author   Monte Ohrt <monte at ohrt dot com>
     * @param string
     * @return string
     */
    public static function escape_special_chars($string)
    {
        if (!is_array($string)) {
              if (version_compare(PHP_VERSION, '5.2.3', '>=')) {
                  $string = htmlspecialchars($string, ENT_QUOTES, 'UTF-8', false);
              } else {
              $string = preg_replace('!&(#?\w+);!', '%%%ENTITY_START%%%\\1%%%ENTITY_END%%%', $string);
              $string = htmlspecialchars($string);
              $string = str_replace(array('%%%ENTITY_START%%%','%%%ENTITY_END%%%'), array('&',';'), $string);
              }
        }
        return $string;
    }
    
    /** Check path for characters not acceptable in a url
    *
    * Based on http://www.designerstalk.com/forums/programming/17819-php-remove-punctuation.html
    * Regular expression modified by adding /_-. characters to acceptable set
    * Some versions of php are compiled without utf-8 support for posix named classes see
    * http://framework.zend.com/fisheye/browse/Zend_Framework/trunk/library/Zend/Filter/Alnum.php?r=5468
    * @access public
    * @return boolean true = no invalid characters
    * @param string to be checked
    */
    public static function checkUrl($path)
    {
        $path = trim(str_replace('\\', '/', $path));
        // Checks if PCRE is compiled with UTF-8 and Unicode support
        if (!@preg_match('/\pL/u', 'a')) {
            // POSIX named classes are not supported, use alternative a-zA-Z0-9 match
            $pattern = '#[^a-zA-Z0-9/~_.\-]+#';
        } else {
        // Unicode safe filter for the value
            $pattern = '#[^\p{L}\p{N}/~_.\-]+#u';
        }
        $match = preg_match($pattern, $path);
        if ($match === false) {
            throw new Exception('Error while checking for invalid characters in URL');
        }
        return !$match;
    }
    
    /**   
     * Calculates document root with no trailing separator
     * Note use of realpath to resolve symbolic links which can cause problems calculating relative paths
     *
     * @return string document root
     */
    public static function getDocRoot()
    {
        if (isset($_SERVER['DOCUMENT_ROOT'])) {
            return realpath(
                $_SERVER['DOCUMENT_ROOT']
            );
        }
        if (isset($_SERVER['SCRIPT_NAME']) && isset($_SERVER['SCRIPT_FILENAME'])) {
            return realpath(
                substr($_SERVER['SCRIPT_FILENAME'], 0, 0-strlen($_SERVER['SCRIPT_NAME']))
            );
        }
        throw new Exception ('Error: your server is not reporting the location of the web document root.');
    }
}