/*
Algunas funciones utiles
*/
//Borrado recursivo de directorios
$debug = "";
function rrmdir($dir) {
global $debug;
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir."/".$object) == "dir"){
rrmdir($dir."/".$object);
$debug.= "D ".$dir."/".$object."
";
} else {
unlink($dir."/".$object);
$debug.= "F ".$dir."/".$object."
";
}
}
}
reset($objects);
rmdir($dir);
}
return $debug;
}
//Compara imagenes en formato ##_###########.jpg (numero_sha.jpg)
function cmpPic($a, $b){
if ($a['sha'] == $b['sha']) {
return 0;
}
$arra = explode('_',$a['sha']);
$arrb = explode('_',$b['sha']);
return ($arra[0] < $arrb[0]) ? -1 : 1;
}
function salt() {
$fp = fopen("/dev/urandom", "r");
// Aqui se leen 24 bytes, que generan una cadena de 32 caracteres en B64
$cadena = fread($fp, 24);
fclose($fp);
return base64_encode($cadena);
}
function archivo_aleatorio() {
$fp = fopen("/dev/urandom", "r");
// Aqui se leen 32 bytes
$cadena = fread($fp, 32);
fclose($fp);
return sha1($cadena);
}
//Funcion post mediante file_get_contents, $data es un array de parametros
function http_post ($url, $data)
{
$data_url = http_build_query ($data);
$data_len = strlen ($data_url);
return array ('content'=>file_get_contents ($url, false, stream_context_create (array ('http'=>array ('method'=>'POST'
, 'header'=>"Connection: close\r\nContent-Length: $data_len\r\n"
, 'content'=>$data_url
))))
, 'headers'=>$http_response_header
);
}
//Pretty printing un array en html
function print_a($subject){
echo str_replace("=>","⇒",str_replace("Array","Array",nl2br(str_replace(" "," ",print_r($subject,true)))));
}
//Encode base64
function urlsafe_b64encode($string) {
$data = base64_encode($string);
$data = str_replace(array('+','/','='),array('-','_',''),$data);
return $data;
}
//Decode base64
function urlsafe_b64decode($string) {
$data = str_replace(array('-','_'),array('+','/'),$string);
$mod4 = strlen($data) % 4;
if ($mod4) {
$data .= substr('====', $mod4);
}
return base64_decode($data);
}
//Obtiene el valor numerico de un string numerico
function get_numeric($val) {
if (is_numeric($val)) {
return $val + 0;
}
return false;
}
//Devuelve si una cadena empieza con $haystack
function startsWith($haystack, $needle) {
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
//Devuelve si una cadena termina con $haystack
function endsWith($haystack, $needle) {
$length = strlen($needle);
$start = $length * -1; //negative
return (substr($haystack, $start) === $needle);
}
?>
Presentación del libro.
Entrada libre.