service.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /*
  3. * See license terms in /license.txt
  4. * @author Eric Marguin <eric.marguin@dokeos.com>
  5. */
  6. require_once '../../inc/global.inc.php';
  7. /**
  8. * Import users into database from a file located on the server.
  9. * Function registered as service.
  10. * @param string The csv (only csv) file containing users tom import
  11. * @param string Security key (as found in configuration file)
  12. * @return string Error message
  13. */
  14. function import_users_from_file($filepath, $security_key)
  15. {
  16. $errors_returned = array(
  17. 0 => 'success',
  18. 1 => 'file import does not exist',
  19. 2 => 'no users to import',
  20. 3 => 'wrong datas in file',
  21. 4 => 'security error'
  22. );
  23. $security = api_get_configuration_value('security_key');
  24. // Check whether this script is launch by server and security key is ok.
  25. if (empty($_SERVER['REMOTE_ADDR']) ||
  26. $_SERVER['REMOTE_ADDR'] != $_SERVER['SERVER_ADDR'] ||
  27. $security_key != $security
  28. ) {
  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);