Posted by & filed under Linux, Other Computery Things.

I work with a lot of wordpress installations for both work and play and one thing has always bugged me. The merry dance of file permissions that’s needed to get it working properly.

For example if I want to install a new wordpress onto a localhost server I grab the archive from wordpress.org and unzip it into my /var/www/ (or whatever)

Which is fine. I can edit the files in there fine and all that jazz. Except that the webserver can’t. So I can’t use the theme/plugin editors, or create new .htaccess stuff for permalinks etc. I can’t use the automatic update system without FTP details etc etc…

Anyone who’s ever found this a nightmare has also undoubtedly got caught in a seemingly endless chmod/chown -R loop to get each thing working properly. Well I think I’ve found the right order of commands to get it working so that you can edit (either in your editor of choice or in the wordpress editors) and update and well, have it playing nice.

I should note here that this is probably only suitable for a dev server, a production server should probably have slightly tighter permissions.

All the following expects you to be running as your own user, part of the sudoers, from within your web-root (above the wordpress folder, which for convenience we’ll call wordpress)

First, who owns the files?
For me I find the best owner of the files should be the webserver, group owned by my user, so the first command I use is…

sudo chown -R www-data:billythekid wordpress

Next, can users in the group edit the files?
Nope, we can’t even add files to the folders. This has undoubtedly been the hardest part to get right for me over the years. You need to have specific permissions set for certain folders to allow the uploaders/updaters to work without the FTP stuff etc. So long as the webserver is the owner and you are in the ownergroup (you are). AAAnd, folders and files need different permissions (so there goes our recursive commands eh…)

Try this out…

First, the folders
sudo find wordpress -type d -exec chmod 0775 {} \;

Now the files
sudo find wordpress -type f -exec chmod 0664 {} \;

Using the unix -exec flag with find is genius (hat tip to WombleGoneBad) as this allows us to target, recursively, just files or directories.

This should mean that you can now edit your plugins in the plugin editor, run the 5-minute-setup without needing to manually create an .htaccess file, update your plugins without putting in FTP details and so on.

If this works for you, stick a note in the comments!

Posted by & filed under Linux, Other Computery Things.

Yeah I’m an idiot, let’s get that out of the way first. I’m taking full ownership of this particular screwup. Also, this post may contain obscenities…

So… My computer’s been running slowly recently. I’d installed a whole bunch of stuff, such as gnome, lubuntu and other stuff to try to make it more lightweight. It also had some weird redraw crappiness happening and as I sometimes use this machine for work, where I often need to debug display issues, it wasn’t ideal.

“Fuck it”, thought I, “I’ll do a fresh installation. My /home directory is on another disk anyway so it’ll be as easy as pie.”

(My /home directory is on it’s own disk because I often screw up the OS and need to reinstall, this way I don’t lose anything)

…or so I thought.

Except that I changed computers about a year ago and moved all my old stuff from my other computer onto this one. I never got round to actually doing a /home partition, I just pulled over all my files and wiped the old computer to give to my daughter. I’d forgotten this important fact!

