Вход Регистрация
Файл: class/UltimateHelperClass.php
Строк: 572
<?php

class UltimateHelperClass {

    
/* Private Functions */

    
private function checkCURL() {

        if(!
function_exists('curl_init')) die('cURL is not enabled !');

    }

    
/* Public Functions */
    
    
public function truncate($string, $maximumLength, $suffix = "...") {

        return 
substr($string, 0, $maximumLength) . $suffix;

    }



    public function 
wordTruncate($string, $maximumWords, $suffix = "...") {

        
/* Transform the string into a array, exploding by the space character */
        
$words = explode(' ', $string);

        
/* If the words count is greater than the maximum words then 
        we remove the rest of the words and finally returning the string */
        
if(count($words) > $maximumWords) {
            
$remainingWords = array_slice($words, 0, $maximumWords);
            
$string = implode(' ', $remainingWords) . $suffix;
        }

        return 
$string;

    }



    public function 
randomColor($type = "HEX") {

        
/* If we have the type HEX then we will generate a HEX code */
        
if(strtoupper($type) == "HEX") {

            
/* These are the possibilities of a HEX color */
            
$possibilities = array(1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F" );

            
/* Shuffle the array, randomly re-order them */
            
shuffle($possibilities);

            
/* Take the first 6 keys of the array and concatonate them */
            
$code = "#";
            for(
$i=1;$i<=6;$i++){
                
$code .= $possibilities[$i];
            }

            return 
$code;

        }

        
/* If we have the type RGB then we will generate a RGB code */
        
else if(strtoupper($type) == "RGB") {

            
/* Generate 3 random numbers between 0 and 255 
            and output them into the coresponding rgb format */
            
return "rgb(" . rand(0, 255) . "," . rand(0, 255) . "," . rand(0, 255) . ")";

        }

        
/* Return an error if the $type is not either RGB or HEX */
        
else return 'Incorrect <strong>$type</strong>, must be <u>RGB</u> or <u>HEX</u> !';

    }



    public function 
generateDistinctRandomNumbers($start, $end, $quantity) {

        
/* Generate an array of numbers between the $start and $end point*/
        
$numbers = range($start, $end);

        
/* Put the array keys in a random order */
        
shuffle($numbers);

        
/* Return an array with the first $quantity numbers */
        
return array_slice($numbers, 0, $quantity);

    }



    public function 
blockIpList($list, $message = "Your IP is blocked !") {

        
/* Get the users IP */
        
$ip = $_SERVER['REMOTE_ADDR'];

        
/* If its an array and the IP is in the list OR if its not an array and
        the ip is equal to the blocked ip then close the script with a custom message */
        
if((is_array($list) && in_array($ip, $list)) || (!is_array($list) && $ip == $list)) {
            die(
$message);
        } 
        
    }



    public function 
convertTemperature($from, $to, $temperature) {
    
        
/* Capitalize the $from and $to variables */
        
$from = strtoupper($from);
        
$to   = strtoupper($to);

        
/* Here we have the possible cases and the
        conversions formulas & returning the result */
        
if($from == "C") {
            switch(
$to) {
                case 
"K" :
                    return 
$output = $temperature + 273.15;
                break;

                case 
"F" :
                    return 
$output = ($temperature / (5/9)) + 32;
                break;
            }
        }


        else if(
$from == "F") {
            switch(
$to) {
                case 
"C" :
                    return 
$output = 5/9 * ($temperature - 32);
                break;

                case 
"K" :
                    
$celsius = 5/9 * ($temperature -32);
                    return 
$output = $celsius + 273.15;
                break;
            }
        }


        else if(
$from == "K") {
            switch(
$to) {
                case 
"C" :
                    return 
$output = $temperature - 273.15;
                break;

                case 
"F" :
                    
$celsius = $temperature - 273.15;
                    return 
$output = ($celsius / (5/9)) + 32;
                break;
            }
        }

    }



    public function 
loremIpsumGenerator($ammount, $type = "paras") {

        
/* Load the content of the XML link */
        
$loremIpsum = simplexml_load_file("http://www.lipsum.com/feed/xml?amount=" . $ammount . "&what=" . $type . "&start=0");

        
/* Return it to the user */
        
return $loremIpsum->lipsum;
        
    }



    public function 
getDomain($link) {

        
/* Removing http://, https://. www. from the link */
        
$link = preg_replace("/^(http://|https://)*(www.)*/is", "", $link);

        
/* Removing the suffix of the link e.g: /search-term/cow */
        
$link = preg_replace("//.*$/is" , "" ,$link);

        return 
$link;

    }



    public function 
getCurrentFileName($extension = true) {

        
/* Get the directory of the script + the name of the file
        and only return the string after the "/", which is the file name */
        
$fileName = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);

        
/* If $extension is true then we return the whole file name,
        else we only return the file name */
        
return ($extension) ? $fileName : preg_replace("/..*/", "", $fileName);

    }



    public function 
getFileExtension($string) {

        
/* Check if has a [dot] and return the string after the last dot */
        
return (strpos($string, ".")) ? @end(explode(".", $string)) : false;

    }



    public function 
generateSlug($string, $delimiter = "-") {

        
/* Convert accents characters */
        
$string = iconv("UTF-8", "ASCII//TRANSLIT", $string);

        
/* Replace all non words characters with the specified $delimiter */
        
$string = preg_replace("/W/", $delimiter, $string);

        
/* Check for double $delimiters and remove them so it only will be 1 delimiter */
        
$string = preg_replace("/-+/", "-", $string);

        
/* Remove the $delimiter character from the start and the end of the string */
        
$string = trim($string, $delimiter);

        
/* Make all the remaining words lowercase */
        
$string = strtolower($string);

        return 
$string;

    }



    public function 
generateReadableString($length = 8) {

        
/* Get all the vocals and consonants into separate arrays */
        
$consonants = array("b","c","d","f","g","h","j","k","l","m","n","p","r","s","t","v","w","x","y","z");
        
$vocals = array("a","e","i","o","u");

        
/* Initiate the output string*/
        
$output = "";

        
/* Start with consonant or vocal 0 = consonant , 1 = vocal*/
        
$start = rand(0, 1);

        
/* Add a consonant and a vocal until the length of the string is $length */
        
for($i=1; $i<=ceil($length/2); $i++) {

            
/* If start is == 0 then start with a consonant, else start with a vocal */
            
if($start == 0) {
                
$output .= $consonants[rand(0, 19)];
                
$output .= $vocals[rand(0, 4)];
            } else {
                
$output .= $vocals[rand(0, 4)];
                
$output .= $consonants[rand(0, 19)];
            }

        }

        return 
$output;

    }



    public function 
shortenUrl($url, $method = "googl") {

        
/* Check if cURL function is available */
        
self::checkCURL();

        
/* Initiate the CURL and the timeout */
        
$curl = curl_init();
        
$timeout = 3;


        
/* If the method is goo.gl then we use the goo.gl api */
        
if($method == "googl") {

            
/* Declare the json variable */
            
$json = '{"longUrl": "' . $url . '"}';

            
/* Set some options for cURL */
            
curl_setopt_array($curl, array(
                
CURLOPT_URL => "https://www.googleapis.com/urlshortener/v1/url",
                
CURLOPT_RETURNTRANSFER => 1,
                
CURLOPT_POST => 1,
                
CURLOPT_POSTFIELDS => $json,
                
CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
                
CURLOPT_CONNECTTIMEOUT => $timeout
            
));

            
/* Execute the cURL */
            
$data = json_decode(curl_exec($curl));

            return (
$data != false || $data != NULL) ? $data->id : false;
        } 


        
/* If the method is tinyurl then we use the tinyurl api */
        
else if($method == "tinyurl") {

            
/* Set some options for cURL */
            
curl_setopt_array($curl, array(
                
CURLOPT_URL => "http://tinyurl.com/api-create.php?url=" . $url,
                
CURLOPT_RETURNTRANSFER => 1,
                
CURLOPT_CONNECTTIMEOUT => $timeout
            
));

            
/* Execute the cURL */
            
$data = curl_exec($curl);

            return 
$data;
        }

    }



    public function 
convertVideoLinks($string, $width = 400, $height = 250) {

        
/* Validate the youtube url and replace it with the iframe */
        
$string = preg_replace(
            
"/(http://|https://)?(www.)?(youtube.com){1}(/watch?v=){1}([a-zA-Z0-9-_]+)/",
            
'<p><iframe width="' . $width . '" height="' . $height . '" src="//www.youtube.com/embed/$5" frameborder="0" allowfullscreen></iframe></p>', 
            
$string
        
);

        
/* Validate the vimeo url and replace it with the iframe */
        
$string = preg_replace(
            
"/(http://|https://)?(www.)?(vimeo.com){1}(/){1}([0-9]+)/",
            
'<p><iframe width="' . $width . '" height="' . $height . '" src="//player.vimeo.com/video/$5" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></p>', 
            
$string
        
);

        return 
$string;

    }



    public function 
youtubeVideo($url, $width = 400, $height = 250, $theme = "dark") {

        
/* Validate the youtube url and replace it with the iframe */
        
$output = preg_replace(
            
"/(http://|https://)?(www.)?(youtube.com){1}(/watch?v=){1}([a-zA-Z0-9-_]+)/",
            
'<p><iframe width="' . $width . '" height="' . $height . '" src="//www.youtube.com/embed/$5?theme='. $theme . '" frameborder="0" allowfullscreen></iframe></p>', 
            
$url
        
);

        return 
$output;

    }



