Example #1
0
 /**
  * @covers mychaelstyle\storage\File::eof
  */
 public function testEof()
 {
     // opened and final line
     $this->object->open('r');
     while (!$this->object->eof()) {
         $result = $this->object->gets();
     }
     $result = $this->object->eof();
     $this->assertEquals(true, $result);
     $this->object->close();
 }
Example #2
0
 /**
  * Downloads a package archive from an http URL.
  * 
  * @param	string		$httpUrl
  * @param	string		$prefix
  * @return	string		path to the dowloaded file
  */
 public static function downloadFileFromHttp($httpUrl, $prefix = 'package')
 {
     $extension = strrchr($httpUrl, '.');
     //$newFileName = self::getTemporaryFilename($prefix.'_', $extension);
     $newFileName = self::getTemporaryFilename($prefix . '_');
     $localFile = new File($newFileName);
     // the file to write.
     // get proxy
     $options = array();
     if (PROXY_SERVER_HTTP) {
         $options['http']['proxy'] = PROXY_SERVER_HTTP;
     }
     // first look if php's built-in fopen() is available, and if so, use it.
     if (function_exists('fopen') && ini_get('allow_url_fopen')) {
         $remoteFile = new File($httpUrl, 'rb', $options);
         // the file to read.
         // get the content of the remote file and write it to a local file.
         while (!$remoteFile->eof()) {
             $buffer = $remoteFile->gets(4096);
             $localFile->write($buffer);
         }
     } else {
         $port = 80;
         $parsedUrl = parse_url($httpUrl);
         $host = $parsedUrl['host'];
         $path = $parsedUrl['path'];
         require_once WCF_DIR . 'lib/system/io/RemoteFile.class.php';
         $remoteFile = new RemoteFile($host, $port, 30, $options);
         // the file to read.
         if (!isset($remoteFile)) {
             $localFile->close();
             unlink($newFileName);
             throw new SystemException("cannot connect to http host '" . $host . "'", 14000);
         }
         // build and send the http request.
         $request = "GET " . $path . (!empty($parsedUrl['query']) ? '?' . $parsedUrl['query'] : '') . " HTTP/1.0\r\n";
         $request .= "User-Agent: HTTP.PHP (FileUtil.class.php; WoltLab Community Framework/" . WCF_VERSION . "; " . WCF::getLanguage()->getLanguageCode() . ")\r\n";
         $request .= "Accept: */*\r\n";
         $request .= "Accept-Language: " . WCF::getLanguage()->getLanguageCode() . "\r\n";
         $request .= "Host: " . $host . "\r\n";
         $request .= "Connection: Close\r\n\r\n";
         $remoteFile->puts($request);
         $waiting = true;
         $readResponse = array();
         // read http response.
         while (!$remoteFile->eof()) {
             $readResponse[] = $remoteFile->gets();
             // look if we are done with transferring the requested file.
             if ($waiting) {
                 if (rtrim($readResponse[count($readResponse) - 1]) == '') {
                     $waiting = false;
                 }
             } else {
                 // look if the webserver sent an error http statuscode
                 // This has still to be checked if really sufficient!
                 $arrayHeader = array('201', '301', '302', '303', '307', '404');
                 foreach ($arrayHeader as $code) {
                     $error = strpos($readResponse[0], $code);
                 }
                 if ($error !== false) {
                     $localFile->close();
                     unlink($newFileName);
                     throw new SystemException("file " . $path . " not found at host '" . $host . "'", 14001);
                 }
                 // write to the target system.
                 $localFile->write($readResponse[count($readResponse) - 1]);
             }
         }
     }
     $remoteFile->close();
     $localFile->close();
     return $newFileName;
 }
Example #3
0
 /**
  * 处理错误机制
  * 
  * @access public
  * @param mixed $code 错误代码 
  * @param mixed $message 错误信息
  * @param mixed $file 错误文件
  * @param mixed $line 行号
  * @param mixed $errContext 错误内容
  * @return void
  */
 public static function handleError($code, $message, $file, $line, $errContext)
 {
     //页面中正确运行的部分
     if (ob_get_length() > 0) {
         ob_end_clean();
     }
     $errorStack = null;
     if ($code & error_reporting()) {
         restore_error_handler();
         restore_exception_handler();
         $log = "{$message} (" . str_replace(TINY_ROOT, "", $file) . ":{$line})\r\nStack trace:\r\n";
         $trace = debug_backtrace();
         self::paseErrorTrace($trace, $log);
         $errorStack = $trace;
     }
     try {
         $errorType;
         switch ($code) {
             case E_ERROR:
                 $errorType = 'ERROR';
                 break;
             case E_WARNING:
                 $errorType = 'WARNING';
                 break;
             case E_NOTICE:
                 $errorType = 'NOTICE';
                 break;
             case E_USER_ERROR:
                 $errorType = 'USER_ERROR';
                 break;
             case E_USER_WARNING:
                 $errorType = 'USER_WARNING';
                 break;
             case E_USER_NOTICE:
                 $errorType = 'USER_NOTICE';
                 break;
             case E_PARSE:
                 $errorType = 'PARSE_ERROR';
                 break;
             default:
                 $errorType = 'UNKNOWN';
                 break;
         }
         if (isset($log)) {
             Tiny::log($log, $errorType);
         }
         $error_file = new File($file);
         $codes = $error_file->gets($line);
         if (defined("APP_ROOT")) {
             $file = str_replace(APP_ROOT, "", $file);
         }
         $file = str_replace(TINY_ROOT, "", $file);
         $datas = array('errorType' => $errorType, 'file' => $file, 'line' => $line, 'codes' => htmlspecialchars($codes), 'errorStack' => $errorStack, 'errorContent' => $errContext);
         $error = new Error(Tiny::app(), $message, null, $datas);
         if ($error) {
             $error->handle();
             exit;
         } else {
             self::displayError($code, $message, $file, $line);
         }
     } catch (Exception $e) {
         self::displayException($e);
     }
     exit;
 }