Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: mbstring, openssl
extensions: mbstring, openssl, imagick
coverage: none

- name: Get Composer cache directory
Expand Down
6 changes: 3 additions & 3 deletions PHPUtils/Crypto.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function genIV(string $method): string {
public function encryptwithpw(string $str, string $password, string $method = 'aes-256-cbc', bool $iv = false): string {
if ($iv) {
$ivRaw = hex2bin($this->genIV($method));
$encrypted = openssl_encrypt($str, $method, $password, iv: $ivRaw);
$encrypted = openssl_encrypt($str, $method, $password, OPENSSL_RAW_DATA, $ivRaw);
return base64_encode($ivRaw . $encrypted);
}
return openssl_encrypt($str, $method, $password);
Expand All @@ -63,13 +63,13 @@ public function decryptwithpw(string $str, string $password, string $method = 'a
if ($iv !== '') {
$ivRaw = hex2bin($iv);
$ciphertext = base64_decode($str, true) ?: $str;
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fallback logic base64_decode($str, true) ?: $str creates API ambiguity when an external IV is provided. The function accepts both base64-encoded and raw ciphertext, but this dual behavior is not documented in the docstring. Consider either: (1) requiring a specific format and documenting it clearly, or (2) adding a parameter to explicitly specify the input format. The current implementation may lead to silent errors if a user mistakenly passes the wrong format.

Copilot uses AI. Check for mistakes.
return openssl_decrypt($ciphertext, $method, $password, iv: $ivRaw);
return openssl_decrypt($ciphertext, $method, $password, OPENSSL_RAW_DATA, $ivRaw);
}
$decoded = base64_decode($str, true);
if ($decoded !== false && strlen($decoded) > $ivLen) {
$ivRaw = substr($decoded, 0, $ivLen);
$ciphertext = substr($decoded, $ivLen);
return openssl_decrypt($ciphertext, $method, $password, iv: $ivRaw);
return openssl_decrypt($ciphertext, $method, $password, OPENSSL_RAW_DATA, $ivRaw);
}
return openssl_decrypt($str, $method, $password);
}
Expand Down
41 changes: 14 additions & 27 deletions PHPUtils/SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function sqlite_get_tables(\SQLite3 $db) : array {
$tables[] = $row['name'];
}
return $this->res("SUCCESS", $tables);
} catch (Exception $e) {
} catch (\Exception $e) {
return $this->res("ERROR", $e->getMessage());
}
}
Expand All @@ -130,7 +130,7 @@ public function sqlite_get_tables(\SQLite3 $db) : array {
*/
public function sqlite_create_table(\SQLite3 $db, string $table_name, array $columns = ["column_name" => "column_type"]) : array {
try {
if (sqlite_table_exists($db, $table_name)) {
if ($this->sqlite_table_exists($db, $table_name)) {
return $this->res("ERROR", "Table $table_name already exists.");
}
$query = 'CREATE TABLE IF NOT EXISTS "'.$table_name.'" (
Expand All @@ -142,7 +142,7 @@ public function sqlite_create_table(\SQLite3 $db, string $table_name, array $col
$query = rtrim($query, ', ');
$query .= ');';
return $this->res("SUCCESS", $db->query($query));
} catch (Exception $e) {
} catch (\Exception $e) {
return $this->res("ERROR", $e->getMessage());
}
}
Expand All @@ -158,7 +158,7 @@ public function sqlite_drop_table(\SQLite3 $db, string $table_name) : array {
try {
$query = 'DROP TABLE IF EXISTS "'.$table_name.'";';
return $this->res("SUCCESS", $db->query($query));
} catch (Exception $e) {
} catch (\Exception $e) {
return $this->res("ERROR", $e->getMessage());
}
}
Expand All @@ -174,7 +174,7 @@ public function sqlite_empty_table(\SQLite3 $db, string $table_name) : array {
try {
$query = 'DELETE FROM "'.$table_name.'";';
return $this->res("SUCCESS", $db->query($query));
} catch (Exception $e) {
} catch (\Exception $e) {
return $this->res("ERROR", $e->getMessage());
}
}
Expand All @@ -193,7 +193,7 @@ public function sqlite_table_exists(\SQLite3 $db, string $table_name) : array {
$query = 'SELECT name FROM sqlite_master WHERE type="table" AND name="'.$table_name.'";';
$result = $db->querySingle($query);
return $this->res("SUCCESS", ($result === $table_name));
} catch (Exception $e) {
} catch (\Exception $e) {
return $this->res("ERROR", $e->getMessage());
}
}
Expand All @@ -219,7 +219,7 @@ public function sqlite_get_columns(\SQLite3 $db, string $table_name) : array {
];
}
return $this->res("SUCCESS", $columns);
} catch (Exception $e) {
} catch (\Exception $e) {
return $this->res("ERROR", $e->getMessage());
}
}
Expand All @@ -243,7 +243,7 @@ public function sqlite_query(?\SQLite3 $db, $query) : array {
return $this->res("ERROR", $db->lastErrorMsg());
}
return $this->res("SUCCESS", $query);
} catch (Exception $e) {
} catch (\Exception $e) {
return $this->res("ERROR", $e->getMessage());
}
}
Expand Down Expand Up @@ -277,7 +277,7 @@ public function sqlite_insert(\SQLite3 $db, string $table, array $data) : array
return $this->res("ERROR", $db->lastErrorMsg());
}
return $this->res("SUCCESS", $insert);
} catch (Exception $e) {
} catch (\Exception $e) {
return $this->res("ERROR", $e->getMessage());
}
}
Expand All @@ -296,7 +296,7 @@ public function sqlite_insert(\SQLite3 $db, string $table, array $data) : array
*/
public function sqlite_select(\SQLite3 $db, string $table, array $columns = ["*"], string $where = "", array $params = [], string $order = "", string $limit = "") : array {
try {
if (!sqlite_table_exists($db, $table)) {
if (!$this->sqlite_table_exists($db, $table)) {
return $this->res("ERROR", "Table $table does not exist.");
}
$columns = implode(', ', $columns);
Expand Down Expand Up @@ -324,7 +324,7 @@ public function sqlite_select(\SQLite3 $db, string $table, array $columns = ["*"
return $this->res("ERROR", "No columns returned.");
}
return $this->res("SUCCESS", $result);
} catch (Exception $e) {
} catch (\Exception $e) {
return $this->res("ERROR", $e->getMessage());
}
}
Expand Down Expand Up @@ -365,7 +365,7 @@ public function sqlite_update(\SQLite3 $db, string $table, array $data, string $

$stmt->execute();
return $this->res("SUCCESS", $stmt->changes > 0);
} catch (Exception $e) {
} catch (\Exception $e) {
return $this->res("ERROR", $e->getMessage());
}
}
Expand Down Expand Up @@ -394,7 +394,7 @@ public function sqlite_delete(\SQLite3 $db, string $table, string $where, array

$stmt->execute();
return $this->res("SUCCESS", $stmt->changes > 0);
} catch (Exception $e) {
} catch (\Exception $e) {
return $this->res("ERROR", $e->getMessage());
}
}
Expand Down Expand Up @@ -425,20 +425,7 @@ public function sqlite_function($func_name, ...$params) : array {
}
$call = call_user_func($func_name, ...$params);
return $this->res($call["status"], $call["data"]);
$call_status = $call["status"];
$call_data = $call["data"];
if (is_string($call_data)) {
$call_data = clean($call_data);
}
if ($call_status == "ERROR") {
return $this->res("ERROR", $call_data);
}
if ($call_data["status"] == "ERROR") {
return $this->res("ERROR", $call_data);
}
return $this->res($call_status, $call_data);

} catch (Exception $e) {
} catch (\Exception $e) {
return $this->res("ERROR", $e->getMessage());
}
}
Expand Down
6 changes: 3 additions & 3 deletions PHPUtils/Times.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public function getCurrentTime(string $format, string $timezone): string {
* @return string The relative time
*/
public function relativeTime($time, $format = null) {
$now = new \DateTime('now');
$then = new \DateTime($time);
$diff = $now->diff($then);
$currentTime = new \DateTime('now');
$inputTime = new \DateTime($time);
$diff = $currentTime->diff($inputTime);

# Translate the format
$formats = [
Expand Down
Loading