service.php 1.8 KB

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