Modification log added
[mediadatabase.git] / php / searchfiles.php
blob25b88d10a6ac43bc254b4e4e2badc3475b78d486
1 <?php
3 require('inc.header.php');
4 require('inc.html.php');
7 function ShowDataSearchForm()
9 XHTML_Form_GET_Begin($GLOBALS['self']);
11 XHTML_Text("File search");
12 XHTML_Table_Begin();
14 XHTML_Table_Row_Begin();
16 XHTML_Table_Cell_Begin();
17 XHTML_Text("Filename");
18 XHTML_Table_Cell_End();
20 XHTML_Table_Cell_Begin();
21 XHTML_Form_Input_Text("search_filename");
22 XHTML_Text("Exact");
23 XHTML_Form_Input_Checkbox("search_exact");
24 XHTML_Text("case sensitive");
25 XHTML_Form_Input_Checkbox("case");
26 XHTML_Table_Cell_End();
28 XHTML_Table_Row_End();
30 XHTML_Table_Row_Begin();
32 XHTML_Table_Cell_Begin();
33 XHTML_Text("Min size");
34 XHTML_Table_Cell_End();
36 XHTML_Table_Cell_Begin();
38 XHTML_Form_Input_Text("search_minsize");
39 XHTML_Form_Select_Begin("min_size_unit");
40 XHTML_Form_Option("Bytes", "b");
41 XHTML_Form_Option("KBytes", "k", true);
42 XHTML_Form_Option("MBytes", "m");
43 XHTML_Form_Option("GBytes", "g");
44 XHTML_Form_Select_End();
46 XHTML_Table_Cell_End();
48 XHTML_Table_Row_End();
50 XHTML_Table_Row_Begin();
52 XHTML_Table_Cell_Begin();
53 XHTML_Text("Max size");
54 XHTML_Table_Cell_End();
56 XHTML_Table_Cell_Begin();
58 XHTML_Form_Input_Text("search_maxsize");
59 XHTML_Form_Select_Begin("max_size_unit");
60 XHTML_Form_Option("Bytes", "b");
61 XHTML_Form_Option("KBytes", "k", true);
62 XHTML_Form_Option("MBytes", "m");
63 XHTML_Form_Option("GBytes", "g");
64 XHTML_Form_Select_End();
66 XHTML_Table_Cell_End();
68 XHTML_Table_Row_End();
70 XHTML_Table_Row_Begin();
72 XHTML_Table_Cell_Begin();
73 XHTML_Text("Path");
74 XHTML_Table_Cell_End();
76 XHTML_Table_Cell_Begin();
77 XHTML_Form_Input_Text("search_path");
78 XHTML_Text("Exact");
79 XHTML_Form_Input_Checkbox("search_pathexact");
80 XHTML_Text("case sensitive");
81 XHTML_Form_Input_Checkbox("pathcase");
82 XHTML_Table_Cell_End();
84 XHTML_Table_Row_End();
86 if ($GLOBALS['config']['use_categories'])
88 XHTML_Table_Row_Begin();
90 XHTML_Table_Cell_Begin();
91 XHTML_Text("Category");
92 XHTML_Table_Cell_End();
94 XHTML_Table_Cell_Begin();
95 XHTML_Form_Input_Select_Begin("search_category");
97 XHTML_Form_Option("Any category", "any");
98 XHTML_Form_Option("--", "--");
100 mb_fetch_select_categories(4);
102 XHTML_Form_Input_Select_End();
103 XHTML_Table_Cell_End();
107 XHTML_Table_End();
109 XHTML_Form_Input_Submit("Search");
111 XHTML_Form_End();
114 // SQL Query builder
115 $where = array();
116 if (isset($_GET['search_filename']) && strlen($_GET['search_filename']) > 0)
118 if (isset($_GET['case']))
120 $binary = "BINARY ";
122 else
124 $binary = "";
127 if (isset($_GET['search_exact']))
129 $where[] = $binary."f.name = '".addslashes($_GET['search_filename'])."'";
131 else
133 $where[] = "f.name REGEXP ".$binary."'".addslashes($_GET['search_filename'])."'";
137 if (isset($_GET['search_minsize']) && is_numeric($_GET['search_minsize']))
139 $size = $_GET['search_minsize'];
140 switch ($_GET['min_size_unit'])
142 case "g":
143 $size *= 1024;
144 case "m":
145 $size *= 1024;
146 case "k":
147 $size *= 1024;
149 $where[] = "f.size >= ".$size;
152 if (isset($_GET['search_maxsize']) && is_numeric($_GET['search_maxsize']))
154 $size = $_GET['search_maxsize'];
155 switch ($_GET['max_size_unit'])
157 case "g":
158 $size *= 1024;
159 case "m":
160 $size *= 1024;
161 case "k":
162 $size *= 1024;
164 $where[] = "f.size <= ".$size;
167 if (isset($_GET['search_path']) && strlen($_GET['search_path']) > 0)
169 if (isset($_GET['pathcase']))
171 $binary = "BINARY ";
173 else
175 $binary = "";
178 if (isset($_GET['search_pathexact']))
180 $where[] = $binary."f.path = '".addslashes($_GET['search_path'])."'";
182 else
184 $where[] = "f.path REGEXP ".$binary."'".addslashes($_GET['search_path'])."'";
188 if (count($where) > 0)
190 // Database query {{{
191 $tables = $config['tbl_files']." AS f, ".$config['tbl_media']." AS m";
192 $where[] = 'f.mediaid = m.mediaid';
193 $cols = 'f.name, f.path, f.size, f.time, m.mediaid, m.name as medianame';
194 if ($config['use_categories'])
196 $tables .= ", ".$config['tbl_categories']." AS c";
197 $cols .= ", c.catid, c.name as catname";
198 $where[] = 'm.catid = c.catid';
199 $headers[] = 'Category';
201 $query = "SELECT $cols FROM $tables WHERE ".implode(' AND ',$where);
203 //echo "<p>".$query."</p>";
205 if (!$res = mysql_query($query))
207 echo "<b>Error:</b> ".mysql_error()." <i>(".basename(__FILE__).", line ".__LINE__.")</i>\n\n";
209 else
211 if (!$count = mysql_num_rows($res)) {
212 echo "Found no files matching your query.\n\n";
213 } else {
214 $pl = $count > 1 ? 's' : '';
215 echo "Found $count file$pl matching your query.\n<br><br>\n";
217 // Output
218 if ($config['use_categories']) {
219 mb_table_start('Filename','Path','Size','Timestamp','Medium','Media ID','Category');
220 } else {
221 mb_table_start('Filename','Path','Size','Timestamp','Medium','Media ID');
224 while ($row = mysql_fetch_array($res)) {
225 if (substr($row['name'],-1) == '/') {
226 mb_table_col('<nobr>'.mb_icon('__folder').'<a href="index.php?media='.$row['mediaid'].'&dir='.urlencode($row['path']).urlencode($row['name']).'">'.$row['name'].'</a></nobr>');
227 $showsize = false;
228 } else {
229 mb_table_col('<nobr>'.mb_icon('__unknown').$row['name'].'</nobr>');
230 $showsize = true;
232 mb_table_col('<nobr><a href="index.php?media='.$row['mediaid'].'&dir='.urlencode($row['path']).'">'.$row['path'].'</a></nobr>');
233 mb_table_col($showsize?mb_sizetext($row['size']):'');
234 mb_table_col(mb_datetext($row['time']));
235 mb_table_col('<a href="index.php?media='.$row['mediaid'].'">'.$row['medianame'].'</a>');
236 mb_table_col($row['mediaid']);
237 if ($config['use_categories']) {
238 mb_table_col('<a href="index.php?cat='.$row['catid'].'">'.$row['catname'].'</a>');
242 mb_table_end();
245 // }}}
246 echo "<p><a href=\"$self\">New search</a>\n";
248 else
250 ShowDataSearchForm();
253 require('inc.footer.php');