From 344f837c30b6a5c167e3aada7bd9ad8a320062ee Mon Sep 17 00:00:00 2001 From: Kevin Puertas Date: Sat, 16 Dec 2017 20:17:00 +0100 Subject: [PATCH] Optimize index and fix delete older checks from hist --- README.md | 18 +++--- assets/html/indexpage.twig | 14 ++--- assets/html/panel/p_checks.twig | 3 +- assets/html/panel/p_settings.twig | 8 +-- assets/translations/en.php | 1 + assets/translations/es.php | 1 + connect.php | 16 +++-- cronchk.php | 14 ++--- functions.php | 55 ++++++---------- index.php | 23 ++++--- install.php | 10 ++- lib/Twig/CHANGELOG | 24 ++++++- lib/Twig/lib/Twig/Environment.php | 25 ++++++-- lib/Twig/lib/Twig/Extension/Core.php | 4 +- lib/Twig/lib/Twig/Profiler/Dumper/Base.php | 62 +++++++++++++++++++ lib/Twig/lib/Twig/Profiler/Dumper/Html.php | 2 +- lib/Twig/lib/Twig/Profiler/Dumper/Text.php | 41 +----------- lib/Twig/lib/Twig/Profiler/Profile.php | 6 ++ .../lib/Twig/Test/IntegrationTestCase.php | 12 ++++ lib/Twig/lib/Twig/TokenParser/Use.php | 2 + lib/dbwrapper.php | 37 +++++++---- login.php | 16 +++-- panel.php | 5 +- updater.php | 21 +++++++ 24 files changed, 262 insertions(+), 158 deletions(-) create mode 100644 lib/Twig/lib/Twig/Profiler/Dumper/Base.php create mode 100644 updater.php diff --git a/README.md b/README.md index 542fe5c..2c2c23a 100755 --- a/README.md +++ b/README.md @@ -5,19 +5,19 @@ without dependencies and installable only coping repository and ajusting some pa Also can show stadistics for all sites -Demo can be viewed in https://status.jkanetwork.com/ +Real using index 'demo' can be viewed in https://status.jkanetwork.com/ ## Disclaimer -This is an beta, opensource, and human made program. **There may be bugs and problems**, and -although we can help or fix things, JKANetwork is **not** responsable for derivated problems. +This is an beta, opensource, and a human made program. **There may be bugs and problems**, and +although we can update and fix things, JKANetwork is **not** responsable for derivated problems. You can see the master code and tell us any improvment or bug. Plase see indications for installing if you want -to install instead of downloading master and install, that it will not work right. Master is like beta, and tags its like releases. +to install without problems. Master is bleeding edge, use tags for versions and not having problems. ## Requirements --Modern PHP (5.6+) with SQLite enabled +-Modern PHP (5.6+) with SQLite3 enabled -Cron support @@ -36,11 +36,15 @@ For finishing it, you have to add a cron in your system for running checks. The */5 * * * * root cd /srv/http/checkserver/ && /usr/bin/php -f cronchk.php >/dev/null 2>&1 ``` +## Update + ## Changelog -### (NEXT) Version 0.9.1 (Beta) +### (WIP) Version 0.9.1 --TODO: See uptime percent +-See uptime percent + +-Optimized index webpage, times went down to half when have a lot of pages -TODO: Better index admin page (More data) diff --git a/assets/html/indexpage.twig b/assets/html/indexpage.twig index 6759042..bf3db5c 100755 --- a/assets/html/indexpage.twig +++ b/assets/html/indexpage.twig @@ -32,16 +32,16 @@ {# "Switch" type #} {% if check.ID_TC == 1 or check.ID_TC == 2 or check.ID_TC == 4 %} {# Ping, HttpCode, SQL DB #} - {#IF/SWITCH status text#} - {%if check.status == 0%} + {#IF/SWITCH uptime to text#} + {%if check.uptime <= 100 and check.uptime > 90 %} {{T_.status_right}} - {%elseif check.status == 1%} + {%elseif check.uptime <= 95 and check.uptime > 60 %} {{T_.status_lproblems}} - {%elseif check.status == 2%} + {%elseif check.uptime <= 60 and check.uptime > 15 %} {{T_.status_problems}} - {%elseif check.status == 3%} + {%elseif check.uptime <= 15 and check.uptime >= 0 %} {{T_.status_outofserv}} - {%elseif check.status == 4%} + {%elseif check.uptime == 255 %} {{T_.status_maintenance}} {%else%} {{T_.status_empty}} @@ -49,7 +49,7 @@ {#END IF/SWITCH#} ({{ check.uptime}}%) - {% if check.status is defined %}{#Only if records exists#} + {% if check.uptime != -1 %}{#Only if records exists#} {{check.dateLastChk}} - {% if check.failedLastChk == 0 %} diff --git a/assets/html/panel/p_checks.twig b/assets/html/panel/p_checks.twig index 25050d3..3acb4c8 100755 --- a/assets/html/panel/p_checks.twig +++ b/assets/html/panel/p_checks.twig @@ -38,10 +38,11 @@ {{check.name}} {{check.nameTCheck}} - {{check.URL}} + {{T_.view_hist}} {% if you.fullRights == 1 %} {{T_.edit}} {% if check.ID_TC == 3 %}{#Visitas#} - + {% elseif check.ID_TC == 4 %}{#Database#} Codigo para check database diff --git a/assets/html/panel/p_settings.twig b/assets/html/panel/p_settings.twig index 97cf119..ca9a71c 100755 --- a/assets/html/panel/p_settings.twig +++ b/assets/html/panel/p_settings.twig @@ -10,18 +10,18 @@
-
+
- Nombre para sacar en titulos. + Nombre para sacar en titulos.
- +
- 300 es un buen numero + Por defecto, dos semanas
diff --git a/assets/translations/en.php b/assets/translations/en.php index 364b803..86fbee5 100755 --- a/assets/translations/en.php +++ b/assets/translations/en.php @@ -37,4 +37,5 @@ $T_['HTTP_CODE'] = 'Http code'; $T_['VISIT_COUNT'] = 'Visit counter'; $T_['DATABASE'] = 'Database check'; $T_['add_grp_to_add_chk'] = 'Add a group for start adding checks'; +$T_['view_hist'] = 'View history'; ?> \ No newline at end of file diff --git a/assets/translations/es.php b/assets/translations/es.php index 8190912..a55d4df 100755 --- a/assets/translations/es.php +++ b/assets/translations/es.php @@ -37,4 +37,5 @@ $T_['HTTP_CODE'] = 'Código HTTP'; $T_['VISIT_COUNT'] = 'Contador de visitas'; $T_['DATABASE'] = 'Conexión a base de datos'; $T_['add_grp_to_add_chk'] = 'Añade un grupo para empezar a añadir checks'; +$T_['view_hist'] = 'Ver histórico'; ?> \ No newline at end of file diff --git a/connect.php b/connect.php index 29f7cb9..02ba389 100755 --- a/connect.php +++ b/connect.php @@ -1,4 +1,6 @@ Please run updater.php to update before using page"); +} ?> \ No newline at end of file diff --git a/cronchk.php b/cronchk.php index 189549c..4f0cfa3 100755 --- a/cronchk.php +++ b/cronchk.php @@ -72,16 +72,10 @@ while ($site = dbw_fetch_array($db_conn,$sites)){ } -//Delete old Checks (Based on user input) -$sites = dbw_query($db_conn,"SELECT * FROM CHECKS"); -while ($site = dbw_fetch_array($db_conn,$sites)){ - $count = dbw_query_fetch_array($db_conn,"SELECT COUNT(*) FROM CHKHIST WHERE ID_C='$site[ID_C]'")[0]; //Count how much checks has a site - - if ($count > getSystemOpt($db_conn,"maxChecksSave")){ - $todelete = $count - getSystemOpt($db_conn,"maxChecksSave"); //How much to delete - dbw_query($db_conn, "DELETE FROM CHKHIST WHERE ID_C='$site[ID_C]' ORDER BY `timestamp` ASC LIMIT $todelete"); - } -} +//Delete old Checks (Based on time to save, put in secs) +$mintime = time()-(getSystemOpt($db_conn,"maxTimeSave")*24*3600); +dbw_query($db_conn,"DELETE FROM CHKHIST WHERE `timestamp` < $mintime"); + //Delete Checks stored for pages that not exist dbw_query($db_conn, "DELETE FROM CHKHIST WHERE ID_C NOT IN (SELECT ID_C FROM CHECKS)"); diff --git a/functions.php b/functions.php index 59dcba7..2a68482 100755 --- a/functions.php +++ b/functions.php @@ -3,49 +3,31 @@ require_once "connect.php"; require_once 'lib/loadTwig.php'; -/** Function checkStatus checks last "Checks" and their return - Return can be: - A value from 0-1-2 for status. 3 for Out Of Service. 3 and 4 can be turned up manually ( - 0-Perfect - 1-Little problems - 2-Problems - 3-Out of service - 4-In maintenance - 255-Not results recolected (grey) -*/ -function checkStatus($db_conn,$ID_C){ - //First, check if the status is marked to any manual value - $manualPage = dbw_query_fetch_array($db_conn,"SELECT ID_C,manStatus FROM CHECKS WHERE ID_C='$ID_C'")['manStatus']; - if ($manualPage != NULL){return $manualPage;} - //If it's not manual-setted, see real status - $result = checkUptime($db_conn,$ID_C); - //Return status - if ($result == 255){return 255;} - if ($result > 95 && $result <= 100){return 0;} - if ($result > 60 && $result <= 95){return 1;} - if ($result > 10 && $result <= 60){return 2;} - if ($result >= 0 && $result <= 10){return 3;} //If test fail much (90% fail) -} - function checkUptime($db_conn,$ID_C,$precision = 0){ if ($precision == 0){ - $time = time()-30*24*3600; //Last month + $time = time()-30*7*3600; //Last week }else{ $time = $precision; } - if ($precision != 0){ - $good = dbw_query_fetch_array($db_conn,"SELECT COUNT(*) FROM CHKHIST WHERE ID_C = '$ID_C' AND code == 0 AND `timestamp` > $time")[0]; - $all = dbw_query_fetch_array($db_conn,"SELECT COUNT(*) FROM CHKHIST WHERE ID_C = '$ID_C' AND `timestamp` > $time")[0]; - }else{ //Uptime of all time - $good = dbw_query_fetch_array($db_conn,"SELECT COUNT(*) FROM CHKHIST WHERE ID_C = '$ID_C' AND code == 0")[0]; - $all = dbw_query_fetch_array($db_conn,"SELECT COUNT(*) FROM CHKHIST WHERE ID_C = '$ID_C'")[0]; + $res = dbw_query($db_conn,"SELECT code FROM CHKHIST WHERE ID_C='$ID_C' AND `timestamp` > $time"); + $count = 0; + $err = 0; + while ($fila = dbw_fetch_array($db_conn,$res)){ + $count++; + $err += $fila['code']; } - if ($all != 0){ - $uptime = round(($good/$all)*100,2); - return $uptime; + echo $count."-"; + if ($count == 0){ + return '-1'; //No registers }else{ - return 100; + if ($err != 0){ + $uptime = (100 - round(($err/$count)*100,2)); + return $uptime; + }else{ + return 100; + } } + } @@ -103,13 +85,12 @@ function RandomString($length) /** 1 if its logued, 0 if not */ function isLogued(){ - if (isset($_COOKIE['SessionID'])){ + if (isset($_SESSION['UserID'])){ return 1; }else{ return 0; } } - function requireLogin(){ if (isLogued() == 0){ die('You dont have admin rights'); //This blocks edit or create nothing if its not logued diff --git a/index.php b/index.php index a258884..f4a403d 100755 --- a/index.php +++ b/index.php @@ -1,4 +1,5 @@ render('indexpage.twig', array('T_' => $T_, 'you' => $you, 'checks' => $chks, 'news' => $incs)); //Render - +echo "

