access_url.php 13 KB

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