@@ -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,74 @@ public function exec($context, $post_data = null, $custom_request = null) {
152152 return $ response ;
153153 }
154154
155+ /**
156+ * file upload
157+ *
158+ */
159+ public function upload ($ context , $ file_list ) {
160+ $ url = $ this ->host . $ this ->api_uri . '/ ' . preg_replace ('/\// ' , '' , $ context , 1 );
161+
162+ $ ch =curl_init ();
163+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
164+ curl_setopt ($ ch , CURLOPT_URL , $ url );
165+
166+ $ attachments = array ();
167+
168+ $ i = 0 ;
169+ foreach ($ file_list as $ f ) {
170+ $ attachments [$ i ] = array (
171+ 'file ' => '@ ' . realpath ($ f )
172+ );
173+ }
174+
175+ var_dump ($ attachments );
176+ flush ();
177+
178+ // send file
179+ curl_setopt ($ ch , CURLOPT_POST , true );
180+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ attachments );
181+
182+ curl_setopt ($ ch , CURLOPT_USERPWD , "$ this ->username : $ this ->password " );
183+
184+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYHOST , $ this ->CURLOPT_SSL_VERIFYHOST );
185+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , $ this ->CURLOPT_SSL_VERIFYPEER );
186+
187+ curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , true );
188+ curl_setopt ($ ch , CURLOPT_HTTPHEADER ,
189+ array (
190+ 'Accept: */* ' ,
191+ 'Content-Type: multipart/form-data ' ,
192+ 'X-Atlassian-Token: nocheck '
193+ ));
194+
195+ curl_setopt ($ ch , CURLOPT_VERBOSE , $ this ->CURLOPT_VERBOSE );
196+
197+ $ this ->log ->addDebug ('Curl exec= ' . $ url );
198+ $ response = curl_exec ($ ch );
199+
200+ // if request failed.
201+ if (!$ response ) {
202+ $ body = curl_error ($ ch );
203+ curl_close ($ ch );
204+ // HostNotFound, No route to Host, etc Network error
205+ $ this ->log ->addError ("CURL Error: = " . $ body );
206+ throw new JIRAException ("CURL Error: = " . $ body );
207+ } else {
208+ // if request was ok, parsing http response code.
209+ $ this ->http_response = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
210+
211+ curl_close ($ ch );
212+
213+ // don't check 301, 302 because setting CURLOPT_FOLLOWLOCATION
214+ if ($ this ->http_response != 200 && $ this ->http_response != 201 ) {
215+ throw new JIRAException ("CURL HTTP Request Failed: Status Code : "
216+ . $ this ->http_response . ", URL: " . $ url
217+ . "\nError Message : " . $ response , $ this ->http_response );
218+ }
219+ }
220+
221+ return $ response ;
222+ }
155223}
156224
157225
0 commit comments