Advertisement
phpaddict

Search for string in the project files

Sep 7th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.51 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. last modification date: 07.09.2015 4:44PM
  5. */
  6.  
  7. # main settings
  8. error_reporting(E_ALL);
  9. set_time_limit(0);
  10. ini_set('memory_limit', '512M');
  11. ini_set('display_errors', 'On');
  12.  
  13. # files path
  14. $current_dir = dirname(__FILE__);
  15. $current_file = basename(__FILE__);
  16. $current_link = 'http://'. $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
  17.  
  18. # blacklists
  19. $blacklist_files = array(
  20.     '.',
  21.     '..',
  22.     $current_file,
  23. );
  24. $blacklist_folders = array(
  25.     'html',
  26.     // 'phpMyAdmin',
  27. );
  28. $blacklist_extensions = array(
  29.     'htaccess',
  30.     'jpeg',
  31.     'png',
  32.     'bmp',
  33.     'gif',
  34.     'pdf',
  35.     'eot',
  36.     'svg',
  37.     'ttf',
  38.     'woff',
  39.     'html',
  40.     'htm',
  41.     'zip',
  42.     'tar.gz',
  43. );
  44.  
  45. # static content delivery
  46. if (isset($_GET['file']) && is_string($_GET['file'])) {
  47.     switch ($_GET['file'])
  48.     {
  49.         case 'favicon':
  50.             $filetype = 'image/x-icon';
  51.             $content = 'AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYWFh/mFhYf5hYWH+YWFh/mFhYf5hYWH+YWFh/mFhYf5hYWH+YWFh/mFhYf5hYWH+YWFh/mFhYf4AAAAAYWFh/mFhYf5hYWH+YWFh/mFhYf5hYWH+YWFh/mFhYf5hYWH+YWFh/mFhYf5hYWH+YWFh/mFhYf5hYWH+YWFh/mFhYf5hYWH+YWFh/mFhYf5hYWH+YWFh/mFhYf5hYWH+YWFh/mFhYf5hYWH+YWFh/mFhYf5hYWH+YWFh/mFhYf5hYWH+YWFh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FhYf5hYWH+YWFh/mFhYf5hYWH+YWFh/mFhYf4AAAAAgAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAEAAA==';
  52.             break;
  53.     }
  54.  
  55.     if (isset($filetype)) {
  56.         header('Content-Type: '. $filetype);
  57.         echo base64_decode($content);
  58.         exit;
  59.     }
  60. }
  61.  
  62. // functions declaration
  63. function dir_to_array($dir) {
  64.  
  65.     static $result = array();
  66.     global $blacklist_files, $blacklist_folders, $blacklist_extensions;
  67.  
  68.     $cdir = scandir($dir);
  69.     foreach ($cdir as $key => $value)
  70.     {
  71.         if (!in_array($value, $blacklist_files))
  72.         {
  73.             $ext = pathinfo($value, PATHINFO_EXTENSION);
  74.             if (!$blacklist_extensions || !in_array($ext, $blacklist_extensions))
  75.             {
  76.                 if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) {
  77.                     if (!$blacklist_folders || !in_array(basename($dir), $blacklist_folders))
  78.                     {
  79.                         $result += dir_to_array($dir . DIRECTORY_SEPARATOR . $value);
  80.                     }
  81.                 } else {
  82.                     $result[] = $dir . DIRECTORY_SEPARATOR . $value;
  83.                 }
  84.             }
  85.         }
  86.     }
  87.  
  88.     return $result;
  89. }
  90.  
  91. function search_in_file($file, $keyword) {
  92.     $content = file_get_contents($file);
  93.     $k_len = strlen($keyword);
  94.     $occurrences = 0;
  95.     $lastPos = 0;
  96.     while (($lastPos = strpos($content, $keyword, $lastPos)) !== false) {
  97.         $lastPos = $lastPos + $k_len;
  98.         $occurrences++;
  99.     }
  100.     return $occurrences;
  101. }
  102.  
  103. function human_filesize($bytes, $decimals = 2) {
  104.     $size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
  105.     $factor = floor((strlen($bytes) - 1) / 3);
  106.     return sprintf("%.{$decimals}f ", $bytes / pow(1024, $factor)) . @$size[$factor];
  107. }
  108.  
  109. function get_first_level_directories_from_root() {
  110.     global $current_dir;
  111.  
  112.     $dirs = array();
  113.     $cdir = scandir($current_dir);
  114.     foreach ($cdir as $file) {
  115.         if ($file != '.' && $file != '..' && is_dir($file)) {
  116.             $dirs[] = $file;
  117.         }
  118.     }
  119.     return $dirs;
  120. }
  121.  
  122. function html_header($title = '') {
  123.     ?>
  124.     <!DOCTYPE html>
  125.     <html>
  126.     <head>
  127.         <meta charset="utf-8">
  128.         <title>Search in files for keyword - <?php echo $title;?></title>
  129.         <link rel="shortcut icon" href="?file=favicon" />
  130.         <style>
  131.         * {
  132.             margin: 0;
  133.         }
  134.         body {
  135.             padding: 10px;
  136.             font-size: 15px;
  137.             line-height: 1;
  138.             color: #444;
  139.         }
  140.         a, a:hover {
  141.             color: #FF6000;
  142.         }
  143.         h2 {
  144.             color: #444;
  145.             display: block;
  146.             margin-bottom: 10px;
  147.         }
  148.         p, form {
  149.             margin-bottom: 20px;
  150.         }
  151.         hr {
  152.             margin: 20px 0;
  153.             color: #eee;
  154.         }
  155.         table {
  156.             border-collapse: collapse;
  157.             border-spacing: 0;
  158.         }
  159.         thead td {
  160.             font-weight: bold;
  161.             font-size: 16px;
  162.             background-color: #fafafa;
  163.         }
  164.         table td {
  165.             border: 1px #ccc solid;
  166.             padding: 5px 12px;
  167.         }
  168.         tbody tr:hover td {
  169.             background-color: #FCF3C9;
  170.         }
  171.         input[type="text"] {
  172.             width: 220px;
  173.             border: 1px #888 solid;
  174.             padding: 10px 10px;
  175.             color: #888;
  176.             border-radius: 2px;
  177.         }
  178.         input[type="text"]:focus {
  179.             border-color: #008EE8;
  180.             box-shadow: inset 0 0 3px #eee;
  181.         }
  182.         input[type="submit"] {
  183.             border: 1px #888 solid;
  184.             padding: 9px 20px;
  185.             cursor: pointer;
  186.             background-color: #222;
  187.             color: #fff;
  188.             border-radius: 2px;
  189.         }
  190.         input[type="submit"]:hover {
  191.             background-color: #008EE8;
  192.             color: #fff;
  193.         }
  194.         .mb10 {
  195.             margin-bottom: 10px;
  196.         }
  197.         .db {
  198.             display: block;
  199.         }
  200.         .blue {
  201.             color: #008EE8;
  202.         }
  203.         .orange {
  204.             color: #FF6000;
  205.         }
  206.         .btn-warning {
  207.             background-color: #F6BB42;
  208.             color: #FFFFFF;
  209.         }
  210.         .btn-warning:hover {
  211.             background-color: #F4AE1D;
  212.             border-color: rgba(0, 0, 0, 0.05);
  213.             color: #FFFFFF;
  214.         }
  215.         .btn {
  216.             -moz-user-select: none;
  217.             background-image: none;
  218.             border: 1px solid rgba(0, 0, 0, 0);
  219.             border-radius: 0;
  220.             cursor: pointer;
  221.             display: inline-block;
  222.             font-size: 14px;
  223.             font-weight: normal;
  224.             line-height: 1.2;
  225.             margin-bottom: 0;
  226.             padding: 9px 12px;
  227.             text-align: center;
  228.             text-decoration: none;
  229.             vertical-align: middle;
  230.             white-space: nowrap;
  231.         }
  232.         .scrollbox {
  233.             border: 1px solid #CCCCCC;
  234.             width: 350px;
  235.             height: 100px;
  236.             background: #FFFFFF;
  237.             overflow-y: scroll;
  238.         }
  239.         .scrollbox div {
  240.             padding: 3px;
  241.         }
  242.         .scrollbox div input {
  243.             margin: 0px;
  244.             padding: 0px;
  245.             margin-right: 3px;
  246.         }
  247.         .scrollbox div.even {
  248.             background: #FFFFFF;
  249.         }
  250.         .scrollbox div.odd {
  251.             background: #E4EEF7;
  252.         }
  253.         </style>
  254.     </head>
  255.     <body>
  256.     <?php
  257. }
  258.  
  259. function html_footer() {
  260.     ?>
  261.     </body>
  262.     </html>
  263.     <?php
  264. }
  265.  
  266. # application start
  267. $found_in_files = array();
  268. $keyword = '';
  269. if (isset($_GET['keyword']) && is_string($_GET['keyword']) && !empty($_GET['keyword'])) {
  270.     $files_array = dir_to_array($current_dir);
  271.     $keyword = $_GET['keyword'];
  272.  
  273.     foreach ($files_array as $file) {
  274.         $occurrences = search_in_file($file, $keyword);
  275.         if (!$occurrences) continue;
  276.         $found_in_files[] = array(
  277.             'filename'    => basename($file),
  278.             'dirname'     => str_replace($current_dir, '', dirname($file)),
  279.             'occurrences' => $occurrences,
  280.             'filesize'    => filesize($file)
  281.         );
  282.     }
  283. }
  284.  
  285. # allowed sections
  286. $sections = array(
  287.     'home',
  288.     'options',
  289.     'download'
  290. );
  291.  
  292. if (!isset($_GET['section']) || !in_array($_GET['section'], $sections) || $_GET['section'] == 'home') {
  293.  
  294.     $directories = get_first_level_directories_from_root();
  295.    
  296.     if (!isset($_GET['dirs']) || !is_array($_GET['dirs'])) {
  297.         $dirs = array();
  298.     }
  299.     else {
  300.         $dirs = array_map('trim', $_GET['dirs']);
  301.     }
  302.  
  303. ?>
  304.     <?php html_header('Homepage'); ?>
  305.     <h2>Search in files for keyword</h2>
  306.     <div class="mb10">
  307.         <a href="?section=options">Options</a>
  308.     </div>
  309.     <p>Current path is <b><?php echo $current_dir;?></b></p>
  310.     <hr>
  311.     <form method="get" action="<?php echo $current_link;?>">
  312.         <label class="db mb10"><b>KEYWORD:</b> <input type="text" name="keyword" value="<?php echo htmlentities($keyword, ENT_QUOTES, 'UTF-8');?>" /></label>
  313.         <h4 class="mb10">Search directories</h4>
  314.         <div class="scrollbox mb10">
  315.             <?php foreach ($directories as $i => $directory) { ?>
  316.                 <div class="<?php if ($i % 2 == 0) { ?>even<?php } else { ?>odd<?php } ?>"><label><input type="checkbox" value="<?php echo $directory;?>" name="dirs[]"<?php if (empty($dirs) || in_array($directory, $dirs)) { ?> checked<?php } ?>> <?php echo $directory;?></label></div>
  317.             <?php } ?>
  318.         </div>
  319.         <input type="submit" name="sbm" value="Search" />
  320.         <?php if (1 || $keyword) { ?>
  321.             <a href="<?php echo $current_link;?>">Reset search</a>
  322.         <?php } ?>
  323.     </form>
  324.     <?php if ($keyword) { ?>
  325.         <?php if ($found_in_files) { ?>
  326.             <table>
  327.                 <thead>
  328.                     <tr>
  329.                         <td>#</td>
  330.                         <td>Filename</td>
  331.                         <td>Path</td>
  332.                         <td>FullPath</td>
  333.                         <td>Occurrences</td>
  334.                         <td>Filesize</td>
  335.                     </tr>
  336.                 </thead>
  337.                 <tbody>
  338.                     <?php foreach ($found_in_files as $i => $file) {
  339.                         if ($file['dirname'] != '') {
  340.                             $first_lvl_dirname = substr($file['dirname'], 0, strpos($file['dirname'], '/', 1));
  341.                             $first_lvl_dirname = ltrim($first_lvl_dirname, '/');
  342.                         } else {
  343.                             $first_lvl_dirname = '';
  344.                         }
  345.  
  346.                         if (!$dirs || in_array($first_lvl_dirname, $dirs)) { ?>
  347.                             <tr>
  348.                                 <td><?php echo ++$i;?></td>
  349.                                 <td class="blue"><?php echo $file['filename'];?></td>
  350.                                 <td class="blue"><?php echo $file['dirname'];?></td>
  351.                                 <td class="orange"><?php echo $file['dirname'].'/'.$file['filename'];?></td>
  352.                                 <td class="orange"><?php echo $file['occurrences'];?></td>
  353.                                 <td><?php echo str_replace('.00', '', human_filesize($file['filesize']));?></td>
  354.                             </tr>
  355.                         <?php }
  356.                     } ?>
  357.                 </tbody>
  358.             </table>
  359.         <?php } else { ?>
  360.             <p class="orange">No files were found</p>
  361.         <?php } ?>
  362.     <?php } else { ?>
  363.         <p class="orange">Please insert a keyword</p>
  364.     <?php } ?>
  365.     <?php html_footer(); ?>
  366. <?php } elseif ($_GET['section'] == 'download') {
  367.  
  368.     $data = file_get_contents(__FILE__);
  369.     header('Content-Type: application/x-httpd-php');
  370.     header('Content-Disposition: attachment; filename="'.$current_file.'"');
  371.     header("Content-Transfer-Encoding: binary");
  372.     header('Expires: 0');
  373.     header('Pragma: no-cache');
  374.     header("Content-Length: ".strlen($data));
  375.     echo $data;
  376.     exit;
  377.  
  378. } elseif ($_GET['section'] == 'options') { ?>
  379.     <?php html_header('Options'); ?>
  380.     <h2>Search in files for keyword</h2>
  381.     <div class="mb10">
  382.         <a href="?section=home">&lt; Homepage</a>
  383.     </div>
  384.     <p>Current path is <b><?php echo $current_dir;?></b></p>
  385.     <hr>
  386.     <h3 class="mb10">Download this file</h3>
  387.     <a href="?section=download" class="btn btn-warning">Download</a>
  388.    
  389. <?php } ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement