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

299 lines
8.4KB

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