1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
<?php
class Modeldb extends Model
{
/** @var PDO */
protected $bdd;
protected $arttable;
protected $database;
protected $artstore;
public function __construct() {
//$this->setbdd();
//self::setdb();
$this->dbinit();
}
public function dbinit()
{
$this->database = new Dbengine(__DIR__ . '/../../w_database');
$this->artstore = $this->database->store(Config::arttable());
}
public function setbdd()
{
$caught = true;
try {
$this->bdd = new PDO('mysql:host=' . Config::host() . ';dbname=' . Config::dbname() . ';charset=utf8', Config::user(), Config::password(), array(PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT));
//$this->bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
$caught = false;
echo '<h1>Error 500, database offline</h1>';
if ($this->session() >= self::EDITOR) {
echo '<p>Error : ' . $e->getMessage() . '</p>';
if ($this->session() == self::ADMIN) {
echo '<p>Go to the <a href="?aff=admin">Admin Panel</a> to edit your database credentials</p>';
} else {
echo '<p>Logout and and come back with an <strong>admin password</strong> to edit the database connexions settings.</p>';
}
} else {
echo '<p><a href=".">Homepage for admin login</a> (connect on the top right side)</p>';
}
exit;
}
return $caught;
}
public static function setdb()
{
$caught = true;
try {
self::$db = new PDO('mysql:host=' . Config::host() . ';dbname=' . Config::dbname() . ';charset=utf8', Config::user(), Config::password(), array(PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT));
//$this->bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
$caught = false;
echo '<h1>Error 500, database offline</h1>';
if ($this->session() >= self::EDITOR) {
echo '<p>Error : ' . $e->getMessage() . '</p>';
if ($this->session() == self::ADMIN) {
echo '<p>Go to the <a href="?aff=admin">Admin Panel</a> to edit your database credentials</p>';
} else {
echo '<p>Logout and and come back with an <strong>admin password</strong> to edit the database connexions settings.</p>';
}
} else {
echo '<p><a href=".">Homepage for admin login</a> (connect on the top right side)</p>';
}
exit;
}
return $caught;
}
public function settable(Config $config)
{
if (!empty(Config::arttable())) {
$this->arttable = Config::arttable();
} else {
echo '<h1>Table Error</h1>';
if ($this->session() >= self::EDITOR) {
if ($this->session() == self::ADMIN) {
echo '<p>Go to the <a href="?aff=admin">Admin Panel</a> to select or add an Article table</p>';
} else {
echo '<p>Logout and and come back with an <strong>admin password</strong> to edit table settings.</p>';
}
} else {
echo '<p><a href=".">Homepage for admin login</a> (connect on the top right side)</p>';
}
$caught = false;
exit;
}
}
public function bddinit(Config $config)
{
$test = $this->setbdd($config);
if ($test) {
$this->settable($config);
}
}
public function tableexist($dbname, $tablename)
{
$req = $this->bdd->prepare('SELECT COUNT(*)
FROM information_schema.tables
WHERE table_schema = :dbname AND
table_name like :tablename');
$req->execute(array(
'dbname' => $dbname,
'tablename' => $tablename
));
$donnees = $req->fetch(PDO::FETCH_ASSOC);
$req->closeCursor();
$exist = intval($donnees['COUNT(*)']);
return $exist;
}
public function tablelist($dbname)
{
$request = 'SHOW TABLES IN ' . $dbname;
$req = $this->bdd->query($request);
$donnees = $req->fetchAll(PDO::FETCH_ASSOC);
$req->closeCursor();
$arttables = [];
foreach ($donnees as $table) {
$arttables[] = $table['Tables_in_' . $dbname];
}
return $arttables;
}
public function tableduplicate($dbname, $arttable, $tablename)
{
$arttable = strip_tags($arttable);
$tablename = str_clean($tablename);
if ($this->tableexist($dbname, $arttable) && !$this->tableexist($dbname, $tablename)) {
$duplicate = " CREATE TABLE `$tablename` LIKE `$arttable`;";
$alter = "ALTER TABLE `$tablename` ADD PRIMARY KEY (`id`);";
$insert = "INSERT `$tablename` SELECT * FROM `$arttable`;";
$req = $this->bdd->query($duplicate . $alter . $insert);
return 'tableduplicated';
} else {
return 'tablealreadyexist';
}
}
}
|