<?php
if(!defined('API_ROOT_PATH')){
define('API_ROOT_PATH', dirname(__FILE__));
}
require_once(API_ROOT_PATH . '/lib/BaeBase.class.php');
require_once(API_ROOT_PATH . '/images/BaeImageConstant.class.php');
require_once(API_ROOT_PATH . '/images/BaeImageAnnotate.class.php');
require_once(API_ROOT_PATH . '/images/BaeImageComposite.class.php');
require_once(API_ROOT_PATH . '/images/BaeImageQRCode.class.php');
require_once(API_ROOT_PATH . '/images/BaeImageTransform.class.php');
require_once(API_ROOT_PATH . '/images/BaeImageVCode.class.php');
require_once(API_ROOT_PATH . '/lib/RequestCore.class.php');
class BaeImageService extends BaeBase{
const METHOD = 'method';
const HOST = 'host';
const PRODUCT = 'imageui';
const SIGN = 'sign';
const ACCESS_TOKEN = 'access_token';
const SECRET_KEY = 'client_secret';
const ACCESS_KEY = 'client_id';
const DEFAULT_HOST = 'image.duapp.com';
const TIMESTAMP = 'timestamp';
const EXPIRES = 'expires';
const VERSION = 'v';
const RESOURCE = 'resource';
private $_requestId;
private $_clientId;
private $_clientSecret;
private $_host;
private $paramArr;
private $ProcType_Annotate = 1;
private $ProcType_Composite = 2;
private $ProcType_QRCode = 0;
private $_arrayErrorMap = array(
'0' => 'php sdk error',
BaeImageConstant::BAE_IMAGEUI_SDK_SYS => 'php sdk error',
BaeImageConstant::BAE_IMAGEUI_SDK_INIT_FAIL => 'php sdk init error',
BaeImageConstant::BAE_IMAGEUI_SDK_PARAM => 'param invalid',
BaeImageConstant::BAE_IMAGEUI_SDK_HTTP_STATUS_ERROR_AND_RESULT_ERROR
=> 'http status is error, and the body returned is not a json string',
BaeImageConstant::BAE_IMAGEUI_SDK_HTTP_STATUS_OK_BUT_RESULT_ERROR
=> 'http status is ok, but the body returned is not a json string',
);
public function getRequestId()
{
return $this->_requestId;
}
public function __construct($accessKey, $secretKey, $host)
{
$this->paramArr = array();
if (is_null($accessKey) || $this->_checkString($accessKey, 1, 64)) {
$this->_clientId = $accessKey;
} else {
throw new BaeException("invalid param - access key[${accessKey}], which must be a 1 - 64 length string",
BaeImageConstant::BAE_IMAGEUI_SDK_INIT_FAIL);
}
if (is_null($secretKey) || $this->_checkString($secretKey, 1, 64)) {
$this->_clientSecret = $secretKey;
} else {
throw new BaeException("invalid param - secret key[${secretKey}], which must be a 1 - 64 length string",
BaeImageConstant::BAE_IMAGEUI_SDK_INIT_FAIL);
}
if (is_null($host) || $this->_checkString($host, 1, 1024)) {
if (!is_null($host)) {
$this->_host = $host;
}
} else {
throw new BaeException("invalid param - host[${host}], which must be a 1 - 1024 length string",
BaeImageConstant::BAE_IMAGEUI_SDK_INIT_FAIL);
}
$this->_resetErrorStatus();
}
private function _get_ak_sk_host(&$opt, $opt_key, $member, $g_key, $env_key, $min, $max)
{
$dis = array(
'client_id' => 'access_key',
'client_secret' => 'secret_key',
'host' => 'host'
);
global $$g_key;
if (isset($opt[$opt_key])) {
if (!$this->_checkString($opt[$opt_key], $min, $max)) {
throw new BaeException('invalid ' . $dis[$opt_key] . ' in $optinal('
. $opt[$opt_key] . '), which must be a ' . $min . ' - ' . $max . ' length string',
BaeImageConstant::BAE_IMAGEUI_SDK_PARAM);
}
return;
}
if ($this->_checkString($member, $min, $max)) {
$opt[$opt_key]= $member;
return;
}
if (isset($$g_key)) {
if (!$this->_checkString($$g_key, $min, $max)) {
throw new BaeException('invalid ' . $g_key . ' in global area('
. $$g_key . '), which must be a ' . $min . ' - ' . $max . ' length string',
BaeImageConstant::BAE_IMAGEUI_SDK_PARAM);
}
$opt[$opt_key]= $$g_key;
return;
}
if (false !== getenv($env_key)) {
if (! $this->_checkString(getenv($env_key), $min, $max)) {
throw new BaeException('invalid ' . $env_key . ' in environment variable('
. getenv($env_key). '), which must be a ' . $min . ' - ' . $max . ' length string',
BaeImageConstant::BAE_IMAGEUI_SDK_PARAM);
}
$opt[$opt_key]= getenv($env_key);
return;
}
if ($opt_key === self::HOST) {
$opt[$opt_key]= self::DEFAULT_HOST;
return;
}
throw new BaeException('no param(' . $dis[$opt_key] . ')was found', BaeImageConstant::BAE_IMAGEUI_SDK_PARAM);
}
private function _adjustOpt(&$opt)
{
if (! isset($opt) || empty($opt) || !is_array($opt)) {
throw new BaeException('no params are set', BaeImageConstant::BAE_IMAGEUI_SDK_PARAM);
}
if (!isset($opt[self::TIMESTAMP])) {
$opt[self::TIMESTAMP]= time();
}
$this->_get_ak_sk_host($opt, self::ACCESS_KEY, $this->_clientId,
'g_accessKey', 'HTTP_BAE_ENV_AK', 1, 64);
$this->_get_ak_sk_host($opt, self::SECRET_KEY, $this->_clientSecret,
'g_secretKey', 'HTTP_BAE_ENV_SK', 1, 64);
$this->_get_ak_sk_host($opt, self::HOST, $this->_host,
'g_host', 'HTTP_BAE_ENV_ADDR_BUS', 1, 1024);
}
private function _getSign(&$opt, &$arrContent, $arrNeed = array())
{
$arrData = array();
$arrContent = array();
$arrNeed[] = self::TIMESTAMP;
$arrNeed[] = self::METHOD;
$arrNeed[] = self::ACCESS_KEY;
if (isset($opt[self::EXPIRES])) {
$arrNeed[] = self::EXPIRES;
}
if (isset($opt[self::VERSION])) {
$arrNeed[] = self::VERSION;
}
$arrExclude = array(self::HOST, self::SECRET_KEY);
foreach ($arrNeed as $key) {
if (!isset($opt[$key]) || (!is_integer($opt[$key]) && empty($opt[$key]))) {
throw new BaeException("lack param(${key})", BaeImageConstant::BAE_IMAGEUI_SDK_PARAM);
}
if (in_array($key, $arrExclude)) {
continue;
}
$arrData[$key] = $opt[$key];
$arrContent[$key] = $opt[$key];
}
foreach ($opt as $key => $value) {
if (!in_array($key, $arrNeed) && !in_array($key, $arrExclude)) {
$arrData[$key]= $value;
$arrContent[$key]= $value;
}
}
ksort($arrData);
$url = 'http://' . $opt[self::HOST] . '/rest/2.0/' . self::PRODUCT . '/';
if (isset($opt[self::RESOURCE]) && !is_null($opt[self::RESOURCE])){
$url .= $opt[self::RESOURCE];
$arrContent[self::RESOURCE]= $opt[self::RESOURCE];
}else {
$url .= self::RESOURCE;
}
$basicString = 'POST' . $url;
foreach ($arrData as $key => $value) {
$basicString .= $key . '=' . $value;
}
$basicString .= $opt[self::SECRET_KEY];
$sign = md5(urlencode($basicString));
$arrContent[self::SIGN] = $sign;
$arrContent[self::HOST] = $opt[self::HOST];
}
private function _baseControl($opt)
{
$content = '';
$resource = self::RESOURCE;
if (isset($opt[self::RESOURCE]) && !is_null($opt[self::RESOURCE])) {
$resource = $opt[self::RESOURCE];
}
$host = $opt[self::HOST];
unset($opt[self::HOST]);
foreach ($opt as $k => $v) {
if (is_string($v)) {
$v = urlencode($v);
}
$content .= $k . '=' . $v . '&';
}
$content = substr($content, 0, strlen($content)- 1);
$url = 'http://' . $host . '/rest/2.0/' . self::PRODUCT . '/';
$url .= $resource;
$request = new RequestCore($url);
$headers['Content-Type'] = 'application/x-www-form-urlencoded';
$headers['User-Agent'] = 'Baidu ImageUi Phpsdk Client';
foreach ($headers as $headerKey => $headerValue) {
$headerValue = str_replace(array("\r", "\n"), '', $headerValue);
if ($headerValue !== '') {
$request->add_header($headerKey, $headerValue);
}
}
$request->set_method('POST');
$request->set_body($content);
$request->send_request();
return new ResponseCore($request->get_response_header(),
$request->get_response_body(),
$request->get_response_code());
}
private function _commonProce