openoffice_document.class.php 16 KB

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