Posted By

crypticsoft on 05/26/12


Tagged

php convert ssi includes


Versions (?)

Parse PHP files into ASP extensions


 / Published in: PHP
 

For those cases where you want to code in PHP but need to deploy to ASP. It doesn't account for php logic but only converts the php include() statements into SSI includes. Could be useful for converting basic websites from php to asp, rare but it happens :)

  1. <?php
  2. /*
  3. ## Convert from PHP to ASP extensions with SSI includes ##
  4.  
  5. getFiles() - Get all php files from root / includes directory
  6. stripIncludes() - Regex php syntax and includes for SSI
  7. saveFile(filename) - Save string to (.asp) file extension while preserving the filename
  8.  
  9. ## Concept -
  10. 1) function getFiles( directory ) - all php files from directory and loop through each
  11. 2) function parseDir( directory ) - copy file to .asp extension and remove the .php version
  12. - replace php includes with SSI then save file
  13.  
  14. ## Examples:
  15. //parse includes folder '/inc' or use $_SERVER['DOCUMENT_ROOT']
  16. parseDir( dirname( __FILE__ ) . '/inc/' );
  17.  
  18. //parse root
  19. parseDir( dirname( __FILE__ ) );
  20.  
  21. */
  22. //copy all of your php source into another directory, replace 'asp_directory' and set to your export path
  23. parseDir( $_SERVER['DOCUMENT_ROOT'] . '/asp_directory/' );
  24. parseDir( $_SERVER['DOCUMENT_ROOT'] . '/asp_directory/inc/' );
  25.  
  26. function getFiles($dir){
  27. //start with root files
  28. if ($handle = opendir($dir)) {
  29. $files = '';
  30. //loop through all of the files
  31. while (false !== ($file = readdir($handle))) {
  32. if($file!='.'&&$file!='..'&&stripos($file,'.php')){ //all php files
  33. $files[]=$file;
  34. }
  35. }
  36.  
  37. closedir($handle);
  38. }
  39. return $files;
  40. }
  41.  
  42. function parseDir($dir){
  43.  
  44. foreach( getFiles( $dir ) as $file ){
  45.  
  46. if( $file != 'convert.php' && $file != '' ){ //parse all files except this one
  47.  
  48. $fname = str_replace('.php','.asp',$file);//strip off extension to add in fwrite
  49. $fpath = $dir . $fname;//new filename and path
  50. $orig = $dir . $file;
  51.  
  52. //copy from php to asp extension
  53. if( copy($orig, $fpath) ) {
  54. unlink($orig);//remove the php version
  55. }else{
  56. print "Error: unable to copy file " . $orig . " - to - " . $fpath . " <br>check file permissions";
  57. }
  58.  
  59. //remove the old php version
  60.  
  61. //read the file contents into a string for replacements
  62. $file_str = file_get_contents( $fpath );
  63.  
  64. //replace the php include statements with SSI virtual includes
  65. $file_str = str_replace( "<? include('", '<!--#include virtual="', $file_str ); // 'virtual' = relative path
  66. $file_str = str_replace( ".php'); ?>", '.asp"-->', $file_str ); //closing SSI tags
  67. $file_str = str_replace( ".php", ".asp", $file_str ); //replace all .php link references to .asp
  68.  
  69. //if the file is writeable then proceed
  70. if( is_writeable( $fpath ) ){
  71.  
  72. //write the new string to the .asp file - just as it should be (plain text)
  73. $fp = fopen( $fpath, 'w' );
  74. fwrite( $fp, $file_str ); //write the new file
  75. fclose($fp);//close
  76.  
  77. }else{
  78. print 'Error: Cannot write to directory, check permissions on' . $fpath;
  79. }//end if
  80.  
  81. }//end if file
  82. print "parsed: " . $fpath . "<br />";
  83. }//end foreach
  84.  
  85. }//end parseDir
  86.  
  87.  
  88. ?>

Report this snippet  

You need to login to post a comment.