PHP Killer: Do not do this on your code

Unless you want/need to kill some time… do not do the following on your PHP code, or you will be banging your head against the wall:

 

1. Use trim() inside empty():

 

sample:

if (!empty(trim($item))) {

}

 

work around:

$item = trim($item);

if (!empty($item)) {

}

 

 

2. Typecast a parameter that is sent by reference to a function:

$random = shuffle((array) $items);

 

workround:

$items = (array) $items;

$random = shuffle($items);

 

 

 

Kohana 3.2.6: Change Default Route

Let’s define a redirect module that will handle all requests that were not handled by any other controller. It will also be a good place to include a 404 page.

modules/application/bootstrap.php

Kohana::modules(array(
.
.
'redirect' => MODPATH . 'redirect', //Redirect module
.
.
));

Route::set('default', '(<request>)',
 array(
 'request' => '(.*)',
 ))
 ->defaults(array(
 'controller' => 'redirect',
 'action' => 'index',
 ));

 

And then define the controller itself:

modules/redirect/classes/controller/redirect.php

<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Handles all the redirecions that may happen
 *
 * @package......Redirect
 * @category.....SEO
 * @author.......Mauricio Otta
 * @copyright....PortNumber53.com
 */

class Controller_Redirect extends Controller {

  static public function action_index() {
    echo "Controller_Redirect::action_index();<br />";
    echo Request::current()->param('request');
  }

}

 

 

 

Code:: css-message-box

I just started a Google Code hosted project since I’m always in need of pretty message boxes for my HTML things.

Let me know if you want/can contribute to it. It’s currently licensed under GPL 2.0

Here’s the address:

http://code.google.com/p/css-message-box/

 

Why:

 

Installing DropBox on Slackware 13.1 64-bit

Before you start, let's make sure you have gnome docutils installed:

Get the package:

http://www.linuxfromscratch.org/blfs/view/cvs/gnome/gnome-doc-utils.html

Install GNOME Doc Utils (this part is borrowed from LinuxFromScratch book)

./configure --prefix=$(pkg-config --variable=prefix ORBit-2.0) --mandir=$(pkg-config --variable=prefix ORBit-2.0)/share/man make 

Test it

make check

and then install as root:

make install

switch to Dropbox installation folder

cd /nautilus-dropbox-0.6.3

./configure

Kohana 3.1.x: Common controller [step 2]

Define parent controller:

 

 

<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Common_Content extends Controller_Template {

 public $template                = '';
 public $template_name           = 'default';
 public $layout                  = 'default';

 public $save_auto_render        = null;

 public $content                 = '{Do something to generate content}';

 public function __construct(Request $request, Response $response) {
 parent::__construct($request, $response);

 $this->session = Session::instance();

 }

 public function before() {

 $this->save_auto_render = $this->auto_render;
 $this->auto_render = false;
 parent::before();
 $this->auto_render = $this->save_auto_render;

 if ($this->auto_render) {
 $this->template = View::factory('template/' . $this->template_name . '/' . $this->layout)
 ->bind('content', $this->content);

 }

 }

 public function action_view_html() {
 $path = $this->request->param('path');

 echo "PATH: $path";
 }

 public function after() {
 parent::after();

 if ($this->auto_render) {
 } else {
 $this->response->headers('content-type', 'application/json');
 echo json_encode($this->content);
 }

 }

}

 

 

 

 

Kohana 3.2.x: Master Template Controller [step 1]

Let's create a content module that will handle both static and dynamic content:

First enable the module:

modules/application/bootstrap.php

Kohana::modules(array(
.
.
'content' => MODPATH . 'content',   // Content module
.
.
));

 

Second, create the master template controller:

modules/content/classes/controller/base/template.php

template_name = Kohana::$config->load('content.template.name');
		$this->template = 'template/'.$this->template_name.'/index';
		parent::before();

		if ($this->auto_render) {
			$this->template->title		= '';
			$this->template->content	= '';

			$this->template->styles		= array();
			$this->template->scripts	= array();
		}

	}

	public function after() {
		if ($this->auto_render) {
			$styles = Kohana::$config->load('content.template.style');
			$scripts = Kohana::$config->load('content.template.script');

			$this->template->styles = array_merge( $this->template->styles, $styles );
			$this->template->scripts = array_merge( $this->template->scripts, $scripts );
		}
		parent::after();;
	}

}

 

Define routes to call the content module:

modules/content/init.php

<?php defined('SYSPATH') or die('No direct script access.');
 
/**
 * Routes for Content
 *
 * @package……Content
 * @category…..Routing
 * @author…….Mauricio Otta
 * @copyright….PortNumber53.com
 */
 
if (! Route::$cache) {
Route::set('homepage-content', '(<language–>"/)()',
array(
  'language' => '(pt-br)',
  'nothing' => '',
))
  ->defaults(array(
  'controller' => 'content',
  'action'     => 'homepage',
));

 Route::set('static-content', '(/).html',
  array(
   'language' => '(pt-br)',
   'path' => '[a-zA-Z0-9_/\.=\-]+',
  ))
  ->defaults(array(
   'controller' => 'content',
   'action'     => 'static',
  ));
}

 

