access_url.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 __DIR__.'/../inc/global.inc.php';
  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 integer $code
  17. */
  18. function return_error($code)
  19. {
  20. $fault = null;
  21. switch ($code) {
  22. case WS_ERROR_SECRET_KEY:
  23. $fault = new soap_fault(
  24. 'Server',
  25. '',
  26. 'Secret key is not correct or params are not correctly set'
  27. );
  28. break;
  29. case WS_ERROR_NOT_FOUND_RESULT:
  30. $fault = new soap_fault(
  31. 'Server',
  32. '',
  33. 'No result was found for this query'
  34. );
  35. break;
  36. case WS_ERROR_INVALID_INPUT:
  37. $fault = new soap_fault(
  38. 'Server',
  39. '',
  40. 'The input variables are invalid o are not correctly set'
  41. );
  42. break;
  43. case WS_ERROR_SETTING:
  44. $fault = new soap_fault(
  45. 'Server',
  46. '',
  47. 'Please check the configuration for this webservice'
  48. );
  49. break;
  50. }
  51. return $fault;
  52. }
  53. /**
  54. * @param array $params
  55. * @return bool
  56. */
  57. function WSHelperVerifyKey($params)
  58. {
  59. global $_configuration, $debug;
  60. if (is_array($params)) {
  61. $secret_key = $params['secret_key'];
  62. } else {
  63. $secret_key = $params;
  64. }
  65. //error_log(print_r($params,1));
  66. $check_ip = false;
  67. $ip_matches = false;
  68. $ip = trim($_SERVER['REMOTE_ADDR']);
  69. // if we are behind a reverse proxy, assume it will send the
  70. // HTTP_X_FORWARDED_FOR header and use this IP instead
  71. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  72. list($ip1, $ip2) = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  73. $ip = trim($ip1);
  74. }
  75. if ($debug) {
  76. error_log("ip: $ip");
  77. }
  78. // Check if a file that limits access from webservices exists and contains
  79. // the restraining check
  80. if (is_file('webservice-auth-ip.conf.php')) {
  81. include 'webservice-auth-ip.conf.php';
  82. if ($debug) {
  83. error_log("webservice-auth-ip.conf.php file included");
  84. }
  85. if (!empty($ws_auth_ip)) {
  86. $check_ip = true;
  87. $ip_matches = api_check_ip_in_range($ip, $ws_auth_ip);
  88. if ($debug) {
  89. error_log("ip_matches: $ip_matches");
  90. }
  91. }
  92. }
  93. if ($debug) {
  94. error_log("checkip ".intval($check_ip));
  95. }
  96. if ($check_ip) {
  97. $security_key = $_configuration['security_key'];
  98. } else {
  99. $security_key = $ip.$_configuration['security_key'];
  100. //error_log($secret_key.'-'.$security_key);
  101. }
  102. $result = api_is_valid_secret_key($secret_key, $security_key);
  103. //error_log($secret_key.'-'.$security_key);
  104. if ($debug) {
  105. error_log('WSHelperVerifyKey result: '.intval($result));
  106. }
  107. return $result;
  108. }
  109. // Create the server instance
  110. $server = new soap_server();
  111. /** @var HookWSRegistration $hook */
  112. $hook = HookWSRegistration::create();
  113. if (!empty($hook)) {
  114. $hook->setEventData(array('server' => $server));
  115. $res = $hook->notifyWSRegistration(HOOK_EVENT_TYPE_PRE);
  116. if (!empty($res['server'])) {
  117. $server = $res['server'];
  118. }
  119. }
  120. $server->soap_defencoding = 'UTF-8';
  121. // Initialize WSDL support
  122. $server->configureWSDL('WSAccessUrl', 'urn:WSAccessUrl');
  123. $server->wsdl->addComplexType(
  124. 'portalItem',
  125. 'complexType',
  126. 'struct',
  127. 'all',
  128. '',
  129. array(
  130. 'id' => array('name' => 'id', 'type' => 'xsd:string'),
  131. 'url' => array('name' => 'url', 'type' => 'xsd:string')
  132. )
  133. );
  134. $server->wsdl->addComplexType(
  135. 'portalList',
  136. 'complexType',
  137. 'array',
  138. '',
  139. 'SOAP-ENC:Array',
  140. array(),
  141. array(
  142. array(
  143. 'ref' => 'SOAP-ENC:arrayType',
  144. 'wsdl:arrayType' => 'tns:portalItem[]',
  145. ),
  146. ),
  147. 'tns:portalItem'
  148. );
  149. $server->wsdl->addComplexType(
  150. 'getPortals',
  151. 'complexType',
  152. 'struct',
  153. 'all',
  154. '',
  155. array(
  156. 'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string')
  157. )
  158. );
  159. // Register the method to expose
  160. $server->register('WSGetPortals', // method name
  161. array('getPortals' => 'tns:getPortals'), // input parameters
  162. array('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. array(
  196. 'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string'),
  197. 'user_id' => array('name' => 'user_id', 'type' => 'xsd:string'),
  198. 'portal_id' => array('name' => 'portal_id', 'type' => 'xsd:string')
  199. )
  200. );
  201. // Register the method to expose
  202. $server->register(
  203. 'WSAddUserToPortal', // method name
  204. array('addUserToPortal' => 'tns:AddUserToPortal'), // input parameters
  205. array('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. array('removeUserFromPortal' => 'tns:AddUserToPortal'), // input parameters
  231. array('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. array(
  260. 'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string'),
  261. 'user_id' => array('name' => 'user_id', 'type' => 'xsd:string'),
  262. )
  263. );
  264. // Register the method to expose
  265. $server->register(
  266. 'WSGetPortalListFromUser', // method name
  267. array('getPortalListFromUser' => 'tns:getPortalListFromUser'), // input parameters
  268. array('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. array(
  298. 'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string'),
  299. 'original_course_id_name' => array('name' => 'original_course_id_name', 'type' => 'xsd:string'),
  300. 'original_course_id_value' => array('name' => 'original_course_id_value', 'type' => 'xsd:string')
  301. )
  302. );
  303. // Register the method to expose
  304. $server->register(
  305. 'WSGetPortalListFromCourse', // method name
  306. array('getPortalListFromCourse' => 'tns:getPortalListFromCourse'), // input parameters
  307. array('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. array(
  340. 'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string'),
  341. 'portal_id' => array('name' => 'portal_id', 'type' => 'xsd:string'),
  342. 'original_course_id_name' => array('name' => 'original_course_id_name', 'type' => 'xsd:string'),
  343. 'original_course_id_value' => array('name' => 'original_course_id_value', 'type' => 'xsd:string')
  344. )
  345. );
  346. // Register the method to expose
  347. $server->register('WSAddCourseToPortal', // method name
  348. array('addCourseToPortal' => 'tns:addCourseToPortal'), // input parameters
  349. array('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('WSRemoveCourseFromPortal', // method name
  374. array('removeCourseFromPortal' => 'tns:addCourseToPortal'), // input parameters
  375. array('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(array('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);