/
Web.pm
3815 lines (3252 loc) · 160 KB
/
Web.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
#
# This code was forked from the LiveJournal project owned and operated
# by Live Journal, Inc. The code has been modified and expanded by
# Dreamwidth Studios, LLC. These files were originally licensed under
# the terms of the license supplied by Live Journal, Inc, which can
# currently be found at:
#
# http://code.livejournal.org/trac/livejournal/browser/trunk/LICENSE-LiveJournal.txt
#
# In accordance with the original license, this code and all its
# modifications are provided under the GNU General Public License.
# A copy of that license can be found in the LICENSE file included as
# part of this distribution.
package LJ;
use strict;
use Carp;
use POSIX;
use DW::External::Site;
use DW::Request;
use LJ::Event;
use LJ::Subscription::Pending;
use LJ::Directory::Search;
use LJ::Directory::Constraint;
use LJ::PageStats;
# <LJFUNC>
# name: LJ::img
# des: Returns an HTML <img> or <input> tag to an named image
# code, which each site may define with a different image file with
# its own dimensions. This prevents hard-coding filenames & sizes
# into the source. The real image data is stored in LJ::Img, which
# has default values provided in cgi-bin/LJ/Global/Img.pm but can be
# overridden in cgi-bin/LJ/Local/Img.pm or etc/config.pl.
# args: imagecode, type?, attrs?
# des-imagecode: The unique string key to reference the image. Not a filename,
# but the purpose or location of the image.
# des-type: By default, the tag returned is an <img> tag, but if 'type'
# is "input", then an input tag is returned.
# des-attrs: Optional hashref of other attributes. If this isn't a hashref,
# then it's assumed to be a scalar for the 'name' attribute for
# input controls.
# </LJFUNC>
sub img
{
my $ic = shift;
my $type = shift; # either "" or "input"
my $attr = shift;
my ( $attrs, $alt, $ssl ) = ( '', '', 0 );
if ( $attr ) {
if ( ref $attr eq "HASH" ) {
if ( exists $attr->{alt} ) {
$alt = LJ::ehtml( $attr->{alt} );
delete $attr->{alt};
}
if ( exists $attr->{ssl} ) {
$ssl = $attr->{ssl} && $LJ::IS_SSL;
delete $attr->{ssl};
}
$attrs .= " $_=\"" . LJ::ehtml( $attr->{$_} || '' ) . "\""
foreach keys %$attr;
} else {
$attrs = " name=\"$attr\"";
}
}
my $i = $LJ::Img::img{$ic};
my $imgprefix = $ssl ? $LJ::SSLIMGPREFIX : $LJ::IMGPREFIX;
$alt ||= LJ::Lang::string_exists( $i->{alt} ) ? LJ::Lang::ml( $i->{alt} ) : $i->{alt};
if ($type eq "") {
return "<img src=\"$imgprefix$i->{src}\" width=\"$i->{width}\" ".
"height=\"$i->{height}\" alt=\"$alt\" title=\"$alt\" ".
"border='0'$attrs />";
}
if ($type eq "input") {
return "<input type=\"image\" src=\"$LJ::IMGPREFIX$i->{'src'}\" ".
"width=\"$i->{'width'}\" height=\"$i->{'height'}\" title=\"$alt\" ".
"alt=\"$alt\" border='0'$attrs />";
}
return "<b>XXX</b>";
}
# <LJFUNC>
# name: LJ::date_to_view_links
# class: component
# des: Returns HTML of date with links to user's journal.
# args: u, date
# des-date: date in yyyy-mm-dd form.
# returns: HTML with yyyy, mm, and dd all links to respective views.
# </LJFUNC>
sub date_to_view_links
{
my ($u, $date) = @_;
return unless $date =~ /^(\d\d\d\d)-(\d\d)-(\d\d)/;
my ($y, $m, $d) = ($1, $2, $3);
my $base = $u->journal_base;
my $ret;
$ret .= "<a href=\"$base/$y/\">$y</a>-";
$ret .= "<a href=\"$base/$y/$m/\">$m</a>-";
$ret .= "<a href=\"$base/$y/$m/$d/\">$d</a>";
return $ret;
}
# <LJFUNC>
# name: LJ::auto_linkify
# des: Takes a plain-text string and changes URLs into <a href> tags (auto-linkification).
# args: str
# des-str: The string to perform auto-linkification on.
# returns: The auto-linkified text.
# </LJFUNC>
sub auto_linkify
{
my $str = shift;
my $match = sub {
my $str = shift;
if ($str =~ /^(.*?)(&(#39|quot|lt|gt)(;.*)?)$/) {
return "<a href='$1'>$1</a>$2";
} else {
return "<a href='$str'>$str</a>";
}
};
$str =~ s!(https?://[^\s\'\"\<\>]+[a-zA-Z0-9_/&=\-])! $match->( $1 ); !ge
if defined $str;
return $str;
}
# return 1 if URL is a safe stylesheet that S1/S2/etc can pull in.
# return 0 to reject the link tag
# return a URL to rewrite the stylesheet URL
# $href will always be present. $host and $path may not.
sub valid_stylesheet_url {
my ($href, $host, $path) = @_;
unless ($host && $path) {
return 0 unless $href =~ m!^https?://([^/]+?)(/.*)$!;
($host, $path) = ($1, $2);
}
my $cleanit = sub {
# allow tag, if we're doing no css cleaning
return 1 unless LJ::is_enabled('css_cleaner');
# remove tag, if we have no CSSPROXY configured
return 0 unless $LJ::CSSPROXY;
# rewrite tag for CSS cleaning
return "$LJ::CSSPROXY?u=" . LJ::eurl($href);
};
return 1 if $LJ::TRUSTED_CSS_HOST{$host};
return $cleanit->() unless $host =~ /\Q$LJ::DOMAIN\E$/i;
# let users use system stylesheets.
return 1 if $host eq $LJ::DOMAIN || $host eq $LJ::DOMAIN_WEB ||
$href =~ /^\Q$LJ::STATPREFIX\E/;
# S2 stylesheets:
return 1 if $path =~ m!^(/\w+)?/res/(\d+)/stylesheet(\?\d+)?$!;
# unknown, reject.
return $cleanit->();
}
# <LJFUNC>
# name: LJ::make_authas_select
# des: Given a u object and some options, determines which users the given user
# can switch to. If the list exists, returns a select list and a submit
# button with labels. Otherwise returns a hidden element.
# returns: string of HTML elements
# args: u, opts?
# des-opts: Optional. Valid keys are:
# 'authas' - current user, gets selected in drop-down;
# 'label' - label to go before form elements;
# 'button' - button label for submit button;
# 'type' - journaltype (affects label & list filtering)
# others - arguments to pass to $u->get_authas_list.
# </LJFUNC>
sub make_authas_select {
my ( $u, $opts ) = @_; # type, authas, label, button
my $authas = $opts->{authas} || $u->user;
my $button = $opts->{button} || $BML::ML{'web.authas.btn'};
my $foundation = $opts->{foundation} || 0;
my @list = $u->get_authas_list( $opts );
# only do most of form if there are options to select from
if ( @list > 1 || $list[0] ne $u->user ) {
my $menu = LJ::html_select( { name => 'authas', selected => $authas,
class => 'hideable', id => 'authas' },
map { $_, $_ } @list );
my $ret = '';
if ( $opts->{selectonly} ) {
$ret = $menu;
} else {
$ret = $foundation
? q{<div class='row collapse'><div class='columns medium-1'><label class='inline'>} . LJ::Lang::ml( 'web.authas.select.label' ) . q{</label></div>}
. q{<div class='columns medium-11'><div class='row'>}
. q{<div class='columns medium-4'>} . $menu . q{</div>}
. q{<div class='columns medium-2 end'>} . LJ::html_submit( undef, $button, { class => "secondary button"} ) . q{</div>}
. q{</div></div>}
. q{</div>}
: "<br/>"
. LJ::Lang::ml( 'web.authas.select', { menu => $menu, username => LJ::ljuser($authas) } ) . " " . LJ::html_submit( undef, $button )
. "<br/><br/>\n";
}
return $ret;
}
# no communities to choose from, give the caller a hidden
return LJ::html_hidden( authas => $authas );
}
# <LJFUNC>
# name: LJ::make_postto_select
# des: Given a u object and some options, determines which users the given user
# can post to. If the list exists, returns a select list and a submit
# button with labels. Otherwise returns a hidden element.
# returns: string of HTML elements
# args: u, opts?
# des-opts: Optional. Valid keys are:
# 'authas' - current user, gets selected in drop-down;
# 'label' - label to go before form elements;
# 'button' - button label for submit button;
# </LJFUNC>
sub make_postto_select {
my ( $u, $opts ) = @_;
my $authas = $opts->{authas} || $u->user;
my $label = $opts->{label} || $BML::ML{'web.postto.label'};
my $button = $opts->{button} || $BML::ML{'web.postto.btn'};
my @list = ( $u, $u->posting_access_list );
# only do most of form if there are options to select from
if ( @list > 1 ) {
return "$label " .
LJ::html_select( { name => 'authas', selected => $authas },
map { $_->user, $_->user } @list ) . " " .
LJ::html_submit( undef, $button );
}
# no communities to choose from, give the caller a hidden
return LJ::html_hidden( authas => $authas );
}
# <LJFUNC>
# name: LJ::help_icon
# des: Returns BML to show a help link/icon given a help topic, or nothing
# if the site hasn't defined a URL for that topic. Optional arguments
# include HTML/BML to place before and after the link/icon, should it
# be returned.
# args: topic, pre?, post?
# des-topic: Help topic key.
# See etc/config-local.pl, or [special[helpurls]] for examples.
# des-pre: HTML/BML to place before the help icon.
# des-post: HTML/BML to place after the help icon.
# </LJFUNC>
sub help_icon
{
my $topic = shift;
my $pre = shift;
my $post = shift;
return "" unless (defined $LJ::HELPURL{$topic});
return "$pre<?help $LJ::HELPURL{$topic} help?>$post";
}
# like help_icon, but no BML.
sub help_icon_html {
my $topic = shift;
my $url = $LJ::HELPURL{$topic} or return "";
my $pre = shift || "";
my $post = shift || "";
return "$pre<a href=\"$url\" class=\"helplink\" target=\"_blank\">" .
LJ::img( 'help', '' ) . "</a>$post";
}
# <LJFUNC>
# name: LJ::bad_input
# des: Returns common BML for reporting form validation errors in
# a bulleted list.
# returns: BML showing errors.
# args: error*
# des-error: A list of errors
# </LJFUNC>
sub bad_input
{
my @errors = @_;
my $ret = "";
$ret .= "<?badcontent?>\n<ul>\n";
foreach my $ei (@errors) {
my $err = LJ::errobj($ei) or next;
$err->log;
$ret .= $err->as_bullets;
}
$ret .= "</ul>\n";
return $ret;
}
# <LJFUNC>
# name: LJ::error_list
# des: Returns an error bar with bulleted list of errors.
# returns: BML showing errors.
# args: error*
# des-error: A list of errors
# </LJFUNC>
sub error_list
{
# FIXME: retrofit like bad_input above? merge? make aliases for each other?
my @errors = @_;
my $ret;
$ret .= "<?errorbar ";
$ret .= "<strong>";
$ret .= BML::ml('error.procrequest');
$ret .= "</strong><ul>";
foreach my $ei (@errors) {
my $err = LJ::errobj($ei) or next;
$err->log;
$ret .= $err->as_bullets;
}
$ret .= " </ul> errorbar?>";
return $ret;
}
# <LJFUNC>
# name: LJ::error_noremote
# des: Returns an error telling the user to log in.
# returns: Translation string "error.notloggedin"
# </LJFUNC>
sub error_noremote
{
return "<?needlogin?>";
}
# <LJFUNC>
# name: LJ::warning_list
# des: Returns a warning bar with bulleted list of warnings.
# returns: BML showing warnings
# args: warnings*
# des-warnings: A list of warnings
# </LJFUNC>
sub warning_list
{
my @warnings = @_;
my $ret;
$ret .= "<?warningbar ";
$ret .= "<strong>";
$ret .= BML::ml('label.warning');
$ret .= "</strong><ul>";
foreach (@warnings) {
$ret .= "<li>$_</li>";
}
$ret .= " </ul> warningbar?>";
return $ret;
}
# <LJFUNC>
# name: LJ::did_post
# des: Cookies should only show pages which make no action.
# When an action is being made, check the request coming
# from the remote user is a POST request.
# info: When web pages are using cookie authentication, you can't just trust that
# the remote user wants to do the action they're requesting. It's way too
# easy for people to force other people into making GET requests to
# a server. What if a user requested http://server/delete_all_journal.bml,
# and that URL checked the remote user and immediately deleted the whole
# journal? Now anybody has to do is embed that address in an image
# tag and a lot of people's journals will be deleted without them knowing.
# Cookies should only show pages which make no action. When an action is
# being made, check that it's a POST request.
# returns: true if REQUEST_METHOD == "POST"
# </LJFUNC>
sub did_post
{
return (BML::get_method() eq "POST");
}
# <LJFUNC>
# name: LJ::robot_meta_tags
# des: Returns meta tags to instruct a robot/crawler to not index or follow links.
# returns: A string with appropriate meta tags
# </LJFUNC>
sub robot_meta_tags
{
return "<meta name=\"robots\" content=\"noindex, nofollow, noarchive\" />\n" .
"<meta name=\"googlebot\" content=\"noindex, nofollow, noarchive, nosnippet\" />\n";
}
sub paging_bar {
my ( $page, $pages, $opts ) = @_;
my $self_link = $opts->{self_link} ||
sub { LJ::page_change_getargs( page => $_[0] ) };
my $href_opts = $opts->{href_opts} || sub { '' };
my $nav = '';
return $nav unless $pages && $pages > 1;
$nav .= "<p style='font-weight: bolder; margin: 0 0 .5em 0'>";
$nav .= LJ::Lang::ml( 'ljlib.pageofpages',
{ page => $page, total => $pages } );
$nav .= "</p>\n";
my $linkify = sub { "<a href='" . $self_link->( $_[0] ) . "'" .
$href_opts->( $_[0] ) . ">$_[1]</a>\n" };
my ( $left, $right ) = ( "<b><<</b>", "<b>>></b>" );
$left = $linkify->( $page - 1, $left ) if $page > 1;
$right = $linkify->( $page + 1, $right ) if $page < $pages;
my @pagelinks;
for ( my $i = 1; $i <= $pages; $i++ ) {
my $link = "[$i]";
$link = ( $i != $page ) ? $linkify->( $i, $link ) : "<b>$link</b>";
push @pagelinks, "<br />" if $i > 10 && ( $i == 11 || $i % 10 == 0 );
push @pagelinks, $link;
}
$nav .= "$left ";
$nav .= "<span style='text-align: center'>";
$nav .= join ' ', @pagelinks;
$nav .= "</span>";
$nav .= " $right";
return "<div class='action-box'><div class='inner'>$nav</div></div><div class='clear-floats'></div>\n";
}
=head2 C<< LJ::page_change_getargs( %args ) >>
Returns the current URL with a modified list of GET arguments.
=cut
sub page_change_getargs {
my %args = @_;
my %cu_opts = ( keep_args => 1, no_blank => 1 );
# specified args will override keep_args
return LJ::create_url( undef, args => \%args, %cu_opts );
}
=head2 C<< LJ::paging( $listref, $page, $pagesize ) >>
Drop-in replacement for BML::paging in non-BML context.
=cut
sub paging {
my ( $listref, $page, $pagesize ) = @_;
$page = 1 unless $page && $page == int $page;
return unless $pagesize; # let's not divide by zero
my @items = @{ $listref };
my %self;
my $newurl = sub {
# replaces BML::page_newurl
return LJ::page_change_getargs( page => $_[0] );
};
$self{itemcount} = scalar @items;
$self{pages} = $self{itemcount} / $pagesize;
$self{pages} = int( $self{pages} ) + 1
if $self{pages} != int( $self{pages} ); # round up any fraction
$page = 1 if $page < 1;
$page = $self{pages} if $page > $self{pages};
$self{page} = $page;
$self{itemfirst} = $pagesize * ( $page - 1 ) + 1;
$self{itemlast} = $pagesize * $page;
$self{itemlast} = $self{itemcount} if $self{pages} == $page;
my @range = ( $self{itemfirst} - 1 ) .. ( $self{itemlast} - 1 );
$self{items} = [ @items[@range] ];
my ( $prev, $next ) = ( $newurl->( $page - 1 ), $newurl->( $page + 1 ) );
$self{backlink} = "<a href=\"$prev\"><<<</a>" unless $page == 1;
$self{nextlink} = "<a href=\"$next\">>>></a>" unless $page == $self{pages};
return %self;
}
# Returns HTML to display user search results
# Args: %args
# des-args:
# users => hash ref of userid => u object like LJ::load userids
# returns or array ref of user objects
# userids => array ref of userids to include in results, ignored
# if users is defined
# timesort => set to 1 to sort by last updated instead
# of username
# perpage => Enable pagination and how many users to display on
# each page
# curpage => What page of results to display
# navbar => Scalar reference for paging bar
# pickwd => userpic keyword to display instead of default if it
# exists for the user
# self_link => Sub ref to generate link to use for pagination
sub user_search_display {
my %args = @_;
my $loaded_users;
unless (defined $args{users}) {
$loaded_users = LJ::load_userids(@{$args{userids}});
} else {
if (ref $args{users} eq 'HASH') { # Assume this is direct from LJ::load_userids
$loaded_users = $args{users};
} elsif (ref $args{users} eq 'ARRAY') { # They did a grep on it or something
foreach (@{$args{users}}) {
$loaded_users->{$_->userid} = $_;
}
} else {
return undef;
}
}
# If we're sorting by last updated, we need to load that
# info for all users before the sort. If sorting by
# username we can load it for a subset of users later,
# if paginating.
my $updated;
my $disp_sort;
if ($args{timesort}) {
$updated = LJ::get_timeupdate_multi(keys %$loaded_users);
my $def_upd = sub { $updated->{$_[0]->userid} || 0 };
# let undefined values be zero for sorting purposes
$disp_sort = sub { $def_upd->($b) <=> $def_upd->($a) };
} else {
$disp_sort = sub { $a->{user} cmp $b->{user} };
}
my @display = sort $disp_sort values %$loaded_users;
if (defined $args{perpage}) {
my %items = LJ::paging( \@display, $args{curpage}, $args{perpage} );
# Fancy paging bar
my $opts;
$opts->{self_link} = $args{self_link} if $args{self_link};
${$args{navbar}} = LJ::paging_bar($items{'page'}, $items{'pages'}, $opts);
# Now pull out the set of users to display
@display = @{$items{'items'}};
}
# If we aren't sorting by time updated, load last updated time for the
# set of users we are displaying.
$updated = LJ::get_timeupdate_multi( map { $_->userid } @display )
unless $args{timesort};
# Allow caller to specify a custom userpic to use instead
# of the user's default all userpics
my $get_picid = sub {
my $u = shift;
return $u->{'defaultpicid'} unless defined $args{'pickwd'};
return $u->get_picid_from_keyword( $args{pickwd} );
};
my $ret;
foreach my $u (@display) {
# We should always have loaded user objects, but it seems
# when the site is overloaded we don't always load the users
# we request.
next unless LJ::isu($u);
$ret .= "<div class='user-search-display'>";
$ret .= "<table summary='' style='height: 105px'><tr>";
$ret .= "<td style='width: 100px; text-align: center;'>";
$ret .= "<a href='" . $u->allpics_base . "'>";
if (my $picid = $get_picid->($u)) {
$ret .= "<img src='$LJ::USERPIC_ROOT/$picid/" . $u->userid . "' alt='";
$ret .= $u->user . " userpic' style='border: 1px solid #000;' />";
} else {
$ret .= LJ::img( "nouserpic", "",
{ style => 'border: 1px solid #000;' } );
}
$ret .= "</a>";
$ret .= "</td><td style='padding-left: 5px;' valign='top'><table summary=''>";
$ret .= "<tr><td class='searchusername' colspan='2' style='text-align: left;'>";
$ret .= $u->ljuser_display({ head_size => $args{head_size} });
$ret .= "</td></tr><tr>";
if ($u->{name}) {
$ret .= "<td width='1%' style='font-size: smaller' valign='top'>" . BML::ml( 'search.user.name' );
$ret .= "</td><td style='font-size: smaller'><a href='" . $u->profile_url . "'>";
$ret .= LJ::ehtml($u->{name});
$ret .= "</a>";
$ret .= "</td></tr><tr>";
}
if (my $jtitle = $u->prop('journaltitle')) {
$ret .= "<td width='1%' style='font-size: smaller' valign='top'>" . BML::ml( 'search.user.journal' );
$ret .= "</td><td style='font-size: smaller'><a href='" . $u->journal_base . "'>";
$ret .= LJ::ehtml($jtitle) . "</a>";
$ret .= "</td></tr>";
}
$ret .= "<tr><td colspan='2' style='text-align: left; font-size: smaller' class='lastupdated'>";
my $upd = $updated->{$u->userid};
if ( defined $upd && $upd > 0 ) {
$ret .= LJ::Lang::ml( 'search.user.update.last', { time => LJ::diff_ago_text( $upd ) } );
} else {
$ret .= LJ::Lang::ml( 'search.user.update.never' );
}
$ret .= "</td></tr>";
$ret .= "</table>";
$ret .= "</td></tr>";
$ret .= "</table></div>";
}
return $ret;
}
# <LJFUNC>
# class: web
# name: LJ::make_cookie
# des: Prepares cookie header lines.
# returns: An array of cookie lines.
# args: name, value, expires, path?, domain?
# des-name: The name of the cookie.
# des-value: The value to set the cookie to.
# des-expires: The time (in seconds) when the cookie is supposed to expire.
# Set this to 0 to expire when the browser closes. Set it to
# undef to delete the cookie.
# des-path: The directory path to bind the cookie to.
# des-domain: The domain (or domains) to bind the cookie to.
# </LJFUNC>
sub make_cookie
{
my ($name, $value, $expires, $path, $domain) = @_;
my $cookie = "";
my @cookies = ();
# let the domain argument be an array ref, so callers can set
# cookies in both .foo.com and foo.com, for some broken old browsers.
if ($domain && ref $domain eq "ARRAY") {
foreach (@$domain) {
push(@cookies, LJ::make_cookie($name, $value, $expires, $path, $_));
}
return;
}
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($expires);
$year+=1900;
my @day = qw{Sunday Monday Tuesday Wednesday Thursday Friday Saturday};
my @month = qw{Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec};
$cookie = sprintf "%s=%s", LJ::eurl($name), LJ::eurl($value);
# this logic is confusing potentially
unless (defined $expires && $expires==0) {
$cookie .= sprintf "; expires=$day[$wday], %02d-$month[$mon]-%04d %02d:%02d:%02d GMT",
$mday, $year, $hour, $min, $sec;
}
$cookie .= "; path=$path" if $path;
$cookie .= "; domain=$domain" if $domain;
push(@cookies, $cookie);
return @cookies;
}
# <LJFUNC>
# name: LJ::check_referer
# class: web
# des: Checks if the user is coming from a given URI.
# args: uri?, referer?
# des-uri: string; the URI we want the user to come from.
# des-referer: string; the location the user is posting from.
# If not supplied, will be retrieved with BML::get_client_header.
# In general, you don't want to pass this yourself unless
# you already have it or know we can't get it from BML.
# returns: 1 if they're coming from that URI, else undef
# </LJFUNC>
sub check_referer {
my $uri = shift(@_) || '';
my $referer = shift(@_) || BML::get_client_header('Referer');
# get referer and check
return 1 unless $referer;
my ( $origuri, $origreferer ) = ( $uri, $referer );
# escape any regex characters, like the '.' in '.bml'
$uri = quotemeta( $uri );
# check that the end of the uri matches exactly (no extra characters or dir levels)
# or else that the uri is followed immediately by additional parameters
my $checkend = '(?:$|\\?)';
# allow us to properly check URIs without .bml extensions
if ( $origuri =~ /\.bml($|\?)/ ) {
$checkend = '' if $1 eq '?';
$uri =~ s/\\.bml($|\\\?)/$1$checkend/;
$referer =~ s/\.bml($|\?)/$1/;
} elsif ( $uri ) {
$uri .= $checkend;
} else {
$uri = '(/|$)';
}
return 1 if $LJ::SITEROOT && $referer =~ m!^\Q$LJ::SITEROOT\E$uri!;
return 1 if $LJ::SSLROOT && $referer =~ m!^\Q$LJ::SSLROOT\E$uri!;
return 1 if $LJ::DOMAIN && $referer =~ m!^https?://\Q$LJ::DOMAIN\E$uri!;
return 1 if $LJ::DOMAIN_WEB && $referer =~ m!^https?://\Q$LJ::DOMAIN_WEB\E$uri!;
return 1 if $LJ::USER_VHOSTS && $referer =~ m!^https?://([A-Za-z0-9_\-]{1,25})\.\Q$LJ::DOMAIN\E$uri!;
return 1 if $origuri =~ m!^https?://! && $origreferer eq $origuri;
return undef;
}
# Checks if the user is flagged as having a bad password and redirects
# to changepassword page. If returl is on it returns the URL to
# redirect to vs doing the redirect itself. Useful in non-web context
# and for QuickReply links.
sub bad_password_redirect {
my ( $opts ) = @_;
return undef unless LJ::is_enabled( 'force_pass_change' );
my $remote = LJ::get_remote();
return undef unless $remote && $remote->prop( 'badpassword' );
my $redir = "$LJ::SITEROOT/changepassword";
if ( defined $opts->{returl} ) {
return $redir;
} else {
my $r = DW::Request->get;
return $r->redirect( $redir );
}
}
# <LJFUNC>
# name: LJ::form_auth
# class: web
# des: Creates an authentication token to be used later to verify that a form
# submission came from a particular user.
# args: raw?
# des-raw: boolean; If true, returns only the token (no HTML).
# returns: HTML hidden field to be inserted into the output of a page.
# </LJFUNC>
sub form_auth {
my $raw = shift;
my $chal = $LJ::REQ_GLOBAL{form_auth_chal};
unless ($chal) {
my $remote = LJ::get_remote();
my $id = $remote ? $remote->id : 0;
my $sess = $remote && $remote->session ? $remote->session->id : LJ::UniqCookie->current_uniq;
my $auth = join('-', LJ::rand_chars(10), $id, $sess);
$chal = LJ::challenge_generate(86400, $auth);
$LJ::REQ_GLOBAL{form_auth_chal} = $chal;
}
return $raw ? $chal : LJ::html_hidden("lj_form_auth", $chal);
}
# <LJFUNC>
# name: LJ::check_form_auth
# class: web
# des: Verifies form authentication created with [func[LJ::form_auth]].
# returns: Boolean; true if the current data in %POST is a valid form, submitted
# by the user in $remote using the current session,
# or false if the user has changed, the challenge has expired,
# or the user has changed session (logged out and in again, or something).
# </LJFUNC>
sub check_form_auth {
my $formauth = shift || $BMLCodeBlock::POST{'lj_form_auth'};
return 0 unless $formauth;
my $remote = LJ::get_remote();
my $id = $remote ? $remote->id : 0;
my $sess = $remote && $remote->session ? $remote->session->id : LJ::UniqCookie->current_uniq;
# check the attributes are as they should be
my $attr = LJ::get_challenge_attributes($formauth);
my ($randchars, $chal_id, $chal_sess) = split(/\-/, $attr);
return 0 unless $id == $chal_id;
return 0 unless $sess eq $chal_sess;
# check the signature is good and not expired
my $opts = { dont_check_count => 1 }; # in/out
LJ::challenge_check($formauth, $opts);
return $opts->{valid} && ! $opts->{expired};
}
# <LJFUNC>
# name: LJ::create_qr_div
# class: web
# des: Creates the hidden div that stores the QuickReply form.
# returns: undef upon failure or HTML for the div upon success
# args: user, remote, ditemid, style args, userpic, viewing thread
# des-u: user object or userid for journal reply in.
# des-ditemid: ditemid for this comment.
# des-style_opts: the viewing style arguments on this page, as a hashref.
# des-userpic: alternate default userpic.
# </LJFUNC>
sub create_qr_div {
my ( $user, $ditemid, %opts ) = @_;
my $u = LJ::want_user( $user );
my $remote = LJ::get_remote();
return undef unless $u && $remote && $ditemid;
my $style_opts = $opts{style_opts} || {};
my $userpic_kw = $opts{userpic};
my $viewing_thread = $opts{thread};
return undef if $remote->prop( "opt_no_quickreply" );
my $e = LJ::Entry->new( $u, ditemid => $ditemid );
my $separator = %$style_opts ? "&" : "?";
my $basepath = $e->url( style_opts => LJ::viewing_style_opts( %$style_opts ) ) . $separator;
my $usertype = ($remote->openid_identity && $remote->is_validated) ? 'openid_cookie' : 'cookieuser';
my $hidden_form_elements .= LJ::html_hidden({'name' => 'replyto', 'id' => 'replyto', 'value' => ''},
{'name' => 'parenttalkid', 'id' => 'parenttalkid', 'value' => ''},
{'name' => 'journal', 'id' => 'journal', 'value' => $u->{'user'}},
{'name' => 'itemid', 'id' => 'itemid', 'value' => $ditemid},
{'name' => 'usertype', 'id' => 'usertype', 'value' => $usertype },
{'name' => 'qr', 'id' => 'qr', 'value' => '1'},
{'name' => 'cookieuser', 'id' => 'cookieuser', 'value' => $remote->{'user'}},
{'name' => 'dtid', 'id' => 'dtid', 'value' => ''},
{'name' => 'basepath', 'id' => 'basepath', 'value' => $basepath},
{'name' => 'viewing_thread', 'id' => 'viewing_thread', 'value' => $viewing_thread},
);
while ( my ( $key, $value ) = each %$style_opts ) {
$hidden_form_elements .= LJ::html_hidden( { name => $key, id => $key, value => $value } );
}
# rate limiting challenge
{
my ($time, $secret) = LJ::get_secret();
my $rchars = LJ::rand_chars(20);
my $chal = $ditemid . "-$u->{userid}-$time-$rchars";
my $res = Digest::MD5::md5_hex( $secret . $chal );
$hidden_form_elements .= LJ::html_hidden( "chrp1", "$chal-$res" );
}
my @pics;
# Userpic selector
{
my %res;
LJ::do_request({ mode => "login",
ver => $LJ::PROTOCOL_VER,
user => $remote->user,
getpickws => 1,
getpickwurls => 1,
},
\%res, { noauth => 1, userid => $remote->userid }
);
if ( $res{pickw_count} ) {
for (my $i=1; $i<= $res{pickw_count}; $i++) {
push @pics, [ $res{"pickw_$i"}, $res{"pickwurl_$i"} ];
}
@pics = sort { lc($a->[0]) cmp lc($b->[0]) } @pics;
@pics = ( { value => "", text => LJ::Lang::ml('/talkpost.bml.opt.defpic' ), data => { url => $res{defaultpicurl} } },
map { { value => $_->[0], text => $_->[0], data => { url => $_->[1] } } } @pics );
}
}
my $post_disabled = $u->does_not_allow_comments_from( $remote ) || $u->does_not_allow_comments_from_unconfirmed_openid( $remote );
return DW::Template->template_string( 'journal/quickreply.tt', {
form_url => LJ::create_url( '/talkpost_do', host => $LJ::DOMAIN_WEB ),
hidden_form_elements => $hidden_form_elements,
can_checkspell => $LJ::SPELLER ? 1 : 0,
minimal => $opts{minimal} ? 1 : 0,
post_disabled => $post_disabled,
post_button_class => $post_disabled ? 'ui-state-disabled' : '',
quote_button_js => LJ::Talk::js_quote_button( 'body' ),
iconbrowser_js => $remote->can_use_userpic_select ? LJ::Talk::js_iconbrowser_button() : "",
current_icon_kw => $userpic_kw,
current_icon => LJ::Userpic->new_from_keyword( $remote, $userpic_kw ),
remote => {
ljuser => $remote->ljuser_display,
user => $remote->user,
icons_url => $remote->allpics_base,
icons => \@pics,
can_use_iconbrowser => $remote->can_use_userpic_select,
},
journal => {
is_iplogging => $u->opt_logcommentips eq 'A',
is_linkstripped => !$remote || ( $remote && $remote->is_identity && !$u->trusts_or_has_member( $remote ) ),
},
help => {
icon => LJ::help_icon_html("userpics", " "),
iplogging => LJ::help_icon_html("iplogging", " "),
},
ejs => sub { return LJ::ejs( @_ ) },
});
}
# <LJFUNC>
# name: LJ::get_lastcomment
# class: web
# des: Looks up the last talkid and journal the remote user posted in.
# returns: talkid, jid
# args:
# </LJFUNC>
sub get_lastcomment {
my $remote = LJ::get_remote();
my ( $talkid, $jid );
# Figure out their last post
if ( $remote ) {
my $memkey = [$remote->{'userid'}, "lastcomm:$remote->{'userid'}"];
my $memval = LJ::MemCache::get( $memkey );
( $jid, $talkid ) = split( /:/, $memval ) if $memval;
}
return ( $talkid, $jid );
}
# <LJFUNC>
# name: LJ::make_qr_target
# class: web
# des: Returns a div usable for QuickReply boxes.
# returns: HTML for the div
# args:
# </LJFUNC>
sub make_qr_target {
my $name = shift;
return "<div id='ljqrt$name' name='ljqrt$name'></div>";
}
# <LJFUNC>
# name: LJ::set_lastcomment
# class: web
# des: Sets the lastcomm memcached key for this user's last comment.
# returns: undef on failure
# args: u, remote, dtalkid, life?
# des-u: Journal they just posted in, either u or userid
# des-remote: Remote user
# des-dtalkid: Talkid for the comment they just posted
# des-life: How long, in seconds, the memcached key should live.
# </LJFUNC>
sub set_lastcomment
{
my ($u, $remote, $dtalkid, $life) = @_;
my $userid = LJ::want_userid($u);
return undef unless $userid && $remote && $dtalkid;
# By default, this key lasts for 10 seconds.
$life ||= 10;
# Set memcache key for highlighting the comment
my $memkey = [$remote->{'userid'}, "lastcomm:$remote->{'userid'}"];
LJ::MemCache::set($memkey, "$userid:$dtalkid", time()+$life);
return;
}
sub deemp {
"<span class='de'>$_[0]</span>";
}
=head2 C<< LJ::determine_viewing_style( $args, $view, $u ) >>
Takes a hashref of get args, and the current view, and an optional user.
Returns "original", "mine", "site", or "light" as the style.
=cut
sub determine_viewing_style {
my ( $args, $view, $u ) = @_;
my $style = 'original';
# incorporate any user preferences