Define the base content controller

response->body('Controller_Base_Content::index();
');
	}

	public function action_homepage() {
		$this->template->content = 'homepage';
	}

	public function action_static() {

		$path = $this->request->param('path');

		if ($filepath = Kohana::find_file('views', 'content/static/' . $path . '.html')) {
			$filepath = 'content/static/' . $path . '.html';
			//$this->template->content = 'static here'.$filepath;

			$content = new Model_Content();
			$latest_content = $content->get_last_posts(10);

			View::bind_global('latest_content', $latest_content);
			$this->template->content = View::factory($filepath, array(
				'latest_content' => $latest_content,
			))->render();

			switch ($path) {
				case 'privacy-policy': $title = 'Privacy Policy'; break;
				default:
					$title = 'unknown: ' . $path;
			}
			$this->template->facebook_og['og:title'] = $title;
			View::set_global('title', $title);
		} else {
			$this->action_dynamic();
		}
	}

	public function action_dynamic() {

		$path = '/'.$this->request->param('path', '');

		$content = new Model_Content();
		$latest_content = $content->get_last_posts(10);
		View::bind_global('latest_content', $latest_content);

		$post = $content->get_post_by_url($path);

		if ($post && $latest_content) {
			$this->template->content = View::factory('content/main', array(
					'latest_content' => $latest_content,
					'content' => $post,
				)
			);
		} else {
			$this->template->content = View::factory('content/main', array(
					'content' => array(
						'type' => Content::TYPE_UNKNOWN,
						'description' => 'Content not found',
					),
				)
			);
		}
	}
}

 

Define the content controller (~/modules/content/classes/controller/content.php):

<?php
class Controller_Content extends Controller_Base_Content { }
?>

 

Store some configuration values for the module (~/modules/content/config/content.php):

 array(
		'name' => 'default',
		'style' => array(
			'media/css/3-column-px.css' => 'screen',
			'media/css/screen.css' => 'screen, projection',
			'media/css/print.css' => 'print',
			'template/default/css/style.css' => 'screen',
		),
		'script' => array(
			'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js',
		),
	),
);

 

 

……………………………………………………………

 

LAMP: Installing on Slackware 13.37 64-bit

Ongoing post:

 

1. Download Apache ( http://httpd.apache.org )

tar -xvjf httpd-2.2.21.tar.bz2

cd httpd-2.2.21

I like Slackware, but I also like the Apache layout installed in Debian distributions, so:

./configure --enable-layout=Debian--enable-module=most --enable-mods-shared=most --enable-ssl=shared
make
make install


2. PostgreSQL

./configure –with-perl –with-openssl

gmake all

gmake install

 

 

PHP

./configure –with-pgsql –enable-soap –enable-zip –with-mysql=/usr/local/mysql/ –with-mcrypt –enable-mbstring –with-gd –enable-ftp –with-jpeg-dir –with-png-dir –with-bz2 –with-zlib –enable-safe-mode –with-apxs2=/usr/sbin/apxs

make

make install

 

(ve) Installing LAMP on Debian Squeeze – 64bit

aptitude update

aptitude safe-upgrade

aptitude install mysql-server mysql-client

 

 

 

NGINX

aptitude install libpcre3-dev build-essential libssl-dev sudo

cd /opt/
http://nginx.org/en/download.html
wget http://nginx.org/download/nginx-1.0.8.tar.gz
tar -xvzf nginx-1.0.8.tar.gz
cd nginx-1.0.8
./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module --conf-path=/etc/nginx/nginx.conf
--error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access_log --with-http_ssl_module
--with-http_geoip_module --pid-path=/var/run/nginx.pid

  nginx path prefix: "/opt/nginx"
  nginx binary file: "/opt/nginx/sbin/nginx"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file: "/etc/nginx/nginx.conf"
  nginx pid file: "/var/run/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access_log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

make
make install
#PECL
apt-get install make php5-dev php-pear
aptitude install libcurl4-openssl-dev
pecl install uploadprogress
pecl install pecl_http

echo -e "extension=uploadprogress.so" > /etc/php5/conf.d/uploadprogress.ini
echo -e "extension=http.so" > /etc/php5/conf.d/uploadprogress.ini
 ##FIX APACHE

<?php

// Let's fix the document root because of a bug in Apache's mod_vhost_alias not
// setting up the correct $_SERVER['DOCUMENT_ROOT'] env variable.
$__mod_vhost_alias_fix_doc_root = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'domains' . DIRECTORY_SEPARATOR . $_SERVER['HTTP_HOST'] . DIRECTORY_SEPARATOR . 'html';

if (is_dir($__mod_vhost_alias_fix_doc_root)) {
        $_SERVER['__MOD_VHOST_FIX_OLD_DOCUMENT_ROOT'] = $_SERVER['DOCUMENT_ROOT'];
        $_SERVER['DOCUMENT_ROOT'] = $__mod_vhost_alias_fix_doc_root;
}

?>
  • IGlr