国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
5.5KB

  1. <?php if(!defined('DEDEINC')) exit('Request Error!');
  2. // Copyright 2020 The DedeBiz Authors. All rights reserved.
  3. // license that can be found in the LICENSE file.
  4. // @copyright Copyright (c) 2020, DedeBIZ.COM
  5. // @license https://www.dedebiz.com/license
  6. // @link https://www.dedebiz.com
  7. // 本文件为DedeCMS商业组件(www.dedebiz.com)PHP SDK
  8. // 目的是弥补织梦内容管理系统(DedeCMS)性能和安全方面的不足,提供更多功能
  9. define("DEDEBIZ", true);
  10. // 本文件用于和DedeBiz商业组件进行通信,以获取更多额外的扩展功能
  11. class DedeBizClient
  12. {
  13. var $socket;
  14. var $appid;
  15. var $key;
  16. var $err;
  17. function __construct($ipaddr, $port)
  18. {
  19. $this->err = "";
  20. if (!function_exists("socket_create")) {
  21. $this->err = (object)array(
  22. "code" => -1,
  23. "data" => null,
  24. "msg" => "请在php.ini开启extension=sockets",
  25. );
  26. return;
  27. }
  28. $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  29. $rs = @socket_connect($this->socket, $ipaddr, $port);
  30. if (!$rs) {
  31. $this->err = (object)array(
  32. "code" => -1,
  33. "data" => null,
  34. "msg" => "连接DedeBiz商业组件服务失败\r\n",
  35. );
  36. return;
  37. }
  38. }
  39. function request(&$req){
  40. // 进行签名
  41. $this->MakeSign($req);
  42. $str = json_encode($req);
  43. $length = strlen($str);
  44. $s = @socket_write($this->socket, $str, $length);
  45. if (!$s) {
  46. return (object)array(
  47. "code" => -1,
  48. "data" => null,
  49. "msg" => "请求DedeBiz商业组件服务失败\r\n",
  50. );
  51. }
  52. if (!empty($this->err)) {
  53. return $this->err;
  54. }
  55. $msg = "";
  56. while(($str = socket_read($this->socket, 1024)) !== FALSE){
  57. $msg .= $str;
  58. if (strlen($str) < 1024) {
  59. break;
  60. }
  61. }
  62. return $this->CheckSign($msg);
  63. }
  64. // 用户获取当前服务器状态信息
  65. function SystemInfo(){
  66. $req = array(
  67. "method" => "system_info",
  68. );
  69. return $this->request($req);
  70. }
  71. // 检测是否连接
  72. function Ping($i)
  73. {
  74. $req = array(
  75. "method" => "ping",
  76. "parms" => array(
  77. "name" => "www.dedebiz.com",
  78. )
  79. );
  80. return $this->request($req);
  81. }
  82. // 获取一个管理员信息
  83. function AdminGetOne()
  84. {
  85. $req = array(
  86. "method" => "admin_get_one",
  87. "parms" => array(
  88. "name" => "admin",
  89. )
  90. );
  91. return $this->request($req);
  92. }
  93. // 缓存
  94. // $key:键 $val:值 $d:缓存时间
  95. function CacheSet($key,$val,$duration)
  96. {
  97. $req = array(
  98. "method" => "cache_set",
  99. "parms" => array(
  100. "k" => $key,
  101. "v" => $val,
  102. "d" => $duration,
  103. )
  104. );
  105. return $this->request($req);
  106. }
  107. // 获取缓存内容
  108. // $key:键
  109. function CacheGet($key)
  110. {
  111. $req = array(
  112. "method" => "cache_get",
  113. "parms" => array(
  114. "k" => $key,
  115. )
  116. );
  117. return $this->request($req);
  118. }
  119. // 删除缓存内容
  120. // $key:键
  121. function CacheDel($key)
  122. {
  123. $req = array(
  124. "method" => "cache_del",
  125. "parms" => array(
  126. "k" => $key,
  127. )
  128. );
  129. return $this->request($req);
  130. }
  131. // 获取分词结果
  132. // $key:键
  133. function Spliteword($body)
  134. {
  135. $req = array(
  136. "method" => "spliteword",
  137. "parms" => array(
  138. "body" => $body,
  139. )
  140. );
  141. return $this->request($req);
  142. }
  143. // 获取分词结果
  144. // $body:内容 $sep:分隔符
  145. function Pinyin($body,$sep)
  146. {
  147. $req = array(
  148. "method" => "pinyin",
  149. "parms" => array(
  150. "body" => $body,
  151. "sep" => $sep,
  152. )
  153. );
  154. return $this->request($req);
  155. }
  156. // 拼接规则就是method+
  157. function MakeSign(&$req)
  158. {
  159. if (empty($req['timestamp'])) {
  160. $req['timestamp'] = time();
  161. }
  162. if (isset($req['parms']) && count($req['parms']) > 0) {
  163. ksort($req['parms']);
  164. }
  165. $pstr = "appid={$this->appid}method={$req['method']}key={$this->key}";
  166. if (isset($req['parms']) && count($req['parms']) > 0) {
  167. foreach ($req['parms'] as $key => $value) {
  168. $pstr .= "$key=$value";
  169. }
  170. }
  171. $pstr .= "timestamp={$req['timestamp']}";
  172. $req['sign'] = hash("sha256", $pstr);
  173. }
  174. // 校验返回数据是否正确
  175. function CheckSign(&$msg)
  176. {
  177. $rsp = json_decode($msg);
  178. if (!is_object($rsp)) {
  179. return null;
  180. }
  181. $str = sprintf("appid=%skey=%scode=%dmsg=%sdata=%stimestamp=%d", $this->appid, $this->key, $rsp->code, $rsp->msg, $rsp->data, $rsp->timestamp);
  182. if (hash("sha256", $str) === $rsp->sign) {
  183. return $rsp;
  184. } else {
  185. return null;
  186. }
  187. }
  188. // 关闭通信接口
  189. // !!!一次页面操作后一定记得要关闭连接,否则会占用系统资源
  190. function Close()
  191. {
  192. socket_close($this->socket);
  193. }
  194. }