BLUE DRAGON PHP SDK
All Classes Functions Variables Pages
rvp_php_sdk.class.php
1 <?php
2 /***************************************************************************************************************************/
24 defined( 'RVP_PHP_SDK_ACCESS' ) or die ( 'Cannot Execute Directly' ); // Makes sure that this file is in the correct context.
25 require_once(dirname(__FILE__).'/rvp_php_sdk_login.class.php');
26 require_once(dirname(__FILE__).'/rvp_php_sdk_user.class.php');
27 require_once(dirname(__FILE__).'/rvp_php_sdk_place.class.php');
28 require_once(dirname(__FILE__).'/rvp_php_sdk_thing.class.php');
29 require_once(dirname(__FILE__).'/lang/common.php');
30 
31 define('__SDK_VERSION__', '1.0.0.2000');
32 
33 /****************************************************************************************************************************/
43 class RVP_PHP_SDK {
44  protected $_server_uri;
45  protected $_sdk_lang;
46  protected $_server_secret;
47  protected $_api_key;
48  protected $_login_timeout;
49  protected $_login_time_limit;
50  protected $_error;
51  protected $_my_login_info;
52  protected $_my_user_info;
54  protected $_available_plugins;
55  protected $_localizations;
56  protected $_localized_errors;
57 
58  /************************************************************************************************************************/
59  /*################################################ INTERNAL STATIC METHODS #############################################*/
60  /************************************************************************************************************************/
61  /***********************/
65  protected static function _get_string_match_table() {
66  return [
67  'name' => 'search_name',
68  'tag0' => 'search_tag0',
69  'tag1' => 'search_tag1',
70  'tag2' => 'search_tag2',
71  'tag3' => 'search_tag3',
72  'tag4' => 'search_tag4',
73  'tag5' => 'search_tag5',
74  'tag6' => 'search_tag6',
75  'tag7' => 'search_tag7',
76  'tag8' => 'search_tag8',
77  'tag9' => 'search_tag9',
78  'description' => 'search_description',
79  'surname' => 'search_surname',
80  'middle_name' => 'search_middle_name',
81  'given_name' => 'search_given_name',
82  'nickname' => 'search_nickname',
83  'prefix' => 'search_prefix',
84  'suffix' => 'search_suffix',
85  'venue' => 'search_venue',
86  'street' => 'search_street_address',
87  'street_address' => 'search_street_address',
88  'extra_information' => 'search_extra_information',
89  'city' => 'search_town',
90  'town' => 'search_town',
91  'county' => 'search_county',
92  'state' => 'search_state',
93  'province' => 'search_state',
94  'postal_code' => 'search_postal_code',
95  'zip_code' => 'search_postal_code',
96  'nation' => 'search_nation',
97  ];
98  }
99 
100  /***********************/
104  protected static function _get_tag_match( $in_string
105  ) {
106  $ret = $in_string;
107  $table = static::_get_string_match_table();
108 
109  if (isset($table[$ret]) && $table[$ret]) {
110  $ret = $table[$ret];
111  }
112 
113  return $ret;
114  }
115 
116  /************************************************************************************************************************/
117  /*#################################################### INTERNAL METHODS ################################################*/
118  /************************************************************************************************************************/
119  /***********************/
125  protected function _call_REST_API( $method,
131  $url_extension,
132  $data_input = NULL,
133  $display_log = false
134  ) {
135 
136  $method = strtoupper(trim($method)); // Make sure the method is always uppercase.
137  // Initialize function local variables.
138  $file = NULL; // This will be a file handle, for uploads.
139  $content_type = NULL; // This is used to signal the content-type for uploaded files.
140  $file_size = 0; // This is the size, in bytes, of uploaded files.
141  $temp_file_name = NULL; // This is a temporary file that is used to hold files before they are sent to the server.
142  $file_data = NULL;
143 
144  // If data is provided by the caller, we read it into a temporary location, and Base64-encode it.
145  if ($data_input) {
146 
147  $file_data = base64_encode($data_input['data']);
148 
149  $temp_file_name = tempnam(sys_get_temp_dir(), 'RVP');
150 
151  $file = fopen($temp_file_name, 'w');
152 
153  fwrite($file, $file_data, strlen($file_data));
154 
155  fclose($file);
156 
157  $content_type = $data_input['type'].':base64';
158  $file_size = filesize($temp_file_name);
159 
160  $file = fopen($temp_file_name, 'rb');
161  }
162 
163  $curl = curl_init(); // Initialize the cURL handle.
164 
165  // Different methods require different ways of dealing with any file that has been passed in.
166  // The file is ignored for GET and DELETE.
167  // We ask the server not to send us EXPECT (HTTP 100) calls for POST and PUT.
168  switch ($method) {
169  case "POST":
170  curl_setopt($curl, CURLOPT_POST, true);
171 
172  // POST sends the file as a standard multipart/form-data item.
173  if ($file) {
174  curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
175  curl_setopt($curl, CURLOPT_HTTPHEADER, ['Expect:', 'Content-type: multipart/form-data']);
176  $post = Array('payload'=> curl_file_create($temp_file_name, $content_type));
177  curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
178  } else {
179  curl_setopt($curl, CURLOPT_HTTPHEADER, ['Expect:']);
180  }
181  break;
182 
183  case "PUT":
184  curl_setopt($curl, CURLOPT_HTTPHEADER, ['Expect:']);
185  curl_setopt($curl, CURLOPT_PUT, true);
186 
187  // PUT requires a direct inline file transfer.
188  if ($file) {
189  curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
190  curl_setopt($curl, CURLOPT_INFILE, $file);
191  curl_setopt($curl, CURLOPT_INFILESIZE, $file_size);
192  }
193  break;
194 
195  case "DELETE":
196  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
197  }
198 
199  // Authentication. We provide the Server Secret and the API key here.
200  if (isset($this->_server_secret) && isset($this->_api_key)) {
201  curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
202  curl_setopt($curl, CURLOPT_USERPWD, $this->_server_secret.':'.$this->_api_key);
203 
204  // This is because some servers may intercept the auth headers, so we also supply the credentials as URL query arguments.
205  if (isset($url_extension) && (false !== strpos($url_extension, '?'))) { // See if we need to append, or begin a new query.
206  $url_extension .= '&';
207  } else {
208  $url_extension .= '?';
209  }
210 
211  $url_extension .= 'login_server_secret='.urlencode($this->_server_secret).'&login_api_key='.urlencode($this->_api_key);
212  }
213 
214  curl_setopt($curl, CURLOPT_HEADER, false); // Do not return any headers, please.
215  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Please return to sender as a function response.
216  curl_setopt($curl, CURLOPT_VERBOSE, false); // Let's keep this thing simple.
217  $url = $this->_server_uri.'/'.trim($url_extension, '/');
218  curl_setopt($curl, CURLOPT_URL, $url); // This is the URL we are calling.
219 
220  // This is if we want to see a display log (echoed directly).
221  if (isset($display_log) && $display_log) {
222  curl_setopt($curl, CURLOPT_HEADERFUNCTION, function ( $curl, $header_line ) {
223  echo "<pre>$header_line</pre>";
224  return strlen($header_line);
225  });
226  echo('<div style="margin:1em">');
227  echo("<h4>Sending REST $method CALL:</h4>");
228  echo('<div>URL: <code>'.htmlspecialchars($url).'</code></div>');
229 
230  if ($this->_api_key) {
231  echo('<div>API KEY:<pre>'.htmlspecialchars($this->_api_key).'</pre></div>');
232  } else {
233  echo('<div>NO API KEY</div>');
234  }
235  }
236 
237  $result = curl_exec($curl); // Do it to it.
238 
239  $this->_last_response_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
240 
241  curl_close($curl); // Bye, now.
242 
243  // More reportage.
244  if (isset($display_log) && $display_log) {
245  if (isset($file_data)) {
246  $len = floatval(strlen($file_data));
247 
248  if (0 == $len) {
249  echo('<p><strong>ADDITIONAL DATA LENGTH IS ZERO!</strong></p>');
250  } elseif ( 1024 > $len) {
251  echo('<p><strong>ADDITIONAL DATA LENGTH:</strong> <big><code>'.$len.' Bytes</code></big></p>');
252  } elseif ( (1024 * 1024) > $len) {
253  echo('<p><strong>ADDITIONAL DATA LENGTH:</strong> <big><code>'.($len / 1024).' Kilobytes</code></big></p>');
254  } elseif ( (1024 * 1024 * 1024) > $len) {
255  echo('<p><strong>ADDITIONAL DATA LENGTH:</strong> <big><code>'.($len / (1024 * 1024)).' Megabytes</code></big></p>');
256  } elseif ( (1024 * 1024 * 1024 * 1024) > $len) {
257  echo('<p><strong>ADDITIONAL DATA LENGTH:</strong> <big><code>'.($len / (1024 * 1024 * 1024)).' Gigabytes</code></big></p>');
258  }
259  echo('<p><strong>ADDITIONAL DATA SHA:</strong> <big><code>'.sha1($file_data).'</code></big></p>');
260  }
261 
262  if (isset($httpCode) && $httpCode) {
263  echo('<div>HTTP CODE:<code>'.htmlspecialchars($httpCode, true).'</code></div>');
264  }
265 
266  if ((2048 * 1024) <= strlen($result)) { // Anything over 2MB gets spewed to a file.
267  $integer = 1;
268  $original_file_name = dirname(dirname(__FILE__)).'/text-dump-result';
269  $file_name = $original_file_name.'.txt';
270  while(file_exists($file_name)) {
271  $file_name = $original_file_name.'-'.$integer.'.txt';
272  $integer++;
273  }
274  $file_handle = fopen($file_name, 'w');
275  fwrite($file_handle, $result);
276  fclose($file_handle);
277  echo('<div>RESULT SAVED TO FILE" '.$file_name.'.</div>');
278  } else {
279  echo('<div>RESULT:<pre>'.htmlspecialchars(print_r(chunk_split($result, 1024), true)).'</pre></div>');
280  }
281  echo("</div>");
282  }
283 
284  // If we had a file open for transfer, we close it now.
285  if ($file) {
286  fclose($file);
287  }
288 
289  return $result;
290  }
291 
292  /***********************/
296  protected function _decode_handlers ( $in_handlers
297  ) {
298  $ret = NULL;
299  $plugin_list = [];
300  if (isset($in_handlers->people) && is_array($in_handlers->people) && count($in_handlers->people)) {
301  $plugin_list['people'] = $in_handlers->people;
302  }
303  if (isset($in_handlers->places) && is_array($in_handlers->places) && count($in_handlers->places)) {
304  $plugin_list['places'] = $in_handlers->places;
305  }
306  if (isset($in_handlers->things) && is_array($in_handlers->things) && count($in_handlers->things)) {
307  $plugin_list['things'] = $in_handlers->things;
308  }
309 
310  if (isset($plugin_list) && is_array($plugin_list) && count($plugin_list)) {
311  $ret = [];
312  foreach ($plugin_list as $plugin => $list) {
313  sort($list);
314  foreach ($list as $id) {
315  $id = intval($id);
316 
317  if (1 < $id) {
318  switch ($plugin) {
319  case 'people':
320  $new_object = new RVP_PHP_SDK_User($this, $id);
321  if (isset($new_object) && ($new_object instanceof RVP_PHP_SDK_User)) {
322  $ret[] = $new_object;
323  } else {
324  $this->set_error(_ERR_INTERNAL_ERR__);
325  return NULL;
326  }
327  break;
328 
329  case 'places':
330  $new_object = new RVP_PHP_SDK_Place($this, $id);
331  if (isset($new_object) && ($new_object instanceof RVP_PHP_SDK_Place)) {
332  $ret[] = $new_object;
333  } else {
334  $this->set_error(_ERR_INTERNAL_ERR__);
335  return NULL;
336  }
337  break;
338 
339  case 'things':
340  $new_object = new RVP_PHP_SDK_Thing($this, $id);
341  if (isset($new_object) && ($new_object instanceof RVP_PHP_SDK_Thing)) {
342  $ret[] = $new_object;
343  } else {
344  $this->set_error(_ERR_INTERNAL_ERR__);
345  return NULL;
346  }
347  break;
348  }
349  }
350  }
351  }
352  }
353 
354  return $ret;
355  }
356 
357  /***********************/
363  protected function _load_localizations() {
364  $locale_dir = dirname(__FILE__).'/lang';
365  $locale_name_array = [];
366  foreach (new DirectoryIterator($locale_dir) as $fileInfo) {
367  if (($fileInfo->getExtension() === 'php') && ('index.php' != $fileInfo->getBasename()) && ('common.php' != $fileInfo->getBasename())) {
368  $locale_name_array[] = $fileInfo->getBasename('.php');
369  }
370  }
371 
372  $this->_localizations = [];
373 
374  // Read each available file, and add it to our list.
375  foreach ($locale_name_array as $locale) {
376  if ($locale != $this->_sdk_lang) {
377  $this->_localizations[] = $locale;
378  }
379  }
380 
381  sort($this->_localizations); // Simple alpha-sort.
382  array_unshift($this->_localizations, $this->_sdk_lang); // Make sure the first one is always our default.
383  }
384 
385  /***********************/
389  protected function _get_my_info() {
390  $ret = NULL;
391 
392  if ($this->is_logged_in()) {
393  $info = $this->fetch_data('json/people/logins/my_info');
394  if ($info) {
395  $temp = json_decode($info);
396  if (isset($temp) && isset($temp->people) && isset($temp->people->logins) && isset($temp->people->logins->my_info)) {
397  $login_info = $temp->people->logins->my_info;
398  if (isset($login_info->user_object_id) && (1 < intval($login_info->user_object_id))) {
399  $ret = ['login' => $login_info];
400  $info = $this->fetch_data('json/people/people/my_info');
401  if ($info) {
402  $temp = json_decode($info);
403  if (isset($temp) && isset($temp->people) && isset($temp->people->people) && isset($temp->people->people->my_info)) {
404  $user_info = $temp->people->people->my_info;
405  $ret['user'] = $user_info;
406  } else {
407  $this->set_error(_ERR_COMM_ERR__);
408  }
409  } else {
410  $this->set_error(_ERR_NO_RESULTS__);
411  }
412  } else {
413  $ret = ['login' => $login_info];
414  }
415  }
416  } else {
417  $this->set_error(_ERR_NO_RESULTS__);
418  }
419  }
420 
421  return $ret;
422  }
423 
424  /***********************/
428  protected function _get_plugins() {
429  $ret = NULL;
430 
431  $info = $this->fetch_data('json/baseline');
432  if ($info) {
433  $temp = json_decode($info);
434  if (isset($temp) && isset($temp->baseline) && isset($temp->baseline->plugins) && is_array($temp->baseline->plugins)) {
435  return $temp->baseline->plugins;
436  } else {
437  $this->set_error(_ERR_COMM_ERR__);
438  }
439  } else {
440  $this->set_error(_ERR_NO_RESULTS__);
441  }
442 
443  return $ret;
444  }
445 
446  /***********************/
452  protected function _set_up_login_info() {
453  if ($this->_api_key) {
454  $this->_my_login_info = NULL;
455  $this->_my_user_info = NULL;
456 
457  $info = $this->_get_my_info();
458 
459  if (!$this->get_error()) {
460  if (isset($info['login'])) {
461  $this->_my_login_info = new RVP_PHP_SDK_Login($this, $info['login']->id, $info['login'], true);
462 
463  if (!($this->_my_login_info instanceof RVP_PHP_SDK_Login)) {
464  $this->set_error(_ERR_INTERNAL_ERR__);
465  $this->logout();
466  $this->_api_key = NULL;
467  $this->_login_time_limit = -1;
468  $this->_my_login_info = NULL;
469  $this->_my_user_info = NULL;
470  return false;
471  }
472  }
473 
474  if (isset($info['user'])) {
475  $this->_my_user_info = new RVP_PHP_SDK_User($this, $info['user']->id, $info['user'], true);
476 
477  if (!($this->_my_user_info instanceof RVP_PHP_SDK_User)) {
478  $this->set_error(_ERR_INTERNAL_ERR__);
479  $this->logout();
480  $this->_api_key = NULL;
481  $this->_login_time_limit = -1;
482  $this->_my_login_info = NULL;
483  $this->_my_user_info = NULL;
484  return false;
485  }
486  }
487 
488  return true;
489  }
490 
491  $this->_api_key = NULL;
492  }
493 
494  return false;
495  }
496 
497  /************************************************************************************************************************/
498  /*#################################################### PUBLIC METHODS ##################################################*/
499  /************************************************************************************************************************/
500  /***********************/
504  function __construct( $in_server_uri,
505  $in_server_secret,
506  $in_username = NULL,
507  $in_password = NULL,
508  $in_login_timeout = 0
509  ) {
510  $this->_server_uri = trim($in_server_uri, '/'); // This is the server's base URI.
511  $this->_server_secret = $in_server_secret; // This is the secret that we need to provide with authentication.
512  $this->_api_key = NULL; // If we log in, this will be non-NULL, and will contain the active API key for this instance.
513  $this->_login_time_limit = -1; // No timeout to start.
514  $this->_login_timeout = $in_login_timeout; // Save this for posterity.
515  $this->_my_login_info = NULL; // If we have logged in, we have the info for our login here.
516  $this->_my_user_info = NULL; // If we have logged in, we have the info for our user (if available) here.
517 
518  $this->clear_error(); // Start off clean.
519  $this->set_lang('en'); // Set to default (English). The implementor should call this after instantiation to change.
520 
521  $this->_available_plugins = $this->_get_plugins();
522 
523  if ($this->valid()) {
524  if ($in_username && $in_password && $in_login_timeout) {
525  $this->login($in_username, $in_password, $in_login_timeout);
526  }
527  }
528  }
529 
530  /***********************/
534  function __destruct() {
535  $this->logout(); // Don't bother checking for current login. Just call logout().
536  }
537 
538  /***********************/
542  function set_lang( $in_lang
543  ) {
544  $this->_sdk_lang = $in_lang;
545 
546  $this->_load_localizations();
547  }
548 
549  /***********************/
557  function login( $in_username,
558  $in_password,
559  $in_login_timeout = -1
560  ) {
561  if (!$this->_api_key && $this->valid()) {
562  $this->_login_time_limit = (0 < $in_login_timeout) ? (floatval($in_login_timeout) + microtime(true)) : -1;
563  $api_key = $this->fetch_data('login', 'login_id='.urlencode($in_username).'&password='.urlencode($in_password));
564 
565  if (isset($api_key) && $api_key) { // If we logged in, then we get our info.
566  $this->_api_key = $api_key;
567  return $this->_set_up_login_info();
568  } else {
569  $this->set_error(_ERR_INVALID_LOGIN__);
570  $this->_api_key = NULL;
571  $this->_login_time_limit = -1;
572  $this->_my_login_info = NULL;
573  $this->_my_user_info = NULL;
574  }
575  } else {
576  $this->set_error(_ERR_PREV_LOGIN__);
577  }
578 
579  return false;
580  }
581 
582  /***********************/
588  function logout() {
589  if ($this->is_logged_in()) {
590  $this->_call_REST_API('GET', 'logout'); // We call this directly, because we will not be using a return type.
591 
592  if (205 == intval($this->_last_response_code)) {
593  $this->_api_key = NULL;
594  $this->_login_time_limit = -1;
595  $this->_my_login_info = NULL;
596  $this->_my_user_info = NULL;
597  return true;
598  } else {
599  $this->set_error(_ERR_COMM_ERR__);
600  }
601  } else {
602  $this->set_error(_ERR_NOT_LOGGED_IN__);
603  $this->_api_key = NULL;
604  $this->_login_time_limit = -1;
605  $this->_my_login_info = NULL;
606  $this->_my_user_info = NULL;
607  }
608 
609  return false;
610  }
611 
612  /***********************/
616  function get_error() {
617  $ret = NULL;
618 
619  if ($this->_error) {
620  $message_class = 'RVP_Locale_'.$this->_sdk_lang;
621  require_once(dirname(__FILE__).'/lang/'.$this->_sdk_lang.'.php');
622  $ret = ['code' => intval($this->_error)];
623  $ret['message'] = $message_class::get_error_message($ret['code']);
624  }
625 
626  return $ret;
627  }
628 
629  /***********************/
633  function set_error( $in_code
634  ) {
635  $this->_error = isset($in_code) && (0 != intval($in_code)) ? intval($in_code) : NULL;
636  }
637 
638  /***********************/
642  function clear_error() {
643  $this->set_error(NULL);
644  }
645 
646  /***********************/
650  function valid() {
651  return isset($this->_available_plugins) && is_array($this->_available_plugins) && (3 < count($this->_available_plugins));
652  }
653 
654  /***********************/
657  function force_reload() {
658  return $this->_set_up_login_info();
659  }
660 
661  /***********************/
665  function is_logged_in() {
666  return isset($this->_api_key) && (0 < $this->login_time_left());
667  }
668 
669  /***********************/
673  function is_manager() {
674  if ($this->is_logged_in() && isset($this->_my_login_info)) {
675  return $this->_my_login_info->is_manager();
676  }
677 
678  return false;
679  }
680 
681  /***********************/
685  function is_main_admin() {
686  if ($this->is_manager() && isset($this->_my_login_info)) {
687  return $this->_my_login_info->is_main_admin();
688  }
689 
690  return false;
691  }
692 
693  /***********************/
697  function current_login_id() {
698  if ($this->is_logged_in() && isset($this->_my_login_info)) {
699  return $this->_my_login_info->id();
700  }
701 
702  return NULL;
703  }
704 
705  /***********************/
710  if ($this->is_logged_in() && isset($this->_my_login_info)) {
711  return $this->_my_login_info->login_id();
712  }
713 
714  return NULL;
715  }
716 
717  /***********************/
721  function current_login_object() {
722  $ret = NULL;
723 
724  if ($this->is_logged_in() && isset($this->_my_login_info)) {
725  $ret = new RVP_PHP_SDK_Login($this, $this->_my_login_info->login_id());
726  }
727 
728  return $ret;
729  }
730 
731  /***********************/
737  function login_time_left() {
738  if (0 < $this->_login_time_limit) {
739  return floor(100 * max($this->_login_time_limit - microtime(true), 0)) / 100;
740  }
741 
742  return 0;
743  }
744 
745  /***********************/
749  function my_info() {
750  if ($this->is_logged_in() && isset($this->_my_login_info)) {
751  $ret = ['login' => $this->_my_login_info];
752 
753  if (isset($this->_my_user_info)) {
754  $ret['user'] = $this->_my_user_info;
755  }
756 
757  return $ret;
758  } else {
759  return NULL;
760  }
761  }
762 
763  /***********************/
767  function my_tokens() {
768  $ret = NULL;
769  if ($this->is_logged_in() && isset($this->_my_login_info)) {
770  $ret = $this->_my_login_info->security_tokens();
771  }
772 
773  return $ret;
774  }
775 
776  /***********************/
780  function plugins() {
781  $ret = [];
782 
783  if ($this->valid()) {
784  $ret = $this->_available_plugins;
785  }
786 
787  return $ret;
788  }
789 
790  /***********************/
798  function change_my_password_to( $in_new_password
799  ) {
800  $ret = NULL;
801 
802  if ($this->is_logged_in() && !$this->is_main_admin()) {
803  $my_login_id = $this->_my_login_info->login_id();
804  $result = $this->put_data('json/people/logins/my_info', 'password='.urlencode($in_new_password));
805 
806  if (isset($result)) {
807  $result = json_decode($result);
808  // The reason for this crazy Fabergé egg, is because it's easier to debug a nested set of comparisons.
809  if (isset($result)) {
810  if (isset($result->people)) {
811  if (isset($result->people->logins)) {
812  if (isset($result->people->logins->changed_logins)) {
813  if (is_array($result->people->logins->changed_logins)) {
814  if ((1 == count($result->people->logins->changed_logins))) {
815  if (isset($result->people->logins->changed_logins[0])) {
816  if (isset($result->people->logins->changed_logins[0]->after)) {
817  if (isset($result->people->logins->changed_logins[0]->after->password)) {
818  if (($in_new_password == $result->people->logins->changed_logins[0]->after->password)) {
819  $this->_api_key = NULL;
820  $ret = $this->login($my_login_id, $in_new_password, $this->_login_timeout);
821  }
822  }
823  }
824  }
825  }
826  }
827  }
828  }
829  }
830  }
831  }
832  }
833 
834  return $ret;
835  }
836 
837  /***********************/
843  function fetch_data( $in_plugin_path,
844  $in_query_args = NULL
845  ) {
846  if (isset($in_query_args) && trim($in_query_args)) {
847  $in_plugin_path .= '?'.ltrim($in_query_args, '&');
848  }
849 
850  $response = $this->_call_REST_API('GET', $in_plugin_path);
851 
852  return $response;
853  }
854 
855  /***********************/
862  function put_data( $in_plugin_path,
863  $in_query_args,
864  $in_data_object = NULL
865  ) {
866  $response = NULL;
867 
868  if ($this->is_logged_in() && isset($in_plugin_path) && trim($in_plugin_path) && isset($in_query_args) && trim($in_query_args)) {
869  $in_plugin_path .= '?'.ltrim($in_query_args, '&');
870  $response = $this->_call_REST_API('PUT', $in_plugin_path, $in_data_object);
871  } elseif ($this->is_logged_in()) {
872  $this->set_error(_ERR_NOT_AUTHORIZED__);
873  } else {
874  $this->set_error(_ERR_INVALID_PARAMETERS__);
875  }
876 
877  return $response;
878  }
879 
880  /***********************/
888  function post_data( $in_plugin_path,
889  $in_query_args = NULL,
890  $in_data_object = NULL
891  ) {
892  $response = NULL;
893 
894  if ($this->is_logged_in() && isset($in_plugin_path) && trim($in_plugin_path)) {
895  if (isset($in_query_args) && trim($in_query_args)) {
896  $in_plugin_path .= '?'.ltrim($in_query_args, '&');
897  }
898 
899  $response = $this->_call_REST_API('POST', $in_plugin_path, $in_data_object);
900  } elseif ($this->is_logged_in()) {
901  $this->set_error(_ERR_NOT_AUTHORIZED__);
902  } else {
903  $this->set_error(_ERR_INVALID_PARAMETERS__);
904  }
905 
906  return $response;
907  }
908 
909  /***********************/
916  function delete_data( $in_plugin_path
917  ) {
918  $response = NULL;
919 
920  if ($this->is_logged_in() && isset($in_plugin_path) && trim($in_plugin_path)) {
921  $response = $this->_call_REST_API('DELETE', $in_plugin_path);
922  } elseif ($this->is_logged_in()) {
923  $this->set_error(_ERR_NOT_AUTHORIZED__);
924  } else {
925  $this->set_error(_ERR_INVALID_PARAMETERS__);
926  }
927 
928  return $response;
929  }
930 
931  /***********************/
939  function get_objects() {
940  $func_args = func_get_args();
941 
942  // If they passed an array as the only argument, then we switch to that.
943  if (is_array($func_args) && (1 == count($func_args)) && is_array($func_args[0]) && count($func_args[0])) {
944  $func_args = $func_args[0];
945  }
946 
947  $ret = [];
948 
949  $args = array_map('intval', $func_args);
950  $arg_array = array_chunk($args, 10); // Split into groups of 10, so we don't create too large a GET request.
951 
952  foreach($arg_array as $args) {
953  $handlers = $this->fetch_data('json/baseline/handlers/'.implode(',', $args));
954  if (isset($handlers)) {
955  $handlers = json_decode($handlers);
956  if (isset($handlers) && isset($handlers->baseline)) {
957  $results = $this->_decode_handlers($handlers->baseline);
958 
959  if (isset($results) && is_array($results) && count($results)) {
960  $ret = array_merge($ret, $results);
961  }
962  }
963  } else {
964  $this->set_error(_ERR_COMM_ERR__);
965  return NULL;
966  }
967  }
968 
969  if (isset($ret) && is_array($ret) && (1 < count($ret))) {
970  usort($ret, function($a, $b) {
971  if ($a->id() == $b->id()) {
972  return 0;
973  }
974 
975  if ($a->id() < $b->id()) {
976  return -1;
977  }
978 
979  return 1;
980  }
981  );
982  }
983 
984  return $ret;
985  }
986 
987  /***********************/
991  function get_user_info( $in_user_id
992  ) {
993  $ret = NULL;
994 
995  if ($this->is_logged_in()) {
996  $info = $this->fetch_data('json/people/people/'.intval($in_user_id), 'show_details');
997  if ($info) {
998  $temp = json_decode($info);
999  if (isset($temp) && isset($temp->people) && isset($temp->people->people) && isset($temp->people->people[0])) {
1000  $ret = new RVP_PHP_SDK_User($this, $temp->people->people[0]->id, $temp->people->people[0], true);
1001  if (!isset($ret) || !($ret instanceof RVP_PHP_SDK_User)) {
1002  $this->set_error(_ERR_INTERNAL_ERR__);
1003  $ret = NULL;
1004  }
1005  }
1006  } else {
1007  $this->set_error(_ERR_COMM_ERR__);
1008  }
1009  }
1010 
1011  return $ret;
1012  }
1013 
1014  /***********************/
1018  function get_login_info( $in_login_id
1019  ) {
1020  $ret = NULL;
1021 
1022  if ($this->is_logged_in()) {
1023  $info = $this->fetch_data('json/people/logins/'.intval($in_login_id), 'show_details');
1024  if ($info) {
1025  $temp = json_decode($info);
1026  if (isset($temp) && isset($temp->people) && isset($temp->people->logins) && isset($temp->people->logins[0])) {
1027  $ret = new RVP_PHP_SDK_Login($this, $temp->people->logins[0]->id, $temp->people->logins[0], true);
1028  if (!isset($ret) || !($ret instanceof RVP_PHP_SDK_Login)) {
1029  $this->set_error(_ERR_INTERNAL_ERR__);
1030  $ret = NULL;
1031  }
1032  }
1033  } else {
1034  $this->set_error(_ERR_COMM_ERR__);
1035  }
1036  }
1037 
1038  return $ret;
1039  }
1040 
1041  /***********************/
1045  function get_place_info( $in_place_id
1046  ) {
1047  $ret = NULL;
1048 
1049  $info = $this->fetch_data('json/places/'.intval($in_place_id), 'show_details');
1050  if ($info) {
1051  $temp = json_decode($info);
1052  if (isset($temp) && isset($temp->places) && isset($temp->places->results) && is_array($temp->places->results) && isset($temp->places->results[0])) {
1053  $ret = new RVP_PHP_SDK_Place($this, $temp->places->results[0]->id, $temp->places->results[0], true);
1054  if (!isset($ret) || !($ret instanceof RVP_PHP_SDK_Place)) {
1055  $this->set_error(_ERR_INTERNAL_ERR__);
1056  $ret = NULL;
1057  }
1058  }
1059  } else {
1060  $this->set_error(_ERR_COMM_ERR__);
1061  }
1062 
1063  return $ret;
1064  }
1065 
1066  /***********************/
1070  function get_thing_info( $in_thing_id
1071  ) {
1072  $ret = NULL;
1073 
1074  $info = $this->fetch_data('json/things/'.urlencode($in_thing_id), 'show_details');
1075  if ($info) {
1076  $temp = json_decode($info);
1077  if (isset($temp) && isset($temp->things) && isset($temp->things[0])) {
1078  $ret = new RVP_PHP_SDK_Thing($this, $temp->things[0]->id, $temp->things[0], true);
1079  if (!isset($ret) || !($ret instanceof RVP_PHP_SDK_Thing)) {
1080  $this->set_error(_ERR_INTERNAL_ERR__);
1081  $ret = NULL;
1082  }
1083  }
1084  } else {
1085  $this->set_error(_ERR_COMM_ERR__);
1086  }
1087 
1088  return $ret;
1089  }
1090 
1091  /***********************/
1098  function general_search( $in_text_array = [],
1105  $in_location = NULL,
1106  $in_writeable = false
1107  ) {
1108  $ret = [];
1109 
1110  $added_parameters = '';
1111 
1112  if (is_array($in_text_array) && count($in_text_array)) {
1113  foreach ($in_text_array as $key => $value) {
1114  $added_parameters .= urlencode(self::_get_tag_match($key)).'='.urlencode($value);
1115  }
1116  }
1117 
1118  if ($in_writeable && $this->is_logged_in()) { // We ignore writeable if we are not logged in.
1119  $added_parameters .= '&writeable';
1120  }
1121 
1122  if (NULL !== $in_location) {
1123  $added_parameters .= '&search_latitude='.floatval($in_location['latitude']).'&search_longitude='.floatval($in_location['longitude']).'&search_radius='.floatval($in_location['radius']);
1124  }
1125 
1126  $handlers = $this->fetch_data('json/baseline/search/', $added_parameters);
1127  if (isset($handlers)) {
1128  $handlers = json_decode($handlers);
1129  if (isset($handlers) && isset($handlers->baseline)) {
1130  $ret = $this->_decode_handlers($handlers->baseline);
1131 
1132  if (isset($ret) && is_array($ret) && (0 < count($ret))) {
1133  usort($ret, function($a, $b) {
1134  if ($a->id() == $b->id()) {
1135  return 0;
1136  }
1137 
1138  if ($a->id() < $b->id()) {
1139  return -1;
1140  }
1141 
1142  return 1;
1143  }
1144  );
1145  }
1146  }
1147  } else {
1148  $this->set_error(_ERR_COMM_ERR__);
1149  return [];
1150  }
1151 
1152  return $ret;
1153  }
1154 
1155  /***********************/
1163  function people_search( $in_text_array = [],
1178  $in_location = NULL,
1179  $in_get_logins_only = false,
1180  $in_writeable = false
1181  ) {
1182  $ret = [];
1183 
1184  $added_parameters = '';
1185 
1186  if (is_array($in_text_array) && count($in_text_array)) {
1187  foreach ($in_text_array as $key => $value) {
1188  $added_parameters .= urlencode(self::_get_tag_match($key)).'='.urlencode($value);
1189  }
1190  }
1191 
1192  if ($in_get_logins_only) {
1193  $added_parameters .= '&login_user';
1194  }
1195 
1196  if ($in_writeable && $this->is_logged_in()) { // We ignore writeable if we are not logged in.
1197  $added_parameters .= '&writeable';
1198  }
1199 
1200  if (NULL !== $in_location) {
1201  $added_parameters .= '&search_latitude='.floatval($in_location['latitude']).'&search_longitude='.floatval($in_location['longitude']).'&search_radius='.floatval($in_location['radius']);
1202  }
1203 
1204  $response = $this->fetch_data('json/people/people/', $added_parameters);
1205  if (isset($response)) {
1206  $response = json_decode($response);
1207  if (isset($response) && isset($response->people) && isset($response->people->people)) {
1208  $ret = [];
1209  $people = (array)$response->people->people;
1210  foreach ($people as $person) {
1211  if (isset($person->id)) {
1212  if ($in_get_logins_only && isset($person->associated_login)) {
1213  $new_object = new RVP_PHP_SDK_Login($this, $person->associated_login->id, $person->associated_login, true);
1214  if (isset($new_object) && ($new_object instanceof RVP_PHP_SDK_Login)) {
1215  $ret[] = $new_object;
1216  } else {
1217  $this->set_error(_ERR_INTERNAL_ERR__);
1218  return [];
1219  }
1220  } elseif (!$in_get_logins_only) {
1221  $new_object = new RVP_PHP_SDK_User($this, $person->id, $person);
1222  if (isset($new_object) && ($new_object instanceof RVP_PHP_SDK_User)) {
1223  $ret[] = $new_object;
1224  } else {
1225  $this->set_error(_ERR_INTERNAL_ERR__);
1226  return [];
1227  }
1228  }
1229  }
1230  }
1231 
1232  if (isset($ret) && is_array($ret) && (0 < count($ret))) {
1233  usort($ret, function($a, $b) {
1234  if ($a->id() == $b->id()) {
1235  return 0;
1236  }
1237 
1238  if ($a->id() < $b->id()) {
1239  return -1;
1240  }
1241 
1242  return 1;
1243  }
1244  );
1245  }
1246  }
1247  } else {
1248  $this->set_error(_ERR_COMM_ERR__);
1249  return [];
1250  }
1251 
1252  return $ret;
1253  }
1254 
1255  /***********************/
1263  function places_search( $in_text_array = [],
1279  $in_location = NULL,
1280  $in_writeable = false
1281  ) {
1282  $ret = [];
1283 
1284  $added_parameters = '';
1285 
1286  if (is_array($in_text_array) && count($in_text_array)) {
1287  foreach ($in_text_array as $key => $value) {
1288  $added_parameters .= urlencode(self::_get_tag_match($key)).'='.urlencode($value);
1289  }
1290  }
1291 
1292  if ($in_writeable && $this->is_logged_in()) { // We ignore writeable if we are not logged in.
1293  $added_parameters .= '&writeable';
1294  }
1295 
1296  if (NULL !== $in_location) {
1297  $added_parameters .= '&search_latitude='.floatval($in_location['latitude']).'&search_longitude='.floatval($in_location['longitude']).'&search_radius='.floatval($in_location['radius']);
1298  }
1299 
1300  $response = $this->fetch_data('json/places/', $added_parameters);
1301  if (isset($response)) {
1302  $response = json_decode($response);
1303  if (isset($response) && isset($response->places) && isset($response->places->results) && is_array($response->places->results) && count($response->places->results)) {
1304  $ret = [];
1305  foreach ($response->places->results as $place) {
1306  $new_object = new RVP_PHP_SDK_Place($this, $place->id, $place);
1307  if (isset($new_object) && ($new_object instanceof RVP_PHP_SDK_Place)) {
1308  $ret[] = $new_object;
1309  } else {
1310  $this->set_error(_ERR_INTERNAL_ERR__);
1311  return [];
1312  }
1313  }
1314 
1315  if (isset($ret) && is_array($ret) && (0 < count($ret))) {
1316  usort($ret, function($a, $b) {
1317  if ($a->id() == $b->id()) {
1318  return 0;
1319  }
1320 
1321  if ($a->id() < $b->id()) {
1322  return -1;
1323  }
1324 
1325  return 1;
1326  }
1327  );
1328  }
1329  }
1330  } else {
1331  $this->set_error(_ERR_COMM_ERR__);
1332  return [];
1333  }
1334 
1335  return $ret;
1336  }
1337 
1338  /***********************/
1346  function things_search( $in_text_array = [],
1354  $in_location = NULL,
1355  $in_writeable = false
1356  ) {
1357  $ret = [];
1358 
1359  $added_parameters = '';
1360 
1361  if (is_array($in_text_array) && count($in_text_array)) {
1362  foreach ($in_text_array as $key => $value) {
1363  $added_parameters .= urlencode(self::_get_tag_match($key)).'='.urlencode($value);
1364  }
1365  }
1366 
1367  if ($in_writeable && $this->is_logged_in()) { // We ignore writeable if we are not logged in.
1368  $added_parameters .= '&writeable';
1369  }
1370 
1371  if (NULL !== $in_location) {
1372  $added_parameters .= '&search_latitude='.floatval($in_location['latitude']).'&search_longitude='.floatval($in_location['longitude']).'&search_radius='.floatval($in_location['radius']);
1373  }
1374 
1375  $response = $this->fetch_data('json/things/', $added_parameters);
1376  if (isset($response)) {
1377  $response = json_decode($response);
1378  if (isset($response) && isset($response->things) && is_array($response->things) && count($response->things)) {
1379  $ret = [];
1380  foreach ($response->things as $thing) {
1381  $new_object = new RVP_PHP_SDK_Thing($this, $thing->id, $thing);
1382  if (isset($new_object) && ($new_object instanceof RVP_PHP_SDK_Thing)) {
1383  $ret[] = $new_object;
1384  } else {
1385  $this->set_error(_ERR_INTERNAL_ERR__);
1386  return [];
1387  }
1388  }
1389 
1390  if (isset($ret) && is_array($ret) && (0 < count($ret))) {
1391  usort($ret, function($a, $b) {
1392  if ($a->id() == $b->id()) {
1393  return 0;
1394  }
1395 
1396  if ($a->id() < $b->id()) {
1397  return -1;
1398  }
1399 
1400  return 1;
1401  }
1402  );
1403  }
1404  }
1405  } else {
1406  $this->set_error(_ERR_COMM_ERR__);
1407  return [];
1408  }
1409 
1410  return $ret;
1411  }
1412 
1413  /***********************/
1417  function general_location_search( $in_location
1418  ) {
1419  return $this->general_search(NULL, $in_location);
1420  }
1421 
1422  /***********************/
1426  function people_location_search( $in_location,
1427  $in_get_logins_only = false
1428  ) {
1429  return $this->people_search(NULL, $in_location, $in_get_logins_only);
1430  }
1431 
1432  /***********************/
1436  function place_location_search( $in_location
1437  ) {
1438  return $this->places_search(NULL, $in_location);
1439  }
1440 
1441  /***********************/
1445  function thing_location_search( $in_location
1446  ) {
1447  return $this->things_search(NULL, $in_location);
1448  }
1449 
1450  /***********************/
1461  function auto_radius_search( $in_center_point,
1462  $in_target_number = 10,
1463  $in_search_type = 'all',
1470  $in_search_string_criteria = NULL,
1471  $in_step_size_in_km = 0.5,
1472  $in_max_width_in_km = 100,
1473  $step_callback = NULL
1487  ) {
1488  $radius = 0.0;
1489  $results = [];
1490  $location = ['latitude' => floatval($in_center_point['latitude']), 'longitude' => floatval($in_center_point['longitude']), 'radius' => $radius];
1491 
1492  while (($in_target_number > count($results)) && ($in_max_width_in_km >= ($radius + $in_step_size_in_km))) {
1493  $radius += floatval($in_step_size_in_km);
1494  $location['radius'] = $radius;
1495  switch (strtolower(trim($in_search_type))) {
1496  case 'people':
1497  case 'users':
1498  $in_search_type = 'users';
1499  case 'logins':
1500  $results = $this->people_search($in_search_string_criteria, $location, ('logins' == $in_search_type));
1501  break;
1502 
1503  case 'places':
1504  $results = $this->places_search($in_search_string_criteria, $location);
1505  break;
1506 
1507  case 'things':
1508  $results = $this->things_search($in_search_string_criteria, $location);
1509  break;
1510 
1511  default:
1512  $in_search_type = 'all';
1513  $results = $this->general_search($in_search_string_criteria, $location);
1514  break;
1515  }
1516 
1517  // The user can provide a callback that gets a report, and can abort the search.
1518  if (isset($step_callback)) {
1519  $abort = false;
1520 
1521  if (is_array($step_callback) && (2 == count($step_callback))) {
1522  $object = $step_callback[0];
1523  $method = $step_callback[1];
1524  if (method_exists($object, $method)) {
1525  $abort = $object->$method($this, $results, strtolower(trim($in_search_type)), $in_target_number, $in_step_size_in_km, $in_max_width_in_km, $location, $in_search_string_criteria);
1526  }
1527  } elseif (function_exists($step_callback)) {
1528  $abort = $step_callback($this, $results, strtolower(trim($in_search_type)), $in_target_number, $in_step_size_in_km, $in_max_width_in_km, $location, $in_search_string_criteria);
1529  }
1530 
1531  if ($abort) {
1532  break;
1533  }
1534  }
1535  }
1536 
1537  return $results;
1538  }
1539 
1540  /***********************/
1554  function bulk_upload( $in_csv_data
1555  ) {
1556  if ($this->is_main_admin()) {
1557  return json_decode($this->post_data('json/baseline/bulk-loader', NULL, ['data' => $in_csv_data, 'type' => 'text/csv']));
1558  } else {
1559  $this->set_error(_ERR_NOT_AUTHORIZED__);
1560  return NULL;
1561  }
1562  }
1563 
1564  /***********************/
1570  function backup() {
1571  if ($this->is_main_admin()) {
1572  return $this->fetch_data('csv/baseline/backup');
1573  } else {
1574  $this->set_error(_ERR_NOT_AUTHORIZED__);
1575  return NULL;
1576  }
1577  }
1578 
1579  /***********************/
1585  function get_serverinfo() {
1586  if ($this->is_main_admin()) {
1587  $result = json_decode($this->fetch_data('json/baseline/serverinfo'));
1588  if (isset($result->baseline) && isset($result->baseline->serverinfo)) {
1589  return $result->baseline->serverinfo;
1590  }
1591  } else {
1592  $this->set_error(_ERR_NOT_AUTHORIZED__);
1593  }
1594 
1595  return NULL;
1596  }
1597 
1598  /***********************/
1607  function test_visibility( $in_id,
1608  $in_is_token = false
1609  ) {
1610  $ret = NULL;
1611 
1612  $in_id = intval($in_id); // Make sure that we're an integer.
1613 
1614  // We have our basic standards.
1615  if ((1 < $in_id) || ($in_is_token && (0 <= $in_id))) {
1616  $uri = 'json/baseline/visibility/'. ($in_is_token ? 'token/' : '');
1617  $uri .= $in_id;
1618  $response = $this->fetch_data($uri);
1619  if (isset($response)) {
1620  $response = json_decode($response);
1621  if (isset($response) && isset($response->baseline)) {
1622  $response = $response->baseline;
1623  if (isset($response->token) && isset($response->token->login_ids) && is_array($response->token->login_ids) && count($response->token->login_ids)) {
1624  $ret = $response->token->login_ids;
1625  } elseif (isset($response->id) && isset($response->id->id)) {
1626  $response = $response->id;
1627  $ret = ['id' => $response->id];
1628  if (isset($response->writeable)) {
1629  $ret['writeable'] = true;
1630  }
1631  if (isset($response->read_login_ids) && is_array($response->read_login_ids) && count($response->read_login_ids)) {
1632  $ret['read_login_ids'] = $response->read_login_ids;
1633  }
1634  if (isset($response->write_login_ids) && is_array($response->write_login_ids) && count($response->write_login_ids)) {
1635  $ret['write_login_ids'] = $response->write_login_ids;
1636  }
1637  }
1638  }
1639  } else {
1640  }
1641  } else {
1642  $this->set_error(_ERR_INVALID_PARAMETERS__);
1643  return NULL;
1644  }
1645 
1646  return $ret;
1647  }
1648 
1649  /***********************/
1657  function new_user( $in_user_name,
1658  $in_tokens,
1663  $in_login_id = NULL
1664  ) {
1665  $ret = NULL;
1666  $need_a_write_in = false; // This will be true, if we need to force a write token.
1667  $login_id = (isset($in_login_id) && trim($in_login_id)) ? trim($in_login_id) : NULL; // See if we have a login ID.
1668 
1669  if ($this->is_manager()) { // Must be a manager.
1670  if ($login_id) { // some prerequisites.
1671  // We test to see if the login they want is already in use.
1672  $uri = 'json/people/logins/'.$login_id.'?test';
1673  $response = $this->fetch_data($uri);
1674  if (isset($response)) {
1675  $response = json_decode($response);
1676  if (isset($response) && isset($response->people) && isset($response->people->logins) && isset($response->people->logins->login_exists) && $response->people->logins->login_exists) {
1677  $this->set_error(_ERR_INVALID_LOGIN_ID__);
1678  return NULL;
1679  } else {
1680  if (!isset($in_tokens) || !is_array($in_tokens) || !isset($in_tokens['write']) || !intval($in_tokens['write'])) { // If a valid write token was not supplied, we'll need to add one.
1681  $need_a_write_in = true;
1682  }
1683  }
1684  } else {
1685  $this->set_error(_ERR_COMM_ERR__);
1686  return NULL;
1687  }
1688  } else { // If we are not creating a login, then we are required to have a valid write token.
1689  if (!isset($in_tokens) || !isset($in_tokens['write']) || (1 > intval($in_tokens['write'])) || !in_array(intval($in_tokens['write']), $this->my_tokens())) {
1690  $this->set_error(_ERR_INVALID_PARAMETERS__);
1691  return NULL;
1692  }
1693  }
1694 
1695  } else {
1696  $this->set_error(_ERR_NOT_AUTHORIZED__);
1697  return NULL;
1698  }
1699 
1700  // If we got here, then we're clear, so far.
1701  $uri = 'json/people/people';
1702  $params = '';
1703  if ($login_id) {
1704  $params .= '&login_id='.urlencode($login_id);
1705  }
1706 
1707  if (isset($in_tokens) && is_array($in_tokens) && count($in_tokens) && isset($in_tokens['read'])) {
1708  $params .= '&read_token='.intval($in_tokens['read']);
1709  }
1710 
1711  if (isset($in_tokens) && is_array($in_tokens) && count($in_tokens) && isset($in_tokens['write']) && (0 < intval($in_tokens['write']))) {
1712  $params .= '&write_token='.intval($in_tokens['write']);
1713  }
1714 
1715  if (isset($in_tokens) && is_array($in_tokens) && count($in_tokens) && isset($in_tokens['tokens'])) {
1716  $params .= '&tokens='.implode(',', array_map('intval', $in_tokens['tokens']));
1717  }
1718 
1719  $uri .= '/?'.trim($params, "\&");
1720 
1721  $response = $this->post_data($uri);
1722 
1723  if (isset($response)) {
1724  $response = json_decode($response);
1725 
1726  if (isset($response) && isset($response->people) && isset($response->people->people) && isset($response->people->people->new_user)) {
1727  $ret = [];
1728 
1729  if (isset($response->people->people->new_user->associated_login)) {
1730  $ret['login_id'] = $response->people->people->new_user->associated_login->login_id;
1731  $ret['password'] = $response->people->people->new_user->associated_login->password;
1732  unset($response->people->people->new_user->associated_login->password);
1733  $id = $response->people->people->new_user->associated_login->id;
1734  $response->people->people->new_user->associated_login_id = $id;
1735  $ret['login'] = new RVP_PHP_SDK_Login($this, $id, $response->people->people->new_user->associated_login, true);
1736  unset($response->people->people->new_user->associated_login);
1737  }
1738 
1739  $ret['user'] = new RVP_PHP_SDK_User($this, $response->people->people->new_user->id, $response->people->people->new_user, true);
1740  }
1741  }
1742 
1743  return $ret;
1744  }
1745 
1746  /***********************/
1756  function new_place( $in_place_name,
1757  $in_tokens = [],
1761  $in_latitude = NULL,
1762  $in_longitude = NULL,
1763  $in_fuzz_factor = NULL
1764  ) {
1765  $ret = NULL;
1766 
1767  if ($this->is_logged_in()) { // Must be logged in.
1768  $uri = 'json/places';
1769  $params = '';
1770  if ($in_place_name) {
1771  $params .= '&name='.urlencode($in_place_name);
1772  }
1773 
1774  if ($in_latitude) {
1775  $params .= '&latitude='.floatval($in_latitude);
1776  }
1777 
1778  if ($in_longitude) {
1779  $params .= '&longitude='.floatval($in_longitude);
1780  }
1781 
1782  if ($in_fuzz_factor) {
1783  $params .= '&fuzz_factor='.floatval($in_fuzz_factor);
1784  }
1785 
1786  if (isset($in_tokens) && is_array($in_tokens) && count($in_tokens) && isset($in_tokens['read'])) {
1787  $params .= '&read_token='.intval($in_tokens['read']);
1788  }
1789 
1790  if (isset($in_tokens) && is_array($in_tokens) && count($in_tokens) && isset($in_tokens['write']) && (0 < intval($in_tokens['write']))) {
1791  $params .= '&write_token='.intval($in_tokens['write']);
1792  }
1793 
1794  $params = trim($params, "\&");
1795 
1796  $response = $this->post_data($uri, $params);
1797 
1798  if (isset($response)) {
1799  $response = json_decode($response);
1800 
1801  if (isset($response) && isset($response->places) && isset($response->places->new_place)) {
1802  $response = $response->places->new_place;
1803 
1804  $ret = new RVP_PHP_SDK_Place($this, $response->id, $response, true);
1805  }
1806  }
1807  } else {
1808  $this->set_error(_ERR_NOT_AUTHORIZED__);
1809  return NULL;
1810  }
1811 
1812  return $ret;
1813  }
1814 
1815  /***********************/
1822  function new_thing( $in_thing_key,
1823  $in_thing_value,
1824  $in_tokens = [],
1828  $in_thing_name = NULL,
1829  $in_thing_description = NULL
1830  ) {
1831  $ret = NULL;
1832 
1833  if ($this->is_logged_in() && isset($in_thing_key) && $in_thing_key && isset($in_thing_value) && $in_thing_value) { // Must be logged in, and we must have the required parameters.
1834  $uri = 'json/things';
1835  $params = '';
1836 
1837  $in_thing_key = trim($in_thing_key);
1838 
1839  if (false !== strpos($in_thing_key, ',')) { // Cannot have commas in the key.
1840  $in_thing_key = NULL;
1841  }
1842 
1843  if ($in_thing_key) {
1844  $params .= '&key='.urlencode($in_thing_key);
1845  }
1846 
1847  if (isset($in_tokens) && is_array($in_tokens) && count($in_tokens) && isset($in_tokens['read'])) {
1848  $params .= '&read_token='.intval($in_tokens['read']);
1849  }
1850 
1851  if (isset($in_tokens) && is_array($in_tokens) && count($in_tokens) && isset($in_tokens['write']) && (0 < intval($in_tokens['write']))) {
1852  $params .= '&write_token='.intval($in_tokens['write']);
1853  }
1854 
1855  if ($in_thing_name) {
1856  $params .= '&name='.urlencode($in_thing_name);
1857  }
1858 
1859  if ($in_thing_description) {
1860  $params .= '&description='.urlencode($in_thing_description);
1861  }
1862 
1863  $params = trim($params, "\&");
1864 
1865  $temp_file = tempnam(sys_get_temp_dir(), 'RVP');
1866  file_put_contents($temp_file , $in_thing_value);
1867  $finfo = finfo_open(FILEINFO_MIME_TYPE);
1868  $content_type = finfo_file($finfo, $temp_file);
1869  unlink($temp_file);
1870  $payload = ['data' => $in_thing_value, 'type' => $content_type];
1871 
1872  $response = $this->post_data($uri, $params, $payload);
1873 
1874  if (isset($response)) {
1875  $response = json_decode($response);
1876 
1877  if (isset($response) && isset($response->things) && isset($response->things->new_thing)) {
1878  $response = $response->things->new_thing;
1879 
1880  $ret = new RVP_PHP_SDK_Thing($this, $response->id, $response, true);
1881  }
1882  }
1883  } else {
1884  // If they are logged in, then they are authorized, but they don't have required parameters.
1885  $this->set_error($this->is_logged_in() ? _ERR_INVALID_PARAMETERS__ : _ERR_NOT_AUTHORIZED__);
1886  return NULL;
1887  }
1888 
1889  return $ret;
1890  }
1891 
1892  /***********************/
1899  function delete_user( $in_object
1900  ) {
1901  $ret = false;
1902 
1903  if ($this->is_manager() && isset($in_object)) { // Must be at least a manager.
1904  $user_id = 0;
1905  $login_id = 0;
1906 
1907  if ($in_object instanceof RVP_PHP_SDK_User) {
1908  if ($in_object->writeable()) { // Have to be able to edit.
1909  $user_id = $in_object->id();
1910  }
1911  } elseif ($in_object instanceof RVP_PHP_SDK_Login) {
1912  if ($in_object->writeable()) { // Have to be able to edit.
1913  $login_id = $in_object->id();
1914  }
1915  } else {
1916  $user_id = intval($in_object);
1917  }
1918 
1919  if ($user_id || $login_id) { // We need to have something to proceed.
1920  $uri = 'json/people';
1921  $ret = true;
1922 
1923  if ($user_id) {
1924  $response = $this->delete_data($uri.'/people/'.$user_id);
1925 
1926  if (isset($response)) {
1927  $response = json_decode($response);
1928 
1929  if (!(isset($response) && isset($response->people->people) && isset($response->people->people->deleted_users) && is_array($response->people->people->deleted_users) && (1 == count($response->people->people->deleted_users)) && ($user_id == $response->people->people->deleted_users[0]->id))) {
1930  $this->set_error(_ERR_COMM_ERR__);
1931  $ret = false;
1932  } elseif (isset($response->people->people->deleted_users[0]->associated_login_id)) {
1933  $login_id = intval($response->people->people->deleted_users[0]->associated_login_id);
1934  } elseif (isset($response->people->people->deleted_users[0]->associated_login)) {
1935  $login_id = intval($response->people->people->deleted_users[0]->associated_login->id);
1936  }
1937  } else {
1938  $this->set_error(_ERR_COMM_ERR__);
1939  $ret = false;
1940  }
1941  }
1942 
1943  if ($ret && $login_id) {
1944  $response = $this->delete_data($uri.'/logins/'.$login_id);
1945 
1946  if (isset($response)) {
1947  $response = json_decode($response);
1948 
1949  if (!(isset($response) && isset($response->people->logins) && isset($response->people->logins->deleted_logins) && is_array($response->people->logins->deleted_logins) && (1 == count($response->people->logins->deleted_logins)) && ($login_id == $response->people->logins->deleted_logins[0]->id))) {
1950  $this->set_error(_ERR_COMM_ERR__);
1951  $ret = false;
1952  }
1953  } else {
1954  $this->set_error(_ERR_COMM_ERR__);
1955  $ret = false;
1956  }
1957  }
1958  }
1959  } else {
1960  // If they are logged in as a manager, then they are authorized, but they don't have required parameters.
1961  $this->set_error($this->is_manager() ? _ERR_INVALID_PARAMETERS__ : _ERR_NOT_AUTHORIZED__);
1962  }
1963 
1964  return $ret;
1965  }
1966 
1967  /***********************/
1973  function delete_place( $in_object
1974  ) {
1975  $ret = false;
1976 
1977  if ($this->is_logged_in() && isset($in_object)) { // Must be logged in.
1978  $place_id = 0;
1979 
1980  if ($in_object instanceof RVP_PHP_SDK_Place) {
1981  $place_id = $in_object->id();
1982  } else {
1983  $place_id = intval($in_object);
1984  }
1985 
1986  if ($place_id) { // Assuming that we have an ID...
1987  if (isset($this->test_visibility($place_id)['writeable'])) { // We must be able to write.
1988  $uri = 'json/places/'.$place_id;
1989  $ret = true;
1990  $response = $this->delete_data($uri);
1991 
1992  if (isset($response)) {
1993  $response = json_decode($response);
1994 
1995  if (!(isset($response) && isset($response->places) && isset($response->places->deleted_places) && is_array($response->places->deleted_places) && (1 == count($response->places->deleted_places)) && ($place_id == $response->places->deleted_places[0]->id))) {
1996  $this->set_error(_ERR_COMM_ERR__);
1997  $ret = false;
1998  }
1999  }
2000  } else {
2001  $this->set_error(_ERR_NOT_AUTHORIZED__);
2002  }
2003  }
2004  } else {
2005  // If they are logged in, then they are authorized, but they don't have required parameters.
2006  $this->set_error($this->is_logged_in() ? _ERR_INVALID_PARAMETERS__ : _ERR_NOT_AUTHORIZED__);
2007  }
2008 
2009  return $ret;
2010  }
2011 
2012  /***********************/
2018  function delete_thing( $in_object
2019  ) {
2020  $ret = false;
2021 
2022  if ($this->is_logged_in() && isset($in_object)) { // Must be logged in.
2023  $thing_id = 0;
2024 
2025  if ($in_object instanceof RVP_PHP_SDK_thing) {
2026  $thing_id = $in_object->id();
2027  } else { // This is how we resolve string keys. Just fetch the damn object, and extract its ID.
2028  $thing = $this->get_thing_info($in_object);
2029  if (isset($thing) && ($thing instanceof RVP_PHP_SDK_thing)) {
2030  $thing_id = intval($thing->id());
2031  }
2032  }
2033 
2034  if ($thing_id) { // Assuming that we have an ID...
2035  if (isset($this->test_visibility($thing_id)['writeable'])) { // We must be able to write.
2036  $uri = 'json/things/'.$thing_id;
2037  $ret = true;
2038  $response = $this->delete_data($uri);
2039 
2040  if (isset($response)) {
2041  $response = json_decode($response);
2042 
2043  if (!(isset($response) && isset($response->things) && isset($response->things->deleted_things) && is_array($response->things->deleted_things) && (1 == count($response->things->deleted_things)))) {
2044  $this->set_error(_ERR_COMM_ERR__);
2045  $ret = false;
2046  }
2047  }
2048  } else {
2049  $this->set_error(_ERR_NOT_AUTHORIZED__);
2050  }
2051  }
2052  } else {
2053  // If they are logged in, then they are authorized, but they don't have required parameters.
2054  $this->set_error($this->is_logged_in() ? _ERR_INVALID_PARAMETERS__ : _ERR_NOT_AUTHORIZED__);
2055  }
2056 
2057  return $ret;
2058  }
2059 };
get_place_info( $in_place_id)
$_server_uri
This is the URI of the BAOBAB server.
static _get_tag_match( $in_string)
fetch_data( $in_plugin_path, $in_query_args=NULL)
get_login_info( $in_login_id)
$_my_login_info
This will contain any login information for the current login (NULL if not logged in)...
thing_location_search( $in_location)
$_login_time_limit
If >0, then this is the maximum time at which the current login is valid.
set_error( $in_code)
new_place( $in_place_name, $in_tokens=[], $in_latitude=NULL, $in_longitude=NULL, $in_fuzz_factor=NULL)
delete_place( $in_object)
people_location_search( $in_location, $in_get_logins_only=false)
$_localizations
An array of string, with the available localizations. The first will always be the default localizati...
static _get_string_match_table()
general_search( $in_text_array=[], $in_location=NULL, $in_writeable=false)
new_user( $in_user_name, $in_tokens, $in_login_id=NULL)
get_user_info( $in_user_id)
$_error
This is supposed to be NULL. However, if we have an error, it will contain an integer code...
$_login_timeout
The actual timeout value that was originally passed in.
auto_radius_search( $in_center_point, $in_target_number=10, $in_search_type='all', $in_search_string_criteria=NULL, $in_step_size_in_km=0.5, $in_max_width_in_km=100, $step_callback=NULL)
new_thing( $in_thing_key, $in_thing_value, $in_tokens=[], $in_thing_name=NULL, $in_thing_description=NULL)
things_search( $in_text_array=[], $in_location=NULL, $in_writeable=false)
$_available_plugins
This will be an array of string, with available plugins on the server.
delete_thing( $in_object)
$_my_user_info
This will contain any login information for the current login (NULL if not logged in)...
test_visibility( $in_id, $in_is_token=false)
place_location_search( $in_location)
login( $in_username, $in_password, $in_login_timeout=-1)
_call_REST_API( $method, $url_extension, $data_input=NULL, $display_log=false)
delete_data( $in_plugin_path)
people_search( $in_text_array=[], $in_location=NULL, $in_get_logins_only=false, $in_writeable=false)
change_my_password_to( $in_new_password)
get_thing_info( $in_thing_id)
_decode_handlers( $in_handlers)
$_api_key
This is the current session API key.
places_search( $in_text_array=[], $in_location=NULL, $in_writeable=false)
bulk_upload( $in_csv_data)
__construct( $in_server_uri, $in_server_secret, $in_username=NULL, $in_password=NULL, $in_login_timeout=0)
post_data( $in_plugin_path, $in_query_args=NULL, $in_data_object=NULL)
$_localized_errors
This will be an associative array, with the loaded RVP_Local_Error_* instances for display of error m...
general_location_search( $in_location)
$_last_response_code
This will contain any response code from the last cURL call.
set_lang( $in_lang)
delete_user( $in_object)
$_server_secret
This is the "server secret" that is specified by the admin of the BAOBAB server.
$_sdk_lang
The language specified for this SDK instance. Default is "en" (English).
put_data( $in_plugin_path, $in_query_args, $in_data_object=NULL)