access_url.php 13 KB

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