您现在的位置是:首页 > 文章详情

HDwiki+discuz在启用https下的整合问题

日期:2018-09-05点击:383

这几天在折腾HDwiki+discuz,系统环境是FreeBSD 10.4, PHP 5.6(HDwiki要求),PHP-PDO_MYSQL,PHP-GD,PHP-XML等,MYSQL 5.7

HDwiki 5.1(6.0安装成功后打开前台,提示http 500错误,找不到原因)
discuz X3.4

因为宽带运营商关闭了80端口,443端口有开,所以就直接用上https,结果问题一大堆。

首先,discuz和ucenter在https下通信失败,后来找到修改方法:

DISCUZ 开启https后ucenter通信失败解决方法,一般是做完301重定向https后通信失败的,下面是具体解决方法:

打开目录 uc_server/model/misc.php 文件;找到69行,插入下面代码:

...... $port = !empty($matches['port']) ? $matches['port'] : ($matches['scheme'] == 'https' ? 443 : 80); //以下为插入代码 if(substr($url,0,5)=='https'){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if($post){ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); } if($cookie){ curl_setopt($ch, CURLOPT_COOKIE, $cookie); } curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); return curl_exec($ch); } //插入代码结束 if($post) {             $out = "POST $path HTTP/1.0\r\n"; ......

接下来设置HDwiki和ucenter,问题比较多了。
一开始,只要设置https,就提示“您输入的URL地址不正确!”
网上也没有任何资料,经过调试,发现要修改HDwiki目录下的control/admin_setting.php的659行

原来长这样:

if(empty($ucapi) || !preg_match("/^(http:\/\/)/i", $ucapi)) {


修改成这样:

if(empty($ucapi) || !preg_match("/^((http:|https:)\/\/)/i", $ucapi)) {

这个问题解决后,出现的是“uc_url_unreachable”错误。走了不少弯路后,发现还是https的问题。
admin_setting.php调用了api/uc_client/client.php里的uc_fopen(),而这个函数使用fsockopen模拟浏览器进行访问。
并且只有拼接了http协议的情况。对这个接口文件的分析,见这篇文章http://blog.csdn.net/yanhui_wei/article/details/17919645

综合这些分析,比较一下discuz的uc_server/model/misc.php和HDwiki下的api/uc_client/client.php,会发现:

discuz的uc_server/model/misc.php里的function dfopen2()和 function dfopen()与HDwiki下的api/uc_client/client.php的uc_fopen2()和uc_fopen()基本上是类似的。

所以解决方法如下:
1、拷贝discuz下的uc_client目录,替换HDwiki下的api/uc_client目录
2、修改api/uc_client/client.php文件,重点在于用function dfopen2()和 function dfopen()的内容替换uc_fopen2()和uc_fopen(),修改完成的结果如下:

function uc_fopen2($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE, $encodetype  = 'URLENCODE') {         $__times__ = isset($_GET['__times__']) ? intval($_GET['__times__']) + 1 : 1;         if($__times__ > 2) {             return '';         }         $url .= (strpos($url, '?') === FALSE ? '?' : '&')."__times__=$__times__";         return uc_fopen($url, $limit, $post, $cookie, $bysocket, $ip, $timeout, $block, $encodetype);     } function uc_fopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE    , $ip = '', $timeout = 15, $block = TRUE, $encodetype  = 'URLENCODE') {         $return = '';         $matches = parse_url($url);         $scheme = $matches['scheme'];         $host = $matches['host'];         $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';         $port = !empty($matches['port']) ? $matches['port'] : ($matches['scheme'] == 'https' ? 443 : 80);         /*         //以下内容可以不需要         if($scheme=='https'){             $curl = curl_init();             curl_setopt($curl, CURLOPT_URL, $url);             curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);             if($post) {                 curl_setopt($curl, CURLOPT_POST, 1);                 curl_setopt($curl, CURLOPT_POSTFIELDS, $post);             }             if($cookie) {                 curl_setopt($curl, CURLOPT_COOKIE, $cookie);             }             curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);             curl_setopt($curl, CURLOPT_HEADER, 0);             curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);             $return = curl_exec($curl);             if (curl_errno($curl)) {                 echo '<pre><b>错误:</b><br />'.curl_error($curl);              }             curl_close($curl);             return $return;         }         // 结束         */           if($post) {             $out = "POST $path HTTP/1.0\r\n";             $header = "Accept: */*\r\n";             $header .= "Accept-Language: zh-cn\r\n";             $boundary = $encodetype == 'URLENCODE' ? '' : ';'.substr($post, 0, trim(strpos($post, "\n")));             $header .= $encodetype == 'URLENCODE' ? "Content-Type: application/x-www-form-urlencoded\r\n" : "Content-Type: multipart/form-data$boundary\r\n";             $header .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";             $header .= "Host: $host:$port\r\n";             $header .= 'Content-Length: '.strlen($post)."\r\n";             $header .= "Connection: Close\r\n";             $header .= "Cache-Control: no-cache\r\n";             $header .= "Cookie: $cookie\r\n\r\n";             $out .= $header.$post;         } else {             $out = "GET $path HTTP/1.0\r\n";             $header = "Accept: */*\r\n";             $header .= "Accept-Language: zh-cn\r\n";             $header .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";             $header .= "Host: $host:$port\r\n";             $header .= "Connection: Close\r\n";             $header .= "Cookie: $cookie\r\n\r\n";             $out .= $header;         }         $fpflag = 0;         if(!$fp = @fsocketopen(($scheme == 'https' ? 'ssl' : $scheme).'://'.($scheme == 'https' ? $host : ($ip ? $ip : $host)), $port, $errno, $errstr, $timeout)) {             $context = array(                 'http' => array(                     'method' => $post ? 'POST' : 'GET',                     'header' => $header,                     'content' => $post,                     'timeout' => $timeout,                 ),             );             $context = stream_context_create($context);             $fp = @fopen($scheme.'://'.($scheme == 'https' ? $host : ($ip ? $ip : $host)).':'.$port.$path, 'b', false, $context);             $fpflag = 1;         }         if(!$fp) {             return '';         } else {             stream_set_blocking($fp, $block);             stream_set_timeout($fp, $timeout);             @fwrite($fp, $out);             $status = stream_get_meta_data($fp);             if(!$status['timed_out']) {                 while (!feof($fp) && !$fpflag) {                     if(($header = @fgets($fp)) && ($header == "\r\n" ||  $header == "\n")) {                         break;                     }                 }                 $stop = false;                 while(!feof($fp) && !$stop) {                     $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));                     $return .= $data;                     if($limit) {                         $limit -= strlen($data);                         $stop = $limit <= 0;                     }                 }             }             @fclose($fp);             return $return;         }     } .... //找到同步登录登出代码,修改成以下内容 /**  * 进入同步登录代码  *  * @param int $uid        用户ID  * @return string         HTML代码  */ function uc_user_synlogin($uid) {     $uid = intval($uid);     $return = uc_api_post('user', 'synlogin', array('uid'=>$uid));     return $return; } /**  * 进入同步登出代码  *  * @return string         HTML代码  */ function uc_user_synlogout() {     $return = uc_api_post('user', 'synlogout', array());     return $return; } ....


然后就可以看到一切都正常了。
另外需要注意的是,必须保证ssl证书在有效期内,因为这个,浪费了2天的时间。

原文链接:https://yq.aliyun.com/articles/636274
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章