阻止从pdf直接访问,但让服务器请求pdfjs

时间:2017-01-19 08:25:57

标签: php apache .htaccess pdf joomla

我知道有很多问题但很遗憾,其中任何一项都适合我。

我正在尝试阻止pdf直接访问,但让服务器在PDFJS中显示它。

这是我在尝试查看PDFJS中的pdf时可以得到的:文件丢失,403禁止,意外的服务器响应

我尝试过但没有用过:

  • 拒绝所有人(.htaccess)
  • RewriteRule: RewriteEngine on rewriteRule ^path/to/folder? index.php [L]
  • 在htaccess
  • 中有“全部拒绝”的同时从服务器根路径访问文件
  • 拒绝所有人使用“允许来自127.0.0.1”/“允许来自localhost”

这是执行所有验证的php文件的代码

    <?php

            // Loading Joomla User core Files
            define( '_JEXEC', 1 );
            define('JPATH_BASE', '../');
            require_once ( '../includes/defines.php' );
            require_once ( '../includes/framework.php' );

            // Create the Application
            $app = JFactory::getApplication('site');

            echo "<!DOCTYPE html><html dir='ltr' mozdisallowselectionprint moznomarginboxes><head><link rel='stylesheet' href='web/style/style.css'></head>";

            // Error Messages
            $error1 = "Veuillez vous connecter."; // You need to Log-in
            $error2 = "Votre compte n'est pas abonné au journal électronique. <br /> <span style='font-size: 0.6em;'>Merci de visiter la <a href='' target='_blank'>page d'abonnement</a> si vous souhaiter vous y inscrire.</span>"; // Your account isn't subscribed to our web newspaper
            $error3 = "Une erreur s'est produite durant votre requête. <br /> <span>Merci de réessayer. Si cette erreur persiste, merci de nous en faire part à travers le <a href='' target='_blank'>formulaire du support technique</a>.</span>"; // You encountered an error during your request. Plese refresh the page
            $error4 = "Le lien ne dirige vers aucun pdf. <br /> <span style='font-size: 0.6em;'> Merci d'utiliser un lien correcte."; // You link doesn't lead to any pdf. Please use a correct link
            $error5 = "Le fichier pdf est introuvable. <br /> <span style='font-size: 0.6em;'>Il s'aggit soit d'une erreur du lien URL ou bien le fichier pdf demandé n'existe plus. Veuillez vous refferrez à nos archives si vous souhaitez retrouver une ancienne édition.</span>"; // There's an error in your link.

            if(empty($_GET["page"])) {
                $page = "";
            } else {
                $page = htmlspecialchars($_GET["page"]);
            }

            // Get pdf
            $file = "./pdf/" . $page . ".pdf";

            // Get account
            $user = JFactory::getUser();

            // Define user id
            $userId = $user->id;

            // Connect to databases
            $link = mysqli_connect("localhost", "root", "", "dbName");

            // Define query to execute if page isn't empty and file exists
            $resultabo = mysqli_query($link, "SELECT * FROM tableName WHERE user_id='$userId' ORDER BY id DESC LIMIT 1");

            // STARTING FILES REQUEST
            if(empty($page)) {
                    // If nothing is entered in the page, display error
                    header("HTTP/1.0 404 Not Found");
                    echo "<p id='errorMsg'>" . $error4 . "</p>";
                    exit(0);

            } elseif(file_exists($file)) {

                // If file is existing, continu php

                // Check wether the user is a visitor or a registered member
                if($userId == 0) {

                    header('HTTP/1.0 403 Forbidden');
                    echo "<p id='errorMsg'>" . $error1 . "</p>";
                    exit(0);

                } else {

                    // Controling wether the user is logged-in in a subscribed account
                    while($row = $resultabo->fetch_assoc()) {

                        // Getting account type for checking if subcribed
                        $abo = $row["account_id"];

                                if($abo == 1) {

                                    // Note : 1 is a registered member but not subscribed

                                    // echo message : Your account is not subscribed to our website, please go on WebsiteName to subscribe.   
                                    header('HTTP/1.0 403 Forbidden');
                                    echo "<p id='errorMsg'>" . $error2 . "</p>";
                                    exit(0); 

                                } else {

                                    // Note : All $abo between 6 and 7 are "subscribed"
                                    // SUCCESS, PDF IS BEING SHOWED

                                    // Viewer.html
                                    echo 'PDFJS html code from viewer.html (code example)';

                                }

                    }
                }

            } else {

                // Cannot find file, give an error.
                header("HTTP/1.0 404 Not Found");
                echo "<p id='errorMsg'>" . $error5 . "</p>";
                exit(0);
             }       

        echo "</head></html>";

        exit(0);

?>

无法粘贴整个html查看器代码,但它与其网站上的演示示例相同。为了显示文件,我用我的$文件更改了DEFAULT_URL。

由于

1 个答案:

答案 0 :(得分:1)

最好的解决方案是将文件放在项目文件夹之外,类似于:

-joomla folder
--administrator
--components
--...
-pdf

通过这种方式,无法从网址访问您的pdf文件,但仍可从脚本访问:

$file = JPATH_ROOT."/../pdf/" . $page . ".pdf";