openoffice_document.class.php 16 KB

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