openoffice_document.class.php 14 KB

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