logging.inc.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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. // -------------------------------------------------------------------------------
  12. // **************************************************************************************
  13. // **************************************************************************************
  14. // ** **
  15. // ** **
  16. function logAccess() {
  17. // --------------
  18. // This function logs user accesses to the site
  19. // --------------
  20. // -------------------------------------------------------------------------
  21. // Global variables
  22. // -------------------------------------------------------------------------
  23. global $net2ftp_globals, $net2ftp_settings, $net2ftp_result;
  24. if ($net2ftp_settings["log_access"] == "yes") {
  25. // -------------------------------------------------------------------------
  26. // Date and time
  27. // -------------------------------------------------------------------------
  28. $date = date("Y-m-d");
  29. $time = date("H:i:s");
  30. // -------------------------------------------------------------------------
  31. // Logging to the database
  32. // -------------------------------------------------------------------------
  33. if ($net2ftp_settings["use_database"] == "yes") {
  34. // ----------------------------------------------
  35. // Do not log accesses, errors and consumption while the logs are being rotated
  36. // ----------------------------------------------
  37. $logStatus = getLogStatus();
  38. if ($net2ftp_result["success"] == false) { return false; }
  39. if ($logStatus != 0) { return true; }
  40. // ----------------------------------------------
  41. // Input checks
  42. // ----------------------------------------------
  43. // Add slashes to variables which are used in a SQL query, and which are
  44. // potentially unsafe (supplied by the user).
  45. // $date is calculated in this function
  46. // $time is calculated in this function
  47. $REMOTE_ADDR_safe = addslashes($net2ftp_globals["REMOTE_ADDR"]);
  48. $REMOTE_PORT_safe = addslashes($net2ftp_globals["REMOTE_PORT"]);
  49. $HTTP_USER_AGENT_safe = addslashes($net2ftp_globals["HTTP_USER_AGENT"]);
  50. $PHP_SELF_safe = addslashes($net2ftp_globals["PHP_SELF"]);
  51. if (isset($net2ftp_globals["consumption_datatransfer"]) == true) {
  52. $consumption_datatransfer_safe = addslashes($net2ftp_globals["consumption_datatransfer"]);
  53. }
  54. else {
  55. $consumption_datatransfer_safe = "0";
  56. }
  57. if (isset($net2ftp_globals["consumption_executiontime"]) == true) {
  58. $consumption_executiontime_safe = addslashes($net2ftp_globals["consumption_executiontime"]);
  59. }
  60. else {
  61. $consumption_executiontime_safe = "0";
  62. }
  63. $net2ftp_ftpserver_safe = addslashes($net2ftp_globals["ftpserver"]);
  64. $net2ftp_username_safe = addslashes($net2ftp_globals["username"]);
  65. $state_safe = addslashes($net2ftp_globals["state"]);
  66. $state2_safe = addslashes($net2ftp_globals["state2"]);
  67. $screen_safe = addslashes($net2ftp_globals["screen"]);
  68. $directory_safe = addslashes($net2ftp_globals["directory"]);
  69. $entry_safe = addslashes($net2ftp_globals["entry"]);
  70. $HTTP_REFERER_safe = addslashes($net2ftp_globals["HTTP_REFERER"]);
  71. // ----------------------------------------------
  72. // Connect to the DB
  73. // ----------------------------------------------
  74. $mydb = connect2db();
  75. if ($net2ftp_result["success"] == false) { return false; }
  76. // ----------------------------------------------
  77. // Add record to the database table
  78. // ----------------------------------------------
  79. $sqlquery1 = "INSERT INTO net2ftp_log_access VALUES(null, '$date', '$time', '$REMOTE_ADDR_safe', '$REMOTE_PORT_safe', '$HTTP_USER_AGENT_safe', '$PHP_SELF_safe', '$consumption_datatransfer_safe', '$consumption_executiontime_safe', '$net2ftp_ftpserver_safe', '$net2ftp_username_safe', '$state_safe', '$state2_safe', '$screen_safe', '$directory_safe', '$entry_safe', '$HTTP_REFERER_safe')";
  80. $result1 = mysql_query($sqlquery1);
  81. if ($result1 == false) {
  82. $errormessage = __("Unable to execute the SQL query.");
  83. setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
  84. return false;
  85. }
  86. $nrofrows1 = mysql_affected_rows($mydb);
  87. $net2ftp_globals["log_access_id"] = mysql_insert_id();
  88. } // end if use_database
  89. // -------------------------------------------------------------------------
  90. // Logging to the system log
  91. // -------------------------------------------------------------------------
  92. if ($net2ftp_settings["use_syslog"] == "yes") {
  93. // ----------------------------------------------
  94. // Get consumption values
  95. // ----------------------------------------------
  96. if (isset($net2ftp_globals["consumption_datatransfer"]) == true) {
  97. $consumption_datatransfer = $net2ftp_globals["consumption_datatransfer"];
  98. }
  99. else {
  100. $consumption_datatransfer = "0";
  101. }
  102. if (isset($net2ftp_globals["consumption_executiontime"]) == true) {
  103. $consumption_executiontime = $net2ftp_globals["consumption_executiontime"];
  104. }
  105. else {
  106. $consumption_executiontime = "0";
  107. }
  108. // ----------------------------------------------
  109. // Create message
  110. // ----------------------------------------------
  111. $message2log = "$date $time " . $net2ftp_globals["REMOTE_ADDR"] . " " . $net2ftp_globals["REMOTE_PORT"] . " " . $net2ftp_globals["PHP_SELF"] . " " . $consumption_datatransfer . " " . $consumption_executiontime . " " . $net2ftp_globals["ftpserver"] . " " . $net2ftp_globals["username"] . " " . $net2ftp_globals["state"] . " " . $net2ftp_globals["state2"] . " " . $net2ftp_globals["screen"] . " " . $net2ftp_globals["directory"] . " " . $net2ftp_globals["entry"];
  112. $result2 = openlog($net2ftp_settings["syslog_ident"], 0, $net2ftp_settings["syslog_facility"]);
  113. if ($result2 == false) {
  114. $errormessage = __("Unable to open the system log.");
  115. setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
  116. return false;
  117. }
  118. // ----------------------------------------------
  119. // Write message to system logger
  120. // ----------------------------------------------
  121. $result3 = syslog($net2ftp_settings["syslog_priority"], $message2log);
  122. if ($result3 == false) {
  123. $errormessage = __("Unable to write a message to the system log.");
  124. setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
  125. return false;
  126. }
  127. } // end if use_syslog
  128. } // end if log_access
  129. } // end logAccess()
  130. // ** **
  131. // ** **
  132. // **************************************************************************************
  133. // **************************************************************************************
  134. // **************************************************************************************
  135. // **************************************************************************************
  136. // ** **
  137. // ** **
  138. function logError() {
  139. // --------------
  140. // This function logs user accesses to the site
  141. //
  142. // IMPORTANT: this function uses, but does not change the global $net2ftp_result[""] variables.
  143. // It returns true on success, false on failure.
  144. // --------------
  145. // -------------------------------------------------------------------------
  146. // Global variables
  147. // -------------------------------------------------------------------------
  148. global $net2ftp_globals, $net2ftp_settings, $net2ftp_result;
  149. if ($net2ftp_settings["log_error"] == "yes") {
  150. // -------------------------------------------------------------------------
  151. // Take a copy of the $net2ftp_result
  152. // If an error occurs within logError, logError will return false and reset the
  153. // $net2ftp_result variable to it's original value
  154. // Also if no error occurs logError will return the variable to it's original value
  155. // -------------------------------------------------------------------------
  156. $net2ftp_result_original = $net2ftp_result;
  157. setErrorVars(true, "", "", "", "");
  158. // -------------------------------------------------------------------------
  159. // Errormessage and debug backtrace
  160. // -------------------------------------------------------------------------
  161. $errormessage = addslashes($net2ftp_result_original["errormessage"]);
  162. $debug_backtrace = "";
  163. $i = sizeof($net2ftp_result_original["debug_backtrace"])-1;
  164. if ($i > 0) {
  165. $debug_backtrace .= addslashes("function " . $net2ftp_result_original["debug_backtrace"][$i]["function"] . " (" . $net2ftp_result_original["debug_backtrace"][$i]["file"] . " on line " . $net2ftp_result_original["debug_backtrace"][$i]["line"] . ")\n");
  166. for ($j=0; $j<sizeof($net2ftp_result_original["debug_backtrace"][$i]["args"]); $j++) {
  167. $debug_backtrace .= addslashes("argument $j: " . $net2ftp_result_original["debug_backtrace"][$i]["args"][$j] . "\n");
  168. }
  169. }
  170. // -------------------------------------------------------------------------
  171. // Date and time
  172. // -------------------------------------------------------------------------
  173. $date = date("Y-m-d");
  174. $time = date("H:i:s");
  175. // -------------------------------------------------------------------------
  176. // Logging to the database
  177. // -------------------------------------------------------------------------
  178. if ($net2ftp_settings["use_database"] == "yes") {
  179. // ----------------------------------------------
  180. // Do not log accesses and errors while the logs are being rotated
  181. // ----------------------------------------------
  182. $logStatus = getLogStatus();
  183. if ($net2ftp_result["success"] == false) { return false; }
  184. if ($logStatus != 0) { return true; }
  185. // ----------------------------------------------
  186. // Input checks
  187. // ----------------------------------------------
  188. // Add slashes to variables which are used in a SQL query, and which are
  189. // potentially unsafe (supplied by the user).
  190. // $date is calculated in this function
  191. // $time is calculated in this function
  192. $net2ftp_ftpserver_safe = addslashes($net2ftp_globals["ftpserver"]);
  193. $net2ftp_username_safe = addslashes($net2ftp_globals["username"]);
  194. $state_safe = addslashes($net2ftp_globals["state"]);
  195. $state2_safe = addslashes($net2ftp_globals["state2"]);
  196. $directory_safe = addslashes($net2ftp_globals["directory"]);
  197. $REMOTE_ADDR_safe = addslashes($net2ftp_globals["REMOTE_ADDR"]);
  198. $REMOTE_PORT_safe = addslashes($net2ftp_globals["REMOTE_PORT"]);
  199. $HTTP_USER_AGENT_safe = addslashes($net2ftp_globals["HTTP_USER_AGENT"]);
  200. // ----------------------------------------------
  201. // Connect to the DB
  202. // ----------------------------------------------
  203. $mydb = connect2db();
  204. if ($net2ftp_result["success"] == false) {
  205. setErrorVars($net2ftp_result_original["success"], $net2ftp_result_original["errormessage"], $net2ftp_result_original["debug_backtrace"], $net2ftp_result_original["file"], $net2ftp_result_original["line"]);
  206. return false;
  207. }
  208. // ----------------------------------------------
  209. // Add record to the database table
  210. // ----------------------------------------------
  211. $sqlquerystring = "INSERT INTO net2ftp_log_error VALUES('$date', '$time', '$net2ftp_ftpserver_safe', '$net2ftp_username_safe', '$errormessage', '$debug_backtrace', '$state_safe', '$state2_safe', '$directory_safe', '$REMOTE_ADDR_safe', '$REMOTE_PORT_safe', '$HTTP_USER_AGENT_safe')";
  212. $result_mysql_query = mysql_query($sqlquerystring);
  213. if ($result_mysql_query == false) {
  214. setErrorVars($net2ftp_result_original["success"], $net2ftp_result_original["errormessage"], $net2ftp_result_original["debug_backtrace"], $net2ftp_result_original["file"], $net2ftp_result_original["line"]);
  215. return false;
  216. }
  217. } // end if use_database
  218. // -------------------------------------------------------------------------
  219. // Logging to the system log
  220. // -------------------------------------------------------------------------
  221. if ($net2ftp_settings["use_syslog"] == "yes") {
  222. // ----------------------------------------------
  223. // Get consumption values
  224. // ----------------------------------------------
  225. if (isset($net2ftp_globals["consumption_datatransfer"]) == true) {
  226. $consumption_datatransfer = $net2ftp_globals["consumption_datatransfer"];
  227. }
  228. else {
  229. $consumption_datatransfer = "0";
  230. }
  231. if (isset($net2ftp_globals["consumption_executiontime"]) == true) {
  232. $consumption_executiontime = $net2ftp_globals["consumption_executiontime"];
  233. }
  234. else {
  235. $consumption_executiontime = "0";
  236. }
  237. // ----------------------------------------------
  238. // Create message
  239. // ----------------------------------------------
  240. $message2log = "$date $time " . $net2ftp_globals["ftpserver"] . " " . $net2ftp_globals["username"] . " " . $net2ftp_result["errormessage"] . " $debug_backtrace " . $net2ftp_globals["state"] . " " . $net2ftp_globals["state2"] . " " . $net2ftp_globals["directory"] . " " . $net2ftp_globals["REMOTE_ADDR"] . " " . $net2ftp_globals["HTTP_USER_AGENT"];
  241. $result2 = openlog($net2ftp_settings["syslog_ident"], 0, $net2ftp_settings["syslog_facility"]);
  242. if ($result2 == false) {
  243. $errormessage = __("Unable to open the system log.");
  244. setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
  245. return false;
  246. }
  247. // ----------------------------------------------
  248. // Write message to system logger
  249. // ----------------------------------------------
  250. $result3 = syslog($net2ftp_settings["syslog_priority"], $message2log);
  251. if ($result3 == false) {
  252. $errormessage = __("Unable to write a message to the system log.");
  253. setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
  254. return false;
  255. }
  256. } // end if use_syslog
  257. // -------------------------------------------------------------------------
  258. // Reset the variable to it's original value
  259. // -------------------------------------------------------------------------
  260. setErrorVars($net2ftp_result_original["success"], $net2ftp_result_original["errormessage"], $net2ftp_result_original["debug_backtrace"], $net2ftp_result_original["file"], $net2ftp_result_original["line"]);
  261. } // end if logErrors
  262. } // end logError()
  263. // ** **
  264. // ** **
  265. // **************************************************************************************
  266. // **************************************************************************************
  267. // **************************************************************************************
  268. // **************************************************************************************
  269. // ** **
  270. // ** **
  271. function getLogStatus() {
  272. // --------------
  273. // This function reads the log rotation status from the database.
  274. // --------------
  275. // -------------------------------------------------------------------------
  276. // Global variables
  277. // -------------------------------------------------------------------------
  278. global $net2ftp_globals, $net2ftp_settings, $net2ftp_result;
  279. // -------------------------------------------------------------------------
  280. // Initial checks
  281. // -------------------------------------------------------------------------
  282. // Verify if a database is used. If not: don't continue.
  283. if ($net2ftp_settings["use_database"] != "yes") { return true; }
  284. // -------------------------------------------------------------------------
  285. // Determine current month and last month
  286. // -------------------------------------------------------------------------
  287. $currentmonth = date("Ym"); // e.g. 201207
  288. $lastmonth = date("Ym", mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
  289. // -------------------------------------------------------------------------
  290. // Connect to the database
  291. // -------------------------------------------------------------------------
  292. $mydb = connect2db();
  293. if ($net2ftp_result["success"] == false) { return false; }
  294. // -------------------------------------------------------------------------
  295. // Get log rotation status
  296. // -------------------------------------------------------------------------
  297. $sqlquery1 = "SELECT status FROM net2ftp_log_status WHERE month = '$currentmonth';";
  298. $result1 = mysql_query("$sqlquery1") or die("Unable to execute SQL SELECT query (getLogStatus > sqlquery1) <br /> $sqlquery1");
  299. $nrofrows1 = mysql_num_rows($result1);
  300. if ($nrofrows1 == 0) {
  301. $logStatus = 1;
  302. }
  303. elseif ($nrofrows1 == 1) {
  304. $resultRow1 = mysql_fetch_row($result1);
  305. $logStatus = $resultRow1[0];
  306. }
  307. else {
  308. setErrorVars(false, __("Table net2ftp_log_status contains duplicate entries."), debug_backtrace(), __FILE__, __LINE__);
  309. return false;
  310. }
  311. // Return $logStatus
  312. return $logStatus;
  313. } // End getLogStatus
  314. // ** **
  315. // ** **
  316. // **************************************************************************************
  317. // **************************************************************************************
  318. // **************************************************************************************
  319. // **************************************************************************************
  320. // ** **
  321. // ** **
  322. function putLogStatus($logStatus) {
  323. // --------------
  324. // This function writes the log rotation status to the database.
  325. // --------------
  326. // -------------------------------------------------------------------------
  327. // Global variables
  328. // -------------------------------------------------------------------------
  329. global $net2ftp_globals, $net2ftp_settings, $net2ftp_result;
  330. // -------------------------------------------------------------------------
  331. // Initial checks
  332. // -------------------------------------------------------------------------
  333. // Verify if a database is used. If not: don't continue.
  334. if ($net2ftp_settings["use_database"] != "yes") { return true; }
  335. // -------------------------------------------------------------------------
  336. // Determine current month and last month
  337. // -------------------------------------------------------------------------
  338. $currentmonth = date("Ym"); // e.g. 201207
  339. $lastmonth = date("Ym", mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
  340. $datetime = mytime();
  341. // -------------------------------------------------------------------------
  342. // Connect to the database
  343. // -------------------------------------------------------------------------
  344. $mydb = connect2db();
  345. if ($net2ftp_result["success"] == false) { return false; }
  346. // -------------------------------------------------------------------------
  347. // Put log rotation status
  348. // -------------------------------------------------------------------------
  349. $sqlquery1 = "SELECT status, changelog FROM net2ftp_log_status WHERE month = '$currentmonth';";
  350. $result1 = mysql_query("$sqlquery1");
  351. $nrofrows1 = mysql_num_rows($result1);
  352. if ($nrofrows1 == 1) {
  353. $resultRow1 = mysql_fetch_row($result1);
  354. $logStatus_old = $resultRow1[0];
  355. $changelog_old = $resultRow1[1];
  356. $changelog_new = $changelog_old . "From $logStatus_old to $logStatus on $datetime. ";
  357. $sqlquery2 = "UPDATE net2ftp_log_status SET status = '" . $logStatus . "', changelog = '" . $changelog_new . "' WHERE month = '$currentmonth';";
  358. $result2 = mysql_query("$sqlquery2");
  359. $nrofrows2 = mysql_affected_rows($mydb);
  360. }
  361. // Don't check on the UPDATE nr of rows, because when the values in the variables and in the table are the same,
  362. // the $nrofrows2 is set to 0.
  363. elseif ($nrofrows1 == 0) {
  364. $changelog_new = "Set to $logStatus on $datetime. ";
  365. $sqlquery3 = "INSERT INTO net2ftp_log_status VALUES('$currentmonth', '" . $logStatus . "', '" . $changelog_new . "');";
  366. $result3 = mysql_query("$sqlquery3");
  367. $nrofrows3 = mysql_affected_rows($mydb);
  368. if ($nrofrows3 != 1) {
  369. setErrorVars(false, __("Table net2ftp_log_status could not be updated."), debug_backtrace(), __FILE__, __LINE__);
  370. return false;
  371. }
  372. }
  373. else {
  374. setErrorVars(false, __("Table net2ftp_log_status contains duplicate entries."), debug_backtrace(), __FILE__, __LINE__);
  375. return false;
  376. }
  377. // -------------------------------------------------------------------------
  378. // Return true
  379. // -------------------------------------------------------------------------
  380. return true;
  381. } // End putLogStatus
  382. // ** **
  383. // ** **
  384. // **************************************************************************************
  385. // **************************************************************************************
  386. // **************************************************************************************
  387. // **************************************************************************************
  388. // ** **
  389. // ** **
  390. function emptyLogs($datefrom, $dateto) {
  391. // --------------
  392. // This function deletes the log records for the dates between $datefrom
  393. // and $dateto.
  394. // The global variable $net2ftp_output["emptyLogs"] is filled with result messages.
  395. // The function returns true on success, false on failure.
  396. // --------------
  397. // -------------------------------------------------------------------------
  398. // Global variables
  399. // -------------------------------------------------------------------------
  400. global $net2ftp_globals, $net2ftp_result, $net2ftp_output;
  401. $toreturn = true;
  402. // -------------------------------------------------------------------------
  403. // Connect to the database
  404. // -------------------------------------------------------------------------
  405. $mydb = connect2db();
  406. if ($net2ftp_result["success"] == false) { return false; }
  407. $tables[1] = "net2ftp_log_access";
  408. $tables[2] = "net2ftp_log_error";
  409. $tables[3] = "net2ftp_log_consumption_ftpserver";
  410. $tables[4] = "net2ftp_log_consumption_ipaddress";
  411. // -------------------------------------------------------------------------
  412. // Execute the queries
  413. // -------------------------------------------------------------------------
  414. for ($i=1; $i<=sizeof($tables); $i++) {
  415. $sqlquery_empty = "DELETE FROM $tables[$i] WHERE date BETWEEN '$datefrom' AND '$dateto';";
  416. $result_empty[$i] = mysql_query("$sqlquery_empty");
  417. $sqlquery_optimize = "OPTIMIZE TABLE `" . $tables[$i] . "`;";
  418. $result_optimize[$i] = mysql_query("$sqlquery_optimize");
  419. if ($result_empty[$i] == true) {
  420. $net2ftp_output["emptyLogs"][] = __("The table <b>%1\$s</b> was emptied successfully.", $tables[$i]);
  421. }
  422. else {
  423. $toreturn = false;
  424. $net2ftp_output["emptyLogs"][] = __("The table <b>%1\$s</b> could not be emptied.", $tables[$i]);
  425. }
  426. if ($result_optimize[$i] == true) {
  427. $net2ftp_output["emptyLogs"][] = __("The table <b>%1\$s</b> was optimized successfully.", $tables[$i]);
  428. }
  429. else {
  430. $toreturn = false;
  431. $net2ftp_output["emptyLogs"][] = __("The table <b>%1\$s</b> could not be optimized.", $tables[$i]);
  432. }
  433. } // end for
  434. return $toreturn;
  435. } // end emptyLogs()
  436. // ** **
  437. // ** **
  438. // **************************************************************************************
  439. // **************************************************************************************
  440. // **************************************************************************************
  441. // **************************************************************************************
  442. // ** **
  443. // ** **
  444. function rotateLogs() {
  445. // --------------
  446. // Rotate the tables
  447. // net2ftp_log_access = active table
  448. // net2ftp_log_access_YYYYMM = archive table with information of month MM and year YYYY
  449. // net2ftp_log_access_template = template table (empty table)
  450. //
  451. // To avoid that the log rotation actions would be executed multiple times at
  452. // the end of the period, a "log rotation status" is used:
  453. // 0 = no rotation needed
  454. // 1 = step 1 not yet started (renaming active tables to archived tables)
  455. // 2 = step 1 in progress
  456. // 3 = step 2 not yet started (copying template tables to the active tables)
  457. // 4 = step 2 in progress
  458. // 5 = step 3 not yet started (dropping oldest archive tables)
  459. // 6 = step 3 in progress
  460. // --------------
  461. // -------------------------------------------------------------------------
  462. // Global variables
  463. // -------------------------------------------------------------------------
  464. global $net2ftp_globals, $net2ftp_settings, $net2ftp_result, $net2ftp_output;
  465. $toreturn = true;
  466. // -------------------------------------------------------------------------
  467. // Verify if a database is used. If not: don't continue.
  468. // -------------------------------------------------------------------------
  469. if ($net2ftp_settings["use_database"] != "yes") { return true; }
  470. // -------------------------------------------------------------------------
  471. // Check if the setting is within the allowed range; if not, set it to 12 months
  472. // -------------------------------------------------------------------------
  473. if (!($net2ftp_settings["log_length_months"] > 1 && $net2ftp_settings["log_length_months"] < 99)) {
  474. $net2ftp_settings["log_length_months"] = 12;
  475. }
  476. // -------------------------------------------------------------------------
  477. // Current month, next month, previous month
  478. // -------------------------------------------------------------------------
  479. $currentmonth = date("Ym"); // e.g. 201207
  480. $lastmonth = date("Ym", mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
  481. $nextmonth = date("Ym", mktime(0, 0, 0, date("m")+1, date("d"), date("Y")));
  482. $dropmonth = date("Ym", mktime(0, 0, 0, date("m")-$net2ftp_settings["log_length_months"]-1, date("d"), date("Y")));
  483. // -------------------------------------------------------------------------
  484. // Connect to the database
  485. // -------------------------------------------------------------------------
  486. $mydb = connect2db();
  487. if ($net2ftp_result["success"] == false) { return false; }
  488. // -------------------------------------------------------------------------
  489. // Get the log rotation status
  490. // -------------------------------------------------------------------------
  491. $logStatus = getLogStatus();
  492. if ($net2ftp_result["success"] == false) { return false; }
  493. // No log rotation needed
  494. if ($logStatus === 0) { return true; }
  495. // -------------------------------------------------------------------------
  496. // Table names and SQL queries to create the tables
  497. // -------------------------------------------------------------------------
  498. $tables[1]["name"] = "net2ftp_log_access";
  499. $tables[2]["name"] = "net2ftp_log_error";
  500. $tables[3]["name"] = "net2ftp_log_consumption_ftpserver";
  501. $tables[4]["name"] = "net2ftp_log_consumption_ipaddress";
  502. // -------------------------------------------------------------------------
  503. // step 1 of rotation: rename active tables to archived tables
  504. // -------------------------------------------------------------------------
  505. if ($logStatus == 1) {
  506. // Set the log status to indicate this step is in progress
  507. putLogStatus(2);
  508. if ($net2ftp_result["success"] == false) { return false; }
  509. // Execute the step
  510. for ($i=1; $i<=sizeof($tables); $i++) {
  511. $table = $tables[$i]["name"]; // Example: net2ftp_log_access
  512. $table_archive = $table . "_" . $lastmonth; // Example: net2ftp_log_access_201206
  513. $table_archive_drop = $table . "_" . $dropmonth; // Example: net2ftp_log_access_201106
  514. $sqlquery_rename = "RENAME TABLE $table TO $table_archive";
  515. $result_rename[$i] = mysql_query("$sqlquery_rename");
  516. if ($result_rename[$i] == true) {
  517. $net2ftp_output["rotateLogs"][] = __("The log tables were renamed successfully.");
  518. }
  519. else {
  520. $toreturn = false;
  521. $net2ftp_output["rotateLogs"][] = __("The log tables could not be renamed.");
  522. }
  523. } // end for
  524. // Set the log status to indicate this step is in done and the next can start
  525. putLogStatus(3);
  526. if ($net2ftp_result["success"] == false) { return false; }
  527. }
  528. // -------------------------------------------------------------------------
  529. // step 2 of rotation: copy template tables to the active
  530. // -------------------------------------------------------------------------
  531. elseif ($logStatus == 3) {
  532. // Set the log status to indicate this step is in progress
  533. putLogStatus(4);
  534. if ($net2ftp_result["success"] == false) { return false; }
  535. // Execute the step
  536. for ($i=1; $i<=sizeof($tables); $i++) {
  537. $table = $tables[$i]["name"]; // Example: net2ftp_log_access
  538. $table_archive = $table . "_" . $lastmonth; // Example: net2ftp_log_access_201206
  539. $table_archive_drop = $table . "_" . $dropmonth; // Example: net2ftp_log_access_201106
  540. $sqlquery_copy = "CREATE TABLE $table LIKE $table_archive";
  541. $result_copy[$i] = mysql_query("$sqlquery_copy");
  542. if ($result_copy[$i] == true) {
  543. $net2ftp_output["rotateLogs"][] = __("The log tables were copied successfully.");
  544. }
  545. else {
  546. $toreturn = false;
  547. $net2ftp_output["rotateLogs"][] = __("The log tables could not be copied.");
  548. }
  549. } // end for
  550. // Set the log status to indicate this step is in done and the next can start
  551. putLogStatus(5);
  552. if ($net2ftp_result["success"] == false) { return false; }
  553. }
  554. // -------------------------------------------------------------------------
  555. // step 3 of rotation: drop oldest archive tables
  556. // -------------------------------------------------------------------------
  557. elseif ($logStatus == 5) {
  558. // Set the log status to indicate this step is in progress
  559. putLogStatus(6);
  560. if ($net2ftp_result["success"] == false) { return false; }
  561. // Execute the step
  562. for ($i=1; $i<=sizeof($tables); $i++) {
  563. $table = $tables[$i]["name"]; // Example: net2ftp_log_access
  564. $table_archive = $table . "_" . $lastmonth; // Example: net2ftp_log_access_201206
  565. $table_archive_drop = $table . "_" . $dropmonth; // Example: net2ftp_log_access_201106
  566. $sqlquery_drop = "DROP TABLE IF EXISTS $table_archive_drop;";
  567. $result_drop[$i] = mysql_query("$sqlquery_drop");
  568. if ($result_drop[$i] == true) {
  569. $net2ftp_output["rotateLogs"][] = __("The oldest log table was dropped successfully.");
  570. }
  571. else {
  572. $toreturn = false;
  573. $net2ftp_output["rotateLogs"][] = __("The oldest log table could not be dropped.");
  574. }
  575. } // end for
  576. // Set the log status to indicate this step is in done and the rotation is over
  577. putLogStatus(0);
  578. if ($net2ftp_result["success"] == false) { return false; }
  579. }
  580. return $toreturn;
  581. } // end rotateLogs()
  582. // ** **
  583. // ** **
  584. // **************************************************************************************
  585. // **************************************************************************************
  586. ?>