Posts

I had a bug recently which pointed to a lack of delegate support for Little CMS inside my ImageMagickPHP install. Installing using Homebrew proved unsuccessful at first, as the defaults do not appear to support it, and most signs indicated that I was doing the right thing (curse you, internet).

I had tried:
brew install imagemagick

but this only installed a very limited set of modules and delegates. Some hours of searching and experimenting later, I stumbled upon a flag that you *only* have to use when installing via Homebrew (as opposed to compiling from source or using MacPorts).

The command that finally worked was:
brew install imagemagick --with-little-cms2

For whatever reason, the old install flag for Little CMS has been replaced by a second version, without any indication that the old one no longer works.

Hopefully this saves someone a dozen hours of searching and compiling. Homebrew is a timesaver – until it isn’t.

If you’re coding for the web, you almost certainly have encountered frameworks that make use of the Dependency Injection Container. Incredibly useful – but what is it?

In the most simplistic way – it’s just a great big bag of stuff. That’s it. There’s nothing magical about the container, it’s simply an object that stores references to other objects. Combined with a liberal use of Interfaces, this allows for a high level of abstraction and therefore also aids in unit testing smaller code parts.

For example, if you’re injecting the entire DIC into a component, you are metaphorically handing that component a great big bag of stuff and saying “here’s what we have to work with. Ask for it by name.” The component may then access the container and all configured dependencies.

You may well now be saying “well that sounds a bit like code soup” – and you’d be right. Normally, you’d only inject the dependencies that a component requires (and not the whole bag). The major frameworks (e.g. Laravel, Symfony, Zend) feature Dependency Injection Containers and an associated service locator and dependency manager. These are no longer simply “great big bags of stuff“, but true DI systems that allow for a high level of abstraction.

For a super simple Dependency Injection Container, see Pimple (by Sensio Labs). This is a great place to get started with understanding how a system will utilise a DIC. It is super simple, and forces you to understand how things are bootstrapped and provided for your system.

For more detailed information on dependency injection, see these links:

Opening a Terminal to the extracted directory, I then compiled the code:

PowerShell

1

2

3

$cdicu/source

$./runConfigureICU MacOSX

$make&&sudo make install

As I did not have autoconf, I installed it using Brew. Alternatively, you can compile it from source if you do not have Brew.

PowerShell

1

$brew install autoconf

The next few steps are where I had some problems. I was getting strange errors:

fatal error: 'php.h' file not found
and"ERROR: `phpize' failed"

Digging around, it turns out there’s a bit of a strange bug here. It can be quite simply solved by downloading/updating XCode from the App Store and symlinking the XCode SDK headers to the MAMP directory:

I don’t always plan what I’m going to write about. Usually I just plod along in my daily grind and problems present themselves. I spend time figuring out what’s going on, and if the solution is somewhat weird, I try to explain why I thought so.

In the last twelve months or so, I’ve taken up residence at a Berlin startup called Arzttermine.de. They specialise in taking appointments for doctors online. It also lets me forget about agency work for a while and instead focus on developing a single piece of software.

…Aaand still here we are. The problem with the platform we had was that it was… old. It was built in about 14(!) days from scratch using a very old (scratch-built) CMS platform. A miracle by any stretch of investor imagination. After number of, let’s say, less-than-qualified contractors worked on it, it became a staggeringly bloated mess full of untestable spaghetti-code.

At the moment, it feels like I’m poking my head up from underwater, as the codebase is somewhat refactored and new features can be developed again without big if/else blocks wrapping method calls. I wish I worked on something interesting in the last twelve months. I like breaking down my problems into dumb little pieces and then explaining it online. I’ve been told more than a few times that my posts are helpful, and that is amazing to know.

Due to this lack of work-related creativity, I’ve started producing trite little YouTube videos that some people appear to like. If this is your thing, give it a quick look and say hi. If it isn’t, well come to Twitter and say hi anyway. I welcome the distraction from work.

Take the headache out of saving your Snapchat files from Part 1 by automating the entire process using Tasker.

