How to protect your emails with PHP and Javascript

Working with or without javascript it’s combine two technique for email protection ROT13 and CSS

function mail($email, $text = null) {
 return '<script type="text/javascript">document.write("' .
 addslashes(
  str_rot13(
   '<a href="mailto:' . $email . '" rel="nofollow">' . ($text ? : $email) . '</a>'
  )
 ) . '".replace(/[a-zA-Z]/g,function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);}));</script>' .
 '<noscript><span style="unicode-bidi: bidi-override; direction: rtl;">' . strrev($email) . '</span></noscript>';
}

Prevent Heartbleed - upgrade openssl with brew on your mac

The Heartbleed Bug is a serious vulnerability in the popular OpenSSL cryptographic software library. Follow brew command will upgrade your openssl library:

brew install openssl && brew link openssl --force

Relaunch your Terminal (has to be hard quit ⌘ + Q) and check your openssl version:

openssl version

Now you shall have OpenSSL 1.0.1g 7 Apr 2014 or newer.

How to keep GitHub Fork Updated (no merge - the right way)

Here is solution how to keep your forks updated without merge and clean history

git clone git@github.com:[username]/[repo].git
git remote add upstream git@github.com:[username2]/[repo].git

When something change in forked repo

git fetch upstream
git checkout [branch]
git rebase upstream/[branch]

Convert JSON data to valid PHP code

Do you need convert JSON data to valid PHP code without pain? I’ve got a short script for that! My simple solution used var_export() function. This function generates almost valid, well formatted PHP code from usual JSON data. Invalidity of code can be fix with one regular expression, which replace stdClass::__set_state/ with (object).

Take a closer look at example data. I have here currency.json - PHP code should look something like this:

<?php
 return array (
  'USD' =>
  (object)(array(
     'symbol' => '$',
     'name' => 'US Dollar',
     'symbol_native' => '$',
     'decimal_digits' => 2,
     'rounding' => 0,
     'code' => 'USD',
     'name' => 'US dollars',
  )),
  'CAD' =>
  //...
);

It’s can be done in only four lines of PHP code:

$data = json_decode(file_get_contents(__DIR__ . '/currency.json'));
$code = var_export((array)$data, true);
$code = "<?php\n return " . preg_replace('/stdClass::__set_state/', '(object)', $code) . ';';
file_put_contents(__DIR__ . '/currencies.locale.php', $code);

If you need call some function (like gettext) above the variable, just add more regular expression:

$data = json_decode(file_get_contents(__DIR__ . '/currency.json'));
$code = var_export((array)$data, true);

// add gettext function call
$code = preg_replace("/'name' => '(.+)'/", "'name' => __('$1')", $code);
$code = preg_replace("/'name_plural' => '(.+)'/", "'name' => __('$1')", $code);

// save PHP code
$code = "<?php\n return " . preg_replace('/stdClass::__set_state/', '(object)', $code) . ';';
file_put_contents(__DIR__ . '/currencies.locale.php', $code);

 

Find all unnecessary images without link from code

How to find all unnecessary images in your website project?

find public/images -type f|while read line ; do printf "$(basename $line) > " && ack --nofilter -l -c "$(basename $line)" | wc -l; done

Output will be:

sprite.gif > 0
ajax-loader-bg-f9.gif > 2
ajax-loader-bg-trans.gif > 3
ajax-loader-static-bg-trans.gif > 2
bg_about.png > 2
...

If you want output filenames not just marches count remove

| wc -l

Upgrade all npm packages to latest versions

Install follow package npm-check-updates by

npm install -g npm-check-updates

and run ncu -a for upgrade package.json versions.

How to clean flash drive before unmount on Mac

How to clean flash drive and delete all hidden (dot) files on mac before unmount? It’s simple, save follow commands as flash:

#!/bin/bash

if [ -n "$1" ]; then
  read -r -p "Clean /Volumes/$1/ and unmount? [y/N] " response
  if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]; then      
    find /Volumes/$1/ -name '._*' -type f -delete
    rm -rf /Volumes/$1/.Spotlight-V100/
    rm -rf /Volumes/$1/.Trashes/
    diskutil unmount /Volumes/$1/
    echo "Done..."
  fi
else
  echo "Flash drive name missing"
fi

Then change the access mode of a file chmod +x flash. To unmount a clean flash drive just run ./flash Flashka (Flashka is name od drive).

Wordpress - get post excerpt

$excerpt = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id));

Run FTP server on Mac OS X

Setup everyhing: sudo subl /etc/ftpd.conf

# match umask from Mac OS X Server ftpd
umask all 022
chroot GUEST /Users/roman/ftp
modify guest off
umask  guest 0707
upload guest on

Start FTP server

sudo launchctl load /System/Library/LaunchDaemons/ftp.plist

And stop again:

sudo launchctl unload /System/Library/LaunchDaemons/ftp.plist

Composer require PEAR package

{
 "repositories": [
  {
   "type": "pear",
   "url": "http://pear.php.net"
  }
 ],
 "require": {
  "pear-pear/Mail": ">=1.2.0"
 }
}