openoffice_document.class.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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) {
  25. error_log('In OpenofficeDocument::OpenofficeDocument()', 0);
  26. }
  27. if (!empty($course_code) && !empty($resource_id) && !empty($user_id)) {
  28. parent::__construct($course_code, $resource_id, $user_id);
  29. } else {
  30. // Do nothing but still build the presentation object.
  31. }
  32. }
  33. public function convert_document($file, $action_after_conversion = 'make_lp') {
  34. global $_course, $_user, $_configuration;
  35. $this->file_name = pathinfo($file['name'], PATHINFO_FILENAME);
  36. // Create the directory
  37. $result = $this->generate_lp_folder($_course, $this->file_name);
  38. // Create the directory
  39. $this->base_work_dir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  40. ///learning_path/ppt_dirname directory
  41. $this->created_dir = substr($result['dir'], 0, strlen($result['dir']) -1);
  42. $this->file_path = $this->created_dir.'/'.replace_dangerous_char($file['name'], 'strict');
  43. //var_dump($this->file_name, $this->file_path, $this->base_work_dir, $this->created_dir);
  44. /*
  45. * Original code
  46. global $_course, $_user, $_configuration;
  47. $this->file_name = (strrpos($file['name'], '.') > 0 ? substr($file['name'], 0, strrpos($file['name'], '.')) : $file['name']);
  48. $this->file_name = replace_dangerous_char($this->file_name, 'strict');
  49. $this->file_name = strtolower($this->file_name);
  50. $visio_dir = ($action_after_conversion == 'add_docs_to_visio') ? VIDEOCONF_UPLOAD_PATH : '';
  51. $this->file_path = $visio_dir.'/'.$this->file_name.'.'.pathinfo($file['name'], PATHINFO_EXTENSION);
  52. $dir_name = $visio_dir.'/'.$this->file_name;
  53. // Create the directory.
  54. $this->base_work_dir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  55. $this->created_dir = create_unexisting_directory($_course, $_user['user_id'], api_get_session_id(), 0, 0, $this->base_work_dir, $dir_name);
  56. var_dump($this->file_name, $this->file_path, $this->base_work_dir, $this->created_dir);
  57. */
  58. $ppt2lp_host = api_get_setting('service_ppt2lp', 'host');
  59. if ($ppt2lp_host == 'localhost') {
  60. move_uploaded_file($file['tmp_name'], $this->base_work_dir.'/'.$this->file_path);
  61. //var_dump( $this->base_work_dir.$this->created_dir.$this->file_path);
  62. $perm = api_get_setting('permissions_for_new_files');
  63. if (IS_WINDOWS_OS) { // IS_WINDOWS_OS has been defined in main_api.lib.php
  64. $converter_path = str_replace('/', '\\', api_get_path(SYS_PATH) . 'main/inc/lib/ppt2png');
  65. $class_path = $converter_path . ';' . $converter_path . '/jodconverter-2.2.2.jar;' . $converter_path . '/jodconverter-cli-2.2.2.jar';
  66. //$cmd = 'java -cp "'.$class_path.'" DokeosConverter';
  67. $cmd = 'java -Dfile.encoding=UTF-8 -cp "' . $class_path . '" DokeosConverter';
  68. } else {
  69. $converter_path = api_get_path(SYS_PATH) . 'main/inc/lib/ppt2png';
  70. //$class_path = '-cp .:jodconverter-2.2.1.jar:jodconverter-cli-2.2.1.jar';
  71. $class_path = ' -Dfile.encoding=UTF-8 -cp .:jodconverter-2.2.2.jar:jodconverter-cli-2.2.2.jar';
  72. $cmd = 'cd ' . $converter_path . ' && java ' . $class_path . ' DokeosConverter';
  73. }
  74. $cmd .= ' -p ' . api_get_setting('service_ppt2lp', 'port');
  75. // Call to the function implemented by child.
  76. $cmd .= $this->add_command_parameters();
  77. // To allow openoffice to manipulate docs.
  78. @chmod($this->base_work_dir, 0777);
  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();
  84. $return = 0;
  85. $shell = exec($cmd, $files, $return);
  86. if ($return != 0) { // If the java application returns an error code.
  87. switch ($return) {
  88. // Can't connect to openoffice.
  89. case 1: $this->error = get_lang('CannotConnectToOpenOffice');
  90. break;
  91. // Conversion failed in openoffice.
  92. case 2: $this->error = get_lang('OogieConversionFailed');
  93. break;
  94. // Conversion can't be launch because command failed.
  95. case 255: $this->error = get_lang('OogieUnknownError');
  96. break;
  97. }
  98. DocumentManager::delete_document($_course, $this->created_dir, $this->base_work_dir);
  99. return false;
  100. }
  101. } else {
  102. // get result from webservices
  103. $result = $this->_get_remote_ppt2lp_files($file);
  104. $result = unserialize(base64_decode($result));
  105. // Save remote images to server
  106. chmod($this->base_work_dir.$this->created_dir, api_get_permissions_for_new_directories());
  107. if (!empty($result['images'])) {
  108. foreach ($result['images'] as $image => $img_data) {
  109. $image_path = $this->base_work_dir.$this->created_dir;
  110. @file_put_contents($image_path . '/' . $image, base64_decode($img_data));
  111. @chmod($image_path . '/' . $image, 0777);
  112. }
  113. }
  114. // files info
  115. $files = $result['files'];
  116. }
  117. if (!empty($files)) {
  118. // Create lp
  119. $this->lp_id = learnpath::add_lp($_course['id'], $this->file_name, '', 'guess', 'manual');
  120. // Call to the function implemented by child following action_after_conversion parameter.
  121. switch ($action_after_conversion) {
  122. case 'make_lp':
  123. $this->make_lp($files);
  124. break;
  125. case 'add_docs_to_visio':
  126. $this->add_docs_to_visio($files);
  127. break;
  128. }
  129. chmod($this->base_work_dir, api_get_permissions_for_new_directories());
  130. }
  131. return $this->first_item;
  132. }
  133. /**
  134. * Get images files from remote host (with webservices)
  135. * @param array current ppt file
  136. * @return array images files
  137. */
  138. private function _get_remote_ppt2lp_files($file) {
  139. require_once api_get_path(LIBRARY_PATH) . 'nusoap/nusoap.php';
  140. // host
  141. $ppt2lp_host = api_get_setting('service_ppt2lp', 'host');
  142. // secret key
  143. $secret_key = sha1(api_get_setting('service_ppt2lp', 'ftp_password'));
  144. // client
  145. $client = new nusoap_client($ppt2lp_host, true);
  146. $result = '';
  147. $err = $client->getError();
  148. if (!$err) {
  149. $file_data = base64_encode(file_get_contents($file['tmp_name']));
  150. $file_name = $file['name'];
  151. $service_ppt2lp_size = api_get_setting('service_ppt2lp', 'size');
  152. $params = array(
  153. 'secret_key' => $secret_key,
  154. 'file_data' => $file_data,
  155. 'file_name' => $file_name,
  156. 'service_ppt2lp_size' => $service_ppt2lp_size,
  157. );
  158. $result = $client->call('ws_convert_ppt', array('convert_ppt' => $params));
  159. } else {
  160. return false;
  161. }
  162. return $result;
  163. }
  164. abstract function make_lp();
  165. abstract function add_docs_to_visio();
  166. abstract function add_command_parameters();
  167. }