@@ -67,13 +67,13 @@ public function __construct($config)
6767 $ this ->password = $ config ['password ' ];
6868
6969 if (isset ($ config ['CURLOPT_SSL_VERIFYHOST ' ]))
70- $ this ->CURLOPT_SSL_VERIFYHOST = $ config ['CURLOPT_SSL_VERIFYHOST ' ] === ' true ' ? true : false ;
70+ $ this ->CURLOPT_SSL_VERIFYHOST = $ config ['CURLOPT_SSL_VERIFYHOST ' ] === true ? true : false ;
7171
7272 if (isset ($ config ['CURLOPT_SSL_VERIFYPEER ' ]))
73- $ this ->CURLOPT_SSL_VERIFYPEER = $ config ['CURLOPT_SSL_VERIFYPEER ' ] === ' true ' ? true : false ;
73+ $ this ->CURLOPT_SSL_VERIFYPEER = $ config ['CURLOPT_SSL_VERIFYPEER ' ] === true ? true : false ;
7474
7575 if (isset ($ config ['CURLOPT_VERBOSE ' ]))
76- $ this ->CURLOPT_VERBOSE = $ config ['CURLOPT_VERBOSE ' ] === ' true ' ? true : false ;
76+ $ this ->CURLOPT_VERBOSE = $ config ['CURLOPT_VERBOSE ' ] === true ? true : false ;
7777
7878 if (isset ($ config ['LOG_FILE ' ]))
7979 $ this ->LOG_FILE = $ config ['LOG_FILE ' ];
@@ -152,6 +152,71 @@ public function exec($context, $post_data = null, $custom_request = null) {
152152 return $ response ;
153153 }
154154
155+ /**
156+ * file upload
157+ *
158+ * @param context url context
159+ * @param upload_file upload file.
160+ *
161+ * @todo multiple file upload suppport.
162+ *
163+ */
164+ public function upload ($ context , $ upload_file ) {
165+ $ url = $ this ->host . $ this ->api_uri . '/ ' . preg_replace ('/\// ' , '' , $ context , 1 );
166+
167+ $ ch =curl_init ();
168+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
169+ curl_setopt ($ ch , CURLOPT_URL , $ url );
170+
171+ $ attachments = array (
172+ 'file ' => '@ ' . realpath ($ upload_file )
173+ );
174+
175+ // send file
176+ curl_setopt ($ ch , CURLOPT_POST , true );
177+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ attachments );
178+
179+ curl_setopt ($ ch , CURLOPT_USERPWD , "$ this ->username : $ this ->password " );
180+
181+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYHOST , $ this ->CURLOPT_SSL_VERIFYHOST );
182+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , $ this ->CURLOPT_SSL_VERIFYPEER );
183+
184+ curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , true );
185+ curl_setopt ($ ch , CURLOPT_HTTPHEADER ,
186+ array (
187+ 'Accept: */* ' ,
188+ 'Content-Type: multipart/form-data ' ,
189+ 'X-Atlassian-Token: nocheck '
190+ ));
191+
192+ curl_setopt ($ ch , CURLOPT_VERBOSE , $ this ->CURLOPT_VERBOSE );
193+
194+ $ this ->log ->addDebug ('Curl exec= ' . $ url );
195+ $ response = curl_exec ($ ch );
196+
197+ // if request failed.
198+ if (!$ response ) {
199+ $ body = curl_error ($ ch );
200+ curl_close ($ ch );
201+ // HostNotFound, No route to Host, etc Network error
202+ $ this ->log ->addError ("CURL Error: = " . $ body );
203+ throw new JIRAException ("CURL Error: = " . $ body );
204+ } else {
205+ // if request was ok, parsing http response code.
206+ $ this ->http_response = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
207+
208+ curl_close ($ ch );
209+
210+ // don't check 301, 302 because setting CURLOPT_FOLLOWLOCATION
211+ if ($ this ->http_response != 200 && $ this ->http_response != 201 ) {
212+ throw new JIRAException ("CURL HTTP Request Failed: Status Code : "
213+ . $ this ->http_response . ", URL: " . $ url
214+ . "\nError Message : " . $ response , $ this ->http_response );
215+ }
216+ }
217+
218+ return $ response ;
219+ }
155220}
156221
157222
0 commit comments