Skip to content

OO wrapper #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions OX3_Api_Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function __construct($uri, $email, $password, $consumer_key, $consumer_se
// Set the proxy['adapter'] if $proxy config was passed in
if (!empty($proxy)) {
$proxy['adapter'] = 'Zend_Http_Client_Adapter_Proxy';
$proxy['useragent'] = 'OX3-PHP-API-Client/1.0';
}

// Initilize the cookie jar, from the $cookieJarFile if present
Expand Down
134 changes: 134 additions & 0 deletions OX3_Api_Wrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

/**
* OO wrappers around a few OX objects, transparently handles v3 and v4.
*
* PHP version 5
*
* @package OX_API
* @author Harold Martin <[email protected]>
* @version 0.4
* @link https://github.com/openx/OX3-PHP-API-Client/tree/wrapper
* @see OX3_Api_Client.php
*/

require_once 'OX3_Api_Client.php';

class OX_API
{
protected $client;
protected $v;
protected $hash;
public function __construct($ox_api_config)
{
$sso = array(
'siteUrl' => $ox_api_config['sso'] . '/api/index/initiate',
'requestTokenUrl' => $ox_api_config['sso'] . '/api/index/initiate',
'accessTokenUrl' => $ox_api_config['sso'] . '/api/index/token',
'authorizeUrl' => $ox_api_config['sso'] . '/login/login',
'loginUrl' => $ox_api_config['sso'] . '/login/process',
);
if ($ox_api_config['version'] == 4) {
$version_path = '/ox/4.0';
$this->v = 4;
} else {
$version_path = '/ox/3.0';
$this->v = 3;
}
$this->hash = $ox_api_config['hash'];
$this->client = new OX3_API_Client($ox_api_config['uri'], $ox_api_config['email'], $ox_api_config['password'], $ox_api_config['key'], $ox_api_config['secret'], $ox_api_config['realm'], (dirname(__FILE__) . '/' . $ox_api_config['realm'] . '.txt'), $sso, '', $version_path);
}
public function get_hash() {
return $this->hash;
}
public function v1_to_uuid($obj_type, $obj_id) {
$TYPES_UUID_DICT = array(
'account' => 0xaccf,
'user' => 0xacc0,
'accountrelationship' => 0xacc1,
'paymenthistory' => 0xacc2,
'site' => 0xe000,
'sitesection' => 0xe001,
'adunit' => 0xe0ad,
'adunitgroup' => 0xe003,
'audiencesegment' => 0xe004,
'optimization' => 0xf001,
'adproduct' => 0xf002,
'conversiontag' => 0xceaf,
'creative' => 0xceae,
'competitiveexclusion'=> 0xcead,
'order' => 0xc001,
'lineitem' => 0xc002,
'ad' => 0xc0ad,
'report' => 0xa010,
);
$api_version = 0xfff1;
$clk_data = 0x8123;
return sprintf('%08x-%04x-%04x-%04x-%s', $obj_id, $TYPES_UUID_DICT[$obj_type], $api_version, $clk_data, substr($this->hash, -6));
}
public function adunit($id)
{
if ($this->v == 3) {
return json_decode($this->client->get(('/a/adunit/'. $id), array('overload' => 'medium'))->getBody(), true);
}
else if ($this->v == 4) {
return json_decode($this->client->get('/adunit/'. (is_numeric($id) ? $this->v1_to_uuid('adunit', $id) : $id))->getBody(), true)[0];
}
}
public function adunit_list($id)
{
if ($this->v == 3) {
return json_decode($this->client->get('/a/site/'. $id . '/listAdUnits')->getBody(), true);
}
else if ($this->v == 4) {
$res = $this->client->get('/adunit', array('site_uid' => (is_numeric($id) ? $this->v1_to_uuid('site', $id) : $id)));
return json_decode($res->getBody(), true)['objects'];
}
}
public function site($id)
{
if ($this->v == 3) {
return json_decode($this->client->get(('/a/site/'. $id), array('overload' => 'medium'))->getBody(), true);
}
else if ($this->v == 4) {
return json_decode($this->client->get('/site/'. (is_numeric($id) ? $this->v1_to_uuid('site', $id) : $id))->getBody(), true)[0];
}
}
public function site_list($id)
{
if ($this->v == 3) {
return json_decode($this->client->get('/a/account/' . $id . '/listSites')->getBody(), true);
}
else if ($this->v == 4) {
$res = $this->client->get('/site', array('account_uid' => (is_numeric($id) ? $this->v1_to_uuid('account', $id) : $id)));
return json_decode($res->getBody(), true)['objects'];
}
}
public function account($id)
{
if ($this->v == 3) {
return json_decode($this->client->get(('/a/account/'. $id), array('overload' => 'medium'))->getBody(), true);
}
else if ($this->v == 4) {
return json_decode($this->client->get('/account/'. (is_numeric($id) ? $this->v1_to_uuid('account', $id) : $id))->getBody(), true)[0];
}
}
public function publisher_list()
{
if ($this->v == 3) {
$accts = json_decode($this->client->get('/a/acl/account', array('overload' => 'medium'))->getBody(), true);
foreach($accts as $elementKey => $element) {
if ($element['account_type_id'] != 2 && $element['account_type_id'] != 8) {
unset($accts[$elementKey]);
}
}
return $accts;
}
else if ($this->v == 4) {
$res = $this->client->get('/account', array('type_full' => 'account.publisher'));
return json_decode($res->getBody(), true)['objects'];
}
}
}

