před 3 dny

How to switch Homebrew formulas to older version

Sometimes you just won’t be able install latest version of any program and need some older (e.g. PHP 5.6.1 won’t work and you need install at least 5.6.0 and getting your work done).

As you know all brew formulas are GIT repos, you can swith to older version easly:

$ cd /usr/local/Library/Taps/homebrew/homebrew-php/Formula
$ git log --pretty="%h - %s" -10
71b2069 - Update to PHP 5.5.18
b8aeb54 - Use homebrew's openssl for IMAP
f0d721a - php56: improve phpdbg logic
0dc3f1c - Update to PHP 5.6.1
908fedd - Update to Blitz 0.8.12
4801697 - Updates formula for WP CLI  to version 0.17.0
00560f3 - Upgrade php*-swoole to 1.7.5
8cbd369 - Updated PHP_CodeSniffer
a81eba6 - update pecl_http to 2.1.2
6a88856 - Add HEAD url for composer

Then just switch

$ git checkout 908fedd

And then run brew install php56 and older version PHP will be installed. Procedure can be used for any formulas.

#brew #osx #php56

5. 10. 2014

Hardcore Simple Events Dispatcher for PHP

Hardcore simple events for PHP with filter, prioritizing, removing handlers and stop propagation. It’s even simpler then my previous version https://github.com/OzzyCzech/events.

<?php
/**
 * Hardcore simple events for PHP. 
 *
 * @author Roman Ozana <ozana@omdesign.cz>
 */

/** @var array $events */
$events = [];

/**
 * Return listeners
 *
 * @param $event
 * @return mixed
 */
function listeners($event) {
	global $events;
	if (isset($events[$event])) {
		ksort($events[$event]);
		return call_user_func_array('array_merge', $events[$event]);
	}
}

/**
 * Add event listener
 *
 * @param $event
 * @param callable $listener
 * @param int $priority
 */
function on($event, callable $listener = null, $priority = 10) {
	global $events;
	$events[$event][$priority][] = $listener;
}

/**
 * Trigger only once.
 *
 * @param $event
 * @param callable $listener
 * @param int $priority
 */
function once($event, callable $listener, $priority = 10) {
	$onceListener = function () use (&$onceListener, $event, $listener) {
		off($event, $onceListener);
		call_user_func_array($listener, func_get_args());
	};

	on($event, $onceListener, $priority);
}

/**
 * Remove one or all listeners from event.
 *
 * @param $event
 * @param callable $listener
 * @return bool
 */
function off($event, callable $listener = null) {
	global $events;
	if (!isset($events[$event])) return;

	if ($listener === null) {
		unset($events[$event]);
	} else {
		foreach ($events[$event] as $priority => $listeners) {
			if (false !== ($index = array_search($listener, $listeners, true))) {
				unset($events[$event][$priority][$index]);
			}
		}
	}

	return true;
}

/**
 * Trigger events
 *
 * @param $event
 */
function trigger($event) {
	$args = func_get_args();
	$event = array_shift($args);

	foreach ((array)listeners($event) as $listener) {
		if (call_user_func_array($listener, $args) === false) break; // return false; // will break
	}
}

/**
 * Pass variable with all filters.
 *
 * @param $event
 * @param null $value
 * @return mixed|null
 */
function filter($event, $value = null) {
	$args = func_get_args();
	$event = array_shift($args);

	foreach ((array)listeners($event) as $listener) {
		$args[0] = $value;
		$value = call_user_func_array($listener, $args);
	}

	return $value;
}

// ---------------------------------------------------- aliases ---------------------------------------------------- //

/**
 * Trigger an action.
 *
 * @param $event
 * @return mixed
 */
function action($event) {
	return call_user_func_array('\trigger', func_get_args());
}

/**
 * @param $event
 * @param callable $listener
 * @param int $priority
 */
function add_action($event, callable $listener, $priority = 10) {
	on($event, $listener, $priority);
}

/**
 * @param $event
 * @param callable $listener
 * @param int $priority
 */
function add_filter($event, callable $listener, $priority = 10) {
	on($event, $listener, $priority);
}

Download here: https://gist.github.com/OzzyCzech/fa03b06b8dd12b0bc570

#php #events

28. 9. 2014

Getting GitHub latest release URL

Do you need Download Latest Version button? Try follow PHP code:

function getLatestTagUrl($repository, $default = 'master') {
    $file = @json_decode(@file_get_contents("https://api.github.com/repos/$repository/tags", false,
        stream_context_create(['http' => ['header' => "User-Agent: Vestibulum\r\n"]])
    ));
    return sprintf("https://github.com/$repository/archive/%s.zip", $file ? reset($file)->name : $default);
}

echo getLatestTagUrl('OzzyCzech/vestibulum');
// will return https://github.com/OzzyCzech/vestibulum/archive/v0.2.1.zip

#php #github #tip

25. 9. 2014

How to fix “Shell Shock” bash bug on OS X

First try if your bash version it’s vulnerable. Go to terminal and run:

$ env x='() { :;}; echo vulnerable' bash -c 'echo hello'

Follow output is an example of a non-vulnerable bash version.

bash: warning: x: ignoring function definition attempt
bash: error importing function definition for `x'
hello

If you see the word vulnerable in the output of that command your bash is vulnerable and you should update.

How to update bash with Homebrew

First check your bash version:

$ bash --version    
GNU bash, version 4.3.18(1)-release (x86_64-apple-darwin13.3.0)
Copyright (C) 2013 Free Software Foundation, Inc.

Then find your bash directory (full path can be usefull later):

$ ls -la /usr/local/bin/bash
/usr/local/bin/bash -> ../Cellar/bash/4.3.25/bin/bash

Then upgrade bash:

$ brew update
$ brew upgrade bash

If your terminal starts returning weird error like:

dyld: Library not loaded: @@HOMEBREW_PREFIX@@/opt/readline/lib/libreadline.6.dylib
  Referenced from: /usr/local/bin/bash
  Reason: image not found

You will need upgrade also readline with follow commands:

$ brew rm -f readline
$ brew install readline
$ brew link readline --force

Tip: If you accidentally close Terminal before getting above errors and your bash just stop working at all. Go to Terminal preferences⌘+, and change bash version to older e.g./usr/local/Cellar/bash/4.3.18/bin/bash then quit Terminal ⌘+Q. Upgrade readline and change path back to /usr/local/bin/bash and restart Terminal again.

Bash settings

#brew #bash #shell

24. 9. 2014

AngularJS $http not sending X-Requested-With header

Angular $http isn’t appending the header X-Requested-With = XMLHttpRequest since Angular 1.3.0.

X-Requested-With header is rarely used in practice and by using it all the time we are riggering preflight checks for crossdomain requests.

See commit message

That can cause some problems on PHP side. If you need somehow differentiate XHR and common requests. For example Zend Framework and lot of others PHP frameworks use follow code:

function isXmlHttpRequest() {
 return !empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
  $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest';
}

You can easly addit on Angular side by following code:

myAppModule.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
}]);

#angular #js #ajax

1 2 88 89