service.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. 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 api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  30. require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
  31. require_once api_get_path(LIBRARY_PATH).'classmanager.lib.php';
  32. require_once api_get_path(LIBRARY_PATH).'import.lib.php';
  33. require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
  34. require_once 'import.lib.php';
  35. // Check is users file exists.
  36. if (!is_file($filepath)) {
  37. return $errors_returned[1];
  38. }
  39. // Get list of users
  40. $users = parse_csv_data($filepath);
  41. if (count($users) == 0) {
  42. return $errors_returned[2];
  43. }
  44. // Check the datas for each user
  45. $errors = validate_data($users);
  46. if (count($errors) > 0) {
  47. return $errors_returned[3];
  48. }
  49. // Apply modifications in database
  50. save_data($users);
  51. return $errors_returned[0]; // Import successfull
  52. }
  53. $server = new soap_server();
  54. $server->register('import_users_from_file');
  55. $http_request = (isset($HTTP_RAW_POST_DATA)?$HTTP_RAW_POST_DATA:'');
  56. $server->service($http_request);