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

282 line
7.0KB

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