Edit: This post is now obsolete! Snapchat have upgraded their app, and since the time of writing, many free apps have appeared on the Google Play and Apple App Stores.

In the previous instalment, we set up our rooted device to save Snapchat files using a handy little script. Although this works, it’s quite tedious to repeatedly have to call the script whenever you receive a snap. To save ourselves some time, we’re going to set up our script to run automatically using Tasker.

A plugin for Tasker called Secure Settings. I’ve found that Tasker’s built-in “Run as root” option does not work for this, but this plugin works beautifully.

Set up
I recommend reading through this guide before starting. The process is rather long but quite simple.

Open up Tasker. Click “+” to create a new Task and select the “Application” context option. Find Snapchat from the list and tap to select it. Click “App selection” in the corner to go back to the main screen once that’s selected.

Step 1

Step 2

Step 3

Now that we have a context specified, we need to tell Tasker what to do when the app launches. Click “New Task” to open up the task creator, then click “+” to add an action.

Step 4

Step 5

From the popup, select “Plugin” from the list of available actions, then choose Secure Settings.

Step 6

Step 7

We now need to configure exactly what our Secure Settings action is doing. Click “Edit”, then find “Run Command”. On the “Run Command” configuration screen, enter the path of your script from part 1. In my example, this is /data/snapchat.sh. Note: I’ve since updated the path due to some people having difficulties with /etc.

DO NOT FORGET to check the box labelled “Use Root”, as this is responsible for ensuring that the script runs with the proper permissions (and actually does something). Also, I like to enable “Show Notification” so I know that the action is running.

After naming the action, you can save your configuration by clicking the save icon (bottom left) or by tapping the “Run Command”/back header in the top corner and selecting the save option from the popup dialog.

Step 8

Step 9

Step 10

Click the headers in the corner to go back to the main screen. This will also serve to save your changes. Once this is done, you should be able to boot up Snapchat for a test run.

Step 11

Step 12

If you didn’t run into any trouble throughout the guide, Snapchat should now trigger your script, saving any downloaded snaps to your specified directory.

Tips
There is some weird behaviour that may pop up when using the script/Tasker combo. Here are some things I’ve encountered during my time with the script and how to work around them.

The script is not saving snaps
This is either because 1) the script is faulty; or 2) Snapchat has not downloaded your files. As there is occasionally a delay, I like to load Snapchat, wait for the files to load, then check my gallery for the files. The usual culprit is poor phone reception.

The saved snaps are not showing in the gallery
This is the most annoying “bug” that I encounter. Android caches the thumbnails for the gallery, and the script bypasses any media refresh hooks. You can manually refresh the gallery by rebooting the phone, taking a photo, or use an app. I used SD Card Rescan and it works amazingly. It can take a while to scan fully, depending on the size of your card, so bear that in mind.

SO MANY DUPLICATES
Due to the nature of the script, it is very indiscriminate when it comes to saving files. It will always attempt to save whatever it finds, duplicate or not. I just delete them.

These are the three main things I run into regularly. I’m happy to investigate any other weird things posted to the comments.

Hopefully this guide made sense, and you are now happily (read: sneakily) saving your incoming Snapchats. Feel free to leave a note below if this worked/did not work for you.

If you’re using Snapchat, chances are you want to save some of the pictures and/or videos you receive. Now you can.

Edit: This post is now obsolete! Snapchat have upgraded their app, and since the time of writing, many free apps have appeared on the Google Play and Apple App Stores.

I should start with a preface: You probably shouldn’t do this. Half the fun of Snapchat is knowing it’s a one-off, and there’s a sort of “honour among thieves” type of policy between users. That being said, I do like a challenge, even if a solution does make me look creepy.

What you’ll need

I assume that if you’re reading this, you have Snapchat. If you don’t, you can grab it from one of the links on their website.

An Android phone. So far, this solution only applies to Android. I’m currently playing around with a solution for Apple devices.

A rooted phone. I probably haven’t thought hard enough about the problem, but from what I can tell, you need to have root access for this to work.

