access_url.php 14 KB

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