openoffice_document.class.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Defines the OpenofficeDocument class, which is meant as a mother class
  5. * to help in the conversion of Office documents to learning paths
  6. * @package chamilo.learnpath
  7. * @author Eric Marguin <eric.marguin@dokeos.com>
  8. * @license GNU/GPL
  9. */
  10. /**
  11. * Defines the "OpenofficeDocument" child of class "learnpath"
  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. public function __construct($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) && !empty($resource_id) && !empty($user_id)) {
  26. parent::__construct($course_code, $resource_id, $user_id);
  27. } else {
  28. // Do nothing but still build the presentation object.
  29. }
  30. }
  31. public function convert_document($file, $action_after_conversion = 'make_lp') {
  32. global $_course, $_user, $_configuration;
  33. $this->file_name = (strrpos($file['name'], '.') > 0 ? substr($file['name'], 0, strrpos($file['name'], '.')) : $file['name']);
  34. $this->file_name = replace_dangerous_char($this->file_name, 'strict');
  35. $this->file_name = strtolower($this->file_name);
  36. $visio_dir = ($action_after_conversion == 'add_docs_to_visio') ? VIDEOCONF_UPLOAD_PATH : '';
  37. $this->file_path = $visio_dir.'/'.$this->file_name.'.'.pathinfo($file['name'], PATHINFO_EXTENSION);
  38. $dir_name = $visio_dir.'/'.$this->file_name;
  39. // Create the directory.
  40. $this->base_work_dir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  41. $this->created_dir = create_unexisting_directory($_course, $_user['user_id'], 0, 0, $this->base_work_dir, $dir_name);
  42. $ppt2lp_host = api_get_setting('service_ppt2lp', 'host');
  43. if ($ppt2lp_host == 'localhost') {
  44. move_uploaded_file($file['tmp_name'], $this->base_work_dir.'/'.$this->file_path);
  45. $perm = api_get_setting('permissions_for_new_files');
  46. if (IS_WINDOWS_OS) { // IS_WINDOWS_OS has been defined in main_api.lib.php
  47. $converter_path = str_replace('/', '\\', api_get_path(SYS_PATH).'main/inc/lib/ppt2png');
  48. $class_path = $converter_path.';'.$converter_path.'/jodconverter-2.2.2.jar;'.$converter_path.'/jodconverter-cli-2.2.2.jar';
  49. //$cmd = 'java -cp "'.$class_path.'" DokeosConverter';
  50. $cmd = 'java -Dfile.encoding=UTF-8 -cp "'.$class_path.'" DokeosConverter';
  51. } else {
  52. $converter_path = api_get_path(SYS_PATH).'main/inc/lib/ppt2png';
  53. //$class_path = '-cp .:jodconverter-2.2.1.jar:jodconverter-cli-2.2.1.jar';
  54. $class_path = ' -Dfile.encoding=UTF-8 -cp .:jodconverter-2.2.2.jar:jodconverter-cli-2.2.2.jar';
  55. $cmd = 'cd '.$converter_path.' && java '.$class_path.' DokeosConverter';
  56. }
  57. $cmd .= ' -p '.api_get_setting('service_ppt2lp', 'port');
  58. // Call to the function implemented by child.
  59. $cmd .= $this -> add_command_parameters();
  60. // To allow openoffice to manipulate docs.
  61. @chmod($this->base_work_dir.$this->created_dir, 0777);
  62. @chmod($this->base_work_dir.$this->file_path, 0777);
  63. $locale = $this->original_locale; // TODO: Improve it because we're not sure this locale is present everywhere.
  64. putenv('LC_ALL='.$locale);
  65. $files = array(); $return = 0;
  66. $shell = exec($cmd, $files, $return);
  67. if ($return != 0) { // If the java application returns an error code.
  68. switch ($return) {
  69. // Can't connect to openoffice.
  70. case 1: $this->error = get_lang('CannotConnectToOpenOffice'); break;
  71. // Conversion failed in openoffice.
  72. case 2: $this->error = get_lang('OogieConversionFailed'); break;
  73. // Conversion can't be launch because command failed.
  74. case 255: $this->error = get_lang('OogieUnknownError'); break;
  75. }
  76. DocumentManager::delete_document($_course, $dir_name, $this->base_work_dir);
  77. return false;
  78. }
  79. } else {
  80. // get result from webservices
  81. $result = $this->_get_remote_ppt2lp_files($file);
  82. $result = unserialize(base64_decode($result));
  83. // save remote images to server
  84. chmod ($this->base_work_dir.$this->created_dir, api_get_permissions_for_new_directories());
  85. if (!empty($result['images'])) {
  86. foreach ($result['images'] as $image => $img_data) {
  87. $image_path = $this->base_work_dir.$this->created_dir;
  88. @file_put_contents($image_path.'/'.$image, base64_decode($img_data));
  89. @chmod($image_path.'/'.$image, 0777);
  90. }
  91. }
  92. // files info
  93. $files = $result['files'];
  94. }
  95. if (!empty($files)) {
  96. // Create lp.
  97. $this->lp_id = learnpath::add_lp($_course['id'], ucfirst(pathinfo($file['name'], PATHINFO_FILENAME)), '', 'guess', 'manual');
  98. // Call to the function implemented by child following action_after_conversion parameter.
  99. switch ($action_after_conversion) {
  100. case 'make_lp': $this -> make_lp($files);
  101. break;
  102. case 'add_docs_to_visio': $this -> add_docs_to_visio($files);
  103. break;
  104. }
  105. chmod ($this->base_work_dir.$this->created_dir, api_get_permissions_for_new_directories());
  106. }
  107. return $this->first_item;
  108. }
  109. /**
  110. * Get images files from remote host (with webservices)
  111. * @param array current ppt file
  112. * @return array images files
  113. */
  114. private function _get_remote_ppt2lp_files($file) {
  115. require_once api_get_path(LIBRARY_PATH).'nusoap/nusoap.php';
  116. // host
  117. $ppt2lp_host = api_get_setting('service_ppt2lp', 'host');
  118. // secret key
  119. $secret_key = sha1(api_get_setting('service_ppt2lp', 'ftp_password'));
  120. //$secret_key = sha1($_SERVER['REMOTE_ADDR'].api_get_setting('service_ppt2lp', 'ftp_password'));
  121. //error_log('IP:'.$_SERVER['REMOTE_ADDR']);
  122. // client
  123. $client = new nusoap_client($ppt2lp_host, true);
  124. $result = '';
  125. $err = $client->getError();
  126. if (!$err) {
  127. $file_data = base64_encode(file_get_contents($file['tmp_name']));
  128. $file_name = $file['name'];
  129. $service_ppt2lp_size = api_get_setting('service_ppt2lp', 'size');
  130. $params = array(
  131. 'secret_key' => $secret_key,
  132. 'file_data' => $file_data,
  133. 'file_name' => $file_name,
  134. 'service_ppt2lp_size' => $service_ppt2lp_size,
  135. );
  136. $result = $client->call('ws_convert_ppt', array('convert_ppt' => $params));
  137. } else {
  138. return false;
  139. }
  140. return $result;
  141. }
  142. abstract function make_lp();
  143. abstract function add_docs_to_visio();
  144. abstract function add_command_parameters();
  145. }