예제 #1
0
function install_dfopen($url, $limit = 10485760, $post = '', $cookie = '', $bysocket = false, $timeout = 2, $agent = "")
{
    if (ini_get('allow_url_fopen') && !$bysocket && !$post) {
        $fp = @fopen($url, 'r');
        $s = $t = '';
        if ($fp) {
            while ($t = fread($fp, 2048)) {
                $s .= $t;
            }
            fclose($fp);
        }
        if ($s) {
            return $s;
        }
    }
    $return = '';
    $agent = $agent ? $agent : "Mozilla/5.0 (compatible; Googlebot/2.1; +http:/" . "/www.google.com/bot.html)";
    $matches = parse_url($url);
    $host = $matches['host'];
    $script = $matches['path'] . ($matches['query'] ? '?' . $matches['query'] : '') . ($matches['fragment'] ? '#' . $matches['fragment'] : '');
    $script = $script ? $script : '/';
    $port = !empty($matches['port']) ? $matches['port'] : 80;
    if ($post) {
        $out = "POST {$script} HTTP/1.1\r\n";
        $out .= "Accept: */" . "*\r\n";
        $out .= "Referer: {$url}\r\n";
        $out .= "Accept-Language: zh-cn\r\n";
        $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
        $out .= "Accept-Encoding: none\r\n";
        $out .= "User-Agent: {$agent}\r\n";
        $out .= "Host: {$host}\r\n";
        $out .= 'Content-Length: ' . strlen($post) . "\r\n";
        $out .= "Connection: Close\r\n";
        $out .= "Cache-Control: no-cache\r\n";
        $out .= "Cookie: {$cookie}\r\n\r\n";
        $out .= $post;
    } else {
        $out = "GET {$script} HTTP/1.1\r\n";
        $out .= "Accept: */" . "*\r\n";
        $out .= "Referer: {$url}\r\n";
        $out .= "Accept-Language: zh-cn\r\n";
        $out .= "Accept-Encoding:\r\n";
        $out .= "User-Agent: {$agent}\r\n";
        $out .= "Host: {$host}\r\n";
        $out .= "Connection: Close\r\n";
        $out .= "Cookie: {$cookie}\r\n\r\n";
    }
    $fp = jfsockopen($host, $port, $errno, $errstr, $timeout);
    if (!$fp) {
        return false;
    } else {
        fwrite($fp, $out);
        $return = '';
        while (!feof($fp) && $limit > -1) {
            $limit -= 8192;
            $return .= @fread($fp, 8192);
            if (!isset($status)) {
                preg_match("|^HTTP/[^\\s]*\\s(.*?)\\s|", $return, $status);
                $status = $status[1];
                if ($status != 200) {
                    return false;
                }
            }
        }
        fclose($fp);
        preg_match("/^Location: ([^\r\n]+)/m", $return, $match);
        if (!empty($match[1]) && ($location = $match[1])) {
            if (strpos($location, ":/" . "/") === false) {
                $location = dirname($url) . '/' . $location;
            }
            $args = func_get_args();
            $args[0] = $location;
            return call_user_func_array("dfopen", $args);
        }
        if (false !== ($strpos = strpos($return, "\r\n\r\n"))) {
            $return = substr($return, $strpos);
            $return = preg_replace("~^\r\n\r\n(?:[\\w\\d]{1,8}\r\n)?~", "", $return);
            if ("\r\n\r\n" == substr($return, -4)) {
                $return = preg_replace("~(?:\r\n[\\w\\d]{1,8})?\r\n\r\n\$~", "", $return);
            }
        }
        return $return;
    }
}
예제 #2
0
 function download_range($url, $offset = 0, $length = 1024, $all = 0)
 {
     $matches = parse_url($url);
     $host = $matches['host'];
     $script = $matches['path'] . (isset($matches['query']) ? '?' . $matches['query'] : '');
     $script = $script ? $script : '/';
     $port = !empty($matches['port']) ? $matches['port'] : 80;
     $offset_end = $offset + $length;
     if ($offset_end >= $all && $all > 0) {
         $offset_end = $all - 1;
     }
     $out = "GET {$script} HTTP/1.1\r\n";
     $out .= "Accept: */" . "*\r\n";
     $out .= "Referer: {$this->Config['site_url']}/~" . SYS_VERSION . "~" . SYS_BUILD . "\r\n";
     $out .= "Accept-Encoding: none\r\n";
     $out .= "Range: bytes={$offset}-{$offset_end}\r\n";
     $out .= "User-Agent: Cenwor.Downloader.Agent(PHP)[MOYO].2012.08.16\r\n";
     $out .= "Host: {$host}\r\n";
     $out .= "Connection: Close\r\n\r\n";
     $errno = 0;
     $errstr = '';
     $timeout = 180;
     $fp = jfsockopen($host, $port, $errno, $errstr, $timeout);
     if (!$fp) {
         return false;
     } else {
         fwrite($fp, $out);
         $header = '';
         while ($str = trim(fgets($fp, 4096))) {
             $header .= $str . "\n";
         }
         $body = '';
         while (!feof($fp)) {
             $body .= fgets($fp, 4096);
         }
         fclose($fp);
         $http = array('header' => $header, 'data' => $body);
     }
     preg_match('/HTTP\\/1.1 (\\d+)/i', $http['header'], $mchs);
     $staCode = $mchs[1];
     if ($staCode == 206) {
         preg_match('/bytes\\s+(\\d+)-(\\d+)\\/(\\d+)/i', $http['header'], $mchs);
         $return = array('bytes_start' => $mchs[1], 'bytes_finish' => $mchs[2], 'bytes_all' => $mchs[3]);
         if ($return['bytes_finish'] + 1 >= $return['bytes_all']) {
             return array('next' => false, 'bin' => $http['data']);
         } else {
             $return['next'] = array('url' => $url, 'offset' => $return['bytes_finish'] + 1, 'length' => $length, 'all' => $return['bytes_all']);
             $return['bin'] = $http['data'];
             return $return;
         }
     } elseif ($staCode == 200) {
         return array('next' => false, 'bin' => $http['data']);
     } else {
         return array('next' => false, 'bin' => false);
     }
 }
예제 #3
0
function _send_mail_by_smtp($email_to, $email_subject, $email_message, $smtp_config = array(), $html = true)
{
    $sys_config = jconf::get();
    if (empty($smtp_config)) {
        $smtp_conf = jconf::get('smtp');
        $k = array_rand($smtp_conf['smtp']);
        $smtp_config = $smtp_conf['smtp'][$k];
    }
    if (empty($smtp_config)) {
        jlog('SMTP', '$smtp_config is empty', 0);
    }
    $mail['from'] = $smtp_config['mail'];
    $mail['server'] = ($smtp_config['ssl'] ? 'ssl:/' . '/' : '') . $smtp_config['host'];
    $mail['port'] = $smtp_config['port'];
    $mail['auth'] = (bool) ($smtp_config['username'] && $smtp_config['password']);
    $mail['auth_username'] = $smtp_config['username'];
    $mail['auth_password'] = $smtp_config['password'];
    $log = 'jlog';
    $charset = $sys_config['charset'];
    $bbname = $sys_config['site_name'];
    $adminemail = $sys_config['site_admin_email'];
    $maildelimiter = NEW_LINE;
    $mailusername = 1;
    $email_subject = '=?' . $charset . '?B?' . base64_encode(str_replace("\r", '', str_replace("\n", '', $email_subject))) . '?=';
    $email_message = chunk_split(base64_encode(str_replace("\r\n.", " \r\n..", str_replace("\n", "\r\n", str_replace("\r", "\n", str_replace("\r\n", "\n", str_replace("\n\r", "\r", $email_message)))))));
    $email_from = $smtp_config['email_from'] ? $smtp_config['email_from'] : $smtp_config['mail'];
    $email_from = $email_from == '' ? '=?' . $charset . '?B?' . base64_encode($bbname) . "?= <{$adminemail}>" : (preg_match('/^(.+?) \\<(.+?)\\>$/', $email_from, $from) ? '=?' . $charset . '?B?' . base64_encode($from[1]) . "?= <{$from['2']}>" : $email_from);
    foreach (explode(',', $email_to) as $touser) {
        $tousers[] = preg_match('/^(.+?) \\<(.+?)\\>$/', $touser, $to) ? $mailusername ? '=?' . $charset . '?B?' . base64_encode($to[1]) . "?= <{$to['2']}>" : $to[2] : $touser;
    }
    $email_to = implode(',', $tousers);
    $headers = "From: {$email_from}{$maildelimiter}X-Priority: 3{$maildelimiter}X-Mailer: JishiGou " . SYS_VERSION . "{$maildelimiter}MIME-Version: 1.0{$maildelimiter}Content-type: text/" . ($html ? 'html' : 'plain') . "; charset={$charset}{$maildelimiter}Content-Transfer-Encoding: base64{$maildelimiter}";
    $mail['port'] = $mail['port'] ? $mail['port'] : 25;
    if (!($fp = jfsockopen($mail['server'], $mail['port'], $errno, $errstr, 3))) {
        $log('SMTP', "({$mail['server']}:{$mail['port']}) CONNECT - Unable to connect to the SMTP server", 0);
        return false;
    }
    stream_set_blocking($fp, true);
    $lastmessage = fgets($fp, 512);
    if (substr($lastmessage, 0, 3) != '220') {
        $log('SMTP', "{$mail['server']}:{$mail['port']} CONNECT - {$lastmessage}", 0);
        return false;
    }
    fputs($fp, ($mail['auth'] ? 'EHLO' : 'HELO') . " JishiGou\r\n");
    $lastmessage = fgets($fp, 512);
    if (substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250) {
        $log('SMTP', "({$mail['server']}:{$mail['port']}) HELO/EHLO - {$lastmessage}", 0);
        return false;
    }
    while (1) {
        if (substr($lastmessage, 3, 1) != '-' || empty($lastmessage)) {
            break;
        }
        $lastmessage = fgets($fp, 512);
    }
    if ($mail['auth']) {
        fputs($fp, "AUTH LOGIN\r\n");
        $lastmessage = fgets($fp, 512);
        if (substr($lastmessage, 0, 3) != 334) {
            $log('SMTP', "({$mail['server']}:{$mail['port']}) AUTH LOGIN - {$lastmessage}", 0);
            return false;
        }
        fputs($fp, base64_encode($mail['auth_username']) . "\r\n");
        $lastmessage = fgets($fp, 512);
        if (substr($lastmessage, 0, 3) != 334) {
            $log('SMTP', "({$mail['server']}:{$mail['port']}) USERNAME - {$lastmessage}", 0);
            return false;
        }
        fputs($fp, base64_encode($mail['auth_password']) . "\r\n");
        $lastmessage = fgets($fp, 512);
        if (substr($lastmessage, 0, 3) != 235) {
            $log('SMTP', "({$mail['server']}:{$mail['port']}) PASSWORD - {$lastmessage}", 0);
            return false;
        }
        $email_from = $mail['from'];
    }
    fputs($fp, "MAIL FROM: <" . preg_replace("/.*\\<(.+?)\\>.*/", "\\1", $email_from) . ">\r\n");
    $lastmessage = fgets($fp, 512);
    if (substr($lastmessage, 0, 3) != 250) {
        fputs($fp, "MAIL FROM: <" . preg_replace("/.*\\<(.+?)\\>.*/", "\\1", $email_from) . ">\r\n");
        $lastmessage = fgets($fp, 512);
        if (substr($lastmessage, 0, 3) != 250) {
            $log('SMTP', "({$mail['server']}:{$mail['port']}) MAIL FROM - {$lastmessage}", 0);
            return false;
        }
    }
    $email_tos = array();
    foreach (explode(',', $email_to) as $touser) {
        $touser = trim($touser);
        if ($touser) {
            fputs($fp, "RCPT TO: <" . preg_replace("/.*\\<(.+?)\\>.*/", "\\1", $touser) . ">\r\n");
            $lastmessage = fgets($fp, 512);
            if (substr($lastmessage, 0, 3) != 250) {
                fputs($fp, "RCPT TO: <" . preg_replace("/.*\\<(.+?)\\>.*/", "\\1", $touser) . ">\r\n");
                $lastmessage = fgets($fp, 512);
                $log('SMTP', "({$mail['server']}:{$mail['port']}) RCPT TO - {$lastmessage}", 0);
                return false;
            }
        }
    }
    fputs($fp, "DATA\r\n");
    $lastmessage = fgets($fp, 512);
    if (substr($lastmessage, 0, 3) != 354) {
        $log('SMTP', "({$mail['server']}:{$mail['port']}) DATA - {$lastmessage}", 0);
        return false;
    }
    $headers .= 'Message-ID: <' . gmdate('YmdHs') . '.' . substr(md5($email_message . microtime()), 0, 6) . rand(100000, 999999) . '@' . $_SERVER['HTTP_HOST'] . ">{$maildelimiter}";
    fputs($fp, "Date: " . date('r') . "\r\n");
    fputs($fp, "To: " . $email_to . "\r\n");
    fputs($fp, "Subject: " . $email_subject . "\r\n");
    fputs($fp, $headers . "\r\n");
    fputs($fp, "\r\n\r\n");
    fputs($fp, "{$email_message}\r\n.\r\n");
    $lastmessage = fgets($fp, 512);
    if (substr($lastmessage, 0, 3) != 250) {
        $log('SMTP', "({$mail['server']}:{$mail['port']}) END - {$lastmessage}", 0);
        return false;
    }
    fputs($fp, "QUIT\r\n");
    return true;
}
예제 #4
0
 /**
  * Http请求接口
  *
  * @param string $url
  * @param array $params
  * @param string $method 支持 GET / POST / DELETE
  * @param false|array $multi false:普通post array: array ( 'fieldname'=>array('type'=>'mine','name'=>'filename','data'=>'filedata') ) 文件上传
  * @return string
  */
 function http_socket($url, $params, $method = 'GET', $multi = false)
 {
     $method = strtoupper($method);
     $postdata = '';
     $urls = @parse_url($url);
     $httpurl = $urlpath = $urls['path'] . ($urls['query'] ? '?' . $urls['query'] : '');
     if (!$multi) {
         if (is_array($params)) {
             $parts = array();
             foreach ($params as $key => $val) {
                 $parts[] = urlencode($key) . '=' . urlencode($val);
             }
             $postdata = implode('&', $parts);
         } else {
             $postdata = $params;
         }
         if ($postdata) {
             $httpurl = $httpurl . (strpos($httpurl, '?') ? '&' : '?') . $postdata;
         }
     }
     $host = $urls['host'];
     $port = $urls['port'] ? $urls['port'] : 80;
     $version = '1.1';
     if ($urls['scheme'] === 'https') {
         $port = 443;
     }
     $headers = array();
     if ($method == 'GET') {
         $headers[] = "GET {$httpurl} HTTP/{$version}";
     } else {
         if ($method == 'DELETE') {
             $headers[] = "DELETE {$httpurl} HTTP/{$version}";
         } else {
             $headers[] = "POST {$urlpath} HTTP/{$version}";
         }
     }
     $headers[] = 'Host: ' . $host;
     $headers[] = 'User-Agent: ' . $this->useragent;
     $headers[] = 'Connection: Close';
     if ($method == 'POST') {
         if ($multi) {
             $boundary = uniqid('------------------');
             $MPboundary = '--' . $boundary;
             $endMPboundary = $MPboundary . '--';
             $multipartbody = '';
             $headers[] = 'Content-Type: multipart/form-data; boundary=' . $boundary;
             foreach ($params as $key => $val) {
                 $multipartbody .= $MPboundary . "\r\n";
                 $multipartbody .= 'Content-Disposition: form-data; name="' . $key . "\"\r\n\r\n";
                 $multipartbody .= $val . "\r\n";
             }
             foreach ($multi as $key => $data) {
                 $multipartbody .= $MPboundary . "\r\n";
                 $multipartbody .= 'Content-Disposition: form-data; name="' . $key . '"; filename="' . $data['name'] . '"' . "\r\n";
                 $multipartbody .= 'Content-Type: ' . $data['type'] . "\r\n\r\n";
                 $multipartbody .= $data['data'] . "\r\n";
             }
             $multipartbody .= $endMPboundary . "\r\n";
             $postdata = $multipartbody;
         } else {
             $headers[] = 'Content-Type: application/x-www-form-urlencoded';
         }
     }
     $ret = '';
     $fp = jfsockopen($host, $port, $errno, $errstr, 5);
     if (!$fp) {
         $error = 'Open Socket Error';
         return '';
     } else {
         if ($method != 'GET' && $postdata) {
             $headers[] = 'Content-Length: ' . strlen($postdata);
         }
         fwrite($fp, implode("\r\n", $headers));
         fwrite($fp, "\r\n\r\n");
         if ($method != 'GET' && $postdata) {
             fwrite($fp, $postdata);
         }
         //skip headers
         while (!feof($fp)) {
             $ret .= fgets($fp, 1024);
         }
         if ($this->_debug) {
             echo $ret;
         }
         fclose($fp);
         $pos = strpos($ret, "\r\n\r\n");
         if ($pos) {
             $rt = trim(substr($ret, $pos + 1));
             $responseHead = trim(substr($ret, 0, $pos));
             $responseHeads = explode("\r\n", $responseHead);
             $httpcode = explode(' ', $responseHeads[0]);
             $this->_httpcode = $httpcode[1];
             if (strpos(substr($ret, 0, $pos), 'Transfer-Encoding: chunked')) {
                 $response = explode("\r\n", $rt);
                 $t = array_slice($response, 1, -1);
                 return implode('', $t);
             }
             return $rt;
         }
         return '';
     }
 }