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

https://github.com/pjwiseman/joomla-cms · PHP · 419 lines · 239 code · 33 blank · 147 comment · 22 complexity · 89610f68d17b6abed6b540aeedf7f074 MD5 · raw file

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