On Thu, Dec 15, 2005 at 03:13:37PM -0500, Charles Fry wrote:
> > Would you be interested in taking my dinky little fixes and running with 
> > them?

> Absolutely! Thank you for your willingness to contribute them back to the 
> package.

Attached.  This is definitely not ready to be stuck into the 
libhtml-mason-perl package as-is.  While I've used Mason for developing 
sites for quite a while, I don't understand much at all about the internals.

Summary of what I did:

* Comment out the "use Apache::Constant" line and change "return DECLINED" 
to "return -1" throughout.

* Comment out the "my $apr = Apache::Request..." line and all 
"$apr->log->..." lines that follow.  (I.e., there's no logging being done.)

* Change "/var/spool/mason" to "/var/cache/mason/users", where per-user 
component roots will be created.  The "users" directory is not automatically 
created--I did it this way because default permission don't allow the 
per-user directories to be created in /var/cache/mason.

Like I said, I wanted to allow for each user to have their own perl module 
directory, but don't know how to do it.  You can't 'use lib 
"/home/$username/lib" because $username is undef at compile time, and 
'unshift @INC, "/home/$username/lib"' puts the path into @INC, but modules 
there still aren't found for some reason.

I put an .htaccess in my public_html directory like so, and it works:

<FilesMatch "\.html$">
   SetHandler perl-script
   PerlHandler HTML::Mason::ApacheUserDirHandler
</FilesMatch>

-- 
Troy Davis
[EMAIL PROTECTED]
http://www.tdavis.org/
# -*- perl -*-

# Written by Steve Haslam. All rights reserved.
# This program is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.

# HTML::Mason::ApacheUserDirHandler
#  Run as a mod_perl request handler, create
#  HTML::Mason::ApacheHandler objects on demand to handle requests,
#  keying on the username from the URI. Keep the created ApacheHandler
#  objects in a cache.

require 5;
package HTML::Mason::ApacheUserDirHandler;
use strict;
#use Apache::Constants qw(:common);
use Apache2::Log;
use HTML::Mason::ApacheHandler;
use vars qw(%userdir_handlers);

sub handler {
    my $r = shift;

#    my $apr = Apache2::Request->new($r);

#    $apr->log->debug("Finding correct handler for ".$r->uri);

    if ($r->uri =~ m|^/~([a-zA-Z][a-zA-Z0-9_]*)/(.*)|) {
	my($username, $subpath) = ($1, $2);

	my $h = $userdir_handlers{$username};
	if ($h) {
	    if (ref($h)) {
#		$apr->log->debug("Reusing $h for user \"$username\"");
		return $h->handle_request($r);
	    }
	    else {
#		$apr->log->debug("Skipping previously bad username \"$username\" ($h)");
		return -1;
	    }
	}

#	$apr->log->debug("Trying to create new handler for \"$username\"");
	my ($u_name, $u_passwd, $u_uid, $u_gid, $u_quota, $u_comment, $u_gcos, $u_dir, $u_shell, $u_expire) = getpwnam($username);
	if (!$u_name) {
#	    $apr->log->error("User \"$username\" not found");
	    $userdir_handlers{$username} = "User not found";
	    return -1;
	}

	if (!-d $u_dir) {
#	    $apr->log->error("User \"$username\" has non-existent home directory \"$u_dir\"");
	    $userdir_handlers{$username} = "Home directory does not exist";
	    return -1;
	}

	my $comp_root = "$u_dir/public_html";
	if (!-d $comp_root) {
#	    $apr->log->error("User \"$username\": proposed component root $comp_root does not exist");
	    $userdir_handlers{$username} = "Proposed component root does not exist";
	    return -1;
	}

	eval {
	    my $spooldir = $r->dir_config('XMasonSpoolDir') || "/var/cache/mason/users";
	    my $spoolname = $username; # Vet $username here if we expand the regex to match it above
	    $h = HTML::Mason::ApacheHandler->new(data_dir => "$spooldir/$spoolname", comp_root => $comp_root,
						 apache_status_title => "HTML::Mason status (for $username)",
						 in_package => "HTML::Mason::UserDirCommands::$username");
	};
	if ($@) {
	    my $err = $@;
#	    $apr->log->error("Failed to create Mason handler object: $err");
	    $userdir_handlers{$username} = $err;
	    return -1;
	}
	
#	$apr->log->debug("New handler created as $h, chaining request");
	$userdir_handlers{$username} = $h;
	return $h->handle_request($r);
    }
    else {
#	$apr->log->debug("$r->uri does not look like a userdir URI, declining");
	return -1;
    }
}

__END__

Reply via email to