.htaccess
1 year ago
CPanel.php
1 year ago
DirectAdmin.php
1 year ago
Vendor.php
1 year ago
index.html
1 year ago
web.config
1 year ago
Vendor.php
168 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JetBackup\Export\Vendor; |
| 4 | |
| 5 | use JetBackup\Cron\Task\Task; |
| 6 | use JetBackup\Data\ArrayData; |
| 7 | use JetBackup\JetBackup; |
| 8 | use JetBackup\Log\LogController; |
| 9 | |
| 10 | if (!defined( '__JETBACKUP__')) die('Direct access is not allowed'); |
| 11 | |
| 12 | abstract class Vendor extends ArrayData { |
| 13 | |
| 14 | const TYPE_CPANEL = 1; |
| 15 | const TYPE_DIRECT_ADMIN = 2; |
| 16 | |
| 17 | const ALL_VENDORS = [ |
| 18 | self::TYPE_CPANEL, |
| 19 | self::TYPE_DIRECT_ADMIN |
| 20 | ]; |
| 21 | //const TYPE_PLESK = 3; |
| 22 | |
| 23 | const USERNAME = 'username'; |
| 24 | const PASSWORD = 'password'; |
| 25 | const DOMAIN = 'domain'; |
| 26 | const DATABASE = 'database'; |
| 27 | const DATABASE_USER = 'database_user'; |
| 28 | const DATABASE_TABLES = 'database_tables'; |
| 29 | const HOMEDIR = 'homedir'; |
| 30 | const EMAIL = 'email'; |
| 31 | const DESTINATION = 'destination'; |
| 32 | public const PANEL_TYPE = 'panel_type'; |
| 33 | |
| 34 | const DATABASE_NAME = '%s_wpdb'; |
| 35 | const DATABASE_USER_NAME = '%s_wpuser'; |
| 36 | const DB_HOST = 'localhost'; |
| 37 | |
| 38 | private Task $_task; |
| 39 | |
| 40 | public function __construct(Task $task) { |
| 41 | $this->_task = $task; |
| 42 | } |
| 43 | |
| 44 | public function getUsername():string { return $this->get(self::USERNAME); } |
| 45 | private function _setUsername(string $username):void { $this->set(self::USERNAME, $username); } |
| 46 | public function getHashedPassword():string { return strtoupper(bin2hex(sha1(sha1($this->getPassword(), true), true))); } |
| 47 | public function getPassword():string { return $this->get(self::PASSWORD); } |
| 48 | public function setPassword(string $password):void { $this->set(self::PASSWORD, $password); } |
| 49 | |
| 50 | public function getDomain():string { return $this->get(self::DOMAIN); } |
| 51 | |
| 52 | /** |
| 53 | * @param string $domain |
| 54 | * @return void |
| 55 | * |
| 56 | * Sets the full domain retrieved from "Wordpress::getSiteURL()" |
| 57 | * prefix (http) is removed during set or not set at all |
| 58 | */ |
| 59 | public function setDomain(string $domain):void { $this->set(self::DOMAIN, $domain); } |
| 60 | |
| 61 | /** |
| 62 | * Extract clean domain, even if WordPress is nested inside subfolder. |
| 63 | * Example: http://mydomain.com/subfolder1/subfolder2 -> mydomain.com |
| 64 | * We have to add 'http' and ending '/' to trigger parse_url |
| 65 | */ |
| 66 | public function getDomainOnly() : string { |
| 67 | $domain = 'http://' . trim($this->getDomain(), '/') . '/'; // mydomain.com -> http://mydomain.com/ |
| 68 | return parse_url($domain, PHP_URL_HOST); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * If WordPress is nested inside a subfolder - extract it |
| 73 | * Using php core 'parse_url' for this, added'/' suffix just to trigger parse_url (if not ending with "/" will return null) |
| 74 | * Example: http://mydomain.com/subfolder1 -> subfolder1 |
| 75 | */ |
| 76 | public function getDomainSubFolder() : ?string { |
| 77 | $domain = 'http://' . trim($this->getDomain(), '/') . '/'; // mydomain.com -> http://mydomain.com/ |
| 78 | return trim(parse_url($domain, PHP_URL_PATH), '/'); |
| 79 | } |
| 80 | |
| 81 | public function getDatabase():string { return $this->get(self::DATABASE); } |
| 82 | private function _setDatabase(string $database):void { $this->set(self::DATABASE, $database); } |
| 83 | public function getDatabaseUser():string { return $this->get(self::DATABASE_USER); } |
| 84 | private function _setDatabaseUser(string $user):void { $this->set(self::DATABASE_USER, $user); } |
| 85 | public function getDatabaseTables():array { return $this->get(self::DATABASE_TABLES, []); } |
| 86 | public function setDatabaseTables(array $tables):void { $this->set(self::DATABASE_TABLES, $tables); } |
| 87 | public function getHomedir():string { return $this->get(self::HOMEDIR); } |
| 88 | public function setHomedir(string $homedir):void { $this->set(self::HOMEDIR, $homedir); } |
| 89 | public function getEmailAddress():string { return $this->get(self::EMAIL); } |
| 90 | public function setEmailAddress(string $email):void { $this->set(self::EMAIL, $email); } |
| 91 | public function getDestination():string { return $this->get(self::DESTINATION); } |
| 92 | public function setDestination(string $destination):void { $this->set(self::DESTINATION, $destination); } |
| 93 | public function getTask():Task { return $this->_task; } |
| 94 | public function getLogController():LogController { return $this->getTask()->getLogController(); } |
| 95 | |
| 96 | abstract public function _build():string; |
| 97 | |
| 98 | public function build():string { |
| 99 | |
| 100 | // If domain is IP address, Change it to temporary domain |
| 101 | if(!$this->getDomain() || filter_var($this->getDomain(), FILTER_VALIDATE_IP)) { |
| 102 | $this->getLogController()->logMessage("Invalid domain (domain: {$this->getDomain()}), Setting temporary domain 'tempdomain.loc'"); |
| 103 | $this->setDomain('tempdomain.loc'); |
| 104 | } |
| 105 | |
| 106 | // Generate username |
| 107 | $this->_setUsername($this->getTask()->func(function() { |
| 108 | |
| 109 | $this->getLogController()->logMessage("Generating username"); |
| 110 | $username = preg_replace('/[^a-z0-9]/', '', strtolower($this->getDomain())); |
| 111 | if(substr($username, 0, 4) == 'test') $username = 'user' . $username; |
| 112 | $username = substr($username, 0, 8); |
| 113 | |
| 114 | $this->getLogController()->logMessage("Username: $username"); |
| 115 | |
| 116 | return $username; |
| 117 | }, [], 'generate_username')); |
| 118 | |
| 119 | // Set the new database name |
| 120 | $this->_setDatabase(sprintf(self::DATABASE_NAME, $this->getUsername())); |
| 121 | // Set the new database user name |
| 122 | $this->_setDatabaseUser(sprintf(self::DATABASE_USER_NAME, $this->getUsername())); |
| 123 | |
| 124 | // If Email address not exists or invalid, set default Email address |
| 125 | if(!$this->getEmailAddress() || !filter_var($this->getEmailAddress(), FILTER_VALIDATE_EMAIL)) { |
| 126 | $email = $this->getUsername() . '@' . $this->getDomainOnly(); |
| 127 | $this->getLogController()->logMessage("Invalid Email address (Email: {$this->getEmailAddress()}), Setting temporary email address '$email'"); |
| 128 | $this->setEmailAddress($email); |
| 129 | } |
| 130 | |
| 131 | // Change database and database user names |
| 132 | $this->getTask()->func(function() { |
| 133 | |
| 134 | $this->getLogController()->logMessage("Changing database and database user names in wp-config.php file"); |
| 135 | |
| 136 | $config = $this->getHomedir() . JetBackup::SEP . 'wp-config.php'; |
| 137 | $config_new = $config . '.new'; |
| 138 | |
| 139 | $fd = fopen($config_new, 'w'); |
| 140 | |
| 141 | $this->getTask()->fileRead($config, function($line) use ($fd) { |
| 142 | switch(true) { |
| 143 | case preg_match("/^define\(\s*'DB_NAME'\s*,\s*'.*?'\s*\);/", $line): |
| 144 | $line = "define('DB_NAME', '" . $this->getDatabase() . "');" . PHP_EOL; |
| 145 | break; |
| 146 | case preg_match("/^define\(\s*'DB_USER'\s*,\s*'.*?'\s*\);/", $line): |
| 147 | $line = "define('DB_USER', '" . $this->getDatabaseUser() . "');" . PHP_EOL; |
| 148 | break; |
| 149 | case preg_match("/^define\(\s*'DB_PASSWORD'\s*,\s*'.*?'\s*\);/", $line); |
| 150 | $line = "define('DB_PASSWORD', '" . $this->getPassword() . "');" . PHP_EOL; |
| 151 | break; |
| 152 | case preg_match("/^define\(\s*'WP_CONTENT_DIR'\s*,\s*'.*?'\s*\);/", $line): return; |
| 153 | case preg_match("/^define\(\s*'DB_HOST'\s*,\s*'.*?'\s*\);/", $line): |
| 154 | $line = "define('DB_HOST', '" . self::DB_HOST . "');" . PHP_EOL; |
| 155 | break; |
| 156 | } |
| 157 | fwrite($fd, $line); |
| 158 | }); |
| 159 | |
| 160 | fclose($fd); |
| 161 | unlink($config); |
| 162 | rename($config_new, $config); |
| 163 | |
| 164 | }, [], 'change_db_name'); |
| 165 | |
| 166 | return $this->_build(); |
| 167 | } |
| 168 | } |