punBB extension: Post Share

Showing links in every post

Hey folks, in a short time i’ve made some punbb extension. Here is another, it’s for sharing post. It inserts share button in every post, here is the screenshot:

You will find the configuration at Administration → Settings → Features

Administration Area

Enjoy :)

punBB extension: We miss you!

Just wrote a punBB plugin for Projanmo Forum and now sharing with the community. The plugin describes it’s uses. You will find the configuration at Administration → Settings → Features Here is the screenshot:

Download: We miss you

Requesting bug report :)

punBB extension: Login with ID

Just wrote an another punBB plugin for Projanmo Forum.

In normal uses, everyone sign’s in with username and password. But for a special purpose, i needed to login with user ID and password. Because, when the username is in a complex script language like Bangla and you want to login from the mobile and you don’t have Bangla writing system in your mobile, this feature comes in action.

This extension is written for mobile users actually. When you will visit the forum from mobile, you will be redirected to the login screen containing user ID and password.

Download: Login with ID

Enjoy!!! :D

Bangla date as a PHP class

I have worked with Bangla date a long time ago. The first initiative was to make a extension for forum engine punBB. After that, i made a javascript widget to display Bangla date in any site. The third initiative was to make a facebook application that shows current Bangla date to your facebook profile.

I always wanted to publish this as a PHP Class but never got that chance to do so. After a long time i worked with Bangla date today and finally created a PHP Class to use it anywhere by anyone. Sorry for the late.

Usage:

<?php
include_once 'class.banglaDate.php';
$bn_date = new BanglaDate(time());
$date = $bn_date->get_date();
?>

You will get an array of converted date in Bangla.

A few things needed to know. Bangla date counted or changes after the sun rise. That means, where English date changes at 12′O clock at the night, Bangla date changes at the morning after the sunrise.

Class Constructor:
This class constructor has 2 parameters. The first one in timestamp and the second one is the hour of changing the date. If you want to change the Bangla date like English at 12’0 clock at the night, you should use $bn_date = new BanglaDate(time(), 0). But if you want to change the date at 6′O clock at the morning, use as $bn_date = new BanglaDate(time(), 6). If you don’t pass the second parameter, the default value is 6. Use as you like.

After Creating the class object, you can set another date using set_time() like $bn_date->set_time(time(), 4);

Bangla Date class on PHPClasses.org

Create your own twitter image signature

Many days ago i built two image signature as my forum signature. One of the signature shows the latest post from my blog and another shows my latest twitter update. I was inspired to do that signature by seeing Shiplu Vai’s signature. Originally he was using them and i copied the concept :D

Anyway, here is my latest blog post signature Blog Update Signature and here is my twitter signature Twitter image signature

I will show you the process to create your own twitter image signature like me.

1. At first we will get the latest twitter update by this

<?php
$username = "tareq_cse"; //Twitter Username
$url = "http://twitter.com/statuses/user_timeline/$username.json?count=1"; //json request url
$json = json_decode(file_get_contents($url));
$status = $json[0]->text; //get the status from array/object

2. Now we will get the time distance from now and the twitted time and will format it
Read the rest of this entry »

Sending mail with Gmail’s SMTP server with fsockopen

Today i was trying to find a way to send mail from my localhost (xampp) with Gmails SMTP server. I found some code in popular and also fastest forum engine – punBB.

I got the codes and did some cleanup for working fine as an external script. Here is the codes -

<?php

//this functin processes the server return codes and generates errors if needed
function server_parse($socket, $expected_response)
{
	$server_response = '';
	while (substr($server_response, 3, 1) != ' ')
	{
		if (!($server_response = fgets($socket, 256)))
			echo 'Couldn\'t get mail server response codes. Please contact the forum administrator.', __FILE__, __LINE__;
	}

	if (!(substr($server_response, 0, 3) == $expected_response))
		echo 'Unable to send e-mail. Please contact the forum administrator with the following error message reported by the SMTP server: "'.$server_response.'"', __FILE__, __LINE__;
}

