UseBB Community

The official board for UseBB help and discussion

UseBB Community » Help & Support » Undefined index errors with wrapper

Undefined index errors with wrapper

Moderators: Gaia.

Page: 1

Author Post
Member
Registered: Aug 2006
Posts: 43
Location: The Netherlands
Hi,
I'm currently making a wrapper for my site to be able to use UseBB's authentication.
It seems to work correctly, but when I include it in my index page (../index.php from the point of view in the forum directory), it trows an error at me:
Quote
In file index.php on line 6:

- Undefined index: page

My wrapper contains the following code:

<?php
if(!defined('INCLUDED')){
die('...');
}
define('PORTAL_ROOT', './'); // Portal root path
define('ROOT_PATH', './forum/'); // Forum root path

require(ROOT_PATH.'sources/common.php'); // Include forum engine
require(PORTAL_ROOT.'sources/engine/common.php'); // Include portal engine

if (!isset($session->sess_info['user_id'])) {
$profiledata = array();
$profiledata['id'] = 0;
$profiledata['level'] = 0;
$profiledata['name'] = 'Guest';
}
else {
$profiledata = $session->sess_info['user_info'];

}
?>

First part of my index.php file, but I get index errors throughout the file no matter if I remove the error-causing piece of code.

define('INCLUDED', true); // Security against direct viewers
require('wrapper.php');

$session->update('portal');
$switch = $_GET['page']; <-- THIS is what the error message above refers to
switch($switch){ // Switching
case 'tags': // Tags page
$page_title = $portal->pageTitle("Tags"); // Set page title to 'Tags'
$tags = $_GET['tags']; // Be able to set a variable with tags provided
.....


I'm using the TinyButStrong template engine for my portal.
I've got absolutely no clue what is causing this problem, and suggestions would be greatly apreciated!
Thanks in advance,
martijn
Developer
Registered: Apr 2004
Posts: 2202
Location: Belgium
UseBB uses strict error handling, this means it shows ALL possible errors in the attempt of people to write clean code with enough checks. In your code, the problem is most likely you use array values which are possibly not always set. For example, $switch = $_GET['page']; works when "page" is set as a GET variable, but when it isn't, the page index does not exist in the $_GET array and thus generates a notice in PHP.

You should first check if it exists before you use the array value.
if ( !empty($_GET['page']) )
-or-
if ( isset($_GET['page']) )
-or-
if ( array_key_exists('page', $_GET) )
_______________
--Dietrich (developer)
UseBB roadmap, dev mailing list & weblog
Member
Registered: Aug 2006
Posts: 43
Location: The Netherlands
Thanks for that suggestion.
But how would I do this when the array is build from a database?
Like in my tag building code? (The ---> marker is where the next error occurs, and possibly it would break with the explode function as well.)

		// ************** Tag cloud: start ****************** \\
$select = mysql_query("SELECT tags FROM `files_releases` WHERE 1");// Get all tags
while ($line = mysql_fetch_array($select, MYSQL_ASSOC)) {// Put them in a while loop
----> $adds.= $line["tags"]." "; // Put the tags into variables
}
----> $adds = explode(' ',$adds); // Explode everything into a array
$o = new Cal_TagCloud($adds); // Initiate the TagCloud class
$o->setSpacer(" &nbsp; ")->setmaxTags(CLOUD_MAX_TAGS)->setShowScore(CLOUD_SHOW_SCORE)->setLink(SITE_URL.'tags-%s.html')->buildCloud();
// Using fluent interface, one call does it all.
// ************** Tag cloud: end ********************** \\

This is the code I used to fix the first error, btw ;)

	if ( isset($_GET['page']) ){
$switch = $_GET['page'];
}
else{
$switch = "";
}

Thanks in advance!
martijn
Developer
Registered: Apr 2004
Posts: 2202
Location: Belgium
It doesn't matter where the array comes from, if the index is possibly not always there, you should check if it exists using the methods explained previously.

explode() doesn't issue a notice, but using $adds will if it is undefined (you can't use .= unless the variable exists).

