Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php defined('SYSPATH') or die('No direct script access.');
- class Icontact extends EmailService {
- /**
- * Auth flow
- */
- public $auth_flow = 'headers';
- /**
- * API
- */
- protected $api = 'https://app.sandbox.icontact.com/icp'; // FIXME No API location here?
- /**
- * Constructing the object
- *
- * @throws Kohana_Exception
- */
- public function __construct()
- {
- // Call the parent
- parent::__construct();
- // Load config
- $config = Kohana::config('emailservices.icontact');
- // Check vars
- if (!isset($config['consumer_key']))
- throw new Kohana_Exception('No consumer key!');
- if (!isset($config['callback']))
- throw new Kohana_Exception('No callback URL defined!');
- // Consumer key
- $this->consumer_key = $config['consumer_key'];
- // Callback
- $this->callback = $config['callback'];
- }
- /**
- * Checking and saving credentials
- * @param array $credentials
- * @return TRUE|FALSE
- */
- public function check_and_save_credentials($credentials)
- {
- // Prepare request
- list($credentials, $url, $options) = $this->prepare_ws_request('contacts', $credentials);
- // Create request
- try {
- if ($this->request_create($url, null, 'get', $options))
- if (!TokenDB::save($this->encode_credentials($credentials), 'credentials', 'icontact'))
- return false;
- }
- // Catch the exception
- catch (Exception $e)
- {
- Kohana::$log->add(Kohana::ERROR, $e->getMessage());
- return false;
- }
- return true;
- }
- public function get_contact_lists()
- {
- }
- /**
- * Preparing web service request
- *
- * @param array $credentials
- * @return array
- */
- protected function prepare_ws_request($resource, $credentials)
- {
- // Decode the credentials
- $credentials = unserialize($credentials);
- // Build the url
- $this->build_ws_url($resource, $credentials);
- // Set the headers for authentication
- $options = array(
- CURLOPT_HTTPHEADER => array(
- 'Accept' => 'application/json',
- 'Content-Type' => 'application/json',
- 'API-Version' => '2.2',
- 'API-AppId' => $this->consumer_key,
- 'API-Username' => $credentials['username'],
- 'API-Password' => $credentials['password'],
- ),
- );
- // Return stuff
- return array(
- $credentials,
- $url,
- $options,
- );
- }
- /**
- * Making web service request
- *
- * @param string $resource
- * @param serialized_string $credentials
- * @param array $params
- * @param string $method Can be either 'get' or 'post'
- * @return string
- */
- public function make_request($resource, $credentials, $params = null, $method = 'get', $opt = null)
- {
- // Prepare request
- list($credentials, $url, $options) = $this->prepare_ws_request($resource, $credentials, true);
- // Join options
- if (!is_null($opt))
- $options = array_merge($options, $opt);
- // Make the request
- try {
- $result = $this->request_create($url, $params, $method, $options);
- }
- // Catch the exception
- catch (Exception $e)
- {
- Kohana::$log->add(Kohana::ERROR, $e->getMessage());
- return $e->getMessage();
- }
- // Decode and return result
- return json_decode($result, true);
- }
- /**
- * Building web service URL
- *
- * @param string $resource
- * @param array $credentials
- * @return string
- */
- protected function build_ws_url($resource, $credentials, $https = true)
- {
- $api = $this->api;
- $https ? : $api = str_replace('https://', 'http://', $api);
- return $api . $credentials['username'] .'/' . $resource;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement