Ejemplo n.º 1
0
 /**
  * コンストラクタ
  *
  * @param string $url
  * @param string $destination
  * @param array $options
  * @param P2HttpCallback $onSuccess
  * @param P2HttpCallback $onFailure
  */
 public function __construct($url, $destination, array $options = null, P2HttpCallback $onSuccess = null, P2HttpCallback $onFailure = null)
 {
     global $_conf;
     if ($options === null) {
         $options = array();
     }
     if (!isset($options['connecttimeout'])) {
         $options['connecttimeout'] = $_conf['fsockopen_time_limit'];
     }
     if (!isset($options['timeout'])) {
         $options['timeout'] = $_conf['fsockopen_time_limit'] * 2;
     }
     if (!isset($options['compress'])) {
         $options['compress'] = true;
     }
     if (!isset($options['useragent'])) {
         $options['useragent'] = P2Util::getP2UA($withMonazilla = true);
     }
     if ($_conf['proxy_use'] && !isset($options['proxyhost']) && !empty($_conf['proxy_host'])) {
         $options['proxyhost'] = $_conf['proxy_host'];
         if (!empty($_conf['proxy_port']) && is_numeric($_conf['proxy_port'])) {
             $options['proxyport'] = (int) $_conf['proxy_port'];
         } elseif (strpos($_conf['proxy_host'], ':') === false) {
             $options['proxyport'] = 80;
         }
         /*
         $options['proxytype'] = HTTP_PROXY_HTTP;
         if (isset($_conf['proxy_type'])) {
             switch ($_conf['proxy_type']) {
             case 'http':   $options['proxytype'] = HTTP_PROXY_HTTP;   break;
             case 'socks4': $options['proxytype'] = HTTP_PROXY_SOCKS4; break;
             case 'socks5': $options['proxytype'] = HTTP_PROXY_SOCKS5; break;
             default:
                 if (is_numeric($options['proxytype'])) {
                     $options['proxytype'] = (int)$_conf['proxy_type'];
                 }
             }
         }
         
         if (!empty($_conf['proxy_auth'])) {
             $options['proxy_auth'] = $_conf['proxy_auth'];
             $options['proxyauthtype'] = HTTP_AUTH_BASIC;
             if (isset($_conf['proxy_auth_type'])) {
                 switch ($_conf['proxy_auth_type']) {
                 case 'basic':  $options['proxyauthtype'] = HTTP_AUTH_BASIC;  break;
                 case 'digest': $options['proxyauthtype'] = HTTP_AUTH_DIGEST; break;
                 case 'ntlm':   $options['proxyauthtype'] = HTTP_AUTH_NTLM;   break;
                 case 'gssneg': $options['proxyauthtype'] = HTTP_AUTH_GSSNEG; break;
                 case 'any':    $options['proxyauthtype'] = HTTP_AUTH_ANY;    break;
                 default:
                     if (is_numeric($options['proxytype'])) {
                         $options['proxyauthtype'] = (int)$_conf['proxy_auth_type'];
                     }
                 }
             }
         }
         */
     }
     if (!isset($options['lastmodified']) && file_exists($destination)) {
         $options['lastmodified'] = filemtime($destination);
     } else {
         FileCtl::mkdir_for($destination);
     }
     $this->_destination = $destination;
     $this->_permission = !empty($_conf['dl_perm']) ? $_conf['dl_perm'] : 0666;
     $this->_errorCode = self::E_NONE;
     $this->_errorInfo = '';
     $this->_onSuccess = $onSuccess;
     $this->_onFailure = $onFailure;
     $this->_next = null;
     parent::__construct($url, HttpRequest::METH_GET, $options);
 }
