access_url.php 13 KB

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