-
Notifications
You must be signed in to change notification settings - Fork 0
/
go.php
182 lines (153 loc) · 6.38 KB
/
go.php
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
<?php
/**
* locate a page by name
*
* The page locator is aiming to streamline the usage of nick names.
*
* YACS is supporting nick names for a long-time for articles, categories, sections and also for user profiles.
* Nick names are good, except that you have to remember the kind of things they are referencing.
* This is because the URL used to view user profiles is different from the URL used to access
* other kinds of things, including categories or server profiles.
*
* For example to view the user profile associated with the nick name 'foo' you have to use
* the URL '[code]/yacs/users/view.php/foo[/code]'.
*
* But to view the category with the nick name 'bar' you would use '[code]/yacs/categories/view.php/bar[/code]' instead.
*
* The page locator will take care of nick names at a central place.
* It attempts to search the provided name in: sections, categories, articles and user profiles.
*
* This means that to look for user 'foo' or for category 'bar' now you just have to submit nick names
* to the page locator with either '[code]/yacs/go.php/foo[/code]' or '[code]/yacs/go.php/bar[/code]'.
*
* On successful search this script will redirect the surfer to the matching page.
* Else it will offer to redirect to the regular search engine.
*
* Please note that you can invoke this script from within any article, using the yacs code [code][go=<name>, <label>][/code], like in
* the following example:
*
* [snippet]
* Please use our [go=monthly, monthly archive] for reference
* [/snippet]
*
* In the case of twin sections or of twin pages, this script lists all items that have the same nick name to allow
* the surfer to make his own choice.
*
* If only one item has the given name the locator redirects directly to it.
*
* If no item has the name, the resulting page offers several choices:
* - Create a new page with the name
* - Look for another name
* - Trigger the search engine on the given name
*
* This page locator has been largely inspired from the Cisco web site, featuring the famous [code]cisco.com/go[/code] URLs
*
* @link http://www.cisco.com/ Cisco home page
*
* Note that the [code]mod_rewrite[/code] module is not required to run this script.
*
* However, you can add ad hoc directives to [code].htaccess[/code] configuration file.
* If the mod_rewrite module is available, '[code]go.php[/code]' can be rewritten to '[code]go[/code]', making this script
* strictly equivalent to the Cisco's GO link.
*
* @author Bernard Paques
* @tester Thierry Pinelli (ThierryP)
* @reference
* @license http://www.gnu.org/copyleft/lesser.txt GNU Lesser General Public License
*
* @see search.php
*/
// common definitions and initial processing
include_once 'shared/global.php';
// look for the id
$id = NULL;
if(isset($_REQUEST['id']))
$id = $_REQUEST['id'];
elseif(isset($context['arguments'][0]))
$id = $context['arguments'][0];
$id = strip_tags($id);
// load localized strings
i18n::bind('root');
// load the skin
load_skin('go');
// the title of the page
$context['page_title'] = i18n::s('Page locator');
// ensure we have a non-empty string
if(!($id = trim($id)) || !preg_match('/^[\w~-]+$/', $id)) {
$context['text'] .= '<p>'.i18n::s('Please indicate a nick name to look for.')."</p>\n";
// allow one hook here
} elseif(is_callable(array('Hooks', 'serve_scripts')) && $url = Hooks::serve_scripts('go.php', $id)) {
Safe::redirect($url);
// short link to some article
} elseif(!strncmp($id, 'a~', 2) && ($item = Articles::get(restore_number(substr($id, 2))))) {
Safe::redirect(Articles::get_permalink($item));
// short link to some section
} elseif(!strncmp($id, 's~', 2) && ($item = Sections::get(restore_number(substr($id, 2))))) {
Safe::redirect(Sections::get_permalink($item));
// look in sections
} elseif($items = Sections::list_for_name($id, NULL, 'full')) {
// only one section has this name
if(count($items) == 1) {
$url = key($items);
Safe::redirect($url);
}
// splash
$context['text'] .= '<p>'.i18n::s('Select below among available sections.').'</p>';
// several pages
$context['text'] .= Skin::build_list($items, 'decorated');
// look in categories
} elseif(($item = Categories::get($id)) || ($item = Categories::get_by_keyword($id))) {
Safe::redirect(Categories::get_permalink($item));
// look in articles
} elseif($items = Articles::list_for_name($id, NULL, 'full')) {
// only one page has this name
if(count($items) == 1) {
$url = key($items);
Safe::redirect($url);
}
// splash
$context['text'] .= '<p>'.i18n::s('Select below among available pages.').'</p>';
// several pages
$context['text'] .= Skin::build_list($items, 'decorated');
// look in user profiles
} elseif($item = Users::get($id)) {
Safe::redirect(Users::get_permalink($item));
// not found
} else {
$context['text'] .= '<p>'.sprintf(i18n::s('Sorry, no page has the provided name: %s'), $id).'</p>'."\n";
// offer to create a new page
$context['text'] .= '<p>'.Skin::build_link('articles/edit.php?name='.urlencode($id), i18n::s('Add a page with this name'), 'shortcut').'</p>'."\n";
}
// the form to submit a new search
if($id)
$label = i18n::s('Look for');
else
$label = i18n::s('Nick name');
$input = '<input type="text" name="id" id="id" size="25" value="'.encode_field($id).'" maxlength="64" />'
.' '.Skin::build_submit_button(i18n::s('Search'));
$context['text'] .= '<form method="get" action="'.$context['script_url'].'" onsubmit="return validateDocumentPost(this)" id="main_form"><p>'
.$label.' '.$input.'</p></form>';
// the script used for form handling at the browser
Page::insert_script(
// check that main fields are not empty
' func'.'tion validateDocumentPost(container) {'."\n"
// search is mandatory
.' if(!container.id.value) {'."\n"
.' alert("'.i18n::s('Please type something to search for').'");'."\n"
.' Yacs.stopWorking();'."\n"
.' return false;'."\n"
.' }'."\n"
// successful check
.' return true;'."\n"
.' }'."\n"
."\n"
// set the focus on first form field
.'$("#id").focus();'."\n"
);
// extend the process to the search engine
if($id) {
// submit the request to our search engine
$context['text'] .= '<p>'.sprintf(i18n::s('Submit "%s" to our %s in case some pages could match this keyword.'), $id, Skin::build_link('search.php?search='.urlencode($id), i18n::s('search engine'), 'basic')).'</p>'."\n";
}
// render the skin
render_skin();