PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/php/main/conference/api.php

https://bitbucket.org/frchico/chamilo_openshift
PHP | 251 lines | 213 code | 13 blank | 25 comment | 18 complexity | 2f43364c5c7f1f19fc44155065f26ae5 MD5 | raw file
  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. {
  17. // dump the request
  18. $v = array_keys(get_defined_vars());
  19. error_log(var_export($v, true),3, '/tmp/log');
  20. foreach (array_keys(get_defined_vars()) as $k) {
  21. if ($k == 'GLOBALS')
  22. continue;
  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 (api_get_path(LIBRARY_PATH)."course.lib.php");
  38. require_once (api_get_path(LIBRARY_PATH)."document.lib.php");
  39. require_once (api_get_path(LIBRARY_PATH)."fileUpload.lib.php");
  40. require_once ("../newscorm/learnpath.class.php");
  41. require_once ("../newscorm/openoffice_presentation.class.php");
  42. /*==== Variables initialisation ====*/
  43. $action = $_REQUEST["action"]; //safe as only used in if()'s
  44. $seek = array('/','%2F','..');
  45. $destroy = array('','','');
  46. $cidReq = str_replace($seek,$destroy,$_REQUEST["cidReq"]);
  47. $cidReq = Security::remove_XSS($cidReq);
  48. $user_id = api_get_user_id();
  49. $coursePath = api_get_path(SYS_COURSE_PATH).$cidReq.'/document';
  50. $_course = CourseManager::get_course_information($cidReq);
  51. $_course['path'] = $_course['directory'];
  52. // FIXME: add_document needs this to work
  53. $_course['dbName'] = $_course['db_name'];
  54. // FIXME: check if CourseManager::get_user_in_course_status return !=
  55. // COURSEMANAGER when the code is not valid
  56. if ($debug>0) error_log($coursePath, 0);
  57. if ($action == "uploadgui")
  58. {
  59. echo '<form enctype="multipart/form-data" action="api.php" method="POST">
  60. <input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
  61. <input type="hidden" name="action" value="upload" />
  62. <input type="hidden" name="cidReq" value="'.$cidReq.'" />
  63. <input type="hidden" name="sid" value="'.Security::remove_XSS($_REQUEST["sid"]).'" />
  64. '.get_lang('SelectFile').': <input name="Filedata" type="file" /><br />
  65. <input type="submit" value="'.get_lang('UploadFile').'" />
  66. </form>
  67. ';
  68. die();
  69. }
  70. else if ($action == "upload")
  71. {
  72. if ($debug >0) error_log("upload".$_FILES['Filedata']);
  73. /*==== PERMISSION ====*/
  74. $permissions = CourseManager::get_user_in_course_status($user_id, $cidReq);
  75. if ($permissions != COURSEMANAGER)
  76. {
  77. if ($debug >0) error_log("Upload from videoconf not allowed !!!",0);
  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. {
  85. $result = create_unexisting_directory($_course,$user_id, api_get_session_id(), 0,NULL,$coursePath,VIDEOCONF_UPLOAD_PATH);
  86. if (!$result)
  87. {
  88. if ($debug>0) error_log("Can't create ".$destPath." folder",0);
  89. }
  90. }
  91. /*==== file upload ====*/
  92. $newPath = $_FILES['Filedata']['name'];
  93. if($debug>0) error_log($newPath);
  94. /*==== extension extraction ====*/
  95. $file_name = (strrpos($newPath,'.')>0 ? substr($newPath, 0, strrpos($newPath,'.')) : $newPath);
  96. $file_extension = (strrpos($newPath,'.')>0 ? substr($newPath, strrpos($newPath,'.'),10) : '');
  97. if($debug>0) error_log(strrpos($newPath,'.'));
  98. if($debug>0) error_log($file_extension);
  99. /*==== conversion if needed ====*/
  100. if (!in_array(strtolower($file_extension), $image_extension))
  101. {
  102. if($debug>0) error_log("converting: ".$file_extension);
  103. $take_slide_name = false;
  104. $o_ppt = new OpenofficePresentation($take_slide_name);
  105. $o_ppt -> set_slide_size(640,480);
  106. $o_ppt -> convert_document($_FILES['Filedata'],'add_docs_to_visio');
  107. }
  108. echo '<html><body><script language="javascript">setTimeout(1000,window.close());</script></body></html>';
  109. }
  110. else if ($action == "service")
  111. {
  112. /*==== List files ====*/
  113. if ($debug>0) error_log("sending file list",0);
  114. $subaction = $_REQUEST["subaction"];
  115. $is_manager = (CourseManager::get_user_in_course_status($user_id, $cidReq) == COURSEMANAGER);
  116. if ($subaction == "list")
  117. {
  118. // FIXME: check security around $_REQUEST["cwd"]
  119. $cwd = $_REQUEST["cwd"];
  120. // treat /..
  121. $nParent = 0; // the number of /.. into the url
  122. while (substr($cwd, -3, 3) == "/..")
  123. {
  124. // go to parent directory
  125. $cwd= substr($cwd, 0, -3);
  126. if (strlen($cwd) == 0) $cwd="/";
  127. $nParent++;
  128. }
  129. for (;$nParent >0; $nParent--){
  130. $cwd = (strrpos($cwd,'/')>-1 ? substr($cwd, 0, strrpos($cwd,'/')) : $cwd);
  131. }
  132. if (strlen($cwd) == 0) $cwd="/";
  133. if (Security::check_abs_path($cwd,api_get_path(SYS_PATH)))
  134. die();
  135. // check if user can delete files. He must be manager and be inside /videoconf
  136. $is_below_videoconf_dir = (substr($cwd,0,strlen(VIDEOCONF_UPLOAD_PATH)) == VIDEOCONF_UPLOAD_PATH);
  137. if($debug>0) error_log('Current working directory: '.$cwd);
  138. if($debug>0) error_log('Videoconf upload path: '.VIDEOCONF_UPLOAD_PATH);
  139. /* $canDelete = ($canDelete && $isBellowVideoConfUploadPath);
  140. */
  141. $can_delete = ($is_manager && $is_below_videoconf_dir);
  142. // get files list
  143. $files = DocumentManager::get_all_document_data($_course, $cwd, 0, NULL, false);
  144. printf("<dokeosobject><fileListMeta></fileListMeta><fileList>");
  145. printf("<folders>");
  146. // title filter
  147. if (is_array($files)) foreach (array_keys($files) as $k)
  148. {
  149. // converting to UTF-8
  150. $files[$k]['title'] = api_convert_encoding(
  151. api_strlen($files[$k]['title']) > 32 ?
  152. api_substr($files[$k]['title'],0, 32)."..." :
  153. $files[$k]['title'],
  154. 'utf-8',api_get_system_encoding());
  155. // removing '<', '>' and '_'
  156. $files[$k]['title'] = str_replace(array('<','>','_'),' ', $files[$k]['title']);
  157. }
  158. if(is_array($files))
  159. {
  160. foreach($files as $i)
  161. {
  162. if ($i["filetype"] == "folder")
  163. printf('<folder><path>%s</path><title>%s</title><canDelete>%s</canDelete></folder>', $i['path'],$i['title'],($can_delete?'true':'false'));
  164. }
  165. }
  166. printf("</folders><files>");
  167. if(is_array($files))
  168. {
  169. foreach($files as $i) {
  170. $extension = (strrpos($i['path'],'.')>0 ? substr($i['path'], strrpos($i['path'],'.'),10) : '');
  171. if ($i["filetype"] == "file" && in_array(strtolower($extension), $image_extension))
  172. printf('<file><path>%s</path><title>%s</title><canDelete>%s</canDelete></file>', $i['path'],$i['title'],($can_delete?'true':'false'));
  173. }
  174. }
  175. printf("</files><ppts>");
  176. printf("</ppts>");
  177. printf("</fileList></dokeosobject>");
  178. }
  179. else if ($subaction == "delete")
  180. {
  181. /*==== PERMISSION ====*/
  182. $permissions = CourseManager::get_user_in_course_status($user_id, $cidReq);
  183. if ($permissions != COURSEMANAGER)
  184. {
  185. if ($debug > 0) error_log("Upload from videoconf not allowed !!!",0);
  186. die(); // this user is not allowed to add upload documents
  187. }
  188. /*==== DELETE ====*/
  189. $path = str_replace('../','',$_REQUEST["path"]);
  190. if ((substr($path,0,strlen(VIDEOCONF_UPLOAD_PATH)) != VIDEOCONF_UPLOAD_PATH))
  191. {
  192. if ($debug >0 ) error_log("Delete from videoconf for "+$path+" NOT ALLOWED",0);
  193. die();
  194. }
  195. DocumentManager::delete_document($_course, $path, $coursePath);
  196. echo "<result>OK</result>"; // We have to return something to OpenLaszlo
  197. }
  198. } else if ($action == "download") {
  199. /*==== DOWNLOAD ====*/
  200. //check if the document is in the database
  201. if(!DocumentManager::get_document_id($_course,$_REQUEST['file'])) {
  202. //file not found!
  203. if ($debug>0) error_log("404 ".$_REQUEST["file"]);
  204. header("HTTP/1.0 404 Not Found");
  205. $error404 = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">';
  206. $error404 .= '<html><head>';
  207. $error404 .= '<title>404 Not Found</title>';
  208. $error404 .= '</head><body>';
  209. $error404 .= '<h1>Not Found</h1>';
  210. $error404 .= '<p>The requested URL was not found on this server.</p>';
  211. $error404 .= '<hr>';
  212. $error404 .= '</body></html>';
  213. echo($error404);
  214. exit;
  215. }
  216. $doc_url = str_replace('../','',$_REQUEST['file']);
  217. if ($debug >0) error_log($doc_url);
  218. $full_file_name = $coursePath.$doc_url;
  219. if (Security::check_abs_path($full_file_name, $coursePath.'/')) {
  220. DocumentManager::file_send_for_download($full_file_name,false);
  221. }
  222. exit;
  223. }