From c920bf6a63edcc2beed16bcca7187c705df42bad Mon Sep 17 00:00:00 2001 From: "Michael Kaufmann (d00p)" Date: Mon, 12 Mar 2018 10:45:46 +0100 Subject: [PATCH] some code-reduction Signed-off-by: Michael Kaufmann (d00p) --- lib/classes/api/abstract.ApiCommand.php | 185 ++++-------------- lib/classes/api/abstract.ApiParameter.php | 180 +++++++++++++++++ .../api/commands/class.EmailForwarders.php | 36 +--- lib/classes/api/commands/class.Emails.php | 45 +---- lib/classes/api/commands/class.Ftps.php | 36 +--- lib/classes/api/commands/class.Mysqls.php | 53 ++--- lib/classes/api/commands/class.SubDomains.php | 61 ++---- 7 files changed, 261 insertions(+), 335 deletions(-) create mode 100644 lib/classes/api/abstract.ApiParameter.php diff --git a/lib/classes/api/abstract.ApiCommand.php b/lib/classes/api/abstract.ApiCommand.php index da3f0430..a0c66726 100644 --- a/lib/classes/api/abstract.ApiCommand.php +++ b/lib/classes/api/abstract.ApiCommand.php @@ -15,7 +15,7 @@ * @since 0.10.0 * */ -abstract class ApiCommand +abstract class ApiCommand extends ApiParameter { /** @@ -53,13 +53,6 @@ abstract class ApiCommand */ private $mail = null; - /** - * array of parameters passed to the command - * - * @var array - */ - private $cmd_params = null; - /** * language strings array * @@ -103,14 +96,12 @@ abstract class ApiCommand { global $lng, $version, $dbversion, $branding; + parent::__construct($params); + $this->version = $version; $this->dbversion = $dbversion; $this->branding = $branding; - if (! is_null($params)) { - $params = $this->trimArray($params); - } - $this->cmd_params = $params; if (! empty($header)) { $this->readUserData($header); } elseif (! empty($userinfo)) { @@ -268,97 +259,6 @@ abstract class ApiCommand return $this->user_data; } - /** - * get specific parameter from the parameterlist; - * check for existence and != empty if needed. - * Maybe more in the future - * - * @param string $param - * parameter to get out of the request-parameter list - * @param bool $optional - * default: false - * @param mixed $default - * value which is returned if optional=true and param is not set - * - * @throws Exception - * @return mixed - */ - protected function getParam($param = null, $optional = false, $default = '') - { - // does it exist? - if (! isset($this->cmd_params[$param])) { - if ($optional === false) { - // get module + function for better error-messages - $inmod = $this->getModFunctionString(); - throw new Exception('Requested parameter "' . $param . '" could not be found for "' . $inmod . '"', 404); - } - return $default; - } - // is it empty? - test really on string, as value 0 is being seen as empty by php - if ($this->cmd_params[$param] === "") { - if ($optional === false) { - // get module + function for better error-messages - $inmod = $this->getModFunctionString(); - throw new Exception('Requested parameter "' . $param . '" is empty where it should not be for "' . $inmod . '"', 406); - } - return ''; - } - // everything else is fine - return $this->cmd_params[$param]; - } - - /** - * get specific parameter which also has and unlimited-field - * - * @param string $param - * parameter to get out of the request-parameter list - * @param string $ul_field - * parameter to get out of the request-parameter list - * @param bool $optional - * default: false - * @param mixed $default - * value which is returned if optional=true and param is not set - * - * @return mixed - */ - protected function getUlParam($param = null, $ul_field = null, $optional = false, $default = 0) - { - $param_value = intval_ressource($this->getParam($param, $optional, $default)); - $ul_field_value = $this->getParam($ul_field, true, 0); - if ($ul_field_value != 0) { - $param_value = - 1; - } - return $param_value; - } - - /** - * update value of parameter - * - * @param string $param - * @param mixed $value - * - * @throws Exception - * @return boolean - */ - protected function updateParam($param, $value = null) - { - if (isset($this->cmd_params[$param])) { - $this->cmd_params[$param] = $value; - return true; - } - throw new Exception("Unable to update parameter '" . $param . "' as it does not exist", 500); - } - - /** - * return list of all parameters - * - * @return array - */ - protected function getParamList() - { - return $this->cmd_params; - } - /** * return logger instance * @@ -471,6 +371,38 @@ abstract class ApiCommand return $customer_ids; } + /** + * returns an array of customer data for customer, or by customer-id/loginname for admin/reseller + * + * @param int $customerid + * optional, required if loginname is empty + * @param string $loginname + * optional, required of customerid is empty + * @param string $customer_resource_check + * optional, when called as admin, check the resources of the target customer + * + * @throws Exception + * @return array + */ + protected function getCustomerData($customer_resource_check = '') + { + if ($this->isAdmin()) { + $customerid = $this->getParam('customerid', true, 0); + $loginname = $this->getParam('loginname', true, ''); + $customer = $this->apiCall('Customers.get', array( + 'id' => $customerid, + 'loginname' => $loginname + )); + // check whether the customer has enough resources + if (! empty($customer_resource_check) && $customer[$customer_resource_check . '_used'] >= $customer[$customer_resource_check] && $customer[$customer_resource_check] != '-1') { + throw new Exception("Customer has no more resources available", 406); + } + } else { + $customer = $this->getUserData(); + } + return $customer; + } + /** * increase/decrease a resource field for customers/admins * @@ -493,35 +425,6 @@ abstract class ApiCommand ), true, true); } - /** - * returns "module::function()" for better error-messages (missing parameter etc.) - * makes debugging a whole lot more comfortable - * - * @return string - */ - private function getModFunctionString() - { - $_class = get_called_class(); - $level = 2; - if (version_compare(PHP_VERSION, "5.4.0", "<")) { - $trace = debug_backtrace(); - } else { - $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); - } - while (true) { - $class = $trace[$level]['class']; - $func = $trace[$level]['function']; - if ($class != $_class) { - $level ++; - if ($level > 5) { - break; - } - continue; - } - return $class . ':' . $func; - } - } - /** * read user data from database by api-request-header fields * @@ -563,22 +466,4 @@ abstract class ApiCommand } throw new Exception("Invalid API credentials", 400); } - - /** - * run 'trim' function on an array recursively - * - * @param array $input - * - * @return array - */ - private function trimArray($input) - { - if (! is_array($input)) { - return trim($input); - } - return array_map(array( - $this, - 'trimArray' - ), $input); - } } diff --git a/lib/classes/api/abstract.ApiParameter.php b/lib/classes/api/abstract.ApiParameter.php new file mode 100644 index 00000000..749b4958 --- /dev/null +++ b/lib/classes/api/abstract.ApiParameter.php @@ -0,0 +1,180 @@ + (2010-) + * @license GPLv2 http://files.froxlor.org/misc/COPYING.txt + * @package API + * @since 0.10.0 + * + */ +abstract class ApiParameter +{ + + /** + * array of parameters passed to the command + * + * @var array + */ + private $cmd_params = null; + + /** + * + * @param array $params + * optional, array of parameters (var=>value) for the command + * + * @throws Exception + */ + public function __construct($params = null) + { + if (! is_null($params)) { + $params = $this->trimArray($params); + } + $this->cmd_params = $params; + } + + /** + * get specific parameter from the parameterlist; + * check for existence and != empty if needed. + * Maybe more in the future + * + * @param string $param + * parameter to get out of the request-parameter list + * @param bool $optional + * default: false + * @param mixed $default + * value which is returned if optional=true and param is not set + * + * @throws Exception + * @return mixed + */ + protected function getParam($param = null, $optional = false, $default = '') + { + // does it exist? + if (! isset($this->cmd_params[$param])) { + if ($optional === false) { + // get module + function for better error-messages + $inmod = $this->getModFunctionString(); + throw new Exception('Requested parameter "' . $param . '" could not be found for "' . $inmod . '"', 404); + } + return $default; + } + // is it empty? - test really on string, as value 0 is being seen as empty by php + if ($this->cmd_params[$param] === "") { + if ($optional === false) { + // get module + function for better error-messages + $inmod = $this->getModFunctionString(); + throw new Exception('Requested parameter "' . $param . '" is empty where it should not be for "' . $inmod . '"', 406); + } + return ''; + } + // everything else is fine + return $this->cmd_params[$param]; + } + + /** + * get specific parameter which also has and unlimited-field + * + * @param string $param + * parameter to get out of the request-parameter list + * @param string $ul_field + * parameter to get out of the request-parameter list + * @param bool $optional + * default: false + * @param mixed $default + * value which is returned if optional=true and param is not set + * + * @return mixed + */ + protected function getUlParam($param = null, $ul_field = null, $optional = false, $default = 0) + { + $param_value = intval_ressource($this->getParam($param, $optional, $default)); + $ul_field_value = $this->getParam($ul_field, true, 0); + if ($ul_field_value != 0) { + $param_value = - 1; + } + return $param_value; + } + + /** + * update value of parameter + * + * @param string $param + * @param mixed $value + * + * @throws Exception + * @return boolean + */ + protected function updateParam($param, $value = null) + { + if (isset($this->cmd_params[$param])) { + $this->cmd_params[$param] = $value; + return true; + } + throw new Exception("Unable to update parameter '" . $param . "' as it does not exist", 500); + } + + /** + * return list of all parameters + * + * @return array + */ + protected function getParamList() + { + return $this->cmd_params; + } + + /** + * returns "module::function()" for better error-messages (missing parameter etc.) + * makes debugging a whole lot more comfortable + * + * @return string + */ + private function getModFunctionString() + { + $_class = get_called_class(); + $level = 2; + if (version_compare(PHP_VERSION, "5.4.0", "<")) { + $trace = debug_backtrace(); + } else { + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + } + while (true) { + $class = $trace[$level]['class']; + $func = $trace[$level]['function']; + if ($class != $_class) { + $level ++; + if ($level > 5) { + break; + } + continue; + } + return $class . ':' . $func; + } + } + + /** + * run 'trim' function on an array recursively + * + * @param array $input + * + * @return array + */ + private function trimArray($input) + { + if (! is_array($input)) { + return trim($input); + } + return array_map(array( + $this, + 'trimArray' + ), $input); + } +} diff --git a/lib/classes/api/commands/class.EmailForwarders.php b/lib/classes/api/commands/class.EmailForwarders.php index f4658c9b..ee95f349 100644 --- a/lib/classes/api/commands/class.EmailForwarders.php +++ b/lib/classes/api/commands/class.EmailForwarders.php @@ -69,21 +69,8 @@ class EmailForwarders extends ApiCommand implements ResourceEntity standard_error('destinationalreadyexist', $destination, true); } - // get needed customer info to reduce the email-address-counter by one - if ($this->isAdmin()) { - // get customer id - $customer_id = $this->getParam('customerid'); - $customer = $this->apiCall('Customers.get', array( - 'id' => $customer_id - )); - // check whether the customer has enough resources to get the mail-forwarder added - if ($customer['email_forwarders_used'] >= $customer['email_forwarders'] && $customer['email_forwarders'] != '-1') { - throw new Exception("Customer has no more resources available", 406); - } - } else { - $customer_id = $this->getUserDetail('customerid'); - $customer = $this->getUserData(); - } + // get needed customer info to reduce the email-forwarder-counter by one + $customer = $this->getCustomerData('email_forwarders'); // add destination to address $result['destination'] .= ' ' . $destination; @@ -93,13 +80,13 @@ class EmailForwarders extends ApiCommand implements ResourceEntity "); $params = array( "dest" => makeCorrectDestination($result['destination']), - "cid" => $customer_id, + "cid" => $customer['customerid'], "id" => $id ); Database::pexecute($stmt, $params, true, true); // update customer usage - Customers::increaseUsage($customer_id, 'email_forwarders_used'); + Customers::increaseUsage($customer['customerid'], 'email_forwarders_used'); // update admin usage Admins::increaseUsage($customer['adminid'], 'email_forwarders_used'); @@ -169,17 +156,8 @@ class EmailForwarders extends ApiCommand implements ResourceEntity $result['destination'] = explode(' ', $result['destination']); if (isset($result['destination'][$forwarderid]) && $result['email'] != $result['destination'][$forwarderid]) { - // get needed customer info to reduce the email-address-counter by one - if ($this->isAdmin()) { - // get customer id - $customer_id = $this->getParam('customer_id'); - $customer = $this->apiCall('Customers.get', array( - 'id' => $customer_id - )); - } else { - $customer_id = $this->getUserDetail('customerid'); - $customer = $this->getUserData(); - } + // get needed customer info to reduce the email-forwarder-counter by one + $customer = $this->getCustomerData(); // get specific forwarder $forwarder = $result['destination'][$forwarderid]; @@ -207,7 +185,7 @@ class EmailForwarders extends ApiCommand implements ResourceEntity )); // update customer usage - Customers::decreaseUsage($customer_id, 'email_forwarders_used'); + Customers::decreaseUsage($customer['customerid'], 'email_forwarders_used'); // update admin usage Admins::decreaseUsage($customer['adminid'], 'email_forwarders_used'); diff --git a/lib/classes/api/commands/class.Emails.php b/lib/classes/api/commands/class.Emails.php index b72bfbeb..9fe3273e 100644 --- a/lib/classes/api/commands/class.Emails.php +++ b/lib/classes/api/commands/class.Emails.php @@ -85,20 +85,7 @@ class Emails extends ApiCommand implements ResourceEntity } // get needed customer info to reduce the email-address-counter by one - if ($this->isAdmin()) { - // get customer id - $customer_id = $this->getParam('customerid'); - $customer = $this->apiCall('Customers.get', array( - 'id' => $customer_id - )); - // check whether the customer has enough resources to get the mail-address added - if ($customer['emails_used'] >= $customer['emails'] && $customer['emails'] != '-1') { - throw new Exception("Customer has no more resources available", 406); - } - } else { - $customer_id = $this->getUserDetail('customerid'); - $customer = $this->getUserData(); - } + $customer = $this->getCustomerData('emails'); // duplicate check $stmt = Database::prepare(" @@ -138,7 +125,7 @@ class Emails extends ApiCommand implements ResourceEntity $address_id = Database::lastInsertId(); // update customer usage - Customers::increaseUsage($customer_id, 'emails_used'); + Customers::increaseUsage($customer['customerid'], 'emails_used'); // update admin usage Admins::increaseUsage($customer['adminid'], 'emails_used'); @@ -236,16 +223,7 @@ class Emails extends ApiCommand implements ResourceEntity $iscatchall = $this->getParam('iscatchall', true, $result['iscatchall']); // get needed customer info to reduce the email-address-counter by one - if ($this->isAdmin()) { - // get customer id - $customer_id = $this->getParam('customerid'); - $customer = $this->apiCall('Customers.get', array( - 'id' => $customer_id - )); - } else { - $customer_id = $this->getUserDetail('customerid'); - $customer = $this->getUserData(); - } + $customer = $this->getCustomerData(); // check for catchall-flag if ($iscatchall) { @@ -348,16 +326,7 @@ class Emails extends ApiCommand implements ResourceEntity $delete_userfiles = $this->getParam('delete_userfiles', true, 0); // get needed customer info to reduce the email-address-counter by one - if ($this->isAdmin()) { - // get customer id - $customer_id = $this->getParam('customerid'); - $customer = $this->apiCall('Customers.get', array( - 'id' => $customer_id - )); - } else { - $customer_id = $this->getUserDetail('customerid'); - $customer = $this->getUserData(); - } + $customer = $this->getCustomerData(); // check for forwarders $number_forwarders = 0; @@ -373,7 +342,7 @@ class Emails extends ApiCommand implements ResourceEntity if (Settings::Get('system.mail_quota_enabled') == 1) { $stmt = Database::prepare("SELECT `quota` FROM `" . TABLE_MAIL_USERS . "` WHERE `customerid`= :customerid AND `id`= :id"); $res_quota = Database::pexecute_first($stmt, array( - "customerid" => $customer_id, + "customerid" => $customer['customerid'], "id" => $result['popaccountid'] ), true, true); Customers::decreaseUsage($customer['customerid'], 'email_quota_used', '', $res_quota['quota']); @@ -383,7 +352,7 @@ class Emails extends ApiCommand implements ResourceEntity // delete account $stmt = Database::prepare("DELETE FROM `" . TABLE_MAIL_USERS . "` WHERE `customerid`= :customerid AND `id`= :id"); Database::pexecute($stmt, array( - "customerid" => $customer_id, + "customerid" => $customer['customerid'], "id" => $result['popaccountid'] ), true, true); Customers::decreaseUsage($customer['customerid'], 'email_accounts_used'); @@ -398,7 +367,7 @@ class Emails extends ApiCommand implements ResourceEntity // delete address $stmt = Database::prepare("DELETE FROM `" . TABLE_MAIL_VIRTUAL . "` WHERE `customerid`= :customerid AND `id`= :id"); Database::pexecute($stmt, array( - "customerid" => $customer_id, + "customerid" => $customer['customerid'], "id" => $id ), true, true); Customers::decreaseUsage($customer['customerid'], 'emails_used'); diff --git a/lib/classes/api/commands/class.Ftps.php b/lib/classes/api/commands/class.Ftps.php index 56ef099b..09c4f193 100644 --- a/lib/classes/api/commands/class.Ftps.php +++ b/lib/classes/api/commands/class.Ftps.php @@ -83,20 +83,7 @@ class Ftps extends ApiCommand implements ResourceEntity $params = array(); // get needed customer info to reduce the ftp-user-counter by one - if ($this->isAdmin()) { - // get customer id - $customer_id = $this->getParam('customer_id'); - $customer = $this->apiCall('Customers.get', array( - 'id' => $customer_id - )); - // check whether the customer has enough resources to get the ftp-user added - if ($customer['ftps_used'] >= $customer['ftps'] && $customer['ftps'] != '-1') { - throw new Exception("Customer has no more resources available", 406); - } - } else { - $customer_id = $this->getUserDetail('customerid'); - $customer = $this->getUserData(); - } + $customer = $this->getCustomerData('ftps'); if ($sendinfomail != 1) { $sendinfomail = 0; @@ -114,7 +101,7 @@ class Ftps extends ApiCommand implements ResourceEntity AND `customerid` = :customerid"); $ftpdomain_check = Database::pexecute_first($ftpdomain_check_stmt, array( "domain" => $ftpdomain, - "customerid" => $customer_id + "customerid" => $customer['customerid'] ), true, true); if ($ftpdomain_check && $ftpdomain_check['domain'] != $ftpdomain) { @@ -144,7 +131,7 @@ class Ftps extends ApiCommand implements ResourceEntity (`customerid`, `username`, `description`, `password`, `homedir`, `login_enabled`, `uid`, `gid`, `shell`) VALUES (:customerid, :username, :description, :password, :homedir, 'y', :guid, :guid, :shell)"); $params = array( - "customerid" => $customer_id, + "customerid" => $customer['customerid'], "username" => $username, "description" => $description, "password" => $cryptPassword, @@ -179,14 +166,14 @@ class Ftps extends ApiCommand implements ResourceEntity "); $params = array( "username" => $username, - "customerid" => $customer_id, + "customerid" => $customer['customerid'], "guid" => $customer['guid'] ); Database::pexecute($stmt, $params, true, true); // update customer usage - Customers::increaseUsage($customer_id, 'ftps_used'); - Customers::increaseUsage($customer_id, 'ftp_lastaccountnumber'); + Customers::increaseUsage($customer['customerid'], 'ftps_used'); + Customers::increaseUsage($customer['customerid'], 'ftp_lastaccountnumber'); // update admin usage Admins::increaseUsage($customer['adminid'], 'ftps_used'); @@ -360,16 +347,7 @@ class Ftps extends ApiCommand implements ResourceEntity } // get needed customer info to reduce the ftp-user-counter by one - if ($this->isAdmin()) { - // get customer id - $customer_id = $this->getParam('customer_id'); - $customer = $this->apiCall('Customers.get', array( - 'id' => $customer_id - )); - } else { - $customer_id = $this->getUserDetail('customerid'); - $customer = $this->getUserData(); - } + $customer = $this->getCustomerData(); // password update? if ($password != '') { diff --git a/lib/classes/api/commands/class.Mysqls.php b/lib/classes/api/commands/class.Mysqls.php index dfb1cd55..2954660b 100644 --- a/lib/classes/api/commands/class.Mysqls.php +++ b/lib/classes/api/commands/class.Mysqls.php @@ -68,19 +68,7 @@ class Mysqls extends ApiCommand implements ResourceEntity } // get needed customer info to reduce the mysql-usage-counter by one - if ($this->isAdmin()) { - // get customer id - $customer_id = $this->getParam('customer_id'); - $customer = $this->apiCall('Customers.get', array( - 'id' => $customer_id - )); - // check whether the customer has enough resources to get the database added - if ($customer['mysqls_used'] >= $customer['mysqls'] && $customer['mysqls'] != '-1') { - throw new Exception("Customer has no more resources available", 406); - } - } else { - $customer_id = $this->getUserDetail('customerid'); - } + $customer = $this->getCustomerData('mysqls'); $newdb_params = array( 'loginname' => ($this->isAdmin() ? $customer['loginname'] : $this->getUserDetail('loginname')), @@ -105,7 +93,7 @@ class Mysqls extends ApiCommand implements ResourceEntity `dbserver` = :dbserver "); $params = array( - "customerid" => ($this->isAdmin() ? $customer['customerid'] : $this->getUserDetail('customerid')), + "customerid" => $customer['customerid'], "databasename" => $username, "description" => $databasedescription, "dbserver" => $dbserver @@ -115,8 +103,8 @@ class Mysqls extends ApiCommand implements ResourceEntity $params['id'] = $databaseid; // update customer usage - Customers::increaseUsage(($this->isAdmin() ? $customer['customerid'] : $this->getUserDetail('customerid')), 'mysqls_used'); - Customers::increaseUsage(($this->isAdmin() ? $customer['customerid'] : $this->getUserDetail('customerid')), 'mysql_lastaccountnumber'); + Customers::increaseUsage($customer['customerid'], 'mysqls_used'); + Customers::increaseUsage($customer['customerid'], 'mysql_lastaccountnumber'); // update admin usage Admins::increaseUsage($this->getUserDetail('adminid'), 'mysqls_used'); @@ -132,7 +120,7 @@ class Mysqls extends ApiCommand implements ResourceEntity Database::needSqlData(); $sql_root = Database::getSqlData(); Database::needRoot(false); - $userinfo = ($this->isAdmin() ? $customer : $this->getUserData()); + $userinfo = $customer; $replace_arr = array( 'SALUTATION' => getCorrectUserSalutation($userinfo), @@ -351,19 +339,7 @@ class Mysqls extends ApiCommand implements ResourceEntity } // get needed customer info to reduce the mysql-usage-counter by one - if ($this->isAdmin()) { - // get customer id - $customer_id = $this->getParam('customer_id'); - $customer = $this->apiCall('Customers.get', array( - 'id' => $customer_id - )); - // check whether the customer has enough resources to get the database added - if ($customer['mysqls_used'] >= $customer['mysqls'] && $customer['mysqls'] != '-1') { - throw new Exception("Customer has no more resources available", 406); - } - } else { - $customer_id = $this->getUserDetail('customerid'); - } + $customer = $this->getCustomerData(); if ($password != '') { // validate password @@ -398,7 +374,7 @@ class Mysqls extends ApiCommand implements ResourceEntity "); $params = array( "desc" => $databasedescription, - "customerid" => ($this->isAdmin() ? $customer['customerid'] : $this->getUserDetail('customerid')), + "customerid" => $customer['customerid'], "id" => $id ); Database::pexecute($stmt, $params, true, true); @@ -519,19 +495,12 @@ class Mysqls extends ApiCommand implements ResourceEntity ), true, true); // get needed customer info to reduce the mysql-usage-counter by one - if ($this->isAdmin()) { - $customer = $this->apiCall('Customers.get', array( - 'id' => $result['customerid'] - )); - $mysql_used = $customer['mysqls_used']; - $customer_id = $customer['customer_id']; - } else { - $mysql_used = $this->getUserDetail('mysqls_used'); - $customer_id = $this->getUserDetail('customerid'); - } + $customer = $this->getCustomerData(); + $mysql_used = $customer['mysqls_used']; + // reduce mysql-usage-counter $resetaccnumber = ($mysql_used == '1') ? " , `mysql_lastaccountnumber` = '0' " : ''; - Customers::decreaseUsage($customer_id, 'mysqls_used', $resetaccnumber); + Customers::decreaseUsage($customer['customerid'], 'mysqls_used', $resetaccnumber); // update admin usage Admins::decreaseUsage(($this->isAdmin() ? $customer['adminid'] : $this->getUserDetail('adminid')), 'mysqls_used'); diff --git a/lib/classes/api/commands/class.SubDomains.php b/lib/classes/api/commands/class.SubDomains.php index ba257882..306d7ae9 100644 --- a/lib/classes/api/commands/class.SubDomains.php +++ b/lib/classes/api/commands/class.SubDomains.php @@ -78,20 +78,7 @@ class SubDomains extends ApiCommand implements ResourceEntity } // get needed customer info to reduce the subdomain-usage-counter by one - if ($this->isAdmin()) { - // get customer id - $customer_id = $this->getParam('customer_id'); - $customer = $this->apiCall('Customers.get', array( - 'id' => $customer_id - )); - // check whether the customer has enough resources to get the subdomain added - if ($customer['subdomains_used'] >= $customer['subdomains'] && $customer['subdomains'] != '-1') { - throw new Exception("Customer has no more resources available", 406); - } - } else { - $customer_id = $this->getUserDetail('customerid'); - $customer = $this->getUserData(); - } + $customer = $this->getCustomerData('subdomains'); // validation if (substr($subdomain, 0, 4) == 'xn--') { @@ -127,7 +114,7 @@ class SubDomains extends ApiCommand implements ResourceEntity "); $completedomain_check = Database::pexecute_first($completedomain_stmt, array( "domain" => $completedomain, - "customerid" => $customer_id + "customerid" => $customer['customerid'] ), true, true); if ($completedomain_check) { @@ -153,7 +140,7 @@ class SubDomains extends ApiCommand implements ResourceEntity "); $aliasdomain_check = Database::pexecute_first($aliasdomain_stmt, array( "id" => $aliasdomain, - "customerid" => $customer_id + "customerid" => $customer['customerid'] ), true, true); if ($aliasdomain_check['id'] != $aliasdomain) { standard_error('domainisaliasorothercustomer', '', true); @@ -461,20 +448,7 @@ class SubDomains extends ApiCommand implements ResourceEntity } // get needed customer info to reduce the subdomain-usage-counter by one - if ($this->isAdmin()) { - // get customer id - $customer_id = $this->getParam('customer_id'); - $customer = $this->apiCall('Customers.get', array( - 'id' => $customer_id - )); - // check whether the customer has enough resources to get the subdomain added - if ($customer['subdomains_used'] >= $customer['subdomains'] && $customer['subdomains'] != '-1') { - throw new Exception("Customer has no more resources available", 406); - } - } else { - $customer_id = $this->getUserDetail('customerid'); - $customer = $this->getUserData(); - } + $customer = $this->getCustomerData(); $alias_stmt = Database::prepare("SELECT COUNT(`id`) AS count FROM `" . TABLE_PANEL_DOMAINS . "` WHERE `aliasdomain`= :aliasdomain"); $alias_check = Database::pexecute_first($alias_stmt, array( @@ -494,7 +468,7 @@ class SubDomains extends ApiCommand implements ResourceEntity "); $aliasdomain_check = Database::pexecute_first($aliasdomain_stmt, array( "id" => $aliasdomain, - "customerid" => $customer_id + "customerid" => $customer['customerid'] ), true, true); if ($aliasdomain_check['id'] != $aliasdomain) { standard_error('domainisaliasorothercustomer', '', true); @@ -743,20 +717,13 @@ class SubDomains extends ApiCommand implements ResourceEntity $id = $result['id']; // get needed customer info to reduce the subdomain-usage-counter by one - if ($this->isAdmin()) { - $customer = $this->apiCall('Customers.get', array( - 'id' => $result['customerid'] - )); - $subdomains_used = $customer['subdomains_used']; - $customer_id = $customer['customer_id']; - } else { - if ($result['caneditdomain'] == 0) { - throw new Exception("You cannot edit this resource", 405); - } - $subdomains_used = $this->getUserDetail('subdomains_used'); - $customer_id = $this->getUserDetail('customerid'); + $customer = $this->getCustomerData(); + $subdomains_used = $customer['subdomains_used']; + + if (!$this->isAdmin() && $result['caneditdomain'] == 0) { + throw new Exception("You cannot edit this resource", 405); } - + if ($result['isemaildomain'] == '1') { // check for e-mail addresses $emails_stmt = Database::prepare(" @@ -764,7 +731,7 @@ class SubDomains extends ApiCommand implements ResourceEntity WHERE `customerid` = :customerid AND `domainid` = :domainid "); $emails = Database::pexecute_first($emails_stmt, array( - "customerid" => $customer_id, + "customerid" => $customer['customerid'], "domainid" => $id ), true, true); @@ -780,7 +747,7 @@ class SubDomains extends ApiCommand implements ResourceEntity DELETE FROM `" . TABLE_PANEL_DOMAINS . "` WHERE `customerid` = :customerid AND `id` = :id "); Database::pexecute($stmt, array( - "customerid" => $customer_id, + "customerid" => $customer['customerid'], "id" => $id ), true, true); @@ -825,7 +792,7 @@ class SubDomains extends ApiCommand implements ResourceEntity inserttask('4'); // reduce subdomain-usage-counter - Customers::decreaseUsage($customer_id, 'subdomains_used'); + Customers::decreaseUsage($customer['customerid'], 'subdomains_used'); // update admin usage Admins::decreaseUsage(($this->isAdmin() ? $customer['adminid'] : $this->getUserDetail('adminid')), 'subdomains_used');