lp.php 20 KB

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