Advertisement
bagnz0r

Untitled

Nov 3rd, 2011
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.49 KB | None | 0 0
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2.  
  3. class Icontact extends EmailService {
  4.    
  5.     /**
  6.      * Auth flow
  7.      */
  8.     public $auth_flow = 'headers';
  9.    
  10.     /**
  11.      * API
  12.      */
  13.     protected $api = 'https://app.sandbox.icontact.com/icp'; // FIXME No API location here?
  14.    
  15.     /**
  16.     * Constructing the object
  17.     *
  18.     * @throws Kohana_Exception
  19.     */
  20.     public function __construct()
  21.     {
  22.         // Call the parent
  23.         parent::__construct();
  24.    
  25.         // Load config
  26.         $config = Kohana::config('emailservices.icontact');
  27.    
  28.         // Check vars
  29.         if (!isset($config['consumer_key']))
  30.             throw new Kohana_Exception('No consumer key!');
  31.    
  32.         if (!isset($config['callback']))
  33.             throw new Kohana_Exception('No callback URL defined!');
  34.            
  35.         // Consumer key
  36.         $this->consumer_key = $config['consumer_key'];
  37.    
  38.         // Callback
  39.         $this->callback = $config['callback'];
  40.     }
  41.    
  42.     /**
  43.      * Checking and saving credentials
  44.      * @param array $credentials
  45.      * @return TRUE|FALSE
  46.      */
  47.     public function check_and_save_credentials($credentials)
  48.     {
  49.         // Prepare request
  50.         list($credentials, $url, $options) = $this->prepare_ws_request('contacts', $credentials);
  51.        
  52.         // Create request
  53.         try {
  54.             if ($this->request_create($url, null, 'get', $options))
  55.                 if (!TokenDB::save($this->encode_credentials($credentials), 'credentials', 'icontact'))
  56.                     return false;
  57.         }
  58.            
  59.         // Catch the exception
  60.         catch (Exception $e)
  61.         {
  62.             Kohana::$log->add(Kohana::ERROR, $e->getMessage());
  63.             return false;
  64.         }
  65.        
  66.         return true;
  67.     }
  68.    
  69.     public function get_contact_lists()
  70.     {
  71.        
  72.     }
  73.    
  74.     /**
  75.     * Preparing web service request
  76.     *
  77.     * @param array $credentials
  78.     * @return array
  79.     */
  80.     protected function prepare_ws_request($resource, $credentials)
  81.     {
  82.         // Decode the credentials
  83.         $credentials = unserialize($credentials);
  84.        
  85.         // Build the url
  86.         $this->build_ws_url($resource, $credentials);
  87.        
  88.         // Set the headers for authentication
  89.         $options = array(
  90.             CURLOPT_HTTPHEADER => array(
  91.                 'Accept' => 'application/json',
  92.                 'Content-Type' => 'application/json',
  93.                 'API-Version' => '2.2',
  94.                 'API-AppId' => $this->consumer_key,
  95.                 'API-Username' => $credentials['username'],
  96.                 'API-Password' => $credentials['password'],
  97.             ),
  98.         );
  99.        
  100.         // Return stuff
  101.         return array(
  102.             $credentials,
  103.             $url,
  104.             $options,
  105.         );
  106.     }
  107.    
  108.     /**
  109.     * Making web service request
  110.     *
  111.     * @param string $resource
  112.     * @param serialized_string $credentials
  113.     * @param array $params
  114.     * @param string $method Can be either 'get' or 'post'
  115.     * @return string
  116.     */
  117.     public function make_request($resource, $credentials, $params = null, $method = 'get', $opt = null)
  118.     {
  119.         // Prepare request
  120.         list($credentials, $url, $options) = $this->prepare_ws_request($resource, $credentials, true);
  121.    
  122.         // Join options
  123.         if (!is_null($opt))
  124.             $options = array_merge($options, $opt);
  125.    
  126.         // Make the request
  127.         try {
  128.             $result = $this->request_create($url, $params, $method, $options);
  129.         }
  130.    
  131.         // Catch the exception
  132.         catch (Exception $e)
  133.         {
  134.             Kohana::$log->add(Kohana::ERROR, $e->getMessage());
  135.             return $e->getMessage();
  136.         }
  137.    
  138.         // Decode and return result
  139.         return json_decode($result, true);
  140.     }
  141.    
  142.     /**
  143.     * Building web service URL
  144.     *
  145.     * @param string $resource
  146.     * @param array $credentials
  147.     * @return string
  148.     */
  149.     protected function build_ws_url($resource, $credentials, $https = true)
  150.     {
  151.         $api = $this->api;
  152.         $https ? : $api = str_replace('https://', 'http://', $api);
  153.    
  154.         return $api . $credentials['username'] .'/' . $resource;
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement