469,610 Members | 1,649 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

Parsing .htaccess file

Hi all!

I want to parse the contents of an .htaccess file from within PHP. The
contents of the .htaccess file looks like this:

<Files foobar.tar.gz>
AuthType Basic
AuthUserFile /foobar
AuthName "Foobar"
<Limit GET>
Require user FOOBAR
</Limit>
</Files>

And so on. When I read in that file with the following code:

$fd = fopen(".htaccess", 'rb');
while (!feof($fd)) {
$line = fgets($fd, 1024);
echo "|".$line."|\n";
}
fclose($fd);

Then I get empty lines for those lines containing <Files ...>,
<Limit ...> and so on. How can I get at those lines as well?

What I need to know is basically the information "which user can access
which file?"

--
Stefan Bellon
Feb 10 '06 #1
4 2590
Stefan Bellon wrote:
Hi all!

I want to parse the contents of an .htaccess file from within PHP. The
contents of the .htaccess file looks like this:

<Files foobar.tar.gz>
AuthType Basic
AuthUserFile /foobar
AuthName "Foobar"
<Limit GET>
Require user FOOBAR
</Limit>
</Files>

And so on. When I read in that file with the following code:

$fd = fopen(".htaccess", 'rb');
while (!feof($fd)) {
$line = fgets($fd, 1024);
echo "|".$line."|\n";
}
fclose($fd);

Then I get empty lines for those lines containing <Files ...>,
<Limit ...> and so on. How can I get at those lines as well?

What I need to know is basically the information "which user can access
which file?"

Hi,

Maybe you are in htmlcontext?

What does this produce?

$myLines = file('path/to/.htaccess');
foreach($myLines as $oneLine){
echo htmlentities($oneLine)."<br>";
}

Does that show correctly?

Regards,
Erwin Moller
Feb 10 '06 #2
Erwin Moller wrote:
$myLines = file('path/to/.htaccess');
foreach($myLines as $oneLine){
echo htmlentities($oneLine)."<br>";
}

Does that show correctly?


This does print the content correctly, yes, thanks a lot. However, if I
want to parse (and replace) the content using ereg_replace I get funny
results as the bracket < seems to be represented as &lt; internally.
So, when trying to get at the filename, I need to do something like

ereg_replace("&lt;Files (.*)&gt;", "\\1", htmlentities($line))

Wow. Is this the easiest way of filtering out the information? All I
want to do is build a list of "who may access which file according to
the information inside the .htaccess file".

--
Stefan Bellon
Feb 10 '06 #3
Stefan Bellon wrote:
What I need to know is basically the information "which user can
access which file?"


In the meantime I've come up with a solution. Just in case somebody
else needs something similar, here is my PHP code:

<?php

## Read in the .htgroups file and build array of groups of array of
## users.
$tmp=file('.htgroups');
$groups=array();
foreach($tmp as $line)
{
$exploded=explode(": ", $line);
$groups[$exploded[0]] = explode(" ", $exploded[1]);
}

## Read in the .htaccess file looking for group accesses for files.
$tmp=file('.htaccess');
$file="";
$files=array();
foreach($tmp as $line)
{
## htmlentities necessary in order to get at HTML-like tags at all,
## but then they're escaped, so &lt; and &gt; are necessary.
if (ereg("&lt;Files .*&gt;", htmlentities($line)))
{
## We have a file section starting, so remember the file name.
$file=trim(ereg_replace("&lt;Files (.*)&gt;", "\\1",
htmlentities($line)));
}
elseif (($file != "") &&
($file != "index.php") &&
(ereg("AuthName \".*\"", $line)))
{
## Get the authentication name of the file.
$files[$file]=array
('Name' => ereg_replace("AuthName \"(.*)\"", "\\1", $line),
'access' => 0);
}
elseif (($file != "") &&
($file != "index.php") &&
(ereg("Require user .*", $line)))
{
## We have a user permission.
$user=trim(ereg_replace("Require user (.*)", "\\1", $line));

if ($user == $_SERVER['REMOTE_USER'])
{
$files[$file]['access']=1;
}
}
elseif (($file != "") &&
($file != "index.php") &&
(ereg("Require group .*", $line)))
{
## We have a group permission, so get the group.
$group=trim(ereg_replace("Require group (.*)", "\\1", $line));

## Look in the users of the group whether REMOTE_USER is listed.
foreach($groups[$group] as $user)
{
if ($user == $_SERVER['REMOTE_USER'])
{
$files[$file]['access']=1;
break;
}
}
}
}

## Do whatever HTML stuff you want here, and later on:

## Now loop through the file permission data and built HTML code.
foreach($files as $file => $data)
{
if (($data['access'] == 1) && (file_exists($file)))
{
## Do something with $file and/or data['Name']
}
}

## Rest of HTML stuff.

?>

--
Stefan Bellon
Feb 12 '06 #4
Stefan Bellon wrote:
Erwin Moller wrote:
$myLines = file('path/to/.htaccess');
foreach($myLines as $oneLine){
echo htmlentities($oneLine)."<br>";
}

Does that show correctly?


This does print the content correctly, yes, thanks a lot. However, if I
want to parse (and replace) the content using ereg_replace I get funny
results as the bracket < seems to be represented as &lt; internally.
So, when trying to get at the filename, I need to do something like

ereg_replace("&lt;Files (.*)&gt;", "\\1", htmlentities($line))

Wow. Is this the easiest way of filtering out the information? All I
want to do is build a list of "who may access which file according to
the information inside the .htaccess file".


Hi,

No, it is not the easiest way, but it shows the output right to the browser.
If you use < and > in your output, your webbrowser will interpret that as a
tag.
adding htmlentities replaces the <> (and other problematic characters) by
their HTMLequivalents.

You can do what you were trying to do, but I cannot help you with the
regexp, because I suck at regexpr.
So my response was just a warning to use htmlentities whenever you are
feeding information to a browser, because of the unexpected result you get
when you do not.
Of course: Looking in the source (view source) of the page could help too.

Regards,
Erwin
Feb 17 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by yawnmoth | last post: by
8 posts views Thread by Joshua Beall | last post: by
4 posts views Thread by Ivo | last post: by
7 posts views Thread by John | last post: by
reply views Thread by Stoco | last post: by
reply views Thread by Jack Hambabo | last post: by
29 posts views Thread by lenbell | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.