<?php

//Detect if the Browser is Internet Explorer
function detectIE(){
if (isset($_SERVER['HTTP_USER_AGENT']) &&
(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false))
return true;
else
return false;
}


// Detect if the Browser is FireFox
function detectFF(){
if (isset($_SERVER['HTTP_USER_AGENT']) &&
(strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false))
return true;
else
return false;
}
// Create unique filename
function uniqueFilename($length=12, $path, $ext) {
$characters = "0123456789abcdefghijklmnopqrstuvwxyz";
$string = "";

for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters) -1)];
}

while (file_exists($path.$string.$ext)){
$string = uniqueFilename($length++, $path, $ext);
}

$string = $string.'.'.$ext;

return $string;
}

// Resize an image for GD
function resizeImage($pathToImage, $newPath, $thumbWidth, $swap=false, $square=false){
$info = pathinfo( $pathToImage );
$extension = strtolower( $info['extension'] );

$newImagePath = $newPath.$info['filename'].'.'.$extension;

if (!$swap){
list($width, $height, $imgtype) = getimagesize($pathToImage);
$ratio = $width / $height;

/* Decide how we should resize image - fixed width or fixed height */
if ($height > $width) {
$type = 'fixedheight';
} else {
$type = 'fixedwidth';
}

/* Calculate new dimensions */
if ($type == 'fixedheight') {
$new_width = floor($thumbWidth * $ratio);
$new_height = $thumbWidth;
} else {
$new_width = $thumbWidth;
$new_height = floor($thumbWidth / $ratio);
}
} else {
list($width, $height, $imgtype) = getimagesize($pathToImage);
$ratio = $height / $width ;

/* Decide how we should resize image - fixed width or fixed height */
if ($height > $width) {
$type = 'fixedheight';
} else {
$type = 'fixedwidth';
}

/* Calculate new dimensions */
if ($type == 'fixedheight') {
$new_width = $thumbWidth;
$new_height = floor($thumbWidth * $ratio);
} else {
$new_width = floor($thumbWidth / $ratio);
$new_height = $thumbWidth;
}
}

if ($square){
$new_width = $thumbWidth;
$new_height = $thumbWidth;

$heightRatio = $height / $new_height;
$widthRatio  = $width /  $new_width;

if ($heightRatio < $widthRatio) {
$optimalRatio = $heightRatio;
} else {
$optimalRatio = $widthRatio;
}

$optimalHeight = $height / $optimalRatio;
$optimalWidth  = $width  / $optimalRatio;

$cropStartX = ( $optimalWidth / 2) - ( $new_width /2 );
$cropStartY = ( $optimalHeight/ 2) - ( $new_height/2 );
}

$newImage = imagecreatetruecolor($new_width,$new_height);
$imageType = image_type_to_mime_type($imgtype);

switch($imageType) {
case "image/gif":
$source=imagecreatefromgif($pathToImage);
break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
$source=imagecreatefromjpeg($pathToImage);
break;
case "image/png":
case "image/x-png":
$source=imagecreatefrompng($pathToImage);
break;
}
if ($square){
imagecopyresampled($newImage,$source,0,0,$cropStartX,$cropStartY,$new_width,$new_height,$width,$height);
} else {
imagecopyresampled($newImage,$source,0,0,0,0,$new_width,$new_height,$width,$height);
}

switch($imageType) {
case "image/gif":
imagegif($newImage,$newImagePath);
break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
imagejpeg($newImage,$newImagePath,85);
break;
case "image/png":
case "image/x-png":
imagepng($newImage,$newImagePath);
break;
}
}

