'', 'type' => '', 'etag' => '', 'since' => 0, 'mtime' => 0, 'postmark' => 0, ); $g_mc_handler = false; function log_it($msg) { $date = date('Y-m-d'); @file_put_contents(LOG_PATH."/".$date.".log", $msg."\n", FILE_APPEND); } function get_cgival() { global $g_arg; $g_arg['pid'] = $_GET['pic_id']; $g_arg['type'] = $_GET['type']; $g_arg['postmark'] = $_GET['pm']; if ('theme' == $g_arg['type'] || 'bbspic' == $g_arg['type']) $g_arg['type'] = 'orignal'; if (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER)) { $g_arg['etag'] = trim($_SERVER['HTTP_IF_NONE_MATCH']); } if (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) { $since = strtolower($_SERVER['HTTP_IF_MODIFIED_SINCE']); if ($since) { $arr = explode(';', $since); if (is_array($arr) && $arr[0]) { $g_arg['since'] = strtotime($arr[0]); } } } } function check_cgival() { global $g_arg, $g_errno, $g_all_types; if (strlen($g_arg['pid']) != 21) { $g_errno = E_Invalid_pid; return false; } if (!in_array($g_arg['type'], $g_all_types)) { $g_errno = E_Invalid_type; return false; } return true; } function get_mc_handler($pid) { global $g_mc_host; global $g_mc_handler; if ($g_mc_handler) { return $g_mc_handler; } $hash = hexdec(substr($pid, -2)) % count($g_mc_host); $host = $g_mc_host[$hash]; $g_mc_handler = new Memcache; $g_mc_handler->connect($host['host'], $host['port'], 1); return $g_mc_handler; } function been_cached($pid, $type) { global $g_nocache; global $g_arg, $g_errno, $g_all_types, $mc; if (in_array($type, $g_nocache)) { return false; } $mc = get_mc_handler($pid); if (!$mc) { $g_errno = E_CONNECT_MCD; return false; } $res = $mc->get($pid); return $res; } function get_image_path($pid) { global $g_arg, $g_errno; $uid = hexdec(substr($pid, 0, 8)); $idx = new pic_index($uid); $path = $idx->get($pid); return $path; } function cache_pic($pid, $type, $blob) { global $g_arg, $g_errno, $g_all_types, $mc; global $g_nocache; $img = new Image($blob, 'b'); if ($img->isError()) { $g_errno = E_INIT_IMAGE; unset($img); return false; } //²»cache orignal&middle if (in_array($type, $g_nocache)) { unset($img); return $blob; } $img->resize('bmiddle'); if ($img->isError()) { $g_errno = E_RESIZE_IMAGE; unset($img); return false; } $blob = $img->get_blob(); if ($blob) { $mc = get_mc_handler($pid); if ($mc) { $mc->set($pid, $blob); } } unset($img); return $blob; } function output_header() { global $g_arg, $g_errno; header('Pragma: public'); header('Expires: ' . gmdate('D, d M Y H:i:s', SECS_CLIENT_CACHE + time()) . ' GMT'); header('Cache-Control: max-age=' . SECS_CLIENT_CACHE); if ($g_arg['since'] && $g_arg['mtime'] && $g_arg['since'] >= $g_arg['mtime']) { header("HTTP/1.0 304 Not Modified"); exit(); } } function redirect_to_defpic($type) { global $g_errno; $url = "{$_REQUEST["HTTP_HOST"]}/image/default_{$type}.gif#{$g_errno}"; header("Cache-Control: no-cache, must-revalidate"); header("Location: " . $url); exit; } function redirect_to_homeland($pid, $type) { global $g_errno, $g_arg; global $G_FLAG_2_LOCATION; if ($g_arg['postmark']) { $g_errno = E_REQ_TERMINATE; redirect_to_defpic($type); exit; } if (!array_key_exists($pid[8], $G_FLAG_2_LOCATION)) { $g_errno = E_REQ_TERMINATE; redirect_to_defpic($type); exit; } $lc = $G_FLAG_2_LOCATION[$pid[8]]; $url = "http://{$lc}.static.photo.sina.com.cn/show_mop.php?pic_id={$pid}&type={$type}&pm=1"; header("Cache-Control: no-cache, must-revalidate"); header("Location: " . $url); exit; } function show_pic($blob, $type) { global $g_errno, $g_arg; $img = new Image($blob, 'b'); if ($img->isError()) { $g_errno = E_INIT_BLOB; redirect_to_defpic($type); unset($img); exit; } if ($g_arg['mtime']) { $img->set_lastMod($g_arg['mtime']); } if ('orignal' != $type/* && 'bmiddle' != $type*/) { $img->resize($type); } if ($img->isError()) { log_it("[ZERO] {$g_arg['pid']}"); $g_errno = E_NOT_IMAGE; redirect_to_defpic($g_arg['type']); unset($img); exit; } $img->output(true); unset($img); } function data_array_src($path) { global $G_NFS_VOLUMES; $ret = ereg("^/nfs_pics_filer([0-9]+)/n_pic/(.+)$", $path, $reg); if (!$ret) { return false; } $vol_no = $reg[1]+0; //$vol_no = substr($path, 15, 2) + 0; /* if ($vol_no < DATA_ARRAY_NUM) { header("X-Debug-DA: v-".$vol_no); return sprintf(DATA_SRC_URL_FORMAT, $vol_no, substr($path, 23)); } */ if (in_array($vol_no, $G_NFS_VOLUMES)) { return false; } header("X-Debug-DA: v-".$vol_no); return sprintf(DATA_SRC_URL_FORMAT, $vol_no, $reg[2]); } function read_image_src($path) { global $g_arg, $g_errno; $src = data_array_src($path); if (!$src) { //on filer $st = @stat($path); if (!$st) { return false; } $g_arg['mtime'] = $st['mtime']; } else { $g_arg['mtime'] = DATA_ARRAY_LAST_MODIFY; } $src = $src ? $src : $path; $blob = @file_get_contents($src); return $blob; } /* pid's homeland url if this pic is upload from bj then return false */ function get_homeland_url($pid, $type) { global $g_cgival, $G_FLAG_2_LOCATION; $flag = $pid[8]; if (array_key_exists($flag, $G_FLAG_2_LOCATION)) { $domain = $G_FLAG_2_LOCATION[$flag] . ".static.photo.sina.com.cn"; } else { $domain = false; } $url = $domain ? "http://{$domain}/{$type}/{$pid}&690" : false; return $url; } /* get imgage from homeland */ function proxy_pic($url, &$data) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 3); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_REFERER, "http://blog.sina.com.cn"); curl_setopt($ch, CURLOPT_TIMECONDITION, 1); $data = curl_exec($ch); $isok = curl_getinfo($ch,CURLINFO_HTTP_CODE); curl_close($ch); return $isok; } //////////[MAIN]////////////////////////// get_cgival(); if (!check_cgival()) { redirect_to_defpic($g_arg['type']); exit; } $path = get_image_path($g_arg['pid']); if (!$path) { $g_errno = E_NO_INDEX; redirect_to_defpic($g_arg['type']); exit; } //$blob = been_cached($g_arg['pid'], $g_arg['type']); //header("x-mcd-cached: " . ($blob?1:0)); $blob = read_image_src($path); if (!$blob) { $g_errno = E_NO_FILE; /* shit code by wushen; if referer is 3g.sina.com.cn(wap) then start proxy policy */ $refhost = parse_url($_SERVER['HTTP_REFERER']); if (preg_match("(3g.sina.com.cn)",$refhost['host']) != false && ($homeurl = get_homeland_url($g_arg['pid'], $g_arg['type'])) != false) { $httpcode = proxy_pic($homeurl, $data); if ($data === false) { $httpcode = proxy_pic($url, $data); } if ($data === false) { $httpcode = proxy_pic($url, $data); } if ($data == false) { //redirect_to_defpic($g_arg['type']); header("HTTP/1.0 404 Not Found"); exit; } //header("debugx: " . $homeurl); //header("debugh: " . $httpcode); header("proxy: true"); if ($httpcode == 200) { //split header and body by \r\n\r\n; a[0]:header; a[1]:body $a = split("\r\n\r\n",$data,2); //parse header; b[1] "HTTP\1.1...."; b[>=2] key=>val $b = split("\r\n",$a[0]); //output headers $end = count($b); for ($i = 1; $i <= $end; $i++) { header($b[$i]); } //output boyd echo $a[1]; exit; } } if (array_key_exists($g_arg['pid'][8], $G_FLAG_2_LOCATION)) { redirect_to_homeland($g_arg['pid'], $g_arg['type']); } else { redirect_to_defpic($g_arg['type']); } exit; } if (!$blob) { redirect_to_defpic($g_arg['type']); exit; } output_header(); show_pic($blob, $g_arg['type']); ?>