/** * Save some CSV data to a file, and create a quasi-$_FILES entry for it. * @param string $data * @return string|array */ private function saveDataFile($data) { $test_filename = Config::storageDirTmp('test') . '/' . uniqid() . '.csv'; file_put_contents($test_filename, $data); $uploaded = array('type' => 'text/csv', 'file' => $test_filename); return $uploaded; }
public function render() { // Generate the DOT source code, and write to a file. $dot = new \Tabulate\Template('erd/erd.twig'); $dot->tables = $this->tables; $dot->selectedTables = $this->selectedTables; $dotCode = $dot->render(); $tmpFilePath = Config::storageDirTmp('erd/' . uniqid()); $dotFile = $tmpFilePath . '/erd.dot'; $pngFile = $tmpFilePath . '/erd.png'; file_put_contents($dotFile, $dotCode); // Generate the image. $cmd = Config::dotCommand() . ' -Tpng -o' . escapeshellarg($pngFile) . ' ' . escapeshellarg($dotFile); $ds = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')); $pipes = false; $proc = proc_open($cmd, $ds, $pipes, Config::storageDirTmp('erd'), array()); fclose($pipes[0]); $out = stream_get_contents($pipes[1]); fclose($pipes[1]); $err = stream_get_contents($pipes[2]); fclose($pipes[2]); proc_close($proc); if (!empty($err)) { throw new \Exception("Error generating graph image. {$err}"); } // Send the image. header('Content-Type:image/png'); echo file_get_contents($pngFile); // Clean up. \Tabulate\File::rmdir($tmpFilePath); }
protected function redirect($route) { $url = \Tabulate\Config::baseUrl() . '/' . ltrim($route, '/ '); http_response_code(303); header("Location: {$url}"); exit(0); }
protected function installData(\Tabulate\DB\Database $db) { $this->write("Confirming existance of administrative user, group, and grant"); // Can't log changes without a user (admin, in this case). So we create a user manually. $pwd = password_hash('admin', PASSWORD_DEFAULT); $adminUserData = ['id' => Users::ADMIN, 'name' => 'Admin', 'email' => Config::siteEmail(), 'password' => $pwd]; $adminSql = "INSERT IGNORE INTO `users` SET `id`=:id, `name`=:name, `email`=:email, `password`=:password"; $db->query($adminSql, $adminUserData); // Then we want to create a second user (anon), but this time recording changes. The change-tracker needs to // know about permissions, so before creating the 2nd user that we need to grant permission to admin. // Permissions are granted to groups, not users, so we put admin in an admin group first (manually). $params2 = ['id' => Groups::ADMINISTRATORS, 'name' => 'Administrators']; $db->query("INSERT IGNORE INTO `groups` SET `id`=:id, `name`=:name", $params2); $params3 = ['user' => Users::ADMIN, 'group' => Groups::ADMINISTRATORS]; $db->query("INSERT IGNORE INTO `group_members` SET `user`=:user, `group`=:group", $params3); // Now we can grant everything (on everything) to the admin group. $db->query("INSERT IGNORE INTO `grants` SET `group`=:group", ['group' => Groups::ADMINISTRATORS]); // And finally 'reset' the DB so it knows about the above new records. $db->reset(); // Start tracking changes now that there's a user to attribute it to. $db->setCurrentUser(Users::ADMIN); $changeTracker = new \Tabulate\DB\ChangeTracker($db); $changeTracker->openChangeset('Installation', true); // Create remaining default users and groups. if (!$db->getTable('users')->getRecord(Users::ANON)) { $this->write("Inserting user 'Anonymous'"); $db->getTable('users')->saveRecord(['id' => Users::ANON, 'name' => 'Anonymous']); } if (!$db->getTable('groups', false)->getRecord(Groups::GENERAL_PUBLIC)) { $this->write("Inserting group 'General Public'"); $db->getTable('groups', false)->saveRecord(['id' => Groups::GENERAL_PUBLIC, 'name' => 'General Public']); } // Add Anon user to the General Public group. $groupMembers = $db->getTable('group_members', false); $groupMembers->addFilter('user', '=', Users::ANON); $groupMembers->addFilter('group', '=', Groups::GENERAL_PUBLIC); if ($groupMembers->getRecordCount() === 0) { $this->write("Adding user 'Anonymous' to group 'General Public'"); $groupMembers->saveRecord(['group' => Groups::GENERAL_PUBLIC, 'user' => Users::ANON]); } // Add first report (to list reports). if (0 == $db->query("SELECT COUNT(*) FROM `" . Reports::reportsTableName() . "`")->fetchColumn()) { // Create the default report, to list all reports. $templateString = "<dl>\n" . "{% for report in reports %}\n" . " <dt><a href='{{baseurl}}/reports/{{report.id}}'>{{report.title}}</a></dt>\n" . " <dd>{{report.description}}</dd>\n" . "{% endfor %}\n" . "</dl>"; $sql1 = "INSERT INTO `" . Reports::reportsTableName() . "` SET" . " id = " . Reports::DEFAULT_REPORT_ID . ", " . " title = 'Reports', " . " description = 'List of all Reports.'," . " template = :template;"; $db->query($sql1, ['template' => $templateString]); // And the query for the above report. $query = "SELECT * FROM " . Reports::reportsTableName(); $sql2 = "INSERT INTO `" . Reports::reportSourcesTableName() . "` SET " . " report = " . Reports::DEFAULT_REPORT_ID . "," . " name = 'reports'," . " query = :query;"; $db->query($sql2, ['query' => $query]); } // Finish up. $changeTracker->closeChangeset(); }
/** * Get a list of tables that the current user can read. * @return string[] The table names. */ public function getTableNames($checkGrants = true) { if (!$this->tableNames) { $this->tableNames = $this->query('SHOW TABLES')->fetchAll(); } $out = []; foreach ($this->tableNames as $row) { $tableName = $row->{'Tables_in_' . Config::databaseName()}; if (!$checkGrants || $this->checkGrant(Grants::READ, $tableName)) { $out[] = $tableName; } } return $out; }
/** * Get a fully-qualified URL to a Back End page for this table. * @param string $action Which action to use ('index', 'import', etc.). * @param string[] $extra_params Other query string parameters to add. * @param string $controller Which controller to use ('table', 'record', etc.). * @return string The full URL. */ public function getUrl($action = 'index', $extra_params = false, $controller = 'table') { $params = []; if (is_array($extra_params)) { $params = array_merge($_GET, $params, $extra_params); } $paramString = http_build_query($params); return Config::baseUrl() . '/table/' . $this->getName() . (!empty($paramString) ? "?{$paramString}" : ""); }
public function getUrl() { return Config::baseUrl() . '/record/' . $this->table->getName() . '/' . $this->getPrimaryKey(); }
protected function byline() { return 'Tabulate ' . Config::version(); }