access_url.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\UserBundle\Entity\User;
  4. /**
  5. * @package chamilo.webservices
  6. */
  7. require_once __DIR__.'/../inc/global.inc.php';
  8. $debug = true;
  9. define('WS_ERROR_SECRET_KEY', 1);
  10. define('WS_ERROR_NOT_FOUND_RESULT', 2);
  11. define('WS_ERROR_INVALID_INPUT', 3);
  12. define('WS_ERROR_SETTING', 4);
  13. /**
  14. * @param int $code
  15. */
  16. function return_error($code)
  17. {
  18. $fault = null;
  19. switch ($code) {
  20. case WS_ERROR_SECRET_KEY:
  21. $fault = new soap_fault(
  22. 'Server',
  23. '',
  24. 'Secret key is not correct or params are not correctly set'
  25. );
  26. break;
  27. case WS_ERROR_NOT_FOUND_RESULT:
  28. $fault = new soap_fault(
  29. 'Server',
  30. '',
  31. 'No result was found for this query'
  32. );
  33. break;
  34. case WS_ERROR_INVALID_INPUT:
  35. $fault = new soap_fault(
  36. 'Server',
  37. '',
  38. 'The input variables are invalid o are not correctly set'
  39. );
  40. break;
  41. case WS_ERROR_SETTING:
  42. $fault = new soap_fault(
  43. 'Server',
  44. '',
  45. 'Please check the configuration for this webservice'
  46. );
  47. break;
  48. }
  49. return $fault;
  50. }
  51. /**
  52. * @param array $params
  53. *
  54. * @return bool
  55. */
  56. function WSHelperVerifyKey($params)
  57. {
  58. global $_configuration, $debug;
  59. if (is_array($params)) {
  60. $secret_key = $params['secret_key'];
  61. } else {
  62. $secret_key = $params;
  63. }
  64. //error_log(print_r($params,1));
  65. $check_ip = false;
  66. $ip_matches = false;
  67. $ip = trim($_SERVER['REMOTE_ADDR']);
  68. // if we are behind a reverse proxy, assume it will send the
  69. // HTTP_X_FORWARDED_FOR header and use this IP instead
  70. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  71. list($ip1, $ip2) = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  72. $ip = trim($ip1);
  73. }
  74. if ($debug) {
  75. error_log("ip: $ip");
  76. }
  77. // Check if a file that limits access from webservices exists and contains
  78. // the restraining check
  79. if (is_file('webservice-auth-ip.conf.php')) {
  80. include 'webservice-auth-ip.conf.php';
  81. if ($debug) {
  82. error_log("webservice-auth-ip.conf.php file included");
  83. }
  84. if (!empty($ws_auth_ip)) {
  85. $check_ip = true;
  86. $ip_matches = api_check_ip_in_range($ip, $ws_auth_ip);
  87. if ($debug) {
  88. error_log("ip_matches: $ip_matches");
  89. }
  90. }
  91. }
  92. if ($debug) {
  93. error_log("checkip ".intval($check_ip));
  94. }
  95. if ($check_ip) {
  96. $security_key = $_configuration['security_key'];
  97. } else {
  98. $security_key = $ip.$_configuration['security_key'];
  99. //error_log($secret_key.'-'.$security_key);
  100. }
  101. $result = api_is_valid_secret_key($secret_key, $security_key);
  102. //error_log($secret_key.'-'.$security_key);
  103. if ($debug) {
  104. error_log('WSHelperVerifyKey result: '.intval($result));
  105. }
  106. return $result;
  107. }
  108. // Create the server instance
  109. $server = new soap_server();
  110. /** @var HookWSRegistration $hook */
  111. $hook = HookWSRegistration::create();
  112. if (!empty($hook)) {
  113. $hook->setEventData(['server' => $server]);
  114. $res = $hook->notifyWSRegistration(HOOK_EVENT_TYPE_PRE);
  115. if (!empty($res['server'])) {
  116. $server = $res['server'];
  117. }
  118. }
  119. $server->soap_defencoding = 'UTF-8';
  120. // Initialize WSDL support
  121. $server->configureWSDL('WSAccessUrl', 'urn:WSAccessUrl');
  122. $server->wsdl->addComplexType(
  123. 'portalItem',
  124. 'complexType',
  125. 'struct',
  126. 'all',
  127. '',
  128. [
  129. 'id' => ['name' => 'id', 'type' => 'xsd:string'],
  130. 'url' => ['name' => 'url', 'type' => 'xsd:string'],
  131. ]
  132. );
  133. $server->wsdl->addComplexType(
  134. 'portalList',
  135. 'complexType',
  136. 'array',
  137. '',
  138. 'SOAP-ENC:Array',
  139. [],
  140. [
  141. [
  142. 'ref' => 'SOAP-ENC:arrayType',
  143. 'wsdl:arrayType' => 'tns:portalItem[]',
  144. ],
  145. ],
  146. 'tns:portalItem'
  147. );
  148. $server->wsdl->addComplexType(
  149. 'getPortals',
  150. 'complexType',
  151. 'struct',
  152. 'all',
  153. '',
  154. [
  155. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  156. ]
  157. );
  158. // Register the method to expose
  159. $server->register(
  160. 'WSGetPortals', // method name
  161. ['getPortals' => 'tns:getPortals'], // input parameters
  162. ['return' => 'tns:portalList'], // output parameters
  163. 'urn:WSAccessUrl', // namespace
  164. 'urn:WSAccessUrl#WSGetPortals', // soapaction
  165. 'rpc', // style
  166. 'encoded', // use
  167. 'This service adds a user to portal' // documentation
  168. );
  169. // Define the method WSAddUserToPortal
  170. function WSGetPortals($params)
  171. {
  172. global $debug;
  173. if (!WSHelperVerifyKey($params['secret_key'])) {
  174. return return_error(WS_ERROR_SECRET_KEY);
  175. }
  176. $urlData = UrlManager::get_url_data();
  177. $return = [];
  178. foreach ($urlData as $data) {
  179. $return[] = [
  180. 'id' => $data['id'],
  181. 'url' => $data['url'],
  182. ];
  183. }
  184. if ($debug) {
  185. error_log(print_r($return, 1));
  186. }
  187. return $return;
  188. }
  189. $server->wsdl->addComplexType(
  190. 'AddUserToPortal',
  191. 'complexType',
  192. 'struct',
  193. 'all',
  194. '',
  195. [
  196. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  197. 'user_id' => ['name' => 'user_id', 'type' => 'xsd:string'],
  198. 'portal_id' => ['name' => 'portal_id', 'type' => 'xsd:string'],
  199. ]
  200. );
  201. // Register the method to expose
  202. $server->register(
  203. 'WSAddUserToPortal', // method name
  204. ['addUserToPortal' => 'tns:AddUserToPortal'], // input parameters
  205. ['return' => 'xsd:string'], // output parameters
  206. 'urn:WSAccessUrl', // namespace
  207. 'urn:WSAccessUrl#WSAddUserToPortal', // soapaction
  208. 'rpc', // style
  209. 'encoded', // use
  210. 'This service adds a user to portal' // documentation
  211. );
  212. // Define the method WSAddUserToPortal
  213. function WSAddUserToPortal($params)
  214. {
  215. if (!WSHelperVerifyKey($params['secret_key'])) {
  216. return return_error(WS_ERROR_SECRET_KEY);
  217. }
  218. $userId = $params['user_id'];
  219. $portalId = $params['portal_id'];
  220. UrlManager::add_user_to_url($userId, $portalId);
  221. $result = UrlManager::relation_url_user_exist($userId, $portalId);
  222. if (!empty($result)) {
  223. return 1;
  224. }
  225. return 0;
  226. }
  227. // Register the method to expose
  228. $server->register(
  229. 'WSRemoveUserFromPortal', // method name
  230. ['removeUserFromPortal' => 'tns:AddUserToPortal'], // input parameters
  231. ['return' => 'xsd:string'], // output parameters
  232. 'urn:WSAccessUrl', // namespace
  233. 'urn:WSAccessUrl#WSRemoveUserFromPortal', // soapaction
  234. 'rpc', // style
  235. 'encoded', // use
  236. 'This service remove a user from a portal' // documentation
  237. );
  238. // Define the method WSDeleteUserFromGroup
  239. function WSRemoveUserFromPortal($params)
  240. {
  241. if (!WSHelperVerifyKey($params['secret_key'])) {
  242. return return_error(WS_ERROR_SECRET_KEY);
  243. }
  244. $userId = $params['user_id'];
  245. $portalId = $params['portal_id'];
  246. UrlManager::delete_url_rel_user($userId, $portalId);
  247. $result = UrlManager::relation_url_user_exist($userId, $portalId);
  248. if (empty($result)) {
  249. return 1;
  250. }
  251. return 0;
  252. }
  253. $server->wsdl->addComplexType(
  254. 'getPortalListFromUser',
  255. 'complexType',
  256. 'struct',
  257. 'all',
  258. '',
  259. [
  260. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  261. 'user_id' => ['name' => 'user_id', 'type' => 'xsd:string'],
  262. ]
  263. );
  264. // Register the method to expose
  265. $server->register(
  266. 'WSGetPortalListFromUser', // method name
  267. ['getPortalListFromUser' => 'tns:getPortalListFromUser'], // input parameters
  268. ['return' => 'tns:portalList'], // output parameters
  269. 'urn:WSAccessUrl', // namespace
  270. 'urn:WSAccessUrl#WSGetPortalListFromUser', // soapaction
  271. 'rpc', // style
  272. 'encoded', // use
  273. 'This service remove a user from a portal' // documentation
  274. );
  275. // Define the method WSDeleteUserFromGroup
  276. function WSGetPortalListFromUser($params)
  277. {
  278. if (!WSHelperVerifyKey($params['secret_key'])) {
  279. return return_error(WS_ERROR_SECRET_KEY);
  280. }
  281. $userId = $params['user_id'];
  282. $result = UrlManager::get_access_url_from_user($userId);
  283. if (!empty($result)) {
  284. foreach ($result as &$data) {
  285. $data['id'] = $data['access_url_id'];
  286. }
  287. }
  288. return $result;
  289. }
  290. // Course ws
  291. $server->wsdl->addComplexType(
  292. 'getPortalListFromCourse',
  293. 'complexType',
  294. 'struct',
  295. 'all',
  296. '',
  297. [
  298. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  299. 'original_course_id_name' => ['name' => 'original_course_id_name', 'type' => 'xsd:string'],
  300. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  301. ]
  302. );
  303. // Register the method to expose
  304. $server->register(
  305. 'WSGetPortalListFromCourse', // method name
  306. ['getPortalListFromCourse' => 'tns:getPortalListFromCourse'], // input parameters
  307. ['return' => 'tns:portalList'], // output parameters
  308. 'urn:WSAccessUrl', // namespace
  309. 'urn:WSAccessUrl#getPortalListFromCourse', // soapaction
  310. 'rpc', // style
  311. 'encoded', // use
  312. 'This service remove a user from a portal' // documentation
  313. );
  314. // Define the method WSDeleteUserFromGroup
  315. function WSGetPortalListFromCourse($params)
  316. {
  317. if (!WSHelperVerifyKey($params['secret_key'])) {
  318. return return_error(WS_ERROR_SECRET_KEY);
  319. }
  320. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  321. $params['original_course_id_value'],
  322. $params['original_course_id_name']
  323. );
  324. $courseId = $courseInfo['real_id'];
  325. $result = UrlManager::get_access_url_from_course($courseId);
  326. if (!empty($result)) {
  327. foreach ($result as &$data) {
  328. $data['id'] = $data['access_url_id'];
  329. }
  330. }
  331. return $result;
  332. }
  333. $server->wsdl->addComplexType(
  334. 'addCourseToPortal',
  335. 'complexType',
  336. 'struct',
  337. 'all',
  338. '',
  339. [
  340. 'secret_key' => ['name' => 'secret_key', 'type' => 'xsd:string'],
  341. 'portal_id' => ['name' => 'portal_id', 'type' => 'xsd:string'],
  342. 'original_course_id_name' => ['name' => 'original_course_id_name', 'type' => 'xsd:string'],
  343. 'original_course_id_value' => ['name' => 'original_course_id_value', 'type' => 'xsd:string'],
  344. ]
  345. );
  346. // Register the method to expose
  347. $server->register(
  348. 'WSAddCourseToPortal', // method name
  349. ['addCourseToPortal' => 'tns:addCourseToPortal'], // input parameters
  350. ['return' => 'xsd:string'], // output parameters
  351. 'urn:WSAccessUrl', // namespace
  352. 'urn:WSAccessUrl#WSAddCourseToPortal', // soapaction
  353. 'rpc', // style
  354. 'encoded', // use
  355. 'This service adds a course to portal' // documentation
  356. );
  357. // Define the method WSAddUserToPortal
  358. function WSAddCourseToPortal($params)
  359. {
  360. if (!WSHelperVerifyKey($params['secret_key'])) {
  361. return return_error(WS_ERROR_SECRET_KEY);
  362. }
  363. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  364. $params['original_course_id_value'],
  365. $params['original_course_id_name']
  366. );
  367. $courseId = $courseInfo['real_id'];
  368. $portalId = $params['portal_id'];
  369. UrlManager::add_course_to_url($courseId, $portalId);
  370. $result = UrlManager::relation_url_course_exist($courseId, $portalId);
  371. return intval($result);
  372. }
  373. // Register the method to expose
  374. $server->register(
  375. 'WSRemoveCourseFromPortal', // method name
  376. ['removeCourseFromPortal' => 'tns:addCourseToPortal'], // input parameters
  377. ['return' => 'xsd:string'], // output parameters
  378. 'urn:WSAccessUrl', // namespace
  379. 'urn:WSAccessUrl#WSRemoveCourseFromPortal', // soapaction
  380. 'rpc', // style
  381. 'encoded', // use
  382. 'This service remove a course from a portal' // documentation
  383. );
  384. // Define the method WSDeleteUserFromGroup
  385. function WSRemoveCourseFromPortal($params)
  386. {
  387. if (!WSHelperVerifyKey($params['secret_key'])) {
  388. return return_error(WS_ERROR_SECRET_KEY);
  389. }
  390. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  391. $params['original_course_id_value'],
  392. $params['original_course_id_name']
  393. );
  394. $courseId = $courseInfo['real_id'];
  395. $portalId = $params['portal_id'];
  396. UrlManager::delete_url_rel_course($courseId, $portalId);
  397. $result = UrlManager::relation_url_course_exist($courseId, $portalId);
  398. if (empty($result)) {
  399. return true;
  400. }
  401. return false;
  402. }
  403. /* Delete user from group Web Service end */
  404. // Add more webservices through hooks from plugins
  405. if (!empty($hook)) {
  406. $hook->setEventData(['server' => $server]);
  407. $res = $hook->notifyWSRegistration(HOOK_EVENT_TYPE_POST);
  408. if (!empty($res['server'])) {
  409. $server = $res['server'];
  410. }
  411. }
  412. // Use the request to (try to) invoke the service
  413. $GLOBALS['HTTP_RAW_POST_DATA'] = file_get_contents('php://input');
  414. $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
  415. // If you send your data in utf8 then this value must be false.
  416. $decodeUTF8 = api_get_setting('registration.soap.php.decode_utf8');
  417. if ($decodeUTF8 === 'true') {
  418. $server->decode_utf8 = true;
  419. } else {
  420. $server->decode_utf8 = false;
  421. }
  422. $server->service($HTTP_RAW_POST_DATA);