Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unicode Support: Replace Standard PHP String Functions with Multibyte Counterparts #1051

Merged
merged 16 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,8 @@ public function load($all_configs, $key) {
*/
private function get_user_defaults() {
foreach ($this->config as $name => $val) {
if (substr($name, 0, 15) == 'default_setting') {
$this->user_defaults[substr($name, 16).'_setting'] = $val;
if (mb_substr($name, 0, 15) == 'default_setting') {
$this->user_defaults[mb_substr($name, 16).'_setting'] = $val;
}
}
}
Expand All @@ -512,7 +512,7 @@ public function get_modules() {
*/
function load_user_config_object($config) {
$type = $config->get('user_config_type', 'file');
if (strstr($type, ':')) {
if (mb_strstr($type, ':')) {
list($type, $class) = explode(':', $type);
}
switch ($type) {
Expand Down
2 changes: 1 addition & 1 deletion lib/crypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public static function hash_password($password, $salt = false, $count = false, $
*/
public static function check_password($password, $hash) {
$type = 'php';
if (substr($hash, 0, 6) === 'sha512') {
if (mb_substr($hash, 0, 6) === 'sha512') {
$type = 'pbkdf2';
}
if (function_exists('password_verify') && $type === 'php') {
Expand Down
2 changes: 1 addition & 1 deletion lib/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ static public function execute($dbh, $sql, $args, $type = false, $all = false) {
* @return string
*/
static private function execute_type($sql) {
switch(substr($sql, 0, 1)) {
switch(mb_substr($sql, 0, 1)) {
case 'd':
case 'u':
case 'i':
Expand Down
2 changes: 1 addition & 1 deletion lib/dispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function validate_request_uri($uri) {
if (array_key_exists('scheme', $parts)) {
return '/';
}
if ($parts === false || !array_key_exists('path', $parts) || strpos($parts['path'], '..') !== false) {
if ($parts === false || !array_key_exists('path', $parts) || mb_strpos($parts['path'], '..') !== false) {
return '/';
}
return $uri;
Expand Down
4 changes: 2 additions & 2 deletions lib/environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ function process_config_array($filename) {
if (is_array($array)) {
return array_map(function ($value) {
return is_array($value) ? $value : (
is_string($value) && strtolower($value) === 'true' ? true : (
is_string($value) && strtolower($value) === 'false' ? false : $value
is_string($value) && mb_strtolower($value) === 'true' ? true : (
is_string($value) && mb_strtolower($value) === 'false' ? false : $value
)
);
}, $array);
Expand Down
4 changes: 2 additions & 2 deletions lib/format.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ public static function stringify($data, $encoding = 'base64_encode') {
* @return array|false
*/
public static function convert($data) {
if (substr($data, 0, 2) === 'a:') {
if (mb_substr($data, 0, 2) === 'a:') {
return @unserialize($data);
} elseif (substr($data, 0, 1) === '{' || substr($data, 0, 1) === '[') {
} elseif (mb_substr($data, 0, 1) === '{' || mb_substr($data, 0, 1) === '[') {
return @json_decode($data, true);
}
return false;
Expand Down
6 changes: 3 additions & 3 deletions lib/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ trait Hm_Handler_Validate {
* @return bool
*/
public function validate_method($session, $request) {
if (!in_array(strtolower($request->method), ['get', 'post'], true)) {
if (!in_array(mb_strtolower($request->method), ['get', 'post'], true)) {
if ($session->loaded) {
$session->destroy($request);
Hm_Debug::add(sprintf('LOGGED OUT: invalid method %s', $request->method));
Expand Down Expand Up @@ -427,7 +427,7 @@ public function process_form($form) {
* @return bool
*/
public function module_is_supported($name) {
return in_array(strtolower($name), $this->config->get_modules(true), true);
return in_array(mb_strtolower($name), $this->config->get_modules(true), true);
}

public function save_hm_msgs() {
Expand Down Expand Up @@ -529,7 +529,7 @@ public function translate_number($number) {
if (!is_numeric($number) || !in_array($this->lang, ['fa'])) {
return $number;
}
$number_splitted = str_split($number);
$number_splitted = mb_str_split($number);
$translated_number = "";
foreach ($number_splitted as $number_splitted) {
$translated_number .= $this->trans($number_splitted);
Expand Down
10 changes: 5 additions & 5 deletions lib/request.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ private function is_mobile() {
* @return void
*/
private function is_tls() {
if (!empty($this->server['HTTPS']) && strtolower($this->server['HTTPS']) == 'on') {
if (!empty($this->server['HTTPS']) && mb_strtolower($this->server['HTTPS']) == 'on') {
$this->tls = true;
} elseif (!empty($this->server['REQUEST_SCHEME']) && strtolower($this->server['REQUEST_SCHEME']) == 'https') {
} elseif (!empty($this->server['REQUEST_SCHEME']) && mb_strtolower($this->server['REQUEST_SCHEME']) == 'https') {
$this->tls = true;
}
}
Expand All @@ -213,7 +213,7 @@ private function get_request_type() {
* @return bool true if the request is from an AJAX call
*/
public function is_ajax() {
return !empty($this->server['HTTP_X_REQUESTED_WITH']) && strtolower($this->server['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
return !empty($this->server['HTTP_X_REQUESTED_WITH']) && mb_strtolower($this->server['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
}

/**
Expand All @@ -222,14 +222,14 @@ public function is_ajax() {
* @return string clean url path
*/
private function get_clean_url_path($uri) {
if (strpos($uri, '?') !== false) {
if (mb_strpos($uri, '?') !== false) {
$parts = explode('?', $uri, 2);
$path = $parts[0];
} else {
$path = $uri;
}
$path = str_replace('index.php', '', $path);
if (substr($path, -1) != '/') {
if (mb_substr($path, -1) != '/') {
$path .= '/';
}
return $path;
Expand Down
12 changes: 6 additions & 6 deletions lib/scram.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ScramAuthenticator {
);

private function getHashAlgorithm($scramAlgorithm) {
$parts = explode('-', strtolower($scramAlgorithm));
$parts = explode('-', mb_strtolower($scramAlgorithm));
return $this->hashes[$parts[1]] ?? 'sha1'; // Default to sha1 if the algorithm is not found
}
private function log($message) {
Expand Down Expand Up @@ -51,10 +51,10 @@ public function authenticateScram($scramAlgorithm, $username, $password, $getSer
$scramCommand = 'AUTHENTICATE ' . $scramAlgorithm . "\r\n";
$sendCommand($scramCommand);
$response = $getServerResponse();
if (!empty($response) && substr($response[0], 0, 2) == '+ ') {
if (!empty($response) && mb_substr($response[0], 0, 2) == '+ ') {
$this->log("Received server challenge: " . $response[0]);
// Extract salt and server nonce from the server's challenge
$serverChallenge = base64_decode(substr($response[0], 2));
$serverChallenge = base64_decode(mb_substr($response[0], 2));
$parts = explode(',', $serverChallenge);
$serverNonce = base64_decode(substr($parts[0], strpos($parts[0], "=") + 1));
$salt = base64_decode(substr($parts[1], strpos($parts[1], "=") + 1));
Expand All @@ -67,7 +67,7 @@ public function authenticateScram($scramAlgorithm, $username, $password, $getSer
$clientProof = $this->generateClientProof($username, $password, $salt, $clientNonce, $serverNonce, $algorithm);

// Construct client final message
$channelBindingData = (stripos($scramAlgorithm, 'plus') !== false) ? 'c=' . base64_encode('tls-unique') . ',' : 'c=biws,';
$channelBindingData = (mb_stripos($scramAlgorithm, 'plus') !== false) ? 'c=' . base64_encode('tls-unique') . ',' : 'c=biws,';
$clientFinalMessage = $channelBindingData . 'r=' . $serverNonce . $clientNonce . ',p=' . $clientProof;
$clientFinalMessageEncoded = base64_encode($clientFinalMessage);
$this->log("Sending client final message: " . $clientFinalMessageEncoded);
Expand All @@ -76,8 +76,8 @@ public function authenticateScram($scramAlgorithm, $username, $password, $getSer

// Verify server's response
$response = $getServerResponse();
if (!empty($response) && substr($response[0], 0, 2) == '+ ') {
$serverFinalMessage = base64_decode(substr($response[0], 2));
if (!empty($response) && mb_substr($response[0], 0, 2) == '+ ') {
$serverFinalMessage = base64_decode(mb_substr($response[0], 2));
$parts = explode(',', $serverFinalMessage);
$serverProof = substr($parts[0], strpos($parts[0], "=") + 1);

Expand Down
2 changes: 1 addition & 1 deletion lib/session_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function is_admin() {
return false;
}
$user = $this->get('username', '');
if (!strlen($user)) {
if (!mb_strlen($user)) {
return false;
}
return in_array($user, $admins, true);
Expand Down
36 changes: 18 additions & 18 deletions lib/webdav_formats.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ private function process_value($value, $type = false) {
* @return boolean
*/
private function invalid_prop($prop) {
if (strtolower(substr($prop, 0, 2)) == 'x-') {
if (mb_strtolower(mb_substr($prop, 0, 2)) == 'x-') {
return false;
}
foreach ($this->properties as $name => $value) {
if (strtolower($prop) == strtolower($value)) {
if (mb_strtolower($prop) == mb_strtolower($value)) {
return false;
}
}
Expand All @@ -80,7 +80,7 @@ private function invalid_prop($prop) {
*/
private function invalid_param($param) {
foreach ($this->parameters as $val) {
if (strtolower($param) == strtolower($val)) {
if (mb_strtolower($param) == mb_strtolower($val)) {
return false;
}
}
Expand Down Expand Up @@ -120,7 +120,7 @@ private function parse_prop_params($vals) {
if ($this->invalid_param($pair[0])) {
continue;
}
$res[strtolower($pair[0])] = $this->flatten(
$res[mb_strtolower($pair[0])] = $this->flatten(
$this->process_value($pair[1]));
}
}
Expand Down Expand Up @@ -258,7 +258,7 @@ private function is_type($type, $vals) {
}
if (is_array($vals['type']) && in_array($type, $vals['type'])) {
return true;
} elseif (strtolower($type) == strtolower($vals['type'])) {
} elseif (mb_strtolower($type) == mb_strtolower($vals['type'])) {
return true;
}
return false;
Expand Down Expand Up @@ -293,10 +293,10 @@ private function parse($lines) {
$data['values'] = $this->flatten(
$this->process_value($vals[1], $prop['prop']));
$data['id'] = $id;
if (array_key_exists(strtolower($prop['prop']), $this->data)) {
$this->data[strtolower($prop['prop'])][] = $data;
if (array_key_exists(mb_strtolower($prop['prop']), $this->data)) {
$this->data[mb_strtolower($prop['prop'])][] = $data;
} else {
$this->data[strtolower($prop['prop'])] = [$data];
$this->data[mb_strtolower($prop['prop'])] = [$data];
}
}
$this->data['raw'] = $this->raw_card;
Expand All @@ -314,13 +314,13 @@ private function is_valid($lines) {
if (count($lines) < 4) {
$res = false;
}
if (count($lines) > 0 && strtolower(substr($lines[0], 0, 5)) != 'begin') {
if (count($lines) > 0 && mb_strtolower(mb_substr($lines[0], 0, 5)) != 'begin') {
$res = false;
}
if (count($lines) > 1 && strtolower(substr($lines[1], 0, 7)) != 'version') {
if (count($lines) > 1 && mb_strtolower(mb_substr($lines[1], 0, 7)) != 'version') {
$res = false;
}
if (count($lines) && strtolower(substr($lines[(count($lines) - 1)], 0, 3)) != 'end') {
if (count($lines) && mb_strtolower(mb_substr($lines[(count($lines) - 1)], 0, 3)) != 'end') {
$res = false;
}
if (!$res) {
Expand Down Expand Up @@ -362,7 +362,7 @@ protected function format_vcard_generic($name) {
$res;
}
foreach ($vals as $val) {
$name = substr($name, 0, 2) == 'x-' ? $name : strtoupper($name);
$name = mb_substr($name, 0, 2) == 'x-' ? $name : mb_strtoupper($name);
$params = array_merge([$name], $this->build_vcard_params($val));
$res[] = sprintf("%s:%s", implode(';', $params), $val['values']);
}
Expand All @@ -377,9 +377,9 @@ protected function format_vcard_generic($name) {
protected function build_vcard_params($fld_val) {
$props = [];
foreach ($this->parameters as $param) {
if (array_key_exists(strtolower($param), $fld_val)) {
$props[] = sprintf('%s=%s', strtoupper($param),
$this->combine($fld_val[strtolower($param)]));
if (array_key_exists(mb_strtolower($param), $fld_val)) {
$props[] = sprintf('%s=%s', mb_strtoupper($param),
$this->combine($fld_val[mb_strtolower($param)]));
}
}
return $props;
Expand Down Expand Up @@ -461,7 +461,7 @@ protected function parse_n($vals) {
protected function format_addr($vals) {
$name = 'address';
if (array_key_exists('type', $vals)) {
$name = sprintf('%s_address', strtolower($vals['type']));
$name = sprintf('%s_address', mb_strtolower($vals['type']));
}
$vals = $vals['values'];
$street = $vals['street'];
Expand Down Expand Up @@ -575,9 +575,9 @@ protected function parse_trigger($vals) {
protected function parse_dt($vals) {
foreach ($vals as $index => $dates) {
$dt = $vals[0]['values'];
if (substr($dt, -1, 1) == 'Z') {
if (mb_substr($dt, -1, 1) == 'Z') {
$vals[0]['tzid'] = 'UTC';
$dt = substr($dt, 0, -1);
$dt = mb_substr($dt, 0, -1);
}
$vals[$index]['values'] = date_parse_from_format('Ymd\THis', $dt);
}
Expand Down
2 changes: 1 addition & 1 deletion modules/2fa/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ protected function output() {
$lang = 'en-us';
$dir = 'ltr';
if ($this->lang) {
$lang = strtolower(str_replace('_', '-', $this->lang));
$lang = mb_strtolower(str_replace('_', '-', $this->lang));
}
if ($this->dir) {
$dir = $this->dir;
Expand Down
6 changes: 3 additions & 3 deletions modules/advanced_search/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ private function imap_search($flags, $imap, $params, $limit) {
}
$server_details = Hm_IMAP_List::dump($this->imap_id);
foreach ($imap->get_message_list($msgs) as $msg) {
if (array_key_exists('content-type', $msg) && stristr($msg['content-type'], 'multipart/mixed')) {
if (array_key_exists('content-type', $msg) && mb_stristr($msg['content-type'], 'multipart/mixed')) {
$msg['flags'] .= ' \Attachment';
}
if (stristr($msg['flags'], 'deleted')) {
if (mb_stristr($msg['flags'], 'deleted')) {
continue;
}
$msg['server_id'] = $this->imap_id;
Expand All @@ -131,7 +131,7 @@ private function imap_search($flags, $imap, $params, $limit) {
}

private function validate_source($val) {
if (substr_count($val, '_') !== 2) {
if (mb_substr_count($val, '_') !== 2) {
return false;
}
$source_parts = explode('_', $val);
Expand Down
4 changes: 2 additions & 2 deletions modules/calendar/hm-calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ private function output_event_details($event) {
$res = '<div class="event_details">'.
'<div class="event_title d-flex flex-column border-bottom mb-2"><span class="fs-5">'.$this->output_mod->html_safe($event['title']).'</span>'.
'<small class="event_date fw-lighter">'.$this->output_mod->html_safe(date('H:i A', $event['ts'])).'</small></div>';
if (strlen(trim($event['description']))) {
if (mb_strlen(trim($event['description']))) {
$res .= '<div class="event_detail mb-2">'.$this->output_mod->html_safe($event['description']).'</div>';
}
if (strlen(trim($event['repeat_interval']))) {
if (mb_strlen(trim($event['repeat_interval']))) {
$res .= '<div class="event_repeat"><small class="fw-lighter fst-italic">'.$this->output_mod->trans(sprintf('Repeats every %s', $event['repeat_interval'])).'</small></div>';
}
$res .= '<form method="post"><input type="hidden" name="delete_ts" value="'.$this->output_mod->html_safe($event['ts']).'" />'.
Expand Down
4 changes: 2 additions & 2 deletions modules/carddav_contacts/hm-carddav.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function add_contact($form) {
$card = array('BEGIN:VCARD', 'VERSION:3', sprintf('UID:%s', $uid));
foreach ($this->card_flds as $name => $cname) {
if (array_key_exists($name, $form) && trim($form[$name])) {
$card[] = sprintf('%s:%s', strtoupper($cname), $form[$name]);
$card[] = sprintf('%s:%s', mb_strtoupper($cname), $form[$name]);
}
}
$card[] = 'END:VCARD';
Expand Down Expand Up @@ -206,7 +206,7 @@ private function discover() {
}

private function parse_xml($xml) {
if (substr((string) $this->api->last_status, 0, 1) != '2') {
if (mb_substr((string) $this->api->last_status, 0, 1) != '2') {
Hm_Debug::add(sprintf('ERRUnable to access CardDav server (%d)', $this->api->last_status));
return false;
}
Expand Down
Loading