Zend Framwork: Accessing Database in different ways

Info.Tech, PHP, Web Development, Zend Framework View Comments

Assuming I declared some variables in application.ini named webbyConfig and initialized in Bootstrap.php;

$config = Zend_Registry::get('webbyConfig');
$this->_db = new Zend_Db_Adapter_Pdo_Mysql(array(
        'host'     => $config['db_host'],
        'username' => $config['db_user'],
        'password' => $config['db_passwd'],
        'dbname'   => $config['db_name'],
    ));

....

$sql = 'SELECT a.col1,b.col2 FROM tableA AS a, tableB AS b WHERE a.id = b.id AND a.id = 1';

$stmt = $this->_db->query($sql);
print_r($stmt->fetchAll());

and this is another way accessing the db…

$sql = 'SELECT a.col1,b.col2 FROM tableA AS a, tableB AS b WHERE a.id = b.id AND a.id = 1';

$db = Zend_Db_Table::getDefaultAdapter();

$stmt = $db->query($sql);
print_r($stmt->fetchAll());

That’s what I know so far… I think there’s more… still exploring ZF. :)

PHP Warning: PHP Startup: Unable to load dynamic library ‘/usr/lib64/php/modules/mcrypt.so’ – libmcrypt.so.4: cannot open shared object file: No such file or directory in Unknown on line 0

Administration, Info.Tech, Linux System Administration, PHP View Comments

I created some script in php, encrypting and decrypting my emails. but when I ran the script, got some issues. When I traced it, the php-mcrypt library is missing. I am using CentOS 6.2 at 64bit architecture. Some says I will move the libmcrypt.so.4 and  libmcrypt.so.4.4.8 from /usr/lib to usr/lib64 and /usr/lib/php/mcrypt.so to /usr/lib64/php/mcrypt.so but when I check the file, its not there. The files aren’t there at the installed directory. to check this issues, cast php -v;

[root@mail lib64]# php -v
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/mcrypt.so' - libmcrypt.so.4: cannot open shared object file: No such file or directory in Unknown on line 0
PHP 5.3.3 (cli) (built: Feb  2 2012 23:47:49)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with the ionCube PHP Loader v4.0.10, Copyright (c) 2002-2011, by ionCube Ltd.

If there’s PHP Warning, then that means there are missing files… if more warnings, probably there’s a problem during installation of your php. But in this guide, I will only discuss how to fix the php-mcrypt issues.

First download the lacking files, which are;

  1. libmcrypt-2.5.8-4.el5.centos.x86_64.rpm
  2. php-mcrypt-5.3.3-1.el6.x86_64.rpm

You can download those files at rpmfind.net or rpm.pbone.net. Then extract the files inside the rpm pack by using the command rpm2cpio.

[root@mail ~]# rpm2cpio libmcrypt-2.5.8-4.el5.centos.x86_64.rpm | cpio -idmv
[root@mail ~]# rpm2cpio php-mcrypt-5.3.3-1.el6.x86_64.rpm | cpio -idmv

Then move the files that you just extracted to /usr/lib64 and after moving the files, restart httpd and cast again the php -v.

[root@mail ~]# php -v
PHP 5.3.3 (cli) (built: Feb  2 2012 23:47:49)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with the ionCube PHP Loader v4.0.10, Copyright (c) 2002-2011, by ionCube Ltd.
[root@mail ~]#

This is the output you should get, no PHP Warnings and php-mcrypt should be working fine. That’s it! Happy solving!

AGILE introduction

Info.Tech, Project Management View Comments

Watching this video will totally understand what is AGILE all about… :)

Zend Framework: Upload File Demo

Info.Tech, PHP, Web Development, Zend Framework View Comments

I’ve came across in uploading file using Zend Framework (ZF)… Since ZF is new to me, its like I’m studying PHP just like when I started, I have to memorize the predefined variables and functions but in ZF, its their classes, defined variables and its functions. Here’s my codes though its not perfect but its working.

<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
        $form = new Application_Form_Upload();
        $this->view->assign('form',$form);
    }

		private function getFileExtension($filename)
		{
			$fext_tmp = explode('.',$filename);
			return $fext_tmp[(count($fext_tmp) - 1)];
		}

		public function uploadAction()
		{
			$dest_dir = "uploads/";

			/* Uploading Document File on Server */
			$upload = new Zend_File_Transfer_Adapter_Http();
			$upload->setDestination($dest_dir)
						 ->addValidator('Count', false, 1)
						 ->addValidator('Size', false, 1048576)
						 ->addValidator('Extension', false, 'jpg,png,gif,pdf');
			$files = $upload->getFileInfo();

			// debug mode [start]
			echo '<hr />
							<pre>';
			print_r($files);
			echo '	</pre>
						<hr />';
			// debug mode [end]

			try
			{
				// upload received file(s)
				$upload->receive();
			}
			catch (Zend_File_Transfer_Exception $e)
			{
				$e->getMessage();
				exit;
			}

			$mime_type = $upload->getMimeType('doc_path');
			$fname = $upload->getFileName('doc_path');
			$size = $upload->getFileSize('doc_path');
			$file_ext = $this->getFileExtension($fname);
			$new_file = $dest_dir.md5(mktime()).'.'.$file_ext;

			$filterFileRename = new Zend_Filter_File_Rename(
				array(
					'target' => $new_file, 'overwrite' => true
			));

			$filterFileRename->filter($fname);

			if (file_exists($new_file))
			{
				$request = $this->getRequest();
				$caption = $request->getParam('caption');

				$html = 'Orig Filename: '.$fname.'<br />';
				$html .= 'New Filename: '.$new_file.'<br />';
				$html .= 'File Size: '.$size.'<br />';
				$html .= 'Mime Type: '.$mime_type.'<br />';
				$html .= 'Caption: '.$caption.'<br />';
			}
			else
			{
				$html = 'Unable to upload the file!';
			}

			$this->view->assign('up_result',$html);
		}

}

