api.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. /*
  3. ===============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2007 Dokeos S.A.
  6. Copyright (c) Arnaud Ligot
  7. For a full list of contributors, see "credits.txt".
  8. The full license can be read in "license.txt".
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13. See the GNU General Public License for more details.
  14. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  15. Mail: info@dokeos.com
  16. ===============================================================================
  17. */
  18. /**
  19. ==============================================================================
  20. * This is an interface between Dokeos and Videoconference application
  21. *
  22. ==============================================================================
  23. */
  24. /*
  25. ==============================================================================
  26. DEBUG
  27. ==============================================================================
  28. */
  29. $debug=0;
  30. /*
  31. ==============================================================================
  32. CONSTANTS
  33. ==============================================================================
  34. */
  35. define('VIDEOCONF_UPLOAD_PATH', '/videoconf');
  36. $PRESENTATION_EXTENSION = array('.ppt', '.odp');
  37. $IMAGE_EXTENSION = array ('.png', '.jpg', '.gif', '.jpeg');
  38. /*
  39. ==============================================================================
  40. SETTING Session ID because flash do not send the cookie
  41. ==============================================================================
  42. */
  43. if (!is_array($_COOKIE) || !isset($_COOKIE["dk_sid"])){
  44. if ($debug>0) error_log("setting SID to:".$_REQUEST["sid"]);
  45. session_id($_REQUEST["sid"]);
  46. }
  47. if ($debug>0)
  48. {
  49. // dump the request
  50. $v = array_keys(get_defined_vars());
  51. error_log(var_export($v, true),3, '/tmp/log');
  52. foreach (array_keys(get_defined_vars()) as $k) {
  53. if ($k == 'GLOBALS')
  54. continue;
  55. error_log($k, 3, '/tmp/log');
  56. error_log(var_export($$k, true), 3, '/tmp/log');
  57. }
  58. }
  59. /*
  60. ==============================================================================
  61. INCLUDE
  62. ==============================================================================
  63. */
  64. require_once ('../inc/global.inc.php');
  65. api_block_anonymous_users();
  66. require_once (api_get_path(LIBRARY_PATH)."course.lib.php");
  67. require_once (api_get_path(LIBRARY_PATH)."document.lib.php");
  68. require_once (api_get_path(LIBRARY_PATH)."fileUpload.lib.php");
  69. /*
  70. ==============================================================================
  71. SETTING UP SOME VARIABLES
  72. ==============================================================================
  73. */
  74. $debug=0;
  75. $action = $_REQUEST["action"];
  76. $cidReq = $_REQUEST["cidReq"];
  77. $user_id = api_get_user_id();
  78. $coursePath = api_get_path(SYS_COURSE_PATH).$cidReq.'/document';
  79. $_course = CourseManager::get_course_information($cidReq);
  80. // FIXME: this sound very strange !!! but add_document need this to works
  81. $_course['dbName'] = $_course['db_name'];
  82. // FIXME: check if CourseManager::get_user_in_course_status return !=
  83. // COURSEMANAGER when the code is not valid
  84. // FIXME: There is no valid course code which contains things like ../
  85. if ($debug>0) error_log($coursePath, 0);
  86. if ($action == "upload")
  87. {
  88. /*
  89. ==============================================================================
  90. PERMISSION
  91. ==============================================================================
  92. */
  93. $permissions = CourseManager::get_user_in_course_status($user_id, $cidReq);
  94. if ($permissions != COURSEMANAGER)
  95. {
  96. if ($debug >0) error_log("Upload from videoconf not allowed !!!",0);
  97. die(); // this user is not allowed to add upload documents
  98. }
  99. /*
  100. ==============================================================================
  101. UPLOAD
  102. ==============================================================================
  103. */
  104. $destPath = $coursePath.VIDEOCONF_UPLOAD_PATH;
  105. if (!is_dir($destPath)){
  106. $result = create_unexisting_directory($_course,$user_id,0,NULL,$coursePath,VIDEOCONF_UPLOAD_PATH);
  107. if (!$result)
  108. if ($debug>0) error_log("Can't create ".$destPath." folder",0);
  109. }
  110. $newPath = handle_uploaded_document($_course,$_FILES['Filedata'],$coursePath,VIDEOCONF_UPLOAD_PATH,$user_id,0,NULL,'',0,'rename',false);
  111. // based on ../newscorm/presentation.class.php
  112. $file_name = (strrpos($newPath,'.')>0 ? substr($newPath, 0, strrpos($newPath,'.')) : $newPath);
  113. $file_extension = (strrpos($newPath,'.')>0 ? substr($newPath, strrpos($newPath,'.'),10) : '');
  114. /* error_log($newPath,0);
  115. error_log($file_name,0);
  116. error_log($file_extension,0);
  117. */
  118. if (in_array($file_extension, $PRESENTATION_EXTENSION))
  119. {
  120. if ($debug > 0) error_log("converting $coursePath$newPath", 0);
  121. /* creating output folder */
  122. $created_dir = create_unexisting_directory($_course,$user_id,0,NULL,$coursePath,$file_name);
  123. /* alow user of openoffice to write into the folder */
  124. // FIXME
  125. chmod($coursePath.$created_dir, 0777);
  126. /*
  127. * exec java application
  128. * the parameters of the program are :
  129. * - javacommand on this server ;
  130. * - host where openoffice is running;
  131. * - port with which openoffice is listening
  132. * - file to convert
  133. * - folder where put the slides
  134. * - ftppassword if required
  135. * The program fills $files with the list of slides created
  136. */
  137. /* building command line */
  138. $classpath = '-cp .:ridl.jar:js.jar:juh.jar:jurt.jar:jut.jar:java_uno.jar:java_uno_accessbridge.jar:edtftpj-1.5.2.jar:unoil.jar';
  139. if(strpos($_ENV['OS'],'Windows') !== false)
  140. {
  141. $classpath = str_replace(':',';',$classpath);
  142. }
  143. $slide_width=640;
  144. $slide_height=480;
  145. if(strpos($_ENV['OS'],'Windows') !== false)
  146. {
  147. $cmd = 'cd '.str_replace('/','\\',api_get_path(SYS_PATH)).'main/inc/lib/ppt2png && java '.$classpath.' DocumentConverter '.api_get_setting('service_ppt2lp','host').' 2002'.' "'.$coursePath.$newPath.'" "'.$coursePath.$created_dir.'"'.' '.$slide_width.' '.$slide_height.' '.api_get_setting('service_ppt2lp','user').' '.api_get_setting('service_ppt2lp','ftp_password');
  148. }
  149. else
  150. {
  151. $cmd = 'cd '.api_get_path(SYS_PATH).'main/inc/lib/ppt2png && java '.$classpath.' DocumentConverter '.api_get_setting('service_ppt2lp','host').' 2002'.' "'.$coursePath.$newPath.'" "'.$coursePath.$created_dir.'"'.' '.$slide_width.' '.$slide_height.' '.api_get_setting('service_ppt2lp','user').' '.api_get_setting('service_ppt2lp','ftp_password');
  152. }
  153. if ($debug>0) error_log($cmd,0);
  154. /* Exec */
  155. $shell = exec($cmd, $files, $return); // files: list of created files, return: shell return code
  156. /* Add Files */
  157. foreach($files as $f)
  158. {
  159. $did = add_document($_course, $created_dir.'/'.$f, 'file', filesize($coursePath.$created_dir.'/'.$f), $f);
  160. if ($did)
  161. api_item_property_update($_course, TOOL_DOCUMENT, $did, 'DocumentAdded', $user_id, 0, NULL);
  162. }
  163. }
  164. } else if ($action == "service")
  165. {/*
  166. ==============================================================================
  167. LIST FILES
  168. ==============================================================================
  169. */
  170. if ($debug>0) error_log("sending file list",0);
  171. $subaction = $_REQUEST["subaction"];
  172. $canDelete = (CourseManager::get_user_in_course_status($user_id, $cidReq) == COURSEMANAGER);
  173. if ($subaction == "list") {
  174. // FIXME: check security around $_REQUEST["cwd"]
  175. $cwd = $_REQUEST["cwd"];
  176. // treat /..
  177. $nParent = 0; // the number of /.. into the url
  178. while (substr($cwd, -3, 3) == "/..")
  179. {
  180. // go to parent directory
  181. $cwd= substr($cwd, 0, -3);
  182. if (strlen($cwd) == 0) $cwd="/";
  183. $nParent++;
  184. }
  185. for (;$nParent >0; $nParent--){
  186. $cwd = (strrpos($cwd,'/')>-1 ? substr($cwd, 0, strrpos($cwd,'/')) : $cwd);
  187. }
  188. if (strlen($cwd) == 0) $cwd="/";
  189. // check if user can delete files. He must be manager and be inside /videoconf
  190. $isBellowVideoConfUploadPath = (substr($cwd,0,strlen(VIDEOCONF_UPLOAD_PATH)) == VIDEOCONF_UPLOAD_PATH);
  191. $canDelete = ($canDelete && $isBellowVideoConfUploadPath);
  192. // get files list
  193. $files = DocumentManager::get_all_document_data($_course, $cwd, 0, NULL, false);
  194. printf("<dokeosobject><fileListMeta></fileListMeta><fileList>");
  195. printf("<folders>");
  196. foreach($files as $i)
  197. if ($i["filetype"] != "folder")
  198. continue;
  199. else
  200. {
  201. printf('<folder><path>%s</path><title>%s</title><canDelete>%s</canDelete></folder>', $i['path'],$i['title'],($canDelete?"true":"false"));
  202. }
  203. printf("</folders><files>");
  204. foreach($files as $i) {
  205. $extension = (strrpos($i['path'],'.')>0 ? substr($i['path'], strrpos($i['path'],'.'),10) : '');
  206. if ($i["filetype"] != "file" || !in_array($extension, $IMAGE_EXTENSION))
  207. continue;
  208. else
  209. {
  210. printf('<file><path>%s</path><title>%s</title><canDelete>%s</canDelete></file>', $i['path'],$i['title'],($canDelete?"true":"false"));
  211. }
  212. }
  213. printf("</files><ppts>");
  214. // foreach($files as $i) {
  215. // FIXME: Please optimise me !!!
  216. // $extension = (strrpos($i['path'],'.')>0 ? substr($i['path'], strrpos($i['path'],'.'),10) : '');
  217. // if ($i["filetype"] != "file" || !in_array($extension, $PRESENTATION_EXTENSION))
  218. // continue;
  219. // else
  220. // {
  221. // printf('<ppt><path>%s</path><title>%s</title><canDelete>%s</canDelete></ppt>', $i['path'], $i['title'],($canDelete?"true":"false"));
  222. // }
  223. // }
  224. // printf("<file><realname>fr1</realname><name>fn1</name></file>");
  225. printf("</ppts>");
  226. printf("</fileList></dokeosobject>");
  227. } else if ($subaction == "delete") {/*
  228. ==============================================================================
  229. PERMISSION
  230. ==============================================================================
  231. */
  232. $permissions = CourseManager::get_user_in_course_status($user_id, $cidReq);
  233. if ($permissions != COURSEMANAGER)
  234. {
  235. if ($debug > 0) error_log("Upload from videoconf not allowed !!!",0);
  236. die(); // this user is not allowed to add upload documents
  237. }
  238. /*
  239. ==============================================================================
  240. DELETE
  241. ==============================================================================
  242. */
  243. $path = $_REQUEST["path"];
  244. if ((substr($path,0,strlen(VIDEOCONF_UPLOAD_PATH)) != VIDEOCONF_UPLOAD_PATH))
  245. {
  246. if ($debug >0 ) error_log("Delete from videoconf for "+$path+" NOT ALLOWED",0);
  247. die();
  248. }
  249. DocumentManager::delete_document($_course, $path, $coursePath);
  250. echo "<result>OK</result>"; // We have to returns something to OpenLaszlo
  251. }
  252. } else if ($action == "download")
  253. {
  254. /*
  255. ==============================================================================
  256. DOWNLOAD
  257. ==============================================================================
  258. */
  259. //check if the document is in the database
  260. if(!DocumentManager::get_document_id($_course,$_REQUEST['file']))
  261. {
  262. //file not found!
  263. if ($debug>0) error_log("404 ".$_REQUEST["file"]);
  264. header("HTTP/1.0 404 Not Found");
  265. $error404 = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">';
  266. $error404 .= '<html><head>';
  267. $error404 .= '<title>404 Not Found</title>';
  268. $error404 .= '</head><body>';
  269. $error404 .= '<h1>Not Found</h1>';
  270. $error404 .= '<p>The requested URL was not found on this server.</p>';
  271. $error404 .= '<hr>';
  272. $error404 .= '</body></html>';
  273. echo($error404);
  274. exit;
  275. }
  276. $doc_url=$_REQUEST['file'];
  277. if ($debug >0) error_log($doc_url);
  278. $full_file_name = $coursePath.$doc_url;
  279. DocumentManager::file_send_for_download($full_file_name,false);
  280. exit;
  281. }
  282. ?>