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