Skip to content

Instantly share code, notes, and snippets.

@BARNZ
Created January 15, 2017 04:18
Show Gist options
  • Select an option

  • Save BARNZ/70cb3d4458cf1f9dd4ba4343eee4d2a1 to your computer and use it in GitHub Desktop.

Select an option

Save BARNZ/70cb3d4458cf1f9dd4ba4343eee4d2a1 to your computer and use it in GitHub Desktop.

Revisions

  1. BARNZ created this gist Jan 15, 2017.
    28 changes: 28 additions & 0 deletions FixMicrosoftLinks.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    <?php

    namespace App\Http\Middleware;

    use Closure;

    /**
    * This middleware allows us to correctly hotlink to pages from within MS apps.
    *
    * It fixes the issue where clicking a link to a page from within MS app (excel/word etc) fails due to the MS app
    * not sending the correct cookies (resulting in a http 3xx) when it checks if the link is valid.
    *
    * @see http://stackoverflow.com/q/2653626
    * @see https://support.microsoft.com/en-gb/kb/899927
    *
    */
    class FixMicrosoftLinks
    {
    public function handle($request, Closure $next)
    {
    // Only for requests that come from an MS office product.
    if (!preg_match('/Word|Excel|PowerPoint|ms-office/i', $request->header('User-Agent'))) {
    return $next($request);
    }

    return response("<html><head><meta http-equiv='refresh' content='0'/></head><body></body></html>", 200);
    }
    }