mirror of
https://github.com/devcode-it/openstamanager.git
synced 2025-06-05 22:09:38 +02:00
Ridistribuzione funzioni generiche
This commit is contained in:
@ -1,5 +1,10 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Funzioni fondamentali per il corretto funzionamento del nucleo del progetto.
|
||||
*
|
||||
* @since 2.3
|
||||
*/
|
||||
|
||||
/**
|
||||
* Esegue il redirect.
|
||||
@ -36,6 +41,13 @@ function sanitizeFilename($filename)
|
||||
return $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Elimina i file indicati.
|
||||
*
|
||||
* @param array $files
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function delete($files)
|
||||
{
|
||||
// Filesystem Symfony
|
||||
@ -51,6 +63,13 @@ function delete($files)
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Controlla l'esistenza e i permessi di scrittura sul percorso indicato.
|
||||
*
|
||||
* @param string $path
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function directory($path)
|
||||
{
|
||||
if (is_dir($path) && is_writable($path)) {
|
||||
@ -74,18 +93,9 @@ function directory($path)
|
||||
/**
|
||||
* Copy a file, or recursively copy a folder and its contents.
|
||||
*
|
||||
* @author Aidan Lister <aidan@php.net>
|
||||
*
|
||||
* @version 1.0.1
|
||||
*
|
||||
* @see http://aidanlister.com/repos/v/function.copyr.php
|
||||
*
|
||||
* @param string $source
|
||||
* Source path
|
||||
* @param string $dest
|
||||
* Destination path
|
||||
* @param array|string $ignores
|
||||
* Paths to ingore
|
||||
* @param string $source Source path
|
||||
* @param string $dest Destination path
|
||||
* @param array|string $ignores Paths to ingore
|
||||
*
|
||||
* @return bool Returns TRUE on success, FALSE on failure
|
||||
*/
|
||||
@ -209,97 +219,6 @@ function checkZip($zip_file)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Individua la differenza tra le date indicate.
|
||||
* $interval può essere:
|
||||
* yyyy - Number of full years
|
||||
* q - Number of full quarters
|
||||
* m - Number of full months
|
||||
* y - Difference between day numbers
|
||||
* (eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".)
|
||||
* d - Number of full days
|
||||
* w - Number of full weekdays
|
||||
* ww - Number of full weeks
|
||||
* h - Number of full hours
|
||||
* n - Number of full minutes
|
||||
* s - Number of full seconds (default).
|
||||
*
|
||||
* @param unknown $interval
|
||||
* @param unknown $datefrom
|
||||
* @param unknown $dateto
|
||||
* @param string $using_timestamps
|
||||
*/
|
||||
function datediff($interval, $datefrom, $dateto, $using_timestamps = false)
|
||||
{
|
||||
if (!$using_timestamps) {
|
||||
$datefrom = strtotime($datefrom, 0);
|
||||
$dateto = strtotime($dateto, 0);
|
||||
}
|
||||
$difference = $dateto - $datefrom; // Difference in seconds
|
||||
switch ($interval) {
|
||||
case 'yyyy': // Number of full years
|
||||
$years_difference = floor($difference / 31536000);
|
||||
if (mktime(date('H', $datefrom), date('i', $datefrom), date('s', $datefrom), date('n', $datefrom), date('j', $datefrom), date('Y', $datefrom) + $years_difference) > $dateto) {
|
||||
--$years_difference;
|
||||
}
|
||||
if (mktime(date('H', $dateto), date('i', $dateto), date('s', $dateto), date('n', $dateto), date('j', $dateto), date('Y', $dateto) - ($years_difference + 1)) > $datefrom) {
|
||||
++$years_difference;
|
||||
}
|
||||
$datediff = $years_difference;
|
||||
break;
|
||||
case 'q': // Number of full quarters
|
||||
$quarters_difference = floor($difference / 8035200);
|
||||
while (mktime(date('H', $datefrom), date('i', $datefrom), date('s', $datefrom), date('n', $datefrom) + ($quarters_difference * 3), date('j', $dateto), date('Y', $datefrom)) < $dateto) {
|
||||
++$months_difference;
|
||||
}
|
||||
--$quarters_difference;
|
||||
$datediff = $quarters_difference;
|
||||
break;
|
||||
case 'm': // Number of full months
|
||||
$months_difference = floor($difference / 2678400);
|
||||
while (mktime(date('H', $datefrom), date('i', $datefrom), date('s', $datefrom), date('n', $datefrom) + ($months_difference), date('j', $dateto), date('Y', $datefrom)) < $dateto) {
|
||||
++$months_difference;
|
||||
}
|
||||
--$months_difference;
|
||||
$datediff = $months_difference;
|
||||
break;
|
||||
case 'y': // Difference between day numbers
|
||||
$datediff = date('z', $dateto) - date('z', $datefrom);
|
||||
break;
|
||||
case 'd': // Number of full days
|
||||
$datediff = floor($difference / 86400);
|
||||
break;
|
||||
case 'w': // Number of full weekdays
|
||||
$days_difference = floor($difference / 86400);
|
||||
$weeks_difference = floor($days_difference / 7); // Complete weeks
|
||||
$first_day = date('w', $datefrom);
|
||||
$days_remainder = floor($days_difference % 7);
|
||||
$odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?
|
||||
if ($odd_days > 7) { // Sunday
|
||||
--$days_remainder;
|
||||
}
|
||||
if ($odd_days > 6) { // Saturday
|
||||
--$days_remainder;
|
||||
}
|
||||
$datediff = ($weeks_difference * 5) + $days_remainder;
|
||||
break;
|
||||
case 'ww': // Number of full weeks
|
||||
$datediff = floor($difference / 604800);
|
||||
break;
|
||||
case 'h': // Number of full hours
|
||||
$datediff = floor($difference / 3600);
|
||||
break;
|
||||
case 'n': // Number of full minutes
|
||||
$datediff = floor($difference / 60);
|
||||
break;
|
||||
default: // Number of full seconds (default)
|
||||
$datediff = $difference;
|
||||
break;
|
||||
}
|
||||
|
||||
return $datediff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recupera informazioni sistema operativo dell'utente.
|
||||
*
|
||||
@ -337,68 +256,6 @@ function getOS()
|
||||
return tr('Altro');
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifica che il nome del file non sia già usato nella cartella inserita, nel qual caso aggiungo un suffisso.
|
||||
*
|
||||
* @param string $filename
|
||||
* @param string $dir
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function unique_filename($filename, $dir)
|
||||
{
|
||||
$f = pathinfo($filename);
|
||||
$suffix = 1;
|
||||
while (file_exists($dir.'/'.$filename)) {
|
||||
$filename = $f['filename'].'_'.$suffix.'.'.$f['extension'];
|
||||
++$suffix;
|
||||
}
|
||||
|
||||
return $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Crea le thumbnails di $filename da dentro $dir e le salva in $dir.
|
||||
*
|
||||
* @param string $tmp
|
||||
* @param string $filename
|
||||
* @param string $dir
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function create_thumbnails($tmp, $filename, $dir)
|
||||
{
|
||||
$infos = pathinfo($filename);
|
||||
$name = $infos['filename'];
|
||||
$extension = strtolower($infos['extension']);
|
||||
|
||||
if (!directory($dir)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$driver = extension_loaded('gd') ? 'gd' : 'imagick';
|
||||
Intervention\Image\ImageManagerStatic::configure(['driver' => $driver]);
|
||||
|
||||
$img = Intervention\Image\ImageManagerStatic::make($tmp);
|
||||
|
||||
$img->resize(600, null, function ($constraint) {
|
||||
$constraint->aspectRatio();
|
||||
});
|
||||
$img->save(slashes($dir.'/'.$name.'.'.$extension));
|
||||
|
||||
$img->resize(250, null, function ($constraint) {
|
||||
$constraint->aspectRatio();
|
||||
});
|
||||
$img->save(slashes($dir.'/'.$name.'_thumb250.'.$extension));
|
||||
|
||||
$img->resize(100, null, function ($constraint) {
|
||||
$constraint->aspectRatio();
|
||||
});
|
||||
$img->save(slashes($dir.'/'.$name.'_thumb100.'.$extension));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ottiene l'indirizzo IP del client.
|
||||
*
|
||||
@ -483,28 +340,6 @@ function translateTemplate()
|
||||
echo $template;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sostituisce la prima occorenza di una determinata stringa.
|
||||
*
|
||||
* @param string $str_pattern
|
||||
* @param string $str_replacement
|
||||
* @param string $string
|
||||
*
|
||||
* @since 2.3
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function str_replace_once($str_pattern, $str_replacement, $string)
|
||||
{
|
||||
if (strpos($string, $str_pattern) !== false) {
|
||||
$occurrence = strpos($string, $str_pattern);
|
||||
|
||||
return substr_replace($string, $str_replacement, strpos($string, $str_pattern), strlen($str_pattern));
|
||||
}
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restituisce il percorso del filesystem in modo indipendente dal sistema operativo.
|
||||
*
|
||||
@ -531,43 +366,6 @@ function isAjaxRequest()
|
||||
return \Whoops\Util\Misc::isAjaxRequest() && filter('ajax') !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Esegue una somma precisa tra due interi/array.
|
||||
*
|
||||
* @param array|float $first
|
||||
* @param array|float $second
|
||||
* @param int $decimals
|
||||
*
|
||||
* @since 2.3
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
function sum($first, $second = null, $decimals = 4)
|
||||
{
|
||||
$first = (array) $first;
|
||||
$second = (array) $second;
|
||||
|
||||
$array = array_merge($first, $second);
|
||||
|
||||
$result = 0;
|
||||
|
||||
$decimals = is_numeric($decimals) ? $decimals : Translator::getFormatter()->getPrecision();
|
||||
|
||||
$bcadd = function_exists('bcadd');
|
||||
|
||||
foreach ($array as $value) {
|
||||
$value = round($value, $decimals);
|
||||
|
||||
if ($bcadd) {
|
||||
$result = bcadd($result, $value, $decimals);
|
||||
} else {
|
||||
$result += $value;
|
||||
}
|
||||
}
|
||||
|
||||
return floatval($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Effettua le operazioni automatiche di redirect tra le pagine.
|
||||
*
|
||||
|
Reference in New Issue
Block a user