Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php defined('SYSPATH') or die('No direct script access.');
- class EmailService {
- /**
- * Consumer key
- */
- protected $consumer_key;
- /**
- * Consumer secret
- */
- protected $consumer_secret;
- /**
- * Token secret
- */
- protected $token_secret;
- /**
- * Request token
- */
- protected $request_token;
- /**
- * Token verifier
- */
- protected $verifier;
- /**
- * Configuration
- */
- protected $config;
- /**
- * Callback
- */
- protected $callback;
- /**
- * Authentication flow
- */
- public $auth_flow = 'oauth';
- /**
- * SSL peer validation toggle
- */
- public static $disable_ssl_peer_validation = true;
- /**
- * Constructing the object
- */
- public function __construct()
- {
- // Disabling peer validation
- if (EmailService::$disable_ssl_peer_validation)
- Remote::$default_options = array(
- CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; Kohana v3.0 +http://kohanaframework.org/)',
- CURLOPT_CONNECTTIMEOUT => 5,
- CURLOPT_TIMEOUT => 5,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_SSL_VERIFYPEER => false,
- );
- }
- /**
- * Basic auth spawner
- *
- * @param array $credentials
- * @return array
- */
- protected function basic_auth($credentials)
- {
- // Set auth method and credentials
- return array(
- CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
- CURLOPT_USERPWD => $credentials['username'] . ':' . $credentials['password'],
- );
- }
- /**
- * Creating a HTTP request
- *
- * @param string $url
- * @param array|string $params
- * @param string $method Either 'get' or 'post'
- * @param array $options
- * @throws Kohana_Exception
- */
- protected function request_create($url, $params = null, $method = 'post', $options = null)
- {
- // In case method is get
- if ($method == 'get')
- // Build the URL
- $url = $this->build_url($url, $params);
- else
- {
- // Build data
- if (is_array($params))
- $fields = http_build_query($params);
- else
- $fields = $params;
- // Set options
- $opt = array(
- CURLOPT_POST => 1,
- CURLOPT_POSTFIELDS => $fields,
- );
- }
- // Merge
- if (!is_null($options) && isset($opt))
- $options = array_merge($options, $opt);
- else if (isset($opt))
- $options = $opt;
- // Perform a request
- if (!$result = Remote::get($url, $options))
- throw new Kohana_Exception('Couldn\'t perform a request with URL: ' . $url);
- return $result;
- }
- /**
- * Build an API URL with params provided
- *
- * @param string $url
- * @param array $params
- * @return string
- */
- protected function build_url($url, $params = null)
- {
- // If there are any params
- if (!is_null($params))
- {
- // Get array keys
- $keys = array_keys($params);
- // Index
- $i = 0;
- // Go through params
- foreach ($params as $key => $value)
- {
- // If the param is not array, just create URL
- if (!is_array($value))
- {
- // If param is first, then use '?'
- if ($i == 0)
- $url .= '?' . $key . '=' . $value;
- else
- $url .= '&' . $key . '=' . $value;
- }
- else
- {
- // Beginning of the URL
- // If item is first, use '?'
- if ($i == 0)
- $url .= '?' . $key . '=';
- else
- $url .= '&' . $key . '=';
- // Add comma separated string of params
- $url .= $this->comma_separate_str($value);
- }
- // Increment the index
- $i++;
- }
- // Free some memory
- unset($params);
- }
- // Return the URL
- return $url;
- }
- /**
- * Parsing HTTP fields from string
- *
- * @param string $fields
- * @return array
- */
- protected function parse_http_fields($fields)
- {
- // Split items
- $fields = explode('&', $fields);
- // Returning array
- $items = array();
- // Parse items
- foreach ($fields as $field)
- {
- // Split
- $item = explode('=', $field);
- // Push
- $items[$item[0]] = $item[1];
- }
- return $items;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement