Take care when you use ($this) in your anonymous functions (closures) – it will be copied
Anonymous functions1 (closures) in PHP 5.3 are cool. Awesome is when you can use2 any variable with it. With PHP 5.4 you can even use $this.
The problem with use is that it is making the value available in the current scope by copying2 it, thus not modifying it upwards the chain. To over come this, simply pass by reference1.
1
$anon = function(){
// Some fancy execution right here
};
$anon();
2
$otherRandomVar = 5;
$anon = function() use ($otherRandomVar) {
// $otherRandomVar will be made available in $anon() by copy
// In this case it is not modifying its value in the global scope
$otherRandomVar = 8915 + 945;
};
echo $otherRandomVar; // 5
3 $randomVar = 'abc'; $anonFuncRightHere = function() use (&$randomVar) { // Passing by reference will solve your issue $randomVar = 'cba'; }; echo $randomVar; // cba
How to handle “The plugin generated x characters of unexpected output” errors in WordPress
Some errors are hard to trace, especially when no line of error or function/variable is
If you ever ecounter “The plugin generated x characters of unexpected output” in your WordPress plugin development, a simple solution is to include the following line in your function/method:
file_put_contents(__DIR__ . '/activation_log.txt', ob_get_contents());
This will write any output to a file in the current directory where the file containing the function/method resides.
MAMP fine tuning: enable XDEBUG and turn on error reporting
MAMP addresses developers, so I can’t really understand why these are turned off by default:
- Enable XDEBUG:
- nano/vim /Applications/MAMP/bin/php/php5.6.10/conf/php.ini
(Note: you may be using another version of PHP) - Search for “xdebug”
- Delete the semicolon
- Restart MAMP (or just Apache)
- Test with phpinfo(); It should show as compiled

- nano/vim /Applications/MAMP/bin/php/php5.6.10/conf/php.ini
- Enable PHP Error Reporting:
- nano/vim /Applications/MAMP/bin/php/php5.6.10/conf/php.ini – same file as above
(Note: replace php5.6.10 with your PHP version) - Search for “display_errors”
- Change from “off” to “on”
- Restart MAMP or just Apache
- nano/vim /Applications/MAMP/bin/php/php5.6.10/conf/php.ini – same file as above
Happy debugging!
Encoding long strings with PHP’s serialize()
Sometimes it makes sense to store in DB simple settings or blocks of text (strings) with PHP’s serialize() function – although JSON appears to be more faster (and language agnostic).
Note to self: remove any simple quotes
Note to self: Git basics
Some notes on using Git for the future me.
Setup a global config:
git config --global user.name "Gigi Palemier"
git config --global user.email "gigi@palmier.ro"
The Holy Git Figure:

TODO: branching -vs- tags / advanced workflow
To be updated.
How to clone a Linux server – install same packages onto another server
This small guide is meant for those who need to
- clone an existing server/desktop the hard way (for those who lack virtualization tools)
- start from scratch with all packages previously installed (with or without maintaining the personalized settings)
Steps:
- Get the current installed packages on the main machine: dpkg –get_selections > installed_packages.txt
- Make a copy of your old /etc/apt/sources.list and copy everyting from /etc/apt/sources.list.d/* onto your new machine
- Still on the new new machine, make sure you install dselect: apt-get install dselect
- Update to check for missing keys: apt-get update
- And add the missing keys: apt-key adv –keyserver keyserver.ubuntu.com –recv-keys XXXXXXXX
- Update again: apt-get update
- Register the new packages: dpkg –set-selections < installed_packages.txt
Now the fun part – use dselect to install the missing packages:
- dselect update
- dselect install
If you’d like the same personalized settings, simply copy the config files and folders from your /home/user located on the old machine -> onto the new machine. Same rule applies for other programs (apache, fpm, postfix): copy all files from /etc/apache, /etc/postfix and so on onto the new machine. For the future, using rsync or something similar might come more in handy.
This was the ‘hard way’ of cloning a server. Since the best sysadmin is a lazy sysadmin, you might want to consider Chef/Puppet/Ansible/SaltStack or any virtualization & cloning tool.
Enable DnsCrypt on your Ubuntu machine
For mirroring purposes, this is a working copy-paste of https://madebits.github.io/#blog/2014/2014-12-12-Using-DNSCrypt-on-Ubuntu-14.04.md
DNSCrypt enables making encrypted DNS quires to the DNS providers that support it. There is PPA for DnsCrypt for Ubuntu, but it is not maintained at the time of this writing and it has no binary for Ubuntu 14.04 LTS. To install DNSCryp I used these steps, that I tried on Lubuntu 14.04 LTS:
- Visit DNSCryp PPA packages and download libsodium for trusty and dnscrypt-proxy for saucy (I used the 64 bit version for my machine, you may need the 32 bit versions).
- I used
gdebi-gtktool to install firstlibsodium4_0.4.5-0~trusty5_amd64.deband thendnscrypt-proxy_1.4.0-0~oldconf2+saucy1_amd64.deb(you can also usedpkg -i). dnscrypt-proxyruns then locally in address127.0.0.2on port53(usenetstat -tuplento verify).- The default DNSCryp PPA package
apparmorprofile prevents Ubuntu 14.04 from shutting down. To fix that I edited it (sudo leafpad /etc/apparmor.d/usr.sbin.dnscrypt-proxy) and replacing its content with the following:
# Last Modified: Tue Dec 02 22:20:12 2014
#include <tunables/global>
/usr/sbin/dnscrypt-proxy {
#include <abstractions/base>
network inet stream,
network inet6 stream,
network inet dgram,
network inet6 dgram,
capability net_admin,
capability net_bind_service,
capability setgid,
capability setuid,
capability sys_chroot,
capability ipc_lock,
/bin/false r,
/etc/ld.so.cache r,
/etc/nsswitch.conf r,
/etc/passwd r,
# In case of custom libsodium installation
/usr/local/lib/{@{multiarch}/,}libsodium.so* mr,
# Reasonable pidfile location - tweak this if you prefer a different one
/run/dnscrypt-proxy.pid rw,
}
(Ed.) You may want to which libsodium4 and which dnscrypt-proxy and check the actual paths.
- Optional:
dnscrypt-proxyconfiguration for the init service daemon is found in/etc/default/dnscrypt-proxy. The parameters (with — added) are documented inman dnscrypt-proxy. I edited/etc/default/dnscrypt-proxyas root to specify an alternative DNS server. The list of the official available servers can be found in GitHub, or locally in/usr/share/dnscrypt-proxy/dnscrypt-resolvers.csv. To verify that a given server works usedig -p 443 @176.56.237.171 google.com(replace ip and port as needed). If you edit/etc/default/dnscrypt-proxy, you should runsudo restart dnscrypt-proxyafterwards. - Verify that
dnscrypt-proxyruns by usingps -ef | grep dnscrypt. Then verify it can resolve addresses by usingdig @127.0.0.2 google.com(if you configuredtcp-onlyfordnscrypt-proxythen usedig +vc @127.0.0.2 google.com). - If all ok, you can replace you current DNS servers in the Network Manager UI. If you use DHCP, select Automatic (DHCP) addresses only, and set the 127.0.0.2 in Additional DNS servers. Once done, run
sudo service network-manager restartfor it to take effect. Verify the server used withnm-tool | grep -i dns.
End of copy-paste. You could download the latest package of dnscrypt-proxy and libsodium and compile it locally – best advice so far.
Bash scripting: How to answer “yes” by default to update/upgrade prompt
While giving birth to your first bash scripts, you may encounter various barriers. Today we’ll discuss how to deal with a default value (yes) while updating or upgrading your Linux system.
Simply:
yes | apt-get upgrade
yes | yum update
y | apt-get upgrade
y | yum update
And so on.
Repair/configure a RunAbove snapshot (clone) booted as a new server
If you clone (make a snapshot) of a CentOS 6 in your RunAbove ControlPanel, you might encounter difficulties placing it online. This is due to the fact that a snapshot is literally a clone, therefore cloning IP and MAC addresses onto the new server. To overcome this, we should…
- Check your old MAC address `ifconfig -a`.
- Login into the new server via VNC and do a `ifconfig -a`
- your interface will show up as `eth1` at this very moment.
- go to `/etc/udev/rules.d/70-persistant-net.rules`.
- Confront the two interfaces eth1/eth0 and
- delete or comment out `eth0`
- modify the `eth1` to `eth0` parameter on the line where you MAC (HWaddr) address resembles the `ifconfig -a` on the cloned server
- Reboot and voila, you’re online.
- Some modifications need to be made
- `/etc/hostname` and enter the corresponding hostname + IP address
- run `hostname your.new.hostname`
- `service network restart` or reboot
Shortcut: simply delete the /etc/udev/rules.d/70-persistant-net.rules file and reboot :)
Don’t forget! You’re ssh keys will be cloned too. Fork the old putty config for the new server and simply change the IP address. You may want to generate a new login key and add it to .ssh/authorized_keys!
