Merge pull request #3 from LeOSW42/copilot/create-database-file-connection

Centralize PostgreSQL connection setup across models
This commit is contained in:
2026-01-24 14:03:54 +01:00
committed by GitHub
7 changed files with 66 additions and 76 deletions

21
includes/database.php Normal file
View File

@@ -0,0 +1,21 @@
<?php
namespace Kabano;
function sql_connect() {
global $config;
$connection = "host=".$config['SQL_host']
." dbname=".$config['SQL_db']
." user=".$config['SQL_user']
." password=".$config['SQL_pass'];
$con = pg_connect($connection);
if (!$con) {
$error = error_get_last();
$message = $error && isset($error['message']) ? $error['message'] : "unknown error";
die("Could not connect to server: ".$message."\n");
}
return $con;
}

View File

@@ -11,6 +11,7 @@ namespace Kabano;
**********************************************************/ **********************************************************/
require_once($config['third_folder']."Md/MarkdownExtra.inc.php"); require_once($config['third_folder']."Md/MarkdownExtra.inc.php");
require_once($config['includes_folder']."database.php");
class BlogArticle class BlogArticle
{ {
@@ -52,8 +53,7 @@ class BlogArticle
public function checkPermalink($permalink, $withArchive=0, $elementNb=0) { public function checkPermalink($permalink, $withArchive=0, $elementNb=0) {
global $config; global $config;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "SELECT content_versions.id AS version_id, * FROM contents INNER JOIN content_locales ON contents.id = content_locales.content_id INNER JOIN content_versions ON content_locales.id = content_versions.locale_id WHERE permalink=$1 AND type='blog'"; $query = "SELECT content_versions.id AS version_id, * FROM contents INNER JOIN content_locales ON contents.id = content_locales.content_id INNER JOIN content_versions ON content_locales.id = content_versions.locale_id WHERE permalink=$1 AND type='blog'";
if($withArchive==0) { if($withArchive==0) {
@@ -150,8 +150,7 @@ class BlogArticle
$this->version++; $this->version++;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
pg_query($con, "BEGIN"); pg_query($con, "BEGIN");
@@ -205,8 +204,7 @@ class BlogArticle
global $config; global $config;
global $user; global $user;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "UPDATE contents SET is_public=FALSE WHERE permalink=$1 AND type='blog'"; $query = "UPDATE contents SET is_public=FALSE WHERE permalink=$1 AND type='blog'";
@@ -230,8 +228,7 @@ class BlogArticle
global $config; global $config;
global $user; global $user;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "UPDATE contents SET is_public=TRUE WHERE permalink=$1 AND type='blog'"; $query = "UPDATE contents SET is_public=TRUE WHERE permalink=$1 AND type='blog'";
@@ -255,8 +252,7 @@ class BlogArticle
global $config; global $config;
global $user; global $user;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
pg_query($con, "BEGIN"); pg_query($con, "BEGIN");
@@ -346,8 +342,7 @@ class BlogArticles
public function listArticles($first, $count, $archive=0) { public function listArticles($first, $count, $archive=0) {
global $config; global $config;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "SELECT content_versions.id AS version_id, * FROM contents INNER JOIN content_locales ON contents.id = content_locales.content_id INNER JOIN content_versions ON content_locales.id = content_versions.locale_id WHERE is_archive=FALSE "; $query = "SELECT content_versions.id AS version_id, * FROM contents INNER JOIN content_locales ON contents.id = content_locales.content_id INNER JOIN content_versions ON content_locales.id = content_versions.locale_id WHERE is_archive=FALSE ";
if ($archive != 1) if ($archive != 1)
@@ -375,8 +370,7 @@ class BlogArticles
public function number($archive=0) { public function number($archive=0) {
global $config; global $config;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "SELECT content_versions.id AS version_id, * FROM contents INNER JOIN content_locales ON contents.id = content_locales.content_id INNER JOIN content_versions ON content_locales.id = content_versions.locale_id WHERE is_archive=FALSE "; $query = "SELECT content_versions.id AS version_id, * FROM contents INNER JOIN content_locales ON contents.id = content_locales.content_id INNER JOIN content_versions ON content_locales.id = content_versions.locale_id WHERE is_archive=FALSE ";
if ($archive == 1) if ($archive == 1)
@@ -399,8 +393,7 @@ class BlogArticles
public function getHistory($url) { public function getHistory($url) {
global $config; global $config;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "SELECT content_versions.id AS version_id, * FROM contents INNER JOIN content_locales ON contents.id = content_locales.content_id INNER JOIN content_versions ON content_locales.id = content_versions.locale_id WHERE permalink=$1 AND type='blog' ORDER BY update_date DESC"; $query = "SELECT content_versions.id AS version_id, * FROM contents INNER JOIN content_locales ON contents.id = content_locales.content_id INNER JOIN content_versions ON content_locales.id = content_versions.locale_id WHERE permalink=$1 AND type='blog' ORDER BY update_date DESC";

View File

@@ -11,6 +11,7 @@ namespace Kabano;
**********************************************************/ **********************************************************/
require_once($config['third_folder']."Md/MarkdownExtra.inc.php"); require_once($config['third_folder']."Md/MarkdownExtra.inc.php");
require_once($config['includes_folder']."database.php");
class Comment class Comment
{ {
@@ -34,8 +35,7 @@ class Comment
public function checkID($id) { public function checkID($id) {
global $config; global $config;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "SELECT * FROM content_comments WHERE id=$1"; $query = "SELECT * FROM content_comments WHERE id=$1";
@@ -102,8 +102,7 @@ class Comment
public function insert() { public function insert() {
global $config; global $config;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "INSERT INTO content_comments (version, creation_date, update_date, author, is_public, is_archive, content, comment, locale) VALUES $query = "INSERT INTO content_comments (version, creation_date, update_date, author, is_public, is_archive, content, comment, locale) VALUES
('0', $1, $2, $3, TRUE, FALSE, $4, $5, $6) RETURNING id"; ('0', $1, $2, $3, TRUE, FALSE, $4, $5, $6) RETURNING id";
@@ -125,8 +124,7 @@ class Comment
global $config; global $config;
global $user; global $user;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "UPDATE content_comments SET is_public = FALSE WHERE id = $1"; $query = "UPDATE content_comments SET is_public = FALSE WHERE id = $1";
@@ -150,8 +148,7 @@ class Comment
global $config; global $config;
global $user; global $user;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "UPDATE content_comments SET is_public = TRUE WHERE id = $1"; $query = "UPDATE content_comments SET is_public = TRUE WHERE id = $1";
@@ -204,8 +201,7 @@ class Comments
public function listComments($id, $archive=0) { public function listComments($id, $archive=0) {
global $config; global $config;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "SELECT * FROM content_comments WHERE content = $1 "; $query = "SELECT * FROM content_comments WHERE content = $1 ";
if ($archive == 0) if ($archive == 0)

View File

@@ -10,6 +10,8 @@ namespace Kabano;
*********************************************************** ***********************************************************
**********************************************************/ **********************************************************/
require_once($config['includes_folder']."database.php");
class Locale class Locale
{ {
public $name = 0; public $name = 0;
@@ -22,8 +24,7 @@ class Locale
public function checkName($name) { public function checkName($name) {
global $config; global $config;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "SELECT * FROM locales WHERE name=$1"; $query = "SELECT * FROM locales WHERE name=$1";
@@ -83,8 +84,7 @@ class Locales
public function getAll() { public function getAll() {
global $config; global $config;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "SELECT * FROM locales"; $query = "SELECT * FROM locales";

View File

@@ -12,6 +12,7 @@ namespace Kabano;
require_once($config['third_folder']."Md/MarkdownExtra.inc.php"); require_once($config['third_folder']."Md/MarkdownExtra.inc.php");
require_once($config['includes_folder']."poi_types.struct.php"); require_once($config['includes_folder']."poi_types.struct.php");
require_once($config['includes_folder']."database.php");
class Poi class Poi
{ {
@@ -59,8 +60,7 @@ class Poi
public function checkPermalink($permalink, $withArchive=0, $elementNb=0) { public function checkPermalink($permalink, $withArchive=0, $elementNb=0) {
global $config; global $config;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "SELECT $query = "SELECT
content_versions.id AS version_id, content_versions.id AS version_id,
@@ -214,8 +214,7 @@ class Poi
global $config; global $config;
global $user; global $user;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
pg_query($con, "BEGIN"); pg_query($con, "BEGIN");
@@ -288,8 +287,7 @@ class Poi
$this->version++; $this->version++;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
pg_query($con, "BEGIN"); pg_query($con, "BEGIN");
@@ -344,8 +342,7 @@ class Poi
global $config; global $config;
global $user; global $user;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "UPDATE contents SET is_public = FALSE WHERE id = $1"; $query = "UPDATE contents SET is_public = FALSE WHERE id = $1";
@@ -368,8 +365,7 @@ class Poi
global $config; global $config;
global $user; global $user;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "UPDATE contents SET is_public = TRUE WHERE id = $1"; $query = "UPDATE contents SET is_public = TRUE WHERE id = $1";
@@ -394,8 +390,7 @@ class Pois
public function listPois($archive=0) { public function listPois($archive=0) {
global $config; global $config;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "SELECT $query = "SELECT
content_versions.id AS version_id, content_versions.id AS version_id,
@@ -458,8 +453,7 @@ class Pois
public function getHistory($permalink) { public function getHistory($permalink) {
global $config; global $config;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "SELECT $query = "SELECT
content_versions.id AS version_id, content_versions.id AS version_id,

View File

@@ -11,6 +11,7 @@ namespace Kabano;
**********************************************************/ **********************************************************/
require_once($config['models_folder']."d.locales.php"); require_once($config['models_folder']."d.locales.php");
require_once($config['includes_folder']."database.php");
// This array is related to the defined SQL enum, do not touch. // This array is related to the defined SQL enum, do not touch.
$ranks = array( $ranks = array(
@@ -51,8 +52,7 @@ class User
public function checkID($id) { public function checkID($id) {
global $config; global $config;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "SELECT * FROM users WHERE id=$1"; $query = "SELECT * FROM users WHERE id=$1";
@@ -79,8 +79,7 @@ class User
public function login($login, $pass) { public function login($login, $pass) {
global $config; global $config;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "SELECT * FROM users WHERE name=$1 AND password=$2"; $query = "SELECT * FROM users WHERE name=$1 AND password=$2";
@@ -184,8 +183,7 @@ class User
public function availableName() { public function availableName() {
global $config; global $config;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "SELECT * FROM users WHERE lower(name)=$1"; $query = "SELECT * FROM users WHERE lower(name)=$1";
@@ -214,8 +212,7 @@ class User
public function availableMail() { public function availableMail() {
global $config; global $config;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "SELECT * FROM users WHERE lower(email)=$1"; $query = "SELECT * FROM users WHERE lower(email)=$1";
@@ -252,8 +249,7 @@ class User
$this->locale = "fr_FR"; $this->locale = "fr_FR";
$this->timezone = "Europe/Paris"; $this->timezone = "Europe/Paris";
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "INSERT INTO users (name, version, email, password, website, is_avatar_present, is_archive, rank, locale, timezone, visit_date, register_date) VALUES $query = "INSERT INTO users (name, version, email, password, website, is_avatar_present, is_archive, rank, locale, timezone, visit_date, register_date) VALUES
($1, '0', $2, $3, $4, FALSE, FALSE, 'registered', $5, $6, $7, $8)"; ($1, '0', $2, $3, $4, FALSE, FALSE, 'registered', $5, $6, $7, $8)";
@@ -278,8 +274,7 @@ class User
$this->website = "http://".$this->website; $this->website = "http://".$this->website;
$this->version++; $this->version++;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
if($this->password=='') { if($this->password=='') {
$query = "UPDATE users SET version = $1, name = $2, is_avatar_present = $3, locale = $4, rank = $5, email = $6, website = $7, timezone = $8 WHERE id = $9"; $query = "UPDATE users SET version = $1, name = $2, is_avatar_present = $3, locale = $4, rank = $5, email = $6, website = $7, timezone = $8 WHERE id = $9";
@@ -313,8 +308,7 @@ class User
$newPass = randomPassword(); $newPass = randomPassword();
$this->password = sha1($newPass); $this->password = sha1($newPass);
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "UPDATE users SET password = $1 WHERE email = $2"; $query = "UPDATE users SET password = $1 WHERE email = $2";
@@ -355,8 +349,7 @@ class User
$this->visit_date = date('r'); $this->visit_date = date('r');
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "UPDATE users SET visit_date = $1 WHERE id = $2"; $query = "UPDATE users SET visit_date = $1 WHERE id = $2";
@@ -433,8 +426,7 @@ class Users
public function number() { public function number() {
global $config; global $config;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "SELECT id FROM users"; $query = "SELECT id FROM users";
@@ -454,8 +446,7 @@ class Users
public function list_users($first, $count, $orderby = "id", $order = "ASC") { public function list_users($first, $count, $orderby = "id", $order = "ASC") {
global $config; global $config;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$orders=array("id","name","lastlogin","registered","website","role"); $orders=array("id","name","lastlogin","registered","website","role");
$key=array_search($orderby,$orders); $key=array_search($orderby,$orders);

View File

@@ -11,6 +11,7 @@ namespace Kabano;
**********************************************************/ **********************************************************/
require_once($config['third_folder']."Md/MarkdownExtra.inc.php"); require_once($config['third_folder']."Md/MarkdownExtra.inc.php");
require_once($config['includes_folder']."database.php");
class WikiPage class WikiPage
{ {
@@ -51,8 +52,7 @@ class WikiPage
public function checkPermalink($permalink, $withArchive=0, $elementNb=0) { public function checkPermalink($permalink, $withArchive=0, $elementNb=0) {
global $config; global $config;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "SELECT content_versions.id AS version_id, * FROM contents INNER JOIN content_locales ON contents.id = content_locales.content_id INNER JOIN content_versions ON content_locales.id = content_versions.locale_id WHERE permalink=$1 AND type='wiki'"; $query = "SELECT content_versions.id AS version_id, * FROM contents INNER JOIN content_locales ON contents.id = content_locales.content_id INNER JOIN content_versions ON content_locales.id = content_versions.locale_id WHERE permalink=$1 AND type='wiki'";
if($withArchive==0) { if($withArchive==0) {
@@ -149,8 +149,7 @@ class WikiPage
$this->version++; $this->version++;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
pg_query($con, "BEGIN"); pg_query($con, "BEGIN");
@@ -198,8 +197,7 @@ class WikiPage
global $config; global $config;
global $user; global $user;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "UPDATE contents SET is_public=FALSE WHERE permalink=$1 AND type='wiki'"; $query = "UPDATE contents SET is_public=FALSE WHERE permalink=$1 AND type='wiki'";
@@ -223,8 +221,7 @@ class WikiPage
global $config; global $config;
global $user; global $user;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "UPDATE contents SET is_public=TRUE WHERE permalink=$1 AND type='wiki'"; $query = "UPDATE contents SET is_public=TRUE WHERE permalink=$1 AND type='wiki'";
@@ -248,8 +245,7 @@ class WikiPage
global $config; global $config;
global $user; global $user;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
pg_query($con, "BEGIN"); pg_query($con, "BEGIN");
@@ -330,8 +326,7 @@ class WikiPages
public function getHistory($url) { public function getHistory($url) {
global $config; global $config;
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $con = sql_connect();
or die ("Could not connect to server\n");
$query = "SELECT content_versions.id AS version_id, * FROM contents INNER JOIN content_locales ON contents.id = content_locales.content_id INNER JOIN content_versions ON content_locales.id = content_versions.locale_id WHERE permalink=$1 AND type='wiki' ORDER BY update_date DESC"; $query = "SELECT content_versions.id AS version_id, * FROM contents INNER JOIN content_locales ON contents.id = content_locales.content_id INNER JOIN content_versions ON content_locales.id = content_versions.locale_id WHERE permalink=$1 AND type='wiki' ORDER BY update_date DESC";