openoffice_document.class.php 8.4 KB

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