1. <pre>
  2. <?php
  3. /**
  4.  * parseModRewrite() Function
  5.  * Created by: Joshua H. (TRUSTAbyss)
  6.  *
  7.  * Usage: parseModRewrite("HTACCESS FILE") will output a multi-dimensional array
  8.  * that can be used to convert to Abyss's URL Rewriting.
  9.  */
  10.  
  11. function parseModRewrite($file)
  12. {
  13. // Check to see if the file exists before proceeding.
  14. if (file_exists($file))
  15. {
  16. // Initialize the arrays and the $base variable (used for other rules).
  17. $array = array();
  18. $rewrite = array();
  19. $base = '';
  20.  
  21. // Open the htaccess file to be parsed.
  22. $file_array = file($file);
  23.  
  24. foreach ($file_array as $lineNum => $line)
  25. {
  26. // Remove whitespace from the beginning and end of $line.
  27. $line = trim($line);
  28.  
  29. // Replace whitespace inside RewriteCond/RewriteRule with \s character.
  30. if (preg_match("/\\\\\s/", $line))
  31. {
  32. $line = preg_replace("/\\\\\s/", "\\s", $line);
  33. }
  34.  
  35. // Read the current line and replace multiple whitespaces with a single space.
  36. $line = preg_replace("/\s+/", " ", $line);
  37.  
  38. // Check for a RewriteBase.
  39. if (preg_match("/^RewriteBase\s(\/[^\s]*)$/i", $line, $matches))
  40. {
  41. // Create the variable for RewriteBase ($matches[1]) and store the permanent.
  42. $array['base'] = $matches[1];
  43. $base = $matches[1];
  44. }
  45.  
  46. // Check for a RewriteCond.
  47. if (preg_match("/^RewriteCond\s([^\s]+)\s([^\s]+)(\s\[([^\s]+)\])?$/i", $line, $matches))
  48. {
  49. // Create the variables for Variable ($matches[1]), Expression ($matches[2]), and Flag ($matches[4]).
  50. $array['cond']['var'][] = $matches[1];
  51. $array['cond']['exp'][] = $matches[2];
  52. $array['cond']['fla'][] = isset($matches[4]) ? $matches[4] : 0;
  53.  
  54. // Gather info about the current line (not required).
  55. $array['cond']['info'][] = 'Line '.($lineNum + 1).': '.$matches[0];
  56. }
  57.  
  58. // Check for a RewriteRule.
  59. if (preg_match("/^RewriteRule\s([^\s]+)\s([^\s]+)(\s\[([^\s]+)\])?$/i", $line, $matches))
  60. {
  61. // Ignore the Apache specific RewriteRule that fixes the Internal Dummy Connections.
  62. if ($matches[2] == '-')
  63. {
  64. // Delete the conditions that belong to this RewriteRule.
  65. if (isset($array['cond']))
  66. {
  67. unset($array['cond']);
  68. }
  69.  
  70. // Continue to the next RewriteRule.
  71. continue;
  72. }
  73.  
  74. // Create the variables for Regexp ($matches[1]), Replacement ($matches[2]), and Flag ($matches[4]).
  75. $array['rule']['reg'] = $matches[1];
  76. $array['rule']['rep'] = $matches[2];
  77. $array['rule']['fla'] = isset($matches[4]) ? $matches[4] : 0;
  78.  
  79. // Gather info about the current line (not required).
  80. $array['rule']['info'] = 'Line '.($lineNum + 1).': '.$matches[0];
  81.  
  82. // If needed, create a RewriteBase path based on where the htaccess file is located.
  83. if (!isset($array['base']) && empty($base))
  84. {
  85. // Create the variable for RewriteBase and store the permanent.
  86. $array['base'] = @str_replace('\\', '/', dirname(substr(realpath($file), strlen($_SERVER['DOCUMENT_ROOT']))));
  87. $base = $array['base'];
  88. }
  89.  
  90. // Sort the rewrites in alphabetical order (base, cond, rule)
  91. ksort($array);
  92.  
  93. // Since we came to a RewriteRule, we can add the rewrite.
  94. $rewrite[] = $array;
  95.  
  96. // We need to delete what was already in the array to add another rewrite.
  97. $array = array();
  98.  
  99. // Loop through all the rewrites and change the RewriteBase if needed.
  100. foreach ($rewrite as $group)
  101. {
  102. if (!empty($group['base']))
  103. {
  104. $array['base'] = $base;
  105. }
  106. }
  107. }
  108. }
  109.  
  110. // Return the array or FALSE if it could not be parsed.
  111. return (!empty($rewrite)) ? $rewrite : FALSE;
  112. }
  113. else // The file does not exist.
  114. {
  115. return FALSE; // The function must return FALSE.
  116. }
  117. }
  118.  
  119.  
  120.  
  121. // Here's an example on using this function.
  122. if ($parsed = parseModRewrite("drive:/path/to/.htaccess"))
  123. {
  124. print_r($parsed);
  125. }
  126. ?>
  127. </pre>
  128.