Markus Hedlund

Developer / Photographer

Contact / GitHub / Instagram

Work With Me

PHP escapeshellarg with unicode/utf-8 support

By default escapeshellarg will strip any unicode characters. You can in some cases solve this by setting the locale to a utf-8 variant, but that might not always work.

Another way to do this is to write a custom escapeshellarg function:

function mb_escapeshellarg($arg)
{
    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
        return '"' . str_replace(array('"', '%'), array('', ''), $arg) . '"';
    } else {
        return "'" . str_replace("'", "'\\''", $arg) . "'";
    }
}

The code above is translated from the C source of PHP.

Below is the C code for this function from the file ext/standard/exec.c, if you want to check. Copyright held by respective authors according to the license.

/* {{{ php_escape_shell_arg
 */
PHPAPI char *php_escape_shell_arg(char *str)
{
    int x, y = 0, l = strlen(str);
    char *cmd;
    size_t estimate = (4 * l) + 3;

    TSRMLS_FETCH();

    cmd = safe_emalloc(4, l, 3); /* worst case */

#ifdef PHP_WIN32
    cmd[y++] = '"';
#else
    cmd[y++] = '\'';
#endif

    for (x = 0; x < l; x++) {
        int mb_len = php_mblen(str + x, (l - x));

        /* skip non-valid multibyte characters */
        if (mb_len < 0) {
            continue;
        } else if (mb_len > 1) {
            memcpy(cmd + y, str + x, mb_len);
            y += mb_len;
            x += mb_len - 1;
            continue;
        }

        switch (str[x]) {
#ifdef PHP_WIN32
        case '"':
        case '%':
            cmd[y++] = ' ';
            break;
#else
        case '\'':
            cmd[y++] = '\'';
            cmd[y++] = '\\';
            cmd[y++] = '\'';
#endif
            /* fall-through */
        default:
            cmd[y++] = str[x];
        }
    }
#ifdef PHP_WIN32
    cmd[y++] = '"';
#else
    cmd[y++] = '\'';
#endif
    cmd[y] = '\0';

    if ((estimate - y) > 4096) {
        /* realloc if the estimate was way overill
         * Arbitrary cutoff point of 4096 */
        cmd = erealloc(cmd, y + 1);
    }
    return cmd;
}
/* }}} */

Install a locale on Ubuntu 11.10

To see what locales are installed, run

locale -a

To see what locales are available for installation, run

less /usr/share/i18n/SUPPORTED

To install the en_GB locale, run

locale-gen en_GB

Tip: Leave out the charset and every charset will be installed for that locale.

Change time zone on Ubuntu 11.10 from command line

It's actually very easy.

dpkg-reconfigure tzdata

Just run that and select where you live. Verify that the time is correct with the date command.

Get back missing Photoshop from Adobe Bridge CS5.1

Today when I had finished post-processing some photos, I noticed that the Photoshop sub-menu had disappeared from the Tools menu. After searching the Interwebs and my computer, I found a solution.

Copy the folder C:\Program Files (x86)\Common Files\Adobe\Startup Scripts CS5\Adobe Photoshop to C:\Program Files (x86)\Common Files\Adobe\Startup Scripts CS5.5. Then restart Bridge. It should now tell you that there is a new Startup Script, which you want to enable. Good luck!

Get back Acer 3820TG eRecovery / D2D / Alt + F10

The first thing I did after unpacking my Acer laptop, was to wipe it and install a clean version of Windows. Little did I think of creating any restore DVDs; when would I ever need those?

Well, now I did. And after lots of searching I almost succumbed to Acer and order their rescue pack. Good thing I didn't though, since I've now found a solution.

Disclaimer: I do not take responsibility for any data loss or damage that may happen to your devices.

Note: You should first verify that you have the restore partition intact before you proceed. Fire up some partition manager and look for a partition that is ~13 GB, and invisible in Windows Explorer.

The solution

  1. First you should make sure that D2D isn't just disabled in the BIOS. If it was, that might've been your problem.
  2. If it's not: Disable it.
  3. Boot into Windows, download and install Partedit32.
  4. Open Partedit32 and you will see a grid of input fields. Every row is a partition, putting the cursor in a text field should reveal the partition's size.
  5. You must now change file system identifier of the restore partition, so Windows recognize it. Locate the partition that is ~13 GB of size and has 27 in the first field. Change 27 to 07, save and reboot. A new drive should now be present in Windows Explorer.
  6. Open up Run, enter compmgmt.msc and press OK. Go to Disk Management, right click on the rescue drive and select Mark Partition as Active.
  7. Reboot. You should now be booted into Acer's restore mode.

Good luck!

Source: http://ezinearticles.com/?The-Acer-D2D-Erecovery-101&id=1160163