1 <?php
  2 /**
  3  * @package     Joomla.Platform
  4  * @subpackage  Google
  5  *
  6  * @copyright   Copyright (C) 2005 - 2017 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 use Joomla\Registry\Registry;
 13 
 14 /**
 15  * Google API data class for the Joomla Platform.
 16  *
 17  * @since       12.3
 18  * @deprecated  4.0  Use the `joomla/google` package via Composer instead
 19  */
 20 abstract class JGoogleData
 21 {
 22     /**
 23      * @var    Registry  Options for the Google data object.
 24      * @since  12.3
 25      */
 26     protected $options;
 27 
 28     /**
 29      * @var    JGoogleAuth  Authentication client for the Google data object.
 30      * @since  12.3
 31      */
 32     protected $auth;
 33 
 34     /**
 35      * Constructor.
 36      *
 37      * @param   Registry     $options  Google options object.
 38      * @param   JGoogleAuth  $auth     Google data http client object.
 39      *
 40      * @since   12.3
 41      */
 42     public function __construct(Registry $options = null, JGoogleAuth $auth = null)
 43     {
 44         $this->options = isset($options) ? $options : new Registry;
 45         $this->auth = isset($auth) ? $auth : new JGoogleAuthOauth2($this->options);
 46     }
 47 
 48     /**
 49      * Method to authenticate to Google
 50      *
 51      * @return  boolean  True on success.
 52      *
 53      * @since   12.3
 54      */
 55     public function authenticate()
 56     {
 57         return $this->auth->authenticate();
 58     }
 59 
 60     /**
 61      * Check authentication
 62      *
 63      * @return  boolean  True if authenticated.
 64      *
 65      * @since   12.3
 66      */
 67     public function isAuthenticated()
 68     {
 69         return $this->auth->isAuthenticated();
 70     }
 71 
 72     /**
 73      * Method to validate XML
 74      *
 75      * @param   string  $data  XML data to be parsed
 76      *
 77      * @return  SimpleXMLElement  XMLElement of parsed data
 78      *
 79      * @since   12.3
 80      * @throws UnexpectedValueException
 81      */
 82     protected static function safeXml($data)
 83     {
 84         try
 85         {
 86             return new SimpleXMLElement($data, LIBXML_NOWARNING | LIBXML_NOERROR);
 87         }
 88         catch (Exception $e)
 89         {
 90             throw new UnexpectedValueException("Unexpected data received from Google: `$data`.", $e->getCode(), $e);
 91         }
 92     }
 93 
 94     /**
 95      * Method to retrieve a list of data
 96      *
 97      * @param   array   $url       URL to GET
 98      * @param   int     $maxpages  Maximum number of pages to return
 99      * @param   string  $token     Next page token
100      *
101      * @return  mixed  Data from Google
102      *
103      * @since   12.3
104      * @throws UnexpectedValueException
105      */
106     protected function listGetData($url, $maxpages = 1, $token = null)
107     {
108         $qurl = $url;
109 
110         if (strpos($url, '&') && isset($token))
111         {
112             $qurl .= '&pageToken=' . $token;
113         }
114         elseif (isset($token))
115         {
116             $qurl .= 'pageToken=' . $token;
117         }
118 
119         $jdata = $this->query($qurl);
120         $data = json_decode($jdata->body, true);
121 
122         if ($data && array_key_exists('items', $data))
123         {
124             if ($maxpages != 1 && array_key_exists('nextPageToken', $data))
125             {
126                 $data['items'] = array_merge($data['items'], $this->listGetData($url, $maxpages - 1, $data['nextPageToken']));
127             }
128 
129             return $data['items'];
130         }
131         elseif ($data)
132         {
133             return array();
134         }
135         else
136         {
137             throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
138         }
139     }
140 
141     /**
142      * Method to retrieve data from Google
143      *
144      * @param   string  $url      The URL for the request.
145      * @param   mixed   $data     The data to include in the request.
146      * @param   array   $headers  The headers to send with the request.
147      * @param   string  $method   The type of http request to send.
148      *
149      * @return  mixed  Data from Google.
150      *
151      * @since   12.3
152      */
153     protected function query($url, $data = null, $headers = null, $method = 'get')
154     {
155         return $this->auth->query($url, $data, $headers, $method);
156     }
157 
158     /**
159      * Get an option from the JGoogleData instance.
160      *
161      * @param   string  $key  The name of the option to get.
162      *
163      * @return  mixed  The option value.
164      *
165      * @since   12.3
166      */
167     public function getOption($key)
168     {
169         return $this->options->get($key);
170     }
171 
172     /**
173      * Set an option for the JGoogleData instance.
174      *
175      * @param   string  $key    The name of the option to set.
176      * @param   mixed   $value  The option value to set.
177      *
178      * @return  JGoogleData  This object for method chaining.
179      *
180      * @since   12.3
181      */
182     public function setOption($key, $value)
183     {
184         $this->options->set($key, $value);
185 
186         return $this;
187     }
188 }
189