Page 1 of 1

Change header("Content-type txt/html");

Posted: Wed Jun 22, 2016 8:07 pm
by MESWEB
I have mod to generate sitemap and I need use this php code:

Code: Select all

header("Content-type: application/xml");
for change header but this is not working I always have header as txt/html. How can I use xml in phpBB?

Re: Change header("Content-type txt/html");

Posted: Mon Jun 27, 2016 8:29 pm
by MarkDHamill
In my Smartfeed extension I created a template called feed.xml and placed it inside my extension in styles/all/template. I then used the templating system to populate it with information. You can see it here:

https://github.com/MarkDHamill/smartfee ... e/feed.xml

I never sent anything in the HTTP header to tell pass a content type. It didn't seem to matter. Try my solution and see if it works for you.

Re: Change header("Content-type txt/html");

Posted: Mon Jun 27, 2016 8:43 pm
by 3Di
MESWEB wrote:I have mod to generate sitemap
Are you MODding phpBB3 or phpBB3.1? Extension or what?

Re: Change header("Content-type txt/html");

Posted: Mon Jun 27, 2016 9:10 pm
by kasimi
To modify headers you need to hook into core.page_header_after and overwrite the value for the content type:

Code: Select all

public function page_header_after($event)
{
    $event['http_headers'] = array_merge($event['http_headers'], array(
        'Content-type' => 'application/xml',
    ));
} 
The event is triggered near the end of the function page_header() in /includes/functions.php

Re: Change header("Content-type txt/html");

Posted: Sat Jul 16, 2016 11:29 am
by MESWEB
kasimi wrote:To modify headers you need to hook into core.page_header_after and overwrite the value for the content type:

Code: Select all

public function page_header_after($event)
{
    $event['http_headers'] = array_merge($event['http_headers'], array(
        'Content-type' => 'application/xml',
    ));
} 
The event is triggered near the end of the function page_header() in /includes/functions.php
I don't know where I should put this to this code:

Code: Select all

class sitemap
{
	/**
	* Constructor
	* NOTE: The parameters of this method must match in order and type with
	* the dependencies defined in the services.yml file for this service.
	*

	/** @var \shredder\sitemap\core */
	protected $core;

	/** @var \phpbb\cache\service */
	protected $cache;

	/** @var \phpbb\config\config */
	protected $config;

	public function __construct(\shredder\sitemap\core $core, \phpbb\cache\service $cache, \phpbb\config\config $config)
	{
		$this->core = $core;
		$this->cache = $cache;
		$this->config = $config;
	}

	/**
	* Controller for route /sitemap/
	*
	* @param string		$name
	* @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
	*/
	public function display_sitemap()
	{
		global $f_xml, $urls;

        $f_xml = '';
		$urls = 0;

		$driver = $this->cache->get_driver();
        //print_r($driver);

		if (($f_xml = $driver->get("_sitemap_seo_file")) === false)
		{
			$this->core->generate_sitemap();

			$this->config->set('sitemap_seo_url_count', $urls);

			if ($this->config['sitemap_seo_cache'])
			{
				$driver->put("_sitemap_seo_file", $f_xml, 3600*$this->config['sitemap_seo_cache']);
			}
		}
		
		return new Response($f_xml);
	}
}

Re: Change header("Content-type txt/html");

Posted: Sat Jul 16, 2016 11:43 am
by kasimi
You need to write an event listener class: https://area51.phpbb.com/docs/dev/31x/e ... e-listener

Re: Change header("Content-type txt/html");

Posted: Sat Jul 16, 2016 4:18 pm
by MESWEB
I have listener already working but when I add Your function:

Code: Select all

public function page_header_after($event)
{
    $event['http_headers'] = array_merge($event['http_headers'], array(
        'Content-type' => 'application/xml',
    ));
} 
Nothing happen.

Re: Change header("Content-type txt/html");

Posted: Sat Jul 16, 2016 4:59 pm
by kasimi
Did you actually hook into to the event?

Code: Select all

public static function getSubscribedEvents()
{
    return array(
        'core.page_header_after' => 'page_header_after',
    );
} 

Re: Change header("Content-type txt/html");

