Пример #1
0
 public function postForFileUpload()
 {
     $numargs = func_num_args();
     if ($numargs == 2) {
         $path = func_get_arg(0);
         $filename = func_get_arg(1);
         $fileContentType = $this->guessContentType($filename);
         if ($fileContentType == null) {
             $fileContentType = self::DEFAULT_CONTENT_TYPE;
         }
         return $this->postForFileUpload($path, $filename, $fileContentType);
     } elseif ($numargs == 3) {
         $path = func_get_arg(0);
         $filename = func_get_arg(1);
         $fileContentType = func_get_arg(2);
         return $this->postForFileUpload($path, $filename, $fileContentType, true);
     } elseif ($numargs == 4) {
         $path = func_get_arg(0);
         $filename = func_get_arg(1);
         $fileContentType = func_get_arg(2);
         $loginIfNecessary = func_get_arg(3);
         $newpath = $this->cocoon . $path;
         // add "cocoon/" to path
         $METHOD = "post(String,File,String,Boolean)";
         $this->logg($METHOD . " 1/2: path = " . $newpath . ", file = " . $filename . ", fileContentType = {$fileContentType}, loginIfNecessary = ");
         $this->logg($loginIfNecessary ? 'true<br/>' : 'false<br/>');
         // Prepare connection:
         $connection = new HttpURLConnection($this->urlPrefixWithoutPort, $this->urlPort, false, "JapsClient/PHP");
         // Connect and add Cookies:
         $connect = $connection->post($newpath, false, $this->addCookies(), "file={$filename}", $fileContentType);
         // Check response code:
         if ($connect["head"]["code"] >= 400) {
             echo "Could not connect";
             return;
             //sonst wird login-Abschnitt ausgefuehrt!!!
         }
         // If necessary, handle cookies:
         $this->handleRecievedCookies($connect);
         // Redirect if necessary:
         if ($connect["head"]["code"] >= 300 && $connect["head"]["code"] < 400) {
             // Follow redirect (by Http Get):
             $location = $connect["head"]["location"]["uri"];
             $connect = $this->get($this->getPath($location), $params = array(), false);
             // Check response code again; fail if not OK:
             if ($connect["head"]["code"] != 200) {
                 echo "Could not connect";
                 return;
                 //sonst wird login-Abschnitt ausgefuehrt!!!
             }
         }
         // Login if necessary:
         if ($loginIfNecessary && $this->checkLoginRequired($connect)) {
             $loginStatus = $this->login();
             switch ($loginStatus) {
                 case self::LOGIN_SUCCESSFUL:
                     $connect = $this->postForFileUpload($path, $filename, $fileContentType, false);
                     if ($this->checkLoginRequired($connect)) {
                         echo "rejected after successful login";
                     }
                     break;
                 case self::LOGIN_FAILED:
                     echo "login failed";
                     break;
                 case self::LOGIN_CANCELED:
                     $connection = "";
                     echo "LOGIN_CANCELED";
                     break;
                 default:
                     echo "Bug: unexpected login status: " . $loginStatus;
             }
         }
         $this->logg($METHOD . " 2/2: path = {$newpath}, connection = ");
         $this->logg($connection);
         return $connect;
     }
 }
Пример #2
0
 private function parseHeaders(HttpURLConnection $conn)
 {
     $this->headers = $conn->getHeaderFields();
     return $this->headers;
 }