1<?php 2 3/* 4 [UCenter] (C)2001-2099 Comsenz Inc. 5 This is NOT a freeware, use is subject to license terms 6 7 $Id: misc.php 1182 2014-11-17 08:57:52Z andyzheng $ 8*/ 9 10!defined('IN_UC') && exit('Access Denied'); 11 12define('UC_ARRAY_SEP_1', 'UC_ARRAY_SEP_1'); 13define('UC_ARRAY_SEP_2', 'UC_ARRAY_SEP_2'); 14 15class miscmodel { 16 17 var $db; 18 var $base; 19 20 function __construct(&$base) { 21 $this->miscmodel($base); 22 } 23 24 function miscmodel(&$base) { 25 $this->base = $base; 26 $this->db = $base->db; 27 } 28 29 function get_apps($col = '*', $where = '') { 30 $arr = $this->db->fetch_all("SELECT $col FROM ".UC_DBTABLEPRE."applications".($where ? ' WHERE '.$where : '')); 31 return $arr; 32 } 33 34 function delete_apps($appids) { 35 } 36 37 function update_app($appid, $name, $url, $authkey, $charset, $dbcharset) { 38 } 39 40 function alter_app_table($appid, $operation = 'ADD') { 41 } 42 43 function get_host_by_url($url) { 44 } 45 46 function check_url($url) { 47 } 48 49 function check_ip($url) { 50 } 51 52 function test_api($url, $ip = '') { 53 } 54 55 function dfopen2($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE, $encodetype = 'URLENCODE') { 56 $__times__ = isset($_GET['__times__']) ? intval($_GET['__times__']) + 1 : 1; 57 if($__times__ > 2) { 58 return ''; 59 } 60 $url .= (strpos($url, '?') === FALSE ? '?' : '&')."__times__=$__times__"; 61 return $this->dfopen($url, $limit, $post, $cookie, $bysocket, $ip, $timeout, $block, $encodetype); 62 } 63 64 function dfopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE , $ip = '', $timeout = 15, $block = TRUE, $encodetype = 'URLENCODE') { 65 $return = ''; 66 $matches = parse_url($url); 67 $scheme = $matches['scheme']; 68 $host = $matches['host']; 69 $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/'; 70 $port = !empty($matches['port']) ? $matches['port'] : 80; 71 72 if($post) { 73 $out = "POST $path HTTP/1.0\r\n"; 74 $header = "Accept: */*\r\n"; 75 $header .= "Accept-Language: zh-cn\r\n"; 76 $boundary = $encodetype == 'URLENCODE' ? '' : ';'.substr($post, 0, trim(strpos($post, "\n"))); 77 $header .= $encodetype == 'URLENCODE' ? "Content-Type: application/x-www-form-urlencoded\r\n" : "Content-Type: multipart/form-data$boundary\r\n"; 78 $header .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n"; 79 $header .= "Host: $host:$port\r\n"; 80 $header .= 'Content-Length: '.strlen($post)."\r\n"; 81 $header .= "Connection: Close\r\n"; 82 $header .= "Cache-Control: no-cache\r\n"; 83 $header .= "Cookie: $cookie\r\n\r\n"; 84 $out .= $header.$post; 85 } else { 86 $out = "GET $path HTTP/1.0\r\n"; 87 $header = "Accept: */*\r\n"; 88 $header .= "Accept-Language: zh-cn\r\n"; 89 $header .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n"; 90 $header .= "Host: $host:$port\r\n"; 91 $header .= "Connection: Close\r\n"; 92 $header .= "Cookie: $cookie\r\n\r\n"; 93 $out .= $header; 94 } 95 96 $fpflag = 0; 97 if(!$fp = @fsocketopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout)) { 98 $context = array( 99 'http' => array( 100 'method' => $post ? 'POST' : 'GET', 101 'header' => $header, 102 'content' => $post, 103 'timeout' => $timeout, 104 ), 105 ); 106 $context = stream_context_create($context); 107 $fp = @fopen($scheme.'://'.($ip ? $ip : $host).':'.$port.$path, 'b', false, $context); 108 $fpflag = 1; 109 } 110 111 if(!$fp) { 112 return ''; 113 } else { 114 stream_set_blocking($fp, $block); 115 stream_set_timeout($fp, $timeout); 116 @fwrite($fp, $out); 117 $status = stream_get_meta_data($fp); 118 if(!$status['timed_out']) { 119 while (!feof($fp) && !$fpflag) { 120 if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) { 121 break; 122 } 123 } 124 125 $stop = false; 126 while(!feof($fp) && !$stop) { 127 $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit)); 128 $return .= $data; 129 if($limit) { 130 $limit -= strlen($data); 131 $stop = $limit <= 0; 132 } 133 } 134 } 135 @fclose($fp); 136 return $return; 137 } 138 } 139 140 function array2string($arr) { 141 $s = $sep = ''; 142 if($arr && is_array($arr)) { 143 foreach($arr as $k => $v) { 144 $s .= $sep.addslashes($k).UC_ARRAY_SEP_1.$v; 145 $sep = UC_ARRAY_SEP_2; 146 } 147 } 148 return $s; 149 } 150 151 function string2array($s) { 152 $arr = explode(UC_ARRAY_SEP_2, $s); 153 $arr2 = array(); 154 foreach($arr as $k => $v) { 155 list($key, $val) = explode(UC_ARRAY_SEP_1, $v); 156 $arr2[$key] = $val; 157 } 158 return $arr2; 159 } 160} 161 162?>