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

263 lines
7.2KB

  1. <?php
  2. if (!defined('DEDEINC')) exit('dedebiz');
  3. //Copyright 2020 The DedeBiz Authors. All rights reserved.
  4. //license that can be found in the LICENSE file.
  5. //@copyright Copyright (c) 2022, DedeBIZ.COM
  6. //@license https://www.dedebiz.com/license
  7. //@link https://www.dedebiz.com
  8. //本文件为DedeBIZ商业组件(www.dedebiz.com)PHP SDK
  9. //目的是弥补织梦内容管理系统性能和安全方面的不足,提供更多功能
  10. define("DEDEBIZ", true);
  11. //本文件用于和DedeBIZ商业组件进行通信,以获取更多额外的扩展功能
  12. class DedeBizClient
  13. {
  14. var $socket;
  15. var $appid;
  16. var $key;
  17. var $err;
  18. function __construct($ipaddr, $port)
  19. {
  20. $this->err = "";
  21. if (!function_exists("socket_create")) {
  22. $this->err = (object)array(
  23. "code" => -1,
  24. "data" => null,
  25. "msg" => "请在php.ini开启extension=sockets",
  26. );
  27. return;
  28. }
  29. $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  30. $rs = @socket_connect($this->socket, $ipaddr, $port);
  31. if (!$rs) {
  32. $this->err = (object)array(
  33. "code" => -1,
  34. "data" => null,
  35. "msg" => "连接DedeBiz商业组件服务失败\r\n",
  36. );
  37. return;
  38. }
  39. }
  40. function request(&$req)
  41. {
  42. //进行签名
  43. $this->MakeSign($req);
  44. $str = json_encode($req);
  45. $length = strlen($str);
  46. $s = @socket_write($this->socket, $str, $length);
  47. if (!$s) {
  48. return (object)array(
  49. "code" => -1,
  50. "data" => null,
  51. "msg" => "请求DedeBiz商业组件服务失败\r\n",
  52. );
  53. }
  54. if (!empty($this->err)) {
  55. return $this->err;
  56. }
  57. $msg = "";
  58. while (($str = socket_read($this->socket, 1024)) !== FALSE) {
  59. $msg .= $str;
  60. if (strlen($str) < 1024) {
  61. break;
  62. }
  63. }
  64. return $this->CheckSign($msg);
  65. }
  66. //用户获取当前服务器状态信息
  67. function SystemInfo()
  68. {
  69. $req = array(
  70. "method" => "system_info",
  71. );
  72. return $this->request($req);
  73. }
  74. //检测是否连接
  75. function Ping($i)
  76. {
  77. $req = array(
  78. "method" => "ping",
  79. "parms" => array(
  80. "name" => "www.dedebiz.com",
  81. )
  82. );
  83. return $this->request($req);
  84. }
  85. //发送邮件
  86. function MailSend($to, $subject, $title, $content="", $quote="", $link_url="", $link_title="")
  87. {
  88. $req = array(
  89. "method" => "main_send",
  90. "parms" => array(
  91. "to" => $to,
  92. "subject" => $subject,
  93. "title" => $title,
  94. "content" => $content,
  95. "quote" => $quote,
  96. "link_url" => $link_url,
  97. "link_title" => $link_title,
  98. )
  99. );
  100. return $this->request($req);
  101. }
  102. //获取一个管理员信息
  103. function AdminGetOne()
  104. {
  105. $req = array(
  106. "method" => "admin_get_one",
  107. "parms" => array(
  108. "name" => "admin",
  109. )
  110. );
  111. return $this->request($req);
  112. }
  113. //检查管理员密码是否存在
  114. function AdminPWDExists()
  115. {
  116. $req = array(
  117. "method" => "admin_pwd_exists",
  118. "parms" => array(
  119. "name" => "admin",
  120. )
  121. );
  122. return $this->request($req);
  123. }
  124. //创建DedeBIZ授权密码
  125. function AdminPWDCreate($pwd)
  126. {
  127. $req = array(
  128. "method" => "admin_pwd_create",
  129. "parms" => array(
  130. "pwd" => $pwd,
  131. )
  132. );
  133. return $this->request($req);
  134. }
  135. //设置首页锁定状态
  136. function AdminSetIndexLockState($pwd, $state)
  137. {
  138. $req = array(
  139. "method" => "admin_set_index_lock_state",
  140. "parms" => array(
  141. "pwd" => $pwd,
  142. "lock_state" => $state,
  143. )
  144. );
  145. return $this->request($req);
  146. }
  147. //缓存
  148. //$key:键 $val:值 $d:缓存时间
  149. function CacheSet($key, $val, $duration)
  150. {
  151. $req = array(
  152. "method" => "cache_set",
  153. "parms" => array(
  154. "k" => $key,
  155. "v" => $val,
  156. "d" => (string)$duration,
  157. )
  158. );
  159. return $this->request($req);
  160. }
  161. //获取缓存内容
  162. //$key:键
  163. function CacheGet($key)
  164. {
  165. $req = array(
  166. "method" => "cache_get",
  167. "parms" => array(
  168. "k" => $key,
  169. )
  170. );
  171. return $this->request($req);
  172. }
  173. //删除缓存内容
  174. //$key:键
  175. function CacheDel($key)
  176. {
  177. $req = array(
  178. "method" => "cache_del",
  179. "parms" => array(
  180. "k" => $key,
  181. )
  182. );
  183. return $this->request($req);
  184. }
  185. //获取分词结果
  186. //$key:键
  187. function Spliteword($body)
  188. {
  189. $req = array(
  190. "method" => "spliteword",
  191. "parms" => array(
  192. "body" => $body,
  193. )
  194. );
  195. return $this->request($req);
  196. }
  197. //获取分词结果
  198. //$body:内容 $sep:分隔符
  199. function Pinyin($body, $sep)
  200. {
  201. $req = array(
  202. "method" => "pinyin",
  203. "parms" => array(
  204. "body" => $body,
  205. "sep" => $sep,
  206. )
  207. );
  208. return $this->request($req);
  209. }
  210. //拼接规则就是method+
  211. function MakeSign(&$req)
  212. {
  213. if (empty($req['timestamp'])) {
  214. $req['timestamp'] = time();
  215. }
  216. if (isset($req['parms']) && count($req['parms']) > 0) {
  217. ksort($req['parms']);
  218. }
  219. $pstr = "appid={$this->appid}method={$req['method']}key={$this->key}";
  220. if (isset($req['parms']) && count($req['parms']) > 0) {
  221. foreach ($req['parms'] as $key => $value) {
  222. $pstr .= "$key=$value";
  223. }
  224. }
  225. $pstr .= "timestamp={$req['timestamp']}";
  226. $req['sign'] = hash("sha256", $pstr);
  227. }
  228. //校验返回数据是否正确
  229. function CheckSign(&$msg)
  230. {
  231. $rsp = json_decode($msg);
  232. if (!is_object($rsp)) {
  233. return null;
  234. }
  235. $str = sprintf("appid=%skey=%scode=%dmsg=%sdata=%stimestamp=%d", $this->appid, $this->key, $rsp->code, $rsp->msg, $rsp->data, $rsp->timestamp);
  236. if (hash("sha256", $str) === $rsp->sign) {
  237. return $rsp;
  238. } else {
  239. return null;
  240. }
  241. }
  242. //关闭通信接口
  243. //一次页面操作后一定记得要关闭连接,否则会占用系统资源
  244. function Close()
  245. {
  246. //这里避免重复释放
  247. try {
  248. if (strtolower(get_resource_type($this->socket)) === "socket") {
  249. socket_close($this->socket);
  250. }
  251. return true;
  252. } catch (TypeError $e) {
  253. return false;
  254. } catch (Exception $e) {
  255. return false;
  256. }
  257. }
  258. function __destruct()
  259. {
  260. $this->Close();
  261. }
  262. }