service.php 1.9 KB

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