add EmailFowarders ApiCommand
Signed-off-by: Michael Kaufmann (d00p) <d00p@froxlor.org>
This commit is contained in:
226
lib/classes/api/commands/class.EmailForwarders.php
Normal file
226
lib/classes/api/commands/class.EmailForwarders.php
Normal file
@@ -0,0 +1,226 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Froxlor project.
|
||||
* Copyright (c) 2010 the Froxlor Team (see authors).
|
||||
*
|
||||
* For the full copyright and license information, please view the COPYING
|
||||
* file that was distributed with this source code. You can also view the
|
||||
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
|
||||
*
|
||||
* @copyright (c) the authors
|
||||
* @author Froxlor team <team@froxlor.org> (2010-)
|
||||
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
|
||||
* @package API
|
||||
* @since 0.10.0
|
||||
*
|
||||
*/
|
||||
class EmailForwarders extends ApiCommand implements ResourceEntity
|
||||
{
|
||||
|
||||
/**
|
||||
* add new email-forwarder entry for given email-address by either id or email-address
|
||||
*
|
||||
* @param int $id
|
||||
* optional, the email-address-id
|
||||
* @param string $emailaddr
|
||||
* optional, the email-address to add the forwarder for
|
||||
* @param string $destination
|
||||
* email-address to add as forwarder
|
||||
* @param int $customerid
|
||||
* optional, required when called as admin/reseller
|
||||
*
|
||||
* @access admin,customer
|
||||
* @throws Exception
|
||||
* @return array
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if ($this->isAdmin() == false && Settings::IsInList('panel.customer_hide_options', 'email')) {
|
||||
throw new Exception("You cannot access this resource", 405);
|
||||
}
|
||||
|
||||
if ($this->getUserDetail('email_forwarders_used') < $this->getUserDetail('email_forwarders') || $this->getUserDetail('email_forwarders') == '-1') {
|
||||
|
||||
// parameter
|
||||
$id = $this->getParam('id', true, 0);
|
||||
$ea_optional = ($id <= 0 ? false : true);
|
||||
$emailaddr = $this->getParam('emailaddr', $ea_optional, '');
|
||||
$destination = $this->getParam('destination');
|
||||
|
||||
// validation
|
||||
$idna_convert = new idna_convert_wrapper();
|
||||
$destination = $idna_convert->encode($destination);
|
||||
|
||||
$result = $this->apiCall('Emails.get', array(
|
||||
'id' => $id,
|
||||
'emailaddr' => $emailaddr
|
||||
));
|
||||
$id = $result['id'];
|
||||
|
||||
// current destination array
|
||||
$result['destination_array'] = explode(' ', $result['destination']);
|
||||
|
||||
if (! validateEmail($destination)) {
|
||||
standard_error('destinationiswrong', $destination, true);
|
||||
} elseif ($destination == $result['email']) {
|
||||
standard_error('destinationalreadyexistasmail', $destination, true);
|
||||
} elseif (in_array($destination, $result['destination_array'])) {
|
||||
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();
|
||||
}
|
||||
|
||||
// add destination to address
|
||||
$result['destination'] .= ' ' . $destination;
|
||||
$stmt = Database::prepare("
|
||||
UPDATE `" . TABLE_MAIL_VIRTUAL . "` SET `destination` = :dest
|
||||
WHERE `customerid`= :cid AND `id`= :id
|
||||
");
|
||||
$params = array(
|
||||
"dest" => makeCorrectDestination($result['destination']),
|
||||
"cid" => $customer_id,
|
||||
"id" => $id
|
||||
);
|
||||
Database::pexecute($stmt, $params, true, true);
|
||||
|
||||
// update customer usage
|
||||
Customers::increaseUsage($customer_id, 'email_forwarders_used');
|
||||
|
||||
// update admin usage
|
||||
Admins::increaseUsage($customer['adminid'], 'email_forwarders_used');
|
||||
|
||||
$this->logger()->logAction($this->isAdmin() ? ADM_ACTION : USR_ACTION, LOG_INFO, "[API] added email forwarder for '" . $result['email_full'] . "'");
|
||||
|
||||
$result = $this->apiCall('Emails.get', array(
|
||||
'emailaddr' => $result['email_full']
|
||||
));
|
||||
return $this->response(200, "successfull", $result);
|
||||
}
|
||||
throw new Exception("No more resources available", 406);
|
||||
}
|
||||
|
||||
public function get()
|
||||
{
|
||||
throw new Exception('You cannot directly get an email forwarder. You need to call Emails.get()', 303);
|
||||
}
|
||||
|
||||
public function update()
|
||||
{
|
||||
throw new Exception('You cannot update an email forwarder. You need to delete the entry and create a new one.', 303);
|
||||
}
|
||||
|
||||
public function listing()
|
||||
{
|
||||
throw new Exception('You cannot directly list email forwarders. You need to call Emails.listing()', 303);
|
||||
}
|
||||
|
||||
/**
|
||||
* delete email-forwarder entry for given email-address by either id or email-address and forwarder-id
|
||||
*
|
||||
* @param int $id
|
||||
* optional, the email-address-id
|
||||
* @param string $emailaddr
|
||||
* optional, the email-address to add the forwarder for
|
||||
* @param int $forwarderid
|
||||
* id of the forwarder to delete
|
||||
* @param int $customerid
|
||||
* optional, required when called as admin/reseller
|
||||
*
|
||||
* @access admin,customer
|
||||
* @throws Exception
|
||||
* @return array
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
if ($this->isAdmin() == false && Settings::IsInList('panel.customer_hide_options', 'email')) {
|
||||
throw new Exception("You cannot access this resource", 405);
|
||||
}
|
||||
|
||||
if ($this->getUserDetail('email_forwarders_used') < $this->getUserDetail('email_forwarders') || $this->getUserDetail('email_forwarders') == '-1') {
|
||||
|
||||
// parameter
|
||||
$id = $this->getParam('id', true, 0);
|
||||
$ea_optional = ($id <= 0 ? false : true);
|
||||
$emailaddr = $this->getParam('emailaddr', $ea_optional, '');
|
||||
$forwarderid = $this->getParam('forwarderid');
|
||||
|
||||
// validation
|
||||
$result = $this->apiCall('Emails.get', array(
|
||||
'id' => $id,
|
||||
'emailaddr' => $emailaddr
|
||||
));
|
||||
$id = $result['id'];
|
||||
|
||||
$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 specific forwarder
|
||||
$forwarder = $result['destination'][$forwarderid];
|
||||
// unset it from array
|
||||
unset($result['destination'][$forwarderid]);
|
||||
// rebuild destination-string
|
||||
$result['destination'] = implode(' ', $result['destination']);
|
||||
// update in DB
|
||||
$stmt = Database::prepare("
|
||||
UPDATE `" . TABLE_MAIL_VIRTUAL . "` SET `destination` = :dest
|
||||
WHERE `customerid`= :cid AND `id`= :id
|
||||
");
|
||||
$params = array(
|
||||
"dest" => makeCorrectDestination($result['destination']),
|
||||
"cid" => $customer['customerid'],
|
||||
"id" => $id
|
||||
);
|
||||
Database::pexecute($stmt, $params, true, true);
|
||||
|
||||
$stmt = Database::prepare("UPDATE `" . TABLE_PANEL_CUSTOMERS . "`
|
||||
SET `email_forwarders_used` = `email_forwarders_used` - 1
|
||||
WHERE `customerid`= :cid");
|
||||
Database::pexecute($stmt, array(
|
||||
"cid" => $userinfo['customerid']
|
||||
));
|
||||
|
||||
// update customer usage
|
||||
Customers::decreaseUsage($customer_id, 'email_forwarders_used');
|
||||
|
||||
// update admin usage
|
||||
Admins::decreaseUsage($customer['adminid'], 'email_forwarders_used');
|
||||
|
||||
$this->logger()->logAction($this->isAdmin() ? ADM_ACTION : USR_ACTION, LOG_INFO, "[API] deleted email forwarder for '" . $result['email_full'] . "'");
|
||||
|
||||
$result = $this->apiCall('Emails.get', array(
|
||||
'emailaddr' => $result['email_full']
|
||||
));
|
||||
return $this->response(200, "successfull", $result);
|
||||
}
|
||||
throw new Exception("Unknown forwarder id", 404);
|
||||
}
|
||||
throw new Exception("No more resources available", 406);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user