Parsing .htaccess file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stefan Bellon

    #1

    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(".htacces s", '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
  • Erwin Moller

    #2
    Re: Parsing .htaccess file

    Stefan Bellon wrote:
    [color=blue]
    > 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(".htacces s", '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?"
    >[/color]


    Hi,

    Maybe you are in htmlcontext?

    What does this produce?

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

    Does that show correctly?

    Regards,
    Erwin Moller

    Comment

    • Stefan Bellon

      #3
      Re: Parsing .htaccess file

      Erwin Moller wrote:
      [color=blue]
      > $myLines = file('path/to/.htaccess');
      > foreach($myLine s as $oneLine){
      > echo htmlentities($o neLine)."<br>";
      > }
      >
      > Does that show correctly?[/color]

      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($l ine))

      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

      Comment

      • Stefan Bellon

        #4
        Re: Parsing .htaccess file

        Stefan Bellon wrote:
        [color=blue]
        > What I need to know is basically the information "which user can
        > access which file?"[/color]

        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('.htg roups');
        $groups=array() ;
        foreach($tmp as $line)
        {
        $exploded=explo de(": ", $line);
        $groups[$exploded[0]] = explode(" ", $exploded[1]);
        }

        ## Read in the .htaccess file looking for group accesses for files.
        $tmp=file('.hta ccess');
        $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;File s .*&gt;", htmlentities($l ine)))
        {
        ## We have a file section starting, so remember the file name.
        $file=trim(ereg _replace("&lt;F iles (.*)&gt;", "\\1",
        htmlentities($l ine)));
        }
        elseif (($file != "") &&
        ($file != "index.php" ) &&
        (ereg("AuthName \".*\"", $line)))
        {
        ## Get the authentication name of the file.
        $files[$file]=array
        ('Name' => ereg_replace("A uthName \"(.*)\"", "\\1", $line),
        'access' => 0);
        }
        elseif (($file != "") &&
        ($file != "index.php" ) &&
        (ereg("Require user .*", $line)))
        {
        ## We have a user permission.
        $user=trim(ereg _replace("Requi re 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(ere g_replace("Requ ire 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($f ile)))
        {
        ## Do something with $file and/or data['Name']
        }
        }

        ## Rest of HTML stuff.

        ?>

        --
        Stefan Bellon

        Comment

        • Erwin Moller

          #5
          Re: Parsing .htaccess file

          Stefan Bellon wrote:
          [color=blue]
          > Erwin Moller wrote:
          >[color=green]
          >> $myLines = file('path/to/.htaccess');
          >> foreach($myLine s as $oneLine){
          >> echo htmlentities($o neLine)."<br>";
          >> }
          >>
          >> Does that show correctly?[/color]
          >
          > 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($l ine))
          >
          > 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".
          >[/color]

          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

          Comment

          Working...