Merge pull request #3 from LeOSW42/copilot/create-database-file-connection
Centralize PostgreSQL connection setup across models
This commit is contained in:
21
includes/database.php
Normal file
21
includes/database.php
Normal 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;
|
||||
}
|
||||
@@ -11,6 +11,7 @@ namespace Kabano;
|
||||
**********************************************************/
|
||||
|
||||
require_once($config['third_folder']."Md/MarkdownExtra.inc.php");
|
||||
require_once($config['includes_folder']."database.php");
|
||||
|
||||
class BlogArticle
|
||||
{
|
||||
@@ -52,8 +53,7 @@ class BlogArticle
|
||||
public function checkPermalink($permalink, $withArchive=0, $elementNb=0) {
|
||||
global $config;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$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) {
|
||||
@@ -150,8 +150,7 @@ class BlogArticle
|
||||
|
||||
$this->version++;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
pg_query($con, "BEGIN");
|
||||
|
||||
@@ -205,8 +204,7 @@ class BlogArticle
|
||||
global $config;
|
||||
global $user;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "UPDATE contents SET is_public=FALSE WHERE permalink=$1 AND type='blog'";
|
||||
|
||||
@@ -230,8 +228,7 @@ class BlogArticle
|
||||
global $config;
|
||||
global $user;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "UPDATE contents SET is_public=TRUE WHERE permalink=$1 AND type='blog'";
|
||||
|
||||
@@ -255,8 +252,7 @@ class BlogArticle
|
||||
global $config;
|
||||
global $user;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
pg_query($con, "BEGIN");
|
||||
|
||||
@@ -346,8 +342,7 @@ class BlogArticles
|
||||
public function listArticles($first, $count, $archive=0) {
|
||||
global $config;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$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)
|
||||
@@ -375,8 +370,7 @@ class BlogArticles
|
||||
public function number($archive=0) {
|
||||
global $config;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$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)
|
||||
@@ -399,8 +393,7 @@ class BlogArticles
|
||||
public function getHistory($url) {
|
||||
global $config;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$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";
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace Kabano;
|
||||
**********************************************************/
|
||||
|
||||
require_once($config['third_folder']."Md/MarkdownExtra.inc.php");
|
||||
require_once($config['includes_folder']."database.php");
|
||||
|
||||
class Comment
|
||||
{
|
||||
@@ -34,8 +35,7 @@ class Comment
|
||||
public function checkID($id) {
|
||||
global $config;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "SELECT * FROM content_comments WHERE id=$1";
|
||||
|
||||
@@ -102,8 +102,7 @@ class Comment
|
||||
public function insert() {
|
||||
global $config;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$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";
|
||||
@@ -125,8 +124,7 @@ class Comment
|
||||
global $config;
|
||||
global $user;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "UPDATE content_comments SET is_public = FALSE WHERE id = $1";
|
||||
|
||||
@@ -150,8 +148,7 @@ class Comment
|
||||
global $config;
|
||||
global $user;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "UPDATE content_comments SET is_public = TRUE WHERE id = $1";
|
||||
|
||||
@@ -204,8 +201,7 @@ class Comments
|
||||
public function listComments($id, $archive=0) {
|
||||
global $config;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "SELECT * FROM content_comments WHERE content = $1 ";
|
||||
if ($archive == 0)
|
||||
|
||||
@@ -10,6 +10,8 @@ namespace Kabano;
|
||||
***********************************************************
|
||||
**********************************************************/
|
||||
|
||||
require_once($config['includes_folder']."database.php");
|
||||
|
||||
class Locale
|
||||
{
|
||||
public $name = 0;
|
||||
@@ -22,8 +24,7 @@ class Locale
|
||||
public function checkName($name) {
|
||||
global $config;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "SELECT * FROM locales WHERE name=$1";
|
||||
|
||||
@@ -83,8 +84,7 @@ class Locales
|
||||
public function getAll() {
|
||||
global $config;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "SELECT * FROM locales";
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace Kabano;
|
||||
|
||||
require_once($config['third_folder']."Md/MarkdownExtra.inc.php");
|
||||
require_once($config['includes_folder']."poi_types.struct.php");
|
||||
require_once($config['includes_folder']."database.php");
|
||||
|
||||
class Poi
|
||||
{
|
||||
@@ -59,8 +60,7 @@ class Poi
|
||||
public function checkPermalink($permalink, $withArchive=0, $elementNb=0) {
|
||||
global $config;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "SELECT
|
||||
content_versions.id AS version_id,
|
||||
@@ -214,8 +214,7 @@ class Poi
|
||||
global $config;
|
||||
global $user;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
pg_query($con, "BEGIN");
|
||||
|
||||
@@ -288,8 +287,7 @@ class Poi
|
||||
|
||||
$this->version++;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
pg_query($con, "BEGIN");
|
||||
|
||||
@@ -344,8 +342,7 @@ class Poi
|
||||
global $config;
|
||||
global $user;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "UPDATE contents SET is_public = FALSE WHERE id = $1";
|
||||
|
||||
@@ -368,8 +365,7 @@ class Poi
|
||||
global $config;
|
||||
global $user;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "UPDATE contents SET is_public = TRUE WHERE id = $1";
|
||||
|
||||
@@ -394,8 +390,7 @@ class Pois
|
||||
public function listPois($archive=0) {
|
||||
global $config;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "SELECT
|
||||
content_versions.id AS version_id,
|
||||
@@ -458,8 +453,7 @@ class Pois
|
||||
public function getHistory($permalink) {
|
||||
global $config;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "SELECT
|
||||
content_versions.id AS version_id,
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace Kabano;
|
||||
**********************************************************/
|
||||
|
||||
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.
|
||||
$ranks = array(
|
||||
@@ -51,8 +52,7 @@ class User
|
||||
public function checkID($id) {
|
||||
global $config;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "SELECT * FROM users WHERE id=$1";
|
||||
|
||||
@@ -79,8 +79,7 @@ class User
|
||||
public function login($login, $pass) {
|
||||
global $config;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "SELECT * FROM users WHERE name=$1 AND password=$2";
|
||||
|
||||
@@ -184,8 +183,7 @@ class User
|
||||
public function availableName() {
|
||||
global $config;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "SELECT * FROM users WHERE lower(name)=$1";
|
||||
|
||||
@@ -214,8 +212,7 @@ class User
|
||||
public function availableMail() {
|
||||
global $config;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "SELECT * FROM users WHERE lower(email)=$1";
|
||||
|
||||
@@ -252,8 +249,7 @@ class User
|
||||
$this->locale = "fr_FR";
|
||||
$this->timezone = "Europe/Paris";
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$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)";
|
||||
@@ -278,8 +274,7 @@ class User
|
||||
$this->website = "http://".$this->website;
|
||||
$this->version++;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
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";
|
||||
@@ -313,8 +308,7 @@ class User
|
||||
$newPass = randomPassword();
|
||||
$this->password = sha1($newPass);
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "UPDATE users SET password = $1 WHERE email = $2";
|
||||
|
||||
@@ -355,8 +349,7 @@ class User
|
||||
|
||||
$this->visit_date = date('r');
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "UPDATE users SET visit_date = $1 WHERE id = $2";
|
||||
|
||||
@@ -433,8 +426,7 @@ class Users
|
||||
public function number() {
|
||||
global $config;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "SELECT id FROM users";
|
||||
|
||||
@@ -454,8 +446,7 @@ class Users
|
||||
public function list_users($first, $count, $orderby = "id", $order = "ASC") {
|
||||
global $config;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$orders=array("id","name","lastlogin","registered","website","role");
|
||||
$key=array_search($orderby,$orders);
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace Kabano;
|
||||
**********************************************************/
|
||||
|
||||
require_once($config['third_folder']."Md/MarkdownExtra.inc.php");
|
||||
require_once($config['includes_folder']."database.php");
|
||||
|
||||
class WikiPage
|
||||
{
|
||||
@@ -51,8 +52,7 @@ class WikiPage
|
||||
public function checkPermalink($permalink, $withArchive=0, $elementNb=0) {
|
||||
global $config;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$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) {
|
||||
@@ -149,8 +149,7 @@ class WikiPage
|
||||
|
||||
$this->version++;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
pg_query($con, "BEGIN");
|
||||
|
||||
@@ -198,8 +197,7 @@ class WikiPage
|
||||
global $config;
|
||||
global $user;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "UPDATE contents SET is_public=FALSE WHERE permalink=$1 AND type='wiki'";
|
||||
|
||||
@@ -223,8 +221,7 @@ class WikiPage
|
||||
global $config;
|
||||
global $user;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$query = "UPDATE contents SET is_public=TRUE WHERE permalink=$1 AND type='wiki'";
|
||||
|
||||
@@ -248,8 +245,7 @@ class WikiPage
|
||||
global $config;
|
||||
global $user;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
pg_query($con, "BEGIN");
|
||||
|
||||
@@ -330,8 +326,7 @@ class WikiPages
|
||||
public function getHistory($url) {
|
||||
global $config;
|
||||
|
||||
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
|
||||
or die ("Could not connect to server\n");
|
||||
$con = sql_connect();
|
||||
|
||||
$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";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user