//
// This function was originally a part of the phpBB Group forum software phpBB2 (http://www.phpbb.com).
// They deserve all the credit for writing it. I made small modifications for it to suit PunBB and it's coding standards.
// -------> This message is from punBB developer
//
function smtp_mail($to, $subject, $message, $headers = '')
{
	$recipients = explode(',', $to);
	$user = '<your mail id>';
	$pass = '<your password>';
	$smtp_host = 'ssl://smtp.gmail.com';
	$smtp_port = 465;

	if (!($socket = fsockopen($smtp_host, $smtp_port, $errno, $errstr, 15)))
		echo "Could not connect to smtp host '$smtp_host' ($errno) ($errstr)", __FILE__, __LINE__;

	server_parse($socket, '220');

	fwrite($socket, 'EHLO '.$smtp_host."\r\n");
	server_parse($socket, '250');

	fwrite($socket, 'AUTH LOGIN'."\r\n");
	server_parse($socket, '334');

	fwrite($socket, base64_encode($user)."\r\n");
	server_parse($socket, '334');

	fwrite($socket, base64_encode($pass)."\r\n");
	server_parse($socket, '235');

	fwrite($socket, 'MAIL FROM: <example@gmail.com>'."\r\n");
	server_parse($socket, '250');

	foreach ($recipients as $email)
	{
		fwrite($socket, 'RCPT TO: <'.$email.'>'."\r\n");
		server_parse($socket, '250');
	}

	fwrite($socket, 'DATA'."\r\n");
	server_parse($socket, '354');

	fwrite($socket, 'Subject: '.$subject."\r\n".'To: <'.implode('>, <', $recipients).'>'."\r\n".$headers."\r\n\r\n".$message."\r\n");

	fwrite($socket, '.'."\r\n");
	server_parse($socket, '250');

	fwrite($socket, 'QUIT'."\r\n");
	fclose($socket);

	return true;
}

// Send the mail
if(smtp_mail('example@gmail.com', 'Hi! Test Mail', 'This is a test mail from xampp with fsockopen()'))
{
	echo "Mail sent";
}
else
{
	echo "Some error occured";
}
?>
Tags: , , ,

Image upload & Validation: My first class to phpclasses.org

Recently i had to work with image uploading on my some projects. The issue was to upload the user avatar and rename the avatar’s with their userid. So that it will be easy to work with avatar system.

I first used this idea to one of my project and i did this with procedural method. But again on my current projects i need this facility again. So i thought to finish the job for the last time and decided to make a class to re-use the codes. So here i go…

Firstly, i use my class constructor to define the maximum file size, width, height and also the upload directory. So now create a instance of my class like this-

<?php
//$image = new ImageUloader($max_size, $max_width, $max_height, $upload_dir)
$image = new ImageUploader(26, 200, 150, 'images/avatar/');
?>

Now you need to set the image name to upload. Don’t be afraid, it’s the file input field name of your html form if you have a input file name like this -

<form enctype="multipart/form-data" action="" method="POST">
	<input name="input_field_name" type="file" />
    <input type="submit" name="submit" value="Change Avatar" />
</form>

Then you need to setup the image name like this

<?php
$image->setImage('input_field_name');
?>

Now it’s time for validation process :D
you can check the image size, width, height and also file type by the following method

<?php
$image->checkSize();
$image->checkHeight();
$image->checkWidth();
$image->checkExt();
?>

This method’s return true, if the condition are passed. We have permitted to use only jpg, jped, png and gif files. So lets use them

<?php
if(!$image->checkSize())
	$errors[] = "File size is Big";

if(!$image->checkHeight())
	$errors[] = "File height is Big";

if(!$image->checkWidth())
	$errors[] = "File width is Big";

if(!$image->checkExt())
	$errors[] = "File ext is not supported";
?>

So if the condition are passed, we will set the name of the file to be uploaded and now it’s the time to check if there any file with the same user id. If their is any file with same user id, we delete them for the sake of our need and upload them. By deleting the existing file with same user id clears the confusion of repeating same file name with same user id. Because, their is a possibility to found images with same file name but with different extensions.

<?php
if(!isset($errors)){
		$image->setImageName($userid);
		$image->deleteExisting();
		$image->upload();

		echo "<h2>Avatar Changed Successfully</h2>";
	}
	else{
		echo "<h2>You must correct the errors to proceed</h2><br>";
		print_r($errors);
	}
?>

So here is the complete code for validating and uploading

<?php
if(isset($_POST['submit'])){
	require 'class.imageupload.php';

	//$image = new ImageUloader($max_size, $max_width, $max_height, $upload_dir)
	$image = new ImageUploader(26, 200, 150, 'images/avatar/');
	$image->setImage('input_field_name');

	if(!$image->checkSize())
		$errors[] = "File size is Big";

	if(!$image->checkHeight())
		$errors[] = "File height is Big";

	if(!$image->checkWidth())
		$errors[] = "File width is Big";

	if(!$image->checkExt())
		$errors[] = "File ext is not supported";

	if(!isset($errors)){
		$image->setImageName($userid);
		$image->deleteExisting();
		$image->upload();

		echo "<h2>Avatar Changed Successfully</h2>";
	}
	else{
		echo "<h2>You must correct the errors to proceed</h2><br>";
		print_r($errors);
	}
}
?>

<form enctype="multipart/form-data" action="" method="POST">
	<input name="input_field_name" type="file" />
    <input type="submit" name="submit" value="Change Avatar" />
</form>

Download the class form here

Playing with twitter JSON using PHP

