service.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * See license terms in /license.txt.
  4. *
  5. * @author Eric Marguin <eric.marguin@dokeos.com>
  6. */
  7. require_once __DIR__.'/../../inc/global.inc.php';
  8. /**
  9. * Import users into database from a file located on the server.
  10. * Function registered as service.
  11. *
  12. * @param string The csv (only csv) file containing users tom import
  13. * @param string Security key (as found in configuration file)
  14. *
  15. * @return string Error message
  16. */
  17. function import_users_from_file($filepath, $security_key)
  18. {
  19. $errors_returned = [
  20. 0 => 'success',
  21. 1 => 'file import does not exist',
  22. 2 => 'no users to import',
  23. 3 => 'wrong datas in file',
  24. 4 => 'security error',
  25. ];
  26. $key = api_get_configuration_value('security_key');
  27. // Check whether this script is launch by server and security key is ok.
  28. if (empty($_SERVER['REMOTE_ADDR']) || $_SERVER['REMOTE_ADDR'] != $_SERVER['SERVER_ADDR'] || $security_key != $key) {
  29. return $errors_returned[4];
  30. }
  31. // Libraries
  32. require_once 'import.lib.php';
  33. // Check is users file exists.
  34. if (!is_file($filepath)) {
  35. return $errors_returned[1];
  36. }
  37. // Get list of users
  38. $users = parse_csv_data($filepath);
  39. if (count($users) == 0) {
  40. return $errors_returned[2];
  41. }
  42. // Check the datas for each user
  43. $errors = validate_data($users);
  44. if (count($errors) > 0) {
  45. return $errors_returned[3];
  46. }
  47. // Apply modifications in database
  48. save_data($users);
  49. return $errors_returned[0]; // Import successfull
  50. }
  51. $server = new soap_server();
  52. $server->register('import_users_from_file');
  53. $http_request = (isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '');
  54. $server->service($http_request);