Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2019-08-06 11:06:07

Kjeld
Member
From: Tokyo, Japan
Registered: 2005-02-05
Posts: 446
Website

txp PHP does not output

After I upgraded to txp 4.7.3, the php in my articles is no longer output.

I use the <txp:php> tags, and in Preferences “Allow PHP in pages?” and “Allow PHP in articles?” are both set to “Yes”

Even a simple <txp:php>echo "Hello World!";</txp:php> gives nothing…

Any suggestions for what may be wrong?

This is the debugging output:

Tag error: <txp:php>
// Time difference from GMT: 60 * 60 * 9 hours 
// Prints: 2010 (year, Y) 
$getyear = $GLOBALS['thisarticle']['title'];
$yearthen = $rest = substr("$getyear", 7, 4); 
$yearnow = gmdate('Y', time() + 32400 );
echo $yearnow - $yearthen;
</txp:php> ->  Textpattern Notice: PHP code is disabled for pages. while parsing form calendar_front on page calendar
textpattern/publish/taghandlers.php:4399 trigger_error()
php()
textpattern/vendors/Textpattern/Tag/Registry.php:116 call_user_func()
textpattern/lib/txplib_publish.php:542 Textpattern\Tag\Registry->process()
textpattern/lib/txplib_publish.php:466 processTags()
textpattern/lib/txplib_misc.php:4407 parse()
textpattern/publish.php:976 parse_form()
textpattern/publish.php:1066 doArticles()
textpattern/publish.php:793 parseArticles()
article()

Last edited by Kjeld (2019-08-06 11:10:57)


Old Photos of Japan – Japan in the 1850s~1960s (100% txp)
MeijiShowa – Stock photos of Japan in the 1850s~1960s (100% txp)
JapaneseStreets.com – Japanese street fashion (mostly txp)

Offline

#2 2019-08-06 11:32:35

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

Re: txp PHP does not output

Just out of curiosity, where is your code called from? Is it from within another tag’s attribute or in a Form called from a Form?

The way we handle PHP injection in 4.7 has changed for security purposes. You can now only embed PHP in the first pass of a Page. So if your PHP block is inside a Form or secondary tag (perhaps used in an attribute inside single quotes), or somewhere that requires Txp to have to go back for a second look at the Page to resolve some dangling tags, you’ll get this warning.

The solution is to move the PHP block to somewhere primary – first pass. I normally resolve it by writing the PHP code in its own Form – say, one called page-setup and include that directly with <txp:output_form /> near the top of your Page code.

In that Form, assign the output of the PHP to a variable (more details):

<txp:variable name="myDate">
<txp:php>
...
</txp:php>
</txp:variable>

That should sort it.

Last edited by Bloke (2019-08-06 11:34:38)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2019-08-08 04:22:05

Kjeld
Member
From: Tokyo, Japan
Registered: 2005-02-05
Posts: 446
Website

Re: txp PHP does not output

Bloke wrote #318969:

Just out of curiosity, where is your code called from? Is it from within another tag’s attribute or in a Form called from a Form?

The way we handle PHP injection in 4.7 has changed for security purposes. You can now only embed PHP in the first pass of a Page. So if your PHP block is inside a Form or secondary tag (perhaps used in an attribute inside single quotes), or somewhere that requires Txp to have to go back for a second look at the Page to resolve some dangling tags, you’ll get this warning.

The solution is to move the PHP block to somewhere primary – first pass. I normally resolve it by writing the PHP code in its own Form – say, one called page-setup and include that directly with <txp:output_form /> near the top of your Page code.

In that Form, assign the output of the PHP to a variable (more details):

<txp:variable name="myDate">...

That should sort it.

Thanks, Bloke. That is probably the issue. I am going to try this out!


Old Photos of Japan – Japan in the 1850s~1960s (100% txp)
MeijiShowa – Stock photos of Japan in the 1850s~1960s (100% txp)
JapaneseStreets.com – Japanese street fashion (mostly txp)

