@@ -4,10 +4,10 @@ discard block  | 
                                                    ||
| 4 | 4 | |
| 5 | 5 | use const DIRECTORY_SEPARATOR;  | 
                                                        
| 6 | 6 |  use function { | 
                                                        
| 7 | - dirname, getenv  | 
                                                        |
| 7 | + dirname, getenv  | 
                                                        |
| 8 | 8 | };  | 
                                                        
| 9 | 9 |  use Dotenv\{ | 
                                                        
| 10 | - Dotenv, Validator  | 
                                                        |
| 10 | + Dotenv, Validator  | 
                                                        |
| 11 | 11 | };  | 
                                                        
| 12 | 12 | use Exception;  | 
                                                        
| 13 | 13 | use Ifsnop\Mysqldump\Mysqldump;  | 
                                                        
@@ -25,228 +25,228 @@ discard block  | 
                                                    ||
| 25 | 25 | */  | 
                                                        
| 26 | 26 | class System  | 
                                                        
| 27 | 27 |  { | 
                                                        
| 28 | - /**  | 
                                                        |
| 29 | - * @var Dotenv  | 
                                                        |
| 30 | - */  | 
                                                        |
| 31 | - protected $env;  | 
                                                        |
| 32 | - /**  | 
                                                        |
| 33 | - * @var array  | 
                                                        |
| 34 | - */  | 
                                                        |
| 35 | - protected $errors = [];  | 
                                                        |
| 36 | - /**  | 
                                                        |
| 37 | - * @var bool  | 
                                                        |
| 38 | - */  | 
                                                        |
| 39 | - protected $isCli = false;  | 
                                                        |
| 40 | - /**  | 
                                                        |
| 41 | - * @var array  | 
                                                        |
| 42 | - */  | 
                                                        |
| 43 | - protected $files = [];  | 
                                                        |
| 44 | - /**  | 
                                                        |
| 45 | - * @var Lang  | 
                                                        |
| 46 | - */  | 
                                                        |
| 47 | - protected $l10n;  | 
                                                        |
| 28 | + /**  | 
                                                        |
| 29 | + * @var Dotenv  | 
                                                        |
| 30 | + */  | 
                                                        |
| 31 | + protected $env;  | 
                                                        |
| 32 | + /**  | 
                                                        |
| 33 | + * @var array  | 
                                                        |
| 34 | + */  | 
                                                        |
| 35 | + protected $errors = [];  | 
                                                        |
| 36 | + /**  | 
                                                        |
| 37 | + * @var bool  | 
                                                        |
| 38 | + */  | 
                                                        |
| 39 | + protected $isCli = false;  | 
                                                        |
| 40 | + /**  | 
                                                        |
| 41 | + * @var array  | 
                                                        |
| 42 | + */  | 
                                                        |
| 43 | + protected $files = [];  | 
                                                        |
| 44 | + /**  | 
                                                        |
| 45 | + * @var Lang  | 
                                                        |
| 46 | + */  | 
                                                        |
| 47 | + protected $l10n;  | 
                                                        |
| 48 | 48 | |
| 49 | - /**  | 
                                                        |
| 50 | - * @var self|null  | 
                                                        |
| 51 | - */  | 
                                                        |
| 52 | - private static $_instance;  | 
                                                        |
| 49 | + /**  | 
                                                        |
| 50 | + * @var self|null  | 
                                                        |
| 51 | + */  | 
                                                        |
| 52 | + private static $_instance;  | 
                                                        |
| 53 | 53 | |
| 54 | - /**  | 
                                                        |
| 55 | - * @return self|null  | 
                                                        |
| 56 | - */  | 
                                                        |
| 57 | - public static function getInstance(): ?self  | 
                                                        |
| 58 | -    { | 
                                                        |
| 54 | + /**  | 
                                                        |
| 55 | + * @return self|null  | 
                                                        |
| 56 | + */  | 
                                                        |
| 57 | + public static function getInstance(): ?self  | 
                                                        |
| 58 | +	{ | 
                                                        |
| 59 | 59 | |
| 60 | -        if (self::$_instance === null) { | 
                                                        |
| 61 | - self::$_instance = new self;  | 
                                                        |
| 62 | - }  | 
                                                        |
| 63 | - return self::$_instance;  | 
                                                        |
| 64 | - }  | 
                                                        |
| 60 | +		if (self::$_instance === null) { | 
                                                        |
| 61 | + self::$_instance = new self;  | 
                                                        |
| 62 | + }  | 
                                                        |
| 63 | + return self::$_instance;  | 
                                                        |
| 64 | + }  | 
                                                        |
| 65 | 65 | |
| 66 | - /**  | 
                                                        |
| 67 | - * System constructor.  | 
                                                        |
| 68 | - */  | 
                                                        |
| 69 | - private function __construct()  | 
                                                        |
| 70 | -    { | 
                                                        |
| 71 | - $this->isCli = PHP_SAPI === 'cli';  | 
                                                        |
| 72 | -        try { | 
                                                        |
| 73 | - $this->loadConfigurationEnvironment();  | 
                                                        |
| 74 | -        } catch (\JBZoo\Lang\Exception $e) { | 
                                                        |
| 75 | - throw new RuntimeException($e);  | 
                                                        |
| 76 | -        } catch (\JBZoo\Path\Exception $e) { | 
                                                        |
| 77 | - throw new RuntimeException($e);  | 
                                                        |
| 78 | - }  | 
                                                        |
| 79 | - FileManager::getInstance();  | 
                                                        |
| 80 | - }  | 
                                                        |
| 66 | + /**  | 
                                                        |
| 67 | + * System constructor.  | 
                                                        |
| 68 | + */  | 
                                                        |
| 69 | + private function __construct()  | 
                                                        |
| 70 | +	{ | 
                                                        |
| 71 | + $this->isCli = PHP_SAPI === 'cli';  | 
                                                        |
| 72 | +		try { | 
                                                        |
| 73 | + $this->loadConfigurationEnvironment();  | 
                                                        |
| 74 | +		} catch (\JBZoo\Lang\Exception $e) { | 
                                                        |
| 75 | + throw new RuntimeException($e);  | 
                                                        |
| 76 | +		} catch (\JBZoo\Path\Exception $e) { | 
                                                        |
| 77 | + throw new RuntimeException($e);  | 
                                                        |
| 78 | + }  | 
                                                        |
| 79 | + FileManager::getInstance();  | 
                                                        |
| 80 | + }  | 
                                                        |
| 81 | 81 | |
| 82 | - /**  | 
                                                        |
| 83 | - * Start System initialization  | 
                                                        |
| 84 | - * @return void  | 
                                                        |
| 85 | - * @throws RuntimeException  | 
                                                        |
| 86 | - * @throws \JBZoo\Lang\Exception  | 
                                                        |
| 87 | - * @throws \JBZoo\Path\Exception  | 
                                                        |
| 88 | - */  | 
                                                        |
| 89 | - public function loadConfigurationEnvironment(): void  | 
                                                        |
| 90 | -    { | 
                                                        |
| 91 | -        if (!file_exists(dirname(__DIR__) . DIRECTORY_SEPARATOR . '.env')) { | 
                                                        |
| 92 | -            throw new RuntimeException('Please configure this script with .env file'); | 
                                                        |
| 93 | - }  | 
                                                        |
| 94 | - $this->env = Dotenv::create(dirname(__DIR__));  | 
                                                        |
| 95 | - $this->env->overload();  | 
                                                        |
| 96 | - $this->checkRequirements();  | 
                                                        |
| 97 | -        $this->l10n = new Lang(env('LANGUAGE', 'en')); | 
                                                        |
| 98 | - $this->l10n->load(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'i18n', null, 'yml');  | 
                                                        |
| 99 | -        if (!$this->isCli && !(bool)getenv('ALLOW_EXECUTE_IN_WEB_BROWSER')) { | 
                                                        |
| 100 | -            die($this->l10n->translate('unauthorized_browser')); | 
                                                        |
| 101 | - }  | 
                                                        |
| 102 | -        if ((PHP_MAJOR_VERSION . PHP_MINOR_VERSION) < 72) { | 
                                                        |
| 103 | -            die($this->l10n->translate('unsupported_php_version')); | 
                                                        |
| 104 | - }  | 
                                                        |
| 105 | - }  | 
                                                        |
| 82 | + /**  | 
                                                        |
| 83 | + * Start System initialization  | 
                                                        |
| 84 | + * @return void  | 
                                                        |
| 85 | + * @throws RuntimeException  | 
                                                        |
| 86 | + * @throws \JBZoo\Lang\Exception  | 
                                                        |
| 87 | + * @throws \JBZoo\Path\Exception  | 
                                                        |
| 88 | + */  | 
                                                        |
| 89 | + public function loadConfigurationEnvironment(): void  | 
                                                        |
| 90 | +	{ | 
                                                        |
| 91 | +		if (!file_exists(dirname(__DIR__) . DIRECTORY_SEPARATOR . '.env')) { | 
                                                        |
| 92 | +			throw new RuntimeException('Please configure this script with .env file'); | 
                                                        |
| 93 | + }  | 
                                                        |
| 94 | + $this->env = Dotenv::create(dirname(__DIR__));  | 
                                                        |
| 95 | + $this->env->overload();  | 
                                                        |
| 96 | + $this->checkRequirements();  | 
                                                        |
| 97 | +		$this->l10n = new Lang(env('LANGUAGE', 'en')); | 
                                                        |
| 98 | + $this->l10n->load(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'i18n', null, 'yml');  | 
                                                        |
| 99 | +		if (!$this->isCli && !(bool)getenv('ALLOW_EXECUTE_IN_WEB_BROWSER')) { | 
                                                        |
| 100 | +			die($this->l10n->translate('unauthorized_browser')); | 
                                                        |
| 101 | + }  | 
                                                        |
| 102 | +		if ((PHP_MAJOR_VERSION . PHP_MINOR_VERSION) < 72) { | 
                                                        |
| 103 | +			die($this->l10n->translate('unsupported_php_version')); | 
                                                        |
| 104 | + }  | 
                                                        |
| 105 | + }  | 
                                                        |
| 106 | 106 | |
| 107 | - /**  | 
                                                        |
| 108 | - * @return Validator  | 
                                                        |
| 109 | - */  | 
                                                        |
| 110 | - private function checkRequirements(): Validator  | 
                                                        |
| 111 | -    { | 
                                                        |
| 112 | - return $this->env->required([  | 
                                                        |
| 113 | - 'DB_HOST',  | 
                                                        |
| 114 | - 'DB_USER',  | 
                                                        |
| 115 | - 'DB_PASSWORD',  | 
                                                        |
| 116 | - 'MAIL_FROM',  | 
                                                        |
| 117 | - 'MAIL_FROM_NAME',  | 
                                                        |
| 118 | - 'MAIL_TO',  | 
                                                        |
| 119 | - 'MAIL_TO_NAME',  | 
                                                        |
| 120 | - 'MAIL_SEND_ON_ERROR',  | 
                                                        |
| 121 | - 'MAIL_SEND_ON_SUCCESS',  | 
                                                        |
| 122 | - 'MAIL_SMTP_HOST',  | 
                                                        |
| 123 | - 'MAIL_SMTP_PORT',  | 
                                                        |
| 124 | - 'FILES_DAYS_HISTORY',  | 
                                                        |
| 125 | - 'FILES_PATH_TO_SAVE_BACKUP',  | 
                                                        |
| 126 | - 'LANGUAGE',  | 
                                                        |
| 127 | - 'ALLOW_EXECUTE_IN_WEB_BROWSER'  | 
                                                        |
| 128 | - ])->notEmpty();  | 
                                                        |
| 129 | - }  | 
                                                        |
| 107 | + /**  | 
                                                        |
| 108 | + * @return Validator  | 
                                                        |
| 109 | + */  | 
                                                        |
| 110 | + private function checkRequirements(): Validator  | 
                                                        |
| 111 | +	{ | 
                                                        |
| 112 | + return $this->env->required([  | 
                                                        |
| 113 | + 'DB_HOST',  | 
                                                        |
| 114 | + 'DB_USER',  | 
                                                        |
| 115 | + 'DB_PASSWORD',  | 
                                                        |
| 116 | + 'MAIL_FROM',  | 
                                                        |
| 117 | + 'MAIL_FROM_NAME',  | 
                                                        |
| 118 | + 'MAIL_TO',  | 
                                                        |
| 119 | + 'MAIL_TO_NAME',  | 
                                                        |
| 120 | + 'MAIL_SEND_ON_ERROR',  | 
                                                        |
| 121 | + 'MAIL_SEND_ON_SUCCESS',  | 
                                                        |
| 122 | + 'MAIL_SMTP_HOST',  | 
                                                        |
| 123 | + 'MAIL_SMTP_PORT',  | 
                                                        |
| 124 | + 'FILES_DAYS_HISTORY',  | 
                                                        |
| 125 | + 'FILES_PATH_TO_SAVE_BACKUP',  | 
                                                        |
| 126 | + 'LANGUAGE',  | 
                                                        |
| 127 | + 'ALLOW_EXECUTE_IN_WEB_BROWSER'  | 
                                                        |
| 128 | + ])->notEmpty();  | 
                                                        |
| 129 | + }  | 
                                                        |
| 130 | 130 | |
| 131 | - /**  | 
                                                        |
| 132 | - * @return array|string  | 
                                                        |
| 133 | - */  | 
                                                        |
| 134 | - private function getExcludedDatabases()  | 
                                                        |
| 135 | -    { | 
                                                        |
| 136 | -        if (empty(trim(getenv('DB_EXCLUDE_DATABASES')))) { | 
                                                        |
| 137 | - return [];  | 
                                                        |
| 138 | - }  | 
                                                        |
| 139 | -        return $this->parseAndSanitize(getenv('DB_EXCLUDE_DATABASES')); | 
                                                        |
| 140 | - }  | 
                                                        |
| 131 | + /**  | 
                                                        |
| 132 | + * @return array|string  | 
                                                        |
| 133 | + */  | 
                                                        |
| 134 | + private function getExcludedDatabases()  | 
                                                        |
| 135 | +	{ | 
                                                        |
| 136 | +		if (empty(trim(getenv('DB_EXCLUDE_DATABASES')))) { | 
                                                        |
| 137 | + return [];  | 
                                                        |
| 138 | + }  | 
                                                        |
| 139 | +		return $this->parseAndSanitize(getenv('DB_EXCLUDE_DATABASES')); | 
                                                        |
| 140 | + }  | 
                                                        |
| 141 | 141 | |
| 142 | - /**  | 
                                                        |
| 143 | - * @return array  | 
                                                        |
| 144 | - */  | 
                                                        |
| 145 | - private function getDatabases(): array  | 
                                                        |
| 146 | -    { | 
                                                        |
| 147 | -        $pdo = new PDO('mysql:host=' . getenv('DB_HOST') . ';charset=UTF8', getenv('DB_USER'), getenv('DB_PASSWORD'), [ | 
                                                        |
| 148 | - PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,  | 
                                                        |
| 149 | - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION  | 
                                                        |
| 150 | - ]);  | 
                                                        |
| 151 | -        return $pdo->query('SHOW DATABASES')->fetchAll(); | 
                                                        |
| 152 | - }  | 
                                                        |
| 142 | + /**  | 
                                                        |
| 143 | + * @return array  | 
                                                        |
| 144 | + */  | 
                                                        |
| 145 | + private function getDatabases(): array  | 
                                                        |
| 146 | +	{ | 
                                                        |
| 147 | +		$pdo = new PDO('mysql:host=' . getenv('DB_HOST') . ';charset=UTF8', getenv('DB_USER'), getenv('DB_PASSWORD'), [ | 
                                                        |
| 148 | + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,  | 
                                                        |
| 149 | + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION  | 
                                                        |
| 150 | + ]);  | 
                                                        |
| 151 | +		return $pdo->query('SHOW DATABASES')->fetchAll(); | 
                                                        |
| 152 | + }  | 
                                                        |
| 153 | 153 | |
| 154 | - /**  | 
                                                        |
| 155 | - * Process to backup databases  | 
                                                        |
| 156 | - */  | 
                                                        |
| 157 | - public function processBackup(): void  | 
                                                        |
| 158 | -    { | 
                                                        |
| 159 | -        foreach ($this->getDatabases() as $database) { | 
                                                        |
| 160 | -            if (!\in_array($database->Database, $this->getExcludedDatabases(), true)) { | 
                                                        |
| 161 | -                $file_format = $database->Database . '-' . date($this->l10n->translate('date_format')) . '.sql'; | 
                                                        |
| 162 | -                try { | 
                                                        |
| 163 | -                    $dumper = new Mysqldump('mysql:host=' . env('DB_HOST', | 
                                                        |
| 164 | - 'localhost') . ';dbname=' . $database->Database . ';charset=UTF8',  | 
                                                        |
| 165 | -                        env('DB_USER', 'root'), env('DB_PASSWORD', '')); | 
                                                        |
| 166 | -                    $dumper->start(env('FILES_PATH_TO_SAVE_BACKUP', './Backups') . DIRECTORY_SEPARATOR . $file_format); | 
                                                        |
| 167 | -                    $this->files[] = env('FILES_PATH_TO_SAVE_BACKUP', './Backups') . DIRECTORY_SEPARATOR . $file_format; | 
                                                        |
| 168 | -                } catch (Exception $e) { | 
                                                        |
| 169 | - $this->errors[] = [  | 
                                                        |
| 170 | - 'dbname' => $database->Database,  | 
                                                        |
| 171 | - 'error_message' => $e->getMessage(),  | 
                                                        |
| 172 | - 'error_code' => $e->getCode()  | 
                                                        |
| 173 | - ];  | 
                                                        |
| 174 | - }  | 
                                                        |
| 175 | - }  | 
                                                        |
| 176 | - }  | 
                                                        |
| 177 | - $this->sendMail();  | 
                                                        |
| 178 | - }  | 
                                                        |
| 154 | + /**  | 
                                                        |
| 155 | + * Process to backup databases  | 
                                                        |
| 156 | + */  | 
                                                        |
| 157 | + public function processBackup(): void  | 
                                                        |
| 158 | +	{ | 
                                                        |
| 159 | +		foreach ($this->getDatabases() as $database) { | 
                                                        |
| 160 | +			if (!\in_array($database->Database, $this->getExcludedDatabases(), true)) { | 
                                                        |
| 161 | +				$file_format = $database->Database . '-' . date($this->l10n->translate('date_format')) . '.sql'; | 
                                                        |
| 162 | +				try { | 
                                                        |
| 163 | +					$dumper = new Mysqldump('mysql:host=' . env('DB_HOST', | 
                                                        |
| 164 | + 'localhost') . ';dbname=' . $database->Database . ';charset=UTF8',  | 
                                                        |
| 165 | +						env('DB_USER', 'root'), env('DB_PASSWORD', '')); | 
                                                        |
| 166 | +					$dumper->start(env('FILES_PATH_TO_SAVE_BACKUP', './Backups') . DIRECTORY_SEPARATOR . $file_format); | 
                                                        |
| 167 | +					$this->files[] = env('FILES_PATH_TO_SAVE_BACKUP', './Backups') . DIRECTORY_SEPARATOR . $file_format; | 
                                                        |
| 168 | +				} catch (Exception $e) { | 
                                                        |
| 169 | + $this->errors[] = [  | 
                                                        |
| 170 | + 'dbname' => $database->Database,  | 
                                                        |
| 171 | + 'error_message' => $e->getMessage(),  | 
                                                        |
| 172 | + 'error_code' => $e->getCode()  | 
                                                        |
| 173 | + ];  | 
                                                        |
| 174 | + }  | 
                                                        |
| 175 | + }  | 
                                                        |
| 176 | + }  | 
                                                        |
| 177 | + $this->sendMail();  | 
                                                        |
| 178 | + }  | 
                                                        |
| 179 | 179 | |
| 180 | - /**  | 
                                                        |
| 181 | - * @param string $data  | 
                                                        |
| 182 | - * @return array|string  | 
                                                        |
| 183 | - */  | 
                                                        |
| 184 | - private function parseAndSanitize(string $data)  | 
                                                        |
| 185 | -    { | 
                                                        |
| 186 | -        $results = explode(',', $data); | 
                                                        |
| 187 | -        if (\count($results) > 1) { | 
                                                        |
| 188 | -            foreach ($results as $k => $v) { | 
                                                        |
| 189 | - $results[$k] = trim($v);  | 
                                                        |
| 190 | -                if (empty($v)) { | 
                                                        |
| 191 | - unset($results[$k]);  | 
                                                        |
| 192 | - }  | 
                                                        |
| 193 | - }  | 
                                                        |
| 194 | - return $results;  | 
                                                        |
| 195 | - }  | 
                                                        |
| 196 | - return trim($results[0]);  | 
                                                        |
| 197 | - }  | 
                                                        |
| 180 | + /**  | 
                                                        |
| 181 | + * @param string $data  | 
                                                        |
| 182 | + * @return array|string  | 
                                                        |
| 183 | + */  | 
                                                        |
| 184 | + private function parseAndSanitize(string $data)  | 
                                                        |
| 185 | +	{ | 
                                                        |
| 186 | +		$results = explode(',', $data); | 
                                                        |
| 187 | +		if (\count($results) > 1) { | 
                                                        |
| 188 | +			foreach ($results as $k => $v) { | 
                                                        |
| 189 | + $results[$k] = trim($v);  | 
                                                        |
| 190 | +				if (empty($v)) { | 
                                                        |
| 191 | + unset($results[$k]);  | 
                                                        |
| 192 | + }  | 
                                                        |
| 193 | + }  | 
                                                        |
| 194 | + return $results;  | 
                                                        |
| 195 | + }  | 
                                                        |
| 196 | + return trim($results[0]);  | 
                                                        |
| 197 | + }  | 
                                                        |
| 198 | 198 | |
| 199 | - /**  | 
                                                        |
| 200 | - * Send a mail if error or success backup database  | 
                                                        |
| 201 | - */  | 
                                                        |
| 202 | - private function sendMail(): void  | 
                                                        |
| 203 | -    { | 
                                                        |
| 204 | -        $smtpTransport = new Swift_SmtpTransport(env('MAIL_SMTP_HOST', 'localhost'), env('MAIL_SMTP_PORT', 25)); | 
                                                        |
| 205 | -        $smtpTransport->setUsername(env('MAIL_SMTP_USER', ''))->setPassword(env('MAIL_SMTP_PASSWORD', '')); | 
                                                        |
| 206 | - $mailer = new Swift_Mailer($smtpTransport);  | 
                                                        |
| 207 | -        if (empty($this->errors)) { | 
                                                        |
| 208 | -            if ((bool)env('MAIL_SEND_ON_SUCCESS', false)) { | 
                                                        |
| 209 | -                $body = "<strong>{$this->l10n->translate('mail_db_backup_successfull')}</strong>"; | 
                                                        |
| 210 | -                if ((bool)env('MAIL_SEND_WITH_BACKUP_FILE', false)) { | 
                                                        |
| 211 | -                    $body .= "<br><br>{$this->l10n->translate('mail_db_backup_file')}"; | 
                                                        |
| 212 | - }  | 
                                                        |
| 213 | -                $message = (new Swift_Message($this->l10n->translate('mail_subject_on_success')))->setFrom(env('MAIL_FROM', | 
                                                        |
| 214 | - '[email protected]'),  | 
                                                        |
| 215 | -                    env('MAIL_FROM_NAME', 'Website Mailer for Database Backup')) | 
                                                        |
| 216 | -                    ->setTo(env('MAIL_TO'), | 
                                                        |
| 217 | -                        env('MAIL_TO_NAME', 'Webmaster of my website')) | 
                                                        |
| 218 | - ->setBody($body)  | 
                                                        |
| 219 | -                    ->setCharset('utf-8') | 
                                                        |
| 220 | -                    ->setContentType('text/html'); | 
                                                        |
| 221 | -                if ((bool)getenv('MAIL_SEND_WITH_BACKUP_FILE')) { | 
                                                        |
| 222 | -                    foreach ($this->files as $file) { | 
                                                        |
| 223 | -                        $attachment = Swift_Attachment::fromPath($file)->setContentType('application/sql'); | 
                                                        |
| 224 | - $message->attach($attachment);  | 
                                                        |
| 225 | - }  | 
                                                        |
| 226 | - }  | 
                                                        |
| 227 | - $mailer->send($message);  | 
                                                        |
| 228 | - }  | 
                                                        |
| 229 | -        } elseif ((bool)env('MAIL_SEND_ON_ERROR', false)) { | 
                                                        |
| 230 | -            $body = "<strong>{$this->l10n->translate('mail_db_backup_failed')}}:</strong><br><br><ul>"; | 
                                                        |
| 231 | -            foreach ($this->errors as $error) { | 
                                                        |
| 232 | - $body .= "<li>  | 
                                                        |
| 199 | + /**  | 
                                                        |
| 200 | + * Send a mail if error or success backup database  | 
                                                        |
| 201 | + */  | 
                                                        |
| 202 | + private function sendMail(): void  | 
                                                        |
| 203 | +	{ | 
                                                        |
| 204 | +		$smtpTransport = new Swift_SmtpTransport(env('MAIL_SMTP_HOST', 'localhost'), env('MAIL_SMTP_PORT', 25)); | 
                                                        |
| 205 | +		$smtpTransport->setUsername(env('MAIL_SMTP_USER', ''))->setPassword(env('MAIL_SMTP_PASSWORD', '')); | 
                                                        |
| 206 | + $mailer = new Swift_Mailer($smtpTransport);  | 
                                                        |
| 207 | +		if (empty($this->errors)) { | 
                                                        |
| 208 | +			if ((bool)env('MAIL_SEND_ON_SUCCESS', false)) { | 
                                                        |
| 209 | +				$body = "<strong>{$this->l10n->translate('mail_db_backup_successfull')}</strong>"; | 
                                                        |
| 210 | +				if ((bool)env('MAIL_SEND_WITH_BACKUP_FILE', false)) { | 
                                                        |
| 211 | +					$body .= "<br><br>{$this->l10n->translate('mail_db_backup_file')}"; | 
                                                        |
| 212 | + }  | 
                                                        |
| 213 | +				$message = (new Swift_Message($this->l10n->translate('mail_subject_on_success')))->setFrom(env('MAIL_FROM', | 
                                                        |
| 214 | + '[email protected]'),  | 
                                                        |
| 215 | +					env('MAIL_FROM_NAME', 'Website Mailer for Database Backup')) | 
                                                        |
| 216 | +					->setTo(env('MAIL_TO'), | 
                                                        |
| 217 | +						env('MAIL_TO_NAME', 'Webmaster of my website')) | 
                                                        |
| 218 | + ->setBody($body)  | 
                                                        |
| 219 | +					->setCharset('utf-8') | 
                                                        |
| 220 | +					->setContentType('text/html'); | 
                                                        |
| 221 | +				if ((bool)getenv('MAIL_SEND_WITH_BACKUP_FILE')) { | 
                                                        |
| 222 | +					foreach ($this->files as $file) { | 
                                                        |
| 223 | +						$attachment = Swift_Attachment::fromPath($file)->setContentType('application/sql'); | 
                                                        |
| 224 | + $message->attach($attachment);  | 
                                                        |
| 225 | + }  | 
                                                        |
| 226 | + }  | 
                                                        |
| 227 | + $mailer->send($message);  | 
                                                        |
| 228 | + }  | 
                                                        |
| 229 | +		} elseif ((bool)env('MAIL_SEND_ON_ERROR', false)) { | 
                                                        |
| 230 | +			$body = "<strong>{$this->l10n->translate('mail_db_backup_failed')}}:</strong><br><br><ul>"; | 
                                                        |
| 231 | +			foreach ($this->errors as $error) { | 
                                                        |
| 232 | + $body .= "<li>  | 
                                                        |
| 233 | 233 | <ul>  | 
                                                        
| 234 | 234 |                              <li>{$this->l10n->translate('database')}: {$error['dbname']}</li> | 
                                                        
| 235 | 235 |                              <li>{$this->l10n->translate('error_code')}: {$error['error_code']}</li> | 
                                                        
| 236 | 236 |                              <li>{$this->l10n->translate('error_message')}: {$error['error_message']}</li> | 
                                                        
| 237 | 237 | </ul>  | 
                                                        
| 238 | 238 | </li>";  | 
                                                        
| 239 | - }  | 
                                                        |
| 240 | - $body .= '</ul>';  | 
                                                        |
| 241 | -            $message = (new Swift_Message($this->l10n->translate('mail_subject_on_error')))->setFrom(env('MAIL_FROM', | 
                                                        |
| 242 | - '[email protected]'),  | 
                                                        |
| 243 | -                env('MAIL_FROM_NAME', 'Website Mailer for Database Backup')) | 
                                                        |
| 244 | -                ->setTo(env('MAIL_TO'), | 
                                                        |
| 245 | -                    env('MAIL_TO_NAME', 'Webmaster of my website')) | 
                                                        |
| 246 | - ->setBody($body)  | 
                                                        |
| 247 | -                ->setCharset('utf-8') | 
                                                        |
| 248 | -                ->setContentType('text/html'); | 
                                                        |
| 249 | - $mailer->send($message);  | 
                                                        |
| 250 | - }  | 
                                                        |
| 251 | - }  | 
                                                        |
| 239 | + }  | 
                                                        |
| 240 | + $body .= '</ul>';  | 
                                                        |
| 241 | +			$message = (new Swift_Message($this->l10n->translate('mail_subject_on_error')))->setFrom(env('MAIL_FROM', | 
                                                        |
| 242 | + '[email protected]'),  | 
                                                        |
| 243 | +				env('MAIL_FROM_NAME', 'Website Mailer for Database Backup')) | 
                                                        |
| 244 | +				->setTo(env('MAIL_TO'), | 
                                                        |
| 245 | +					env('MAIL_TO_NAME', 'Webmaster of my website')) | 
                                                        |
| 246 | + ->setBody($body)  | 
                                                        |
| 247 | +				->setCharset('utf-8') | 
                                                        |
| 248 | +				->setContentType('text/html'); | 
                                                        |
| 249 | + $mailer->send($message);  | 
                                                        |
| 250 | + }  | 
                                                        |
| 251 | + }  | 
                                                        |
| 252 | 252 | }  | 
                                                        