So I grabbed the latest Ubuntu and used the startup disk creator to stick it on a USB stick for the installation. Rebooted and was greeted with the grandness of the Ubuntu installer. This comes with about 5 options such as, install beside existing O/S, upgrade existing to newest(which keeps all files and settings), do a clean install (which overwrites everything on the disk) or something else (which lets you set up your own partitions etc.

I chose, foolishly believing my files were on another disk, clean install.

Bish-bash job done. Ubuntu reboots and I have a nice shiny new, zippy fast O/S. Brilliant! Time to map the /home partition to the right place…

SHIT!

Realisation hits me like a bag of hammers. I’ve not only wiped my entire hard drive, featuring *ALL* of my youngest daughter’s pictures/videos etc as well as about every handy little snippet of code I’ve ever written and my entire localhost apache stuff but I’ve also gone and written to the partition! Any security person will tell you if you want to recover your files, don’t make any changes to the disk, the more you change the more you lose, loosely speaking.

BOLLOCKS!

I was impressed by how responsive my computer was though and, being as I’m not a soppy sod I came to terms with the loss fairly quickly. It’s only pictures right. I then made what was possibly the biggest mistake, or at least it felt it at the time, as is often the case time proved this not to be true and it may be the biggest benefit… I told my girlfriend…

Three words were all I needed to hear, three words changed the course of my life for the foreseeable future, three words made it all better…

GET. IT. BACK.

My girlfriend is also a computery person. She writes programs for oil rigs and, since I know I can’t feed her bullshit about how it’s impossible and being that as she’s in oil, she’s rich and powerful and I do what she says… I get it back.

So the first thing I do is stop writing to the disk. I still have the USB disk with the installer on it but this also has a fully functional Ubuntu install on it. One of the gems of Ubuntu this. Rebooting with the USB (after tinkering with the BIOS boot order / hard disk order) had me in that instead. Now I could begin.

First thing needed was some sort of recovery software. I develop web stuff, not low level, disk cylinder reading stuff. I wouldn’t know where to begin. To the googles!

A few searches later and I had the page I needed, thanks to what else but the Ubuntu forums. Well, all but one small issue. I didn’t actually want to rewrite my MBR (what’s explained in that post) I just wanted to get my files back and leave my new Ubuntu install speedy fast. Plus, I was pretty sure that if I did try to get the whole thing back it’d get even more broken than it was at the moment. I’d not just deleted the disk to be recovered but I’d written a new OS to it too! No, I needed something else. Reading on, I found the next post in the thread which said as the guy mentioned, try testdisk and photorec a try. Photorec hmm, what is that?

It’s a little bit of software genius that I’ll forever be indebted to, is what it is.

Essentially it does exactly what I want it to do. It gets files from a scrapped drive and saves them somewhere. I know some of you can already see the flaw here. Where do I save the files? I’m using a USB stick here and I can’t write to the disk being ‘saved’ or it’ll corrupt more stuff! Step in my girlfriend’s 1Tb external hard drive.

I plug that in, unmount it, mkdir a folder on the USB drive called ‘bigdisk’ (easy now!) and mount the disk to that…

I then make a folder on the bigdisk called ‘recoveryhope’ and start up photorec in a terminal…

sudo photorec

I clicked through to choose the disk to save (I selected free space only – essentially anything that’s not already been overwritten by the new OS install) and where to save the files found, it’s easy as pie really.

At the time of writing this it’s saved over 850,000 files from the apparently wiped disk and been running for nearly 12 hours. That’s over 850,000 potential security holes just reformatting your disks would leave to a potential purchaser of an old computer etc. Makes you think how easy it is to get data.

I’m not sure how much I’ve lost with my own stupidity or indeed what’s been saved. At this time though it’s looking good that at least *some* potential nights on the couch might now be spent in my bed!

I’ll pre-emptively call this a WIN!

Posted by & filed under PHP.

Yeah, title says it all really.

I had a customer at work recently who runs an Interspire shopping cart (ISC) and wanted to place code in the <head> section of certain pages on his site. Unfortunately the head is shared throughout the site, much like a wordpress or drupal setup.

No bother, thought I, I’ll just include a PHP file in the head and in that script check against the URL of the page being called and if it’s in the list, have it display the code.

Not so easy as it turns out. Essentially, the included file only ever gives it’s own URL when included. So I thought, well, maybe I can make an HTTP request to the include then (remote include) and just pass it a GET variable, that’ll work right?

Nope.

ISC seems to collect all it’s pages and includes together and parse the PHP before it serves it, so the PHP file never actually knows what the URL of the page is. Searching the Interspire forums doesn’t help. Many have come across the problem, none have solved it…

Well I’m not having that. So here’s what you need to do…

In your ISC, find the index.php file and create a $GLOBALS['pageUrl'] variable set to your $_SERVER['REQUEST_URI']

$GLOBALS['pageUrl'] = $_SERVER['REQUEST_URI'];

See, the $GLOBALS array is available within ISC at run time via %%GLOBAL_key%% variables…

So in the headerHTML.html (or whatever it’s called, can’t remember) we can now put a line like…

%%Include.http://example.com/yourFile.php?page_url=%%GLOBAL_pageUrl%%%%

and in our php script…

if ( !empty( $_GET['page_url'] ) )
{ 
  if( in_array( $_GET['page_url'] ,$pagesArray ) )
  {
    echo 'the appropriate code';
  }
}

…and it all works!

Posted by & filed under Linux.

I had this issue where I couldn’t re-order tabs in Chrome on Ubuntu 12.04 all of a sudden.

Turns out I was logging in to Ubuntu 2D from the login screen which seems to go no faster than regular Ubuntu but disables key functionality like that! Nice one eh.

So if you’re experiencing this, log out. Check your display manager before logging back in is Ubuntu (or KDE or Lubuntu or Gnome or whatever you actually want to use) but Ubuntu 2D won’t let you reorder the tabs.

Posted by & filed under Other Computery Things.

I’ve been using git more recently and the existing cheatsheets and tutorials, while excellent, don’t give it to me in little words I can remember, in the right order with only the things I actually need.

I tried a couple of GUIs such as gitg and giggle(which I like/kept) but they don’t much help, probably because I’m quite new to it. I’ve used git before but, well, the theory hadn’t sunk-in enough for me to be confident enough to go at it with no guide to follow. (I don’t quite get what the HEAD means or it’s importance. I have no idea what happens if I have more than one (non-master) branch, can I just add files from one branch to another? Why would I even need 2 branches of the one project?… etc.)

So here’s my simple, daily use, git flow. This assumes I’m already all set up with keys and have my project in git(and .git in my local folder etc etc.)

  • check where I am…
    git status
  • if I’m in master branch, create a new branch called whatever…
    git branch bugfixes
    git checkout bugfixes
  • check that worked…
    git status
  • **fix a bug or do whatever editing the task requires – update some files**
  • check status again – should have some red files…
    git status
  • add the changed files to the branch…
    git add .
  • commit the changes with a message…
    git commit -m 'fixed this bug'
  • repeat the last few steps (edit files, add them, commit) until you’re done for the day/session/whatever
  • change back to the master branch…
    git checkout master
  • merge today’s commits into the master branch
    git merge bugfixes
  • get rid of the bugfixes branch for a fresh slate tomorrow
    git branch -d bugfixes
  • push my changes to the remote git repo (beanstalk/github/whatever)…
    git push

*Note to self* That’s ALL. Don’t go fannying around with git diff or git reset or whatever, follow just this, you’re new and you’ll only break something!

Posted by & filed under Javascript, My Scripts.

Last night I had the pleasure of attending the second Refresh Aberdeen talks. One of the speakers, Mark Kemp, gave a really interesting and passionate talk on typefaces and associated typeface geekery. He said the most overused and unoriginal font he could think of off the top of his head was Bleeding Cowboys, showing a sample of “every country and western iTunes cover image”.

Well, I’m not having that. Anything cowboy related is immediately weighted higher than that with no cowboy connection at all right? Right!

So with this in mind I grabbed a copy of said font and stuck it on an ubuntuOne drive. Then I put together this little Bookmarklet:

Drag that link to your bookmarks bar and click it to easily make any page cowboy-gasmic.

<a href="javascript:function addGlobalStyle(css){var head, style;head=document.getElementsByTagName('head')[0];if(!head){return;}style= document.createElement('style');style.type = 'text/css';style.innerHTML = css;head.appendChild(style);}addGlobalStyle('@font-face{font-family:Bleeding_Cowboys;src: url(http://ubuntuone.com/4oRhS1ZAO9QQKGTwcIu7Vw) format(truetype);}*{font-family:Bleeding_Cowboys !important}');">Cowboy Up!</a>