Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php defined('SYSPATH') or die('No direct script access.');
- class ConstantContact extends EmailService {
- /**
- * Auth flow
- */
- public $auth_flow = 'basic';
- /**
- * API
- */
- protected $api = 'https://api.constantcontact.com/ws/customers/';
- /**
- * Constructing the object
- *
- * @throws Kohana_Exception
- */
- public function __construct()
- {
- // Call the parent
- parent::__construct();
- // Load config
- $config = Kohana::config('emailservices.constantcontact');
- // 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'];
- }
- /**
- * Getting lists of contacts
- *
- * @param serialized_string $credentials
- * @return array
- */
- public function get_contact_lists($credentials)
- {
- // Make request
- $response = $this->make_request('lists', $credentials);
- // Empty array
- $result = array();
- // Go through entire response to build array
- foreach ($response->entry as $entry)
- {
- $result[(string)$entry->content->ContactList->attributes()->id] = 'constant contact/' . (string)$entry->content->ContactList->Name;
- }
- return $result;
- }
- public function get_emails($credentials)
- {
- // Make request
- $response = $this->make_request('settings/emailaddresses', $credentials);
- // Emtpy array
- $result = array();
- // Go through entire response to build array
- foreach ($response->entry as $entry)
- {
- $result[(string)$entry->id] = array(
- 'title' => (string)$entry->title,
- 'link' => (string)$entry->link->attributes()->href,
- 'email' => (string)$entry->content->Email->EmailAddress,
- 'status' => (string)$entry->content->Email->Status,
- );
- }
- return $result;
- }
- /**
- * Getting campaigns
- *
- * @param serialized_string $credentials
- * @return array
- */
- public function get_campaigns($credentials)
- {
- // Make request
- $response = $this->make_request('campaigns', $credentials);
- // Empty array
- $result = array();
- // Go through entire response to build array
- foreach ($response->entry as $entry)
- {
- $result[(string)$entry->id] = array(
- 'link' => (string)$entry->link->attributes(),
- 'title' => (string)$entry->title,
- 'updated' => (string)$entry->updated,
- 'author' => (string)$entry->author->name,
- 'name' => (string)$entry->content->Campaign->Name,
- 'status' => (string)$entry->content->Campaign->Status,
- 'date' => (string)$entry->content->Campaign->Date,
- );
- }
- return $result;
- }
- /**
- * Creating an email campaign
- *
- * @param array $data Model_Ads()->get() array with email
- * @param array $emails Array of emails retrieved by get_emails() method
- * @param array $lists Array of lists retrieved by get_contact_lists() method
- * @param serialized_string $credentials
- */
- public function create_campaign($data, $emails, $lists, $credentials)
- {
- // Status of campaign
- $status = 'Draft';
- // First the header
- $options = array(
- CURLOPT_HTTPHEADER => array(
- 'Content-Type' => 'application/atom+xml',
- ),
- );
- // Unserialize credentials for use with building data
- $unserialized = unserialize($credentials);
- // Get the RFC3339 timestamp
- $timestamp = date(DateTime::RFC3339);
- // Prepare contacts lists data
- $list_data = array();
- $list_data = $this->prepare_contact_lists_data($lists, $list_data);
- // Prepare email data
- $email_data = array();
- list($email, $email_data) = $this->prepare_email_data($emails, $email_data);
- // Spawn new AtomBuilder to create data for this request
- $atom_builder = new AtomBuilder(array(
- // The one and only entry
- array(
- // Request basics required by atom specification
- 'link' => array(
- 'href' => '/ws/customers/' . $unserialized['username'] . '/campaigns',
- 'rel' => 'edit',
- ),
- 'id' => array(
- '@field' => $this->build_ws_url('campaigns', $unserialized, false),
- ),
- 'title' => array(
- 'type' => 'text',
- '@field' => $data['title'],
- ),
- 'updated' => array(
- '@field' => $timestamp,
- ),
- 'author' => array(
- '@sub:name' => array(
- '@field' => 'AdSwaps',
- ),
- ),
- // This is content of the campaign
- 'content' => array(
- 'type' => 'application/vnd.ctct+xml',
- '@sub:Campaign' => array(
- 'xmlns' => 'http://ws.constantcontact.com/ns/1.0/',
- 'id' => $this->build_ws_url('campaigns', $unserialized, false),
- // Campaign information
- '@sub:Name' => array(
- '@field' => $data['title'],
- ),
- '@sub:Status' => array(
- '@field' => $status,
- ),
- '@sub:Date' => array(
- '@field' => $timestamp,
- ),
- // Message meta
- '@sub:Subject' => array(
- '@field' => $data['email_subject'],
- ),
- '@sub:FromName' => array(
- '@field' => $email,
- ),
- '@sub:ViewAsWebpage' => array(
- '@field' => 'No',
- ),
- '@sub:ViewAsWebpageLinkText' => array(
- '@field' => '',
- ),
- '@sub:ViewAsWebpageText' => array(
- '@field' => '',
- ),
- // We do not need a permission reminder
- '@sub:PermissionReminder' => array(
- '@field' => 'NO',
- ),
- '@sub:PermissionReminderText' => array(
- '@field' => '',
- ),
- // Message greeting
- '@sub:GreetingSalutation' => array(
- '@field' => 'Dear',
- ),
- '@sub:GreetingName' => array(
- '@field' => 'FirstName',
- ),
- '@sub:GreetingString' => array(
- '@field' => 'Greetings!',
- ),
- // Do not include forward email
- '@sub:IncludeForwardEmail' => array(
- '@field' => 'NO',
- ),
- '@sub:ForwardEmailLinkText' => array(
- '@field' => '',
- ),
- // Do not include subscribe link
- '@sub:IncludeSubscribeLink' => array(
- '@field' => 'NO',
- ),
- '@sub:SubscribeLinkText' => array(
- '@field' => '',
- ),
- // The email contents
- '@sub:EmailContentFormat' => array(
- '@field' => 'HTML',
- ),
- '@sub:EmailContent' => array(
- '@field' => htmlentities($data['email_contents']),
- ),
- '@sub:EmailTextContent' => array(
- '@field' => htmlentities('<Text>' . strip_tags($data['email_contents']) . '</Text>'),
- ),
- '@sub:StyleSheet' => array(
- '@field' => '',
- ),
- '@sub:ContactLists' => $list_data,
- '@sub:FromEmail' => $email_data,
- ),
- ),
- 'source' => array(
- '@sub:id' => array(
- '@field' => $this->build_ws_url('campaigns', $credentials, false),
- ),
- '@sub:title' => array(
- 'type' => 'text',
- '@field' => 'Campaigns',
- ),
- '@sub:link' => array(
- 'href' => 'campaigns',
- 'rel' => 'self',
- ),
- '@sub:author' => array(
- '@sub:name' => array(
- '@field' => $unserialized['username'],
- ),
- ),
- '@sub:updated' => array(
- '@field' => $timestamp,
- ),
- ),
- ),
- ));
- // Free useful memory
- unset($unserialized);
- // Make the request
- try {
- $result = $this->make_request('campaigns', $credentials, $atom_builder->get(false), 'post', $options);
- }
- // Catch the exception
- catch (Exception $e)
- {
- Kohana::$log->add(Kohana::ERROR, $e->getMessage());
- return $e->getMessage();
- }
- return $result;
- }
- /**
- * 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', 'constantcontact'))
- return false;
- }
- // Catch the exception
- catch (Exception $e)
- {
- Kohana::$log->add(Kohana::ERROR, $e->getMessage());
- return false;
- }
- return true;
- }
- /**
- * Making web service request
- *
- * @param string $resource
- * @param serialized_string $credentials
- * @param array $params
- * @param string $method Can be either 'get' or 'post'
- * @return SimpleXMLElement
- */
- 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 new SimpleXMLElement($result);
- }
- /**
- * Preparing web service request
- * @param array $credentials
- * @param bool $decode
- * @return array
- */
- protected function prepare_ws_request($resource, $credentials, $decode = false)
- {
- // Decode credentials
- if ($decode)
- $credentials = $this->decode_credentials($credentials);
- // Build URL
- $url = $this->build_ws_url($resource, $credentials);
- // Join credentials
- $credentials_joined = $this->join_key($credentials);
- // Prepare basic auth
- $options = $this->basic_auth($credentials_joined);
- // We don't need joined credentials anymore
- unset($credentials_joined);
- // Return stuff
- return array(
- $credentials,
- $url,
- $options,
- );
- }
- /**
- * Encoding credentials
- *
- * @param array $credentials
- * @return serialized_string
- */
- protected function encode_credentials($credentials)
- {
- // Encode password
- $credentials['password'] = base64_encode($credentials['password']);
- // Serialize
- return serialize($credentials);
- }
- /**
- * Decoding credentials
- *
- * @param serialized_string $credentials
- * @return array
- */
- protected function decode_credentials($credentials)
- {
- // Unserialize
- $credentials = unserialize($credentials);
- // Decode password
- $credentials['password'] = base64_decode($credentials['password']);
- return $credentials;
- }
- /**
- * Joining credentials with consumer key
- *
- * @param array $credentials
- * @return array
- */
- protected function join_key($credentials)
- {
- $credentials['username'] = $this->consumer_key . '%' . $credentials['username'];
- return $credentials;
- }
- /**
- * Preparing list data for campaign creation
- *
- * @param array $lists
- * @param array $list_data
- */
- protected function prepare_contact_lists_data($lists, $list_data)
- {
- foreach ($lists as $id => $name)
- {
- $list_data = array_merge($list_data, array(
- '@sub:ContactList' => array(
- 'id' => $id,
- '@sub:link' => array(
- 'xmlns' => 'http://www.w3.org/2005/Atom',
- 'href' => str_replace('http://api.constantcontact.com', '', $id),
- 'rel' => 'self',
- ),
- ),
- ));
- }
- return $list_data;
- }
- /**
- * Preparing email data for campaign creation
- *
- * @param array $emails
- * @param array $email_data
- */
- protected function prepare_email_data($emails, $email_data)
- {
- foreach ($emails as $id => $data)
- {
- $email_data = array_merge($email_data, array(
- '@sub:Email' => array(
- 'id' => $id,
- '@sub:link' => array(
- 'xmlns' => 'http://www.w3.org/2005/Atom',
- 'href' => $data['link'],
- 'rel' => 'self',
- ),
- ),
- '@sub:EmailAddress' => array(
- '@field' => $data['email'],
- )
- ));
- $email = $data['email'];
- }
- return array(
- $email,
- $email_data,
- );
- }
- /**
- * 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;
- }
- }
Add Comment
Please, Sign In to add comment