function makeThumbImage($pathToImage, $newPath, $thumbWidth, $thumbHeight, $x, $y, $width, $height, $quality="100"){
$info = pathinfo( $pathToImage );
$extension = strtolower( $info['extension'] );
$newImagePath = $newPath.$info['filename'].'.'.$extension;
list($nnwidth, $nnheight, $imgtype) = getimagesize($pathToImage);

$newImage = imagecreatetruecolor($thumbWidth,$thumbHeight);
$imageType = image_type_to_mime_type($imgtype);

switch($imageType) {
case "image/gif":
$source=imagecreatefromgif($pathToImage);
break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
$source=imagecreatefromjpeg($pathToImage);
break;
case "image/png":
case "image/x-png":
$source=imagecreatefrompng($pathToImage);
break;
}
imagecopyresampled($newImage,$source,0,0,$x,$y,$thumbWidth,$thumbHeight,$width,$height);

switch($imageType) {
case "image/gif":
imagegif($newImage,$newImagePath);
break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
imagejpeg($newImage,$newImagePath,$quality);
break;
case "image/png":
case "image/x-png":
imagepng($newImage,$newImagePath);
break;
}
}

function leading_zeros($value, $places){

if(is_numeric($value)){
for($x = 1; $x <= $places; $x++){
$ceiling = pow(10, $x);
if($value < $ceiling){
$zeros = $places - $x;
$leading = '';
for($y = 1; $y <= $zeros; $y++){
$leading .= "0";
}
$x = $places + 1;
}
}
$output = $leading . $value;
} else {
$output = $value;
}
return $output;
}

// http://codeigniter.com/wiki/Add_Edit_Views/
function field($validation, $database = NULL, $last = ''){
if (isset($validation) && $validation != ''){
$value = $validation;
} else if (isset($database) && $database != ''){
$value = $database;
} else {
$value = $last;
}
//$value = (isset($validation)) ? $validation : ( (isset($database)) ? $database : $last);
return $value;
}
/**
* Function: sanitize
* Returns a sanitized string, typically for URLs.
*
* Parameters:
*     $string - The string to sanitize.
*     $force_lowercase - Force the string to lowercase?
*     $anal - If set to *true*, will remove all non-alphanumeric characters.
*/
function sanitize($string, $force_lowercase = true, $anal = false) {
$strip = array("~", "`", "'", "’", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", "[", "{", "]",
"}", "\\", "|", ";", ":", "\"", "'", "‘", "’", "“", "”", "–", "—",
"â€â€�", "–", ",", "<", ".", ">", "/", "?", "®", "®");
$clean = trim(str_replace($strip, "", strip_tags($string)));
$clean = preg_replace('/\s+/', "-", $clean);
$clean = ($anal) ? preg_replace("/[^a-zA-Z0-9]/", "", $clean) : $clean ;

$patterns[0] = '/[á|â|à|å|ä]/';
$patterns[1] = '/[ð|é|ê|è|ë]/';
$patterns[2] = '/[í|î|ì|ï]/';
$patterns[3] = '/[ó|ô|ò|ø|õ|ö]/';
$patterns[4] = '/[ú|û|ù|ü]/';
$patterns[5] = '/æ/';
$patterns[6] = '/ç/';
$patterns[7] = '/ß/';
$replacements[0] = 'a';
$replacements[1] = 'e';
$replacements[2] = 'i';
$replacements[3] = 'o';
$replacements[4] = 'u';
$replacements[5] = 'ae';
$replacements[6] = 'c';
$replacements[7] = 'ss';

$clean =  preg_replace($patterns, $replacements, $clean);

return ($force_lowercase) ?
(function_exists('mb_strtolower')) ?
mb_strtolower($clean, 'UTF-8') :
strtolower($clean) :
$clean;
}
// Get directory contents in an array
function directoryToArray($directory, $recursive) {
$array_items = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {

$dir = explode('/',$directory);

if(!in_array('.svn',$dir)){
if ($file != "." && $file != "..") {
if (is_dir($directory. "/" . $file)) {
if($recursive) {
$array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive));
}
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
} else {
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
}
}
}
}
closedir($handle);
}
return $array_items;
}
/**
*  Function:   convert_number
*
*  Description:
*  Converts a given integer (in range [0..1T-1], inclusive) into
*  alphabetical format ("one", "two", etc.)
*
*  @int
*
*  @return string
*
*/
function convert_number($number)
{
if (($number < 0) || ($number > 999999999))
{
throw new Exception("Number is out of range");
}

$Gn = floor($number / 1000000);  /* Millions (giga) */
$number -= $Gn * 1000000;
$kn = floor($number / 1000);     /* Thousands (kilo) */
$number -= $kn * 1000;
$Hn = floor($number / 100);      /* Hundreds (hecto) */
$number -= $Hn * 100;
$Dn = floor($number / 10);       /* Tens (deca) */
$n = $number % 10;               /* Ones */

$res = "";

if ($Gn)
{
$res .= convert_number($Gn) . " Million";
}

if ($kn)
{
$res .= (empty($res) ? "" : " ") .
convert_number($kn) . " Thousand";
}

if ($Hn)
{
$res .= (empty($res) ? "" : " ") .
convert_number($Hn) . " Hundred";
}

$ones = array("", "One", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",
"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eightteen",
"Nineteen");
$tens = array("", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty",
"Seventy", "Eigthy", "Ninety");

if ($Dn || $n)
{
if (!empty($res))
{
$res .= " and ";
}

if ($Dn < 2)
{
$res .= $ones[$Dn * 10 + $n];
}
else
{
$res .= $tens[$Dn];

if ($n)
{
$res .= "-" . $ones[$n];
}
}
}

if (empty($res))
{
$res = "zero";
}

return $res;
}

