service.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. global $_configuration;
  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. // Check whether this script is launch by server and security key is ok.
  24. if (empty($_SERVER['REMOTE_ADDR']) || $_SERVER['REMOTE_ADDR'] != $_SERVER['SERVER_ADDR'] || $security_key != $_configuration['security_key']) {
  25. return $errors_returned[4];
  26. }
  27. // Libraries
  28. require_once 'import.lib.php';
  29. // Check is users file exists.
  30. if (!is_file($filepath)) {
  31. return $errors_returned[1];
  32. }
  33. // Get list of users
  34. $users = parse_csv_data($filepath);
  35. if (count($users) == 0) {
  36. return $errors_returned[2];
  37. }
  38. // Check the datas for each user
  39. $errors = validate_data($users);
  40. if (count($errors) > 0) {
  41. return $errors_returned[3];
  42. }
  43. // Apply modifications in database
  44. save_data($users);
  45. return $errors_returned[0]; // Import successfull
  46. }
  47. $server = new soap_server();
  48. $server->register('import_users_from_file');
  49. $http_request = (isset($HTTP_RAW_POST_DATA)?$HTTP_RAW_POST_DATA:'');
  50. $server->service($http_request);