I recommend to read the PHP manual: http://be.php.net/manual/en/language.variables.php
_______________
--Dietrich (developer)
UseBB roadmap, dev mailing list & weblog
Member
Registered: Aug 2006
Posts: 43
Location: The Netherlands
Hi,
I've got a working wrapper now :) Thanks for the help!
Here's my code for anyone who wants to use it, too:
<?php
if(!defined('INCLUDED')){
die('...');
}
define('PORTAL_ROOT', './'); // Portal root path
define('ROOT_PATH', './forum/'); // Forum root path

require(ROOT_PATH.'sources/common.php'); // Include forum engine
require(PORTAL_ROOT.'sources/engine/common.php'); // Include portal engine
$session->update();
if(isset($session)){
if($session->sess_info['user_info']['level'] == 0){
$sess = array();
$sess['id'] = 0;
$sess['name'] = "Guest"; // Too lazy to do the error checking routine for the language array here...
$sess['displayed_name'] = "Guest"; // ditto^
$sess = array_merge($sess, $session->sess_info['user_info']);
}
else{
$sess = $session->sess_info['user_info'];
}
}
?>


I did have to change some minor things in UseBB files, though.
In UseBB's common.php I had to set magic_quotes to '0', because it made TinyButStrong's template output go weird.
In functions.php I added the ROOT_PATH constant to the redir_to_login function, so I could use the function in my root directory aswell.

Thanks again :)
Developer
Registered: Apr 2004
Posts: 2202
Location: Belgium
You shouldn't change magic_quotes, UseBB depends on it and disabling it will break large parts and possibly open many security holes.
_______________
--Dietrich (developer)
UseBB roadmap, dev mailing list & weblog
Member
Registered: Aug 2006
Posts: 43
Location: The Netherlands
All right, I changed set_magic_quotes_runtime back to 1 and put set_magic_quotes_runtime(0); after I included the forum's common.php in wrapper.php.
Member
Registered: Aug 2006
Posts: 43
Location: The Netherlands
There's also this warning in my AJAX shoutbox, now:
Quote
Non-static method Portal::parseText() cannot be called statically

This function does BBCode and smiley parsing, and is used by TBS through template parameters.
Here's the code

class Portal{
.................
function parseText($FieldName,&$CurrVal)
{
// Strip the slashes from the text
$CurrVal = stripslashes($CurrVal);
// Replace newlines with XHTML <br/>
$CurrVal = str_replace(array("\r\n", "\n", "\r"), '<br />', $CurrVal);
// Borrowed the first line of code below from phpBB to make the [url] code work correctly
$CurrVal = preg_replace('#\[url=([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*?)\]([^?\n\r\t].*?)\[/url\]#is', '<a href="$1" title="$2 - $1">$2</a>', $CurrVal);
// Smileys :: start
$CurrVal = str_replace(':)', '<img src="'.SMILE_PATH.'smile.gif" alt=":)" />', $CurrVal);
$CurrVal = str_replace(':(', '<img src="'.SMILE_PATH.'frown.gif" alt=":(" />', $CurrVal);
$CurrVal = str_replace(':D', '<img src="'.SMILE_PATH.'biggrin.gif" alt=":D" />', $CurrVal);
$CurrVal = str_replace(';)', '<img src="'.SMILE_PATH.'wink.gif" alt=";)" />', $CurrVal);
$CurrVal = str_replace(':o', '<img src="'.SMILE_PATH.'eek.gif" alt=":o" />', $CurrVal);
$CurrVal = str_replace(':rolleyes:', '<img src="'.SMILE_PATH.'rolleyes.gif" alt="rolleyes" />', $CurrVal);
// Smileys :: end
return $CurrVal;
}
...................
}


I know this code needs to be heavily improved, but for the time being I'd still like to be able to use it...
Any help would be great!
Thanks in advance!
martijn
Member
Registered: Aug 2006
Posts: 43
Location: The Netherlands
Sorry, I didn't read carefully enough, changing "function parseText" to "public static function parseText", worked ;) You can delete the previous post if you want to.

Page: 1

UseBB Community » Help & Support » Undefined index errors with wrapper

UseBB Community is powered by UseBB 1 Forum Software