1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Registry\Registry;
13
14 15 16 17 18 19
20 class JGoogleDataAdsense extends JGoogleData
21 {
22 23 24 25 26 27 28 29
30 public function __construct(Registry $options = null, JGoogleAuth $auth = null)
31 {
32 parent::__construct($options, $auth);
33
34 if (isset($this->auth) && !$this->auth->getOption('scope'))
35 {
36 $this->auth->setOption('scope', 'https://www.googleapis.com/auth/adsense');
37 }
38 }
39
40 41 42 43 44 45 46 47 48 49 50
51 public function getAccount($accountID, $subaccounts = true)
52 {
53 if ($this->isAuthenticated())
54 {
55 $url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . urlencode($accountID) . ($subaccounts ? '?tree=true' : '');
56 $jdata = $this->query($url);
57
58 if ($data = json_decode($jdata->body, true))
59 {
60 return $data;
61 }
62 else
63 {
64 throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
65 }
66 }
67 else
68 {
69 return false;
70 }
71 }
72
73 74 75 76 77 78 79 80 81 82 83
84 public function listAccounts($options = array(), $maxpages = 1)
85 {
86 if ($this->isAuthenticated())
87 {
88 $next = array_key_exists('nextPageToken', $options) ? $options['nextPage'] : null;
89 unset($options['nextPageToken']);
90 $url = 'https://www.googleapis.com/adsense/v1.1/accounts?' . http_build_query($options);
91
92 return $this->listGetData($url, $maxpages, $next);
93 }
94 else
95 {
96 return false;
97 }
98 }
99
100 101 102 103 104 105 106 107 108 109 110 111
112 public function listClients($accountID, $options = array(), $maxpages = 1)
113 {
114 if ($this->isAuthenticated())
115 {
116 $next = array_key_exists('nextPageToken', $options) ? $options['nextPage'] : null;
117 unset($options['nextPageToken']);
118 $url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . urlencode($accountID) . '/adclients?' . http_build_query($options);
119
120 return $this->listGetData($url, $maxpages, $next);
121 }
122 else
123 {
124 return false;
125 }
126 }
127
128 129 130 131 132 133 134 135 136 137 138 139
140 public function getUnit($accountID, $adclientID, $adunitID)
141 {
142 if ($this->isAuthenticated())
143 {
144 $url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . urlencode($accountID);
145 $url .= '/adclients/' . urlencode($adclientID) . '/adunits/' . urlencode($adunitID);
146 $jdata = $this->query($url);
147
148 if ($data = json_decode($jdata->body, true))
149 {
150 return $data;
151 }
152 else
153 {
154 throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
155 }
156 }
157 else
158 {
159 return false;
160 }
161 }
162
163 164 165 166 167 168 169 170 171 172 173 174 175 176
177 public function listUnitChannels($accountID, $adclientID, $adunitID, $options = array(), $maxpages = 1)
178 {
179 if ($this->isAuthenticated())
180 {
181 $next = array_key_exists('nextPageToken', $options) ? $options['nextPage'] : null;
182 unset($options['nextPageToken']);
183 $url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . urlencode($accountID);
184 $url .= '/adclients/' . urlencode($adclientID) . '/adunits/' . urlencode($adunitID) . '/customchannels?' . http_build_query($options);
185
186 return $this->listGetData($url, $maxpages, $next);
187 }
188 else
189 {
190 return false;
191 }
192 }
193
194 195 196 197 198 199 200 201 202 203 204 205
206 public function getChannel($accountID, $adclientID, $channelID)
207 {
208 if ($this->isAuthenticated())
209 {
210 $url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . urlencode($accountID) . '/adclients/';
211 $url .= urlencode($adclientID) . '/customchannels/' . urlencode($channelID);
212 $jdata = $this->query($url);
213
214 if ($data = json_decode($jdata->body, true))
215 {
216 return $data;
217 }
218 else
219 {
220 throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
221 }
222 }
223 else
224 {
225 return false;
226 }
227 }
228
229 230 231 232 233 234 235 236 237 238 239 240 241
242 public function listChannels($accountID, $adclientID, $options = array(), $maxpages = 1)
243 {
244 if ($this->isAuthenticated())
245 {
246 $next = array_key_exists('nextPageToken', $options) ? $options['nextPage'] : null;
247 unset($options['nextPageToken']);
248 $url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . urlencode($accountID) . '/adclients/' . urlencode($adclientID);
249 $url .= '/customchannels?' . http_build_query($options);
250
251 return $this->listGetData($url, $maxpages, $next);
252 }
253 else
254 {
255 return false;
256 }
257 }
258
259 260 261 262 263 264 265 266 267 268 269 270 271 272
273 public function listChannelUnits($accountID, $adclientID, $channelID, $options = array(), $maxpages = 1)
274 {
275 if ($this->isAuthenticated())
276 {
277 $next = array_key_exists('nextPageToken', $options) ? $options['nextPage'] : null;
278 unset($options['nextPageToken']);
279 $url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . urlencode($accountID) . '/adclients/' . urlencode($adclientID);
280 $url .= '/customchannels/' . urlencode($channelID) . '/adunits?' . http_build_query($options);
281
282 return $this->listGetData($url, $maxpages, $next);
283 }
284 else
285 {
286 return false;
287 }
288 }
289
290 291 292 293 294 295 296 297 298 299 300 301 302
303 public function listUrlChannels($accountID, $adclientID, $options = array(), $maxpages = 1)
304 {
305 if ($this->isAuthenticated())
306 {
307 $next = array_key_exists('nextPageToken', $options) ? $options['nextPage'] : null;
308 unset($options['nextPageToken']);
309 $url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . urlencode($accountID);
310 $url .= '/adclients/' . urlencode($adclientID) . '/urlchannels?' . http_build_query($options);
311
312 return $this->listGetData($url, $maxpages, $next);
313 }
314 else
315 {
316 return false;
317 }
318 }
319
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
335 public function generateReport($accountID, $start, $end = false, $options = array(), $maxpages = 1)
336 {
337 if ($this->isAuthenticated())
338 {
339 if (is_int($start))
340 {
341 $startobj = new DateTime;
342 $startobj->setTimestamp($start);
343 }
344 elseif (is_string($start))
345 {
346 $startobj = new DateTime($start);
347 }
348 elseif (is_a($start, 'DateTime'))
349 {
350 $startobj = $start;
351 }
352 else
353 {
354 throw new InvalidArgumentException('Invalid start time.');
355 }
356
357 if (!$end)
358 {
359 $endobj = new DateTime;
360 }
361 elseif (is_int($end))
362 {
363 $endobj = new DateTime;
364 $endobj->setTimestamp($end);
365 }
366 elseif (is_string($end))
367 {
368 $endobj = new DateTime($end);
369 }
370 elseif (is_a($end, 'DateTime'))
371 {
372 $endobj = $end;
373 }
374 else
375 {
376 throw new InvalidArgumentException('Invalid end time.');
377 }
378
379 $options['startDate'] = $startobj->format('Y-m-d');
380 $options['endDate'] = $endobj->format('Y-m-d');
381
382 unset($options['startIndex']);
383
384 $url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . urlencode($accountID) . '/reports?' . http_build_query($options);
385
386 if (strpos($url, '&'))
387 {
388 $url .= '&';
389 }
390
391 $i = 0;
392 $data['rows'] = array();
393
394 do
395 {
396 $jdata = $this->query($url . 'startIndex=' . count($data['rows']));
397 $newdata = json_decode($jdata->body, true);
398
399 if ($newdata && array_key_exists('rows', $newdata))
400 {
401 $newdata['rows'] = array_merge($data['rows'], $newdata['rows']);
402 $data = $newdata;
403 }
404 else
405 {
406 throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
407 }
408
409 $i++;
410 }
411 while (count($data['rows']) < $data['totalMatchedRows'] && $i < $maxpages);
412
413 return $data;
414 }
415 else
416 {
417 return false;
418 }
419 }
420 }
421