[TYPO3-english] Run PHP code

Xavier Perseguers xavier at typo3.org
Wed Nov 20 08:55:00 CET 2013


Hi Mike,

This question should be asked in developer list, this is definitely not
a general user question.

Mike Kane wrote:
> Greetings everyone.
> 
> I'm trying to do a self-made solution to deal with expiring usergroups
> (itypo_expiring_fe_groups) with some PHP code... I made some successful
> tests in XAMPP in a "normal" database, but when i tried to apply this to
> typo3, it was a big failure, as it does nothing.
> 
> Here it is my code ( used page_php_content extension to place it in one
> random page)

page_php_content does not exist anymore on TER, why? because it has long
ago been marked as "insecure", please DO NOT USE IT anymore.
=> Create a simple extension (e.g., using EXT:kickstarter to help you).

> <?php
> 
> $exp_date = mysql_query("SELECT tx_itypoexpiringfegroups_groups,
> SUBSTRING(tx_itypoexpiringfegroups_groups,15,10) FROM fe_users WHERE
> uid=24702");

Please ALWAYS use the TYPO3 database API

$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
	'uid, tx_itypoexpiringfegroups_groups',
	'fe_users',
	'uid=24702'
);

(why are you substring'ing in MySQL ???)

> 
> while($row = mysql_fetch_array($exp_date))

while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {

	// See below

}

// Please free the resources!
$GLOBALS['TYPO3_DB']->sql_free_result($res);



>  {
>  echo (strftime ( '%b-%d-%Y, %H:%M', $row
> ['SUBSTRING(tx_itypoexpiringfegroups_groups,15,10)'])). "<br />";

at least you should use an alias in your query when you have a SQL
expression...



	// You have something like that: "23|1383569880|1415105880*"
	$groupParts = explode('|', $row['tx_itypoexpiringfegroups_groups']);
	// Seems that with SUBSTRING(..., 15, 10) you are looking for the last
part without the asterisk:
	$timestamp = substr($groupParts[2], 0, -1);


>  $teste = mysql_query("UPDATE fe_users SET usergroup='2' WHERE
> date(from_unixtime(SUBSTRING(tx_itypoexpiringfegroups_groups,15,10))) <
> date(now())");

	// Oh .... :'(
	if ($timestamp < $GLOBALS['EXEC_TIME']) {
		// Update corresponding record right away or (better) aggregate uid
and batch update after the while loop
		$GLOBALS['TYPO3_DB']->exec_UPDATEquery(
			'fe_users',
			'uid=' . intval($row['uid']),
			array(
				'usergroup' => 2,
				'tstamp' => $GLOBALS['EXEC_TIME'],
			)
		);
	}

> If by this code is not very clear what i want to do, the main goal is to
> read the date of expiration of the membership (i used SUBSTRING because
> the field has other information like this "23|1383569880|1415105880*"
> and then compare it with the date of the server....If the server date is
> more recent than the expiration date, the usergroup must be changed.

Just a side note for your original code, why are you converting
timestamp to human readable dates ("date()", "from_unixtime"), this just
does not make sense.

> Like i said before, i ran tests in Apache with a normal database and it
> worked perfectly, so maybe this is a wrong way to do what i need.

The wrong way is to create crazy queries instead of having clear and
easy readable business logic where it belongs in this case => PHP code.

The other wrong way is to hack something in TYPO3 with the few PHP you
seem to know instead of looking at existing (simple) extensions to "know
how TYPO3 works and how I should write my code properly".

Kind regards
Xavier

-- 
Xavier Perseguers
Release Manager TYPO3 4.6

TYPO3 .... inspiring people to share!
Get involved: http://typo3.org



More information about the TYPO3-english mailing list