@@ -221,17 +221,6 @@ private function scanPad(ScannerServiceCallbackInterface $callback): void
221221 $ this ->doSocketWebsocket ($ socketIoVersion , $ cookieString , $ callback , $ token );
222222 } catch (Exception $ e ) {
223223 $ callback ->onScanPadException ($ e );
224-
225- try {
226- if ($ socketIoVersion === ElephantClient::CLIENT_4X ) {
227- $ this ->doSocketPolling4 ($ cookies , $ token , $ callback );
228- return ;
229- }
230-
231- $ this ->doSocketPolling ($ socketIoVersion , $ cookies , $ token , $ callback );
232- } catch (Exception $ e ) {
233- $ callback ->onScanPadException ($ e );
234- }
235224 }
236225 }
237226
@@ -356,191 +345,6 @@ private function scanHealth(ScannerServiceCallbackInterface $callback): void
356345 }
357346 }
358347
359- private function doSocketPolling (
360- int $ socketIoVersion ,
361- CookieJar $ cookies ,
362- string $ token ,
363- ScannerServiceCallbackInterface $ callback
364- ): void {
365- $ engine = ElephantClient::engine ($ socketIoVersion , '' );
366-
367- $ queryParameters = [
368- 'padId ' => $ this ->padId ,
369- 'EIO ' => $ engine ->getOptions ()['version ' ],
370- 'transport ' => 'polling ' ,
371- 't ' => Yeast::yeast (),
372- 'b64 ' => 1 ,
373- ];
374-
375- $ response = $ this ->client ->get ($ this ->baseUrl . 'socket.io/ ' , [
376- 'query ' => $ queryParameters ,
377- 'cookies ' => $ cookies ,
378- ]);
379- $ body = (string )$ response ->getBody ();
380- if ($ body === 'Welcome to socket.io. ' ) {
381- $ this ->packageVersion = '1.4.0 ' ;
382- throw new Exception ('Socket.io 1 not supported ' );
383- }
384- $ curlyBracketPos = strpos ($ body , '{ ' );
385- if ($ curlyBracketPos === false ) {
386- throw new Exception ('No JSON response: ' . $ body );
387- }
388- $ body = substr ($ body , $ curlyBracketPos );
389- $ data = json_decode ($ body , true , 512 , JSON_THROW_ON_ERROR );
390- $ sid = $ data ['sid ' ];
391-
392- $ queryParameters ['sid ' ] = $ sid ;
393- $ queryParameters ['t ' ] = Yeast::yeast ();
394-
395- $ response = $ this ->client ->get ($ this ->baseUrl . 'socket.io/ ' , [
396- 'query ' => $ queryParameters ,
397- 'cookies ' => $ cookies ,
398- ]);
399- $ body = (string )$ response ->getBody ();
400- if ($ body !== '2:40 ' ) {
401- throw new Exception ('Invalid response: ' . $ body );
402- }
403-
404- $ postData = json_encode ([
405- 'message ' ,
406- [
407- 'component ' => 'pad ' ,
408- 'type ' => 'CLIENT_READY ' ,
409- 'padId ' => $ this ->padId ,
410- 'sessionID ' => 'null ' ,
411- 'token ' => $ token ,
412- 'password ' => null ,
413- 'protocolVersion ' => 2 ,
414- ]
415- ]);
416-
417- $ queryParameters ['t ' ] = Yeast::yeast ();
418- $ response = $ this ->client ->post ($ this ->baseUrl . 'socket.io/ ' , [
419- 'query ' => $ queryParameters ,
420- 'body ' => (mb_strlen ($ postData ) + 2 ) . ':42 ' . $ postData ,
421- 'cookies ' => $ cookies ,
422- ]);
423- $ body = (string )$ response ->getBody ();
424- if ($ body !== 'ok ' ) {
425- throw new Exception ('Invalid response: ' . $ body );
426- }
427-
428- $ queryParameters ['t ' ] = Yeast::yeast ();
429- $ response = $ this ->client ->get ($ this ->baseUrl . 'socket.io/ ' , [
430- 'query ' => $ queryParameters ,
431- 'cookies ' => $ cookies ,
432- ]);
433- $ this ->handleClientVarsResponse ($ response , $ callback );
434- }
435-
436- private function doSocketPolling4 (
437- CookieJar $ cookies ,
438- string $ token ,
439- ScannerServiceCallbackInterface $ callback
440- ): void {
441- $ queryParameters = [
442- 'padId ' => $ this ->padId ,
443- 'EIO ' => 4 ,
444- 'transport ' => 'polling ' ,
445- 't ' => Yeast::yeast (),
446- 'b64 ' => 1 ,
447- ];
448-
449- $ response = $ this ->client ->get ($ this ->baseUrl . 'socket.io/ ' , [
450- 'query ' => $ queryParameters ,
451- 'cookies ' => $ cookies ,
452- ]);
453- $ body = (string )$ response ->getBody ();
454- $ curlyBracketPos = strpos ($ body , '{ ' );
455- if ($ curlyBracketPos === false ) {
456- throw new Exception ('No JSON response: ' . $ body );
457- }
458- $ body = substr ($ body , $ curlyBracketPos );
459- $ data = json_decode ($ body , true , 512 , JSON_THROW_ON_ERROR );
460- $ sid = $ data ['sid ' ];
461-
462- $ queryParameters ['sid ' ] = $ sid ;
463- $ queryParameters ['t ' ] = Yeast::yeast ();
464-
465- $ response = $ this ->client ->post ($ this ->baseUrl . 'socket.io/ ' , [
466- 'query ' => $ queryParameters ,
467- 'cookies ' => $ cookies ,
468- 'body ' => '40 ' ,
469- ]);
470- $ body = (string )$ response ->getBody ();
471- if ($ body !== 'ok ' ) {
472- throw new Exception ('Invalid response: ' . $ body );
473- }
474-
475- $ queryParameters ['t ' ] = Yeast::yeast ();
476-
477- $ response = $ this ->client ->get ($ this ->baseUrl . 'socket.io/ ' , [
478- 'query ' => $ queryParameters ,
479- 'cookies ' => $ cookies ,
480- ]);
481- $ body = (string )$ response ->getBody ();
482-
483- if (str_starts_with ($ body , '40 ' ) === false ) {
484- throw new Exception ('Invalid response: ' . $ body );
485- }
486-
487- $ postData = json_encode ([
488- 'message ' ,
489- [
490- 'component ' => 'pad ' ,
491- 'type ' => 'CLIENT_READY ' ,
492- 'padId ' => $ this ->padId ,
493- 'sessionID ' => 'null ' ,
494- 'token ' => $ token ,
495- 'password ' => null ,
496- 'protocolVersion ' => 2 ,
497- ]
498- ]);
499-
500- $ queryParameters ['t ' ] = Yeast::yeast ();
501- $ response = $ this ->client ->post ($ this ->baseUrl . 'socket.io/ ' , [
502- 'query ' => $ queryParameters ,
503- 'body ' => '42 ' . $ postData ,
504- 'cookies ' => $ cookies ,
505- ]);
506- $ body = (string )$ response ->getBody ();
507- if ($ body !== 'ok ' ) {
508- throw new Exception ('Invalid response: ' . $ body );
509- }
510-
511- $ queryParameters ['t ' ] = Yeast::yeast ();
512- $ response = $ this ->client ->get ($ this ->baseUrl . 'socket.io/ ' , [
513- 'query ' => $ queryParameters ,
514- 'cookies ' => $ cookies ,
515- ]);
516- $ this ->handleClientVarsResponse ($ response , $ callback );
517- }
518-
519- private function handleClientVarsResponse (
520- ResponseInterface $ response ,
521- ScannerServiceCallbackInterface $ callback ,
522- ): void
523- {
524- $ body = (string )$ response ->getBody ();
525- $ body = substr ($ body , strpos ($ body , '[ ' ));
526- $ data = json_decode ($ body , true , 512 , JSON_THROW_ON_ERROR );
527- $ data = $ data [1 ];
528- $ accessStatus = $ data ['accessStatus ' ] ?? null ;
529- if ($ accessStatus === 'deny ' ) {
530- $ callback ->onScanPadException (new EtherpadServiceNotPublicException ('Pads are not publicly accessible ' ));
531- return ;
532- }
533-
534- $ version = $ data ['data ' ]['plugins ' ]['plugins ' ]['ep_etherpad-lite ' ]['package ' ]['version ' ];
535- $ onlyPlugins = $ data ['data ' ]['plugins ' ]['plugins ' ];
536- unset($ onlyPlugins ['ep_etherpad-lite ' ]);
537-
538- $ this ->packageVersion = $ version ;
539- $ callback ->onClientVars ($ version , $ data );
540- $ callback ->onScanPluginsList ($ onlyPlugins );
541- $ callback ->onScanPadSuccess ();
542- }
543-
544348 private function doSocketWebsocket (
545349 int $ socketIoVersion ,
546350 string $ cookieString ,
@@ -560,7 +364,7 @@ private function doSocketWebsocket(
560364 ]
561365 ]), $ callback ->getConsoleLogger ());
562366
563- $ socketIoClient ->initialize ();
367+ $ socketIoClient ->connect ();
564368 $ socketIoClient ->of ('/ ' );
565369 $ socketIoClient ->emit ('message ' , [
566370 'component ' => 'pad ' ,
@@ -572,12 +376,8 @@ private function doSocketWebsocket(
572376 'protocolVersion ' => 2 ,
573377 ]);
574378
575- $ expirationTime = microtime (true ) + 2 ;
576-
577- while (microtime (true ) < $ expirationTime ) {
578- usleep (10000 );
579- $ result = $ socketIoClient ->drain ();
580- if ($ result !== null && is_array ($ result ->data )) {
379+ while ($ result = $ socketIoClient ->wait ('message ' , 2 )) {
380+ if (is_array ($ result ->data )) {
581381 $ accessStatus = $ result ->data ['accessStatus ' ] ?? null ;
582382 if ($ accessStatus === 'deny ' ) {
583383 $ callback ->onScanPadException (new EtherpadServiceNotPublicException ('Pads are not publicly accessible ' ));
@@ -600,11 +400,11 @@ private function doSocketWebsocket(
600400 }
601401 }
602402
603- $ socketIoClient ->close ();
403+ $ socketIoClient ->disconnect ();
604404 }
605405
606406 public function getBaseUrl (): string
607407 {
608408 return $ this ->baseUrl ;
609409 }
610- }
410+ }
0 commit comments