Smarty regex_replace

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Smarty regex_replace

Post by Don Chambers »

I know s9y 1.3 included a security fix regarding this function, so perhaps that is the problem...

Anyway, when inserting media into an extended property field, s9y's default behavior is to insert the path to the media item like this: /uploads/folder/file.ext

The reality is that someone COULD insert a path that includes the site's full url, ie http://www.example.com/uploads/folder/file.ext.

So, we might, or might not have, the url, but I definitely NEED the url. Judebert suggested replacing the leading "/" with whatever I wanted, which would be $serendipityBaseURL:

Code: Select all

{$var|regex_replace:"^/":"$serendipityBaseURL"}
Only that's not working..... I get this error:

Code: Select all

Warning:  preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: No ending delimiter '^' found in \full_path_to_server\bundled-libs\Smarty\libs\plugins\modifier.regex_replace.php</b> on line 32
Is this just a matter of smarty not having a "beginning of line" character?

Is there a better way to accomplish my objective?
=Don=
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Smarty regex_replace

Post by garvinhicking »

Hi!

Try:

Code: Select all

{$var|regex_replace:"@^/@":"$serendipityBaseURL"}
Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

Thanks Garvin - that worked... but revealed a problem with my approach.

The above method works fine with s9y is installed in the domain root.... however, when s9y is installed in a folder, such as www.example.com/serendipity/, this is how the media library inserts media into an extended property field:

relative_path/upload_path/file.ext.

So, prepending $serendipityBaseURL to the ep_field results in:

http://www.example.com/serendipity/sere ... s/file.ext.

Suggestions?

EDIT: Now that I think about it, why does the media library insert the relative path at all? If a s9y page is already at or below example.com/serendipity, then <img src="upload_path/file.ext"> is sufficient to show the image, or am I missing something???
=Don=
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

I am still looking for suggestions on this issue, if anyone has any.
=Don=
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

Sorry, Don. So busy...

It sounds like the media paths don't have a relative path, but an absolute path: starts with /, includes the s9y subfolder. It's not a complete URL, though, as it doesn't include the http:// or the domain.

Is that an accurate assessment? Are we sure it's the same when Serendipity is installed in multiple levels of folder, i.e, /site1/blogs/serendipity or something?
Judebert
---
Website | Wishlist | PayPal
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

When you use "uploads/image.jpg" it will work in your overview page, but if you use url rewriting and access your article from "archives/1-blabla" and use "uploads/image.jpg" it would look inside the path "archives/uploads/image.jpg" which doesn'T exist.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

Thanks Judebert & Garvin.

So here is a test case:

URL to site($serendipityBaseURL): http://www.example.com/serendipity/
Upload path: uploads/
relative path: /serendipity/
relative upload path: uploads/

So, if the media library inserts a path to media as relative_path/upload_path/file.ext, (ie, /serendipity/uploads/file.jpg) and $serendipityBaseURL also contains the "relative path" (/serendipty/), how do I strip the relative path out of the string inserted by the media library so I ultimately only have http://www.example.com/serendipity/uploads/file.jpg (and also cover any issues presented by using, or not using, URL rewriting?
=Don=
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

So, what you really need is {$domain}/{$relative_path}.

We once figured out how to pull just the domain from the $serendipity_baseURL, but it was pretty ugly. There's a somewhat easier way, using Smarty reserved variables:

Code: Select all

{$smarty.server.HTTP_HOST}
I think your entire Smarty regex would look like:

Code: Select all

{$var|regex_replace:"@^/@":"$smarty.server.HTTP_HOST/"}
Judebert
---
Website | Wishlist | PayPal
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

Is there any reason not to use this?

Code: Select all

{$serendipityBaseURL|regex_replace:"@$serendipityHTTPPath@":""}
Or is this going to be a problem when s9y is NOT installed in a folder, and $serendipityHTTPPath is a "/"?

EDIT - yes, it will be a problem if serendipityHTTPPath is just /, which means it will strip the slashes from http://.

So, unless someone can think of a single method, the only thing I can think to do is this ($var is /install_path/uploads/file.jpg):

Code: Select all

