1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2014 Piwigo Team http://piwigo.org | |
---|
6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
8 | // +-----------------------------------------------------------------------+ |
---|
9 | // | This program is free software; you can redistribute it and/or modify | |
---|
10 | // | it under the terms of the GNU General Public License as published by | |
---|
11 | // | the Free Software Foundation | |
---|
12 | // | | |
---|
13 | // | This program is distributed in the hope that it will be useful, but | |
---|
14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
16 | // | General Public License for more details. | |
---|
17 | // | | |
---|
18 | // | You should have received a copy of the GNU General Public License | |
---|
19 | // | along with this program; if not, write to the Free Software | |
---|
20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
21 | // | USA. | |
---|
22 | // +-----------------------------------------------------------------------+ |
---|
23 | |
---|
24 | //--------------------------------------------------------------------- include |
---|
25 | define('PHPWG_ROOT_PATH','./'); |
---|
26 | include_once( PHPWG_ROOT_PATH.'include/common.inc.php' ); |
---|
27 | |
---|
28 | // +-----------------------------------------------------------------------+ |
---|
29 | // | Check Access and exit when user status is not ok | |
---|
30 | // +-----------------------------------------------------------------------+ |
---|
31 | check_status(ACCESS_GUEST); |
---|
32 | |
---|
33 | trigger_notify('loc_begin_search'); |
---|
34 | |
---|
35 | //------------------------------------------------------------------ form check |
---|
36 | $search = array(); |
---|
37 | if (isset($_POST['submit'])) |
---|
38 | { |
---|
39 | foreach ($_POST as $post_key => $post_value) |
---|
40 | { |
---|
41 | if (!is_array($post_value)) |
---|
42 | { |
---|
43 | $_POST[$post_key] = pwg_db_real_escape_string($post_value); |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | if (isset($_POST['search_allwords']) |
---|
48 | and !preg_match('/^\s*$/', $_POST['search_allwords'])) |
---|
49 | { |
---|
50 | check_input_parameter('mode', $_POST, false, '/^(OR|AND)$/'); |
---|
51 | |
---|
52 | $fields = array_intersect($_POST['fields'], array('name', 'comment', 'file')); |
---|
53 | |
---|
54 | $drop_char_match = array( |
---|
55 | '-','^','$',';','#','&','(',')','<','>','`','\'','"','|',',','@','_', |
---|
56 | '?','%','~','.','[',']','{','}',':','\\','/','=','\'','!','*'); |
---|
57 | $drop_char_replace = array( |
---|
58 | ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','','',' ',' ',' ',' ','',' ', |
---|
59 | ' ',' ',' ',' ',' ',' ',' ',' ','' ,' ',' ',' ',' ',' '); |
---|
60 | |
---|
61 | // Split words |
---|
62 | $search['fields']['allwords'] = array( |
---|
63 | 'words' => array_unique( |
---|
64 | preg_split( |
---|
65 | '/\s+/', |
---|
66 | str_replace( |
---|
67 | $drop_char_match, |
---|
68 | $drop_char_replace, |
---|
69 | $_POST['search_allwords'] |
---|
70 | ) |
---|
71 | ) |
---|
72 | ), |
---|
73 | 'mode' => $_POST['mode'], |
---|
74 | 'fields' => $fields, |
---|
75 | ); |
---|
76 | } |
---|
77 | |
---|
78 | if (isset($_POST['tags'])) |
---|
79 | { |
---|
80 | check_input_parameter('tags', $_POST, true, PATTERN_ID); |
---|
81 | check_input_parameter('tag_mode', $_POST, false, '/^(OR|AND)$/'); |
---|
82 | |
---|
83 | $search['fields']['tags'] = array( |
---|
84 | 'words' => $_POST['tags'], |
---|
85 | 'mode' => $_POST['tag_mode'], |
---|
86 | ); |
---|
87 | } |
---|
88 | |
---|
89 | if (isset($_POST['authors']) and is_array($_POST['authors']) and count($_POST['authors']) > 0) |
---|
90 | { |
---|
91 | $authors = array(); |
---|
92 | |
---|
93 | foreach ($_POST['authors'] as $author) |
---|
94 | { |
---|
95 | $authors[] = strip_tags($author); |
---|
96 | } |
---|
97 | |
---|
98 | $search['fields']['author'] = array( |
---|
99 | 'words' => $authors, |
---|
100 | 'mode' => 'OR', |
---|
101 | ); |
---|
102 | } |
---|
103 | |
---|
104 | if (isset($_POST['cat'])) |
---|
105 | { |
---|
106 | check_input_parameter('cat', $_POST, true, PATTERN_ID); |
---|
107 | |
---|
108 | $search['fields']['cat'] = array( |
---|
109 | 'words' => $_POST['cat'], |
---|
110 | 'sub_inc' => ($_POST['subcats-included'] == 1) ? true : false, |
---|
111 | ); |
---|
112 | } |
---|
113 | |
---|
114 | // dates |
---|
115 | $type_date = $_POST['date_type']; |
---|
116 | |
---|
117 | if (!empty($_POST['start_year'])) |
---|
118 | { |
---|
119 | $search['fields'][$type_date.'-after'] = array( |
---|
120 | 'date' => sprintf( |
---|
121 | '%d-%02d-%02d', |
---|
122 | $_POST['start_year'], |
---|
123 | $_POST['start_month'] != 0 ? $_POST['start_month'] : '01', |
---|
124 | $_POST['start_day'] != 0 ? $_POST['start_day'] : '01' |
---|
125 | ), |
---|
126 | 'inc' => true, |
---|
127 | ); |
---|
128 | } |
---|
129 | |
---|
130 | if (!empty($_POST['end_year'])) |
---|
131 | { |
---|
132 | $search['fields'][$type_date.'-before'] = array( |
---|
133 | 'date' => sprintf( |
---|
134 | '%d-%02d-%02d', |
---|
135 | $_POST['end_year'], |
---|
136 | $_POST['end_month'] != 0 ? $_POST['end_month'] : '12', |
---|
137 | $_POST['end_day'] != 0 ? $_POST['end_day'] : '31' |
---|
138 | ), |
---|
139 | 'inc' => true, |
---|
140 | ); |
---|
141 | } |
---|
142 | |
---|
143 | if (!empty($search)) |
---|
144 | { |
---|
145 | // default search mode : each clause must be respected |
---|
146 | $search['mode'] = 'AND'; |
---|
147 | |
---|
148 | // register search rules in database, then they will be available on |
---|
149 | // thumbnails page and picture page. |
---|
150 | $query =' |
---|
151 | INSERT INTO '.SEARCH_TABLE.' |
---|
152 | (rules, last_seen) |
---|
153 | VALUES |
---|
154 | (\''.serialize($search).'\', NOW()) |
---|
155 | ;'; |
---|
156 | pwg_query($query); |
---|
157 | |
---|
158 | $search_id = pwg_db_insert_id(SEARCH_TABLE); |
---|
159 | } |
---|
160 | else |
---|
161 | { |
---|
162 | $page['errors'][] = l10n('Empty query. No criteria has been entered.'); |
---|
163 | } |
---|
164 | } |
---|
165 | //----------------------------------------------------------------- redirection |
---|
166 | if (isset($_POST['submit']) and count($page['errors']) == 0) |
---|
167 | { |
---|
168 | redirect( |
---|
169 | make_index_url( |
---|
170 | array( |
---|
171 | 'section' => 'search', |
---|
172 | 'search' => $search_id, |
---|
173 | ) |
---|
174 | ) |
---|
175 | ); |
---|
176 | } |
---|
177 | //----------------------------------------------------- template initialization |
---|
178 | |
---|
179 | // |
---|
180 | // Start output of page |
---|
181 | // |
---|
182 | $title= l10n('Search'); |
---|
183 | $page['body_id'] = 'theSearchPage'; |
---|
184 | |
---|
185 | $template->set_filename('search' ,'search.tpl' ); |
---|
186 | |
---|
187 | $month_list = $lang['month']; |
---|
188 | $month_list[0]='------------'; |
---|
189 | ksort($month_list); |
---|
190 | |
---|
191 | $template->assign( |
---|
192 | array( |
---|
193 | 'F_SEARCH_ACTION' => 'search.php', |
---|
194 | 'U_HELP' => PHPWG_ROOT_PATH.'popuphelp.php?page=search', |
---|
195 | |
---|
196 | 'month_list' => $month_list, |
---|
197 | 'START_DAY_SELECTED' => @$_POST['start_day'], |
---|
198 | 'START_MONTH_SELECTED' => @$_POST['start_month'], |
---|
199 | 'END_DAY_SELECTED' => @$_POST['end_day'], |
---|
200 | 'END_MONTH_SELECTED' => @$_POST['end_month'], |
---|
201 | ) |
---|
202 | ); |
---|
203 | |
---|
204 | $available_tags = get_available_tags(); |
---|
205 | |
---|
206 | if (count($available_tags) > 0) |
---|
207 | { |
---|
208 | usort( $available_tags, 'tag_alpha_compare'); |
---|
209 | |
---|
210 | $template->assign('TAGS', $available_tags); |
---|
211 | } |
---|
212 | |
---|
213 | // authors |
---|
214 | $authors = array(); |
---|
215 | |
---|
216 | $query = ' |
---|
217 | SELECT |
---|
218 | author, |
---|
219 | id |
---|
220 | FROM '.IMAGES_TABLE.' AS i |
---|
221 | JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON ic.image_id = i.id |
---|
222 | '.get_sql_condition_FandF( |
---|
223 | array( |
---|
224 | 'forbidden_categories' => 'category_id', |
---|
225 | 'visible_categories' => 'category_id', |
---|
226 | 'visible_images' => 'id' |
---|
227 | ), |
---|
228 | ' WHERE ' |
---|
229 | ).' |
---|
230 | AND author IS NOT NULL |
---|
231 | GROUP BY author, id |
---|
232 | ORDER BY author |
---|
233 | ;'; |
---|
234 | $author_counts = array(); |
---|
235 | $result = pwg_query($query); |
---|
236 | while ($row = pwg_db_fetch_assoc($result)) |
---|
237 | { |
---|
238 | if (!isset($author_counts[ $row['author'] ])) |
---|
239 | { |
---|
240 | $author_counts[ $row['author'] ] = 0; |
---|
241 | } |
---|
242 | |
---|
243 | $author_counts[ $row['author'] ]++; |
---|
244 | } |
---|
245 | |
---|
246 | foreach ($author_counts as $author => $counter) |
---|
247 | { |
---|
248 | $authors[] = array( |
---|
249 | 'author' => $author, |
---|
250 | 'counter' => $counter, |
---|
251 | ); |
---|
252 | } |
---|
253 | |
---|
254 | $template->assign('AUTHORS', $authors); |
---|
255 | |
---|
256 | //------------------------------------------------------------- categories form |
---|
257 | $query = ' |
---|
258 | SELECT id,name,global_rank,uppercats |
---|
259 | FROM '.CATEGORIES_TABLE.' |
---|
260 | '.get_sql_condition_FandF |
---|
261 | ( |
---|
262 | array |
---|
263 | ( |
---|
264 | 'forbidden_categories' => 'id', |
---|
265 | 'visible_categories' => 'id' |
---|
266 | ), |
---|
267 | 'WHERE' |
---|
268 | ).' |
---|
269 | ;'; |
---|
270 | display_select_cat_wrapper($query, array(), 'category_options', true); |
---|
271 | |
---|
272 | // include menubar |
---|
273 | $themeconf = $template->get_template_vars('themeconf'); |
---|
274 | if (!isset($themeconf['hide_menu_on']) OR !in_array('theSearchPage', $themeconf['hide_menu_on'])) |
---|
275 | { |
---|
276 | include( PHPWG_ROOT_PATH.'include/menubar.inc.php'); |
---|
277 | } |
---|
278 | |
---|
279 | //------------------------------------------------------------ html code display |
---|
280 | include(PHPWG_ROOT_PATH.'include/page_header.php'); |
---|
281 | trigger_notify('loc_end_search'); |
---|
282 | flush_page_messages(); |
---|
283 | $template->pparse('search'); |
---|
284 | include(PHPWG_ROOT_PATH.'include/page_tail.php'); |
---|
285 | ?> |
---|