Offline

#4 2019-08-14 08:59:23

Kjeld
Member
From: Tokyo, Japan
Registered: 2005-02-05
Posts: 446
Website

Re: txp PHP does not output

Bloke wrote #318969:

Just out of curiosity, where is your code called from? Is it from within another tag’s attribute or in a Form called from a Form?

The way we handle PHP injection in 4.7 has changed for security purposes. You can now only embed PHP in the first pass of a Page. So if your PHP block is inside a Form or secondary tag (perhaps used in an attribute inside single quotes), or somewhere that requires Txp to have to go back for a second look at the Page to resolve some dangling tags, you’ll get this warning.

The solution is to move the PHP block to somewhere primary – first pass. I normally resolve it by writing the PHP code in its own Form – say, one called page-setup and include that directly with <txp:output_form /> near the top of your Page code.

In that Form, assign the output of the PHP to a variable (more details):

<txp:variable name="myDate">...

That should sort it.

This is indeed the issue, Bloke. But I can’t figure out how to solve it…

This is my issue:

  1. I am making an article list
  2. In the title of each article is the year of an event
  3. Before the title I want to show how many years ago the event was
  4. Until now, I used the php function shown in my first post in this thread to pull the year from the title, subtract it from NOW and echo ‘X years ago’ before the title.

It created a list like this:

• 151 years ago • 01-01 (1868) Kobe Port Opens
The tiny port of Hyogo, soon better known as Kobe, is opened to foreign trade

• 146 years ago • 01-01 (1873) Japan Starts Using the Gregorian Calendar
Japan begins using the Western calendar and a modified traditional Japanese era calendar

• 184 years ago • 01-10 (1835) Yukichi Fukuzawa is Born
Yukichi Fukuzawa, writer, educator and founder of Keio University, is born in Osaka

• 146 years ago • 01-10 (1873) Japan Abolishes Samurai Rights
The samurai’s right to be the only armed force is abolished, and conscription is started

PHP can only be used in the first pass. That works for a single article, but not for an article list…

I am stumped and can’t think of any alternative way to add the ‘X years ago’ before the title in an article list. Any ideas or suggestions?


Old Photos of Japan – Japan in the 1850s~1960s (100% txp)
MeijiShowa – Stock photos of Japan in the 1850s~1960s (100% txp)
JapaneseStreets.com – Japanese street fashion (mostly txp)

Offline

#5 2019-08-14 10:20:15

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

Re: txp PHP does not output

Kjeld wrote #319036:

I am making an article list

Okay. So if it doesn’t work for article lists then you must have two levels of nesting somewhere in your list container/form that’s triggering a second pass. Does your debugging tag trace give you any indication what’s being called in secondpass? It should give you some info that might help narrow down where to look.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#6 2019-08-14 10:23:15

Kjeld
Member
From: Tokyo, Japan
Registered: 2005-02-05
Posts: 446
Website

Re: txp PHP does not output

I think I have found a solution to getting the year from the title and calculating the years between today and that year.

I just discovered the new txp:evaluate tag:

<txp:evaluate query='2019 - substring("<txp:title />", 8, 4)' />

I am still trying to figure out the best way to calculate the current year.

Incidentally, am I using txp:evaluate correctly here?


Old Photos of Japan – Japan in the 1850s~1960s (100% txp)
MeijiShowa – Stock photos of Japan in the 1850s~1960s (100% txp)
JapaneseStreets.com – Japanese street fashion (mostly txp)

Offline

#7 2019-08-14 10:37:54

Kjeld
Member
From: Tokyo, Japan
Registered: 2005-02-05
Posts: 446
Website

Re: txp PHP does not output

This is what I have now:

On the top of the page:

<txp:output_form form="get_current_year" />

Form:

<txp:variable name="currentYear">
<txp:php>
// Time difference from GMT: 60 * 60 * 9 hours 
// Prints: 2020 (year, Y) 
$yearnow = gmdate('Y', time() + 32400 );
echo $yearnow;
</txp:php>
</txp:variable>

