2012-04-28 17:28:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CLASS: Share Your Cart API
|
|
|
|
* AUTHOR: Barandi Solutions
|
|
|
|
* COUNTRY: Romania
|
|
|
|
* EMAIL: catalin.paun@barandisolutions.ro
|
|
|
|
* VERSION : 1.0
|
|
|
|
* DESCRIPTION: This class is used as a wrapper to communicate to the ShareYourCart API.
|
|
|
|
* * Copyright (C) 2011 Barandi Solutions
|
|
|
|
*/
|
|
|
|
|
|
|
|
if(!class_exists('ShareYourCartAPI',false)){
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
class ShareYourCartAPI {
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
protected $SHAREYOURCART_URL = "www.shareyourcart.com";
|
|
|
|
protected $SHAREYOURCART_SANDBOX_URL = "sandbox.shareyourcart.com";
|
|
|
|
protected $SHAREYOURCART_API;
|
|
|
|
protected $SHAREYOURCART_API_REGISTER;
|
|
|
|
protected $SHAREYOURCART_API_RECOVER;
|
|
|
|
protected $SHAREYOURCART_API_ACTIVATE;
|
|
|
|
protected $SHAREYOURCART_API_DEACTIVATE;
|
|
|
|
protected $SHAREYOURCART_API_CREATE;
|
|
|
|
protected $SHAREYOURCART_API_VALIDATE;
|
|
|
|
protected $SHAREYOURCART_API_STATUS;
|
2012-11-16 11:55:15 +00:00
|
|
|
protected $SHAREYOURCART_API_TRANSLATION;
|
2012-04-28 17:28:18 +00:00
|
|
|
protected $SHAREYOURCART_CONFIGURE;
|
|
|
|
protected $SHAREYOURCART_BUTTON_JS;
|
|
|
|
protected $SHAREYOURCART_BUTTON_URL;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* @param null
|
|
|
|
*/
|
|
|
|
function __construct() {
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
$is_live = true;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
//check if the current object has a secret key function
|
|
|
|
//this is for backward compatibility
|
|
|
|
if(method_exists ($this,'getSecretKey')){
|
|
|
|
$secretKey = $this->getSecretKey();
|
|
|
|
if(empty($secretKey)){
|
2012-11-16 11:55:15 +00:00
|
|
|
throw new Exception(SyC::t('sdk',"You must specify a valid secret key"));
|
2012-04-28 17:28:18 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
//check if the secret key is a sandbox one
|
|
|
|
if(strpos($secretKey,"sndbx_") === 0){
|
|
|
|
$this->SHAREYOURCART_URL = $this->SHAREYOURCART_SANDBOX_URL;
|
|
|
|
$is_live = false;
|
|
|
|
}
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
$this->SHAREYOURCART_API = (isset($_SERVER['HTTPS']) && !strcasecmp($_SERVER['HTTPS'],'on') ? 'https://' : 'http://') . $this->SHAREYOURCART_URL;
|
|
|
|
$this->SHAREYOURCART_REGISTER = $this->SHAREYOURCART_API.'/account/create';
|
|
|
|
$this->SHAREYOURCART_API_REGISTER = $this->SHAREYOURCART_API.'/account/create';
|
|
|
|
$this->SHAREYOURCART_API_RECOVER = $this->SHAREYOURCART_API.'/account/recover';
|
|
|
|
$this->SHAREYOURCART_API_ACTIVATE = $this->SHAREYOURCART_API.'/account/activate';
|
|
|
|
$this->SHAREYOURCART_API_DEACTIVATE = $this->SHAREYOURCART_API.'/account/deactivate';
|
|
|
|
$this->SHAREYOURCART_API_CREATE = $this->SHAREYOURCART_API.'/session/create';
|
|
|
|
$this->SHAREYOURCART_API_VALIDATE = $this->SHAREYOURCART_API.'/session/validate';
|
|
|
|
$this->SHAREYOURCART_API_STATUS = $this->SHAREYOURCART_API.'/sdk';
|
2012-11-16 11:55:15 +00:00
|
|
|
$this->SHAREYOURCART_API_TRANSLATION = $this->SHAREYOURCART_API.'/sdk/translation';
|
2012-11-27 16:22:47 +00:00
|
|
|
$this->SHAREYOURCART_CONFIGURE = $this->SHAREYOURCART_API.'/configure';
|
2012-04-28 17:28:18 +00:00
|
|
|
$this->SHAREYOURCART_BUTTON_JS = $this->SHAREYOURCART_API.'/js/'.($is_live ? 'button.js' : 'button_sandbox.js');
|
|
|
|
$this->SHAREYOURCART_BUTTON_URL = $this->SHAREYOURCART_API.'/button';
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
/**
|
|
|
|
* startSession
|
|
|
|
* @param array $params
|
2012-11-27 16:22:47 +00:00
|
|
|
* @return array $data
|
2012-04-28 17:28:18 +00:00
|
|
|
*/
|
|
|
|
public function startSession($params) {
|
|
|
|
//make sure the session is started
|
|
|
|
if(session_id() == '')
|
|
|
|
session_start();
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
$session = curl_init($this->SHAREYOURCART_API_CREATE);
|
|
|
|
|
|
|
|
// Tell curl to use HTTP POST
|
|
|
|
curl_setopt($session, CURLOPT_POST, true);
|
|
|
|
// Tell curl that this is the body of the POST
|
|
|
|
curl_setopt($session, CURLOPT_POSTFIELDS, http_build_query($params,'','&'));
|
|
|
|
// Tell curl not to return headers, but do return the response
|
|
|
|
curl_setopt($session, CURLOPT_HEADER, false);
|
|
|
|
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
|
|
|
|
$response = curl_exec($session);
|
|
|
|
$httpCode = curl_getinfo($session, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close($session);
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2013-03-03 17:07:31 +00:00
|
|
|
// If the operation was not successful, print the error
|
2012-11-27 16:22:47 +00:00
|
|
|
if($httpCode != 200)
|
2012-04-28 17:28:18 +00:00
|
|
|
throw new Exception($response);
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
// Decode the result
|
|
|
|
$results = json_decode($response, true);
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
// Find the token
|
|
|
|
if(isset($results['token'])) {
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
// Link the token with the current cart ( held in session id )
|
|
|
|
$data = array(
|
|
|
|
'token' => $results['token'],
|
|
|
|
'session_id' => session_id(),
|
|
|
|
);
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
// A token was obtained, so redirect the browser
|
|
|
|
header("Location: $results[session_url]", true, 302);
|
|
|
|
return $data;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
//show the raw response received ( for debug purposes )
|
|
|
|
throw new Exception($response);
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
/**
|
|
|
|
* make sure the coupon is valid
|
2012-11-27 16:22:47 +00:00
|
|
|
* @param null
|
2012-04-28 17:28:18 +00:00
|
|
|
*/
|
|
|
|
public function assertCouponIsValid($token, $coupon_code, $coupon_value, $coupon_type) {
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
// Verifies POST information
|
|
|
|
if(!isset($token, $coupon_code, $coupon_value, $coupon_type)) {
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
throw new Exception(SyC::t('sdk',"At least one of the parameters is missing."));
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
// Urlencode and concatenate the POST arguments
|
|
|
|
$params = array(
|
|
|
|
'token' => $token,
|
|
|
|
'coupon_code' => $coupon_code,
|
|
|
|
'coupon_value' => $coupon_value,
|
|
|
|
'coupon_type'=> $coupon_type,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Make the API call
|
|
|
|
$session = curl_init($this->SHAREYOURCART_API_VALIDATE);
|
|
|
|
|
|
|
|
// Tell curl to use HTTP POST
|
|
|
|
curl_setopt($session, CURLOPT_POST, true);
|
|
|
|
// Tell curl that this is the body of the POST
|
|
|
|
curl_setopt($session, CURLOPT_POSTFIELDS, http_build_query($params,'','&'));
|
|
|
|
// Tell curl not to return headers, but do return the response
|
|
|
|
curl_setopt($session, CURLOPT_HEADER, false);
|
|
|
|
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
|
|
|
|
$response = curl_exec($session);
|
|
|
|
$httpCode = curl_getinfo($session, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close($session);
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2013-03-03 17:07:31 +00:00
|
|
|
//if the operation was not successful, print the error
|
2012-11-27 16:22:47 +00:00
|
|
|
if($httpCode != 200)
|
2012-04-28 17:28:18 +00:00
|
|
|
throw new Exception(SyC::t('sdk',"Coupon Invalid: {coupon}", array('{coupon}' => $response)));
|
2012-11-27 16:22:47 +00:00
|
|
|
|
|
|
|
$results = json_decode($response, true);
|
2012-04-28 17:28:18 +00:00
|
|
|
|
|
|
|
//if the result is not valid, print it
|
2012-11-27 16:22:47 +00:00
|
|
|
if(!isset($results['valid']) || !($results['valid'] === true))
|
2012-04-28 17:28:18 +00:00
|
|
|
throw new Exception(SyC::t('sdk',"Coupon Invalid: {coupon}", array('{coupon}' => $response)));
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
/**
|
|
|
|
* register
|
|
|
|
* @param string $secretKey
|
|
|
|
* @param string $domain
|
|
|
|
* @param string $email
|
2012-11-27 16:22:47 +00:00
|
|
|
* @param string $message
|
|
|
|
* @return array json_decode
|
2012-04-28 17:28:18 +00:00
|
|
|
*/
|
|
|
|
public function register($secretKey, $domain, $email, &$message = null) {
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
// Urlencode and concatenate the POST arguments
|
|
|
|
$params = array(
|
|
|
|
'secret_key' => $secretKey,
|
|
|
|
'domain' => $domain,
|
|
|
|
'email' => $email,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Make the API call
|
|
|
|
$session = curl_init($this->SHAREYOURCART_API_REGISTER);
|
|
|
|
|
|
|
|
// Tell curl to use HTTP POST
|
|
|
|
curl_setopt($session, CURLOPT_POST, true);
|
|
|
|
// Tell curl that this is the body of the POST
|
|
|
|
curl_setopt($session, CURLOPT_POSTFIELDS, http_build_query($params,'','&'));
|
|
|
|
// Tell curl not to return headers, but do return the response
|
|
|
|
curl_setopt($session, CURLOPT_HEADER, false);
|
|
|
|
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
|
|
|
|
$response = curl_exec($session);
|
|
|
|
$httpCode = curl_getinfo($session, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close($session);
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2013-03-03 17:07:31 +00:00
|
|
|
// If the operation was not successful, return FALSE
|
2012-04-28 17:28:18 +00:00
|
|
|
if($httpCode != 200) {
|
|
|
|
if(isset($message)) $message = $response;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
|
|
|
return false;
|
2012-04-28 17:28:18 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
//if the caller is expecting a message
|
|
|
|
//let him know what happened
|
|
|
|
if(isset($message)){
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
$message = SyC::t('sdk','The account has been registered');
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
// Return the response after decoding it
|
2012-11-27 16:22:47 +00:00
|
|
|
return json_decode($response, true);
|
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
/**
|
|
|
|
* recover
|
|
|
|
* @param string $secretKey
|
|
|
|
* @param string $domain
|
|
|
|
* @param string $email
|
2012-11-27 16:22:47 +00:00
|
|
|
* @param string $message
|
2012-04-28 17:28:18 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function recover($secretKey, $domain, $email, &$message = null) {
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
// Urlencode and concatenate the POST arguments
|
|
|
|
$params = array(
|
|
|
|
'secret_key' => $secretKey,
|
|
|
|
'domain' => $domain,
|
|
|
|
'email' => $email,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Make the API call
|
|
|
|
$session = curl_init($this->SHAREYOURCART_API_RECOVER);
|
|
|
|
|
|
|
|
// Tell curl to use HTTP POST
|
|
|
|
curl_setopt($session, CURLOPT_POST, true);
|
|
|
|
// Tell curl that this is the body of the POST
|
|
|
|
curl_setopt($session, CURLOPT_POSTFIELDS, http_build_query($params,'','&'));
|
|
|
|
// Tell curl not to return headers, but do return the response
|
|
|
|
curl_setopt($session, CURLOPT_HEADER, false);
|
|
|
|
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
|
|
|
|
$response = curl_exec($session);
|
|
|
|
$httpCode = curl_getinfo($session, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close($session);
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
//if the operation was not succesfull, return FALSE
|
|
|
|
if($httpCode != 200) {
|
|
|
|
if(isset($message)) $message = $response;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
//if the caller is expecting a message
|
|
|
|
//let him know what happened
|
|
|
|
if(isset($message)){
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
$message = (!empty($response) ? $response : SyC::t('sdk',"An email has been sent with your credentials at {email}",array('{email}' => $email)));
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
return true;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
/**
|
|
|
|
* setAccountStatus
|
|
|
|
* @param string $secretKey
|
|
|
|
* @param string $clientId
|
|
|
|
* @param string $appKey
|
|
|
|
* @param string $activate
|
2012-11-27 16:22:47 +00:00
|
|
|
* @param string $message
|
2012-04-28 17:28:18 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function setAccountStatus($secretKey, $clientId, $appKey, $activate = true, &$message = null) {
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
// Urlencode and concatenate the POST arguments
|
|
|
|
$params = array(
|
|
|
|
'secret_key' => $secretKey,
|
|
|
|
'client_id' => $clientId,
|
|
|
|
'app_key' => $appKey,
|
|
|
|
);
|
|
|
|
|
|
|
|
//make the API call
|
|
|
|
$session = curl_init($activate ? $this->SHAREYOURCART_API_ACTIVATE : $this->SHAREYOURCART_API_DEACTIVATE);
|
|
|
|
|
|
|
|
// Tell curl to use HTTP POST
|
|
|
|
curl_setopt($session, CURLOPT_POST, true);
|
|
|
|
// Tell curl that this is the body of the POST
|
|
|
|
curl_setopt($session, CURLOPT_POSTFIELDS, http_build_query($params,'','&'));
|
|
|
|
// Tell curl not to return headers, but do return the response
|
|
|
|
curl_setopt($session, CURLOPT_HEADER, false);
|
|
|
|
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
|
|
|
|
$response = curl_exec($session);
|
|
|
|
$httpCode = curl_getinfo($session, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close($session);
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
// Notify the caller
|
|
|
|
if($httpCode != 200) {
|
|
|
|
if(isset($message)) $message = $response;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
//if the caller is expecting a message
|
|
|
|
//let him know what happened
|
|
|
|
if(isset($message)){
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
$message = (!empty($response) ? $response : ($activate ? SyC::t('sdk','The account has been enabled') : SyC::t('sdk','The account has been disabled')));
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
/**
|
|
|
|
* setAccountStatus
|
|
|
|
* @param string $secretKey
|
|
|
|
* @param string $clientId
|
|
|
|
* @param string $appKey
|
2012-11-27 16:22:47 +00:00
|
|
|
* @param string $message
|
2012-04-28 17:28:18 +00:00
|
|
|
* @return array or FALSE
|
|
|
|
*/
|
|
|
|
public function getSDKStatus($secretKey=null, $clientId=null, $appKey=null, &$message = null)
|
|
|
|
{
|
|
|
|
$params = array();
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
if(isset($secretKey)) $params['secret_key'] = $secretKey;
|
|
|
|
if(isset($clientId)) $params['client_id'] = $clientId;
|
|
|
|
if(isset($appKey)) $params['app_key'] = $appKey;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
//make the API call
|
|
|
|
$session = curl_init($this->SHAREYOURCART_API_STATUS);
|
|
|
|
|
|
|
|
// Tell curl to use HTTP POST
|
|
|
|
curl_setopt($session, CURLOPT_POST, true);
|
|
|
|
// Tell curl that this is the body of the POST
|
|
|
|
curl_setopt($session, CURLOPT_POSTFIELDS, http_build_query($params,'','&'));
|
|
|
|
// Tell curl not to return headers, but do return the response
|
|
|
|
curl_setopt($session, CURLOPT_HEADER, false);
|
|
|
|
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
|
|
|
|
$response = curl_exec($session);
|
|
|
|
$httpCode = curl_getinfo($session, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close($session);
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
// Notify the caller
|
|
|
|
if($httpCode != 200) {
|
|
|
|
if(isset($message)) $message = $response;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
// Decode the result
|
|
|
|
return json_decode($response, true);
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-16 11:55:15 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Returns an array of messages for the SDK, in the specified language
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function getSDKTranslation($lang, &$message = null)
|
|
|
|
{
|
|
|
|
$params = array('lang' => $lang);
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-16 11:55:15 +00:00
|
|
|
//make the API call
|
|
|
|
$session = curl_init($this->SHAREYOURCART_API_TRANSLATION);
|
|
|
|
|
|
|
|
// Tell curl to use HTTP POST
|
|
|
|
curl_setopt($session, CURLOPT_POST, true);
|
|
|
|
// Tell curl that this is the body of the POST
|
|
|
|
curl_setopt($session, CURLOPT_POSTFIELDS, http_build_query($params,'','&'));
|
|
|
|
// Tell curl not to return headers, but do return the response
|
|
|
|
curl_setopt($session, CURLOPT_HEADER, false);
|
|
|
|
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
|
|
|
|
$response = curl_exec($session);
|
|
|
|
$httpCode = curl_getinfo($session, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close($session);
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-16 11:55:15 +00:00
|
|
|
// Notify the caller
|
|
|
|
if($httpCode != 200) {
|
|
|
|
if(isset($message)) $message = $response;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-16 11:55:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-16 11:55:15 +00:00
|
|
|
// Decode the result
|
|
|
|
return json_decode($response, true);
|
|
|
|
}
|
2012-04-28 17:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* This class should contain all the static methods in the SDK
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class SyC
|
|
|
|
{
|
|
|
|
static $_messages;
|
|
|
|
static $_language = 'en';
|
2012-11-16 11:55:15 +00:00
|
|
|
static $loadLanguage = array('SyC','loadFileLanguage'); //variable that holds the name of the function used to load a particular language
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Change the language the SDK should be displayed in
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function setLanguage($newLanguage = null){
|
|
|
|
self::$_language = $newLanguage;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
//reset the old messages, so that they are reloaded
|
2012-11-27 16:22:47 +00:00
|
|
|
self::$_messages = null;
|
2012-04-28 17:28:18 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-16 11:55:15 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Get the language that is currently loaded
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getLanguage(){
|
|
|
|
return self::$_language;
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-16 11:55:15 +00:00
|
|
|
/**
|
|
|
|
* Return the checksum of the currently loaded translation
|
|
|
|
*/
|
|
|
|
public static function getLanguageChecksum($category = 'sdk')
|
|
|
|
{
|
|
|
|
//load the translation from file if not done so
|
|
|
|
if(!isset(self::$_messages)){
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-16 11:55:15 +00:00
|
|
|
//load the language
|
|
|
|
self::$_messages = call_user_func(self::$loadLanguage,self::$_language,$category);
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-16 11:55:15 +00:00
|
|
|
return md5(json_encode(self::$_messages));
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-16 11:55:15 +00:00
|
|
|
/**
|
|
|
|
* Reload the language
|
|
|
|
*/
|
|
|
|
public static function reloadLanguage()
|
|
|
|
{
|
|
|
|
self::$_messages = null;
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
/*
|
|
|
|
* Translate the specified message
|
2012-11-27 16:22:47 +00:00
|
|
|
*
|
2012-04-28 17:28:18 +00:00
|
|
|
*/
|
|
|
|
public static function t($category,$message,$params=array())
|
|
|
|
{
|
|
|
|
//load the translation from file if not done so
|
|
|
|
if(!isset(self::$_messages)){
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-16 11:55:15 +00:00
|
|
|
//load the language
|
|
|
|
self::$_messages = call_user_func(self::$loadLanguage,self::$_language,$category);
|
2012-04-28 17:28:18 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
//check if the text has a valid translation
|
|
|
|
if(isset(self::$_messages[$message]) && !empty(self::$_messages[$message])){
|
|
|
|
$message = self::$_messages[$message];
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
//return the translated message, with the parameters replaced
|
|
|
|
return $params!==array() ? strtr($message,$params) : $message;
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-16 11:55:15 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* change the language loader method
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function setLanguageLoader($loader)
|
|
|
|
{
|
|
|
|
//make sure the loader is ok
|
|
|
|
if(!is_callable($loader))
|
|
|
|
throw new Exception(SyC::t('sdk',"The language loader is not a valid callback"));
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-16 11:55:15 +00:00
|
|
|
self::$loadLanguage = $loader;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-16 11:55:15 +00:00
|
|
|
//reset the old messages, so that they are reloaded with the new loader
|
|
|
|
self::$_messages = null;
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-16 11:55:15 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Function to load a language from the hard-drive.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function loadFileLanguage($lang, $category)
|
|
|
|
{
|
|
|
|
//The language is the folder name, and the category is the name of the file
|
|
|
|
$messageFile = dirname(__FILE__).DIRECTORY_SEPARATOR.'messages'.DIRECTORY_SEPARATOR.$lang.DIRECTORY_SEPARATOR.$category.'.php';
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-16 11:55:15 +00:00
|
|
|
$messages = null;
|
|
|
|
if(is_file($messageFile))
|
|
|
|
{
|
|
|
|
$messages=include($messageFile);
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-16 11:55:15 +00:00
|
|
|
//make sure we have an array for this variable
|
|
|
|
if(!is_array($messages)) $messages=array();
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-16 11:55:15 +00:00
|
|
|
return $messages;
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-16 11:55:15 +00:00
|
|
|
public static function relativepath($from, $to, $ps = '/' ,$ds = DIRECTORY_SEPARATOR)
|
2012-11-27 16:22:47 +00:00
|
|
|
{
|
2012-04-28 17:28:18 +00:00
|
|
|
$arFrom = explode($ds, rtrim($from, $ds));
|
|
|
|
$arTo = explode($ds, rtrim($to, $ds));
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-04-28 17:28:18 +00:00
|
|
|
while(count($arFrom) && count($arTo) && ($arFrom[0] == $arTo[0]))
|
|
|
|
{
|
|
|
|
array_shift($arFrom);
|
|
|
|
array_shift($arTo);
|
|
|
|
}
|
|
|
|
return str_pad("", count($arFrom) * 3, '..'.$ps).implode($ps, $arTo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* rel2Abs
|
|
|
|
* @param string $src
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-11-16 11:55:15 +00:00
|
|
|
public static function rel2Abs($rel, $base) {
|
2012-04-28 17:28:18 +00:00
|
|
|
|
|
|
|
/* return if already absolute URL */
|
|
|
|
if (parse_url($rel, PHP_URL_SCHEME) != '')
|
|
|
|
return $rel;
|
|
|
|
|
|
|
|
/* queries and anchors */
|
|
|
|
if ($rel[0] == '#' || $rel[0] == '?')
|
|
|
|
return $base . $rel;
|
|
|
|
|
|
|
|
/* parse base URL and convert to local variables:
|
|
|
|
$scheme, $host, $path */
|
|
|
|
extract(parse_url($base));
|
|
|
|
|
|
|
|
/* remove non-directory element from path */
|
|
|
|
$path = preg_replace('#/[^/]*$#', '', @$path);
|
|
|
|
|
|
|
|
/* destroy path if relative url points to root */
|
|
|
|
if ($rel[0] == '/')
|
|
|
|
$path = '';
|
|
|
|
|
|
|
|
/* dirty absolute URL */
|
|
|
|
$abs = "$host$path/$rel";
|
|
|
|
|
|
|
|
/* replace '//' or '/./' or '/foo/../' with '/' */
|
|
|
|
$re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
|
|
|
|
for ($n = 1; $n > 0; $abs = preg_replace($re, '/', $abs, -1, $n)) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* absolute URL is ready! */
|
|
|
|
return $scheme . '://' . $abs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* htmlIndent
|
|
|
|
* @param string $src
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-11-16 11:55:15 +00:00
|
|
|
public static function htmlIndent($src) {
|
2012-04-28 17:28:18 +00:00
|
|
|
|
|
|
|
//replace all leading spaces with
|
|
|
|
//Attention: this will render wrong html if you split a tag on more lines!
|
|
|
|
return preg_replace_callback('/(^|\n)( +)/', create_function('$match', 'return str_repeat(" ", strlen($match[0]));'
|
|
|
|
), $src);
|
|
|
|
}
|
|
|
|
|
2012-11-16 11:55:15 +00:00
|
|
|
/**
|
|
|
|
* returns TRUE if haystack starts with needle
|
|
|
|
*/
|
|
|
|
public static function startsWith($haystack, $needle)
|
|
|
|
{
|
|
|
|
$length = strlen($needle);
|
|
|
|
return (substr($haystack, 0, $length) === $needle);
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-16 11:55:15 +00:00
|
|
|
/**
|
|
|
|
* returns TRUE if haystack ends with needle
|
|
|
|
*/
|
|
|
|
public static function endsWith($haystack, $needle)
|
|
|
|
{
|
|
|
|
return (substr($haystack, strlen($haystack) - strlen($needle)) === $needle);
|
|
|
|
}
|
2012-04-28 17:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} //END IF
|
|
|
|
?>
|