1919use OCP \AppFramework \Controller ;
2020use OCP \AppFramework \Http ;
2121use OCP \AppFramework \Http \DataResponse ;
22+ use OCP \Http \Client \IClientService ;
2223use OCP \IConfig ;
2324use OCP \IL10N ;
2425use OCP \IRequest ;
@@ -39,6 +40,7 @@ public function __construct(
3940 private CacheService $ cacheService ,
4041 private LoggerInterface $ logger ,
4142 private IAppManager $ appManager ,
43+ private IClientService $ clientService ,
4244 private ?string $ userId
4345 ) {
4446 parent ::__construct ($ appName , $ request );
@@ -269,26 +271,20 @@ public function getUserInfo(): DataResponse {
269271 ])
270272 ];
271273
272- $ ch = curl_init ();
273- curl_setopt ($ ch , CURLOPT_URL , $ apiUrl );
274- curl_setopt ($ ch , CURLOPT_POST , true );
275- curl_setopt ($ ch , CURLOPT_POSTFIELDS , http_build_query ($ postData ));
276- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
277- curl_setopt ($ ch , CURLOPT_TIMEOUT , 15 );
278- curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , false );
279- curl_setopt ($ ch , CURLOPT_HTTPHEADER , [
280- 'Content-Type: application/x-www-form-urlencoded ' ,
281- 'Auth-Token: ' . $ applicationToken ,
282- 'User-Agent: Nextcloud-iTop-Integration/1.0 '
283- ]);
284-
285- $ result = curl_exec ($ ch );
286- $ httpCode = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
287- $ error = curl_error ($ ch );
288- curl_close ($ ch );
289-
290- if ($ result === false || !empty ($ error )) {
291- return new DataResponse (['error ' => $ this ->l10n ->t ('Connection failed: %s ' , [$ error ?: $ this ->l10n ->t ('Unknown error ' )])], Http::STATUS_SERVICE_UNAVAILABLE );
274+ try {
275+ $ client = $ this ->clientService ->newClient ();
276+ $ response = $ client ->post ($ apiUrl , [
277+ 'body ' => http_build_query ($ postData ),
278+ 'headers ' => [
279+ 'Content-Type ' => 'application/x-www-form-urlencoded ' ,
280+ 'Auth-Token ' => $ applicationToken ,
281+ 'User-Agent ' => 'Nextcloud-iTop-Integration/1.0 '
282+ ],
283+ 'timeout ' => 15 ,
284+ ]);
285+ $ result = $ response ->getBody ();
286+ } catch (\Exception $ e ) {
287+ return new DataResponse (['error ' => $ this ->l10n ->t ('Connection failed: %s ' , [$ e ->getMessage ()])], Http::STATUS_SERVICE_UNAVAILABLE );
292288 }
293289
294290 $ responseData = json_decode ($ result , true );
@@ -412,30 +408,24 @@ public function testApplicationToken(string $token = ''): DataResponse {
412408 ])
413409 ];
414410
415- $ ch = curl_init ();
416- curl_setopt ($ ch , CURLOPT_URL , $ apiUrl );
417- curl_setopt ($ ch , CURLOPT_POST , true );
418- curl_setopt ($ ch , CURLOPT_POSTFIELDS , http_build_query ($ postData ));
419- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
420- curl_setopt ($ ch , CURLOPT_TIMEOUT , 15 );
421- curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , false );
422- curl_setopt ($ ch , CURLOPT_HTTPHEADER , [
423- 'Content-Type: application/x-www-form-urlencoded ' ,
424- 'Auth-Token: ' . $ token ,
425- 'User-Agent: Nextcloud-iTop-Integration/1.0 '
426- ]);
427-
428- $ result = curl_exec ($ ch );
429- $ httpCode = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
430- $ error = curl_error ($ ch );
431- curl_close ($ ch );
432-
433- $ this ->logger ->info ('iTop application token test - Method 1 (Auth-Token header) response: ' . $ result , ['app ' => Application::APP_ID ]);
411+ try {
412+ $ client = $ this ->clientService ->newClient ();
413+ $ response = $ client ->post ($ apiUrl , [
414+ 'body ' => http_build_query ($ postData ),
415+ 'headers ' => [
416+ 'Content-Type ' => 'application/x-www-form-urlencoded ' ,
417+ 'Auth-Token ' => $ token ,
418+ 'User-Agent ' => 'Nextcloud-iTop-Integration/1.0 '
419+ ],
420+ 'timeout ' => 15 ,
421+ ]);
422+ $ result = $ response ->getBody ();
434423
435- if ($ result === false || !empty ($ error )) {
424+ $ this ->logger ->info ('iTop application token test response: ' . $ result , ['app ' => Application::APP_ID ]);
425+ } catch (\Exception $ e ) {
436426 return new DataResponse ([
437427 'status ' => 'error ' ,
438- 'message ' => $ this ->l10n ->t ('Connection failed: %s ' , [$ error ?: $ this -> l10n -> t ( ' Unknown error ' )])
428+ 'message ' => $ this ->l10n ->t ('Connection failed: %s ' , [$ e -> getMessage ( )])
439429 ]);
440430 }
441431
@@ -532,37 +522,23 @@ public function testAdminConnection(string $url = ''): DataResponse {
532522 'operation ' => 'core/check_credentials '
533523 ])
534524 ];
535-
536- $ ch = curl_init ();
537- curl_setopt ($ ch , CURLOPT_URL , $ apiUrl );
538- curl_setopt ($ ch , CURLOPT_POST , true );
539- curl_setopt ($ ch , CURLOPT_POSTFIELDS , http_build_query ($ postData ));
540- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
541- curl_setopt ($ ch , CURLOPT_TIMEOUT , 15 );
542- curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , false );
543- curl_setopt ($ ch , CURLOPT_HTTPHEADER , [
544- 'Content-Type: application/x-www-form-urlencoded ' ,
545- 'User-Agent: Nextcloud-iTop-Integration/1.0 '
546- ]);
547-
548- $ result = curl_exec ($ ch );
549- $ httpCode = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
550- $ error = curl_error ($ ch );
551- curl_close ($ ch );
552-
553- if ($ result === false || !empty ($ error )) {
554- return new DataResponse ([
555- 'status ' => 'error ' ,
556- 'message ' => $ this ->l10n ->t ('Connection failed: %s ' , [$ error ?: $ this ->l10n ->t ('Unknown error ' )]),
557- 'details ' => ['url ' => $ testUrl , 'api_url ' => $ apiUrl ]
558- ]);
559- }
560525
561- if ($ httpCode !== 200 ) {
526+ try {
527+ $ client = $ this ->clientService ->newClient ();
528+ $ response = $ client ->post ($ apiUrl , [
529+ 'body ' => http_build_query ($ postData ),
530+ 'headers ' => [
531+ 'Content-Type ' => 'application/x-www-form-urlencoded ' ,
532+ 'User-Agent ' => 'Nextcloud-iTop-Integration/1.0 '
533+ ],
534+ 'timeout ' => 15 ,
535+ ]);
536+ $ result = $ response ->getBody ();
537+ } catch (\Exception $ e ) {
562538 return new DataResponse ([
563539 'status ' => 'error ' ,
564- 'message ' => $ this ->l10n ->t ('iTop API endpoint returned HTTP %d ' , [$ httpCode ]),
565- 'details ' => ['http_code ' => $ httpCode , ' url ' => $ testUrl , 'api_url ' => $ apiUrl ]
540+ 'message ' => $ this ->l10n ->t ('Connection failed: %s ' , [$ e -> getMessage () ]),
541+ 'details ' => ['url ' => $ testUrl , 'api_url ' => $ apiUrl ]
566542 ]);
567543 }
568544
@@ -1207,30 +1183,24 @@ private function validatePersonalTokenAndExtractPersonId(string $personalToken):
12071183 ])
12081184 ];
12091185
1210- $ ch = curl_init ();
1211- curl_setopt ($ ch , CURLOPT_URL , $ apiUrl );
1212- curl_setopt ($ ch , CURLOPT_POST , true );
1213- curl_setopt ($ ch , CURLOPT_POSTFIELDS , http_build_query ($ postData ));
1214- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
1215- curl_setopt ($ ch , CURLOPT_TIMEOUT , 15 );
1216- curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , false );
1217- curl_setopt ($ ch , CURLOPT_HTTPHEADER , [
1218- 'Content-Type: application/x-www-form-urlencoded ' ,
1219- 'Auth-Token: ' . $ personalToken ,
1220- 'User-Agent: Nextcloud-iTop-Integration/1.0 '
1221- ]);
1222-
1223- $ result = curl_exec ($ ch );
1224- $ httpCode = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
1225- $ error = curl_error ($ ch );
1226- curl_close ($ ch );
1227-
1228- if ($ result === false || !empty ($ error )) {
1186+ try {
1187+ $ client = $ this ->clientService ->newClient ();
1188+ $ response = $ client ->post ($ apiUrl , [
1189+ 'body ' => http_build_query ($ postData ),
1190+ 'headers ' => [
1191+ 'Content-Type ' => 'application/x-www-form-urlencoded ' ,
1192+ 'Auth-Token ' => $ personalToken ,
1193+ 'User-Agent ' => 'Nextcloud-iTop-Integration/1.0 '
1194+ ],
1195+ 'timeout ' => 15 ,
1196+ ]);
1197+ $ result = $ response ->getBody ();
1198+ } catch (\Exception $ e ) {
12291199 return [
12301200 'success ' => false ,
12311201 'person_id ' => null ,
12321202 'user_info ' => null ,
1233- 'error ' => $ this ->l10n ->t ('Connection failed: %s ' , [$ error ?: $ this -> l10n -> t ( ' Unknown error ' )])
1203+ 'error ' => $ this ->l10n ->t ('Connection failed: %s ' , [$ e -> getMessage ( )])
12341204 ];
12351205 }
12361206
@@ -1297,6 +1267,11 @@ private function validatePersonalTokenAndExtractPersonId(string $personalToken):
12971267 $ applicationToken = $ this ->crypto ->decrypt ($ encryptedAppToken );
12981268
12991269 // Query User class using application token
1270+ // Validate personId to prevent OQL injection
1271+ if (!is_numeric ($ personId ) || $ personId < 0 ) {
1272+ throw new \InvalidArgumentException ('Invalid person ID ' );
1273+ }
1274+ $ personId = (int )$ personId ;
13001275 $ getUserData = [
13011276 'json_data ' => json_encode ([
13021277 'operation ' => 'core/get ' ,
@@ -1305,23 +1280,19 @@ private function validatePersonalTokenAndExtractPersonId(string $personalToken):
13051280 'output_fields ' => 'id,login,finalclass '
13061281 ])
13071282 ];
1308-
1309- $ ch2 = curl_init ();
1310- curl_setopt ($ ch2 , CURLOPT_URL , $ apiUrl );
1311- curl_setopt ($ ch2 , CURLOPT_POST , true );
1312- curl_setopt ($ ch2 , CURLOPT_POSTFIELDS , http_build_query ($ getUserData ));
1313- curl_setopt ($ ch2 , CURLOPT_RETURNTRANSFER , true );
1314- curl_setopt ($ ch2 , CURLOPT_TIMEOUT , 15 );
1315- curl_setopt ($ ch2 , CURLOPT_SSL_VERIFYPEER , false );
1316- curl_setopt ($ ch2 , CURLOPT_HTTPHEADER , [
1317- 'Content-Type: application/x-www-form-urlencoded ' ,
1318- 'Auth-Token: ' . $ applicationToken ,
1319- 'User-Agent: Nextcloud-iTop-Integration/1.0 '
1283+
1284+ $ client = $ this ->clientService ->newClient ();
1285+ $ response = $ client ->post ($ apiUrl , [
1286+ 'body ' => http_build_query ($ getUserData ),
1287+ 'headers ' => [
1288+ 'Content-Type ' => 'application/x-www-form-urlencoded ' ,
1289+ 'Auth-Token ' => $ applicationToken ,
1290+ 'User-Agent ' => 'Nextcloud-iTop-Integration/1.0 '
1291+ ],
1292+ 'timeout ' => 15 ,
13201293 ]);
1321-
1322- $ userResult = curl_exec ($ ch2 );
1323- curl_close ($ ch2 );
1324-
1294+ $ userResult = $ response ->getBody ();
1295+
13251296 $ userData = json_decode ($ userResult , true );
13261297 if (isset ($ userData ['objects ' ]) && !empty ($ userData ['objects ' ])) {
13271298 $ userObject = reset ($ userData ['objects ' ]);
0 commit comments