Skip to content

Commit ea5791f

Browse files
committed
Link: Use GuzzleHttp to check URLs
1 parent cc278f0 commit ea5791f

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

main/inc/lib/link.lib.php

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* For licensing terms, see /license.txt */
44

55
use Chamilo\CourseBundle\Entity\CLink;
6+
use GuzzleHttp\Client;
67

78
/**
89
* Function library for the links tool.
@@ -1553,8 +1554,9 @@ public static function listLinksAndCategories(
15531554
if ($showChildren) {
15541555
$childrenContent = self::showLinksPerCategory(
15551556
$myrow['id'],
1556-
api_get_course_int_id(),
1557-
api_get_session_id()
1557+
$course_id,
1558+
$session_id,
1559+
$showActionLinks
15581560
);
15591561
}
15601562

@@ -1791,49 +1793,45 @@ public static function moveLinkDown($id)
17911793
return self::moveLinkDisplayOrder($id, 'DESC');
17921794
}
17931795

1794-
/**
1795-
* @param string $url
1796-
*
1797-
* @return bool
1798-
*/
1799-
public static function checkUrl($url)
1796+
public static function checkUrl(string $url): bool
18001797
{
1801-
// Check if curl is available.
1802-
if (!in_array('curl', get_loaded_extensions())) {
1803-
return false;
1804-
}
1805-
1806-
// set URL and other appropriate options
18071798
$defaults = [
1808-
CURLOPT_URL => $url,
1809-
CURLOPT_FOLLOWLOCATION => true, // follow redirects accept youtube.com
1810-
CURLOPT_HEADER => 0,
1811-
CURLOPT_RETURNTRANSFER => true,
1812-
CURLOPT_TIMEOUT => 4,
1799+
'allow_redirects' => [
1800+
'max' => 5, // max number of redirects allowed
1801+
'strict' => true, // whether to use strict redirects or not
1802+
'referer' => true, // whether to add the Referer header when redirecting
1803+
'protocols' => ['http', 'https'], // protocols allowed to be redirected to
1804+
'track_redirects' => true // whether to keep track of the number of redirects
1805+
],
1806+
'connect_timeout' => 4,
1807+
'timeout' => 4,
18131808
];
18141809

18151810
$proxySettings = api_get_configuration_value('proxy_settings');
18161811

18171812
if (!empty($proxySettings) &&
18181813
isset($proxySettings['curl_setopt_array'])
18191814
) {
1820-
$defaults[CURLOPT_PROXY] = $proxySettings['curl_setopt_array']['CURLOPT_PROXY'];
1821-
$defaults[CURLOPT_PROXYPORT] = $proxySettings['curl_setopt_array']['CURLOPT_PROXYPORT'];
1815+
$defaults['proxy'] = sprintf(
1816+
'%s:%s',
1817+
$proxySettings['curl_setopt_array']['CURLOPT_PROXY'],
1818+
$proxySettings['curl_setopt_array']['CURLOPT_PROXYPORT']
1819+
);
18221820
}
18231821

1824-
// Create a new cURL resource
1825-
$ch = curl_init();
1826-
curl_setopt_array($ch, $defaults);
1822+
$client = new Client(['defaults' => $defaults]);
18271823

1828-
// grab URL and pass it to the browser
1829-
ob_start();
1830-
$result = curl_exec($ch);
1831-
ob_get_clean();
1824+
try {
1825+
$response = $client->get($url);
18321826

1833-
// close cURL resource, and free up system resources
1834-
curl_close($ch);
1827+
if (200 !== $response->getStatusCode()) {
1828+
return false;
1829+
}
18351830

1836-
return $result;
1831+
return true;
1832+
} catch (Exception $e) {
1833+
return false;
1834+
}
18371835
}
18381836

18391837
/**

0 commit comments

Comments
 (0)