~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/lib/Foswiki/Engine/CLI.pm

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# See bottom of file for license and copyright information
 
2
 
 
3
=begin TML
 
4
 
 
5
---+!! package Foswiki::Engine::CLI
 
6
 
 
7
Class that implements CGI scripts functionality when called from
 
8
command line or cron job
 
9
 
 
10
Refer to Foswiki::Engine documentation for explanation about methos below.
 
11
 
 
12
=cut
 
13
 
 
14
package Foswiki::Engine::CLI;
 
15
use base 'Foswiki::Engine';
 
16
 
 
17
use strict;
 
18
use Assert;
 
19
 
 
20
use Foswiki::Request;
 
21
use Foswiki::Request::Upload;
 
22
use Foswiki::Response;
 
23
 
 
24
sub run {
 
25
    my $this = shift;
 
26
    my @args = @ARGV;    # Copy, so original @ARGV doesn't get modified
 
27
    while ( scalar @args ) {
 
28
        my $name;
 
29
        my $arg = shift @args;
 
30
        if ( $arg =~ /^-([a-z0-9_]+)/) {
 
31
            ($name, $arg) = (TAINT($1), shift( @args ));
 
32
        } elsif ( $arg =~ /([a-z0-9_]+)=(.*)$/i ) {
 
33
            ($name, $arg) = (TAINT($1), TAINT($2));
 
34
        }
 
35
        if ($name && $name eq 'user' ) {
 
36
            $this->{user} = $arg;
 
37
        }
 
38
        elsif ($name) {
 
39
            push @{ $this->{plist} }, $name
 
40
              unless exists $this->{params}->{$name};
 
41
            push @{ $this->{params}->{$name} }, $arg;
 
42
        }
 
43
        else {
 
44
            $this->{path_info} = $arg; # keep it tainted
 
45
        }
 
46
    }
 
47
    my $req = $this->prepare;
 
48
    if ( UNIVERSAL::isa( $req, 'Foswiki::Request' ) ) {
 
49
        my $res = Foswiki::UI::handleRequest($req);
 
50
        $this->finalize( $res, $req );
 
51
    }
 
52
}
 
53
 
 
54
sub prepareConnection {
 
55
    my ( $this, $req ) = @_;
 
56
    $req->remoteAddress('127.0.0.1');
 
57
}
 
58
 
 
59
sub prepareQueryParameters {
 
60
    my ( $this, $req ) = @_;
 
61
    foreach my $name ( @{ $this->{plist} } ) {
 
62
        $req->param( -name => $name, -value => $this->{params}->{$name} );
 
63
    }
 
64
    delete $this->{plist};
 
65
    delete $this->{params};
 
66
}
 
67
 
 
68
sub prepareHeaders {
 
69
    my ( $this, $req ) = @_;
 
70
    if ( defined $this->{user} ) {
 
71
        $req->remoteUser( $this->{user} );
 
72
        delete $this->{user};
 
73
    }
 
74
    else {
 
75
        $req->remoteUser( $Foswiki::cfg{SuperAdminGroup} );
 
76
    }
 
77
}
 
78
 
 
79
sub preparePath {
 
80
    my ( $this, $req ) = @_;
 
81
    if ( $ENV{FOSWIKI_ACTION} ) {
 
82
        $req->action( $ENV{FOSWIKI_ACTION} );
 
83
    }
 
84
    else {
 
85
        require File::Spec;
 
86
        $req->action( ( File::Spec->splitpath($0) )[2] );
 
87
    }
 
88
    if ( exists $this->{path_info} ) {
 
89
        $req->pathInfo( $this->{path_info} );
 
90
        delete $this->{path_info};
 
91
    }
 
92
}
 
93
 
 
94
sub prepareCookies { }
 
95
 
 
96
sub finalizeHeaders { }
 
97
 
 
98
sub write {
 
99
    my ( $this, $buffer ) = @_;
 
100
    print $buffer;
 
101
}
 
102
 
 
103
1;
 
104
__DATA__
 
105
# Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/
 
106
#
 
107
# Copyright (C) 2008-2009 Foswiki Contributors. Foswiki Contributors
 
108
# are listed in the AUTHORS file in the root of this distribution.
 
109
# NOTE: Please extend that file, not this notice.
 
110
#
 
111
# Additional copyrights apply to some or all of the code in this
 
112
# file as follows:
 
113
#
 
114
# Copyright (C) 1999-2007 Peter Thoeny, peter@thoeny.org
 
115
# and TWiki Contributors. All Rights Reserved. TWiki Contributors
 
116
# are listed in the AUTHORS file in the root of this distribution.
 
117
#
 
118
# This module is based/inspired on Catalyst framework. Refer to
 
119
#
 
120
# http://search.cpan.org/~mramberg/Catalyst-Runtime-5.7010/lib/Catalyst.pm
 
121
#
 
122
# for credits and liscence details.
 
123
#
 
124
# This program is free software; you can redistribute it and/or
 
125
# modify it under the terms of the GNU General Public License
 
126
# as published by the Free Software Foundation; either version 2
 
127
# of the License, or (at your option) any later version. For
 
128
# more details read LICENSE in the root of this distribution.
 
129
#
 
130
# This program is distributed in the hope that it will be useful,
 
131
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
132
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
133
#
 
134
# As per the GPL, removal of this notice is prohibited.