main.inc.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <?php
  2. // -------------------------------------------------------------------------------
  3. // | net2ftp: a web based FTP client |
  4. // | Copyright (c) 2003-2008 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. // Run the script to the end, even 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. if ($net2ftp_settings["error_reporting"] == "ALL") { error_reporting(E_ALL); }
  108. elseif ($net2ftp_settings["error_reporting"] == "NONE") { error_reporting(0); }
  109. else { error_reporting(E_ERROR | E_WARNING | E_PARSE); }
  110. // Timer: start
  111. $net2ftp_globals["starttime"] = microtime();
  112. $net2ftp_globals["endtime"] = microtime();
  113. }
  114. // Set the PHP temporary directory
  115. // putenv("TMPDIR=" . $net2ftp_globals["application_tempdir"]);
  116. // -------------------------------------------------------------------------
  117. // Function libraries:
  118. // 1. Libraries which are always needed
  119. // 2. Register global variables
  120. // 3. Function libraries which are needed depending on certain variables
  121. // // --> Do this only once, when $action == "sendHttpHeaders"
  122. // -------------------------------------------------------------------------
  123. if ($action == "sendHttpHeaders") {
  124. // 1. Libraries which are always needed
  125. require_once($net2ftp_globals["application_includesdir"] . "/authorizations.inc.php");
  126. require_once($net2ftp_globals["application_includesdir"] . "/consumption.inc.php");
  127. require_once($net2ftp_globals["application_includesdir"] . "/database.inc.php");
  128. require_once($net2ftp_globals["application_includesdir"] . "/errorhandling.inc.php");
  129. require_once($net2ftp_globals["application_includesdir"] . "/filesystem.inc.php");
  130. require_once($net2ftp_globals["application_includesdir"] . "/html.inc.php");
  131. require_once($net2ftp_globals["application_includesdir"] . "/StonePhpSafeCrypt.php");
  132. require_once($net2ftp_globals["application_languagesdir"] . "/languages.inc.php");
  133. require_once($net2ftp_globals["application_skinsdir"] . "/skins.inc.php");
  134. // 1. Define functions which are used, but which did not exist before PHP version 4.3.0
  135. if (version_compare(phpversion(), "4.3.0", "<")) {
  136. require_once($net2ftp_globals["application_includesdir"] . "/before430.inc.php");
  137. }
  138. // 2. Register global variables (POST, GET, GLOBAL, ...)
  139. require_once($net2ftp_globals["application_includesdir"] . "/registerglobals.inc.php");
  140. // 3. Function libraries which are needed depending on certain variables
  141. if ($net2ftp_globals["state"] == "upload" || $net2ftp_globals["state"] == "unzip") {
  142. require_once($net2ftp_globals["application_includesdir"] . "/pclerror.lib.php");
  143. require_once($net2ftp_globals["application_includesdir"] . "/pcltar.lib.php");
  144. require_once($net2ftp_globals["application_includesdir"] . "/pcltrace.lib.php");
  145. require_once($net2ftp_globals["application_includesdir"] . "/pclzip.lib.php");
  146. }
  147. if ($net2ftp_globals["state"] == "advanced_ftpserver" || $net2ftp_globals["state"] == "advanced_parsing" ||
  148. $net2ftp_globals["state"] == "advanced_webserver" || $net2ftp_globals["state"] == "browse" ||
  149. $net2ftp_globals["state"] == "copymovedelete" || $net2ftp_globals["state"] == "chmod" ||
  150. $net2ftp_globals["state"] == "calculatesize" || $net2ftp_globals["state"] == "downloadzip" ||
  151. $net2ftp_globals["state"] == "findstring" || $net2ftp_globals["state"] == "followsymlink" ||
  152. $net2ftp_globals["state"] == "install" || $net2ftp_globals["state"] == "zip") {
  153. require_once($net2ftp_globals["application_includesdir"] . "/browse.inc.php");
  154. }
  155. if ($net2ftp_globals["state"] == "downloadzip" || $net2ftp_globals["state"] == "zip") {
  156. require_once($net2ftp_globals["application_includesdir"] . "/zip.lib.php");
  157. }
  158. // 4. Load the plugins
  159. require_once($net2ftp_globals["application_pluginsdir"] . "/plugins.inc.php");
  160. $net2ftp_globals["activePlugins"] = getActivePlugins();
  161. net2ftp_plugin_includePhpFiles();
  162. // 5. Load the language file
  163. includeLanguageFile();
  164. }
  165. // -------------------------------------------------------------------------
  166. // Execute function shutdown() if the script reaches the maximum execution time (usually 30 seconds)
  167. // -------------------------------------------------------------------------
  168. if ($action == "sendHttpHeaders") {
  169. register_shutdown_function("net2ftp_shutdown");
  170. }
  171. // -------------------------------------------------------------------------
  172. // Log access
  173. // --> Do this only once, when $action == "sendHttpHeaders"
  174. // -------------------------------------------------------------------------
  175. if ($action == "sendHttpHeaders") {
  176. logAccess();
  177. if ($net2ftp_result["success"] == false) {
  178. logError();
  179. return false;
  180. }
  181. }
  182. // -------------------------------------------------------------------------
  183. // Check authorizations
  184. // --> Do this only once, when $action == "sendHttpHeaders"
  185. // -------------------------------------------------------------------------
  186. if ($action == "sendHttpHeaders" && $net2ftp_settings["check_authorization"] == "yes" && $net2ftp_globals["ftpserver"] != "") {
  187. checkAuthorization($net2ftp_globals["ftpserver"], $net2ftp_globals["ftpserverport"], $net2ftp_globals["directory"], $net2ftp_globals["username"]);
  188. if ($net2ftp_result["success"] == false) {
  189. logError();
  190. return false;
  191. }
  192. }
  193. // -------------------------------------------------------------------------
  194. // Get the consumption counter values from the database
  195. // This retrieves the consumption of network and server resources for the
  196. // current IP address and FTP server from the database, and stores these
  197. // values in global variables. See /includes/consumption.inc.php for the details.
  198. // --> Do this only once, when $action == "sendHttpHeaders"
  199. // -------------------------------------------------------------------------
  200. if ($action == "sendHttpHeaders") {
  201. getConsumption();
  202. if ($net2ftp_result["success"] == false) {
  203. logError();
  204. return false;
  205. }
  206. }
  207. // -------------------------------------------------------------------------
  208. // Execute the action!
  209. // -------------------------------------------------------------------------
  210. // ------------------------------------
  211. // For most modules, everything must be done: send headers, print body, etc
  212. // ------------------------------------
  213. if ($net2ftp_globals["state"] == "admin" ||
  214. $net2ftp_globals["state"] == "admin_createtables" ||
  215. $net2ftp_globals["state"] == "admin_emptylogs" ||
  216. $net2ftp_globals["state"] == "admin_viewlogs" ||
  217. $net2ftp_globals["state"] == "advanced" ||
  218. $net2ftp_globals["state"] == "advanced_ftpserver" ||
  219. $net2ftp_globals["state"] == "advanced_parsing" ||
  220. $net2ftp_globals["state"] == "advanced_webserver" ||
  221. $net2ftp_globals["state"] == "bookmark" ||
  222. $net2ftp_globals["state"] == "browse" ||
  223. $net2ftp_globals["state"] == "calculatesize" ||
  224. $net2ftp_globals["state"] == "chmod" ||
  225. $net2ftp_globals["state"] == "copymovedelete" ||
  226. $net2ftp_globals["state"] == "edit" ||
  227. $net2ftp_globals["state"] == "findstring" ||
  228. $net2ftp_globals["state"] == "install" ||
  229. ($net2ftp_globals["state"] == "jupload" && $net2ftp_globals["screen"] == 1) ||
  230. $net2ftp_globals["state"] == "login" ||
  231. $net2ftp_globals["state"] == "login_small" ||
  232. $net2ftp_globals["state"] == "logout" ||
  233. $net2ftp_globals["state"] == "newdir" ||
  234. $net2ftp_globals["state"] == "raw" ||
  235. $net2ftp_globals["state"] == "rename" ||
  236. $net2ftp_globals["state"] == "unzip" ||
  237. $net2ftp_globals["state"] == "upload" ||
  238. ($net2ftp_globals["state"] == "view" && $net2ftp_globals["state2"] == "") ||
  239. $net2ftp_globals["state"] == "zip") {
  240. require_once($net2ftp_globals["application_modulesdir"] . "/" . $net2ftp_globals["state"] . "/" . $net2ftp_globals["state"] . ".inc.php");
  241. if ($action == "sendHttpHeaders") {
  242. net2ftp_module_sendHttpHeaders();
  243. // If needed, exit to avoid sending non-header output (by net2ftp or other application)
  244. // Example: if a module sends a HTTP redirect header (See /includes/authorizations.inc.php function checkAdminUsernamePassword()!)
  245. if ($net2ftp_result["exit"] == true) { exit(); }
  246. }
  247. elseif ($action == "printJavascript") {
  248. net2ftp_module_printJavascript();
  249. net2ftp_plugin_printJavascript();
  250. }
  251. elseif ($action == "printCss") {
  252. net2ftp_module_printCss();
  253. net2ftp_plugin_printCss();
  254. }
  255. elseif ($action == "printBodyOnload") {
  256. net2ftp_module_printBodyOnload();
  257. net2ftp_plugin_printBodyOnload();
  258. }
  259. elseif ($action == "printBody") {
  260. // Print the status bar to be able to show the progress
  261. if (isStatusbarActive() == true) {
  262. require_once($net2ftp_globals["application_skinsdir"] . "/" . $net2ftp_globals["skin"] . "/statusbar.template.php");
  263. }
  264. require_once($net2ftp_globals["application_skinsdir"] . "/" . $net2ftp_globals["skin"] . "/status/status.inc.php");
  265. // Do the work and meanwhile update the progress bar
  266. net2ftp_module_printBody();
  267. // Update the consumption statistics
  268. $net2ftp_globals["endtime"] = microtime();
  269. $net2ftp_globals["time_taken"] = timer();
  270. addConsumption(0, $net2ftp_globals["time_taken"]);
  271. putConsumption();
  272. // Set the progress bar to "finished"
  273. if (isStatusbarActive() == true) {
  274. $statusmessage = __("Script finished in %1\$s seconds", $net2ftp_globals["time_taken"]);
  275. setStatus(1, 1, $statusmessage);
  276. }
  277. }
  278. }
  279. // ------------------------------------
  280. // For some modules, only headers must be sent
  281. // ------------------------------------
  282. elseif ($net2ftp_globals["state"] == "clearcookies" ||
  283. $net2ftp_globals["state"] == "downloadfile" ||
  284. $net2ftp_globals["state"] == "downloadzip" ||
  285. $net2ftp_globals["state"] == "followsymlink" ||
  286. ($net2ftp_globals["state"] == "jupload" && $net2ftp_globals["screen"] == 2) ||
  287. ($net2ftp_globals["state"] == "view" && $net2ftp_globals["state2"] != "")) {
  288. require_once($net2ftp_globals["application_modulesdir"] . "/" . $net2ftp_globals["state"] . "/" . $net2ftp_globals["state"] . ".inc.php");
  289. if ($action == "sendHttpHeaders") {
  290. // Do the work - do not update the progress bar
  291. net2ftp_module_sendHttpHeaders();
  292. // Update the consumption statistics
  293. $net2ftp_globals["endtime"] = microtime();
  294. $net2ftp_globals["time_taken"] = timer();
  295. addConsumption(0, $net2ftp_globals["time_taken"]);
  296. putConsumption();
  297. // Exit to avoid sending non-header output (by net2ftp or other application)
  298. exit();
  299. }
  300. elseif ($action == "printJavascript") { }
  301. elseif ($action == "printCss") { }
  302. elseif ($action == "printBodyOnload") { }
  303. elseif ($action == "printBody") { }
  304. }
  305. elseif ($net2ftp_globals["state"] == "error") {
  306. logError();
  307. return false;
  308. }
  309. else {
  310. $errormessage = __("Unexpected state string: %1\$s. Exiting.", $net2ftp_globals["state"]);
  311. setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
  312. logError();
  313. return false;
  314. }
  315. } // end function net2ftp_main
  316. // ** **
  317. // ** **
  318. // **************************************************************************************
  319. // **************************************************************************************
  320. // **************************************************************************************
  321. // **************************************************************************************
  322. // ** **
  323. // ** **
  324. function isStatusbarActive() {
  325. // --------------
  326. // This function returns if the status bar should be shown or not, depending
  327. // on the state and state2 variables
  328. // --------------
  329. global $net2ftp_globals;
  330. // If $net2ftp_globals["isStatusbarActive"] is not yet filled, calculate its value
  331. // and fill it in
  332. if (isset($net2ftp_globals["isStatusbarActive"]) == false) {
  333. if ($net2ftp_globals["skin"] == "openlaszlo") { $net2ftp_globals["isStatusbarActive"] = false; }
  334. elseif (
  335. $net2ftp_globals["state"] == "admin" ||
  336. $net2ftp_globals["state"] == "admin_createtables" ||
  337. $net2ftp_globals["state"] == "admin_emptylogs" ||
  338. $net2ftp_globals["state"] == "admin_viewlogs" ||
  339. $net2ftp_globals["state"] == "advanced" ||
  340. $net2ftp_globals["state"] == "advanced_ftpserver" ||
  341. $net2ftp_globals["state"] == "advanced_parsing" ||
  342. $net2ftp_globals["state"] == "advanced_webserver" ||
  343. $net2ftp_globals["state"] == "bookmark" ||
  344. ($net2ftp_globals["state"] == "browse" && $net2ftp_globals["state2"] == "main") ||
  345. $net2ftp_globals["state"] == "calculatesize" ||
  346. $net2ftp_globals["state"] == "chmod" ||
  347. $net2ftp_globals["state"] == "copymovedelete" ||
  348. $net2ftp_globals["state"] == "easywebsite" ||
  349. $net2ftp_globals["state"] == "findstring" ||
  350. $net2ftp_globals["state"] == "install" ||
  351. $net2ftp_globals["state"] == "jupload" ||
  352. $net2ftp_globals["state"] == "newdir" ||
  353. $net2ftp_globals["state"] == "newfile" ||
  354. $net2ftp_globals["state"] == "raw" ||
  355. $net2ftp_globals["state"] == "rename" ||
  356. $net2ftp_globals["state"] == "unzip" ||
  357. $net2ftp_globals["state"] == "updatefile" ||
  358. $net2ftp_globals["state"] == "upload" ||
  359. $net2ftp_globals["state"] == "view" ||
  360. $net2ftp_globals["state"] == "zip") {
  361. $net2ftp_globals["isStatusbarActive"] = true;
  362. }
  363. else {
  364. $net2ftp_globals["isStatusbarActive"] = false;
  365. }
  366. }
  367. // Return the value of $net2ftp_globals["isStatusbarActive"]
  368. return $net2ftp_globals["isStatusbarActive"];
  369. } // end function isStatusbarActive
  370. // ** **
  371. // ** **
  372. // **************************************************************************************
  373. // **************************************************************************************
  374. // **************************************************************************************
  375. // **************************************************************************************
  376. // ** **
  377. // ** **
  378. function stopwatch() {
  379. // --------------
  380. // This function prints the total time elapsed, and the time elapsed since the previous call
  381. // --------------
  382. global $net2ftp_globals;
  383. // Now
  384. list($now_usec, $now_sec) = explode(' ', microtime());
  385. $now = ((float)$now_usec + (float)$now_sec);
  386. // Initialization
  387. if (isset($net2ftp_globals["stopwatch_starttime"]) == false) {
  388. $net2ftp_globals["stopwatch_starttime"] = $now;
  389. }
  390. if (isset($net2ftp_globals["stopwatch_endtime"]) == false) {
  391. $net2ftp_globals["stopwatch_endtime"] = $now;
  392. }
  393. // Total time elapsed = now - starttime
  394. $total_elapsed = $now - $net2ftp_globals["stopwatch_starttime"];
  395. $total_elapsed = number_format($total_elapsed, 4);
  396. // Time since previous stopwatch = now - previous endtime
  397. $delta_elapsed = $now - $net2ftp_globals["stopwatch_endtime"];
  398. $delta_elapsed = number_format($delta_elapsed, 4);
  399. // Set the new value for endtime
  400. $net2ftp_globals["stopwatch_endtime"] = $now;
  401. // Print $total_elapsed and $delta_elapsed
  402. echo $total_elapsed . " - " . $delta_elapsed . "<br />\n";
  403. } // End function stopwatch()
  404. // ** **
  405. // ** **
  406. // **************************************************************************************
  407. // **************************************************************************************
  408. ?>