diff --git a/README.md b/README.md new file mode 100644 index 0000000..f86f2c9 --- /dev/null +++ b/README.md @@ -0,0 +1,100 @@ +OpenfireRestPhpApi +===================== + +Use PHP to manage remote Openfire server using Rest Api of Openfire with restapi plugin. + +## REQUIREMENTS +- PHP 5.4+ + +## INSTALLATION + +### With Composer +------------- +You can install it via [composer](http://getcomposer.org/). Create `composer.json` file and add the requirement in require object, then run the `composer` install/update command. + +```json +{ + "require": { + "shaileshjangir/OpenfireRestPhpApi": "dev-master" + } +} +``` + +## USAGE +```php +include "vendor/autoload.php"; + +// Create the Openfire Rest api object +$OfApi = new ShaileshJangir\OpenfireRestPhpApi\OpenfireRestPhpApi; + +// Set the required config parameters +$OfApi->secret = "OpenFireRestSecret HERE"; +$OfApi->host = "IP OR HOST NAME OF JABBER SERVER"; // default localhost +$OfApi->port = "9090"; // default 9090 + +// Optional parameters (showing default values) + +$OfApi->useSSL = false; +$OfApi->plugin = "/plugins/restapi/v1"; // plugin + +// Add a new user to OpenFire and add to a group +$result = $OfApi->addUser('Username', 'Password', 'Real Name', 'johnsmith@example.com', array('Group 1')); + +// Check result if command is succesful +if($result['status']) { + // Display result, and check if it's an error or correct response + echo 'Success: '; + echo $result['message']; +} else { + // Something went wrong, probably connection issues + echo 'Error: '; + echo $result['message']; +} + +//Delete a user from OpenFire +$result = $OfApi->deleteUser($username); + + +//Disable a user +$result = $OfApi->lockoutUser($username); + + +//Enable a user +$result = $OfApi->unlockUser($username); + +/** + * Update a user + * + * The $password, $name, $email, $groups arguments are optional + * + */ +$result = $OfApi->updateUser($username, $password, $name, $email, $groups) + +//Add to roster +$OfApi->addToRoster($username, $jid); + +//Delete from roster +$OfApi->addToRoster($username, $jid); + +//Update user roster +$OfApi->updateRoster($username, $jid, $nickname, $subscription]); + +// Get all groups +$OfApi->getGroups(); + +// Retrieve group +$OfApi->getGroup($name); + +// Create a group +$OfApi->createGroup($group_name, $description); + +// Update a group description +$OfApi->updateGroup($group_name, $description); + +// Delete a group +$OfApi->deleteGroup($group_name); + +``` + +## CONTACT +- Shailesh Jangir diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..bcae6ce --- /dev/null +++ b/composer.json @@ -0,0 +1,26 @@ +{ + "name": "ShaileshJangir/OpenfireRestPhpApi", + "description": "Manage Openfire server using Rest Api", + "version": "1.0.0", + "keywords": ["openfire", "xmpp", "chat"], + "authors": [ + { + "name": "Shailesh Jangir", + "email": "shaileshjangir@gmail.com" + } + ], + "support": { + "email": "shaileshjangir@gmail.com" + }, + "require": { + "php": ">=5.4.0", + "ext-curl": "*", + "guzzlehttp/guzzle": "~5.0" + }, + "autoload": { + "psr-0": { + "ShaileshJangir\\OpenfireRestPhpApi": "src/" + } + }, + "minimum-stability": "dev" +} \ No newline at end of file diff --git a/src/ShaileshJangir/OpenfireRestPhpApi/OpenfireRestPhpApi.php b/src/ShaileshJangir/OpenfireRestPhpApi/OpenfireRestPhpApi.php new file mode 100644 index 0000000..4119ae7 --- /dev/null +++ b/src/ShaileshJangir/OpenfireRestPhpApi/OpenfireRestPhpApi.php @@ -0,0 +1,284 @@ +client = new Client(); + } + + /** + * Make the request and analyze the result + * + * @param string $type Request method + * @param string $endpoint Api request endpoint + * @param array $params Parameters + * @return array|false Array with data or error, or False when something went fully wrong + */ + + private function callOpenFireApi($type, $endpoint, $params=array()) + { + $base = ($this->useSSL) ? "https" : "http"; + $url = $base . "://" . $this->host . ":" .$this->port.$this->plugin.$endpoint; + $headers = array( + 'Accept' => 'application/json', + 'Authorization' => $this->secret + ); + + $body = json_encode($params); + + switch ($type) { + case 'get': + $result = $this->client->get($url, compact('headers')); + break; + case 'post': + $headers += ['Content-Type'=>'application/json']; + $result = $this->client->post($url, compact('headers','body')); + break; + case 'delete': + $headers += ['Content-Type'=>'application/json']; + $result = $this->client->delete($url, compact('headers','body')); + break; + case 'put': + $headers += ['Content-Type'=>'application/json']; + $result = $this->client->put($url, compact('headers','body')); + break; + default: + $result = null; + break; + } + + if ($result->getStatusCode() == 200 || $result->getStatusCode() == 201) { + return array('status'=>true, 'message'=>json_decode($result->getBody())); + } + return array('status'=>false, 'message'=>json_decode($result->getBody())); + + } + + + /** + * Get all registered users + * + * @return json|false Json with data or error, or False when something went fully wrong + */ + public function getUsers() + { + $endpoint = '/users'; + return $this->callOpenFireApi('get',$endpoint); + } + + + /** + * Get information for a specified user + * + * @return json|false Json with data or error, or False when something went fully wrong + */ + public function getUser($username) + { + $endpoint = '/users/'.$username; + return $this->callOpenFireApi('get', $endpoint); + } + + + /** + * Creates a new OpenFire user + * + * @param string $username Username + * @param string $password Password + * @param string|false $name Name (Optional) + * @param string|false $email Email (Optional) + * @param string[]|false $groups Groups (Optional) + * @return json|false Json with data or error, or False when something went fully wrong + */ + public function addUser($username, $password, $name=false, $email=false, $groups=false) + { + $endpoint = '/users'; + return $this->callOpenFireApi('post', $endpoint, compact('username', 'password','name','email', 'groups')); + } + + + /** + * Deletes an OpenFire user + * + * @param string $username Username + * @return json|false Json with data or error, or False when something went fully wrong + */ + public function deleteUser($username) + { + $endpoint = '/users/'.$username; + return $this->callOpenFireApi('delete', $endpoint); + } + + /** + * Updates an OpenFire user + * + * @param string $username Username + * @param string|false $password Password (Optional) + * @param string|false $name Name (Optional) + * @param string|false $email Email (Optional) + * @param string[]|false $groups Groups (Optional) + * @return json|false Json with data or error, or False when something went fully wrong + */ + public function updateUser($username, $password, $name=false, $email=false, $groups=false) + { + $endpoint = '/users/'.$username; + return $this->callOpenFireApi('put', $endpoint, compact('username', 'password','name','email', 'groups')); + } + + /** + * locks/Disables an OpenFire user + * + * @param string $username Username + * @return json|false Json with data or error, or False when something went fully wrong + */ + public function lockoutUser($username) + { + $endpoint = '/lockouts/'.$username; + return $this->callOpenFireApi('post', $endpoint); + } + + + /** + * unlocks an OpenFire user + * + * @param string $username Username + * @return json|false Json with data or error, or False when something went fully wrong + */ + public function unlockUser($username) + { + $endpoint = '/lockouts/'.$username; + return $this->callOpenFireApi('delete', $endpoint); + } + + + /** + * Adds to this OpenFire user's roster + * + * @param string $username Username + * @param string $jid JID + * @param string|false $name Name (Optional) + * @param int|false $subscription Subscription (Optional) + * @return json|false Json with data or error, or False when something went fully wrong + */ + public function addToRoster($username, $jid, $name=false, $subscription=false) + { + $endpoint = '/users/'.$username.'/roster'; + return $this->callOpenFireApi('post', $endpoint, compact('jid','name','subscription')); + } + + + /** + * Removes from this OpenFire user's roster + * + * @param string $username Username + * @param string $jid JID + * @return json|false Json with data or error, or False when something went fully wrong + */ + public function deleteFromRoster($username, $jid) + { + $endpoint = '/users/'.$username.'/roster/'.$jid; + return $this->callOpenFireApi('delete', $endpoint, $jid); + } + + /** + * Updates this OpenFire user's roster + * + * @param string $username Username + * @param string $jid JID + * @param string|false $nickname Nick Name (Optional) + * @param int|false $subscriptionType Subscription (Optional) + * @return json|false Json with data or error, or False when something went fully wrong + */ + public function updateRoster($username, $jid, $nickname=false, $subscriptionType=false) + { + $endpoint = '/users/'.$username.'/roster/'.$jid; + return $this->callOpenFireApi('put', $endpoint, $jid, compact('jid','username','subscriptionType')); + } + + /** + * Get all groups + * + * @return json|false Json with data or error, or False when something went fully wrong + */ + public function getGroups() + { + $endpoint = '/groups'; + return $this->callOpenFireApi('get', $endpoint); + } + + /** + * Retrieve a group + * + * @param string $name Name of group + * @return json|false Json with data or error, or False when something went fully wrong + */ + public function getGroup($name) + { + $endpoint = '/groups/'.$name; + return $this->callOpenFireApi('get', $endpoint); + } + + /** + * Create a group + * + * @param string $name Name of the group + * @param string $description Some description of the group + * + * @return json|false Json with data or error, or False when something went fully wrong + */ + public function createGroup($name, $description = false) + { + $endpoint = '/groups/'; + return $this->callOpenFireApi('post', $endpoint, compact('name','description')); + } + + /** + * Delete a group + * + * @param string $name Name of the Group to delete + * @return json|false Json with data or error, or False when something went fully wrong + */ + public function deleteGroup($name) + { + $endpoint = '/groups/'.$name; + return $this->callOpenFireApi('delete', $endpoint); + } + + /** + * Update a group (description) + * + * @param string $name Name of group + * @param string $description Some description of the group + * + */ + public function updateGroup($name, $description) + { + $endpoint = '/groups/'.$name; + return $this->callOpenFireApi('put', $endpoint, compact('name','description')); + } + + /** + * Gell all active sessions + * + * @return json|false Json with data or error, or False when something went fully wrong + */ + public function getSessions() + { + $endpoint = '/sessions'; + return $this->callOpenFireApi('get', $endpoint); + } +} \ No newline at end of file