00001 #!/usr/bin/perl -w
00002 #
00003 # This is the perl-based module handler. It is the counterpart of mythweb.php
00004 #
00005 # @url $URL: http://svn.mythtv.org/svn/branches/release-0-21-fixes/mythplugins/mythweb/mythweb.pl $
00006 # @date $Date: 2008-02-16 22:25:10 -0500 (Sat, 16 Feb 2008) $
00007 # @version $Revision: 16105 $
00008 # @author $Author: xris $
00009 #
00010
00011 # Load some required modules
00012 use CGI qw/:standard/;
00013 use DBI;
00014 use Cwd 'abs_path';
00015 use File::Basename;
00016
00017 # pwd is / when running under mod_rewrite, so we should chdir to the script
00018 # directory for consistency
00019 chdir dirname(abs_path($ENV{'SCRIPT_FILENAME'} or $0));
00020
00021 # Don't forget to include the current directory in the search path.
00022 substr($ENV{'PATH'}, 0, 0) = '.:';
00023
00024 # Create a cgi object;
00025 our $cgi = new CGI;
00026
00027 # Extract the requested path
00028 our @Path;
00029 $Path[0] = ($ENV{'PATH_INFO'} or url_param('PATH_INFO'));
00030 $Path[0] =~ s#^/+##sg;
00031 $Path[0] =~ s#\s+$##sg;
00032 @Path = split('/', $Path[0]);
00033 shift @Path if ($Path[0] eq 'pl');
00034
00035 # Figure out the root web directory
00036 our $web_root = dirname($ENV{'SCRIPT_NAME'}).'/';
00037 $web_root =~ s#
00038
00039 # Add a directory to the search path?
00040 if ($ENV{'include_path'}) {
00041 $ENV{'PATH'} .= ':'.$ENV{'include_path'};
00042 }
00043
00044 # Work around a lighttpd bug: http://trac.lighttpd.net/trac/ticket/420
00045 foreach my $key (keys %ENV) {
00046 next if ($key eq uc($key));
00047 $ENV{$key} ||= $ENV{uc($key)};
00048 }
00049
00050 # Connect to the database
00051 END { $dbh->disconnect() if ($dbh); }
00052 our $dbh = DBI->connect("dbi:mysql:database=$ENV{'db_name'}:host=$ENV{'db_server'}",
00053 $ENV{'db_login'},
00054 $ENV{'db_password'});
00055 unless ($dbh) {
00056 print header(),
00057 "Cannot connect to database: $!\n\n";
00058 exit;
00059 }
00060
00061 # Find the path to the modules directory
00062 our $modules_dir = dirname(dirname(find_in_path('modules/tv/init.php')));
00063
00064 # Figure out what the user is trying to do
00065 if ($Path[0]) {
00066 if (-e "$modules_dir/$Path[0]") {
00067 if (-e "$modules_dir/$Path[0]/handler.pl") {
00068 require "modules/$Path[0]/handler.pl";
00069 }
00070 else {
00071 print header(),
00072 "Module '$Path[0]' doesn't have a perl handler.";
00073 }
00074 }
00075 elsif ($Path[0] =~ /\w/) {
00076 print header(),
00077 "Unknown module: $Path[0]";
00078 }
00079 }
00080 else {
00081 print header(-location => $web_root);
00082 print " \n";
00083 }
00084
00085 # Exit nicely
00086 exit;
00087
00088 ################################################################################
00089
00090 # Find a file in the current include path
00091 sub find_in_path {
00092 my $file = shift;
00093 # Split out each of the search paths
00094 foreach my $path (@INC, split(/:/, $ENV{'PATH'})) {
00095 # Formulate the absolute path
00096 my $full_path = "$path/$file";
00097 # Exists?
00098 return $full_path if (-e $full_path);
00099 }
00100 return undef;
00101 }
00102