Página creada en ".round(microtime(true)-$d_time,2)." segundos

"; ?> \ No newline at end of file diff --git a/install.php b/install.php index 8e63e58..c00f6dc 100755 --- a/install.php +++ b/install.php @@ -1,6 +1,5 @@ render('install.twig'); }else{ if ($_POST['passw1'] !== $_POST['passw2']){ - die("NONO"); + die("Passwords didn't match"); }else{ //Creamos toda la base de datos require_once "lib/dbwrapper.php"; //For using database @@ -30,7 +29,6 @@ CREATE TABLE "USERS" ( `ID_U` INTEGER PRIMARY KEY AUTOINCREMENT, `nick` TEXT NOT NULL UNIQUE, `passw` TEXT, - `SessionID` INTEGER UNIQUE, `fullRights` INTEGER NOT NULL ); CREATE TABLE "TOKENS" ( @@ -78,10 +76,10 @@ COMMIT;'; dbw_query($db_conn,"INSERT INTO USERS (nick,passw,fullRights) VALUES('$user','$pass',1)"); //Create user //System opts - dbw_query($db_conn,"INSERT INTO SYS VALUES ('maxChecksSave',300)"); + dbw_query($db_conn,"INSERT INTO SYS VALUES ('maxTimeSave',14)"); dbw_query($db_conn,"INSERT INTO SYS VALUES ('lang','$_POST[lang]')"); dbw_query($db_conn,"INSERT INTO SYS VALUES ('websiteTitle','Status')"); - dbw_query($db_conn,"INSERT INTO SYS VALUES ('version','$ver')"); + dbw_query($db_conn,"INSERT INTO SYS VALUES ('version','".VERSION."')"); echo $twig->render('install.twig', array('part' => 2)); } diff --git a/lib/Twig/CHANGELOG b/lib/Twig/CHANGELOG index 94c18e4..1725c30 100755 --- a/lib/Twig/CHANGELOG +++ b/lib/Twig/CHANGELOG @@ -1,3 +1,13 @@ +* 2.4.4 (2017-09-27) + + * added Twig_Profiler_Profile::reset() + * fixed use TokenParser to return an empty Node + * added RuntimeExtensionInterface + * added circular reference detection when loading templates + * added support for runtime loaders in IntegrationTestCase + * fixed deprecation when using Twig_Profiler_Dumper_Html + * removed @final from Twig_Profiler_Dumper_Text + * 2.4.3 (2017-06-07) * fixed namespaces introduction @@ -69,9 +79,17 @@ * improved the performance of the filesystem loader * removed features that were deprecated in 1.x -* 1.34.4 (2017-XX-XX) +* 1.35.0 (2017-XX-XX) - * n/a + * added Twig_Profiler_Profile::reset() + * fixed use TokenParser to return an empty Node + * added RuntimeExtensionInterface + * added circular reference detection when loading templates + +* 1.34.4 (2017-07-04) + + * added support for runtime loaders in IntegrationTestCase + * fixed deprecation when using Twig_Profiler_Dumper_Html * 1.34.3 (2017-06-07) @@ -212,7 +230,7 @@ * fixed reserved keywords (forbids true, false, null and none keywords for variables names) * fixed support for PHP7 (Throwable support) - * marked the following methods as being internals on Twig_Environment: + * marked the following methods as being internals on Twig_Environment: getFunctions(), getFilters(), getTests(), getFunction(), getFilter(), getTest(), getTokenParsers(), getTags(), getNodeVisitors(), getUnaryOperators(), getBinaryOperators(), getFunctions(), getFilters(), getGlobals(), initGlobals(), initExtensions(), and initExtension() diff --git a/lib/Twig/lib/Twig/Environment.php b/lib/Twig/lib/Twig/Environment.php index 0e0ba14..7658cca 100755 --- a/lib/Twig/lib/Twig/Environment.php +++ b/lib/Twig/lib/Twig/Environment.php @@ -16,11 +16,11 @@ */ class Twig_Environment { - const VERSION = '2.4.3'; - const VERSION_ID = 20403; + const VERSION = '2.4.4'; + const VERSION_ID = 20404; const MAJOR_VERSION = 2; const MINOR_VERSION = 4; - const RELEASE_VERSION = 3; + const RELEASE_VERSION = 4; const EXTRA_VERSION = ''; private $charset; @@ -42,6 +42,7 @@ class Twig_Environment private $runtimeLoaders = array(); private $runtimes = array(); private $optionsHash; + private $loading = array(); /** * Constructor. @@ -308,6 +309,10 @@ class Twig_Environment * * @param string|Twig_TemplateWrapper|Twig_Template $name The template name * + * @throws Twig_Error_Loader When the template cannot be found + * @throws Twig_Error_Runtime When a previously generated cache is corrupted + * @throws Twig_Error_Syntax When an error occurred during compilation + * * @return Twig_TemplateWrapper */ public function load($name) @@ -382,7 +387,19 @@ class Twig_Environment // to be removed in 3.0 $this->extensionSet->initRuntime($this); - return $this->loadedTemplates[$cls] = new $cls($this); + if (isset($this->loading[$cls])) { + throw new Twig_Error_Runtime(sprintf('Circular reference detected for Twig template "%s", path: %s.', $name, implode(' -> ', array_merge($this->loading, array($name))))); + } + + $this->loading[$cls] = $name; + + try { + $this->loadedTemplates[$cls] = new $cls($this); + } finally { + unset($this->loading[$cls]); + } + + return $this->loadedTemplates[$cls]; } /** diff --git a/lib/Twig/lib/Twig/Extension/Core.php b/lib/Twig/lib/Twig/Extension/Core.php index 25fd9d8..ae62de6 100755 --- a/lib/Twig/lib/Twig/Extension/Core.php +++ b/lib/Twig/lib/Twig/Extension/Core.php @@ -1430,8 +1430,6 @@ function twig_array_batch($items, $size, $fill = null) */ function twig_get_attribute(Twig_Environment $env, Twig_Source $source, $object, $item, array $arguments = array(), $type = Twig_Template::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false) { - static $cache = array(); - // array if (Twig_Template::METHOD_CALL !== $type) { $arrayItem = is_bool($item) || is_float($item) ? (int) $item : $item; @@ -1518,6 +1516,8 @@ function twig_get_attribute(Twig_Environment $env, Twig_Source $source, $object, } } + static $cache = array(); + $class = get_class($object); // object method diff --git a/lib/Twig/lib/Twig/Profiler/Dumper/Base.php b/lib/Twig/lib/Twig/Profiler/Dumper/Base.php new file mode 100644 index 0000000..913afd4 --- /dev/null +++ b/lib/Twig/lib/Twig/Profiler/Dumper/Base.php @@ -0,0 +1,62 @@ + + */ +abstract class Twig_Profiler_Dumper_Base +{ + private $root; + + public function dump(Twig_Profiler_Profile $profile) + { + return $this->dumpProfile($profile); + } + + abstract protected function formatTemplate(Twig_Profiler_Profile $profile, $prefix); + + abstract protected function formatNonTemplate(Twig_Profiler_Profile $profile, $prefix); + + abstract protected function formatTime(Twig_Profiler_Profile $profile, $percent); + + private function dumpProfile(Twig_Profiler_Profile $profile, $prefix = '', $sibling = false) + { + if ($profile->isRoot()) { + $this->root = $profile->getDuration(); + $start = $profile->getName(); + } else { + if ($profile->isTemplate()) { + $start = $this->formatTemplate($profile, $prefix); + } else { + $start = $this->formatNonTemplate($profile, $prefix); + } + $prefix .= $sibling ? '│ ' : ' '; + } + + $percent = $this->root ? $profile->getDuration() / $this->root * 100 : 0; + + if ($profile->getDuration() * 1000 < 1) { + $str = $start."\n"; + } else { + $str = sprintf("%s %s\n", $start, $this->formatTime($profile, $percent)); + } + + $nCount = count($profile->getProfiles()); + foreach ($profile as $i => $p) { + $str .= $this->dumpProfile($p, $prefix, $i + 1 !== $nCount); + } + + return $str; + } +} + +class_alias('Twig_Profiler_Dumper_Base', 'Twig\Profiler\Dumper\BaseDumper', false); +class_exists('Twig_Profiler_Profile'); diff --git a/lib/Twig/lib/Twig/Profiler/Dumper/Html.php b/lib/Twig/lib/Twig/Profiler/Dumper/Html.php index 186a250..7c2f791 100755 --- a/lib/Twig/lib/Twig/Profiler/Dumper/Html.php +++ b/lib/Twig/lib/Twig/Profiler/Dumper/Html.php @@ -12,7 +12,7 @@ /** * @author Fabien Potencier */ -final class Twig_Profiler_Dumper_Html extends Twig_Profiler_Dumper_Text +final class Twig_Profiler_Dumper_Html extends Twig_Profiler_Dumper_Base { private static $colors = array( 'block' => '#dfd', diff --git a/lib/Twig/lib/Twig/Profiler/Dumper/Text.php b/lib/Twig/lib/Twig/Profiler/Dumper/Text.php index 1171e5c..863112d 100755 --- a/lib/Twig/lib/Twig/Profiler/Dumper/Text.php +++ b/lib/Twig/lib/Twig/Profiler/Dumper/Text.php @@ -11,18 +11,9 @@ /** * @author Fabien Potencier - * - * @final */ -class Twig_Profiler_Dumper_Text +final class Twig_Profiler_Dumper_Text extends Twig_Profiler_Dumper_Base { - private $root; - - public function dump(Twig_Profiler_Profile $profile) - { - return $this->dumpProfile($profile); - } - protected function formatTemplate(Twig_Profiler_Profile $profile, $prefix) { return sprintf('%s└ %s', $prefix, $profile->getTemplate()); @@ -37,36 +28,6 @@ class Twig_Profiler_Dumper_Text { return sprintf('%.2fms/%.0f%%', $profile->getDuration() * 1000, $percent); } - - private function dumpProfile(Twig_Profiler_Profile $profile, $prefix = '', $sibling = false) - { - if ($profile->isRoot()) { - $this->root = $profile->getDuration(); - $start = $profile->getName(); - } else { - if ($profile->isTemplate()) { - $start = $this->formatTemplate($profile, $prefix); - } else { - $start = $this->formatNonTemplate($profile, $prefix); - } - $prefix .= $sibling ? '│ ' : ' '; - } - - $percent = $this->root ? $profile->getDuration() / $this->root * 100 : 0; - - if ($profile->getDuration() * 1000 < 1) { - $str = $start."\n"; - } else { - $str = sprintf("%s %s\n", $start, $this->formatTime($profile, $percent)); - } - - $nCount = count($profile->getProfiles()); - foreach ($profile as $i => $p) { - $str .= $this->dumpProfile($p, $prefix, $i + 1 !== $nCount); - } - - return $str; - } } class_alias('Twig_Profiler_Dumper_Text', 'Twig\Profiler\Dumper\TextDumper', false); diff --git a/lib/Twig/lib/Twig/Profiler/Profile.php b/lib/Twig/lib/Twig/Profiler/Profile.php index d7b3c41..2177f60 100755 --- a/lib/Twig/lib/Twig/Profiler/Profile.php +++ b/lib/Twig/lib/Twig/Profiler/Profile.php @@ -149,6 +149,12 @@ class Twig_Profiler_Profile implements IteratorAggregate, Serializable ); } + public function reset() + { + $this->starts = $this->ends = $this->profiles = array(); + $this->enter(); + } + public function getIterator() { return new ArrayIterator($this->profiles); diff --git a/lib/Twig/lib/Twig/Test/IntegrationTestCase.php b/lib/Twig/lib/Twig/Test/IntegrationTestCase.php index d7588cf..e4518a0 100755 --- a/lib/Twig/lib/Twig/Test/IntegrationTestCase.php +++ b/lib/Twig/lib/Twig/Test/IntegrationTestCase.php @@ -24,6 +24,14 @@ abstract class Twig_Test_IntegrationTestCase extends TestCase */ abstract protected function getFixturesDir(); + /** + * @return Twig_RuntimeLoaderInterface[] + */ + protected function getRuntimeLoaders() + { + return array(); + } + /** * @return Twig_ExtensionInterface[] */ @@ -143,6 +151,10 @@ abstract class Twig_Test_IntegrationTestCase extends TestCase ), $match[2] ? eval($match[2].';') : array()); $twig = new Twig_Environment($loader, $config); $twig->addGlobal('global', 'global'); + foreach ($this->getRuntimeLoaders() as $runtimeLoader) { + $twig->addRuntimeLoader($runtimeLoader); + } + foreach ($this->getExtensions() as $extension) { $twig->addExtension($extension); } diff --git a/lib/Twig/lib/Twig/TokenParser/Use.php b/lib/Twig/lib/Twig/TokenParser/Use.php index 69b05b1..0f35c9d 100755 --- a/lib/Twig/lib/Twig/TokenParser/Use.php +++ b/lib/Twig/lib/Twig/TokenParser/Use.php @@ -55,6 +55,8 @@ final class Twig_TokenParser_Use extends Twig_TokenParser $stream->expect(Twig_Token::BLOCK_END_TYPE); $this->parser->addTrait(new Twig_Node(array('template' => $template, 'targets' => new Twig_Node($targets)))); + + return new Twig_Node(); } public function getTag() diff --git a/lib/dbwrapper.php b/lib/dbwrapper.php index 46d1665..44d0cf0 100755 --- a/lib/dbwrapper.php +++ b/lib/dbwrapper.php @@ -49,14 +49,20 @@ function dbw_escape_string($conn,$string){ /** Make query */ function dbw_query($conn,$query){ + $time = microtime(true); switch ($conn[1]){ case "mysqli": - return mysqli_query($conn[0],$query); + $ret = mysqli_query($conn[0],$query); + break; case "sqlite": - return $conn[0]->query($query); + $ret = $conn[0]->query($query); + break; case "PgSQL": - return pg_query($query); //Last error (pg_last_error()) not implemented + $ret = pg_query($query); + break; } + //echo "

