openoffice_document.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. $_course = api_get_course_info();
  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.'/'.api_replace_dangerous_char($file['name']);
  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 = api_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. {
  185. $_course = api_get_course_info();
  186. $ids = array();
  187. $originalPathInfo = pathinfo($originalPath);
  188. $convertedPathInfo = pathinfo($convertedPath);
  189. $this->base_work_dir = $originalPathInfo['dirname'];
  190. $this->file_path = $originalPathInfo['basename'];
  191. $this->created_dir = $convertedPathInfo['basename'];
  192. $ppt2lpHost = api_get_setting('service_ppt2lp', 'host');
  193. $permissionFile = api_get_permissions_for_new_files();
  194. $permissionFolder = api_get_permissions_for_new_directories();
  195. if (file_exists($this->base_work_dir . '/' . $this->created_dir)) {
  196. return $ids;
  197. }
  198. if ($ppt2lpHost == 'localhost') {
  199. if (IS_WINDOWS_OS) { // IS_WINDOWS_OS has been defined in main_api.lib.php
  200. $converterPath = str_replace('/', '\\', api_get_path(SYS_PATH) . 'main/inc/lib/ppt2png');
  201. $classPath = $converterPath . ';' . $converterPath . '/jodconverter-2.2.2.jar;' . $converterPath . '/jodconverter-cli-2.2.2.jar';
  202. $cmd = 'java -Dfile.encoding=UTF-8 -jar "' . $classPath . '/jodconverter-2.2.2.jar"';
  203. } else {
  204. $converterPath = api_get_path(SYS_PATH) . 'main/inc/lib/ppt2png';
  205. $classPath = ' -Dfile.encoding=UTF-8 -jar jodconverter-cli-2.2.2.jar';
  206. $cmd = 'cd ' . $converterPath . ' && java ' . $classPath . ' ';
  207. }
  208. $cmd .= ' -p ' . api_get_setting('service_ppt2lp', 'port');
  209. // Call to the function implemented by child.
  210. $cmd .= ' "' . $this->base_work_dir . '/' . $this->file_path . '" "' . $this->base_work_dir . '/' . $this->created_dir . '"';
  211. // To allow openoffice to manipulate docs.
  212. @chmod($this->base_work_dir, $permissionFolder);
  213. @chmod($this->base_work_dir . '/' . $this->file_path, $permissionFile);
  214. $locale = $this->original_locale; // TODO: Improve it because we're not sure this locale is present everywhere.
  215. putenv('LC_ALL=' . $locale);
  216. $files = array();
  217. $return = 0;
  218. $shell = exec($cmd, $files, $return);
  219. // TODO: Chown is not working, root keep user privileges, should be www-data
  220. @chown($this->base_work_dir . '/' . $this->created_dir, 'www-data');
  221. @chmod($this->base_work_dir . '/' . $this->created_dir, $permissionFile);
  222. if ($return != 0) { // If the java application returns an error code.
  223. switch ($return) {
  224. // Can't connect to openoffice.
  225. case 1: $this->error = get_lang('CannotConnectToOpenOffice');
  226. break;
  227. // Conversion failed in openoffice.
  228. case 2: $this->error = get_lang('OogieConversionFailed');
  229. break;
  230. // Conversion can't be launch because command failed.
  231. case 255: $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('ConvertedFromXToY'),
  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. }