PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/upgrade.php

https://github.com/cxc222/weibo
PHP | 538 lines | 487 code | 15 blank | 36 comment | 10 complexity | c51dc2082eb5e443b7b818b7316d4122 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * 1. 核心升级 + 应用升级
  4. * 2. 夸版本升级 (即: 数据库的夸版本升级)
  5. */
  6. error_reporting(E_ERROR);
  7. header('Content-type: text/html; charset=UTF-8');
  8. /*** 增加统计代码 ***/
  9. define('SITE_PATH','');
  10. $_REQUEST = array_merge($_GET,$_POST);
  11. $sitehost = h($_REQUEST['site']);
  12. $siteip = h($_SERVER['REMOTE_ADDR']);
  13. $version = h($_REQUEST['version']);
  14. $config = require 'config.inc.php';
  15. $dbconfig = array();
  16. $dbconfig['DB_TYPE'] = $config['DB_TYPE'];
  17. $dbconfig['DB_HOST'] = $config['DB_HOST'];
  18. $dbconfig['DB_NAME'] = $config['DB_NAME'];
  19. $dbconfig['DB_USER'] = $config['DB_USER'];
  20. $dbconfig['DB_PWD'] = $config['DB_PWD'];
  21. $dbconfig['DB_PORT'] = $config['DB_PORT'];
  22. $dbconfig['DB_PREFIX'] = $config['DB_PREFIX'];
  23. $dbconfig['DB_CHARSET'] = $config['DB_CHARSET'];
  24. //实例化数据库
  25. $db = new Db($dbconfig);
  26. $result = $db->query("SELECT * FROM ts_onlive_site WHERE siteip='".$siteip."';");
  27. if(!$result) {
  28. $add_result = $db->execute('INSERT INTO ts_onlive_site (`sitehost`,`siteip`,`sitedata`,`version`) VALUES ("'.$sitehost.'","'.$siteip.'","", "'.$version.'");');
  29. }
  30. $result = array();
  31. // 模拟数据
  32. $result['info'] = 'ThinkSNS V3 预览版于2013年1月21日发布,<a href="http://demo.thinksns.com/t3/">查看详情</a>';
  33. // 输出结果
  34. switch(strtolower($_REQUEST['output_format'])) {
  35. case 'json':
  36. echo json_encode($result);
  37. break;
  38. case 'php':
  39. dump($result);
  40. break;
  41. default:
  42. echo serialize($result);
  43. }
  44. exit();
  45. /**
  46. * 获取应用的最新版本的信息 (包括"核心")
  47. *
  48. * 注意: 本函数不检查版本号的有效性, 请在调用本函数前检查.
  49. *
  50. * @param string $app 应用名
  51. * @param string $now_version 应用当前版本的版本号
  52. * @param string $lastest_version 应用最新版本的版本号
  53. * @return array
  54. * <code>
  55. * array(
  56. * 'error' => '0', // 无错误
  57. * 'error_message' => '', // 无错误信息
  58. * 'has_update' => int, // 0: 无更新 1:有更新
  59. * 'version' => string, // 版本号 [仅核心(Core)时有效]
  60. * 'current_version_number' => int, // 当前版本的版本号
  61. * 'lastest_version_number' => int, // 最新版本的版本号
  62. * 'download_url' => string, // 下载地址
  63. * 'changelog' => text, // ChangeLog
  64. * )
  65. * </code>
  66. */
  67. function getLastestVersionInfo($app, $current_version, $lastest_version)
  68. {
  69. if ($current_version >= $lastest_version)
  70. return array(
  71. 'error' => '0',
  72. 'error_message' => '',
  73. 'has_update' => '0',
  74. 'current_version_number' => $current_version,
  75. 'lastest_version_number' => $lastest_version,);
  76. global $versions;
  77. // 下载地址
  78. $var_download_url = $app . '_download_url_' . $lastest_version;
  79. global $$var_download_url;
  80. $info = array(
  81. 'error' => '0',
  82. 'error_message' => '',
  83. 'has_update' => '1',
  84. 'current_version_number' => $current_version,
  85. 'lastest_version_number' => $lastest_version,
  86. 'download_url' => $$var_download_url);
  87. // 版本名称 (核心升级时必须, 如: ThinkSNS 2.1 Build 10992)
  88. if ($app == 'core') {
  89. $var_core_version = 'core_version_' . $lastest_version;
  90. global $$var_core_version;
  91. $info['lastest_version'] = $$var_core_version;
  92. }
  93. // changelog
  94. $info['changelog'] = '';
  95. foreach ($versions[$app] as $version_no) {
  96. if ($current_version >= $version_no)
  97. continue ;
  98. $var_changelog = $app . '_changelog_' . $version_no;
  99. global $$var_changelog;
  100. $info['changelog'] .= $$var_changelog;
  101. }
  102. // 版本列表
  103. $info['version_number_list'] = $versions[$app];
  104. return $info;
  105. }
  106. // 浏览器友好的输出
  107. function dump($var, $echo=true,$label=null, $strict=true)
  108. {
  109. $label = ($label===null) ? '' : rtrim($label) . ' ';
  110. if(!$strict) {
  111. if (ini_get('html_errors')) {
  112. $output = print_r($var, true);
  113. $output = '<pre style="text-align:left">'.$label.htmlspecialchars($output,ENT_QUOTES).'</pre>';
  114. } else {
  115. $output = $label . " : " . print_r($var, true);
  116. }
  117. }else {
  118. ob_start();
  119. var_dump($var);
  120. $output = ob_get_clean();
  121. if(!extension_loaded('xdebug')) {
  122. $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
  123. $output = '<pre style="text-align:left">'. $label. htmlspecialchars($output, ENT_QUOTES). '</pre>';
  124. }
  125. }
  126. if ($echo) {
  127. echo($output);
  128. return null;
  129. }else
  130. return $output;
  131. }
  132. function h($text) {
  133. //过滤标签
  134. $text = nl2br($text);
  135. $text = htmlspecialchars_decode($text);
  136. $text = strip_tags($text);
  137. $text = str_ireplace(array("\r\t","\n\l","\r","\n","\t","\l","'",'&nbsp;','&amp;'),'',$text);
  138. $text = str_ireplace(array(chr('0001'),chr('0002'),chr('0003'),chr('0004'),chr('0005'),chr('0006'),chr('0007'),chr('0008')),'',$text);
  139. $text = str_ireplace(array(chr('0009'),chr('0010'),chr('0011'),chr('0012'),chr('0013'),chr('0014'),chr('0015'),chr('0016')),'',$text);
  140. $text = str_ireplace(array(chr('0017'),chr('0018'),chr('0019'),chr('0020'),chr('0021'),chr('0022'),chr('0023'),chr('0024')),'',$text);
  141. $text = str_ireplace(array(chr('0025'),chr('0026'),chr('0027'),chr('0028'),chr('0029'),chr('0030'),chr('0031'),chr('0032')),'',$text);
  142. return $text;
  143. }
  144. /**
  145. +------------------------------------------------------------------------------
  146. * ThinkPHP 简洁模式数据库中间层实现类
  147. * 只支持mysql
  148. +------------------------------------------------------------------------------
  149. */
  150. class Db
  151. {
  152. static private $_instance = null;
  153. // 是否显示调试信息 如果启用会在日志文件记录sql语句
  154. public $debug = false;
  155. // 是否使用永久连接
  156. protected $pconnect = false;
  157. // 当前SQL指令
  158. protected $queryStr = '';
  159. // 最后插入ID
  160. protected $lastInsID = null;
  161. // 返回或者影响记录数
  162. protected $numRows = 0;
  163. // 返回字段数
  164. protected $numCols = 0;
  165. // 事务指令数
  166. protected $transTimes = 0;
  167. // 错误信息
  168. protected $error = '';
  169. // 当前连接ID
  170. protected $linkID = null;
  171. // 当前查询ID
  172. protected $queryID = null;
  173. // 是否已经连接数据库
  174. protected $connected = false;
  175. // 数据库连接参数配置
  176. protected $config = '';
  177. // SQL 执行时间记录
  178. protected $beginTime;
  179. /**
  180. +----------------------------------------------------------
  181. * 架构函数
  182. +----------------------------------------------------------
  183. * @access public
  184. +----------------------------------------------------------
  185. * @param array $config 数据库配置数组
  186. +----------------------------------------------------------
  187. */
  188. public function __construct($config=''){
  189. if ( !extension_loaded('mysql') ) {
  190. throw_exception('not support mysql');
  191. }
  192. $this->config = $this->parseConfig($config);
  193. }
  194. /**
  195. +----------------------------------------------------------
  196. * 连接数据库方法
  197. +----------------------------------------------------------
  198. * @access public
  199. +----------------------------------------------------------
  200. * @throws ThinkExecption
  201. +----------------------------------------------------------
  202. */
  203. public function connect() {
  204. if(!$this->connected) {
  205. $config = $this->config;
  206. // 处理不带端口号的socket连接情况
  207. $host = $config['hostname'].($config['hostport']?":{$config['hostport']}":'');
  208. if($this->pconnect) {
  209. $this->linkID = mysql_pconnect( $host, $config['username'], $config['password']);
  210. }else{
  211. $this->linkID = mysql_connect( $host, $config['username'], $config['password'],true);
  212. }
  213. if ( !$this->linkID || (!empty($config['database']) && !mysql_select_db($config['database'], $this->linkID)) ) {
  214. throw_exception(mysql_error());
  215. }
  216. $dbVersion = mysql_get_server_info($this->linkID);
  217. if ($dbVersion >= "4.1") {
  218. //使用UTF8存取数据库 需要mysql 4.1.0以上支持
  219. mysql_query("SET NAMES 'UTF8'", $this->linkID);
  220. }
  221. //设置 sql_model
  222. if($dbVersion >'5.0.1'){
  223. mysql_query("SET sql_mode=''",$this->linkID);
  224. }
  225. // 标记连接成功
  226. $this->connected = true;
  227. // 注销数据库连接配置信息
  228. unset($this->config);
  229. }
  230. }
  231. /**
  232. +----------------------------------------------------------
  233. * 释放查询结果
  234. +----------------------------------------------------------
  235. * @access public
  236. +----------------------------------------------------------
  237. */
  238. public function free() {
  239. mysql_free_result($this->queryID);
  240. $this->queryID = 0;
  241. }
  242. /**
  243. +----------------------------------------------------------
  244. * 执行查询 主要针对 SELECT, SHOW 等指令
  245. * 返回数据集
  246. +----------------------------------------------------------
  247. * @access public
  248. +----------------------------------------------------------
  249. * @param string $str sql指令
  250. +----------------------------------------------------------
  251. * @return mixed
  252. +----------------------------------------------------------
  253. * @throws ThinkExecption
  254. +----------------------------------------------------------
  255. */
  256. public function query($str='') {
  257. $this->connect();
  258. if ( !$this->linkID ) return false;
  259. if ( $str != '' ) $this->queryStr = $str;
  260. //释放前次的查询结果
  261. if ( $this->queryID ) { $this->free(); }
  262. $this->Q(1);
  263. $this->queryID = mysql_query($this->queryStr, $this->linkID);
  264. $this->debug();
  265. if ( !$this->queryID ) {
  266. if ( $this->debug )
  267. throw_exception($this->error());
  268. else
  269. return false;
  270. } else {
  271. $this->numRows = mysql_num_rows($this->queryID);
  272. return $this->getAll();
  273. }
  274. }
  275. /**
  276. +----------------------------------------------------------
  277. * 执行语句 针对 INSERT, UPDATE 以及DELETE
  278. +----------------------------------------------------------
  279. * @access public
  280. +----------------------------------------------------------
  281. * @param string $str sql指令
  282. +----------------------------------------------------------
  283. * @return integer
  284. +----------------------------------------------------------
  285. * @throws ThinkExecption
  286. +----------------------------------------------------------
  287. */
  288. public function execute($str='') {
  289. $this->connect();
  290. if ( !$this->linkID ) return false;
  291. if ( $str != '' ) $this->queryStr = $str;
  292. //释放前次的查询结果
  293. if ( $this->queryID ) { $this->free(); }
  294. $this->W(1);
  295. $result = mysql_query($this->queryStr, $this->linkID) ;
  296. $this->debug();
  297. if ( false === $result) {
  298. if ( $this->debug )
  299. throw_exception($this->error());
  300. else
  301. return false;
  302. } else {
  303. $this->numRows = mysql_affected_rows($this->linkID);
  304. $this->lastInsID = mysql_insert_id($this->linkID);
  305. return $this->numRows;
  306. }
  307. }
  308. /**
  309. +----------------------------------------------------------
  310. * 获得所有的查询数据
  311. +----------------------------------------------------------
  312. * @access public
  313. +----------------------------------------------------------
  314. * @return array
  315. +----------------------------------------------------------
  316. * @throws ThinkExecption
  317. +----------------------------------------------------------
  318. */
  319. public function getAll() {
  320. if ( !$this->queryID ) {
  321. throw_exception($this->error());
  322. return false;
  323. }
  324. //返回数据集
  325. $result = array();
  326. if($this->numRows >0) {
  327. while($row = mysql_fetch_assoc($this->queryID)){
  328. $result[] = $row;
  329. }
  330. mysql_data_seek($this->queryID,0);
  331. }
  332. return $result;
  333. }
  334. /**
  335. +----------------------------------------------------------
  336. * 关闭数据库
  337. +----------------------------------------------------------
  338. * @access public
  339. +----------------------------------------------------------
  340. * @throws ThinkExecption
  341. +----------------------------------------------------------
  342. */
  343. public function close() {
  344. if (!empty($this->queryID))
  345. mysql_free_result($this->queryID);
  346. if ($this->linkID && !mysql_close($this->linkID)){
  347. throw_exception($this->error());
  348. }
  349. $this->linkID = 0;
  350. }
  351. /**
  352. +----------------------------------------------------------
  353. * 数据库错误信息
  354. * 并显示当前的SQL语句
  355. +----------------------------------------------------------
  356. * @access public
  357. +----------------------------------------------------------
  358. * @return string
  359. +----------------------------------------------------------
  360. */
  361. public function error() {
  362. $this->error = mysql_error($this->linkID);
  363. if($this->queryStr!=''){
  364. $this->error .= "\n [ SQL语句 ] : ".$this->queryStr;
  365. }
  366. return $this->error;
  367. }
  368. /**
  369. +----------------------------------------------------------
  370. * SQL指令安全过滤
  371. +----------------------------------------------------------
  372. * @access public
  373. +----------------------------------------------------------
  374. * @param string $str SQL字符串
  375. +----------------------------------------------------------
  376. * @return string
  377. +----------------------------------------------------------
  378. */
  379. public function escape_string($str) {
  380. return mysql_escape_string($str);
  381. }
  382. /**
  383. +----------------------------------------------------------
  384. * 析构方法
  385. +----------------------------------------------------------
  386. * @access public
  387. +----------------------------------------------------------
  388. */
  389. public function __destruct()
  390. {
  391. // 关闭连接
  392. $this->close();
  393. }
  394. /**
  395. +----------------------------------------------------------
  396. * 取得数据库类实例
  397. +----------------------------------------------------------
  398. * @static
  399. * @access public
  400. +----------------------------------------------------------
  401. * @return mixed 返回数据库驱动类
  402. +----------------------------------------------------------
  403. */
  404. public static function getInstance($db_config='')
  405. {
  406. if ( self::$_instance==null ){
  407. self::$_instance = new Db($db_config);
  408. }
  409. return self::$_instance;
  410. }
  411. /**
  412. +----------------------------------------------------------
  413. * 分析数据库配置信息,支持数组和DSN
  414. +----------------------------------------------------------
  415. * @access private
  416. +----------------------------------------------------------
  417. * @param mixed $db_config 数据库配置信息
  418. +----------------------------------------------------------
  419. * @return string
  420. +----------------------------------------------------------
  421. */
  422. private function parseConfig($_db_config='') {
  423. // 如果配置为空,读取配置文件设置
  424. $db_config = array (
  425. 'dbms' => $_db_config['DB_TYPE'],
  426. 'username' => $_db_config['DB_USER'],
  427. 'password' => $_db_config['DB_PWD'],
  428. 'hostname' => $_db_config['DB_HOST'],
  429. 'hostport' => $_db_config['DB_PORT'],
  430. 'database' => $_db_config['DB_NAME'],
  431. 'dsn' => $_db_config['DB_DSN'],
  432. 'params' => $_db_config['DB_PARAMS'],
  433. );
  434. return $db_config;
  435. }
  436. /**
  437. +----------------------------------------------------------
  438. * 数据库调试 记录当前SQL
  439. +----------------------------------------------------------
  440. * @access protected
  441. +----------------------------------------------------------
  442. */
  443. protected function debug() {
  444. // 记录操作结束时间
  445. if ( $this->debug ) {
  446. $runtime = number_format(microtime(TRUE) - $this->beginTime, 6);
  447. Log::record(" RunTime:".$runtime."s SQL = ".$this->queryStr,Log::SQL);
  448. }
  449. }
  450. /**
  451. +----------------------------------------------------------
  452. * 查询次数更新或者查询
  453. +----------------------------------------------------------
  454. * @access public
  455. +----------------------------------------------------------
  456. * @param mixed $times
  457. +----------------------------------------------------------
  458. * @return void
  459. +----------------------------------------------------------
  460. */
  461. public function Q($times='') {
  462. static $_times = 0;
  463. if(empty($times)) {
  464. return $_times;
  465. }else{
  466. $_times++;
  467. // 记录开始执行时间
  468. $this->beginTime = microtime(TRUE);
  469. }
  470. }
  471. /**
  472. +----------------------------------------------------------
  473. * 写入次数更新或者查询
  474. +----------------------------------------------------------
  475. * @access public
  476. +----------------------------------------------------------
  477. * @param mixed $times
  478. +----------------------------------------------------------
  479. * @return void
  480. +----------------------------------------------------------
  481. */
  482. public function W($times='') {
  483. static $_times = 0;
  484. if(empty($times)) {
  485. return $_times;
  486. }else{
  487. $_times++;
  488. // 记录开始执行时间
  489. $this->beginTime = microtime(TRUE);
  490. }
  491. }
  492. /**
  493. +----------------------------------------------------------
  494. * 获取最近一次查询的sql语句
  495. +----------------------------------------------------------
  496. * @access public
  497. +----------------------------------------------------------
  498. * @return string
  499. +----------------------------------------------------------
  500. */
  501. public function getLastSql() {
  502. return $this->queryStr;
  503. }
  504. }//类定义结束