api.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /* See license terms in /license.txt */
  3. /* FIX for IE cache when using https */
  4. session_cache_limiter("none");
  5. /**
  6. * This is an interface between Chamilo and Videoconference application
  7. *
  8. */
  9. /*==== DEBUG ====*/
  10. $debug = 0;
  11. /*==== CONSTANTS ==== */
  12. define('VIDEOCONF_UPLOAD_PATH', '/videoconf');
  13. $presentation_extension = array('.ppt', '.odp');
  14. $image_extension = array('.png', '.jpg', '.gif', '.jpeg');
  15. if ($debug > 0) {
  16. // dump the request
  17. $v = array_keys(get_defined_vars());
  18. error_log(var_export($v, true), 3, '/tmp/log');
  19. foreach (array_keys(get_defined_vars()) as $k) {
  20. if ($k == 'GLOBALS') {
  21. continue;
  22. }
  23. error_log($k, 3, '/tmp/log');
  24. error_log(var_export($$k, true), 3, '/tmp/log');
  25. }
  26. }
  27. /*==== Flash loose the cookie ===*/
  28. /* needed when using the nice upload window :
  29. if ($_SERVER['HTTP_USER_AGENT'] == 'Shockwave Flash') {
  30. $sid = $_REQUEST['sid'];
  31. if ($debug>0) error_log("reusing: ".$sid);
  32. session_id($sid);
  33. } */
  34. /*==== INCLUDE ====*/
  35. require_once '../inc/global.inc.php';
  36. api_block_anonymous_users();
  37. require_once ("../newscorm/learnpath.class.php");
  38. require_once ("../newscorm/openoffice_presentation.class.php");
  39. /*==== Variables initialisation ====*/
  40. $action = $_REQUEST["action"]; //safe as only used in if()'s
  41. $seek = array('/', '%2F', '..');
  42. $destroy = array('', '', '');
  43. $cidReq = str_replace($seek, $destroy, $_REQUEST["cidReq"]);
  44. $cidReq = Security::remove_XSS($cidReq);
  45. $user_id = api_get_user_id();
  46. $coursePath = api_get_path(SYS_COURSE_PATH).$cidReq.'/document';
  47. $_course = CourseManager::get_course_information($cidReq);
  48. $_course['path'] = $_course['directory'];
  49. // FIXME: FileManager::add_document needs this to work
  50. $_course['dbName'] = $_course['db_name'];
  51. // FIXME: check if CourseManager::get_user_in_course_status return !=
  52. // COURSEMANAGER when the code is not valid
  53. if ($debug > 0) {
  54. error_log($coursePath, 0);
  55. }
  56. if ($action == "uploadgui") {
  57. echo '<form enctype="multipart/form-data" action="api.php" method="POST">
  58. <input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
  59. <input type="hidden" name="action" value="upload" />
  60. <input type="hidden" name="cidReq" value="'.$cidReq.'" />
  61. <input type="hidden" name="sid" value="'.Security::remove_XSS($_REQUEST["sid"]).'" />
  62. '.get_lang('SelectFile').': <input name="Filedata" type="file" /><br />
  63. <input type="submit" value="'.get_lang('UploadFile').'" />
  64. </form>
  65. ';
  66. die();
  67. } else {
  68. if ($action == "upload") {
  69. if ($debug > 0) {
  70. error_log("upload".$_FILES['Filedata']);
  71. }
  72. /*==== PERMISSION ====*/
  73. $permissions = CourseManager::get_user_in_course_status($user_id, $cidReq);
  74. if ($permissions != COURSEMANAGER) {
  75. if ($debug > 0) {
  76. error_log("Upload from videoconf not allowed !!!", 0);
  77. }
  78. die('Not allowed'); // this user is not allowed to add upload documents
  79. }
  80. /*==== UPLOAD ====*/
  81. $destPath = $coursePath.VIDEOCONF_UPLOAD_PATH;
  82. /*==== creation of /videoconf ====*/
  83. if (!is_dir($destPath)) {
  84. $result = FileManager::create_unexisting_directory(
  85. $_course,
  86. $user_id,
  87. api_get_session_id(),
  88. 0,
  89. null,
  90. $coursePath,
  91. VIDEOCONF_UPLOAD_PATH
  92. );
  93. if (!$result) {
  94. if ($debug > 0) {
  95. error_log("Can't create ".$destPath." folder", 0);
  96. }
  97. }
  98. }
  99. /*==== file upload ====*/
  100. $newPath = $_FILES['Filedata']['name'];
  101. if ($debug > 0) {
  102. error_log($newPath);
  103. }
  104. /*==== extension extraction ====*/
  105. $file_name = (strrpos($newPath, '.') > 0 ? substr($newPath, 0, strrpos($newPath, '.')) : $newPath);
  106. $file_extension = (strrpos($newPath, '.') > 0 ? substr($newPath, strrpos($newPath, '.'), 10) : '');
  107. if ($debug > 0) {
  108. error_log(strrpos($newPath, '.'));
  109. }
  110. if ($debug > 0) {
  111. error_log($file_extension);
  112. }
  113. /*==== conversion if needed ====*/
  114. if (!in_array(strtolower($file_extension), $image_extension)) {
  115. if ($debug > 0) {
  116. error_log("converting: ".$file_extension);
  117. }
  118. $take_slide_name = false;
  119. $o_ppt = new OpenofficePresentation($take_slide_name);
  120. $o_ppt->set_slide_size(640, 480);
  121. $o_ppt->convert_document($_FILES['Filedata'], 'add_docs_to_visio');
  122. }
  123. echo '<html><body><script language="javascript">setTimeout(1000,window.close());</script></body></html>';
  124. } else {
  125. if ($action == "service") {
  126. /*==== List files ====*/
  127. if ($debug > 0) {
  128. error_log("sending file list", 0);
  129. }
  130. $subaction = $_REQUEST["subaction"];
  131. $is_manager = (CourseManager::get_user_in_course_status($user_id, $cidReq) == COURSEMANAGER);
  132. if ($subaction == "list") {
  133. // FIXME: check security around $_REQUEST["cwd"]
  134. $cwd = $_REQUEST["cwd"];
  135. // treat /..
  136. $nParent = 0; // the number of /.. into the url
  137. while (substr($cwd, -3, 3) == "/..") {
  138. // go to parent directory
  139. $cwd = substr($cwd, 0, -3);
  140. if (strlen($cwd) == 0) {
  141. $cwd = "/";
  142. }
  143. $nParent++;
  144. }
  145. for (; $nParent > 0; $nParent--) {
  146. $cwd = (strrpos($cwd, '/') > -1 ? substr($cwd, 0, strrpos($cwd, '/')) : $cwd);
  147. }
  148. if (strlen($cwd) == 0) {
  149. $cwd = "/";
  150. }
  151. if (Security::check_abs_path($cwd, api_get_path(SYS_PATH))) {
  152. die();
  153. }
  154. // check if user can delete files. He must be manager and be inside /videoconf
  155. $is_below_videoconf_dir = (substr($cwd, 0, strlen(VIDEOCONF_UPLOAD_PATH)) == VIDEOCONF_UPLOAD_PATH);
  156. if ($debug > 0) {
  157. error_log('Current working directory: '.$cwd);
  158. }
  159. if ($debug > 0) {
  160. error_log('Videoconf upload path: '.VIDEOCONF_UPLOAD_PATH);
  161. }
  162. /* $canDelete = ($canDelete && $isBellowVideoConfUploadPath);
  163. */
  164. $can_delete = ($is_manager && $is_below_videoconf_dir);
  165. // get files list
  166. $files = DocumentManager::get_all_document_data($_course, $cwd, 0, null, false);
  167. printf("<dokeosobject><fileListMeta></fileListMeta><fileList>");
  168. printf("<folders>");
  169. // title filter
  170. if (is_array($files)) {
  171. foreach (array_keys($files) as $k) {
  172. // converting to UTF-8
  173. $files[$k]['title'] = api_convert_encoding(
  174. api_strlen($files[$k]['title']) > 32 ?
  175. api_substr($files[$k]['title'], 0, 32)."..." :
  176. $files[$k]['title'],
  177. 'utf-8',
  178. api_get_system_encoding()
  179. );
  180. // removing '<', '>' and '_'
  181. $files[$k]['title'] = str_replace(array('<', '>', '_'), ' ', $files[$k]['title']);
  182. }
  183. }
  184. if (is_array($files)) {
  185. foreach ($files as $i) {
  186. if ($i["filetype"] == "folder") {
  187. printf(
  188. '<folder><path>%s</path><title>%s</title><canDelete>%s</canDelete></folder>',
  189. $i['path'],
  190. $i['title'],
  191. ($can_delete ? 'true' : 'false')
  192. );
  193. }
  194. }
  195. }
  196. printf("</folders><files>");
  197. if (is_array($files)) {
  198. foreach ($files as $i) {
  199. $extension = (strrpos($i['path'], '.') > 0 ? substr(
  200. $i['path'],
  201. strrpos($i['path'], '.'),
  202. 10
  203. ) : '');
  204. if ($i["filetype"] == "file" && in_array(strtolower($extension), $image_extension)) {
  205. printf(
  206. '<file><path>%s</path><title>%s</title><canDelete>%s</canDelete></file>',
  207. $i['path'],
  208. $i['title'],
  209. ($can_delete ? 'true' : 'false')
  210. );
  211. }
  212. }
  213. }
  214. printf("</files><ppts>");
  215. printf("</ppts>");
  216. printf("</fileList></dokeosobject>");
  217. } else {
  218. if ($subaction == "delete") {
  219. /*==== PERMISSION ====*/
  220. $permissions = CourseManager::get_user_in_course_status($user_id, $cidReq);
  221. if ($permissions != COURSEMANAGER) {
  222. if ($debug > 0) {
  223. error_log("Upload from videoconf not allowed !!!", 0);
  224. }
  225. die(); // this user is not allowed to add upload documents
  226. }
  227. /*==== DELETE ====*/
  228. $path = str_replace('../', '', $_REQUEST["path"]);
  229. if ((substr($path, 0, strlen(VIDEOCONF_UPLOAD_PATH)) != VIDEOCONF_UPLOAD_PATH)) {
  230. if ($debug > 0) {
  231. error_log("Delete from videoconf for " + $path + " NOT ALLOWED", 0);
  232. }
  233. die();
  234. }
  235. DocumentManager::delete_document($_course, $path, $coursePath);
  236. echo "<result>OK</result>"; // We have to return something to OpenLaszlo
  237. }
  238. }
  239. } else {
  240. if ($action == "download") {
  241. /*==== DOWNLOAD ====*/
  242. //check if the document is in the database
  243. if (!DocumentManager::get_document_id($_course, $_REQUEST['file'])) {
  244. //file not found!
  245. if ($debug > 0) {
  246. error_log("404 ".$_REQUEST["file"]);
  247. }
  248. header("HTTP/1.0 404 Not Found");
  249. $error404 = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">';
  250. $error404 .= '<html><head>';
  251. $error404 .= '<title>404 Not Found</title>';
  252. $error404 .= '</head><body>';
  253. $error404 .= '<h1>Not Found</h1>';
  254. $error404 .= '<p>The requested URL was not found on this server.</p>';
  255. $error404 .= '<hr>';
  256. $error404 .= '</body></html>';
  257. echo($error404);
  258. exit;
  259. }
  260. $doc_url = str_replace('../', '', $_REQUEST['file']);
  261. if ($debug > 0) {
  262. error_log($doc_url);
  263. }
  264. $full_file_name = $coursePath.$doc_url;
  265. if (Security::check_abs_path($full_file_name, $coursePath.'/')) {
  266. DocumentManager::file_send_for_download($full_file_name, false);
  267. }
  268. exit;
  269. }
  270. }
  271. }
  272. }