[2] | 1 | <?php |
---|
[354] | 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[2297] | 4 | // +-----------------------------------------------------------------------+ |
---|
[26461] | 5 | // | Copyright(C) 2008-2014 Piwigo Team http://piwigo.org | |
---|
[2297] | 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 | // +-----------------------------------------------------------------------+ |
---|
[2] | 23 | |
---|
[455] | 24 | //--------------------------------------------------------------------- include |
---|
[364] | 25 | define('PHPWG_ROOT_PATH','./'); |
---|
| 26 | include_once( PHPWG_ROOT_PATH.'include/common.inc.php' ); |
---|
[1072] | 27 | |
---|
| 28 | // +-----------------------------------------------------------------------+ |
---|
| 29 | // | Check Access and exit when user status is not ok | |
---|
| 30 | // +-----------------------------------------------------------------------+ |
---|
| 31 | check_status(ACCESS_GUEST); |
---|
| 32 | |
---|
[28587] | 33 | trigger_notify('loc_begin_search'); |
---|
[18063] | 34 | |
---|
[455] | 35 | //------------------------------------------------------------------ form check |
---|
| 36 | $search = array(); |
---|
| 37 | if (isset($_POST['submit'])) |
---|
[2] | 38 | { |
---|
[4753] | 39 | foreach ($_POST as $post_key => $post_value) |
---|
| 40 | { |
---|
| 41 | if (!is_array($post_value)) |
---|
| 42 | { |
---|
[6518] | 43 | $_POST[$post_key] = pwg_db_real_escape_string($post_value); |
---|
[4753] | 44 | } |
---|
| 45 | } |
---|
| 46 | |
---|
[634] | 47 | if (isset($_POST['search_allwords']) |
---|
| 48 | and !preg_match('/^\s*$/', $_POST['search_allwords'])) |
---|
[1059] | 49 | { |
---|
[26825] | 50 | check_input_parameter('mode', $_POST, false, '/^(OR|AND)$/'); |
---|
[28709] | 51 | |
---|
| 52 | $fields = array_intersect($_POST['fields'], array('name', 'comment', 'file')); |
---|
[26825] | 53 | |
---|
[634] | 54 | $drop_char_match = array( |
---|
| 55 | '-','^','$',';','#','&','(',')','<','>','`','\'','"','|',',','@','_', |
---|
| 56 | '?','%','~','.','[',']','{','}',':','\\','/','=','\'','!','*'); |
---|
| 57 | $drop_char_replace = array( |
---|
| 58 | ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','','',' ',' ',' ',' ','',' ', |
---|
| 59 | ' ',' ',' ',' ',' ',' ',' ',' ','' ,' ',' ',' ',' ',' '); |
---|
[1059] | 60 | |
---|
[634] | 61 | // Split words |
---|
[1008] | 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'], |
---|
[28709] | 74 | 'fields' => $fields, |
---|
[1008] | 75 | ); |
---|
[455] | 76 | } |
---|
[1059] | 77 | |
---|
[1119] | 78 | if (isset($_POST['tags'])) |
---|
| 79 | { |
---|
[5195] | 80 | check_input_parameter('tags', $_POST, true, PATTERN_ID); |
---|
[26825] | 81 | check_input_parameter('tag_mode', $_POST, false, '/^(OR|AND)$/'); |
---|
[4753] | 82 | |
---|
[1119] | 83 | $search['fields']['tags'] = array( |
---|
| 84 | 'words' => $_POST['tags'], |
---|
| 85 | 'mode' => $_POST['tag_mode'], |
---|
| 86 | ); |
---|
| 87 | } |
---|
[1125] | 88 | |
---|
[28707] | 89 | if (isset($_POST['authors']) and is_array($_POST['authors']) and count($_POST['authors']) > 0) |
---|
[455] | 90 | { |
---|
[28707] | 91 | $authors = array(); |
---|
| 92 | |
---|
| 93 | foreach ($_POST['authors'] as $author) |
---|
| 94 | { |
---|
| 95 | $authors[] = strip_tags($author); |
---|
| 96 | } |
---|
| 97 | |
---|
[1008] | 98 | $search['fields']['author'] = array( |
---|
[28707] | 99 | 'words' => $authors, |
---|
[1015] | 100 | 'mode' => 'OR', |
---|
[1008] | 101 | ); |
---|
[17] | 102 | } |
---|
[1059] | 103 | |
---|
[621] | 104 | if (isset($_POST['cat'])) |
---|
[2] | 105 | { |
---|
[5195] | 106 | check_input_parameter('cat', $_POST, true, PATTERN_ID); |
---|
[4753] | 107 | |
---|
[1008] | 108 | $search['fields']['cat'] = array( |
---|
| 109 | 'words' => $_POST['cat'], |
---|
| 110 | 'sub_inc' => ($_POST['subcats-included'] == 1) ? true : false, |
---|
| 111 | ); |
---|
[2] | 112 | } |
---|
[634] | 113 | |
---|
| 114 | // dates |
---|
| 115 | $type_date = $_POST['date_type']; |
---|
[1059] | 116 | |
---|
[621] | 117 | if (!empty($_POST['start_year'])) |
---|
[634] | 118 | { |
---|
[1008] | 119 | $search['fields'][$type_date.'-after'] = array( |
---|
[6518] | 120 | 'date' => sprintf( |
---|
[25005] | 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' |
---|
[1008] | 125 | ), |
---|
| 126 | 'inc' => true, |
---|
| 127 | ); |
---|
[634] | 128 | } |
---|
[621] | 129 | |
---|
[634] | 130 | if (!empty($_POST['end_year'])) |
---|
[621] | 131 | { |
---|
[1008] | 132 | $search['fields'][$type_date.'-before'] = array( |
---|
[6518] | 133 | 'date' => sprintf( |
---|
[25005] | 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' |
---|
[6518] | 138 | ), |
---|
[1008] | 139 | 'inc' => true, |
---|
| 140 | ); |
---|
[621] | 141 | } |
---|
[1059] | 142 | |
---|
[621] | 143 | if (!empty($search)) |
---|
| 144 | { |
---|
[1008] | 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.' |
---|
[1816] | 152 | (rules, last_seen) |
---|
[1008] | 153 | VALUES |
---|
[1816] | 154 | (\''.serialize($search).'\', NOW()) |
---|
[1008] | 155 | ;'; |
---|
| 156 | pwg_query($query); |
---|
| 157 | |
---|
[4892] | 158 | $search_id = pwg_db_insert_id(SEARCH_TABLE); |
---|
[455] | 159 | } |
---|
[621] | 160 | else |
---|
[455] | 161 | { |
---|
[25018] | 162 | $page['errors'][] = l10n('Empty query. No criteria has been entered.'); |
---|
[455] | 163 | } |
---|
[2] | 164 | } |
---|
[455] | 165 | //----------------------------------------------------------------- redirection |
---|
[12764] | 166 | if (isset($_POST['submit']) and count($page['errors']) == 0) |
---|
[455] | 167 | { |
---|
[1082] | 168 | redirect( |
---|
| 169 | make_index_url( |
---|
| 170 | array( |
---|
| 171 | 'section' => 'search', |
---|
| 172 | 'search' => $search_id, |
---|
| 173 | ) |
---|
| 174 | ) |
---|
| 175 | ); |
---|
[455] | 176 | } |
---|
[2] | 177 | //----------------------------------------------------- template initialization |
---|
[621] | 178 | |
---|
[345] | 179 | // |
---|
| 180 | // Start output of page |
---|
| 181 | // |
---|
[5021] | 182 | $title= l10n('Search'); |
---|
[850] | 183 | $page['body_id'] = 'theSearchPage'; |
---|
[345] | 184 | |
---|
[2223] | 185 | $template->set_filename('search' ,'search.tpl' ); |
---|
[1125] | 186 | |
---|
[2223] | 187 | $month_list = $lang['month']; |
---|
| 188 | $month_list[0]='------------'; |
---|
| 189 | ksort($month_list); |
---|
| 190 | |
---|
| 191 | $template->assign( |
---|
[1314] | 192 | array( |
---|
[2223] | 193 | 'F_SEARCH_ACTION' => 'search.php', |
---|
[1314] | 194 | 'U_HELP' => PHPWG_ROOT_PATH.'popuphelp.php?page=search', |
---|
[2324] | 195 | |
---|
[2223] | 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'], |
---|
[1314] | 201 | ) |
---|
| 202 | ); |
---|
| 203 | |
---|
[1677] | 204 | $available_tags = get_available_tags(); |
---|
[1125] | 205 | |
---|
[1314] | 206 | if (count($available_tags) > 0) |
---|
| 207 | { |
---|
[2409] | 208 | usort( $available_tags, 'tag_alpha_compare'); |
---|
[1119] | 209 | |
---|
[28708] | 210 | $template->assign('TAGS', $available_tags); |
---|
[1314] | 211 | } |
---|
[355] | 212 | |
---|
[28707] | 213 | // authors |
---|
[29554] | 214 | $authors = array(); |
---|
| 215 | |
---|
[28707] | 216 | $query = ' |
---|
| 217 | SELECT |
---|
| 218 | author, |
---|
[29430] | 219 | id |
---|
[28707] | 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', |
---|
[28744] | 226 | 'visible_images' => 'id' |
---|
[28707] | 227 | ), |
---|
| 228 | ' WHERE ' |
---|
| 229 | ).' |
---|
| 230 | AND author IS NOT NULL |
---|
[29430] | 231 | GROUP BY author, id |
---|
[28707] | 232 | ORDER BY author |
---|
| 233 | ;'; |
---|
[29430] | 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 | } |
---|
[28707] | 245 | |
---|
[29430] | 246 | foreach ($author_counts as $author => $counter) |
---|
| 247 | { |
---|
| 248 | $authors[] = array( |
---|
| 249 | 'author' => $author, |
---|
| 250 | 'counter' => $counter, |
---|
| 251 | ); |
---|
| 252 | } |
---|
| 253 | |
---|
[28707] | 254 | $template->assign('AUTHORS', $authors); |
---|
| 255 | |
---|
[455] | 256 | //------------------------------------------------------------- categories form |
---|
[614] | 257 | $query = ' |
---|
[2324] | 258 | SELECT id,name,global_rank,uppercats |
---|
[1677] | 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 | ).' |
---|
[614] | 269 | ;'; |
---|
[28710] | 270 | display_select_cat_wrapper($query, array(), 'category_options', true); |
---|
[621] | 271 | |
---|
[10812] | 272 | // include menubar |
---|
| 273 | $themeconf = $template->get_template_vars('themeconf'); |
---|
[10824] | 274 | if (!isset($themeconf['hide_menu_on']) OR !in_array('theSearchPage', $themeconf['hide_menu_on'])) |
---|
[10812] | 275 | { |
---|
| 276 | include( PHPWG_ROOT_PATH.'include/menubar.inc.php'); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | //------------------------------------------------------------ html code display |
---|
[1627] | 280 | include(PHPWG_ROOT_PATH.'include/page_header.php'); |
---|
[28587] | 281 | trigger_notify('loc_end_search'); |
---|
[20609] | 282 | flush_page_messages(); |
---|
[2223] | 283 | $template->pparse('search'); |
---|
[369] | 284 | include(PHPWG_ROOT_PATH.'include/page_tail.php'); |
---|
[362] | 285 | ?> |
---|