Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2015-07-17 04:40:41

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 595
Website

Idea

On my own TXP sites and on a few larger TXP sites I run for a client, I often spin up new sections for little app-like projects, and I was thinking today that this might be convenient.1

Along with other per-section options, let’s say this option is available to a given section:

[x] Convert invalid URL segments to variables

Then let’s say you visit /section/example/, and there’s no article there. If you have this option turned on, instead of showing a 404 error, your browser is redirected to /section/ and now the Textpattern variable urlSegment1 has the value “example” and is available for use in the page template being used at /section/.

So you could do something like:

http://www.example.com/books/id/345

And then inside the /books/ page template (pseudocode):

if (urlSegment1 == "id" && urlSegment2 != "") {
    //query third-party database
    book_lookup(urlSegment2)
}

Thoughts? Too edge-casey for TXP? Just thinkin’.

1 And it is conveniently stolen from another CMS. Still I think this really harnesses one of TXP’s strengths—you can easily start up a new section in your browser and get coding.

Last edited by maruchan (2015-07-17 04:46:59)

Offline

#2 2015-07-17 08:00:26

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

Re: Idea

That’s just the right side of crazy to be useful.

How do you propose the passed info is made secure? Any sanitisation done before being turned into a variable?

Regardless, I’d be tempted to just stuff any future parts into an array instead of separate variables:

if (isset($urlParam[0]) && urlParam[0] === "id"
         && isset($urlParam[1]) && urlParam[1] != "") {
    book_lookup($urlParam[1]);
}

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 2015-07-17 12:24:52

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

Re: Idea

I’ve got a plugin, etc_url, that can extract (sanitized) txp-related parameters from an url. For example, <txp:etc_url url='<txp:page_url />' type="id" /> will return 345 on http://www.example.com/books/345, even if this article does not exist.

So you could include something like this at the beginning of in your error_404 page template:

...
<txp:variable name="id" value='<txp:etc_url url=''<txp:page_url />'' type="id" />' />
<txp:if_variable name="id">
    <txp:php>txp_status_header();</txp:php>
    do something special
<txp:else />
    normal error handling
<txp:if_variable>

The download site is currently offline, but I can send it by email.

It would be even cleaner to intercept the 404 error, registering a callback on textpattern that sets $pretext['status'] = 200 and $pretext['page'] = 'default' (or whatever), and include the snippet above (without txp_status_header) in your section page.

Offline

#4 2015-07-17 12:49:03

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

Re: Idea

etc wrote #293377:

It would be even cleaner to intercept the 404 error, registering a callback on textpattern

Can you use the callback in txp_die()?

callback_event('txp_die', $code, 0, $url);

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

#5 2015-07-17 13:09:56

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

Re: Idea

Bloke wrote #293380:

Can you use the callback in txp_die()?

Sure you can, but isn’t it fired too late? Your only options will be 1) redirect 2) process the error page?

Offline

#6 2015-07-17 17:46:45

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 595
Website

Re: Idea

How do you propose the passed info is made secure? Any sanitisation done before being turned into a variable?

Personally I like the idea of pushing sanitization-thinking onto end-user designers/developers and I think it’d be pretty cool to add some sanitization options to txp:variable for e.g. email, phone, etc. and then maybe adding a warning in Diagnostics and/or in Testing mode if txp:variable is in use without sanitization. But I have only broad ideas there.

I’ve got a plugin, etc_url

Love to try it. Would it support longer parameter lists like /products/motorcycles/honda, where only /products/ exists as a section in the DB?

Last edited by maruchan (2015-07-17 17:47:15)

Offline

#7 2015-07-17 20:17:05

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

Re: Idea

maruchan wrote #293415:

Would it support longer parameter lists like /products/motorcycles/honda, where only /products/ exists as a section in the DB?

No, it only can disassemble a “true” txp link. I guess I have misunderstood your question, sorry.

Offline

Board footer

Powered by FluxBB