XF 1.5 Load XenForo outside without sent headers

WRE

New member
Hi everybody,
Firstly, i'm sorry, i'm not good enough in english. I will try to be easily understand.

I'm a new user of XenForo. It will be installed in a subdirectory /forum/

On my homepage, there is a widget who list last public threads.
This widget works. Before XenForo, i used phpbb, and it worked too.
But now, when i load my homepage, the code start only with my forum widget until the end. I miss all my code before this widget : html declaration, head, etc.

I think XenForo send headers and "echo" my page when i call it. But i just want to use theses classes. Someone know how can i do it ?

I've put all code below, thanks for reading, and maybe helping ;)

Example of my homepage :
PHP:
<html>
    <body>
    <h1>My Website</h1>

    <?php
    $widget_forum = forum_get_last_topics( 9 );
    echo $widget_forum;
    ?>

    <p>Text / Image / List / Footer / etc.</p>
    </body>
</html>

My function forum_get_last_topics() look likes this :
PHP:
<?php
// Start XenForo outside
require_once( XF_ROOT . '/library/XenForo/Autoloader.php' );
XenForo_Autoloader::getInstance()->setupAutoloader( XF_ROOT . '/library' );

XenForo_Application::initialize( XF_ROOT . '/library', XF_ROOT );
XenForo_Application::set( 'page_start_time', time() );

XenForo_Session::startPublicSession();

// Guest
$userId = 0;

/* @var $threadModel XenForo_Model_Thread */
$threadModel = XenForo_Model::create( 'XenForo_Model_Thread' );
/* @var $permissionModel XenForo_Model_Permission */
$permissionModel = XenForo_Model::create( 'XenForo_Model_Permission' );
/** @var XenForo_Model_Node $nodeModel */
$nodeModel = XenForo_Model::create( 'XenForo_Model_Node' );
/** @var XenForo_Model_RouteFilter $routeFilterModel */
$routeFilterModel = XenForo_Model::create( 'XenForo_Model_RouteFilter' );

// We get permissions for visitors group
$guestPermissionCombinations = $permissionModel->getPermissionCombinationsByUserGroupId( 1 );
$guestPermissionCombinations = array_shift( $guestPermissionCombinations );

// We get nodes that can view a visitor
$nodeViewablePermission = $nodeModel->getNodePermissionsForPermissionCombination( $guestPermissionCombinations['permission_combination_id'] );
$nodeViewable           = $nodeModel->getViewableNodeList( $nodeViewablePermission );

$nodeIds = [];
foreach ( $nodeViewable as $node ) {
    $nodeIds[] = $node['node_id'];
}

/*
 * We get last threads that can view a visitor
 */
$conditions = [
    'not_discussion_type' => 'redirect',
    'deleted'             => false,
    'moderated'           => false,
    'find_new'            => true,
    'node_id'             => $nodeIds,
];

$fetchOptions = [
    'limit'          => $nb_max_topics, // = (int) 9
    'order'          => 'post_date',
    'orderDirection' => 'desc',
    'readUserId'     => $userId, // = (int) 0
];

$threads = $threadModel->getThreads( $conditions, $fetchOptions );

if ( ! isset( $threads ) || empty( $threads ) ) {
    return false;
}

// We get public url filters and set it
$routeFilters = $routeFilterModel->getRouteFilters( 'public' );

$prefixSetFilters = [];
foreach ( $routeFilters as $route_filter ) {
    $prefixSetFilters[ $route_filter['prefix'] ]['public'] = $route_filter;
}
XenForo_Link::setRouteFiltersOut( $prefixSetFilters );

// We set public url filtred for each thread
foreach ( $threads as &$thread ) {
    $thread['thread_url'] = XenForo_Link::buildPublicLink( 'canonical:threads', $thread );
}

/*
 * We create widget
 */
if ( empty( $classes ) ) {
    $body = "<ul>";
} else {
    $body = "<ul class=\"" . implode( ' ', $classes ) . "\">";
}

foreach ( $threads as $topic ) {
    $body .= "<li><a href='" . $topic["thread_url"] . "'>" . $topic["title"] . "</a></li>";
}
$body .= "</ul>";

return $body;
?>
 
XenForo_Application::initialize() takes a 4th argument to change what happens during initialization. It looks like you're relying on output buffering, so you want to disable us changing that. Passing this as that argument will likely work:
Code:
['resetOutputBuffering' => false]
 
Thank you ! it seems to work. I suspected it was an option to pass but I could not find where :/ Thank you very much again !
 
Back
Top Bottom