openoffice_document.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. *
  7. * @package chamilo.learnpath
  8. *
  9. * @author Eric Marguin <eric.marguin@dokeos.com>
  10. * @author Julio Montoya
  11. * @license GNU/GPL
  12. */
  13. /**
  14. * Defines the "OpenofficeDocument" child of class "learnpath".
  15. */
  16. abstract class OpenofficeDocument extends learnpath
  17. {
  18. public $first_item = 0;
  19. public $original_charset = 'utf-8';
  20. public $original_locale = 'en_US.UTF-8';
  21. public $slide_width;
  22. public $slide_height;
  23. /**
  24. * Class constructor. Based on the parent constructor.
  25. *
  26. * @param string Course code
  27. * @param int Learnpath ID in DB
  28. * @param int User ID
  29. */
  30. public function __construct($course_code = null, $resource_id = null, $user_id = null)
  31. {
  32. if ($this->debug > 0) {
  33. error_log('In OpenofficeDocument::OpenofficeDocument()', 0);
  34. }
  35. if (!empty($course_code) && !empty($resource_id) && !empty($user_id)) {
  36. parent::__construct($course_code, $resource_id, $user_id);
  37. }
  38. }
  39. /**
  40. * Calls the LibreOffice server to convert the PPTs to a set of HTML + png files in a learning path.
  41. *
  42. * @param string $file
  43. * @param string $action_after_conversion
  44. * @param string $size The size to which we want the slides to be generated
  45. *
  46. * @return bool|int
  47. */
  48. public function convert_document($file, $action_after_conversion = 'make_lp', $size = null)
  49. {
  50. $_course = api_get_course_info();
  51. $this->file_name = pathinfo($file['name'], PATHINFO_FILENAME);
  52. // Create the directory
  53. $result = $this->generate_lp_folder($_course, $this->file_name);
  54. // Create the directory
  55. $this->base_work_dir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  56. ///learning_path/ppt_dirname directory
  57. $this->created_dir = $result['dir'];
  58. if (substr($this->created_dir, -1, 1) == '/') {
  59. $this->file_path = $this->created_dir.api_replace_dangerous_char($file['name']);
  60. } else {
  61. $this->file_path = $this->created_dir.'/'.api_replace_dangerous_char($file['name']);
  62. }
  63. $dirMode = api_get_permissions_for_new_directories();
  64. $fileMode = api_get_permissions_for_new_files();
  65. //var_dump($this->file_name, $this->file_path, $this->base_work_dir, $this->created_dir);
  66. /*
  67. * Original code
  68. global $_course, $_user, $_configuration;
  69. $this->file_name = (strrpos($file['name'], '.') > 0 ? substr($file['name'], 0, strrpos($file['name'], '.')) : $file['name']);
  70. $this->file_name = api_replace_dangerous_char($this->file_name, 'strict');
  71. $this->file_name = strtolower($this->file_name);
  72. $visio_dir = ($action_after_conversion == 'add_docs_to_visio') ? VIDEOCONF_UPLOAD_PATH : '';
  73. $this->file_path = $visio_dir.'/'.$this->file_name.'.'.pathinfo($file['name'], PATHINFO_EXTENSION);
  74. $dir_name = $visio_dir.'/'.$this->file_name;
  75. // Create the directory.
  76. $this->base_work_dir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  77. $this->created_dir = create_unexisting_directory($_course, $_user['user_id'], api_get_session_id(), 0, 0, $this->base_work_dir, $dir_name);
  78. var_dump($this->file_name, $this->file_path, $this->base_work_dir, $this->created_dir);
  79. */
  80. if (!empty($size)) {
  81. list($w, $h) = explode('x', $size);
  82. if (!empty($w) && !empty($h)) {
  83. $this->slide_width = $w;
  84. $this->slide_height = $h;
  85. }
  86. }
  87. $ppt2lp_host = api_get_setting('service_ppt2lp', 'host');
  88. if ($ppt2lp_host == 'localhost') {
  89. move_uploaded_file($file['tmp_name'], $this->base_work_dir.'/'.$this->file_path);
  90. //var_dump( $this->base_work_dir.$this->created_dir.$this->file_path);
  91. $perm = api_get_setting('permissions_for_new_files');
  92. if (IS_WINDOWS_OS) { // IS_WINDOWS_OS has been defined in main_api.lib.php
  93. $converter_path = str_replace('/', '\\', api_get_path(SYS_PATH).'main/inc/lib/ppt2png');
  94. $class_path = $converter_path.';'.$converter_path.'/jodconverter-2.2.2.jar;'.$converter_path.'/jodconverter-cli-2.2.2.jar';
  95. //$cmd = 'java -cp "'.$class_path.'" DokeosConverter';
  96. $cmd = 'java -Dfile.encoding=UTF-8 -cp "'.$class_path.'" DokeosConverter';
  97. } else {
  98. $converter_path = api_get_path(SYS_PATH).'main/inc/lib/ppt2png';
  99. //$class_path = '-cp .:jodconverter-2.2.1.jar:jodconverter-cli-2.2.1.jar';
  100. $class_path = ' -Dfile.encoding=UTF-8 -cp .:jodconverter-2.2.2.jar:jodconverter-cli-2.2.2.jar';
  101. $cmd = 'cd '.$converter_path.' && java '.$class_path.' DokeosConverter';
  102. }
  103. $cmd .= ' -p '.api_get_setting('service_ppt2lp', 'port');
  104. // Call to the function implemented by child.
  105. $cmd .= $this->add_command_parameters();
  106. // To allow openoffice to manipulate docs.
  107. @chmod($this->base_work_dir, $dirMode);
  108. @chmod($this->base_work_dir.$this->created_dir, $dirMode);
  109. @chmod($this->base_work_dir.$this->file_path, $fileMode);
  110. $locale = $this->original_locale; // TODO: Improve it because we're not sure this locale is present everywhere.
  111. putenv('LC_ALL='.$locale);
  112. $files = [];
  113. $return = 0;
  114. $shell = exec($cmd, $files, $return);
  115. if ($return != 0) { // If the java application returns an error code.
  116. switch ($return) {
  117. case 1:
  118. // Can't connect to openoffice.
  119. $this->error = get_lang('CannotConnectToOpenOffice');
  120. break;
  121. case 2:
  122. // Conversion failed in openoffice.
  123. $this->error = get_lang('OogieConversionFailed');
  124. break;
  125. case 255:
  126. // Conversion can't be launch because command failed.
  127. $this->error = get_lang('OogieUnknownError');
  128. break;
  129. }
  130. DocumentManager::delete_document($_course, $this->created_dir, $this->base_work_dir);
  131. return false;
  132. }
  133. } else {
  134. // get result from webservices
  135. $result = $this->_get_remote_ppt2lp_files($file, $size);
  136. $result = unserialize($result);
  137. // Save remote images to server
  138. chmod($this->base_work_dir.$this->created_dir, api_get_permissions_for_new_directories());
  139. if (!empty($result['images'])) {
  140. foreach ($result['images'] as $image => $img_data) {
  141. $image_path = $this->base_work_dir.$this->created_dir;
  142. @file_put_contents($image_path.'/'.$image, base64_decode($img_data));
  143. @chmod($image_path.'/'.$image, $fileMode);
  144. }
  145. }
  146. // files info
  147. $files = $result['files'];
  148. }
  149. if (!empty($files)) {
  150. // Create lp
  151. $this->lp_id = learnpath::add_lp($_course['id'], $this->file_name, '', 'guess', 'manual');
  152. // make sure we have a course code available for later
  153. $this->cc = $_course['id'];
  154. $this->course_info = $_course;
  155. // Call to the function implemented by child following action_after_conversion parameter.
  156. switch ($action_after_conversion) {
  157. case 'make_lp':
  158. $this->make_lp($files);
  159. break;
  160. case 'add_docs_to_visio':
  161. $this->add_docs_to_visio($files);
  162. break;
  163. }
  164. chmod($this->base_work_dir, api_get_permissions_for_new_directories());
  165. }
  166. return $this->first_item;
  167. }
  168. abstract public function make_lp();
  169. abstract public function add_docs_to_visio();
  170. abstract public function add_command_parameters();
  171. /**
  172. * Used to convert copied from document.
  173. *
  174. * @param string $originalPath
  175. * @param string $convertedPath
  176. * @param string $convertedTitle
  177. *
  178. * @return bool
  179. */
  180. public function convertCopyDocument($originalPath, $convertedPath, $convertedTitle)
  181. {
  182. $_course = api_get_course_info();
  183. $ids = [];
  184. $originalPathInfo = pathinfo($originalPath);
  185. $convertedPathInfo = pathinfo($convertedPath);
  186. $this->base_work_dir = $originalPathInfo['dirname'];
  187. $this->file_path = $originalPathInfo['basename'];
  188. $this->created_dir = $convertedPathInfo['basename'];
  189. $ppt2lpHost = api_get_setting('service_ppt2lp', 'host');
  190. $permissionFile = api_get_permissions_for_new_files();
  191. $permissionFolder = api_get_permissions_for_new_directories();
  192. if (file_exists($this->base_work_dir.'/'.$this->created_dir)) {
  193. return $ids;
  194. }
  195. if ($ppt2lpHost == 'localhost') {
  196. if (IS_WINDOWS_OS) { // IS_WINDOWS_OS has been defined in main_api.lib.php
  197. $converterPath = str_replace('/', '\\', api_get_path(SYS_PATH).'main/inc/lib/ppt2png');
  198. $classPath = $converterPath.';'.$converterPath.'/jodconverter-2.2.2.jar;'.$converterPath.'/jodconverter-cli-2.2.2.jar';
  199. $cmd = 'java -Dfile.encoding=UTF-8 -jar "'.$classPath.'/jodconverter-2.2.2.jar"';
  200. } else {
  201. $converterPath = api_get_path(SYS_PATH).'main/inc/lib/ppt2png';
  202. $classPath = ' -Dfile.encoding=UTF-8 -jar jodconverter-cli-2.2.2.jar';
  203. $cmd = 'cd '.$converterPath.' && java '.$classPath.' ';
  204. }
  205. $cmd .= ' -p '.api_get_setting('service_ppt2lp', 'port');
  206. // Call to the function implemented by child.
  207. $cmd .= ' "'.$this->base_work_dir.'/'.$this->file_path.'" "'.$this->base_work_dir.'/'.$this->created_dir.'"';
  208. // To allow openoffice to manipulate docs.
  209. @chmod($this->base_work_dir, $permissionFolder);
  210. @chmod($this->base_work_dir.'/'.$this->file_path, $permissionFile);
  211. $locale = $this->original_locale; // TODO: Improve it because we're not sure this locale is present everywhere.
  212. putenv('LC_ALL='.$locale);
  213. $files = [];
  214. $return = 0;
  215. $shell = exec($cmd, $files, $return);
  216. // TODO: Chown is not working, root keep user privileges, should be www-data
  217. @chown($this->base_work_dir.'/'.$this->created_dir, 'www-data');
  218. @chmod($this->base_work_dir.'/'.$this->created_dir, $permissionFile);
  219. if ($return != 0) { // If the java application returns an error code.
  220. switch ($return) {
  221. case 1:
  222. // Can't connect to openoffice.
  223. $this->error = get_lang('CannotConnectToOpenOffice');
  224. break;
  225. case 2:
  226. // Conversion failed in openoffice.
  227. $this->error = get_lang('OogieConversionFailed');
  228. break;
  229. case 255:
  230. // Conversion can't be launch because command failed.
  231. $this->error = get_lang('OogieUnknownError');
  232. break;
  233. }
  234. DocumentManager::delete_document($_course, $this->created_dir, $this->base_work_dir);
  235. return false;
  236. }
  237. } else {
  238. /*
  239. * @TODO Create method to use webservice
  240. // get result from webservices
  241. $result = $this->_get_remote_ppt2lp_files($file);
  242. $result = unserialize(base64_decode($result));
  243. // Save remote images to server
  244. chmod($this->base_work_dir.$this->created_dir, api_get_permissions_for_new_directories());
  245. if (!empty($result['images'])) {
  246. foreach ($result['images'] as $image => $img_data) {
  247. $image_path = $this->base_work_dir.$this->created_dir;
  248. @file_put_contents($image_path . '/' . $image, base64_decode($img_data));
  249. @chmod($image_path . '/' . $image, 0777);
  250. }
  251. }
  252. // files info
  253. $files = $result['files'];
  254. */
  255. }
  256. if (file_exists($this->base_work_dir.'/'.$this->created_dir)) {
  257. // Register Files to Document tool
  258. $ids[] = add_document(
  259. $_course,
  260. '/'.$this->created_dir,
  261. 'file',
  262. filesize($this->base_work_dir.'/'.$this->created_dir),
  263. $convertedTitle,
  264. sprintf(
  265. get_lang('FileConvertedFromXToY'),
  266. strtoupper($originalPathInfo['extension']),
  267. strtoupper($convertedPathInfo['extension'])
  268. ),
  269. 0,
  270. true,
  271. null,
  272. api_get_session_id()
  273. );
  274. chmod($this->base_work_dir, $permissionFolder);
  275. }
  276. return $ids;
  277. }
  278. /**
  279. * Get images files from remote host (with webservices).
  280. *
  281. * @param array $file current ppt file details
  282. * @param string $size The expected final size of the rendered slides
  283. *
  284. * @return array images files
  285. */
  286. private function _get_remote_ppt2lp_files($file, $size = null)
  287. {
  288. // host
  289. $ppt2lp_host = api_get_setting('service_ppt2lp', 'host');
  290. // SOAP URI (just the host)
  291. $matches = [];
  292. $uri = '';
  293. $result = preg_match('/^([a-zA-Z0-9]*):\/\/([^\/]*)\//', $ppt2lp_host, $matches);
  294. if ($result) {
  295. $uri = $matches[1].'://'.$matches[2].'/';
  296. } else {
  297. $uri = $ppt2lp_host;
  298. }
  299. // secret key
  300. $secret_key = sha1(api_get_setting('service_ppt2lp', 'ftp_password'));
  301. // client
  302. $options = [
  303. 'location' => $ppt2lp_host,
  304. 'uri' => $uri,
  305. 'trace' => 1,
  306. 'exceptions' => true,
  307. 'cache_wsdl' => WSDL_CACHE_NONE,
  308. 'keep_alive' => false,
  309. 'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
  310. 'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | 9,
  311. ];
  312. if (substr($ppt2lp_host, 0, 5) === 'https') {
  313. $options['ssl_method'] = SOAP_SSL_METHOD_TLS;
  314. // If using SSL, please note that *not* supporting the SSLv2
  315. // (broken in terms of security), the server tends to generate
  316. // the following issue:
  317. // SoapClient::__doRequest(): SSL: Connection reset by peer
  318. }
  319. $client = new SoapClient(null, $options);
  320. $result = '';
  321. $file_data = base64_encode(file_get_contents($file['tmp_name']));
  322. $file_name = $file['name'];
  323. if (empty($size)) {
  324. $size = api_get_setting('service_ppt2lp', 'size');
  325. }
  326. $params = [
  327. 'secret_key' => $secret_key,
  328. 'file_data' => $file_data,
  329. 'file_name' => $file_name,
  330. 'service_ppt2lp_size' => $size,
  331. ];
  332. try {
  333. //error_log('['.time().'] Calling wsConvertPpt webservice on ' . $ppt2lp_host);
  334. $result = $client->__call('wsConvertPpt', ['pptData' => $params]);
  335. } catch (Exception $e) {
  336. error_log('['.time().'] Chamilo SOAP call error: '.$e->getMessage());
  337. }
  338. // Make sure we destroy the SOAP client as it may generate SSL connection
  339. // binding issue (if using SSL)
  340. unset($client);
  341. return $result;
  342. }
  343. }