/search.php

https://gitlab.com/alidzapp/gamerz-file-explorer · PHP · 208 lines · 179 code · 13 blank · 16 comment · 19 complexity · 2a725fa77a325c0a0f27a00972b492e2 MD5 · raw file

  1. <?php
  2. ### Require Config, Setting And Function Files
  3. require( 'config.php' );
  4. require( 'settings.php' );
  5. require( 'functions.php' );
  6. ### Start Timer
  7. start_timer();
  8. ### Check Whether Search Is Enabled
  9. if( ! GFE_CAN_SEARCH ) {
  10. display_error( 'The Administrator Has Disabled The Searching Of Files' );
  11. }
  12. ### Variables Variables Variables
  13. $get_sort_order = ! empty( $_GET['order'] ) ? trim( $_GET['order'] ) : '';
  14. $get_sort_by = ! empty( $_GET['by'] ) ? trim( $_GET['by'] ) : '';
  15. $search_keyword = ! empty( $_GET['search'] ) ? trim( strip_tags( stripslashes( $_GET['search'] ) ) ) : '';
  16. $search_in = ! empty( $_GET['in'] ) ? trim( strip_tags( stripslashes( $_GET['in'] ) ) ) : '';
  17. // Variables Variables Variables
  18. $sort_order = '';
  19. $sort_order_image = '';
  20. $search_results = [];
  21. $sort_by = 'date';
  22. $sort_order_text = 'Descending';
  23. ### Process Search
  24. if( ! empty( $_GET['search'] ) ) {
  25. // Determine Sort Order
  26. if( empty( $get_sort_order ) ) {
  27. $get_sort_order = GFE_DEFAULT_SORT_ORDER;
  28. }
  29. switch( $get_sort_order ) {
  30. case 'asc':
  31. $sort_order = SORT_ASC;
  32. $sort_order_text = 'Ascending';
  33. break;
  34. case 'desc':
  35. default:
  36. $sort_order = SORT_DESC;
  37. $sort_order_text = 'Descending';
  38. }
  39. // Determine Sort By
  40. if( empty( $get_sort_by ) ) {
  41. $get_sort_by = GFE_DEFAULT_SORT_BY;
  42. }
  43. switch( $get_sort_by ) {
  44. case 'name':
  45. case 'size':
  46. case 'type':
  47. case 'date':
  48. $sort_by = $get_sort_by;
  49. break;
  50. default:
  51. $sort_by = 'date';
  52. }
  53. // Determine Search In
  54. if( empty( $search_in ) ) {
  55. $search_in = 'all';
  56. }
  57. // List All The files
  58. list_files( GFE_ROOT_DIR );
  59. // Check For Matches
  60. foreach( $gmz_files as $gmz_file ) {
  61. if( $search_in !== 'all' ) {
  62. if( strpos( strtolower( $gmz_file['name'] ), strtolower( $search_keyword ) ) !== false && strpos( $gmz_file['path'], $search_in ) !== false ) {
  63. $search_results[] = $gmz_file;
  64. }
  65. } else {
  66. if( strpos( strtolower( $gmz_file['name'] ), strtolower( $search_keyword ) ) !== false ) {
  67. $search_results[] = $gmz_file;
  68. }
  69. }
  70. }
  71. // We Do Not Need The File Listings Anymore
  72. unset( $gmz_files );
  73. // Sort The Array
  74. if( $sort_by === 'name' ) {
  75. $search_results = array_alphabetsort( $search_results, $sort_by, $sort_order );
  76. } elseif( $sort_by === 'type' ) {
  77. $search_results = array_alphabetsort( $search_results, $sort_by, $sort_order );
  78. } else {
  79. usort( $search_results, 'array_numbersort' );
  80. if( $sort_order === SORT_DESC ) {
  81. $search_results = array_reverse( $search_results );
  82. }
  83. }
  84. } else {
  85. // List All Directories
  86. list_directories( GFE_ROOT_DIR );
  87. }
  88. ?>
  89. <?php template_header( ! empty( $search_keyword ) ? ' - Search - ' . $search_keyword : ' - Search' ); ?>
  90. <!-- Search Files -->
  91. <form class="form" method="get" action="<?php echo GFE_URL; ?>/search.php">
  92. <div class="form-group row">
  93. <label for="search-term" class="col-sm-2 form-control-label">Search Term</label>
  94. <div class="col-sm-10">
  95. <input type="text" name="search" class="form-control" id="search-term" placeholder="Files ..." value="<?php echo $search_keyword; ?>">
  96. </div>
  97. </div>
  98. <div class="form-group row">
  99. <label for="search-in" class="col-sm-2 form-control-label">Search In</label>
  100. <div class="col-sm-10">
  101. <select id="search-in" name="in" class="form-control" size="1">
  102. <option value="all">All Folders</option>
  103. <?php
  104. foreach( $gmz_directories as $gmz_directory ) {
  105. if( $gmz_directory === $search_in ) {
  106. echo '<option value="' . $gmz_directory . '" selected="selected">' . $gmz_directory . '</option>';
  107. } else {
  108. echo '<option value="' . $gmz_directory . '">' . $gmz_directory . '</option>';
  109. }
  110. }
  111. ?>
  112. </select>
  113. </div>
  114. </div>
  115. <div class="form-group row">
  116. <label for="sort-by" class="col-sm-2 form-control-label">Sort By</label>
  117. <div class="col-sm-10">
  118. <select id="sort-by" name="by" class="form-control" size="1">
  119. <option value="name"<?php echo ( $sort_by === 'name' ? ' selected="selected"' : '' ); ?>>File Name</option>
  120. <option value="size"<?php echo ( $sort_by === 'size' ? ' selected="selected"' : '' ); ?>>File Size</option>
  121. <option value="type"<?php echo ( $sort_by === 'type' ? ' selected="selected"' : '' ); ?>>File Type</option>
  122. <option value="date"<?php echo ( $sort_by === 'date' ? ' selected="selected"' : '' ); ?>>File Date</option>
  123. </select>
  124. </div>
  125. </div>
  126. <div class="form-group row">
  127. <label for="sort-order" class="col-sm-2 form-control-label">Sort Order</label>
  128. <div class="col-sm-10">
  129. <select id="sort-order" name="order" class="form-control" size="1">
  130. <option value="asc"<?php echo ( $sort_order_text === 'Ascending' ? ' selected="selected"' : '' ); ?>>Ascending</option>
  131. <option value="desc"<?php echo ( $sort_order_text === 'Descending' ? ' selected="selected"' : '' ); ?>>Descending</option>
  132. </select>
  133. </div>
  134. </div>
  135. <div class="form-group row">
  136. <div class="col-sm-offset-2 col-sm-10">
  137. <button type="submit" class="btn btn-primary">Search</button>
  138. </div>
  139. </div>
  140. </form>
  141. <?php
  142. ### If Not Searching, Don't Display Results Page
  143. if( ! empty( $search_keyword ) ) {
  144. $total_size = 0;
  145. ?>
  146. <!-- List Search Results Files -->
  147. <div class="table-responsive">
  148. <table class="table table-sm table-hover">
  149. <thead class="thead-default">
  150. <tr>
  151. <th style="width: 50%;" title="Name">Name</th>
  152. <th style="width: 10%;" title="Size">Size</th>
  153. <th style="width: 20%;" title="Type">Type</th>
  154. <th style="width: 20%;" title="Date">Date</th>
  155. </tr>
  156. </thead>
  157. <tbody>
  158. <?php
  159. if( ! empty( $search_results ) ) {
  160. foreach( $search_results as $key => $value ) {
  161. $file_name = $value['name'];
  162. $file_size = format_size( $value['size'] );
  163. $file_date = date( 'jS F Y', $value['date'] );
  164. $file_extension = $value['type'];
  165. $total_size += $value['size'];
  166. echo '<tr>';
  167. echo '<td><a href="' . url( $value['path'], 'file' ) . '" title="File: ' . $file_name . ' ('. $file_size . ')"><i class="fa fa-fw ' . file_icon( $value['ext'] ) . '"></i>&nbsp;' . $file_name . '</a></td>';
  168. echo '<td>' . $file_size . '</td>';
  169. echo '<td>' . $file_extension . '</td>';
  170. echo '<td>' . $file_date . '</td>';
  171. echo '</tr>';
  172. }
  173. } else {
  174. echo '<tr class="table-info"><td class="text-center" colspan="4">No files found with the search term \'' . $search_keyword . '\'.</td></tr>';
  175. }
  176. // File Stats Variables
  177. $total_files = sizeof( $search_results );
  178. $total_size = format_size( $total_size );
  179. $total_files_name = ( $total_files > 1 ? 'files' : 'file' );
  180. ?>
  181. </tbody>
  182. <tfoot>
  183. <tr>
  184. <td><strong><?php echo $total_files . ' ' . $total_files_name; ?></strong></td>
  185. <td><strong><?php echo $total_size; ?></strong></td>
  186. <td>-</td>
  187. <td>-</td>
  188. </tr>
  189. </tfoot>
  190. </table>
  191. </div>
  192. <?php
  193. }
  194. ?>
  195. <?php template_footer(); ?>