예제 #1
0
 /**
  * I've chosen to go with the HTTP PUT method as it is quicker, simpler
  * and more reliable than using the API or POST methods.
  * 
  * @access	public
  * @param	integer		$AlbumID The AlbumID the image is to be uploaded to
  * @param	string		$File The path to the local file that is being uploaded
  * @param	string		$FileName (Optional) The filename to give the file
  *						on upload
  * @param	mixed		$arguments (Optional) Additional arguments. See
  *						SmugMug API documentation.
  * @uses	request
  * @link	http://wiki.smugmug.net/display/API/Uploading
  * @return	array|false
  * @todo Add support for multiple asynchronous uploads
  **/
 public function images_upload()
 {
     $args = phpSmug::processArgs(func_get_args());
     if (!array_key_exists('File', $args)) {
         throw new PhpSmugException('No upload file specified.');
     }
     // Set FileName, if one isn't provided in the method call
     if (!array_key_exists('FileName', $args)) {
         $args['FileName'] = basename($args['File']);
     }
     // Ensure the FileName is phpSmug::urlencodeRFC3986 encoded - caters for stange chars and spaces
     $args['FileName'] = phpSmug::urlencodeRFC3986($args['FileName']);
     // OAuth Stuff
     if ($this->OAuthSecret) {
         $sig = $this->generate_signature('Upload', array('FileName' => $args['FileName']));
     }
     if (is_file($args['File'])) {
         $fp = fopen($args['File'], 'r');
         $data = fread($fp, filesize($args['File']));
         fclose($fp);
     } else {
         throw new PhpSmugException("File doesn't exist: {$args['File']}");
     }
     // Create a new object as we still need the other request object
     $upload_req = new httpRequest();
     $upload_req->setMethod('PUT');
     $upload_req->setConfig(array('follow_redirects' => TRUE, 'max_redirects' => 3, 'ssl_verify_peer' => FALSE, 'ssl_verify_host' => FALSE, 'connect_timeout' => 60));
     $upload_req->setAdapter($this->adapter);
     // Set the proxy if one has been set earlier
     if (isset($this->proxy) && is_array($this->proxy)) {
         $upload_req->setConfig(array('proxy_host' => $this->proxy['server'], 'proxy_port' => $this->proxy['port'], 'proxy_user' => $this->proxy['user'], 'proxy_password' => $this->proxy['password']));
     }
     $upload_req->setHeader(array('User-Agent' => "{$this->AppName} using phpSmug/" . phpSmug::$version, 'Content-MD5' => md5_file($args['File']), 'Connection' => 'keep-alive'));
     if ($this->loginType == 'authd') {
         $upload_req->setHeader('X-Smug-SessionID', $this->SessionID);
     } else {
         $upload_req->setHeader('Authorization', 'OAuth realm="http://api.smugmug.com/",' . 'oauth_consumer_key="' . $this->APIKey . '",' . 'oauth_token="' . $this->oauth_token . '",' . 'oauth_signature_method="' . $this->oauth_signature_method . '",' . 'oauth_signature="' . urlencode($sig) . '",' . 'oauth_timestamp="' . $this->oauth_timestamp . '",' . 'oauth_version="1.0",' . 'oauth_nonce="' . $this->oauth_nonce . '"');
     }
     $upload_req->setHeader(array('X-Smug-Version' => $this->APIVer, 'X-Smug-ResponseType' => 'PHP', 'X-Smug-AlbumID' => $args['AlbumID'], 'X-Smug-Filename' => basename($args['FileName'])));
     // This is actually optional, but we may as well use what we're given
     /* Optional Headers */
     foreach ($args as $arg => $value) {
         if ($arg == 'File') {
             continue;
         }
         $upload_req->setHeader('X-Smug-' . $arg, $value);
     }
     //$proto = ( $this->oauth_signature_method == 'PLAINTEXT' || $this->secure ) ? 'https' : 'http';	// No secure uploads at this time.
     //$upload_req->setURL( $proto . '://upload.smugmug.com/'.$args['FileName'] );
     $upload_req->setURL('http://upload.smugmug.com/' . $args['FileName']);
     $upload_req->setBody($data);
     //Send Requests
     $upload_req->execute();
     $this->response = $upload_req->getBody();
     // For some reason the return string is formatted with \n and extra space chars.  Remove these.
     $replace = array('\\n', '\\t', '  ');
     $this->response = str_replace($replace, '', $this->response);
     $this->parsed_response = unserialize(trim($this->response));
     if ($this->parsed_response['stat'] == 'fail') {
         $this->error_code = $this->parsed_response['code'];
         $this->error_msg = $this->parsed_response['message'];
         $this->parsed_response = FALSE;
         throw new PhpSmugException("SmugMug API Error for method image_upload: {$this->error_msg}", $this->error_code);
     } else {
         $this->error_code = FALSE;
         $this->error_msg = FALSE;
     }
     return $this->parsed_response ? $this->parsed_response['Image'] : FALSE;
 }