Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing an .htaccess File With PHP

I am using the Zend Framework, and I use the .htaccess for some settings. I am now writing command line scripts for scheduling (e.g. cron). Command line scripts don't look at the .htaccess file because they're not served up by Apache. I would like to parse the .htaccess with my script to retrieve some settings. Here are the lines I'm specifically interested in:

SetEnv APPLICATION_ENV development

php_value date.timezone America/New_York

I noticed the PEAR File_HtAccess package, but it seems to only address authentication portions of the .htaccess file.


SOLUTION: (with credit due to Bamieater)

echo statements for debug output, removed from working code.

$htaccess = file(realpath(dirname(__FILE__)) . '/.htaccess');
echo '<pre>';
foreach ($htaccess as $line) {
    if (preg_match('/^\s*SetEnv\s+APPLICATION_ENV\s+(.*?)\s*$/', trim($line), $matches)) {
        defined('APPLICATION_ENV') || define('APPLICATION_ENV', $matches[1]);
        echo APPLICATION_ENV . PHP_EOL;
    } elseif (preg_match('/^\s*php_(?:admin_)?value\s+date\.timezone\s+(.*?)\s*$/', $line, $matches)) {
        date_default_timezone_set($matches[1]);
        echo date_default_timezone_get() . PHP_EOL;
    }
}
echo '</pre>';
like image 597
Sonny Avatar asked Sep 01 '10 13:09

Sonny


3 Answers

Read the .htaccess file per line and use a regular expression to access the data.

For example something like this:

$line = "php_value date.timezone America/New_York";
$pattern = "@^php_value date.timezone (.*)$@";

if(preg_match($pattern,$line,$matches))
{
    print_r($matches);
}
like image 91
Bamieater Avatar answered Oct 23 '22 01:10

Bamieater


Borrowing a bit from Bamieater, here's a more complete function you can drop into the beginning of your cron job file that sets the environment variables and includes the php_prepend_file.

function isCLI() {
    return (PHP_SAPI == 'cli');
}

function loadEnvironmentFromHtaccess($file) {
    $line = file_get_contents($file);

    $pattern = '@SetEnv (?P<env>[^ ]*) (?P<value>[^ \n]*)@';
    preg_match_all($pattern, $line, $matches, PREG_SET_ORDER);
    foreach ($matches as $match) {
        $_SERVER[$match['env']] = $match['value'];
    }

    $pattern = '@php_value auto_prepend_file (?P<value>[^ \n]*)@';
    if (preg_match($pattern, $line, $matches)) {
        require $matches['value'];
    }
}

if (isCLI()) {
    loadEnvironmentFromHtaccess(dirname(__FILE__).'/../../.htaccess');
}
like image 26
wesman16 Avatar answered Oct 23 '22 01:10

wesman16


Parsing .htaccess, despite it is easy to do, is not the best solution in my opinion.

The environment variable may be set in different places, so you'd have to check all the files (eg. main Apache configuration, other .htaccesses).

I'd recommend setting separate environment variable for your shell scripts,

export APPLICATION_ENV=staging

I keep those settings in global properties of the server (apache config and .bashrc), so automatically all the apps know where they are without changing the files upon deployment.

like image 28
takeshin Avatar answered Oct 23 '22 02:10

takeshin