-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathayah.php
266 lines (235 loc) · 8.18 KB
/
ayah.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/*
* Are You A Human
* PHP Integration Library
*
* @version 1.0.2
*
* - Documentation and latest version
* http://portal.areyouahuman.com/help
* - Get an AYAH Publisher Key
* https://portal.areyouahuman.com
* - Discussion group
* http://getsatisfaction.com/areyouahuman
*
* Copyright (c) 2011 AYAH LLC -- http://www.areyouahuman.com
* AUTHORS:
* Jonathan Brown - [email protected]
* Sneaky Pete - [email protected]
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* History
*
* 1.0.2: 03/21/2012:
* - Changed the script tag to include language="JavaScript" and type="text/javascript" for maximum compatibility.
*
* 1.0.1: 12/12/2011:
* - Add fallback support for JSON decode with Michal Migurski's library
* - Add fallback support when there is no cURL library
* - Changed the handling for the constants so that, if they don't exist, it doesn't freak out
* - Changed the handling for ayah_config.php so that, if it doesn't exist, it doesn't freak out
*
* 1.0.0: 11/15/2011: Initial release with script-implementation support
*
*
*/
if (file_exists('ayah_config.php')) {
require_once('ayah_config.php');
} else {
error_log("AYAH: ayah_config.php missing; will require setting of specific values.");
}
class AYAH {
protected $ayah_publisher_key;
protected $ayah_scoring_key;
protected $ayah_web_service_host;
protected $session_secret;
/**
* Returns the markup for the PlayThru
*
* @return string
*/
public function getPublisherHTML() {
$url = 'https://' . $this->ayah_web_service_host . "/ws/script/" .
urlencode($this->ayah_publisher_key);
return "<div id='AYAH'></div><script src='". $url ."' type='text/javascript' language='JavaScript'></script>";
}
/**
* Check whether the user is a human
* Wrapper for the scoreGame API call
*
* @return boolean
*/
public function scoreResult() {
$result = false;
if ($this->session_secret) {
$fields = array(
'session_secret' => urlencode($this->session_secret),
'scoring_key' => $this->ayah_scoring_key
);
$resp = $this->doHttpsPostReturnJSONArray($this->ayah_web_service_host, "/ws/scoreGame", $fields);
if ($resp) {
$result = ($resp->status_code == 1);
}
}
return $result;
}
/**
* Records a conversion
* Called on the goal page that A and B redirect to
* A/B Testing Specific Function
*
* @return boolean
*/
public function recordConversion(){
if( isset( $this->session_secret ) ){
return '<iframe style="border: none;" height="0" width="0" src="https://' .
$this->ayah_web_service_host . '/ws/recordConversion/'.
urlencode($this->session_secret) . '"></iframe>';
} else {
error_log('AYAH::recordConversion - AYAH Conversion Error: No Session Secret');
return FALSE;
}
}
/**
* Constructor
* If the session secret exists in input, it grabs it
* @param $params associative array with keys publisher_key, scoring_key, web_service_host
*
*/
public function __construct($params = array()) {
if(array_key_exists("session_secret", $_REQUEST)){
$this->session_secret = $_REQUEST["session_secret"];
}
// Set them to defaults
$this->ayah_publisher_key = "";
$this->ayah_scoring_key = "";
$this->ayah_web_service_host = "ws.areyouahuman.com";
// If the constants exist, override with those
if (defined('AYAH_PUBLISHER_KEY')) {
$this->ayah_publisher_key = AYAH_PUBLISHER_KEY;
}
if (defined('AYAH_SCORING_KEY')) {
$this->ayah_scoring_key = AYAH_SCORING_KEY;
}
if (defined('AYAH_WEB_SERVICE_HOST')) {
$this->ayah_web_service_host = AYAH_WEB_SERVICE_HOST;
}
// Lastly grab the parameters input and save them
foreach (array_keys($params) as $key) {
if (in_array($key, array("publisher_key", "scoring_key", "web_service_host"))) {
$variable = "ayah_" . $key;
$this->$variable = $params[$key];
} else {
error_log("AYAH::__construct: Unrecognized key for constructor param: $key");
}
}
// Generate some warnings if a foot shot is coming
if ($this->ayah_publisher_key == "") {
error_log("AYAH::__construct: Warning: Publisher key is not defined. This won't work.");
}
if ($this->ayah_scoring_key == "") {
error_log("AYAH::__construct: Warning: Scoring key is not defined. This won't work.");
}
if ($this->ayah_web_service_host == "") {
error_log("AYAH::__construct: Warning: Web service host is not defined. This won't work.");
}
}
/**
* Do a HTTPS POST, return some JSON decoded as array (Internal function)
* @param $host hostname
* @param $path path
* @param $fields associative array of fields
* return JSON decoded data structure or empty data structure
*/
protected function doHttpsPostReturnJSONArray($hostname, $path, $fields) {
$result = $this->doHttpsPost($hostname, $path, $fields);
if ($result) {
$result = $this->doJSONArrayDecode($result);
} else {
error_log("AYAH::doHttpsPostGetJSON: Post to https://$hostname$path returned no result.");
$result = array();
}
return $result;
}
// Internal function; does an HTTPS post
protected function doHttpsPost($hostname, $path, $fields) {
$result = "";
// URLencode the post string
$fields_string = "";
foreach($fields as $key=>$value) {
if (is_array($value)) {
foreach ($value as $v) {
$fields_string .= $key . '[]=' . $v . '&';
}
} else {
$fields_string .= $key.'='.$value.'&';
}
}
rtrim($fields_string,'&');
// cURL or something else
if (function_exists('curl_init')) {
$curlsession = curl_init();
curl_setopt($curlsession,CURLOPT_URL,"https://" . $hostname . $path);
curl_setopt($curlsession,CURLOPT_POST,count($fields));
curl_setopt($curlsession,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($curlsession,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curlsession,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($curlsession,CURLOPT_SSL_VERIFYPEER,false);
$result = curl_exec($curlsession);
} else {
error_log("No cURL support.");
// Build a header
$http_request = "POST $path HTTP/1.1\r\n";
$http_request .= "Host: $hostname\r\n";
$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
$http_request .= "Content-Length: " . strlen($fields_string) . "\r\n";
$http_request .= "User-Agent: AreYouAHuman/PHP 1.0.2\r\n";
$http_request .= "Connection: Close\r\n";
$http_request .= "\r\n";
$http_request .= $fields_string ."\r\n";
$result = '';
$errno = $errstr = "";
$fs = fsockopen("ssl://" . $hostname, 443, $errno, $errstr, 10);
if( false == $fs ) {
error_log('Could not open socket');
} else {
fwrite($fs, $http_request);
while (!feof($fs)) {
$result .= fgets($fs, 4096);
}
$result = explode("\r\n\r\n", $result, 2);
$result = $result[1];
}
}
return $result;
}
// Internal function: does a JSON decode of the string
protected function doJSONArrayDecode($string) {
$result = array();
if (function_exists("json_decode")) {
try {
$result = json_decode( $string);
} catch (Exception $e) {
error_log("AYAH::doJSONArrayDecode() - Exception when calling json_decode: " . $e->getMessage());
$result = null;
}
} elseif (file_Exists("json.php")) {
$json = new Services_JSON();
$result = $json->decode($string);
if (!is_array($result)) {
error_log("AYAH::doJSONArrayDecode: Expected array; got something else: $result");
$result = array();
}
} else {
error_log("AYAH::doJSONArrayDecode: No JSON decode function available.");
}
return $result;
}
}