additional_webservices.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.webservices
  5. * @author Francis Gonzales
  6. */
  7. require_once '../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. $fileData = $pptData['file_data'];
  19. $dataInfo = pathinfo($pptData['file_name']);
  20. $fileName = basename($pptData['file_name'], '.' . $dataInfo['extension']);
  21. $fullFileName = $pptData['file_name'];
  22. $size = $pptData['service_ppt2lp_size'];
  23. $w = '800';
  24. $h = '600';
  25. if (!empty($size)) {
  26. list($w, $h) = explode('x', $size);
  27. }
  28. $tempArchivePath = api_get_path(SYS_ARCHIVE_PATH);
  29. $tempPath = $tempArchivePath . 'wsConvert/' . $fileName . '/';
  30. $tempPathNewFiles = $tempArchivePath . 'wsConvert/' . $fileName . '-n/';
  31. $oldumask = umask(0);
  32. $perms = api_get_permissions_for_new_directories();
  33. pptConverterDirectoriesCreate($tempPath, $tempPathNewFiles, $fileName, $perms);
  34. $file = base64_decode($fileData);
  35. file_put_contents($tempPath . $fullFileName, $file);
  36. $cmd = pptConverterGetCommandBaseParams();
  37. $cmd .= ' -w ' . $w . ' -h ' . $h . ' -d oogie "' . $tempPath . $fullFileName.'" "' . $tempPathNewFiles . $fileName . '.html"';
  38. chmod($tempPathNewFiles . $fileName, $perms);
  39. $files = array();
  40. $return = 0;
  41. $shell = exec($cmd, $files, $return);
  42. umask($oldumask);
  43. if ($return === 0) {
  44. $images = array();
  45. if (is_array($files) && !empty($files)) {
  46. foreach ($files as $file) {
  47. $imageData = explode('||', $file);
  48. $images[$imageData[1]] = base64_encode(file_get_contents($tempPathNewFiles . $fileName . '/' . $imageData[1]));
  49. }
  50. }
  51. $data = array(
  52. 'files' => $files,
  53. 'images' => $images
  54. );
  55. deleteDirectory($tempPath);
  56. deleteDirectory($tempPathNewFiles);
  57. return serialize($data);
  58. } else {
  59. deleteDirectory($tempPath);
  60. deleteDirectory($tempPathNewFiles);
  61. return false;
  62. }
  63. }
  64. /**
  65. * @param $directoryPath
  66. * @return bool
  67. */
  68. function deleteDirectory($directoryPath)
  69. {
  70. $files = array_diff(scandir($directoryPath), array('.','..'));
  71. foreach ($files as $file) {
  72. if (is_dir("$directoryPath/$file")) {
  73. deleteDirectory("$directoryPath/$file");
  74. } else {
  75. unlink("$directoryPath/$file");
  76. }
  77. }
  78. return rmdir($directoryPath);
  79. }
  80. /**
  81. * Helper function to create the directory structure for the PPT converter
  82. * @param string $tempPath
  83. * @param string $tempPathNewFiles
  84. * @param string $fileName
  85. * @param string $perms
  86. * @return void
  87. */
  88. function pptConverterDirectoriesCreate($tempPath, $tempPathNewFiles, $fileName, $perms)
  89. {
  90. if (!is_dir($tempPath)) {
  91. mkdir($tempPath, $perms, true);
  92. }
  93. if (!is_dir($tempPathNewFiles)) {
  94. mkdir($tempPathNewFiles, $perms, true);
  95. }
  96. if (!is_dir($tempPathNewFiles . $fileName)) {
  97. mkdir($tempPathNewFiles . $fileName, $perms, true);
  98. }
  99. }
  100. /**
  101. * Helper function to build the command line parameters for the converter
  102. * @return string $cmd
  103. */
  104. function pptConverterGetCommandBaseParams()
  105. {
  106. if (IS_WINDOWS_OS) { // IS_WINDOWS_OS has been defined in main_api.lib.php
  107. $converterPath = str_replace('/', '\\', api_get_path(SYS_PATH) . 'main/inc/lib/ppt2png');
  108. $classPath = $converterPath . ';' . $converterPath . '/jodconverter-2.2.2.jar;' . $converterPath . '/jodconverter-cli-2.2.2.jar';
  109. $cmd = 'java -Dfile.encoding=UTF-8 -cp "' . $classPath . '" DokeosConverter';
  110. } else {
  111. $converterPath = api_get_path(SYS_PATH) . 'main/inc/lib/ppt2png';
  112. $classPath = ' -Dfile.encoding=UTF-8 -cp .:jodconverter-2.2.2.jar:jodconverter-cli-2.2.2.jar';
  113. $cmd = 'cd ' . $converterPath . ' && java ' . $classPath . ' DokeosConverter';
  114. }
  115. $cmd .= ' -p ' . api_get_setting('service_ppt2lp', 'port');
  116. return $cmd;
  117. }
  118. $webPath = api_get_path(WEB_PATH);
  119. $webCodePath = api_get_path(WEB_CODE_PATH);
  120. $options = array(
  121. 'uri' => $webPath,
  122. 'location' => $webCodePath . 'webservices/additional_webservices.php'
  123. );
  124. $soapServer = new SoapServer(NULL, $options);
  125. $soapServer->addFunction('wsConvertPpt');
  126. $soapServer->handle();