source: trunk/search.php @ 8595

Last change on this file since 8595 was 6518, checked in by nikrou, 14 years ago

Bug 1730 fixed : search doesn't work when database engine is not mysql
For posgresql and sqlite date must allways use day and month with two diggits.

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