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

375 lines
12KB

  1. <?php
  2. require_once(dirname(__FILE__)."/config.php");
  3. CheckPurview('api_ucenter');
  4. if(!function_exists('file_put_contents')){ function file_put_contents($filename, $s)
  5. {
  6. $fp = @fopen($filename, 'w');
  7. @fwrite($fp, $s);
  8. @fclose($fp);
  9. return TRUE;
  10. }}
  11. require_once(DEDEINC.'/dedetemplate.class.php');
  12. if(file_exists(DEDEROOT.'/uc_client/client.php'))
  13. {
  14. if(!defined('UC_API')) define('UC_API', '');
  15. include_once DEDEROOT.'/uc_client/client.php';
  16. }
  17. else
  18. {
  19. ShowMsg('请安装UCenter模块!',-1);
  20. exit();
  21. }
  22. $dopost = api_gpc('dopost','R');
  23. $uc = new api_ucenter($dopost);
  24. class api_ucenter
  25. {
  26. var $action;
  27. var $dtp;
  28. var $config;
  29. //php5构造函数PHP>=5.0
  30. function __construct($ac = '')
  31. {
  32. $action = 'uc_'.(empty($ac)||(!in_array($ac,array('install','edit'))) ? 'show' : trim($ac));
  33. $this->dtp = new DedeTemplate();
  34. $this->config = DEDEINC.'/common.inc.php';
  35. $this->$action();
  36. }
  37. //构造类成员PHP<5.0
  38. function api_ucenter($ac = '')
  39. {
  40. $this->__construct($ac);
  41. }
  42. function uc_install()
  43. {
  44. $uc_setings = api_gpc('uc_setings','P');
  45. if(!isset($uc_setings['authkey']) || empty($uc_setings['authkey']))
  46. {
  47. ShowMsg('请填写uc创始人密码!',-1);
  48. exit();
  49. }
  50. $uc_setings['ucapi'] = preg_replace("/\/$/", '', trim($uc_setings['ucapi']));
  51. if(empty($uc_setings['ucapi']) || !preg_match("/^(http:\/\/)/i", $uc_setings['ucapi']))
  52. {
  53. ShowMsg('请填正确的服务端地址以http://开头!',-1);
  54. exit();
  55. }
  56. else
  57. {
  58. if(!$uc_setings['ucip'])
  59. {
  60. $temp = @parse_url($uc_setings['ucapi']);
  61. $uc_setings['ucapi'] = gethostbyname($temp['host']);
  62. if(ip2long($uc_setings['ucapi']) == -1 || ip2long($uc_setings['ucapi']) === FALSE)
  63. {
  64. $uc_setings['ucip'] = '127.0.0.1';
  65. }
  66. }
  67. }
  68. $ucinfo = api_fopen($uc_setings['ucapi'].'/index.php?m=app&a=ucinfo', 500, '', '', 1, $uc_setings['ucip']);
  69. list($status, $ucversion, $ucrelease, $uccharset, $ucdbcharset, $apptypes) = explode('|', $ucinfo);
  70. if($status != 'UC_STATUS_OK')
  71. {
  72. ShowMsg('uc服务端地址无效,请仔细检查您安装的uc服务端地址!',-1);
  73. exit();
  74. }
  75. else
  76. {
  77. $ucdbcharset = strtolower($ucdbcharset ? str_replace('-', '', $ucdbcharset) : $ucdbcharset);
  78. if(UC_CLIENT_VERSION > $ucversion)
  79. {
  80. ShowMsg('uc服务端版本不一致,您当前的uc客服端版本为:'.UC_CLIENT_VERSION.',而服务端版本为:'.$ucversion.'!',-1);
  81. exit();
  82. }
  83. elseif($ucdbcharset != 'gbk')
  84. {
  85. ShowMsg('uc服务端编码与DedeCMS编码不一致!要求您的uc服务端编码为:gbk编码.',-1);
  86. exit();
  87. }
  88. //标签应用模板
  89. $app_tagtemplates = 'apptagtemplates[template]='.urlencode('<a href="{url}" target="_blank">{title}</a>').'&'.
  90. 'apptagtemplates[fields][title]='.urlencode('标题').'&'.
  91. 'apptagtemplates[fields][writer]='.urlencode('作者').'&'.
  92. 'apptagtemplates[fields][pubdate]='.urlencode('时间').'&'.
  93. 'apptagtemplates[fields][url]='.urlencode('地址');
  94. $postdata = 'm=app&a=add&ucfounder=&ucfounderpw='.urlencode($uc_setings['authkey']).'&apptype=OTHER&appname='.urlencode($GLOBALS['cfg_webname']).'&appurl='.urlencode($GLOBALS['cfg_basehost']).'&appip=&appcharset=gbk&appdbcharset=gbk&'.$app_tagtemplates.'&release='.UC_CLIENT_RELEASE;
  95. $ucconfig = api_fopen($uc_setings['ucapi'].'/index.php', 500, $postdata, '', 1, $uc_setings['ucip']);
  96. if(strstr($ucconfig,'<?xml'))
  97. {
  98. $temp = explode('<?xml', $ucconfig);
  99. $ucconfig = $temp[0]; unset($temp);
  100. }
  101. if(empty($ucconfig))
  102. {
  103. ShowMsg('请填写有效的配置信息!',-1);
  104. exit();
  105. }
  106. elseif($ucconfig == '-1')
  107. {
  108. ShowMsg('创始人密码错误!',-1);
  109. exit();
  110. }
  111. else
  112. {
  113. list($appauthkey, $appid) = explode('|', $ucconfig);
  114. if(empty($appauthkey) || empty($appid))
  115. {
  116. ShowMsg('数据获取失败!',-1);
  117. exit();
  118. }
  119. elseif($succeed = api_write_config($ucconfig."|".$uc_setings['ucapi']."|".$uc_setings['ucip'], $this->config))
  120. {
  121. ShowMsg('安装成功!',-1);
  122. exit();
  123. }
  124. else
  125. {
  126. ShowMsg('写入配置数据失败!'.$this->config.' 请设置可写权限!',-1);
  127. exit();
  128. }
  129. }
  130. }
  131. }
  132. function uc_edit()
  133. {
  134. $uc_setings = api_gpc('uc_setings','P');
  135. $uc_dbpass = $uc_setings['dbpass'] == '********' ? UC_DBPW : $uc_setings['dbpass'];
  136. $fp = fopen($this->config, 'r');
  137. $content = fread($fp, filesize($this->config));
  138. $content = trim($content);
  139. $content = substr($content, -2) == '?>' ? substr($content, 0, -2) : $content;
  140. $content = strstr($content, '_|cfg_|GLOBALS') ? str_replace('_|cfg_|GLOBALS','cfg_|GLOBALS',$content) : $content;
  141. fclose($fp);
  142. $connect = '';
  143. if($uc_setings['connect'])
  144. {
  145. $uc_dblink = @mysql_connect($uc_setings['dbhost'], $uc_setings['dbuser'], $uc_dbpass, 1);
  146. if(!$uc_dblink)
  147. {
  148. ShowMsg('数据库连接失败!',-1);
  149. exit();
  150. }else{
  151. mysql_close($uc_dblink);
  152. }
  153. $connect = 'mysql';
  154. $content = api_insert_config($content, "/define\('UC_DBHOST',\s*'.*?'\);/i", "define('UC_DBHOST', '".$uc_setings['dbhost']."');");
  155. $content = api_insert_config($content, "/define\('UC_DBUSER',\s*'.*?'\);/i", "define('UC_DBUSER', '".$uc_setings['dbuser']."');");
  156. $content = api_insert_config($content, "/define\('UC_DBPW',\s*'.*?'\);/i", "define('UC_DBPW', '".$uc_dbpass."');");
  157. $content = api_insert_config($content, "/define\('UC_DBNAME',\s*'.*?'\);/i", "define('UC_DBNAME', '".$uc_setings['dbname']."');");
  158. $content = api_insert_config($content, "/define\('UC_DBTABLEPRE',\s*'.*?'\);/i", "define('UC_DBTABLEPRE', '`".$uc_setings['dbname'].'`.'.$uc_setings['dbtablepre']."');");
  159. }
  160. $content = api_insert_config($content, "/define\('UC_CONNECT',\s*'.*?'\);/i", "define('UC_CONNECT', '$connect');");
  161. $content = api_insert_config($content, "/define\('UC_KEY',\s*'.*?'\);/i", "define('UC_KEY', '".$uc_setings['authkey']."');");
  162. $content = api_insert_config($content, "/define\('UC_API',\s*'.*?'\);/i", "define('UC_API', '".$uc_setings['ucapi']."');");
  163. $content = api_insert_config($content, "/define\('UC_IP',\s*'.*?'\);/i", "define('UC_IP', '".$uc_setings['ucip']."');");
  164. $content = api_insert_config($content, "/define\('UC_APPID',\s*'?.*?'?\);/i", "define('UC_APPID', '".UC_APPID."');");
  165. $content .= '?>';
  166. if($fp = @fopen($this->config, 'w'))
  167. {
  168. @fwrite($fp, trim($content));
  169. @fclose($fp);
  170. ShowMsg('配置已经更改!',-1);
  171. exit();
  172. }else{
  173. ShowMsg('写入配置数据失败!'.$this->config.' 请设置可写权限!',-1);
  174. exit();
  175. }
  176. }
  177. function uc_show()
  178. {
  179. $this->dtp->Assign('uc_config_file',$this->config);
  180. if(!defined('UC_APPID'))
  181. {
  182. $this->dtp->LoadTemplate(DEDEADMIN.'/templets/api_ucenter_install.htm');
  183. }
  184. else
  185. {
  186. $uc_api_open = false;
  187. $ucapparray = uc_app_ls();
  188. foreach($ucapparray as $apparray)
  189. {
  190. if($apparray['appid'] == UC_APPID)
  191. {
  192. $uc_api_open = true;
  193. break;
  194. }
  195. }
  196. if(!$uc_api_open)
  197. {
  198. ShowMsg("DedeCMS没找到正确的uc配置!",-1);
  199. exit();
  200. }
  201. list($dbname,$dbtablepre) = explode('.',str_replace('`','',UC_DBTABLEPRE));
  202. $uc_setings = array('appid' => UC_APPID, 'ucapi' => UC_API, 'connect' => UC_CONNECT, 'dbhost' => UC_DBHOST, 'dbuser' => UC_DBUSER,'dbpass' => UC_DBPW, 'dbname' => $dbname, 'dbtablepre' => $dbtablepre,'ucip' => UC_IP,'authkey' => UC_KEY);
  203. $this->dtp->Assign('uc_setings',$uc_setings);
  204. $this->dtp->LoadTemplate(DEDEADMIN.'/templets/api_ucenter_edit.htm');
  205. }
  206. $this->dtp->Display();
  207. exit();
  208. }
  209. }
  210. /*
  211. class uc_function{...}
  212. */
  213. function api_fopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE)
  214. {
  215. $return = '';
  216. $matches = parse_url($url);
  217. $host = $matches['host'];
  218. $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
  219. $port = !empty($matches['port']) ? $matches['port'] : 80;
  220. if($post)
  221. {
  222. $out = "POST $path HTTP/1.0\r\n";
  223. $out .= "Accept: */*\r\n";
  224. //$out .= "Referer: $boardurl\r\n";
  225. $out .= "Accept-Language: zh-cn\r\n";
  226. $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
  227. $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  228. $out .= "Host: $host\r\n";
  229. $out .= 'Content-Length: '.strlen($post)."\r\n";
  230. $out .= "Connection: Close\r\n";
  231. $out .= "Cache-Control: no-cache\r\n";
  232. $out .= "Cookie: $cookie\r\n\r\n";
  233. $out .= $post;
  234. }else{
  235. $out = "GET $path HTTP/1.0\r\n";
  236. $out .= "Accept: */*\r\n";
  237. //$out .= "Referer: $boardurl\r\n";
  238. $out .= "Accept-Language: zh-cn\r\n";
  239. $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  240. $out .= "Host: $host\r\n";
  241. $out .= "Connection: Close\r\n";
  242. $out .= "Cookie: $cookie\r\n\r\n";
  243. }
  244. $fp = @fsockopen(($host ? $host : $ip), $port, $errno, $errstr, $timeout);
  245. if(!$fp)
  246. {
  247. return '';
  248. }else{
  249. stream_set_blocking($fp, $block);
  250. stream_set_timeout($fp, $timeout);
  251. @fwrite($fp, $out);
  252. $status = stream_get_meta_data($fp);
  253. if(!$status['timed_out'])
  254. {
  255. while (!feof($fp))
  256. {
  257. if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n"))
  258. {
  259. break;
  260. }
  261. }
  262. $stop = false;
  263. while(!feof($fp) && !$stop)
  264. {
  265. $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
  266. $return .= $data;
  267. if($limit)
  268. {
  269. $limit -= strlen($data);
  270. $stop = $limit <= 0;
  271. }
  272. }
  273. }
  274. @fclose($fp);
  275. return $return;
  276. }
  277. }
  278. function api_write_config($config, $file)
  279. {
  280. $success = false;
  281. list($appauthkey, $appid, $ucdbhost, $ucdbname, $ucdbuser, $ucdbpw, $ucdbcharset, $uctablepre, $uccharset, $ucapi, $ucip) = explode('|', $config);
  282. if($content = file_get_contents($file))
  283. {
  284. $content = trim($content);
  285. $content = substr($content, -2) == '?>' ? substr($content, 0, -2) : $content;
  286. $content = strstr($content, '_|cfg_|GLOBALS') ? str_replace('_|cfg_|GLOBALS','cfg_|GLOBALS',$content) : $content;
  287. $link = mysql_connect($ucdbhost, $ucdbuser, $ucdbpw, 1);
  288. $uc_connnect = $link && mysql_select_db($ucdbname, $link) ? 'mysql' : '';
  289. $content = api_insert_config($content, "/define\('UC_CONNECT',\s*'.*?'\);/i", "define('UC_CONNECT', '$uc_connnect');");
  290. $content = api_insert_config($content, "/define\('UC_DBHOST',\s*'.*?'\);/i", "define('UC_DBHOST', '$ucdbhost');");
  291. $content = api_insert_config($content, "/define\('UC_DBUSER',\s*'.*?'\);/i", "define('UC_DBUSER', '$ucdbuser');");
  292. $content = api_insert_config($content, "/define\('UC_DBPW',\s*'.*?'\);/i", "define('UC_DBPW', '$ucdbpw');");
  293. $content = api_insert_config($content, "/define\('UC_DBNAME',\s*'.*?'\);/i", "define('UC_DBNAME', '$ucdbname');");
  294. $content = api_insert_config($content, "/define\('UC_DBCHARSET',\s*'.*?'\);/i", "define('UC_DBCHARSET', '$ucdbcharset');");
  295. $content = api_insert_config($content, "/define\('UC_DBTABLEPRE',\s*'.*?'\);/i", "define('UC_DBTABLEPRE', '`$ucdbname`.$uctablepre');");
  296. $content = api_insert_config($content, "/define\('UC_DBCONNECT',\s*'.*?'\);/i", "define('UC_DBCONNECT', '0');");
  297. $content = api_insert_config($content, "/define\('UC_KEY',\s*'.*?'\);/i", "define('UC_KEY', '$appauthkey');");
  298. $content = api_insert_config($content, "/define\('UC_API',\s*'.*?'\);/i", "define('UC_API', '$ucapi');");
  299. $content = api_insert_config($content, "/define\('UC_CHARSET',\s*'.*?'\);/i", "define('UC_CHARSET', '$uccharset');");
  300. $content = api_insert_config($content, "/define\('UC_IP',\s*'.*?'\);/i", "define('UC_IP', '$ucip');");
  301. $content = api_insert_config($content, "/define\('UC_APPID',\s*'?.*?'?\);/i", "define('UC_APPID', '$appid');");
  302. $content = api_insert_config($content, "/define\('UC_PPP',\s*'?.*?'?\);/i", "define('UC_PPP', '20');");
  303. $content .= "\r\n".'?>';
  304. if(@file_put_contents($file, $content))
  305. {
  306. $success = true;
  307. }
  308. }
  309. return $success;
  310. }
  311. function api_insert_config($s, $find, $replace)
  312. {
  313. if(preg_match($find, $s))
  314. {
  315. $s = preg_replace($find, $replace, $s);
  316. }else{
  317. // 插入到最后一行
  318. $s .= "\r\n".$replace;
  319. }
  320. return $s;
  321. }
  322. function api_gpc($k, $var='R')
  323. {
  324. switch($var)
  325. {
  326. case 'G': $var = &$_GET; break;
  327. case 'P': $var = &$_POST; break;
  328. case 'C': $var = &$_COOKIE; break;
  329. case 'R': $var = &$_REQUEST; break;
  330. }
  331. return isset($var[$k]) ? $var[$k] : NULL;
  332. }
  333. if(!function_exists('file_put_contents')){ function file_put_contents($filename, $s)
  334. {
  335. $fp = @fopen($filename, 'w');
  336. @fwrite($fp, $s);
  337. @fclose($fp);
  338. return TRUE;
  339. }}
  340. ?>