Now twitter is playing a major roll to our day. Many of us is using twitter. Now lets play something with it. Today i will use JSON to play with twitter LOL.

Lets play with our statuses. If you are using twitter, then your status url is http://twitter.com/status/user_timeline/YOUR_USERNAME.json. So if your twitter id is tareq_cse then the url will be http://twitter.com/status/user_timeline/tareq_cse.json. Now i will add a extra parameter here and this is count. This url shows the last 20 status update of yours. So if i need only 5 update then? Then we will add a count variable there. So the url will be http://twitter.com/status/user_timeline/tareq_cse.json?count=5

Now come to the php code.

<?php
$json = file_get_contents("http://twitter.com/status/user_timeline/tareq_cse.json?count=10", true); //getting the file content
$decode = json_decode($json, true); //getting the file content as array

echo "<pre>";
print_r($decode);
echo "</pre>";
?>

Now run the code and see. You will get output like this

Array
(
    [0] => Array
        (
            [text] => is digging wordpress
            [in_reply_to_user_id] =>
            [user] => Array
                (
                    [notifications] =>
                    [time_zone] => Dhaka
                    [description] => A blogger, microblogger and learner
                    [following] =>
                    [utc_offset] => 21600
						...............
						...............
                    [profile_text_color] => 666666
                    [location] => Rajshahi, Bangladesh
                    [id] => 15842888
                    [profile_background_image_url] => http://static.twitter.com/images/themes/theme9/bg.gif
                    [profile_link_color] => 2FC2EF
                )

            [favorited] =>
            [in_reply_to_screen_name] =>
            [truncated] =>
            [created_at] => Sun May 03 20:56:00 +0000 2009
            [id] => 1689697302
            [in_reply_to_status_id] =>
            [source] => TwitterFox
        )
)

Now get some usefull data from this. If we need the last 10 twitter update write the code

<?php
$json = file_get_contents("http://twitter.com/status/user_timeline/tareq_cse.json?count=10", true);
$decode = json_decode($json, true);

echo "<pre>";
$count = count($decode); //counting the number of status
for($i=0;$i<$count;$i++){
echo $decode[$i][text]."<br>";
}
echo "</pre>";
?>

Lets pull some userdata

<?php
$json = file_get_contents("http://twitter.com/status/user_timeline/tareq_cse.json?count=10", true);
$decode = json_decode($json, true);

echo "<img src=\"".$decode[0][user][profile_image_url]."\"</img><br>"; //getting the profile image
echo "Name: ".$decode[0][user][name]."<br>"; //getting the username
echo "Web: ".$decode[0][user][url]."<br>"; //getting the web site address
echo "Location: ".$decode[0][user][location]."<br>"; //user location
echo "Updates: ".$decode[0][user][statuses_count]."<br>"; //number of updates
echo "Follower: ".$decode[0][user][followers_count]."<br>"; //number of followers
echo "Following: ".$decode[0][user][friends_count]."<br>"; // following
echo "Description: ".$decode[0][user][description]."<br>"; //user description
?>

Following this method, you can pull any data by analyzing the first output

Tags: , ,

Activate cURL in XAMPP

cURL is a command line tool for transferring files with url syntax. cURL has a library for php.
Working with scripts that needs cURL, we must have cURL support.
In xampp, cURL support is not enabled built-in. Within a second we can activate cURL library.
You just need to follow the simple steps:

1. Go to the xampp directory of your server
2. Open bin directory under Apache.
3. Open php.ini file with notepad
4. Remove the semicolon (;) from front the text “extension=php_curl.dll
5. Restart your apache server
You are done

Tags: , , ,

Formatting date, time and time zone with php

Formatting date, time and time zone with php is a very easy task. You may have a date
string in your database like (March 7, 2009, 5:16 pm), (03.10.01), (Sat Mar 10 15:16:08 MST 2001)
or as a timestamp like (1232886599) or something else. But you want to show this time in other
formats.
As example, you may have (March 7, 2009, 5:16 pm), but you want show this as 10-03-2001. So how
will you do this task? The answer is simple. Following code shows that

<?php
$date = 'March 7, 2009, 5:16 pm'; //date from your database
//make it as time stamp
$date = strtotime($date); //makes the date as timestamp
$date = date('d-m-Y',$date); // show as (07-03-2009)
echo $date;
?>

You can make any kind of formate with the timestamp. So, at first you need to convert the date
in timestamp.

Another Thing is to show a time in your time zone. How to do this?

<?php
$date = $some_gmt_date; //date from your database
//make it as time stamp
$date = strtotime($date); //makes the date as timestamp
date_default_timezone_set('Asia/Dhaka'); //this is for +6 with gmt
$date = date('d-m-Y',$date); // show as (07-03-2009)
echo $date;
?>

That was preety easy

Tags: , , ,