".$query."->".(microtime(true)-$time)." milisegundos

"; + return $ret; } /** Fetch array from query */ @@ -83,23 +89,27 @@ function dbw_fetch_array($conn,$result,$typearray = NULL){ /** Make query and fetch array */ function dbw_query_fetch_array($conn,$query){ + $time = microtime(true); + switch ($conn[1]){ case "mysqli": - $query = mysqli_query($conn[0],$query); - if ($query == false || $query == NULL){return false;} - return mysqli_fetch_array($query); + $result = mysqli_query($conn[0],$query); + if (!$result){return false;} + $ret = mysqli_fetch_array($result); break; case "sqlite": - $query = $conn[0]->query($query); - if ($query == false || $query == NULL){return false;} - return $query->fetchArray(); + $result = $conn[0]->query($query); + if (!$result){return false;} + $ret = $result->fetchArray(); break; case "PgSQL": - $query = pg_query($query); - if ($query == false || $query == NULL){return false;} - return pg_fetch_array($query); //Last error (pg_last_error()) not implemented + $result = pg_query($query); + if (!$result){return false;} + $ret = pg_fetch_array($result); //Last error (pg_last_error()) not implemented break; } + //echo "

".$query."->".(microtime(true)-$time)." milisegundos

"; + return $ret; } /** Goes a query to $row. $row starts in 0 as first row as if not specified */ @@ -155,7 +165,8 @@ function dbw_num_rows($conn,$result){ case "mysqli": return mysqli_num_rows($result); case "sqlite": - return $result->numRows(); + die("Sqlite3 not supports numRows, use query with COUNT(*)"); + return $result->numRows(); //ERROR case "PgSQL": return pg_num_rows ($result); } diff --git a/login.php b/login.php index 39b73c4..d20b87f 100755 --- a/login.php +++ b/login.php @@ -4,13 +4,13 @@ require "functions.php"; if (isset($_GET['do'])){ if ($_GET['do'] == 'logout'){ require_once "connect.php"; - dbw_query($db_conn, "UPDATE USERS SET SessionID = NULL WHERE SessionID='$_COOKIE[SessionID]'"); - setcookie("SessionID", "", time()-3600,"/"); + //dbw_query($db_conn, "UPDATE USERS SET SessionID = NULL WHERE SessionID='$_COOKIE[SessionID]'"); + session_destroy(); header('Location: index.php'); } } -if (isset($_COOKIE['SessionID'])){ //Ya está logueado +if (isset($_SESSION['UserID'])){ //Ya está logueado header('Location: panel.php'); } @@ -20,14 +20,12 @@ if (!isset($_POST['nick'])){ if (isset($_POST['nick'])){ $nick=$_POST['nick']; $pass= hash("sha256",$_POST['pass']); - $finalpass = dbw_query_fetch_array($db_conn,"SELECT `Passw` FROM `USERS` WHERE nick ='$nick'"); - $passcomprobar = $finalpass[0]; + $resql = dbw_query_fetch_array($db_conn,"SELECT ID_U,`passw` FROM `USERS` WHERE nick ='$nick'"); + $passcomprobar = $resql['passw']; if ($pass == $passcomprobar) { - $sesid = RandomString(12); - dbw_query($db_conn, "UPDATE USERS SET SessionID = '$sesid' WHERE nick = '$nick'"); - setcookie("SessionID",$sesid,"0","/"); - header('Location: panel.php'); + $_SESSION['UserID'] = $resql['ID_U']; + header('Location: panel.php'); }else{ echo $twig->render('login.twig', array('status' => 'error')); } diff --git a/panel.php b/panel.php index 1f62435..367e2d1 100755 --- a/panel.php +++ b/panel.php @@ -208,7 +208,10 @@ switch($page){ header('Location: panel.php?page=news'); break; - + case 'savesettings': + //All group names and IDs + dbw_query($db_conn,"UPDATE SYS SET `value`='$_POST[name]' WHERE `option`='name'"); + dbw_query($db_conn,"UPDATE SYS SET `value`='$_POST[maxTimeSave]' WHERE `option`='maxTimeSave'"); case 'settings': //All group names and IDs $results = dbw_query($db_conn,"SELECT * FROM SYS"); //All groups diff --git a/updater.php b/updater.php new file mode 100644 index 0000000..edf2239 --- /dev/null +++ b/updater.php @@ -0,0 +1,21 @@ +Updater of CheckServer"; +echo "

Connecting to database

"; +require_once 'connect.php'; +$version = dbw_query_fetch_array($db_conn,"SELECT value FROM SYS WHERE option = 'version'")[0]; +echo "

Your version: ".$version; +echo "

Updating to lastest

"; +switch ($version){ + case '0.9': + echo "

0.9->0.9.1

"; + dbw_query($db_conn,"DELETE FROM SYS WHERE option='maxChecksSave'"); + dbw_query($db_conn,"INSERT INTO SYS VALUES('maxTimeSave',14)"); + //Can not delete USERS.SessionID because sqlite3 not supports it, but doesn't do any harm. + + case '0.9.1': + echo "0.9.1-Lastest"; + echo "

Updated to lastest. Close this and go to index

"; + //Remove files on a good update + unlink('updater.php'); + unlink('installer.php'); +} \ No newline at end of file