Skip to content
This repository has been archived by the owner on Sep 28, 2020. It is now read-only.

Commit

Permalink
Added src files
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-godfrey committed Apr 5, 2018
1 parent e7267bc commit b4a4855
Show file tree
Hide file tree
Showing 6 changed files with 355 additions and 18 deletions.
21 changes: 3 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Vtiger (Laravel 5 Package)
# Vtiger (Laravel 5 Package)
Use the Vtiger webservice (REST) API from within Laravel for operations Create, Retrieve and Update.

See [Third Party App Integration (REST APIs)](http://community.vtiger.com/help/vtigercrm/developers/third-party-app-integration.html)
Expand All @@ -17,7 +17,7 @@ See [Third Party App Integration (REST APIs)](http://community.vtiger.com/help/v
2. Then in your config/app.php add the following to the providers array:
```
Clystnet\Vtiger\VtigerServiceProvider::class,
Clystnet\Vtiger\VtigerServiceProvider::class,
```
3. In the same config/app.php add the following to the aliases array:
Expand Down Expand Up @@ -123,19 +123,4 @@ Please report any issue you find in the issues page. Pull requests are more than
## License
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "clystnet/vtiger",
"description": "Package to run Vtiger Webservice API",
"type": "laravel",
"require": {},
"license": "MIT",
"authors": [
{
"name": "Adam Godfrey",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"extra": {
"laravel": {
"providers": [
"Clystnet\\Vtiger\\VtigerServiceProvider"
],
"aliases": {
"Vtiger": "Clystnet\\Vtiger\\Facades\\Vtiger"
}
}
}
}
8 changes: 8 additions & 0 deletions src/Config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

// vTiger API constants
return [
'url' => 'path/to/vtiger/webservice',
'username' => '',
'accesskey' => '',
];
13 changes: 13 additions & 0 deletions src/Facades/Vtiger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Clystnet\Vtiger\Facades;

use Illuminate\Support\Facades\Facade;

class Vtiger extends Facade
{
protected static function getFacadeAccessor()
{
return 'clystnet-vtiger';
}
}
266 changes: 266 additions & 0 deletions src/Vtiger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
<?php
namespace Clystnet\Vtiger;

use Storage;
use Config;

class Vtiger
{
protected $url;
protected $username;
protected $accesskey;

public function __construct() {
// set the API url and username
$this->url = Config::get('vtiger.url');
$this->username = Config::get('vtiger.username');
$this->accesskey = Config::get('vtiger.accesskey');
}

protected function sessionid()
{
// session file exists
if(Storage::disk('local')->exists('session.json')) {
$json = json_decode(Storage::disk('local')->get('session.json'));

if($json->expireTime < time() || empty($json->token)) {
$updated = self::gettoken();

$json = (object) $updated;

Storage::disk('local')->put('session.json', json_encode($json));
}
}
else {
$updated = self::gettoken();

$json = (object) $updated;

Storage::disk('local')->put('session.json', json_encode($json));
}

$token = $json->token;

// Create unique key using combination of challengetoken and accesskey
$generatedkey = md5($token . $this->accesskey);

$post = array(
'operation' => 'login',
'username' => $this->username,
'accessKey' => $generatedkey
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

// decode the response
$login_result = json_decode($result);

// If api login failed
if($httpcode !== 200 || !$login_result->success) {
return json_encode(array(
'success' => false,
'message' => $login_result->error->message
));
}

// login ok so get sessionid
$sessionid = $login_result->result->sessionName;

return $sessionid;
}

protected function gettoken()
{
$bool = false;

do {
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $this->url . "?operation=getchallenge&username=" . $this->username);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_HEADER, false);

$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if(curl_error($ch)) {
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}

curl_close($ch);

// decode the response
$challenge = json_decode($result);

// If challenge failed
if($httpcode === 200 || $challenge->success) {
$bool = true;
}
}
while(!$bool);

$json = array(
'token' => $challenge->result->token,
'expireTime' => $challenge->result->expireTime
);

return $json;
}

protected function close($sessionid)
{
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $this->url . "?operation=logout&sessionName=" . $sessionid);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);

$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

// decode the response
$data = json_decode($result);

return $data;
}

public function query($query)
{
$sessionid = self::sessionid();

if(isset($sessionid->success)) {
return $sessionid->message;
}

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $this->url . "?operation=query&sessionName=" . $sessionid . "&query=" . urlencode($query));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);

$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if(curl_error($ch)) {
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}

curl_close($ch);

// decode the response
$data = json_decode($result);

self::close($sessionid);

return $data;
}

public function retrieve($id)
{
$sessionid = self::sessionid();

if(isset($sessionid->success)) {
return $sessionid->message;
}

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $this->url . "?operation=retrieve&sessionName=" . $sessionid . "&id=" . $id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);

$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

// decode the response
$data = json_decode($result);

self::close($sessionid);

return $data;
}

public function create($elem, $data)
{
$sessionid = self::sessionid();

if(isset($sessionid->success)) {
return $sessionid->message;
}

$post = array(
'operation' => 'create',
'sessionName' => $sessionid,
'element' => $data,
'elementType' => $elem
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

// decode the response
$data = json_decode($result);

self::close($sessionid);

return $data;
}

public function update($object)
{
$sessionid = self::sessionid();

if(isset($sessionid->success)) {
return $sessionid->message;
}

// send a request using a database query to get back any user with the email from the form POST request
$post = array(
'operation' => 'update',
'sessionName' => $sessionid,
'element' => json_encode($object),
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

// decode the response
$data = json_decode($result);

self::close($sessionid);

return $data;
}
}
41 changes: 41 additions & 0 deletions src/VtigerServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Clystnet\Vtiger;

use Illuminate\Support\ServiceProvider;

class VtigerServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$this->publishes([
__DIR__ . '/Config/config.php' => config_path('vtiger.php'),
], 'vtiger');

// use the vendor configuration file as fallback
$this->mergeConfigFrom(
__DIR__ . '/Config/config.php', 'vtiger'
);
}

/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->bind('clystnet-vtiger', function () {
return new Vtiger();
});

config([
'config/vtiger.php',
]);
}
}

0 comments on commit b4a4855

Please sign in to comment.