dir.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. /* $Id: dir.php 3225 2007-09-27 21:23:13Z danez $ */
  3. /*******************************************************************************
  4. LICENSE
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License (GPL)
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  14. *******************************************************************************/
  15. /*
  16. munk TODO:
  17. Check each of these items for correct functionality with encoding/decoding
  18. of HTML and URLs, including inc/iid/item.php and any templates associated with
  19. the item:
  20. vlc
  21. */
  22. // prevent direct invocation
  23. if ((!isset($cfg['user'])) || (isset($_REQUEST['cfg']))) {
  24. @ob_end_clean();
  25. @header("location: ../../index.php");
  26. exit();
  27. }
  28. /******************************************************************************/
  29. // common functions
  30. require_once('inc/functions/functions.common.php');
  31. // dir functions
  32. require_once('inc/functions/functions.dir.php');
  33. // config
  34. initRestrictedDirEntries();
  35. // check incoming path
  36. checkIncomingPath();
  37. // get request-vars
  38. $chmod = UrlHTMLSlashesDecode(tfb_getRequestVar('chmod'));
  39. $del = UrlHTMLSlashesDecode(tfb_getRequestVar('del'));
  40. $down = UrlHTMLSlashesDecode(tfb_getRequestVar('down'));
  41. $tar = UrlHTMLSlashesDecode(tfb_getRequestVar('tar'));
  42. $multidel = UrlHTMLSlashesDecode(tfb_getRequestVar('multidel'));
  43. $dir = UrlHTMLSlashesDecode(tfb_getRequestVar('dir'));
  44. // check dir-var
  45. if (tfb_isValidPath($dir) !== true) {
  46. AuditAction($cfg["constants"]["error"], "ILLEGAL DIR: ".$cfg["user"]." tried to access ".$dir);
  47. @error("Invalid Dir", "index.php?iid=dir", "", array($dir));
  48. }
  49. /*******************************************************************************
  50. * chmod
  51. ******************************************************************************/
  52. if ($chmod != "") {
  53. // is enabled ?
  54. if ($cfg["dir_enable_chmod"] != 1) {
  55. AuditAction($cfg["constants"]["error"], "ILLEGAL ACCESS: ".$cfg["user"]." tried to use chmod (".$dir.")");
  56. @error("chmod is disabled", "index.php?iid=index", "");
  57. }
  58. // only valid entry with permission
  59. if ((isValidEntry(basename($dir))) && (hasPermission($dir, $cfg["user"], 'w')))
  60. chmodRecursive($cfg["path"].$dir);
  61. else
  62. AuditAction($cfg["constants"]["error"], "ILLEGAL CHMOD: ".$cfg["user"]." tried to chmod ".$dir);
  63. @header("Location: index.php?iid=dir&dir=".UrlHTMLSlashesEncode($dir));
  64. exit();
  65. }
  66. /*******************************************************************************
  67. * delete
  68. ******************************************************************************/
  69. if ($del != "") {
  70. // only valid entry with permission
  71. if ((isValidEntry(basename($del))) && (hasPermission($del, $cfg["user"], 'w'))) {
  72. $current = delDirEntry($del);
  73. } else {
  74. AuditAction($cfg["constants"]["error"], "ILLEGAL DELETE: ".$cfg["user"]." tried to delete (".$del.")");
  75. $current = $del;
  76. if (tfb_isValidPath($del)) {
  77. $arTemp = explode("/", $del);
  78. if (count($arTemp) > 1) {
  79. array_pop($arTemp);
  80. $current = implode("/", $arTemp);
  81. }
  82. }
  83. }
  84. @header("Location: index.php?iid=dir&dir=".UrlHTMLSlashesEncode($current));
  85. exit();
  86. }
  87. /*******************************************************************************
  88. * multi-delete
  89. ******************************************************************************/
  90. if ($multidel != "") {
  91. foreach($_POST['file'] as $key => $element) {
  92. $element = urldecode($element);
  93. // only valid entry with permission
  94. if ((isValidEntry(basename($element))) && (hasPermission($element, $cfg["user"], 'w')))
  95. delDirEntry($element);
  96. else
  97. AuditAction($cfg["constants"]["error"], "ILLEGAL DELETE: ".$cfg["user"]." tried to delete ".$element);
  98. }
  99. @header("Location: index.php?iid=dir&dir=".UrlHTMLSlashesEncode($dir));
  100. exit();
  101. }
  102. /*******************************************************************************
  103. * download
  104. ******************************************************************************/
  105. if ($down != "") {
  106. // is enabled ?
  107. if ($cfg["enable_file_download"] != 1) {
  108. AuditAction($cfg["constants"]["error"], "ILLEGAL ACCESS: ".$cfg["user"]." tried to use download (".$down.")");
  109. @error("download is disabled", "index.php?iid=index", "");
  110. }
  111. // only valid entry with permission
  112. if ((isValidEntry(basename($down))) && (hasPermission($down, $cfg["user"], 'r'))) {
  113. $current = downloadFile($down);
  114. } else {
  115. AuditAction($cfg["constants"]["error"], "ILLEGAL DOWNLOAD: ".$cfg["user"]." tried to download ".$down);
  116. $current = $down;
  117. if (tfb_isValidPath($down)) {
  118. $path = $cfg["path"].$down;
  119. $p = explode(".", $path);
  120. $pc = count($p);
  121. $f = explode("/", $path);
  122. $file = array_pop($f);
  123. $arTemp = explode("/", $down);
  124. if (count($arTemp) > 1) {
  125. array_pop($arTemp);
  126. $current = implode("/", $arTemp);
  127. }
  128. }
  129. }
  130. @header("Location: index.php?iid=dir&dir=".UrlHTMLSlashesEncode($current));
  131. exit();
  132. }
  133. /*******************************************************************************
  134. * download as archive
  135. ******************************************************************************/
  136. if ($tar != "") {
  137. // is enabled ?
  138. if ($cfg["enable_file_download"] != 1) {
  139. AuditAction($cfg["constants"]["error"], "ILLEGAL ACCESS: ".$cfg["user"]." tried to use download (".$tar.")");
  140. @error("download is disabled", "index.php?iid=index", "");
  141. }
  142. // only valid entry with permission
  143. if ((isValidEntry(basename($tar))) && (hasPermission($tar, $cfg["user"], 'r'))) {
  144. $current = downloadArchive($tar);
  145. } else {
  146. AuditAction($cfg["constants"]["error"], "ILLEGAL TAR DOWNLOAD: ".$cfg["user"]." tried to download ".$tar);
  147. $current = $tar;
  148. if (tfb_isValidPath($tar)) {
  149. $arTemp = explode("/", $tar);
  150. if (count($arTemp) > 1) {
  151. array_pop($arTemp);
  152. $current = implode("/", $arTemp);
  153. }
  154. }
  155. }
  156. @header("Location: index.php?iid=dir&dir=".UrlHTMLSlashesEncode($current));
  157. exit();
  158. }
  159. /*******************************************************************************
  160. * dir-page
  161. ******************************************************************************/
  162. // check dir-var
  163. if (isset($dir)) {
  164. if ($dir != "")
  165. $dir = $dir."/";
  166. } else {
  167. $dir = "";
  168. }
  169. // dir-name
  170. $dirName = $cfg["path"].$dir;
  171. // dir-check
  172. if (!(@is_dir($dirName))) {
  173. // our dir is no dir but a file. use parent-directory.
  174. if (preg_match("/^(.+)\/.+$/", $dir, $matches) == 1)
  175. header("Location: index.php?iid=dir&dir=".UrlHTMLSlashesEncode($matches[1]));
  176. else
  177. header("Location: index.php?iid=dir");
  178. exit();
  179. }
  180. if (($dir != "") && (isValidEntry($dir) !== true)) {
  181. AuditAction($cfg["constants"]["error"], "ILLEGAL DIR: ".$cfg["user"]." tried to access ".$dir);
  182. @error("Invalid Dir", "index.php?iid=dir", "", array($dir));
  183. }
  184. // init template-instance
  185. tmplInitializeInstance($cfg["theme"], "page.dir.tmpl");
  186. // dirstats
  187. if ($cfg['enable_dirstats'] == 1) {
  188. $tmpl->setvar('enable_dirstats', 1);
  189. $du = dirsize($dirName);
  190. $tmpl->setvar('duTotal', formatBytesTokBMBGBTB($du));
  191. $tmpl->setvar('_TDDU', $cfg['_TDDU']);
  192. }
  193. else
  194. {
  195. $tmpl->setvar('enable_dirstats', 0);
  196. }
  197. // read in entries
  198. $entrys = array();
  199. $handle = opendir($dirName);
  200. while (false !== ($entry = readdir($handle))) {
  201. if (empty($dir)) { // parent dir
  202. if ((isValidEntry($entry)) && (hasPermission($entry, $cfg["user"], 'r')))
  203. array_push($entrys, $entry);
  204. } else { // sub-dir
  205. if (hasPermission($dir, $cfg["user"], 'r')) {
  206. if (isValidEntry($entry))
  207. array_push($entrys, $entry);
  208. }
  209. }
  210. }
  211. closedir($handle);
  212. natsort($entrys);
  213. // process entries and fill dir- + file-array
  214. $list = array();
  215. foreach ($entrys as $entry) {
  216. // acl-write-check
  217. if (empty($dir)) /* parent dir */
  218. $aclWrite = (hasPermission($entry, $cfg["user"], 'w')) ? 1 : 0;
  219. else /* sub-dir */
  220. $aclWrite = (hasPermission($dir, $cfg["user"], 'w')) ? 1 : 0;
  221. // symbolic links
  222. if(!is_link($dirName.$entry))
  223. {
  224. $islink = 0;
  225. }
  226. else
  227. {
  228. if(!($slink = readlink($dirName.$entry)))
  229. $slink = "";
  230. $islink = 1;
  231. }
  232. // dirstats
  233. if ($cfg['enable_dirstats'] == 1)
  234. {
  235. if($islink == 0) // it's not a symbolic link
  236. {
  237. $size = (is_dir($dirName.$entry))? formatBytesTokBMBGBTB(dirsize($dirName.$entry)):formatBytesTokBMBGBTB(filesize($dirName.$entry));
  238. $timeStamp = filemtime($dirName.$entry);
  239. $date = date("m-d-Y h:i a", $timeStamp);
  240. }
  241. else // it's a symbolic link
  242. {
  243. $size = 0;
  244. $date = "";
  245. }
  246. }
  247. else
  248. {
  249. $size = 0;
  250. $date = "";
  251. }
  252. if (is_dir($dirName.$entry)) // dir
  253. {
  254. // sfv
  255. if (($cfg['enable_sfvcheck'] == 1) && (false !== ($sfv = findSFV($dirName.$entry))))
  256. {
  257. $show_sfv = 1;
  258. $sfvdir = $sfv['dir'];
  259. $sfvsfv = $sfv['sfv'];
  260. }
  261. else
  262. {
  263. $show_sfv = 0;
  264. $sfvdir = "";
  265. $sfvsfv = "";
  266. }
  267. $isdir = 1;
  268. $show_nfo = 0;
  269. $show_rar = 0;
  270. }
  271. else if (!@is_dir($dirName.$entry)) // file
  272. {
  273. // image
  274. $image = "themes/".$cfg['theme']."/images/time.gif";
  275. $imageOption = "themes/".$cfg['theme']."/images/files/".getExtension($entry).".png";
  276. if (file_exists("./".$imageOption))
  277. $image = $imageOption;
  278. // nfo
  279. $show_nfo = ($cfg["enable_view_nfo"] == 1) ? isNfo($entry) : 0;
  280. // rar
  281. $show_rar = (($cfg["enable_rar"] == 1) && ($aclWrite == 1)) ? isRar($entry) : 0;
  282. // add entry to file-array
  283. $isdir = 0;
  284. $show_sfv = 0;
  285. $sfvdir = "";
  286. $sfvsfv = "";
  287. }
  288. // get Permission and format it userfriendly
  289. if(($fperm = fileperms($dirName.$entry)) !== FALSE)
  290. {
  291. $permission_oct = substr(decoct($fperm),-3);
  292. $permission = (is_dir($dirName.$entry))? "d":"-";
  293. for($i=0;$i<=2;$i++)
  294. {
  295. $permission_bin = decbin($permission_oct[$i]);
  296. $permission .= ($permission_bin[0] == 1)? "r":"-";
  297. $permission .= ($permission_bin[1] == 1)? "w":"-";
  298. $permission .= ($permission_bin[2] == 1)? "x":"-";
  299. }
  300. $permission .= " (0".$permission_oct.")";
  301. }
  302. if(function_exists('mb_detect_encoding') && function_exists('utf8_decode') && mb_detect_encoding(" ".$entry." ",'UTF-8,ISO-8859-1') == 'UTF-8')
  303. $entry = utf8_decode($entry);
  304. // add entry to dir-array
  305. array_push($list, array(
  306. 'is_dir' => $isdir,
  307. 'is_link' => $islink,
  308. 'aclWrite' => $aclWrite,
  309. 'permission' => $permission,
  310. 'entry' => $entry,
  311. 'real_entry' => $slink,
  312. 'urlencode1' => UrlHTMLSlashesEncode($dir.$entry),
  313. 'urlencode2' => UrlHTMLSlashesEncode($dir),
  314. 'urlencode3' => UrlHTMLSlashesEncode($entry),
  315. 'addslashes1' => addslashes($entry),
  316. 'size' => $size,
  317. 'date' => $date,
  318. 'image' => $image,
  319. 'show_sfv' => $show_sfv,
  320. 'sfvdir' => UrlHTMLSlashesEncode($sfvdir),
  321. 'sfvsfv' => UrlHTMLSlashesEncode($sfvsfv),
  322. 'show_nfo' => $show_nfo,
  323. 'show_rar' => $show_rar
  324. )
  325. );
  326. }
  327. // set template-loop
  328. $tmpl->setloop('list', $list);
  329. // define some things
  330. // dir
  331. $tmpl->setvar('dir', $dir);
  332. if($dirName != "/")
  333. $tmpl->setvar('parentdir', preg_replace("/.*\/(.+?)\//",'$1',$dirName));
  334. else
  335. $tmpl->setvar('parentdir', "/ (root)");
  336. // parent url
  337. if($dir != "")
  338. {
  339. if (preg_match("/^(.+)\/.+$/", $dir, $matches) == 1)
  340. $tmpl->setvar('parentURL', "index.php?iid=dir&dir=" . UrlHTMLSlashesEncode($matches[1]));
  341. else
  342. $tmpl->setvar('parentURL', "index.php?iid=dir");
  343. $tmpl->setvar('showparentURL', TRUE);
  344. }
  345. else
  346. $tmpl->setvar('showparentURL', FALSE);
  347. // chmod, parent-dir cannot be chmodded
  348. if ($dir == "")
  349. $tmpl->setvar('show_chmod', 0);
  350. else
  351. $tmpl->setvar('show_chmod', (($cfg["dir_enable_chmod"] == 1) && (hasPermission($dir, $cfg['user'], 'w'))) ? 1 : 0);
  352. //
  353. $tmpl->setvar('enable_rename', $cfg["enable_rename"]);
  354. $tmpl->setvar('enable_move', $cfg["enable_move"]);
  355. $tmpl->setvar('enable_sfvcheck', $cfg['enable_sfvcheck']);
  356. $tmpl->setvar('enable_vlc', $cfg['enable_vlc']);
  357. $tmpl->setvar('enable_rar', $cfg["enable_rar"]);
  358. $tmpl->setvar('enable_view_nfo', $cfg["enable_view_nfo"]);
  359. $tmpl->setvar('enable_file_download', $cfg["enable_file_download"]);
  360. $tmpl->setvar('package_type', $cfg["package_type"]);
  361. $tmpl->setvar('enable_maketorrent', $cfg["enable_maketorrent"]);
  362. $tmpl->setvar('bgDark', $cfg['bgDark']);
  363. $tmpl->setvar('bgLight', $cfg['bgLight']);
  364. //
  365. $tmpl->setvar('_DELETE', $cfg['_DELETE']);
  366. $tmpl->setvar('_DIR_REN_LINK', $cfg['_DIR_REN_LINK']);
  367. $tmpl->setvar('_DIR_MOVE_LINK', $cfg['_DIR_MOVE_LINK']);
  368. $tmpl->setvar('_ABOUTTODELETE', $cfg['_ABOUTTODELETE']);
  369. $tmpl->setvar('_BACKTOPARRENT', $cfg['_BACKTOPARRENT']);
  370. $tmpl->setvar('_ID_IMAGES', $cfg['_ID_IMAGES']);
  371. //
  372. tmplSetTitleBar($cfg["pagetitle"].' - '.$cfg['_DIRECTORYLIST']);
  373. tmplSetDriveSpaceBar();
  374. tmplSetFoot();
  375. tmplSetIidVars();
  376. // parse template
  377. $tmpl->pparse();
  378. ?>