· 10 years ago · May 31, 2016, 06:06 AM
1<form method="post" action="search1.php" class="container 50%" id="searchform">
2 <input type="text" name="name" placeholder="Enter the terms you wish to search for" />
3 <input type="submit" name="submit" value="Search" class="fit special" />
4<input type="radio" id="name" name="search" value="name" class="fit special" />
5 <input type="radio" id="phrase" name="search" value="phrase" class="fit special" />
6 </form>
7
8<?php require_once("/includes/functions.php"); ?>
9<?php require_once("/includes/class.php"); ?>
10
11<?php
12$dbhost = "localhost";
13$dbuser = "root";
14$dbpass = "sandeep";
15$dbname = "dbtuts";
16mysql_connect($dbhost,$dbuser,$dbpass) or die('cannot connect to the server');
17mysql_select_db($dbname) or die('database selection problem');
18?>
19
20<!DOCTYPE html>
21<html>
22<head>
23<title>SEARCHED FILES</title>
24<link rel="stylesheet" href="assets/css/main.css" />
25</head>
26<body>
27<section>
28<div class="table-wrapper">
29 <table class="alt">
30 <thead>
31 <tr>
32 <th>File Name</th>
33 <th>View</th>
34 </tr>
35 </thead>
36<?php
37 if(isset($_POST['submit'])){
38 $name=$_POST['name'];
39 if($name!=NULL)
40 {
41 if (!empty($_POST['search'])) {
42 if ($_POST['search']=="phrase") { //search by phrase
43 $searchthis = $name;
44 $matches = array();
45
46 $query = "SELECT file from ada ";
47 $query .= "UNION ";
48 $query .= "SELECT file from cdr ";
49 $query .= "UNION ";
50 $query .= "SELECT file from others ";
51 $query .= "UNION ";
52 $query .= "SELECT file from pdr ";
53 $query .= "UNION ";
54 $query .= "SELECT file from rr ";
55 $query .= "UNION ";
56 $query .= "SELECT file from sdd ";
57 $query .= "UNION ";
58 $query .= "SELECT file from tbl_uploads ";
59
60 $result = mysql_query($query);
61 $new_file = fopen("sample.txt","w") or die("Unable to open file!!");
62
63 while($row=mysql_fetch_array($result))
64 {
65 $filepath = getcwd() . "uploads\".$row['file'];
66 $path = str_replace('//', '\', $filepath);
67 $Obj = new DocxConversion($path);
68 $Text= $Obj->convertToText();
69 fwrite($new_file,$Text);
70 echo $new_file."<br/>";
71 $handle = fopen($new_file, "r");
72 if ($handle)
73 {
74 while (!feof($handle))
75 {
76 $buffer = fgets($handle);
77 if(strpos($buffer, $searchthis) !== FALSE)
78 {
79 $matches[] = $row['file'];
80 break;
81 }
82
83 }
84 fclose($handle);
85 }
86 }
87 $matches = array_filter($matches);
88
89 if (!empty($matches))
90 {
91 foreach($matches as $row)
92 {
93 ?>
94 <tr>
95 <td><?php echo $row ?></td>
96 <td><a href="uploads/<?php echo $row ?>" target="_blank">view file</a></td>
97 </tr>
98 <?php
99 }
100 }
101 else
102 {
103 //echo " Phrase not found!!!";
104 ?>
105 <script>
106 alert('Phrase not Found');
107 window.location.href='homepage.php';
108 </script>
109 <?php
110 }
111 }
112 else{ //search by name
113 $array = array(
114 "db1" => "ada",
115 "db2" => "cdr",
116 "db3" => "others",
117 "db4" => "pdr",
118 "db5" => "rr",
119 "db6" => "sdd",
120 "db7" => "tbl_uploads",
121 );
122
123 //connect to the database
124 $db=mysql_connect("localhost","root","sandeep") or die ('I cannot connect to the database because:'.mysql_error());
125
126 //-select the database to use
127 $mydb=mysql_select_db("dbtuts");
128 $no_of_access = false;
129 while ($db_name = current($array))
130 {
131
132 //-query the database table
133 $sql = "SELECT * FROM $db_name WHERE (file LIKE '%$name%')";
134
135 //-run the query against the mysql query function
136 $result=mysql_query($sql);
137 $num_rows = mysql_num_rows($result);
138 if($num_rows > 0)
139 {
140 //-create while loop and loop through result set
141 $no_of_access = true;
142 while($row=mysql_fetch_array($result))
143 {
144 ?>
145 <tr>
146 <td><?php echo $row['file'] ?></td>
147 <td><a href="uploads/<?php echo $row['file'] ?>" target="_blank">view file</a></td>
148 </tr>
149 <?php
150 }
151 }
152 else
153 {
154 if(!$no_of_access && $db_name == "tbl_uploads")
155 //echo "<p> Result not found!!<p>";
156 {
157 ?>
158 <script>
159 alert('Result Not Found!!');
160 window.location.href='homepage.php';
161 </script>
162 <?php
163 }
164 }
165 next($array);
166 }
167
168 }
169 }
170 else
171 {
172 //echo "<p>Please select an option</p>";
173 ?>
174 <script>
175 alert('Please Select an option');
176 window.location.href='homepage.php';
177 </script>
178 <?php
179 }
180 }
181 else
182 {
183 //echo "<p>Please enter a search query</p>";
184 ?>
185 <script>
186 alert('Please enter a search query');
187 window.location.href='homepage.php';
188 </script>
189 <?php
190 }
191 }
192?>
193</table>
194</div>
195</section>
196</body>
197</html>
198
199<?php require_once("/includes/pdf.php"); ?>
200<?php
201class DocxConversion{
202 private $filename;
203
204 public function __construct($filePath) {
205 $this->filename = $filePath;
206 }
207
208/************************doc file************************************/
209 private function read_doc() {
210 $fileHandle = fopen($this->filename, "r");
211 $line = @fread($fileHandle, filesize($this->filename));
212 $lines = explode(chr(0x0D),$line);
213 $outtext = "";
214 foreach($lines as $thisline)
215 {
216 $pos = strpos($thisline, chr(0x00));
217 if (($pos !== FALSE)||(strlen($thisline)==0))
218 {
219 } else {
220 $outtext .= $thisline." ";
221 }
222 }
223 $outtext = preg_replace("/[^a-zA-Z0-9s,.-nrt@/_()]/","",$outtext);
224 return $outtext;
225 }
226
227/************************docx file************************************/
228 private function read_docx(){
229
230 $striped_content = '';
231 $content = '';
232
233 $zip = zip_open($this->filename);
234
235 if (!$zip || is_numeric($zip)) return false;
236
237 while ($zip_entry = zip_read($zip)) {
238
239 if (zip_entry_open($zip, $zip_entry) == FALSE) continue;
240
241 if (zip_entry_name($zip_entry) != "word/document.xml") continue;
242
243 $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
244
245 zip_entry_close($zip_entry);
246 }// end while
247
248 zip_close($zip);
249
250 $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);
251 $content = str_replace('</w:r></w:p>', "rn", $content);
252 $striped_content = strip_tags($content);
253
254 return $striped_content;
255 }
256
257/************************PDF file************************************/
258 private function read_pdf(){
259 $a=new PDF2Text();
260 $a->setFilename($this->filename);
261 $a->decodePDF();
262 echo $a->output();
263
264 }
265
266 /************************excel sheet************************************/
267
268function xlsx_to_text($input_file){
269 $xml_filename = "xl/sharedStrings.xml"; //content file name
270 $zip_handle = new ZipArchive;
271 $output_text = "";
272 if(true === $zip_handle->open($input_file)){
273 if(($xml_index = $zip_handle->locateName($xml_filename)) !== false){
274 $xml_datas = $zip_handle->getFromIndex($xml_index);
275 $xml_handle = new DOMDocument();
276 $xml_handle->loadXML($xml_datas, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
277 $output_text = strip_tags($xml_handle->saveXML());
278 }else{
279 $output_text .="";
280 }
281 $zip_handle->close();
282 }else{
283 $output_text .="";
284 }
285 return $output_text;
286}
287
288/*************************power point files*****************************/
289function pptx_to_text($input_file){
290 $zip_handle = new ZipArchive;
291 $output_text = "";
292 if(true === $zip_handle->open($input_file)){
293 $slide_number = 1; //loop through slide files
294 while(($xml_index = $zip_handle->locateName("ppt/slides/slide".$slide_number.".xml")) !== false){
295 $xml_datas = $zip_handle->getFromIndex($xml_index);
296 $xml_handle = new DOMDocument();
297 $xml_handle->loadXML($xml_datas, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
298 $output_text .= strip_tags($xml_handle->saveXML());
299 $slide_number++;
300 }
301 if($slide_number == 1){
302 $output_text .="";
303 }
304 $zip_handle->close();
305 }else{
306 $output_text .="";
307 }
308 return $output_text;
309}
310
311
312 public function convertToText() {
313
314 if(isset($this->filename) && !file_exists($this->filename)) {
315 return "File Not exists";
316 }
317
318 $fileArray = pathinfo($this->filename);
319 $file_ext = $fileArray['extension'];
320 if($file_ext == "doc" || $file_ext == "docx" || $file_ext == "xlsx" || $file_ext == "pptx" || $file_ext == "pdf")
321 {
322 if($file_ext == "doc") {
323 return $this->read_doc($this->filename);
324 } elseif($file_ext == "docx") {
325 return $this->read_docx($this->filename);
326 } elseif($file_ext == "xlsx") {
327 return $this->xlsx_to_text($this->filename);
328 }elseif($file_ext == "pptx") {
329 return $this->pptx_to_text($this->filename);
330 }elseif($file_ext == "pdf") {
331 return $this->read_pdf($this->filename);
332 }
333 } else {
334 return "Invalid File Type";
335 }
336 }
337
338}
339
340?>