PluginProbe
Wp-Insert / 1.5.2
Wp-Insert v1.5.2
trunk 1.0 1.1 1.2 1.2.1 1.2.2 1.3.0 1.3.1 1.3.3 1.4 1.5.0 1.5.1 1.5.2 1.5.3 1.6.0 1.6.1 1.6.2 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 2.0 All 63 releases
wp-insert / includes / adsense.php

adsense.php in Wp-Insert 1.5.2, at includes/adsense.php

399 lines 14.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * PHP class that can retrieve data from AdSense account
5 * It's based on PHP AdSense account monitor class (http://www.webtoolkit.info/php-adsense-account-monitor.html)
6 * Copyright (C) 2009 Alex Polski
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21
22 /**
23 * sys_get_temp_dir function for the proper work in PHP4
24 * Based on http://www.php.net/sys_get_temp_dir
25 */
26 if (!function_exists('sys_get_temp_dir')) {
27 function sys_get_temp_dir() {
28 if (!empty($_ENV['TMP'])) { return realpath($_ENV['TMP']); }
29 if (!empty($_ENV['TMPDIR'])) { return realpath( $_ENV['TMPDIR']); }
30 if (!empty($_ENV['TEMP'])) { return realpath( $_ENV['TEMP']); }
31 $tempfile = tempnam(uniqid(rand(), TRUE), '');
32 if (file_exists($tempfile)) {
33 unlink($tempfile);
34 return realpath(dirname($tempfile));
35 }
36 }
37 }
38
39 /**
40 * PHP class that can retrieve data from AdSense account
41 * @package AdSense
42 */
43 class AdSense {
44 /**
45 * Stores curl connection handle
46 * @var resource
47 */
48 var $curl = null;
49
50
51 /**
52 * AdSense::AdSense()
53 * AdSense class constructor
54 */
55 function AdSense(){
56 $this->cookieFile = tempnam(sys_get_temp_dir(), 'cookie');
57
58 $this->curl = curl_init();
59 curl_setopt($this->curl, CURLOPT_HEADER, false);
60 curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
61 curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
62 curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);
63 curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
64 curl_setopt($this->curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7");
65 curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookieFile);
66 curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookieFile);
67
68 register_shutdown_function(array(&$this, '__destructor'));
69 }
70
71
72 /**
73 * AdSense::__destructor()
74 * AdSense class destructor
75 */
76 function __destructor(){
77 @curl_close($this->curl);
78 @unlink($this->cookieFile);
79 }
80
81
82 /**
83 * AdSense::connect()
84 * Connects to AdSense account using supplied credentials
85 * Returns true on unsuccessful connection, false otherwise
86 *
87 * @param string $username AdSense username
88 * @param string $password AdSense password
89 * @return boolean
90 */
91 function connect($username, $password){
92 // /adsense/
93 curl_setopt($this->curl, CURLOPT_POST, false);
94 curl_setopt($this->curl, CURLOPT_URL, 'https://www.google.com/adsense/');
95 $content = curl_exec($this->curl);
96
97 // /adsense/login-box.js
98 curl_setopt($this->curl, CURLOPT_POST, false);
99 curl_setopt($this->curl, CURLOPT_URL, 'https://www.google.com/adsense/login-box.js');
100 $content = curl_exec($this->curl);
101 $content = preg_replace(
102 array("/\\\\75/", "/\\\\42/", "/\\\\46/", "/\\\\075/"),
103 array('=', '"', '&', '='),
104 $content);
105 preg_match('/src="([^"]+)"/', $content, $match);
106 $next_url = $match[1];
107 $next_url = str_replace('&amp;', '&', $next_url);
108
109 // /accounts/ServiceLoginBox
110 curl_setopt($this->curl, CURLOPT_POST, false);
111 curl_setopt($this->curl, CURLOPT_URL, $next_url);
112 $content = curl_exec($this->curl);
113 preg_match_all('<input type="hidden" name="(.*?)" value="(.*?)">', curl_exec($this->curl), $out);
114 $params = array();
115 foreach($out[1] as $key=>$name) {
116 $params[] = $name . '=' . urlencode($out[2][$key]);
117 }
118 $params[] = 'Email=' . urlencode($username);
119 $params[] = 'Passwd=' . urlencode($password);
120 $params[] = 'null=' . urlencode('Sign in');
121
122 // /accounts/ServiceLoginBoxAuth
123 curl_setopt($this->curl, CURLOPT_POST, true);
124 curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/accounts/ServiceLoginBoxAuth");
125 curl_setopt($this->curl, CURLOPT_POSTFIELDS, join('&', $params));
126 $content = curl_exec($this->curl);
127 preg_match("/<a href=\"([^\"]+)\"[^>]*>click here to continue<\/a>/", $content, $match);
128 $next_url = $match[1];
129 $next_url = str_replace('&amp;', '&', $next_url);
130
131 // /accounts/CheckCookie
132 curl_setopt($this->curl, CURLOPT_POST, false);
133 curl_setopt($this->curl, CURLOPT_URL, $next_url);
134 $content = curl_exec($this->curl);
135 preg_match('/location\.replace\("(.+?)"\)/', $content, $match);
136 $next_url = $match[1];
137 $next_url = preg_replace_callback("/\\\\x(..)/i", create_function('$match', 'return chr(hexdec($match[1]));'), $next_url);
138
139 // /accounts/SetSID
140 curl_setopt($this->curl, CURLOPT_POST, false);
141 curl_setopt($this->curl, CURLOPT_URL, $next_url);
142 $content = curl_exec($this->curl);
143
144 // did we login ?
145 if (eregi("<a href=\"/adsense/signout\">", $content)) {
146 return true;
147 } else {
148 return false;
149 }
150 }
151
152 /**
153 * Log out
154 */
155 function log_out(){
156 curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/signout");
157 curl_exec($this->curl);
158 }
159
160 /**
161 * AdSense::parse()
162 * Parses AdSense page and gets all stats
163 * Returns associative array with collected data
164 *
165 * @param string $content AdSense page content
166 * @return array
167 */
168 function parse($content){
169 preg_match_all('/<td nowrap valign="top" style="text-align:right" class="">(.*?)<\/td>/', $content, $matches);
170 return array(
171 "impressions" => $matches[1][0],
172 "clicks" => $matches[1][1],
173 "ctr" => $matches[1][2],
174 "ecpm" => $matches[1][3],
175 "earnings" => $matches[1][4]
176 );
177
178 }
179
180
181 /**
182 * AdSense::today()
183 * Gets AdSense data for the period: today
184 * Returns associative array with collected data
185 *
186 * @return array
187 */
188 function today(){
189 curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=today");
190 return $this->parse(curl_exec($this->curl));
191 }
192
193
194 /**
195 * AdSense::yesterday()
196 * Gets AdSense data for the period: yesterday
197 * Returns associative array with collected data
198 *
199 * @return array
200 */
201 function yesterday(){
202 curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=yesterday");
203 return $this->parse(curl_exec($this->curl));
204 }
205
206
207 /**
208 * AdSense::last7days()
209 * Gets AdSense data for the period: last7days
210 * Returns associative array with collected data
211 *
212 * @return array
213 */
214 function last7days(){
215 curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=last7days");
216 return $this->parse(curl_exec($this->curl));
217 }
218
219
220 /**
221 * AdSense::lastmonth()
222 * Gets AdSense data for the period: lastmonth
223 * Returns associative array with collected data
224 *
225 * @return array
226 */
227 function lastmonth(){
228 curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=lastmonth");
229 return $this->parse(curl_exec($this->curl));
230 }
231
232
233 /**
234 * AdSense::thismonth()
235 * Gets AdSense data for the period: thismonth
236 * Returns associative array with collected data
237 *
238 * @return array
239 */
240 function thismonth(){
241 curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=thismonth");
242 return $this->parse(curl_exec($this->curl));
243 }
244
245
246 /**
247 * AdSense::sincelastpayment()
248 * Gets AdSense data for the period: sincelastpayment
249 * Returns associative array with collected data
250 *
251 * @return array
252 */
253 function sincelastpayment(){
254 curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview?timePeriod=sincelastpayment");
255 return $this->parse(curl_exec($this->curl));
256 }
257
258 /**
259 * Get current report id by report name.
260 * Helps to avoid problems with report editing and ids.
261 *
262 * @param string $name
263 * @return int
264 */
265 function get_report_id_from_name($name){
266 curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/overview");
267 $page = curl_exec($this->curl);
268
269 preg_match('~<a href=".*reportId=([0-9]+)">'.$name.'</a>~i', $page, $matches);
270
271 if(empty($matches[1])) return false;
272
273 return $matches[1];
274 }
275
276 /**
277 * Get csv data from custom report
278 * Warning: if you will edit report at Google Adsense page, its id will change!
279 *
280 * @param int $id
281 * @param string $encoding
282 * @return string
283 */
284 function get_report_as_csv($report_id, $encoding = 'UTF-8'){
285 curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/view-custom.do?reportId=$report_id&outputFormat=TSV_EXCEL");
286
287 //By default report is in UTF-16LE
288 return iconv('UTF-16', $encoding, curl_exec($this->curl));
289 }
290
291 /**
292 * Get report data as array. Uses html parser. A bit slower than get_report_as_csv.
293 * Warning: if you will edit report at Google Adsense page, its id will change!
294 *
295 * @param int $report_id
296 * @return array
297 */
298 function report($report_id){
299 $result = array();
300 curl_setopt($this->curl, CURLOPT_URL, "https://www.google.com/adsense/report/view-custom.do?reportId=$report_id");
301 $content = curl_exec($this->curl);
302 if (preg_match('/var\s+reportTable\s+=\s+new\s+AsyncReportTable\(([^\)]+)\);/si', $content, $matches)) {
303 $params = array();
304 foreach (explode(",\n", $matches[1]) as $v) {
305 $params[] = trim($v, " \t'\"\n\r");
306 }
307 curl_setopt($this->curl, CURLOPT_URL,
308 'https://www.google.com/adsense/report/online-stored-reporttable?storedReportId=' .
309 $params[0] . '&reportUri=' . str_replace('?', '%3F', str_replace('/', '%2F', str_replace('\x', '%', $params[2]))) .
310 '&formId=' . $params[3] . '&title=' . str_replace('+', '%20', urlencode($params[1])) . '&waitTime0');
311 do {
312 $content = curl_exec($this->curl);
313 if (empty($content)) {
314 sleep(1);
315 }
316 } while (empty($content));
317 if (preg_match_all('/<tr\s+class="(odd|even)\s+datarow">\s*' .
318 '<td[^>]*>(.*?)<\/td>\s*' .
319 '<td[^>]*>(.*?)<\/td>\s*' .
320 '<td[^>]*>(.*?)<\/td>\s*' .
321 '<td[^>]*>(.*?)<\/td>\s*' .
322 '<td[^>]*>(.*?)<\/td>\s*' .
323 '<td[^>]*>(.*?)<\/td>\s*' .
324 '<\/tr>/si', $content, $matches)) {
325 foreach ($matches[2] as $k => $v) {
326 $result[$k]['channel'] = preg_replace('/<.+>/', '', $v);
327 $result[$k]['impressions'] = $matches[3][$k];
328 $result[$k]['clicks'] = $matches[4][$k];
329 $result[$k]['ctr'] = $matches[5][$k];
330 $result[$k]['ecpm'] = $matches[6][$k];
331 $result[$k]['earnings'] = $matches[7][$k];
332 }
333 }
334 }
335 return $result;
336 }
337
338 function quick_report($url){
339 $result = array();
340 curl_setopt($this->curl, CURLOPT_URL, $url);
341 $content = curl_exec($this->curl);
342 if (preg_match('/var\s+reportTable\s+=\s+new\s+AsyncReportTable\(([^\)]+)\);/si', $content, $matches)) {
343 $params = array();
344 foreach (explode(",\n", $matches[1]) as $v) {
345 $params[] = trim($v, " \t'\"\n\r");
346 }
347 $reportUri = preg_replace(
348 array('/&/', '/=/', '/\?/', '/\//'),
349 array('%26', '%3D', '%3F', '%2F'),
350 $params[2]);
351 $reportUri = str_replace('\x', '%', $reportUri);
352 curl_setopt($this->curl, CURLOPT_URL,
353 'https://www.google.com/adsense/report/online-stored-reporttable?storedReportId=' .
354 $params[0] . '&reportUri=' . $reportUri .
355 '&formId=' . $params[3] . '&title=' . str_replace('+', '%20', urlencode($params[1])) . '&waitTime0');
356 $tr = 0;
357 do {
358 $content = curl_exec($this->curl);
359 if (empty($content) or $content === true) {
360 sleep(1);
361 if (++$tr > 10) {
362 echo "Can't get the report data!\n";
363 die;
364 }
365 }
366 } while (empty($content) or $content === true);
367 if (preg_match_all('/<tr\s+class="(odd|even)\s+datarow">\s*' .
368 '<td[^>]*>(.*?)<\/td>\s*' .
369 '<td[^>]*>(.*?)<\/td>\s*' .
370 '<td[^>]*>(.*?)<\/td>\s*' .
371 '<td[^>]*>(.*?)<\/td>\s*' .
372 '<td[^>]*>(.*?)<\/td>\s*' .
373 '<td[^>]*>(.*?)<\/td>\s*' .
374 '<td[^>]*>(.*?)<\/td>\s*' .
375 '<\/tr>/si', $content, $matches)) {
376 $day = 0;
377 $n = 0;
378 foreach ($matches[3] as $k => $v) {
379 if ($day === 0) {
380 $day = date('Y-m-d', strtotime($matches[2][$k]));
381 }
382 if (isset($matches[2][$k - 1]) && $matches[2][$k - 1] != $matches[2][$k]) {
383 $day = date('Y-m-d', strtotime($matches[2][$k]));
384 $n = 0;
385 }
386 $result[$day][$n]['channel'] = preg_replace('/\s*<[^>]+>\s*/', '', $v);
387 $result[$day][$n]['impressions'] = $matches[4][$k];
388 $result[$day][$n]['clicks'] = $matches[5][$k];
389 $result[$day][$n]['ctr'] = $matches[6][$k];
390 $result[$day][$n]['ecpm'] = $matches[7][$k];
391 $result[$day][$n]['earnings'] = $matches[8][$k];
392 $n++;
393 }
394 }
395 }
396 return $result;
397 }
398 }
399