lp.php 19 KB

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