PageRenderTime 28ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/assets/components/janitor/extplorer/include/list.php

https://github.com/shamblett/janitor
PHP | 338 lines | 227 code | 42 blank | 69 comment | 77 complexity | 7e7866e8f3a9fc4e0638daa293cca7af MD5 | raw file
  1. <?php
  2. // ensure this file is being included by a parent file
  3. if( !defined( '_JEXEC' ) && !defined( '_VALID_MOS' ) ) die( 'Restricted access' );
  4. /**
  5. * @version $Id: list.php 105 2008-05-31 13:38:38Z soeren $
  6. * @package eXtplorer
  7. * @copyright soeren 2007
  8. * @author The eXtplorer project (http://sourceforge.net/projects/extplorer)
  9. * @author The The QuiX project (http://quixplorer.sourceforge.net)
  10. *
  11. * @license
  12. * The contents of this file are subject to the Mozilla Public License
  13. * Version 1.1 (the "License"); you may not use this file except in
  14. * compliance with the License. You may obtain a copy of the License at
  15. * http://www.mozilla.org/MPL/
  16. *
  17. * Software distributed under the License is distributed on an "AS IS"
  18. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  19. * License for the specific language governing rights and limitations
  20. * under the License.
  21. *
  22. * Alternatively, the contents of this file may be used under the terms
  23. * of the GNU General Public License Version 2 or later (the "GPL"), in
  24. * which case the provisions of the GPL are applicable instead of
  25. * those above. If you wish to allow use of your version of this file only
  26. * under the terms of the GPL and not to allow others to use
  27. * your version of this file under the MPL, indicate your decision by
  28. * deleting the provisions above and replace them with the notice and
  29. * other provisions required by the GPL. If you do not delete
  30. * the provisions above, a recipient may use your version of this file
  31. * under either the MPL or the GPL."
  32. *
  33. * Directory-Listing Functions
  34. */
  35. //------------------------------------------------------------------------------
  36. // HELPER FUNCTIONS (USED BY MAIN FUNCTION 'list_dir', SEE BOTTOM)
  37. function make_list(&$_list1, &$_list2) { // make list of files
  38. $list = array();
  39. if($GLOBALS["direction"]=="ASC") {
  40. $list1 = $_list1;
  41. $list2 = $_list2;
  42. } else {
  43. $list1 = $_list2;
  44. $list2 = $_list1;
  45. }
  46. if(is_array($list1)) {
  47. while (list($key, $val) = each($list1)) {
  48. $list[$key] = $val;
  49. }
  50. }
  51. if(is_array($list2)) {
  52. while (list($key, $val) = each($list2)) {
  53. $list[$key] = $val;
  54. }
  55. }
  56. return $list;
  57. }
  58. /**
  59. * make tables & place results in reference-variables passed to function
  60. * also 'return' total filesize & total number of items
  61. *
  62. * @param string $dir
  63. * @param array $dir_list
  64. * @param array $file_list
  65. * @param int $tot_file_size
  66. * @param int $num_items
  67. */
  68. function get_dircontents($dir, &$dir_list, &$file_list, &$tot_file_size, &$num_items) { // make table of files in dir
  69. $homedir = realpath($GLOBALS['home_dir']);
  70. $tot_file_size = $num_items = 0;
  71. // Open directory
  72. $handle = @$GLOBALS['ext_File']->opendir(get_abs_dir($dir));
  73. if($handle===false && $dir=="") {
  74. $handle = @$GLOBALS['ext_File']->opendir($homedir . $GLOBALS['separator']);
  75. }
  76. if($handle===false) {
  77. ext_Result::sendResult('list', false, $dir.": ".$GLOBALS["error_msg"]["opendir"]);
  78. }
  79. $file_list = array();
  80. $dir_list = array();
  81. // Read directory
  82. while(($new_item = @$GLOBALS['ext_File']->readdir($handle))!==false) {
  83. if( is_array( $new_item )) {
  84. $abs_new_item = $new_item;
  85. } else {
  86. $abs_new_item = get_abs_item($dir, $new_item);
  87. }
  88. /*if(get_is_dir( $abs_new_item)) {
  89. continue;
  90. }*/
  91. if ($new_item == "." || $new_item == "..") continue;
  92. if(!@$GLOBALS['ext_File']->file_exists($abs_new_item)) {
  93. //ext_Result::sendResult( 'list', false, $dir."/$abs_new_item: ".$GLOBALS["error_msg"]["readdir"]);
  94. }
  95. if(!get_show_item($dir, $new_item)) continue;
  96. $new_file_size = @$GLOBALS['ext_File']->filesize($abs_new_item);
  97. $tot_file_size += $new_file_size;
  98. $num_items++;
  99. $new_item_name = $new_item;
  100. if( ext_isFTPMode() ) {
  101. $new_item_name = $new_item['name'];
  102. }
  103. if(get_is_dir( $abs_new_item)) {
  104. if($GLOBALS["order"]=="modified") {
  105. $dir_list[$new_item_name] =
  106. @$GLOBALS['ext_File']->filemtime($abs_new_item);
  107. } else { // order == "size", "type" or "name"
  108. $dir_list[$new_item_name] = $new_item;
  109. }
  110. } else {
  111. if($GLOBALS["order"]=="size") {
  112. $file_list[$new_item_name] = $new_file_size;
  113. } elseif($GLOBALS["order"]=="modified") {
  114. $file_list[$new_item_name] =
  115. @$GLOBALS['ext_File']->filemtime($abs_new_item);
  116. } elseif($GLOBALS["order"]=="type") {
  117. $file_list[$new_item_name] =
  118. get_mime_type( $abs_new_item, "type");
  119. } else { // order == "name"
  120. $file_list[$new_item_name] = $new_item;
  121. }
  122. }
  123. }
  124. @$GLOBALS['ext_File']->closedir($handle);
  125. // sort
  126. if(is_array($dir_list)) {
  127. if($GLOBALS["order"]=="modified") {
  128. if($GLOBALS["direction"]=="ASC") arsort($dir_list);
  129. else asort($dir_list);
  130. } else { // order == "size", "type" or "name"
  131. if($GLOBALS["direction"]=="ASC") ksort($dir_list);
  132. else krsort($dir_list);
  133. }
  134. }
  135. // sort
  136. if(is_array($file_list)) {
  137. if($GLOBALS["order"]=="modified") {
  138. if($GLOBALS["direction"]=="ASC") arsort($file_list);
  139. else asort($file_list);
  140. } elseif($GLOBALS["order"]=="size" || $GLOBALS["order"]=="type") {
  141. if($GLOBALS["direction"]=="ASC") asort($file_list);
  142. else arsort($file_list);
  143. } else { // order == "name"
  144. if($GLOBALS["direction"]=="ASC") ksort($file_list);
  145. else krsort($file_list);
  146. }
  147. }
  148. if( $GLOBALS['start'] > $num_items ) {
  149. $GLOBALS['start'] = 0;
  150. }
  151. }
  152. /**
  153. * This function assembles an array (list) of files or directories in the directory specified by $dir
  154. * The result array is send using JSON
  155. *
  156. * @param string $dir
  157. * @param string $sendWhat Can be "files" or "dirs"
  158. */
  159. function send_dircontents($dir, $sendWhat='files') { // print table of files
  160. global $dir_up, $mainframe;
  161. // make file & dir tables, & get total filesize & number of items
  162. get_dircontents($dir, $dir_list, $file_list, $tot_file_size, $num_items);
  163. if( $sendWhat == 'files') {
  164. $list = $file_list;
  165. } elseif( $sendWhat == 'dirs') {
  166. $list = $dir_list;
  167. } else {
  168. $list = make_list( $dir_list, $file_list );
  169. }
  170. $i = 0;
  171. $items['totalCount'] = count($list);
  172. $items['items'] = array();
  173. $dirlist = array();
  174. if( $sendWhat != 'dirs' ) {
  175. // Replaced array_splice, because it resets numeric indexes (like files or dirs with a numeric name)
  176. // Here we reduce the list to the range of $limit beginning at $start
  177. $a = 0;
  178. $output_array = array();
  179. foreach( $list as $key => $value ) {
  180. if( $a >= $GLOBALS['start'] && ($a - $GLOBALS['start'] < $GLOBALS['limit'] )) {
  181. $output_array[$key] = $value;
  182. }
  183. $a++;
  184. }
  185. $list = $output_array;
  186. }
  187. while(list($item,$info) = each($list)) {
  188. // link to dir / file
  189. if( is_array( $info )) {
  190. $abs_item=$info;
  191. if( extension_loaded('posix')) {
  192. $user_info = posix_getpwnam( $info['user']);
  193. $file_info['uid'] = $user_info['uid'];
  194. $file_info['gid'] = $user_info['gid'];
  195. }
  196. } else {
  197. $abs_item=get_abs_item(utf8_decode($dir), $item);
  198. $file_info = @stat( $abs_item );
  199. }
  200. $is_dir = get_is_dir($abs_item);
  201. $items['items'][$i]['name'] = ext_isFTPMode() ? $item : utf8_encode($item);
  202. $items['items'][$i]['is_file'] = get_is_file($abs_item);
  203. $items['items'][$i]['is_archive'] = ext_isArchive( $item ) && !ext_isFTPMode();
  204. $items['items'][$i]['is_writable'] = $is_writable = @$GLOBALS['ext_File']->is_writable( $abs_item );
  205. $items['items'][$i]['is_chmodable'] = $is_chmodable = @$GLOBALS['ext_File']->is_chmodable( $abs_item );
  206. $items['items'][$i]['is_readable'] = $is_readable =@$GLOBALS['ext_File']->is_readable( $abs_item );
  207. $items['items'][$i]['is_deletable'] = $is_deletable = @$GLOBALS['ext_File']->is_deletable( $abs_item );
  208. $items['items'][$i]['is_editable'] = get_is_editable($abs_item);
  209. $items['items'][$i]['icon'] = _EXT_URL."/images/".get_mime_type($abs_item, "img");
  210. $items['items'][$i]['size'] = parse_file_size(get_file_size( $abs_item));
  211. // type
  212. $items['items'][$i]['type'] = get_mime_type( $abs_item, "type");
  213. // modified
  214. $items['items'][$i]['modified'] = parse_file_date( get_file_date($abs_item) );
  215. // permissions
  216. $perms = get_file_perms( $abs_item );
  217. if( strlen($perms)>3) {
  218. $perms = substr( $perms, 2 );
  219. }
  220. $items['items'][$i]['perms'] = $perms. ' ('. parse_file_perms( $perms ).')';
  221. if( extension_loaded( "posix" )) {
  222. $user_info = posix_getpwuid( $file_info["uid"] );
  223. //$group_info = posix_getgrgid($file_info["gid"] );
  224. $items['items'][$i]['owner'] = $user_info["name"]. " (".$file_info["uid"].")";
  225. } else {
  226. $items['items'][$i]['owner'] = 'n/a';
  227. }
  228. if( $is_dir && $sendWhat != 'files') {
  229. $id = str_replace('/', $GLOBALS['separator'], $dir). $GLOBALS['separator'].$item;
  230. $id = str_replace( $GLOBALS['separator'], '_RRR_', $id );
  231. $qtip ="<strong>".ext_Lang::mime('dir',true)."</strong><br /><strong>".ext_Lang::msg('miscperms',true).":</strong> ".$perms."<br />";
  232. $qtip.='<strong>'.ext_Lang::msg('miscowner',true).':</strong> '.$items['items'][$i]['owner'];
  233. $dirlist[] = array('text' => htmlspecialchars(ext_isFTPMode() ? $item : utf8_encode($item)),
  234. 'id' => ext_isFTPMode() ? $id : utf8_encode($id),
  235. 'qtip' => $qtip,
  236. 'is_writable' => $is_writable,
  237. 'is_chmodable' => $is_chmodable,
  238. 'is_readable' => $is_readable,
  239. 'is_deletable' => $is_deletable,
  240. 'cls' => 'folder');
  241. }
  242. if( !$is_dir && $sendWhat == 'files' || $sendWhat == 'both') {
  243. $i++;
  244. }
  245. }
  246. while( @ob_end_clean() );
  247. if( $sendWhat == 'dirs') {
  248. $result = $dirlist;
  249. } else {
  250. $result = $items;
  251. }
  252. $json = new ext_Json();
  253. echo $json->encode( $result );
  254. ext_exit();
  255. }
  256. class ext_List extends ext_Action {
  257. function execAction($dir) { // list directory contents
  258. global $dir_up, $mosConfig_live_site, $_VERSION;
  259. $allow=($GLOBALS["permissions"]&01)==01;
  260. $admin=((($GLOBALS["permissions"]&04)==04) || (($GLOBALS["permissions"]&02)==02));
  261. $dir_up = dirname($dir);
  262. if($dir_up==".") $dir_up = "";
  263. if(!get_show_item($dir_up,basename($dir))) {
  264. ext_Result::sendResult('list', false, $dir." : ".$GLOBALS["error_msg"]["accessdir"]);
  265. }
  266. // Sorting of items
  267. if($GLOBALS["direction"]=="ASC") {
  268. $_srt = "no";
  269. } else {
  270. $_srt = "yes";
  271. }
  272. show_header();
  273. $scriptTag = '
  274. <script type="text/javascript" src="'. _EXT_URL . '/fetchscript.php?'
  275. .'&amp;subdir[]=scripts/editarea/&amp;file[]=edit_area_full_with_plugins.js'
  276. .'&amp;subdir[]=scripts/extjs/&amp;file[]=yui-utilities.js'
  277. .'&amp;subdir[]=scripts/extjs/&amp;file[]=ext-yui-adapter.js'
  278. .'&amp;subdir[]=scripts/extjs/&amp;file[]=ext-all.js&amp;gzip=1"></script>
  279. <script type="text/javascript" src="'. $GLOBALS['script_name'].'?option=com_extplorer&amp;action=include_javascript&amp;file=functions.js"></script>
  280. <link rel="stylesheet" href="'. _EXT_URL . '/fetchscript.php?subdir[0]=scripts/extjs/css/&file[0]=ext-all.css&amp;subdir[1]=scripts/extjs/css/&file[1]=xtheme-aero.css&amp;gzip=1" />';
  281. if( defined( 'EXT_STANDALONE' )) {
  282. $GLOBALS['mainframe']->addcustomheadtag( $scriptTag );
  283. } else {
  284. echo $scriptTag;
  285. }
  286. ?>
  287. <div id="dirtree"></div>
  288. <div id="dirtree-panel"></div>
  289. <div id="item-grid"></div>
  290. <div id="ext_statusbar" class="ext_statusbar"></div>
  291. <?php
  292. // That's the main javascript file to build the Layout & App Logic
  293. include( _EXT_PATH.'/scripts/application.js.php' );
  294. }
  295. }
  296. ?>