/libraries/joomla/google/data/adsense.php

https://bitbucket.org/pastor399/newcastleunifc · PHP · 416 lines · 239 code · 32 blank · 145 comment · 22 complexity · bbc66e55545195d2c13fcd39ff2145a5 MD5 · raw file

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