From 49a65d4d1a846b7ed573b93c2b3588c7722cc313 Mon Sep 17 00:00:00 2001 From: Kilian Hofmann Date: Fri, 12 Jul 2024 12:12:31 +0200 Subject: [PATCH] OOP Counter --- base/autoloader.php | 7 + homework/1/colourSelector/index.php | 6 +- homework/1/themeSwitcher/index.php | 6 +- homework/2/counterDB/counter/counter.php | 6 +- homework/2/counterDB/counter/functions.php | 2 +- homework/2/counterDB/index.php | 8 +- tasks/calc/index.php | 6 +- tasks/counter/index.php | 6 +- tasks/functions/index.php | 6 +- tasks/functions2/index.php | 8 +- tasks/guestBook/actions/comment.php | 4 +- tasks/guestBook/index.php | 8 +- tasks/guestBookDB/actions/comment.php | 8 +- tasks/guestBookDB/actions/login.php | 8 +- tasks/guestBookDB/actions/logout.php | 2 +- tasks/guestBookDB/actions/register.php | 10 +- tasks/guestBookDB/confirm.php | 10 +- tasks/guestBookDB/index.php | 14 +- tasks/guestBookDB/login.php | 6 +- tasks/guestBookDB/register.php | 6 +- tasks/itemList/index.php | 10 +- tasks/oop/classes/counter/counter.php | 180 +++++++++++++++++++++ tasks/oop/counter/index.php | 21 +++ tasks/oop/fieldValidator/index.php | 8 +- tasks/oop/formValidator/index.php | 10 +- tasks/oop/hyperlink/index.php | 8 +- tasks/oop/person/index.php | 8 +- tasks/random/arrays.php | 4 +- 28 files changed, 297 insertions(+), 89 deletions(-) create mode 100644 base/autoloader.php create mode 100644 tasks/oop/classes/counter/counter.php create mode 100644 tasks/oop/counter/index.php diff --git a/base/autoloader.php b/base/autoloader.php new file mode 100644 index 0000000..e39cf24 --- /dev/null +++ b/base/autoloader.php @@ -0,0 +1,7 @@ + - + Colour Selector @@ -10,8 +10,8 @@ - + Theme Switcher @@ -10,8 +10,8 @@ - + Counter DB Tester @@ -10,13 +10,13 @@ This is a dummy test for the database Counter - + \ No newline at end of file diff --git a/tasks/calc/index.php b/tasks/calc/index.php index 857c615..e43446a 100644 --- a/tasks/calc/index.php +++ b/tasks/calc/index.php @@ -2,7 +2,7 @@ - + Calculator @@ -11,8 +11,8 @@ - + Counter @@ -10,8 +10,8 @@ - + Functions - + Functions 2 - + Guest Book @@ -13,10 +13,10 @@ - + Guest Book DB @@ -15,14 +15,14 @@ - + \ No newline at end of file diff --git a/tasks/guestBookDB/login.php b/tasks/guestBookDB/login.php index a4bd28f..50d7adc 100644 --- a/tasks/guestBookDB/login.php +++ b/tasks/guestBookDB/login.php @@ -2,7 +2,7 @@ - + Guest Book DB - Login @@ -12,9 +12,9 @@ - + Guest Book DB - Register @@ -11,9 +11,9 @@ - + @@ -11,12 +11,12 @@ db = DB::openConnection(); + + if (empty($siteID)) { + $this->siteID = substr($_SERVER["REQUEST_URI"], strlen($_SERVER["REQUEST_URI"]) - 256); + } else { + $this->siteID = $siteID; + } + } + + /* + * Render Component + */ + + public function render() + { + $this->cleanupIPs(); + $hasVisited = $this->hasVisited(); + if (!$hasVisited) { + $this->addHit(); + $this->addIp(); + } + + $ret = + << + .span-v3Yq3 { + display: inline-block; + padding: 6px 6px 4px; + border-radius: 3px; + background: #cccccc; + margin-right: -2px; + } + + .center-v3Yq3 { + display: flex; + justify-content: center; + gap: 5px; + width: 50%; + margin: 20px auto; + } + + .message-v3Yq3 { + color: #aaaaaa; + font-size: 12px; + text-align: center; + } + +
+
+ EOT; + foreach (mb_str_split(($this->getHits()) . "") as $digit) { + $ret .= + <<$digit + EOT; + } + $ret .= + << +
+ Have visited this site. + EOT; + if (!$hasVisited) { + $ret .= + << + This is your first visit tody + EOT; + } + $ret .= + << +
+ EOT; + + echo $ret; + } + + /* + * Implementation + */ + + private function getHits() + { + $query = " + SELECT + hits + FROM + Counter + WHERE + siteID LIKE :SID"; + + $stmt = $this->db->prepare($query); + $stmt->bindValue(":SID", $this->siteID); + $stmt->execute(); + $hits = $stmt->fetch(PDO::FETCH_COLUMN); + return $hits !== false ? $hits : 0; + } + + private function addHit() + { + $query = " + INSERT INTO + Counter(siteID, hits) + VALUES + (:SID, 1) + ON DUPLICATE KEY UPDATE + hits = hits + 1"; + + $stmt = $this->db->prepare($query); + $stmt->bindValue(":SID", $this->siteID); + $stmt->execute(); + } + + private function addIP() + { + $query = " + INSERT INTO + CounterIP(siteID, ip, timestamp) + VALUES + (:SID, :IP, NOW())"; + + $stmt = $this->db->prepare($query); + $stmt->bindValue(":SID", $this->siteID); + $stmt->bindValue(":IP", $_SERVER['REMOTE_ADDR']); + $stmt->execute(); + } + + private function cleanupIPs() + { + $query = " + DELETE FROM + CounterIP + WHERE + TIMESTAMPDIFF(MINUTE, timestamp, NOW()) > 1"; + + $stmt = $this->db->prepare($query); + $stmt->execute(); + } + + private function hasVisited() + { + $query = " + SELECT + COUNT(*) + FROM + CounterIP + WHERE + siteID = :SID + AND + ip = :IP + AND + TIMESTAMPDIFF(MINUTE, timestamp, NOW()) <= 1"; + + $stmt = $this->db->prepare($query); + $stmt->bindValue(":SID", $this->siteID); + $stmt->bindValue(":IP", $_SERVER['REMOTE_ADDR']); + $stmt->execute(); + $cnt = $stmt->fetch(PDO::FETCH_COLUMN); + return $cnt > 0; + } +} diff --git a/tasks/oop/counter/index.php b/tasks/oop/counter/index.php new file mode 100644 index 0000000..f881c39 --- /dev/null +++ b/tasks/oop/counter/index.php @@ -0,0 +1,21 @@ + + + + + + + FieldValidator Test + + + + render(); + ?> + + + \ No newline at end of file diff --git a/tasks/oop/fieldValidator/index.php b/tasks/oop/fieldValidator/index.php index 6c838f8..4ba0720 100644 --- a/tasks/oop/fieldValidator/index.php +++ b/tasks/oop/fieldValidator/index.php @@ -2,19 +2,19 @@ - + FieldValidator Test - + FormValidator Test - + Person Test - + Person Test