Jump to content

A little code I can't migrate from PHP 4 to PHP 5


radio_fan

Recommended Posts

Hi all, I'm trying for a few hours already to change a script I have, in order to fit the new PHP5 server environment. It' s little snippet of code, I try to change, without messing up the script..

 

if (!$noparsing && !$isnew)  {
			$catloop = explode(',', $settings->parsecodecategories);
			for ($x=0; $x<sizeof($catloop); $x++)   {
				$this->$catloop[$x] = dowsncodes($this->$catloop[$x]);
			}

			$catloop = explode(',', $settings->smiliescategories);
			for ($x=0; $x<sizeof($catloop); $x++)   {
				$this->$catloop[$x] = dosmilies($this->$catloop[$x]);
			}

			$catloop = explode(',', $settings->linebreakcategories);
			for ($x=0; $x<sizeof($catloop); $x++)   {
				$this->$catloop[$x] = str_replace("\n", "<br>", $this->$catloop[$x]);
			}

			$this->description = parsemessage($this->description);

			if ($settings->wordwrap > 0) $this->description = wraplines($this->description, $settings->wordwrap);
		}

 

I try changing $catloop[$x] to catloop[$x] (without the $) and the script starts, but I can't get the titles of the pages to load. Is it possible to mend this code, in order to make it PHP5 suitable?

 

I also wonder, if there's an adequate replacement for "$this->$name = $value;". Because I leave it like "$this->name = $value;", but I'm not sure it'd work...

Link to comment
Share on other sites

So, $catloop is empty.  You need to find out why $this->$catloop is empty.  You could try removing $this->, as $catloop is not in the object scope.  It is being defined from your explode.

 

Even if it were in object scope, it should be $this->catloop.

Link to comment
Share on other sites

There's a problem with your $settings object. Either one of the $settings-> properties is empty or non-existent (null). The explode is producing an array with one empty string as an element and when you attempt to loop over this and dynamically create properties, you are getting that error.

 

Does this have anything to do with php4 vs php5, I don't know. Php4 might have been treating this condition as a warning and continuing. It's more likely your settings don't exist and the code is not testing if the exploded string produced an array that contains settings to produce properties from.

 

Short-answer: there's nothing technically wrong with the code and you should not be randomly trying different things in an attempt to get it to work. Find out why it is producing that error and fix the problem. Debug what is in the $settings object, using var_dump

 

Link to comment
Share on other sites

The problem is , that in PHP4, with the original code, I had the title of the current category displayed in the header. In the .tpl file I have a string {LANG_NAVORIGIN} , which , whatever I edit in the abovequoted code, remains {LANG_NAVORIGIN} in PHP 5, and it is not filled with information about the current category, as in PHP 4. So I have <title>{LANG_NAVORIGIN}</title> in the header under PHP 5.  I have tried removing "$this->", but to no avail.

Link to comment
Share on other sites

As $catloop has just been assigned a value, to me this would make more sense:

 

$catloop = explode(',', $settings->parsecodecategories);
for ($x=0; $x<sizeof($catloop); $x++)   {
    $this->catloop[$x] = dowsncodes($catloop[$x]);
}

 

However , the next two for loops are going to overwrite whatever goes into $this->catloop[$x]

Link to comment
Share on other sites

Thank you a lot, I've changed it to

 

