author | Dan |
Mon, 24 Mar 2008 02:53:42 -0400 | |
changeset 10 | d3059e20b0fa |
parent 8 | a8d108f37363 |
child 12 | b3fcc21e557f |
permissions | -rwxr-xr-x |
6
5f35ebc4f9bb
First release version. Renamed to Greyhound and readme/license files added.
Dan
parents:
5
diff
changeset
|
1 |
#!/usr/bin/env php |
0
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
2 |
<?php |
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 |
/** |
6
5f35ebc4f9bb
First release version. Renamed to Greyhound and readme/license files added.
Dan
parents:
5
diff
changeset
|
5 |
* 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:
4
diff
changeset
|
6 |
* Copyright (C) 2008 Dan Fuhry |
0
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
7 |
* |
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:
4
diff
changeset
|
8 |
* 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:
4
diff
changeset
|
9 |
* 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:
4
diff
changeset
|
10 |
* |
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:
4
diff
changeset
|
11 |
* 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:
4
diff
changeset
|
12 |
* 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
|
13 |
*/ |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
14 |
|
8
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
15 |
// required for signal handling to work |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
16 |
declare(ticks=1); |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
17 |
|
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
18 |
// trap SIGTERM |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
19 |
pcntl_signal(SIGTERM, 'sigterm'); |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
20 |
pcntl_signal(SIGINT, 'sigterm'); |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
21 |
|
0
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
22 |
$public = true; |
4 | 23 |
$allowcontrol = true; |
0
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
24 |
$theme = 'funkymonkey'; |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
25 |
|
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
26 |
@ini_set('display_errors', 'on'); |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
27 |
|
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
28 |
// include files |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
29 |
require('functions.php'); |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
30 |
|
8
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
31 |
// get the root |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
32 |
define('GREY_ROOT', dirname(__FILE__)); |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
33 |
|
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
34 |
// create directories |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
35 |
@mkdir('./compiled'); |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
36 |
|
0
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
37 |
// start up... |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
38 |
|
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
39 |
status('Starting WebControl v0.1-hg'); |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
40 |
status('loading files'); |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
41 |
|
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
42 |
require('webserver.php'); |
8
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
43 |
define('SMARTY_DIR', GREY_ROOT . '/smarty/'); |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
44 |
require(GREY_ROOT . '/smarty/Smarty.class.php'); |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
45 |
require(GREY_ROOT . '/playlist.php'); |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
46 |
require(GREY_ROOT . '/json.php'); |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
47 |
require(GREY_ROOT . '/ajax.php'); |
0
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 |
status('doing home directory detection'); |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
50 |
|
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
51 |
// get home directory |
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 |
if ( !isset($_ENV['HOME']) ) |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
54 |
{ |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
55 |
burnout('Could not get your home directory'); |
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 |
|
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
58 |
$homedir =& $_ENV['HOME']; |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
59 |
|
8
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
60 |
// signal handler |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
61 |
function sigterm($signal) |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
62 |
{ |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
63 |
global $httpd; |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
64 |
status("Caught SIGTERM, cleaning up."); |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
65 |
@socket_close($httpd->sock); |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
66 |
exit(); |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
67 |
} |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
68 |
|
0
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
69 |
status('initializing playlist'); |
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 |
// init playlist object |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
72 |
$playlist = array(); |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
73 |
rebuild_playlist(); |
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 |
// startup webserver |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
76 |
$ip = ( $public ) ? '0.0.0.0' : '127.0.0.1'; |
3
e7447a6044ec
$allowcontrol = false working and turned on now; switched default port back to 7447 instead of random; added favicon and apple-touch-icon
Dan
parents:
2
diff
changeset
|
77 |
$port = 7447; |
0
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
78 |
|
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
79 |
try |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
80 |
{ |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
81 |
status('starting PhpHttpd'); |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
82 |
$httpd = new WebServer($ip, $port); |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
83 |
|
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
84 |
// setup handlers |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
85 |
status('initializing handlers'); |
8
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
86 |
$httpd->add_handler('index', 'function', 'amarok_playlist'); |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
87 |
$httpd->add_handler('action.json', 'function', 'ajax_request_handler'); |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
88 |
$httpd->add_handler('scripts', 'dir', GREY_ROOT . '/scripts'); |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
89 |
$httpd->add_handler('favicon.ico', 'file', GREY_ROOT . '/amarok_icon.ico'); |
a8d108f37363
Modified to trap signals properly and not write to the root directory (only working directory)
Dan
parents:
7
diff
changeset
|
90 |
$httpd->add_handler('apple-touch-icon.png', 'file', GREY_ROOT . '/apple-touch-icon.png'); |
0
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
91 |
$httpd->allow_dir_list = true; |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
92 |
$httpd->default_document = 'index'; |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
93 |
|
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
94 |
status("Entering main server loop - ^C to interrupt, listening on port $port"); |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
95 |
$httpd->serve(); |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
96 |
} |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
97 |
catch( Exception $e ) |
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 |
burnout("Exception caught while running webserver:\n$e"); |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
100 |
} |
c63de9eb7045
First commit. Basic things are working and implemented (webserver, templating, DCOP interface)
Dan
parents:
diff
changeset
|
101 |