You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
6.6 KiB

  1. <?php
  2. // Project: Web Reference Database (refbase) <http://www.refbase.net>
  3. // Copyright: Matthias Steffens <mailto:refbase@extracts.de> and the file's
  4. // original author(s).
  5. //
  6. // This code is distributed in the hope that it will be useful,
  7. // but WITHOUT ANY WARRANTY. Please see the GNU General Public
  8. // License for more details.
  9. //
  10. // File: ./includes/execute.inc.php
  11. // Repository: $HeadURL: file:///svn/p/refbase/code/branches/bleeding-edge/includes/execute.inc.php $
  12. // Author(s): Richard Karnesky <mailto:karnesky@gmail.com> and
  13. // Matthias Steffens <mailto:refbase@extracts.de>
  14. //
  15. // Created: 16-Dec-05, 18:00
  16. // Modified: $Date: 2012-11-12 22:43:32 +0000 (Mon, 12 Nov 2012) $
  17. // $Author: karnesky $
  18. // $Revision: 1366 $
  19. // This file contains functions that deal with execution of shell commands and provides
  20. // fixes for 'exec()' on certain win32 systems (based on rivera at spamjoy dot unr dot edu's
  21. // 'wind_exec()' function <http://php.net/function.exec>).
  22. // Note: Since the 'exec()' function is used, some things may not work if
  23. //'safe_mode' is set to 'On' in your 'php.ini' file. If you need or want to
  24. // keep 'safe_mode=ON' then you'll need to put the programs within the
  25. // directory that's specified in 'safe_mode_exec_dir'.
  26. // --------------------------------------------------------------------
  27. // Import records using the bibutils program given in '$program'
  28. function importBibutils($sourceText, $program)
  29. {
  30. global $contentTypeCharset; // defined in 'ini.inc.php'
  31. // Get the absolute path for the bibutils package:
  32. // (function 'getExternalUtilityPath()' is defined in 'include.inc.php')
  33. $bibutilsPath = getExternalUtilityPath("bibutils");
  34. // Write the source data to a temporary file:
  35. $tempFile = writeToTempFile($sourceText);
  36. // Set input and output encoding:
  37. if ($contentTypeCharset != "UTF-8")
  38. {
  39. $inputEncodingArg = " -i iso8859_1";
  40. $outputEncodingArg = " -o iso8859_1";
  41. }
  42. else
  43. {
  44. $inputEncodingArg = " -i utf8";
  45. $outputEncodingArg = " -o utf8";
  46. }
  47. // Pass this temp file to the bibutils utility for conversion:
  48. $outputFile = convertBibutils($bibutilsPath, $tempFile, $program, $inputEncodingArg, $outputEncodingArg);
  49. unlink($tempFile);
  50. // Read the resulting output file and return the converted data:
  51. $resultString = readFromFile($outputFile);
  52. unlink($outputFile);
  53. return $resultString;
  54. }
  55. // --------------------------------------------------------------------
  56. // Export records using the bibutils program given in '$program'
  57. function exportBibutils($result, $program)
  58. {
  59. global $contentTypeCharset; // these variables are defined in 'ini.inc.php'
  60. global $convertExportDataToUTF8;
  61. // Get the absolute path for the bibutils package:
  62. // (function 'getExternalUtilityPath()' is defined in 'include.inc.php')
  63. $bibutilsPath = getExternalUtilityPath("bibutils");
  64. // Generate and serve a MODS XML file of ALL records:
  65. // (function 'modsCollection()' is defined in 'modsxml.inc.php')
  66. $recordCollection = modsCollection($result);
  67. // Write the MODS XML data to a temporary file:
  68. $tempFile = writeToTempFile($recordCollection);
  69. // Set input and output encoding:
  70. if (($convertExportDataToUTF8 == "no") AND ($contentTypeCharset != "UTF-8"))
  71. {
  72. $inputEncodingArg = " -i iso8859_1";
  73. $outputEncodingArg = " -o iso8859_1";
  74. }
  75. else
  76. {
  77. $inputEncodingArg = " -i utf8";
  78. $outputEncodingArg = " -o utf8";
  79. }
  80. // Pass this temp file to the bibutils utility for conversion:
  81. $outputFile = convertBibutils($bibutilsPath, $tempFile, $program, $inputEncodingArg, $outputEncodingArg);
  82. unlink($tempFile);
  83. // Read the resulting output file and return the converted data:
  84. $resultString = readFromFile($outputFile);
  85. unlink($outputFile);
  86. return $resultString;
  87. }
  88. // --------------------------------------------------------------------
  89. // Convert file contents using the bibutils program given in '$program'
  90. function convertBibutils($bibutilsPath, $tempFile, $program, $inputEncodingArg, $outputEncodingArg)
  91. {
  92. global $sessionTempDir; // defined in 'ini.inc.php'
  93. $outputFile = tempnam($sessionTempDir, "refbase-");
  94. $cmd = $bibutilsPath . $program . $inputEncodingArg . $outputEncodingArg . " " . $tempFile;
  95. execute($cmd, $outputFile);
  96. return $outputFile;
  97. }
  98. // --------------------------------------------------------------------
  99. // Execute shell command
  100. function execute($cmd, $outputFile)
  101. {
  102. if (getenv("OS") == "Windows_NT")
  103. executeWin32($cmd . " > " . $outputFile);
  104. else
  105. {
  106. exec($cmd, $output);
  107. arrayToFile($output, $outputFile);
  108. }
  109. }
  110. // --------------------------------------------------------------------
  111. // Execute shell command on win32 systems
  112. function executeWin32($cmd)
  113. {
  114. $cmdline = "cmd /C ". $cmd;
  115. // Make a new instance of the COM object
  116. $WshShell = new COM("WScript.Shell");
  117. // Make the command window but dont show it
  118. $oExec = $WshShell->Run($cmdline, 0, true);
  119. }
  120. // --------------------------------------------------------------------
  121. // Write data to a temporary file
  122. function writeToTempFile($sourceText)
  123. {
  124. global $sessionTempDir; // defined in 'ini.inc.php'
  125. $tempFile = tempnam($sessionTempDir, "refbase-");
  126. $tempFileHandle = fopen($tempFile, "w"); // open temp file with write permission
  127. fwrite($tempFileHandle, $sourceText); // save data to temp file
  128. fclose($tempFileHandle); // close temp file
  129. return $tempFile;
  130. }
  131. // --------------------------------------------------------------------
  132. // Get file contents
  133. function readFromFile($file)
  134. {
  135. // Enable PHP to detect Mac (CR) EOL conventions:
  136. // (see <http://www.php.net/manual/en/ref.filesystem.php#ini.auto-detect-line-endings>)
  137. ini_set('auto_detect_line_endings', true);
  138. $fileContents = file_get_contents($file);
  139. // Remove UTF-8 BOM if present
  140. $bom = "\xef\xbb\xbf";
  141. if (0 == strncmp($fileContents, $bom, 3)) {
  142. $fileContents = substr($fileContents, 3);
  143. }
  144. return $fileContents;
  145. }
  146. // --------------------------------------------------------------------
  147. // Write an array (as from $return argument in exec) to a file
  148. function arrayToFile($array, $outputFile)
  149. {
  150. return (stringToFile(implode("\n", $array), $outputFile));
  151. }
  152. function stringToFile($string, $outputFile)
  153. {
  154. $rc = false;
  155. do
  156. {
  157. if (!($f = fopen($outputFile, "wa+")))
  158. {
  159. $rc = 1;
  160. break;
  161. }
  162. if (!fwrite($f, $string))
  163. {
  164. $rc = 2;
  165. break;
  166. }
  167. $rc = true;
  168. }
  169. while (0);
  170. if ($f)
  171. fclose($f);
  172. return ($rc);
  173. }
  174. // --------------------------------------------------------------------
  175. ?>