PHP nosql file search
file searchunknown
php
3 years ago
9.3 kB
26
Indexable
<?php error_reporting(0); ?>
<html>
  <head>
  <link rel="icon" href="logo.jpg">
  <title>
    Home
  </title>
    <style>
      body {
        font-family: Arial;
      }    
      .main {
        padding: 5px;        
      }
      
      td {
        /*  remove the proportion can break site alignment */
        width: 33%;   
      } 
      .search {
        padding: 5px;
        margin: 2px;
      }
      a{
        padding-right: 10px;
      }
 
    </style>
  </head>
  <body>
    <table class='main' width='100%'>
      <tr>
        <td width='80%'>
          <a href='index.php'>Home</a>         
        </td>
        <td>
          <div align='right'>
          <form action="index.php" method="POST">
            <input type="text" placeholder="Insert a link or URL" name="link">
            <input type="text" placeholder="Name" name="name">
            <input type="text" placeholder="Category" name="category">
            <input type="submit" name="submit" value="Insert">            
          </form>
            <form action="index.php" method="POST">
              <input type="text" class="search" placeholder="Search" name="search">
              <input type="submit" class="search" value="Search">    
            </form>
          </div>
        </td>
      </tr>
    </table>
<?php
$link = $_POST['link'];
$name = $_POST['name'];
$category = $_POST['category'];
if(!$name){
    $name = preg_replace('/[^a-zA-Z0-9]/', ' ', $link);
}
if(!$category){
    $category = "others";
}
// Avoid write files in not allowed directories
$name = str_replace('.', "", $name);
$category = str_replace('.', "", $category);
$category = strtolower($category);
$no_symbol = array ('<', '>');
// Avoid write javascript in the files
$link = str_replace($no_symbol, "", $link);
$name = str_replace($no_symbol, "", $name);
// Get the last occurrence of '.' and the remaining text
$lastDotIndex = strrpos($link, ".");
if ($lastDotIndex) {
    $filetype = substr($link, $lastDotIndex + 1);
} else {
    $filetype = "others";
}
$filetype = str_replace('.', "", $filetype);
$filetype = strtolower($filetype);
if(!file_exists("categories")){
    mkdir("categories");
}
if(!file_exists("categories/$category")){
    mkdir("categories/$category");
}
if (isset($_POST['submit'])) {
    if(!file_exists("categories/$category/$name")){
        $file = fopen("categories/$category/$name", "w");
        fwrite($file, $link);
        fclose($file); 
        $result = "<br><a href='categories/$category/$name' target='_blank'>Sent with success!</a>";
    } else {
        
        $result = "<br>File already exists!";
    }
}
$start = $_GET['start'];  
	
if (!$start){$start = 0;}
$search = $_POST['search'];
if ($search == ""){$search = $_GET['search'];}	
// Avoid accessing the above directories
//$search = str_replace('.', "", $search);
if ($search == ""){$search = '.';}
$c = 0;
$limit = 20;
$ini = $start *  $limit;
$end = $ini + $limit;
$entry = 0;
$collum = 0;
$search = strtolower($search);
echo "<hr>$result <div align='center'><table width='100%'>";
if ($search != "" ){
    $dir = 'categories';
    // Open the directory
    if ($handle = opendir($dir)) {
        // Loop through each subdirectory
        while (false !== ($subdir = readdir($handle))) {
            if ($subdir != "." && $subdir != ".." && is_dir($dir.'/'.$subdir)) {
                // Open the subdirectory
                if ($subhandle = opendir($dir.'/'.$subdir)) {
                    // Loop through each file in the subdirectory
                    while (false !== ($file = readdir($subhandle))) { 
                        
                        $filename_written = $file;  
 
                        // Find the occurrence in lower or uppercase                     
                        $file_l = strtolower($file);
                        //if ($subdir == $search){echo 'ok';}
                        
                        // Check if the filename contains the string 
                        // If there is a category with the searched name all the files within that category will be displayed
                        if (strpos($file_l, $search) !== false || $subdir == $search) {
                            // Pagination
                            if($entry >= $ini and $entry  < $end){
                                
                                if ($file == "." || $file == ".."){continue;}                                
                                $file_path = $dir.'/'.$subdir.'/'.$file;
                                // Get size of the file
                                $filesize = filesize($file_path);
                                // Checks if is a binary file or small text content
                                if($filesize > 500){
                                    $contents = $file_path;
                                } else {
                                    $contents = file_get_contents($file_path);
                                }
                                // For get the last dot in the filename
                                $lastDotIndex = strrpos($contents, ".");
                                
                                if ($lastDotIndex) {
                                    $filetype = substr($contents, $lastDotIndex + 1);  
                                    $filetype = strtolower($filetype);
                                    // Don't show characters or variables after the file extension
                                    $filetype = substr($filetype, 0, 3);       
                                
                                } else{
                                    $filetype = "none";
                                }                               
                                $display_name = $filename_written;
                                $name_len = strlen($filename_written);
                                // Cut string if name is long
                                if ($name_len > 40){
                                    $display_name = substr($display_name, 0, 40) . "...";
                                }           
                                $img = "";
                                // Show case be a picture extension
                                if($filetype == "png" || $filetype == "jpg" || $filetype == "jpeg" || $filetype == "gif"){
                                   $img = "<td align='center'><a href='$contents' target='_blank'><img src='$contents' width='75%'></a><br>$display_name<br><a href='comment.php?comment_file=$file' target='_blank'>Comment</a></td>";                                     
                                }
                                // Show a thumbnail case exists
                                if(file_exists('thumbs/' . $subdir . '/' . $filename_written . '.jpg')){
                                   $img = "<td align='center'><a href='categories/$subdir/$filename_written' target='_blank'><img src='thumbs/$subdir/$filename_written.jpg' width='75%'></a><br>$display_name<br><a href='comment.php?comment_file=$file' target='_blank'>Comment</a></td>";                   
                                }
                                // Toggle table colors                                
                                // Show in the end the contents that don't are pictures (for don't break the pictures flow the 'continue' is used)
                                if ($img == ""){$toggle++; $td_color = $toggle % 2 == 0 ? '#FFF' : '#EEE'; $text_result = $text_result . "<tr style='background-color: $td_color;'><td>  <a href='$contents' target='_blank'>$filename_written</a></td><td>$subdir</td><td>$filetype</td><td><a href='comment.php?comment_file=$file' target='_blank'>Comment</a></td></tr>";$search_break++; continue;}
      
                                // Align and format content in three columns
                                if($collum == 0){echo "<tr>";}
                                echo $img;
                                $collum++;  
                                if($collum == 3){echo "</tr>"; $collum = 0;}                            
                                
                                $search_break++;
                            
                            }
                            $entry++;
                        
                            // Skip the files of the directory when reached the total results
                            if($search_break == $end){break;}
                        }
                   
                    }
                    if($search_break == $end){break;}
          
                    // Close the subdirectory
                    closedir($subhandle); 
                }
            
            }
        }
        // Close the directory
        closedir($handle);
    }
    if ($search_break == 0){
        echo "<br>Not found.";
    }
} 
echo "</table>";
echo "<br><table width='80%'>$text_result</table>";
if (!$search && !$start && !$result){
  echo "Hello!";
} 
echo "<br><br>";
if ($entry){
    for ($i = $start; $i < $start + 20; $i++) {
        echo "<a href='index.php?start=$i&search=$search'>$i </a>";
    }
} 
echo "</div>";
?>Editor is loading...