Below is the source file of whole project in Zend Framework for you to test it out… Enjoy!

Source Code: ZF Upload File Demo (7)

Securing Web Folders

Administration, Info.Tech, Linux System Administration View Comments

Create or Add this to .htaccess

AuthType Basic
AuthName "Members Only"
AuthUserFile /home/user/public_html/secretfolder/.htpasswd
<limit GET PUT POST>
         require valid-user
</limit>

then execute the htpasswd command;

htpasswd -c /home/user/public_html/secretfolder/.htpasswd admin

That’s it.. if you visit http://servername.com/~user/secretfolder/ — it will prompt a username and password. So you have to enter admin as username and the password you entered.

Enjoy~!

Syntax error on line 293 of /etc/httpd/conf/httpd.conf: DocumentRoot must be a directory

Info.Tech, Linux System Administration View Comments

Fixing the error

Syntax error on line 293 of /etc/httpd/conf/httpd.conf: DocumentRoot must be a directory

I already encounter this problem few years back yet I forgot the solution. As you know, its a basic thing to do that we will set users home and its directories right? And I’m done with it. yet the error keeps coming when restarting Apache.

[root@localhost ~]# service httpd restart
Stopping httpd:                                            [FAILED]
Starting httpd: Syntax error on line 293 of /etc/httpd/conf/httpd.conf:
DocumentRoot must be a directory
                                                           [FAILED]

This is actually the SELINUX causing this error. After you change the following;

[root@localhost home]# chmod 711 user
[root@localhost home]# chmod 755 user/public_html

You should disabled your SELINUX.

In CentOS 5.2, you can simply type “setup“, go to Firewall Configuration and select SELinux: Disabled

But in CentOS 6.2, you can’t find it at “setup”, edit the file /etc/selinux/config and change the following from:

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

to

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
#SELINUX=enforcing
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

Then reboot your machine/box!

That’s it! It should be working fine… You can use paths in users home. :)

Zend Framework: changing pulic to public_html for CPanel

PHP, Web Development, Zend Framework View Comments

In Zend Framework, the public files are located at public folder, but CPanel are located at public_html. What you will do is create a symlink.

ln -s public public_html

then this structure will work:

htdocs/
  myvhost.com/
    public/
    application/
    library/
    public_html # this is actually a symlink pointing to public

That’s it… :)

 

Reference: http://stackoverflow.com/questions/3903127/hosting-php-zend-framework-application-on-shared-cpanel-server

Thanks to: prodigitalson

 

Zendify: windows application to auto-populate to the host file and httpd.conf for ZendFramework Projects

Info.Tech, Software Development, Visual C#, Web Development View Comments

I’m been studying again about ZendFramework and its been a hassle for me to edit the hosts file from “C:\WINDOWS\system32\drivers\etc\” and the Apache httpd.conf file from “C:\Program Files\Apache Group\Apache2\conf\”. So I created my windows application to auto-populate my ZendFramework project entry. To check how hassle in setting the configurations, see ZendFramework – Intro video and you’ll know. :)

 

 

Here it is… Below is the download link of my windows application, you can use it also if you want. I am sharing it to the world for free and of course no warranty. :)

 

Requirements: .Net Framework 2.0 or latest version

Download: zendify (9)

ReCaptcha and SAJAX Integration

Info.Tech, PHP, Web Development View Comments

It’s been a while using this method and I’m posting this since my friends are asking how to integrate the Recaptcha and SAJAX.

What to edit:

  1. Edit recaptchalib.php (ReCaptcha Library File), at line 106 or under recaptcha_get_html() function, add “id” attributes of both recaptcha_challenge_field and recaptcha_response_field.
function recaptcha_get_html ($pubkey, $error = null, $use_ssl = false)
{
	if ($pubkey == null || $pubkey == '') {
		die ("To use reCAPTCHA you must get an API key from <a href='https://www.google.com/recaptcha/admin/create'>https://www.google.com/recaptcha/admin/create</a>");
	}

	if ($use_ssl) {
                $server = RECAPTCHA_API_SECURE_SERVER;
        } else {
                $server = RECAPTCHA_API_SERVER;
        }

        $errorpart = "";
        if ($error) {
           $errorpart = "&error=" . $error;
        }
        return '<script type="text/javascript" src="'. $server . '/challenge?k=' . $pubkey . $errorpart . '&hl=en"></script>

	<noscript>
  		<iframe src="'. $server . '/noscript?k=' . $pubkey . $errorpart . '" height="300" width="500" frameborder="0"></iframe><br/>
  		<textarea name="recaptcha_challenge_field" id="recaptcha_challenge_field" rows="3" cols="40"></textarea>
  		<input type="hidden" name="recaptcha_response_field" id="recaptcha_response_field" value="manual_challenge"/>
	</noscript>';
}

 

Source Code: Sample Recaptcha and SAJAX (5)

Protected: My Standard Firewall in CentOS Web Server (iptables)

Administration, Info.Tech, Linux System Administration Enter your password to view comments.

This post is password protected. To view it please enter your password below:


WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in