@@ -8,72 +8,72 @@  | 
                                                    ||
| 8 | 8 | */  | 
                                                        
| 9 | 9 | class FileManager  | 
                                                        
| 10 | 10 |  { | 
                                                        
| 11 | - /**  | 
                                                        |
| 12 | - * @var array  | 
                                                        |
| 13 | - */  | 
                                                        |
| 14 | - private $params;  | 
                                                        |
| 15 | - /**  | 
                                                        |
| 16 | - * @var array  | 
                                                        |
| 17 | - */  | 
                                                        |
| 18 | - private $files = [];  | 
                                                        |
| 19 | - /**  | 
                                                        |
| 20 | - * @var self  | 
                                                        |
| 21 | - */  | 
                                                        |
| 22 | - private static $_instance;  | 
                                                        |
| 11 | + /**  | 
                                                        |
| 12 | + * @var array  | 
                                                        |
| 13 | + */  | 
                                                        |
| 14 | + private $params;  | 
                                                        |
| 15 | + /**  | 
                                                        |
| 16 | + * @var array  | 
                                                        |
| 17 | + */  | 
                                                        |
| 18 | + private $files = [];  | 
                                                        |
| 19 | + /**  | 
                                                        |
| 20 | + * @var self  | 
                                                        |
| 21 | + */  | 
                                                        |
| 22 | + private static $_instance;  | 
                                                        |
| 23 | 23 | |
| 24 | - /**  | 
                                                        |
| 25 | - * @return FileManager|null  | 
                                                        |
| 26 | - */  | 
                                                        |
| 27 | - public static function getInstance(): ?self  | 
                                                        |
| 28 | -    { | 
                                                        |
| 24 | + /**  | 
                                                        |
| 25 | + * @return FileManager|null  | 
                                                        |
| 26 | + */  | 
                                                        |
| 27 | + public static function getInstance(): ?self  | 
                                                        |
| 28 | +	{ | 
                                                        |
| 29 | 29 | |
| 30 | -        if (self::$_instance === null) { | 
                                                        |
| 31 | - self::$_instance = new self;  | 
                                                        |
| 32 | - }  | 
                                                        |
| 33 | - return self::$_instance;  | 
                                                        |
| 34 | - }  | 
                                                        |
| 30 | +		if (self::$_instance === null) { | 
                                                        |
| 31 | + self::$_instance = new self;  | 
                                                        |
| 32 | + }  | 
                                                        |
| 33 | + return self::$_instance;  | 
                                                        |
| 34 | + }  | 
                                                        |
| 35 | 35 | |
| 36 | - /**  | 
                                                        |
| 37 | - * FileManager constructor.  | 
                                                        |
| 38 | - */  | 
                                                        |
| 39 | - public function __construct()  | 
                                                        |
| 40 | -    { | 
                                                        |
| 41 | - System::getInstance();  | 
                                                        |
| 42 | - $this->params = [  | 
                                                        |
| 43 | -            'files_path' => env('FILES_PATH_TO_SAVE_BACKUP', './Backups'), | 
                                                        |
| 44 | -            'days_interval' => env('FILES_DAYS_HISTORY', 3) | 
                                                        |
| 45 | - ];  | 
                                                        |
| 46 | - $this->_getFiles();  | 
                                                        |
| 47 | -        env('FILES_DAYS_HISTORY', 3) > 0 ?: $this->removeOldFilesByIntervalDays(); | 
                                                        |
| 48 | - }  | 
                                                        |
| 36 | + /**  | 
                                                        |
| 37 | + * FileManager constructor.  | 
                                                        |
| 38 | + */  | 
                                                        |
| 39 | + public function __construct()  | 
                                                        |
| 40 | +	{ | 
                                                        |
| 41 | + System::getInstance();  | 
                                                        |
| 42 | + $this->params = [  | 
                                                        |
| 43 | +			'files_path' => env('FILES_PATH_TO_SAVE_BACKUP', './Backups'), | 
                                                        |
| 44 | +			'days_interval' => env('FILES_DAYS_HISTORY', 3) | 
                                                        |
| 45 | + ];  | 
                                                        |
| 46 | + $this->_getFiles();  | 
                                                        |
| 47 | +		env('FILES_DAYS_HISTORY', 3) > 0 ?: $this->removeOldFilesByIntervalDays(); | 
                                                        |
| 48 | + }  | 
                                                        |
| 49 | 49 | |
| 50 | - /**  | 
                                                        |
| 51 | - * @return void  | 
                                                        |
| 52 | - */  | 
                                                        |
| 53 | - private function _getFiles(): void  | 
                                                        |
| 54 | -    { | 
                                                        |
| 55 | - $files = scandir($this->params['files_path'], SCANDIR_SORT_ASCENDING);  | 
                                                        |
| 56 | -        $this->files = array_filter($files, function ($k) { | 
                                                        |
| 57 | - $return = [];  | 
                                                        |
| 58 | -            if ($k !== '..' && $k !== '.') { | 
                                                        |
| 59 | - $return[] = $k;  | 
                                                        |
| 60 | - }  | 
                                                        |
| 61 | - return $return;  | 
                                                        |
| 62 | - });  | 
                                                        |
| 63 | - }  | 
                                                        |
| 50 | + /**  | 
                                                        |
| 51 | + * @return void  | 
                                                        |
| 52 | + */  | 
                                                        |
| 53 | + private function _getFiles(): void  | 
                                                        |
| 54 | +	{ | 
                                                        |
| 55 | + $files = scandir($this->params['files_path'], SCANDIR_SORT_ASCENDING);  | 
                                                        |
| 56 | +		$this->files = array_filter($files, function ($k) { | 
                                                        |
| 57 | + $return = [];  | 
                                                        |
| 58 | +			if ($k !== '..' && $k !== '.') { | 
                                                        |
| 59 | + $return[] = $k;  | 
                                                        |
| 60 | + }  | 
                                                        |
| 61 | + return $return;  | 
                                                        |
| 62 | + });  | 
                                                        |
| 63 | + }  | 
                                                        |
| 64 | 64 | |
| 65 | - /**  | 
                                                        |
| 66 | - * @return void  | 
                                                        |
| 67 | - */  | 
                                                        |
| 68 | - private function removeOldFilesByIntervalDays(): void  | 
                                                        |
| 69 | -    { | 
                                                        |
| 70 | - $time_before = time() - $this->params['days_interval'] * 86400;  | 
                                                        |
| 71 | -        foreach ($this->files as $file) { | 
                                                        |
| 72 | -            $f = explode('-', $file); | 
                                                        |
| 73 | -            $f = str_replace('.sql', '', $f); | 
                                                        |
| 74 | -            if ($f[1] < $time_before) { | 
                                                        |
| 75 | - @unlink($this->params['files_path'] . '/' . $file);  | 
                                                        |
| 76 | - }  | 
                                                        |
| 77 | - }  | 
                                                        |
| 78 | - }  | 
                                                        |
| 65 | + /**  | 
                                                        |
| 66 | + * @return void  | 
                                                        |
| 67 | + */  | 
                                                        |
| 68 | + private function removeOldFilesByIntervalDays(): void  | 
                                                        |
| 69 | +	{ | 
                                                        |
| 70 | + $time_before = time() - $this->params['days_interval'] * 86400;  | 
                                                        |
| 71 | +		foreach ($this->files as $file) { | 
                                                        |
| 72 | +			$f = explode('-', $file); | 
                                                        |
| 73 | +			$f = str_replace('.sql', '', $f); | 
                                                        |
| 74 | +			if ($f[1] < $time_before) { | 
                                                        |
| 75 | + @unlink($this->params['files_path'] . '/' . $file);  | 
                                                        |
| 76 | + }  | 
                                                        |
| 77 | + }  | 
                                                        |
| 78 | + }  | 
                                                        |
| 79 | 79 | }  | 
                                                        
