| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824 |
- <?php
- namespace ThurData\Servers\KerioEmail\Api;
- use ThurData\Servers\KerioEmail\Api\KerioConnectApi;
- /**
- *
- * Administration API for Kerio Connect - Sample Class.
- *
- * @copyright Copyright © 2012-2012 Kerio Technologies s.r.o.
- * @version 1.3.0.62
- */
- class KerioWhmcs extends KerioConnectApi {
- /**
- * Class constructor.
- * Register application
- *
- * @param string Application name
- * @param string Vendor of the application
- * @param string Application versiopn
- *
- * @return void
- */
- public function __construct($name, $vendor, $version) {
- $this->api = parent::__construct($name, $vendor, $version);
- return $this->api;
- }
- /**
- * Login and get list of available constants user by Kerio Connect
- *
- * @param string Hostname of remote server
- * @param string Administrator username
- * @param string Administrator password
- *
- * @return void
- */
- public function login($hostname, $username, $password) {
- $login = parent::login($hostname, $username, $password);
- $this->getServerConstants();
- return $login;
- }
- /**
- * Obtain server constants
- *
- * @param void
- * @return void
- */
- protected function getServerConstants() {
- $this->constants = parent::getConstants();
- }
- /**
- * Return list of available constants
- *
- * @param void
- * @return void
- */
- public function getConstants() {
- return $this->constants;
- }
- /**
- * Get list of available domains
- *
- * @param array List of fields to be obtained from engine
- * @return array List of available domains
- */
- public function getDomains($fields) {
- $method = 'Domains.get';
- $params = array(
- 'query' => array(
- 'fields' => $fields
- )
- );
- $result = $this->sendRequest($method, $params);
- return $result['list'];
- }
- /**
- * Get Id of a domain by name
- *
- * @param string Domain name
- * @return string Domain Id
- */
- public function getDomainId($domain) {
- $method = 'Domains.get';
- $params = array(
- 'query' => array(
- 'fields' => array(
- 'id',
- 'name'
- )
- )
- );
- $result = $this->sendRequest($method, $params);
- foreach($result['list'] as $key) {
- if ($key['name'] == $domain) {
- return $key['id'];
- }
- }
- return FALSE;
- }
- /**
- * Get list of users from a domain
- *
- * @param array List of fields to be obtained from engine
- * @param string Domain Id
- * @param array Additional condition for the request
- *
- * @return array List of users
- */
- public function getUsers($fields, $domainId, $conditions = null) {
- $method = 'Users.get';
- $params = array(
- 'query' => array(
- 'fields' => $fields,
- 'orderBy' => array(array(
- 'columnName' => 'loginName',
- 'direction' => 'Asc'
- ))
- ),
- 'domainId' => $domainId
- );
- if ($conditions) {
- $params['query']['conditions'] = $conditions;
- }
- $result = $this->sendRequest($method, $params);
- return $result['list'];
- }
- /**
- * Get address of a user
- *
- * @param string User Id
- *
- * @return array address of user
- */
- public function getAddress($id) {
- $method = 'Users.getPersonalContact';
- $params = array(
- 'userIds' => array(
- $id,
- )
- );
- $result = $this->sendRequest($method, $params);
- return $result;
- }
- /**
- * Set address of a user
- *
- * @param string User Id
- * @param array User Attributes
- *
- * @return array address of user
- */
- public function setAddress($id, $fields) {
- $method = 'Users.setPersonalContact';
- $params = array(
- 'userIds' => array(
- $id,
- ),
- 'contact' => $fields,
- );
- $result = $this->sendRequest($method, $params);
- return $result;
- }
- /**
- * Get login name by user's Id
- *
- * @param string User Id
- * @param string Domain Id
- *
- * @return string Login name
- */
- public function getUserById($userId, $domainId) {
- $fields = array('id', 'loginName');
- $userList = $this->getUsers($fields, $domainId);
-
- foreach ($userList as $user) {
- if ($user['id'] == $userId) return $user['loginName'];
- }
-
- return FALSE;
- }
- /**
- * Get list of groups from a domain
- *
- * @param array List of fields to be obtained from engine
- * @param string Domain Id
- * @param array Additional condition for the request
- *
- * @return array List of groups
- */
- public function getGroups($fields, $domainId, $conditions = null) {
- $method = 'Groups.get';
- $params = array(
- 'query' => array(
- 'fields' => $fields,
- 'orderBy' => array(array(
- 'columnName' => 'name',
- 'direction' => 'Asc'
- ))
- ),
- 'domainId' => $domainId
- );
- if ($conditions) {
- $params['query']['conditions'] = $conditions;
- }
- $result = $this->sendRequest($method, $params);
- return $result['list'];
- }
- /**
- * Create new group
- *
- * @param array User defined params
- * @return array Result of create action
- */
- public function createGroup($params) {
- $method = 'Groups.create';
- $result = $this->sendRequest($method, $params);
- return $result['result'];
- }
- /**
- * Add members to group of given ID
- *
- * @param string Group ID
- * @param array List of user IDs to be added
- *
- * @return void
- */
- public function addMembersToGroup($groupId, $userList) {
- $method = 'Groups.addMemberList';
- $params = array(
- 'userList' => $userList,
- 'groupId' => $groupId
- );
- $this->sendRequest($method, $params);
- }
- /**
- * Get list of mailing lists from a domain
- *
- * @param array List of fields to be obtained from engine
- * @param string Domain Id
- * @param array Additional condition for the request
- *
- * @return array List of mailing lists
- */
- public function getMailingLists($fields, $domainId, $conditions = null) {
- $method = 'MailingLists.get';
- $params = array(
- 'query' => array(
- 'fields' => $fields,
- 'orderBy' => array(array(
- 'columnName' => 'name',
- 'direction' => 'Asc'
- ))
- ),
- 'domainId' => $domainId
- );
- if ($conditions) {
- $params['query']['conditions'] = $conditions;
- }
- $result = $this->sendRequest($method, $params);
- return $result['list'];
- }
- /**
- * Create new mailinglist
- *
- * @param array Mailinglist
- * @return array Result of create action
- */
- public function createMailinglist($list) {
- $method = 'MailingLists.create';
- $params['mailingLists'] = array($list);
- $result = $this->sendRequest($method, $params);
- return $result['result'];
- }
- /**
- * Modify mailinglist
- *
- * @param string MailinglistID
- * @param array fields to set
- * @return array Result of delete action
- */
- public function modifyMailinglist($fields,$mlId) {
- $method = 'MailingLists.set';
- $params['mlIds'] = array($mlId);
- $params['pattern'] = $fields;
- $result = $this->sendRequest($method, $params);
- return $result;
- }
- /**
- * Delete mailinglist
- *
- * @param string MailinglistID
- * @return array Result of delete action
- */
- public function deleteMailinglist($mlId) {
- $method = 'MailingLists.remove';
- $params['mlIds'] = array($mlId);
- $result = $this->sendRequest($method, $params);
- return $result;
- }
- /**
- * Get list of mailing lists from a domain
- *
- * @param array List of fields to be obtained from engine
- * @param string Mailing list Id
- *
- * @return array List of mailing lists
- */
- public function getMlUserList($fields, $mlId) {
- $method = 'MailingLists.getMlUserList';
- $params = array(
- 'query' => array(
- 'fields' => $fields
- ),
- 'mlId' => $mlId
- );
- $result = $this->sendRequest($method, $params);
- return $result['list'];
- }
- /**
- * Add users to mailing list
- *
- * @param array List of fields of users to ba added
- * @param string Mailing list Id
- *
- * @return array List of mailing lists
- */
- public function addMlUserList($members, $mlId) {
- $method = 'MailingLists.addMlUserList';
- $params['members'] = $members;
- $params['mlId'] = $mlId;
- $result = $this->sendRequest($method, $params);
- return $result;
- }
- /**
- * Remove users from mailing list
- *
- * @param array List of fields of users to ba added
- * @param string Mailing list Id
- *
- * @return array List of mailing lists
- */
- public function delMlUserList($members, $mlId) {
- $method = 'MailingLists.removeMlUserList';
- $params['members'] = $members;
- $params['mlId'] = $mlId;
- $result = $this->sendRequest($method, $params);
- return $result;
- }
- /**
- * Get list of resources from a domain
- *
- * @param array List of fields to be obtained from engine
- * @param string Domain Id
- * @param array Additional condition for the request
- *
- * @return array List of mailing lists
- */
- public function getResources($fields, $domainId, $conditions = null) {
- $method = 'Resources.get';
- $params = array(
- 'query' => array(
- 'fields' => $fields,
- 'orderBy' => array(array(
- 'columnName' => 'name',
- 'direction' => 'Asc'
- ))
- ),
- 'domainId' => $domainId
- );
- if ($conditions) {
- $params['query']['conditions'] = $conditions;
- }
- $result = $this->sendRequest($method, $params);
- return $result['list'];
- }
- /**
- * Create resource.
- *
- * @param array Resource attributes
- * @param string Domain ID
- * @return array Result
- */
- function createResouce($attr, $domain) {
- $params = array(
- 'resources' => array(array(
- 'name' => $attr['name'],
- 'description' => $attr['description'],
- 'type' => $attr['type'],
- 'isEnabled' => $attr['status'] === 'active' ? TRUE : FALSE,
- 'resourceUsers' => array('id' => $domain, 'type' => 'AuthDomainPrincipal'),
- 'manager' => array('id' => $attr['manager'], 'type' => 'UserPrincipal'),
- 'domainId' => $domain
- ))
- );
- $result = $this->sendRequest('Resources.create', $params);
- logModuleCall(
- 'kerioEmail',
- __FUNCTION__,
- $result,
- 'Debug Error',
- $params
- );
- return $result;
- }
- /**
- * Get list of aliases from a domain
- *
- * @param array List of fields to be obtained from engine
- * @param string Domain Id
- * @param array Additional condition for the request
- *
- * @return array List of aliases
- */
- public function getAliases($fields, $domainId, $conditions = null) {
- $method = 'Aliases.get';
- $params = array(
- 'query' => array(
- 'fields' => $fields,
- 'orderBy' => array(array(
- 'columnName' => 'name',
- 'direction' => 'Asc'
- )),
- 'combining' => 'Or'
- ),
- 'domainId' => $domainId
- );
- if ($conditions) {
- $params['query']['conditions'] = $conditions;
- }
-
- $result = $this->sendRequest($method, $params);
-
- return $result['list'];
- }
- /**
- * Get list of all services
- *
- * @param void
- * @return array List of services
- */
- function getServices() {
- $method = 'Services.get';
- $params = array();
- $result = $this->sendRequest($method, $params);
- return $result['services'];
- }
- /**
- * Get list of all services
- *
- * @param void
- * @return array List of services
- */
- function getServerStatistics() {
- $method = 'Statistics.get';
- $params = array();
- $result = $this->sendRequest($method, $params);
- return $result['statistics'];
- }
- /**
- * Get server info
- *
- * @param void
- * @return array List of services
- */
- function getServerInfo() {
- $method = 'Server.getProductInfo';
- $params = array();
- $result = $this->sendRequest($method, $params);
- return $result;
- }
- /**
- * Create alias.
- *
- * @param string Domain ID
- * @param string Alias
- * @param string Email
- * @param string Description, optional
- * @return array Result
- */
- function createAlias($domain, $alias, $email, $description = '') {
- $params = array(
- 'aliases' => array(array(
- 'name' => $alias,
- 'domainId' => $domain,
- 'deliverTo' => $email,
- 'description' => $description,
- 'deliverToSelect' => 'TypeEmailAddress'
- ))
- );
- $result = $this->sendRequest('Aliases.create', $params);
- return $result;
- }
- /**
- * Delete Alias.
- *
- * @param string aliasID
- * @return array Result
- */
- function deleteAlias($aliasID) {
- $params = array(
- 'aliasIds' => array($aliasID),
- );
- $result = $this->sendRequest('Aliases.remove', $params);
- return $result;
- }
- /**
- * Create user.
- *
- * @param string Domain ID
- * @param string Username
- * @param string Password
- * @return array Result
- */
- function createUser($domain, $username, $password, $active = TRUE) {
- $params = array(
- 'users' => array(array(
- 'loginName' => $username,
- 'password' => $password,
- 'domainId' => $domain,
- 'isEnabled' => $active
- ))
- );
- $result = $this->sendRequest('Users.create', $params);
- return $result;
- }
- /**
- * Modify user.
- *
- * @param string User ID
- * @param array Attributes
- * @return array Result
- */
- function modifyUser($userId, $attr) {
- $params = array(
- 'userIds' => [ $userId ],
- 'pattern' => $attr
- );
- $result = $this->sendRequest('Users.set', $params);
- return $result;
- }
- /**
- * Count user.
- *
- * @param string Domain ID
- * @return int Result
- */
- function countUser($domainId) {
- $params = array(
- 'domainId' => $domainId
- );
- $result = $this->sendRequest('Domains.getUserCountInfo', $params);
- return intval($result['countInfo']['currentUsers']);
- }
- /**
- * Delete user.
- *
- * @param string User ID
- * @return array Result
- */
- function deleteUser($userId) {
- $params = array(
- 'requests' => array(
- array(
- 'userId' => $userId,
- 'mode' => 'DSModeDeactivate',
- 'method' => 'UDeleteFolder',
- 'targetUserId' => '',
- 'removeReferences' => TRUE,
- ),
- ),
- );
- $result = $this->sendRequest('Users.remove', $params);
- return $result;
- }
- /**
- * Create domain.
- *
- * @param string Domain name
- * @return array Result
- */
- function createDomain($domain) {
- $params = array(
- 'domains' => array(array(
- 'name' => $domain
- ))
- );
- $result = $this->sendRequest('Domains.create', $params);
- return $result;
- }
-
- /**
- * Modify domain.
- *
- * @param string DomainID
- * @param array Attributes
- * @return array Result
- */
- function modifyDomain($domainID, $attr) {
- $params = array(
- 'domainIds' => array($domainID),
- 'pattern' => $attr,
- );
- $result = $this->sendRequest('Domains.set', $params);
- return $result;
- }
-
- /**
- * Delete domain.
- *
- * @param string DomainID
- * @return array Result
- */
- function deleteDomain($domainID) {
- $params = array(
- 'domainIds' => array($domainID),
- );
- $result = $this->sendRequest('Domains.remove', $params);
- return $result;
- }
-
- /**
- * Get list of IP addresses from a file.
- *
- * Local function used in example spam_blacklist
- *
- * @param string Filename
- * @return array List of IP addresses
- * @throws KerioApiException
- */
- public function getBlacklistRecords($file) {
- $blacklist = array();
- if(file_exists($file) && is_readable($file)) {
- $data = file_get_contents($file);
- foreach (preg_split("/\n/", $data) as $record) {
- if (empty($record)) continue;
- array_push($blacklist, $record);
- }
- }
- else {
- throw new KerioApiException(sprintf('Cannot open file %s', $file));
- }
- return $blacklist;
- }
- /**
- * Get list of IP addesses from a group
- *
- * Local function used in example spam_blacklist
- *
- * @param string Group name
- * @return array List of IP addresses
- */
- public function getIpGroupList($name) {
- $params = array(
- "query" => array(
- "conditions" => array(array(
- "fieldName" => "name",
- "comparator" => "Like",
- "value" => $name
- )),
- "orderBy" => array(array(
- "columnName" => "item",
- "direction" => "Asc"
- ))
- )
- );
- $result = $this->sendRequest('IpAddressGroups.get', $params);
- return $result['list'];
- }
- /**
- * Add a IP address to a IP Group
- *
- * Local function used in example spam_blacklist
- *
- * @param string Group name
- * @param string IP address
- * @param string Description, optional
- * @return array Result
- */
- public function addHostToIpGroup($group, $ip, $description = '') {
- if(empty($description)) {
- $description = sprintf('Automatically added on %s', date(DATE_RFC822));
- }
- $params = array(
- "groups" => array(array(
- "groupId" => "",
- "groupName" => $group,
- "host" => $ip,
- "type" => "Host",
- "description" => $description,
- "enabled" => TRUE
- ))
- );
- $result = $this->sendRequest('IpAddressGroups.create', $params);
- return $result;
- }
- /**
- * Remove a IP address from a IP Group
- *
- * Local function used in example spam_blacklist
- *
- * @param string Group name
- * @param string IP address
- * @return array Result
- */
- public function removeHostFromIpGroup($group, $ip) {
- $list = $this->getIpGroupList(NULL);
- foreach ($list as $record) {
- if(($record['groupName'] != $group) || ($record['host'] != $ip)) continue;
- $hostId = $record['id'];
- }
- $params = array("groupIds" => array($hostId));
- $result = $this->sendRequest('IpAddressGroups.remove', $params);
- return $result;
- }
- /**
- * Random password generator
- *
- * Local function used in example createUser.
- *
- * @param integer Password lenght, default 10
- * @return string Random password
- */
- function genRandomPassword($length = 10) {
- $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
- $string = '';
- for ($p = 0; $p < $length; $p++) {
- $string .= $characters[mt_rand(0, (strlen($characters))-1)];
- }
-
- return $string;
- }
- }
|