Getting output of page using PHP

Permalink 1 user found helpful
I thought this should be an easy thing but I couldn't figure out a nice way to fetch the content of a complete page from code. All I want is a variable in PHP which contains all the output rendered to the browser as if a normal website users opens the page.

I usually used this code:

$fh = Loader::helper('file');
$nh = Loader::helper('navigation');
$pageObj = Page::getById($pageId);         
$pageContent = $fh->getContents($nh->getLinkToCollection($pageObj,true));


This works fine but as soon as the page is protected (no permissions for "guest" to view the page) it doesn't work anymore. This is not a surprise as I get the page by using a http://.. address.

What's the proper way to get the content of a protected page from concrete5? I also toyed around with output buffering but that's not really the way I want to go.. Any ideas?

Remo
 
jordanlev replied on at Permalink Reply
jordanlev
I don't know about the specific ways to do this in C5, but would it be possible to temporarily override the global User object and set it to the super-admin (or someone else who has permissions for that page)?

Output buffering is also a good solution, although I'm not sure how you'd get around the permissions problem in that way either. But if you did know how to, I'm curious why you don't want to go that route -- I've seen it done and done it myself in many situations (outside of C5) and never had a problem with it (despite it seeming kind of "hack-ish").

BTW, congratulations on the book! I got my paper copy a week ago -- I know it's geared towards more beginning C5 developers but it was great to read through and re-think the system in a complete way. Also really appreciated the section on advanced permissions, since I've never actually used that myself on a site, but will be less afraid of it now :)

-Jordan
Remo replied on at Permalink Reply
Remo
Working with the user object / super admin doesn't really work because $fh->getContents gets the content of the page by using curl. I'd have to send a post request to create a c5 session first and then get the page content..

output buffer causes some problems when I toyed around with it a while ago. Mostly because there is some output buffering stuff in the core as well... Also, I couldn't really find the perfect method to render the page..

Thanks Jordan! As a matter of fact I don't like the advanced permission mode myself. I'm not saying it's a bad thing but in my experience a feature like this adds a, often unnecessary, complexity to the project.. Lots of my customers are looking for something like this but end up using the basic permission mode..
Remo replied on at Permalink Best Answer Reply
Remo
I ended up using output buffering but also had to override $c as it is sometimes used globally. The page rendering process calls code $c->getCollectionAttributeValue which is why the rendered object variable must always be called $c.

In case someone wonders, this is a possible solution:

<?php
defined('C5_EXECUTE') or die(_("Access Denied."));   
$oldC = $c;
// we have to override $c because it is
// later used as a global variable
$c = Page::getByID(66);
// start output buffering
ob_start();
// render page object
$v = View::getInstance();
$v->render($c);
// get page content
$content = ob_get_contents();
// stop output buffering
if (ob_get_level() > OB_INITIAL_LEVEL) {


I'm not super happy with it but I don't think there's a more elegant way without rewriting some core stuff.