if (!$noparsing && !$isnew)  {
			$catloop = explode(',', $settings->parsecodecategories);
for ($x=0; $x<sizeof($catloop); $x++)   {
    $this->catloop[$x] = dowsncodes($catloop[$x]);
}

			$catloop = explode(',', $settings->smiliescategories);
			for ($x=0; $x<sizeof($catloop); $x++)   {
				$this->catloop[$x] = dosmilies($catloop[$x]);
			}

			$catloop = explode(',', $settings->linebreakcategories);
			for ($x=0; $x<sizeof($catloop); $x++)   {
				$this->catloop[$x] = str_replace("\n", "<br>", $this->catloop[$x]);
			}

 

But to no avail. Still I get {LANG_NAVORIGIN} instead of name of category in the header..

 

I had one more error, except this one:

 

in

$this->downloadcontent = fileread($path);
		$data = decodelanguage($this->downloadcontent);
		$this->sourcedata = $data;
		$groups = explode('|||END LINE|||', $data);
		foreach ($groups as $group)   {
			$parts = explode('|||DIVIDER|||', $group);
			$name = $parts[0];
			$value = $parts[1];
			$this->$name = $value;

			if ($this->internalnamelist == '') $this->internalnamelist = $name; else $this->internalnamelist .= ','. $name;
		}

 

I've changed $this->$name = $value; to $this->name = $value; in order to escape the error. Could this be related, i don't know.. maybe I should change that line in another manner?

Link to comment
Share on other sites

Here's a working example of the original posted code -

 

<?php

// some faked functions that modify the value for demo purposes
function dowsncodes($var){return '[dc]'.$var;}
function dosmilies($var){return '[ds]'.$var;}
function parsemessage($var){return '[pm]'.$var;}
function wraplines($var){return '[wl]'.$var;}

class foo{

var $description = 'some thing';

// pretend the following properties, corresponding to the exploded settings strings in the following code, were dynamically created and assigned values, instead of being listed here
var $cat1 = 'c1',$cat2 = 'c2',$cat3= 'c3';
var $smi1 = '(o',$smi2= '(;';
var $lbc1 = "a\nb",$lbc2 ="c\nd";

function bar(){

// fake a settings object that when referenced in the posted code, sets/references dynamically created properties
$settings = (object)array('parsecodecategories'=>'cat1,cat2,cat3','smiliescategories'=>'smi1,smi2','linebreakcategories'=>'lbc1,lbc2','wordwrap'=>25);

// an example of the $settings object that produces the OP's error
//$settings = (object)array('parsecodecategories'=>'','smiliescategories'=>'smi1,smi2','linebreakcategories'=>'lbc1,lbc2','wordwrap'=>25);

// original posted code 
$catloop = explode(',', $settings->parsecodecategories);
for ($x=0; $x<sizeof($catloop); $x++)   {
	$this->$catloop[$x] = dowsncodes($this->$catloop[$x]);
}
$catloop = explode(',', $settings->smiliescategories);
for ($x=0; $x<sizeof($catloop); $x++)   {
	$this->$catloop[$x] = dosmilies($this->$catloop[$x]);
}
$catloop = explode(',', $settings->linebreakcategories);
for ($x=0; $x<sizeof($catloop); $x++)   {
	$this->$catloop[$x] = str_replace("\n", "<br>", $this->$catloop[$x]);
}
$this->description = parsemessage($this->description);
if ($settings->wordwrap > 0) $this->description = wraplines($this->description, $settings->wordwrap);
}
}

$class = new foo;
$class->bar();

echo "<pre>";
print_r($class);

 

Output:

 

foo Object
(
    [description] => [wl][pm]some thing
    [cat1] => [dc]c1
    [cat2] => [dc]c2
    [cat3] => [dc]c3
    [smi1] => [ds](o
    [smi2] => [ds](;
    [lbc1] => a
b
    [lbc2] => c
d
)

 

Output with the second example $settings object uncommented:

Fatal error: Cannot access empty property in foobar.php on line 29

 

The indirect/variable/dynamic property syntax being used: $somevar = 'abc'; $this->$somevar (evaluates as $this->abc) was valid in php4 and is unlikely to be the cause of the problem the OP is having.

 

 

Link to comment
Share on other sites

PFMaBiSmAd, thank you very, very much.....

 

But when I replace the code I get a blank page, I don't even get an error.

 

I'll describe all I had to change, after the hosting migrated to PHP 5.

 

Initially I got that error:

Fatal error: Cannot access empty property in /home/www/radioaccount/radio/classes/language.php on line 46

 

This is the original PHP4 code:

 

<?php 
class language {

	function language($languagegroup)  {
		global $inadmindir, $settings;
		$this->groupid = $languagegroup;

		if ($inadmindir) {
			$path = '../';
			$prefix = '../';
		}

		$path .= 'languages/';

		if (!file_exists($path . $languagegroup .'.lng'))   {
			$path .= 'setup/'. $languagegroup .'.lng';

			if (!file_exists($path))     {
				$newpath = $prefix .'languages/'. getvalidlanguage() .'.lng';

				if (file_exists($newpath)) {
					$path = $newpath;
				} else {

					if (file_exists($prefix .'languages/setup/fullenglish.lng')) $path = $prefix .'languages/setup/fullenglish.lng'; else          {
						$newpath = $prefix .'languages/setup/'. getvalidlanguage() .'.lng';
						$path = $newpath;
					}

				}

			}

		} else   {
			$path .= $languagegroup .'.lng';
		}

		$this->downloadcontent = fileread($path);
		$data = decodelanguage($this->downloadcontent);
		$this->sourcedata = $data;
		$groups = explode('|||END LINE|||', $data);
		foreach ($groups as $group)   {
			$parts = explode('|||DIVIDER|||', $group);
			$name = $parts[0];
			$value = $parts[1];
			$this->$name = $value;

			if ($this->internalnamelist == '') $this->internalnamelist = $name; else $this->internalnamelist .= ','. $name;
		}

	}


	function allnames()  {
		return $this->internalnamelist;
	}


	function updateitem($name)  {
		return $this->writelanguage();
	}


	function writelanguage()  {
		global $inadmindir;
		$langarray = get_object_vars($this);
		while(list($key, $value) = each($langarray))    {

			if ($key != 'internalnamelist' && $key != 'groupid' && $key != 'sourcedata' && $key != 'downloadcontent')      $data .= $key .'|||DIVIDER|||'. $value .'|||END LINE|||';
		}


		if ($inadmindir) $test = filewrite('../languages/'. $this->groupid .'.lng', $data); else $test = filewrite('languages/'. $this->groupid .'.lng', $data);
		return $test;
	}


	function deleteitem($name)  {
		unset($this->$name);
		$test = $this->writelanguage();
		return $test;
	}


	function filter($field, $filter)  {
		$filter = strtolower($filter);
		$langarray = get_object_vars($this);
		$skip = '    groupid internalnamelist downloadcontent sourcedata   ';
		while(list($name, $value) = each($langarray))    {

			if (!strstr($skip, ' '. $name .' '))    {

				if ($field == 'name')     {

					if ($filter == '') $filteredarray[] = array($name,$value); else
					if (strstr(strtolower($name), $filter))   {
						$filteredarray[] = array($name,$value);
					}

				} else
				if ($field == 'value')     {

					if ($filter == '') $filteredarray[] = array($name,$value); else
					if (strstr(strtolower($this->$name), $filter))   {
						$filteredarray[] = array($name,$value);
					}

				}

			}

		}

		@sort($filteredarray);
		return $filteredarray;
	}


	function filteruntranslated()  {
		$english = new language("setup/fullenglish");
		$langarray = get_object_vars($this);
		$skip = '    groupid internalnamelist downloadcontent sourcedata catselector_indent cutoff dateformat_comments dateformat_regular edit email_emaillinktitle nav_separator pageselection_left pageselection_right subcats title_divider commenttypes  ';
		while(list($name, $value) = each($langarray))    {

			if (!strstr($skip, ' '. $name .' '))    {

				if ($value == $english->$name) $filteredarray[] = array($name,$value);
			}

		}

		@sort($filteredarray);
		return $filteredarray;
	}


	function downloadcontent()  {
		return $this->downloadcontent;
	}


	function removeduplicates()  {
		global $inadmindir;
		$list = explode(',', $this->allnames());
		$num = sizeof($list);
		return $duplist;
	}


	function append($filename, $groupid)  {
		global $itemsadded, $debug, $inadmindir;
		$result = true;
		$testlang = new language($groupid);
		$oldlangnames = $testlang->allnames();
		$text = fileread($filename);

		if (stristr($text, 'INSERT INTO {PREFIX}'))    {
			echo "Cannot append using 2.3x style language. Please use a 2.40+ language file.";
			$result = false;
		} else   {
			$thestuff = explode('|||END LINE|||', $text);
			$num = sizeof($thestuff);
			for ($c=0; $c<$num; $c++)    {
				$line = explode('|||DIVIDER|||', $thestuff[$c]);
				$name = decodesqlline($line[0]);
				$value = decodesqlline($line[1]);

				if (($name != '') && (!stristr(','. $oldlangnames .',', ','. $name .',')))     {
					$stufftoappend .= $name .'|||DIVIDER|||'. $value .'|||END LINE|||';

					if ($debug == 1) echo "$name was not found so its being inserted. <br>";
					$itemsadded .= '|||'. $name .'|||';
				}

			}


			if ($inadmindir) $path = '../';
			$path .= 'languages/'. $groupid .'.lng';

			if ($stufftoappend != '') {
				$test = fileappend($path, $stufftoappend);

				if (!$test) echo "Cannot write to the file $path! You must chmod it to 666.";
			}

		}

		return $result;
	}

}

?>

 

So I changed line 46 from $this->$name = $value; to $this->name = $value;

 

That fatal error disappeared and I got another one:

Link to comment
Share on other sites

Fatal error: Cannot access empty property in /home/www/radioaccount/radio/classes/category.php on line 128

 

This is the original PHP4 code: http://file.bg/f173780QdMkp

 

and i've changed line 124 the way I've described above. Then everything starts to work, except i get {LANG_NAVORIGIN} instead of the real name of the category in the header.

 

I had one more identical fatal error, not with categories, but with links.

 

The code is just like the problematic

 

if (!$noparsing && !$isnew)  {
			$catloop = explode(',', $settings->parsecodecategories);
			for ($x=0; $x<sizeof($catloop); $x++)   {
				$this->$catloop[$x] = dowsncodes($this->$catloop[$x]);
			}

			$catloop = explode(',', $settings->smiliescategories);
			for ($x=0; $x<sizeof($catloop); $x++)   {
				$this->$catloop[$x] = dosmilies($this->$catloop[$x]);
			}

			$catloop = explode(',', $settings->linebreakcategories);
			for ($x=0; $x<sizeof($catloop); $x++)   {
				$this->$catloop[$x] = str_replace("\n", "<br>", $this->$catloop[$x]);
			}

			$this->description = parsemessage($this->description);

			if ($settings->wordwrap > 0) $this->description = wraplines($this->description, $settings->wordwrap);
		}

 

But it is

 

if (!$noparsing && !$isnew)  {
			$linkloop = explode(',', $settings->parsecodelinks);
			for ($x=0; $x<sizeof($linkloop); $x++)   {
				$this->$linkloop[$x] = dowsncodes($this->$linkloop[$x]);
			}

			$linkloop = explode(',', $settings->smilieslinks);
			for ($x=0; $x<sizeof($linkloop); $x++)   {
				$this->$linkloop[$x] = dosmilies($this->$linkloop[$x]);
			}

			$linkloop = explode(',', $settings->linebreaklinks);
			for ($x=0; $x<sizeof($linkloop); $x++)   {
				$this->$linkloop[$x] = str_replace("\n", "<br>", $this->$linkloop[$x]);
			}

			$this->description = parsemessage($this->description);
			$this->text = parsemessage($this->text);

			if ($settings->wordwrap > 0) $this->description = wraplines($this->description, $settings->wordwrap);

			if ($settings->wordwrap > 0) $this->text = wraplines($this->text, $settings->wordwrap);
		}

 

So, I just need the correct change for language.php and category.php, while onelink.php is similar.

 

PS Sorry for the two posts, I thought I would be able to post the original PHP file here!

Link to comment
Share on other sites

Here's a slight possibility for a php4 vs php5 explanation for this. IF (a big if) php4 produced an empty array when exploding an empty string, the for() loops would not run at all, whereas php5 definitely produces an array with one empty string element in it when exploding an empty or a non-existent string and the for() loops will run through one iteration.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.