function main() { global $to, $mSubject; $newLine = "\r\n"; $arrayMessage = get_request_headers(); $headerSender = 'MIME-Version: 1.0' . $newLine; $headerSender .= 'Content-type: text/plain; charset=UTF-8' . $newLine; $headerSender .= 'Return-Path: ' . $to . $newLine; $strEmail = trim($arrayMessage['email']); $emailSubject = $mSubject . $arrayMessage['subject']; /* Perform concatenation manually, because order is important */ $strMessage = "First:\t" . $arrayMessage['first'] . $newLine; $strMessage .= "Last:\t" . $arrayMessage['last'] . $newLine; $strMessage .= "Email:\t" . $arrayMessage['email'] . $newLine; $strMessage .= "Message:\t" . $arrayMessage['message']; $strResponseMessage; $objMail = mail($to, $emailSubject, $strMessage, $headerSender); if (!$objMail) { $strResponseMessage = 'Error: email failed'; } else { $strResponseMessage = 'Thank you for your e-mail. We will reply within 24 hours.'; } return $strResponseMessage; }
<?php require_once '../config.php'; // string with url requested by visitor. Usually in the form of: // controller/action/arg1/arg2?param1=value1 // @see public/.htaccess $requested_route = @$_GET['_url']; // clear params used for routing unset($_GET['_url'], $_REQUEST['_url']); $headers = get_request_headers(); // transfer posted json data to global request data arrays if (stripos(@$headers['Content-Type'], 'application/json') !== false) { $data = file_get_contents('php://input'); $json_data = json_decode($data, true); if (is_array($json_data)) { $_REQUEST = array_merge($_REQUEST, $json_data); $_POST = $json_data; } } try { ApplicationController::load($requested_route, $headers, $_REQUEST); } catch (FileNotFoundException $e) { error_log($e->getMessage()); echo '<h1>File Not Found</h1>'; die(htmlentities($e->getMessage())); }
function forward($cb_before = null, $cb_after = null, $url = '') { //生成url if ($url === '') { $url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; } else { if (!preg_match("/https?:/i", $url)) { if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") { $url = "https://" . $_SERVER['HTTP_HOST'] . "/" . ltrim($url, "/"); } else { $url = "http://" . $_SERVER['HTTP_HOST'] . "/" . ltrim($url, "/"); } } } //获取转发需要的头内容 $headers = get_request_headers(); //转发POST内容 $data_to_post = null; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (in_array(get_content_type($headers), array('application/x-www-form-urlencoded', 'multipart/form-data'))) { $data_to_post = $_POST; } else { //就抓出原始的post数据即可 $fp = fopen('php://input', 'r'); $post = stream_get_contents($fp); fclose($fp); $data_to_post = $post; } } if ($cb_before) { call_user_func_array($cb_before, [&$url, &$data_to_post, &$headers]); } //初始化curl选项 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS, 10); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_TIMEOUT, REQUEST_TIMEOUT); //将生成的头,设置在curl中 set_request_headers($ch, $headers); //设置POST数据 if ($data_to_post) { set_post($ch, $data_to_post); } //执行curl请求 //要防止长连接用这种方法 fixme $data = curl_exec($ch); $info = curl_getinfo($ch); curl_close($ch); //获取返回的body内容 $body = $info["size_download"] ? substr($data, $info["header_size"], $info["size_download"]) : ""; $headers_str = substr($data, 0, $info["header_size"]); $headers = get_response_headers($headers_str); if ($cb_after) { $encoding = get_content_encoding($headers); $body_str = $body; if ($encoding === 'deflate') { $body_str = gzinflate($body); } if ($encoding === 'gzip') { $body_str = gzdecode($body); } //调用过滤钩子,检测是否有修改内容 $old_md5 = md5($body_str); call_user_func_array($cb_after, [$info, &$headers, &$body_str]); $new_md5 = md5($body_str); //如果内容有修改,则需要从新打包,和计算内容长度 if ($old_md5 !== $new_md5) { switch ($encoding) { case 'deflate': $body = gzdeflate($body_str); break; case 'gzip': $body = gzencode($body_str); break; default: $body = $body_str; } //修正发出的内容长度 $headers = set_content_length($headers, strlen($body)); } } //转发返回的头内 set_response_headers($headers); //输出html内容到浏览器 echo $body; //函数返回结果给缓存使用 return [$headers, $body]; }