Ejemplo n.º 2
0
 /**
  * >>1のみをプレビュー表示するためのHTMLを取得する(オンザフライに対応)
  *
  * @access  public
  * @return  string|false
  */
 function previewOne()
 {
     global $_conf, $ptitle_ht;
     if (!($this->host && $this->bbs && $this->key)) {
         return false;
     }
     $first_line = '';
     // ローカルdatから取得
     if (is_readable($this->keydat)) {
         $fd = fopen($this->keydat, "rb");
         $first_line = fgets($fd, 32800);
         fclose($fd);
     }
     if ($first_line) {
         // be.2ch.net ならEUC→SJIS変換
         if (P2Util::isHostBe2chNet($this->host)) {
             $first_line = mb_convert_encoding($first_line, 'SJIS-win', 'eucJP-win');
         }
         $first_datline = rtrim($first_line);
         if (strstr($first_datline, "<>")) {
             $datline_sepa = "<>";
         } else {
             $datline_sepa = ",";
             $this->dat_type = "2ch_old";
         }
         $d = explode($datline_sepa, $first_datline);
         $this->setTtitle($d[4]);
         // 便宜上
         if (!$this->readnum) {
             $this->readnum = 1;
         }
     }
     // ローカルdatなければオンラインから
     if (!$first_line) {
         $url = $this->getDatUrl($this->host, $this->bbs, $this->key);
         $purl = parse_url($url);
         $purl['query'] = isset($purl['query']) ? '?' . $purl['query'] : '';
         // プロキシ
         if ($_conf['proxy_use']) {
             $send_host = $_conf['proxy_host'];
             $send_port = $_conf['proxy_port'];
             $send_path = $url;
         } else {
             $send_host = $purl['host'];
             $send_port = isset($purl['port']) ? $purl['port'] : null;
             $send_path = $purl['path'] . $purl['query'];
         }
         // デフォルトを80
         !$send_port and $send_port = 80;
         $request = 'GET ' . $send_path . " HTTP/1.0\r\n";
         $request .= "Host: " . $purl['host'] . "\r\n";
         $request .= 'User-Agent: ' . P2Util::getP2UA($withMonazilla = true) . "\r\n";
         // $request .= "Range: bytes={$from_bytes}-\r\n";
         // Basic認証用のヘッダ
         if (isset($purl['user']) && isset($purl['pass'])) {
             $request .= "Authorization: Basic " . base64_encode($purl['user'] . ":" . $purl['pass']) . "\r\n";
         }
         $request .= "Connection: Close\r\n";
         $request .= "\r\n";
         // WEBサーバへ接続
         $fp = fsockopen($send_host, $send_port, $errno, $errstr, $_conf['fsockopen_time_limit']);
         if (!$fp) {
             P2Util::pushInfoHtml(sprintf('<p>サーバ接続エラー: %s (%s)<br>p2 info - %s に接続できませんでした。</p>', $errstr, $errno, P2View::tagA(P2Util::throughIme($url), hs($url), array('target' => $_conf['ext_win_target']))));
             $this->diedat = true;
             return false;
         }
         // HTTPリクエスト送信
         fputs($fp, $request);
         // HTTPヘッダレスポンスを取得する
         $h = $this->freadHttpHeader($fp);
         if ($h === false) {
             fclose($fp);
             $this->_pushInfoHtmlFreadHttpHeaderError($url);
             $this->diedat = true;
             return false;
         }
         // {{{ HTTPコードをチェック
         $code = $h['code'];
         // Partial Content
         if ($code == "200") {
             // OK。何もしない
             // 予期しないHTTPコード。なかったと判断する
         } else {
             fclose($fp);
             $this->previewOneNotFound();
             return false;
         }
         // }}}
         if (isset($h['headers']['Content-Length'])) {
             if (preg_match("/^([0-9]+)/", $h['headers']['Content-Length'], $matches)) {
                 $onbytes = $h['headers']['Content-Length'];
             }
         }
         // bodyを一行目だけ読む
         $first_line = fgets($fp, 32800);
         fclose($fp);
         // be.2ch.net ならEUC→SJIS変換
         if (P2Util::isHostBe2chNet($this->host)) {
             $first_line = mb_convert_encoding($first_line, 'SJIS-win', 'eucJP-win');
         }
         $first_datline = rtrim($first_line);
         if (strstr($first_datline, '<>')) {
             $datline_sepa = '<>';
         } else {
             $datline_sepa = ',';
             $this->dat_type = '2ch_old';
         }
         $d = explode($datline_sepa, $first_datline);
         $this->setTtitle($d[4]);
         $this->onthefly = true;
     }
     // 厳密にはオンザフライではないが、個人にとっては(既読記録がされないという意味で)オンザフライ
     if (!$this->isKitoku()) {
         $this->onthefly = true;
     }
     $body = '';
     if (!empty($this->onthefly)) {
         // PC
         if (UA::isPC()) {
             $body .= '<div><span class="onthefly">プレビュー</span></div>';
             // 携帯
         } else {
             $body .= '<div><font size="-1" color="#00aa00">プレビュー</font></div>';
         }
     }
     UA::isPC() and $body .= '<dl>';
     require_once P2_LIB_DIR . '/ShowThread.php';
     // PC
     if (UA::isPC()) {
         require_once P2_LIB_DIR . '/ShowThreadPc.php';
         $aShowThread = new ShowThreadPc($this);
         // 携帯
     } else {
         require_once P2_LIB_DIR . '/ShowThreadK.php';
         $aShowThread = new ShowThreadK($this);
     }
     $body .= $aShowThread->transRes($first_line, 1);
     // 1を表示
     UA::isPC() and $body .= "</dl>\n";
     return $body;
 }