function.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * environmental check
  4. */
  5. function env_check(&$env_items) {
  6. $env_items[] = array('name' => '操作系统', 'min' => '无限制', 'good' => 'linux', 'cur'=>PHP_OS, 'status' => 1);
  7. $env_items[] = array('name' => 'PHP版本', 'min' => '5.3', 'good' => '5.4', 'cur' => PHP_VERSION, 'status'=>(PHP_VERSION < 5.3 ? 0:1));
  8. $tmp = function_exists('gd_info') ? gd_info() : array();
  9. preg_match("/[\d.]+/", $tmp['GD Version'],$match);
  10. unset($tmp);
  11. $env_items[] = array('name' => 'GD库', 'min' => '2.0', 'good' => '2.0', 'cur' => $match[0], 'status' => ($match[0] < 2 ? 0:1));
  12. $env_items[] = array('name' => '附件上传', 'min' => '未限制', 'good' => '2M','cur' => ini_get('upload_max_filesize'), 'status' => 1);
  13. $disk_place = function_exists('disk_free_space') ? floor(disk_free_space(ROOT_PATH) / (1024*1024)) : 0;
  14. $env_items[] = array('name' => '磁盘空间', 'min' => '100M', 'good' => '>100M','cur' => empty($disk_place) ? '未知' : $disk_place.'M', 'status' => $disk_place < 100 ? 0:1);
  15. }
  16. /**
  17. * file check
  18. */
  19. function dirfile_check(&$dirfile_items) {
  20. foreach($dirfile_items as $key => $item) {
  21. $item_path = '/'.$item['path'];
  22. if($item['type'] == 'dir') {
  23. if(!dir_writeable(ROOT_PATH.$item_path)) {
  24. if(is_dir(ROOT_PATH.$item_path)) {
  25. $dirfile_items[$key]['status'] = 0;
  26. $dirfile_items[$key]['current'] = '+r';
  27. } else {
  28. $dirfile_items[$key]['status'] = -1;
  29. $dirfile_items[$key]['current'] = 'nodir';
  30. }
  31. } else {
  32. $dirfile_items[$key]['status'] = 1;
  33. $dirfile_items[$key]['current'] = '+r+w';
  34. }
  35. } else {
  36. if(file_exists(ROOT_PATH.$item_path)) {
  37. if(is_writable(ROOT_PATH.$item_path)) {
  38. $dirfile_items[$key]['status'] = 1;
  39. $dirfile_items[$key]['current'] = '+r+w';
  40. } else {
  41. $dirfile_items[$key]['status'] = 0;
  42. $dirfile_items[$key]['current'] = '+r';
  43. }
  44. } else {
  45. if ($fp = @fopen(ROOT_PATH.$item_path,'wb+')){
  46. $dirfile_items[$key]['status'] = 1;
  47. $dirfile_items[$key]['current'] = '+r+w';
  48. @fclose($fp);
  49. @unlink(ROOT_PATH.$item_path);
  50. }else {
  51. $dirfile_items[$key]['status'] = -1;
  52. $dirfile_items[$key]['current'] = 'nofile';
  53. }
  54. }
  55. }
  56. }
  57. }
  58. /**
  59. * dir is writeable
  60. * @return number
  61. */
  62. function dir_writeable($dir) {
  63. $writeable = 0;
  64. if(!is_dir($dir)) {
  65. @mkdir($dir, 0755);
  66. }else {
  67. @chmod($dir,0755);
  68. }
  69. if(is_dir($dir)) {
  70. if($fp = @fopen("$dir/test.txt", 'w')) {
  71. @fclose($fp);
  72. @unlink("$dir/test.txt");
  73. $writeable = 1;
  74. } else {
  75. $writeable = 0;
  76. }
  77. }
  78. return $writeable;
  79. }
  80. /**
  81. * function is exist
  82. */
  83. function function_check(&$func_items) {
  84. $func = array();
  85. foreach($func_items as $key => $item) {
  86. $func_items[$key]['status'] = function_exists($item['name']) ? 1 : 0;
  87. }
  88. }
  89. function show_msg($msg){
  90. global $html_title,$html_header,$html_footer;
  91. include 'step_msg.php';
  92. exit();
  93. }
  94. /**
  95. * database check
  96. */
  97. function check_db($db_host, $db_user, $db_pwd, $db_name, $db_prefix, $db_port) {
  98. if(!function_exists('mysql_connect')) {
  99. show_msg('undefined function : mysql_connect()');
  100. }
  101. if(!@mysql_connect($db_host.":".$db_port, $db_user, $db_pwd)) {
  102. show_msg('database connect failed');
  103. } else {
  104. if($query = mysql_query("SHOW TABLES FROM $db_name")) {
  105. while($row = mysql_fetch_row($query)) {
  106. if(preg_match("/^$db_prefix/", $row[0])) {
  107. return false;
  108. }
  109. }
  110. }
  111. }
  112. return true;
  113. }
  114. //make rand
  115. function random($length, $numeric = 0) {
  116. $seed = base_convert(md5(print_r($_SERVER, 1).microtime()), 16, $numeric ? 10 : 35);
  117. $seed = $numeric ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));
  118. $hash = '';
  119. $max = strlen($seed) - 1;
  120. for($i = 0; $i < $length; $i++) {
  121. $hash .= $seed[mt_rand(0, $max)];
  122. }
  123. return $hash;
  124. }
  125. /**
  126. * drop table
  127. */
  128. function droptable($table_name){
  129. return "DROP TABLE IF EXISTS `". $table_name ."`;";
  130. }
  131. //database class
  132. class db {
  133. var $querynum = 0;
  134. var $link;
  135. var $histories;
  136. var $time;
  137. var $tablepre;
  138. function connect($dbhost, $dbuser, $dbpw, $dbname = '', $dbcharset, $pconnect = 0, $tablepre='', $time = 0) {
  139. $this->time = $time;
  140. $this->tablepre = $tablepre;
  141. if($pconnect) {
  142. if(!$this->link = mysql_pconnect($dbhost, $dbuser, $dbpw)) {
  143. $this->halt('Can not connect to MySQL server');
  144. }
  145. } else {
  146. if(!$this->link = mysql_connect($dbhost, $dbuser, $dbpw, 1)) {
  147. $this->halt('Can not connect to MySQL server');
  148. }
  149. }
  150. if($this->version() > '5.0') {
  151. if($dbcharset) {
  152. mysql_query("SET character_set_connection=".$dbcharset.", character_set_results=".$dbcharset.", character_set_client=binary", $this->link);
  153. }
  154. if($this->version() > '5.0.1') {
  155. mysql_query("SET sql_mode=''", $this->link);
  156. }
  157. }
  158. if($dbname) {
  159. mysql_select_db($dbname, $this->link);
  160. }
  161. }
  162. function fetch_array($query, $result_type = MYSQL_ASSOC) {
  163. return mysql_fetch_array($query, $result_type);
  164. }
  165. function result_first($sql, &$data) {
  166. $query = $this->query($sql);
  167. $data = $this->result($query, 0);
  168. }
  169. function fetch_first($sql, &$arr) {
  170. $query = $this->query($sql);
  171. $arr = $this->fetch_array($query);
  172. }
  173. function fetch_all($sql, &$arr) {
  174. $query = $this->query($sql);
  175. while($data = $this->fetch_array($query)) {
  176. $arr[] = $data;
  177. }
  178. }
  179. function query($sql, $type = '', $cachetime = FALSE) {
  180. $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query';
  181. if(!($query = $func($sql, $this->link)) && $type != 'SILENT') {
  182. $this->halt('MySQL Query Error', $sql);
  183. }
  184. $this->querynum++;
  185. $this->histories[] = $sql;
  186. return $query;
  187. }
  188. function affected_rows() {
  189. return mysql_affected_rows($this->link);
  190. }
  191. function error() {
  192. return (($this->link) ? mysql_error($this->link) : mysql_error());
  193. }
  194. function errno() {
  195. return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
  196. }
  197. function result($query, $row) {
  198. $query = @mysql_result($query, $row);
  199. return $query;
  200. }
  201. function num_rows($query) {
  202. $query = mysql_num_rows($query);
  203. return $query;
  204. }
  205. function num_fields($query) {
  206. return mysql_num_fields($query);
  207. }
  208. function free_result($query) {
  209. return mysql_free_result($query);
  210. }
  211. function insert_id() {
  212. return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
  213. }
  214. function fetch_row($query) {
  215. $query = mysql_fetch_row($query);
  216. return $query;
  217. }
  218. function fetch_fields($query) {
  219. return mysql_fetch_field($query);
  220. }
  221. function version() {
  222. return mysql_get_server_info($this->link);
  223. }
  224. function close() {
  225. return mysql_close($this->link);
  226. }
  227. function halt($message = '', $sql = '') {
  228. echo mysql_error();echo "<br />";
  229. }
  230. }