|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace OCA\Memento\Controller;
-
- require_once __DIR__ . '/findMementos.php';
- require_once __DIR__ . '/datetimeConversion.php';
-
- use OCP\IRequest;
- use OCP\IURLGenerator;
- use OCP\IServerContainer;
- use OCP\AppFramework\Controller;
- use OCP\AppFramework\Http\RedirectResponse;
- use OCP\AppFramework\Http\DataDisplayResponse;
-
- use findMementos;
-
- class TimeGateController extends Controller {
- private $userFolder;
- private $URLGenerator;
-
- public function __construct(
- $AppName,
- IRequest $request,
- $UserId,
- IServerContainer $serverContainer,
- IURLGenerator $URLGenerator
- ) {
- parent::__construct($AppName, $request);
- $this->userFolder = $serverContainer->getUserFolder($UserId);
- $this->URLGenerator = $URLGenerator;
- }
-
- /**
- * @NoAdminRequired
- * @NoCSRFRequired
- */
- public function timeGate($url) {
- $matchingMementos = findMementos($this->userFolder, $url);
-
- // Choose one of the matched mementos, if any.
- if (count($matchingMementos) === 0) {
- // No matches. :(
- $message = "<h1>No snapshots found for requested URL. :(</h1>";
- return new DataDisplayResponse($message, 404);
- } else if (count($matchingMementos) === 1) {
- // One match; no need to choose.
- $chosenMemento = $matchingMementos[0];
- } else {
- // Multiple matches: choose based on requested date.
- $acceptDatetimeHeader = $this->request->getHeader('Accept-Datetime');
- if ($acceptDatetimeHeader) {
- try {
- $requestedDatetime = datetimeStringToTimestamp($acceptDatetimeHeader);
- } catch (Exception $e) {
- return new DataDisplayResponse("Invalid Accept-Datetime header.", 400);
- }
- } else {
- // Not sending the header means requesting the most recent version.
- $requestedDatetime = time();
- }
- // Pick the one closest to the requested date (either before or after it).
- $chosenMemento = minBy($matchingMementos,
- function ($matchingMemento) use ($requestedDatetime) {
- return abs($matchingMemento['datetime'] - $requestedDatetime);
- }
- );
- }
-
- // Send a 302 Found redirect pointing to the chosen memento.
- $response = new RedirectResponse($chosenMemento['mementoUrl']);
- $response->setStatus(302);
- $response->addHeader('Vary', 'accept-datetime');
-
- // Add a link to the original and to the timemap.
- $originalLink = "<{$chosenMemento['originalUrl']}>;rel=\"original\"";
- // XXX hardcoding the route URL.
- $timeMapUrl = $this->URLGenerator->getAbsoluteUrl("/apps/memento/timemap/$url");
- $firstDatetime = datetimeTimestampToString($matchingMementos[0]['datetime']);
- $lastMemento = $matchingMementos[count($matchingMementos)-1];
- $lastDatetime = datetimeTimestampToString($lastMemento['datetime']);
- $timeMapLink = "<$timeMapUrl>"
- . ";rel=\"timemap\""
- . ";type=\"application/link-format\""
- . ";from=\"$firstDatetime\";until=\"$lastDatetime\"";
- $response->addHeader('Link', "$originalLink, $timeMapLink");
-
- return $response;
- }
- }
-
- function minBy($array, $iteratee) {
- // is there any simpler way for this in php?
- $values = array_map($iteratee, $array);
- $argmin = array_search(min($values), $values);
- return $array[$argmin];
- }
|