webserver.php
author Dan
Fri, 15 Aug 2008 23:59:15 -0400
changeset 35 8040903d25de
parent 34 3b817b961984
child 37 65e70ada71c9
permissions -rw-r--r--
Updated readme for imminent release 0.1a3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     1
<?php
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     2
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     3
/**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     4
 * Webserver class
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     5
 * 
6
5f35ebc4f9bb First release version. Renamed to Greyhound and readme/license files added.
Dan
parents: 5
diff changeset
     6
 * Greyhound - real web management for Amarok
5
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 0
diff changeset
     7
 * Copyright (C) 2008 Dan Fuhry
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
     8
 *
5
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 0
diff changeset
     9
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 0
diff changeset
    10
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 0
diff changeset
    11
 *
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 0
diff changeset
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9b96265b5918 Relicensed to GPLv2. Previous revisions should not be downloaded as they do not contain copies of appropriate licenses, which will be added in a later commit. Completed interface for mobile devices.
Dan
parents: 0
diff changeset
    13
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    14
 */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    15
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    16
/**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    17
 * Version of the server
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    18
 * @const string
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    19
 */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    20
34
3b817b961984 Merging changes from nighthawk; added support for dumb terminals
Dan
parents: 32 29
diff changeset
    21
define('HTTPD_VERSION', '0.1b4');
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    22
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    23
/**
26
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
    24
 * Length of keep-alive connections
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
    25
 * @const int
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
    26
 */
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
    27
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
    28
define('HTTPD_KEEP_ALIVE_TIMEOUT', 300);
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
    29
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
    30
