44
55namespace PhpMyAdmin \Utils ;
66
7+ use Composer \CaBundle \CaBundle ;
8+
79use function base64_encode ;
810use function curl_exec ;
911use function curl_getinfo ;
1517use function ini_get ;
1618use function intval ;
1719use function is_array ;
20+ use function is_dir ;
1821use function parse_url ;
1922use function preg_match ;
2023use function stream_context_create ;
2124use function strlen ;
2225
2326use const CURL_IPRESOLVE_V4 ;
2427use const CURLINFO_HTTP_CODE ;
25- use const CURLINFO_SSL_VERIFYRESULT ;
2628use const CURLOPT_CAINFO ;
2729use const CURLOPT_CAPATH ;
2830use const CURLOPT_CONNECTTIMEOUT ;
@@ -145,7 +147,6 @@ private function response(
145147 * @param bool $returnOnlyStatus If set to true, the method would only return response status
146148 * @param mixed $content Content to be sent with HTTP request
147149 * @param string $header Header to be set for the HTTP request
148- * @param int $ssl SSL mode to use
149150 *
150151 * @return string|bool|null
151152 */
@@ -154,8 +155,7 @@ private function curl(
154155 $ method ,
155156 $ returnOnlyStatus = false ,
156157 $ content = null ,
157- $ header = '' ,
158- $ ssl = 0
158+ $ header = ''
159159 ) {
160160 $ curlHandle = curl_init ($ url );
161161 if ($ curlHandle === false ) {
@@ -188,21 +188,14 @@ private function curl(
188188 $ curlStatus &= (int ) curl_setopt ($ curlHandle , CURLOPT_POSTFIELDS , $ content );
189189 }
190190
191- $ curlStatus &= (int ) curl_setopt ($ curlHandle , CURLOPT_SSL_VERIFYHOST , '2 ' );
192- $ curlStatus &= (int ) curl_setopt ($ curlHandle , CURLOPT_SSL_VERIFYPEER , '1 ' );
193-
194- /**
195- * Configure ISRG Root X1 to be able to verify Let's Encrypt SSL
196- * certificates even without properly configured curl in PHP.
197- *
198- * See https://letsencrypt.org/certificates/
199- */
200- $ certsDir = ROOT_PATH . 'libraries/certs/ ' ;
201- /* See code below for logic */
202- if ($ ssl == CURLOPT_CAPATH ) {
203- $ curlStatus &= (int ) curl_setopt ($ curlHandle , CURLOPT_CAPATH , $ certsDir );
204- } elseif ($ ssl == CURLOPT_CAINFO ) {
205- $ curlStatus &= (int ) curl_setopt ($ curlHandle , CURLOPT_CAINFO , $ certsDir . 'cacert.pem ' );
191+ $ curlStatus &= (int ) curl_setopt ($ curlHandle , CURLOPT_SSL_VERIFYHOST , 2 );
192+ $ curlStatus &= (int ) curl_setopt ($ curlHandle , CURLOPT_SSL_VERIFYPEER , true );
193+
194+ $ caPathOrFile = CaBundle::getSystemCaRootBundlePath ();
195+ if (is_dir ($ caPathOrFile )) {
196+ $ curlStatus &= (int ) curl_setopt ($ curlHandle , CURLOPT_CAPATH , $ caPathOrFile );
197+ } else {
198+ $ curlStatus &= (int ) curl_setopt ($ curlHandle , CURLOPT_CAINFO , $ caPathOrFile );
206199 }
207200
208201 $ curlStatus &= (int ) curl_setopt ($ curlHandle , CURLOPT_RETURNTRANSFER , true );
@@ -217,28 +210,6 @@ private function curl(
217210
218211 $ response = @curl_exec ($ curlHandle );
219212 if ($ response === false ) {
220- /*
221- * In case of SSL verification failure let's try configuring curl
222- * certificate verification. Unfortunately it is tricky as setting
223- * options incompatible with PHP build settings can lead to failure.
224- *
225- * So let's rather try the options one by one.
226- *
227- * 1. Try using system SSL storage.
228- * 2. Try setting CURLOPT_CAINFO.
229- * 3. Try setting CURLOPT_CAPATH.
230- * 4. Fail.
231- */
232- if (curl_getinfo ($ curlHandle , CURLINFO_SSL_VERIFYRESULT ) != 0 ) {
233- if ($ ssl == 0 ) {
234- return $ this ->curl ($ url , $ method , $ returnOnlyStatus , $ content , $ header , CURLOPT_CAINFO );
235- }
236-
237- if ($ ssl == CURLOPT_CAINFO ) {
238- return $ this ->curl ($ url , $ method , $ returnOnlyStatus , $ content , $ header , CURLOPT_CAPATH );
239- }
240- }
241-
242213 return null ;
243214 }
244215
@@ -273,6 +244,10 @@ private function fopen(
273244 'user_agent ' => 'phpMyAdmin ' ,
274245 'header ' => 'Accept: */* ' ,
275246 ],
247+ 'ssl ' => [
248+ 'verify_peer ' => true ,
249+ 'verify_peer_name ' => true ,
250+ ],
276251 ];
277252 if ($ header ) {
278253 $ context ['http ' ]['header ' ] .= "\n" . $ header ;
@@ -282,6 +257,13 @@ private function fopen(
282257 $ context ['http ' ]['content ' ] = $ content ;
283258 }
284259
260+ $ caPathOrFile = CaBundle::getSystemCaRootBundlePath ();
261+ if (is_dir ($ caPathOrFile )) {
262+ $ context ['ssl ' ]['capath ' ] = $ caPathOrFile ;
263+ } else {
264+ $ context ['ssl ' ]['cafile ' ] = $ caPathOrFile ;
265+ }
266+
285267 $ context = $ this ->handleContext ($ context );
286268 $ response = @file_get_contents (
287269 $ url ,
0 commit comments