如何在浏览器中显示非公开的PDF文件?

时间:2019-07-15 17:50:20

标签: php ajax wordpress pdf

我有点问题。我目前正在建立一个服务,人们可以在WordPress中签名PDF。为此,我使用的模板位于WordPress的公共上传文件夹中。

因此,在用户签名之前,他可以通过https://www.my-site.de/wp-content/uploads/template.pdf在浏览器中轻松查看此模板。

他签名后,我正在创建此模板的副本以及所有相关信息,并将其移至子文件夹signed-templates

$pdf->Output( 'F', wp_upload_dir()['basedir'] . '/signed-templates/123456.pdf' );

在签名模板文件夹中,我有一个.htaccess文件,其中包含以下内容,以防止通过浏览器进行公共访问:

deny from all

现在我要解决我的问题。第一个用户对其进行了签名,并将其保存在上面的文件夹中。现在,第二个用户需要查看并签名。

在他可以对它签名之前,我将通过链接在浏览器中向他显示​​文件。那我该怎么做呢?我的意思是由于.htaccess文件的原因,似乎无法访问URL,但是这是防止泄漏个人信息所必需的。

每个文件都有一个唯一的ID,此ID保存在我的数据库中。因此,我仅有的信息是文件名123456.pdf及其路径。

也许可以通过AJAX进行显示吗?这是我唯一的主意。

1 个答案:

答案 0 :(得分:2)

在各种网站上成功使用的MY方法是:

1) 创建一个将加载数据的PHP文件( PDF

2) 给予PHP某种形式的身份验证(立即密钥值或类似的数据库驱动的唯一值)

3) PHP文件接受身份验证(例如来自电子邮件的链接),然后从受保护的目录中加载相应的PDF。

.htaccess阻止浏览器访问文件,但是由于PHP在本地文件系统上,因此它仍然可以加载PDF文件并将其作为匿名文件直接输出到客户端浏览器。 “ countersign.php”文件。用户2将永远不知道PDF在服务器上的实际存储位置(,即使保存了,他们也将无法直接访问它)。


示例

存储PDF的目录为/files/signed
PHP文件目录为/countersign.php
数据库将具有id (INT(8) A_I ) | filename (VARCHAR(255)) | nonce_code (VARCHAR(255) UNIQUE)

然后,一旦用户1签名,便会生成一个Nonce(又名PRNG)代码,并通过电子邮件发送给用户2。

示例数据库行:

  id    |   filename   |               nonce_code
===========================================================
   4    |  123456.pdf  | AB463747CH567BC5456AB45023DBC36214

用户2收到一封带有链接的电子邮件:

  

https://www.my-site.de/countersign.php?id=AB463747CF567BC5456AB45023DBC36214

用户单击链接; PHP将搜索该链接:

  

注意::下面的代码块包含相当一部分摘要和伪代码-只是用来说明这种方法,而不是符合您条件的确切方法

$id = $_GET['id']; 
/***
 * DB check value is valid and do basic cleaning such as with a Hex checker. 
 * In this  example the nonce value is a Hex string but you can use your own system
 ***/
$dataBase = new dataBaseClass();

/***
 * Check database for this value and return the PDF string. 
 ***/ 
$result = $dataBase->prepare("SELECT filename FROM table WHERE nonce_code = :ncode ");
$result->execute([':ncode' => $id]);


/***
 * Now PHP loads the retrieved filename from the signed directory, 
 * and outputs it to the browser 
 ***/
$f = $result->fetch();
$file = wp_upload_dir()['basedir'] . '/signed-templates/'.'$f['filename'];
if(is_file($file)){
    /***
     * You should have a security, check that the file called is a PDF type 
     * (not shown here)
     ***/
    $output = file_get_contents(urlencode($file));
    if(!empty($output)){
        /***
         * PHP tells the browser it will act as if it is a PDF file:
         ***/
       header("Content-type: application/pdf");
       header("Content-Disposition: inline; filename=filename.pdf");

       /***
        * The PDF will only be accessible to the user who loads the
        * page with the correct nonce code
        ***/
       print $output;  
    }
    else {
         // File error.
    }
}
else {
    // no file found.
    /***
     * Add a delay to throttle spam attempts to discover files
     * (various other practises can be employed depending on the required
     * security level)
     ***/
    sleep(2);
}

一旦对PDF进行了反签名,就可以更新数据库以清除nonce_code值。