lp.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.webservices
  5. */
  6. require_once __DIR__.'/../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. return 'manifest.xml file not found';
  172. }
  173. $manifestData = $oScorm->parse_manifest($manifest);
  174. if (!empty($manifestData)) {
  175. $oScorm->import_manifest(
  176. $courseInfo['code'],
  177. $maxScore,
  178. $sessionId,
  179. $userId
  180. );
  181. $oScorm->set_name($lpName);
  182. $oScorm->set_proximity($proximity, $courseId);
  183. $oScorm->set_maker($maker, $courseId);
  184. //$oScorm->set_jslib('scorm_api.php');
  185. if ($debug) error_log('scorm was added');
  186. return 1;
  187. } else {
  188. if ($debug) error_log('manifest data empty');
  189. return 'manifest data empty';
  190. }
  191. }
  192. $server->wsdl->addComplexType(
  193. 'paramsGetLpList',
  194. 'complexType',
  195. 'struct',
  196. 'all',
  197. '',
  198. array(
  199. 'course_id_name' => array(
  200. 'name' => 'course_id_name',
  201. 'type' => 'xsd:string',
  202. ),
  203. 'course_id_value' => array(
  204. 'name' => 'course_id_name',
  205. 'type' => 'xsd:string',
  206. ),
  207. 'session_id_name' => array(
  208. 'name' => 'session_id_name',
  209. 'type' => 'xsd:string',
  210. ),
  211. 'session_id_value' => array(
  212. 'name' => 'session_id_value',
  213. 'type' => 'xsd:string',
  214. ),
  215. 'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string'),
  216. )
  217. );
  218. $server->wsdl->addComplexType(
  219. 'lpListItem',
  220. 'complexType',
  221. 'struct',
  222. 'all',
  223. '',
  224. array(
  225. 'id' => array('name' => 'id', 'type' => 'xsd:string'),
  226. 'name' => array('name' => 'name', 'type' => 'xsd:string'),
  227. )
  228. );
  229. $server->wsdl->addComplexType(
  230. 'lpList',
  231. 'complexType',
  232. 'array',
  233. '',
  234. 'SOAP-ENC:Array',
  235. array(),
  236. array(array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:lpListItem[]')),
  237. 'tns:lpListItem'
  238. );
  239. // Register the method to expose
  240. $server->register('WSGetLpList', // method name
  241. array('params' => 'tns:paramsGetLpList'), // input parameters
  242. array('return' => 'tns:lpList'), // output parameters
  243. 'urn:WSLP', // namespace
  244. 'urn:WSLP#WSGetLpList', // soapaction
  245. 'rpc', // style
  246. 'encoded', // use
  247. 'This service adds users' // documentation
  248. );
  249. /**
  250. * @param array $params
  251. * @return int|string
  252. */
  253. function WSGetLpList($params)
  254. {
  255. global $debug;
  256. if (!WSHelperVerifyKey($params)) {
  257. return return_error(WS_ERROR_SECRET_KEY);
  258. }
  259. require_once api_get_path(SYS_CODE_PATH).'lp/learnpathList.class.php';
  260. require_once api_get_path(SYS_CODE_PATH).'lp/learnpath.class.php';
  261. require_once api_get_path(SYS_CODE_PATH).'lp/learnpathItem.class.php';
  262. $courseIdName = $params['course_id_name'];
  263. $courseIdValue = $params['course_id_value'];
  264. $sessionIdName = isset($params['session_id_name']) ? $params['session_id_name'] : null;
  265. $sessionIdValue = isset($params['session_id_value']) ? $params['session_id_value'] : null;
  266. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  267. $courseIdValue,
  268. $courseIdName
  269. );
  270. if (empty($courseInfo)) {
  271. if ($debug) error_log("Course not found: $courseIdName : $courseIdValue");
  272. return 'Course not found';
  273. }
  274. $courseId = $courseInfo['real_id'];
  275. $sessionId = 0;
  276. if (!empty($sessionIdName) && !empty($sessionIdValue)) {
  277. $sessionId = SessionManager::get_session_id_from_original_id(
  278. $sessionIdValue,
  279. $sessionIdName
  280. );
  281. if (empty($sessionId)) {
  282. if ($debug) error_log('Session not found');
  283. return 'Session not found';
  284. }
  285. }
  286. $list = new LearnpathList(null, $courseInfo['code'], $sessionId);
  287. $flatList = $list->get_flat_list();
  288. $result = array();
  289. foreach ($flatList as $id => $lp) {
  290. $result[] = array(
  291. 'id' => $id,
  292. 'name' => $lp['lp_name'],
  293. );
  294. }
  295. return $result;
  296. }
  297. $server->wsdl->addComplexType(
  298. 'paramsDeleteLp',
  299. 'complexType',
  300. 'struct',
  301. 'all',
  302. '',
  303. array(
  304. 'course_id_name' => array(
  305. 'name' => 'course_id_name',
  306. 'type' => 'xsd:string',
  307. ),
  308. 'course_id_value' => array(
  309. 'name' => 'course_id_name',
  310. 'type' => 'xsd:string',
  311. ),
  312. 'lp_id' => array(
  313. 'name' => 'lp_id',
  314. 'type' => 'xsd:string',
  315. ),
  316. 'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string'),
  317. )
  318. );
  319. // Register the method to expose
  320. $server->register('WSDeleteLp', // method name
  321. array('params' => 'tns:paramsDeleteLp'), // input parameters
  322. array('return' => 'xsd:string'), // output parameters
  323. 'urn:WSLP', // namespace
  324. 'urn:WSLP#WSDeleteLp', // soapaction
  325. 'rpc', // style
  326. 'encoded', // use
  327. 'This service deletes a LP' // documentation
  328. );
  329. /**
  330. * @param array $params
  331. * @return int|string
  332. */
  333. function WSDeleteLp($params)
  334. {
  335. global $debug;
  336. if (!WSHelperVerifyKey($params)) {
  337. return return_error(WS_ERROR_SECRET_KEY);
  338. }
  339. require_once api_get_path(SYS_CODE_PATH).'lp/learnpathList.class.php';
  340. require_once api_get_path(SYS_CODE_PATH).'lp/learnpath.class.php';
  341. require_once api_get_path(SYS_CODE_PATH).'lp/learnpathItem.class.php';
  342. $courseIdName = $params['course_id_name'];
  343. $courseIdValue = $params['course_id_value'];
  344. $lpId = $params['lp_id'];
  345. $sessionIdName = isset($params['session_id_name']) ? $params['session_id_name'] : null;
  346. $sessionIdValue = isset($params['session_id_value']) ? $params['session_id_value'] : null;
  347. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  348. $courseIdValue,
  349. $courseIdName
  350. );
  351. if (empty($courseInfo)) {
  352. if ($debug) error_log("Course not found: $courseIdName : $courseIdValue");
  353. return 'Course not found';
  354. }
  355. $courseId = $courseInfo['real_id'];
  356. $courseCode = $courseInfo['code'];
  357. $sessionId = 0;
  358. /*
  359. if (!empty($sessionIdName) && !empty($sessionIdValue)) {
  360. $sessionId = SessionManager::get_session_id_from_original_id(
  361. $sessionIdValue,
  362. $sessionIdName
  363. );
  364. if (empty($sessionId)) {
  365. if ($debug) error_log('Session not found');
  366. return 'Session not found';
  367. }
  368. }
  369. */
  370. $lp = new learnpath($courseCode, $lpId, null);
  371. if ($lp) {
  372. if ($debug) error_log("LP deleted $lpId");
  373. $course_dir = $courseInfo['directory'].'/document';
  374. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  375. $base_work_dir = $sys_course_path.$course_dir;
  376. $items = $lp->get_flat_ordered_items_list($lpId, 0, $courseId);
  377. if (!empty($items)) {
  378. /** @var $item learnpathItem */
  379. foreach ($items as $itemId) {
  380. $item = new learnpathItem($itemId, null, $courseId);
  381. if ($item) {
  382. $documentId = $item->get_path();
  383. if ($debug) error_log("lp item id found #$itemId");
  384. $documentInfo = DocumentManager::get_document_data_by_id(
  385. $documentId,
  386. $courseInfo['code'],
  387. false,
  388. $sessionId
  389. );
  390. if (!empty($documentInfo)) {
  391. if ($debug) {
  392. error_log("Document id deleted #$documentId");
  393. }
  394. DocumentManager::delete_document(
  395. $courseInfo,
  396. null,
  397. $base_work_dir,
  398. $sessionId,
  399. $documentId
  400. );
  401. } else {
  402. if ($debug) {
  403. error_log("No document found for id #$documentId");
  404. }
  405. }
  406. } else {
  407. if ($debug) error_log("Document not found #$itemId");
  408. }
  409. }
  410. }
  411. $lp->delete($courseInfo, $lpId, 'remove');
  412. return 1;
  413. }
  414. return 0;
  415. }
  416. $server->wsdl->addComplexType(
  417. 'lpItem',
  418. 'complexType',
  419. 'struct',
  420. 'all',
  421. '',
  422. array(
  423. 'data' => array('name' => 'data', 'type' => 'xsd:string'),
  424. 'title' => array('name' => 'title', 'type' => 'xsd:string'),
  425. 'filename' => array('name' => 'filename', 'type' => 'xsd:string'),
  426. )
  427. );
  428. $server->wsdl->addComplexType(
  429. 'lpItemList',
  430. 'complexType',
  431. 'array',
  432. '',
  433. 'SOAP-ENC:Array',
  434. array(),
  435. array(array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:lpItem[]')),
  436. 'tns:lpItem'
  437. );
  438. $server->wsdl->addComplexType(
  439. 'paramsCreateLp',
  440. 'complexType',
  441. 'struct',
  442. 'all',
  443. '',
  444. array(
  445. 'course_id_name' => array(
  446. 'name' => 'course_id_name',
  447. 'type' => 'xsd:string',
  448. ),
  449. 'course_id_value' => array(
  450. 'name' => 'course_id_name',
  451. 'type' => 'xsd:string',
  452. ),
  453. /*'session_id_name' => array(
  454. 'name' => 'session_id_name',
  455. 'type' => 'xsd:string',
  456. ),
  457. 'session_id_value' => array(
  458. 'name' => 'session_id_value',
  459. 'type' => 'xsd:string',
  460. ),*/
  461. 'lp_name' => array(
  462. 'name' => 'lp_name',
  463. 'type' => 'xsd:string',
  464. ),
  465. 'lp_item_list' => array(
  466. 'name' => 'lp_item_list',
  467. 'type' => 'tns:lpItemList',
  468. ),
  469. 'secret_key' => array('name' => 'secret_key', 'type' => 'xsd:string'),
  470. )
  471. );
  472. // Register the method to expose
  473. $server->register('WSCreateLp', // method name
  474. array('params' => 'tns:paramsCreateLp'), // input parameters
  475. array('return' => 'xsd:string'), // output parameters
  476. 'urn:WSLP', // namespace
  477. 'urn:WSLP#WSCreateLp', // soapaction
  478. 'rpc', // style
  479. 'encoded', // use
  480. 'This service creates a LP' // documentation
  481. );
  482. /**
  483. * @param array $params
  484. * @return null|soap_fault
  485. */
  486. function WSCreateLp($params)
  487. {
  488. global $debug;
  489. if (!WSHelperVerifyKey($params)) {
  490. return return_error(WS_ERROR_SECRET_KEY);
  491. }
  492. if ($debug) {
  493. error_log('WSCreateLp');
  494. }
  495. $courseIdName = $params['course_id_name'];
  496. $courseIdValue = $params['course_id_value'];
  497. $lpName = $params['lp_name'];
  498. $lpItemList = $params['lp_item_list'];
  499. /*$sessionIdName = isset($params['session_id_name']) ? $params['session_id_name'] : null;
  500. $sessionIdValue = isset($params['session_id_value']) ? $params['session_id_value'] : null;*/
  501. $courseInfo = CourseManager::getCourseInfoFromOriginalId(
  502. $courseIdValue,
  503. $courseIdName
  504. );
  505. if (empty($courseInfo)) {
  506. if ($debug) {
  507. error_log('Course not found');
  508. }
  509. }
  510. $userId = 1;
  511. $courseId = $courseInfo['real_id'];
  512. $courseCode = $courseInfo['code'];
  513. /*$sessionId = 0;
  514. if (!empty($sessionIdName) && !empty($sessionIdValue)) {
  515. $sessionId = SessionManager::get_session_id_from_original_id(
  516. $sessionIdValue,
  517. $sessionIdName
  518. );
  519. if (empty($sessionId)) {
  520. if ($debug) {
  521. error_log('Session not found');
  522. }
  523. return 'Session not found';
  524. }
  525. }*/
  526. if ($debug) {
  527. error_log('add_lp');
  528. }
  529. $lpId = learnpath::add_lp(
  530. $courseCode,
  531. $lpName,
  532. '',
  533. 'chamilo',
  534. 'manual',
  535. '',
  536. '',
  537. '',
  538. 0,
  539. $userId
  540. );
  541. if ($lpId) {
  542. if ($debug) {
  543. error_log('LP created');
  544. }
  545. $lp = new learnpath($courseCode, $lpId, null);
  546. $previousId = 0;
  547. foreach ($lpItemList as $lpItem) {
  548. $info = pathinfo($lpItem['filename']);
  549. $extension = $info['extension'];
  550. $data = base64_decode($lpItem['data']);
  551. if ($debug) {
  552. error_log('create_document: '.$info['filename']);
  553. }
  554. $documentId = $lp->create_document(
  555. $courseInfo,
  556. $data,
  557. $info['filename'],
  558. $extension,
  559. 0,
  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);