Files
FreshRSS/app/Models/UserDAO.php
Alexandre Alapetite e3e5954394 PDO refactoring for code simplification (#2522)
* PDO refactor

* Automatic prefix when using the syntax `_tableName`
* Uniformity: MySQL is now PDO::ATTR_EMULATE_PREPARES = false just like SQLite and PostgreSQL, with consequences such as only one statement per query
* Use PDO methods exec(), query(), prepare() + execute() in a more efficient way
* Remove auto-update SQL code for versions older than FreshRSS 1.5 (3 years old)
* The name of the default category is set in PHP instead of in the DB (simplies SQL and allows changing the name according to the FreshRSS language)
* Rename `->bd` to `->pdo` (less of a frenshism, and more informative)
* Fix some requests, which were not compatible with MySQL prepared statements

* Whitespace

* Fix syntax for PostgreSQL sequences

+ MySQL install

* Minor formatting

* Fix lastInsertId for PostgreSQL

* Use PHP 5.6+ const

Take advantage of https://github.com/FreshRSS/FreshRSS/pull/2527
https://www.php.net/manual/en/migration56.new-features.php

* A bit of forgotten PHP 5.6 simplification for cURL

* Forgotten $s

* Mini fix custom user config

https://github.com/FreshRSS/FreshRSS/pull/2490/files#r326290346

* More work on install.php but not finished

* install.php working

* More cleaning of PDO in install

* Even more simplification

Take advantage of PDO->exec() to run multiple statements

* Disallow changing the name of the default category

https://github.com/FreshRSS/FreshRSS/pull/2522#discussion_r326967724
2019-09-29 16:22:50 +02:00

69 lines
2.0 KiB
PHP

<?php
class FreshRSS_UserDAO extends Minz_ModelPdo {
public function createUser($insertDefaultFeeds = false) {
require_once(APP_PATH . '/SQL/install.sql.' . $this->pdo->dbType() . '.php');
try {
$sql = SQL_CREATE_TABLES . SQL_CREATE_TABLE_ENTRYTMP . SQL_CREATE_TABLE_TAGS;
$ok = $this->pdo->exec($sql) !== false; //Note: Only exec() can take multiple statements safely.
if ($ok && $insertDefaultFeeds) {
$default_feeds = FreshRSS_Context::$system_conf->default_feeds;
$stm = $this->pdo->prepare(SQL_INSERT_FEED);
foreach ($default_feeds as $feed) {
$parameters = [
':url' => $feed['url'],
':name' => $feed['name'],
':website' => $feed['website'],
':description' => $feed['description'],
];
$ok &= ($stm && $stm->execute($parameters));
}
}
} catch (Exception $e) {
Minz_Log::error('Error while creating database for user: ' . $e->getMessage());
}
if ($ok) {
return true;
} else {
$info = empty($stm) ? array(2 => 'syntax error') : $stm->errorInfo();
Minz_Log::error(__METHOD__ . ' error: ' . $info[2]);
return false;
}
}
public function deleteUser() {
if (defined('STDERR')) {
fwrite(STDERR, 'Deleting SQL data for user “' . $this->current_user . "”…\n");
}
require_once(APP_PATH . '/SQL/install.sql.' . $this->pdo->dbType() . '.php');
$ok = $this->pdo->exec(SQL_DROP_TABLES) !== false;
if ($ok) {
return true;
} else {
$info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
Minz_Log::error(__METHOD__ . ' error: ' . $info[2]);
return false;
}
}
public static function exists($username) {
return is_dir(USERS_PATH . '/' . $username);
}
public static function touch($username = '') {
if (!FreshRSS_user_Controller::checkUsername($username)) {
$username = Minz_Session::param('currentUser', '_');
}
return touch(USERS_PATH . '/' . $username . '/config.php');
}
public static function mtime($username) {
return @filemtime(USERS_PATH . '/' . $username . '/config.php');
}
}