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

291 lines
8.1KB

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