Skip to content

Commit

Permalink
Merge pull request cypht-org#948 from TheDigitalOrchard/lib-framework…
Browse files Browse the repository at this point in the history
…-code-optimizations

Lib framework code optimizations
  • Loading branch information
kroky authored Apr 30, 2024
2 parents 57756bc + eb3fdc4 commit 8fbd655
Show file tree
Hide file tree
Showing 24 changed files with 300 additions and 382 deletions.
8 changes: 4 additions & 4 deletions lib/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Hm_API_Curl {
* Init
* @param string $format format of the result
*/
public function __construct($format='json') {
public function __construct($format = 'json') {
$this->format = $format;
}

Expand All @@ -29,7 +29,7 @@ public function __construct($format='json') {
* @param array $post post fields
* @return array
*/
public function command($url, $headers=array(), $post=array(), $body='', $method=false) {
public function command($url, $headers = [], $post = [], $body = '', $method = false) {
$ch = Hm_Functions::c_init();
if (!$ch) {
return [];
Expand Down Expand Up @@ -86,7 +86,7 @@ private function curl_result($ch) {
}
$result = @json_decode($curl_result, true);
if ($result === NULL) {
return array();
return [];
}
return $result;
}
Expand All @@ -97,7 +97,7 @@ private function curl_result($ch) {
* @return string
*/
private function format_post_data($data) {
$post = array();
$post = [];
if (!is_array($data)) {
return $data;
}
Expand Down
35 changes: 16 additions & 19 deletions lib/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ class Hm_Auth_DB extends Hm_Auth {
*/
public function check_credentials($user, $pass) {
$this->connect();
$row = Hm_DB::execute($this->dbh, 'select hash from hm_user where username = ?', array($user));
if ($row && array_key_exists('hash', $row) && $row['hash'] && Hm_Crypt::check_password($pass, $row['hash'])) {
$row = Hm_DB::execute($this->dbh, 'select hash from hm_user where username = ?', [$user]);
if ($row && !empty($row['hash']) && Hm_Crypt::check_password($pass, $row['hash'])) {
return true;
}
sleep(2);
Expand All @@ -87,7 +87,7 @@ public function check_credentials($user, $pass) {
*/
public function delete($user) {
$this->connect();
if (Hm_DB::execute($this->dbh, 'delete from hm_user where username = ?', array($user))) {
if (Hm_DB::execute($this->dbh, 'delete from hm_user where username = ?', [$user])) {
return true;
}
return false;
Expand Down Expand Up @@ -115,7 +115,7 @@ protected function connect() {
public function change_pass($user, $pass) {
$this->connect();
$hash = Hm_Crypt::hash_password($pass);
if (Hm_DB::execute($this->dbh, 'update hm_user set hash=? where username=?', array($hash, $user))) {
if (Hm_DB::execute($this->dbh, 'update hm_user set hash=? where username=?', [$hash, $user])) {
return true;
}
return false;
Expand All @@ -130,13 +130,13 @@ public function change_pass($user, $pass) {
public function create($user, $pass) {
$this->connect();
$result = 0;
$res = Hm_DB::execute($this->dbh, 'select username from hm_user where username = ?', array($user));
$res = Hm_DB::execute($this->dbh, 'select username from hm_user where username = ?', [$user]);
if (!empty($res)) {
$result = 1;
}
else {
$hash = Hm_Crypt::hash_password($pass);
if (Hm_DB::execute($this->dbh, 'insert into hm_user values(?,?)', array($user, $hash))) {
if (Hm_DB::execute($this->dbh, 'insert into hm_user values(?,?)', [$user, $hash])) {
$result = 2;
}
}
Expand All @@ -159,7 +159,7 @@ public function __construct($config) {
}

/* IMAP authentication server settings */
private $imap_settings = array();
private $imap_settings = [];

/**
* @param object $imap imap connection object
Expand Down Expand Up @@ -196,11 +196,11 @@ public function check_credentials($user, $pass) {
Hm_Debug::add('Invalid IMAP auth configuration settings');
return false;
}
$this->imap_settings = array('server' => $server, 'port' => $port,
$this->imap_settings = ['server' => $server, 'port' => $port,
'tls' => $tls, 'username' => $user, 'password' => $pass,
'no_caps' => false, 'blacklisted_extensions' => array('enable'),
'no_caps' => false, 'blacklisted_extensions' => ['enable'],
'sieve_config_host' => $sieve_config
);
];
return $this->check_connection($imap);
}

Expand All @@ -219,7 +219,7 @@ public function save_auth_detail($session) {
*/
class Hm_Auth_LDAP extends Hm_Auth {

protected $config = array();
protected $config = [];
protected $fh;
protected $source = 'ldap';

Expand All @@ -231,7 +231,7 @@ private function connect_details() {
$prefix = 'ldaps://';
$server = $this->apply_config_value('server', 'localhost');
$port = $this->apply_config_value('port', 389);
if (array_key_exists('enable_tls', $this->config) && !$this->config['enable_tls']) {
if (!empty($this->config['enable_tls'])) {
$prefix = 'ldap://';
}
return $prefix.$server.':'.$port;
Expand All @@ -243,10 +243,7 @@ private function connect_details() {
* @return mixed
*/
private function apply_config_value($name, $default) {
if (array_key_exists($name, $this->config) && trim($this->config[$name])) {
return $this->config[$name];
}
return $default;
return !empty($this->config[$name]) ? $this->config[$name] : $default;
}

/**
Expand All @@ -260,14 +257,14 @@ public function check_credentials($user, $pass) {
$base_dn = $this->site_config->get('ldap_auth_base_dn', false);
if ($server && $port && $base_dn) {
$user = sprintf('cn=%s,%s', $user, $base_dn);
$this->config = array(
$this->config = [
'server' => $server,
'port' => $port,
'enable_tls' => $tls,
'base_dn' => $base_dn,
'user' => $user,
'pass' => $pass
);
];
return $this->connect();
}
Hm_Debug::add('Invalid LDAP auth configuration settings');
Expand Down Expand Up @@ -315,7 +312,7 @@ function get_auth_config($config, $prefix) {
$server = $config->get($prefix.'_auth_server', false);
$port = $config->get($prefix.'_auth_port', false);
$tls = $config->get($prefix.'_auth_tls', false);
$ret = array($server, $port, $tls);
$ret = [$server, $port, $tls];
if ($prefix == 'imap') {
$ret[] = $config->get($prefix.'_auth_sieve_conf_host', false);
}
Expand Down
20 changes: 10 additions & 10 deletions lib/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
trait Hm_Uid_Cache {

/* UID list */
private static $read = array();
private static $unread = array();
private static $read = [];
private static $unread = [];

/* Load UIDs from an outside source
* @param array $data list of uids
Expand Down Expand Up @@ -75,7 +75,7 @@ public static function is_read($uid) {
* @return array list of known UIDs
*/
public static function dump() {
return array(array_keys(self::$read), array_keys(self::$unread));
return [array_keys(self::$read), array_keys(self::$unread)];
}

/**
Expand Down Expand Up @@ -153,7 +153,7 @@ public function del($key) {
* @param string $crypt_key encryption key
* @return boolean
*/
public function set($key, $val, $lifetime=600, $crypt_key='') {
public function set($key, $val, $lifetime = 600, $crypt_key = '') {
if (!$this->is_active()) {
return false;
}
Expand All @@ -165,7 +165,7 @@ public function set($key, $val, $lifetime=600, $crypt_key='') {
* @param string $crypt_key encryption key
* @return false|array|string
*/
public function get($key, $crypt_key='') {
public function get($key, $crypt_key = '') {
if (!$this->is_active()) {
return false;
}
Expand Down Expand Up @@ -258,8 +258,7 @@ private function connect() {
$this->cache_con = false;
return false;
}
}
catch (Exception $oops) {
} catch (Exception $oops) {
Hm_Debug::add('Redis connect failed');
$this->cache_con = false;
return false;
Expand Down Expand Up @@ -373,6 +372,7 @@ class Hm_Noop_Cache {
public function del($key) {
return true;
}

public function set($key, $val, $lifetime, $crypt_key) {
return false;
}
Expand Down Expand Up @@ -475,7 +475,7 @@ protected function log($key, $msg_type) {
* @param boolean $session store in the session instead of the enabled cache
* @return boolean
*/
public function set($key, $val, $lifetime=600, $session=false) {
public function set($key, $val, $lifetime = 600, $session = false) {
if ($session || $this->type == 'session') {
return $this->session_set($key, $val, false);
}
Expand All @@ -488,7 +488,7 @@ public function set($key, $val, $lifetime=600, $session=false) {
* @param boolean $session fetch from the session instead of the enabled cache
* @return mixed
*/
public function get($key, $default=false, $session=false) {
public function get($key, $default = false, $session = false) {
if ($session || $this->type == 'session') {
return $this->session_get($key, $default);
}
Expand All @@ -500,7 +500,7 @@ public function get($key, $default=false, $session=false) {
* @param boolean $session fetch from the session instead of the enabled cache
* @return boolean
*/
public function del($key, $session=false) {
public function del($key, $session = false) {
if ($session || $this->type == 'session') {
return $this->session_del($key);
}
Expand Down
Loading

0 comments on commit 8fbd655

Please sign in to comment.