| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Description of Logs |
| 5 |
* |
| 6 |
* @author Ali2Woo Team |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace AliNext_Lite;; |
| 10 |
|
| 11 |
use Throwable; |
| 12 |
|
| 13 |
class Logs{ |
| 14 |
|
| 15 |
private static ?Logs $_instance = null; |
| 16 |
|
| 17 |
private string $a2wl_logs_file = '/ali2woo/a2wl_debug.txt'; |
| 18 |
|
| 19 |
//old version log |
| 20 |
private string $a2wl_old_logs_file = '/ali2woo/a2wl_debug.log'; |
| 21 |
|
| 22 |
protected function __construct() { |
| 23 |
if (get_setting('write_info_log')) { |
| 24 |
$upload_dir = wp_upload_dir(); |
| 25 |
$a2wl_logs_dir = $upload_dir['basedir'] . '/ali2woo'; |
| 26 |
|
| 27 |
$old_file = $upload_dir['basedir'] . $this->a2wl_old_logs_file; |
| 28 |
$new_file = $upload_dir['basedir'] . $this->a2wl_logs_file; |
| 29 |
|
| 30 |
if (!file_exists($a2wl_logs_dir)) { |
| 31 |
mkdir($a2wl_logs_dir, 0755, true); |
| 32 |
} |
| 33 |
|
| 34 |
if (file_exists($old_file) && !file_exists($new_file)) { |
| 35 |
if (!rename($old_file, $new_file)) { |
| 36 |
error_log("Failed to rename old log file"); |
| 37 |
} |
| 38 |
} elseif (!file_exists($new_file)) { |
| 39 |
$fp = fopen($new_file, 'w'); |
| 40 |
fclose($fp); |
| 41 |
chmod($new_file, 0644); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
protected function __clone() {} |
| 47 |
|
| 48 |
static public function getInstance(): ?Logs |
| 49 |
{ |
| 50 |
if (is_null(self::$_instance)) { |
| 51 |
self::$_instance = new self(); |
| 52 |
} |
| 53 |
return self::$_instance; |
| 54 |
} |
| 55 |
|
| 56 |
public function write($message): void |
| 57 |
{ |
| 58 |
if (get_setting('write_info_log')) { |
| 59 |
try { |
| 60 |
$fp = fopen($this->log_path(), 'a'); |
| 61 |
fwrite($fp, $message . "\r\n"); |
| 62 |
} catch (Throwable $e) { |
| 63 |
error_log($e->getTraceAsString()); |
| 64 |
} finally { |
| 65 |
if (!empty($fp)) { |
| 66 |
fclose($fp); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
public function delete(): void |
| 73 |
{ |
| 74 |
unlink($this->log_path()); |
| 75 |
} |
| 76 |
|
| 77 |
public function log_path(): string |
| 78 |
{ |
| 79 |
$upload_dir = wp_upload_dir(); |
| 80 |
return $upload_dir['basedir'] . $this->a2wl_logs_file; |
| 81 |
} |
| 82 |
|
| 83 |
public function log_url(): string |
| 84 |
{ |
| 85 |
$upload_dir = wp_upload_dir(); |
| 86 |
return $upload_dir['baseurl'] . $this->a2wl_logs_file; |
| 87 |
} |
| 88 |
} |
| 89 |
|