1
+ <?php
2
+ /*
3
+ * Copyright (c) 2009 by Jeff Weisberg
4
+ * Author: Jeff Weisberg
5
+ * Created: 2009-Jun-22 16:44 (EDT)
6
+ * Function: SolveMedia API php code
7
+ *
8
+ * $Id$
9
+ *
10
+ * This is a PHP library that handles calling SolveMedia.
11
+ * - Documentation and latest version
12
+ * http://www.solvemedia.com/
13
+ * - Get a SolveMedia API Keys
14
+ * http://api.solvemedia.com/public/signup
15
+ */
16
+
17
+ /* This code is based on code from,
18
+ * and copied, modified and distributed with permission in accordance with its terms:
19
+ *
20
+ * Copyright (c) 2007 reCAPTCHA
21
+ * AUTHORS:
22
+ * Mike Crawford
23
+ * Ben Maurer
24
+ *
25
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
26
+ * of this software and associated documentation files (the "Software"), to deal
27
+ * in the Software without restriction, including without limitation the rights
28
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
29
+ * copies of the Software, and to permit persons to whom the Software is
30
+ * furnished to do so, subject to the following conditions:
31
+ *
32
+ * The above copyright notice and this permission notice shall be included in
33
+ * all copies or substantial portions of the Software.
34
+ *
35
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
41
+ * THE SOFTWARE.
42
+ */
43
+
44
+ /**
45
+ * The solvemedia server URL's
46
+ */
47
+ define ("ADCOPY_API_SERVER " , "http://api.solvemedia.com " );
48
+ define ("ADCOPY_API_SECURE_SERVER " , "https://api-secure.solvemedia.com " );
49
+ define ("ADCOPY_VERIFY_SERVER " , "verify.solvemedia.com " );
50
+ define ("ADCOPY_SIGNUP " , "http://api.solvemedia.com/public/signup " );
51
+
52
+ /**
53
+ * Encodes the given data into a query string format
54
+ * @param $data - array of string elements to be encoded
55
+ * @return string - encoded request
56
+ */
57
+ function _adcopy_qsencode ($ data ) {
58
+ $ req = "" ;
59
+ foreach ( $ data as $ key => $ value )
60
+ $ req .= $ key . '= ' . urlencode ( stripslashes ($ value ) ) . '& ' ;
61
+
62
+ // Cut the last '&'
63
+ $ req =substr ($ req ,0 ,strlen ($ req )-1 );
64
+ return $ req ;
65
+ }
66
+
67
+
68
+
69
+ /**
70
+ * Submits an HTTP POST to a solvemedia server
71
+ * @param string $host
72
+ * @param string $path
73
+ * @param array $data
74
+ * @param int port
75
+ * @return array response
76
+ */
77
+ function _adcopy_http_post ($ host , $ path , $ data , $ port = 80 ) {
78
+
79
+ $ req = _adcopy_qsencode ($ data );
80
+
81
+ $ http_request = "POST $ path HTTP/1.0 \r\n" ;
82
+ $ http_request .= "Host: $ host \r\n" ;
83
+ $ http_request .= "Content-Type: application/x-www-form-urlencoded; \r\n" ;
84
+ $ http_request .= "Content-Length: " . strlen ($ req ) . "\r\n" ;
85
+ $ http_request .= "User-Agent: solvemedia/PHP \r\n" ;
86
+ $ http_request .= "\r\n" ;
87
+ $ http_request .= $ req ;
88
+
89
+ $ response = '' ;
90
+ if ( false == ( $ fs = @fsockopen ($ host , $ port , $ errno , $ errstr , 10 ) ) ) {
91
+ die ('Could not open socket ' );
92
+ }
93
+
94
+ fwrite ($ fs , $ http_request );
95
+
96
+ while ( !feof ($ fs ) )
97
+ $ response .= fgets ($ fs , 1024 ); // One TCP-IP packet [sic]
98
+ fclose ($ fs );
99
+ $ response = explode ("\r\n\r\n" , $ response , 2 );
100
+
101
+ return $ response ;
102
+ }
103
+
104
+
105
+
106
+ /**
107
+ * Gets the challenge HTML (javascript and non-javascript version).
108
+ * This is called from the browser, and the resulting solvemedia HTML widget
109
+ * is embedded within the HTML form it was called from.
110
+ * @param string $pubkey A public key for solvemedia
111
+ * @param string $error The error given by solvemedia (optional, default is null)
112
+ * @param boolean $use_ssl Should the request be made over ssl? (optional, default is false)
113
+
114
+ * @return string - The HTML to be embedded in the user's form.
115
+ */
116
+ function solvemedia_get_html ($ pubkey , $ error = null , $ use_ssl = false )
117
+ {
118
+ if ($ pubkey == null || $ pubkey == '' ) {
119
+ die ("To use solvemedia you must get an API key from <a href=' " . ADCOPY_SIGNUP . "'> " . ADCOPY_SIGNUP . "</a> " );
120
+ }
121
+
122
+ if ($ use_ssl ) {
123
+ $ server = ADCOPY_API_SECURE_SERVER ;
124
+ } else {
125
+ $ server = ADCOPY_API_SERVER ;
126
+ }
127
+
128
+ $ errorpart = "" ;
129
+ if ($ error ) {
130
+ $ errorpart = ";error=1 " ;
131
+ }
132
+ return '<script type="text/javascript" src=" ' . $ server . '/papi/challenge.script?k= ' . $ pubkey . $ errorpart . '"></script>
133
+
134
+ <noscript>
135
+ <iframe src=" ' . $ server . '/papi/challenge.noscript?k= ' . $ pubkey . $ errorpart . '" height="300" width="500" frameborder="0"></iframe><br/>
136
+ <textarea name="adcopy_challenge" rows="3" cols="40"></textarea>
137
+ <input type="hidden" name="adcopy_response" value="manual_challenge"/>
138
+ </noscript> ' ;
139
+ }
140
+
141
+
142
+
143
+
144
+ /**
145
+ * A SolveMediaResponse is returned from solvemedia_check_answer()
146
+ */
147
+ class SolveMediaResponse {
148
+ var $ is_valid ;
149
+ var $ error ;
150
+ }
151
+
152
+
153
+ /**
154
+ * Calls an HTTP POST function to verify if the user's guess was correct
155
+ * @param string $privkey
156
+ * @param string $remoteip
157
+ * @param string $challenge
158
+ * @param string $response
159
+ * @param string $hashkey
160
+ * @return SolveMediaResponse
161
+ */
162
+ function solvemedia_check_answer ($ privkey , $ remoteip , $ challenge , $ response , $ hashkey = '' )
163
+ {
164
+ if ($ privkey == null || $ privkey == '' ) {
165
+ die ("To use solvemedia you must get an API key from <a href=' " . ADCOPY_SIGNUP . "'> " . ADCOPY_SIGNUP . "</a> " );
166
+ }
167
+
168
+ if ($ remoteip == null || $ remoteip == '' ) {
169
+ die ("For security reasons, you must pass the remote ip to solvemedia " );
170
+ }
171
+
172
+ //discard spam submissions
173
+ if ($ challenge == null || strlen ($ challenge ) == 0 || $ response == null || strlen ($ response ) == 0 ) {
174
+ $ adcopy_response = new SolveMediaResponse ();
175
+ $ adcopy_response ->is_valid = false ;
176
+ $ adcopy_response ->error = 'incorrect-solution ' ;
177
+ return $ adcopy_response ;
178
+ }
179
+
180
+ $ response = _adcopy_http_post (ADCOPY_VERIFY_SERVER , "/papi/verify " ,
181
+ array (
182
+ 'privatekey ' => $ privkey ,
183
+ 'remoteip ' => $ remoteip ,
184
+ 'challenge ' => $ challenge ,
185
+ 'response ' => $ response
186
+ )
187
+ );
188
+
189
+ $ answers = explode ("\n" , $ response [1 ]);
190
+ $ adcopy_response = new SolveMediaResponse ();
191
+
192
+ if ( strlen ($ hashkey ) ){
193
+ # validate message authenticator
194
+ $ hash = sha1 ( $ answers [0 ] . $ challenge . $ hashkey );
195
+
196
+ if ( $ hash != $ answers [2 ] ){
197
+ $ adcopy_response ->is_valid = false ;
198
+ $ adcopy_response ->error = 'hash-fail ' ;
199
+ return $ adcopy_response ;
200
+ }
201
+ }
202
+
203
+ if (trim ($ answers [0 ]) == 'true ' ) {
204
+ $ adcopy_response ->is_valid = true ;
205
+ }
206
+ else {
207
+ $ adcopy_response ->is_valid = false ;
208
+ $ adcopy_response ->error = $ answers [1 ];
209
+ }
210
+ return $ adcopy_response ;
211
+
212
+ }
213
+
214
+ /**
215
+ * gets a URL where the user can sign up for solvemedia. If your application
216
+ * has a configuration page where you enter a key, you should provide a link
217
+ * using this function.
218
+ * @param string $domain The domain where the page is hosted
219
+ * @param string $appname The name of your application
220
+ */
221
+ function solvemedia_get_signup_url ($ domain = null , $ appname = null ) {
222
+ return ADCOPY_SIGNUP . "? " . _adcopy_qsencode (array ('domain ' => $ domain , 'app ' => $ appname ));
223
+ }
224
+
225
+
226
+ /* Mailhide related code */
227
+ /* [ deleted ] */
228
+
229
+ ?>
0 commit comments