diff --git a/README.md b/README.md index 993cb42..7347ee1 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,11 @@ Generate api key ```bash bash /usr/local/vesta/bin/v-generate-api-key ``` +Or you can view existing keys +```sh +ls -l /usr/local/vesta/data/keys/ +``` ## Usage @@ -47,5 +51,5 @@ Simple usage ```php use VestaAPI\Facades\Vesta; -$backups = Vesta::server('testVDS')->user('MyUserName')->listUserBackups(); +$backups = Vesta::server('testVDS')->setUserName($userThatUWantToView)->listUserBackups(); diff --git a/composer.json b/composer.json index 3376583..c309069 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,6 @@ ], "type": "library", "require": { - "guzzlehttp/guzzle": "^6.2", "laravel/framework": "^5.0" }, "license": "GPL-3.0", diff --git a/config/vesta.php b/config/vesta.php index 1039789..ddbc7de 100644 --- a/config/vesta.php +++ b/config/vesta.php @@ -19,6 +19,7 @@ 'servers' => [ 'testVDS' => [ 'host' => '0.0.0.0', + 'admin' => 'admin', 'key' => 'secretString', ], ], diff --git a/src/Services/Cron.php b/src/Services/Cron.php index e977ddd..5dfb0fd 100644 --- a/src/Services/Cron.php +++ b/src/Services/Cron.php @@ -11,75 +11,65 @@ trait Cron */ public function listCron() { - $this->returnCode = 'no'; - $listDns = $this->send('v-list-cron-jobs', $this->userName, 'json'); - $data = json_decode($listDns, true); - - return $data; + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-cron-jobs', $this->getUserName(), 'json')); } /** * Add Cron. * - * @param $min - * @param $hour - * @param $day - * @param $month - * @param $wday - * @param $cmd + * @param $min + * @param $hour + * @param $day + * @param $month + * @param $wday + * @param $cmd * * @return mixed */ public function addCron($min, $hour, $day, $month, $wday, $cmd) { - return $this->send('v-add-cron-job', $this->userName, $min, $hour, $day, $month, $wday, - $cmd); + return $this->toString($this->send('v-add-cron-job', $this->getUserName(), $min, $hour, $day, $month, $wday, $cmd)); } /** * Show Cron. * - * @param $job + * @param $job * * @return mixed */ public function showCron($job) { - $this->returnCode = 'no'; - $request = $this->send('v-list-cron-job', $this->userName, $job, 'json'); - $data = json_decode($request, true); - - return $data; + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-cron-job', $this->getUserName(), $job, 'json')); } /** * Delete cron. * - * @param $job + * @param $job * * @return mixed */ public function deleteCron($job) { - return $this->send('v-delete-cron-job', $this->userName, $job); + return $this->toString($this->send('v-delete-cron-job', $this->getUserName(), $job)); } /** * Edit cron. * - * @param $job - * @param $min - * @param $hour - * @param $day - * @param $month - * @param $wday - * @param $cmd + * @param $job + * @param $min + * @param $hour + * @param $day + * @param $month + * @param $wday + * @param $cmd * * @return mixed */ public function editCron($job, $min, $hour, $day, $month, $wday, $cmd) { - return $this->send('v-change-cron-job', $this->userName, $job, $min, $hour, $day, $month, - $wday, $cmd); + return $this->toString($this->send('v-change-cron-job', $this->getUserName(), $job, $min, $hour, $day, $month, $wday, $cmd)); } } diff --git a/src/Services/DNS.php b/src/Services/DNS.php deleted file mode 100644 index 164516c..0000000 --- a/src/Services/DNS.php +++ /dev/null @@ -1,162 +0,0 @@ -returnCode = 'no'; - $listDns = $this->send('v-list-dns-domains', $this->userName, 'json'); - $data = json_decode($listDns, true); - - return $data; - } - - /** - * List Only DNS. - * - * @param $dns - * - * @return mixed - */ - public function listOnlyDNS($dns) - { - $this->returnCode = 'no'; - $listDNS = $this->send('v-list-dns-domain', $this->userName, $dns, 'json'); - $data = json_decode($listDNS, true); - - return $data; - } - - /** - * Delete DNS record domain. - * - * @param $domain - * - * @return mixed - */ - public function deleteDNDDomain($domain) - { - return $this->send('v-delete-dns-domain', $this->userName, $domain); - } - - /** - * Delete DNS record. - * - * @param $domain - * @param $recordId - * - * @return mixed - */ - public function deleteDNSRecord($domain, $recordId) - { - return $this->send('v-delete-dns-record', $this->userName, $domain, $recordId); - } - - /** - * Add DNS domain. - * - * @param $domain - * @param $ip - * @param $ns1 - * @param $ns2 - * @param null $ns3 - * @param null $ns4 - * - * @return mixed - */ - public function addDNSDomain($domain, $ip, $ns1, $ns2, $ns3 = null, $ns4 = null) - { - return $this->send('v-add-dns-domain', $this->userName, $domain, $ip, $ns1, $ns2, $ns3, - $ns4, 'no'); - } - - /** - * Set expiriation date. - * - * @param $domain - * @param $exp - * - * @return mixed - */ - public function changeDNSDomainExp($domain, $exp) - { - return $this->send('v-change-dns-domain-exp', $this->userName, $domain, $exp, 'no'); - } - - /** - * Set TTL. - * - * @param $domain - * @param $ttl - * - * @return mixed - */ - public function changeDNSDomainTtl($domain, $ttl) - { - return $this->send('v-change-dns-domain-ttl', $this->userName, $domain, $ttl, 'no'); - } - - /** - * List DNS record domain. - * - * @param $domain - * - * @return mixed - */ - public function listDNSRecords($domain) - { - $this->returnCode = 'no'; - $data = $this->send('v-list-dns-records', $this->userName, $domain, 'json'); - $data = json_decode($data, true); - - return $data; - } - - /** - * Change DNS domain record. - * - * @param $domain - * @param $recordId - * @param $val - * @param $priority - * - * @return mixed - */ - public function changeDNSDomainRecord($domain, $recordId, $val, $priority) - { - return $this->send('v-change-dns-record', $this->userName, $domain, $recordId, $val, - $priority); - } - - /** - * Remove DNS record. - * - * @param $domain - * @param $recordId - */ - public function removeDNSRecord($domain, $recordId) - { - $this->send('v-delete-dns-record', $this->userName, $domain, $recordId); - } - - /** - * Add DNS record. - * - * @param $domain - * @param $rec - * @param $type - * @param $val - * @param $priority - */ - public function addDNSRecord($domain, $rec, $type, $val, $priority) - { - $this->send('v-add-dns-record', $this->userName, $domain, $rec, $type, $val, $priority); - } -} diff --git a/src/Services/BD.php b/src/Services/Db.php similarity index 54% rename from src/Services/BD.php rename to src/Services/Db.php index 56265cc..e10e565 100644 --- a/src/Services/BD.php +++ b/src/Services/Db.php @@ -2,7 +2,7 @@ namespace VestaAPI\Services; -trait BD +trait Db { /** * List data base. @@ -11,22 +11,20 @@ trait BD */ public function listBD() { - $this->returnCode = 'no'; - - return json_decode($this->send('v-list-databases', $this->userName, 'json'), true); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-databases', $this->getUserName(), 'json')); } /** * Change user database. * * @param $database - * @param $dbuser + * @param $dbUser * * @return mixed */ - public function changeDbUser($database, $dbuser) + public function changeDbUser($database, $dbUser) { - return $this->send('v-change-database-user', $this->userName, $database, $dbuser); + return $this->toString($this->send('v-change-database-user', $this->getUserName(), $database, $dbUser)); } /** @@ -39,7 +37,7 @@ public function changeDbUser($database, $dbuser) */ public function changeDbPassword($database, $password) { - return $this->send('v-change-database-password', $this->userName, $database, $password); + return $this->toString($this->send('v-change-database-password', $this->getUserName(), $database, $password)); } /** @@ -51,11 +49,7 @@ public function changeDbPassword($database, $password) */ public function listOnlyBD($database) { - $this->returnCode = 'no'; - $listBd = $this->send('v-list-database', $this->userName, $database, 'json'); - $data = json_decode($listBd, true); - - return $data; + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-database', $this->getUserName(), $database, 'json')); } /** @@ -71,19 +65,19 @@ public function listOnlyBD($database) */ public function addDateBase($database, $dbuser, $password, $type, $charset) { - return $this->send('v-add-database', $this->userName, $database, $dbuser, $password, $type, - 'localhost', $charset); + return $this->toString($this->send('v-add-database', $this->getUserName(), $database, $dbuser, $password, $type, + 'localhost', $charset)); } /** * Delete data base. * - * @param $database + * @param $database * * @return mixed */ public function deleteDateBase($database) { - return $this->send('v-delete-database', $this->userName, $database); + return $this->toString($this->send('v-delete-database', $this->getUserName(), $database)); } } diff --git a/src/Services/Dns.php b/src/Services/Dns.php new file mode 100644 index 0000000..5247282 --- /dev/null +++ b/src/Services/Dns.php @@ -0,0 +1,156 @@ +setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-domains', $this->getUserName(), 'json')); + } + + /** + * List Only DNS. + * + * @param $dns + * + * @return mixed + */ + public function listOnlyDNS($dns) + { + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-domain', $this->getUserName(), $dns, + 'json')); + } + + /** + * Delete DNS record domain. + * + * @param $domain + * + * @return mixed + */ + public function deleteDNDDomain($domain) + { + return $this->toString($this->send('v-delete-dns-domain', $this->getUserName(), $domain)); + } + + /** + * Delete DNS record. + * + * @param $domain + * @param $recordId + * + * @return mixed + */ + public function deleteDNSRecord($domain, $recordId) + { + return $this->toString($this->send('v-delete-dns-record', $this->getUserName(), $domain, $recordId)); + } + + /** + * Add DNS domain. + * + * @param $domain + * @param $ip + * @param $ns1 + * @param $ns2 + * @param null $ns3 + * @param null $ns4 + * + * @return mixed + */ + public function addDNSDomain($domain, $ip, $ns1, $ns2, $ns3 = null, $ns4 = null) + { + return $this->toString($this->send('v-add-dns-domain', $this->getUserName(), $domain, $ip, $ns1, $ns2, $ns3, $ns4, 'no')); + } + + /** + * Set expiriation date. + * + * @param $domain + * @param $exp + * + * @return mixed + */ + public function changeDNSDomainExp($domain, $exp) + { + return $this->toString($this->send('v-change-dns-domain-exp', $this->getUserName(), $domain, $exp, 'no')); + } + + /** + * Set TTL. + * + * @param $domain + * @param $ttl + * + * @return mixed + */ + public function changeDNSDomainTtl($domain, $ttl) + { + return $this->toString($this->send('v-change-dns-domain-ttl', $this->getUserName(), $domain, $ttl, 'no')); + } + + /** + * List DNS record domain. + * + * @param $domain + * + * @return mixed + */ + public function listDNSRecords($domain) + { + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-records', $this->getUserName(), $domain, 'json')); + } + + /** + * Change DNS domain record. + * + * @param $domain + * @param $recordId + * @param $val + * @param $priority + * + * @return mixed + */ + public function changeDNSDomainRecord($domain, $recordId, $val, $priority) + { + return $this->toString($this->send('v-change-dns-record', $this->getUserName(), $domain, $recordId, $val, $priority)); + } + + /** + * Remove DNS record. + * + * @param $domain + * @param $recordId + * + * @return string + */ + public function removeDNSRecord($domain, $recordId) + { + return $this->toString($this->send('v-delete-dns-record', $this->getUserName(), $domain, $recordId)); + } + + /** + * Add DNS record. + * + * @param $domain + * @param $rec + * @param $type + * @param $val + * @param $priority + * + * @return string + */ + public function addDNSRecord($domain, $rec, $type, $val, $priority) + { + return $this->toString($this->send('v-add-dns-record', $this->getUserName(), $domain, $rec, $type, $val, + $priority)); + } +} diff --git a/src/Services/FileSystem.php b/src/Services/FileSystem.php index 38b8eaa..4c280bf 100644 --- a/src/Services/FileSystem.php +++ b/src/Services/FileSystem.php @@ -7,7 +7,7 @@ trait FileSystem /** * @var string */ - protected $delimeter = '|'; + protected $delimiter = '|'; /** * @var array @@ -24,16 +24,14 @@ trait FileSystem ]; /** - * @param $src - * @param $dst + * @param $src + * @param $dst * * @return mixed */ public function moveFile($src, $dst) { - $this->returnCode = 'no'; - - return $this->send('v-move-fs-file', $this->userName, $src, $dst); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-move-fs-file', $this->getUserName(), $src, $dst)); } /** @@ -43,48 +41,40 @@ public function moveFile($src, $dst) */ public function openFile($path = '') { - $path = '/home/'.$this->userName.'/'.$path; - $this->returnCode = 'no'; - - return $this->send('v-open-fs-file', $this->userName, $path); + $path = '/home/'.$this->getUserName().'/'.$path; + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-open-fs-file', $this->getUserName(), $path)); } /** - * @param $patch + * @param $path * * @return mixed */ - public function addDir($patch) + public function addDir($path) { - $this->returnCode = 'no'; - - return $this->send('v-add-fs-directory', $this->userName, $patch); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-add-fs-directory', $this->getUserName(), $path)); } /** - * @param $patch + * @param $path * * @return mixed */ - public function addFile($patch) + public function addFile($path) { - $this->returnCode = 'no'; - - return $this->send('v-add-fs-file', $this->userName, $patch); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-add-fs-file', $this->getUserName(), $path)); } /** - * @param $srcFile - * @param $permissions + * @param $srcFile + * @param $permissions * * @return mixed */ public function changePermission($srcFile, $permissions) { - $srcFile = '/home/'.$this->userName.'/'.$srcFile; - $this->returnCode = 'no'; - - return $this->send('v-change-fs-file-permission', $this->userName, $srcFile, $permissions); + $srcFile = '/home/'.$this->getUserName().'/'.$srcFile; + return $this->setReturnCode('no')->toArray($this->send('v-change-fs-file-permission', $this->getUserName(), $srcFile, $permissions)); } /** @@ -95,9 +85,7 @@ public function changePermission($srcFile, $permissions) */ public function copyDir($srcDir, $dstDir) { - $this->returnCode = 'no'; - - return $this->send('v-copy-fs-directory', $this->userName, $srcDir, $dstDir); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-copy-fs-directory', $this->getUserName(), $srcDir, $dstDir)); } /** @@ -108,22 +96,18 @@ public function copyDir($srcDir, $dstDir) */ public function copyFile($srcDir, $dstDir) { - $this->returnCode = 'no'; - - return $this->send('v-copy-fs-file', $this->userName, $srcDir, $dstDir); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-copy-fs-file', $this->getUserName(), $srcDir, $dstDir)); } /** - * @param $dstDir + * @param $dstDir * * @return mixed */ public function deleteDir($dstDir) { - $this->returnCode = 'no'; - $dstDir = '/home/'.$this->userName.'/'.$dstDir; - - return $this->send('v-delete-fs-dir', $this->userName, $dstDir); + $dstDir = '/home/'.$this->getUserName().'/'.$dstDir; + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-delete-fs-dir', $this->getUserName(), $dstDir)); } /** @@ -133,10 +117,8 @@ public function deleteDir($dstDir) */ public function deleteFile($dstFile) { - $this->returnCode = 'no'; - $dstFile = '/home/'.$this->userName.'/'.$dstFile; - - return $this->send('v-delete-fs-file', $this->userName, $dstFile); + $dstFile = '/home/'.$this->getUserName().'/'.$dstFile; + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-delete-fs-file', $this->getUserName(), $dstFile)); } /** @@ -147,21 +129,18 @@ public function deleteFile($dstFile) */ public function extractArchive($srcFile, $dstDir) { - $this->returnCode = 'no'; - - return $this->send('v-extract-fs-archive', $this->userName, $srcFile, $dstDir); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-extract-fs-archive', $this->getUserName(), $srcFile, $dstDir)); } /** - * @param $path + * @param string $path * * @return mixed */ public function listDirectory($path = '') { - $path = '/home/'.$this->userName.'/'.$path; - $this->returnCode = 'no'; - $responseVesta = $this->send('v-list-fs-directory', $this->userName, $path); + $path = '/home/'.$this->getUserName().'/'.$path; + $responseVesta = $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-fs-directory', $this->getUserName(), $path)); return $this->parseListing($responseVesta); } @@ -178,8 +157,10 @@ public function parseListing($raw) $data = []; foreach ($raw as $o) { - $info = explode($this->delimeter, $o); - + $info = explode($this->delimiter, $o); + if (empty($info)) { + continue; + } $value = [ 'type' => $info[$this->info_positions['TYPE']], 'permissions' => $info[$this->info_positions['PERMISSIONS']], @@ -190,7 +171,6 @@ public function parseListing($raw) 'size' => $info[$this->info_positions['SIZE']], 'name' => (!empty($info[$this->info_positions['NAME']])) ? $info[$this->info_positions['NAME']] : '../', ]; - array_push($data, $value); } diff --git a/src/Services/Sender.php b/src/Services/Sender.php new file mode 100644 index 0000000..64bbb59 --- /dev/null +++ b/src/Services/Sender.php @@ -0,0 +1,137 @@ +uri = $uri; + return $this; + } + + /** + * @param array $postString + * + * @return Sender + */ + public function setPostString($postString) + { + $this->postString = http_build_query($postString); + return $this; + } + + /** + * @param int $timeout + * + * @return Sender + */ + public function setTimeout($timeout) + { + $this->timeout = $timeout; + return $this; + } + + /** + * @param bool $sslVerify + * + * @return Sender + */ + public function setSslVerify($sslVerify) + { + $this->sslVerify = $sslVerify; + return $this; + } + + /** + * @return mixed + */ + public function getRaw() + { + return $this->get(); + } + + /** + * @return array + */ + public function getArray() + { + return $this->toArray($this->get()); + } + + /** + * @return mixed + */ + private function get() + { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $this->uri); + curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $this->postString); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout); + if (!$this->sslVerify) { + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + } + $result = curl_exec($ch); + curl_close($ch); + return $result; + } + + /** + * @param $json + * + * @return mixed + */ + private function toArray($json) + { + if (empty($json)) { + return []; + } + return json_decode($json, true); + } + +} \ No newline at end of file diff --git a/src/Services/Service.php b/src/Services/Service.php index b72a07a..6a05a0d 100644 --- a/src/Services/Service.php +++ b/src/Services/Service.php @@ -11,7 +11,7 @@ trait Service */ public function restartDNSServer() { - return $this->send('v-restart-dns'); + return $this->toString($this->send('v-restart-dns')); } /** @@ -21,9 +21,7 @@ public function restartDNSServer() */ public function userSearch($query) { - $this->returnCode = 'no'; - - return json_decode($this->send('v-search-user-object', $this->userName, $query, 'json'), true); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-search-user-object', $this->getUserName(), $query, 'json')); } /** @@ -31,9 +29,7 @@ public function userSearch($query) */ public function listStats() { - $this->returnCode = 'no'; - $data = json_decode($this->send('v-list-users-stats', 'json'), true); - + $data = $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-users-stats', 'json')); return array_reverse($data, true); } @@ -42,9 +38,7 @@ public function listStats() */ public function listRRD() { - $this->returnCode = 'no'; - - return json_decode($this->send('v-list-sys-rrd', 'json'), true); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-sys-rrd', 'json')); } /** @@ -52,9 +46,7 @@ public function listRRD() */ public function listSysInfo() { - $this->returnCode = 'no'; - - return json_decode($this->send('v-list-sys-info', 'json'), true); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-sys-info', 'json')); } /** @@ -62,9 +54,7 @@ public function listSysInfo() */ public function listSysService() { - $this->returnCode = 'no'; - - return json_decode($this->send('v-list-sys-services', 'json'), true); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-sys-services', 'json')); } /** @@ -74,7 +64,7 @@ public function listSysService() */ public function restartService($service) { - return $this->send('v-restart-service', $service); + return $this->toString($this->send('v-restart-service', $service)); } /** @@ -84,7 +74,7 @@ public function restartService($service) */ public function startService($service) { - return $this->send('v-start-service', $service); + return $this->toString($this->send('v-start-service', $service)); } /** @@ -94,7 +84,7 @@ public function startService($service) */ public function stopService($service) { - return $this->send('v-stop-service', $service); + return $this->toString($this->send('v-stop-service', $service)); } /** @@ -102,9 +92,7 @@ public function stopService($service) */ public function listIp() { - $this->returnCode = 'no'; - - return json_decode($this->send('v-list-sys-ips', 'json'), true); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-sys-ips', 'json')); } /** @@ -114,29 +102,25 @@ public function listIp() */ public function getIp($ip) { - $this->returnCode = 'no'; - - return json_decode($this->send('v-list-sys-ip', $ip, 'json'), true); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-sys-ip', $ip, 'json')); } /** * @return mixed - * * @internal param string $restart */ public function rebuildWebDomains() { - return $this->send('v-rebuild-web-domains', $this->userName); + return $this->toString($this->send('v-rebuild-web-domains', $this->getUserName())); } /** * @return mixed - * * @internal param string $restart */ public function rebuildDNSDomains() { - return $this->send('v-rebuild-dns-domains', $this->userName); + return $this->toString($this->send('v-rebuild-dns-domains', $this->getUserName())); } /** @@ -144,7 +128,7 @@ public function rebuildDNSDomains() */ public function rebuildMailDomains() { - return $this->send('v-rebuild-mail-domains', $this->userName); + return $this->toString($this->send('v-rebuild-mail-domains', $this->getUserName())); } /** @@ -152,25 +136,26 @@ public function rebuildMailDomains() */ public function rebuildDataBases() { - return $this->send('v-rebuild-databases', $this->userName); + return $this->toString($this->send('v-rebuild-databases', $this->getUserName())); } /** * @return mixed - * * @internal param string $restart */ public function rebuildCronJobs() { - return $this->send('v-rebuild-cron-jobs', $this->userName); + return $this->toString($this->send('v-rebuild-cron-jobs', $this->getUserName())); } /** + * @param $user + * * @return mixed */ - public function updateUserCounters() + public function updateUserCounters($user) { - return $this->send('v-update-user-counters', $this->userName); + return $this->toString($this->send('v-update-user-counters', $this->getUserName())); } /** @@ -180,6 +165,6 @@ public function updateUserCounters() */ public function updateSysVesta($package) { - return $this->send('v-update-sys-vesta', $package); + return $this->toString($this->send('v-update-sys-vesta', $package)); } } diff --git a/src/Services/User.php b/src/Services/User.php index 05dc8e8..85a318d 100644 --- a/src/Services/User.php +++ b/src/Services/User.php @@ -18,7 +18,7 @@ trait User */ public function regUser($username, $password, $email, $package, $fistName, $lastName) { - return $this->send('v-add-user', $username, $password, $email, $package, $fistName, $lastName); + return $this->toString($this->send('v-add-user', $username, $password, $email, $package, $fistName, $lastName)); } /** @@ -28,7 +28,7 @@ public function regUser($username, $password, $email, $package, $fistName, $last */ public function changeUserPassword($password) { - return $this->send('v-change-user-password', $this->userName, $password); + return $this->toString($this->send('v-change-user-password', $this->getUserName(), $password)); } /** @@ -38,7 +38,7 @@ public function changeUserPassword($password) */ public function changeUserEmail($email) { - return $this->send('v-change-user-contact', $this->userName, $email); + return $this->toString($this->send('v-change-user-contact', $this->getUserName(), $email)); } /** @@ -48,11 +48,7 @@ public function changeUserEmail($email) */ public function listUserAccount() { - $this->returnCode = 'no'; - $answer = $this->send('v-list-user', $this->userName, 'json'); - $data = json_decode($answer, true); - - return $data; + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user', $this->getUserName(), 'json')); } /** @@ -60,11 +56,7 @@ public function listUserAccount() */ public function listUserLog() { - $this->returnCode = 'no'; - $answer = $this->send('v-list-user-log', $this->userName, 'json'); - $data = json_decode($answer, true); - - return $data; + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user-log', $this->getUserName(), 'json')); } /** @@ -74,11 +66,7 @@ public function listUserLog() */ public function listUserBackups() { - $this->returnCode = 'no'; - $answer = $this->send('v-list-user-backups', $this->userName, 'json'); - $data = json_decode($answer, true); - - return $data; + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user-backups', $this->getUserName(), 'json')); } /** @@ -90,7 +78,7 @@ public function listUserBackups() */ public function deleteUserBackup($backup) { - return $this->send('v-delete-user-backup', $this->userName, $backup); + return $this->toString($this->send('v-delete-user-backup', $this->getUserName(), $backup)); } /** @@ -102,11 +90,7 @@ public function deleteUserBackup($backup) */ public function showUserBackup($backup) { - $this->returnCode = 'no'; - $answer = $this->send('v-list-user-backup', $this->userName, $backup, 'json'); - $data = json_decode($answer, true); - - return $data; + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user-backup', $this->getUserName(), $backup, 'json')); } /** @@ -125,8 +109,8 @@ public function restoreBackup($arg) $udir = 'no'; extract($arg, EXTR_OVERWRITE); - return $this->send('v-schedule-user-restore', $this->userName, $backup, $web, $dns, $mail, $db, - $cron, $udir); + return $this->toString($this->send('v-schedule-user-restore', $this->getUserName(), $backup, $web, $dns, $mail, + $db, $cron, $udir)); } /** @@ -134,7 +118,7 @@ public function restoreBackup($arg) */ public function suspendUser() { - return $this->send('v-suspend-user', $this->userName, 'no'); + return $this->toString($this->send('v-suspend-user', $this->getUserName(), 'no')); } /** @@ -144,7 +128,7 @@ public function suspendUser() */ public function changePackage($package) { - return $this->send('v-suspend-user', $this->userName, $package); + return $this->toString($this->send('v-suspend-user', $this->getUserName(), $package)); } /** @@ -152,11 +136,7 @@ public function changePackage($package) */ public function listUserPackages() { - $this->returnCode = 'no'; - $answer = $this->send('v-list-user-packages', 'json'); - $data = json_decode($answer, true); - - return $data; + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user-packages', 'json')); } /** @@ -166,9 +146,7 @@ public function listUserPackages() */ public function getValue($option) { - $this->returnCode = 'no'; - - return $this->send('v-get-user-value', $this->userName, $option); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-get-user-value', $this->getUserName(), $option)); } /** @@ -178,7 +156,7 @@ public function getValue($option) */ public function changeShell($ssh = 'nologin') { - return $this->send('v-change-user-shell', $this->userName, $ssh); + return $this->toString($this->send('v-change-user-shell', $this->getUserName(), $ssh)); } /** @@ -186,11 +164,7 @@ public function changeShell($ssh = 'nologin') */ public function adminListUserAccount() { - $this->returnCode = 'no'; - $answer = $this->send('v-list-user', $this->userName, 'json'); - $data = json_decode($answer, true); - - return $data; + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user', $this->getUserName(), 'json')); } /** @@ -198,11 +172,7 @@ public function adminListUserAccount() */ public function adminListUserPackages() { - $this->returnCode = 'no'; - $answer = $this->send('v-list-user-packages', 'json'); - $data = json_decode($answer, true); - - return $data; + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user-packages', 'json')); } /** @@ -210,10 +180,6 @@ public function adminListUserPackages() */ public function adminListUserShell() { - $this->returnCode = 'no'; - $answer = $this->send('v-list-sys-shells', 'json'); - $data = json_decode($answer, true); - - return $data; + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-sys-shells', 'json')); } } diff --git a/src/Services/VestaAPI.php b/src/Services/VestaAPI.php index d573efd..5f753ce 100644 --- a/src/Services/VestaAPI.php +++ b/src/Services/VestaAPI.php @@ -2,24 +2,31 @@ namespace VestaAPI\Services; -use GuzzleHttp\Client; use VestaAPI\Exceptions\VestaExceptions; class VestaAPI { - use BD, DNS, User, Web, Service, Cron, FileSystem; + use Db, Dns, User, Web, Service, Cron, FileSystem; + + const RETURN_CODE_YES = 'yes', + RETURN_CODE_NO = 'no'; /** - * @var + * @var string + */ + private $adminUser = ''; + + /** + * @var string */ - public $userName = ''; + private $userName = ''; /** * return no|yes|json. * * @var string */ - public $returnCode = 'yes'; + private $returnCode = 'yes'; /** * @var string @@ -31,6 +38,11 @@ class VestaAPI */ private $host = ''; + /** + * @var bool + */ + private $toArray = false; + /** * @param string $server * @@ -53,8 +65,9 @@ public function server($server = '') throw new \Exception('Specified server config does not contain host or key'); } - $this->key = (string) $allServers[$server]['key']; - $this->host = (string) $allServers[$server]['host']; + $this->key = $allServers[$server]['key']; + $this->host = $allServers[$server]['host']; + $this->adminUser = $allServers[$server]['admin']; return $this; } @@ -80,25 +93,62 @@ private function keysCheck($server, $config) public function setUserName($userName = '') { if (empty($userName)) { - throw new \Exception('Server is not specified'); + throw new \Exception('User is not specified'); } $this->userName = $userName; return $this; } + /** + * @return string + */ + public function getUserName() + { + return $this->userName; + } + + /** + * @return string + */ + public function getReturnCode() + { + return $this->returnCode; + } + + /** + * @param string $returnCode + * + * @return VestaAPI + */ + public function setReturnCode($returnCode) + { + $this->returnCode = $returnCode; + return $this; + } + + /** + * @param bool $toArray + * + * @return VestaAPI + */ + public function setToArray($toArray) + { + $this->toArray = $toArray; + return $this; + } + /** * @param string $cmd * + * @return Sender * @throws VestaExceptions - * - * @return string */ public function send($cmd) { $postVars = [ - 'user' => $this->userName, - 'password' => $this->key, + 'user' => $this->adminUser, + 'hash' => $this->key, // api key 'returncode' => $this->returnCode, 'cmd' => $cmd, ]; @@ -110,21 +160,34 @@ public function send($cmd) $postVars['arg'.$num] = $args[$num]; } - $client = new Client([ - 'base_uri' => 'https://'.$this->host.':8083/api/', - 'timeout' => 10.0, - 'verify' => false, - 'form_params' => $postVars, - ]); - - $query = $client->post('index.php') - ->getBody() - ->getContents(); + $query = Sender::create() + ->setUri('https://' . $this->host . ':8083/api/') + ->setPostString($postVars) + ->setTimeout(10); - if ($this->returnCode == 'yes' && $query != 0) { + if ($this->getReturnCode() == 'yes' && $query != 0) { throw new VestaExceptions($query); } - return $query; } + + /** + * @param Sender $sender + * + * @return mixed + */ + public function toString(Sender $sender) + { + return $sender->getRaw(); + } + + /** + * @param Sender $sender + * + * @return array + */ + public function toArray(Sender $sender) + { + return $sender->getArray(); + } } diff --git a/src/Services/Web.php b/src/Services/Web.php index df0fdd2..2395254 100644 --- a/src/Services/Web.php +++ b/src/Services/Web.php @@ -13,10 +13,7 @@ trait Web */ public function listEditWebDomain($domain) { - $this->returnCode = 'no'; - $answer = $this->send('v-list-web-domain', $this->userName, $domain, 'json'); - - $data = json_decode($answer, true); + $data = $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-web-domain', $this->getUserName(), $domain, 'json')); $ftpU = strpos($data[$domain]['FTP_USER'], ':'); $ftpPath = strpos($data[$domain]['FTP_PATH'], ':'); @@ -40,11 +37,7 @@ public function listEditWebDomain($domain) */ public function listWebDomain() { - $this->returnCode = 'no'; - $answer = $this->send('v-list-web-domains', $this->userName, 'json'); - $data = json_decode($answer, true); - - return $data; + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-web-domains', $this->getUserName(), 'json')); } /** @@ -57,7 +50,7 @@ public function listWebDomain() */ public function addWebDomain($domain, $ip) { - return $this->send('v-add-web-domain', $this->userName, $domain, $ip); + return $this->toString($this->send('v-add-web-domain', $this->getUserName(), $domain, $ip)); } /** @@ -70,7 +63,7 @@ public function addWebDomain($domain, $ip) */ public function addDns($domain, $ip) { - return $this->send('v-add-dns-domain', $this->userName, $domain, $ip); + return $this->toString($this->send('v-add-dns-domain', $this->getUserName(), $domain, $ip)); } /** @@ -82,7 +75,7 @@ public function addDns($domain, $ip) */ public function addMail($domain) { - return $this->send('v-add-mail-domain', $this->userName, $domain); + return $this->toString($this->send('v-add-mail-domain', $this->getUserName(), $domain)); } /** @@ -93,7 +86,7 @@ public function addMail($domain) */ public function addWebDomainAlias($domain, $alias) { - $this->send('v-add-web-domain-alias', $this->userName, $domain, $alias, 'no'); + return $this->toString($this->send('v-add-web-domain-alias', $this->getUserName(), $domain, $alias, 'no')); } /** @@ -106,7 +99,7 @@ public function addWebDomainAlias($domain, $alias) */ public function addDnsAlias($domain, $alias) { - return $this->send('v-add-dns-on-web-alias', $this->userName, $domain, $alias, 'no'); + return $this->toString($this->send('v-add-dns-on-web-alias', $this->getUserName(), $domain, $alias, 'no')); } /** @@ -119,7 +112,7 @@ public function addDnsAlias($domain, $alias) */ public function deleteWebDomainAlias($domain, $alias) { - return $this->send('v-delete-web-domain-alias', $this->userName, $domain, $alias, 'no'); + return $this->toString($this->send('v-delete-web-domain-alias', $this->getUserName(), $domain, $alias, 'no')); } /** @@ -134,8 +127,8 @@ public function deleteWebDomainAlias($domain, $alias) */ public function addFtpDomain($domain, $ftpUserName, $ftpPassword, $ftpPath) { - return $this->send('v-add-web-domain-ftp', $this->userName, $domain, $ftpUserName, $ftpPassword, - $ftpPath); + return $this->toString($this->send('v-add-web-domain-ftp', $this->getUserName(), $domain, $ftpUserName, $ftpPassword, + $ftpPath)); } /** @@ -148,7 +141,7 @@ public function addFtpDomain($domain, $ftpUserName, $ftpPassword, $ftpPath) */ public function addDomainProxy($domain, $ext) { - return $this->send('v-add-web-domain-proxy', $this->userName, $domain, '', $ext, 'no'); + return $this->toString($this->send('v-add-web-domain-proxy', $this->getUserName(), $domain, '', $ext, 'no')); } /** @@ -160,7 +153,7 @@ public function addDomainProxy($domain, $ext) */ public function deleteDomain($domain) { - return $this->send('v-delete-domain', $this->userName, $domain); + return $this->toString($this->send('v-delete-domain', $this->getUserName(), $domain)); } /** @@ -173,7 +166,7 @@ public function deleteDomain($domain) */ public function changeWebDomainIp($domain, $ip) { - return $this->send('v-change-web-domain-ip', $this->userName, $domain, $ip, 'no'); + return $this->toString($this->send('v-change-web-domain-ip', $this->getUserName(), $domain, $ip, 'no')); } /** @@ -186,7 +179,7 @@ public function changeWebDomainIp($domain, $ip) */ public function deleteWebDomain($domain, $ftpUserName) { - return $this->send('v-delete-web-domain-ftp', $this->userName, $domain, $ftpUserName); + return $this->toString($this->send('v-delete-web-domain-ftp', $this->getUserName(), $domain, $ftpUserName)); } /** @@ -200,8 +193,8 @@ public function deleteWebDomain($domain, $ftpUserName) */ public function changeWebDomain($domain, $ftpUserName, $ftpPath) { - return $this->send('v-change-web-domain-ftp-path', $this->userName, $domain, $ftpUserName, - $ftpPath); + return $this->toString($this->send('v-change-web-domain-ftp-path', $this->getUserName(), $domain, $ftpUserName, + $ftpPath)); } /** @@ -215,7 +208,7 @@ public function changeWebDomain($domain, $ftpUserName, $ftpPath) */ public function changeFtpPassword($domain, $ftpUserName, $password) { - return $this->send('v-change-web-domain-ftp-password', $this->userName, $domain, $ftpUserName, - $password); + return $this->toString($this->send('v-change-web-domain-ftp-password', $this->getUserName(), $domain, $ftpUserName, + $password)); } }