| 1 |  |  | <?php namespace Arcanedev\NoCaptcha; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Arcanedev\NoCaptcha\Utilities\ResponseV3; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Illuminate\Support\Arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Class     NoCaptchaV3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @package  Arcanedev\NoCaptcha | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @author   ARCANEDEV <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class NoCaptchaV3 extends AbstractNoCaptcha | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /* ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      |  Properties | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      | ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * Decides if we've already loaded the script file or not. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $scriptLoaded = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /* ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      |  Main Methods | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      | ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param  string  $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @return \Illuminate\Support\HtmlString | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 12 |  |     public function input($name = 'g-recaptcha-response') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 12 |  |         return $this->toHtmlString( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 12 |  |             '<input type="hidden" id="'.$name.'" name="'.$name.'">' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * Get script tag. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param  string|null  $callbackName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @return \Illuminate\Support\HtmlString | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 20 |  |     public function script($callbackName = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 20 |  |         $script = ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 20 |  |         if ( ! $this->scriptLoaded) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 20 |  |             $script = implode(PHP_EOL, [ | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 20 |  |                 '<script src="'.$this->getScriptSrc($callbackName).'"></script>', | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 20 |  |             $this->scriptLoaded = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 20 |  |         return $this->toHtmlString($script); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * Get the NoCaptcha API Script. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @return \Illuminate\Support\HtmlString | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 4 |  |     public function getApiScript() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 4 |  |         return $this->toHtmlString( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             "<script> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 window.noCaptcha = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                     render: function(action, callback) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 4 |  |                         grecaptcha.execute('".$this->getSiteKey()."', {action}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                               .then(callback); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |             </script>" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /* ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      |  Check Methods | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      | ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * Check if callback is not empty. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param  string|null  $callbackName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 20 |  |     private function hasCallbackName($callbackName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 20 |  |         return ! (is_null($callbackName) || trim($callbackName) === ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /* ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      |  Other Methods | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      | ----------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * Parse the response. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @param  string  $json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @return \Arcanedev\NoCaptcha\Utilities\AbstractResponse|mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 20 |  |     protected function parseResponse($json) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 20 |  |         return ResponseV3::fromJson($json); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * Get script source link. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @param  string|null  $callbackName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 20 |  |     private function getScriptSrc($callbackName = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 20 |  |         $queries = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 20 |  |         if ($this->hasLang()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 8 |  |             Arr::set($queries, 'hl', $this->lang); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 20 |  |         Arr::set($queries, 'render', $this->getSiteKey()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 20 |  |         if ($this->hasCallbackName($callbackName)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 4 |  |             Arr::set($queries, 'onload', $callbackName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 20 |  |         return static::CLIENT_URL . (count($queries) ? '?' . http_build_query($queries) : ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 140 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 141 |  |  |  |