{if $serendipityHTTPPath =='/'}
    {$var|regex_replace:"@^/@":"$serendipityBaseURL"}
{else}
    {$serendipityBaseURL|regex_replace:"@$serendipityHTTPPath@":""}{$var}
{/if}
=Don=
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

I suppose we could do it in PHP, too. A little something in the config.inc.php wouldn't be hard to come up with. We could even create a new Serendipity variable that included just the connection and domain, but what would we call it? $serendipity_baseBaseURL?
Judebert
---
Website | Wishlist | PayPal
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

I'm screwed on my current method too...

so I have $var, which USUALLY contains something like/relativepath/uploads/file.jpg. So, if s9y is installed in the root ("/") the leading "/" is removed, and replaced with the baseURL so I get something like http://www.example.com/uploads/file.jpg.

If s9y is installed in a folder, such as /serendipity/, the {else} would apply and serendipityBasrURL of http://www.example.com/serendipity/ is stripped of its serendipityHTTPPath (/serendipity/) and $var, which in this scenario is /serendipity/uploads/file.jpg, is added to the end, giving me http://www.example.com/serendipity/uploads/file.jpg.

So, what if $var actually contained a full URL to begin with, such as http://www.somewhere/somepath/file.jpg ?

The first scenario was originally created to detect the leading slash, which would have left this example untouched since it began with http, and not a slash. But a full URL might be provided regardless of whether or not s9y is installed in a folder.

So, I'm at a loss again.

BTW - the reason for all of this is due to feeds being emailed, such as with feedburner. Using serendipty's current media insert syntax of /relativepath/uploadpath/file.ext, an email simply does not know what URL that string is supposed to point to.
=Don=
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

How about just adding $_SERVER['HTTP_HOST'] ($smarty.server.HTTP_HOST} to the URL? That variable only holds the hostname (add "http://" before it). Then you just need to check if the string "http://" is already in the variable, and only if not prepend it to your $var

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

You're nearly there, Don. Your latest if/else code provides the proper "base", including the correct protocol (http:// or https://) and domain. So if we just combine it with the earlier regexp, which only adds the "base" if the URL starts with a slash, you're golden!

Here's what it'll look like (untested, I'm afraid):

Code: Select all

{if $serendipityHTTPPath =='/'}
    {$var|regex_replace:"@^/@":"$serendipityBaseURL"}
{else}
    {$var|regex_replace:"@^/@":$serendipityBaseURL|regex_replace:"@$serendipityHTTPPath@":""}{$var}
{/if}
In either case, if it starts with /, the $serendpityBaseURL gets substituted. If the path is a plain /, we do a straight replacement, since the $serendipityBaseURL is ready to use; if it's got extra paths, we remove them before substituting the first /.

A somewhat more complicated regex, which removes the need for the {if}, might work if positional parameters are supported:

Code: Select all

{$var|regex_replace:"@^/@":$serendipityBaseURL|regex_replace:"@^(https?://[^/]*)/$serendipityHTTPPath@":"\\1"}
Judebert
---
Website | Wishlist | PayPal
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

@ Garvin - thanks. $smarty.server.HTTP_HOST contains the host. What is the most efficient way to check the value of $var to determine if the leading portion of it contains "http://"? Am I screwed if someone enters www.something.com/whatever/file.jpg yet failed to provide "http://"

@Judebert - I think the syntax you provided is bad.... isn't regex_replace looking for QUOTE find this ENDQUOTE COLON QUOTE substitute this instead ENDQUOTE? I am getting smarty errors with your suggestions and think (but do not know) that the error is caused by syntax.
=Don=
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

Don, you're probably correct. I was hoping it would see the second PIPE symbol and start a new modification. Without it, we're going to have to capture or assign:

Code: Select all

{if $serendipityHTTPPath =='/'}
    {$var|regex_replace:"@^/@":"$serendipityBaseURL"}<br />
{else}
    {assign var='baseURL' value=$serendipityBaseURL|regex_replace:"@$serendipityHTTPPath@":"/"}
    {$var|regex_replace:"@^/@":$baseURL}<br />
{/if}
I copied this directly from my sandbox, so I know it works (caught a confusing / bug, too). Sorry for leading you down so many blind alleys.
Judebert
---
Website | Wishlist | PayPal
Post Reply