473,383 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 2796
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: yawnmoth | last post by:
i'm trying to write a php script that will password protect some random directory by creating a .htaccess file, and a password file to accompany the .htaccess file, and it isn't working... ...
8
by: Joshua Beall | last post by:
Hi All, How do I disable magic quotes via .htaccess? I put the following file in my webroot, but it does not disable magic_quotes_gpc (according to phpinfo(), both the local and master value...
4
by: Ivo | last post by:
Greetings newsgroup, I am moving some php scripts to a new host. While getting to know the server, I ran into a strange problem. If I add a .htaccess file, or more specifically: a .htaccess with...
7
by: John | last post by:
Hello. I want to get this blasted .htaccess file sorted out, so I can have sessions without register_globals being on. I have looked everywhere for info on this and I mean everywhere...
0
by: Stoco | last post by:
I am trying to create a generic interface that will manage various ..htaccess protected directories. In the ideal world, the .htaccess would trigger a cgi script that would take login information...
0
by: Jack Hambabo | last post by:
Hi, I'm searching for a php script that can find out whether the current user (I know _SERVER will give me the name for non-cgi php) has the right to view a specific file. My dream is that I...
0
by: asherwolf | last post by:
Hi, I'm trying to do something I think is pretty neat, but I've just about pulled my hair out by the behavior of my server. I'm hosting on GoDaddy, using a subdomain (www.mywebpage.com maps to...
0
by: deco12 | last post by:
Hi, I am new to web design and Apache, but have managed to password protect my site by putting a .htaccess file in the sites root directory. I have set it up so that a number of members have...
29
by: lenbell | last post by:
It's old stupid and lazy here again I have been wanting to keep using my WYSIWYG (What You See Is What You Get - for my fellow stupids) html editor. But I was told that you HAD to rename your...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.