additional_webservices.php 4.8 KB

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