dokeos
[ class tree: dokeos ] [ index: dokeos ] [ all elements ]

Source for file api.php

Documentation is available at api.php

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

Documentation generated on Thu, 12 Jun 2008 12:59:07 -0500 by phpDocumentor 1.4.1