Posted: Sat Jul 16, 2016 5:38 pm
by MESWEB
Yes I have added but nothing change. There is no $event['http_headers']

Code: Select all

<?php

/**
*
* @package phpBB3 SEO Sitemap
* @copyright (c) 2014 www.phpbb-work.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

namespace shredder\sitemap\event;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Event listener
*/
class listener implements EventSubscriberInterface
{
    /** @var \phpbb\auth\auth */
    protected $auth;

    /** @var \phpbb\config\config */
    protected $config;

    /** @var \phpbb\user */
    protected $user;

    /** @var \phpbb\db\driver\driver_interface */
    protected $db;

    /** @var string */
    protected $phpbb_root_path;
    protected $php_ext;

    /**
    * Constructor
    * 
    * @param \phpbb\auth\auth $auth
    * @param \phpbb\config\config $config
    * @param \phpbb\user $user
    * @param \phpbb\db\driver\driver $db
    * @param string $phpbb_root_path Root path
    * @param string $phpbb_ext
    */
    public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\user $user, \phpbb\db\driver\driver_interface $db, $phpbb_root_path, $php_ext)
    {
        $this->auth = $auth;
        $this->config = $config;
        $this->user = $user;
        $this->db = $db;
        $this->phpbb_root_path = $phpbb_root_path;
        $this->php_ext = $php_ext;
    }

    /**
    * Assign functions defined in this class to event listeners in the core
    *
    * @return array
    * @static
    * @access public
    */
    static public function getSubscribedEvents()
    {
        return array(
            'core.user_setup'                    =>    'load_language_on_setup',
            'core.submit_post_end'                    =>    'update_post_modified_time',
            'core.page_header_after' => 'page_header_after',
        );
    }

    public function page_header_after($event)
    {
        $event['http_headers'] = array_merge($event['http_headers'], array(
            'Content-type' => 'application/xml',
        ));
        print_r($event['http_headers']);
    }

    /**
    * Load common files during user setup
    *
    * @param object $event The event object
    * @return null
    * @access public
    */
    public function load_language_on_setup($event)
    {
        $lang_set_ext = $event['lang_set_ext'];
        $lang_set_ext[] = array(
            'ext_name' => 'shredder/sitemap',
            'lang_set' => 'info_acp_seo_sitemap',
        );
        $event['lang_set_ext'] = $lang_set_ext;
    }

    /**
    * Update post modification time when posting or edit
    *
    * @return null
    * @access public
    */
    public function update_post_modified_time($event)
    {
        $data = $event['data'];

        $sql = 'UPDATE ' . POSTS_TABLE . '
                SET post_modified = ' . (int) time() . '
            WHERE post_id = ' . (int) $data['post_id'];
        $this->db->sql_query($sql);
    }
} 

Re: Change header("Content-type txt/html");

Posted: Sat Jul 16, 2016 6:06 pm
by kasimi
What do you mean there is no event['http_headers']? Is it empty? Are you calling the page_header() function in your extension with the $send_headers argument set to false? In that case the $event['http_headers'] array is empty, but it should work regardless. If $event['http_headers'] doesn't exist at all your includes/functions.php might be outdated: https://github.com/phpbb/phpbb/blob/rel ... .php#L5307

This is what I see when I do print_r($event['http_headers']); in the event handler:

Code: Select all

Array
(
    [Content-type] => text/html; charset=UTF-8
    [Cache-Control] => private, no-cache="set-cookie"
    [Expires] => Sat, 16 Jul 2016 17:56:13 GMT
) 

Re: Change header("Content-type txt/html");

Posted: Sat Jul 16, 2016 6:43 pm
by MESWEB
Yes $event is empty. I have PHPBB 3.1.8 and I have http_headers in function.php :

Code: Select all

    $vars = array('page_title', 'display_online_list', 'item_id', 'item', 'http_headers');
    extract($phpbb_dispatcher->trigger_event('core.page_header_after', compact($vars)));

    foreach ($http_headers as $hname => $hval)
    {
        header((string) $hname . ': ' . (string) $hval);
    }

    return; 
Ok I see now. This is working only for all sites of PHPBB not for file generated by mod. This mod creates file and read content to browser so I need change headers for before save file or display file. So how do this? File is always txt/html