lp.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.webservices
  5. */
  6. require_once '../inc/global.inc.php';
  7. $libpath = api_get_path(LIBRARY_PATH);
  8. ini_set('memory_limit', -1);
  9. /*
  10. ini_set('upload_max_filesize', '4000M');
  11. ini_set('post_max_size', '4000M');
  12. ini_set('max_execution_time', '80000');
  13. ini_set('max_input_time', '80000');
  14. */
  15. $debug = true;
  16. define('WS_ERROR_SECRET_KEY', 1);
  17. function return_error($code) {
  18. $fault = null;
  19. switch ($code) {
  20. case WS_ERROR_SECRET_KEY:
  21. $fault = new soap_fault('Server', '', 'Secret key is not correct or params are not correctly set');
  22. break;
  23. }
  24. return $fault;
  25. }
  26. function WSHelperVerifyKey($params)
  27. {
  28. global $debug;
  29. $securityFromConfiguration = api_get_configuration_value('security_key');
  30. if (is_array($params)) {
  31. $secret_key = $params['secret_key'];
  32. } else {
  33. $secret_key = $params;
  34. }
  35. //error_log(print_r($params,1));
  36. $check_ip = false;
  37. $ip_matches = false;
  38. $ip = trim($_SERVER['REMOTE_ADDR']);
  39. // if we are behind a reverse proxy, assume it will send the
  40. // HTTP_X_FORWARDED_FOR header and use this IP instead
  41. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  42. list($ip1) = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  43. $ip = trim($ip1);
  44. }
  45. if ($debug)
  46. error_log("ip: $ip");
  47. // Check if a file that limits access from webservices exists and contains
  48. // the restraining check
  49. if (is_file('webservice-auth-ip.conf.php')) {
  50. include 'webservice-auth-ip.conf.php';
  51. if ($debug)
  52. error_log("webservice-auth-ip.conf.php file included");
  53. if (!empty($ws_auth_ip)) {
  54. $check_ip = true;
  55. $ip_matches = api_check_ip_in_range($ip, $ws_auth_ip);
  56. if ($debug)
  57. error_log("ip_matches: $ip_matches");
  58. }
  59. }
  60. if ($debug) {
  61. error_log("checkip " . intval($check_ip));
  62. }
  63. if ($check_ip) {
  64. $security_key = $securityFromConfiguration;
  65. } else {
  66. $security_key = $ip.$securityFromConfiguration;
  67. //error_log($secret_key.'-'.$security_key);
  68. }
  69. $result = api_is_valid_secret_key($secret_key, $security_key);
  70. //error_log($secret_key.'-'.$security_key);
  71. if ($debug)
  72. error_log('WSHelperVerifyKey result: '.intval($result));
  73. return $result;
  74. }
  75. // Create the server instance
  76. $server = new soap_server();
  77. //$server->soap_defencoding = 'UTF-8';
  78. // Initialize WSDL support
  79. $server->configureWSDL('WSLP', 'urn:WSLP');
  80. $server->wsdl->addComplexType(
  81. 'params',
  82. 'complexType',
  83. 'struct',
  84. 'all',
  85. '',
  86. array(
  87. 'course_id_name' => array(
  88. 'name' => 'course_id_name',
  89. 'type' => 'xsd:string',
  90. ),
  91. 'course_id_value' => array(
  92. 'name' => 'course_id_name',
  93. 'type' => 'xsd:string',
  94. ),
  95. 'session_id_name' => array(
  96. 'name' => 'session_id_name',
  97. 'type' => 'xsd:string',
  98. ),
  99. 'session_id_value' => array(
  100. 'name' => 'session_id_value',
  101. 'type' => 'xsd:string',
  102. ),
  103. 'file_data' => array('name' => 'file', 'type' => 'xsd:string'),
  104. 'filename' => array('name' => 'filename', 'type' => 'xsd:string'),
  105. 'lp_name' => array('name' => 'lp_name', 'type' => 'xsd:string'),
  106. 'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string'),
  107. )
  108. );
  109. // Register the method to expose
  110. $server->register('WSImportLP', // method name
  111. array('params' => 'tns:params'), // input parameters
  112. array('return' => 'xsd:string'), // output parameters
  113. 'urn:WSLP', // namespace
  114. 'urn:WSLP#WSImportLP', // soapaction
  115. 'rpc', // style
  116. 'encoded', // use
  117. 'This service adds users' // documentation
  118. );
  119. /**
  120. * @param array $params
  121. * @return int|string
  122. */
  123. function WSImportLP($params)
  124. {
  125. global $debug;
  126. if (!WSHelperVerifyKey($params)) {
  127. return return_error(WS_ERROR_SECRET_KEY);
  128. }
  129. if ($debug) error_log('WSImportLP');
  130. $courseIdName = $params['course_id_name'];
  131. $courseIdValue = $params['course_id_value'];
  132. $sessionIdName = isset($params['session_id_name']) ? $params['session_id_name'] : null;
  133. $sessionIdValue = isset($params['session_id_value']) ? $params['session_id_value'] : null;
  134. $lpName = $params['lp_name'];
  135. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  136. $courseIdValue,
  137. $courseIdName
  138. );
  139. $courseId = $courseInfo['real_id'];
  140. if (empty($courseInfo)) {
  141. if ($debug) error_log('Course not found');
  142. return 'Course not found';
  143. }
  144. $sessionId = 0;
  145. if (!empty($sessionIdName) && !empty($sessionIdValue)) {
  146. $sessionId = SessionManager::getSessionIdFromOriginalId(
  147. $sessionIdValue,
  148. $sessionIdName
  149. );
  150. if (empty($sessionId)) {
  151. if ($debug) error_log('Session not found');
  152. return 'Session not found';
  153. }
  154. }
  155. $proximity = 'local';
  156. $maker = 'Scorm';
  157. $maxScore = ''; //$_REQUEST['use_max_score']
  158. $oScorm = new scorm($courseInfo['code']);
  159. $fileData = base64_decode($params['file_data']);
  160. $uniqueFile = uniqid();
  161. $userId = 1; // admin
  162. $filePath = api_get_path(SYS_ARCHIVE_PATH) . $uniqueFile;
  163. file_put_contents($filePath, $fileData);
  164. $fileName = $params['filename'];
  165. $fileInfo = array(
  166. 'tmp_name' => $filePath,
  167. 'name' => $fileName,
  168. );
  169. $manifest = $oScorm->import_package($fileInfo, '', $courseInfo);
  170. if (!$manifest) {
  171. if ($debug) error_log('manifest.xml file not found');
  172. //if api_set_failure
  173. return 'manifest.xml file not found';
  174. }
  175. $manifestData = $oScorm->parse_manifest($manifest);
  176. if (!empty($manifestData)) {
  177. $oScorm->import_manifest(
  178. $courseInfo['code'],
  179. $maxScore,
  180. $sessionId,
  181. $userId
  182. );
  183. $oScorm->set_name($lpName);
  184. $oScorm->set_proximity($proximity, $courseId);
  185. $oScorm->set_maker($maker, $courseId);
  186. //$oScorm->set_jslib('scorm_api.php');
  187. if ($debug) error_log('scorm was added');
  188. return 1;
  189. } else {
  190. if ($debug) error_log('manifest data empty');
  191. return 'manifest data empty';
  192. }
  193. }
  194. $server->wsdl->addComplexType(
  195. 'paramsGetLpList',
  196. 'complexType',
  197. 'struct',
  198. 'all',
  199. '',
  200. array(
  201. 'course_id_name' => array(
  202. 'name' => 'course_id_name',
  203. 'type' => 'xsd:string',
  204. ),
  205. 'course_id_value' => array(
  206. 'name' => 'course_id_name',
  207. 'type' => 'xsd:string',
  208. ),
  209. 'session_id_name' => array(
  210. 'name' => 'session_id_name',
  211. 'type' => 'xsd:string',
  212. ),
  213. 'session_id_value' => array(
  214. 'name' => 'session_id_value',
  215. 'type' => 'xsd:string',
  216. ),
  217. 'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string'),
  218. )
  219. );
  220. $server->wsdl->addComplexType(
  221. 'lpListItem',
  222. 'complexType',
  223. 'struct',
  224. 'all',
  225. '',
  226. array(
  227. 'id' => array('name' => 'id', 'type' => 'xsd:string'),
  228. 'name' => array('name' => 'name', 'type' => 'xsd:string'),
  229. )
  230. );
  231. $server->wsdl->addComplexType(
  232. 'lpList',
  233. 'complexType',
  234. 'array',
  235. '',
  236. 'SOAP-ENC:Array',
  237. array(),
  238. array(array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:lpListItem[]')),
  239. 'tns:lpListItem'
  240. );
  241. // Register the method to expose
  242. $server->register('WSGetLpList', // method name
  243. array('params' => 'tns:paramsGetLpList'), // input parameters
  244. array('return' => 'tns:lpList'), // output parameters
  245. 'urn:WSLP', // namespace
  246. 'urn:WSLP#WSGetLpList', // soapaction
  247. 'rpc', // style
  248. 'encoded', // use
  249. 'This service adds users' // documentation
  250. );
  251. /**
  252. * @param array $params
  253. * @return int|string
  254. */
  255. function WSGetLpList($params)
  256. {
  257. global $debug;
  258. if (!WSHelperVerifyKey($params)) {
  259. return return_error(WS_ERROR_SECRET_KEY);
  260. }
  261. require_once api_get_path(SYS_CODE_PATH).'newscorm/learnpathList.class.php';
  262. require_once api_get_path(SYS_CODE_PATH).'newscorm/learnpath.class.php';
  263. require_once api_get_path(SYS_CODE_PATH).'newscorm/learnpathItem.class.php';
  264. $courseIdName = $params['course_id_name'];
  265. $courseIdValue = $params['course_id_value'];
  266. $sessionIdName = isset($params['session_id_name']) ? $params['session_id_name'] : null;
  267. $sessionIdValue = isset($params['session_id_value']) ? $params['session_id_value'] : null;
  268. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  269. $courseIdValue,
  270. $courseIdName
  271. );
  272. if (empty($courseInfo)) {
  273. if ($debug) error_log("Course not found: $courseIdName : $courseIdValue");
  274. return 'Course not found';
  275. }
  276. $courseId = $courseInfo['real_id'];
  277. $sessionId = 0;
  278. if (!empty($sessionIdName) && !empty($sessionIdValue)) {
  279. $sessionId = SessionManager::get_session_id_from_original_id(
  280. $sessionIdValue,
  281. $sessionIdName
  282. );
  283. if (empty($sessionId)) {
  284. if ($debug) error_log('Session not found');
  285. return 'Session not found';
  286. }
  287. }
  288. $list = new LearnpathList(null, $courseInfo['code'], $sessionId);
  289. $flatList = $list->get_flat_list();
  290. $result = array();
  291. foreach ($flatList as $id => $lp) {
  292. $result[] = array(
  293. 'id' => $id,
  294. 'name' => $lp['lp_name'],
  295. );
  296. }
  297. return $result;
  298. }
  299. $server->wsdl->addComplexType(
  300. 'paramsDeleteLp',
  301. 'complexType',
  302. 'struct',
  303. 'all',
  304. '',
  305. array(
  306. 'course_id_name' => array(
  307. 'name' => 'course_id_name',
  308. 'type' => 'xsd:string',
  309. ),
  310. 'course_id_value' => array(
  311. 'name' => 'course_id_name',
  312. 'type' => 'xsd:string',
  313. ),
  314. 'lp_id' => array(
  315. 'name' => 'lp_id',
  316. 'type' => 'xsd:string',
  317. ),
  318. 'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string'),
  319. )
  320. );
  321. // Register the method to expose
  322. $server->register('WSDeleteLp', // method name
  323. array('params' => 'tns:paramsDeleteLp'), // input parameters
  324. array('return' => 'xsd:string'), // output parameters
  325. 'urn:WSLP', // namespace
  326. 'urn:WSLP#WSDeleteLp', // soapaction
  327. 'rpc', // style
  328. 'encoded', // use
  329. 'This service deletes a LP' // documentation
  330. );
  331. /**
  332. * @param array $params
  333. * @return int|string
  334. */
  335. function WSDeleteLp($params)
  336. {
  337. global $debug;
  338. if (!WSHelperVerifyKey($params)) {
  339. return return_error(WS_ERROR_SECRET_KEY);
  340. }
  341. require_once api_get_path(SYS_CODE_PATH).'newscorm/learnpathList.class.php';
  342. require_once api_get_path(SYS_CODE_PATH).'newscorm/learnpath.class.php';
  343. require_once api_get_path(SYS_CODE_PATH).'newscorm/learnpathItem.class.php';
  344. $courseIdName = $params['course_id_name'];
  345. $courseIdValue = $params['course_id_value'];
  346. $lpId = $params['lp_id'];
  347. $sessionIdName = isset($params['session_id_name']) ? $params['session_id_name'] : null;
  348. $sessionIdValue = isset($params['session_id_value']) ? $params['session_id_value'] : null;
  349. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  350. $courseIdValue,
  351. $courseIdName
  352. );
  353. if (empty($courseInfo)) {
  354. if ($debug) error_log("Course not found: $courseIdName : $courseIdValue");
  355. return 'Course not found';
  356. }
  357. $courseId = $courseInfo['real_id'];
  358. $courseCode = $courseInfo['code'];
  359. $sessionId = 0;
  360. /*
  361. if (!empty($sessionIdName) && !empty($sessionIdValue)) {
  362. $sessionId = SessionManager::get_session_id_from_original_id(
  363. $sessionIdValue,
  364. $sessionIdName
  365. );
  366. if (empty($sessionId)) {
  367. if ($debug) error_log('Session not found');
  368. return 'Session not found';
  369. }
  370. }
  371. */
  372. $lp = new learnpath($courseCode, $lpId, null);
  373. if ($lp) {
  374. if ($debug) error_log("LP deleted $lpId");
  375. $course_dir = $courseInfo['directory'] . '/document';
  376. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  377. $base_work_dir = $sys_course_path . $course_dir;
  378. $items = $lp->get_flat_ordered_items_list($lpId, 0, $courseId);
  379. if (!empty($items)) {
  380. /** @var $item learnpathItem */
  381. foreach ($items as $itemId) {
  382. $item = new learnpathItem($itemId, null, $courseId);
  383. if ($item) {
  384. $documentId = $item->get_path();
  385. if ($debug) error_log("lp item id found #$itemId");
  386. $documentInfo = DocumentManager::get_document_data_by_id(
  387. $documentId,
  388. $courseInfo['code'],
  389. false,
  390. $sessionId
  391. );
  392. if (!empty($documentInfo)) {
  393. if ($debug) {
  394. error_log("Document id deleted #$documentId");
  395. }
  396. DocumentManager::delete_document(
  397. $courseInfo,
  398. null,
  399. $base_work_dir,
  400. $sessionId,
  401. $documentId
  402. );
  403. } else {
  404. if ($debug) {
  405. error_log("No document found for id #$documentId");
  406. }
  407. }
  408. } else {
  409. if ($debug) error_log("Document not found #$itemId");
  410. }
  411. }
  412. }
  413. $lp->delete($courseInfo, $lpId, 'remove');
  414. return 1;
  415. }
  416. return 0;
  417. }
  418. $server->wsdl->addComplexType(
  419. 'lpItem',
  420. 'complexType',
  421. 'struct',
  422. 'all',
  423. '',
  424. array(
  425. 'data' => array('name' => 'data', 'type' => 'xsd:string'),
  426. 'title' => array('name' => 'title', 'type' => 'xsd:string'),
  427. 'filename' => array('name' => 'filename', 'type' => 'xsd:string'),
  428. )
  429. );
  430. $server->wsdl->addComplexType(
  431. 'lpItemList',
  432. 'complexType',
  433. 'array',
  434. '',
  435. 'SOAP-ENC:Array',
  436. array(),
  437. array(array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:lpItem[]')),
  438. 'tns:lpItem'
  439. );
  440. $server->wsdl->addComplexType(
  441. 'paramsCreateLp',
  442. 'complexType',
  443. 'struct',
  444. 'all',
  445. '',
  446. array(
  447. 'course_id_name' => array(
  448. 'name' => 'course_id_name',
  449. 'type' => 'xsd:string',
  450. ),
  451. 'course_id_value' => array(
  452. 'name' => 'course_id_name',
  453. 'type' => 'xsd:string',
  454. ),
  455. /*'session_id_name' => array(
  456. 'name' => 'session_id_name',
  457. 'type' => 'xsd:string',
  458. ),
  459. 'session_id_value' => array(
  460. 'name' => 'session_id_value',
  461. 'type' => 'xsd:string',
  462. ),*/
  463. 'lp_name' => array(
  464. 'name' => 'lp_name',
  465. 'type' => 'xsd:string',
  466. ),
  467. 'lp_item_list' => array(
  468. 'name' => 'lp_item_list',
  469. 'type' => 'tns:lpItemList',
  470. ),
  471. 'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string'),
  472. )
  473. );
  474. // Register the method to expose
  475. $server->register('WSCreateLp', // method name
  476. array('params' => 'tns:paramsCreateLp'), // input parameters
  477. array('return' => 'xsd:string'), // output parameters
  478. 'urn:WSLP', // namespace
  479. 'urn:WSLP#WSCreateLp', // soapaction
  480. 'rpc', // style
  481. 'encoded', // use
  482. 'This service creates a LP' // documentation
  483. );
  484. /**
  485. * @param array $params
  486. * @return null|soap_fault
  487. */
  488. function WSCreateLp($params)
  489. {
  490. global $debug;
  491. if (!WSHelperVerifyKey($params)) {
  492. return return_error(WS_ERROR_SECRET_KEY);
  493. }
  494. if ($debug) {
  495. error_log('WSCreateLp');
  496. }
  497. $courseIdName = $params['course_id_name'];
  498. $courseIdValue = $params['course_id_value'];
  499. $lpName = $params['lp_name'];
  500. $lpItemList = $params['lp_item_list'];
  501. /*$sessionIdName = isset($params['session_id_name']) ? $params['session_id_name'] : null;
  502. $sessionIdValue = isset($params['session_id_value']) ? $params['session_id_value'] : null;*/
  503. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  504. $courseIdValue,
  505. $courseIdName
  506. );
  507. if (empty($courseInfo)) {
  508. if ($debug) {
  509. error_log('Course not found');
  510. }
  511. }
  512. $userId = 1;
  513. $courseId = $courseInfo['real_id'];
  514. $courseCode = $courseInfo['code'];
  515. /*$sessionId = 0;
  516. if (!empty($sessionIdName) && !empty($sessionIdValue)) {
  517. $sessionId = SessionManager::get_session_id_from_original_id(
  518. $sessionIdValue,
  519. $sessionIdName
  520. );
  521. if (empty($sessionId)) {
  522. if ($debug) {
  523. error_log('Session not found');
  524. }
  525. return 'Session not found';
  526. }
  527. }*/
  528. if ($debug) {
  529. error_log('add_lp');
  530. }
  531. $lpId = learnpath::add_lp(
  532. $courseCode,
  533. $lpName,
  534. '',
  535. 'chamilo',
  536. 'manual',
  537. '',
  538. '',
  539. '',
  540. 0,
  541. $userId
  542. );
  543. if ($lpId) {
  544. if ($debug) {
  545. error_log('LP created');
  546. }
  547. $lp = new learnpath($courseCode, $lpId, null);
  548. $previousId = 0;
  549. foreach ($lpItemList as $lpItem) {
  550. $info = pathinfo($lpItem['filename']);
  551. $extension = $info['extension'];
  552. $data = base64_decode($lpItem['data']);
  553. if ($debug) {
  554. error_log('create_document: '.$info['filename']);
  555. }
  556. $documentId = $lp->create_document(
  557. $courseInfo,
  558. $data,
  559. $info['filename'],
  560. $extension,
  561. 0,
  562. $userId
  563. );
  564. if ($documentId) {
  565. if ($debug) {
  566. error_log("Document created $documentId");
  567. $itemId = $lp->add_item(
  568. null,
  569. $previousId,
  570. 'document',
  571. $documentId,
  572. $lpItem['title'],
  573. '',
  574. '',
  575. 0,
  576. $userId
  577. );
  578. $previousId = $itemId;
  579. if ($itemId) {
  580. if ($debug) {
  581. error_log("Item added");
  582. }
  583. } else {
  584. if ($debug) {
  585. error_log("Item not added");
  586. }
  587. }
  588. }
  589. } else {
  590. if ($debug) {
  591. error_log("Document NOT created");
  592. }
  593. }
  594. }
  595. return 1;
  596. } else {
  597. if ($debug) {
  598. error_log('LP not created');
  599. }
  600. }
  601. return 0;
  602. }
  603. // Use the request to (try to) invoke the service
  604. $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
  605. // If you send your data in utf8 then this value must be false.
  606. if (isset($_configuration['registration.soap.php.decode_utf8'])) {
  607. if ($_configuration['registration.soap.php.decode_utf8']) {
  608. $server->decode_utf8 = true;
  609. } else {
  610. $server->decode_utf8 = false;
  611. }
  612. }
  613. $server->service($HTTP_RAW_POST_DATA);