tcc.soap.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\ExtraField as EntityExtraField;
  4. use Chamilo\UserBundle\Entity\User;
  5. /**
  6. * @package chamilo.webservices
  7. */
  8. require_once '../inc/global.inc.php';
  9. error_reporting(E_COMPILE_ERROR | E_ERROR | E_CORE_ERROR);
  10. $libpath = api_get_path(LIBRARY_PATH);
  11. $debug = true;
  12. define('WS_ERROR_SECRET_KEY', 1);
  13. define('WS_ERROR_NOT_FOUND_RESULT', 2);
  14. define('WS_ERROR_INVALID_INPUT', 3);
  15. define('WS_ERROR_SETTING', 4);
  16. /**
  17. * @param string $code
  18. * @return null|soap_fault
  19. */
  20. function returnError($code)
  21. {
  22. $fault = null;
  23. switch ($code) {
  24. case WS_ERROR_SECRET_KEY:
  25. $fault = new soap_fault('Server', '', 'Secret key is not correct or params are not correctly set');
  26. break;
  27. case WS_ERROR_NOT_FOUND_RESULT:
  28. $fault = new soap_fault('Server', '', 'No result was found for this query');
  29. break;
  30. case WS_ERROR_INVALID_INPUT:
  31. $fault = new soap_fault('Server', '', 'The input variables are invalid o are not correctly set');
  32. break;
  33. case WS_ERROR_SETTING:
  34. $fault = new soap_fault('Server', '', 'Please check the configuration for this webservice');
  35. break;
  36. }
  37. return $fault;
  38. }
  39. /**
  40. * @param array $params
  41. * @return bool
  42. */
  43. function WSHelperVerifyKey($params)
  44. {
  45. global $_configuration, $debug;
  46. if (is_array($params)) {
  47. $secret_key = $params['secret_key'];
  48. } else {
  49. $secret_key = $params;
  50. }
  51. //error_log(print_r($params,1));
  52. $check_ip = false;
  53. $ip_matches = false;
  54. $ip = trim($_SERVER['REMOTE_ADDR']);
  55. // if we are behind a reverse proxy, assume it will send the
  56. // HTTP_X_FORWARDED_FOR header and use this IP instead
  57. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  58. list($ip1) = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  59. $ip = trim($ip1);
  60. }
  61. if ($debug)
  62. error_log("ip: $ip");
  63. // Check if a file that limits access from webservices exists and contains
  64. // the restraining check
  65. if (is_file('webservice-auth-ip.conf.php')) {
  66. include 'webservice-auth-ip.conf.php';
  67. if ($debug)
  68. error_log("webservice-auth-ip.conf.php file included");
  69. if (!empty($ws_auth_ip)) {
  70. $check_ip = true;
  71. $ip_matches = api_check_ip_in_range($ip, $ws_auth_ip);
  72. if ($debug) {
  73. error_log("ip_matches: $ip_matches");
  74. }
  75. }
  76. }
  77. if ($debug) {
  78. error_log("checkip " . intval($check_ip));
  79. }
  80. if ($check_ip) {
  81. $security_key = $_configuration['security_key'];
  82. } else {
  83. $security_key = $ip.$_configuration['security_key'];
  84. //error_log($secret_key.'-'.$security_key);
  85. }
  86. $result = api_is_valid_secret_key($secret_key, $security_key);
  87. if ($debug) {
  88. error_log('WSHelperVerifyKey result: '.intval($result));
  89. }
  90. return $result;
  91. }
  92. // Create the server instance
  93. $server = new soap_server();
  94. $server->soap_defencoding = 'UTF-8';
  95. // Initialize WSDL support
  96. $server->configureWSDL('WSTCC', 'urn:WSTCC');
  97. /* Register WSCreateUserPasswordCrypted function */
  98. // Register the data structures used by the service
  99. // Input params for editing users
  100. $server->wsdl->addComplexType(
  101. 'paramsUpdateTCCUserIdAndGetUser',
  102. 'complexType',
  103. 'struct',
  104. 'all',
  105. '',
  106. array(
  107. 'email' => array('name' => 'email', 'type' => 'xsd:string'),
  108. 'tcc_user_id' => array('name' => 'tcc_user_id', 'type' => 'xsd:string'),
  109. 'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string'),
  110. )
  111. );
  112. $fields = array(
  113. 'Genre' => array('name' => 'Genre', 'type' => 'xsd:string'),
  114. 'Nom' => array('name' => 'Nom', 'type' => 'xsd:string'),
  115. 'Prenom' => array('name' => 'Prenom', 'type' => 'xsd:string'),
  116. 'DateNaissance' => array('name' => 'DateNaissance', 'type' => 'xsd:string'),
  117. 'Langue' => array('name' => 'Langue', 'type' => 'xsd:string'),
  118. 'Nationalite' => array('name' => 'Nationalite', 'type' => 'xsd:string'),
  119. 'Pays' => array('name' => 'Pays', 'type' => 'xsd:string'),
  120. 'Adresse' => array('name' => 'Adresse', 'type' => 'xsd:string'),
  121. 'CodePostal' => array('name' => 'CodePostal', 'type' => 'xsd:string'),
  122. 'Ville' => array('name' => 'Ville', 'type' => 'xsd:string'),
  123. 'Email'=> array('name' => 'Email', 'type' => 'xsd:string')
  124. );
  125. $server->wsdl->addComplexType(
  126. 'resultUpdateTCCUserIdAndGetUserArray',
  127. 'complexType',
  128. 'struct',
  129. 'all',
  130. '',
  131. $fields
  132. );
  133. // Register the method to expose
  134. $server->register('WSUpdateTCCUserIdAndGetUser', // method name
  135. array('paramsUpdateTCCUserIdAndGetUser' => 'tns:paramsUpdateTCCUserIdAndGetUser'), // input parameters
  136. array('return' => 'tns:resultUpdateTCCUserIdAndGetUserArray'), // output parameters
  137. 'urn:WSTCC', // namespace
  138. 'urn:WSTCC#WSCreateUserPasswordCrypted', // soapaction
  139. 'rpc', // style
  140. 'encoded', // use
  141. 'This service adds users' // documentation
  142. );
  143. // Define the method WSUpdateTCCUserIdAndGetUser
  144. function WSUpdateTCCUserIdAndGetUser($params)
  145. {
  146. global $_configuration, $debug;
  147. $debug = 1;
  148. if ($debug) error_log('WSUpdateTCCUserIdAndGetUser');
  149. if ($debug) error_log(print_r($params, 1));
  150. if (!WSHelperVerifyKey($params)) {
  151. return returnError(WS_ERROR_SECRET_KEY);
  152. }
  153. $users = UserManager::getRepository()->getUsersByEmail($params['email']);
  154. if (!empty($users)) {
  155. if (isset($users[0]) && $users[0] instanceof User) {
  156. /** @var User $user */
  157. $user = $users[0];
  158. $userInfo = api_get_user_info(
  159. $user->getId(),
  160. false,
  161. false,
  162. true,
  163. false
  164. );
  165. if ($params['tcc_user_id'] !== '') {
  166. $extraFieldValue = new ExtraFieldValue('user');
  167. $extraField = new ExtraField('user');
  168. $extraFieldData = $extraField->get_handler_field_info_by_field_variable('tcc_user_id');
  169. $params = [
  170. 'field_id' => $extraFieldData['id'],
  171. 'value' => $params['tcc_user_id'],
  172. 'item_id' => $user->getId(),
  173. ];
  174. $extraFieldValue->save($params);
  175. }
  176. $extraFields = [
  177. 'terms_genre',
  178. 'terms_datedenaissance',
  179. 'terms_ville',
  180. 'terms_paysresidence',
  181. 'terms_nationalite',
  182. 'terms_codepostal',
  183. 'terms_adresse'
  184. ];
  185. $extraFieldResults = [];
  186. foreach ($userInfo['extra'] as $field) {
  187. /** @var \Chamilo\CoreBundle\Entity\ExtraFieldValues $extraFieldValue */
  188. $extraFieldValue = $field['value'];
  189. $variable = $extraFieldValue->getField()->getVariable();
  190. $extraFieldResults[$variable] = '';
  191. if (in_array($variable, $extraFields)) {
  192. $extraFieldResults[$variable] = $extraFieldValue->getValue();
  193. }
  194. }
  195. $parts = explode('-', $extraFieldResults['terms_datedenaissance']);
  196. $extraFieldResults['terms_datedenaissance'] = $parts[0].'/'.$parts[1].'/'.$parts[2];
  197. $extraFieldResults['terms_genre'] = $extraFieldResults['terms_genre'] === 'homme' ? 'Masculin' : 'Féminin';
  198. $language = 'fr-FR';
  199. switch ($user->getLanguage()) {
  200. case 'french2':
  201. case 'french':
  202. $language = 'fr-FR';
  203. break;
  204. case 'german':
  205. case 'german2':
  206. $language = 'de-DE';
  207. break;
  208. }
  209. $result = [
  210. 'Genre' => $extraFieldResults['terms_genre'],
  211. 'Nom' => $user->getLastname(),
  212. 'Prenom' => $user->getFirstname(),
  213. 'DateNaissance' => $extraFieldResults['terms_datedenaissance'],
  214. 'Langue' => $language,
  215. 'Nationalite' => $extraFieldResults['terms_nationalite'],
  216. 'Pays' => $extraFieldResults['terms_paysresidence'],
  217. 'Adresse' => $extraFieldResults['terms_adresse'],
  218. 'CodePostal' => $extraFieldResults['terms_codepostal'],
  219. 'Ville' => $extraFieldResults['terms_ville'],
  220. 'Email' => $user->getEmail(),
  221. ];
  222. if ($debug) error_log(print_r($result, 1));
  223. return $result;
  224. }
  225. }
  226. return [];
  227. }
  228. $fields = $fields + [
  229. 'tcc_user_id' => array('name' => 'tcc_user_id', 'type' => 'xsd:string'),
  230. 'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string')
  231. ];
  232. /* Register WSEditUser function */
  233. // Register the data structures used by the service
  234. $server->wsdl->addComplexType(
  235. 'editUser',
  236. 'complexType',
  237. 'struct',
  238. 'all',
  239. '',
  240. $fields
  241. );
  242. // Register the method to expose
  243. $server->register('WSEditUserTCC', // method name
  244. array('editUser' => 'tns:editUser'), // input parameters
  245. array('return' => 'xsd:string'), // output parameters
  246. 'urn:WSTCC', // namespace
  247. 'urn:WSTCC#WSEditUserTCC', // soapaction
  248. 'rpc', // style
  249. 'encoded', // use
  250. 'This service edits a user from wiener' // documentation
  251. );
  252. // Define the method WSEditUser
  253. function WSEditUserTCC($params)
  254. {
  255. if (!WSHelperVerifyKey($params)) {
  256. return returnError(WS_ERROR_SECRET_KEY);
  257. }
  258. $extraFieldValue = new ExtraFieldValue('user');
  259. $data = $extraFieldValue->get_item_id_from_field_variable_and_field_value('tcc_user_id', $params['tcc_user_id']);
  260. if ($data && isset($data['item_id'])) {
  261. $userId = $data['item_id'];
  262. $user = api_get_user_entity($userId);
  263. if (!empty($user)) {
  264. switch ($params['Langue']) {
  265. case 'fr-FR':
  266. $params['Langue'] = 'french2';
  267. break;
  268. case 'de-DE':
  269. $params['Langue'] = 'german2';
  270. break;
  271. }
  272. $user
  273. ->setFirstname($params['Prenom'])
  274. ->setLastname($params['Nom'])
  275. ->setLanguage($params['Langue'])
  276. ->setEmail($params['Email'])
  277. ;
  278. $em = Database::getManager();
  279. $em->merge($user);
  280. $em->flush();
  281. $extraField = new ExtraField('user');
  282. $extraFieldValue = new ExtraFieldValue('user');
  283. $fields = [
  284. 'terms_genre' => 'Genre',
  285. 'terms_datedenaissance' => 'DateNaissance',
  286. 'terms_ville' => 'Ville',
  287. 'terms_paysresidence' => 'Pays',
  288. 'terms_nationalite' => 'Nationalite',
  289. 'terms_codepostal' => 'CodePostal',
  290. 'terms_adresse' => 'Adresse'
  291. ];
  292. foreach ($fields as $extraFieldName => $externalName) {
  293. $fieldInfo = $extraField->get_handler_field_info_by_field_variable($extraFieldName);
  294. switch ($extraFieldName) {
  295. case 'terms_genre':
  296. $params[$externalName] = $params[$externalName] === 'Masculin' ? 'homme' : 'femme';
  297. break;
  298. case 'terms_datedenaissance':
  299. if (!empty($params[$externalName])) {
  300. $parts = explode('/', $params[$externalName]);// dd/mm/yyyy
  301. $params[$externalName] = $parts[2].'-'.$parts[1].'-'.$parts[0];
  302. }
  303. break;
  304. }
  305. if ($fieldInfo) {
  306. $paramsToSave = [
  307. 'field_id' => $fieldInfo['id'],
  308. 'item_id' => $userId,
  309. 'value' => $params[$externalName]
  310. ];
  311. error_log($extraFieldName);
  312. error_log(print_r($paramsToSave, 1));
  313. $extraFieldValue->save($paramsToSave);
  314. }
  315. }
  316. return 1;
  317. }
  318. return 0;
  319. }
  320. }
  321. // If you send your data in utf8 then this value must be false.
  322. $decodeUTF8 = api_get_setting('registration.soap.php.decode_utf8');
  323. if ($decodeUTF8 === 'true') {
  324. $server->decode_utf8 = true;
  325. } else {
  326. $server->decode_utf8 = false;
  327. }
  328. $server->service(file_get_contents('php://input'));