| version 1.4, 2003/01/13 12:17:53 |
version 1.5, 2003/01/13 12:43:33 |
|
|
| |
|
| class Platon |
class Platon |
| { |
{ |
| /** common/cgi/fnc.http_prefix_on_CGI_vars.php - adds 'http://' |
/** common/cgi/fnc.http_prefix_on_CGI_vars.php - adds 'http://' |
| * prefix on passed CGI variables if it is missing; accepts string |
* prefix on passed CGI variables if it is missing; accepts string |
| * or array of strings on input |
* or array of strings on input |
| */ |
*/ |
| function http_prefix_on_CGI_vars($param) /* {{{ */ |
function http_prefix_on_CGI_vars($param) /* {{{ */ |
| { |
{ |
| $ret = true; |
$ret = true; |
| if (is_string($param)) { |
if (is_string($param)) { |
| global $HTTP_GET_VARS; |
global $HTTP_GET_VARS; |
| global $HTTP_POST_VARS; |
global $HTTP_POST_VARS; |
| global $$param; |
global $$param; |
| |
|
| $$param = trim($$param); |
$$param = trim($$param); |
| if (! empty($$param)) { |
if (! empty($$param)) { |
| if (! preg_match('|^http[s]?://|', $$param)) |
if (! preg_match('|^http[s]?://|', $$param)) |
| $$param = 'http://' . $$param; |
$$param = 'http://' . $$param; |
| } |
} |
| $HTTP_GET_VARS[$param] = trim($HTTP_GET_VARS[$param]); |
$HTTP_GET_VARS[$param] = trim($HTTP_GET_VARS[$param]); |
| if (! empty($HTTP_GET_VARS[$param])) { |
if (! empty($HTTP_GET_VARS[$param])) { |
| if (! preg_match('|^http[s]?://|', $HTTP_GET_VARS[$param])) |
if (! preg_match('|^http[s]?://|', $HTTP_GET_VARS[$param])) |
| $HTTP_GET_VARS[$param] = |
$HTTP_GET_VARS[$param] = |
| 'http://' . $HTTP_GET_VARS[$param]; |
'http://' . $HTTP_GET_VARS[$param]; |
| } |
} |
| $HTTP_POST_VARS[$param] = trim($HTTP_POST_VARS[$param]); |
$HTTP_POST_VARS[$param] = trim($HTTP_POST_VARS[$param]); |
| if (! empty($HTTP_POST_VARS[$param])) { |
if (! empty($HTTP_POST_VARS[$param])) { |
| if (! preg_match('|^http[s]?://|', $HTTP_POST_VARS[$param])) |
if (! preg_match('|^http[s]?://|', $HTTP_POST_VARS[$param])) |
| $HTTP_POST_VARS[$param] = |
$HTTP_POST_VARS[$param] = |
| 'http://' . $HTTP_POST_VARS[$param]; |
'http://' . $HTTP_POST_VARS[$param]; |
| } |
} |
| } |
} |
| elseif (is_array($param)) { |
elseif (is_array($param)) { |
| foreach ($param as $var) { |
foreach ($param as $var) { |
| $ret = $ret && http_prefix_on_CGI_vars($var); |
$ret = $ret && http_prefix_on_CGI_vars($var); |
| } |
} |
| } |
} |
| else { |
else { |
| $ret = false; |
$ret = false; |
| } |
} |
| return $ret; |
return $ret; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** common/cgi/fnc.parse_id_param.php - parses ID param from CGI |
/** common/cgi/fnc.parse_id_param.php - parses ID param from CGI |
| */ |
*/ |
| function parse_id_param($key_name = 'id', $exploded_qs = true) /* {{{ */ |
function parse_id_param($key_name = 'id', $exploded_qs = true) /* {{{ */ |
| { |
{ |
| $ret = ''; |
$ret = ''; |
| if ($exploded_qs != false) { |
if ($exploded_qs != false) { |
| if (! is_array($exploded_qs)) { |
if (! is_array($exploded_qs)) { |
| global $HTTP_SERVER_VARS; |
global $HTTP_SERVER_VARS; |
| $exploded_qs = explode('&', $HTTP_SERVER_VARS['QUERY_STRING']); |
$exploded_qs = explode('&', $HTTP_SERVER_VARS['QUERY_STRING']); |
| } |
} |
| foreach ($exploded_qs as $val) { |
foreach ($exploded_qs as $val) { |
| if (strlen($val) > 0 && strpos($val, '=') === false) { |
if (strlen($val) > 0 && strpos($val, '=') === false) { |
| $ret = $val; |
$ret = $val; |
| break; |
break; |
| } |
} |
| } |
} |
| } |
} |
| if ($ret == '') { |
if ($ret == '') { |
| global $HTTP_GET_VARS; |
global $HTTP_GET_VARS; |
| foreach ($HTTP_GET_VARS as $key => $val) { |
foreach ($HTTP_GET_VARS as $key => $val) { |
| if (! strcasecmp($key, $key_name) && strlen($val) > 0) { |
if (! strcasecmp($key, $key_name) && strlen($val) > 0) { |
| $ret = $val; |
$ret = $val; |
| break; |
break; |
| } |
} |
| } |
} |
| } |
} |
| if ($ret == '') { |
if ($ret == '') { |
| global $HTTP_POST_VARS; |
global $HTTP_POST_VARS; |
| foreach ($HTTP_POST_VARS as $key => $val) { |
foreach ($HTTP_POST_VARS as $key => $val) { |
| if (! strcasecmp($key, $key_name) && strlen($val) > 0) { |
if (! strcasecmp($key, $key_name) && strlen($val) > 0) { |
| $ret = $val; |
$ret = $val; |
| break; |
break; |
| } |
} |
| } |
} |
| } |
} |
| return $ret; |
return $ret; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** common/cgi/fnc.stripslashes_from_CGI_vars.php - strips slashes |
/** common/cgi/fnc.stripslashes_from_CGI_vars.php - strips slashes |
| * from CGI variables; accepts string or array of strings on input |
* from CGI variables; accepts string or array of strings on input |
| */ |
*/ |
| function stripslashes_from_CGI_vars($param) /* {{{ */ |
function stripslashes_from_CGI_vars($param) /* {{{ */ |
| { |
{ |
| $ret = true; |
$ret = true; |
| if (is_string($param)) { |
if (is_string($param)) { |
| global $HTTP_GET_VARS; |
global $HTTP_GET_VARS; |
| global $HTTP_POST_VARS; |
global $HTTP_POST_VARS; |
| global $$param; |
global $$param; |
| if (isset($$param)) |
if (isset($$param)) |
| $$param = stripslashes($$param); |
$$param = stripslashes($$param); |
| if (isset($HTTP_GET_VARS[$param])) |
if (isset($HTTP_GET_VARS[$param])) |
| $HTTP_GET_VARS[$param] = |
$HTTP_GET_VARS[$param] = |
| stripslashes($HTTP_GET_VARS[$param]); |
stripslashes($HTTP_GET_VARS[$param]); |
| if (isset($HTTP_POST_VARS[$param])) |
if (isset($HTTP_POST_VARS[$param])) |
| $HTTP_POST_VARS[$param] = |
$HTTP_POST_VARS[$param] = |
| stripslashes($HTTP_POST_VARS[$param]); |
stripslashes($HTTP_POST_VARS[$param]); |
| } elseif (is_array($param)) { |
} elseif (is_array($param)) { |
| foreach ($param as $var) |
foreach ($param as $var) |
| $ret = $ret && stripslashes_from_CGI_vars($var); |
$ret = $ret && stripslashes_from_CGI_vars($var); |
| } else { |
} else { |
| $ret = false; |
$ret = false; |
| } |
} |
| return $ret; |
return $ret; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** common/cgi/fnc.unset_all.php - unsets all CGI types of variable |
/** common/cgi/fnc.unset_all.php - unsets all CGI types of variable |
| */ |
*/ |
| |
|
| function unset_all($var) /* {{{ */ |
function unset_all($var) /* {{{ */ |
| { |
{ |
| global $HTTP_POST_VARS; |
global $HTTP_POST_VARS; |
| global $HTTP_GET_VARS; |
global $HTTP_GET_VARS; |
| global $HTTP_COOKIE_VARS; |
global $HTTP_COOKIE_VARS; |
| global $$var; |
global $$var; |
| unset($HTTP_POST_VARS[$var]); |
unset($HTTP_POST_VARS[$var]); |
| unset($HTTP_GET_VARS[$var]); |
unset($HTTP_GET_VARS[$var]); |
| unset($HTTP_COOKIE_VARS[$var]); |
unset($HTTP_COOKIE_VARS[$var]); |
| //unset($$var); |
//unset($$var); |
| $$var = ''; |
$$var = ''; |
| return true; |
return true; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** common/cgi/fnc.get_cgi_var.php - return value of CGI variable |
/** common/cgi/fnc.get_cgi_var.php - return value of CGI variable |
| */ |
*/ |
| function get_cgi_var($name, $default_value = null) /* {{{ */ |
function get_cgi_var($name, $default_value = null) /* {{{ */ |
| { |
{ |
| global $HTTP_GET_VARS; |
global $HTTP_GET_VARS; |
| $var = $HTTP_GET_VARS[$name]; |
$var = $HTTP_GET_VARS[$name]; |
| if (! isset($var)) { |
if (! isset($var)) { |
| global $HTTP_POST_VARS; |
global $HTTP_POST_VARS; |
| $var = $HTTP_POST_VARS[$name]; |
$var = $HTTP_POST_VARS[$name]; |
| } |
} |
| if (isset($var)) { |
if (isset($var)) { |
| if (is_array($var)) { |
if (is_array($var)) { |
| foreach (array_keys($var) as $key) { |
foreach (array_keys($var) as $key) { |
| $var[$key] = stripslashes($var[$key]); |
$var[$key] = stripslashes($var[$key]); |
| } |
} |
| } else { |
} else { |
| $var = stripslashes($var); |
$var = stripslashes($var); |
| } |
} |
| } else { |
} else { |
| $var = $default_value; |
$var = $default_value; |
| } |
} |
| return $var; |
return $var; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** common/misc/fnc.chdir_back.php - changes directory back of $dir |
/** common/misc/fnc.chdir_back.php - changes directory back of $dir |
| */ |
*/ |
| //require_once dirname(__FILE__) . '/fnc.dir_back.php'; |
//require_once dirname(__FILE__) . '/fnc.dir_back.php'; |
| |
|
| function chdir_back($dir) /* {{{ */ |
function chdir_back($dir) /* {{{ */ |
| { |
{ |
| $dir_back = Platon::dir_back($dir); |
$dir_back = Platon::dir_back($dir); |
| if (strlen($dir_back) > 0) |
if (strlen($dir_back) > 0) |
| return chdir($dir_back); |
return chdir($dir_back); |
| return false; |
return false; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** common/misc/fnc.dir_back.php - creates back directory of $dir |
/** common/misc/fnc.dir_back.php - creates back directory of $dir |
| */ |
*/ |
| //require_once dirname(__FILE__) . '/../str/fnc.append_slash.php'; |
//require_once dirname(__FILE__) . '/../str/fnc.append_slash.php'; |
| |
|
| function dir_back($dir) /* {{{ */ |
function dir_back($dir) /* {{{ */ |
| { |
{ |
| $dir_back = ''; |
$dir_back = ''; |
| $i = substr_count(Platon::append_slash($dir), '/'); |
$i = substr_count(Platon::append_slash($dir), '/'); |
| for (; $i > 0; $i--) |
for (; $i > 0; $i--) |
| $dir_back .= '../'; |
$dir_back .= '../'; |
| return $dir_back; |
return $dir_back; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** common/misc/fnc.get_mtime.php - returns microtime |
/** common/misc/fnc.get_mtime.php - returns microtime |
| */ |
*/ |
| function get_mtime() /* {{{ */ |
function get_mtime() /* {{{ */ |
| { |
{ |
| $mtime = microtime(); |
$mtime = microtime(); |
| $mtime = explode(" ", $mtime); |
$mtime = explode(" ", $mtime); |
| $mtime = $mtime[1] + $mtime[0]; |
$mtime = $mtime[1] + $mtime[0]; |
| return $mtime; |
return $mtime; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** common/misc/fnc.redirect_test.php - tests if redirect is neccessary |
/** common/misc/fnc.redirect_test.php - tests if redirect is neccessary |
| */ |
*/ |
| function redirect_test($file, $url) /* {{{ */ |
function redirect_test($file, $url) /* {{{ */ |
| { |
{ |
| global $HTTP_SERVER_VARS; |
global $HTTP_SERVER_VARS; |
| if (! isset($HTTP_SERVER_VARS['PHP_SELF']) |
if (! isset($HTTP_SERVER_VARS['PHP_SELF']) |
| || ! eregi($file, $HTTP_SERVER_VARS['PHP_SELF'])) { |
|| ! eregi($file, $HTTP_SERVER_VARS['PHP_SELF'])) { |
|
|
| if (strlen($url) <= 0) { |
if (strlen($url) <= 0) { |
| die('empty $url in redirect_test()'); |
die('empty $url in redirect_test()'); |
| } |
} |
| $schema = ''; |
$schema = ''; |
| if (strncmp($url, "http://", 7)) { |
if (strncmp($url, "http://", 7)) { |
| $schema = 'http'; |
$schema = 'http'; |
| } elseif (strncmp($url, "https://", 8)) { |
} elseif (strncmp($url, "https://", 8)) { |
| $schema = 'https'; |
$schema = 'https'; |
| } |
} |
| if (strlen($schema) > 0) { |
if (strlen($schema) > 0) { |
| // TODO: create delim according to $HTTP_SERVER_VARS['SCRIPT_NAME'] |
// TODO: create delim according to $HTTP_SERVER_VARS['SCRIPT_NAME'] |
| $delim = $url[0] == '/' ? '' : '/'; |
$delim = $url[0] == '/' ? '' : '/'; |
| $http_host = @$HTTP_SERVER_VARS['HTTP_HOST']; |
$http_host = @$HTTP_SERVER_VARS['HTTP_HOST']; |
| $server_name = @$HTTP_SERVER_VARS['SERVER_NAME']; |
$server_name = @$HTTP_SERVER_VARS['SERVER_NAME']; |
| |
|
| if (isset($http_host) |
if (isset($http_host) |
| && is_string($http_host) |
&& is_string($http_host) |
| && strlen($http_host) > 0) { |
&& strlen($http_host) > 0) { |
| $url = "$schema://$http_host$delim$url"; |
$url = "$schema://$http_host$delim$url"; |
| } elseif (isset($server_name) |
} elseif (isset($server_name) |
| && is_string($server_name) |
&& is_string($server_name) |
| && strlen($server_name) > 0) { |
&& strlen($server_name) > 0) { |
| $url = "$schema://$server_name$delim$url"; |
$url = "$schema://$server_name$delim$url"; |
| } |
} |
| } |
} |
| //echo("Location: $url [$file][".$HTTP_SERVER_VARS['PHP_SELF']."]"); |
//echo("Location: $url [$file][".$HTTP_SERVER_VARS['PHP_SELF']."]"); |
| header("Location: $url"); |
header("Location: $url"); |
| exit; |
exit; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** common/misc/fnc.write_origvars_html.php - creates HTML hidden |
/** common/misc/fnc.write_origvars_html.php - creates HTML hidden |
| * form fields for passed GET variables |
* form fields for passed GET variables |
| */ |
*/ |
| function write_origvars_html($origvars, $default_value = null, $method = 'POST') /* {{{ */ |
function write_origvars_html($origvars, $default_value = null, $method = 'POST') /* {{{ */ |
| { |
{ |
| if (! is_array($origvars)) { |
if (! is_array($origvars)) { |
| foreach (explode('&', $origvars) as $param) { |
foreach (explode('&', $origvars) as $param) { |
| $parts = explode('=', $param, 2); |
$parts = explode('=', $param, 2); |
|
|
| } |
} |
| } else { |
} else { |
| foreach ($origvars as $key => $val) { |
foreach ($origvars as $key => $val) { |
| echo '<input type="hidden" name="'.htmlspecialchars($key).'"'; |
echo '<input type="hidden" name="'.htmlspecialchars($key).'"'; |
| if (isset($val)) { |
if (isset($val)) { |
| echo ' value="'.htmlspecialchars($val).'"'; |
echo ' value="'.htmlspecialchars($val).'"'; |
| } |
} |
|
|
| return true; |
return true; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** common/rand/fnc.rand_pass.php - returns pronounceable random key |
/** common/rand/fnc.rand_pass.php - returns pronounceable random key |
| * Note: You may edit the $num_letters and $array variables |
* Note: You may edit the $num_letters and $array variables |
| * Author: Sebastien Cevey <seb@cine7.net> |
* Author: Sebastien Cevey <seb@cine7.net> |
| * Project: Web OpenScripts <http://wos.sourceforge.net> |
* Project: Web OpenScripts <http://wos.sourceforge.net> |
| */ |
*/ |
| function rand_pass() /* {{{ */ |
function rand_pass() /* {{{ */ |
| { |
{ |
| // Pronounceable pieces of words |
// Pronounceable pieces of words |
| $array = array( |
$array = array( |
| 'ap', 'baz', 'car', 'dus', 'tin', 'rog', 'sti', |
'ap', 'baz', 'car', 'dus', 'tin', 'rog', 'sti', |
| 'rev', 'pik', 'sty', 'lev', 'qot', 'rel', 'vid', |
'rev', 'pik', 'sty', 'lev', 'qot', 'rel', 'vid', |
| 'kro', 'xo', 'pro', 'wia', 'axi', 'jer', 'foh', |
'kro', 'xo', 'pro', 'wia', 'axi', 'jer', 'foh', |
| 'mu', 'ya', 'zol', 'gu', 'pli', 'cra', 'den', |
'mu', 'ya', 'zol', 'gu', 'pli', 'cra', 'den', |
| 'bi', 'sat', 'ry', 'qui', 'wip', 'fla', 'gro', |
'bi', 'sat', 'ry', 'qui', 'wip', 'fla', 'gro', |
| 'tav', 'peh', 'gil', 'lot', 'kal', 'zan', 'noc', |
'tav', 'peh', 'gil', 'lot', 'kal', 'zan', 'noc', |
| 'bat', 'tev', 'lun', 'pal', 'hom', 'cun', 'wos', 'vox'); |
'bat', 'tev', 'lun', 'pal', 'hom', 'cun', 'wos', 'vox'); |
| |
|
| // Minimum number of letters |
// Minimum number of letters |
| $num_letters = 6; |
$num_letters = 6; |
| // Maximum number of letters |
// Maximum number of letters |
| $max_letters = 10; |
$max_letters = 10; |
| // Fraction of uppercased letters (randomized too...) |
// Fraction of uppercased letters (randomized too...) |
| $uppercased = 0; |
$uppercased = 0; |
| // Randomize on microseconds |
// Randomize on microseconds |
| mt_srand ((double)microtime() * 1000000); |
mt_srand ((double)microtime() * 1000000); |
| // Create random pass (too long for the moment) |
// Create random pass (too long for the moment) |
| for($i = 0; $i < $num_letters; $i++) |
for($i = 0; $i < $num_letters; $i++) |
| $pass .= $array[mt_rand(0, (count($array) - 1))]; |
$pass .= $array[mt_rand(0, (count($array) - 1))]; |
| |
|
| //print "$pass "; |
//print "$pass "; |
| // Make sure there is not twice the same letter one after the other |
// Make sure there is not twice the same letter one after the other |
| for($i = 1; $i < strlen($pass); $i++) { |
for($i = 1; $i < strlen($pass); $i++) { |
| if(substr($pass, $i, 1) == substr($pass, $i - 1, 1)) |
if(substr($pass, $i, 1) == substr($pass, $i - 1, 1)) |
| $pass = substr($pass, 0, $i) . substr($pass, $i + 1); |
$pass = substr($pass, 0, $i) . substr($pass, $i + 1); |
| } |
} |
| // Randomize the pass case [for each letter] |
// Randomize the pass case [for each letter] |
| if ($uppercased) |
if ($uppercased) |
| for($i = 0; $i < strlen($pass); $i++) { |
for($i = 0; $i < strlen($pass); $i++) { |
| if(mt_rand(0, $uppercased) == 0) |
if(mt_rand(0, $uppercased) == 0) |
| $pass = substr($pass,0,$i) |
$pass = substr($pass,0,$i) |
| . strtoupper(substr($pass, $i,1)) |
. strtoupper(substr($pass, $i,1)) |
| . substr($pass, $i + 1); |
. substr($pass, $i + 1); |
| } |
} |
| // Shorten it now |
// Shorten it now |
| $pass = substr($pass, 0, $num_letters |
$pass = substr($pass, 0, $num_letters |
| + mt_rand(0,$max_letters - $num_letters)); |
+ mt_rand(0,$max_letters - $num_letters)); |
| // Return the password |
// Return the password |
| return $pass; |
return $pass; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** common/rand/fnc.rand_str.php - returns random string of length |
/** common/rand/fnc.rand_str.php - returns random string of length |
| * $len consisting of A-Z and 0-9; first character will always be |
* $len consisting of A-Z and 0-9; first character will always be |
| * an alphabet; requires srand() |
* an alphabet; requires srand() |
| */ |
*/ |
| function rand_str($len = 8) /* {{{ */ |
function rand_str($len = 8) /* {{{ */ |
| { |
{ |
| $s = ''; |
$s = ''; |
| for ($i = 0; $i < $len; $i++) { |
for ($i = 0; $i < $len; $i++) { |
| if ($i == 0) { |
if ($i == 0) { |
| $r = rand() % 26; |
$r = rand() % 26; |
| } else { |
} else { |
| $r = rand() % 36; |
$r = rand() % 36; |
| } |
} |
| if ($r >= 26) { |
if ($r >= 26) { |
| $s .= chr($r + ord('0') - 26); |
$s .= chr($r + ord('0') - 26); |
| } else { |
} else { |
| $s.= chr($r + ord('A')); |
$s.= chr($r + ord('A')); |
| } |
} |
| } |
} |
| return $s; |
return $s; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** common/str/fnc.append_slash.php - appends slash at the end of |
/** common/str/fnc.append_slash.php - appends slash at the end of |
| * string if it is not empty and slash is not already present at |
* string if it is not empty and slash is not already present at |
| * the end of string; returns changed string on success, on |
* the end of string; returns changed string on success, on |
| * failure returns false |
* failure returns false |
| */ |
*/ |
| function append_slash($string, $char = '/') /* {{{ */ |
function append_slash($string, $char = '/') /* {{{ */ |
| { |
{ |
| if (! is_string($string) || ! is_string($char)) { |
if (! is_string($string) || ! is_string($char)) { |
| return false; |
return false; |
| } |
} |
| if (strlen($string) > 0) { |
if (strlen($string) > 0) { |
| if ($string[strlen($string) - 1] != $char[0]) |
if ($string[strlen($string) - 1] != $char[0]) |
| $string .= $char[0]; |
$string .= $char[0]; |
| } |
} |
| return $string; |
return $string; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** common/str/fnc.set_value_by_substr.php - returns appropriate |
/** common/str/fnc.set_value_by_substr.php - returns appropriate |
| * value from array according to key compared with passed string |
* value from array according to key compared with passed string |
| * or empty string when no key contains passed string |
* or empty string when no key contains passed string |
| */ |
*/ |
| function set_value_by_substr($string, $substr_ar) /* {{{ */ |
function set_value_by_substr($string, $substr_ar) /* {{{ */ |
| { |
{ |
| if (! is_array($substr_ar)) { |
if (! is_array($substr_ar)) { |
| return stristr($substr_ar, $string) != false ? $substr_ar : ''; |
return stristr($substr_ar, $string) != false ? $substr_ar : ''; |
| } |
} |
| foreach ($substr_ar as $key => $value) { |
foreach ($substr_ar as $key => $value) { |
| if ($key == '' || stristr($string, $key) != false) |
if ($key == '' || stristr($string, $key) != false) |
| return $value; |
return $value; |
| } |
} |
| return ''; |
return ''; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** common/str/fnc.str_replace_postfix - replaces postfix of string |
/** common/str/fnc.str_replace_postfix - replaces postfix of string |
| */ |
*/ |
| function str_replace_postfix($search, $replace, $subject) /* {{{ */ |
function str_replace_postfix($search, $replace, $subject) /* {{{ */ |
| { |
{ |
| $idx = strlen($subject) - strlen($search); |
$idx = strlen($subject) - strlen($search); |
| if ($idx < 0) { |
if ($idx < 0) { |
| return $subject; |
return $subject; |
| } |
} |
| if (! strcmp(substr($subject, $idx), $search)) { |
if (! strcmp(substr($subject, $idx), $search)) { |
| return substr($subject, 0, $idx) . $replace; |
return substr($subject, 0, $idx) . $replace; |
| } |
} |
| return $subject; |
return $subject; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** common/str/fnc.str_replace_prefix - replaces prefix of string |
/** common/str/fnc.str_replace_prefix - replaces prefix of string |
| */ |
*/ |
| function str_replace_prefix($search, $replace, $subject) /* {{{ */ |
function str_replace_prefix($search, $replace, $subject) /* {{{ */ |
| { |
{ |
| if (! strncmp($subject, $search, strlen($search))) { |
if (! strncmp($subject, $search, strlen($search))) { |
| return $replace . substr($subject, strlen($search)); |
return $replace . substr($subject, strlen($search)); |
| } |
} |
| return $subject; |
return $subject; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** common/str/fnc.pretty_substr.php - pretty formated substring |
/** common/str/fnc.pretty_substr.php - pretty formated substring |
| * Author: Frank Schmitt <frank.schmitt@vista.com> [May-2002] |
* Author: Frank Schmitt <frank.schmitt@vista.com> [May-2002] |
| */ |
*/ |
| # |
# |
| # Oracle PL/SQL -- the same author |
# Oracle PL/SQL -- the same author |
| # warning: code below contains at least one bug |
# warning: code below contains at least one bug |
|
|
| # return result; |
# return result; |
| # end pretty_substr; |
# end pretty_substr; |
| # / |
# / |
| function pretty_substr($string, $length, $slop = 4) /* {{{ */ |
function pretty_substr($string, $length, $slop = 4) /* {{{ */ |
| { |
{ |
| if (strlen($string) > $length) { |
if (strlen($string) > $length) { |
| $string = substr($string, 0, $length - 1); |
$string = substr($string, 0, $length - 1); |
| $lastSpace = strrpos($string, ' '); |
$lastSpace = strrpos($string, ' '); |
| if ($lastSpace > $length - $slop) { |
if ($lastSpace > $length - $slop) { |
| $string = substr($string, 0, $lastSpace); |
$string = substr($string, 0, $lastSpace); |
| } |
} |
| $string .= '…'; |
$string .= '…'; |
| } |
} |
| return $string; |
return $string; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** common/str/fnc.get_english_num.php - returns english number |
/** common/str/fnc.get_english_num.php - returns english number |
| */ |
*/ |
| function get_english_num($number) /* {{{ */ |
function get_english_num($number) /* {{{ */ |
| { |
{ |
| $number = intval($number); |
$number = intval($number); |
| switch ($number % 10) { |
switch ($number % 10) { |
| case 1: $number .= 'st'; break; |
case 1: $number .= 'st'; break; |
| case 2: $number .= 'nd'; break; |
case 2: $number .= 'nd'; break; |
| case 3: $number .= 'rd'; break; |
case 3: $number .= 'rd'; break; |
| default: $number .= 'th'; break; |
default: $number .= 'th'; break; |
| } |
} |
| return $number; |
return $number; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** common/str/fnc.htmlspecialchars2.php - common HTML special |
/** common/str/fnc.htmlspecialchars2.php - common HTML special |
| * characters substitution with replacing space for ' ' |
* characters substitution with replacing space for ' ' |
| */ |
*/ |
| function htmlspecialchars2($str) /* {{{ */ |
function htmlspecialchars2($str) /* {{{ */ |
| { |
{ |
| return str_replace(' ', ' ', htmlspecialchars($str)); |
return str_replace(' ', ' ', htmlspecialchars($str)); |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** common/str/fnc.escape_email.php - escape e-mail address |
/** common/str/fnc.escape_email.php - escape e-mail address |
| */ |
*/ |
| function escape_email($email, $mode = 'create_hypertext', $attributes = '') /* {{{ */ |
function escape_email($email, $mode = 'create_hypertext', $attributes = '') /* {{{ */ |
| { |
{ |
| /* Code borrowed from Smarty template engine; |
/* Code borrowed from Smarty template engine; |
| Smarty-2.1.0/plugins/modifier.escape.php file */ |
Smarty-2.1.0/plugins/modifier.escape.php file */ |
| |
|
| $ret = ''; |
$ret = ''; |
| switch ($mode) { |
switch ($mode) { |
| case 'url': |
case 'url': |
| // escape every character into hex |
// escape every character into hex |
| for ($x = 0; $x < strlen($email); $x++) { |
for ($x = 0; $x < strlen($email); $x++) { |
| $ret .= '%' . bin2hex($email[$x]); |
$ret .= '%' . bin2hex($email[$x]); |
| } |
} |
| break; |
break; |
| case 'html': |
case 'html': |
| for ($x = 0; $x < strlen($email); $x++) { |
for ($x = 0; $x < strlen($email); $x++) { |
| $ret .= (ord($email[$x]) > 127 ? $email[$x] : '&#x'.bin2hex($email[$x]).';'); |
$ret .= (ord($email[$x]) > 127 ? $email[$x] : '&#x'.bin2hex($email[$x]).';'); |
| } |
} |
| break; |
break; |
| default: |
default: |
| case 'create_hypertext': |
case 'create_hypertext': |
| is_array($attributes) && $attributes = join(' ', $attributes); |
is_array($attributes) && $attributes = join(' ', $attributes); |
| $attributes != '' && $attributes .= ' '; |
$attributes != '' && $attributes .= ' '; |
| $ret = '<a '.$attributes.'href="mailto:' |
$ret = '<a '.$attributes.'href="mailto:' |
| . escape_email($email, 'url') . '">' |
. escape_email($email, 'url') . '">' |
| . escape_email($email, 'html') . '</a>'; |
. escape_email($email, 'html') . '</a>'; |
| break; |
break; |
| } |
} |
| return $ret; |
return $ret; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** common/url/fnc.get_path_without_scriptname.php - returns passed |
/** common/url/fnc.get_path_without_scriptname.php - returns passed |
| * path without scriptname; function also accepts array returned |
* path without scriptname; function also accepts array returned |
| * from glue_url() function |
* from glue_url() function |
| */ |
*/ |
| function get_path_without_scriptname($url) /* {{{ */ |
function get_path_without_scriptname($url) /* {{{ */ |
| { |
{ |
| $path = is_array($url) ? $url['path'] : $url; |
$path = is_array($url) ? $url['path'] : $url; |
| $path = preg_replace('/http[s]?:\/\/[^\/]+/', '', $path); |
$path = preg_replace('/http[s]?:\/\/[^\/]+/', '', $path); |
| $path = preg_replace('/\?.*/', '', $path); |
$path = preg_replace('/\?.*/', '', $path); |
| $path = preg_replace('/[^\/]+\z/', '', $path); |
$path = preg_replace('/[^\/]+\z/', '', $path); |
| return $path; |
return $path; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| /** common/url/fnc.glue_url.php - creates URL string from components; |
/** |
| * components are array as it is returned from glue_url() function |
* Creates URL string from components; components are array as it is |
| * Author: Tomas V.V.Cox <cox@idecnet.com> [19-Feb-2001] |
* returned from glue_url() function. |
| */ |
* |
| function glue_url($url) /* {{{ */ |
* @param array $url Components array |
| { |
* @return glued url |
| if (! is_array($url)) |
* @author Tomas V.V.Cox <cox@idecnet.com> |
| return false; |
* @date 19/Feb/2001 |
| // scheme |
* @access public |
| $uri = empty($url['scheme']) ? '' : $url['scheme'] . '://'; |
*/ |
| // user and pass |
function glue_url($url) /* {{{ */ |
| if (! empty($url['user'])) { |
{ |
| $uri .= $url['user'].':'.$url['pass'].'@'; |
if (! is_array($url)) |
| } |
return false; |
| // host |
// scheme |
| $uri .= $url['host']; |
$uri = empty($url['scheme']) ? '' : $url['scheme'] . '://'; |
| // port |
// user and pass |
| $port = empty($url['port']) ? '' : ':'.$url['port']; |
if (! empty($url['user'])) { |
| $uri .= $port; |
$uri .= $url['user'].':'.$url['pass'].'@'; |
| // path |
} |
| $uri .= $url['path']; |
// host |
| // fragment or query |
$uri .= $url['host']; |
| if (isset($url['fragment'])) { |
// port |
| $uri .= '#' . $url['fragment']; |
$port = empty($url['port']) ? '' : ':'.$url['port']; |
| } |
$uri .= $port; |
| elseif (isset($url['query'])) { |
// path |
| $uri .= '?' . $url['query']; |
$uri .= $url['path']; |
| } |
// fragment or query |
| return $uri; |
if (isset($url['fragment'])) { |
| } /* }}} */ |
$uri .= '#' . $url['fragment']; |
| |
} |
| |
elseif (isset($url['query'])) { |
| |
$uri .= '?' . $url['query']; |
| |
} |
| |
return $uri; |
| |
} /* }}} */ |
| } |
} |
| |
|
| /* vim: set expandtab tabstop=4 shiftwidth=4: |
/* vim: set expandtab tabstop=4 shiftwidth=4: |