additional_webservices.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.webservices
  5. *
  6. * @author Francis Gonzales
  7. */
  8. require_once __DIR__.'/../inc/global.inc.php';
  9. /**
  10. * Function to convert from ppt to png
  11. * This function is used from Chamilo Rapid Lesson.
  12. *
  13. * @param array $pptData
  14. *
  15. * @return string
  16. */
  17. function wsConvertPpt($pptData)
  18. {
  19. global $_configuration;
  20. $ip = trim($_SERVER['REMOTE_ADDR']);
  21. // If an IP filter array is defined in configuration.php,
  22. // check if this IP is allowed
  23. if (!empty($_configuration['ppt2lp_ip_filter'])) {
  24. if (!in_array($ip, $_configuration['ppt2lp_ip_filter'])) {
  25. return false;
  26. }
  27. }
  28. $fileData = $pptData['file_data'];
  29. $dataInfo = pathinfo($pptData['file_name']);
  30. $fileName = basename($pptData['file_name'], '.'.$dataInfo['extension']);
  31. $fullFileName = $pptData['file_name'];
  32. $size = $pptData['service_ppt2lp_size'];
  33. $w = '800';
  34. $h = '600';
  35. if (!empty($size)) {
  36. list($w, $h) = explode('x', $size);
  37. }
  38. $tempArchivePath = api_get_path(SYS_ARCHIVE_PATH);
  39. $tempPath = $tempArchivePath.'wsConvert/'.$fileName.'/';
  40. $tempPathNewFiles = $tempArchivePath.'wsConvert/'.$fileName.'-n/';
  41. $oldumask = umask(0);
  42. //$perms = api_get_permissions_for_new_directories();
  43. // Set permissions the most permissively possible: these files will
  44. // be deleted below and we need a parallel process to be able to write them
  45. $perms = api_get_permissions_for_new_directories();
  46. pptConverterDirectoriesCreate($tempPath, $tempPathNewFiles, $fileName, $perms);
  47. $file = base64_decode($fileData);
  48. file_put_contents($tempPath.$fullFileName, $file);
  49. $cmd = pptConverterGetCommandBaseParams();
  50. $cmd .= ' -w '.$w.' -h '.$h.' -d oogie "'.$tempPath.$fullFileName.'" "'.$tempPathNewFiles.$fileName.'.html"';
  51. //$perms = api_get_permissions_for_new_files();
  52. chmod($tempPathNewFiles.$fileName, $perms);
  53. $files = [];
  54. $return = 0;
  55. $shell = exec($cmd, $files, $return);
  56. umask($oldumask);
  57. if ($return === 0) {
  58. $images = [];
  59. if (is_array($files) && !empty($files)) {
  60. foreach ($files as $file) {
  61. $imageData = explode('||', $file);
  62. $images[$imageData[1]] = base64_encode(file_get_contents($tempPathNewFiles.$fileName.'/'.$imageData[1]));
  63. }
  64. }
  65. $data = [
  66. 'files' => $files,
  67. 'images' => $images,
  68. ];
  69. deleteDirectory($tempPath);
  70. deleteDirectory($tempPathNewFiles);
  71. return serialize($data);
  72. } else {
  73. deleteDirectory($tempPath);
  74. deleteDirectory($tempPathNewFiles);
  75. return false;
  76. }
  77. }
  78. /**
  79. * @param $directoryPath
  80. *
  81. * @return bool
  82. */
  83. function deleteDirectory($directoryPath)
  84. {
  85. $files = array_diff(scandir($directoryPath), ['.', '..']);
  86. foreach ($files as $file) {
  87. if (is_dir("$directoryPath/$file")) {
  88. deleteDirectory("$directoryPath/$file");
  89. } else {
  90. unlink("$directoryPath/$file");
  91. }
  92. }
  93. return rmdir($directoryPath);
  94. }
  95. /**
  96. * Helper function to create the directory structure for the PPT converter.
  97. *
  98. * @param string $tempPath
  99. * @param string $tempPathNewFiles
  100. * @param string $fileName
  101. * @param string $perms
  102. */
  103. function pptConverterDirectoriesCreate($tempPath, $tempPathNewFiles, $fileName, $perms)
  104. {
  105. if (!is_dir($tempPath)) {
  106. mkdir($tempPath, $perms, true);
  107. }
  108. if (!is_dir($tempPathNewFiles)) {
  109. mkdir($tempPathNewFiles, $perms, true);
  110. }
  111. if (!is_dir($tempPathNewFiles.$fileName)) {
  112. mkdir($tempPathNewFiles.$fileName, $perms, true);
  113. }
  114. }
  115. /**
  116. * Helper function to build the command line parameters for the converter.
  117. *
  118. * @return string $cmd
  119. */
  120. function pptConverterGetCommandBaseParams()
  121. {
  122. if (IS_WINDOWS_OS) { // IS_WINDOWS_OS has been defined in main_api.lib.php
  123. $converterPath = str_replace('/', '\\', api_get_path(SYS_PATH).'main/inc/lib/ppt2png');
  124. $classPath = $converterPath.';'.$converterPath.'/jodconverter-2.2.2.jar;'.$converterPath.'/jodconverter-cli-2.2.2.jar';
  125. $cmd = 'java -Dfile.encoding=UTF-8 -cp "'.$classPath.'" DokeosConverter';
  126. } else {
  127. $converterPath = api_get_path(SYS_PATH).'main/inc/lib/ppt2png';
  128. $classPath = ' -Dfile.encoding=UTF-8 -cp .:jodconverter-2.2.2.jar:jodconverter-cli-2.2.2.jar';
  129. $cmd = 'cd '.$converterPath.' && java '.$classPath.' DokeosConverter';
  130. }
  131. $cmd .= ' -p '.api_get_setting('service_ppt2lp', 'port');
  132. return $cmd;
  133. }
  134. $webPath = api_get_path(WEB_PATH);
  135. $webCodePath = api_get_path(WEB_CODE_PATH);
  136. $options = [
  137. 'uri' => $webPath,
  138. 'location' => $webCodePath.'webservices/additional_webservices.php',
  139. ];
  140. $soapServer = new SoapServer(null, $options);
  141. $soapServer->addFunction('wsConvertPpt');
  142. $soapServer->handle();