decode($data); } /** * Handle proxying the data from an aliased URL. */ function processAliasGetRequest() { $alias_name = substr($_SERVER['PATH_INFO'], 1); $alias_record = _getRecord(_aliasNameToPath($alias_name)); // Use the HTTP cache to fetch the original URL's contents. if (isset($_GET['direct']) && $_GET['direct']) { // TODO: Is using the Location: header really the best way to handle the aliases? header("Location: $alias_orig_url"); return; } else { $alias_orig_url = $alias_record->{'url'}; $cache =& new HTTPCache($alias_orig_url); $data = $cache->fetch(); // Copy certain response headers returned in the request // TODO: Determine if these are sufficient headers for passthru $passthru_headers = array( 'content-type', 'etag', 'date', 'expires' ); if ($cache->client->headers) { foreach ($cache->client->headers as $header) { if (strpos($header, ':') === false) { error_log($header); continue; } // Parse out the name and value of the header. list($name, $value) = explode(': ', $header); $lc_name = strtolower($name); // If this is a header to be passed through, do so. if (strpos($lc_name, $passthru_headers) !== false) { header($lines[0]); } } } } // Finally, spew out the fetched data. header('Content-Type: text/xml', false); echo $data; } /** * Handle creating a new URL alias record. */ function processAliasBuildRequest() { // Build the initial record for the alias. $alias_record = array( 'name' => $_POST['name'], 'description' => $_POST['description'], 'url' => $_POST['original'] ); // Build a safe filename for this alias record. $path_name = preg_replace('/\//', '_', $alias_record['name']); $alias_path = ALIASES_PATH . '/' . $path_name; // TODO: Report error if file not writable $json = new Services_JSON(); $data = $json->encode($alias_record); file_put_contents($alias_path, $data); // Work out the URL to the newly created alias $alias_url = $_SERVER['PHP_SELF'] . '/' . $alias_record['name']; if ($_POST['post_action'] == 'create') { // Redirect to the chosen alias upon creation header("Location: $alias_url"); } else { // Return to editing form upon update. showAliasForm(); } } /** * Present the URL alias construction form. */ function showAliasForm() { if (isset($_GET['edit'])) { $alias_name = $_GET['name']; $alias_record = _getRecord(_aliasNameToPath($alias_name)); $alias_name = $alias_record->{'name'}; $alias_url = $alias_record->{'url'}; $alias_description = $alias_record->{'description'}; $post_action = 'edit'; } else { $alias_name = ''; $alias_url = ''; $alias_description = ''; $post_action = 'create'; } $form_url = $_SERVER['PHP_SELF']; ?>