#!/usr/bin/perl
# phpini_directives v1.0  (c) 21.12.2012 by Andreas Ley  (u) 21.12.2012
# Extract php.ini directives from http://php.net/manual/en/ini.list.php

use locale;
use Getopt::Std;

sub usage
{
	my $image = $0;
	$image =~ s!.*/!!;
	print STDERR "Usage: $image [-v] [filename [...]]\n";
	exit(1);
}

getopts('Dhxv') or &usage;

&usage if (defined($opt_h));

if (defined($opt_v) || defined($opt_D)) {
	$|=1;
	select((select(STDERR),$|=1)[0]);
}

while (<>) {
	chomp;
	print STDERR "\"$_\"\n" if ($opt_D>0);
	if (/<tr>/i) {
		undef $directive;
		$column = 0;
	}
	elsif (/<td>(.+)<\/td>/i) {
		$td = $1;
		print STDERR "$column \"$_\"\n" if ($opt_D>0);
		if ($column == 0 && $td =~ /<a [^>]*>([^<]*)<\/a>/i) {
			$directive = $1;
			print STDERR "=> \"$directive\"\n" if ($opt_D>0);
		}
		# http://php.net/manual/en/configuration.changes.modes.php
		elsif ($column == 2 && ($td eq "PHP_INI_PERDIR" || $td eq "PHP_INI_ALL") && defined($directive)) {
			print $directive,"\n";
		}
		$column++;
	}
}
