openoffice_document.class.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php //$id:$
  2. /**
  3. * Defines the OpenofficeDocument class, which is meant as a mother class
  4. * to help in the conversion of Office documents to learning paths
  5. * @package dokeos.learnpath
  6. * @author Eric Marguin <eric.marguin@dokeos.com>
  7. * @license GNU/GPL - See Dokeos license directory for details
  8. */
  9. /**
  10. * Defines the "OpenofficeDocument" child of class "learnpath"
  11. * @package dokeos.learnpath.aicc
  12. */
  13. abstract class OpenofficeDocument extends learnpath {
  14. public $first_item = 0;
  15. public $original_charset = 'utf-8';
  16. public $original_locale = 'en_US.UTF-8';
  17. /**
  18. * Class constructor. Based on the parent constructor.
  19. * @param string Course code
  20. * @param integer Learnpath ID in DB
  21. * @param integer User ID
  22. */
  23. function OpenofficeDocument($course_code=null,$resource_id=null,$user_id=null) {
  24. if($this->debug>0){error_log('In OpenofficeDocument::OpenofficeDocument()',0);}
  25. if(!empty($course_code) and !empty($resource_id) and !empty($user_id))
  26. {
  27. parent::learnpath($course_code, $resource_id, $user_id);
  28. }else{
  29. //do nothing but still build the presentation object
  30. }
  31. }
  32. function convert_document($file, $action_after_conversion='make_lp'){
  33. global $_course, $_user, $_configuration;
  34. $this->file_name = (strrpos($file['name'],'.')>0 ? substr($file['name'], 0, strrpos($file['name'],'.')) : $file['name']);
  35. $this->file_name = replace_dangerous_char($this->file_name,'strict');
  36. $this->file_name = strtolower($this->file_name);
  37. $visio_dir = ($action_after_conversion=='add_docs_to_visio')?VIDEOCONF_UPLOAD_PATH:'';
  38. $this->file_path = $visio_dir.'/'.$this->file_name.'.'.pathinfo($file['name'],PATHINFO_EXTENSION);
  39. $dir_name = $visio_dir.'/'.$this->file_name;
  40. //create the directory
  41. $this->base_work_dir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  42. $this->created_dir = create_unexisting_directory($_course,$_user['user_id'],0,0,$this->base_work_dir,$dir_name);
  43. move_uploaded_file($file['tmp_name'],$this->base_work_dir.'/'.$this->file_path);
  44. $perm = api_get_setting('permissions_for_new_files');
  45. /*
  46. $classpath = '-cp .:jodconverter-2.2.1.jar:jodconverter-cli-2.2.1.jar';
  47. if(isset($_ENV['OS']) && strpos($_ENV['OS'],'Windows') !== false)
  48. {
  49. $classpath = str_replace(':',';',$classpath);
  50. }
  51. if(isset($_ENV['OS']) && strpos($_ENV['OS'],'Windows') !== false)
  52. {
  53. $cmd = 'cd '.str_replace('/','\\',api_get_path(SYS_PATH).'main/inc/lib/ppt2png ').$classpath.' DokeosConverter';
  54. }
  55. else
  56. {
  57. $cmd = 'cd '.api_get_path(SYS_PATH).'main/inc/lib/ppt2png && java '.$classpath.' DokeosConverter';
  58. }
  59. $cmd .= ' -p '.api_get_setting('service_ppt2lp','port');
  60. */
  61. if (IS_WINDOWS_OS) // IS_WINDOWS_OS has been defined in main_api.lib.php
  62. {
  63. $converter_path = str_replace('/','\\',api_get_path(SYS_PATH).'main/inc/lib/ppt2png');
  64. $class_path = $converter_path.';'.$converter_path.'/jodconverter-2.2.1.jar;'.$converter_path.'/jodconverter-cli-2.2.1.jar';
  65. //$cmd = 'java -cp "'.$class_path.'" DokeosConverter';
  66. $cmd = 'java -Dfile.encoding=UTF-8 -cp "'.$class_path.'" DokeosConverter';
  67. }
  68. else
  69. {
  70. $converter_path = api_get_path(SYS_PATH).'main/inc/lib/ppt2png';
  71. //$class_path = '-cp .:jodconverter-2.2.1.jar:jodconverter-cli-2.2.1.jar';
  72. $class_path = ' -Dfile.encoding=UTF-8 -cp .:jodconverter-2.2.1.jar:jodconverter-cli-2.2.1.jar';
  73. $cmd = 'cd '.$converter_path.' && java '.$class_path.' DokeosConverter';
  74. }
  75. $cmd .= ' -p '.api_get_setting('service_ppt2lp','port');
  76. // call to the function implemented by child
  77. $cmd .= $this -> add_command_parameters();
  78. // to allow openoffice to manipulate docs.
  79. @chmod ($this->base_work_dir.$this->created_dir,0777);
  80. @chmod ($this->base_work_dir.$this->file_path,0777);
  81. $locale = $this->original_locale; // TODO : improve it because we're not sure this locale is present everywhere
  82. putenv('LC_ALL='.$locale);
  83. $files = array(); $return = 0;
  84. $shell = exec($cmd, $files, $return);
  85. if($return != 0) { //if the java application returns an error code
  86. switch($return)
  87. {
  88. // can't connect to openoffice
  89. case 1 : $this->error = get_lang('CannotConnectToOpenOffice');break;
  90. // conversion failed in openoffice
  91. case 2 : $this->error = get_lang('OogieConversionFailed');break;
  92. // conversion can't be launch because command failed
  93. case 255 : $this->error = get_lang('OogieUnknownError');break;
  94. }
  95. DocumentManager::delete_document($_course, $dir_name, $this->base_work_dir);
  96. return false;
  97. }
  98. // create lp
  99. $this->lp_id = learnpath::add_lp($_course['id'], ucfirst(pathinfo($file['name'], PATHINFO_FILENAME)),'','guess','manual');
  100. // call to the function implemented by child following action_after_conversion parameter
  101. switch ($action_after_conversion)
  102. {
  103. case 'make_lp':$this -> make_lp($files);
  104. break;
  105. case 'add_docs_to_visio':$this -> add_docs_to_visio($files);
  106. break;
  107. }
  108. chmod ($this->base_work_dir.$this->created_dir, api_get_permissions_for_new_directories());
  109. return $this->first_item;
  110. }
  111. abstract function make_lp();
  112. abstract function add_docs_to_visio();
  113. abstract function add_command_parameters();
  114. }
  115. ?>