service.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * See license terms in /license.txt
  4. * @author Eric Marguin <eric.marguin@dokeos.com>
  5. */
  6. require_once __DIR__.'/../../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. global $_configuration;
  17. $errors_returned = array(
  18. 0 => 'success',
  19. 1 => 'file import does not exist',
  20. 2 => 'no users to import',
  21. 3 => 'wrong datas in file',
  22. 4 => 'security error'
  23. );
  24. // Check whether this script is launch by server and security key is ok.
  25. if (empty($_SERVER['REMOTE_ADDR']) || $_SERVER['REMOTE_ADDR'] != $_SERVER['SERVER_ADDR'] || $security_key != $_configuration['security_key']) {
  26. return $errors_returned[4];
  27. }
  28. // Libraries
  29. require_once 'import.lib.php';
  30. // Check is users file exists.
  31. if (!is_file($filepath)) {
  32. return $errors_returned[1];
  33. }
  34. // Get list of users
  35. $users = parse_csv_data($filepath);
  36. if (count($users) == 0) {
  37. return $errors_returned[2];
  38. }
  39. // Check the datas for each user
  40. $errors = validate_data($users);
  41. if (count($errors) > 0) {
  42. return $errors_returned[3];
  43. }
  44. // Apply modifications in database
  45. save_data($users);
  46. return $errors_returned[0]; // Import successfull
  47. }
  48. $server = new soap_server();
  49. $server->register('import_users_from_file');
  50. $http_request = (isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '');
  51. $server->service($http_request);