MySQL: Save Arabic Characters / Words

Database / Table for Arabic Usage

  1. Charset – UTF-8
  2. Collation – utf8_unicode_ci

Example:


# Database
CREATE DATABASE `db_arabic`CHARACTER SET utf8 COLLATE utf8_unicode_ci;

# Table
CREATE TABLE `db_arabic`.`users`( `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR(255), `created` DATETIME, `modified` DATETIME, PRIMARY KEY (`id`) ) CHARSET=utf8 COLLATE=utf8_unicode_ci; 

In CakePHP, make sure to set 'encoding' => 'utf8' in database.php.

You can have Arabic Lorem Ipsum at Multilanguage Lorem Ipsum Generator

For web page, please ensure to add meta tag, charset `utf-8′, as following:

<meta charset="utf-8" />

Reference: How to save Arabic Words Into MySQL Table

CakePHP: Running Shell Script with Cronjob

Create a Shell Script

Create a ScheduleShell.php in cakephp/app/Console/Command.

<?php
class ScheduleShell extends Shell {
    function main() {
    	$this->out('Hello World');
    }
}
?>

Add a cronjob

sudo crontab -e

I have a cronjob running every 5 minute for ScheduleShell.php

# cronjob run every 5 minute
*/5  *    *    *    * cd /var/www/html/cakephp/app && Console/cake schedule

CakePHP: Upload File

The Model

<?php

App::uses('AppModel', 'Model');

class Picture extends AppModel {
	public $useTable = 'picture';
	public $primaryKey = 'id';
	public $name = 'Picture';
	public $order = 'Picture.id ASC';

	protected $_displayFields = array(
		'id',
		'name',
		'link',
	);
}
?>

The Database

CREATE TABLE `picture` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `link` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

The View

<?php
	echo $this->Form->create('Upload',array(
		'enctype' => 'multipart/form-data',
		'url' => array(
			'controller' => 'upload', 
			'action' => 'index'
		)
	));
	echo $this->Form->file('file');
	echo $this->Form->end(array(
		'label' => 'Submit',
		'class' => 'btn btn-success'
	));
?>

The Controller

<?php

App::uses('AppController', 'Controller');

class UploadController extends AppController { 
	public function index() {
                // in this example, we're trying to accept gif,jpeg and png

                $image = $this->request->data['Upload']['file'];

                $imageTypes = array("image/gif", "image/jpeg", "image/png");

                $uploadFolder = "img" . DS . "photos" . DS;

                $uploadPath = WWW_ROOT . $uploadFolder;

                $id = null;

                if(!empty($image['name']))
                {
                        $ext = substr(strtolower(strrchr($image['name'], '.')), 1); //get the extension

                        if(in_array($image['type'], $imageTypes))
                        {
                                $date = date_create();
                                $rand = rand(100000,999999);
                                $link = 'pic_' . date_format($date, 'U') . '_' . $rand . '.' . $ext;

                                move_uploaded_file($image['tmp_name'], $uploadPath . $link);

                                $this->Picture->create();
                                $this->Picture->set(array('name' => $image['name'], 'link' => $link));
                                $this->Picture->save();
                                $id = $this->Picture->id;
                                $this->Session->setFlash('Picture uploaded!');
                        }
                        else
                        {
                                $this->Session->setFlash('Only files with extension .gif, .png and .jpeg are allowed to be upload.');
                        }
                }

	}
}
?>

CakePHP: PHPMailer Component – Using GMail SMTP

Requirements:

  1. CakePHP 2.x
  2. PHPMailer

Download PHPMailer & extract it to APP/vendors.

Add following lines in APP/vendors/phpmailer/class.phpmail.php line 542, in ELSE statement. This to ensure we have a secured domain through SSL / TLS.

// add a variable named SMTPSecure - SSL / TLS
var $SMTPSecure

// line 542 
if(!empty($this->SMTPSecure))
{
    $secure = $this->SMTPSecure . '://';
}

Create a component called EmailComponent in APP/app/Controller/Component

<?php

App::uses('Component', 'Controller');
App::import('Vendor', 'phpmailer', array('file' => 'phpmailer'.DS.'class.phpmailer.php'));

class EmailComponent extends Component {

  public function send($to, $subject, $message) {
    $sender = "from_you@you.com"; // this will be overwritten by GMail

    $header = "X-Mailer: PHP/".phpversion() . "Return-Path: $sender";

    $mail = new PHPMailer();

    $mail->IsSMTP();
    $mail->Host = "smtp.gmail.com"; 
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = "ssl";
    $mail->Port = 465;
    $mail->SMTPDebug  = 2; // turn it off in production
    $mail->Username   = "[yourGMailAccount]@gmail.com";  
    $mail->Password   = "password";
    
    $mail->From = $sender;
    $mail->FromName = "From Me";

    $mail->AddAddress($to);

    $mail->IsHTML(true);
    $mail->CreateHeader($header);

    $mail->Subject = $subject;
    $mail->Body    = nl2br($message);
    $mail->AltBody = nl2br($message);

    // return an array with two keys: error & message
    if(!$mail->Send()) {
      return array('error' => true, 'message' => 'Mailer Error: ' . $mail->ErrorInfo);
    } else {
      return array('error' => false, 'message' =>  "Message sent!");
    }
  }
}

?>

Create a controller, ExampleController.php to test out our EmailComponent

<?php

App::uses('AppController', 'Controller');

class ExampleController extends AppController {
	public $components = array('Email');
	public function index() {
		// just to test out the sending email using SMTP is OK, create a method that will be able to access from public
		$to = '[someone@you.com]';
		$subject = 'Hi buddy, i got a message for you.';
		$message = 'Nothing much. Just test out my Email Component using PHPMailer.'
		$mail = $this->Email->send($to, $subject, $message);
		$this->set('mail',$mail);
		$this->render(false);
	}
}

When you navigate to http://localhost/APP/Example, the email should send to [someone@you.com]. Please do check the mailbox. 🙂

References:

  1. http://phpmailer.worxware.com/?pg=examplebgmail
  2. http://blog.teamtreehouse.com/sending-email-with-phpmailer-and-smtp
  3. http://book.cakephp.org/2.0/en/controllers/components.html#creating-a-component