Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Textpattern clean urls with Lighttpd, the elegant way

I love this shit:

server.error-handler-404 = "/index.php"

Switching a working copy with subversion

If you find that the location to a subversion repository has changed, do this:

svn switch --relocate http://textpattern.com/svn/repos/current/textpattern http://svn.textpattern.com/current/textpattern


Where the old url is http://textpattern.com/svn/repos/current/textpattern

And the new one is http://svn.textpattern.com/current/textpattern

To see if it worked, do this:

svn info | grep URL


And you should see something like this:

URL: http://svn.textpattern.com/current/textpattern


(source: Switching a Working Copy)

Textpattern form for Paypal shopping-cart integration

A site I've been working on has a retail area which uses Paypal's shopping cart; since the whole site is Textpattern powered, the necessary HTML for each item is generated by a form. This code is based on 1.0rc1 and using the rei_show_custom plugin. I know that in rc3 there's probably something built in to pull out arbitrary custom fields, so just substitute that.

To use, add each product as an article; in the first custom field put the name you want passed to the cart, and in the second custom field put the price of the item. Then make sure this is called by the article form:

<fieldset>
<legend>Buy this product:</legend>
<p>Our fine <txp:rei_show_custom /> is available for the low, low price of $<txp:rei_show_custom customid="2" />.</p>
<p><label for="os0">What sort of <txp:rei_show_custom /> would you like?</label></p>
<form action="https://www.paypal.com" method="post">
<p><input type="hidden" name="on0" value="variable" /><select name="os0" id="os0">
<option selected="selected" value="type1">Type 1</option>
<option value="type2">Type 2</option>
<option value="type2">Type 3</option>
</select>
</p>
<p>
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="business" value="yourbiz@yourdomain" />
<input type="hidden" name="item_name" value="<txp:rei_show_custom />" />
<input type="hidden" name="amount" value="<txp:rei_show_custom customid="2" />" />
<input type="image" id="add_to_cart" src="/path/to/your_cart_image" name="submit" alt="Add this item to your cart" />
<input type="hidden" name="add" value="1" />
</p>
</form>

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<p>
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="business" value="yourbiz@yourdomain" />
<input type="image" id="view_cart" src="/path/to/your_view_cart_image.png" name="submit" alt="View all items currently in your cart" />
<input type="hidden" name="display" value="1" />
</p>
</form>
</fieldset>


That'll display both an "add to cart" and a "view cart" button.

You'll want to fill in your own Paypal account address and product options, of course.

Upload all .gif and .jpg files in a directory to textpattern

This will upload all files in the current directory ending in jpg jpeg or gif through your textpattern-admin panel to your site.

Save this as a shell script, adjust the first 3 variables and excute. (Note: LOGIN and PASS should be ascii only, or else manually url-encoded)

URL='http://www.mysite.com/textpattern/'
LOGIN='simplename'
PASS='yourpassword'

COOKIE=$(curl -s -D - -d "p_userid=$LOGIN&p_password=$PASS" \
    $URL | head -n10 | sed -n 's/^Set\-Cookie\: //p')

if [ -z $COOKIE ] 
then 
  echo "Can't log in."
  exit 1
else 
  echo "Cookie: "$COOKIE
fi


for file in $(ls -1|egrep '(gif)|(jpe?g)$') ; 
do
 echo "Sending "$file
 curl -s -H "Cookie: $COOKIE" -F "thefile=@$file" \
   -F "event=image" -F "step=image_insert" $URL > /dev/null
done


<strike>This will not output anything.</strike> But after it returns to the prompt, log in to textpattern and take a look into the image section.

Update: Added rudimentary check wether login is successful and basic progress meter.

Clean Textpattern install from the current repository

In a shell, cd to the directory from which you'd like to serve the site, and rename or delete any '.htaccess' or 'index.php' files, and any directories called 'images' or 'files', that may be there.

Checkout the current code:

svn co http://svn.textpattern.com/current/ .


(Note the dot at the end). To complete the installation, just load http://thesiteurl/textpattern/ in a browser, fill in your database info, and when presented with the config.php block, copy it, go back to the shell client and:

nano textpattern/config.php


Paste in the code block, save and exit (ctrl-x, then y, then return).

Back to the browser, click through, add personal details, and that, as they say, is it.

Whenever you want to update to the latest code, cd to the same directory and:

svn update


...and the latest code will be seamlessly integrated with your installation.