aboutsummaryrefslogtreecommitdiffstats
path: root/genconf
blob: e90126273415d18c4a8f628840b59b85edcbfc75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env php
<?php
/**
 * @author Laurent Bachelier <laurent@bachelier.name>
 */

error_reporting(E_ALL|E_STRICT);

$opts = getopt('d:oa:n:p:');
$opt_keys = array(
  'd'=>'default',
  'o'=>'only',
  'a'=>'allow',
  'n'=>'nophp',
  'p'=>'path',
);
$options = array('default'=>'index', 'allow'=>false, 'nophp'=>false, 'path'=>null);
foreach ($opts as $kopt => $opt)
{
  $options[$opt_keys[$kopt]] = $opt;
}
$options['only'] = isset($options['only']);
$options['allow'] = $options['allow']
                  ? explode(',', $options['allow'])
                  : array();
$options['nophp'] = $options['nophp']
                  ? explode(',', $options['nophp'])
                  : array('uploads');
$options['path'] = $options['path']
                  ? realpath($options['path'])
                  // Not using __FILE__ since it resolves symlinks
                  : realpath(dirname($argv[0]).'/../web');

$files = array('dir'=>array(), 'php'=>array(), 'file'=>array());
foreach (new DirectoryIterator($options['path']) as $file)
{
  $name = $file->getFilename();
  if ($name[0] != '.')
  {
    if ($file->isDir())
    {
      $files['dir'][] = $name;
    }
    elseif (!preg_match('/\.php$/', $name))
    {
      $files['file'][] = $name;
    }
    elseif (empty($options['only']))
    {
        $files['php'][] = $name;
    }
  }
}
foreach ($options['allow'] as $name)
{
  $files['php'][] = $name.'.php';
}
?>
server.document-root = "<?php echo $options['path'] ?>"

url.rewrite-once = (
<?php foreach ($files['dir'] as $name): ?>
  "^/<?php echo preg_quote($name) ?>/.+" => "$0",
<?php endforeach ?>

<?php foreach ($files['file'] as $name): ?>
  "^/<?php echo preg_quote($name) ?>$" => "$0",
<?php endforeach ?>

<?php foreach ($files['php'] as $name): ?>
  "^/<?php echo preg_quote($name) ?>(/[^\?]*)?(\?.*)?" => "/<?php echo $name ?>$1$2",
<?php endforeach ?>

  "^(/[^\?]*)(\?.*)?" => "/<?php echo $options['default'] ?>.php$1$2"
)

<?php foreach ($options['nophp'] as $name): ?>
<?php if (in_array($name, $files['dir'])): ?>
$HTTP["url"] =~ "^/<?php echo $name ?>/" {
  url.access-deny = (".php")
}
<?php endif ?>
<?php endforeach ?>