| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace PaySys\CardPay; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Nette; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Nette\Application\LinkGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Nette\Utils\Strings; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use PaySys\PaySys\IConfiguration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @method string getMid() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @method string getRurl() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @method string getKey() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @method string getIpc() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @method string getLang() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @method string getMode() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @method string getRem() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 1 |  | final class Configuration implements IConfiguration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	const TEST = "test"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	const PRODUCTION = "production"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	/** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	private $mid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	/** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	private $rurl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	/** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	private $lang = 'sk'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	/** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	private $ipc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	/** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	private $key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	/** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	private $rem = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	/** @var LinkGenerator */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	private $linkGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	/** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	private $mode = self::PRODUCTION; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	/** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	private $buttonTemplate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	public function __construct(string $mid, $rurl, string $key, LinkGenerator $linkGenerator = NULL) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  | 		$this->linkGenerator = $linkGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  | 		$this->setMid($mid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  | 		$this->setRurl($rurl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  | 		$this->setKey($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  | 		$this->setIpc(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  | 		$this->setButtonTemplate(__DIR__ . '/template/button.latte'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	public function setMid(string $mid) : Configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  | 		if (!Validator::isMid($mid)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  | 			throw new \PaySys\PaySys\ConfigurationException(sprintf("Parameter MID must have 3 or 4 characters. '%s' is invalid.", $mid)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  | 		$this->mid = $mid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	public function setRurl($originalRurl) : Configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  | 		$supportedTypes = ['string', 'array']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  | 		if (!in_array(gettype($originalRurl), $supportedTypes)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  | 			throw new \PaySys\PaySys\ConfigurationException(sprintf("RURL type of '%s' is invalid. Must be %s.", gettype($originalRurl), implode(' or ', $supportedTypes))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  | 		$rurl = $originalRurl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  | 		if ($this->linkGenerator instanceof LinkGenerator) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  | 				if (is_string($originalRurl)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  | 					$rurl = $this->linkGenerator->link($originalRurl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 				} elseif (is_array($originalRurl)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  | 					$rurl = $this->linkGenerator->link($originalRurl['dest'], @$originalRurl['params']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  | 			} catch (Nette\Application\UI\InvalidLinkException $e) {} | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  | 		if (!Validator::isRurl($rurl)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  | 			throw new \PaySys\PaySys\ConfigurationException(sprintf("RURL '%s' is invalid. Must be valid URL by RFC 1738.", $originalRurl)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  | 		$this->rurl = $rurl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 98 |  | View Code Duplication | 	public function setKey(string $originalKey) : Configuration | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 1 |  | 		$key = $this->getNormalizedKey($originalKey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 1 |  | 		if (!Validator::isKey($key)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 			throw new \PaySys\PaySys\ConfigurationException(sprintf("Key '%s' is invalid. Must have 64 byte standard string or 128 byte in hexadecimal format.", $originalKey)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  | 		$this->key = $key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	public function setIpc(string $ipc = NULL) : Configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  | 		if ($ipc === NULL) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  | 			$ipc = $this->getIpAddress(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 1 |  | 		if (!Validator::isIp($ipc)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  | 			throw new \PaySys\PaySys\ConfigurationException(sprintf("IP '%s' is not valid.", $ipc)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 1 |  | 		$this->ipc = $ipc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 121 |  | View Code Duplication | 	public function setLang(string $originalLang) : Configuration | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 1 |  | 		$lang = strtolower($originalLang); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 1 |  | 		if (!Validator::isLang($lang)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 1 |  | 			throw new \PaySys\PaySys\ConfigurationException(sprintf("Lang '%s' is not supported.", $originalLang)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 1 |  | 		$this->lang = $lang; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 1 |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	public function setMode(string $mode) : Configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 1 |  | 		if (!in_array($mode, [self::TEST, self::PRODUCTION])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 1 |  | 			throw new \PaySys\PaySys\ConfigurationException(sprintf("Mode '%s' is not valid. Use please '%s' or '%s'.", $mode, self::TEST, self::PRODUCTION)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 1 |  | 		$this->mode = $mode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 1 |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  | 	public function setRem(string $rem) : Configuration | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 142 | 1 |  | 		if (!Nette\Utils\Validators::isEmail($rem)) | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  | 			throw new \PaySys\PaySys\ConfigurationException(sprintf("'%s' is not valid e-mail.", $rem)); | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 145 | 1 |  | 		$this->rem = $rem; | 
            
                                                                        
                            
            
                                    
            
            
                | 146 | 1 |  | 		return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	public function setButtonTemplate(string $path) : Configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 1 |  | 		if (!file_exists($path)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 1 |  | 			throw new \PaySys\PaySys\ConfigurationException(sprintf("Template file '%s' not exists.", $path)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 1 |  | 		$this->buttonTemplate = $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 1 |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	public function getButtonTemplate() : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 1 |  | 		return $this->buttonTemplate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	public function __call($method, $arguments) : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 1 |  | 		return $this->{strtolower(substr($method, 3))}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 	private function getNormalizedKey(string $originalKey) : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 1 |  | 		if (strlen($originalKey) === 128 OR strlen($originalKey) === 191) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 1 |  | 			if (strlen($originalKey) === 191) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 1 |  | 				$key = str_replace(':', '', $originalKey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 1 |  | 				$key = $originalKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 1 |  | 			if (strlen($key) === 128) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 1 |  | 				return pack("H*", $key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 | 1 |  | 		return $originalKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 	private function getIpAddress() : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 1 |  | 		foreach (['HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR'] as $key) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 | 1 |  | 			if (array_key_exists($key, $_SERVER) === TRUE) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 				foreach (explode(',', $_SERVER[$key]) as $ip) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 					$ip = Strings::trim($ip); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 					if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== FALSE) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 | 1 |  | 						return $ip; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 					} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 | 1 |  | 		return "0.0.0.0"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 198 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 199 |  |  |  |