1
0

main.inc.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <?php
  2. // -------------------------------------------------------------------------------
  3. // | net2ftp: a web based FTP client |
  4. // | Copyright (c) 2003-2013 by David Gartner |
  5. // | |
  6. // | This program is free software; you can redistribute it and/or |
  7. // | modify it under the terms of the GNU General Public License |
  8. // | as published by the Free Software Foundation; either version 2 |
  9. // | of the License, or (at your option) any later version. |
  10. // | |
  11. // | This program is distributed in the hope that it will be useful, |
  12. // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  13. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  14. // | GNU General Public License for more details. |
  15. // | |
  16. // | You should have received a copy of the GNU General Public License |
  17. // | along with this program; if not, write to the Free Software |
  18. // | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
  19. // | |
  20. // -------------------------------------------------------------------------------
  21. // **************************************************************************************
  22. // **************************************************************************************
  23. // ** **
  24. // ** **
  25. function net2ftp($action) {
  26. // --------------
  27. // This function is the main net2ftp function; it is the interface between 3rd party
  28. // scripts (CMS, control panels, etc), and the internal net2ftp modules and plugins.
  29. //
  30. // This function is called 5 times per pageload: to send the HTTP headers, to print
  31. // the javascript code, to print the CSS code, to print the body onload actions and
  32. // finally to print the body content.
  33. // --------------
  34. // -------------------------------------------------------------------------
  35. // Check that "sendHttpHeaders" action is only executed once
  36. // Check that no other actions can be executed if "sendHttpHeaders" has not yet been executed
  37. // -------------------------------------------------------------------------
  38. if ($action == "sendHttpHeaders") {
  39. if (defined("NET2FTP_SENDHTTPHEADERS") == true) { echo "Error: please call the net2ftp(\$action) function only once with \$action = \"sendHttpHeaders\"!"; return false; }
  40. else { define("NET2FTP_SENDHTTPHEADERS", 1); }
  41. }
  42. else {
  43. if (defined("NET2FTP_SENDHTTPHEADERS") == false) { echo "Error: please call the net2ftp(\$action) function first with \$action = \"sendHttpHeaders\"!"; return false; }
  44. }
  45. // -------------------------------------------------------------------------
  46. // Global variables
  47. // -------------------------------------------------------------------------
  48. global $net2ftp_settings, $net2ftp_globals, $net2ftp_result, $net2ftp_messages;
  49. // Set the NET2FTP constant which is used to check if template files are called by net2ftp
  50. if (defined("NET2FTP") == false) { define("NET2FTP", 1); }
  51. // Initialize the global variables
  52. if ($action == "sendHttpHeaders") {
  53. $net2ftp_globals = array();
  54. $net2ftp_messages = array();
  55. $net2ftp_output = array();
  56. $net2ftp_result["success"] = true;
  57. $net2ftp_result["errormessage"] = "";
  58. $net2ftp_result["debug_backtrace"] = "";
  59. $net2ftp_result["exit"] = false;
  60. $net2ftp_settings = array();
  61. }
  62. // -------------------------------------------------------------------------
  63. // If an error occured during a previous execution of net2ftp(), return false
  64. // and let index.php print the error message
  65. // -------------------------------------------------------------------------
  66. if ($net2ftp_result["success"] == false) { return false; }
  67. // -------------------------------------------------------------------------
  68. // Input checks
  69. // -------------------------------------------------------------------------
  70. if ($action != "sendHttpHeaders" && $action != "printJavascript" && $action != "printCss" && $action != "printBodyOnload" && $action != "printBody") {
  71. $net2ftp_result["success"] = false;
  72. $net2ftp_result["errormessage"] = "The \$action variable has an unknown value: $action.";
  73. $net2ftp_result["debug_backtrace"] = debug_backtrace();
  74. logError();
  75. return false;
  76. }
  77. // -------------------------------------------------------------------------
  78. // Read settings files
  79. // -------------------------------------------------------------------------
  80. if ($action == "sendHttpHeaders") {
  81. require(NET2FTP_APPLICATION_ROOTDIR . "/settings.inc.php");
  82. require(NET2FTP_APPLICATION_ROOTDIR . "/settings_authorizations.inc.php");
  83. require(NET2FTP_APPLICATION_ROOTDIR . "/settings_screens.inc.php");
  84. }
  85. // -------------------------------------------------------------------------
  86. // Main directories
  87. // -------------------------------------------------------------------------
  88. $net2ftp_globals["application_rootdir"] = NET2FTP_APPLICATION_ROOTDIR;
  89. if (NET2FTP_APPLICATION_ROOTDIR_URL == "/") { $net2ftp_globals["application_rootdir_url"] = ""; }
  90. else { $net2ftp_globals["application_rootdir_url"] = NET2FTP_APPLICATION_ROOTDIR_URL; }
  91. $net2ftp_globals["application_includesdir"] = $net2ftp_globals["application_rootdir"] . "/includes";
  92. $net2ftp_globals["application_languagesdir"] = $net2ftp_globals["application_rootdir"] . "/languages";
  93. $net2ftp_globals["application_modulesdir"] = $net2ftp_globals["application_rootdir"] . "/modules";
  94. $net2ftp_globals["application_pluginsdir"] = $net2ftp_globals["application_rootdir"] . "/plugins";
  95. $net2ftp_globals["application_skinsdir"] = $net2ftp_globals["application_rootdir"] . "/skins";
  96. $net2ftp_globals["application_tempdir"] = $net2ftp_globals["application_rootdir"] . "/temp";
  97. // -------------------------------------------------------------------------
  98. // Set basic settings
  99. // -------------------------------------------------------------------------
  100. if ($action == "sendHttpHeaders") {
  101. // Do not run the script to the end if the user hits the stop button
  102. // ignore_user_abort();
  103. // Execute function shutdown() if the script reaches the maximum execution time (usually 30 seconds)
  104. // DON'T REGISTER IT HERE YET, as this causes errors on newer versions of PHP; first include the function libraries
  105. // register_shutdown_function("net2ftp_shutdown");
  106. // Set the error reporting level
  107. // Note: the error reporting level is already set to none in index.xml.php, to avoid XML parsing errors
  108. if (substr($_SERVER["PHP_SELF"], strlen($_SERVER["PHP_SELF"])-8, 8) == ".xml.php") { error_reporting(0); }
  109. elseif ($net2ftp_settings["error_reporting"] == "ALL") { error_reporting(E_ALL); }
  110. elseif ($net2ftp_settings["error_reporting"] == "NONE") { error_reporting(0); }
  111. else { error_reporting(E_ERROR | E_WARNING | E_PARSE); }
  112. // Timer: start
  113. $net2ftp_globals["starttime"] = microtime();
  114. $net2ftp_globals["endtime"] = microtime();
  115. }
  116. // Set default timezone
  117. date_default_timezone_set("Europe/Paris");
  118. // Set the PHP temporary directory
  119. // putenv("TMPDIR=" . $net2ftp_globals["application_tempdir"]);
  120. // -------------------------------------------------------------------------
  121. // Function libraries:
  122. // 1. Libraries which are always needed
  123. // 2. Register global variables
  124. // 3. Function libraries which are needed depending on certain variables
  125. // // --> Do this only once, when $action == "sendHttpHeaders"
  126. // -------------------------------------------------------------------------
  127. if ($action == "sendHttpHeaders") {
  128. // 1. Libraries which are always needed
  129. require_once($net2ftp_globals["application_includesdir"] . "/authorizations.inc.php");
  130. require_once($net2ftp_globals["application_includesdir"] . "/consumption.inc.php");
  131. require_once($net2ftp_globals["application_includesdir"] . "/database.inc.php");
  132. require_once($net2ftp_globals["application_includesdir"] . "/errorhandling.inc.php");
  133. require_once($net2ftp_globals["application_includesdir"] . "/filesystem.inc.php");
  134. require_once($net2ftp_globals["application_includesdir"] . "/html.inc.php");
  135. require_once($net2ftp_globals["application_includesdir"] . "/StonePhpSafeCrypt.php");
  136. require_once($net2ftp_globals["application_includesdir"] . "/logging.inc.php");
  137. require_once($net2ftp_globals["application_languagesdir"] . "/languages.inc.php");
  138. require_once($net2ftp_globals["application_skinsdir"] . "/skins.inc.php");
  139. // 1. Define functions which are used, but which did not exist before PHP version 4.3.0
  140. if (version_compare(phpversion(), "4.3.0", "<")) {
  141. require_once($net2ftp_globals["application_includesdir"] . "/before430.inc.php");
  142. }
  143. // 2. Register global variables (POST, GET, GLOBAL, ...)
  144. require_once($net2ftp_globals["application_includesdir"] . "/registerglobals.inc.php");
  145. // 3. Function libraries which are needed depending on certain variables
  146. if ($net2ftp_globals["state"] == "upload" || $net2ftp_globals["state"] == "unzip") {
  147. require_once($net2ftp_globals["application_includesdir"] . "/pclerror.lib.php");
  148. require_once($net2ftp_globals["application_includesdir"] . "/pcltar.lib.php");
  149. require_once($net2ftp_globals["application_includesdir"] . "/pcltrace.lib.php");
  150. require_once($net2ftp_globals["application_includesdir"] . "/pclzip.lib.php");
  151. }
  152. if ($net2ftp_globals["state"] == "advanced_ftpserver" || $net2ftp_globals["state"] == "advanced_parsing" ||
  153. $net2ftp_globals["state"] == "advanced_webserver" || $net2ftp_globals["state"] == "browse" ||
  154. $net2ftp_globals["state"] == "copymovedelete" || $net2ftp_globals["state"] == "chmod" ||
  155. $net2ftp_globals["state"] == "calculatesize" || $net2ftp_globals["state"] == "downloadzip" ||
  156. $net2ftp_globals["state"] == "findstring" || $net2ftp_globals["state"] == "followsymlink" ||
  157. $net2ftp_globals["state"] == "install" || $net2ftp_globals["state"] == "zip") {
  158. require_once($net2ftp_globals["application_includesdir"] . "/browse.inc.php");
  159. }
  160. if ($net2ftp_globals["state"] == "downloadzip" || $net2ftp_globals["state"] == "zip") {
  161. require_once($net2ftp_globals["application_includesdir"] . "/zip.lib.php");
  162. }
  163. // 4. Load the plugins
  164. require_once($net2ftp_globals["application_pluginsdir"] . "/plugins.inc.php");
  165. $net2ftp_globals["activePlugins"] = getActivePlugins();
  166. net2ftp_plugin_includePhpFiles();
  167. // 5. Load the language file
  168. includeLanguageFile();
  169. }
  170. // -------------------------------------------------------------------------
  171. // Execute function shutdown() if the script reaches the maximum execution time (usually 30 seconds)
  172. // -------------------------------------------------------------------------
  173. if ($action == "sendHttpHeaders") {
  174. register_shutdown_function("net2ftp_shutdown");
  175. }
  176. // -------------------------------------------------------------------------
  177. // Log access and rotate logs
  178. // --> Do this only once, when $action == "sendHttpHeaders"
  179. // -------------------------------------------------------------------------
  180. if ($action == "sendHttpHeaders") {
  181. logAccess();
  182. if ($net2ftp_result["success"] == false) {
  183. logError();
  184. return false;
  185. }
  186. rotateLogs();
  187. if ($net2ftp_result["success"] == false) {
  188. logError();
  189. return false;
  190. }
  191. }
  192. // -------------------------------------------------------------------------
  193. // Check authorizations
  194. // --> Do this only once, when $action == "sendHttpHeaders"
  195. // -------------------------------------------------------------------------
  196. if ($action == "sendHttpHeaders" && $net2ftp_settings["check_authorization"] == "yes" && $net2ftp_globals["ftpserver"] != "") {
  197. checkAuthorization($net2ftp_globals["ftpserver"], $net2ftp_globals["ftpserverport"], $net2ftp_globals["directory"], $net2ftp_globals["username"]);
  198. if ($net2ftp_result["success"] == false) {
  199. logError();
  200. return false;
  201. }
  202. }
  203. // -------------------------------------------------------------------------
  204. // Get the consumption counter values from the database
  205. // This retrieves the consumption of network and server resources for the
  206. // current IP address and FTP server from the database, and stores these
  207. // values in global variables. See /includes/consumption.inc.php for the details.
  208. // --> Do this only once, when $action == "sendHttpHeaders"
  209. // -------------------------------------------------------------------------
  210. if ($action == "sendHttpHeaders") {
  211. getConsumption();
  212. if ($net2ftp_result["success"] == false) {
  213. logError();
  214. return false;
  215. }
  216. }
  217. // -------------------------------------------------------------------------
  218. // Execute the action!
  219. // -------------------------------------------------------------------------
  220. // ------------------------------------
  221. // For most modules, everything must be done: send headers, print body, etc
  222. // ------------------------------------
  223. if ($net2ftp_globals["state"] == "admin" ||
  224. $net2ftp_globals["state"] == "admin_createtables" ||
  225. $net2ftp_globals["state"] == "admin_emptylogs" ||
  226. $net2ftp_globals["state"] == "admin_viewlogs" ||
  227. $net2ftp_globals["state"] == "advanced" ||
  228. $net2ftp_globals["state"] == "advanced_ftpserver" ||
  229. $net2ftp_globals["state"] == "advanced_parsing" ||
  230. $net2ftp_globals["state"] == "advanced_webserver" ||
  231. $net2ftp_globals["state"] == "bookmark" ||
  232. $net2ftp_globals["state"] == "browse" ||
  233. $net2ftp_globals["state"] == "calculatesize" ||
  234. $net2ftp_globals["state"] == "chmod" ||
  235. $net2ftp_globals["state"] == "copymovedelete" ||
  236. $net2ftp_globals["state"] == "edit" ||
  237. $net2ftp_globals["state"] == "findstring" ||
  238. $net2ftp_globals["state"] == "getcookies" ||
  239. $net2ftp_globals["state"] == "install" ||
  240. ($net2ftp_globals["state"] == "jupload" && $net2ftp_globals["screen"] == 1) ||
  241. $net2ftp_globals["state"] == "login" ||
  242. $net2ftp_globals["state"] == "login_small" ||
  243. $net2ftp_globals["state"] == "logout" ||
  244. $net2ftp_globals["state"] == "newdir" ||
  245. $net2ftp_globals["state"] == "raw" ||
  246. $net2ftp_globals["state"] == "rename" ||
  247. $net2ftp_globals["state"] == "unzip" ||
  248. $net2ftp_globals["state"] == "upload" ||
  249. ($net2ftp_globals["state"] == "view" && $net2ftp_globals["state2"] == "") ||
  250. $net2ftp_globals["state"] == "zip") {
  251. require_once($net2ftp_globals["application_modulesdir"] . "/" . $net2ftp_globals["state"] . "/" . $net2ftp_globals["state"] . ".inc.php");
  252. if ($action == "sendHttpHeaders") {
  253. net2ftp_module_sendHttpHeaders();
  254. // If needed, exit to avoid sending non-header output (by net2ftp or other application)
  255. // Example: if a module sends a HTTP redirect header (See /includes/authorizations.inc.php function checkAdminUsernamePassword()!)
  256. if ($net2ftp_result["exit"] == true) { exit(); }
  257. }
  258. elseif ($action == "printJavascript") {
  259. net2ftp_module_printJavascript();
  260. net2ftp_plugin_printJavascript();
  261. net2ftp_skin_printJavascript();
  262. }
  263. elseif ($action == "printCss") {
  264. net2ftp_module_printCss();
  265. net2ftp_plugin_printCss();
  266. net2ftp_skin_printCss();
  267. }
  268. elseif ($action == "printBodyOnload") {
  269. net2ftp_module_printBodyOnload();
  270. net2ftp_plugin_printBodyOnload();
  271. }
  272. elseif ($action == "printBody") {
  273. // Print the status bar to be able to show the progress
  274. if (isStatusbarActive() == true) {
  275. require_once($net2ftp_globals["application_skinsdir"] . "/" . $net2ftp_globals["skin"] . "/header.template.php");
  276. }
  277. require_once($net2ftp_globals["application_skinsdir"] . "/" . $net2ftp_globals["skin"] . "/status/status.inc.php");
  278. // Do the work and meanwhile update the progress bar
  279. net2ftp_module_printBody();
  280. // Update the consumption statistics
  281. $net2ftp_globals["endtime"] = microtime();
  282. $net2ftp_globals["time_taken"] = timer();
  283. addConsumption(0, $net2ftp_globals["time_taken"]);
  284. putConsumption();
  285. // Set the progress bar to "finished"
  286. if (isStatusbarActive() == true) {
  287. $statusmessage = __("Script finished in %1\$s seconds", $net2ftp_globals["time_taken"]);
  288. setStatus(1, 1, $statusmessage);
  289. }
  290. }
  291. }
  292. // ------------------------------------
  293. // For some modules, only headers must be sent
  294. // ------------------------------------
  295. elseif ($net2ftp_globals["state"] == "clearcookies" ||
  296. $net2ftp_globals["state"] == "downloadfile" ||
  297. $net2ftp_globals["state"] == "downloadzip" ||
  298. $net2ftp_globals["state"] == "followsymlink" ||
  299. ($net2ftp_globals["state"] == "jupload" && $net2ftp_globals["screen"] == 2) ||
  300. ($net2ftp_globals["state"] == "view" && $net2ftp_globals["state2"] != "")) {
  301. require_once($net2ftp_globals["application_modulesdir"] . "/" . $net2ftp_globals["state"] . "/" . $net2ftp_globals["state"] . ".inc.php");
  302. if ($action == "sendHttpHeaders") {
  303. // Do the work - do not update the progress bar
  304. net2ftp_module_sendHttpHeaders();
  305. // Update the consumption statistics
  306. $net2ftp_globals["endtime"] = microtime();
  307. $net2ftp_globals["time_taken"] = timer();
  308. addConsumption(0, $net2ftp_globals["time_taken"]);
  309. putConsumption();
  310. // Exit to avoid sending non-header output (by net2ftp or other application)
  311. exit();
  312. }
  313. elseif ($action == "printJavascript") { }
  314. elseif ($action == "printCss") { }
  315. elseif ($action == "printBodyOnload") { }
  316. elseif ($action == "printBody") { }
  317. }
  318. else {
  319. $errormessage = __("Unexpected state string: %1\$s. Exiting.", $net2ftp_globals["state"]);
  320. setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
  321. logError();
  322. return false;
  323. }
  324. // -------------------------------------------------------------------------
  325. // Log errors
  326. // -------------------------------------------------------------------------
  327. if ($net2ftp_result["success"] == false) {
  328. logError();
  329. return false;
  330. }
  331. } // end function net2ftp_main
  332. // ** **
  333. // ** **
  334. // **************************************************************************************
  335. // **************************************************************************************
  336. // **************************************************************************************
  337. // **************************************************************************************
  338. // ** **
  339. // ** **
  340. function isStatusbarActive() {
  341. // --------------
  342. // This function returns if the status bar should be shown or not, depending
  343. // on the state and state2 variables
  344. // --------------
  345. global $net2ftp_globals;
  346. // If $net2ftp_globals["isStatusbarActive"] is not yet filled, calculate its value
  347. // and fill it in
  348. if (isset($net2ftp_globals["isStatusbarActive"]) == false) {
  349. if ($net2ftp_globals["skin"] == "openlaszlo") { $net2ftp_globals["isStatusbarActive"] = false; }
  350. elseif (
  351. $net2ftp_globals["state"] == "admin" ||
  352. $net2ftp_globals["state"] == "admin_createtables" ||
  353. $net2ftp_globals["state"] == "admin_emptylogs" ||
  354. $net2ftp_globals["state"] == "admin_viewlogs" ||
  355. $net2ftp_globals["state"] == "advanced" ||
  356. $net2ftp_globals["state"] == "advanced_ftpserver" ||
  357. $net2ftp_globals["state"] == "advanced_parsing" ||
  358. $net2ftp_globals["state"] == "advanced_webserver" ||
  359. $net2ftp_globals["state"] == "bookmark" ||
  360. ($net2ftp_globals["state"] == "browse" && $net2ftp_globals["state2"] == "main") ||
  361. $net2ftp_globals["state"] == "calculatesize" ||
  362. $net2ftp_globals["state"] == "chmod" ||
  363. $net2ftp_globals["state"] == "copymovedelete" ||
  364. $net2ftp_globals["state"] == "easywebsite" ||
  365. $net2ftp_globals["state"] == "findstring" ||
  366. $net2ftp_globals["state"] == "install" ||
  367. $net2ftp_globals["state"] == "jupload" ||
  368. $net2ftp_globals["state"] == "newdir" ||
  369. $net2ftp_globals["state"] == "newfile" ||
  370. $net2ftp_globals["state"] == "raw" ||
  371. $net2ftp_globals["state"] == "rename" ||
  372. $net2ftp_globals["state"] == "unzip" ||
  373. $net2ftp_globals["state"] == "updatefile" ||
  374. $net2ftp_globals["state"] == "upload" ||
  375. $net2ftp_globals["state"] == "view" ||
  376. $net2ftp_globals["state"] == "zip") {
  377. $net2ftp_globals["isStatusbarActive"] = true;
  378. }
  379. else {
  380. $net2ftp_globals["isStatusbarActive"] = false;
  381. }
  382. }
  383. // Return the value of $net2ftp_globals["isStatusbarActive"]
  384. return $net2ftp_globals["isStatusbarActive"];
  385. } // end function isStatusbarActive
  386. // ** **
  387. // ** **
  388. // **************************************************************************************
  389. // **************************************************************************************
  390. // **************************************************************************************
  391. // **************************************************************************************
  392. // ** **
  393. // ** **
  394. function stopwatch() {
  395. // --------------
  396. // This function prints the total time elapsed, and the time elapsed since the previous call
  397. // --------------
  398. global $net2ftp_globals;
  399. // Now
  400. list($now_usec, $now_sec) = explode(' ', microtime());
  401. $now = ((float)$now_usec + (float)$now_sec);
  402. // Initialization
  403. if (isset($net2ftp_globals["stopwatch_starttime"]) == false) {
  404. $net2ftp_globals["stopwatch_starttime"] = $now;
  405. }
  406. if (isset($net2ftp_globals["stopwatch_endtime"]) == false) {
  407. $net2ftp_globals["stopwatch_endtime"] = $now;
  408. }
  409. // Total time elapsed = now - starttime
  410. $total_elapsed = $now - $net2ftp_globals["stopwatch_starttime"];
  411. $total_elapsed = number_format($total_elapsed, 4);
  412. // Time since previous stopwatch = now - previous endtime
  413. $delta_elapsed = $now - $net2ftp_globals["stopwatch_endtime"];
  414. $delta_elapsed = number_format($delta_elapsed, 4);
  415. // Set the new value for endtime
  416. $net2ftp_globals["stopwatch_endtime"] = $now;
  417. // Print $total_elapsed and $delta_elapsed
  418. echo $total_elapsed . " - " . $delta_elapsed . "<br />\n";
  419. } // End function stopwatch()
  420. // ** **
  421. // ** **
  422. // **************************************************************************************
  423. // **************************************************************************************
  424. ?>