/** * Create, store, and return a token for long-term authentication * * @param int $userId * @return string (to store in a cookie, for example) */ public function createAuthToken(int $userId) : string { $f = $this->tableConfig['fields']['longterm']; $selector = \random_bytes(self::SELECTOR_BYTES); $validator = \random_bytes(self::VALIDATOR_BYTES); $this->db->insert($this->tableConfig['table']['longterm'], [$f['userid'] => $userId, $f['selector'] => Base64::encode($selector), $f['validator'] => CryptoUtil::hash($validator)]); return Base64::encode($selector . $validator); }
/** * Renders ReStructuredText * * @param string $string * @param bool $return * @output HTML * @return string */ function render_rst(string $string = '', bool $return = false) : string { static $rst = null; if (empty($rst)) { $rst = (new RSTParser())->setIncludePolicy(false); } $checksum = CryptoUtil::hash('ReStructuredText' . $string); $h1 = Binary::safeSubstr($checksum, 0, 2); $h2 = Binary::safeSubstr($checksum, 2, 2); $hash = Binary::safeSubstr($checksum, 4); $cacheDir = \implode('/', [ROOT, 'tmp', 'cache', 'rst', $h1, $h2]); if (\file_exists($cacheDir . '/' . $hash . '.txt')) { $output = \file_get_contents($cacheDir . '/' . $hash . '.txt'); } else { if (!\is_dir($cacheDir)) { \mkdir($cacheDir, 0775, true); } $output = (string) $rst->parse($string); // Cache for later \file_put_contents($cacheDir . '/' . $hash . '.txt', $output); \chmod($cacheDir . '/' . $hash . '.txt', 0664); } if ($return) { return $output; } echo $output; return ''; }