    public function 
parseYoutubeVideo($url) {

        
/*  Validate the youtube url and get the ID of the video */
        
preg_match("/(http://|https://)?(www.)?(youtube.com){1}(/watch?v=){1}([a-zA-Z0-9-_]+)/", $url, $match);

        
/* Parse the video data from the youtube api */
        
$data = simplexml_load_file("http://gdata.youtube.com/feeds/api/videos/" . $match[5]);

        return array(
            
"title"            => (string) $data->title,
            
"description"    => (string) $data->content,
            
"published"        => (string) $data->published,
            
"author"        => (string) $data->author->name
        
);
    }



    public function 
vimeoVideo($url, $width = 400, $height = 250, $color = "00adef", $autoplay = false, $loop = false) {

        
/* Initiate the $autoplay and $loop variables (transform them) */
        
$autoplay = ($autoplay == true) ? 1 : 0;
        
$loop = ($loop == true) ? 1 : 0;

        
/* Validate the vimeo url and replace it with the iframe */
        
$output = preg_replace(
            
"/(http://|https://)?(www.)?(vimeo.com){1}(/){1}([0-9]+)/",
            
'<p><iframe width="' . $width . '" height="' . $height . '" src="//player.vimeo.com/video/$5?color=' . $color . '&amp;autoplay=' . $autoplay . '&amp;loop=' . $loop . '" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></p>', 
            
$url
        
);

        return 
$output;

    }



    public function 
parseVimeoVideo($id) {

        
/* If its a link to the video then extract the id from the link */
        
if(!is_numeric($id)) {

            
$id = preg_replace(
                
"/(http://|https://)?(www.)?(vimeo.com){1}(/){1}([0-9]+)/",
                
'$5',
                
$id
            
);

        }

        
/* Parse the xml file */
        
$output = simplexml_load_file("http://vimeo.com/api/v2/video/" . $id . ".xml");

        
/* Transform into an array */
        
$output = (array) $output->video;
        
        return 
$output;

    }



    public function 
getGravatar($email, $image= false, $size = 80, $default = "monsterid", $rating = "g") {

        
/* MD5 the email because gravatar needs the email to be md5'd */
        
$email = md5(strtolower(trim($email)));

        
/* Parameters for outputting the image */
        
$parameters = "?s=" . $size . "&d=" . $default . "&r=" . $rating;

        
/* Generating the Url */
        
$url = "http://gravatar.com/avatar/" . $email . $parameters;

        
/* If image is true then return the image directly, else return just the url */
        
return ($image == false) ? $url : "<img src="" . $url . "" />";

    } 



    public function 
generatePassword($length = 8, $uppercase = true, $numbers = true, $symbols = false) {

        
/* Initiating the possibilities variable depending on the parameters */
        
$possibilities = "abcdefghijklmnopqrstuvwxyz";
        
$possibilities .= ($uppercase == true) ? "ABCDEFGHIJKLMNOPQRSTUVWXYZ" : "";
        
$possibilities .= ($numbers == true)   ? "123456789" : "";
        
$possibilities .= ($symbols == true)   ? "!@#$%^&*" : "";

        
/* Initiating the password variable */
        
$password = "";

        
/* Concatonating random characters from the possibilities variable */
        
for($i=1;$i <= $length; $i++) {
            
$password .= $possibilities[rand(0, strlen($possibilities) - 1)];
        }

        return 
$password;

    }



    public function 
getAlexaRank($url) {

        
/* Parse the XML file */
        
$data = simplexml_load_file("http://data.alexa.com/data?cli=10&dat=snbamz&url=" . $url);

        
/* If url is not valid, return false, else return the alexa rank */
        
return ($data["URL"] == "404") ? false : $data->SD[1]->POPULARITY["TEXT"];

    }



    public function 
generateQR($data, $width = 150, $height = 150) {

        return 
'<img src="https://api.qrserver.com/v1/create-qr-code/?size=' . $width . 'x' . $height . '&data=' . $data . '" />';

    }



    public function 
readQR($url) {

        
/* Firstly we need to URL encode it, cause thats how the API works */
        
$url = urlencode($url);

        
/* Parse the results that the api sends */
        
$data = file_get_contents("http://api.qrserver.com/v1/read-qr-code/?fileurl=" . $url);
        
$data = json_decode($data);

        
/* If there is no error, display the data */
        
return (is_null($data[0]->symbol[0]->error)) ? $data[0]->symbol[0]->data : $data[0]->symbol[0]->error;

    } 



