4 use vars qw(%GET %POST $title $body $arg);
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+)$/) {
19 my $faqlink = LJ::Faq->page_url( 'faqid' => $faqid );
20 return LJ::Request->redirect($faqlink);
22 if ($q =~ /^req(uest)? (\d+)$/) {
23 return BML::redirect("$LJ::SITEROOT/support/see_request.bml?id=$2");
25 if ($q =~ m!(.+)/(pics|full)!) {
26 if (my $u = LJ::load_user($1)) {
27 return BML::redirect( $u->profile_url('full' => 1) )
29 return BML::redirect( $u->allpics_base )
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);
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
46 $last_is_untrusted = 1 if $u->{statusvis} eq "V";
50 my $eq = LJ::ehtml($q);
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?>";
57 return BML::redirect($dests[0]->url);
61 $body = $ad . "<?h1 Multiple Choices h1?>";
64 title => "Site pages",
65 text => "The following $LJ::SITENAMESHORT pages match your search for <b>$eq</b>:",
69 if ($last_is_untrusted) {
71 title => "User account",
72 text => "The following user account matches your search for <b>$eq</b>:",
73 list => [ pop @dests ],
77 foreach my $cat (@cats) {
78 $body .= "<?h2 $cat->{title} h2?><?p $cat->{text} p?>";
80 foreach my $dst (@{ $cat->{list} }) {
81 my $lju = $dst->ljuser;
84 $link = $dst->ljuser->ljuser_display . " - " . LJ::ehtml($dst->title);
86 $link = "<a href=\"" . LJ::ehtml($dst->url) . "\">" . LJ::ehtml($dst->title) . "</a>";
88 if (!$lju || ($lju && $lju->{statusvis} eq "V")) {
89 $body .= "<li>$link</li>\n";
98 if ($type eq "user" || $type eq "ljtalk") {
100 $user =~ s!\@$LJ::USER_DOMAIN!!;
102 if ($user =~ s!/(\w+)!!) {
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';
115 if (my $u = LJ::load_user($user)) {
116 $url = $u->profile_url;
117 $url .= "?mode=full" if $what eq 'full';
119 $url = "$LJ::SITEROOT/userinfo.bml?user=$user";
122 return BML::redirect($url);
124 return BML::redirect("$LJ::SITEROOT/random.bml");
128 if ($type eq "int") {
131 return BML::redirect("$LJ::SITEROOT/interests.bml?int=" . LJ::eurl($int));
133 $title = $ML{'.nointerest.title'};
134 $body = $ad . "<?h1 $ML{'Error'} h1?><?p $ML{'.nointerest.text'} p?>";
139 if ($type eq "email") {
142 $title = $ML{'.noaddress.title'};
143 $body = $ad . "<?h1 $ML{'Error'} h1?><?p $ML{'.noaddress.text'} p?>";
146 my $uid = $dbr->selectrow_array(qq{
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
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');
158 return BML::redirect($u->profile_url);
162 $title = $ML{'.nomatch.title'};
163 $body = $ad . "<?h1 $ML{'Sorry'} h1?><?p $ML{'.nomatch.text'} p?>";
169 $type eq "aolim" || $type eq "icq" ||
170 $type eq "yahoo" || $type eq "msn" ||
173 use LJ::Directory::Constraint::ContactInfo;
174 my @uids = LJ::Directory::Constraint::ContactInfo->new(screenname => $q)->matching_uids;
177 my $u = LJ::load_userid(shift @uids);
178 if ($output eq "foaf") {
179 return BML::redirect($u->journal_base . '/data/foaf');
181 return BML::redirect($u->profile_url);
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 ],
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?>";
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?>";
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;
220 } elsif (@parts == 2) {
221 ($city, $state) = @parts;
227 } elsif (@parts == 2) {
228 ($city, $state) = @parts;
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");
235 if ($type eq "faq") {
238 return BML::redirect("$LJ::SITEROOT/support/faq/search.html?q=" . LJ::eurl($term));
240 $title = $ML{'.nofaqsearch.title'};
241 $body = $ad . "<?h1 $ML{'Error'} h1?><?p $ML{'.nofaqsearch.text'} p?>";
246 # Unknown type, try running site hooks
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', {
258 title=><?_code return $title; _code?>
259 body=><?_code return $body; _code?>
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