anttiviljami / wp-pdf-templates

Add PDF templates to your WordPress theme
https://wordpress.org/plugins/wp-pdf-templates
GNU General Public License v3.0
42 stars 18 forks source link

Protected Content Solution (something other than FETCH_COOKIES_ENABLED) #6

Closed adaptifyDesigns closed 9 years ago

adaptifyDesigns commented 9 years ago

Please reference this issue for some back story.

The issue began when PDF Templates stopped working in conjunction with a support plugin called Awesome Support. Eventually we were able to narrow it down to the following line which was added to my wp-config file in order to grant the PDF plugin access to restricted content:

define('FETCH_COOKIES_ENABLED', true);.

I have premium content which is restricted by a Woocommerce extension called Memberships.

Before implementing the suggested define('FETCH_COOKIES_ENABLED', true); line in my wp-config file, the PDF Templates plugin could not access the content, because it does not inherit the current user. The above line successfully allowed PDF Templates to access the content, BUT with the Awesome Support plugin installed, for some reason that same line caused any PDF request to time out and get a 502 response from the server. We tested this in multiple environments, and even though we weren't able to understand why that line causes that behavior, we were able to isolate the issue to that one line.

So we need another solution for granting the PDF Templates plugin access to the restricted content. Perhaps there is another method of passing the current user role to the plugin?

Unfortunately I don't quite understand how this all works, so I'm really hoping you can offer some advice on how to get around the above issue.

To summarize:

I need the members of my site to be able to export certain restricted pages and post type as PDFs, but I can't use the FETCH_COOKIES_ENABLED solution, because it breaks the PDF functionality when the Awesome Support plugin is enabled.

Let me know what you think.

Thanks in advance!

adaptifyDesigns commented 9 years ago

UPDATE:

So my code to restrict access to content and PDFs is as follows:

// Initial Membership Conditional Check:
$user_id = get_current_user_id();
if( wc_memberships_is_user_active_member( $user_id, 'session-plan' ) || current_user_can('manage_options') ) 
{
    // ... show the restricted content to WP Admins or registered members as defined the 
    // WooCommerce Memberships plugin
} else 
{
    // ... display "Restricted" message to non members
}

The above conditional checks work for my php/html templates, however they _do not_ work for the PDF templates I've created. Doesn't matter whether the current user in a logged-in Admin or a registered member, they always get the "Restricted" message.

So I thought, well, the PDF Templates plugin must not inherit the current user session data, so it has no idea whether a user is logged in or what role or capabilities they have. So what if I didn't check for user role/capability but instead checked the referrer and make sure that the only people who can see the PDF page are those that have accessed it from a protected/restricted page.

So I ran the following conditional checks instead:

// Initial REFERER Conditional Check:
$ref = $_SERVER['HTTP_REFERER'];
if( $ref == 'https://my-domain.com/protected-page-one/' || $ref == 'https://my-domain.com/protected-page-two/' ) 
{
    // ... show the restricted content only to users who followed a link from one of
    // the two URLs listed above...
} else 
{
    // ... display "Restricted" message anyone who tries to access the page directly or from an unprotected
   // page...
}

Unfortunately this _did not_ work either :-( I cleared all my caches, hard reloaded several times, and tried accessing a pdf page from both of the above URLs. I still got the "Restricted" message!

I guess I don't understand how the PDF Templates plugin works... Can you explain to me how the /pdf url endpoint works? How is the server request made? Is there anyway of getting the URL referrer from within a PDF template? If I can't access the current $user object, and I can't access the global $_SERVER object from within a PDF template, is there any other way I can conditionally serve PDF content to my site's visitors/users? Even if I could just check whether they were logged in, and/or what user role they were, that would solve my problem.

Let me know what's possible.

Thanks!

anttiviljami commented 9 years ago

Hi, Colin!

As you have correctly deduced by reverse engineering the plugin code, the way content is fetched is that the plugin generates an internal HTTP request to your WordPress site to a ?pdf-template endpoint corresponding your content URI and uses the response as the content that's passed to DOMPDF.

What define('FETCH_COOKIES_ENABLED', true); does, is it also sends all the cookies (user's login information and access tokens) along with the internal http request. This is required for access restricted content to work.

What you need to do is find out what's causing the 502 bad gateway errors while FETCH_COOKIES_ENABLED is set to enabled. If it's an external plugin, there might be a way to circumvent a restriction it enforces. Look at your servert logs to see what's going on there when a 502 is generated.

Hope I can be of assistance,

Antti

adaptifyDesigns commented 9 years ago

Thanks for the response Antti,

I could not figure out what as causing the 502 bad gateway response, so I ended up switching to another plugin that does not conflict with PDF Templates.

FYI: the plugin that conflicts (when FETCH_COOKIES_ENABLED is set to true is called Awesome Support... just in case you want to check it out for yourself, or if anyone else has the same issue.

I ended up going with WP Support Plus Pro, which does not cause the same issue.

If I were better at this sort of stuff I would try to get to the bottom of it. Oh well!

Thanks for your help.