?>
250 changes: 250 additions & 0 deletions Zend/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Cache
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Cache.php 23775 2011-03-01 17:25:24Z ralph $
*/


/**
* @package Zend_Cache
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
abstract class Zend_Cache
{

/**
* Standard frontends
*
* @var array
*/
public static $standardFrontends = array('Core', 'Output', 'Class', 'File', 'Function', 'Page');

/**
* Standard backends
*
* @var array
*/
public static $standardBackends = array('File', 'Sqlite', 'Memcached', 'Libmemcached', 'Apc', 'ZendPlatform',
'Xcache', 'TwoLevels', 'WinCache', 'ZendServer_Disk', 'ZendServer_ShMem');

/**
* Standard backends which implement the ExtendedInterface
*
* @var array
*/
public static $standardExtendedBackends = array('File', 'Apc', 'TwoLevels', 'Memcached', 'Libmemcached', 'Sqlite', 'WinCache');

/**
* Only for backward compatibility (may be removed in next major release)
*
* @var array
* @deprecated
*/
public static $availableFrontends = array('Core', 'Output', 'Class', 'File', 'Function', 'Page');

/**
* Only for backward compatibility (may be removed in next major release)
*
* @var array
* @deprecated
*/
public static $availableBackends = array('File', 'Sqlite', 'Memcached', 'Libmemcached', 'Apc', 'ZendPlatform', 'Xcache', 'WinCache', 'TwoLevels');

/**
* Consts for clean() method
*/
const CLEANING_MODE_ALL = 'all';
const CLEANING_MODE_OLD = 'old';
const CLEANING_MODE_MATCHING_TAG = 'matchingTag';
const CLEANING_MODE_NOT_MATCHING_TAG = 'notMatchingTag';
const CLEANING_MODE_MATCHING_ANY_TAG = 'matchingAnyTag';

/**
* Factory
*
* @param mixed $frontend frontend name (string) or Zend_Cache_Frontend_ object
* @param mixed $backend backend name (string) or Zend_Cache_Backend_ object
* @param array $frontendOptions associative array of options for the corresponding frontend constructor
* @param array $backendOptions associative array of options for the corresponding backend constructor
* @param boolean $customFrontendNaming if true, the frontend argument is used as a complete class name ; if false, the frontend argument is used as the end of "Zend_Cache_Frontend_[...]" class name
* @param boolean $customBackendNaming if true, the backend argument is used as a complete class name ; if false, the backend argument is used as the end of "Zend_Cache_Backend_[...]" class name
* @param boolean $autoload if true, there will no require_once for backend and frontend (useful only for custom backends/frontends)
* @throws Zend_Cache_Exception
* @return Zend_Cache_Core|Zend_Cache_Frontend
*/
public static function factory($frontend, $backend, $frontendOptions = array(), $backendOptions = array(), $customFrontendNaming = false, $customBackendNaming = false, $autoload = false)
{
if (is_string($backend)) {
$backendObject = self::_makeBackend($backend, $backendOptions, $customBackendNaming, $autoload);
} else {
if ((is_object($backend)) && (in_array('Zend_Cache_Backend_Interface', class_implements($backend)))) {
$backendObject = $backend;
} else {
self::throwException('backend must be a backend name (string) or an object which implements Zend_Cache_Backend_Interface');
}
}
if (is_string($frontend)) {
$frontendObject = self::_makeFrontend($frontend, $frontendOptions, $customFrontendNaming, $autoload);
} else {
if (is_object($frontend)) {
$frontendObject = $frontend;
} else {
self::throwException('frontend must be a frontend name (string) or an object');
}
}
$frontendObject->setBackend($backendObject);
return $frontendObject;
}