The most useful tool of all – a Terminal Emulator. There are heaps of apps out there for changing permissions and fiddling with the filesystem, but nothing is better than the ol’ terminal.

When executed (before viewing anything), the script checks the two storage locations for Snapchat (images and video), loop through the list of files, and copies the data to a safe location (a place the app can’t touch). The unusual part of the script is the use of the cat command, which we need due to some filesystem limitations within Android. Also, keep in mind that the paths above are for my device (Samsung Galaxy SIII, running Jelly Bean 4.1). You may need to update them if the script is returning errors.

Setting up the script

If you are Unix-savvy, you probably already know how to do this. You can jump down to the “how-to” section below for some notes.

If you’re a little Unix-rusty, here’s what you need to do to get this script working.

First, copy/paste the script above into a new text file. Save it as whatever you like and add the extension .sh. I called mine snapchat.sh.

Transfer the script onto your phone using Android File Transfer (Mac) or just in Explorer (Windows). I usually just stick it in the sdcard’s “Downloads” folder. It actually doesn’t matter, as this location is temporary anyway.

Open the Terminal Emulator. First switch to the root user by running the su command.

Shell

1

su

Then, make the internal system writable by running:

Shell

1

mount-orw,remount/system

If this step isn’t working, you can try this app instead. It’s what I use, as it offers a one-click solution.

Now that you’re root, we need to move the script to a place on the system to allow us to execute it without any permission errors. Thanks to an Android limitation, this will need to be done using cat. Note: Some people appeared to be having problems copying to /etc. I’m now using /data instead to avoid any difficulties. Everything except the path is the same.

Shell

1

catsnapchat.sh>/data/snapchat.sh

This outputs the contents of the script into a new copy located in /data. Feel free to change this location to basically anything on the phone’s internal storage.

Remove the temporary copy of the script (not essential, just tidy):

Shell

1

rmsnapchat.sh

Once this is done, navigate to the folder you copied the script into (on the phone’s storage), and add execute permissions using chmod.

Shell

1

2

cd/data/

chmod777snapchat.sh

All that’s left is a bit of cleanup:

Shell

1

mount-oremount,ro/system

This will remount the internal storage as read-only (as it was before).

As far as set-up is concerned, that’s (thankfully) it!

How to

Now for the best part – saving files. You will need to use Terminal Emulator (or equivalent) to do this. Be aware that you need to use su, otherwise the script won’t find the files due to permission restrictions.

“Snap received!”

Open Snapchat and wait until your snaps are loaded. Do not open them. I can’t stress this enough. If you open them, the script will not find them.

Jump to your Terminal Emulator and run:

Shell

1

2

su

/data/snapchat.sh

This will execute the script and tell you what it does (as it’s doing it).

Once that’s complete (assuming no errors), you can view your snaps as normal. A copy should have been made in the location specified by the script (in my example, this is /storage/extSdCard/snapchat/)

And that’s all there is to it! Unfortunately, this process needs to be repeated every time new snaps come in. However, in part 2, we’ll be automating the whole process using Tasker, meaning that once you open the app, your files are saved. Very sneaky.

Here’s a WordPress snippet to remove RSS feeds for comments while keeping the main one available.

RSS is super nifty. It’s a great way to keep on top of breaking news and articles from your favourite blogs and/or aggregators (I’ve got over 100 subscriptions in my feedly reader).

So it’s a wonder then that WordPress offers RSS feeds for comments on all your posts. I presume there’s a group of people who really like to track conversations, but in my experience, it’s the minority. This functionality should really be an additional (and not core) part of WordPress. If you want to prevent your site serving pointless additional RSS feeds, you’ll need to remove them from wp_head.

Removing them, however, is a two-step process. Add the following to your functions.php file:

PHP

1

2

3

4

5

6

7

8

9

10

11

12

// First, we remove all the RSS feed links from wp_head using remove_action

remove_action('wp_head','feed_links',2);

remove_action('wp_head','feed_links_extra',3);

// We then need to reinsert the main RSS feed by using add_action to call our function

