Non puoi selezionare più di 25 argomenti
Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
175 righe
4.6 KiB
175 righe
4.6 KiB
<?php |
|
|
|
define('AES_256_CBC', 'aes-256-cbc'); |
|
|
|
if(! function_exists("simple_encrypt")) { |
|
|
|
function simple_encrypt($text, $salt) { |
|
|
|
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length(AES_256_CBC)); |
|
return trim(openssl_encrypt($text, AES_256_CBC, $salt, 0, $iv).":".base64_encode($iv)); |
|
|
|
} |
|
|
|
} |
|
|
|
if(! function_exists("simple_decrypt")) { |
|
|
|
function simple_decrypt($text, $salt) { |
|
|
|
$parts = explode(':', $text); |
|
return openssl_decrypt($parts[0], AES_256_CBC, $salt, 0, base64_decode($parts[1])); |
|
|
|
} |
|
|
|
} |
|
|
|
if(! function_exists("adaptMemoryLimitToFileSize")) { |
|
|
|
function adaptMemoryLimitToFileSize(String $path, String $limit = "4G") { |
|
|
|
if (file_exists($path)) { |
|
|
|
$size = filesize($path); |
|
if ($size > 500 * 1024 * 1024) { $limit = "-1";} |
|
|
|
$actual_memory = ini_get("memory_limit"); |
|
if ($actual_memory != "-1" && $actual_memory != "4G" && $size > 2000000 ) { |
|
|
|
ini_set("memory_limit", $limit); |
|
ini_set("max_execution_time", "600"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if(! function_exists("setup_db_connection")) { |
|
|
|
function setup_db_connection(int $tenant = null) |
|
{ |
|
if(! defined("_MYSQL_DT")) { |
|
define("_MYSQL_DT", "Y-m-d H:i:s"); |
|
} |
|
|
|
try { |
|
|
|
\Illuminate\Support\Facades\DB::connection('mysql')->getPdo(); |
|
|
|
} catch (\Throwable $th) { |
|
|
|
get_config_variables(); |
|
|
|
config([ |
|
"database.connections.mysql.host" => config("json.db_host", null), |
|
"database.connections.mysql.database" => config("json.db_name", null), |
|
"database.connections.mysql.username" => config("json.db_root_user", null), |
|
"database.connections.mysql.password" => config("json.db_root_pass", null) |
|
]); |
|
|
|
\Illuminate\Support\Facades\DB::purge('mysql'); |
|
|
|
} |
|
|
|
if(! empty($tenant) && is_numeric($tenant)) { |
|
|
|
try { |
|
|
|
\Illuminate\Support\Facades\DB::connection('mysql-tenant')->getPdo(); |
|
if(config('json.prefix_db') . "_ente{$tenant}" !== config("database.connections.mysql-tenant.database")) { |
|
throw new \Exception("We must change db connection"); |
|
} |
|
|
|
} catch (\Throwable $th) { |
|
|
|
get_config_variables(); |
|
|
|
config([ |
|
"database.connections.mysql-tenant.host" => config("json.db_host", null), |
|
"database.connections.mysql-tenant.database" => config('json.prefix_db') . "_ente{$tenant}", |
|
"database.connections.mysql-tenant.username" => config("json.db_root_user", null), |
|
"database.connections.mysql-tenant.password" => config("json.db_root_pass", null), |
|
]); |
|
|
|
\Illuminate\Support\Facades\DB::purge('mysql-tenant'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if(! function_exists("get_config_variables")) { |
|
|
|
function get_config_variables() |
|
{ |
|
$path = \dirname(base_path()); |
|
if (\file_exists("{$path}/config.json")) { |
|
|
|
$config = \file_get_contents("{$path}/config.json"); |
|
if(is_json($config)) { |
|
|
|
$config = json_decode($config, true); |
|
config(["json" => $config]); |
|
|
|
} |
|
|
|
} |
|
} |
|
|
|
} |
|
|
|
|
|
if(! function_exists("get_json_configuration_info")) { |
|
|
|
function get_json_configuration_info(String $filename) |
|
{ |
|
$filename = str_replace('.json', '', $filename); |
|
|
|
$path = \dirname(base_path()); |
|
$path = "{$path}/json/{$filename}.json"; |
|
if (\file_exists($path)) { |
|
|
|
$info = \file_get_contents($path); |
|
if(is_json($info)) { |
|
|
|
$info = json_decode($info, true); |
|
config(["info.{$filename}" => $info]); |
|
|
|
} |
|
|
|
} |
|
} |
|
|
|
} |
|
|
|
if(! function_exists("is_json")) { |
|
|
|
function is_json($string) |
|
{ |
|
json_decode($string); |
|
return json_last_error() === JSON_ERROR_NONE; |
|
} |
|
|
|
} |
|
|
|
if(! function_exists("restore_database_dump")) { |
|
|
|
function restore_database_dump($host, $username, $password, $database, $dump_path, $command="zcat") { |
|
exec("{$command} {$dump_path} | mysql -u {$username} -p{$password} -h {$host} {$database} 2>&1", $output); |
|
if(! empty($output) ) { |
|
foreach($output as $line) { |
|
$line = strtolower($line); |
|
if(!str_contains($line, "[warning]") && !str_contains($line, "[warn]")) { |
|
throw new \Illuminate\Database\QueryException("Error while restoring dump {$dump_path}", [], new Exception(json_encode($output), 1)); |
|
} |
|
} |
|
} |
|
} |
|
|
|
}
|
|
|