国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

256 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. // 本文件为DedeBIZ商业组件(www.dedebiz.com)PHP SDK
  8. // 目的是弥补织梦内容管理系统性能和安全方面的不足,提供更多功能
  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. // 进行签名
  42. $this->MakeSign($req);
  43. $str = json_encode($req);
  44. $length = strlen($str);
  45. $s = @socket_write($this->socket, $str, $length);
  46. if (!$s) {
  47. return (object)array(
  48. "code" => -1,
  49. "data" => null,
  50. "msg" => "请求DedeBiz商业组件服务失败\r\n",
  51. );
  52. }
  53. if (!empty($this->err)) {
  54. return $this->err;
  55. }
  56. $msg = "";
  57. while (($str = socket_read($this->socket, 1024)) !== FALSE) {
  58. $msg .= $str;
  59. if (strlen($str) < 1024) {
  60. break;
  61. }
  62. }
  63. return $this->CheckSign($msg);
  64. }
  65. // 用户获取当前服务器状态信息
  66. function SystemInfo()
  67. {
  68. $req = array(
  69. "method" => "system_info",
  70. );
  71. return $this->request($req);
  72. }
  73. // 检测是否连接
  74. function Ping($i)
  75. {
  76. $req = array(
  77. "method" => "ping",
  78. "parms" => array(
  79. "name" => "www.dedebiz.com",
  80. )
  81. );
  82. return $this->request($req);
  83. }
  84. // 获取一个管理员信息
  85. function AdminGetOne()
  86. {
  87. $req = array(
  88. "method" => "admin_get_one",
  89. "parms" => array(
  90. "name" => "admin",
  91. )
  92. );
  93. return $this->request($req);
  94. }
  95. // 检查管理员密码是否存在
  96. function AdminPWDExists()
  97. {
  98. $req = array(
  99. "method" => "admin_pwd_exists",
  100. "parms" => array(
  101. "name" => "admin",
  102. )
  103. );
  104. return $this->request($req);
  105. }
  106. // 创建DedeBIZ授权密码
  107. function AdminPWDCreate($pwd)
  108. {
  109. $req = array(
  110. "method" => "admin_pwd_create",
  111. "parms" => array(
  112. "pwd" => $pwd,
  113. )
  114. );
  115. return $this->request($req);
  116. }
  117. // 设置首页锁定状态
  118. function AdminSetIndexLockState($pwd, $state)
  119. {
  120. $req = array(
  121. "method" => "admin_set_index_lock_state",
  122. "parms" => array(
  123. "pwd" => $pwd,
  124. "lock_state" => $state,
  125. )
  126. );
  127. return $this->request($req);
  128. }
  129. // 缓存
  130. // $key:键 $val:值 $d:缓存时间
  131. function CacheSet($key, $val, $duration)
  132. {
  133. $req = array(
  134. "method" => "cache_set",
  135. "parms" => array(
  136. "k" => $key,
  137. "v" => $val,
  138. "d" => $duration,
  139. )
  140. );
  141. return $this->request($req);
  142. }
  143. // 获取缓存内容
  144. // $key:键
  145. function CacheGet($key)
  146. {
  147. $req = array(
  148. "method" => "cache_get",
  149. "parms" => array(
  150. "k" => $key,
  151. )
  152. );
  153. return $this->request($req);
  154. }
  155. // 删除缓存内容
  156. // $key:键
  157. function CacheDel($key)
  158. {
  159. $req = array(
  160. "method" => "cache_del",
  161. "parms" => array(
  162. "k" => $key,
  163. )
  164. );
  165. return $this->request($req);
  166. }
  167. // 获取分词结果
  168. // $key:键
  169. function Spliteword($body)
  170. {
  171. $req = array(
  172. "method" => "spliteword",
  173. "parms" => array(
  174. "body" => $body,
  175. )
  176. );
  177. return $this->request($req);
  178. }
  179. // 获取分词结果
  180. // $body:内容 $sep:分隔符
  181. function Pinyin($body, $sep)
  182. {
  183. $req = array(
  184. "method" => "pinyin",
  185. "parms" => array(
  186. "body" => $body,
  187. "sep" => $sep,
  188. )
  189. );
  190. return $this->request($req);
  191. }
  192. // 拼接规则就是method+
  193. function MakeSign(&$req)
  194. {
  195. if (empty($req['timestamp'])) {
  196. $req['timestamp'] = time();
  197. }
  198. if (isset($req['parms']) && count($req['parms']) > 0) {
  199. ksort($req['parms']);
  200. }
  201. $pstr = "appid={$this->appid}method={$req['method']}key={$this->key}";
  202. if (isset($req['parms']) && count($req['parms']) > 0) {
  203. foreach ($req['parms'] as $key => $value) {
  204. $pstr .= "$key=$value";
  205. }
  206. }
  207. $pstr .= "timestamp={$req['timestamp']}";
  208. $req['sign'] = hash("sha256", $pstr);
  209. }
  210. // 校验返回数据是否正确
  211. function CheckSign(&$msg)
  212. {
  213. $rsp = json_decode($msg);
  214. if (!is_object($rsp)) {
  215. return null;
  216. }
  217. $str = sprintf("appid=%skey=%scode=%dmsg=%sdata=%stimestamp=%d", $this->appid, $this->key, $rsp->code, $rsp->msg, $rsp->data, $rsp->timestamp);
  218. if (hash("sha256", $str) === $rsp->sign) {
  219. return $rsp;
  220. } else {
  221. return null;
  222. }
  223. }
  224. // 关闭通信接口
  225. // !!!一次页面操作后一定记得要关闭连接,否则会占用系统资源
  226. function Close()
  227. {
  228. socket_close($this->socket);
  229. }
  230. }