.
*/
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
function include_file($_folder, $_fn, $_type, $_plugin = '') {
$_rescue = false;
if (isset($_GET['rescue']) && $_GET['rescue'] == 1) {
$_rescue = true;
}
if ($_folder == '3rdparty') {
$_fn .= '.' . $_type;
$path = __DIR__ . '/../../' . $_folder . '/' . $_fn;
$type = $_type;
} else {
$config = array(
'class' => array('/class', '.class.php', 'php'),
'com' => array('/com', '.com.php', 'php'),
'repo' => array('/repo', '.repo.php', 'php'),
'config' => array('/config', '.config.php', 'php'),
'modal' => array('/modal', '.php', 'php'),
'modalhtml' => array('/modal', '.html', 'php'),
'php' => array('/php', '.php', 'php'),
'css' => array('/css', '.css', 'css'),
'js' => array('/js', '.js', 'js'),
'class.js' => array('/js', '.class.js', 'js'),
'custom.js' => array('/custom', 'custom.js', 'js'),
'custom.css' => array('/custom', 'custom.css', 'css'),
'themes.js' => array('/themes', '.js', 'js'),
'themes.css' => array('/themes', '.css', 'css'),
'api' => array('/api', '.api.php', 'php'),
'html' => array('/html', '.html', 'php'),
'configuration' => array('', '.php', 'php'),
);
$_folder .= $config[$_type][0];
$_fn .= $config[$_type][1];
$type = $config[$_type][2];
}
if ($_plugin != '') {
$_folder = 'plugins/' . $_plugin . '/' . $_folder;
}
$path = __DIR__ . '/../../' . $_folder . '/' . $_fn;
if (!file_exists($path)) {
throw new Exception('Fichier introuvable : ' . $path, 35486);
}
if ($type == 'php') {
if ($_type != 'class') {
ob_start();
require_once $path;
if ($_rescue) {
echo str_replace(array('{{', '}}'), '', ob_get_clean());
} else {
echo translate::exec(ob_get_clean(), $_folder . '/' . $_fn);
}
return;
}
require_once $path;
return;
}
if ($type == 'css') {
echo '';
return;
}
if ($type == 'js') {
echo '';
return;
}
}
function getTemplate($_folder, $_version, $_filename, $_plugin = '') {
$path = ($_plugin == '')
? __DIR__ . '/../../' . $_folder . '/template/' . $_version . '/' . $_filename . '.html'
: __DIR__ . '/../../plugins/' . $_plugin . '/core/template/' . $_version . '/' . $_filename . '.html';
return (file_exists($path)) ? file_get_contents($path) : '';
}
function template_replace($_array, $_subject) {
return str_replace(array_keys($_array), array_values($_array), $_subject);
}
function init($_name, $_default = '') {
if (isset($_GET[$_name])) {
return $_GET[$_name];
}
if (isset($_POST[$_name])) {
return $_POST[$_name];
}
if (isset($_REQUEST[$_name])) {
return $_REQUEST[$_name];
}
return $_default;
}
function sendVarToJS($_varName, $_value) {
$_value = (is_array($_value)) ? 'jQuery.parseJSON("' . addslashes(json_encode($_value, JSON_UNESCAPED_UNICODE)) . '")' : '"' . $_value . '"';
if(strpos($_varName,'.') === false){
echo '';
}else{
echo '';
}
}
function resizeImage($contents, $width, $height) {
// Cacul des nouvelles dimensions
$width_orig = imagesx($contents);
$height_orig = imagesy($contents);
$ratio_orig = $width_orig / $height_orig;
$test = $width / $height > $ratio_orig;
$dest_width = $test ? ceil($height * $ratio_orig) : $width;
$dest_height = $test ? $height : ceil($width / $ratio_orig);
$dest_image = imagecreatetruecolor($width, $height);
$wh = imagecolorallocate($dest_image, 0xFF, 0xFF, 0xFF);
imagefill($dest_image, 0, 0, $wh);
$offcet_x = ($width - $dest_width) / 2;
$offcet_y = ($height - $dest_height) / 2;
if ($dest_image && $contents) {
if (!imagecopyresampled($dest_image, $contents, $offcet_x, $offcet_y, 0, 0, $dest_width, $dest_height, $width_orig, $height_orig)) {
error_log("Error image copy resampled");
return false;
}
}
// start buffering
ob_start();
imagejpeg($dest_image);
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
function getmicrotime() {
list($usec, $sec) = explode(" ", microtime());
return ((float) $usec + (float) $sec);
}
function redirect($_url, $_forceType = null) {
if ($_forceType == 'JS' || headers_sent() || isset($_GET['ajax'])) {
echo '';
} else {
exit(header("Location: $_url"));
}
return;
}
function convertDuration($time) {
$result = '';
$unities = array('j' => 86400, 'h' => 3600, 'min' => 60);
foreach ($unities as $unity => $value) {
if ($time >= $value || $result != '') {
$result .= floor($time / $value) . $unity . ' ';
$time %= $value;
}
}
$result .= $time . 's';
return $result;
}
function getClientIp() {
$sources = array(
'HTTP_X_REAL_IP',
'HTTP_X_FORWARDED_FOR',
'HTTP_CLIENT_IP',
'REMOTE_ADDR',
);
foreach ($sources as $source) {
if (isset($_SERVER[$source])) {
return $_SERVER[$source];
}
}
return '';
}
function mySqlIsHere() {
require_once __DIR__ . '/../class/DB.class.php';
return is_object(DB::getConnection());
}
function displayExeption($e) {
trigger_error('La fonction displayExeption devient displayException', E_USER_DEPRECATED);
return displayException($e);
}
function displayException($e) {
$message = '' . $e->getMessage() . '';
if (DEBUG) {
$message .= 'Show traces';
$message .= '
' . print_r($e->getTrace(), true) . ''; } return $message; } function is_json($_string, $_default = null) { if ($_default !== null) { if (!is_string($_string)) { return $_default; } $return = json_decode($_string, true, 512, JSON_BIGINT_AS_STRING); if (!is_array($return)) { return $_default; } return $return; } return ((is_string($_string) && is_array(json_decode($_string, true, 512, JSON_BIGINT_AS_STRING)))) ? true : false; } function is_sha1($_string = '') { if ($_string == '') { return false; } return preg_match('/^[0-9a-f]{40}$/i', $_string); } function is_sha512($_string = '') { if ($_string == '') { return false; } return preg_match('/^[0-9a-f]{128}$/i', $_string); } function cleanPath($path) { $out = array(); foreach (explode('/', $path) as $i => $fold) { if ($fold == '' || $fold == '.') { continue; } if ($fold == '..' && $i > 0 && end($out) != '..') { array_pop($out); } else { $out[] = $fold; } } return ($path{0} == '/' ? '/' : '') . join('/', $out); } function getRootPath() { return cleanPath(__DIR__ . '/../../'); } function hadFileRight($_allowPath, $_path) { $path = cleanPath($_path); foreach ($_allowPath as $right) { if (strpos($right, '/') !== false || strpos($right, '\\') !== false) { if (strpos($right, '/') !== 0 || strpos($right, '\\') !== 0) { $right = getRootPath() . '/' . $right; } if (dirname($path) == $right || $path == $right) { return true; } } else { if (basename(dirname($path)) == $right || basename($path) == $right) { return true; } } } return false; } function getVersion($_name) { return jeedom::version(); } // got from https://github.com/zendframework/zend-stdlib/issues/58 function polyfill_glob_brace($pattern, $flags) { static $next_brace_sub; if (!$next_brace_sub) { // Find the end of the sub-pattern in a brace expression. $next_brace_sub = function ($pattern, $current) { $length = strlen($pattern); $depth = 0; while ($current < $length) { if ('\\' === $pattern[$current]) { if (++$current === $length) { break; } $current++; } else { if (('}' === $pattern[$current] && $depth-- === 0) || (',' === $pattern[$current] && 0 === $depth)) { break; } elseif ('{' === $pattern[$current++]) { $depth++; } } } return $current < $length ? $current : null; }; } $length = strlen($pattern); // Find first opening brace. for ($begin = 0; $begin < $length; $begin++) { if ('\\' === $pattern[$begin]) { $begin++; } elseif ('{' === $pattern[$begin]) { break; } } // Find comma or matching closing brace. if (null === ($next = $next_brace_sub($pattern, $begin + 1))) { return glob($pattern, $flags); } $rest = $next; // Point `$rest` to matching closing brace. while ('}' !== $pattern[$rest]) { if (null === ($rest = $next_brace_sub($pattern, $rest + 1))) { return glob($pattern, $flags); } } $paths = array(); $p = $begin + 1; // For each comma-separated subpattern. do { $subpattern = substr($pattern, 0, $begin) . substr($pattern, $p, $next - $p) . substr($pattern, $rest + 1); if (($result = polyfill_glob_brace($subpattern, $flags))) { $paths = array_merge($paths, $result); } if ('}' === $pattern[$next]) { break; } $p = $next + 1; $next = $next_brace_sub($pattern, $p); } while (null !== $next); return array_values(array_unique($paths)); } function glob_brace($pattern, $flags = 0) { if (defined("GLOB_BRACE")) { return glob($pattern, $flags + GLOB_BRACE); } else { return polyfill_glob_brace($pattern, $flags); } } function ls($folder = "", $pattern = "*", $recursivly = false, $options = array('files', 'folders')) { if ($folder) { $current_folder = realpath('.'); if (in_array('quiet', $options)) { // If quiet is on, we will suppress the 'no such folder' error if (!file_exists($folder)) { return array(); } } if (!is_dir($folder) || !chdir($folder)) { return array(); } } $get_files = in_array('files', $options); $get_folders = in_array('folders', $options); $both = array(); $folders = array(); // Get the all files and folders in the given directory. if ($get_files) { $both = array(); foreach (glob_brace($pattern, GLOB_MARK) as $file) { if (!is_dir($folder . '/' . $file)) { $both[] = $file; } } } if ($recursivly || $get_folders) { $folders = glob("*", GLOB_ONLYDIR + GLOB_MARK); } //If a pattern is specified, make sure even the folders match that pattern. $matching_folders = array(); if ($pattern !== '*') { $matching_folders = glob($pattern, GLOB_ONLYDIR + GLOB_MARK); } //Get just the files by removing the folders from the list of all files. $all = array_values(array_diff($both, $folders)); if ($recursivly || $get_folders) { foreach ($folders as $this_folder) { if ($get_folders) { //If a pattern is specified, make sure even the folders match that pattern. if ($pattern !== '*') { if (in_array($this_folder, $matching_folders)) { array_push($all, $this_folder); } } else { array_push($all, $this_folder); } } if ($recursivly) { // Continue calling this function for all the folders $deep_items = ls($pattern, $this_folder, $recursivly, $options); # :RECURSION: foreach ($deep_items as $item) { array_push($all, $this_folder . $item); } } } } if ($folder && is_dir($current_folder)) { chdir($current_folder); } if (in_array('datetime_asc', $options)) { global $current_dir; $current_dir = $folder; usort($all, function ($a, $b) { return filemtime($GLOBALS['current_dir'] . '/' . $a) < filemtime($GLOBALS['current_dir'] . '/' . $b); }); } if (in_array('datetime_desc', $options)) { global $current_dir; $current_dir = $folder; usort($all, function ($a, $b) { return filemtime($GLOBALS['current_dir'] . '/' . $a) > filemtime($GLOBALS['current_dir'] . '/' . $b); }); } return $all; } function removeCR($_string) { return trim(str_replace(array("\n", "\r\n", "\r", "\n\r"), '', $_string)); } function rcopy($src, $dst, $_emptyDest = true, $_exclude = array(), $_noError = false, $_params = array()) { if (!file_exists($src)) { return true; } if ($_emptyDest) { rrmdir($dst); } if (is_dir($src)) { if (!file_exists($dst)) { @mkdir($dst); } $files = scandir($src); foreach ($files as $file) { if ($file != "." && $file != ".." && !in_array($file, $_exclude) && !in_array(realpath($src . '/' . $file), $_exclude)) { if (!rcopy($src . '/' . $file, $dst . '/' . $file, $_emptyDest, $_exclude, $_noError, $_params) && !$_noError) { return false; } } } } else { if (!in_array(basename($src), $_exclude) && !in_array(realpath($src), $_exclude)) { $srcSize = filesize($src); if (isset($_params['ignoreFileSizeUnder']) && $srcSize < $_params['ignoreFileSizeUnder']) { if (strpos(realpath($src), 'empty') !== false) { return true; } if (strpos(realpath($src), '.git') !== false) { return true; } if (strpos(realpath($src), '.html') !== false) { return true; } if (strpos(realpath($src), '.txt') !== false) { return true; } if (isset($_params['log']) && $_params['log']) { echo 'Ignore file ' . $src . ' because size is ' . $srcSize . "\n"; } return true; } if (!copy($src, $dst)) { $output = array(); $retval = 0; exec('sudo cp ' . $src . ' ' . $dst, $output, $retval); if ($retval != 0) { if (!$_noError) { return false; } else if (isset($_params['log']) && $_params['log']) { echo 'Error on copy ' . $src . ' to ' . $dst . "\n"; } } } if ($srcSize != filesize($dst)) { if (!$_noError) { return false; } else if (isset($_params['log']) && $_params['log']) { echo 'Error on copy ' . $src . ' to ' . $dst . "\n"; } } return true; } } return true; } function rmove($src, $dst, $_emptyDest = true, $_exclude = array(), $_noError = false, $_params = array()) { if (!file_exists($src)) { return true; } if ($_emptyDest) { rrmdir($dst); } if (is_dir($src)) { if (!file_exists($dst)) { @mkdir($dst); } $files = scandir($src); foreach ($files as $file) { if ($file != "." && $file != ".." && !in_array($file, $_exclude) && !in_array(realpath($src . '/' . $file), $_exclude)) { if (!rmove($src . '/' . $file, $dst . '/' . $file, $_emptyDest, $_exclude, $_noError, $_params) && !$_noError) { return false; } } } } else { if (!in_array(basename($src), $_exclude) && !in_array(realpath($src), $_exclude)) { $srcSize = filesize($src); if (isset($_params['ignoreFileSizeUnder']) && $srcSize < $_params['ignoreFileSizeUnder']) { if (strpos(realpath($src), 'empty') !== false) { return true; } if (strpos(realpath($src), '.git') !== false) { return true; } if (strpos(realpath($src), '.html') !== false) { return true; } if (strpos(realpath($src), '.txt') !== false) { return true; } if (isset($_params['log']) && $_params['log']) { echo 'Ignore file ' . $src . ' because size is ' . $srcSize . "\n"; } return true; } if (!rename($src, $dst)) { $output = array(); $retval = 0; exec('sudo mv ' . $src . ' ' . $dst, $output, $retval); if ($retval != 0) { if (!$_noError) { return false; } else if (isset($_params['log']) && $_params['log']) { echo 'Error on move ' . $src . ' to ' . $dst . "\n"; } } } if ($srcSize != filesize($dst)) { if (!$_noError) { return false; } else if (isset($_params['log']) && $_params['log']) { echo 'Error on move ' . $src . ' to ' . $dst . "\n"; } } return true; } } return true; } // removes files and non-empty directories function rrmdir($dir) { if (is_dir($dir)) { $files = scandir($dir); foreach ($files as $file) { if ($file != "." && $file != "..") { rrmdir("$dir/$file"); } } if (!rmdir($dir)) { $output = array(); $retval = 0; exec('sudo rm -rf ' . $dir, $output, $retval); if ($retval != 0) { return false; } } } else if (file_exists($dir)) { if (!unlink($dir)) { $output = array(); $retval = 0; exec('sudo rm -rf ' . $dir, $output, $retval); if ($retval != 0) { return false; } } } return true; } function date_fr($date_en) { if (config::byKey('language', 'core', 'fr_FR') == 'en_US') { return $date_en; } $texte_long_en = array( "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", ); $texte_short_en = array( "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ); switch (config::byKey('language', 'core', 'fr_FR')) { case 'fr_FR': $texte_long = array( "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche", "Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre", ); $texte_short = array( "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam", "Dim", "Janv.", "Févr.", "Mars", "Avril", "Mai", "Juin", "Juil.", "Août", "Sept.", "Oct.", "Nov.", "Déc.", ); break; case 'de_DE': $texte_long = array( "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag", "Januar", "Februar", "März", "April", "May", "Juni", "July", "August", "September", "October", "November", "December", ); $texte_short = array( "Mon", "Die", "Mit", "Thu", "Don", "Sam", "Son", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ); break; default: return $date_en; break; } return str_replace($texte_short_en, $texte_short, str_replace($texte_long_en, $texte_long, $date_en)); } function convertDayEnToFr($_day) { trigger_error('La fonction convertDayEnToFr devient convertDayFromEn', E_USER_DEPRECATED); return convertDayFromEn($_day); } function convertDayFromEn($_day) { $result = $_day; $daysMapping = array( 'fr_FR' => array( 'Monday' => 'Lundi', 'Mon' => 'Lundi', 'monday' => 'lundi', 'mon' => 'lundi', 'Tuesday' => 'Mardi', 'Tue' => 'Mardi', 'tuesday' => 'mardi', 'tue' => 'mardi', 'Wednesday' => 'Mercredi', 'Wed' => 'Mercredi', 'wednesday' => 'mercredi', 'wed' => 'mercredi', 'Thursday' => 'Jeudi', 'Thu' => 'Jeudi', 'thursday' => 'jeudi', 'thu' => 'jeudi', 'Friday' => 'Vendredi', 'Fri' => 'Vendredi', 'friday' => 'vendredi', 'fri' => 'vendredi', 'Saturday' => 'Samedi', 'Sat' => 'Samedi', 'saturday' => 'samedi', 'sat' => 'samedi', 'Sunday' => 'Dimanche', 'Sun' => 'Dimanche', 'sunday' => 'dimanche', 'sun' => 'dimanche', ), 'de_DE' => array( 'Monday' => 'Montag', 'Mon' => 'Montag', 'monday' => 'montag', 'mon' => 'montag', 'Tuesday' => 'Dienstag', 'Tue' => 'Dienstag', 'tuesday' => 'dienstag', 'tue' => 'dienstag', 'Wednesday' => 'Mittwoch', 'Wed' => 'Mittwoch', 'wednesday' => 'mittwoch', 'wed' => 'mittwoch', 'Thursday' => 'Donnerstag', 'Thu' => 'Donnerstag', 'thursday' => 'donnerstag', 'thu' => 'donnerstag', 'Friday' => 'Freitag', 'Fri' => 'Freitag', 'friday' => 'freitag', 'fri' => 'freitag', 'Saturday' => 'Samstag', 'Sat' => 'Samstag', 'saturday' => 'samstag', 'sat' => 'samstag', 'Sunday' => 'Sonntag', 'Sun' => 'Sonntag', 'sunday' => 'sonntag', 'sun' => 'sonntag', ), ); $language = config::byKey('language', 'core', 'fr_FR'); if (array_key_exists($language, $daysMapping)) { $daysArray = $daysMapping[$language]; if (array_key_exists($_day, $daysArray)) { $result = $daysArray[$_day]; } } return $result; } function create_zip($source_arr, $destination, $_excludes = array()) { if (is_string($source_arr)) { $source_arr = array($source_arr); } if (!extension_loaded('zip')) { throw new Exception('Extension php ZIP non chargée'); } $zip = new ZipArchive(); if (!$zip->open($destination, ZIPARCHIVE::CREATE)) { throw new Exception('Impossible de créer l\'archive ZIP dans le dossier de destination : ' . $destination); } foreach ($source_arr as $source) { if (!file_exists($source)) { continue; } $source = str_replace('\\', '/', realpath($source)); if (is_dir($source) === true) { $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST); foreach ($files as $file) { if (strpos($file, $source) === false) { continue; } if ($file == $source . '/.' || $file == $source . '/..' || in_array(basename($file), $_excludes) || in_array(realpath($file), $_excludes)) { continue; } foreach ($_excludes as $exclude) { if (strpos($file, trim('/' . $exclude . '/', '/')) !== false) { continue (2); } } $file = str_replace('\\', '/', realpath($file)); if (is_dir($file) === true) { $zip->addEmptyDir(str_replace($source . '/', '', $file . '/')); } else if (is_file($file) === true) { $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file)); } } } else if (is_file($source) === true) { $zip->addFromString(basename($source), file_get_contents($source)); } } return $zip->close(); } function br2nl($string) { return preg_replace('/\