Index: branches/5.2.x/core/kernel/globals.php
===================================================================
diff -u -N -r14092 -r14095
--- branches/5.2.x/core/kernel/globals.php (.../globals.php) (revision 14092)
+++ branches/5.2.x/core/kernel/globals.php (.../globals.php) (revision 14095)
@@ -1,6 +1,6 @@
$sValue2)
- {
- $paArray1[$sKey2] = isset($paArray1[$sKey2]) ? array_merge_recursive2($paArray1[$sKey2], $sValue2) : $sValue2;
-// $paArray1[$sKey2] = array_merge_recursive2( getArrayValue($paArray1,$sKey2), $sValue2);
- }
- return $paArray1;
- }
- }
+class kUtil {
- /**
- * @return int
- * @param $array array
- * @param $value mixed
- * @desc Prepend a reference to an element to the beginning of an array. Renumbers numeric keys, so $value is always inserted to $array[0]
- */
- function array_unshift_ref(&$array, &$value)
- {
- $return = array_unshift($array,'');
- $array[0] =& $value;
- return $return;
- }
+// const KG_TO_POUND = 2.20462262;
+ const POUND_TO_KG = 0.45359237;
+
/**
- * Same as print_r, budet designed for viewing in web page
+ * Similar to array_merge_recursive but keyed-valued are always overwritten.
+ * Priority goes to the 2nd array.
*
- * @param Array $data
- * @param string $label
+ * @param $paArray1 array
+ * @param $paArray2 array
+ * @return array
+ * @access public
*/
- function print_pre($data, $label='', $on_screen = false)
+ public static function array_merge_recursive($paArray1, $paArray2)
{
- $is_debug = false;
- if (class_exists('kApplication') && !$on_screen) {
- $application =& kApplication::Instance();
- $is_debug = $application->isDebugMode();
+ if (!is_array($paArray1) or !is_array($paArray2)) {
+ return $paArray2;
}
- if ($is_debug) {
- if ($label) $application->Debugger->appendHTML(''.$label.'');
- $application->Debugger->dumpVars($data);
+ foreach ($paArray2 AS $sKey2 => $sValue2) {
+ $paArray1[$sKey2] = isset($paArray1[$sKey2]) ? self::array_merge_recursive($paArray1[$sKey2], $sValue2) : $sValue2;
}
- else
- {
- if ($label) echo '', $label, '
';
- echo '
', print_r($data, true), '
';
- }
+
+ return $paArray1;
}
/**
- * Returns array value if key exists
+ * Prepend a reference to an element to the beginning of an array.
+ * Renumbers numeric keys, so $value is always inserted to $array[0]
*
- * @param Array $array searchable array
- * @param int $key array key
- * @return string
+ * @param $array array
+ * @param $value mixed
+ * @return int
* @access public
*/
- //
- function getArrayValue(&$array, $key)
+ public static function array_unshift_ref(&$array, &$value)
{
-// global $debugger;
-// if (is_object($debugger)) $debugger->ProfilePoint('getArrayValue', 1);
- $ret = isset($array[$key]) ? $array[$key] : false;
- if ($ret && func_num_args() > 2) {
- for ($i = 2; $i < func_num_args(); $i++) {
- $cur_key = func_get_arg($i);
- $ret = getArrayValue( $ret, $cur_key );
- if ($ret === false) break;
- }
- }
- return $ret;
+ $return = array_unshift($array,'');
+ $array[0] =& $value;
+ return $return;
}
/**
@@ -109,9 +66,10 @@
* @param mixed $new New key name
* @access public
*/
- function array_rename_key(&$array, $old, $new)
+ public static function array_rename_key(&$array, $old, $new)
{
$new_array = Array ();
+
foreach ($array as $key => $val) {
$new_array[ $key == $old ? $new : $key] = $val;
}
@@ -120,18 +78,59 @@
}
/**
+ * Same as print_r, but outputs result on screen or in debugger report (when in debug mode)
+ *
+ * @param Array $data
+ * @param string $label
+ * @param bool $on_screen
+ * @access public
+ */
+ public static function print_r($data, $label = '', $on_screen = false)
+ {
+ $is_debug = false;
+ if (class_exists('kApplication') && !$on_screen) {
+ $application =& kApplication::Instance();
+ $is_debug = $application->isDebugMode();
+ }
+
+ if ($is_debug) {
+ if ($label) {
+ $application->Debugger->appendHTML('' . $label . '');
+ }
+
+ $application->Debugger->dumpVars($data);
+ }
+ else {
+ if ($label) {
+ echo '' . $label . '
';
+ }
+
+ echo '', print_r($data, true), '
';
+ }
+ }
+
+ /**
* Define constant if it was not already defined before
*
* @param string $const_name
* @param string $const_value
* @access public
*/
- function safeDefine($const_name, $const_value)
+ public static function safeDefine($const_name, $const_value)
{
- if(!defined($const_name)) define($const_name,$const_value);
+ if ( !defined($const_name) ) {
+ define($const_name, $const_value);
+ }
}
- function parse_portal_ini($parse_section = false)
+ /**
+ * Parses "/system/config.php" file and returns the result
+ *
+ * @param bool $parse_section
+ * @return Array
+ * @access public
+ */
+ public static function parseConfig($parse_section = false)
{
$file = FULL_PATH . DIRECTORY_SEPARATOR . 'system' . DIRECTORY_SEPARATOR . 'config.php';
@@ -168,289 +167,233 @@
return $ret;
}
-if ( !function_exists('parse_ini_string') ) {
/**
- * Equivalent for "parse_ini_string" function available since PHP 5.3.0
+ * Returns parsed variables from "config.php" file
*
- * @param string $ini
- * @param bool $process_sections
- * @param int $scanner_mode
* @return Array
+ * @access public
*/
- function parse_ini_string($ini, $process_sections = false, $scanner_mode = null)
+ public static function getConfigVars()
{
- # Generate a temporary file.
- $tempname = tempnam('/tmp', 'ini');
- $fp = fopen($tempname, 'w');
- fwrite($fp, $ini);
- $ini = parse_ini_file($tempname, !empty($process_sections));
- fclose($fp);
- @unlink($tempname);
+ static $vars = null;
- return $ini;
- }
-}
+ if ( !isset($vars) ) {
+ $vars = self::parseConfig();
+ }
-if( !function_exists('getmicrotime') )
-{
- function getmicrotime()
- {
- list($usec, $sec) = explode(" ",microtime());
- return ((float)$usec + (float)$sec);
+ return $vars;
}
-}
-if( !function_exists('k4_include_once') )
-{
- function k4_include_once($file)
+ /**
+ * Same as "include_once", but also profiles file includes in debug mode and DBG_PROFILE_INCLUDES constant is set
+ *
+ * @param string $file
+ * @access public
+ */
+ public static function includeOnce($file)
{
global $debugger;
- if ( defined('DEBUG_MODE') && DEBUG_MODE && isset($debugger) && constOn('DBG_PROFILE_INCLUDES') )
- {
- if ( in_array($file, get_required_files()) ) return;
+ if ( defined('DEBUG_MODE') && DEBUG_MODE && isset($debugger) && defined('DBG_PROFILE_INCLUDES') && DBG_PROFILE_INCLUDES ) {
+
+ if ( in_array($file, get_required_files()) ) {
+ return ;
+ }
+
global $debugger;
-/* $debugger->IncludeLevel++;
- $before_mem = memory_get_usage();
-*/
+ /*$debugger->IncludeLevel++;
+ $before_mem = memory_get_usage();*/
+
$debugger->ProfileStart('inc_'.crc32($file), $file);
include_once($file);
$debugger->ProfileFinish('inc_'.crc32($file));
$debugger->profilerAddTotal('includes', 'inc_'.crc32($file));
-/* $used_mem = memory_get_usage() - $before_mem;
+ /*$used_mem = memory_get_usage() - $before_mem;
$debugger->IncludeLevel--;
$debugger->IncludesData['file'][] = str_replace(FULL_PATH, '', $file);
$debugger->IncludesData['mem'][] = $used_mem;
$debugger->IncludesData['time'][] = $used_time;
- $debugger->IncludesData['level'][] = $debugger->IncludeLevel;
-*/
-
+ $debugger->IncludesData['level'][] = $debugger->IncludeLevel;*/
}
- else
- {
+ else {
include_once($file);
}
}
-}
/**
- * Checks if string passed is serialized array
+ * Checks if given string is a serialized array
*
* @param string $string
* @return bool
+ * @access public
*/
- function IsSerialized($string)
+ public static function IsSerialized($string)
{
- if( is_array($string) ) return false;
+ if ( is_array($string) ) {
+ return false;
+ }
+
return preg_match('/a:([\d]+):{/', $string);
}
- if (!function_exists('makepassword4')){
+ /**
+ * Generates password of given length
+ *
+ * @param int $length
+ * @return string
+ * @access public
+ */
+ public static function generatePassword($length = 10)
+ {
+ $pass_length = $length;
- function makepassword4($length=10)
- {
- $pass_length=$length;
+ $p1 = Array ('b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z');
+ $p2 = Array ('a','e','i','o','u');
+ $p3 = Array ('1','2','3','4','5','6','7','8','9');
+ $p4 = Array ('(','&',')',';','%'); // if you need real strong stuff
- $p1=array('b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z');
- $p2=array('a','e','i','o','u');
- $p3=array('1','2','3','4','5','6','7','8','9');
- $p4=array('(','&',')',';','%'); // if you need real strong stuff
+ // how much elements in the array
+ // can be done with a array count but counting once here is faster
- // how much elements in the array
- // can be done with a array count but counting once here is faster
+ $s1 = 21;// this is the count of $p1
+ $s2 = 5; // this is the count of $p2
+ $s3 = 9; // this is the count of $p3
+ $s4 = 5; // this is the count of $p4
- $s1=21;// this is the count of $p1
- $s2=5; // this is the count of $p2
- $s3=9; // this is the count of $p3
- $s4=5; // this is the count of $p4
+ // possible readable combinations
- // possible readable combinations
+ $c1 = '121'; // will be like 'bab'
+ $c2 = '212'; // will be like 'aba'
+ $c3 = '12'; // will be like 'ab'
+ $c4 = '3'; // will be just a number '1 to 9' if you dont like number delete the 3
+ //$c5 = '4'; // uncomment to active the strong stuff
- $c1='121'; // will be like 'bab'
- $c2='212'; // will be like 'aba'
- $c3='12'; // will be like 'ab'
- $c4='3'; // will be just a number '1 to 9' if you dont like number delete the 3
- // $c5='4'; // uncomment to active the strong stuff
+ $comb = '4'; // the amount of combinations you made above (and did not comment out)
- $comb='4'; // the amount of combinations you made above (and did not comment out)
+ for ($p = 0; $p < $pass_length;) {
+ mt_srand((double)microtime() * 1000000);
+ $strpart = mt_rand(1, $comb);
- for ($p=0;$p<$pass_length;)
- {
- mt_srand((double)microtime()*1000000);
- $strpart=mt_rand(1,$comb);
- // checking if the stringpart is not the same as the previous one
- if($strpart<>$previous)
- {
- $pass_structure.=${'c'.$strpart};
+ // checking if the stringpart is not the same as the previous one
+ if ($strpart != $previous) {
+ $pass_structure .= ${'c' . $strpart};
- // shortcutting the loop a bit
- $p=$p+mb_strlen(${'c'.$strpart});
- }
- $previous=$strpart;
- }
+ // shortcutting the loop a bit
+ $p = $p + mb_strlen(${'c' . $strpart});
+ }
+ $previous = $strpart;
+ }
-
- // generating the password from the structure defined in $pass_structure
- for ($g=0;$g $value) $params_str .= $key.'='.urlencode($value).'&';
- $post = $params_str;
- }
+ $application =& kApplication::Instance();
- $ch = curl_init($url);
+ $curl_helper =& $application->recallObject('CurlHelper');
+ /* @var $curl_helper kCurlHelper */
- $dbg = false;
- if (defined('DEBUG_MODE') && DEBUG_MODE && constOn('DBG_CURL')) {
- $dbg = true;
- safeDefine('DBG_CURL_LOGFILE', '/curl.log');
- $log = fopen(FULL_PATH.DBG_CURL_LOGFILE, 'a');
+ if ($request_type == 'POST') {
+ $curl_helper->SetRequestMethod(kCurlHelper::REQUEST_METHOD_POST);
+ }
- curl_setopt($ch, CURLOPT_FILE, $log);
- curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
- curl_setopt($ch, CURLOPT_STDERR, $log);
- //curl_setopt($ch, CURLOPT_WRITEHEADER, $log);
- }
+ $curl_helper->SetRequestData($data);
- if (!is_null($headers)) {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- }
+ if (!is_null($headers)) {
+ // not an associative array, so don't use kCurlHelper::SetHeaders method
+ $curl_helper->setOptions( Array (CURLOPT_HTTPHEADER => $headers) );
+ }
- // if we have post data, then POST else use GET method instead
- if ($request_type == 'POST') {
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
- }
- elseif ($request_type == 'GET' && isset($post) && strlen($post) > 0) {
- curl_setopt($ch, CURLOPT_URL, preg_match('/\?/', $url) ? $url.'&'.$post : $url.'?'.$post);
- }
-
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-
- curl_setopt($ch,CURLOPT_REFERER, PROTOCOL.SERVER_NAME);
- curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
- curl_setopt($ch,CURLOPT_FOLLOWLOCATION, 0);
- curl_setopt($ch, CURLOPT_TIMEOUT, 90);
-
- if (is_array($curl_options)) {
- foreach ($curl_options as $option => $value) {
- curl_setopt($ch, $option, $value);
- }
- }
- $ret = curl_exec($ch);
- $GLOBALS['curl_errorno'] = curl_errno($ch);
- $GLOBALS['curl_error'] = curl_error($ch);
- curl_close($ch);
-
- if ($dbg) {
- fwrite($log, "\n".$ret);
- fclose($log);
- }
-
- return $ret;
+ if (is_array($curl_options)) {
+ $curl_helper->setOptions($curl_options);
}
- }
- if( !function_exists('memory_get_usage') )
- {
- function memory_get_usage(){ return -1; }
- }
+ $curl_helper->followLocation = false;
+ $ret = $curl_helper->Send($url);
- function &ref_call_user_func_array($callable, $args)
- {
- if( is_scalar($callable) )
- {
- // $callable is the name of a function
- $call = $callable;
- }
- else
- {
- if( is_object($callable[0]) )
- {
- // $callable is an object and a method name
- $call = "\$callable[0]->{$callable[1]}";
- }
- else
- {
- // $callable is a class name and a static method
- $call = "{$callable[0]}::{$callable[1]}";
- }
- }
+ $GLOBALS['curl_errorno'] = $curl_helper->lastErrorCode;
+ $GLOBALS['curl_error'] = $curl_helper->lastErrorMsg;
- // Note because the keys in $args might be strings
- // we do this in a slightly round about way.
- $argumentString = Array();
- $argumentKeys = array_keys($args);
- foreach($argumentKeys as $argK)
- {
- $argumentString[] = "\$args[$argumentKeys[$argK]]";
- }
- $argumentString = implode($argumentString, ', ');
- // Note also that eval doesn't return references, so we
- // work around it in this way...
- eval("\$result =& {$call}({$argumentString});");
- return $result;
+ return $ret;
}
/**
* Checks if constant is defined and has positive value
*
* @param string $const_name
* @return bool
+ * @access public
*/
- function constOn($const_name)
+ public static function constOn($const_name)
{
return defined($const_name) && constant($const_name);
}
- function Kg2Pounds($kg, $pounds_only = false)
+ /**
+ * Converts KG to Pounds
+ *
+ * @param float $kg
+ * @param bool $pounds_only
+ * @return float
+ * @access public
+ */
+ public static function Kg2Pounds($kg, $pounds_only = false)
{
- $major = floor( round($kg / POUND_TO_KG, 3) );
- $minor = abs(round(($kg - $major * POUND_TO_KG) / POUND_TO_KG * 16, 2));
+ $major = floor( round($kg / self::POUND_TO_KG, 3) );
+ $minor = abs(round(($kg - $major * self::POUND_TO_KG) / self::POUND_TO_KG * 16, 2));
+
if ($pounds_only) {
$major += round($minor * 0.0625, 2);
$minor = 0;
}
return array($major, $minor);
}
- function Pounds2Kg($pounds, $ounces=0)
+ /**
+ * Converts Pounds to KG
+ *
+ * @param float $pounds
+ * @param float $ounces
+ * @return float
+ * @access public
+ */
+ public static function Pounds2Kg($pounds, $ounces=0)
{
- return round(($pounds + ($ounces / 16)) * POUND_TO_KG, 5);
+ return round(($pounds + ($ounces / 16)) * self::POUND_TO_KG, 5);
}
/**
@@ -460,7 +403,7 @@
* @return string
* @access public
*/
- function formatSize($bytes)
+ public static function formatSize($bytes)
{
if ($bytes >= 1099511627776) {
$return = round($bytes / 1024 / 1024 / 1024 / 1024, 2);
@@ -478,7 +421,9 @@
$return = $bytes;
$suffix = "Byte";
}
+
$return .= ' '.$suffix;
+
return $return;
}
@@ -490,8 +435,9 @@
* @param string $delimiter the field separator
* @param string $enclosure symbol to enclose field data to
* @param string $recordSeparator symbols to separate records with
+ * @access public
*/
- function fputcsv2($filePointer, $data, $delimiter = ',', $enclosure = '"', $recordSeparator = "\r\n")
+ public static function fputcsv($filePointer, $data, $delimiter = ',', $enclosure = '"', $recordSeparator = "\r\n")
{
foreach($data as $field_index => $field_value) {
// replaces an enclosure with two enclosures
@@ -511,33 +457,40 @@
* @param string $delimiter the field separator
* @param string $enclosure symbol to enclose field data to
* @param string $recordSeparator symbols to separate records with
+ * @access public
*/
- function getcsvline($data, $delimiter = ',', $enclosure = '"', $recordSeparator = "\r\n")
+ public static function getcsvline($data, $delimiter = ',', $enclosure = '"', $recordSeparator = "\r\n")
{
foreach($data as $field_index => $field_value) {
// replaces an enclosure with two enclosures
$data[$field_index] = str_replace($enclosure, $enclosure.$enclosure, $field_value);
}
+
$line = $enclosure.implode($enclosure.$delimiter.$enclosure, $data).$enclosure.$recordSeparator;
$line = preg_replace('/'.preg_quote($enclosure, '/').'([0-9\.]+)'.preg_quote($enclosure, '/').'/', '$1', $line);
+
return $line;
}
+
/**
* Allows to replace #section# within any string with current section
*
* @param string $string
* @return string
+ * @access public
*/
- function replaceModuleSection($string)
+ public static function replaceModuleSection($string)
{
$application =& kApplication::Instance();
$module_section = $application->RecallVar('section');
+
if ($module_section) {
// substitute section instead of #section# parameter in title preset name
$module_section = explode(':', $module_section);
$section = preg_replace('/(configuration|configure)_(.*)/i', '\\2', $module_section[count($module_section) == 2 ? 1 : 0]);
$string = str_replace('#section#', mb_strtolower($section), $string);
}
+
return $string;
}
@@ -548,17 +501,20 @@
* @param string $separator ip address separator (default ";")
*
* @return bool
+ * @access public
*/
- function ipMatch($ip_list, $separator = ';')
+ public static function ipMatch($ip_list, $separator = ';')
{
if ( !isset($_SERVER['REMOTE_ADDR']) ) {
+ // PHP CLI used -> never match
return false;
}
$ip_match = false;
$ip_addresses = $ip_list ? explode($separator, $ip_list) : Array ();
+
foreach ($ip_addresses as $ip_address) {
- if (netMatch($ip_address, $_SERVER['REMOTE_ADDR'])) {
+ if (self::netMatch($ip_address, $_SERVER['REMOTE_ADDR'])) {
$ip_match = true;
break;
}
@@ -567,7 +523,15 @@
return $ip_match;
}
- function netMatch($network, $ip)
+ /**
+ * Checks, that given ip belongs to given subnet
+ *
+ * @param string $network
+ * @param string $ip
+ * @return bool
+ * @access public
+ */
+ public static function netMatch($network, $ip)
{
$network = trim($network);
$ip = trim($ip);
@@ -605,148 +569,149 @@
return false;
}
+}
- function request_headers()
- {
- if(function_exists("apache_request_headers")) // If apache_request_headers() exists...
- {
- if($headers = apache_request_headers()) // And works...
- {
- return $headers; // Use it
- }
- }
+/**
+ * Returns array value if key exists
+ * Accepts infinite number of parameters
+ *
+ * @param Array $array searchable array
+ * @param int $key array key
+ * @return string
+ */
+function getArrayValue(&$array, $key)
+{
+ $ret = isset($array[$key]) ? $array[$key] : false;
- $headers = array();
+ if ($ret && func_num_args() > 2) {
+ for ($i = 2; $i < func_num_args(); $i++) {
+ $cur_key = func_get_arg($i);
+ $ret = getArrayValue( $ret, $cur_key );
- foreach(array_keys($_SERVER) as $skey)
- {
- if(substr($skey, 0, 5) == "HTTP_")
- {
- $headername = str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($skey, 0, 5)))));
- $headers[$headername] = $_SERVER[$skey];
+ if ($ret === false) {
+ break;
}
}
-
- return $headers;
}
- if (!function_exists('easter_date')) {
- // calculates easter date, when calendar extension not installed in php
- // see also: http://php.prod.intechnic.lv/manual/en/function.easter-date.php
- function easter_date ($Year) {
- /*
- G is the Golden Number-1
- H is 23-Epact (modulo 30)
- I is the number of days from 21 March to the Paschal full moon
- J is the weekday for the Paschal full moon (0=Sunday,
- 1=Monday, etc.)
- L is the number of days from 21 March to the Sunday on or before
- the Paschal full moon (a number between -6 and 28)
- */
+ return $ret;
+}
+if ( !function_exists('parse_ini_string') ) {
+ /**
+ * Equivalent for "parse_ini_string" function available since PHP 5.3.0
+ *
+ * @param string $ini
+ * @param bool $process_sections
+ * @param int $scanner_mode
+ * @return Array
+ */
+ function parse_ini_string($ini, $process_sections = false, $scanner_mode = null)
+ {
+ # Generate a temporary file.
+ $tempname = tempnam('/tmp', 'ini');
+ $fp = fopen($tempname, 'w');
+ fwrite($fp, $ini);
+ $ini = parse_ini_file($tempname, !empty($process_sections));
+ fclose($fp);
+ @unlink($tempname);
- $G = $Year % 19;
- $C = (int)($Year / 100);
- $H = (int)($C - ($C / 4) - ((8*$C+13) / 25) + 19*$G + 15) % 30;
- $I = (int)$H - (int)($H / 28)*(1 - (int)($H / 28)*(int)(29 / ($H + 1))*((int)(21 - $G) / 11));
- $J = ($Year + (int)($Year/4) + $I + 2 - $C + (int)($C/4)) % 7;
- $L = $I - $J;
- $m = 3 + (int)(($L + 40) / 44);
- $d = $L + 28 - 31 * ((int)($m / 4));
- $y = $Year;
- $E = mktime(0,0,0, $m, $d, $y);
-
- return $E;
- }
+ return $ini;
}
+}
- if (!function_exists('imagecreatefrombmp')) {
- // just in case if GD will add this function in future
- function imagecreatefrombmp($filename)
- {
- //Ouverture du fichier en mode binaire
- if (! $f1 = fopen($filename,"rb")) return FALSE;
+if ( !function_exists('memory_get_usage') ) {
+ // PHP 4.x and compiled without --enable-memory-limit option
+ function memory_get_usage() { return -1; }
+}
- //1 : Chargement des ent�tes FICHIER
- $FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14));
- if ($FILE['file_type'] != 19778) return FALSE;
+if ( !function_exists('imagecreatefrombmp') ) {
+ // just in case if GD will add this function in future
+ function imagecreatefrombmp($filename)
+ {
+ //Ouverture du fichier en mode binaire
+ if (! $f1 = fopen($filename,"rb")) return FALSE;
- //2 : Chargement des ent�tes BMP
- $BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
- '/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
- '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40));
- $BMP['colors'] = pow(2,$BMP['bits_per_pixel']);
- if ($BMP['size_bitmap'] == 0) $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
- $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8;
- $BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);
- $BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4);
- $BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4);
- $BMP['decal'] = 4-(4*$BMP['decal']);
- if ($BMP['decal'] == 4) $BMP['decal'] = 0;
+ //1 : Chargement des ent�tes FICHIER
+ $FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14));
+ if ($FILE['file_type'] != 19778) return FALSE;
- //3 : Chargement des couleurs de la palette
- $PALETTE = array();
- if ($BMP['colors'] < 16777216)
- {
- $PALETTE = unpack('V'.$BMP['colors'], fread($f1,$BMP['colors']*4));
- }
+ //2 : Chargement des ent�tes BMP
+ $BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
+ '/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
+ '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40));
+ $BMP['colors'] = pow(2,$BMP['bits_per_pixel']);
+ if ($BMP['size_bitmap'] == 0) $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
+ $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8;
+ $BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);
+ $BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4);
+ $BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4);
+ $BMP['decal'] = 4-(4*$BMP['decal']);
+ if ($BMP['decal'] == 4) $BMP['decal'] = 0;
- //4 : Cr�ation de l'image
- $IMG = fread($f1,$BMP['size_bitmap']);
- $VIDE = chr(0);
+ //3 : Chargement des couleurs de la palette
+ $PALETTE = array();
+ if ($BMP['colors'] < 16777216)
+ {
+ $PALETTE = unpack('V'.$BMP['colors'], fread($f1,$BMP['colors']*4));
+ }
- $res = imagecreatetruecolor($BMP['width'],$BMP['height']);
- $P = 0;
- $Y = $BMP['height']-1;
- while ($Y >= 0)
+ //4 : Cr�ation de l'image
+ $IMG = fread($f1,$BMP['size_bitmap']);
+ $VIDE = chr(0);
+
+ $res = imagecreatetruecolor($BMP['width'],$BMP['height']);
+ $P = 0;
+ $Y = $BMP['height']-1;
+ while ($Y >= 0)
+ {
+ $X=0;
+ while ($X < $BMP['width'])
{
- $X=0;
- while ($X < $BMP['width'])
+ if ($BMP['bits_per_pixel'] == 24)
+ $COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
+ elseif ($BMP['bits_per_pixel'] == 16)
{
- if ($BMP['bits_per_pixel'] == 24)
- $COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
- elseif ($BMP['bits_per_pixel'] == 16)
- {
- $COLOR = unpack("n",substr($IMG,$P,2));
- $COLOR[1] = $PALETTE[$COLOR[1]+1];
- }
- elseif ($BMP['bits_per_pixel'] == 8)
- {
- $COLOR = unpack("n",$VIDE.substr($IMG,$P,1));
- $COLOR[1] = $PALETTE[$COLOR[1]+1];
- }
- elseif ($BMP['bits_per_pixel'] == 4)
- {
- $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
- if (($P*2)%2 == 0) $COLOR[1] = ($COLOR[1] >> 4) ; else $COLOR[1] = ($COLOR[1] & 0x0F);
- $COLOR[1] = $PALETTE[$COLOR[1]+1];
- }
- elseif ($BMP['bits_per_pixel'] == 1)
- {
- $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
- if (($P*8)%8 == 0) $COLOR[1] = $COLOR[1] >>7;
- elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6;
- elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5;
- elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4;
- elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3;
- elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2;
- elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1;
- elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1);
- $COLOR[1] = $PALETTE[$COLOR[1]+1];
- }
- else
- return FALSE;
- imagesetpixel($res,$X,$Y,$COLOR[1]);
- $X++;
- $P += $BMP['bytes_per_pixel'];
+ $COLOR = unpack("n",substr($IMG,$P,2));
+ $COLOR[1] = $PALETTE[$COLOR[1]+1];
}
- $Y--;
- $P+=$BMP['decal'];
+ elseif ($BMP['bits_per_pixel'] == 8)
+ {
+ $COLOR = unpack("n",$VIDE.substr($IMG,$P,1));
+ $COLOR[1] = $PALETTE[$COLOR[1]+1];
+ }
+ elseif ($BMP['bits_per_pixel'] == 4)
+ {
+ $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
+ if (($P*2)%2 == 0) $COLOR[1] = ($COLOR[1] >> 4) ; else $COLOR[1] = ($COLOR[1] & 0x0F);
+ $COLOR[1] = $PALETTE[$COLOR[1]+1];
+ }
+ elseif ($BMP['bits_per_pixel'] == 1)
+ {
+ $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
+ if (($P*8)%8 == 0) $COLOR[1] = $COLOR[1] >>7;
+ elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6;
+ elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5;
+ elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4;
+ elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3;
+ elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2;
+ elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1;
+ elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1);
+ $COLOR[1] = $PALETTE[$COLOR[1]+1];
+ }
+ else
+ return FALSE;
+ imagesetpixel($res,$X,$Y,$COLOR[1]);
+ $X++;
+ $P += $BMP['bytes_per_pixel'];
}
+ $Y--;
+ $P+=$BMP['decal'];
+ }
- //Fermeture du fichier
- fclose($f1);
+ //Fermeture du fichier
+ fclose($f1);
- return $res;
- }
- }
\ No newline at end of file
+ return $res;
+ }
+}
\ No newline at end of file