1 | <?php |
||
14 | |||
15 | final class Service |
||
16 | { |
||
17 | /** |
||
18 | * The solvemedia server URL's |
||
19 | */ |
||
20 | const ADCOPY_API_SERVER = 'http://api.solvemedia.com'; |
||
21 | const ADCOPY_API_SECURE_SERVER = 'https://api-secure.solvemedia.com'; |
||
22 | const ADCOPY_VERIFY_SERVER = 'http://verify.solvemedia.com/papi/verify'; |
||
23 | const ADCOPY_SIGNUP = 'http://api.solvemedia.com/public/signup'; |
||
24 | |||
25 | /** |
||
26 | * @var ClientInterface |
||
27 | */ |
||
28 | private $_client; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $_pubkey; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $_privkey; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $_hashkey; |
||
44 | |||
45 | /** |
||
46 | * Construct a Service object with the required api key values. |
||
47 | * |
||
48 | * @param ClientInterface $client The guzzle client to send the requests over. |
||
49 | * @param string $pubkey A public key for solvemedia |
||
50 | * @param string $privkey A private key for solvemedia |
||
51 | * @param string $hashkey An optional hash key for verification |
||
52 | * @throws Exception |
||
53 | */ |
||
54 | public function __construct(ClientInterface $client, string $pubkey, string $privkey, string $hashkey = '') |
||
65 | |||
66 | /** |
||
67 | * Gets the challenge HTML (javascript and non-javascript version). |
||
68 | * This is called from the browser, and the resulting solvemedia HTML widget |
||
69 | * is embedded within the HTML form it was called from. |
||
70 | * |
||
71 | * @param string $error The error given by solvemedia (optional, default is null) |
||
72 | * @param boolean $useSsl Should the request be made over ssl? (optional, default is false) |
||
73 | * @return string The HTML to be embedded in the user's form. |
||
74 | */ |
||
75 | public function getHtml(string $error = null, bool $useSsl = false) : string |
||
89 | |||
90 | /** |
||
91 | * Calls an HTTP POST function to verify if the user's guess was correct |
||
92 | * |
||
93 | * @param string $remoteip |
||
94 | * @param string $challenge |
||
95 | * @param string $response |
||
96 | * @throws Exception |
||
97 | * @return Response |
||
98 | */ |
||
99 | public function checkAnswer(string $remoteip, string $challenge = null, string $response = null) : Response |
||
100 | { |
||
101 | if (empty($remoteip)) { |
||
102 | throw new Exception('For security reasons, you must pass the remote ip to solvemedia'); |
||
103 | } |
||
104 | |||
105 | //discard spam submissions |
||
106 | if (empty($challenge) || empty($response)) { |
||
107 | return new Response(false, 'incorrect-solution'); |
||
108 | } |
||
109 | |||
110 | $httpResponse = $this->_client->request( |
||
111 | 'POST', |
||
112 | self::ADCOPY_VERIFY_SERVER, |
||
113 | [ |
||
114 | 'headers' => ['User-Agent' => 'solvemedia/PHP'], |
||
115 | 'form_params' => [ |
||
116 | 'privatekey' => $this->_privkey, |
||
117 | 'remoteip' => $remoteip, |
||
118 | 'challenge' => $challenge, |
||
119 | 'response' => $response, |
||
120 | ], |
||
121 | ] |
||
122 | ); |
||
123 | |||
124 | if ($httpResponse->getStatusCode() !== 200) { |
||
125 | return new Response(false, $httpResponse->getReasonPhrase()); |
||
126 | } |
||
127 | |||
128 | $answers = explode("\n", (string)$httpResponse->getBody()); |
||
129 | |||
130 | if (!empty($this->_hashkey)) { |
||
131 | // validate message authenticator |
||
132 | $hash = sha1($answers[0] . $challenge . $this->_hashkey); |
||
133 | |||
134 | if ($hash !== $answers[2]) { |
||
135 | return new Response(false, 'hash-fail'); |
||
136 | } |
||
160 |