source: trunk/search.php @ 27729

Last change on this file since 27729 was 26825, checked in by plg, 11 years ago

bug 3020 and bug 3021 fixed: additionnal checks in search inputs

  • Property svn:eol-style set to LF
File size: 7.2 KB
RevLine 
[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]25define('PHPWG_ROOT_PATH','./');
26include_once( PHPWG_ROOT_PATH.'include/common.inc.php' );
[1072]27
28// +-----------------------------------------------------------------------+
29// | Check Access and exit when user status is not ok                      |
30// +-----------------------------------------------------------------------+
31check_status(ACCESS_GUEST);
32
[18749]33trigger_action('loc_begin_search');
[18063]34
[455]35//------------------------------------------------------------------ form check
36$search = array();
37if (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)$/');
51   
[634]52    $drop_char_match = array(
53      '-','^','$',';','#','&','(',')','<','>','`','\'','"','|',',','@','_',
54      '?','%','~','.','[',']','{','}',':','\\','/','=','\'','!','*');
55    $drop_char_replace = array(
56      ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','','',' ',' ',' ',' ','',' ',
57      ' ',' ',' ',' ',' ',' ',' ',' ','' ,' ',' ',' ',' ',' ');
[1059]58
[634]59    // Split words
[1008]60    $search['fields']['allwords'] = array(
61      'words' => array_unique(
62        preg_split(
63          '/\s+/',
64          str_replace(
65            $drop_char_match,
66            $drop_char_replace,
67            $_POST['search_allwords']
68            )
69          )
70        ),
71      'mode' => $_POST['mode'],
72      );
[455]73  }
[1059]74
[1119]75  if (isset($_POST['tags']))
76  {
[5195]77    check_input_parameter('tags', $_POST, true, PATTERN_ID);
[26825]78    check_input_parameter('tag_mode', $_POST, false, '/^(OR|AND)$/');
[4753]79   
[1119]80    $search['fields']['tags'] = array(
81      'words' => $_POST['tags'],
82      'mode'  => $_POST['tag_mode'],
83      );
84  }
[1125]85
[621]86  if ($_POST['search_author'])
[455]87  {
[1008]88    $search['fields']['author'] = array(
[1015]89      'words' => preg_split(
90        '/\s+/',
[26825]91        strip_tags($_POST['search_author'])
[1015]92        ),
93      'mode' => 'OR',
[1008]94      );
[17]95  }
[1059]96
[621]97  if (isset($_POST['cat']))
[2]98  {
[5195]99    check_input_parameter('cat', $_POST, true, PATTERN_ID);
[4753]100   
[1008]101    $search['fields']['cat'] = array(
102      'words'   => $_POST['cat'],
103      'sub_inc' => ($_POST['subcats-included'] == 1) ? true : false,
104      );
[2]105  }
[634]106
107  // dates
108  $type_date = $_POST['date_type'];
[1059]109
[621]110  if (!empty($_POST['start_year']))
[634]111  {
[1008]112    $search['fields'][$type_date.'-after'] = array(
[6518]113      'date' => sprintf(
[25005]114        '%d-%02d-%02d',
115        $_POST['start_year'],
116        $_POST['start_month'] != 0 ? $_POST['start_month'] : '01',
117        $_POST['start_day']   != 0 ? $_POST['start_day']   : '01'
[1008]118        ),
119      'inc' => true,
120      );
[634]121  }
[621]122
[634]123  if (!empty($_POST['end_year']))
[621]124  {
[1008]125    $search['fields'][$type_date.'-before'] = array(
[6518]126      'date' => sprintf(
[25005]127        '%d-%02d-%02d',
128        $_POST['end_year'],
129        $_POST['end_month'] != 0 ? $_POST['end_month'] : '12',
130        $_POST['end_day']   != 0 ? $_POST['end_day']   : '31'
[6518]131      ),
[1008]132      'inc' => true,
133      );
[621]134  }
[1059]135
[621]136  if (!empty($search))
137  {
[1008]138    // default search mode : each clause must be respected
139    $search['mode'] = 'AND';
140
141    // register search rules in database, then they will be available on
142    // thumbnails page and picture page.
143    $query ='
144INSERT INTO '.SEARCH_TABLE.'
[1816]145  (rules, last_seen)
[1008]146  VALUES
[1816]147  (\''.serialize($search).'\', NOW())
[1008]148;';
149    pwg_query($query);
150
[4892]151    $search_id = pwg_db_insert_id(SEARCH_TABLE);
[455]152  }
[621]153  else
[455]154  {
[25018]155    $page['errors'][] = l10n('Empty query. No criteria has been entered.');
[455]156  }
[2]157}
[455]158//----------------------------------------------------------------- redirection
[12764]159if (isset($_POST['submit']) and count($page['errors']) == 0)
[455]160{
[1082]161  redirect(
162    make_index_url(
163      array(
164        'section' => 'search',
165        'search'  => $search_id,
166        )
167      )
168    );
[455]169}
[2]170//----------------------------------------------------- template initialization
[621]171
[345]172//
173// Start output of page
174//
[5021]175$title= l10n('Search');
[850]176$page['body_id'] = 'theSearchPage';
[345]177
[2223]178$template->set_filename('search' ,'search.tpl' );
[1125]179
[2223]180$month_list = $lang['month'];
181$month_list[0]='------------';
182ksort($month_list);
183
184$template->assign(
[1314]185  array(
[2223]186    'F_SEARCH_ACTION' => 'search.php',
[1314]187    'U_HELP' => PHPWG_ROOT_PATH.'popuphelp.php?page=search',
[2324]188
[2223]189    'month_list' => $month_list,
190    'START_DAY_SELECTED' => @$_POST['start_day'],
191    'START_MONTH_SELECTED' => @$_POST['start_month'],
192    'END_DAY_SELECTED' => @$_POST['end_day'],
193    'END_MONTH_SELECTED' => @$_POST['end_month'],
[1314]194    )
195  );
196
[1677]197$available_tags = get_available_tags();
[1125]198
[1314]199if (count($available_tags) > 0)
200{
[2409]201  usort( $available_tags, 'tag_alpha_compare');
[1119]202
[2223]203  $template->assign(
204    'TAG_SELECTION',
205    get_html_tag_selection(
[1314]206        $available_tags,
207        'tags',
208        isset($_POST['tags']) ? $_POST['tags'] : array()
[2223]209        )
[1314]210    );
211}
[355]212
[455]213//------------------------------------------------------------- categories form
[614]214$query = '
[2324]215SELECT id,name,global_rank,uppercats
[1677]216  FROM '.CATEGORIES_TABLE.'
217'.get_sql_condition_FandF
218  (
219    array
220      (
221        'forbidden_categories' => 'id',
222        'visible_categories' => 'id'
223      ),
224    'WHERE'
225  ).'
[614]226;';
[2223]227display_select_cat_wrapper($query, array(), 'category_options', false);
[621]228
[10812]229
230// include menubar
231$themeconf = $template->get_template_vars('themeconf');
[10824]232if (!isset($themeconf['hide_menu_on']) OR !in_array('theSearchPage', $themeconf['hide_menu_on']))
[10812]233{
234  include( PHPWG_ROOT_PATH.'include/menubar.inc.php');
235}
236
237//------------------------------------------------------------ html code display
[1627]238include(PHPWG_ROOT_PATH.'include/page_header.php');
[18063]239trigger_action('loc_end_search');
[20609]240flush_page_messages();
[2223]241$template->pparse('search');
[369]242include(PHPWG_ROOT_PATH.'include/page_tail.php');
[362]243?>
Note: See TracBrowser for help on using the repository browser.