<?php

/****
 * toAscii function
 */
if (!function_exists('toAscii')) {
    function toAscii($str, $replace=array(), $delimiter='-', $charset='ISO-8859-1') {


        $str = str_replace(
            array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(133)),
            array("'", "'", '"', '"', '-', '--', '...'),
            $str); // by mordomiamil
        $str = iconv($charset, 'UTF-8', $str); // by lelebart
        if( !empty($replace) ) {
            $str = str_replace((array)$replace, ' ', $str);
        }
        $clean = $str;
        try {
            $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
        } catch( Exception $e) {

        }

        $str = preg_replace('/[^\x{0600}-\x{06FF}A-Za-z !@#$%^&*()]/u','', $str);
        $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
        $clean = strtolower(trim($clean, '-'));
        $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
        $clean = strtolower(trim($clean, '-'));
        return $clean;
    }
}

/**
 * Function to extract links, @mentions #hashtags e.t.c in a string
 */
if (!function_exists('extractIt')) {
    function extractIt($string) {
        require_once('library/extractor/Extractor.php');

        $extractor = Twitter_Extractor::create();
        return $extractor->extract($string);
    }
}

/**
 * Perfect function to serialize and unserialize
 *
 */
if (!function_exists('perfectSerialize')) {
    function perfectSerialize($string) {
        return base64_encode(serialize($string));
    }
}

if (!function_exists('perfectUnserialize')) {
    function perfectUnserialize($string) {

        if (base64_decode($string, true) == true) {

            return unserialize(base64_decode($string));
        } else {
            return @unserialize($string);
        }
    }
}

if (!function_exists('sanitizeText')) {
    function sanitizeText($string, $limit = false) {
        $string = trim($string);
        $string = str_replace('<','&#60;',$string);
        $string = str_replace('>','&#62;',$string);
        $string = str_replace("'",'&#39;',$string);
        $string = htmlspecialchars($string);
        $string = str_replace('\\r\\n','<br>',$string);
        $string = str_replace('\\n\\n','<br>',$string);
        $string = stripslashes($string);
        $string = str_replace('&amp;#','&#',$string);

        if ($limit) {
            $string = substr($string, 0, $limit);
        }
        return $string;
    }
}

if (!function_exists('remoteFileExists')) {
    function remoteFileExists($remote) {
        $curl = curl_init($remote);
        curl_setopt($curl, CURLOPT_NOBODY, true);

        //do request
        $result = curl_exec($curl);

        $ret = false;

        //if request did not fail
        if ($result !== false) {
            //if request was ok, check response code
            $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

            if ($statusCode == 200) {
                $ret = true;
            }
        }

        curl_close($curl);

        return $ret;
    }
}

if (!function_exists('formatBytes')) {
    function formatBytes($bytes = '', $force_unit = NULL, $format = NULL, $si = TRUE) {
        $bytes = ($bytes) ? $bytes : Config::get('image-max-size', '123344');
        // Format string
        $format = ($format === NULL) ? '%01.2f %s' : (string) $format;

        // IEC prefixes (binary)
        if ($si == FALSE OR strpos($force_unit, 'i') !== FALSE)
        {
            $units = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
            $mod   = 1024;
        }
        // SI prefixes (decimal)
        else
        {
            $units = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
            $mod   = 1000;
        }

        // Determine unit to use
        if (($power = array_search((string) $force_unit, $units)) === FALSE)
        {
            $power = ($bytes > 0) ? floor(log($bytes, $mod)) : 0;
        }

        return sprintf($format, $bytes / pow($mod, $power), $units[$power]);
    }
}