PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/kloxo/httpdocs/htmllib/lib/commonfslib.php

https://bitbucket.org/Nemcio/kloxo-mr
PHP | 532 lines | 364 code | 79 blank | 89 comment | 54 complexity | 61ea6f3aa78bbf447ba50a497017b8f5 MD5 | raw file
  1. <?php
  2. function shell_recurse_dir($dir, $func, $arglist = null)
  3. {
  4. $list = lscandir_without_dot($dir);
  5. if (!$list) return;
  6. foreach($list as $file) {
  7. $path = $dir . "/" . $file;
  8. if (lis_dir($path)) {
  9. shell_recurse_dir($path, $func, $arglist);
  10. }
  11. /// After a successfuul recursion, you have to call the $func on the directory itself. So $func is called whether $path is both directory OR a file.
  12. $narglist = null;
  13. $narglist[] = $path;
  14. foreach((array) $arglist as $a) {
  15. $narglist[] = $a;
  16. }
  17. //dprint("calling with :");
  18. //dprintr($narglist);
  19. call_user_func_array($func, $narglist);
  20. }
  21. }
  22. function lxshell_direct($cmd)
  23. {
  24. $username = '__system__';
  25. do_exec_system($username, null, $cmd, $out, $err, $ret, null);
  26. }
  27. function lxfile_disk_free_space($dir)
  28. {
  29. $dir = expand_real_root($dir);
  30. lxfile_mkdir($dir);
  31. $ret = disk_free_space($dir);
  32. $ret = round($ret/(1024 * 1024), 1);
  33. log_shell("Disk Space $dir $ret");
  34. return $ret;
  35. }
  36. function lxshell_unzip_numeric_with_throw($dir, $file, $list = null)
  37. {
  38. $ret = lxshell_unzip_numeric($dir, $file, $list);
  39. if ($ret) {
  40. // throw new lxException("could_not_unzip_file", '');
  41. // MR -- more informative error message
  42. throw new lxException($msg, "Could not unzip file - dir: {$dir}; file: {$file}");
  43. }
  44. }
  45. function lxshell_unzip_with_throw($dir, $file, $list = null)
  46. {
  47. lxuser_unzip_with_throw('__system__', $dir, $file, $list);
  48. }
  49. function lxshell_redirect($file, $cmd)
  50. {
  51. global $gbl, $sgbl, $login, $ghtml;
  52. $start = 2;
  53. eval($sgbl->arg_getting_string);
  54. $cmd = getShellCommand($cmd, $arglist);
  55. $return = null;
  56. system("$cmd > $file 3</dev/null 4</dev/null 5</dev/null 6</dev/null", $return);
  57. return $return;
  58. }
  59. function lxshell_directory($dir, $cmd)
  60. {
  61. global $gbl, $sgbl, $login, $ghtml;
  62. $dir = expand_real_root($dir);
  63. $username = '__system__';
  64. $start = 2;
  65. eval($sgbl->arg_getting_string);
  66. $cmd = getShellCommand($cmd, $arglist);
  67. do_exec_system($username, $dir, $cmd, $out, $err, $ret, null);
  68. return $out;
  69. }
  70. function lxshell_output($cmd)
  71. {
  72. global $gbl, $sgbl, $login, $ghtml;
  73. $username = '__system__';
  74. $start = 1;
  75. eval($sgbl->arg_getting_string);
  76. $cmd = getShellCommand($cmd, $arglist);
  77. do_exec_system($username, null, $cmd, $out, $err, $ret, null);
  78. return $out;
  79. }
  80. function lxshell_return($cmd)
  81. {
  82. global $sgbl;
  83. $username = '__system__';
  84. $start = 1;
  85. eval($sgbl->arg_getting_string);
  86. $cmd = getShellCommand($cmd, $arglist);
  87. do_exec_system($username, null, $cmd, $out, $err, $ret, null);
  88. return $ret;
  89. }
  90. function lxshell_php($cmd)
  91. {
  92. lxshell_return("__path_php_path", $cmd);
  93. }
  94. function lxshell_input($input, $cmd)
  95. {
  96. global $sgbl;
  97. $username = '__system__';
  98. $start = 2;
  99. eval($sgbl->arg_getting_string);
  100. $cmd = getShellCommand($cmd, $arglist);
  101. do_exec_system($username, null, $cmd, $out, $err, $ret, $input);
  102. return $ret;
  103. }
  104. /**
  105. * Unzip file to the given directory as given user
  106. *
  107. * @param $username
  108. * @param $dir path to the output directory
  109. * @param $file file to unzip
  110. * @param $list
  111. */
  112. function lxuser_unzip_with_throw($username, $dir, $file, $list = null)
  113. {
  114. $ret = lxshell_unzip($username, $dir, $file, $list);
  115. if ($ret) {
  116. // throw new lxException("could_not_unzip_file", '');
  117. // MR -- more informative error message
  118. throw new lxException($msg, "Could not unzip file - dir: {$dir}; file: {$file}");
  119. }
  120. }
  121. /**
  122. * Makes new directory and set directory owner
  123. *
  124. * @param string $username directory owner
  125. * @param string $dir path to the directory
  126. * @return bool TRUE on success or FALSE on failure.
  127. */
  128. function lxuser_mkdir($username, $dir)
  129. {
  130. if (!lxfile_mkdir($dir)) {
  131. return false;
  132. }
  133. lxfile_generic_chown($dir, $username);
  134. return true;
  135. }
  136. /**
  137. * Copies file or directory and set owner
  138. *
  139. * @param string $username file or directory owner
  140. * @param string $src path to the source file or directory
  141. * @param string $dst path to the destination file or directory
  142. * @return bool TRUE on success or FALSE on failure.
  143. */
  144. function lxuser_cp($username, $src, $dst)
  145. {
  146. if (!lxfile_cp($src, $dst)) {
  147. return false;
  148. }
  149. if (!lxfile_generic_chown($dst, $username)) {
  150. lxfile_rm($dst);
  151. return false;
  152. }
  153. return true;
  154. }
  155. /**
  156. * Moves file or directory and set owner
  157. *
  158. * @param string $username file or directory owner
  159. * @param string $src path to the file or directory
  160. * @param string $dst new path to the file or directory
  161. * @return bool TRUE on success or FALSE on failure.
  162. */
  163. function lxuser_mv($username, $src, $dst)
  164. {
  165. if (!lxfile_mv($src, $dst)) {
  166. return false;
  167. }
  168. if (!lxfile_generic_chown($dst, $username)) {
  169. lxfile_mv($dst, $src);
  170. return false;
  171. }
  172. return true;
  173. }
  174. /**
  175. * Writes a string to a file
  176. *
  177. * @param string $username file owner
  178. * @param string $file path to the file
  179. * @param mixed $data the data to write
  180. * @param int $flag
  181. */
  182. function lxuser_put_contents($username, $file, $data, $flag = 0)
  183. {
  184. $file = expand_real_root($file);
  185. dprint("Debug: filename is: ". $file . "\n");
  186. if (is_soft_or_hardlink($file)) {
  187. log_log("link_error", "$file is hard or symlink. Not writing\n");
  188. return false;
  189. }
  190. if (!lxuser_mkdir($username, dirname($file))) {
  191. return false;
  192. }
  193. if ($flag == FILE_APPEND) {
  194. $mode = 'a';
  195. } else {
  196. $mode = 'w';
  197. }
  198. $f = fopen($file, $mode);
  199. if (!$f) {
  200. return false;
  201. } else {
  202. if (flock($f, LOCK_EX)) {
  203. $bytes = fwrite($f, $data);
  204. lxfile_generic_chown($file, $username);
  205. flock($f, LOCK_UN);
  206. fclose($f);
  207. return $bytes;
  208. }
  209. else {
  210. return false;
  211. }
  212. }
  213. }
  214. /**
  215. * Changes file mode
  216. *
  217. * @param $username
  218. * @param $file path to the file
  219. * @param $mod mode of the specified file
  220. */
  221. function lxuser_chmod($username, $file, $mod)
  222. {
  223. // I'm not sure how we should implement this method ???
  224. lxfile_generic_chmod($file, $mod);
  225. //lxfile_generic_chown($file, $username); ???
  226. }
  227. /**
  228. * Executes an external program or command as given user
  229. *
  230. * @param $username
  231. * @param $cmd command to execute
  232. * @return depends on executed command
  233. */
  234. function lxuser_return($username, $cmd) {
  235. global $sgbl;
  236. $start = 2;
  237. eval($sgbl->arg_getting_string);
  238. $cmd = getShellCommand($cmd, $arglist);
  239. $ret = new_process_cmd($username, null, $cmd);
  240. return $ret;
  241. }
  242. /**
  243. * Deletes filename or empty directory.
  244. *
  245. * @param $file path to the file or to the directory
  246. * @return TRUE on success or FALSE on failure.
  247. */
  248. function lxfile_rm($file)
  249. {
  250. $file = expand_real_root($file);
  251. log_filesys("Removing $file");
  252. if (lxfile_exists($file)) {
  253. if (lis_dir($file)) {
  254. return rmdir($file);
  255. } else {
  256. return unlink($file);
  257. }
  258. }
  259. return false;
  260. }
  261. /**
  262. * Renames/Moves file
  263. *
  264. * @param $src path to the source file
  265. * @param $dst destination path
  266. * @return TRUE on success or FALSE on failure.
  267. */
  268. function lxfile_mv($src, $dst)
  269. {
  270. global $gbl, $sgbl, $login, $ghtml;
  271. $src = expand_real_root($src);
  272. $dst = expand_real_root($dst);
  273. if (is_dir($dst)) {
  274. $base = basename($src);
  275. $dst = $dst . "/$base";
  276. }
  277. if ($sgbl->dbg > 0) {
  278. log_filesys("Moving $src $dst");
  279. }
  280. if (lxfile_cp($src, $dst)) {
  281. lxfile_rm($src);
  282. return true;
  283. }
  284. return false;
  285. }
  286. function lxfile_exists($file)
  287. {
  288. return lfile_exists($file);
  289. }
  290. function lxfile_real($file)
  291. {
  292. $size = lxfile_size($file);
  293. return ($size != 0);
  294. }
  295. function lxfile_nonzero($file)
  296. {
  297. return lxfile_real($file);
  298. }
  299. /**
  300. * Makes directory
  301. *
  302. * @param $dir the directory path
  303. * @return TRUE on success or FALSE on failure.
  304. */
  305. function lxfile_mkdir($dir)
  306. {
  307. if(empty($dir)){ # Avoid "", NULL or false values
  308. return false;
  309. }
  310. $dir = expand_real_root($dir);
  311. if(lxfile_exists($dir)){
  312. return true;
  313. }
  314. if(file_exists($dir)){ # Real check by PHP
  315. return true;
  316. }
  317. log_filesys("Making directory $dir");
  318. return mkdir($dir, 0755, true);
  319. }
  320. /**
  321. * Copies file
  322. *
  323. * @param $src path to the source file
  324. * @param $dst destination path
  325. * @return TRUE on success or FALSE on failure.
  326. */
  327. function lxfile_cp($src, $dst)
  328. {
  329. global $gbl, $sgbl, $login, $ghtml;
  330. $src = expand_real_root($src);
  331. $dst = expand_real_root($dst);
  332. if (is_dir($dst)) {
  333. $dst = $dst . "/" . basename($src);
  334. }
  335. log_filesys("Copying $src $dst");
  336. system("cp $src $dst", $ret);
  337. return $ret == 0;
  338. }
  339. function lxfile_stat($file, $duflag)
  340. {
  341. $file = expand_real_root($file);
  342. $list = lstat($file);
  343. if (($duflag && is_dir($file)) || $file === ".trash") {
  344. $list['size'] = lxfile_dirsize($file, true);
  345. } else {
  346. $list['size'] = lxfile_size($file);
  347. }
  348. get_file_type($file, $list);
  349. return $list;
  350. }
  351. function get_file_type($file, &$stat)
  352. {
  353. if (is_link($file)) {
  354. $dst = readlink($file);
  355. if (($dst[0] !== '/')) {
  356. $dst = dirname($file) . "/" . $dst;
  357. }
  358. if (!lxfile_exists($dst)) {
  359. $stat['ttype'] = "brokenlink";
  360. $stat['linkto'] = $dst;
  361. return;
  362. }
  363. if (is_dir($dst)) {
  364. $stat['ttype'] = "dirlink";
  365. $stat['linkto'] = $dst;
  366. return;
  367. }
  368. $stat['ttype'] = "filelink";
  369. $stat['linkto'] = $dst;
  370. return;
  371. }
  372. if (is_dir($file)) {
  373. $stat['ttype'] = "directory";
  374. return;
  375. }
  376. $list = pathinfo($file);
  377. $ext = null;
  378. if (isset($list['extension'])) {
  379. $ext = $list['extension'];
  380. }
  381. if ($ext === "zip") {
  382. $stat['ttype'] = "zip";
  383. return;
  384. }
  385. if ($ext === "tgz" || $ext === "tar.gz" || $ext === "gz") {
  386. $stat['ttype'] = "tgz";
  387. return;
  388. }
  389. if ($ext === "tar") {
  390. $stat['ttype'] = "tgz";
  391. return;
  392. }
  393. $stat['ttype'] = "file";
  394. }
  395. function lxfile_dstat($dir, $duflag)
  396. {
  397. $dir = expand_real_root($dir);
  398. $list = lscandir_without_dot($dir);
  399. $ret = null;
  400. foreach($list as $l) {
  401. $stat = lstat("$dir/$l");
  402. get_file_type("$dir/$l", $stat);
  403. remove_unnecessary_stat($stat);
  404. if (($duflag && is_dir("$dir/$l") || $l === ".trash")) {
  405. $stat['size'] = lxfile_dirsize("$dir/$l", true);
  406. } else {
  407. $stat['size'] = lxfile_size("$dir/$l");
  408. }
  409. $stat['name'] = "$dir/$l";
  410. $ret[] = $stat;
  411. }
  412. //dprintr($ret);
  413. return $ret;
  414. }
  415. function lxfile_getfile($file, $bytes = null)
  416. {
  417. $file = expand_real_root($file);
  418. $stat = stat($file);
  419. if ($stat['size'] > 5* 1000 * 1000) {
  420. dprint("File size too high. Taking only the last 200 lines\n");
  421. $lines = 200;
  422. }
  423. if ($lines === 'download') {
  424. throw new lxException('cannot_download_here', '');
  425. $lines = null;
  426. }
  427. if (!$lines) {
  428. $data = file_get_contents($file);
  429. } else {
  430. $data = lxfile_tail($file, $bytes);
  431. }
  432. return $data;
  433. }
  434. function lxfile_tail($file, $getsize)
  435. {
  436. $size = lfilesize($file);
  437. $fp = lfopen($file, "r");
  438. if (!$fp) {
  439. return null;
  440. }
  441. fseek($fp, $size - $getsize);
  442. $ret = null;
  443. while(!feof($fp)) {
  444. $ret .= fread($fp, 1024);
  445. }
  446. return $ret;
  447. }
  448. function lxfile_touch($file)
  449. {
  450. $file = expand_real_root($file);
  451. $ret = touch($file);
  452. return $ret;
  453. }