public static function initDB() { if (!class_exists('sqliteDB')) { main::log('ERROR', 'sqliteDB class not loaded', true); } if (!is_file(wm_db_file)) { if (!fclose(fopen(wm_db_file, 'w'))) { main::log('ERROR', sprintf('can\' create db file %s', wm_db_file)); } } $db = new sqliteDB(wm_db_file); $db_connect = $db->connect(); if (!($db_connect['status'] == 'OK')) { main::log('ERROR', 'database connection failed', true); } $query = "CREATE TABLE IF NOT EXISTS 'sites' (\n `id`\tINTEGER PRIMARY KEY AUTOINCREMENT,\n `hostname`\tTEXT UNIQUE,\n `template_id`\tINTEGER,\n `template_data`\tTEXT,\n `pinged`\tINTEGER DEFAULT '0'\n );"; $db_query = $db->query($query); if ($db_query['status'] != 'OK') { main::log('ERROR', $db_query['message'], true); } $query = "CREATE TABLE IF NOT EXISTS `templates` (\n `id`\tINTEGER PRIMARY KEY AUTOINCREMENT,\n `name`\tTEXT,\n `path`\tTEXT\n );"; $db_query = $db->query($query); if ($db_query['status'] != 'OK') { main::log('ERROR', $db_query['message'], true); } }
private function hostData($hostname) { $db = new sqliteDB(wm_db_file); $db_connect = $db->connect(); if (!($db_connect['status'] == 'OK')) { return $this->response('ERROR', 'database connection failed'); } $query = "SELECT a.id as site_id, a.hostname, a.template_data,\n b.id as template_id, b.name as template_name, b.path as template_path\n FROM sites a\n LEFT JOIN templates b\n ON b.id = a.template_id\n WHERE hostname = '" . $hostname . "'"; $db_query = $db->query($query); if ($db_query['status'] != 'OK') { return false; } $rows = utils::dbObject($db_query['data']); if (count($rows) == 0) { return false; } $data = $rows[0]; if (!($data['template_data'] = json_decode($data['template_data'], true))) { return false; } return ['request' => ['site_id' => $data['site_id'], 'hostname' => $data['hostname']], 'template' => ['id' => $data['template_id'], 'path' => wm_templates_path . $data['template_path'], 'name' => $data['template_name'], 'data' => $data['template_data']]]; }