Skip to content

Commit

Permalink
Merge pull request #2708 from jeedom/alpha
Browse files Browse the repository at this point in the history
Beta 4.4.8
  • Loading branch information
zoic21 authored Jun 24, 2024
2 parents bc16446 + bc3a42c commit 56fa6b8
Show file tree
Hide file tree
Showing 38 changed files with 429 additions and 114 deletions.
2 changes: 1 addition & 1 deletion core/ajax/plugin.ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
if (!isConnect('admin')) {
throw new Exception(__('401 - Accès non autorisé', __FILE__));
}
$plugin = plugin::byId(init('id'));
$plugin = plugin::byId(init('id'),init('full',0));
$update = update::byLogicalId(init('id'));
$return = utils::o2a($plugin);
$return['activate'] = $plugin->isActive();
Expand Down
19 changes: 18 additions & 1 deletion core/api/jeeApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@
/** @var bool $_RESTRICTED */
global $_RESTRICTED;
$_RESTRICTED = false;


if (init('type') != '') {
try {

if (init('type') == 'ask') {
if (trim(init('token')) == '' || strlen(init('token')) < 64) {
throw new Exception(__('Commande inconnue ou Token invalide', __FILE__));
Expand All @@ -60,6 +63,13 @@
sleep(5);
throw new Exception(__('Vous n\'êtes pas autorisé à effectuer cette action, IP :', __FILE__) . ' ' . getClientIp());
}

if(config::byKey('api::forbidden::method', 'core', '') !== '' && preg_match(config::byKey('api::forbidden::method', 'core', ''), init('type'))){
throw new Exception(__('Cette demande n\'est autorisée', __FILE__) . ' ' . getClientIp());
}
if(config::byKey('api::allow::method', 'core', '') !== '' && !preg_match(config::byKey('api::allow::method', 'core', ''), init('type'))){
throw new Exception(__('Cette demande n\'est autorisée', __FILE__) . ' ' . getClientIp());
}
$type = init('type');
log::add('api', 'debug', __('Demande sur l\'api http venant de :', __FILE__) . ' ' . getClientIp() . ' => ' . json_encode($_GET));

Expand Down Expand Up @@ -225,7 +235,7 @@
if ($type == 'fullData') {
log::add('api', 'debug', __('Demande API pour les commandes', __FILE__));
header('Content-Type: application/json');
echo json_encode(jeeObject::fullData(), JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE, 1024);
echo json_encode(jeeObject::fullData(null,$_USER_GLOBAL), JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE, 1024);
die();
}
if ($type == 'variable') {
Expand Down Expand Up @@ -269,6 +279,13 @@
throw new Exception(__('Requête invalide. Version JSON-RPC invalide :', __FILE__) . ' ' . $jsonrpc->getJsonrpc(), -32001);
}

if(config::byKey('api::forbidden::method', 'core', '') !== '' && preg_match(config::byKey('api::forbidden::method', 'core', ''), $jsonrpc->getMethod())){
throw new Exception(__('Cette demande n\'est autorisée', __FILE__));
}
if(config::byKey('api::allow::method', 'core', '') !== '' && !preg_match(config::byKey('api::allow::method', 'core', ''), $jsonrpc->getMethod())){
throw new Exception(__('Cette demande n\'est autorisée', __FILE__) . ' ' . getClientIp());
}

$params = $jsonrpc->getParams();

if (!isset($params['plugin']) || $params['plugin'] == '') {
Expand Down
9 changes: 9 additions & 0 deletions core/class/DB.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ public static function &Prepare($_query, $_params, $_fetchType = self::FETCH_TYP
$stmt = static::getConnection()->prepare($_query);
$res = NULL;
if ($stmt != false && $stmt->execute($_params) != false) {
if(preg_match('/^update|^replace|^delete|^create|^drop|^alter|^truncate/i', $_query)){
$errorInfo = $stmt->errorInfo();
if ($errorInfo[0] != 0000) {
static::$lastConnection = 0;
throw new Exception('[MySQL] Error code : ' . $errorInfo[0] . ' (' . $errorInfo[1] . '). ' . $errorInfo[2] . ' : ' . $_query);
}
static::$lastConnection = strtotime('now');
return $res;
}
if ($_fetchType == static::FETCH_TYPE_ROW) {
if ($_fetch_opt === null) {
$res = $stmt->fetch($_fetch_param);
Expand Down
50 changes: 32 additions & 18 deletions core/class/cache.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,46 +96,60 @@ public static function stats($_details = false) {
* @name getCache()
* @access public
* @static
* @param string $_engine to override the current cache defined in configuration
* @return \Doctrine\Common\Cache\CacheProvider
*/
public static function getCache() {
if (self::$cache !== null) {
public static function getCache($_engine = null) {
if ($_engine === null && self::$cache !== null) {
return self::$cache;
}
$engine = config::byKey('cache::engine');
if ($engine == 'MemcachedCache' && !class_exists('memcached')) {
$engine = 'FilesystemCache';
config::save('cache::engine', 'FilesystemCache');
}
if ($engine == 'RedisCache' && !class_exists('redis')) {
$engine = 'FilesystemCache';
config::save('cache::engine', 'FilesystemCache');
if( $_engine === null){
// get current cache
$engine = config::byKey('cache::engine');
}else{
// override existing configuration
$engine = $_engine;
config::save('cache::engine', $_engine);
}
switch ($engine) {
case 'FilesystemCache':
self::$cache = new \Doctrine\Common\Cache\FilesystemCache(self::getFolder());
break;
case 'PhpFileCache':
self::$cache = new \Doctrine\Common\Cache\FilesystemCache(self::getFolder());
break;
case 'MemcachedCache':
// check if memcached extention is available
if (!class_exists('memcached')) {
log::add( __CLASS__, 'error', 'memcached extension not installed, fall back to FilesystemCache.');
return self::getCache( 'FilesystemCache');
}
$memcached = new Memcached();
$memcached->addServer(config::byKey('cache::memcacheaddr'), config::byKey('cache::memcacheport'));
self::$cache = new \Doctrine\Common\Cache\MemcachedCache();
self::$cache->setMemcached($memcached);
break;
case 'RedisCache':
// check if redis extension is available
if (!class_exists('redis')) {
log::add( __CLASS__, 'error', 'redis extension not installed, fall back to FilesystemCache.');
return self::getCache( 'FilesystemCache');
}
$redis = new Redis();
$redisAddr = config::byKey('cache::redisaddr');
if (strncmp($redisAddr, '/', 1) === 0) {
$redis->connect($redisAddr);
} else {
$redis->connect($redisAddr, config::byKey('cache::redisport'));
try{
// try to connect to redis
if (strncmp($redisAddr, '/', 1) === 0) {
$redis->connect($redisAddr);
} else {
$redis->connect($redisAddr, config::byKey('cache::redisport'));
}
}catch( Exception $e){
// error : fall back to FilesystemCache
log::add( __CLASS__, 'error', 'Unable to connect to redis instance, fall back to FilesystemCache.'."\n".$e->getMessage());
return self::getCache( 'FilesystemCache');
}
self::$cache = new \Doctrine\Common\Cache\RedisCache();
self::$cache->setRedis($redis);
break;
default:
default: // default is FilesystemCache
self::$cache = new \Doctrine\Common\Cache\FilesystemCache(self::getFolder());
break;
}
Expand Down
2 changes: 1 addition & 1 deletion core/class/cmd.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1658,7 +1658,7 @@ public function toHtml($_version = 'dashboard', $_options = '') {
$coupleArray = explode('|', $element);
$cmdValue = $this->getCmdValue();
if (is_object($cmdValue) && $cmdValue->getType() == 'info') {
if ($cmdValue->execCmd() == $coupleArray[0]) {
if ($cmdValue->execCmd() == $coupleArray[0] || $cmdValue->execCmd() == $coupleArray[1]) {
$listOption .= '<option value="' . $coupleArray[0] . '" selected>' . $coupleArray[1] . '</option>';
$foundSelect = true;
} else {
Expand Down
6 changes: 5 additions & 1 deletion core/class/cron.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ public function isDue() {
return false;
}
try {
$schedule = explode(' ',$this->getSchedule());
if(count($schedule) == 6 && $schedule[5] != strtotime('Y')){
return false;
}
$c = new Cron\CronExpression(checkAndFixCron($this->getSchedule()), new Cron\FieldFactory);
try {
if ($c->isDue()) {
Expand Down Expand Up @@ -551,7 +555,7 @@ public function setDeamonSleepTime($_deamonSleepTime) {
}

public function getOption() {
return json_decode($this->option, true);
return json_decode($this->option ?? '', true);
}

public function getOnce($_default = 0) {
Expand Down
5 changes: 4 additions & 1 deletion core/class/eqLogic.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,9 @@ public static function toHumanReadable($_input) {
}

public static function fromHumanReadable($_input) {
if(empty($_input)){
return $_input;
}
$isJson = false;
if (is_json($_input)) {
$isJson = true;
Expand Down Expand Up @@ -525,7 +528,7 @@ public static function fromHumanReadable($_input) {
return $_input;
}
$text = $_input;
preg_match_all("/#\[(.*?)\]\[(.*?)\]#/", $text, $matches);
preg_match_all( "/#\[(.*?)\]\[(.*?)\]#/", $text, $matches);
if (count($matches) == 3) {
$countMatches = count($matches[0]);
for ($i = 0; $i < $countMatches; $i++) {
Expand Down
3 changes: 3 additions & 0 deletions core/class/jeeObject.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ public static function toHumanReadable($_input) {
}

public static function fromHumanReadable($_input) {
if(empty($_input)){
return $_input;
}
$isJson = false;
if (is_json($_input)) {
$isJson = true;
Expand Down
27 changes: 14 additions & 13 deletions core/class/jeedom.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,19 @@ public static function health() {
'comment' => '',
'key' => 'uptodate'
);

$status = shell_exec('systemctl status fail2ban.service');
$failed = stripos($status, 'failed') !== false;
$running = stripos($status, 'running') !== false;
$state = $failed ? 0 : ($running ? 1 : 2);
$return[] = array(
'name' => __('Etat du service fail2ban', __FILE__),
'state' => $failed ? 0 : ($running ? 1 : 2),
'result' => $failed ? __('En échec', __FILE__) : ($running ? __('Actif', __FILE__) : __('Désactivé', __FILE__)),
'comment' => ($failed || !$running) ? __("Le service Linux fail2ban est désactivé ou en échec : les tentatives d'accès infructueuses à Jeedom ne résulteront pas en un bannissement des IP concernées. Vérifiez l'état du service si vous souhaitez réactiver fail2ban.", __FILE__) : '',
'key' => 'service::fail2ban'
);
if (version_compare(self::getOsVersion(), '12', '<')) {
$status = shell_exec('systemctl status fail2ban.service');
$failed = stripos($status, 'failed') !== false;
$running = stripos($status, 'running') !== false;
$state = $failed ? 0 : ($running ? 1 : 2);
$return[] = array(
'name' => __('Etat du service fail2ban', __FILE__),
'state' => $failed ? 0 : ($running ? 1 : 2),
'result' => $failed ? __('En échec', __FILE__) : ($running ? __('Actif', __FILE__) : __('Désactivé', __FILE__)),
'comment' => ($failed || !$running) ? __("Le service Linux fail2ban est désactivé ou en échec : les tentatives d'accès infructueuses à Jeedom ne résulteront pas en un bannissement des IP concernées. Vérifiez l'état du service si vous souhaitez réactiver fail2ban.", __FILE__) : '',
'key' => 'service::fail2ban'
);
}

$state = (config::byKey('enableCron', 'core', 1, true) != 0) ? true : false;
$return[] = array(
Expand Down Expand Up @@ -459,7 +460,7 @@ public static function health() {
);

if (shell_exec('which python') != '') {
$value = shell_exec('python --version');
$value = shell_exec('python --version 2>&1'); // prior python 3.4, 'python --version' output was on stderr
$return[] = array(
'name' => __('Python', __FILE__),
'state' => true,
Expand Down
2 changes: 1 addition & 1 deletion core/class/jsonrpcClient.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private function send($_request, $_timeout = 15, $_file = null, $_maxRetry = 2)
if (curl_errno($ch)) {
$this->error = 'Erreur curl sur : ' . $this->apiAddr . '. Détail :' . curl_error($ch);
}
curl_close($ch);
unset($ch);
return $response;
}

Expand Down
2 changes: 1 addition & 1 deletion core/class/network.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public static function test($_mode = 'external', $_timeout = 15) {
return false;
}
}
curl_close($ch);
unset($ch);
if (trim($data) != 'ok') {
log::add('network', 'debug', 'Retour NOK sur ' . $url . ' => ' . $data);
return false;
Expand Down
45 changes: 29 additions & 16 deletions core/class/plugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ class plugin {

/* * ***********************Méthodes statiques*************************** */

public static function byId($_id) {
public static function byId($_id,$_full = false) {
global $JEEDOM_INTERNAL_CONFIG;
if (is_string($_id) && isset(self::$_cache[$_id])) {
return self::$_cache[$_id];
if (is_string($_id) && isset(self::$_cache[$_id.'::'.$_full])) {
return self::$_cache[$_id.'::'.$_full];
}
if (!file_exists($_id) || strpos($_id, '/') === false) {
$path = self::getPathById($_id);
Expand Down Expand Up @@ -109,7 +109,6 @@ public static function byId($_id) {
$plugin->changelog_beta = (isset($data['changelog_beta'])) ? str_replace('#language#', config::byKey('language', 'core', 'fr_FR'), $data['changelog_beta']) : '';
$plugin->documentation_beta = (isset($data['documentation_beta'])) ? str_replace('#language#', config::byKey('language', 'core', 'fr_FR'), $data['documentation_beta']) : '';
if (isset($data['specialAttributes'])) {

if (isset($data['specialAttributes']['object'])) {
$plugin->specialAttributes['object'] = $data['specialAttributes']['object'];
}
Expand All @@ -132,16 +131,22 @@ public static function byId($_id) {
'type' => 'class',
);
}
$plugin->functionality['interact'] = array('exists' => method_exists($plugin->getId(), 'interact'), 'controlable' => 1);
$plugin->functionality['cron'] = array('exists' => method_exists($plugin->getId(), 'cron'), 'controlable' => 1);
$plugin->functionality['cron5'] = array('exists' => method_exists($plugin->getId(), 'cron5'), 'controlable' => 1);
$plugin->functionality['cron10'] = array('exists' => method_exists($plugin->getId(), 'cron10'), 'controlable' => 1);
$plugin->functionality['cron15'] = array('exists' => method_exists($plugin->getId(), 'cron15'), 'controlable' => 1);
$plugin->functionality['cron30'] = array('exists' => method_exists($plugin->getId(), 'cron30'), 'controlable' => 1);
$plugin->functionality['cronHourly'] = array('exists' => method_exists($plugin->getId(), 'cronHourly'), 'controlable' => 1);
$plugin->functionality['cronDaily'] = array('exists' => method_exists($plugin->getId(), 'cronDaily'), 'controlable' => 1);
$plugin->functionality['deadcmd'] = array('exists' => method_exists($plugin->getId(), 'deadCmd'), 'controlable' => 0);
$plugin->functionality['health'] = array('exists' => method_exists($plugin->getId(), 'health'), 'controlable' => 0);
if($_full){
$plugin->functionality['interact'] = array('exists' => method_exists($plugin->getId(), 'interact'), 'controlable' => 1);
$plugin->functionality['cron'] = array('exists' => method_exists($plugin->getId(), 'cron'), 'controlable' => 1);
$plugin->functionality['cron5'] = array('exists' => method_exists($plugin->getId(), 'cron5'), 'controlable' => 1);
$plugin->functionality['cron10'] = array('exists' => method_exists($plugin->getId(), 'cron10'), 'controlable' => 1);
$plugin->functionality['cron15'] = array('exists' => method_exists($plugin->getId(), 'cron15'), 'controlable' => 1);
$plugin->functionality['cron30'] = array('exists' => method_exists($plugin->getId(), 'cron30'), 'controlable' => 1);
$plugin->functionality['cronHourly'] = array('exists' => method_exists($plugin->getId(), 'cronHourly'), 'controlable' => 1);
$plugin->functionality['cronDaily'] = array('exists' => method_exists($plugin->getId(), 'cronDaily'), 'controlable' => 1);
$plugin->functionality['deadcmd'] = array('exists' => method_exists($plugin->getId(), 'deadCmd'), 'controlable' => 0);
$plugin->functionality['health'] = array('exists' => method_exists($plugin->getId(), 'health'), 'controlable' => 0);
if($plugin->getCache('usedSpace',-1) == -1){
$plugin->setCache('usedSpace',getDirectorySize(__DIR__ . '/../../plugins/' . $data['id']),86400);
}
$plugin->usedSpace = $plugin->getCache('usedSpace',-1);
}
if (!isset($JEEDOM_INTERNAL_CONFIG['plugin']['category'][$plugin->category])) {
foreach ($JEEDOM_INTERNAL_CONFIG['plugin']['category'] as $key => $value) {
if (!isset($value['alias'])) {
Expand All @@ -153,8 +158,7 @@ public static function byId($_id) {
}
}
}
$plugin->usedSpace = getDirectorySize(__DIR__ . '/../../plugins/' . $data['id']);
self::$_cache[$plugin->id] = $plugin;
self::$_cache[$plugin->id.'::'.$_full] = $plugin;
return $plugin;
}

Expand Down Expand Up @@ -1332,4 +1336,13 @@ public function setWhiteListFolders($paths) {
$this->whiteListFolders = (array) $paths;
return $this;
}

public function getCache($_key = '', $_default = '') {
$cache = cache::byKey('pluginCacheAttr' . $this->getId())->getValue();
return utils::getJsonAttr($cache, $_key, $_default);
}

public function setCache($_key, $_value = null, $_lifetime = 0) {
cache::set('pluginCacheAttr' . $this->getId(), utils::setJsonAttr(cache::byKey('pluginCacheAttr' . $this->getId())->getValue(), $_key, $_value), $_lifetime);
}
}
Loading

0 comments on commit 56fa6b8

Please sign in to comment.