In the article form for article list:

<txp:evaluate query='<txp:variable name="currentYear" /> - substring("<txp:title />", 8, 4)' />

This pulls the year from the title, subtracts it from the current year and then prints out the difference.

It works perfectly, but am I doing this correctly?


Old Photos of Japan – Japan in the 1850s~1960s (100% txp)
MeijiShowa – Stock photos of Japan in the 1850s~1960s (100% txp)
JapaneseStreets.com – Japanese street fashion (mostly txp)

Offline

#8 2019-08-14 10:38:16

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

Re: txp PHP does not output

I don’t know of any way to get the current year other than from PHP. So try this:

1. Make a new form called variables.
2. Put in it this code:

<txp:variable name="thisYear"><txp:php>echo date('Y');</txp:php></txp:variable>

3. Include it directly in your Page code:

<txp:output_form form="variables>" />

4. In your evaluate tag:

<txp:evaluate query='<txp:variable name="thisYear" /> - substring("<txp:title />", 8, 4)' />

That should to the trick.

EDIT: Ha! You beat me to it by a minute. Yes, your version’s perfect, although you might want to add escape="trim" to your <txp:variable> declaration in Form get_current_year, for completeness and to make sure you don’t get any newlines in your output :)

Last edited by Bloke (2019-08-14 10:41:04)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#9 2019-08-14 10:50:29

Kjeld
Member
From: Tokyo, Japan
Registered: 2005-02-05
Posts: 446
Website

Re: txp PHP does not output

Bloke wrote #319042:

I don’t know of any way to get the current year other than from PHP. So try this:

1. Make a new form called variables.
2. Put in it this code:

<txp:variable name="thisYear"><txp:php>echo date('Y');</txp:php></txp:variable>...

3. Include it directly in your Page code:

<txp:output_form form="variables>" />...

4. In your evaluate tag:

<txp:evaluate query='<txp:variable name="thisYear" /> - substring("<txp:title />", 8, 4)' />...

That should to the trick.

EDIT: Ha! You beat me to it by a minute. Yes, your version’s perfect, although you might want to add escape="trim" to your <txp:variable> declaration in Form get_current_year, for completeness and to make sure you don’t get any newlines in your output :)

Great minds think alike! lol

I will add escape="trim".

Thank you so much, Bloke. Really appreciate it. I doubt anybody else will find this helpful, but hopefully somebody does. The new txp:evaluate tag is awesome!


Old Photos of Japan – Japan in the 1850s~1960s (100% txp)
MeijiShowa – Stock photos of Japan in the 1850s~1960s (100% txp)
JapaneseStreets.com – Japanese street fashion (mostly txp)

Offline

#10 2019-08-14 10:57:51

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

Re: txp PHP does not output

Kjeld wrote #319044:

I doubt anybody else will find this helpful, but hopefully somebody does.

I’m sure someone will. It’s come up before and will no doubt come up again.

The new txp:evaluate tag is awesome!

Yes it is. Oleg’s Swiss army knife tag ;)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#11 2019-08-14 14:07:30

etc
Developer
Registered: 2010-11-11
Posts: 5,028
Website GitHub

Re: txp PHP does not output

For the record, <txp:php /> has been disabled on the second pass because is_article_body context is not preserved after the first pass. It means that authors privileges are not checked any more, so even low-level users were potentially able to execute php via an easy second pass <txp:php /> injection.

Another point: if your code requires more complex calculations, you can register “safe” php functions for use in <txp:evaluate /> via advanced prefs. For date calculations, please look here.

Offline

#12 2019-08-14 14:54:27

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

Re: txp PHP does not output

etc wrote #319052:

For the record, <txp:php /> has been disabled on the second pass because is_article_body context is not preserved after the first pass.

Yes. I think it’s the right thing to do anyway. I’m glad you found it when tweaking the parser.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

Board footer

Powered by FluxBB