From eb938b344a3f54c24257f3639fcb349e119a9a20 Mon Sep 17 00:00:00 2001 From: Jean-Marc Pigeon Date: Wed, 19 Nov 2025 12:57:58 -0500 Subject: [PATCH] the generic database scan function )named probe) seems to be working --- www/gessql.php | 162 +++++++++++++++++++++++++++++++++++++++++++++++++ www/lvlusr.php | 29 +++++---- 2 files changed, 176 insertions(+), 15 deletions(-) diff --git a/www/gessql.php b/www/gessql.php index 1abc672..ca52def 100644 --- a/www/gessql.php +++ b/www/gessql.php @@ -8,6 +8,168 @@ include_once "subrou.php"; include_once "devsql.php"; +class probe { + private devsql $db; // devsql database instance + private string $table; // table name + + private array $where = []; // WHERE conditions + private array $params = []; // parameters for prepared statements + + private ?string $order = null; // ORDER BY clause + private ?int $limit = null; // LIMIT for pagination + private int $offset = 0; // OFFSET for pagination, internal only + + /** + * Constructor + */ + public function __construct(string $table,int $limit,int $offset) + { + $this->db = sql_connect(); + $this->table = $table; + $this->limit = $limit; + $this->offset = $offset; + } + + /** + * Add a CLOSE condition + */ + public function close(): void + { + $this->db->connection=NULL; + } + + /** + * Add a WHERE condition + */ + public function where(string $column, string $operator, $value): self + { + $param = ':' . $column . count($this->where); + $this->where[] = "$column $operator $param"; + $this->params[$param] = $value; + return $this; + } + + /** + * Set ORDER BY clause + */ + public function order(string $order): self + { + $this->order = $order; + return $this; + } + + /** + * Set LIMIT for pagination + */ + public function limit(int $n): self + { + $this->limit = $n; + return $this; + } + + /** + * Execute the query and return results + */ + public function get(): array + { + $sql = "SELECT * FROM {$this->table}"; + + if ($this->where) { + $sql .= " WHERE " . implode(' AND ', $this->where); + } + + if ($this->order) { + $sql .= " ORDER BY {$this->order}"; + } + + if ($this->limit !== null) { + $sql .= " LIMIT {$this->limit}"; + } + + if ($this->offset > 0) { + $sql .= " OFFSET {$this->offset}"; + } + + $stmt = $this->db->Select($sql, $this->params); + return $stmt->fetchAll(); + } + + /** + * Count total number of items matching the current WHERE conditions + */ + public function count(): int + { + $sql = "SELECT COUNT(*) AS cnt FROM {$this->table}"; + + if ($this->where) { + $sql .= " WHERE " . implode(' AND ', $this->where); + } + + $stmt = $this->db->Select($sql, $this->params); + $row = $stmt->fetch(); + return (int) $row['cnt']; + } + + /** ----------------------------- **/ + /** Pagination / navigation **/ + /** ----------------------------- **/ + + /** + * Move offset to next page + * Does NOT fetch data + */ + public function goNext(): int + { + if ($this->limit !== null) { + $this->offset += $this->limit; + } + return $this->offset; + } + + /** + * Move offset to previous page + * Does NOT fetch data + */ + public function goPrevious(): int + { + if ($this->limit !== null) { + $this->offset = max(0, $this->offset - $this->limit); + } + return $this->offset; + } + + /** + * Move offset to first page + * Does NOT fetch data + */ + public function goFirst(): int + { + $this->offset = 0; + return $this->offset; + } + + /** + * Move offset to last page + * Does NOT fetch data + */ + public function goLast(): int + { + if ($this->limit !== null) { + $total = $this->count(); + $this->offset = max(0, $total - $this->limit); + } + return $this->offset; + } + + /** + * Get current offset value + */ + public function getOffset(): int + { + return $this->offset; + } +} + //============================================================== // // Function to check if password is the right one diff --git a/www/lvlusr.php b/www/lvlusr.php index 799378a..230b58e 100644 --- a/www/lvlusr.php +++ b/www/lvlusr.php @@ -89,39 +89,38 @@ global $logname; $limit=20; $offset=0; $username=""; -$dbsql=sql_connect(); -if (isset($_POST['username'])) - $username=trim(($_POST['username'])); if (isset($_POST['limit'])) $limit=intval($_POST['limit']); if (isset($_POST['offset'])) $offset=intval($_POST['offset']); -$numrec=sql_getnumemail($dbsql,$username); +$rqst=NEW probe("emails",$limit,$offset); +if (isset($_POST['username'])) { + $username=trim(($_POST['username'])); + $rqst->where("email","like","%$username%"); + } +$numrec=$rqst->count(); if (isset($_POST['scanner'])) { $scanner=$_POST['scanner']; switch ($scanner) { case "golast" : - $offset=$numrec-$limit; + $offset=$rqst->goLast(); break; case "gonext" : - - $offset+=$limit; - if ($offset>($numrec-$limit)) - $offset=$numrec-$limit; + $offset=$rqst->goNext(); break; case "goprevious" : - $offset-=$limit; - if ($offset<0) - $offset=0; + $offset=$rqst->goPrevious(); break; case "gofirst" : - $offset=0; + $offset=$rqst->goFirst(); break; default : break; } } -$emails=sql_getemaillist($dbsql,$username,$limit,$offset); +$emails=$rqst->order("email asc") + ->limit($limit) + ->get(); $line=""; $count=$offset; foreach($emails as $email) { @@ -144,7 +143,7 @@ foreach($emails as $email) { $line=$line."$lock\r\n"; $line=$line.""; } -sql_close($dbsql); +$rqst->close(); $start=starthtml("directives"); $stop=endhtml(); -- 2.47.3