tcc.soap.php 13 KB

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