PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/index.php

https://gitlab.com/alidzapp/gamerz-file-explorer
PHP | 194 lines | 158 code | 16 blank | 20 comment | 20 complexity | ff5d0fdbd5661cd8e54f95b4ef5d502b 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. ### Get And Check Current Directory Path
  9. $url_path = ! empty( $_GET['dir'] ) ? urldecode( trim( stripslashes( $_GET['dir'] ) ) ) : '';
  10. if( strpos( $url_path, '../' ) !== false || strpos( $url_path, './' ) !== false || strpos( $url_path, '//' ) !== false ) {
  11. display_error( 'Invalid Directory' );
  12. }
  13. ### Check Whether Directory Is In The Ignore Folders
  14. if(in_array( $url_path, $ignore_folders ) ) {
  15. display_error( 'Invalid Directory' );
  16. }
  17. ### Variables Variables Variables
  18. $get_sort_order = ! empty( $_GET['order'] ) ? trim( $_GET['order'] ) : '';
  19. $get_sort_by = ! empty( $_GET['by'] ) ? trim( $_GET['by'] ) : '';
  20. $full_directory_path = '';
  21. $directories_before_current = '';
  22. $directories_before_current_path = '';
  23. $current_directory_name = '';
  24. $current_directory_path = '';
  25. $sort_order = '';
  26. $sort_order_text = '';
  27. $sort_by = '';
  28. $gmz_files = [];
  29. $gmz_directories = [];
  30. $directory_names = explode( '/', $url_path );
  31. ### Current Directory Name
  32. $current_directory_name = $directory_names[sizeof( $directory_names ) - 1];
  33. ### Unset Current Directory Name
  34. unset( $directory_names[sizeof( $directory_names ) - 1] );
  35. ### Directory Path Up To Current Directory
  36. if( ! empty( $directory_names ) ) {
  37. foreach( $directory_names as $directory_name ) {
  38. $directories_before_current .= $directory_name.'/';
  39. }
  40. $directories_before_current = substr( $directories_before_current, 0, -1 );
  41. }
  42. ### If No Directory Is Specified
  43. if( empty( $url_path) ) {
  44. $full_directory_path = GFE_ROOT_DIR;
  45. } else {
  46. $full_directory_path = GFE_ROOT_DIR . '/' . $url_path;
  47. }
  48. ### If Current Directory Is Not Empty, Add A Trailing Slash
  49. if( ! empty( $current_directory_name ) ) {
  50. $current_directory_path = $current_directory_name . '/';
  51. }
  52. ### If There Is Directory Before The Current Directory, Add A Trailing Slash
  53. if( ! empty( $directories_before_current ) ) {
  54. $directories_before_current_path = $directories_before_current . '/';
  55. }
  56. ### Full URL
  57. $full_url = GFE_ROOT_URL . '/' . $directories_before_current_path . $current_directory_path;
  58. ### Determine Sort Order
  59. if( empty( $get_sort_order ) ) {
  60. $get_sort_order = GFE_DEFAULT_SORT_ORDER;
  61. }
  62. switch( $get_sort_order ) {
  63. case 'asc':
  64. $sort_order = SORT_ASC;
  65. $sort_order_text = 'Ascending';
  66. break;
  67. case 'desc':
  68. default:
  69. $sort_order = SORT_DESC;
  70. $sort_order_text = 'Descending';
  71. }
  72. ### Determine Sort By
  73. if( empty( $get_sort_by ) ) {
  74. $get_sort_by = GFE_DEFAULT_SORT_BY;
  75. }
  76. switch( $get_sort_by ) {
  77. case 'name':
  78. case 'size':
  79. case 'type':
  80. case 'date':
  81. $sort_by = $get_sort_by;
  82. break;
  83. default:
  84. $sort_by = 'date';
  85. }
  86. ### Execute The Function To List Files/Directories, It Will Return An Array
  87. list_directories_files( $full_directory_path );
  88. ### Sort The Array
  89. if( $sort_by === 'name' ) {
  90. $gmz_files = array_alphabetsort( $gmz_files, $sort_by, $sort_order );
  91. $gmz_directories = array_alphabetsort( $gmz_directories, $sort_by, $sort_order );
  92. } elseif( $sort_by === 'type' ) {
  93. $gmz_files = array_alphabetsort( $gmz_files, $sort_by, $sort_order );
  94. } else {
  95. usort( $gmz_files, 'array_numbersort' );
  96. usort( $gmz_directories, 'array_numbersort' );
  97. if( $sort_order === SORT_DESC ) {
  98. $gmz_files = array_reverse( $gmz_files );
  99. $gmz_directories = array_reverse( $gmz_directories );
  100. }
  101. }
  102. ?>
  103. <?php template_header( ! empty( $current_directory_name ) ? ' - Viewing Directory - ' . $current_directory_name : '' ); ?>
  104. <!-- List Directories/Files -->
  105. <div class="table-responsive">
  106. <table class="table table-sm table-hover">
  107. <thead class="thead-default">
  108. <tr>
  109. <th style="width: 50%;" onclick="parent.location.href='<?php echo create_sort_url( 'name' ); ?>';" onmouseover="this.style.cursor = 'pointer';" title="Sort By Name">Name&nbsp;<?php echo create_sort_image( 'name' ); ?></th>
  110. <th style="width: 10%;" onclick="parent.location.href='<?php echo create_sort_url( 'size' ); ?>';" onmouseover="this.style.cursor = 'pointer';" title="Sort By Size">Size&nbsp;<?php echo create_sort_image( 'size' ); ?></th>
  111. <th style="width: 20%;" onclick="parent.location.href='<?php echo create_sort_url( 'type' ); ?>';" onmouseover="this.style.cursor = 'pointer';" title="Sort By Type">Type&nbsp;<?php echo create_sort_image( 'type' ); ?></th>
  112. <th style="width: 20%;" onclick="parent.location.href='<?php echo create_sort_url( 'date' ); ?>';" onmouseover="this.style.cursor = 'pointer';" title="Sort By Date">Date&nbsp;<?php echo create_sort_image( 'date' ); ?></th>
  113. </tr>
  114. </thead>
  115. <tbody>
  116. <?php
  117. // If It Is Down One Level, Provide "Up One Level"
  118. if( ! empty( $url_path ) ) {
  119. if( ! empty( $directory_names ) ) {
  120. $parent_directory = $directories_before_current;
  121. } else {
  122. $parent_directory = 'home';
  123. }
  124. echo '<tr class="table-warning">';
  125. echo '<td colspan="4"><a href="' . url( $parent_directory, 'dir' ) . '" title="Parent Directory"><i class="fa fa-chevron-left"></i>&nbsp;Parent Directory</a></td>';
  126. echo '</tr>';
  127. }
  128. // If There Is Directory
  129. if( ! empty( $gmz_directories ) ) {
  130. foreach( $gmz_directories as $key => $value ) {
  131. $directory_name = $value['name'];
  132. $directory_size = format_size( $value['size'] );
  133. $directory_date = date( 'jS F Y', $value['date'] );
  134. echo '<tr>';
  135. echo '<td><a href="' . url( $directories_before_current_path . $current_directory_path . $directory_name, 'dir' ) . '" title="Folder: ' . $directory_name.' (' . $directory_size . ')"><i class="fa fa-fw fa-folder"></i>&nbsp;' . $directory_name . '</a></td>';
  136. echo '<td>' . $directory_size . '</td>';
  137. echo '<td>File Folder</td>';
  138. echo '<td>' . $directory_date . '</td>';
  139. echo '</tr>';
  140. }
  141. }
  142. // If There Is Files
  143. if( ! empty( $gmz_files ) ) {
  144. foreach( $gmz_files as $key => $value ) {
  145. $file_name = $value['name'];
  146. $file_size = format_size( $value['size'] );
  147. $file_date = date( 'jS F Y', $value['date'] );
  148. $file_extension = $value['type'];
  149. echo '<tr>';
  150. echo '<td><a href="' . url( $directories_before_current_path . $current_directory_path . $file_name, 'file' ) . '" title="File: ' . $file_name . ' ('. $file_size . ')"><i class="fa fa-fw ' . file_icon( $value['ext'] ) . '"></i>&nbsp;' . $file_name . '</a></td>';
  151. echo '<td>' . $file_size . '</td>';
  152. echo '<td>' . $file_extension . '</td>';
  153. echo '<td>' . $file_date . '</td>';
  154. echo '</tr>';
  155. }
  156. } else {
  157. echo '<tr class="table-info"><td class="text-center" colspan="4">No files found.</td></tr>';
  158. }
  159. // Folder And File Stats Variables
  160. $total_folders = sizeof( $gmz_directories );
  161. $total_files = sizeof( $gmz_files );
  162. $total_size = format_size( dir_size( $full_directory_path ) );
  163. $total_folders_name = ( $total_folders > 1 ? 'folders' : 'folder' );
  164. $total_files_name = ( $total_files > 1 ? 'files' : 'file' );
  165. $total_folders_files = $total_folders .' '. $total_folders_name . ', ' . $total_files . ' ' . $total_files_name;
  166. ?>
  167. </tbody>
  168. <tfoot>
  169. <tr>
  170. <td><strong><?php echo $total_folders_files; ?></strong></td>
  171. <td><strong><?php echo $total_size; ?></strong></td>
  172. <td>-</td>
  173. <td>-</td>
  174. </tr>
  175. </tfoot>
  176. </table>
  177. </div>
  178. <?php template_footer(); ?>