function setup_db() { switch (DB_DRIVER) { case 'sqlite': require_once __DIR__ . '/Schema/Sqlite.php'; $params = array('driver' => 'sqlite', 'filename' => DB_FILENAME); break; case 'mysql': require_once __DIR__ . '/Schema/Mysql.php'; $params = array('driver' => 'mysql', 'hostname' => DB_HOSTNAME, 'username' => DB_USERNAME, 'password' => DB_PASSWORD, 'database' => DB_NAME, 'charset' => 'utf8'); break; case 'postgres': require_once __DIR__ . '/Schema/Postgres.php'; $params = array('driver' => 'postgres', 'hostname' => DB_HOSTNAME, 'username' => DB_USERNAME, 'password' => DB_PASSWORD, 'database' => DB_NAME); break; default: die('Database driver not supported'); } $db = new Database($params); if ($db->schema()->check(Schema\VERSION)) { return $db; } else { $errors = $db->getLogMessages(); die('Unable to migrate database schema: <br/><br/><strong>' . (isset($errors[0]) ? $errors[0] : 'Unknown error') . '</strong>'); } }
/** * Execute database migrations * * @static * @access public * @throws RuntimeException * @param Database $db * @return bool */ public static function runMigrations(Database $db) { if (!$db->schema()->check(\Schema\VERSION)) { $messages = $db->getLogMessages(); throw new RuntimeException('Unable to run SQL migrations: ' . implode(', ', $messages) . ' (You may have to fix it manually)'); } return true; }
/** * Apply filter * * @access public * @return FilterInterface */ public function apply() { $task_ids = $this->db->table(TagModel::TABLE)->ilike(TagModel::TABLE . '.name', $this->value)->asc(TagModel::TABLE . '.project_id')->join(TaskTagModel::TABLE, 'tag_id', 'id')->findAllByColumn(TaskTagModel::TABLE . '.task_id'); if (empty($task_ids)) { $task_ids = array(-1); } $this->query->in(TaskModel::TABLE . '.id', $task_ids); return $this; }
/** * Build SQL condition for a given time range * * @access protected * @param string $start_time Start timestamp * @param string $end_time End timestamp * @param string $start_column Start column name * @param string $end_column End column name * @return string */ protected function getCalendarCondition($start_time, $end_time, $start_column, $end_column) { $start_column = $this->db->escapeIdentifier($start_column); $end_column = $this->db->escapeIdentifier($end_column); $conditions = array("({$start_column} >= '{$start_time}' AND {$start_column} <= '{$end_time}')", "({$start_column} <= '{$start_time}' AND {$end_column} >= '{$start_time}')", "({$start_column} <= '{$start_time}' AND ({$end_column} = '0' OR {$end_column} IS NULL))"); return $start_column . ' IS NOT NULL AND ' . $start_column . ' > 0 AND (' . implode(' OR ', $conditions) . ')'; }
/** * Build a select query * * @access public * @return string */ public function buildSelectQuery() { if (empty($this->sqlSelect)) { $this->columns = $this->db->escapeIdentifierList($this->columns); $this->sqlSelect = ($this->distinct ? 'DISTINCT ' : '') . (empty($this->columns) ? '*' : implode(', ', $this->columns)); } $this->groupBy = $this->db->escapeIdentifierList($this->groupBy); return trim(sprintf('SELECT %s FROM %s %s %s %s %s %s %s', $this->sqlSelect, $this->db->escapeIdentifier($this->name), implode(' ', $this->joins), $this->condition->build(), empty($this->groupBy) ? '' : 'GROUP BY ' . implode(', ', $this->groupBy), $this->sqlOrder, $this->sqlLimit, $this->sqlOffset)); }
/** * Save a record in the database * * @access public * @param string $table Table name * @param array $values Form values * @return boolean|integer */ public function persist($table, array $values) { return $this->db->transaction(function ($db) use($table, $values) { if (!$db->table($table)->save($values)) { return false; } return (int) $db->getConnection()->getLastId(); }); }
/** * Handle PDOException * * @access public * @param PDOException $e * @return bool * @throws SQLException */ public function handleSqlError(PDOException $e) { $this->cleanup(); $this->db->cancelTransaction(); $this->db->setLogMessage($e->getMessage()); if ($this->db->getDriver()->isDuplicateKeyError($e->getCode())) { return false; } throw new SQLException('SQL error' . ($this->logQueries ? ': ' . $e->getMessage() : '')); }
function auth() { if (!empty($_GET['database'])) { // Return unauthorized if the requested database could not be found if (!Model\Database\select($_GET['database'])) { return array('api_version' => 3, 'auth' => 0); } } $credentials = Database::getInstance('db')->hashtable('settings')->get('username', 'fever_token'); $api_key = md5($credentials['username'] . ':' . $credentials['fever_token']); $response = array('api_version' => 3, 'auth' => (int) (isset($_POST['api_key']) && strcasecmp($_POST['api_key'], $api_key) === 0), 'last_refreshed_on_time' => time()); return $response; }
/** * Migrate the schema to one version to another * * @access public * @param integer $current_version * @param integer $next_version * @return boolean */ public function migrateTo($current_version, $next_version) { try { $this->db->startTransaction(); $this->db->getDriver()->disableForeignKeys(); for ($i = $current_version + 1; $i <= $next_version; $i++) { $function_name = '\\Schema\\version_' . $i; if (function_exists($function_name)) { call_user_func($function_name, $this->db->getConnection()); } } $this->db->getDriver()->setSchemaVersion($i - 1); $this->db->getDriver()->enableForeignKeys(); $this->db->closeTransaction(); } catch (PDOException $e) { $this->db->setLogMessage($function_name . ' => ' . $e->getMessage()); $this->db->cancelTransaction(); $this->db->getDriver()->enableForeignKeys(); return false; } return true; }
function disable($feed_id) { return Database::getInstance('db')->table('feeds')->eq('id', $feed_id)->save(array('enabled' => 0)); }
function save(array $values) { // Update the password if needed if (!empty($values['password'])) { $values['password'] = password_hash($values['password'], PASSWORD_BCRYPT); } else { unset($values['password']); } unset($values['confirmation']); // If the user does not want content of feeds, remove it in previous ones if (isset($values['nocontent']) && (bool) $values['nocontent']) { Database::get('db')->table('items')->update(array('content' => '')); } if (Database::get('db')->hashtable('settings')->put($values)) { reload(); return true; } return false; }
function get_all_favicons() { if (Config\get('favicons') == 0) { return array(); } $result = Database::getInstance('db')->table('favicons')->columns('favicons_feeds.feed_id', 'favicons.type', 'favicons.hash')->join('favicons_feeds', 'favicon_id', 'id')->findAll(); $map = array(); foreach ($result as $row) { $map[$row['feed_id']] = array("type" => $row['type'], "hash" => $row['hash']); } return $map; }
/** * IS NOT NULL condition * * @access public * @param string $column */ public function notNull($column) { $this->addCondition($this->db->escapeIdentifier($column) . ' IS NOT NULL'); }
/** * Purge orphaned groups from database */ function purge_groups() { $groups = Database::getInstance('db')->table('groups')->join('feeds_groups', 'group_id', 'id')->isnull('feed_id')->findAllByColumn('id'); if (!empty($groups)) { Database::getInstance('db')->table('groups')->in('id', $groups)->remove(); } }
function download_content_id($item_id) { $item = get($item_id); $content = download_content_url($item['url']); if (!empty($content)) { if (!Config\get('nocontent')) { // Save content Database::getInstance('db')->table('items')->eq('id', $item['id'])->save(array('content' => $content)); } Config\write_debug(); return array('result' => true, 'content' => $content); } Config\write_debug(); return array('result' => false, 'content' => ''); }
/** * Get subquery * * @access protected * @return Table */ protected function getSubQuery() { $subquery = $this->db->table(Subtask::TABLE)->columns(Subtask::TABLE . '.user_id', Subtask::TABLE . '.task_id', User::TABLE . '.name', User::TABLE . '.username')->join(User::TABLE, 'id', 'user_id', Subtask::TABLE)->neq(Subtask::TABLE . '.status', Subtask::STATUS_DONE); return $this->applySubQueryFilter($subquery); }
Session\flash_error(t('Unable to update Miniflux, check the console for errors.')); } } Response\redirect('?action=config'); }); // Re-generate tokens Router\get_action('generate-tokens', function () { if (Model\Config\check_csrf(Request\param('csrf'))) { Model\Config\new_tokens(); } Response\redirect('?action=config'); }); // Optimize the database manually Router\get_action('optimize-db', function () { if (Model\Config\check_csrf(Request\param('csrf'))) { Database::get('db')->getConnection()->exec('VACUUM'); } Response\redirect('?action=database'); }); // Download the compressed database Router\get_action('download-db', function () { if (Model\Config\check_csrf(Request\param('csrf'))) { Response\force_download('db.sqlite.gz'); Response\binary(gzencode(file_get_contents(Model\Database\get_path()))); } }); // Display preferences page Router\get_action('config', function () { Response\html(Template\layout('config', array('errors' => array(), 'values' => Model\Config\get_all() + array('csrf' => Model\Config\generate_csrf()), 'languages' => Model\Config\get_languages(), 'timezones' => Model\Config\get_timezones(), 'autoflush_read_options' => Model\Config\get_autoflush_read_options(), 'autoflush_unread_options' => Model\Config\get_autoflush_unread_options(), 'paging_options' => Model\Config\get_paging_options(), 'theme_options' => Model\Config\get_themes(), 'sorting_options' => Model\Config\get_sorting_directions(), 'display_mode' => Model\Config\get_display_mode(), 'redirect_nothing_to_read_options' => Model\Config\get_nothing_to_read_redirections(), 'nb_unread_items' => Model\Item\count_by_status('unread'), 'menu' => 'config', 'title' => t('Preferences')))); }); // Update preferences
/** * Get subquery * * @access protected * @return Table */ protected function getSubQuery() { return $this->db->table(TaskLinkModel::TABLE)->columns(TaskLinkModel::TABLE . '.task_id', LinkModel::TABLE . '.label')->join(LinkModel::TABLE, 'id', 'link_id', TaskLinkModel::TABLE)->ilike(LinkModel::TABLE . '.label', $this->value); }
function getCredentials() { return Database::get('db')->hashtable('settings')->get('username', 'password'); }
protected function getTaskIdsWithGivenTag() { return $this->db->table(TagModel::TABLE)->ilike(TagModel::TABLE . '.name', $this->value)->asc(TagModel::TABLE . '.project_id')->join(TaskTagModel::TABLE, 'tag_id', 'id')->findAllByColumn(TaskTagModel::TABLE . '.task_id'); }
Session\flash_error(t('Unable to update Miniflux, check the console for errors.')); } } Response\redirect('?action=config'); }); // Re-generate tokens Router\get_action('generate-tokens', function () { if (Model\Config\check_csrf(Request\param('csrf'))) { Model\Config\new_tokens(); } Response\redirect('?action=config'); }); // Optimize the database manually Router\get_action('optimize-db', function () { if (Model\Config\check_csrf(Request\param('csrf'))) { Database::getInstance('db')->getConnection()->exec('VACUUM'); } Response\redirect('?action=database'); }); // Download the compressed database Router\get_action('download-db', function () { if (Model\Config\check_csrf(Request\param('csrf'))) { Response\force_download('db.sqlite.gz'); Response\binary(gzencode(file_get_contents(Model\Database\get_path()))); } }); // Display preferences page Router\get_action('config', function () { Response\html(Template\layout('config', array('errors' => array(), 'values' => Model\Config\get_all() + array('csrf' => Model\Config\generate_csrf()), 'languages' => Model\Config\get_languages(), 'timezones' => Model\Config\get_timezones(), 'autoflush_read_options' => Model\Config\get_autoflush_read_options(), 'autoflush_unread_options' => Model\Config\get_autoflush_unread_options(), 'paging_options' => Model\Config\get_paging_options(), 'theme_options' => Model\Config\get_themes(), 'sorting_options' => Model\Config\get_sorting_directions(), 'display_mode' => Model\Config\get_display_mode(), 'redirect_nothing_to_read_options' => Model\Config\get_nothing_to_read_redirections(), 'nb_unread_items' => Model\Item\count_by_status('unread'), 'menu' => 'config', 'title' => t('Preferences')))); }); // Update preferences
/** * Return a new sequence token and update the database * * @access public * @param string $token Session token * @return string */ function update($token) { $new_sequence = Config\generate_token(); Database::getInstance('db')->table(TABLE)->eq('token', $token)->update(array('sequence' => $new_sequence)); return $new_sequence; }