1#  Copyright (C) 2002  Stanislav Sinyagin
2#
3#  This program is free software; you can redistribute it and/or modify
4#  it under the terms of the GNU General Public License as published by
5#  the Free Software Foundation; either version 2 of the License, or
6#  (at your option) any later version.
7#
8#  This program is distributed in the hope that it will be useful,
9#  but WITHOUT ANY WARRANTY; without even the implied warranty of
10#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11#  GNU General Public License for more details.
12#
13#  You should have received a copy of the GNU General Public License
14#  along with this program; if not, write to the Free Software
15#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16
17# Stanislav Sinyagin <ssinyagin@k-open.com>
18
19package Torrus::Renderer::Frontpage;
20use strict;
21use warnings;
22
23use Torrus::ConfigTree;
24use Torrus::Search;
25use Torrus::Log;
26
27use Template;
28use URI::Escape;
29
30# All our methods are imported by Torrus::Renderer;
31
32sub renderUserLogin
33{
34    my $self = shift;
35    my %new_options = @_;
36
37    if( %new_options )
38    {
39        $self->{'options'} = \%new_options;
40    }
41
42    my($t_render, $t_expires, $filename, $mime_type);
43
44    my $cachekey = $self->cacheKey( 'LOGINSCREEN' );
45
46    ($t_render, $t_expires, $filename, $mime_type) =
47        $self->getCache( $cachekey );
48
49    # We don't check the expiration time for login screen
50    if( not defined( $filename ) )
51    {
52        $filename = Torrus::Renderer::newCacheFileName( $cachekey );
53    }
54
55    my $outfile = $Torrus::Global::cacheDir.'/'.$filename;
56
57    $t_expires = time();
58    $mime_type = $Torrus::Renderer::LoginScreen::mimeType;
59    my $tmplfile = $Torrus::Renderer::LoginScreen::template;
60
61    # Create the Template Toolkit processor once, and reuse
62    # it in subsequent render() calls
63
64    if( not defined( $self->{'tt'} ) )
65    {
66        $self->{'tt'} =
67            new Template(INCLUDE_PATH => $Torrus::Global::templateDirs,
68                         TRIM => 1);
69    }
70
71    my $url = $Torrus::Renderer::rendererURL;
72    if( length( $self->{'options'}->{'urlPassTree'} ) > 0 )
73    {
74        $url .= '/' . $self->{'options'}->{'urlPassTree'};
75    }
76
77    my $ttvars =
78    {
79        'url'        => $url,
80        'plainURL'   => $Torrus::Renderer::plainURL,
81        'style'      => sub { return $self->style($_[0]); },
82        'companyName'=> $Torrus::Renderer::companyName,
83        'companyLogo'=> $Torrus::Renderer::companyLogo,
84        'companyURL' => $Torrus::Renderer::companyURL,
85        'lostPasswordURL' => $Torrus::Renderer::lostPasswordURL,
86        'siteInfo'   => $Torrus::Renderer::siteInfo,
87        'version'    => $Torrus::Global::version,
88        'xmlnorm'    => \&Torrus::Renderer::xmlnormalize
89        };
90
91
92    # Pass the options from Torrus::Renderer::render() to Template
93    while( my( $opt, $val ) = each( %{$self->{'options'}} ) )
94    {
95        $ttvars->{$opt} = $val;
96    }
97
98    my $result = $self->{'tt'}->process( $tmplfile, $ttvars, $outfile );
99
100    undef $ttvars;
101
102    my @ret;
103    if( not $result )
104    {
105        Error("Error while rendering login screen: " .
106              $self->{'tt'}->error());
107    }
108    else
109    {
110        $self->setCache($cachekey, time(), $t_expires, $filename, $mime_type);
111        @ret = ($outfile, $mime_type, $t_expires - time());
112    }
113
114    $self->{'options'} = undef;
115
116    return @ret;
117}
118
119
120sub renderTreeChooser
121{
122    my $self = shift;
123    my %new_options = @_;
124
125    if( %new_options )
126    {
127        $self->{'options'} = \%new_options;
128    }
129
130    my($t_render, $t_expires, $filename, $mime_type);
131
132    my $uid = '';
133    if( $self->{'options'}->{'uid'} )
134    {
135        $uid = $self->{'options'}->{'uid'};
136    }
137
138    my $cachekey = $self->cacheKey( $uid . ':' . 'TREECHOOSER' );
139
140    ($t_render, $t_expires, $filename, $mime_type) =
141        $self->getCache( $cachekey );
142
143    if( defined( $filename ) )
144    {
145        if( $t_expires >= time() )
146        {
147            return ($Torrus::Global::cacheDir.'/'.$filename,
148                    $mime_type, $t_expires - time());
149        }
150        # Else reuse the old filename
151    }
152    else
153    {
154        $filename = Torrus::Renderer::newCacheFileName( $cachekey );
155    }
156
157    my $outfile = $Torrus::Global::cacheDir.'/'.$filename;
158
159    $t_expires = time() + $Torrus::Renderer::Chooser::expires;
160    $mime_type = $Torrus::Renderer::Chooser::mimeType;
161
162    my $tmplfile;
163    if( defined( $self->{'options'}{'variables'}{'SEARCH'} ) and
164        $self->mayGlobalSearch() )
165    {
166        $tmplfile = $Torrus::Renderer::Chooser::searchTemplate;
167    }
168    else
169    {
170        $tmplfile = $Torrus::Renderer::Chooser::template;
171    }
172
173    # Create the Template Toolkit processor once, and reuse
174    # it in subsequent render() calls
175
176    if( not defined( $self->{'tt'} ) )
177    {
178        $self->{'tt'} =
179            new Template(INCLUDE_PATH => $Torrus::Global::templateDirs,
180                         TRIM => 1);
181    }
182
183    my $ttvars =
184    {
185        'treeNames' => sub{ return Torrus::SiteConfig::listTreeNames() },
186        'treeDescr' => sub{ return
187                                Torrus::SiteConfig::treeDescription($_[0]) }
188        ,
189        'url'  => sub { return $Torrus::Renderer::rendererURL . '/' . $_[0] },
190        'pathUrl'  => sub { return $Torrus::Renderer::rendererURL . '/' .
191                                $_[0] . '?path=' . $_[1] },
192        'plainURL'   => $Torrus::Renderer::plainURL,
193        'clearVar'   => sub { delete $self->{'options'}{'variables'}{$_[0]};
194                              return undef;},
195        'style'      => sub { return $self->style($_[0]); },
196        'companyName'=> $Torrus::Renderer::companyName,
197        'companyLogo'=> $Torrus::Renderer::companyLogo,
198        'companyURL' => $Torrus::Renderer::companyURL,
199        'siteInfo'   => $Torrus::Renderer::siteInfo,
200        'version'    => $Torrus::Global::version,
201        'xmlnorm'    => \&Torrus::Renderer::xmlnormalize,
202        'userAuth'   => $Torrus::CGI::authorizeUsers,
203        'uid'        => $self->{'options'}->{'uid'},
204        'userAttr'   => sub { return $self->userAttribute( $_[0] ) },
205        'mayDisplayTree' => sub { return $self->
206                                      hasPrivilege( $_[0], 'DisplayTree' ) }
207        ,
208        'mayGlobalSearch' => sub { return $self->mayGlobalSearch(); },
209        'searchResults'   => sub { return $self->doGlobalSearch($_[0]); }
210    };
211
212
213    # Pass the options from Torrus::Renderer::render() to Template
214    while( my( $opt, $val ) = each( %{$self->{'options'}} ) )
215    {
216        $ttvars->{$opt} = $val;
217    }
218
219    my $result = $self->{'tt'}->process( $tmplfile, $ttvars, $outfile );
220
221    undef $ttvars;
222
223    my @ret;
224    if( not $result )
225    {
226        Error("Error while rendering tree chooser: " .
227              $self->{'tt'}->error());
228    }
229    else
230    {
231        $self->setCache($cachekey, time(), $t_expires, $filename, $mime_type);
232        @ret = ($outfile, $mime_type, $t_expires - time());
233    }
234
235    $self->{'options'} = undef;
236
237    return @ret;
238}
239
240
241sub mayGlobalSearch
242{
243    my $self = shift;
244
245    return ( $Torrus::Renderer::globalSearchEnabled and
246             ( not $Torrus::CGI::authorizeUsers or
247               ( $self->hasPrivilege( '*', 'GlobalSearch' ) ) ) );
248}
249
250sub doGlobalSearch
251{
252    my $self = shift;
253    my $string = shift;
254
255    my $sr = new Torrus::Search;
256    $sr->openGlobal();
257    my $result = $sr->searchPrefix( $string );
258
259    my $sorted = [];
260    push( @{$sorted}, sort {$a->[0] cmp $b->[0]} @{$result} );
261
262    # remove duplicating entries
263    my %seen;
264    my $ret = [];
265
266    foreach my $element ( @{$sorted} )
267    {
268        my $str = join( ':', $element->[0], $element->[1] );
269        if( not $seen{$str} )
270        {
271            $seen{$str} = 1;
272            push( @{$ret}, $element );
273        }
274    }
275
276    return $ret;
277}
278
279
280
281
2821;
283
284
285# Local Variables:
286# mode: perl
287# indent-tabs-mode: nil
288# perl-indent-level: 4
289# End:
290