@@ -53,7 +53,7 @@  | 
                                                    ||
| 53 | 53 | private function _getFiles(): void  | 
                                                        
| 54 | 54 |      { | 
                                                        
| 55 | 55 | $files = scandir($this->params['files_path'], SCANDIR_SORT_ASCENDING);  | 
                                                        
| 56 | -        $this->files = array_filter($files, function ($k) { | 
                                                        |
| 56 | +        $this->files = array_filter($files, function($k) { | 
                                                        |
| 57 | 57 | $return = [];  | 
                                                        
| 58 | 58 |              if ($k !== '..' && $k !== '.') { | 
                                                        
| 59 | 59 | $return[] = $k;  | 
                                                        
@@ -1,48 +1,48 @@  | 
                                                    ||
| 1 | 1 | <?php  | 
                                                        
| 2 | 2 |  if (!function_exists('env')) { | 
                                                        
| 3 | - /**  | 
                                                        |
| 4 | - * Gets the value of an environment variable.  | 
                                                        |
| 5 | - *  | 
                                                        |
| 6 | - * @param string $key  | 
                                                        |
| 7 | - * @param mixed $default  | 
                                                        |
| 8 | - * @return mixed  | 
                                                        |
| 9 | - */  | 
                                                        |
| 10 | - function env(string $key, $default = null)  | 
                                                        |
| 11 | -    { | 
                                                        |
| 12 | - $value = getenv($key);  | 
                                                        |
| 13 | -        if ($value === false) { | 
                                                        |
| 14 | - return value($default);  | 
                                                        |
| 15 | - }  | 
                                                        |
| 16 | -        switch (strtolower($value)) { | 
                                                        |
| 17 | - case 'true':  | 
                                                        |
| 18 | - case '(true)':  | 
                                                        |
| 19 | - return true;  | 
                                                        |
| 20 | - case 'false':  | 
                                                        |
| 21 | - case '(false)':  | 
                                                        |
| 22 | - return false;  | 
                                                        |
| 23 | - case 'empty':  | 
                                                        |
| 24 | - case '(empty)':  | 
                                                        |
| 25 | - return '';  | 
                                                        |
| 26 | - case 'null':  | 
                                                        |
| 27 | - case '(null)':  | 
                                                        |
| 28 | - return null;  | 
                                                        |
| 29 | - }  | 
                                                        |
| 30 | -        if (($valueLength = strlen($value)) > 1 && strpos($value, '"') === 0 && $value[$valueLength - 1] === '"') { | 
                                                        |
| 31 | - return substr($value, 1, -1);  | 
                                                        |
| 32 | - }  | 
                                                        |
| 33 | - return $value;  | 
                                                        |
| 34 | - }  | 
                                                        |
| 3 | + /**  | 
                                                        |
| 4 | + * Gets the value of an environment variable.  | 
                                                        |
| 5 | + *  | 
                                                        |
| 6 | + * @param string $key  | 
                                                        |
| 7 | + * @param mixed $default  | 
                                                        |
| 8 | + * @return mixed  | 
                                                        |
| 9 | + */  | 
                                                        |
| 10 | + function env(string $key, $default = null)  | 
                                                        |
| 11 | +	{ | 
                                                        |
| 12 | + $value = getenv($key);  | 
                                                        |
| 13 | +		if ($value === false) { | 
                                                        |
| 14 | + return value($default);  | 
                                                        |
| 15 | + }  | 
                                                        |
| 16 | +		switch (strtolower($value)) { | 
                                                        |
| 17 | + case 'true':  | 
                                                        |
| 18 | + case '(true)':  | 
                                                        |
| 19 | + return true;  | 
                                                        |
| 20 | + case 'false':  | 
                                                        |
| 21 | + case '(false)':  | 
                                                        |
| 22 | + return false;  | 
                                                        |
| 23 | + case 'empty':  | 
                                                        |
| 24 | + case '(empty)':  | 
                                                        |
| 25 | + return '';  | 
                                                        |
| 26 | + case 'null':  | 
                                                        |
| 27 | + case '(null)':  | 
                                                        |
| 28 | + return null;  | 
                                                        |
| 29 | + }  | 
                                                        |
| 30 | +		if (($valueLength = strlen($value)) > 1 && strpos($value, '"') === 0 && $value[$valueLength - 1] === '"') { | 
                                                        |
| 31 | + return substr($value, 1, -1);  | 
                                                        |
| 32 | + }  | 
                                                        |
| 33 | + return $value;  | 
                                                        |
| 34 | + }  | 
                                                        |
| 35 | 35 | |
| 36 | -    if (!function_exists('value')) { | 
                                                        |
| 37 | - /**  | 
                                                        |
| 38 | - * Return the default value of the given value.  | 
                                                        |
| 39 | - *  | 
                                                        |
| 40 | - * @param mixed $value  | 
                                                        |
| 41 | - * @return mixed  | 
                                                        |
| 42 | - */  | 
                                                        |
| 43 | - function value($value)  | 
                                                        |
| 44 | -        { | 
                                                        |
| 45 | - return $value instanceof Closure ? $value() : $value;  | 
                                                        |
| 46 | - }  | 
                                                        |
| 47 | - }  | 
                                                        |
| 36 | +	if (!function_exists('value')) { | 
                                                        |
| 37 | + /**  | 
                                                        |
| 38 | + * Return the default value of the given value.  | 
                                                        |
| 39 | + *  | 
                                                        |
| 40 | + * @param mixed $value  | 
                                                        |
| 41 | + * @return mixed  | 
                                                        |
| 42 | + */  | 
                                                        |
| 43 | + function value($value)  | 
                                                        |
| 44 | +		{ | 
                                                        |
| 45 | + return $value instanceof Closure ? $value() : $value;  | 
                                                        |
| 46 | + }  | 
                                                        |
| 47 | + }  | 
                                                        |
| 48 | 48 | }  | 
                                                        
| 49 | 49 | \ No newline at end of file  |