|
- <?php
- if (!defined('DEDEINC')) exit('dedebiz');
-
-
-
-
-
-
-
-
-
-
- define("DEDEBIZ", true);
-
-
- class DedeBizClient
- {
- var $socket;
- var $appid;
- var $key;
- var $err;
-
- function __construct($ipaddr, $port)
- {
- $this->err = "";
- if (!function_exists("socket_create")) {
- $this->err = (object)array(
- "code" => -1,
- "data" => null,
- "msg" => "请在php.ini开启extension=sockets",
- );
- return;
- }
- $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
- $rs = @socket_connect($this->socket, $ipaddr, $port);
- if (!$rs) {
- $this->err = (object)array(
- "code" => -1,
- "data" => null,
- "msg" => "连接DedeBiz商业组件服务失败\r\n",
- );
- return;
- }
- }
-
- function request(&$req)
- {
-
- $this->MakeSign($req);
- $str = json_encode($req);
- $length = strlen($str);
- $s = @socket_write($this->socket, $str, $length);
-
- if (!$s) {
- return (object)array(
- "code" => -1,
- "data" => null,
- "msg" => "请求DedeBiz商业组件服务失败\r\n",
- );
- }
-
- if (!empty($this->err)) {
- return $this->err;
- }
-
- $msg = "";
- while (($str = socket_read($this->socket, 1024)) !== FALSE) {
- $msg .= $str;
- if (strlen($str) < 1024) {
- break;
- }
- }
- return $this->CheckSign($msg);
- }
-
-
- function SystemInfo()
- {
- $req = array(
- "method" => "system_info",
- );
- return $this->request($req);
- }
-
-
- function Ping($i)
- {
- $req = array(
- "method" => "ping",
- "parms" => array(
- "name" => "www.dedebiz.com",
- )
- );
- return $this->request($req);
- }
-
-
- function MailSend($to, $subject, $title, $content="", $quote="", $link_url="", $link_title="")
- {
- $req = array(
- "method" => "main_send",
- "parms" => array(
- "to" => $to,
- "subject" => $subject,
- "title" => $title,
- "content" => $content,
- "quote" => $quote,
- "link_url" => $link_url,
- "link_title" => $link_title,
- )
- );
- return $this->request($req);
- }
-
-
- function AdminGetOne()
- {
- $req = array(
- "method" => "admin_get_one",
- "parms" => array(
- "name" => "admin",
- )
- );
- return $this->request($req);
- }
-
-
- function AdminPWDExists()
- {
- $req = array(
- "method" => "admin_pwd_exists",
- "parms" => array(
- "name" => "admin",
- )
- );
- return $this->request($req);
- }
-
-
- function AdminPWDCreate($pwd)
- {
- $req = array(
- "method" => "admin_pwd_create",
- "parms" => array(
- "pwd" => $pwd,
- )
- );
- return $this->request($req);
- }
-
-
- function AdminSetIndexLockState($pwd, $state)
- {
- $req = array(
- "method" => "admin_set_index_lock_state",
- "parms" => array(
- "pwd" => $pwd,
- "lock_state" => $state,
- )
- );
- return $this->request($req);
- }
-
-
-
- function CacheSet($key, $val, $duration)
- {
- $req = array(
- "method" => "cache_set",
- "parms" => array(
- "k" => $key,
- "v" => $val,
- "d" => $duration,
- )
- );
- return $this->request($req);
- }
-
-
-
- function CacheGet($key)
- {
- $req = array(
- "method" => "cache_get",
- "parms" => array(
- "k" => $key,
- )
- );
- return $this->request($req);
- }
-
-
-
- function CacheDel($key)
- {
- $req = array(
- "method" => "cache_del",
- "parms" => array(
- "k" => $key,
- )
- );
- return $this->request($req);
- }
-
-
-
- function Spliteword($body)
- {
- $req = array(
- "method" => "spliteword",
- "parms" => array(
- "body" => $body,
- )
- );
- return $this->request($req);
- }
-
-
-
- function Pinyin($body, $sep)
- {
- $req = array(
- "method" => "pinyin",
- "parms" => array(
- "body" => $body,
- "sep" => $sep,
- )
- );
- return $this->request($req);
- }
-
-
- function MakeSign(&$req)
- {
- if (empty($req['timestamp'])) {
- $req['timestamp'] = time();
- }
- if (isset($req['parms']) && count($req['parms']) > 0) {
- ksort($req['parms']);
- }
-
- $pstr = "appid={$this->appid}method={$req['method']}key={$this->key}";
- if (isset($req['parms']) && count($req['parms']) > 0) {
- foreach ($req['parms'] as $key => $value) {
- $pstr .= "$key=$value";
- }
- }
-
- $pstr .= "timestamp={$req['timestamp']}";
- $req['sign'] = hash("sha256", $pstr);
- }
-
-
- function CheckSign(&$msg)
- {
- $rsp = json_decode($msg);
- if (!is_object($rsp)) {
- return null;
- }
- $str = sprintf("appid=%skey=%scode=%dmsg=%sdata=%stimestamp=%d", $this->appid, $this->key, $rsp->code, $rsp->msg, $rsp->data, $rsp->timestamp);
- if (hash("sha256", $str) === $rsp->sign) {
- return $rsp;
- } else {
- return null;
- }
- }
-
-
-
- function Close()
- {
-
- if (strtolower(get_resource_type($this->socket)) === "socket") {
- socket_close($this->socket);
- }
- }
-
- function __destruct()
- {
- $this->Close();
- }
- }
|