add_action('wp_head','reinsert_rss_feed',1);

// This function will reinsert the main RSS feed *after* the others have been removed

And that’s it. The first two actions strip the RSS using remove_action with a high priority. The final add_action call tells WordPress to load our function when processing wp_head. This will go through and reinsert the main feed, leaving the comment feed out.

However, I – like many others, it seems – can’t get the hooked functions to execute. They simply don’t fire. To illustrate the problem, here’s an example from the code:

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

define('MY_INSTAGRAM_PLUGIN_PATH',plugin_dir_path(__FILE__));

require_onceMY_INSTAGRAM_PLUGIN_PATH.'my-instagram-widget.php';

$my_instagram=newMy_Instagram();

classMy_Instagram{

/**

* Standard constructor to initalise hooks and options

*

* @return void

*/

publicfunction__construct()

{

register_activation_hook(__FILE__,array($this,'install'));

register_deactivation_hook(__FILE__,array($this,'uninstall'));

}

/**

* Set up the table and options

*

* @return void

*/

publicfunctioninstall()

{

...

}

/**

* Drop the table and options associated with the plugin

*

* @return void

*/

publicfunctionuninstall()

{

...

}

}

I would fully expect the system to execute the functions hooked within the constructor. Had that been the case, I wouldn’t have lost a day debugging a non-existant problem.

Finding a solution

Of course, everyone’s setup is different. There were many suggestions for fixing the problem. For the sake of usefulness, here’s the other major suggestion:

Your global variables aren’t global

If (like me), your plugin is a global, you actually need to explicitly define it as such with the global keyword. For example, the snippet

PHP

1

$my_instagram=newMy_Instagram();

should instead by written this way:

PHP

1

2

global$my_instagram;

$my_instagram=newMy_Instagram();

Why? Well, for this, we need to consider how the plugin has its code loaded. After installation, your plugin has its code included “normally” with standard includes. This creates the global scope that you would expect. However, when you register a function with register_(de)activation_hook, it is called from within another function’s scope, effectively hiding your non-explicit globals.

Confused? Consider the plugin example from earlier. The variable $my_instagram is declared in the global scope, with global visibility. When your code executes, your plugin is included at the same scope, meaning you will have access to the variable (as they share the same suitably-high scope). Due to the way the hook/filter system in WordPress works, your activation/deactivation hooks are executed within a localised (not global) scope.

Screen beans – the only thing worse than scope.

This scope-confusion applies to all functions created using create_function, or (in our specific example), functions called with call_user_func (and call_user_func_array). These functions have their own scope and are thus shielded from the cheap-and-nasty pile of implicit “globals” that haven’t been properly declared as such.

You can find a suitably more technical (and probably correct) write-up at the WordPress Codex page for register_activation_hook.

tl;dr: Explicitly declare your globals if you want them to work everywhere.

Ok, that’s great, but what’s the answer?

After having accidentally (and rather unwillingly) been given a very rough refresher on scope, I found that this was not my problem at all. Even running proxy functions (which eliminates scope issues) failed to call the functions specified in the hook.

After much trial and error (and I mean much), I found that the paths within register_(de)activation_hook were not matching the path found in plugin_dir_path. In fact, __FILE__ was returning a completely different path.

The problem? I had symlinked the theme directory in my install, meaning that __FILE__ returned a different path to plugin_dir_path. This is especially important, as the file path specified by the first parameter of register_(de)activation_hook is included before the function specified by the second parameter is called. So, if the path in the first parameter is wrong, you probably aren’t going to be executing many functions today.

The Fix

This is always my favourite part. To fix the symlinking problem, you just have to be very specific when you’re asking WordPress to execute a hook for you.

The most important changes occur on lines 1, 16, and 17. The first line fixes the definition by manually specifying the path using the base plugin directory (instead of an automagic WordPress search). Lines 16 and 17 specify the main plugin file itself as the path (instead of relying on __FILE__).

This allowed WordPress to find the plugin files without a hitch, which is especially important in the installation/uninstallation phases.