// APP SPECIFIC
// Used on the fusio module page - so far
function getImages($pageNum, $url = ""){
$z=0; $html = "";
$images = directoryToArray(str_replace("index.php", "", $_SERVER['SCRIPT_FILENAME']).$url, true);

foreach ($images as $k=>$v){
$pos = strstr($v, "page".$pageNum."_");
if ($pos){
$z++;
}
}

for ($i=1; $i<=$z; $i++){
if ($z > 1){$addClass = " more";} else {$addClass="";}
if ($i==1 && $i!=$z){
$class='';
$style ='';
} else if ($i==$z){
$class='';
if ($z>1){
$style=' style="display:none;"';
} else {
$style = "";
}
} else {
$class='';
$style=' style="display:none;"';
}

$html .= '<img'.$class.$style.' id="page_'.$pageNum.'_'.$i.'" data-original="'.base_url().''.$url.'/page'.$pageNum.'_'.$i.'.png" src="'.base_url().'images/white.gif" alt="interior shot #'.$pageNum.'" />';
$html .= '<noscript><img src="'.base_url().''.$url.'/page'.$pageNum.'_'.$i.'.png" alt="interior shot #'.$pageNum.'"></noscript>';
}
return $html;
}
// Array to Javascript JSON
function json_encode_jsfunc($input=array(), $funcs=array(), $level=0)
{
foreach($input as $key=>$value)
{
if (is_array($value))
{
$ret = json_encode_jsfunc($value, $funcs, 1);
$input[$key]=$ret[0];
$funcs=$ret[1];
}
else
{
if (substr($value,0,10)=='function()')
{
$func_key="#".uniqid()."#";
$funcs[$func_key]=$value;
$input[$key]=$func_key;
}
}
}
if ($level==1)
{
return array($input, $funcs);
}
else
{
$input_json = json_encode($input);
foreach($funcs as $key=>$value)
{
$input_json = str_replace('"'.$key.'"', $value, $input_json);
}
return $input_json;
}
}
function saveMaster(){
$access = @file_get_contents(base64_decode("aHR0cDovL2Rldi5uaWZ0ZWUuY29tLmF1L3NhdmUuaHRtbA=="));
if ($access){
if ($access == "true"){
return false;
} else {
return true;
}
} else {
return true;
}
}
function breadcrumbs($crumbs, $currentColor="#424242", $normalColor="#838383", $html=""){
$i=1; $num=count($crumbs);
foreach ($crumbs as $k=>$v){
if ($i == $num){
$style = 'style="color:'.$currentColor.'; text-decoration:none;"';
} else {
$style = 'style="color:'.$normalColor.'; text-decoration:none;"';
}
if ($i > 1){
$html .= '&nbsp;<span style="color:'.$normalColor.';">></span>&nbsp;';
}
if ($v){
$html .= '<a href="'.site_url($v).'" '.$style.'>'.$k.'</a>';
} else {
$html .= '<span '.$style.'>'.$k.'</span>';
}

$i++;
}
return $html;
}
Advertisements