openoffice_document.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. * @author Julio Montoya
  9. * @license GNU/GPL
  10. */
  11. /**
  12. * Defines the "OpenofficeDocument" child of class "learnpath"
  13. */
  14. abstract class OpenofficeDocument extends learnpath
  15. {
  16. public $first_item = 0;
  17. public $original_charset = 'utf-8';
  18. public $original_locale = 'en_US.UTF-8';
  19. /**
  20. * Class constructor. Based on the parent constructor.
  21. * @param string Course code
  22. * @param integer Learnpath ID in DB
  23. * @param integer User ID
  24. */
  25. public function __construct($course_code = null, $resource_id = null, $user_id = null)
  26. {
  27. if ($this->debug > 0) {
  28. error_log('In OpenofficeDocument::OpenofficeDocument()', 0);
  29. }
  30. if (!empty($course_code) && !empty($resource_id) && !empty($user_id)) {
  31. parent::__construct($course_code, $resource_id, $user_id);
  32. }
  33. }
  34. /**
  35. * @param string $file
  36. * @param string $action_after_conversion
  37. * @return bool|int
  38. */
  39. public function convert_document($file, $action_after_conversion = 'make_lp')
  40. {
  41. global $_course;
  42. $this->file_name = pathinfo($file['name'], PATHINFO_FILENAME);
  43. // Create the directory
  44. $result = $this->generate_lp_folder($_course, $this->file_name);
  45. // Create the directory
  46. $this->base_work_dir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  47. ///learning_path/ppt_dirname directory
  48. $this->created_dir = substr($result['dir'], 0, strlen($result['dir']) -1);
  49. $this->file_path = $this->created_dir.'/'.replace_dangerous_char($file['name'], 'strict');
  50. //var_dump($this->file_name, $this->file_path, $this->base_work_dir, $this->created_dir);
  51. /*
  52. * Original code
  53. global $_course, $_user, $_configuration;
  54. $this->file_name = (strrpos($file['name'], '.') > 0 ? substr($file['name'], 0, strrpos($file['name'], '.')) : $file['name']);
  55. $this->file_name = replace_dangerous_char($this->file_name, 'strict');
  56. $this->file_name = strtolower($this->file_name);
  57. $visio_dir = ($action_after_conversion == 'add_docs_to_visio') ? VIDEOCONF_UPLOAD_PATH : '';
  58. $this->file_path = $visio_dir.'/'.$this->file_name.'.'.pathinfo($file['name'], PATHINFO_EXTENSION);
  59. $dir_name = $visio_dir.'/'.$this->file_name;
  60. // Create the directory.
  61. $this->base_work_dir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  62. $this->created_dir = create_unexisting_directory($_course, $_user['user_id'], api_get_session_id(), 0, 0, $this->base_work_dir, $dir_name);
  63. var_dump($this->file_name, $this->file_path, $this->base_work_dir, $this->created_dir);
  64. */
  65. $ppt2lp_host = api_get_setting('service_ppt2lp', 'host');
  66. if ($ppt2lp_host == 'localhost') {
  67. move_uploaded_file($file['tmp_name'], $this->base_work_dir.'/'.$this->file_path);
  68. //var_dump( $this->base_work_dir.$this->created_dir.$this->file_path);
  69. $perm = api_get_setting('permissions_for_new_files');
  70. if (IS_WINDOWS_OS) { // IS_WINDOWS_OS has been defined in main_api.lib.php
  71. $converter_path = str_replace('/', '\\', api_get_path(SYS_PATH) . 'main/inc/lib/ppt2png');
  72. $class_path = $converter_path . ';' . $converter_path . '/jodconverter-2.2.2.jar;' . $converter_path . '/jodconverter-cli-2.2.2.jar';
  73. //$cmd = 'java -cp "'.$class_path.'" DokeosConverter';
  74. $cmd = 'java -Dfile.encoding=UTF-8 -cp "' . $class_path . '" DokeosConverter';
  75. } else {
  76. $converter_path = api_get_path(SYS_PATH) . 'main/inc/lib/ppt2png';
  77. //$class_path = '-cp .:jodconverter-2.2.1.jar:jodconverter-cli-2.2.1.jar';
  78. $class_path = ' -Dfile.encoding=UTF-8 -cp .:jodconverter-2.2.2.jar:jodconverter-cli-2.2.2.jar';
  79. $cmd = 'cd ' . $converter_path . ' && java ' . $class_path . ' DokeosConverter';
  80. }
  81. $cmd .= ' -p ' . api_get_setting('service_ppt2lp', 'port');
  82. // Call to the function implemented by child.
  83. $cmd .= $this->add_command_parameters();
  84. // To allow openoffice to manipulate docs.
  85. @chmod($this->base_work_dir, 0777);
  86. @chmod($this->base_work_dir.$this->created_dir, 0777);
  87. @chmod($this->base_work_dir.$this->file_path, 0777);
  88. $locale = $this->original_locale; // TODO: Improve it because we're not sure this locale is present everywhere.
  89. putenv('LC_ALL=' . $locale);
  90. $files = array();
  91. $return = 0;
  92. $shell = exec($cmd, $files, $return);
  93. if ($return != 0) { // If the java application returns an error code.
  94. switch ($return) {
  95. // Can't connect to openoffice.
  96. case 1: $this->error = get_lang('CannotConnectToOpenOffice');
  97. break;
  98. // Conversion failed in openoffice.
  99. case 2: $this->error = get_lang('OogieConversionFailed');
  100. break;
  101. // Conversion can't be launch because command failed.
  102. case 255: $this->error = get_lang('OogieUnknownError');
  103. break;
  104. }
  105. DocumentManager::delete_document($_course, $this->created_dir, $this->base_work_dir);
  106. return false;
  107. }
  108. } else {
  109. // get result from webservices
  110. $result = $this->_get_remote_ppt2lp_files($file);
  111. $result = unserialize($result);
  112. // Save remote images to server
  113. chmod($this->base_work_dir.$this->created_dir, api_get_permissions_for_new_directories());
  114. if (!empty($result['images'])) {
  115. foreach ($result['images'] as $image => $img_data) {
  116. $image_path = $this->base_work_dir.$this->created_dir;
  117. @file_put_contents($image_path . '/' . $image, base64_decode($img_data));
  118. @chmod($image_path . '/' . $image, 0777);
  119. }
  120. }
  121. // files info
  122. $files = $result['files'];
  123. }
  124. if (!empty($files)) {
  125. // Create lp
  126. $this->lp_id = learnpath::add_lp($_course['id'], $this->file_name, '', 'guess', 'manual');
  127. // Call to the function implemented by child following action_after_conversion parameter.
  128. switch ($action_after_conversion) {
  129. case 'make_lp':
  130. $this->make_lp($files);
  131. break;
  132. case 'add_docs_to_visio':
  133. $this->add_docs_to_visio($files);
  134. break;
  135. }
  136. chmod($this->base_work_dir, api_get_permissions_for_new_directories());
  137. }
  138. return $this->first_item;
  139. }
  140. /**
  141. * Get images files from remote host (with webservices)
  142. * @param array current ppt file
  143. * @return array images files
  144. */
  145. private function _get_remote_ppt2lp_files($file)
  146. {
  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. $options = array(
  153. 'location' => $ppt2lp_host,
  154. 'uri' => $ppt2lp_host,
  155. 'trace' => 1,
  156. 'exception' => 1,
  157. 'cache_wsdl' => WSDL_CACHE_NONE,
  158. );
  159. $client = new SoapClient(null, $options);
  160. $result = '';
  161. $file_data = base64_encode(file_get_contents($file['tmp_name']));
  162. $file_name = $file['name'];
  163. $service_ppt2lp_size = api_get_setting('service_ppt2lp', 'size');
  164. $params = array(
  165. 'secret_key' => $secret_key,
  166. 'file_data' => $file_data,
  167. 'file_name' => $file_name,
  168. 'service_ppt2lp_size' => $service_ppt2lp_size,
  169. );
  170. $result = $client->__call('wsConvertPpt', array('pptData' => $params));
  171. return $result;
  172. }
  173. abstract function make_lp();
  174. abstract function add_docs_to_visio();
  175. abstract function add_command_parameters();
  176. /**
  177. * Used to convert copied from document
  178. * @param string $originalPath
  179. * @param string $convertedPath
  180. * @param string $convertedTitle
  181. * @return bool
  182. */
  183. function convertCopyDocument($originalPath, $convertedPath, $convertedTitle){
  184. global $_course;
  185. $ids = array();
  186. $originalPathInfo = pathinfo($originalPath);
  187. $convertedPathInfo = pathinfo($convertedPath);
  188. $this->base_work_dir = $originalPathInfo['dirname'];
  189. $this->file_path = $originalPathInfo['basename'];
  190. $this->created_dir = $convertedPathInfo['basename'];
  191. $ppt2lpHost = api_get_setting('service_ppt2lp', 'host');
  192. $permissionFile = api_get_permissions_for_new_files();
  193. $permissionFolder = api_get_permissions_for_new_directories();
  194. if (file_exists($this->base_work_dir . '/' . $this->created_dir)) {
  195. return $ids;
  196. }
  197. if ($ppt2lpHost == 'localhost') {
  198. if (IS_WINDOWS_OS) { // IS_WINDOWS_OS has been defined in main_api.lib.php
  199. $converterPath = str_replace('/', '\\', api_get_path(SYS_PATH) . 'main/inc/lib/ppt2png');
  200. $classPath = $converterPath . ';' . $converterPath . '/jodconverter-2.2.2.jar;' . $converterPath . '/jodconverter-cli-2.2.2.jar';
  201. $cmd = 'java -Dfile.encoding=UTF-8 -jar "' . $classPath . '/jodconverter-2.2.2.jar"';
  202. } else {
  203. $converterPath = api_get_path(SYS_PATH) . 'main/inc/lib/ppt2png';
  204. $classPath = ' -Dfile.encoding=UTF-8 -jar jodconverter-cli-2.2.2.jar';
  205. $cmd = 'cd ' . $converterPath . ' && java ' . $classPath . ' ';
  206. }
  207. $cmd .= ' -p ' . api_get_setting('service_ppt2lp', 'port');
  208. // Call to the function implemented by child.
  209. $cmd .= ' "' . $this->base_work_dir . '/' . $this->file_path . '" "' . $this->base_work_dir . '/' . $this->created_dir . '"';
  210. // To allow openoffice to manipulate docs.
  211. @chmod($this->base_work_dir, $permissionFolder);
  212. @chmod($this->base_work_dir . '/' . $this->file_path, $permissionFile);
  213. $locale = $this->original_locale; // TODO: Improve it because we're not sure this locale is present everywhere.
  214. putenv('LC_ALL=' . $locale);
  215. $files = array();
  216. $return = 0;
  217. $shell = exec($cmd, $files, $return);
  218. // TODO: Chown is not working, root keep user privileges, should be www-data
  219. @chown($this->base_work_dir . '/' . $this->created_dir, 'www-data');
  220. @chmod($this->base_work_dir . '/' . $this->created_dir, $permissionFile);
  221. if ($return != 0) { // If the java application returns an error code.
  222. switch ($return) {
  223. // Can't connect to openoffice.
  224. case 1: $this->error = get_lang('CannotConnectToOpenOffice');
  225. break;
  226. // Conversion failed in openoffice.
  227. case 2: $this->error = get_lang('OogieConversionFailed');
  228. break;
  229. // Conversion can't be launch because command failed.
  230. case 255: $this->error = get_lang('OogieUnknownError');
  231. break;
  232. }
  233. DocumentManager::delete_document($_course, $this->created_dir, $this->base_work_dir);
  234. return false;
  235. }
  236. } else {
  237. /*
  238. * @TODO Create method to use webservice
  239. // get result from webservices
  240. $result = $this->_get_remote_ppt2lp_files($file);
  241. $result = unserialize(base64_decode($result));
  242. // Save remote images to server
  243. chmod($this->base_work_dir.$this->created_dir, api_get_permissions_for_new_directories());
  244. if (!empty($result['images'])) {
  245. foreach ($result['images'] as $image => $img_data) {
  246. $image_path = $this->base_work_dir.$this->created_dir;
  247. @file_put_contents($image_path . '/' . $image, base64_decode($img_data));
  248. @chmod($image_path . '/' . $image, 0777);
  249. }
  250. }
  251. // files info
  252. $files = $result['files'];
  253. */
  254. }
  255. if (file_exists($this->base_work_dir . '/' . $this->created_dir)) {
  256. // Register Files to Document tool
  257. $ids[] = add_document(
  258. $_course,
  259. '/' . $this->created_dir,
  260. 'file',
  261. filesize($this->base_work_dir . '/' . $this->created_dir),
  262. $convertedTitle,
  263. sprintf(
  264. get_lang('ConvertedFromXToY'),
  265. strtoupper($originalPathInfo['extension']),
  266. strtoupper($convertedPathInfo['extension'])
  267. ),
  268. 0,
  269. true,
  270. null,
  271. api_get_session_id()
  272. );
  273. chmod($this->base_work_dir, $permissionFolder);
  274. }
  275. return $ids;
  276. }
  277. }