vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Using vb:raw in templates (https://vborg.vbsupport.ru/showthread.php?t=313961)

Black Snow 08-27-2014 09:24 AM

Using vb:raw in templates
 
Hi, I have create multiple custom pages on my forum. I have a navigation bar on the left of all of said pages. Everytime I add/change a link, I need to go into each and every template to add/change that link.

Is there a way to make a template called "custom_nav", add the links for my nav bar, then call that template from each of the custom pages? If so, can someone help me do it or show me where to start please?

Thanks

kh99 08-27-2014 09:40 AM

You could use a template. You would need php code to render your template and save the results in a variable, then register that variable to any template where you want to display it. You could do that in a plugin, or if your custom pages are php scripts, you could do it in your custom scripts.

How are your custom pages implemented?

cellarius 08-27-2014 10:31 AM

See https://vborg.vbsupport.ru/showthread.php?t=228078

Black Snow 08-27-2014 11:26 AM

Quote:

Originally Posted by kh99 (Post 2512847)
You could use a template. You would need php code to render your template and save the results in a variable, then register that variable to any template where you want to display it. You could do that in a plugin, or if your custom pages are php scripts, you could do it in your custom scripts.

How are your custom pages implemented?

The pages are done using templates. I followed an article on here for creating custom pages using templates.

I have this in my php file.
Code:

<?php

// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// #################### DEFINE IMPORTANT CONSTANTS #######################

define('THIS_SCRIPT', 'info');
define('CSRF_PROTECTION', true); 
// change this depending on your filename

// ################### PRE-CACHE TEMPLATES AND DATA ######################
// get special phrase groups
$phrasegroups = array();

// get special data templates from the datastore
$specialtemplates = array();

// pre-cache templates used by all actions
$globaltemplates = array('siterules');

// pre-cache templates used by specific actions
$actiontemplates = array();

// ######################### REQUIRE BACK-END ############################
// if your page is outside of your normal vb forums directory, you should change directories by uncommenting the next line
// chdir ('/path/to/your/forums');
require_once('./global.php');

// #######################################################################
// ######################## START MAIN SCRIPT ############################
// #######################################################################

$navbits = construct_navbits(array('' => 'Forum'));
$navbar = render_navbar_template($navbits);

// ###### YOUR CUSTOM CODE GOES HERE #####

//Use as http:/site.com/test.php?do=siterules
if ($_REQUEST['do'] == "siterules") {
$pagetitle = 'Site Rules';
$templater = vB_Template::create('siterules');
$templater->register_page_templates();
$templater->register('navbar', $navbar);
$templater->register('pagetitle', $pagetitle);
print_output($templater->render());
exit;
}


?>

I think this is all I need in the PHP file.

So, I have a template called "siterules", using the PHP file above. I now want to add a navigation bar on this page and a further 4 pages so when I add/change a link, it reflects on all pages. How can I use a plugin to pull the template "navigation" and insert it on each of the custom pages?

kh99 08-27-2014 02:14 PM

Well, since you have custom php files you don't need to use a plugin. You can add code to render your custom template before the "main" page template. So just after the "YOUR CUSTOM CODE GOES HERE", add something like:
Code:

$templater = vB_Template::create('custom_nav');
$custom_nav  = $templater->render();


Then lower down, add a "register" line to your template code:
Code:

$templater->register('custom_nav', $custom_nav);
then you should be able to put {vb:raw custom_nav} in your siterules template where you want it to appear.

One last thing, for efficiency: add custom_nav to the (existing) array of templates to cache ($globaltemplates):
Code:

$globaltemplates = array('siterules', 'custom_nav');

Black Snow 08-29-2014 08:26 AM

Thanks. I will try that and report back.

--------------- Added [DATE]1409309336[/DATE] at [TIME]1409309336[/TIME] ---------------

Quote:

Originally Posted by kh99 (Post 2512886)
Well, since you have custom php files you don't need to use a plugin. You can add code to render your custom template before the "main" page template. So just after the "YOUR CUSTOM CODE GOES HERE", add something like:
Code:

$templater = vB_Template::create('custom_nav');
$custom_nav  = $templater->render();


Then lower down, add a "register" line to your template code:
Code:

$templater->register('custom_nav', $custom_nav);
then you should be able to put {vb:raw custom_nav} in your siterules template where you want it to appear.

One last thing, for efficiency: add custom_nav to the (existing) array of templates to cache ($globaltemplates):
Code:

$globaltemplates = array('siterules', 'custom_nav');

Worked a treat! :up: thank you very much.

I have one other question. How do I make a page show if there is no
Code:

if ($_REQUEST['do'] == BLAH
I have my pages now set up:
Code:

if ($_REQUEST['do'] == 1
if ($_REQUEST['do'] == 2
if ($_REQUEST['do'] == 3
if ($_REQUEST['do'] == 4

But how do I get page 1 to show if someone goes to http://site.com/info.php

cellarius 08-29-2014 11:39 AM

As a rule: You never, ever once address URL parameters directly like that. Opens you up for all kinds of vulnerabilities.

Read and implement: https://vborg.vbsupport.ru/showthread.php?t=119372

After that, you will have your parameter in a nice variable, for example: do.

And to your question - if a variable is not defined, you define it in your code. See PHP man isset. Along the lines of
Code:

if (!isset($do))
{
    $do = 1;
}

Or do
Code:

if (!isset($do) OR $do == 1))

Black Snow 08-29-2014 03:43 PM

So I could do this:

Code:

if (!isset($do) OR $_REQUEST['do'] == 1)

cellarius 08-29-2014 07:32 PM

Why would you use $_Request again if you have cleaned the input and assigned it to $do? If you havn't done that, $do will not work. And as I said before: Accessing $_REQUEST directly is evil. Do not do it. Read the link I gave you.

This is really basic PHP 101.

Black Snow 08-30-2014 12:05 PM

Quote:

Originally Posted by cellarius (Post 2513076)
Why would you use $_Request again if you have cleaned the input and assigned it to $do? If you havn't done that, $do will not work. And as I said before: Accessing $_REQUEST directly is evil. Do not do it. Read the link I gave you.

This is really basic PHP 101.

Your link doesn't show how to use $vbulletin->input->clean_array_gpc when requesting a page. Could you show me an example?

Scanu 08-30-2014 01:50 PM

PHP Code:

$vbulletin->input->clean_gpc('r''do'TYPE_STR);
$do $vbulletin->GPC['do'];
if (!isset(
$do))
$do 1//default value

if ($do == 1) {

}
if (
$do == 2) {

}
if (
$do == 3) {

}
if (
$do == 4) {




cellarius 08-30-2014 02:22 PM

Quote:

Originally Posted by Black Snow (Post 2513117)
Your link doesn't show how to use $vbulletin->input->clean_array_gpc when requesting a page. Could you show me an example?

Of course it does.

Quote:

Cleaning Superglobal Arrays

By Superglobal, I mean $_POST, $_GET, $_REQUEST and so on. These arrays are created automaticly by PHP and contain the user-sent input. They are referenced in the vBulletin Input Cleaner by nice short single letter names. These are:
p - $_POST
g - $_GET
r - $_REQUEST
s - $_SERVER
e - $_ENV
c - $_COOKIE
f - $_FILES
and so on.

Black Snow 09-01-2014 10:53 AM

Quote:

Originally Posted by Scanu (Post 2513125)
PHP Code:

$vbulletin->input->clean_gpc('r''do'TYPE_STR);
$do $vbulletin->GPC['do'];
if (!isset(
$do))
$do 1//default value

if ($do == 1) {

}
if (
$do == 2) {

}
if (
$do == 3) {

}
if (
$do == 4) {




Thanks for the example. Makes me understand more now.

--------------- Added [DATE]1409646066[/DATE] at [TIME]1409646066[/TIME] ---------------

Quote:

Originally Posted by Scanu (Post 2513125)
PHP Code:

$vbulletin->input->clean_gpc('r''do'TYPE_STR);
$do $vbulletin->GPC['do'];
if (!isset(
$do))
$do 1//default value

if ($do == 1) {

}
if (
$do == 2) {

}
if (
$do == 3) {

}
if (
$do == 4) {




I tried doing this but it won't work if I access the info.php page without the query on the end of the URL:
Code:

$vbulletin->input->clean_gpc('r', 'do', TYPE_STR);
$do = $vbulletin->GPC['do'];
if (!isset($do))
//default value
$do = "siterules";


//Use as http:/site.com/info.php?do=siterules
if ($do == "siterules") {
$pagetitle = 'General Site Rules';
$templater = vB_Template::create('siterules');
$templater->register_page_templates();
$templater->register('navbar', $navbar);
$templater->register('pagetitle', $pagetitle);
$templater->register('custom_nav', $custom_nav);
print_output($templater->render());
exit;
}


Scanu 09-02-2014 12:11 PM

Quote:

Originally Posted by Black Snow (Post 2513340)
Thanks for the example. Makes me understand more now.

--------------- Added [DATE]1409646066[/DATE] at [TIME]1409646066[/TIME] ---------------


I tried doing this but it won't work if I access the info.php page without the query on the end of the URL:
Code:

$vbulletin->input->clean_gpc('r', 'do', TYPE_STR);
$do = $vbulletin->GPC['do'];
if (!isset($do))
//default value
$do = "siterules";


//Use as http:/site.com/info.php?do=siterules
if ($do == "siterules") {
$pagetitle = 'General Site Rules';
$templater = vB_Template::create('siterules');
$templater->register_page_templates();
$templater->register('navbar', $navbar);
$templater->register('pagetitle', $pagetitle);
$templater->register('custom_nav', $custom_nav);
print_output($templater->render());
exit;
}


Try replacing this
PHP Code:

$vbulletin->input->clean_gpc('r''do'TYPE_STR);
$do $vbulletin->GPC['do'];
if (!isset(
$do))
//default value
$do "siterules"

With this
PHP Code:

if (!isset($_REQUEST['do'])
$_REQUEST['do'] = 'siterules';
$vbulletin->input->clean_gpc('r''do'TYPE_STR);
$do $vbulletin->GPC['do']; 


Black Snow 09-02-2014 12:45 PM

Quote:

Originally Posted by Scanu (Post 2513469)
Try replacing this
PHP Code:

$vbulletin->input->clean_gpc('r''do'TYPE_STR);
$do $vbulletin->GPC['do'];
if (!isset(
$do))
//default value
$do "siterules"

With this
PHP Code:

if (!isset($_REQUEST['do'])
$_REQUEST['do'] = 'siterules';
$vbulletin->input->clean_gpc('r''do'TYPE_STR);
$do $vbulletin->GPC['do']; 


Thanks for helping. I still get a blank page.

Scanu 09-02-2014 01:13 PM

I can't see anything wrong now, and I don't have time right now to test it
Maybe this code could make a difference

PHP Code:

[PHP]
$vbulletin->input->clean_gpc('r''do'TYPE_STR); 
$do $vbulletin->GPC['do'];
if (!isset(
$_REQUEST['do']) 
$do 'siterules'

[/PHP]

kh99 09-02-2014 01:16 PM

There is a $vbulletin->GPC_exists[] array, so you could try this:
Code:

$vbulletin->input->clean_gpc('r', 'do', TYPE_STR);
if ($vbulletin->GPC_exists['do'])
  $do = $vbulletin->GPC['do'];
else
  $do = "siterules";

// etc


Another thing you could do is just make 'siterules' the default 'else':
Code:

$vbulletin->input->clean_gpc('r', 'do', TYPE_STR);
$do = $vbulletin->GPC['do'];

if ($do == 'something')
{
 // something
}
else if ($do == 'somethingelse')
{
// something else
}
else // default to siterules
{
  // siterules
}


BTW, I'm not a php expert so I'm not going to argue with what's correct and what's "evil", but I would say that there was nothing actually wrong with what you originally had (as far as introducing vulnerabilities), and in fact the vb scripts do it that way (just for the 'do' variable).


All times are GMT. The time now is 03:36 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04294 seconds
  • Memory Usage 1,831KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (16)bbcode_code_printable
  • (8)bbcode_php_printable
  • (9)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (17)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete