phpoffice365ms-wopi

PHP WOPI host and WOPI client comunication


I am a bit confused about WOPI. I want implement a basic example just for test. I have made a basic WOPI host:

<?php
require 'vendor/autoload.php';
use Pux\Mux;
use Pux\Executor;

class FilesController {

    // route: /files/:name
    public function getFileInfoAction($name) {
        $path = "office/$name";
        if (file_exists($path)) {
            $handle = fopen($path, "r");
            $size = filesize($path);
            $contents = fread($handle, $size);
            $SHA256 = base64_encode(hash('sha256', $contents, true));
            $json = array(
                'BaseFileName' => $name,
                'OwnerId' => 'admin',
                'Size' => $size,
                'SHA256' => $SHA256,
                'Version' => '222888822'
            );
            echo json_encode($json);
        } else {
            echo json_encode(array());
        }
    }

    // route: /files/:name/contents
    public function getFileAction($name) {
        $path = "office/$name";
        if (file_exists($path)) {
            $handle = fopen($path, "r");
            $contents = fread($handle, filesize($path));
            header("Content-type: application/octet-stream");
            echo $contents;
        }
    }
}


$mux = new Mux;
$mux->get('/files/:name', ['FilesController','getFileInfoAction']);
$mux->get('/files/:name/contents', ['FilesController','getFileAction']);
$path = $_SERVER['PATH_INFO'];
$args = explode("&", $path);
$route = $mux->dispatch( $args[0] );
Executor::execute($route);

and basic WOPI client:

<?php
$wopi_url= ''; // ??????
$access_token = 'xxx';
$access_token_ttl = 1000 * 60;
?>

<form id="office_form" name="office_form" target="office_frame" action='<?php $wopi_url; ?>' method="post">
    <input name="access_token" value='<?php $access_token; ?>' type="hidden" />
    <input name="access_token_ttl" value='<?php $access_token_ttl; ?>' type="hidden" />
</form>

<span id="frameholder"></span>

<script type="text/javascript">
    var frameholder = document.getElementById("frameholder");
    var office_frame = document.createElement("iframe");
    office_frame.name = "office_frame";
    office_frame.id ="office_frame";
    frameholder.appendChild(office_frame);
    document.getElementById("office_form").submit();
</script>

i don't understand which $wopi_url i need to set into the client?

i have implemented into the host only:

  1. getFileAction: /files/:name (https://wopi.readthedocs.io/projects/wopirest/en/latest/files/GetFile.html?highlight=getfile)
  2. getFileInfoAction: /files/:name/contents (https://wopi.readthedocs.io/projects/wopirest/en/latest/files/CheckFileInfo.html#checkfileinfo)

Solution

  • You need to fetch the "discovery XML" and then extract the "edit" URL for the "app" you want to use.

    This URL will have certain things as query parameters that you'll have to either remove or keep depending on the features you wish to leverage.