LJSUP-17669: Login.bml form refactoring
[livejournal.git] / htdocs / multisearch.bml
blob7b602a07d1f6fbb04d83a6bc332af47cf1d4e286
1 <?_code
3     use strict;
4     use vars qw(%GET %POST $title $body $arg);
6     $title = "";
7     $body = "";
8     my $type = $POST{'type'} || $GET{'type'};
9     my $q = $POST{'q'} || $GET{'q'};
10     my $output = lc($POST{'output'}) || lc($GET{'output'});
11     my $dbr = LJ::get_db_reader();
13     my $ad = LJ::get_ads({ location => 'bml.multisearch', ljadwrapper => 1 });
15     if ($type eq "nav_and_user" || $type eq "nav") {
16         # Some special shortcuts used for easy navigation
17         if ($q =~ /^faq (\d+)$/) {
18             my $faqid   = $1;
19             my $faqlink = LJ::Faq->page_url( 'faqid' => $faqid );
20             return LJ::Request->redirect($faqlink);
21         }
22         if ($q =~ /^req(uest)? (\d+)$/) {
23             return BML::redirect("$LJ::SITEROOT/support/see_request.bml?id=$2");
24         }
25         if ($q =~ m!(.+)/(pics|full)!) {
26             if (my $u = LJ::load_user($1)) {
27                 return BML::redirect( $u->profile_url('full' => 1) )
28                     if $2 eq "full";
29                 return BML::redirect( $u->allpics_base )
30                     if $2 eq "pics";
31             }
32         }
34         do "LJ/NavTag.pm";
35         my @dests = LJ::NavTag->dests_of_tag($q);
36         my $last_is_untrusted = 0;
38         if ($type eq "nav_and_user") {
39             my $u = LJ::load_user_or_identity($q);
40             if ($u) {
41                 push @dests, LJ::NavTag::Dest->new(type => "LJUSER", dest => $u->{user});
43                 # presumably users will start registering usernames that match our site navigation tags,
44                 # so let's preempt that and throw them into a lame bottom section when site-defined tags
45                 # are alse present.
46                 $last_is_untrusted = 1 if $u->{statusvis} eq "V";
47             }
48         }
50         my $eq = LJ::ehtml($q);
51         if (@dests == 0) {
52             $title = "No matches";
53             $body = $ad . "<?h1 $ML{'Error'} h1?><?p No site page or username matches your search for <b>$eq</b>. p?>";
54             return;
55         }
56         if (@dests == 1) {
57             return BML::redirect($dests[0]->url);
58         }
60         $title = "Choices";
61         $body = $ad . "<?h1 Multiple Choices h1?>";
63         my @cats = ({
64             title => "Site pages",
65             text => "The following $LJ::SITENAMESHORT pages match your search for <b>$eq</b>:",
66             list => \@dests,
67         });
69         if ($last_is_untrusted) {
70             push @cats, {
71                 title => "User account",
72                 text => "The following user account matches your search for <b>$eq</b>:",
73                 list => [ pop @dests ],
74             };
75         }
77         foreach my $cat (@cats) {
78             $body .= "<?h2 $cat->{title} h2?><?p $cat->{text} p?>";
79             $body .= "<ul>";
80             foreach my $dst (@{ $cat->{list} }) {
81                 my $lju = $dst->ljuser;
82                 my $link;
83                 if ($lju) {
84                     $link = $dst->ljuser->ljuser_display . " - " . LJ::ehtml($dst->title);
85                 } else {
86                     $link = "<a href=\"" . LJ::ehtml($dst->url) . "\">" . LJ::ehtml($dst->title) . "</a>";
87                 }
88                 if (!$lju || ($lju && $lju->{statusvis} eq "V")) {
89                     $body .= "<li>$link</li>\n";
90                 }
91             }
92             $body .= "</ul>";
93         }
95         return;
96     }
98     if ($type eq "user" || $type eq "ljtalk") {
99         my $user = lc($q);
100         $user =~ s!\@$LJ::USER_DOMAIN!!;
101         my $what;
102         if ($user =~ s!/(\w+)!!) {
103             $what = $1;
104         }
106         $user =~ s/-/_/g;
107         $user =~ s/[^\w]//g;
108         if ($user) {
109             my $url;
110             if ($what eq "pics") {
111                 $url = "$LJ::SITEROOT/allpics.bml?user=$user";
112             } elsif ($output eq "foaf") {
113                 $url = LJ::journal_base($user) . '/data/foaf';
114             } else {
115                 if (my $u = LJ::load_user($user)) {
116                     $url = $u->profile_url;
117                     $url .= "?mode=full" if $what eq 'full';
118                 } else {
119                     $url = "$LJ::SITEROOT/userinfo.bml?user=$user";
120                 }
121             }
122             return BML::redirect($url);
123         } else {
124             return BML::redirect("$LJ::SITEROOT/random.bml");
125         }
126     }
128     if ($type eq "int") {
129         my $int = lc($q);
130         if ($int) {
131             return BML::redirect("$LJ::SITEROOT/interests.bml?int=" . LJ::eurl($int));
132         } else {
133             $title = $ML{'.nointerest.title'};
134             $body = $ad . "<?h1 $ML{'Error'} h1?><?p $ML{'.nointerest.text'} p?>";
135             return;
136         }
137     }
139     if ($type eq "email") {
140         my $email = lc($q);
141         unless ($email) {
142             $title = $ML{'.noaddress.title'};
143             $body = $ad . "<?h1 $ML{'Error'} h1?><?p $ML{'.noaddress.text'} p?>";
144             return;
145         } else {
146             my $uid = $dbr->selectrow_array(qq{
147                 SELECT e.userid
148                 FROM user u, email e
149                 WHERE e.email=? AND e.userid=u.userid AND u.journaltype='P' AND u.statusvis='V'
150                 AND u.allow_contactshow='Y' LIMIT 1 }, undef, $email
151             );
152             if (my $u = LJ::load_userid($uid)) {
153                 LJ::load_user_props($u, "opt_whatemailshow");
154                 if ($u->{'opt_whatemailshow'} eq "A" || $u->{'opt_whatemailshow'} eq "B") {
155                     if ($output eq "foaf") {
156                         return BML::redirect(LJ::journal_base($u) . '/data/foaf');
157                     } else {
158                         return BML::redirect($u->profile_url);
159                     }
160                 }
161             }
162             $title = $ML{'.nomatch.title'};
163             $body = $ad . "<?h1 $ML{'Sorry'} h1?><?p $ML{'.nomatch.text'} p?>";
164             return;
165         }
166     }
168     if ($type eq "im" ||
169         $type eq "aolim" || $type eq "icq" ||
170         $type eq "yahoo" || $type eq "msn" ||
171         $type eq "jabber") {
173         use LJ::Directory::Constraint::ContactInfo;
174         my @uids = LJ::Directory::Constraint::ContactInfo->new(screenname => $q)->matching_uids;
176         if (@uids == 1) {
177             my $u = LJ::load_userid(shift @uids);
178             if ($output eq "foaf") {
179                 return BML::redirect($u->journal_base . '/data/foaf');
180             } else {
181                 return BML::redirect($u->profile_url);
182             }
183         } elsif (@uids > 1) {
184             my $us = LJ::load_userids(@uids);
186             $title = $ML{'.title.results'};
187             $body .= LJ::user_search_display(
188                                              users      => [ values %$us ],
189                                              timesort   => 1,
190                                              perpage    => 50,
191                                              );
192             return;
193         }
195         # If we haven't returned already then the search turned up nothing.
196         $title = $ML{'.nomatch.title'};
197         $body = $ad . "<?h1 $ML{'Sorry'} h1?><?p $ML{'.nomatch.text'} p?>";
198         return;
199     }
201     if ($type eq "region") {
202         $q =~ s/^\s+//; $q =~ s/\s+$//;
203         my @parts = split(/\s*,\s*/, $q);
204         if (@parts==0 || @parts>3) {
205             $title = $ML{'.formaterror'};
206             $body .= $ad . "<?h1 $ML{'.region.head'} h1?><?p " . BML::ml('.region.bodytext2', {'aopts' => "href='$LJ::SITEROOT/directorysearch.bml'"}) . " p?>";
207             return;
208         }
210         my $ctc = $parts[-1];
211         my $sth = $dbr->prepare("SELECT code FROM codes WHERE type='country' AND (code=? OR item=?) LIMIT 1");
212         $sth->execute($ctc, $ctc);
213         my ($country) = $sth->fetchrow_array;
214         my ($state, $city);
216         if ($country) {
217             pop @parts;
218             if (@parts == 1) {
219                 $state = $parts[0];
220             } elsif (@parts == 2) {
221                 ($city, $state) = @parts;
222             }
223         } else {
224             $country = "US";
225             if (@parts ==1) {
226                 $city = $parts[0];
227             } elsif (@parts == 2) {
228                 ($city, $state) = @parts;
229             }
230         }
231         ($city, $state, $country) = map { LJ::eurl($_); } ($city, $state, $country);
232         return BML::redirect("$LJ::SITEROOT/directory.bml?s_loc=1&loc_cn=$country&loc_st=$state&loc_ci=$city&opt_sort=ut&opt_format=pics&opt_pagesize=50");
233     }
235     if ($type eq "faq") {
236         my $term = lc($q);
237         if ($term) {
238             return BML::redirect("$LJ::SITEROOT/support/faq/search.html?q=" . LJ::eurl($term));
239         } else {
240             $title = $ML{'.nofaqsearch.title'};
241             $body = $ad . "<?h1 $ML{'Error'} h1?><?p $ML{'.nofaqsearch.text'} p?>";
242             return;
243         }
244     }
246     # Unknown type, try running site hooks
247     if ($type) {
248         # TODO: check return value of this hook, and fall back to another
249         # hook that shows the results here, rather than redirecting to another page
250         return LJ::run_hook('multisearch_custom_search_redirect', {
251             type  => $type,
252             query => $q, });
253     }
255     return;
257 _code?><?page
258 title=><?_code return $title; _code?>
259 body=><?_code return $body; _code?>
260 page?><?_c <LJDEP>
261 link: htdocs/talkread.bml, htdocs/allpics.bml, htdocs/userinfo.bml, htdocs/random.bml
262 link: htdocs/interests.bml, htdocs/directorysearch.bml, htdocs/directory.bml
263 hook: multisearch_custom_search_redirect
264 </LJDEP> _c?>