/**
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    31
 * Webserver system icons
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    32
 */
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    33
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    34
define('HTTPD_ICON_SCRIPT', 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAGSSURBVCjPVVFNSwJhEF78Ad79Cf6PvXQRsotUlzKICosuRYmR2RJR0KE6lBFFZVEbpFBSqKu2rum6llFS9HHI4iUhT153n6ZtIWMOM+/MM88z7wwH7s9Ub16SJcnbmrNcxVm2q7Z8/QPvEOtntpj92NkCqITLepEpjix7xQtiLOoQ2b6+E7YAN/5nfOEJ2WbKqOIOJ4bYVMEQx4LfBBQDsvFMhUcCVU1/CxVXmDBGA5ZETrhDCQVcYAPbyEJBhvrnBVPiSpNr6cYDNCQwo4zzU/ySckkgDYuNuVpI42T9k4gLKGMPs/xPzzovQiY2hQYe0jlJfyNNhTqiWDYBq/wBMcSRpnyPzu1oS7WtxjVBSthU1vgVksiQ3Dn6Gp5ah2YOKQo5GiuHPA6xT1EKpxQNCNYejgIR457KKio0S56YckjSa9jo//3mrj+BV0QQagqGTOo+Y7gZIf1puP3WHoLhEb2PjTlCTCWGXtbp8DCX3hZuOdaIc9A+aQvWk4ihq95p67a7nP+u+Ws+r0dql9z/zv0NCYhdCPKZ7oYAAAAASUVORK5CYII=');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    35
define('HTTPD_ICON_FOLDER', 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAGrSURBVDjLxZO7ihRBFIa/6u0ZW7GHBUV0UQQTZzd3QdhMQxOfwMRXEANBMNQX0MzAzFAwEzHwARbNFDdwEd31Mj3X7a6uOr9BtzNjYjKBJ6nicP7v3KqcJFaxhBVtZUAK8OHlld2st7Xl3DJPVONP+zEUV4HqL5UDYHr5xvuQAjgl/Qs7TzvOOVAjxjlC+ePSwe6DfbVegLVuT4r14eTr6zvA8xSAoBLzx6pvj4l+DZIezuVkG9fY2H7YRQIMZIBwycmzH1/s3F8AapfIPNF3kQk7+kw9PWBy+IZOdg5Ug3mkAATy/t0usovzGeCUWTjCz0B+Sj0ekfdvkZ3abBv+U4GaCtJ1iEm6ANQJ6fEzrG/engcKw/wXQvEKxSEKQxRGKE7Izt+DSiwBJMUSm71rguMYhQKrBygOIRStf4TiFFRBvbRGKiQLWP29yRSHKBTtfdBmHs0BUpgvtgF4yRFR+NUKi0XZcYjCeCG2smkzLAHkbRBmP0/Uk26O5YnUActBp1GsAI+S5nRJJJal5K1aAMrq0d6Tm9uI6zjyf75dAe6tx/SsWeD//o2/Ab6IH3/h25pOAAAAAElFTkSuQmCC');
29
8210fdc7c317 Switched default theme to grey.
Dan
parents: 26
diff changeset
    36
define('HTTPD_ICON_FILE',   'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAINSURBVBgZBcG/r55zGAfg6/4+z3va01NHlYgzEfE7MdCIGISFgS4Gk8ViYyM2Mdlsko4GSf8Do0FLRCIkghhYJA3aVBtEz3nP89wf11VJvPDepdd390+8Nso5nESBQoq0pfvXm9fzWf19453LF85vASqJlz748vInb517dIw6EyYBIIG49u+xi9/c9MdvR//99MPPZ7+4cP4IZhhTPbwzT2d+vGoaVRRp1rRliVvHq+cfvM3TD82+7mun0o/ceO7NT+/4/KOXjwZU1ekk0840bAZzMQ2mooqh0A72d5x/6sB9D5zYnff3PoYBoWBgFKPKqDKqjCpjKr//dcu9p489dra88cydps30KswACfNEKanSaxhlntjJ8Mv12Paie+vZ+0+oeSwwQ0Iw1xAR1CiFNJkGO4wu3ZMY1AAzBI0qSgmCNJsJUEOtJSMaCTBDLyQ0CknAGOgyTyFFiLI2awMzdEcSQgSAAKVUmAeNkxvWJWCGtVlDmgYQ0GFtgg4pNtOwbBcwQy/Rife/2yrRRVI0qYCEBly8Z+P4qMEMy7JaVw72N568e+iwhrXoECQkfH91kY7jwwXMsBx1L93ZruqrK6uuiAIdSnTIKKPLPFcvay8ww/Hh+ufeznTXu49v95IMoQG3784gYXdTqvRmqn/Wpa/ADFX58MW3L71SVU9ETgEIQQQIOOzub+fhIvwPRDgeVjWDahIAAAAASUVORK5CYII=');
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    37
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    38
/**
24
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
    39
 * Simple but full-featured embedded web server written in PHP.
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    40
 * @package Amarok
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    41
 * @subpackage WebControl
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    42
 * @author Dan Fuhry
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
    43
 * @license GNU General Public License <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    44
 */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    45
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    46
class WebServer
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    47
{
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    48
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    49
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    50
   * IP address we're bound to
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    51
   * @var string
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    52
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    53
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    54
  var $bind_address = '127.0.0.1';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    55
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    56
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    57
   * Socket resource
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    58
   * @var resource
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    59
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    60
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    61
  var $sock = null;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    62
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    63
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    64
   * Server string
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    65
   * @var string
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    66
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    67
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    68
  var $server_string = 'PhpHttpd';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    69
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    70
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    71
   * Default document (well default handler)
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    72
   * @var string
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    73
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    74
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    75
  var $default_document = false;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    76
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    77
  /**
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    78
   * List of filenames or handlers used when a directory listing is requested
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    79
   * @var array
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    80
   */
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    81
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    82
  var $directory_index = array('index.html', 'index.htm', 'index', 'default.html', 'default.htm');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    83
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
    84
  /**
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    85
   * HTTP response code set by the handler function
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    86
   * @var int
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    87
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    88
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    89
  var $response_code = 0;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    90
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    91
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    92
   * Content type set by the current handler function
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    93
   * @var string
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    94
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    95
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    96
  var $content_type = '';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    97
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    98
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
    99
   * Response headers to send back to the client
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   100
   * @var array
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   101
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   102
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   103
  var $response_headers = array();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   104
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   105
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   106
   * List of handlers
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   107
   * @var array
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   108
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   109
   
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   110
  var $handlers = array();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   111
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   112
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   113
   * Switch to control if directory listing is enabled
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   114
   * @var bool
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   115
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   116
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   117
  var $allow_dir_list = false;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   118
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   119
  /**
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   120
   * Switch to control forking support.
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   121
   * @var bool
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   122
   */
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   123
  
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   124
  var $allow_fork = true;
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   125
  
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   126
  /**
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   127
   * Keep-alive support uses this to track what the client requested.
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   128
   * Only used if $allow_fork is set to true.
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   129
   * @var bool
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   130
   */
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   131
  
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   132
  var $in_keepalive = false;
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   133
  
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   134
  /**
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   135
   * UUID for this server instance
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   136
   * @var string
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   137
   */
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   138
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   139
  var $uuid = '00000000-0000-0000-0000-000000000000';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   140
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   141
  /**
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   142
   * Switch to track whether a scriptlet is running. If it is, send_http_error() does more than normal.
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   143
   * @var bool
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   144
   */
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   145
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   146
  var $in_scriptlet = false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   147
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   148
  /**
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   149
   * Switch to track whether headers have been sent or not.
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   150
   * @var bool
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   151
   */
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   152
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   153
  var $headers_sent = false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   154
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   155
  /**
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   156
   * Switch to track if the socket is bound and thus needs to be freed or not
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   157
   * @var bool
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   158
   */
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   159
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   160
  var $socket_initted = false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   161
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   162
  /**
32
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   163
   * The list of child processes spawned by this server.
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   164
   * @var array
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   165
   */
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   166
  
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   167
  var $child_list = array();
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   168
  
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   169
  /**
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   170
   * The parent process's PID
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   171
   * @var int
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   172
   */
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   173
  
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   174
  var $parent_pid = 0;
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   175
  
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   176
  /**
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   177
   * Constructor.
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   178
   * @param string IPv4 address to bind to
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   179
   * @param int Port number
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   180
   * @param int If port is under 1024, specify a user ID/name to switch to here
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   181
   * @param int If port is under 1024, specify a group ID/name to switch to here
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   182
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   183
  
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   184
  function __construct($address = '127.0.0.1', $port = 8080, $targetuser = null, $targetgroup = null)
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   185
  {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   186
    @set_time_limit(0);
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   187
    @ini_set('memory_limit', '128M');
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   188
    
13
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 12
diff changeset
   189
    // do we have socket functions?
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 12
diff changeset
   190
    if ( !function_exists('socket_create') )
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 12
diff changeset
   191
    {
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 12
diff changeset
   192
      burnout('System does not support socket functions. Please rebuild your PHP or install an appropriate extension.');
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 12
diff changeset
   193
    }
b5db2345c397 Added PHP feature check to prevent script from throwing unneeded exceptions on unsupported PHP configurations
Dan
parents: 12
diff changeset
   194
    
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   195
    // make sure we're not running as root
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   196
    // note that if allow_root is true, you must specify a UID/GID (or user/group) to switch to once the socket is bound
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   197
    $allow_root = ( $port < 1024 ) ? true : false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   198
    if ( function_exists('posix_geteuid') )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   199
    {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   200
      $euid = posix_geteuid();
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   201
      $egid = posix_getegid();
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   202
      $username = posix_getpwuid($euid);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   203
      $username = $username['name'];
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   204
      $group = posix_getgrgid($egid);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   205
      $group = $group['name'];
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   206
      if ( $euid == 0 && !$allow_root )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   207
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   208
        // running as root but not on a privileged port - die for security
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   209
        burnout("Running as superuser (user \"$username\" and group \"$group\"). This is not allowed for security reasons.");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   210
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   211
      else if ( $euid == 0 && $allow_root )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   212
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   213
        // running as root and port below 1024, so notify of the switch and verify that a target UID and GID were passed
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   214
        if ( $targetuser === null || $targetgroup === null )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   215
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   216
          // no target user/group specified
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   217
          burnout("Must specify a target user and group when running server as root");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   218
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   219
        // get info about target user/group
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   220
        if ( is_string($targetuser) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   221
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   222
          $targetuser = posix_getpwnam($targetuser);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   223
          $targetuser = $targetuser['uid'];
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   224
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   225
        if ( is_string($targetgroup) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   226
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   227
          $targetgroup = posix_getgrnam($targetgroup);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   228
          $targetgroup = $targetgroup['uid'];
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   229
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   230
        // make sure all info is valid
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   231
        if ( !is_int($targetuser) || !is_int($targetgroup) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   232
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   233
          burnout('Invalid user or group specified');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   234
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   235
        $userinfo = posix_getpwuid($targetuser);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   236
        $groupinfo = posix_getgrgid($targetgroup);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   237
        if ( function_exists('status') )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   238
          status("Will switch to user \"{$userinfo['name']}\" and group \"{$groupinfo['name']}\" shortly after binding to socket");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   239
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   240
      else if ( $allow_root && $euid > 0 )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   241
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   242
        burnout("Must be superuser to bind to ports below 1024");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   243
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   244
    }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   245
    $socket_do_root = ( $allow_root ) ? function_exists('posix_geteuid') : false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   246
    
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   247
    $this->sock = @socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   248
    if ( !$this->sock )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   249
      throw new Exception('Could not create socket');
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   250
    $result = @socket_bind($this->sock, $address, $port);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   251
    if ( !$result )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   252
      throw new Exception("Could not bind to $address:$port");
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   253
    $this->socket_initted = true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   254
    $result = @socket_listen($this->sock, SOMAXCONN);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   255
    if ( !$result )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   256
      throw new Exception("Could not listen for connections $address:$port");
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   257
    
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   258
    // if running as root and we made it here, switch credentials
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   259
    if ( $socket_do_root )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   260
    {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   261
      posix_setuid($targetuser);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   262
      posix_setgid($targetgroup);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   263
      posix_setegid($targetgroup);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   264
      posix_seteuid($targetuser);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   265
      if ( function_exists('status') )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   266
        status('Successfully switched user ID');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   267
    }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   268
    
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   269
    $this->bind_address = $address;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   270
    $this->server_string = "PhpHttpd/" . HTTPD_VERSION . " PHP/" . PHP_VERSION . "\r\n";
32
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   271
    $this->parent_pid = getmypid();
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   272
    
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   273
    // create a UUID
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   274
    $uuid_base = md5(microtime() . ( function_exists('mt_rand') ? mt_rand() : rand() ));
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   275
    $this->uuid = substr($uuid_base, 0,  8) . '-' .
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   276
                  substr($uuid_base, 8,  4) . '-' .
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   277
                  substr($uuid_base, 12, 4) . '-' .
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   278
                  substr($uuid_base, 16, 4) . '-' .
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   279
                  substr($uuid_base, 20, 20);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   280
  }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   281
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   282
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   283
   * Destructor.
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   284
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   285
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   286
  function __destruct()
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   287
  {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   288
    if ( !defined('HTTPD_WS_CHILD') && $this->socket_initted )
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   289
    {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   290
      if ( function_exists('status') )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   291
        status('WebServer: destroying socket');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   292
      // http://us3.php.net/manual/en/function.socket-bind.php
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   293
      if ( !@socket_set_option($this->sock, SOL_SOCKET, SO_REUSEADDR, 1) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   294
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   295
        echo socket_strerror(socket_last_error($sock)) . "\n";
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   296
      }
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   297
      @socket_shutdown($this->sock, 2);
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   298
      @socket_close($this->sock);
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   299
    }
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   300
  }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   301
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   302
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   303
   * Main server loop
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   304
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   305
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   306
  function serve()
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   307
  {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   308
    while ( true )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   309
    {
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   310
      // if this is a child process, we're finished - close up shop
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   311
      if ( defined('HTTPD_WS_CHILD') && !$this->in_keepalive )
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   312
      {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   313
        if ( function_exists('status') )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   314
          status('Exiting child process');
16
23d4cf2f183b Disabled forking/keep-alive code, firefox doesn't seem to like the way it waits for requests
Dan
parents: 15
diff changeset
   315
        @socket_shutdown($remote);
23d4cf2f183b Disabled forking/keep-alive code, firefox doesn't seem to like the way it waits for requests
Dan
parents: 15
diff changeset
   316
        @socket_close($remote);
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   317
        exit(0);
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   318
      }
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   319
      
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   320
      // wait for connection...
12
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   321
      // trick from http://us.php.net/manual/en/function.socket-accept.php
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   322
      if ( !defined('HTTPD_WS_CHILD') )
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   323
      {
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   324
        $remote = false;
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   325
        $timeout = 5;
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   326
        switch(@socket_select($r = array($this->sock), $w = array($this->sock), $e = array($this->sock), $timeout)) {
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   327
          case 2:
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   328
            break;
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   329
          case 1:
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   330
            $remote = @socket_accept($this->sock);
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   331
            break;
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   332
          case 0:
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   333
            break;
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   334
        }
12
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   335
      }
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   336
         
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   337
      if ( !$remote )
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   338
      {
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   339
        $this->in_keepalive = false;
12
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   340
        continue;
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   341
      }
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   342
      
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   343
      // fork off if possible
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   344
      if ( function_exists('pcntl_fork') && $this->allow_fork && !$this->in_keepalive )
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   345
      {
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   346
        $pid = pcntl_fork();
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   347
        if ( $pid == -1 )
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   348
        {
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   349
          // do nothing; continue responding to request in single-threaded mode
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   350
        }
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   351
        else if ( $pid )
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   352
        {
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   353
          // we are the parent, continue listening
16
23d4cf2f183b Disabled forking/keep-alive code, firefox doesn't seem to like the way it waits for requests
Dan
parents: 15
diff changeset
   354
          socket_close($remote);
32
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   355
          $this->child_list[] = $pid;
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   356
          continue;
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   357
        }
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   358
        else
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   359
        {
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   360
          // this is the child
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   361
          define('HTTPD_WS_CHILD', 1);
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   362
          @socket_set_option($this->sock, SOL_SOCKET, SO_REUSEADDR, 1);
16
23d4cf2f183b Disabled forking/keep-alive code, firefox doesn't seem to like the way it waits for requests
Dan
parents: 15
diff changeset
   363
          socket_close($this->sock);
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   364
        }
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   365
      }
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   366
      
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   367
      $this->in_keepalive = false;
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   368
      $this->headers_sent = false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   369
      $this->in_scriptlet = false;
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   370
      
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   371
      // read request
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   372
      $last_line = '';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   373
      $client_headers = '';
26
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   374
      if ( defined('HTTPD_WS_CHILD') )
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   375
      {
32
86140ed9087d Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
Dan
parents: 26
diff changeset
   376
        @socket_set_timeout($remote, HTTPD_KEEP_ALIVE_TIMEOUT);
26
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   377
      }
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   378
      if ( $line = @socket_read($remote, 1024, PHP_NORMAL_READ) )
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   379
      {
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   380
        do
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   381
        {
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   382
          $line = str_replace("\r", "", $line);
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   383
          if ( empty($line) )
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   384
            continue;
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   385
          if ( $line == "\n" && $last_line == "\n" )
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   386
            break;
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   387
          $client_headers .= $line;
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   388
          $last_line = $line;
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   389
        }
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   390
        while ( $line = @socket_read($remote, 1024, PHP_NORMAL_READ) );
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   391
      }
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   392
      else
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   393
      {
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   394
        if ( defined('HTTPD_WS_CHILD') )
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   395
        {
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   396
          $md = @socket_get_status($remote);
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   397
          if ( @$md['timed_out'] )
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   398
          {
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   399
            status('[debug] keep-alive connection timed out');
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   400
            continue; // will jump back to the start of the loop and kill the child process
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   401
          }
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
   402
        }
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   403
      }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   404
      
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   405
      // parse request
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   406
      $client_headers = trim($client_headers);
29
8210fdc7c317 Switched default theme to grey.
Dan
parents: 26
diff changeset
   407
      
8210fdc7c317 Switched default theme to grey.
Dan
parents: 26
diff changeset
   408
      if ( isset($last_finish_time) && empty($client_headers) && defined('HTTPD_WS_CHILD') && $last_finish_time + HTTPD_KEEP_ALIVE_TIMEOUT < microtime(true) )
8210fdc7c317 Switched default theme to grey.
Dan
parents: 26
diff changeset
   409
      {
8210fdc7c317 Switched default theme to grey.
Dan
parents: 26
diff changeset
   410
        status('[debug] keep-alive connection timed out (checkpoint 2)');
8210fdc7c317 Switched default theme to grey.
Dan
parents: 26
diff changeset
   411
        continue; // will jump back to the start of the loop and kill the child process
8210fdc7c317 Switched default theme to grey.
Dan
parents: 26
diff changeset
   412
      }
8210fdc7c317 Switched default theme to grey.
Dan
parents: 26
diff changeset
   413
      
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   414
      $client_headers = explode("\n", $client_headers);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   415
      
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   416
      // first line
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   417
      $request = $client_headers[0];
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   418
      if ( !preg_match('/^(GET|POST) \/([^ ]*) HTTP\/1\.[01]$/', $request, $match) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   419
      {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   420
        $this->send_http_error($remote, 400, 'Your client issued a malformed or illegal request.');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   421
        continue;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   422
      }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   423
      $method =& $match[1];
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   424
      $uri =& $match[2];
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   425
      
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   426
      // set client headers
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   427
      unset($client_headers[0]);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   428
      foreach ( $client_headers as $line )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   429
      {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   430
        if ( !preg_match('/^([A-z0-9-]+): (.+)$/is', $line, $match) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   431
          continue;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   432
        $key = 'HTTP_' . strtoupper(str_replace('-', '_', $match[1]));
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   433
        $_SERVER[$key] = $match[2];
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   434
      }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   435
      
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   436
      // enable keep-alive if requested
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   437
      if ( isset($_SERVER['HTTP_CONNECTION']) && defined('HTTPD_WS_CHILD') )
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   438
      {
16
23d4cf2f183b Disabled forking/keep-alive code, firefox doesn't seem to like the way it waits for requests
Dan
parents: 15
diff changeset
   439
        $this->in_keepalive = ( strtolower($_SERVER['HTTP_CONNECTION']) === 'keep-alive' );
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   440
      }
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   441
      
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   442
      // parse authorization, if any
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   443
      if ( isset($_SERVER['PHP_AUTH_USER']) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   444
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   445
        unset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   446
      }
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   447
      if ( isset($_SERVER['HTTP_AUTHORIZATION']) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   448
      {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   449
        $data = $_SERVER['HTTP_AUTHORIZATION'];
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   450
        $data = substr(strstr($data, ' '), 1);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   451
        $data = base64_decode($data);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   452
        $_SERVER['PHP_AUTH_USER'] = substr($data, 0, strpos($data, ':'));
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   453
        $_SERVER['PHP_AUTH_PW'] = substr(strstr($data, ':'), 1);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   454
      }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   455
      
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   456
      // anything on POST?
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   457
      $postdata = '';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   458
      $_POST = array();
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   459
      $_FILES = array();
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   460
      if ( $method == 'POST' )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   461
      {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   462
        // read POST data
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   463
        if ( isset($_SERVER['HTTP_CONTENT_TYPE']) && preg_match('#^multipart/form-data; ?boundary=([A-z0-9_-]+)$#i', $_SERVER['HTTP_CONTENT_TYPE'], $match) )
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   464
        {
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   465
          // this is a multipart request
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   466
          $boundary =& $match[1];
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   467
          $mode = 'data';
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   468
          $last_line = '';
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   469
          $i = 0;
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   470
          while ( $data = socket_read($remote, 8388608, PHP_NORMAL_READ) )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   471
          {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   472
            $data_trim = trim($data, "\r\n");
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   473
            if ( $mode != 'data' )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   474
            {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   475
              $data = str_replace("\r", '', $data);
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   476
            }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   477
            if ( ( $data_trim === "--$boundary" || $data_trim === "--$boundary--" ) && $i > 0 )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   478
            {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   479
              // trim off the first LF and the last CRLF
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   480
              $currval_data = substr($currval_data, 1, strlen($currval_data)-3);
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   481
              
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   482
              // this is the end of a part of the message; parse it into either $_POST or $_FILES
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   483
              if ( is_string($have_a_file) )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   484
              {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   485
                
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   486
                // write data to a temporary file
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   487
                $errcode = UPLOAD_ERR_OK;
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   488
                $tempfile = tempnam('phpupload', ( function_exists('sys_get_temp_dir') ? sys_get_temp_dir() : '/tmp' ));
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   489
                if ( $fh = @fopen($tempfile, 'w') )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   490
                {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   491
                  if ( empty($have_a_file) )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   492
                  {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   493
                    $errcode = UPLOAD_ERR_NO_FILE;
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   494
                  }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   495
                  else
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   496
                  {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   497
                    fwrite($fh, $currval_data);
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   498
                  }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   499
                  fclose($fh);
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   500
                }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   501
                else
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   502
                {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   503
                  $errcode = UPLOAD_ERR_CANT_WRITE;
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   504
                }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   505
                $_FILES[$currval_name] = array(
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   506
                    'name' => $have_a_file,
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   507
                    'type' => $currval_type,
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   508
                    'size' => filesize($tempfile),
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   509
                    'tmp_name' => $tempfile,
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   510
                    'error' => $errcode
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   511
                  );
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   512
              }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   513
              else
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   514
              {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   515
                $_POST[$currval_name] = $currval_data;
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   516
              }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   517
            }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   518
            
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   519
            if ( $data_trim === "--$boundary" )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   520
            {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   521
              // switch from "data" mode to "headers" mode
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   522
              $currval_name = '';
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   523
              $currval_data = '';
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   524
              $currval_type = '';
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   525
              $have_a_file = false;
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   526
              $mode = 'headers';
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   527
            }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   528
            else if ( $data_trim === "--$boundary--" )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   529
            {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   530
              // end of request
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   531
              break;
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   532
            }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   533
            else if ( ( empty($data_trim) && empty($last_line) ) && $mode == 'headers' )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   534
            {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   535
              // start of data
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   536
              $mode = 'data';
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   537
            }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   538
            else if ( $mode == 'headers' )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   539
            {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   540
              // read header
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   541
              // we're only looking for Content-Disposition and Content-Type
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   542
              if ( preg_match('#^Content-Disposition: form-data; name="([^"\a\t\r\n]+)"(?:; filename="([^"\a\t\r\n]+)")?#i', $data_trim, $match) )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   543
              {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   544
                // content-disposition header, set name and mode.
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   545
                $currval_name = $match[1];
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   546
                if ( isset($match[2]) )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   547
                {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   548
                  $have_a_file = $match[2];
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   549
                }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   550
                else
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   551
                {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   552
                  $have_a_file = false;
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   553
                }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   554
              }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   555
              else if ( preg_match('#^Content-Type: ([a-z0-9-]+/[a-z0-9/-]+)$#i', $data_trim, $match) )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   556
              {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   557
                $currval_type = $match[1];
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   558
              }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   559
            }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   560
            else if ( $mode == 'data' )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   561
            {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   562
              $currval_data .= $data;
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   563
            }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   564
            $last_line = $data_trim;
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   565
            $i++;
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   566
          }
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   567
        }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   568
        else
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   569
        {
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   570
          if ( isset($_SERVER['HTTP_CONTENT_LENGTH']) )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   571
          {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   572
            $postdata = socket_read($remote, intval($_SERVER['HTTP_CONTENT_LENGTH']), PHP_BINARY_READ);
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   573
          }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   574
          else
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   575
          {
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   576
            $postdata = socket_read($remote, 8388608, PHP_NORMAL_READ);
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   577
          }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   578
          if ( preg_match_all('/(^|&)([a-z0-9_\.\[\]-]+)(=[^ &]+)?/', $postdata, $matches) )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   579
          {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   580
            if ( isset($matches[1]) )
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   581
            {
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   582
              foreach ( $matches[0] as $i => $_ )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   583
              {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   584
                $_POST[$matches[2][$i]] = ( !empty($matches[3][$i]) ) ? urldecode(substr($matches[3][$i], 1)) : true;
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   585
              }
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   586
            }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   587
          }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   588
        }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   589
      }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   590
      
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   591
      // parse URI
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   592
      $params = '';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   593
      if ( strstr($uri, '?') )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   594
      {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   595
        $params = substr(strstr($uri, '?'), 1);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   596
        $uri    = substr($uri, 0, strpos($uri, '?'));
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   597
      }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   598
      
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   599
      // set some server vars
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   600
      $_SERVER['REQUEST_URI'] = '/' . rawurldecode($uri);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   601
      $_SERVER['REQUEST_METHOD'] = $method;
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   602
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   603
      // get remote IP and port
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   604
      socket_getpeername($remote, $_SERVER['REMOTE_ADDR'], $_SERVER['REMOTE_PORT']);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   605
      
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   606
      $_GET = array();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   607
      if ( preg_match_all('/(^|&)([a-z0-9_\.\[\]-]+)(=[^ &]+)?/', $params, $matches) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   608
      {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   609
        if ( isset($matches[1]) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   610
        {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   611
          foreach ( $matches[0] as $i => $_ )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   612
          {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   613
            $_GET[$matches[2][$i]] = ( !empty($matches[3][$i]) ) ? urldecode(substr($matches[3][$i], 1)) : true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   614
          }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   615
        }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   616
      }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   617
      
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   618
      $_GET = $this->parse_multi_depth_array($_GET);
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   619
      $_POST = $this->parse_multi_depth_array($_POST);
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   620
      $_FILES = $this->parse_multi_depth_array($_FILES);
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   621
      
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   622
      // init handler
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   623
      $handler = false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   624
      
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   625
      if ( $uri == '' )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   626
      {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   627
        // user requested the root (/). If there's a default document, use that; else, see if we can do a directory listing
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   628
        $uri = strval($this->default_document);
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   629
        if ( !$this->default_document && $this->allow_dir_list )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   630
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   631
          // we can list directories and this was requested by the user, so list it out
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   632
          $handler = array('type' => 'rootdir');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   633
        }
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   634
      }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   635
      
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   636
      $uri_parts = explode('/', $uri);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   637
      
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   638
      // hook for the special UUID handler
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   639
      if ( $uri_parts[0] === $this->uuid && !$handler )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   640
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   641
        $handler = array('type' => 'sysuuid');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   642
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   643
      
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   644
      // loop through URI parts, see if a handler is set
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   645
      if ( !$handler )
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   646
      {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   647
        for ( $i = count($uri_parts) - 1; $i >= 0; $i-- )
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   648
        {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   649
          $handler_test = implode('/', $uri_parts);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   650
          if ( isset($this->handlers[$handler_test]) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   651
          {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   652
            $handler = $this->handlers[$handler_test];
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   653
            $handler['id'] = $handler_test;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   654
            break;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   655
          }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   656
          unset($uri_parts[$i]);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   657
        }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   658
      }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   659
      
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   660
      if ( !$handler )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   661
      {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   662
        // try to make a fakie
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   663
        if ( $this->check_for_handler_children($uri) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   664
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   665
          $handler = array(
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   666
            'type' => 'folder',
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   667
            'dir' => "/{$this->uuid}/__fakie",
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   668
            'id' => $uri
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   669
          );
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   670
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   671
        if ( !$handler )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   672
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   673
          $this->send_http_error($remote, 404, "The requested URL /$uri was not found on this server.");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   674
          continue;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   675
        }
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   676
      }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   677
      
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   678
      $this->send_standard_response($remote, $handler, $uri, $params);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   679
      
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   680
      // now that we're done sending the response, delete any temporary uploaded files
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   681
      if ( !empty($_FILES) )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   682
      {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   683
        foreach ( $_FILES as $file_data )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   684
        {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   685
          if ( file_exists($file_data['tmp_name']) )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   686
          {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   687
            @unlink($file_data['tmp_name']);
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   688
          }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   689
        }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   690
      }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   691
      
16
23d4cf2f183b Disabled forking/keep-alive code, firefox doesn't seem to like the way it waits for requests
Dan
parents: 15
diff changeset
   692
      if ( !$this->in_keepalive && defined('HTTPD_WS_CHILD') )
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   693
      {
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   694
        // if ( defined('HTTPD_WS_CHILD') )
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   695
        //   status('Closing connection');
16
23d4cf2f183b Disabled forking/keep-alive code, firefox doesn't seem to like the way it waits for requests
Dan
parents: 15
diff changeset
   696
        @socket_shutdown($remote);
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   697
        @socket_close($remote);
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   698
        if ( function_exists('status') )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   699
          status('Exiting child process');
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   700
        exit(0);
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   701
      }
16
23d4cf2f183b Disabled forking/keep-alive code, firefox doesn't seem to like the way it waits for requests
Dan
parents: 15
diff changeset
   702
      else if ( defined('HTTPD_WS_CHILD') )
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   703
      {
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   704
        // if ( defined('HTTPD_WS_CHILD') )
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   705
        //   status('Continuing connection');
17
66c3eb4cdc4c Re-enabled multi-threaded operation after trying with a field test
Dan
parents: 16
diff changeset
   706
        // @socket_write($remote, "\r\n\r\n");
29
8210fdc7c317 Switched default theme to grey.
Dan
parents: 26
diff changeset
   707
        $last_finish_time = microtime(true);
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   708
      }
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   709
      else
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   710
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   711
        @socket_shutdown($remote);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   712
        @socket_close($remote);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   713
      }
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   714
    }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   715
  }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   716
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   717
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   718
   * Sends the client appropriate response headers.
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   719
   * @param resource Socket connection to client
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   720
   * @param int HTTP status code, defaults to 200
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   721
   * @param string Content type, defaults to text/html
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   722
   * @param string Additional headers to send, optional
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   723
   */                     
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   724
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   725
  function send_client_headers($socket, $http_code = 200, $contenttype = 'text/html', $headers = '')
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   726
  {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   727
    global $http_responses;
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   728
    if ( $this->headers_sent )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   729
      return false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   730
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   731
    // this is reset after the request is completed (hopefully)    
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   732
    $this->headers_sent = true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   733
    
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   734
    $reason_code = ( isset($http_responses[$http_code]) ) ? $http_responses[$http_code] : 'Unknown';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   735
    
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   736
    $_SERVER['HTTP_USER_AGENT'] = ( isset($_SERVER['HTTP_USER_AGENT']) ) ? $_SERVER['HTTP_USER_AGENT'] : '(no user agent)';
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   737
    
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   738
    if ( function_exists('status') )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   739
      status("{$_SERVER['REMOTE_ADDR']} {$_SERVER['REQUEST_METHOD']} {$_SERVER['REQUEST_URI']} $http_code {$_SERVER['HTTP_USER_AGENT']}");
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   740
    
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   741
    $headers = str_replace("\r\n", "\n", $headers);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   742
    $headers = str_replace("\n", "\r\n", $headers);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   743
    $headers = preg_replace("#[\r\n]+$#", '', $headers);
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   744
    $connection = ( $this->in_keepalive ) ? 'keep-alive' : 'close';
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   745
    
12
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   746
    @socket_write($socket, "HTTP/1.1 $http_code $reason_code\r\n");
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   747
    @socket_write($socket, "Server: $this->server_string");
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   748
    @socket_write($socket, "Connection: $connection\r\n");
12
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   749
    @socket_write($socket, "Content-Type: $contenttype\r\n");
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   750
    if ( !empty($headers) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   751
    {
12
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   752
      @socket_write($socket, "$headers\r\n");
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   753
    }
12
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   754
    @socket_write($socket, "\r\n");
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   755
  }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   756
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   757
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   758
   * Sends a normal response
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   759
   * @param resource Socket connection to client
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   760
   * @param array Handler
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   761
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   762
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   763
  function send_standard_response($socket, $handler)
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   764
  {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   765
    switch ( $handler['type'] )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   766
    {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   767
      case 'folder':
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   768
        // security
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   769
        $uri = str_replace("\000", '', $_SERVER['REQUEST_URI']);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   770
        if ( preg_match('#(\.\./|\/\.\.)#', $uri) || strstr($uri, "\r") || strstr($uri, "\n") )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   771
        {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   772
          $this->send_http_error($socket, 403, 'Access to this resource is forbidden.');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   773
        }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   774
        
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   775
        // import mimetypes
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   776
        global $mime_types;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   777
        
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   778
        // trim handler id from uri
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   779
        $uri_full = rtrim($uri, '/');
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   780
        $uri = substr($uri, strlen($handler['id']) + 1);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   781
        
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   782
        // get file path
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   783
        $file_path = rtrim($handler['dir'], '/') . $uri;
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   784
        if ( file_exists($file_path) || $this->check_for_handler_children($uri_full) )
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   785
        {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   786
          // found it :-D
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   787
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   788
          // is this a directory?
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   789
          if ( is_dir($file_path) || $this->check_for_handler_children($uri_full) )
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   790
          {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   791
            // allowed to list?
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   792
            if ( !$this->allow_dir_list )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   793
            {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   794
              $this->send_http_error($socket, 403, "Directory listing is not allowed.");
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   795
              return true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   796
            }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   797
            // yes, list contents
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   798
            try
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   799
            {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   800
              $dir_list = $this->list_directory($uri_full, true);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   801
            }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   802
            catch ( Exception $e )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   803
            {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   804
              $this->send_http_error($socket, 500, "Directory listing failed due to an error in the listing core method. This may indicate that the webserver process does not have filesystem access to the specified directory.<br /><br />Debugging details:<pre>$e</pre>");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   805
              return true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   806
            }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   807
            
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   808
            $root = rtrim($uri_full, '/') . '/';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   809
            $parent = rtrim(dirname(rtrim($uri_full, '/')), '/') . '/';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   810
            
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   811
            $contents = <<<EOF
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   812
<html>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   813
  <head>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   814
    <title>Index of: $root</title>
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   815
    <link rel="stylesheet" type="text/css" href="/{$this->uuid}/dirlist.css" />
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   816
  </head>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   817
  <body>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   818
    <h1>Index of $root</h1>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   819
    <ul>
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   820
      <li><tt><a href="$parent">Parent directory</a></tt></li>
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   821
    
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   822
EOF;
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   823
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   824
            foreach ( $dir_list as $filename => $info )
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   825
            {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   826
              $ts = ( $info['type'] == 'folder' ) ? '/' : '';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   827
              $contents .= '  <li><tt><a href="' . htmlspecialchars($root . basename($filename) . $ts) . '"><img alt="[   ]" src="/' . $this->uuid . '/' . $info['type'] . '.png" /> ' . htmlspecialchars($filename) . $ts . '</a></tt></li>' . "\n    ";
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   828
            }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   829
            $contents .= "\n    </ul>\n    <address>Served by {$this->server_string}</address>\n</body>\n</html>\n\n";
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   830
            
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   831
            $sz = strlen($contents);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   832
            $this->send_client_headers($socket, 200, 'text/html', "Content-length: $sz\r\n");
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   833
            
12
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   834
            @socket_write($socket, $contents);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   835
            
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   836
            return true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   837
          }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   838
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   839
          // try to open the file
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   840
          $fh = @fopen($file_path, 'r');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   841
          if ( !$fh )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   842
          {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   843
            // can't open it, send a 404
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   844
            $this->send_http_error($socket, 404, "The requested URL " . htmlspecialchars($_SERVER['REQUEST_URI']) . " was not found on this server.");
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   845
          }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   846
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   847
          // get size
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   848
          $sz = filesize($file_path);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   849
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   850
          // mod time
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   851
          $time = date('r', filemtime($file_path));
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   852
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   853
          // all good, send headers
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   854
          $fileext = substr($file_path, strrpos($file_path, '.') + 1);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   855
          $mimetype = ( isset($mime_types[$fileext]) ) ? $mime_types[$fileext] : 'application/octet-stream';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   856
          $this->send_client_headers($socket, 200, $mimetype, "Content-length: $sz\r\nLast-Modified: $time\r\n");
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   857
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   858
          // send body
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   859
          while ( $blk = @fread($fh, 768000) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   860
          {
12
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   861
            @socket_write($socket, $blk);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   862
          }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   863
          fclose($fh);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   864
          return true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   865
        }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   866
        else
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   867
        {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   868
          $this->send_http_error($socket, 404, "The requested URL " . htmlspecialchars($_SERVER['REQUEST_URI']) . " was not found on this server.");
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   869
        }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   870
        
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   871
        break;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   872
      case 'file':
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   873
        
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   874
        // import mimetypes
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   875
        global $mime_types;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   876
        
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   877
        // get file path
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   878
        $file_path = $handler['file'];
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   879
        if ( file_exists($file_path) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   880
        {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   881
          // found it :-D
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   882
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   883
          // is this a directory?
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   884
          if ( is_dir($file_path) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   885
          {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   886
            $this->send_http_error($socket, 500, "Host script mapped a directory as a file entry.");
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   887
            return true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   888
          }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   889
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   890
          // try to open the file
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   891
          $fh = @fopen($file_path, 'r');
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   892
          if ( !$fh )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   893
          {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   894
            // can't open it, send a 404
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   895
            $this->send_http_error($socket, 404, "The requested URL " . htmlspecialchars($_SERVER['REQUEST_URI']) . " was not found on this server.");
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   896
          }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   897
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   898
          // get size
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   899
          $sz = filesize($file_path);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   900
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   901
          // mod time
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   902
          $time = date('r', filemtime($file_path));
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   903
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   904
          // all good, send headers
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   905
          $fileext = substr($file_path, strrpos($file_path, '.') + 1);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   906
          $mimetype = ( isset($mime_types[$fileext]) ) ? $mime_types[$fileext] : 'application/octet-stream';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   907
          $this->send_client_headers($socket, 200, $mimetype, "Content-length: $sz\r\nLast-Modified: $time\r\n");
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   908
          
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   909
          // send body
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   910
          while ( $blk = @fread($fh, 768000) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   911
          {
12
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   912
            @socket_write($socket, $blk);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   913
          }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   914
          fclose($fh);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   915
          return true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   916
        }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   917
        else
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   918
        {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   919
          $this->send_http_error($socket, 404, "The requested URL " . htmlspecialchars($_SERVER['REQUEST_URI']) . " was not found on this server.");
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   920
        }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   921
        
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   922
        break;
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   923
      case 'script':
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   924
        // init vars
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   925
        $this->content_type = 'text/html';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   926
        $this->response_code = 200;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   927
        $this->response_headers = array();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   928
        
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   929
        // error handling
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   930
        @set_error_handler(array($this, 'function_error_handler'), E_ALL);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   931
        try
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   932
        {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   933
          ob_start();
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   934
          $this->in_scriptlet = true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   935
          $result = @call_user_func($handler['function'], $this, $socket);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   936
          $this->in_scriptlet = false;
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   937
          $output = ob_get_contents();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   938
          ob_end_clean();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   939
        }
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   940
        // throw an HttpExceptionFatal when you need to break out of an in-progress scriptlet due to an error, use it in place of die() or exit()
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   941
        catch ( HttpExceptionFatal $e )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   942
        {
24
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
   943
          ob_end_clean();
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   944
          restore_error_handler();
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   945
          $this->send_http_error($socket, 500, "A handler crashed reporting a fatal exception; see the command line for details.");
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   946
          if ( function_exists('status') )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   947
            status("fatal exception in handler {$handler['id']}:\n$e");
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   948
          return true;
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   949
        }
24
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
   950
        catch ( HttpSuccess $e )
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
   951
        {
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
   952
          // just finish with success
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
   953
          $this->in_scriptlet = false;
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
   954
          $output = ob_get_contents();
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
   955
          ob_end_clean();
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
   956
        }
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   957
        catch ( Exception $e )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   958
        {
24
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
   959
          ob_end_clean();
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   960
          restore_error_handler();
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   961
          $this->send_http_error($socket, 500, "There was an uncaught exception during the execution of a scripted handler function. See the command line for details.");
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   962
          if ( function_exists('status') )
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
   963
            status("uncaught exception in handler {$handler['id']}:\n$e");
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   964
          return true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   965
        }
24
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
   966
        
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   967
        restore_error_handler();
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   968
        
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   969
        // the handler function should return this magic string if it writes its own headers and socket data
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   970
        if ( $output == '__break__' )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   971
        {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   972
          return true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   973
        }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   974
        
16
23d4cf2f183b Disabled forking/keep-alive code, firefox doesn't seem to like the way it waits for requests
Dan
parents: 15
diff changeset
   975
        // $this->header('Transfer-encoding: chunked');
15
2adca0f363fd Added multi-threading/forking/keep-alive support to webserver. w00t, feeling all POSIX-happy today!
Dan
parents: 13
diff changeset
   976
        $this->header("Content-length: " . strlen($output));
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   977
        $headers = implode("\r\n", $this->response_headers);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   978
        
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   979
        // write headers
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   980
        $this->send_client_headers($socket, $this->response_code, $this->content_type, $headers);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   981
        
16
23d4cf2f183b Disabled forking/keep-alive code, firefox doesn't seem to like the way it waits for requests
Dan
parents: 15
diff changeset
   982
        // chunk output
23d4cf2f183b Disabled forking/keep-alive code, firefox doesn't seem to like the way it waits for requests
Dan
parents: 15
diff changeset
   983
        // $output = dechex(strlen($output)) . "\r\n$output";
23d4cf2f183b Disabled forking/keep-alive code, firefox doesn't seem to like the way it waits for requests
Dan
parents: 15
diff changeset
   984
        
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   985
        // write body
12
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
   986
        @socket_write($socket, $output);
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
   987
        
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   988
        $this->headers_sent = false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   989
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   990
        break;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   991
      case 'sysuuid':
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   992
        // requested one of the system's icon images
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   993
        $uri_parts = explode('/', $_SERVER['REQUEST_URI']);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   994
        if ( count($uri_parts) != 3 )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   995
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   996
          $this->send_http_error($socket, 404, "The requested URL " . htmlspecialchars($_SERVER['REQUEST_URI']) . " was not found on this server.");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   997
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   998
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
   999
        // load image data
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1000
        $filename =& $uri_parts[2];
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1001
        switch ( $filename )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1002
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1003
          case 'script.png':
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1004
            ( !isset($image_data) ) ? $image_data = HTTPD_ICON_SCRIPT : null;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1005
          case 'folder.png':
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1006
            ( !isset($image_data) ) ? $image_data = HTTPD_ICON_FOLDER : null;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1007
          case 'file.png':
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1008
            ( !isset($image_data) ) ? $image_data = HTTPD_ICON_FILE : null;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1009
            
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1010
            $image_data = base64_decode($image_data);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1011
            $found = true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1012
            $type = 'image/png';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1013
            break;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1014
          case 'dirlist.css':
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1015
            $type = 'text/css';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1016
            $found = true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1017
            $image_data = <<<EOF
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1018
/**
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1019
 * PhpHttpd directory list visual style
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1020
 */
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1021
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1022
html {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1023
  background-color: #c9c9c9;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1024
  margin: 0;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1025
  padding: 0;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1026
}
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1027
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1028
body {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1029
  background-color: #ffffff;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1030
  margin: 20px;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1031
  padding: 10px;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1032
  border: 1px solid #aaaaaa;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1033
}
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1034
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1035
a {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1036
  text-decoration: none;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1037
}
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1038
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1039
a img {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1040
  border-width: 0;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1041
}
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1042
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1043
ul {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1044
  list-style-type: none;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1045
}
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1046
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1047
EOF;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1048
            break;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1049
          default:
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1050
            $found = false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1051
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1052
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1053
        // ship it out
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1054
        if ( $found )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1055
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1056
          $lm_date = date('r', filemtime(__FILE__));
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1057
          $size = strlen($image_data);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1058
          $this->send_client_headers($socket, 200, $type, "Last-Modified: $lm_date\r\nContent-Length: $size");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1059
          @socket_write($socket, $image_data);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1060
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1061
        else
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1062
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1063
          $this->send_http_error($socket, 404, "The requested URL " . htmlspecialchars($_SERVER['REQUEST_URI']) . " was not found on this server.");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1064
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1065
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1066
        return true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1067
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1068
        break;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1069
      case 'rootdir':
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1070
        //
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1071
        // list the contents of the document root
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1072
        //
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1073
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1074
        $handlers = $this->list_directory('/', true);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1075
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1076
        $contents = <<<EOF
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1077
<html>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1078
  <head>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1079
    <title>Index of: /</title>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1080
    <link rel="stylesheet" type="text/css" href="/{$this->uuid}/dirlist.css" />
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1081
  </head>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1082
  <body>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1083
    <h1>Index of /</h1>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1084
    <ul>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1085
    
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1086
EOF;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1087
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1088
        $html = '';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1089
        // generate content
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1090
        foreach ( $handlers as $uri => $handler )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1091
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1092
          switch($handler['type'])
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1093
          {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1094
            case 'folder':
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1095
              $image = 'folder.png';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1096
              $abbr = 'DIR';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1097
              $add = '/';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1098
              break;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1099
            case 'file':
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1100
            default:
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1101
              $image = 'file.png';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1102
              $abbr = '   ';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1103
              $add = '';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1104
              break;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1105
            case 'script':
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1106
              $image = 'script.png';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1107
              $abbr = 'CGI';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1108
              $add = '';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1109
              break;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1110
          }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1111
          $html .= "  <li><tt><a href=\"/$uri\"><img alt=\"[{$abbr}]\" src=\"/{$this->uuid}/{$image}\" /> {$uri}{$add}</a></tt></li>\n      ";
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1112
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1113
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1114
        $contents .= $html;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1115
        $contents .= <<<EOF
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1116
</ul>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1117
    <address>Served by {$this->server_string}</address>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1118
  </body>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1119
</html>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1120
EOF;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1121
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1122
        // get length
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1123
        $len = strlen($contents);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1124
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1125
        // send headers
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1126
        $this->send_client_headers($socket, 200, 'text/html', "Content-Length: $len");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1127
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1128
        // write to the socket
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1129
        @socket_write($socket, $contents);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1130
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1131
        return true;
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1132
        break;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1133
    }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1134
  }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1135
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1136
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1137
   * Adds an HTTP header value to send back to the client
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1138
   * @var string Header
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1139
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1140
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1141
  function header($str)
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1142
  {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1143
    if ( preg_match('#HTTP/1\.[01] ([0-9]+) (.+?)[\s]*$#', $str, $match) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1144
    {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1145
      $this->response_code = intval($match[1]);
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1146
      return true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1147
    }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1148
    else if ( preg_match('#Content-type: ([^ ;]+)#i', $str, $match) )
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1149
    {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1150
      $this->content_type = $match[1];
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1151
      return true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1152
    }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1153
    $this->response_headers[] = $str;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1154
    return true;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1155
  }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1156
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1157
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1158
   * Sends the client an HTTP error page
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1159
   * @param resource Socket connection to client
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1160
   * @param int HTTP status code
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1161
   * @param string Detailed error string
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1162
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1163
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1164
  function send_http_error($socket, $http_code, $errstring)
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1165
  {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1166
    global $http_responses;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1167
    $reason_code = ( isset($http_responses[$http_code]) ) ? $http_responses[$http_code] : 'Unknown';
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1168
    
26
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1169
    // generate error page
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1170
    $html = <<<EOF
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1171
<html>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1172
  <head>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1173
    <title>$http_code $reason_code</title>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1174
  </head>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1175
  <body>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1176
    <h1>$http_code $reason_code</h1>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1177
    <p>$errstring</p>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1178
    <hr />
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1179
    <address>Served by $this->server_string</address>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1180
  </body>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1181
</html>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1182
EOF;
26
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1183
    
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1184
    // length of the response (required if we want keep-alive to work)
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1185
    $this->header('Content-length: ' . strlen($html));
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1186
    
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1187
    // if we're in a scriptlet, include custom headers
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1188
    if ( $this->in_scriptlet )
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1189
      $headers = implode("\r\n", $this->response_headers);
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1190
    else
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1191
      $headers = 'Content-length: ' . strlen($html);
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1192
      
300d374d89b0 Made error pages send length of response to get keep-alive working properly; added (nonworking, sorry) keep-alive timeout support.
Dan
parents: 24
diff changeset
  1193
    $this->send_client_headers($socket, $http_code, 'text/html', $headers);
12
b3fcc21e557f Backend improvement to webserver that causes interrupts to work properly; made pcntl_signal only be called if it's supported
Dan
parents: 10
diff changeset
  1194
    @socket_write($socket, $html);
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1195
  } 
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1196
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1197
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1198
   * Adds a new handler
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1199
   * @param string URI, minus the initial /
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1200
   * @param string Type of handler - function or dir
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1201
   * @param string Value - function name or absolute/relative path to directory
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1202
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1203
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1204
  function add_handler($uri, $type, $value)
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1205
  {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1206
    if ( $type == 'dir' )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1207
      $type = 'folder';
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1208
    if ( $type == 'function' )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1209
      $type = 'script';
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1210
    switch($type)
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1211
    {
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1212
      case 'folder':
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1213
        $this->handlers[$uri] = array(
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1214
            'type' => 'folder',
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1215
            'dir' => $value
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1216
          );
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1217
        break;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1218
      case 'file':
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1219
        $this->handlers[$uri] = array(
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1220
            'type' => 'file',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1221
            'file' => $value
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1222
          );
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1223
        break;
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1224
      case 'script':
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1225
        $this->handlers[$uri] = array(
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1226
            'type' => 'script',
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1227
            'function' => $value
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1228
          );
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1229
        break;
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1230
    }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1231
  }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1232
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1233
  /**
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1234
   * Error handling function
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1235
   * @param see <http://us.php.net/manual/en/function.set-error-handler.php>
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1236
   */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1237
  
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1238
  function function_error_handler($errno, $errstr, $errfile, $errline, $errcontext)
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1239
  {
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1240
    echo '<div style="border: 1px solid #AA0000; background-color: #FFF0F0; padding: 10px;">';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1241
    echo "<b>PHP warning/error:</b> type $errno ($errstr) caught in <b>$errfile</b> on <b>$errline</b><br />";
10
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents: 6
diff changeset
  1242
    // echo "Error context:<pre>" . htmlspecialchars(print_r($errcontext, true)) . "</pre>";
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1243
    echo '</div>';
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1244
  }
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1245
  
21
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1246
  /**
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1247
   * Lists out the contents of a directory, including virtual handlers.
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1248
   * @example
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1249
   * Example return data: (will be ksorted)
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1250
   <code>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1251
   array(
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1252
       'bar' => 'folder',
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1253
       'baz' => 'script',
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1254
       'foo' => 'file'
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1255
     );
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1256
   </code>
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1257
   * @param string Directory name, relative to the server's document root
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1258
   * @param bool If true, sorts folders first (default: false)
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1259
   * @return array Exception thrown on failure
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1260
   */
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1261
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1262
  function list_directory($dir, $folders_first = false)
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1263
  {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1264
    // clean slashes from the directory name
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1265
    $dir = trim($dir, '/');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1266
    
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1267
    if ( $dir == '' )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1268
    {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1269
      //
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1270
      // list the root, which can consist only of handlers
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1271
      //
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1272
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1273
      // copy the handlers array, which we need to ksort
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1274
      $handlers = $this->handlers;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1275
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1276
      // get rid of multi-depth handlers
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1277
      foreach ( $handlers as $uri => $handler )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1278
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1279
        if ( strpos($uri, '/') )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1280
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1281
          unset($handlers[$uri]);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1282
          $newuri = explode('/', $uri);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1283
          if ( !isset($handlers[$newuri[0]]) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1284
          {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1285
            $handlers[$newuri[0]] = array(
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1286
                'type' => 'folder'
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1287
              );
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1288
          }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1289
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1290
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1291
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1292
      ksort($handlers);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1293
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1294
      if ( $folders_first )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1295
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1296
        // sort folders first
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1297
        $handlers_sorted = array();
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1298
        foreach ( $handlers as $uri => $handler )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1299
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1300
          if ( $handler['type'] == 'folder' )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1301
            $handlers_sorted[$uri] = $handler;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1302
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1303
        foreach ( $handlers as $uri => $handler )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1304
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1305
          if ( $handler['type'] != 'folder' )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1306
            $handlers_sorted[$uri] = $handler;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1307
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1308
        $handlers = $handlers_sorted;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1309
        unset($handlers_sorted);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1310
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1311
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1312
      // done
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1313
      return $handlers;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1314
    }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1315
    else
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1316
    {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1317
      // list something within the root
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1318
      $dir_stack = explode('/', $dir);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1319
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1320
      // lookup handler
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1321
      $handler_search = $dir;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1322
      $found_handler = false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1323
      $fake_handler = false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1324
      $i = 1;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1325
      while ( $i > 0 )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1326
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1327
        if ( isset($this->handlers[$handler_search]) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1328
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1329
          $found_handler = true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1330
          break;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1331
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1332
        $i = strrpos($handler_search, '/');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1333
        $handler_search = substr($handler_search, 0, strrpos($handler_search, '/'));
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1334
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1335
      if ( $this->check_for_handler_children($dir) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1336
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1337
        $fake_handler = true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1338
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1339
      else if ( !$found_handler )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1340
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1341
        // nope. not there.
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1342
        throw new Exception("ERR_NO_SUCH_FILE_OR_DIRECTORY");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1343
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1344
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1345
      // make sure this is a directory
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1346
      if ( !$fake_handler )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1347
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1348
        $handler =& $handler_search;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1349
        if ( $this->handlers[$handler]['type'] != 'folder' )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1350
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1351
          throw new Exception("ERR_NOT_A_DIRECTORY");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1352
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1353
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1354
        // determine real path
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1355
        $real_path = realpath($this->handlers[$handler]['dir'] . substr($dir, strlen($handler)));
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1356
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1357
        // directory is resolved; list contents
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1358
        $dir_contents = array();
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1359
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1360
        if ( $dr = opendir($real_path) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1361
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1362
          while ( $dh = readdir($dr) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1363
          {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1364
            if ( $dh == '.' || $dh == '..' )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1365
            {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1366
              continue;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1367
            }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1368
            $dir_contents[$dh] = array(
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1369
                'type' => ( is_dir("$real_path/$dh") ) ? 'folder' : 'file',
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1370
                'size' => filesize("$real_path/$dh"),
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1371
                'time' => filemtime("$real_path/$dh")
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1372
              );
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1373
          }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1374
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1375
        else
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1376
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1377
          // only if directory open failed
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1378
          throw new Exception("ERR_PERMISSION_DENIED");
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1379
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1380
        
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1381
        closedir($dr);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1382
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1383
        // some cleanup
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1384
        unset($handler, $handler_search);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1385
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1386
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1387
      // list any additional handlers in there
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1388
      foreach ( $this->handlers as $handler => $info )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1389
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1390
        // parse handler name
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1391
        $handler_name = explode('/', trim($handler, '/'));
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1392
        // is this handler in this directory?
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1393
        if ( count($handler_name) != count($dir_stack) + 1 )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1394
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1395
          continue;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1396
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1397
        foreach ( $dir_stack as $i => $_ )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1398
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1399
          if ( $dir_stack[$i] != $handler_name[$i] )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1400
          {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1401
            continue 2;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1402
          }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1403
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1404
        // it's in here!
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1405
        $dir_contents[ basename($handler) ] = array(
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1406
            'type' => $info['type']
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1407
          );
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1408
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1409
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1410
      // list "fake" handlers
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1411
      foreach ( $this->handlers as $handler => $info )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1412
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1413
        // parse handler name
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1414
        $handler_name = explode('/', trim($handler, '/'));
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1415
        // is this handler somewhere underneath this directory?
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1416
        if ( count($handler_name) < count($dir_stack) + 2 )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1417
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1418
          continue;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1419
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1420
        // path check
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1421
        foreach ( $dir_stack as $i => $_ )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1422
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1423
          if ( $dir_stack[$i] != $handler_name[$i] )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1424
          {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1425
            continue 2;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1426
          }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1427
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1428
        // create a "fake" directory
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1429
        $fakie_name = $handler_name[ count($dir_stack) ];
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1430
        $dir_contents[$fakie_name] = array(
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1431
            'type' => 'folder'
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1432
          );
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1433
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1434
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1435
      if ( $folders_first )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1436
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1437
        // perform folder sorting
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1438
        $unsorted = $dir_contents;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1439
        ksort($unsorted);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1440
        $dir_contents = array();
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1441
        foreach ( $unsorted as $name => $info )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1442
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1443
          if ( $info['type'] == 'folder' )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1444
            $dir_contents[$name] = $info;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1445
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1446
        foreach ( $unsorted as $name => $info )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1447
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1448
          if ( $info['type'] != 'folder' )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1449
            $dir_contents[$name] = $info;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1450
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1451
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1452
      else
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1453
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1454
        // not sorting with folders first, so just alphabetize
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1455
        ksort($dir_contents);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1456
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1457
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1458
      // done
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1459
      
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1460
      return $dir_contents;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1461
    }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1462
  }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1463
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1464
  /**
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1465
   * Searches deeper to see if there are sub-handlers within a path to see if a fake handler can be created
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1466
   * @param string URI
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1467
   * @return bool
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1468
   */
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1469
  
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1470
  function check_for_handler_children($file_path)
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1471
  {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1472
    $file_path = trim($file_path, '/');
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1473
    $dir_stack = explode('/', $file_path);
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1474
    
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1475
    // make sure this isn't a "real" handler
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1476
    if ( isset($this->handlers[$file_path]) )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1477
    {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1478
      return false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1479
    }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1480
    
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1481
    // list any additional handlers in there
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1482
    foreach ( $this->handlers as $handler => $info )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1483
    {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1484
      // parse handler name
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1485
      $handler_name = explode('/', trim($handler, '/'));
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1486
      // is this handler in this directory?
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1487
      if ( count($handler_name) != count($dir_stack) + 1 )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1488
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1489
        continue;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1490
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1491
      foreach ( $dir_stack as $i => $_ )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1492
      {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1493
        if ( $dir_stack[$i] != $handler_name[$i] )
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1494
        {
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1495
          continue 2;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1496
        }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1497
      }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1498
      // it's in here!
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1499
      return true;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1500
    }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1501
    
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1502
    return false;
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1503
  }
74edc873234f Made the webserver a bit smarter. It handles running as root properly (only allows it if user/group specified and port < 1024) and directory listing is massively smarter.
Dan
parents: 17
diff changeset
  1504
  
23
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1505
  /**
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1506
   * Takes a flat array with keys of format foo[bar] and parses it into multiple depths.
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1507
   * @param array
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1508
   * @return array
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1509
   */
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1510
  
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1511
  function parse_multi_depth_array($array)
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1512
  {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1513
    foreach ( $array as $key => $value )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1514
    {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1515
      if ( preg_match('/^([^\[\]]+)\[([^\]]+)\]/', $key, $match) )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1516
      {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1517
        $parent =& $match[1];
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1518
        $child =& $match[2];
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1519
        if ( !isset($array[$parent]) || ( isset($array[$parent]) && !is_array($array[$parent]) ) )
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1520
        {
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1521
          $array[$parent] = array();
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1522
        }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1523
        $array[$parent][$child] = $value;
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1524
        unset($array[$key]);
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1525
        $array[$parent] = $this->parse_multi_depth_array($array[$parent]);
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1526
      }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1527
    }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1528
    return $array;
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1529
  }
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1530
  
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1531
}
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1532
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1533
/**
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1534
 * Exception class that allows breaking directly out of a scriptlet.
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1535
 */
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1536
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1537
class HttpExceptionFatal extends Exception
08225c2eb0b6 Added support for multipart forms, file uploads, and fatal exceptions in PhpHttpd; fixed wrong license tag on WebServer class
Dan
parents: 21
diff changeset
  1538
{
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1539
}
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1540
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1541
/**
24
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  1542
 * Exception class that will be treated as a scriptlet ending with success.
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  1543
 */
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  1544
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  1545
class HttpSuccess extends Exception
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  1546
{
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  1547
}
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  1548
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  1549
/**
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1550
 * Array of known HTTP status/error codes
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1551
 */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1552
 
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1553
$http_responses = array(
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1554
    200 => 'OK',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1555
    302 => 'Found',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1556
    307 => 'Temporary Redirect',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1557
    400 => 'Bad Request',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1558
    401 => 'Unauthorized',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1559
    403 => 'Forbidden',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1560
    404 => 'Not Found',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1561
    405 => 'Method Not Allowed',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1562
    406 => 'Not Acceptable',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1563
    500 => 'Internal Server Error',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1564
    501 => 'Not Implemented'
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1565
  );
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1566
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1567
/**
24
d275dc8f4203 Added HttpSuccess exception which can be thrown when a scriptlet is done executing normally
Dan
parents: 23
diff changeset
  1568
 * Array of default extension->mimetype mappings
0
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1569
 */
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1570
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1571
$mime_types = array(
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1572
    'html' => 'text/html',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1573
    'htm'  => 'text/html',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1574
    'png'  => 'image/png',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1575
    'gif'  => 'image/gif',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1576
    'jpeg' => 'image/jpeg',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1577
    'jpg'  => 'image/jpeg',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1578
    'js'   => 'text/javascript',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1579
    'json' => 'text/x-javascript-json',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1580
    'css'  => 'text/css',
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1581
    'php'  => 'application/x-httpd-php'
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1582
  );
c63de9eb7045 First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff changeset
  1583