Advertisement
bagnz0r

EmailServices.php

Nov 2nd, 2011
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.91 KB | None | 0 0
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2.  
  3. class EmailService {
  4.    
  5.     /**
  6.      * Consumer key
  7.      */
  8.     protected $consumer_key;
  9.    
  10.     /**
  11.      * Consumer secret
  12.      */
  13.     protected $consumer_secret;
  14.    
  15.     /**
  16.      * Token secret
  17.      */
  18.     protected $token_secret;
  19.    
  20.     /**
  21.      * Request token
  22.      */
  23.     protected $request_token;
  24.    
  25.     /**
  26.      * Token verifier
  27.      */
  28.     protected $verifier;
  29.    
  30.     /**
  31.      * Configuration
  32.      */
  33.     protected $config;
  34.    
  35.     /**
  36.      * Callback
  37.      */
  38.     protected $callback;
  39.    
  40.     /**
  41.      * Authentication flow
  42.      */
  43.     public $auth_flow = 'oauth';
  44.    
  45.     /**
  46.      * SSL peer validation toggle
  47.      */
  48.     public static $disable_ssl_peer_validation = true;
  49.    
  50.     /**
  51.      * Constructing the object
  52.      */
  53.     public function __construct()
  54.     {      
  55.         // Disabling peer validation
  56.         if (EmailService::$disable_ssl_peer_validation)
  57.             Remote::$default_options = array(
  58.                 CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; Kohana v3.0 +http://kohanaframework.org/)',
  59.                 CURLOPT_CONNECTTIMEOUT => 5,
  60.                 CURLOPT_TIMEOUT => 5,
  61.                 CURLOPT_RETURNTRANSFER => true,
  62.                 CURLOPT_SSL_VERIFYPEER => false,
  63.         );
  64.     }
  65.    
  66.     /**
  67.      * Basic auth spawner
  68.      *
  69.      * @param array $credentials
  70.      * @return array
  71.      */
  72.     protected function basic_auth($credentials)
  73.     {
  74.         // Set auth method and credentials
  75.         return array(
  76.             CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
  77.             CURLOPT_USERPWD => $credentials['username'] . ':' . $credentials['password'],
  78.         );
  79.     }
  80.    
  81.     /**
  82.      * Creating a HTTP request
  83.      *
  84.      * @param string $url
  85.      * @param array|string $params
  86.      * @param string $method Either 'get' or 'post'
  87.      * @param array $options
  88.      * @throws Kohana_Exception
  89.      */
  90.     protected function request_create($url, $params = null, $method = 'post', $options = null)
  91.     {  
  92.         // In case method is get
  93.         if ($method == 'get')
  94.             // Build the URL
  95.             $url = $this->build_url($url, $params);
  96.         else
  97.         {
  98.             // Build data
  99.             if (is_array($params))
  100.                 $fields = http_build_query($params);
  101.             else
  102.                 $fields = $params;
  103.            
  104.             // Set options
  105.             $opt = array(
  106.                 CURLOPT_POST => 1,
  107.                 CURLOPT_POSTFIELDS => $fields,
  108.             );
  109.         }
  110.        
  111.         // Merge
  112.         if (!is_null($options) && isset($opt))
  113.             $options = array_merge($options, $opt);
  114.         else if (isset($opt))
  115.             $options = $opt;
  116.        
  117.         // Perform a request
  118.         if (!$result = Remote::get($url, $options))
  119.             throw new Kohana_Exception('Couldn\'t perform a request with URL: ' . $url);
  120.            
  121.         return $result;
  122.     }
  123.    
  124.     /**
  125.      * Build an API URL with params provided
  126.      *
  127.      * @param string $url
  128.      * @param array $params
  129.      * @return string
  130.      */
  131.     protected function build_url($url, $params = null)
  132.     {
  133.         // If there are any params
  134.         if (!is_null($params))
  135.         {
  136.             // Get array keys
  137.             $keys = array_keys($params);
  138.            
  139.             // Index
  140.             $i = 0;
  141.            
  142.             // Go through params
  143.             foreach ($params as $key => $value)
  144.             {
  145.                 // If the param is not array, just create URL
  146.                 if (!is_array($value))
  147.                 {
  148.                     // If param is first, then use '?'
  149.                     if ($i == 0)
  150.                         $url .= '?' . $key . '=' . $value;
  151.                     else
  152.                         $url .= '&' . $key . '=' . $value;
  153.                 }
  154.                 else
  155.                 {
  156.                     // Beginning of the URL
  157.                     // If item is first, use '?'
  158.                     if ($i == 0)
  159.                         $url .= '?' . $key . '=';
  160.                     else
  161.                         $url .= '&' . $key . '=';
  162.                    
  163.                     // Add comma separated string of params
  164.                     $url .= $this->comma_separate_str($value);
  165.                 }
  166.                
  167.                 // Increment the index
  168.                 $i++;
  169.             }
  170.            
  171.             // Free some memory
  172.             unset($params);
  173.         }
  174.        
  175.         // Return the URL
  176.         return $url;
  177.     }
  178.    
  179.     /**
  180.      * Parsing HTTP fields from string
  181.      *
  182.      * @param string $fields
  183.      * @return array
  184.      */
  185.     protected function parse_http_fields($fields)
  186.     {
  187.         // Split items
  188.         $fields = explode('&', $fields);
  189.        
  190.         // Returning array
  191.         $items = array();
  192.        
  193.         // Parse items
  194.         foreach ($fields as $field)
  195.         {
  196.             // Split
  197.             $item = explode('=', $field);
  198.            
  199.             // Push
  200.             $items[$item[0]] = $item[1];
  201.         }
  202.        
  203.         return $items;
  204.     }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement