From 65d79082131bec56aa49212b5d29bf0245ce98b5 Mon Sep 17 00:00:00 2001 From: Evgeniy Date: Mon, 18 Sep 2017 16:58:10 +0300 Subject: [PATCH 1/8] new client and refactor --- src/Services/BD.php | 16 ++--- src/Services/Cron.php | 10 +-- src/Services/DNS.php | 4 +- src/Services/FileSystem.php | 55 +++++++-------- src/Services/Sender.php | 134 ++++++++++++++++++++++++++++++++++++ src/Services/Service.php | 16 ++--- src/Services/User.php | 44 ++++++------ src/Services/VestaAPI.php | 67 +++++++++++++----- src/Services/Web.php | 34 ++++----- 9 files changed, 274 insertions(+), 106 deletions(-) create mode 100644 src/Services/Sender.php diff --git a/src/Services/BD.php b/src/Services/BD.php index 56265cc..e219e84 100644 --- a/src/Services/BD.php +++ b/src/Services/BD.php @@ -11,9 +11,9 @@ trait BD */ public function listBD() { - $this->returnCode = 'no'; + $this->setReturnCode('no'); - return json_decode($this->send('v-list-databases', $this->userName, 'json'), true); + return json_decode($this->send('v-list-databases', $this->getUserName(), 'json'), true); } /** @@ -26,7 +26,7 @@ public function listBD() */ public function changeDbUser($database, $dbuser) { - return $this->send('v-change-database-user', $this->userName, $database, $dbuser); + return $this->send('v-change-database-user', $this->getUserName(), $database, $dbuser); } /** @@ -39,7 +39,7 @@ public function changeDbUser($database, $dbuser) */ public function changeDbPassword($database, $password) { - return $this->send('v-change-database-password', $this->userName, $database, $password); + return $this->send('v-change-database-password', $this->getUserName(), $database, $password); } /** @@ -51,8 +51,8 @@ public function changeDbPassword($database, $password) */ public function listOnlyBD($database) { - $this->returnCode = 'no'; - $listBd = $this->send('v-list-database', $this->userName, $database, 'json'); + $this->setReturnCode('no'); + $listBd = $this->send('v-list-database', $this->getUserName(), $database, 'json'); $data = json_decode($listBd, true); return $data; @@ -71,7 +71,7 @@ public function listOnlyBD($database) */ public function addDateBase($database, $dbuser, $password, $type, $charset) { - return $this->send('v-add-database', $this->userName, $database, $dbuser, $password, $type, + return $this->send('v-add-database', $this->getUserName(), $database, $dbuser, $password, $type, 'localhost', $charset); } @@ -84,6 +84,6 @@ public function addDateBase($database, $dbuser, $password, $type, $charset) */ public function deleteDateBase($database) { - return $this->send('v-delete-database', $this->userName, $database); + return $this->send('v-delete-database', $this->getUserName(), $database); } } diff --git a/src/Services/Cron.php b/src/Services/Cron.php index e977ddd..531d42a 100644 --- a/src/Services/Cron.php +++ b/src/Services/Cron.php @@ -12,7 +12,7 @@ trait Cron public function listCron() { $this->returnCode = 'no'; - $listDns = $this->send('v-list-cron-jobs', $this->userName, 'json'); + $listDns = $this->send('v-list-cron-jobs', $this->getUserName(), 'json'); $data = json_decode($listDns, true); return $data; @@ -32,7 +32,7 @@ public function listCron() */ public function addCron($min, $hour, $day, $month, $wday, $cmd) { - return $this->send('v-add-cron-job', $this->userName, $min, $hour, $day, $month, $wday, + return $this->send('v-add-cron-job', $this->getUserName(), $min, $hour, $day, $month, $wday, $cmd); } @@ -46,7 +46,7 @@ public function addCron($min, $hour, $day, $month, $wday, $cmd) public function showCron($job) { $this->returnCode = 'no'; - $request = $this->send('v-list-cron-job', $this->userName, $job, 'json'); + $request = $this->send('v-list-cron-job', $this->getUserName(), $job, 'json'); $data = json_decode($request, true); return $data; @@ -61,7 +61,7 @@ public function showCron($job) */ public function deleteCron($job) { - return $this->send('v-delete-cron-job', $this->userName, $job); + return $this->send('v-delete-cron-job', $this->getUserName(), $job); } /** @@ -79,7 +79,7 @@ public function deleteCron($job) */ public function editCron($job, $min, $hour, $day, $month, $wday, $cmd) { - return $this->send('v-change-cron-job', $this->userName, $job, $min, $hour, $day, $month, + return $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 index 164516c..8239a2e 100644 --- a/src/Services/DNS.php +++ b/src/Services/DNS.php @@ -11,7 +11,7 @@ trait DNS */ public function listDNS() { - $this->returnCode = 'no'; + $this->setReturnCode('no'); $listDns = $this->send('v-list-dns-domains', $this->userName, 'json'); $data = json_decode($listDns, true); @@ -27,7 +27,7 @@ public function listDNS() */ public function listOnlyDNS($dns) { - $this->returnCode = 'no'; + $this->setReturnCode('no'); $listDNS = $this->send('v-list-dns-domain', $this->userName, $dns, 'json'); $data = json_decode($listDNS, true); diff --git a/src/Services/FileSystem.php b/src/Services/FileSystem.php index 38b8eaa..440f5c9 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 @@ -31,9 +31,9 @@ trait FileSystem */ public function moveFile($src, $dst) { - $this->returnCode = 'no'; + $this->setReturnCode('no'); - return $this->send('v-move-fs-file', $this->userName, $src, $dst); + return $this->send('v-move-fs-file', $this->getUserName(), $src, $dst); } /** @@ -43,10 +43,10 @@ public function moveFile($src, $dst) */ public function openFile($path = '') { - $path = '/home/'.$this->userName.'/'.$path; - $this->returnCode = 'no'; + $path = '/home/'.$this->getUserName().'/'.$path; + $this->setReturnCode('no'); - return $this->send('v-open-fs-file', $this->userName, $path); + return $this->send('v-open-fs-file', $this->getUserName(), $path); } /** @@ -58,7 +58,7 @@ public function addDir($patch) { $this->returnCode = 'no'; - return $this->send('v-add-fs-directory', $this->userName, $patch); + return $this->send('v-add-fs-directory', $this->getUserName(), $patch); } /** @@ -70,7 +70,7 @@ public function addFile($patch) { $this->returnCode = 'no'; - return $this->send('v-add-fs-file', $this->userName, $patch); + return $this->send('v-add-fs-file', $this->getUserName(), $patch); } /** @@ -81,10 +81,10 @@ public function addFile($patch) */ public function changePermission($srcFile, $permissions) { - $srcFile = '/home/'.$this->userName.'/'.$srcFile; + $srcFile = '/home/'.$this->getUserName().'/'.$srcFile; $this->returnCode = 'no'; - return $this->send('v-change-fs-file-permission', $this->userName, $srcFile, $permissions); + return $this->send('v-change-fs-file-permission', $this->getUserName(), $srcFile, $permissions); } /** @@ -95,9 +95,9 @@ public function changePermission($srcFile, $permissions) */ public function copyDir($srcDir, $dstDir) { - $this->returnCode = 'no'; + $this->setReturnCode('no'); - return $this->send('v-copy-fs-directory', $this->userName, $srcDir, $dstDir); + return $this->send('v-copy-fs-directory', $this->getUserName(), $srcDir, $dstDir); } /** @@ -108,9 +108,9 @@ public function copyDir($srcDir, $dstDir) */ public function copyFile($srcDir, $dstDir) { - $this->returnCode = 'no'; + $this->setReturnCode('no'); - return $this->send('v-copy-fs-file', $this->userName, $srcDir, $dstDir); + return $this->send('v-copy-fs-file', $this->getUserName(), $srcDir, $dstDir); } /** @@ -121,9 +121,9 @@ public function copyFile($srcDir, $dstDir) public function deleteDir($dstDir) { $this->returnCode = 'no'; - $dstDir = '/home/'.$this->userName.'/'.$dstDir; + $dstDir = '/home/'.$this->getUserName().'/'.$dstDir; - return $this->send('v-delete-fs-dir', $this->userName, $dstDir); + return $this->send('v-delete-fs-dir', $this->getUserName(), $dstDir); } /** @@ -133,10 +133,10 @@ public function deleteDir($dstDir) */ public function deleteFile($dstFile) { - $this->returnCode = 'no'; - $dstFile = '/home/'.$this->userName.'/'.$dstFile; + $this->setReturnCode('no'); + $dstFile = '/home/'.$this->getUserName().'/'.$dstFile; - return $this->send('v-delete-fs-file', $this->userName, $dstFile); + return $this->send('v-delete-fs-file', $this->getUserName(), $dstFile); } /** @@ -147,9 +147,9 @@ public function deleteFile($dstFile) */ public function extractArchive($srcFile, $dstDir) { - $this->returnCode = 'no'; + $this->setReturnCode('no'); - return $this->send('v-extract-fs-archive', $this->userName, $srcFile, $dstDir); + return $this->send('v-extract-fs-archive', $this->getUserName(), $srcFile, $dstDir); } /** @@ -159,9 +159,9 @@ public function extractArchive($srcFile, $dstDir) */ 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; + $this->setReturnCode('no'); + $responseVesta = $this->send('v-list-fs-directory', $this->getUserName(), $path); return $this->parseListing($responseVesta); } @@ -178,8 +178,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 +192,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..022072b --- /dev/null +++ b/src/Services/Sender.php @@ -0,0 +1,134 @@ +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 mixed + */ + public function getDecoded() + { + return $this->getArray($this->get()); + } + + /** + * @return mixed + */ + private function get() + { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $this->uri); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $this->postString); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout); + if ($this->sslVerify) { + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); + } + $result = curl_exec($ch); + curl_close($ch); + return $result; + } + + /** + * @param $json + * + * @return mixed + */ + private function getArray($json) + { + 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..65a770a 100644 --- a/src/Services/Service.php +++ b/src/Services/Service.php @@ -21,9 +21,9 @@ public function restartDNSServer() */ public function userSearch($query) { - $this->returnCode = 'no'; + $this->setReturnCode('no'); - return json_decode($this->send('v-search-user-object', $this->userName, $query, 'json'), true); + return json_decode($this->send('v-search-user-object', $this->getUserName(), $query, 'json'), true); } /** @@ -126,7 +126,7 @@ public function getIp($ip) */ public function rebuildWebDomains() { - return $this->send('v-rebuild-web-domains', $this->userName); + return $this->send('v-rebuild-web-domains', $this->getUserName()); } /** @@ -136,7 +136,7 @@ public function rebuildWebDomains() */ public function rebuildDNSDomains() { - return $this->send('v-rebuild-dns-domains', $this->userName); + return $this->send('v-rebuild-dns-domains', $this->getUserName()); } /** @@ -144,7 +144,7 @@ public function rebuildDNSDomains() */ public function rebuildMailDomains() { - return $this->send('v-rebuild-mail-domains', $this->userName); + return $this->send('v-rebuild-mail-domains', $this->getUserName()); } /** @@ -152,7 +152,7 @@ public function rebuildMailDomains() */ public function rebuildDataBases() { - return $this->send('v-rebuild-databases', $this->userName); + return $this->send('v-rebuild-databases', $this->getUserName()); } /** @@ -162,7 +162,7 @@ public function rebuildDataBases() */ public function rebuildCronJobs() { - return $this->send('v-rebuild-cron-jobs', $this->userName); + return $this->send('v-rebuild-cron-jobs', $this->getUserName()); } /** @@ -170,7 +170,7 @@ public function rebuildCronJobs() */ public function updateUserCounters() { - return $this->send('v-update-user-counters', $this->userName); + return $this->send('v-update-user-counters', $this->getUserName()); } /** diff --git a/src/Services/User.php b/src/Services/User.php index 05dc8e8..90f6d3b 100644 --- a/src/Services/User.php +++ b/src/Services/User.php @@ -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->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->send('v-change-user-contact', $this->getUserName(), $email); } /** @@ -48,8 +48,8 @@ public function changeUserEmail($email) */ public function listUserAccount() { - $this->returnCode = 'no'; - $answer = $this->send('v-list-user', $this->userName, 'json'); + $this->setReturnCode('no'); + $answer = $this->send('v-list-user', $this->getUserName(), 'json'); $data = json_decode($answer, true); return $data; @@ -60,8 +60,8 @@ public function listUserAccount() */ public function listUserLog() { - $this->returnCode = 'no'; - $answer = $this->send('v-list-user-log', $this->userName, 'json'); + $this->setReturnCode('no'); + $answer = $this->send('v-list-user-log', $this->getUserName(), 'json'); $data = json_decode($answer, true); return $data; @@ -74,8 +74,8 @@ public function listUserLog() */ public function listUserBackups() { - $this->returnCode = 'no'; - $answer = $this->send('v-list-user-backups', $this->userName, 'json'); + $this->setReturnCode('no'); + $answer = $this->send('v-list-user-backups', $this->getUserName(), 'json'); $data = json_decode($answer, true); return $data; @@ -90,7 +90,7 @@ public function listUserBackups() */ public function deleteUserBackup($backup) { - return $this->send('v-delete-user-backup', $this->userName, $backup); + return $this->send('v-delete-user-backup', $this->getUserName(), $backup); } /** @@ -102,8 +102,8 @@ public function deleteUserBackup($backup) */ public function showUserBackup($backup) { - $this->returnCode = 'no'; - $answer = $this->send('v-list-user-backup', $this->userName, $backup, 'json'); + $this->setReturnCode('no'); + $answer = $this->send('v-list-user-backup', $this->getUserName(), $backup, 'json'); $data = json_decode($answer, true); return $data; @@ -125,7 +125,7 @@ public function restoreBackup($arg) $udir = 'no'; extract($arg, EXTR_OVERWRITE); - return $this->send('v-schedule-user-restore', $this->userName, $backup, $web, $dns, $mail, $db, + return $this->send('v-schedule-user-restore', $this->getUserName(), $backup, $web, $dns, $mail, $db, $cron, $udir); } @@ -134,7 +134,7 @@ public function restoreBackup($arg) */ public function suspendUser() { - return $this->send('v-suspend-user', $this->userName, 'no'); + return $this->send('v-suspend-user', $this->getUserName(), 'no'); } /** @@ -144,7 +144,7 @@ public function suspendUser() */ public function changePackage($package) { - return $this->send('v-suspend-user', $this->userName, $package); + return $this->send('v-suspend-user', $this->getUserName(), $package); } /** @@ -152,7 +152,7 @@ public function changePackage($package) */ public function listUserPackages() { - $this->returnCode = 'no'; + $this->setReturnCode('no'); $answer = $this->send('v-list-user-packages', 'json'); $data = json_decode($answer, true); @@ -166,9 +166,9 @@ public function listUserPackages() */ public function getValue($option) { - $this->returnCode = 'no'; + $this->setReturnCode('no'); - return $this->send('v-get-user-value', $this->userName, $option); + return $this->send('v-get-user-value', $this->getUserName(), $option); } /** @@ -178,7 +178,7 @@ public function getValue($option) */ public function changeShell($ssh = 'nologin') { - return $this->send('v-change-user-shell', $this->userName, $ssh); + return $this->send('v-change-user-shell', $this->getUserName(), $ssh); } /** @@ -186,8 +186,8 @@ public function changeShell($ssh = 'nologin') */ public function adminListUserAccount() { - $this->returnCode = 'no'; - $answer = $this->send('v-list-user', $this->userName, 'json'); + $this->setReturnCode('no'); + $answer = $this->send('v-list-user', $this->getUserName(), 'json'); $data = json_decode($answer, true); return $data; @@ -198,7 +198,7 @@ public function adminListUserAccount() */ public function adminListUserPackages() { - $this->returnCode = 'no'; + $this->setReturnCode('no'); $answer = $this->send('v-list-user-packages', 'json'); $data = json_decode($answer, true); @@ -210,7 +210,7 @@ public function adminListUserPackages() */ public function adminListUserShell() { - $this->returnCode = 'no'; + $this->setReturnCode('no'); $answer = $this->send('v-list-sys-shells', 'json'); $data = json_decode($answer, true); diff --git a/src/Services/VestaAPI.php b/src/Services/VestaAPI.php index d573efd..0748427 100644 --- a/src/Services/VestaAPI.php +++ b/src/Services/VestaAPI.php @@ -10,16 +10,16 @@ class VestaAPI use BD, DNS, User, Web, Service, Cron, FileSystem; /** - * @var + * @var string */ - public $userName = ''; + private $userName = ''; /** * return no|yes|json. * * @var string */ - public $returnCode = 'yes'; + private $returnCode = 'yes'; /** * @var string @@ -80,13 +80,40 @@ 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 string $cmd * @@ -98,7 +125,7 @@ public function send($cmd) { $postVars = [ 'user' => $this->userName, - 'password' => $this->key, + 'hash' => $this->key, // api key 'returncode' => $this->returnCode, 'cmd' => $cmd, ]; @@ -110,18 +137,24 @@ 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(); - - if ($this->returnCode == 'yes' && $query != 0) { +// $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/index.php') + ->setPostString($postVars) + ->setTimeout(10) + ->getRaw(); + + if ($this->getReturnCode() == 'yes' && $query != 0) { throw new VestaExceptions($query); } diff --git a/src/Services/Web.php b/src/Services/Web.php index df0fdd2..3109880 100644 --- a/src/Services/Web.php +++ b/src/Services/Web.php @@ -13,8 +13,8 @@ trait Web */ public function listEditWebDomain($domain) { - $this->returnCode = 'no'; - $answer = $this->send('v-list-web-domain', $this->userName, $domain, 'json'); + $this->setReturnCode('no'); + $answer = $this->send('v-list-web-domain', $this->getUserName(), $domain, 'json'); $data = json_decode($answer, true); @@ -40,8 +40,8 @@ public function listEditWebDomain($domain) */ public function listWebDomain() { - $this->returnCode = 'no'; - $answer = $this->send('v-list-web-domains', $this->userName, 'json'); + $this->setReturnCode('no'); + $answer = $this->send('v-list-web-domains', $this->getUserName(), 'json'); $data = json_decode($answer, true); return $data; @@ -57,7 +57,7 @@ public function listWebDomain() */ public function addWebDomain($domain, $ip) { - return $this->send('v-add-web-domain', $this->userName, $domain, $ip); + return $this->send('v-add-web-domain', $this->getUserName(), $domain, $ip); } /** @@ -70,7 +70,7 @@ public function addWebDomain($domain, $ip) */ public function addDns($domain, $ip) { - return $this->send('v-add-dns-domain', $this->userName, $domain, $ip); + return $this->send('v-add-dns-domain', $this->getUserName(), $domain, $ip); } /** @@ -82,7 +82,7 @@ public function addDns($domain, $ip) */ public function addMail($domain) { - return $this->send('v-add-mail-domain', $this->userName, $domain); + return $this->send('v-add-mail-domain', $this->getUserName(), $domain); } /** @@ -93,7 +93,7 @@ public function addMail($domain) */ public function addWebDomainAlias($domain, $alias) { - $this->send('v-add-web-domain-alias', $this->userName, $domain, $alias, 'no'); + $this->send('v-add-web-domain-alias', $this->getUserName(), $domain, $alias, 'no'); } /** @@ -106,7 +106,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->send('v-add-dns-on-web-alias', $this->getUserName(), $domain, $alias, 'no'); } /** @@ -119,7 +119,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->send('v-delete-web-domain-alias', $this->getUserName(), $domain, $alias, 'no'); } /** @@ -134,7 +134,7 @@ 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, + return $this->send('v-add-web-domain-ftp', $this->getUserName(), $domain, $ftpUserName, $ftpPassword, $ftpPath); } @@ -148,7 +148,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->send('v-add-web-domain-proxy', $this->getUserName(), $domain, '', $ext, 'no'); } /** @@ -160,7 +160,7 @@ public function addDomainProxy($domain, $ext) */ public function deleteDomain($domain) { - return $this->send('v-delete-domain', $this->userName, $domain); + return $this->send('v-delete-domain', $this->getUserName(), $domain); } /** @@ -173,7 +173,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->send('v-change-web-domain-ip', $this->getUserName(), $domain, $ip, 'no'); } /** @@ -186,7 +186,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->send('v-delete-web-domain-ftp', $this->getUserName(), $domain, $ftpUserName); } /** @@ -200,7 +200,7 @@ public function deleteWebDomain($domain, $ftpUserName) */ public function changeWebDomain($domain, $ftpUserName, $ftpPath) { - return $this->send('v-change-web-domain-ftp-path', $this->userName, $domain, $ftpUserName, + return $this->send('v-change-web-domain-ftp-path', $this->getUserName(), $domain, $ftpUserName, $ftpPath); } @@ -215,7 +215,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, + return $this->send('v-change-web-domain-ftp-password', $this->getUserName(), $domain, $ftpUserName, $password); } } From e7fb619ddeedec99c504e777dc0664046a8b82f6 Mon Sep 17 00:00:00 2001 From: Evgeniy Date: Mon, 18 Sep 2017 18:21:14 +0300 Subject: [PATCH 2/8] hard refactor --- README.md | 2 +- composer.json | 1 - src/Services/BD.php | 24 +++++-------- src/Services/Cron.php | 20 +++-------- src/Services/DNS.php | 41 +++++++++------------- src/Services/FileSystem.php | 51 ++++++++------------------- src/Services/Sender.php | 21 ++++++----- src/Services/Service.php | 50 ++++++++++---------------- src/Services/User.php | 70 ++++++++++--------------------------- src/Services/VestaAPI.php | 59 +++++++++++++++++++++---------- src/Services/Web.php | 43 ++++++++++------------- 11 files changed, 153 insertions(+), 229 deletions(-) diff --git a/README.md b/README.md index 993cb42..590143e 100644 --- a/README.md +++ b/README.md @@ -47,5 +47,5 @@ Simple usage ```php use VestaAPI\Facades\Vesta; -$backups = Vesta::server('testVDS')->user('MyUserName')->listUserBackups(); +$backups = Vesta::server('testVDS')->setUserName('MyUserName')->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/src/Services/BD.php b/src/Services/BD.php index e219e84..c023564 100644 --- a/src/Services/BD.php +++ b/src/Services/BD.php @@ -11,22 +11,20 @@ trait BD */ public function listBD() { - $this->setReturnCode('no'); - - return json_decode($this->send('v-list-databases', $this->getUserName(), '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->getUserName(), $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->getUserName(), $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->setReturnCode('no'); - $listBd = $this->send('v-list-database', $this->getUserName(), $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,8 +65,8 @@ public function listOnlyBD($database) */ public function addDateBase($database, $dbuser, $password, $type, $charset) { - return $this->send('v-add-database', $this->getUserName(), $database, $dbuser, $password, $type, - 'localhost', $charset); + return $this->toString($this->send('v-add-database', $this->getUserName(), $database, $dbuser, $password, $type, + 'localhost', $charset)); } /** @@ -84,6 +78,6 @@ public function addDateBase($database, $dbuser, $password, $type, $charset) */ public function deleteDateBase($database) { - return $this->send('v-delete-database', $this->getUserName(), $database); + return $this->toString($this->send('v-delete-database', $this->getUserName(), $database)); } } diff --git a/src/Services/Cron.php b/src/Services/Cron.php index 531d42a..abefe80 100644 --- a/src/Services/Cron.php +++ b/src/Services/Cron.php @@ -11,11 +11,7 @@ trait Cron */ public function listCron() { - $this->returnCode = 'no'; - $listDns = $this->send('v-list-cron-jobs', $this->getUserName(), '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')); } /** @@ -32,8 +28,7 @@ public function listCron() */ public function addCron($min, $hour, $day, $month, $wday, $cmd) { - return $this->send('v-add-cron-job', $this->getUserName(), $min, $hour, $day, $month, $wday, - $cmd); + return $this->toString($this->send('v-add-cron-job', $this->getUserName(), $min, $hour, $day, $month, $wday, $cmd)); } /** @@ -45,11 +40,7 @@ public function addCron($min, $hour, $day, $month, $wday, $cmd) */ public function showCron($job) { - $this->returnCode = 'no'; - $request = $this->send('v-list-cron-job', $this->getUserName(), $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')); } /** @@ -61,7 +52,7 @@ public function showCron($job) */ public function deleteCron($job) { - return $this->send('v-delete-cron-job', $this->getUserName(), $job); + return $this->toString($this->send('v-delete-cron-job', $this->getUserName(), $job)); } /** @@ -79,7 +70,6 @@ public function deleteCron($job) */ public function editCron($job, $min, $hour, $day, $month, $wday, $cmd) { - return $this->send('v-change-cron-job', $this->getUserName(), $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 index 8239a2e..04922c1 100644 --- a/src/Services/DNS.php +++ b/src/Services/DNS.php @@ -11,11 +11,7 @@ trait DNS */ public function listDNS() { - $this->setReturnCode('no'); - $listDns = $this->send('v-list-dns-domains', $this->userName, 'json'); - $data = json_decode($listDns, true); - - return $data; + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-domains', $this->userName, 'json')); } /** @@ -27,11 +23,7 @@ public function listDNS() */ public function listOnlyDNS($dns) { - $this->setReturnCode('no'); - $listDNS = $this->send('v-list-dns-domain', $this->userName, $dns, 'json'); - $data = json_decode($listDNS, true); - - return $data; + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-domain', $this->userName, $dns, 'json')); } /** @@ -43,7 +35,7 @@ public function listOnlyDNS($dns) */ public function deleteDNDDomain($domain) { - return $this->send('v-delete-dns-domain', $this->userName, $domain); + return $this->toString($this->send('v-delete-dns-domain', $this->userName, $domain)); } /** @@ -56,7 +48,7 @@ public function deleteDNDDomain($domain) */ public function deleteDNSRecord($domain, $recordId) { - return $this->send('v-delete-dns-record', $this->userName, $domain, $recordId); + return $this->toString($this->send('v-delete-dns-record', $this->userName, $domain, $recordId)); } /** @@ -73,8 +65,8 @@ public function deleteDNSRecord($domain, $recordId) */ 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'); + return $this->toString($this->send('v-add-dns-domain', $this->userName, $domain, $ip, $ns1, $ns2, $ns3, $ns4, + 'no')); } /** @@ -87,7 +79,7 @@ public function addDNSDomain($domain, $ip, $ns1, $ns2, $ns3 = null, $ns4 = null) */ public function changeDNSDomainExp($domain, $exp) { - return $this->send('v-change-dns-domain-exp', $this->userName, $domain, $exp, 'no'); + return $this->toString($this->send('v-change-dns-domain-exp', $this->userName, $domain, $exp, 'no')); } /** @@ -100,7 +92,7 @@ public function changeDNSDomainExp($domain, $exp) */ public function changeDNSDomainTtl($domain, $ttl) { - return $this->send('v-change-dns-domain-ttl', $this->userName, $domain, $ttl, 'no'); + return $this->toString($this->send('v-change-dns-domain-ttl', $this->userName, $domain, $ttl, 'no')); } /** @@ -112,11 +104,7 @@ public function changeDNSDomainTtl($domain, $ttl) */ 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; + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-records', $this->userName, $domain, 'json')); } /** @@ -131,8 +119,7 @@ public function listDNSRecords($domain) */ public function changeDNSDomainRecord($domain, $recordId, $val, $priority) { - return $this->send('v-change-dns-record', $this->userName, $domain, $recordId, $val, - $priority); + return $this->toString($this->send('v-change-dns-record', $this->userName, $domain, $recordId, $val, $priority)); } /** @@ -140,10 +127,12 @@ public function changeDNSDomainRecord($domain, $recordId, $val, $priority) * * @param $domain * @param $recordId + * + * @return string */ public function removeDNSRecord($domain, $recordId) { - $this->send('v-delete-dns-record', $this->userName, $domain, $recordId); + return $this->toString($this->send('v-delete-dns-record', $this->userName, $domain, $recordId)); } /** @@ -154,9 +143,11 @@ public function removeDNSRecord($domain, $recordId) * @param $type * @param $val * @param $priority + * + * @return string */ public function addDNSRecord($domain, $rec, $type, $val, $priority) { - $this->send('v-add-dns-record', $this->userName, $domain, $rec, $type, $val, $priority); + return $this->toString($this->send('v-add-dns-record', $this->userName, $domain, $rec, $type, $val, $priority)); } } diff --git a/src/Services/FileSystem.php b/src/Services/FileSystem.php index 440f5c9..b3880e8 100644 --- a/src/Services/FileSystem.php +++ b/src/Services/FileSystem.php @@ -31,9 +31,7 @@ trait FileSystem */ public function moveFile($src, $dst) { - $this->setReturnCode('no'); - - return $this->send('v-move-fs-file', $this->getUserName(), $src, $dst); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-move-fs-file', $this->getUserName(), $src, $dst)); } /** @@ -44,33 +42,27 @@ public function moveFile($src, $dst) public function openFile($path = '') { $path = '/home/'.$this->getUserName().'/'.$path; - $this->setReturnCode('no'); - - return $this->send('v-open-fs-file', $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->getUserName(), $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->getUserName(), $patch); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-add-fs-file', $this->getUserName(), $path)); } /** @@ -82,9 +74,7 @@ public function addFile($patch) public function changePermission($srcFile, $permissions) { $srcFile = '/home/'.$this->getUserName().'/'.$srcFile; - $this->returnCode = 'no'; - - return $this->send('v-change-fs-file-permission', $this->getUserName(), $srcFile, $permissions); + 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->setReturnCode('no'); - - return $this->send('v-copy-fs-directory', $this->getUserName(), $srcDir, $dstDir); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-copy-fs-directory', $this->getUserName(), $srcDir, $dstDir)); } /** @@ -108,9 +96,7 @@ public function copyDir($srcDir, $dstDir) */ public function copyFile($srcDir, $dstDir) { - $this->setReturnCode('no'); - - return $this->send('v-copy-fs-file', $this->getUserName(), $srcDir, $dstDir); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-copy-fs-file', $this->getUserName(), $srcDir, $dstDir)); } /** @@ -120,10 +106,8 @@ public function copyFile($srcDir, $dstDir) */ public function deleteDir($dstDir) { - $this->returnCode = 'no'; $dstDir = '/home/'.$this->getUserName().'/'.$dstDir; - - return $this->send('v-delete-fs-dir', $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->setReturnCode('no'); $dstFile = '/home/'.$this->getUserName().'/'.$dstFile; - - return $this->send('v-delete-fs-file', $this->getUserName(), $dstFile); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-delete-fs-file', $this->getUserName(), $dstFile)); } /** @@ -147,9 +129,7 @@ public function deleteFile($dstFile) */ public function extractArchive($srcFile, $dstDir) { - $this->setReturnCode('no'); - - return $this->send('v-extract-fs-archive', $this->getUserName(), $srcFile, $dstDir); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-extract-fs-archive', $this->getUserName(), $srcFile, $dstDir)); } /** @@ -160,8 +140,7 @@ public function extractArchive($srcFile, $dstDir) public function listDirectory($path = '') { $path = '/home/'.$this->getUserName().'/'.$path; - $this->setReturnCode('no'); - $responseVesta = $this->send('v-list-fs-directory', $this->getUserName(), $path); + $responseVesta = $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-fs-directory', $this->getUserName(), $path)); return $this->parseListing($responseVesta); } diff --git a/src/Services/Sender.php b/src/Services/Sender.php index 022072b..64bbb59 100644 --- a/src/Services/Sender.php +++ b/src/Services/Sender.php @@ -94,11 +94,11 @@ public function getRaw() } /** - * @return mixed + * @return array */ - public function getDecoded() + public function getArray() { - return $this->getArray($this->get()); + return $this->toArray($this->get()); } /** @@ -108,13 +108,13 @@ private function get() { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->uri); - curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); + curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $this->postString); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout); - if ($this->sslVerify) { - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); + if (!$this->sslVerify) { + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } $result = curl_exec($ch); curl_close($ch); @@ -126,8 +126,11 @@ private function get() * * @return mixed */ - private function getArray($json) + private function toArray($json) { + if (empty($json)) { + return []; + } return json_decode($json, true); } diff --git a/src/Services/Service.php b/src/Services/Service.php index 65a770a..add5a8a 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->setReturnCode('no'); - - return json_decode($this->send('v-search-user-object', $this->getUserName(), $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,9 +102,7 @@ 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')); } /** @@ -126,7 +112,7 @@ public function getIp($ip) */ public function rebuildWebDomains() { - return $this->send('v-rebuild-web-domains', $this->getUserName()); + return $this->toString($this->send('v-rebuild-web-domains', $this->getUserName())); } /** @@ -136,7 +122,7 @@ public function rebuildWebDomains() */ public function rebuildDNSDomains() { - return $this->send('v-rebuild-dns-domains', $this->getUserName()); + return $this->toString($this->send('v-rebuild-dns-domains', $this->getUserName())); } /** @@ -144,7 +130,7 @@ public function rebuildDNSDomains() */ public function rebuildMailDomains() { - return $this->send('v-rebuild-mail-domains', $this->getUserName()); + return $this->toString($this->send('v-rebuild-mail-domains', $this->getUserName())); } /** @@ -152,7 +138,7 @@ public function rebuildMailDomains() */ public function rebuildDataBases() { - return $this->send('v-rebuild-databases', $this->getUserName()); + return $this->toString($this->send('v-rebuild-databases', $this->getUserName())); } /** @@ -162,7 +148,7 @@ public function rebuildDataBases() */ public function rebuildCronJobs() { - return $this->send('v-rebuild-cron-jobs', $this->getUserName()); + return $this->toString($this->send('v-rebuild-cron-jobs', $this->getUserName())); } /** @@ -170,7 +156,7 @@ public function rebuildCronJobs() */ public function updateUserCounters() { - return $this->send('v-update-user-counters', $this->getUserName()); + return $this->toString($this->send('v-update-user-counters', $this->getUserName())); } /** @@ -180,6 +166,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 90f6d3b..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->getUserName(), $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->getUserName(), $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->setReturnCode('no'); - $answer = $this->send('v-list-user', $this->getUserName(), '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->setReturnCode('no'); - $answer = $this->send('v-list-user-log', $this->getUserName(), '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->setReturnCode('no'); - $answer = $this->send('v-list-user-backups', $this->getUserName(), '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->getUserName(), $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->setReturnCode('no'); - $answer = $this->send('v-list-user-backup', $this->getUserName(), $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->getUserName(), $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->getUserName(), '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->getUserName(), $package); + return $this->toString($this->send('v-suspend-user', $this->getUserName(), $package)); } /** @@ -152,11 +136,7 @@ public function changePackage($package) */ public function listUserPackages() { - $this->setReturnCode('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->setReturnCode('no'); - - return $this->send('v-get-user-value', $this->getUserName(), $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->getUserName(), $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->setReturnCode('no'); - $answer = $this->send('v-list-user', $this->getUserName(), '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->setReturnCode('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->setReturnCode('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 0748427..76e32c8 100644 --- a/src/Services/VestaAPI.php +++ b/src/Services/VestaAPI.php @@ -2,13 +2,14 @@ namespace VestaAPI\Services; -use GuzzleHttp\Client; use VestaAPI\Exceptions\VestaExceptions; class VestaAPI { use BD, DNS, User, Web, Service, Cron, FileSystem; + const RETURN_CODE_YES = 'yes', + RETURN_CODE_NO = 'no'; /** * @var string */ @@ -31,6 +32,11 @@ class VestaAPI */ private $host = ''; + /** + * @var bool + */ + private $toArray = false; + /** * @param string $server * @@ -114,12 +120,22 @@ public function setReturnCode($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) { @@ -137,27 +153,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/index.php') + ->setUri('https://' . $this->host . ':8083/api/') ->setPostString($postVars) - ->setTimeout(10) - ->getRaw(); + ->setTimeout(10); 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 3109880..2395254 100644 --- a/src/Services/Web.php +++ b/src/Services/Web.php @@ -13,10 +13,7 @@ trait Web */ public function listEditWebDomain($domain) { - $this->setReturnCode('no'); - $answer = $this->send('v-list-web-domain', $this->getUserName(), $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->setReturnCode('no'); - $answer = $this->send('v-list-web-domains', $this->getUserName(), '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->getUserName(), $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->getUserName(), $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->getUserName(), $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->getUserName(), $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->getUserName(), $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->getUserName(), $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->getUserName(), $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->getUserName(), $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->getUserName(), $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->getUserName(), $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->getUserName(), $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->getUserName(), $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->getUserName(), $domain, $ftpUserName, - $password); + return $this->toString($this->send('v-change-web-domain-ftp-password', $this->getUserName(), $domain, $ftpUserName, + $password)); } } From 839c136c071ecaaff4a10b0f30b717c1486d4d09 Mon Sep 17 00:00:00 2001 From: Evgeniy Date: Mon, 18 Sep 2017 18:40:32 +0300 Subject: [PATCH 3/8] DNS a little fixes --- src/Services/DNS.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Services/DNS.php b/src/Services/DNS.php index 04922c1..ed59c68 100644 --- a/src/Services/DNS.php +++ b/src/Services/DNS.php @@ -11,7 +11,7 @@ trait DNS */ public function listDNS() { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-domains', $this->userName, 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-domains', $this->getUserName(), 'json')); } /** @@ -23,7 +23,7 @@ public function listDNS() */ public function listOnlyDNS($dns) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-domain', $this->userName, $dns, 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-domain', $this->getUserName(), $dns, 'json')); } /** @@ -35,7 +35,7 @@ public function listOnlyDNS($dns) */ public function deleteDNDDomain($domain) { - return $this->toString($this->send('v-delete-dns-domain', $this->userName, $domain)); + return $this->toString($this->send('v-delete-dns-domain', $this->getUserName(), $domain)); } /** @@ -48,7 +48,7 @@ public function deleteDNDDomain($domain) */ public function deleteDNSRecord($domain, $recordId) { - return $this->toString($this->send('v-delete-dns-record', $this->userName, $domain, $recordId)); + return $this->toString($this->send('v-delete-dns-record', $this->getUserName(), $domain, $recordId)); } /** @@ -65,7 +65,7 @@ public function deleteDNSRecord($domain, $recordId) */ public function addDNSDomain($domain, $ip, $ns1, $ns2, $ns3 = null, $ns4 = null) { - return $this->toString($this->send('v-add-dns-domain', $this->userName, $domain, $ip, $ns1, $ns2, $ns3, $ns4, + return $this->toString($this->send('v-add-dns-domain', $this->getUserName(), $domain, $ip, $ns1, $ns2, $ns3, $ns4, 'no')); } @@ -79,7 +79,7 @@ public function addDNSDomain($domain, $ip, $ns1, $ns2, $ns3 = null, $ns4 = null) */ public function changeDNSDomainExp($domain, $exp) { - return $this->toString($this->send('v-change-dns-domain-exp', $this->userName, $domain, $exp, 'no')); + return $this->toString($this->send('v-change-dns-domain-exp', $this->getUserName(), $domain, $exp, 'no')); } /** @@ -92,7 +92,7 @@ public function changeDNSDomainExp($domain, $exp) */ public function changeDNSDomainTtl($domain, $ttl) { - return $this->toString($this->send('v-change-dns-domain-ttl', $this->userName, $domain, $ttl, 'no')); + return $this->toString($this->send('v-change-dns-domain-ttl', $this->getUserName(), $domain, $ttl, 'no')); } /** @@ -104,7 +104,7 @@ public function changeDNSDomainTtl($domain, $ttl) */ public function listDNSRecords($domain) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-records', $this->userName, $domain, 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-records', $this->getUserName(), $domain, 'json')); } /** @@ -119,7 +119,7 @@ public function listDNSRecords($domain) */ public function changeDNSDomainRecord($domain, $recordId, $val, $priority) { - return $this->toString($this->send('v-change-dns-record', $this->userName, $domain, $recordId, $val, $priority)); + return $this->toString($this->send('v-change-dns-record', $this->getUserName(), $domain, $recordId, $val, $priority)); } /** @@ -132,7 +132,7 @@ public function changeDNSDomainRecord($domain, $recordId, $val, $priority) */ public function removeDNSRecord($domain, $recordId) { - return $this->toString($this->send('v-delete-dns-record', $this->userName, $domain, $recordId)); + return $this->toString($this->send('v-delete-dns-record', $this->getUserName(), $domain, $recordId)); } /** @@ -148,6 +148,6 @@ public function removeDNSRecord($domain, $recordId) */ public function addDNSRecord($domain, $rec, $type, $val, $priority) { - return $this->toString($this->send('v-add-dns-record', $this->userName, $domain, $rec, $type, $val, $priority)); + return $this->toString($this->send('v-add-dns-record', $this->getUserName(), $domain, $rec, $type, $val, $priority)); } } From c0660ac1612e0b1a9c0490925aeceac82df4b4e7 Mon Sep 17 00:00:00 2001 From: Evgeniy Date: Mon, 18 Sep 2017 19:00:12 +0300 Subject: [PATCH 4/8] DNS a little fixes --- README.md | 2 +- src/Services/BD.php | 32 +++++++----- src/Services/Cron.php | 56 +++++++++++---------- src/Services/DNS.php | 99 +++++++++++++++++++++---------------- src/Services/FileSystem.php | 67 ++++++++++++++----------- src/Services/Service.php | 39 +++++++++------ src/Services/User.php | 64 +++++++++++++++--------- 7 files changed, 210 insertions(+), 149 deletions(-) diff --git a/README.md b/README.md index 590143e..c690854 100644 --- a/README.md +++ b/README.md @@ -47,5 +47,5 @@ Simple usage ```php use VestaAPI\Facades\Vesta; -$backups = Vesta::server('testVDS')->setUserName('MyUserName')->listUserBackups(); +$backups = Vesta::server('testVDS')->setUserName('MyUserName')->listUserBackups($userThatUWantView); diff --git a/src/Services/BD.php b/src/Services/BD.php index c023564..5978737 100644 --- a/src/Services/BD.php +++ b/src/Services/BD.php @@ -7,37 +7,41 @@ trait BD /** * List data base. * + * @param string $user + * * @return mixed */ - public function listBD() + public function listBD($user) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-databases', $this->getUserName(), 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-databases', $user, 'json')); } /** * Change user database. * + * @param string $user * @param $database * @param $dbUser * * @return mixed */ - public function changeDbUser($database, $dbUser) + public function changeDbUser($user, $database, $dbUser) { - return $this->toString($this->send('v-change-database-user', $this->getUserName(), $database, $dbUser)); + return $this->toString($this->send('v-change-database-user', $user, $database, $dbUser)); } /** * Change data base password. * + * @param string $user * @param $database * @param $password * * @return mixed */ - public function changeDbPassword($database, $password) + public function changeDbPassword($user, $database, $password) { - return $this->toString($this->send('v-change-database-password', $this->getUserName(), $database, $password)); + return $this->toString($this->send('v-change-database-password', $user, $database, $password)); } /** @@ -47,14 +51,15 @@ public function changeDbPassword($database, $password) * * @return mixed */ - public function listOnlyBD($database) + public function listOnlyBD($user, $database) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-database', $this->getUserName(), $database, 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-database', $user, $database, 'json')); } /** * Add date base. * + * @param $user * @param $database * @param $dbuser * @param $password @@ -63,21 +68,22 @@ public function listOnlyBD($database) * * @return mixed */ - public function addDateBase($database, $dbuser, $password, $type, $charset) + public function addDateBase($user, $database, $dbuser, $password, $type, $charset) { - return $this->toString($this->send('v-add-database', $this->getUserName(), $database, $dbuser, $password, $type, + return $this->toString($this->send('v-add-database', $user, $database, $dbuser, $password, $type, 'localhost', $charset)); } /** * Delete data base. * - * @param $database + * @param string $user + * @param $database * * @return mixed */ - public function deleteDateBase($database) + public function deleteDateBase($user, $database) { - return $this->toString($this->send('v-delete-database', $this->getUserName(), $database)); + return $this->toString($this->send('v-delete-database', $user, $database)); } } diff --git a/src/Services/Cron.php b/src/Services/Cron.php index abefe80..429a83e 100644 --- a/src/Services/Cron.php +++ b/src/Services/Cron.php @@ -7,69 +7,75 @@ trait Cron /** * Cron list. * + * @param string $user + * * @return mixed */ - public function listCron() + public function listCron($user) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-cron-jobs', $this->getUserName(), 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-cron-jobs', $user, 'json')); } /** * Add Cron. * - * @param $min - * @param $hour - * @param $day - * @param $month - * @param $wday - * @param $cmd + * @param string $user + * @param $min + * @param $hour + * @param $day + * @param $month + * @param $wday + * @param $cmd * * @return mixed */ - public function addCron($min, $hour, $day, $month, $wday, $cmd) + public function addCron($user, $min, $hour, $day, $month, $wday, $cmd) { - return $this->toString($this->send('v-add-cron-job', $this->getUserName(), $min, $hour, $day, $month, $wday, $cmd)); + return $this->toString($this->send('v-add-cron-job', $user, $min, $hour, $day, $month, $wday, $cmd)); } /** * Show Cron. * - * @param $job + * @param string $user + * @param $job * * @return mixed */ - public function showCron($job) + public function showCron($user, $job) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-cron-job', $this->getUserName(), $job, 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-cron-job', $user, $job, 'json')); } /** * Delete cron. * - * @param $job + * @param string $user + * @param $job * * @return mixed */ - public function deleteCron($job) + public function deleteCron($user, $job) { - return $this->toString($this->send('v-delete-cron-job', $this->getUserName(), $job)); + return $this->toString($this->send('v-delete-cron-job', $user, $job)); } /** * Edit cron. * - * @param $job - * @param $min - * @param $hour - * @param $day - * @param $month - * @param $wday - * @param $cmd + * @param string $user + * @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) + public function editCron($user, $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)); + return $this->toString($this->send('v-change-cron-job', $user, $job, $min, $hour, $day, $month, $wday, $cmd)); } } diff --git a/src/Services/DNS.php b/src/Services/DNS.php index ed59c68..01428bf 100644 --- a/src/Services/DNS.php +++ b/src/Services/DNS.php @@ -7,132 +7,144 @@ trait DNS /** * List DNS. * + * @param string $user + * * @return mixed */ - public function listDNS() + public function listDNS($user) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-domains', $this->getUserName(), 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-domains', $user, 'json')); } /** * List Only DNS. * - * @param $dns + * @param string $user + * @param $dns * * @return mixed */ - public function listOnlyDNS($dns) + public function listOnlyDNS($user, $dns) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-domain', $this->getUserName(), $dns, 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-domain', $user, $dns, + 'json')); } /** * Delete DNS record domain. * - * @param $domain + * @param string $user + * @param $domain * * @return mixed */ - public function deleteDNDDomain($domain) + public function deleteDNDDomain($user, $domain) { - return $this->toString($this->send('v-delete-dns-domain', $this->getUserName(), $domain)); + return $this->toString($this->send('v-delete-dns-domain', $user, $domain)); } /** * Delete DNS record. * - * @param $domain - * @param $recordId + * @param string $user + * @param $domain + * @param $recordId * * @return mixed */ - public function deleteDNSRecord($domain, $recordId) + public function deleteDNSRecord($user, $domain, $recordId) { - return $this->toString($this->send('v-delete-dns-record', $this->getUserName(), $domain, $recordId)); + return $this->toString($this->send('v-delete-dns-record', $user, $domain, $recordId)); } /** * Add DNS domain. * - * @param $domain - * @param $ip - * @param $ns1 - * @param $ns2 - * @param null $ns3 - * @param null $ns4 + * @param string $user + * @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) + public function addDNSDomain($user, $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')); + return $this->toString($this->send('v-add-dns-domain', $user, $domain, $ip, $ns1, $ns2, $ns3, $ns4, 'no')); } /** * Set expiriation date. * - * @param $domain - * @param $exp + * @param string $user + * @param $domain + * @param $exp * * @return mixed */ - public function changeDNSDomainExp($domain, $exp) + public function changeDNSDomainExp($user, $domain, $exp) { - return $this->toString($this->send('v-change-dns-domain-exp', $this->getUserName(), $domain, $exp, 'no')); + return $this->toString($this->send('v-change-dns-domain-exp', $user, $domain, $exp, 'no')); } /** * Set TTL. * - * @param $domain - * @param $ttl + * @param string $user + * @param $domain + * @param $ttl * * @return mixed */ - public function changeDNSDomainTtl($domain, $ttl) + public function changeDNSDomainTtl($user, $domain, $ttl) { - return $this->toString($this->send('v-change-dns-domain-ttl', $this->getUserName(), $domain, $ttl, 'no')); + return $this->toString($this->send('v-change-dns-domain-ttl', $user, $domain, $ttl, 'no')); } /** * List DNS record domain. * - * @param $domain + * @param string $user + * @param $domain * * @return mixed */ - public function listDNSRecords($domain) + public function listDNSRecords($user, $domain) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-records', $this->getUserName(), $domain, 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-records', $user, $domain, + 'json')); } /** * Change DNS domain record. * - * @param $domain - * @param $recordId - * @param $val - * @param $priority + * @param string $user + * @param $domain + * @param $recordId + * @param $val + * @param $priority * * @return mixed */ - public function changeDNSDomainRecord($domain, $recordId, $val, $priority) + public function changeDNSDomainRecord($user, $domain, $recordId, $val, $priority) { - return $this->toString($this->send('v-change-dns-record', $this->getUserName(), $domain, $recordId, $val, $priority)); + return $this->toString($this->send('v-change-dns-record', $user, $domain, $recordId, $val, $priority)); } /** * Remove DNS record. * - * @param $domain - * @param $recordId + * @param string $user + * @param $domain + * @param $recordId * * @return string */ - public function removeDNSRecord($domain, $recordId) + public function removeDNSRecord($user, $domain, $recordId) { - return $this->toString($this->send('v-delete-dns-record', $this->getUserName(), $domain, $recordId)); + return $this->toString($this->send('v-delete-dns-record', $user, $domain, $recordId)); } /** @@ -148,6 +160,7 @@ public function removeDNSRecord($domain, $recordId) */ public function addDNSRecord($domain, $rec, $type, $val, $priority) { - return $this->toString($this->send('v-add-dns-record', $this->getUserName(), $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 b3880e8..7950a2a 100644 --- a/src/Services/FileSystem.php +++ b/src/Services/FileSystem.php @@ -24,123 +24,134 @@ trait FileSystem ]; /** - * @param $src - * @param $dst + * @param string $user + * @param $src + * @param $dst * * @return mixed */ - public function moveFile($src, $dst) + public function moveFile($user, $src, $dst) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-move-fs-file', $this->getUserName(), $src, $dst)); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-move-fs-file', $user, $src, $dst)); } /** + * @param string $user * @param string $path * * @return mixed */ - public function openFile($path = '') + public function openFile($user, $path = '') { $path = '/home/'.$this->getUserName().'/'.$path; - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-open-fs-file', $this->getUserName(), $path)); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-open-fs-file', $user, $path)); } /** + * @param $user * @param $path * * @return mixed */ - public function addDir($path) + public function addDir($user, $path) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-add-fs-directory', $this->getUserName(), $path)); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-add-fs-directory', $user, $path)); } /** + * @param $user * @param $path * * @return mixed */ - public function addFile($path) + public function addFile($user, $path) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-add-fs-file', $this->getUserName(), $path)); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-add-fs-file', $user, $path)); } /** - * @param $srcFile - * @param $permissions + * @param string $user + * @param $srcFile + * @param $permissions * * @return mixed */ - public function changePermission($srcFile, $permissions) + public function changePermission($user, $srcFile, $permissions) { $srcFile = '/home/'.$this->getUserName().'/'.$srcFile; - return $this->setReturnCode('no')->toArray($this->send('v-change-fs-file-permission', $this->getUserName(), $srcFile, $permissions)); + return $this->setReturnCode('no')->toArray($this->send('v-change-fs-file-permission', $user, $srcFile, $permissions)); } /** + * @param $user * @param $srcDir * @param $dstDir * * @return mixed */ - public function copyDir($srcDir, $dstDir) + public function copyDir($user, $srcDir, $dstDir) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-copy-fs-directory', $this->getUserName(), $srcDir, $dstDir)); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-copy-fs-directory', $user, $srcDir, $dstDir)); } /** + * @param $user * @param $srcDir * @param $dstDir * * @return mixed */ - public function copyFile($srcDir, $dstDir) + public function copyFile($user, $srcDir, $dstDir) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-copy-fs-file', $this->getUserName(), $srcDir, $dstDir)); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-copy-fs-file', $user, $srcDir, $dstDir)); } /** - * @param $dstDir + * @param string $user + * @param $dstDir * * @return mixed */ - public function deleteDir($dstDir) + public function deleteDir($user, $dstDir) { $dstDir = '/home/'.$this->getUserName().'/'.$dstDir; - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-delete-fs-dir', $this->getUserName(), $dstDir)); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-delete-fs-dir', $user, $dstDir)); } /** + * @param $user * @param $dstFile * * @return mixed */ - public function deleteFile($dstFile) + public function deleteFile($user, $dstFile) { $dstFile = '/home/'.$this->getUserName().'/'.$dstFile; - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-delete-fs-file', $this->getUserName(), $dstFile)); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-delete-fs-file', $user, $dstFile)); } /** + * @param $user * @param $srcFile * @param $dstDir * * @return mixed */ - public function extractArchive($srcFile, $dstDir) + public function extractArchive($user, $srcFile, $dstDir) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-extract-fs-archive', $this->getUserName(), $srcFile, $dstDir)); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-extract-fs-archive', $user, $srcFile, $dstDir)); } /** - * @param $path + * @param $user + * @param string $path * * @return mixed */ - public function listDirectory($path = '') + public function listDirectory($user, $path = '') { $path = '/home/'.$this->getUserName().'/'.$path; - $responseVesta = $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-fs-directory', $this->getUserName(), $path)); + $responseVesta = $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-fs-directory', $user, $path)); return $this->parseListing($responseVesta); } diff --git a/src/Services/Service.php b/src/Services/Service.php index add5a8a..6485980 100644 --- a/src/Services/Service.php +++ b/src/Services/Service.php @@ -106,57 +106,66 @@ public function getIp($ip) } /** - * @return mixed + * @param $user * + * @return mixed * @internal param string $restart */ - public function rebuildWebDomains() + public function rebuildWebDomains($user) { - return $this->toString($this->send('v-rebuild-web-domains', $this->getUserName())); + return $this->toString($this->send('v-rebuild-web-domains', $user)); } /** - * @return mixed + * @param $user * + * @return mixed * @internal param string $restart */ - public function rebuildDNSDomains() + public function rebuildDNSDomains($user) { - return $this->toString($this->send('v-rebuild-dns-domains', $this->getUserName())); + return $this->toString($this->send('v-rebuild-dns-domains', $user)); } /** + * @param $user + * * @return mixed */ - public function rebuildMailDomains() + public function rebuildMailDomains($user) { - return $this->toString($this->send('v-rebuild-mail-domains', $this->getUserName())); + return $this->toString($this->send('v-rebuild-mail-domains', $user)); } /** + * @param $user + * * @return mixed */ - public function rebuildDataBases() + public function rebuildDataBases($user) { - return $this->toString($this->send('v-rebuild-databases', $this->getUserName())); + return $this->toString($this->send('v-rebuild-databases', $user)); } /** - * @return mixed + * @param $user * + * @return mixed * @internal param string $restart */ - public function rebuildCronJobs() + public function rebuildCronJobs($user) { - return $this->toString($this->send('v-rebuild-cron-jobs', $this->getUserName())); + return $this->toString($this->send('v-rebuild-cron-jobs', $user)); } /** + * @param $user + * * @return mixed */ - public function updateUserCounters() + public function updateUserCounters($user) { - return $this->toString($this->send('v-update-user-counters', $this->getUserName())); + return $this->toString($this->send('v-update-user-counters', $user)); } /** diff --git a/src/Services/User.php b/src/Services/User.php index 85a318d..38c8873 100644 --- a/src/Services/User.php +++ b/src/Services/User.php @@ -26,79 +26,89 @@ public function regUser($username, $password, $email, $package, $fistName, $last * * @return mixed */ - public function changeUserPassword($password) + public function changeUserPassword($user, $password) { - return $this->toString($this->send('v-change-user-password', $this->getUserName(), $password)); + return $this->toString($this->send('v-change-user-password', $user, $password)); } /** + * @param $user * @param $email * * @return mixed */ - public function changeUserEmail($email) + public function changeUserEmail($user, $email) { - return $this->toString($this->send('v-change-user-contact', $this->getUserName(), $email)); + return $this->toString($this->send('v-change-user-contact', $user, $email)); } /** * List User Account. * + * @param $user + * * @return mixed */ - public function listUserAccount() + public function listUserAccount($user) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user', $this->getUserName(), 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user', $user, 'json')); } /** + * @param $user + * * @return mixed */ - public function listUserLog() + public function listUserLog($user) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user-log', $this->getUserName(), 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user-log', $user, 'json')); } /** * List User Backups. * + * @param $user + * * @return mixed */ - public function listUserBackups() + public function listUserBackups($user) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user-backups', $this->getUserName(), 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user-backups', $user, 'json')); } /** * Delete backup user. * + * @param $user * @param $backup * * @return mixed */ - public function deleteUserBackup($backup) + public function deleteUserBackup($user, $backup) { - return $this->toString($this->send('v-delete-user-backup', $this->getUserName(), $backup)); + return $this->toString($this->send('v-delete-user-backup', $user, $backup)); } /** * Show Backup. * + * @param $user * @param $backup * * @return mixed */ - public function showUserBackup($backup) + public function showUserBackup($user, $backup) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user-backup', $this->getUserName(), $backup, 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user-backup', $user, $backup, 'json')); } /** + * @param $user * @param $arg * * @return mixed */ - public function restoreBackup($arg) + public function restoreBackup($user, $arg) { $backup = 'no'; $web = 'no'; @@ -109,16 +119,18 @@ public function restoreBackup($arg) $udir = 'no'; extract($arg, EXTR_OVERWRITE); - return $this->toString($this->send('v-schedule-user-restore', $this->getUserName(), $backup, $web, $dns, $mail, + return $this->toString($this->send('v-schedule-user-restore', $user, $backup, $web, $dns, $mail, $db, $cron, $udir)); } /** + * @param $user + * * @return mixed */ - public function suspendUser() + public function suspendUser($user) { - return $this->toString($this->send('v-suspend-user', $this->getUserName(), 'no')); + return $this->toString($this->send('v-suspend-user', $user, 'no')); } /** @@ -140,31 +152,35 @@ public function listUserPackages() } /** + * @param $user * @param $option * * @return mixed */ - public function getValue($option) + public function getValue($user, $option) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-get-user-value', $this->getUserName(), $option)); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-get-user-value', $user, $option)); } /** + * @param $user * @param string $ssh * * @return mixed */ - public function changeShell($ssh = 'nologin') + public function changeShell($user, $ssh = 'nologin') { - return $this->toString($this->send('v-change-user-shell', $this->getUserName(), $ssh)); + return $this->toString($this->send('v-change-user-shell', $user, $ssh)); } /** + * @param $user + * * @return mixed */ - public function adminListUserAccount() + public function adminListUserAccount($user) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user', $this->getUserName(), 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user', $user, 'json')); } /** From aec92bfa9a00c206264cec46dfa9db7795fe59bd Mon Sep 17 00:00:00 2001 From: StereoFlo Date: Mon, 18 Sep 2017 21:04:30 +0300 Subject: [PATCH 5/8] readme fix --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index c690854..1459558 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 a existing keys +```sh +ls -l /usr/local/vesta/data/keys/ +``` ## Usage From 91d066c7cf51fa48992003dfe6e186165e0e7be5 Mon Sep 17 00:00:00 2001 From: StereoFlo Date: Mon, 18 Sep 2017 21:04:53 +0300 Subject: [PATCH 6/8] readme fix --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1459558..95d71fa 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Generate api key ```bash bash /usr/local/vesta/bin/v-generate-api-key ``` -Or you can view a existing keys +Or you can view existing keys ```sh ls -l /usr/local/vesta/data/keys/ From c83dccb8ed8548e800916c5e91a4b50721133503 Mon Sep 17 00:00:00 2001 From: Evgeniy Date: Tue, 3 Oct 2017 11:34:18 +0300 Subject: [PATCH 7/8] Renamed classes with PSR-1.3 --- src/Services/{BD.php => Db.php} | 2 +- src/Services/{DNS.php => Dns.php} | 2 +- src/Services/VestaAPI.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename src/Services/{BD.php => Db.php} (99%) rename src/Services/{DNS.php => Dns.php} (99%) diff --git a/src/Services/BD.php b/src/Services/Db.php similarity index 99% rename from src/Services/BD.php rename to src/Services/Db.php index 5978737..8884bc2 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. diff --git a/src/Services/DNS.php b/src/Services/Dns.php similarity index 99% rename from src/Services/DNS.php rename to src/Services/Dns.php index 01428bf..6840425 100644 --- a/src/Services/DNS.php +++ b/src/Services/Dns.php @@ -2,7 +2,7 @@ namespace VestaAPI\Services; -trait DNS +trait Dns { /** * List DNS. diff --git a/src/Services/VestaAPI.php b/src/Services/VestaAPI.php index 76e32c8..b4f38aa 100644 --- a/src/Services/VestaAPI.php +++ b/src/Services/VestaAPI.php @@ -6,7 +6,7 @@ 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'; From 0408326768485317218fe6bf4327231041f653bb Mon Sep 17 00:00:00 2001 From: StereoFlo Date: Wed, 4 Oct 2017 21:09:28 +0300 Subject: [PATCH 8/8] Fix for user that you want to view, and config --- README.md | 2 +- config/vesta.php | 1 + src/Services/Cron.php | 26 ++++++--------- src/Services/Db.php | 30 +++++++---------- src/Services/Dns.php | 50 ++++++++++++----------------- src/Services/FileSystem.php | 55 +++++++++++++------------------ src/Services/Service.php | 32 +++++++------------ src/Services/User.php | 64 ++++++++++++++----------------------- src/Services/VestaAPI.php | 13 ++++++-- 9 files changed, 111 insertions(+), 162 deletions(-) diff --git a/README.md b/README.md index 95d71fa..7347ee1 100644 --- a/README.md +++ b/README.md @@ -51,5 +51,5 @@ Simple usage ```php use VestaAPI\Facades\Vesta; -$backups = Vesta::server('testVDS')->setUserName('MyUserName')->listUserBackups($userThatUWantView); +$backups = Vesta::server('testVDS')->setUserName($userThatUWantToView)->listUserBackups(); 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 429a83e..5dfb0fd 100644 --- a/src/Services/Cron.php +++ b/src/Services/Cron.php @@ -7,19 +7,16 @@ trait Cron /** * Cron list. * - * @param string $user - * * @return mixed */ - public function listCron($user) + public function listCron() { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-cron-jobs', $user, 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-cron-jobs', $this->getUserName(), 'json')); } /** * Add Cron. * - * @param string $user * @param $min * @param $hour * @param $day @@ -29,41 +26,38 @@ public function listCron($user) * * @return mixed */ - public function addCron($user, $min, $hour, $day, $month, $wday, $cmd) + public function addCron($min, $hour, $day, $month, $wday, $cmd) { - return $this->toString($this->send('v-add-cron-job', $user, $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 string $user * @param $job * * @return mixed */ - public function showCron($user, $job) + public function showCron($job) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-cron-job', $user, $job, 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-cron-job', $this->getUserName(), $job, 'json')); } /** * Delete cron. * - * @param string $user * @param $job * * @return mixed */ - public function deleteCron($user, $job) + public function deleteCron($job) { - return $this->toString($this->send('v-delete-cron-job', $user, $job)); + return $this->toString($this->send('v-delete-cron-job', $this->getUserName(), $job)); } /** * Edit cron. * - * @param string $user * @param $job * @param $min * @param $hour @@ -74,8 +68,8 @@ public function deleteCron($user, $job) * * @return mixed */ - public function editCron($user, $job, $min, $hour, $day, $month, $wday, $cmd) + public function editCron($job, $min, $hour, $day, $month, $wday, $cmd) { - return $this->toString($this->send('v-change-cron-job', $user, $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/Db.php b/src/Services/Db.php index 8884bc2..e10e565 100644 --- a/src/Services/Db.php +++ b/src/Services/Db.php @@ -7,41 +7,37 @@ trait Db /** * List data base. * - * @param string $user - * * @return mixed */ - public function listBD($user) + public function listBD() { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-databases', $user, 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-databases', $this->getUserName(), 'json')); } /** * Change user database. * - * @param string $user * @param $database * @param $dbUser * * @return mixed */ - public function changeDbUser($user, $database, $dbUser) + public function changeDbUser($database, $dbUser) { - return $this->toString($this->send('v-change-database-user', $user, $database, $dbUser)); + return $this->toString($this->send('v-change-database-user', $this->getUserName(), $database, $dbUser)); } /** * Change data base password. * - * @param string $user * @param $database * @param $password * * @return mixed */ - public function changeDbPassword($user, $database, $password) + public function changeDbPassword($database, $password) { - return $this->toString($this->send('v-change-database-password', $user, $database, $password)); + return $this->toString($this->send('v-change-database-password', $this->getUserName(), $database, $password)); } /** @@ -51,15 +47,14 @@ public function changeDbPassword($user, $database, $password) * * @return mixed */ - public function listOnlyBD($user, $database) + public function listOnlyBD($database) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-database', $user, $database, 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-database', $this->getUserName(), $database, 'json')); } /** * Add date base. * - * @param $user * @param $database * @param $dbuser * @param $password @@ -68,22 +63,21 @@ public function listOnlyBD($user, $database) * * @return mixed */ - public function addDateBase($user, $database, $dbuser, $password, $type, $charset) + public function addDateBase($database, $dbuser, $password, $type, $charset) { - return $this->toString($this->send('v-add-database', $user, $database, $dbuser, $password, $type, + return $this->toString($this->send('v-add-database', $this->getUserName(), $database, $dbuser, $password, $type, 'localhost', $charset)); } /** * Delete data base. * - * @param string $user * @param $database * * @return mixed */ - public function deleteDateBase($user, $database) + public function deleteDateBase($database) { - return $this->toString($this->send('v-delete-database', $user, $database)); + return $this->toString($this->send('v-delete-database', $this->getUserName(), $database)); } } diff --git a/src/Services/Dns.php b/src/Services/Dns.php index 6840425..5247282 100644 --- a/src/Services/Dns.php +++ b/src/Services/Dns.php @@ -11,56 +11,52 @@ trait Dns * * @return mixed */ - public function listDNS($user) + public function listDNS() { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-domains', $user, 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-domains', $this->getUserName(), 'json')); } /** * List Only DNS. * - * @param string $user * @param $dns * * @return mixed */ - public function listOnlyDNS($user, $dns) + public function listOnlyDNS($dns) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-domain', $user, $dns, + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-domain', $this->getUserName(), $dns, 'json')); } /** * Delete DNS record domain. * - * @param string $user * @param $domain * * @return mixed */ - public function deleteDNDDomain($user, $domain) + public function deleteDNDDomain($domain) { - return $this->toString($this->send('v-delete-dns-domain', $user, $domain)); + return $this->toString($this->send('v-delete-dns-domain', $this->getUserName(), $domain)); } /** * Delete DNS record. * - * @param string $user * @param $domain * @param $recordId * * @return mixed */ - public function deleteDNSRecord($user, $domain, $recordId) + public function deleteDNSRecord($domain, $recordId) { - return $this->toString($this->send('v-delete-dns-record', $user, $domain, $recordId)); + return $this->toString($this->send('v-delete-dns-record', $this->getUserName(), $domain, $recordId)); } /** * Add DNS domain. * - * @param string $user * @param $domain * @param $ip * @param $ns1 @@ -70,57 +66,52 @@ public function deleteDNSRecord($user, $domain, $recordId) * * @return mixed */ - public function addDNSDomain($user, $domain, $ip, $ns1, $ns2, $ns3 = null, $ns4 = null) + public function addDNSDomain($domain, $ip, $ns1, $ns2, $ns3 = null, $ns4 = null) { - return $this->toString($this->send('v-add-dns-domain', $user, $domain, $ip, $ns1, $ns2, $ns3, $ns4, 'no')); + return $this->toString($this->send('v-add-dns-domain', $this->getUserName(), $domain, $ip, $ns1, $ns2, $ns3, $ns4, 'no')); } /** * Set expiriation date. * - * @param string $user * @param $domain * @param $exp * * @return mixed */ - public function changeDNSDomainExp($user, $domain, $exp) + public function changeDNSDomainExp($domain, $exp) { - return $this->toString($this->send('v-change-dns-domain-exp', $user, $domain, $exp, 'no')); + return $this->toString($this->send('v-change-dns-domain-exp', $this->getUserName(), $domain, $exp, 'no')); } /** * Set TTL. * - * @param string $user * @param $domain * @param $ttl * * @return mixed */ - public function changeDNSDomainTtl($user, $domain, $ttl) + public function changeDNSDomainTtl($domain, $ttl) { - return $this->toString($this->send('v-change-dns-domain-ttl', $user, $domain, $ttl, 'no')); + return $this->toString($this->send('v-change-dns-domain-ttl', $this->getUserName(), $domain, $ttl, 'no')); } /** * List DNS record domain. * - * @param string $user * @param $domain * * @return mixed */ - public function listDNSRecords($user, $domain) + public function listDNSRecords($domain) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-records', $user, $domain, - 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-dns-records', $this->getUserName(), $domain, 'json')); } /** * Change DNS domain record. * - * @param string $user * @param $domain * @param $recordId * @param $val @@ -128,23 +119,22 @@ public function listDNSRecords($user, $domain) * * @return mixed */ - public function changeDNSDomainRecord($user, $domain, $recordId, $val, $priority) + public function changeDNSDomainRecord($domain, $recordId, $val, $priority) { - return $this->toString($this->send('v-change-dns-record', $user, $domain, $recordId, $val, $priority)); + return $this->toString($this->send('v-change-dns-record', $this->getUserName(), $domain, $recordId, $val, $priority)); } /** * Remove DNS record. * - * @param string $user * @param $domain * @param $recordId * * @return string */ - public function removeDNSRecord($user, $domain, $recordId) + public function removeDNSRecord($domain, $recordId) { - return $this->toString($this->send('v-delete-dns-record', $user, $domain, $recordId)); + return $this->toString($this->send('v-delete-dns-record', $this->getUserName(), $domain, $recordId)); } /** diff --git a/src/Services/FileSystem.php b/src/Services/FileSystem.php index 7950a2a..4c280bf 100644 --- a/src/Services/FileSystem.php +++ b/src/Services/FileSystem.php @@ -24,134 +24,123 @@ trait FileSystem ]; /** - * @param string $user * @param $src * @param $dst * * @return mixed */ - public function moveFile($user, $src, $dst) + public function moveFile($src, $dst) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-move-fs-file', $user, $src, $dst)); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-move-fs-file', $this->getUserName(), $src, $dst)); } /** - * @param string $user * @param string $path * * @return mixed */ - public function openFile($user, $path = '') + public function openFile($path = '') { $path = '/home/'.$this->getUserName().'/'.$path; - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-open-fs-file', $user, $path)); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-open-fs-file', $this->getUserName(), $path)); } /** - * @param $user * @param $path * * @return mixed */ - public function addDir($user, $path) + public function addDir($path) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-add-fs-directory', $user, $path)); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-add-fs-directory', $this->getUserName(), $path)); } /** - * @param $user * @param $path * * @return mixed */ - public function addFile($user, $path) + public function addFile($path) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-add-fs-file', $user, $path)); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-add-fs-file', $this->getUserName(), $path)); } /** - * @param string $user * @param $srcFile * @param $permissions * * @return mixed */ - public function changePermission($user, $srcFile, $permissions) + public function changePermission($srcFile, $permissions) { $srcFile = '/home/'.$this->getUserName().'/'.$srcFile; - return $this->setReturnCode('no')->toArray($this->send('v-change-fs-file-permission', $user, $srcFile, $permissions)); + return $this->setReturnCode('no')->toArray($this->send('v-change-fs-file-permission', $this->getUserName(), $srcFile, $permissions)); } /** - * @param $user * @param $srcDir * @param $dstDir * * @return mixed */ - public function copyDir($user, $srcDir, $dstDir) + public function copyDir($srcDir, $dstDir) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-copy-fs-directory', $user, $srcDir, $dstDir)); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-copy-fs-directory', $this->getUserName(), $srcDir, $dstDir)); } /** - * @param $user * @param $srcDir * @param $dstDir * * @return mixed */ - public function copyFile($user, $srcDir, $dstDir) + public function copyFile($srcDir, $dstDir) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-copy-fs-file', $user, $srcDir, $dstDir)); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-copy-fs-file', $this->getUserName(), $srcDir, $dstDir)); } /** - * @param string $user * @param $dstDir * * @return mixed */ - public function deleteDir($user, $dstDir) + public function deleteDir($dstDir) { $dstDir = '/home/'.$this->getUserName().'/'.$dstDir; - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-delete-fs-dir', $user, $dstDir)); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-delete-fs-dir', $this->getUserName(), $dstDir)); } /** - * @param $user * @param $dstFile * * @return mixed */ - public function deleteFile($user, $dstFile) + public function deleteFile($dstFile) { $dstFile = '/home/'.$this->getUserName().'/'.$dstFile; - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-delete-fs-file', $user, $dstFile)); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-delete-fs-file', $this->getUserName(), $dstFile)); } /** - * @param $user * @param $srcFile * @param $dstDir * * @return mixed */ - public function extractArchive($user, $srcFile, $dstDir) + public function extractArchive($srcFile, $dstDir) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-extract-fs-archive', $user, $srcFile, $dstDir)); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-extract-fs-archive', $this->getUserName(), $srcFile, $dstDir)); } /** - * @param $user * @param string $path * * @return mixed */ - public function listDirectory($user, $path = '') + public function listDirectory($path = '') { $path = '/home/'.$this->getUserName().'/'.$path; - $responseVesta = $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-fs-directory', $user, $path)); + $responseVesta = $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-fs-directory', $this->getUserName(), $path)); return $this->parseListing($responseVesta); } diff --git a/src/Services/Service.php b/src/Services/Service.php index 6485980..6a05a0d 100644 --- a/src/Services/Service.php +++ b/src/Services/Service.php @@ -106,56 +106,46 @@ public function getIp($ip) } /** - * @param $user - * * @return mixed * @internal param string $restart */ - public function rebuildWebDomains($user) + public function rebuildWebDomains() { - return $this->toString($this->send('v-rebuild-web-domains', $user)); + return $this->toString($this->send('v-rebuild-web-domains', $this->getUserName())); } /** - * @param $user - * * @return mixed * @internal param string $restart */ - public function rebuildDNSDomains($user) + public function rebuildDNSDomains() { - return $this->toString($this->send('v-rebuild-dns-domains', $user)); + return $this->toString($this->send('v-rebuild-dns-domains', $this->getUserName())); } /** - * @param $user - * * @return mixed */ - public function rebuildMailDomains($user) + public function rebuildMailDomains() { - return $this->toString($this->send('v-rebuild-mail-domains', $user)); + return $this->toString($this->send('v-rebuild-mail-domains', $this->getUserName())); } /** - * @param $user - * * @return mixed */ - public function rebuildDataBases($user) + public function rebuildDataBases() { - return $this->toString($this->send('v-rebuild-databases', $user)); + return $this->toString($this->send('v-rebuild-databases', $this->getUserName())); } /** - * @param $user - * * @return mixed * @internal param string $restart */ - public function rebuildCronJobs($user) + public function rebuildCronJobs() { - return $this->toString($this->send('v-rebuild-cron-jobs', $user)); + return $this->toString($this->send('v-rebuild-cron-jobs', $this->getUserName())); } /** @@ -165,7 +155,7 @@ public function rebuildCronJobs($user) */ public function updateUserCounters($user) { - return $this->toString($this->send('v-update-user-counters', $user)); + return $this->toString($this->send('v-update-user-counters', $this->getUserName())); } /** diff --git a/src/Services/User.php b/src/Services/User.php index 38c8873..85a318d 100644 --- a/src/Services/User.php +++ b/src/Services/User.php @@ -26,89 +26,79 @@ public function regUser($username, $password, $email, $package, $fistName, $last * * @return mixed */ - public function changeUserPassword($user, $password) + public function changeUserPassword($password) { - return $this->toString($this->send('v-change-user-password', $user, $password)); + return $this->toString($this->send('v-change-user-password', $this->getUserName(), $password)); } /** - * @param $user * @param $email * * @return mixed */ - public function changeUserEmail($user, $email) + public function changeUserEmail($email) { - return $this->toString($this->send('v-change-user-contact', $user, $email)); + return $this->toString($this->send('v-change-user-contact', $this->getUserName(), $email)); } /** * List User Account. * - * @param $user - * * @return mixed */ - public function listUserAccount($user) + public function listUserAccount() { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user', $user, 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user', $this->getUserName(), 'json')); } /** - * @param $user - * * @return mixed */ - public function listUserLog($user) + public function listUserLog() { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user-log', $user, 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user-log', $this->getUserName(), 'json')); } /** * List User Backups. * - * @param $user - * * @return mixed */ - public function listUserBackups($user) + public function listUserBackups() { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user-backups', $user, 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user-backups', $this->getUserName(), 'json')); } /** * Delete backup user. * - * @param $user * @param $backup * * @return mixed */ - public function deleteUserBackup($user, $backup) + public function deleteUserBackup($backup) { - return $this->toString($this->send('v-delete-user-backup', $user, $backup)); + return $this->toString($this->send('v-delete-user-backup', $this->getUserName(), $backup)); } /** * Show Backup. * - * @param $user * @param $backup * * @return mixed */ - public function showUserBackup($user, $backup) + public function showUserBackup($backup) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user-backup', $user, $backup, 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user-backup', $this->getUserName(), $backup, 'json')); } /** - * @param $user * @param $arg * * @return mixed */ - public function restoreBackup($user, $arg) + public function restoreBackup($arg) { $backup = 'no'; $web = 'no'; @@ -119,18 +109,16 @@ public function restoreBackup($user, $arg) $udir = 'no'; extract($arg, EXTR_OVERWRITE); - return $this->toString($this->send('v-schedule-user-restore', $user, $backup, $web, $dns, $mail, + return $this->toString($this->send('v-schedule-user-restore', $this->getUserName(), $backup, $web, $dns, $mail, $db, $cron, $udir)); } /** - * @param $user - * * @return mixed */ - public function suspendUser($user) + public function suspendUser() { - return $this->toString($this->send('v-suspend-user', $user, 'no')); + return $this->toString($this->send('v-suspend-user', $this->getUserName(), 'no')); } /** @@ -152,35 +140,31 @@ public function listUserPackages() } /** - * @param $user * @param $option * * @return mixed */ - public function getValue($user, $option) + public function getValue($option) { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-get-user-value', $user, $option)); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-get-user-value', $this->getUserName(), $option)); } /** - * @param $user * @param string $ssh * * @return mixed */ - public function changeShell($user, $ssh = 'nologin') + public function changeShell($ssh = 'nologin') { - return $this->toString($this->send('v-change-user-shell', $user, $ssh)); + return $this->toString($this->send('v-change-user-shell', $this->getUserName(), $ssh)); } /** - * @param $user - * * @return mixed */ - public function adminListUserAccount($user) + public function adminListUserAccount() { - return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user', $user, 'json')); + return $this->setReturnCode(self::RETURN_CODE_NO)->toArray($this->send('v-list-user', $this->getUserName(), 'json')); } /** diff --git a/src/Services/VestaAPI.php b/src/Services/VestaAPI.php index b4f38aa..5f753ce 100644 --- a/src/Services/VestaAPI.php +++ b/src/Services/VestaAPI.php @@ -10,6 +10,12 @@ class VestaAPI const RETURN_CODE_YES = 'yes', RETURN_CODE_NO = 'no'; + + /** + * @var string + */ + private $adminUser = ''; + /** * @var string */ @@ -59,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; } @@ -140,7 +147,7 @@ public function setToArray($toArray) public function send($cmd) { $postVars = [ - 'user' => $this->userName, + 'user' => $this->adminUser, 'hash' => $this->key, // api key 'returncode' => $this->returnCode, 'cmd' => $cmd,