    public function 
getWeather($location, $degreeType = "C") {

        
/* Parse the XML weather file from MSN */
        
$data = simplexml_load_file("http://weather.service.msn.com/data.aspx?weadegreetype=" . $degreeType . "&culture=en-US&weasearchstr=" . $location);

        
/* Create an array with some of the parsed data */
        
$weatherData = array(
            
"temperature" => (string) $data->weather[0]->current['temperature'],
            
"sky"          => (string) $data->weather[0]->current['skytext'],
            
"humidity"      => (string) $data->weather[0]->current['humidity'],
            
"wind"          => (string) $data->weather[0]->current['winddisplay'],
        );

        return 
$weatherData;

    }



    public function 
getFacebookStats($url) {

        
/* The query that we're gonna send to facebook */
        
$query = "select like_count, comment_count, share_count from link_stat where url='" . $url . "'";

        
/* Parse the XML data */
        
$data = simplexml_load_file("https://api.facebook.com/method/fql.query?query=" . rawurlencode($query) . "&format=xml");

        return array(
            
"likes"    => (string) $data->link_stat->like_count,
            
"comments" => (string) $data->link_stat->comment_count,
            
"shares"   => (string) $data->link_stat->share_count
        
);

    }



    public function 
getTweetCount($url) {

        
/* Parse JSON data */
        
$data = file_get_contents("http://urls.api.twitter.com/1/urls/count.json?url=" . $url);

        
/* Decode JSON code */
        
$data = json_decode($data);

        return 
$data->count;

    } 



    public function 
getLinkedinCount($url) {

        
/* Parse the LinkedIn api */
        
$data = file_get_contents("http://www.linkedin.com/countserv/count/share?url=" . $url .  "&format=json");

        
/* Decode the json */
        
$data = json_decode($data);

        return 
$data->count;

    }

    public function 
getPinterestCount($url) {

        
/* Parse the Pinterest api */
        
$data = file_get_contents("http://api.pinterest.com/v1/urls/count.json?url=" . $url);

        
/* Get the count with regex */
        
preg_match("/"count": ([0-9]+)/", $data, $output);

        return 
$output[1];

    }



    public function 
KeyGen($length = 4, $times = 4, $separator = "-") {

        
/* Initiating the possibilities variable */
        
$possibilities = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        
/* Initiating the variable we're gonna return later */
        
$key = "";

        
/* Concatonating random characters until we meet the $length and $times */
        
for($i = 1; $i <= $times; $i++) {
            for(
$j = 1; $j <= $length; $j++) {
                
$key .= $possibilities[rand(0, 34)];
            }
            
$key .= $separator;
        }

        return 
substr($key, 0, -1);

    }



    public function 
encryptPassword($password, $method = "sha512", $iteration = 100, $salt = false) {

        
/* Concatonate the salt if there is any salt */
        
$password = ($salt == false) ? $password : $salt . $password;

        
/* Hashing the password */
        
$password = hash($method, $password);

        
/* If iteration is not false then we iterate it by $iteration times */
        
if($iteration !== false) {
            for(
$i = 1;$i <= $iteration; $i++) {
                
$password = hash($method, $password);
            }
        }

        return 
$password;

    }



    public function 
bbcode($string) {

        
$input = array(
                    
'/[b](.*?)[/b]/is',
                    
'/[i](.*?)[/i]/is',
                    
'/[u](.*?)[/u]/is',
                    
'/[img](.*?)[/img]/is',
                    
'/[li](.*?)[/li]/is',
                    
'/[url=(.*?)](.*?)[/url]/is'
                
);

        
$output = array(
                    
'<strong>$1</strong>',
                    
'<em>$1</em>',
                    
'<u>$1</u>',
                    
'<img src="$1" />',
                    
'<li>$1</li>',
                    
'<a href="$1">$2</a>'
                
);

        
$string = preg_replace ($input, $output, $string);

        return 
$string;

    }



    public function 
ipToCountry($ip) {

        
/* Parse the data from WIPmania api */
        
$data = file_get_contents("http://api.wipmania.com/" . $ip);

        return 
$data;

    }



    public function 
checkStatus($ip, $port) {

        
/* Try and connect through a socket */
        
$socket = @fsockopen($ip, $port, $errno, $errstr, 3);

        return (
$socket !== false) ? true : false ;

    }



    public function 
censorWords($string, $words, $replacement = "*") {

        foreach(
$words as $word) {
            
$string = str_replace($word, str_repeat($replacement, strlen($word)), $string);
        }

        return 
$string;
    }



    public function 
validateIp($ip) {

        return (
preg_match("/[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/", $ip) !== 0) ? true : false ;

    }

/* 

Name: Grohs Fabian
Portfolio: codecanyon.net/user/neeesteea
Skpe: neeesteea.soda
Thanks for checking out my class ;)

THE END
*/

}

?>
Онлайн: 2
Реклама