国内流行的内容管理系统(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.

253 lines
6.3KB

  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. function AdminPWDExists()
  95. {
  96. $req = array(
  97. "method" => "admin_pwd_exists",
  98. "parms" => array(
  99. "name" => "admin",
  100. )
  101. );
  102. return $this->request($req);
  103. }
  104. // 创建DedeBIZ授权密码
  105. function AdminPWDCreate($pwd)
  106. {
  107. $req = array(
  108. "method" => "admin_pwd_create",
  109. "parms" => array(
  110. "pwd" => $pwd,
  111. )
  112. );
  113. return $this->request($req);
  114. }
  115. // 设置首页锁定状态
  116. function AdminSetIndexLockState($pwd,$state)
  117. {
  118. $req = array(
  119. "method" => "admin_set_index_lock_state",
  120. "parms" => array(
  121. "pwd" => $pwd,
  122. "lock_state" => $state,
  123. )
  124. );
  125. return $this->request($req);
  126. }
  127. // 缓存
  128. // $key:键 $val:值 $d:缓存时间
  129. function CacheSet($key,$val,$duration)
  130. {
  131. $req = array(
  132. "method" => "cache_set",
  133. "parms" => array(
  134. "k" => $key,
  135. "v" => $val,
  136. "d" => $duration,
  137. )
  138. );
  139. return $this->request($req);
  140. }
  141. // 获取缓存内容
  142. // $key:键
  143. function CacheGet($key)
  144. {
  145. $req = array(
  146. "method" => "cache_get",
  147. "parms" => array(
  148. "k" => $key,
  149. )
  150. );
  151. return $this->request($req);
  152. }
  153. // 删除缓存内容
  154. // $key:键
  155. function CacheDel($key)
  156. {
  157. $req = array(
  158. "method" => "cache_del",
  159. "parms" => array(
  160. "k" => $key,
  161. )
  162. );
  163. return $this->request($req);
  164. }
  165. // 获取分词结果
  166. // $key:键
  167. function Spliteword($body)
  168. {
  169. $req = array(
  170. "method" => "spliteword",
  171. "parms" => array(
  172. "body" => $body,
  173. )
  174. );
  175. return $this->request($req);
  176. }
  177. // 获取分词结果
  178. // $body:内容 $sep:分隔符
  179. function Pinyin($body,$sep)
  180. {
  181. $req = array(
  182. "method" => "pinyin",
  183. "parms" => array(
  184. "body" => $body,
  185. "sep" => $sep,
  186. )
  187. );
  188. return $this->request($req);
  189. }
  190. // 拼接规则就是method+
  191. function MakeSign(&$req)
  192. {
  193. if (empty($req['timestamp'])) {
  194. $req['timestamp'] = time();
  195. }
  196. if (isset($req['parms']) && count($req['parms']) > 0) {
  197. ksort($req['parms']);
  198. }
  199. $pstr = "appid={$this->appid}method={$req['method']}key={$this->key}";
  200. if (isset($req['parms']) && count($req['parms']) > 0) {
  201. foreach ($req['parms'] as $key => $value) {
  202. $pstr .= "$key=$value";
  203. }
  204. }
  205. $pstr .= "timestamp={$req['timestamp']}";
  206. $req['sign'] = hash("sha256", $pstr);
  207. }
  208. // 校验返回数据是否正确
  209. function CheckSign(&$msg)
  210. {
  211. $rsp = json_decode($msg);
  212. if (!is_object($rsp)) {
  213. return null;
  214. }
  215. $str = sprintf("appid=%skey=%scode=%dmsg=%sdata=%stimestamp=%d", $this->appid, $this->key, $rsp->code, $rsp->msg, $rsp->data, $rsp->timestamp);
  216. if (hash("sha256", $str) === $rsp->sign) {
  217. return $rsp;
  218. } else {
  219. return null;
  220. }
  221. }
  222. // 关闭通信接口
  223. // !!!一次页面操作后一定记得要关闭连接,否则会占用系统资源
  224. function Close()
  225. {
  226. socket_close($this->socket);
  227. }
  228. }