openoffice_document.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. // Can't connect to openoffice.
  111. case 1: $this->error = get_lang('CannotConnectToOpenOffice');
  112. break;
  113. // Conversion failed in openoffice.
  114. case 2: $this->error = get_lang('OogieConversionFailed');
  115. break;
  116. // Conversion can't be launch because command failed.
  117. case 255: $this->error = get_lang('OogieUnknownError');
  118. break;
  119. }
  120. DocumentManager::delete_document($_course, $this->created_dir, $this->base_work_dir);
  121. return false;
  122. }
  123. } else {
  124. // get result from webservices
  125. $result = $this->_get_remote_ppt2lp_files($file, $size);
  126. $result = unserialize($result);
  127. // Save remote images to server
  128. chmod($this->base_work_dir . $this->created_dir, api_get_permissions_for_new_directories());
  129. if (!empty($result['images'])) {
  130. foreach ($result['images'] as $image => $img_data) {
  131. $image_path = $this->base_work_dir.$this->created_dir;
  132. @file_put_contents($image_path . '/' . $image, base64_decode($img_data));
  133. @chmod($image_path . '/' . $image, 0777);
  134. }
  135. }
  136. // files info
  137. $files = $result['files'];
  138. }
  139. if (!empty($files)) {
  140. // Create lp
  141. $this->lp_id = learnpath::add_lp($_course['id'], $this->file_name, '', 'guess', 'manual');
  142. // make sure we have a course code available for later
  143. $this->cc = $_course['id'];
  144. $this->course_info = $_course;
  145. // Call to the function implemented by child following action_after_conversion parameter.
  146. switch ($action_after_conversion) {
  147. case 'make_lp':
  148. $this->make_lp($files);
  149. break;
  150. case 'add_docs_to_visio':
  151. $this->add_docs_to_visio($files);
  152. break;
  153. }
  154. chmod($this->base_work_dir, api_get_permissions_for_new_directories());
  155. }
  156. return $this->first_item;
  157. }
  158. /**
  159. * Get images files from remote host (with webservices)
  160. * @param array $file current ppt file details
  161. * @param string $size The expected final size of the rendered slides
  162. * @return array images files
  163. */
  164. private function _get_remote_ppt2lp_files($file, $size = null)
  165. {
  166. // host
  167. $ppt2lp_host = api_get_setting('service_ppt2lp', 'host');
  168. // secret key
  169. $secret_key = sha1(api_get_setting('service_ppt2lp', 'ftp_password'));
  170. // client
  171. $options = array(
  172. 'location' => $ppt2lp_host,
  173. 'uri' => $ppt2lp_host,
  174. 'trace' => 1,
  175. 'exception' => 1,
  176. 'cache_wsdl' => WSDL_CACHE_NONE,
  177. );
  178. $client = new SoapClient(null, $options);
  179. $result = '';
  180. $file_data = base64_encode(file_get_contents($file['tmp_name']));
  181. $file_name = $file['name'];
  182. if (empty($size)) {
  183. $size = api_get_setting('service_ppt2lp', 'size');
  184. }
  185. $params = array(
  186. 'secret_key' => $secret_key,
  187. 'file_data' => $file_data,
  188. 'file_name' => $file_name,
  189. 'service_ppt2lp_size' => $size,
  190. );
  191. $result = $client->__call('wsConvertPpt', array('pptData' => $params));
  192. return $result;
  193. }
  194. abstract function make_lp();
  195. abstract function add_docs_to_visio();
  196. abstract function add_command_parameters();
  197. /**
  198. * Used to convert copied from document
  199. * @param string $originalPath
  200. * @param string $convertedPath
  201. * @param string $convertedTitle
  202. * @return bool
  203. */
  204. function convertCopyDocument($originalPath, $convertedPath, $convertedTitle)
  205. {
  206. $_course = api_get_course_info();
  207. $ids = array();
  208. $originalPathInfo = pathinfo($originalPath);
  209. $convertedPathInfo = pathinfo($convertedPath);
  210. $this->base_work_dir = $originalPathInfo['dirname'];
  211. $this->file_path = $originalPathInfo['basename'];
  212. $this->created_dir = $convertedPathInfo['basename'];
  213. $ppt2lpHost = api_get_setting('service_ppt2lp', 'host');
  214. $permissionFile = api_get_permissions_for_new_files();
  215. $permissionFolder = api_get_permissions_for_new_directories();
  216. if (file_exists($this->base_work_dir . '/' . $this->created_dir)) {
  217. return $ids;
  218. }
  219. if ($ppt2lpHost == 'localhost') {
  220. if (IS_WINDOWS_OS) { // IS_WINDOWS_OS has been defined in main_api.lib.php
  221. $converterPath = str_replace('/', '\\', api_get_path(SYS_PATH) . 'main/inc/lib/ppt2png');
  222. $classPath = $converterPath . ';' . $converterPath . '/jodconverter-2.2.2.jar;' . $converterPath . '/jodconverter-cli-2.2.2.jar';
  223. $cmd = 'java -Dfile.encoding=UTF-8 -jar "' . $classPath . '/jodconverter-2.2.2.jar"';
  224. } else {
  225. $converterPath = api_get_path(SYS_PATH) . 'main/inc/lib/ppt2png';
  226. $classPath = ' -Dfile.encoding=UTF-8 -jar jodconverter-cli-2.2.2.jar';
  227. $cmd = 'cd ' . $converterPath . ' && java ' . $classPath . ' ';
  228. }
  229. $cmd .= ' -p ' . api_get_setting('service_ppt2lp', 'port');
  230. // Call to the function implemented by child.
  231. $cmd .= ' "' . $this->base_work_dir . '/' . $this->file_path . '" "' . $this->base_work_dir . '/' . $this->created_dir . '"';
  232. // To allow openoffice to manipulate docs.
  233. @chmod($this->base_work_dir, $permissionFolder);
  234. @chmod($this->base_work_dir . '/' . $this->file_path, $permissionFile);
  235. $locale = $this->original_locale; // TODO: Improve it because we're not sure this locale is present everywhere.
  236. putenv('LC_ALL=' . $locale);
  237. $files = array();
  238. $return = 0;
  239. $shell = exec($cmd, $files, $return);
  240. // TODO: Chown is not working, root keep user privileges, should be www-data
  241. @chown($this->base_work_dir . '/' . $this->created_dir, 'www-data');
  242. @chmod($this->base_work_dir . '/' . $this->created_dir, $permissionFile);
  243. if ($return != 0) { // If the java application returns an error code.
  244. switch ($return) {
  245. // Can't connect to openoffice.
  246. case 1: $this->error = get_lang('CannotConnectToOpenOffice');
  247. break;
  248. // Conversion failed in openoffice.
  249. case 2: $this->error = get_lang('OogieConversionFailed');
  250. break;
  251. // Conversion can't be launch because command failed.
  252. case 255: $this->error = get_lang('OogieUnknownError');
  253. break;
  254. }
  255. DocumentManager::delete_document($_course, $this->created_dir, $this->base_work_dir);
  256. return false;
  257. }
  258. } else {
  259. /*
  260. * @TODO Create method to use webservice
  261. // get result from webservices
  262. $result = $this->_get_remote_ppt2lp_files($file);
  263. $result = unserialize(base64_decode($result));
  264. // Save remote images to server
  265. chmod($this->base_work_dir.$this->created_dir, api_get_permissions_for_new_directories());
  266. if (!empty($result['images'])) {
  267. foreach ($result['images'] as $image => $img_data) {
  268. $image_path = $this->base_work_dir.$this->created_dir;
  269. @file_put_contents($image_path . '/' . $image, base64_decode($img_data));
  270. @chmod($image_path . '/' . $image, 0777);
  271. }
  272. }
  273. // files info
  274. $files = $result['files'];
  275. */
  276. }
  277. if (file_exists($this->base_work_dir . '/' . $this->created_dir)) {
  278. // Register Files to Document tool
  279. $ids[] = add_document(
  280. $_course,
  281. '/' . $this->created_dir,
  282. 'file',
  283. filesize($this->base_work_dir . '/' . $this->created_dir),
  284. $convertedTitle,
  285. sprintf(
  286. get_lang('FileConvertedFromXToY'),
  287. strtoupper($originalPathInfo['extension']),
  288. strtoupper($convertedPathInfo['extension'])
  289. ),
  290. 0,
  291. true,
  292. null,
  293. api_get_session_id()
  294. );
  295. chmod($this->base_work_dir, $permissionFolder);
  296. }
  297. return $ids;
  298. }
  299. }