/**
* Backend Constructor
*
* @param string $backend
* @param array $backendOptions
* @param boolean $customBackendNaming
* @param boolean $autoload
* @return Zend_Cache_Backend
*/
public static function _makeBackend($backend, $backendOptions, $customBackendNaming = false, $autoload = false)
{
if (!$customBackendNaming) {
$backend = self::_normalizeName($backend);
}
if (in_array($backend, Zend_Cache::$standardBackends)) {
// we use a standard backend
$backendClass = 'Zend_Cache_Backend_' . $backend;
// security controls are explicit
require_once str_replace('_', DIRECTORY_SEPARATOR, $backendClass) . '.php';
} else {
// we use a custom backend
if (!preg_match('~^[\w]+$~D', $backend)) {
Zend_Cache::throwException("Invalid backend name [$backend]");
}
if (!$customBackendNaming) {
// we use this boolean to avoid an API break
$backendClass = 'Zend_Cache_Backend_' . $backend;
} else {
$backendClass = $backend;
}
if (!$autoload) {
$file = str_replace('_', DIRECTORY_SEPARATOR, $backendClass) . '.php';
if (!(self::_isReadable($file))) {
self::throwException("file $file not found in include_path");
}
require_once $file;
}
}
return new $backendClass($backendOptions);
}

/**
* Frontend Constructor
*
* @param string $frontend
* @param array $frontendOptions
* @param boolean $customFrontendNaming
* @param boolean $autoload
* @return Zend_Cache_Core|Zend_Cache_Frontend
*/
public static function _makeFrontend($frontend, $frontendOptions = array(), $customFrontendNaming = false, $autoload = false)
{
if (!$customFrontendNaming) {
$frontend = self::_normalizeName($frontend);
}
if (in_array($frontend, self::$standardFrontends)) {
// we use a standard frontend
// For perfs reasons, with frontend == 'Core', we can interact with the Core itself
$frontendClass = 'Zend_Cache_' . ($frontend != 'Core' ? 'Frontend_' : '') . $frontend;
// security controls are explicit
require_once str_replace('_', DIRECTORY_SEPARATOR, $frontendClass) . '.php';
} else {
// we use a custom frontend
if (!preg_match('~^[\w]+$~D', $frontend)) {
Zend_Cache::throwException("Invalid frontend name [$frontend]");
}
if (!$customFrontendNaming) {
// we use this boolean to avoid an API break
$frontendClass = 'Zend_Cache_Frontend_' . $frontend;
} else {
$frontendClass = $frontend;
}
if (!$autoload) {
$file = str_replace('_', DIRECTORY_SEPARATOR, $frontendClass) . '.php';
if (!(self::_isReadable($file))) {
self::throwException("file $file not found in include_path");
}
require_once $file;
}
}
return new $frontendClass($frontendOptions);
}

/**
* Throw an exception
*
* Note : for perf reasons, the "load" of Zend/Cache/Exception is dynamic
* @param string $msg Message for the exception
* @throws Zend_Cache_Exception
*/
public static function throwException($msg, Exception $e = null)
{
// For perfs reasons, we use this dynamic inclusion
require_once 'Zend/Cache/Exception.php';
throw new Zend_Cache_Exception($msg, 0, $e);
}

/**
* Normalize frontend and backend names to allow multiple words TitleCased
*
* @param string $name Name to normalize
* @return string
*/
protected static function _normalizeName($name)
{
$name = ucfirst(strtolower($name));
$name = str_replace(array('-', '_', '.'), ' ', $name);
$name = ucwords($name);
$name = str_replace(' ', '', $name);
if (stripos($name, 'ZendServer') === 0) {
$name = 'ZendServer_' . substr($name, strlen('ZendServer'));
}

return $name;
}

/**
* Returns TRUE if the $filename is readable, or FALSE otherwise.
* This function uses the PHP include_path, where PHP's is_readable()
* does not.
*
* Note : this method comes from Zend_Loader (see #ZF-2891 for details)
*
* @param string $filename
* @return boolean
*/
private static function _isReadable($filename)
{
if (!$fh = @fopen($filename, 'r', true)) {
return false;
}
@fclose($fh);
return true;
}

}
Loading