Saturngod

What's on my mind

Why am I writing MZ 2.0 ?

In this days, many question & anaswers system are popular. So,  why I write this ? Answer it simple, I want to do myself.

I don’t

  • know python too much and most of the question and answer system are writting by python.
  • want dependent to other company or scripts.
  • want to wait for supporting.
  • want to search plugin or addon or some hacking the system. want to waiting their security fixed issue or upgrade. I want to

  • made my own APIWork framework.

  • show next generation Q&A system.
  • made always upgrade my system.
  • made own control for scripts.
  • use Power of PHP,MySQL,Apache and jQuery.
  • show Myanmar Developer can made from scrath. I have a long plan for this system. In this days, I want to do many things and very busy. I can only write in weekend that is too bad for me. I hope, MZ 2.0 can start at July.

HTML Support CodeIgniter Sugar For Espresso

In [Original CodeIgniter sugar](http://github.com/joshuabaker/codeigniter.sugar) is not support HTML syntax highlighting. So, I can't use CI syntax in view. PHP support html syntax hightlighting. Why CI sugar is not support ? So, I tried to add HTML support. I don't know about how to make sugar but I can do it. I tried to read PHP sugar and can combine it. It's easy, just add 1 line in Syntaxes/CodeIgniterSyntax.xml. I add html support
<include syntax="text.html.basic" />

So, It will be look like this

<?xml version="1.0"?>
<syntax name="sourcecode.php.codeigniter">

<zones>
<include syntax="sourcecode.php"/>
<include syntax="text.html.basic" />
</zones>

</syntax>

If you don’t want to add manually, you can download zip file from github. I made fork from original author.

1. Download zip or tar.gz  from http://github.com/saturngod/codeigniter.sugar

2. extract it.

3.Change the folder name to CodeIgniter.sugar. Folder icon will change.

4. Double click CodeIgniter.sugar. It will be install automatically.

facebook on apiwork

Today, I tried to combine with facebook and apiwork. Great!! it's not too hard. Now, I can call facebook class easily and apiwork can use [facebook developer wiki](http://wiki.developers.facebook.com/index.php/Main_Page). I just use Facebook Class for my apiwork.

In Controller (facebook.php)

<?php
class facebookController extends Controller {

function facebookController()
{
parent::Controller();
}

function index()
{
$this->load->Facebook();
$data[‘fb_user’]=$this->fb->get_loggedin_user();
$data[‘appapikey’]=AWConfig::fbapikey;
$data[‘fb’]=$this->fb;
$this->load->view("facebook_test",$data);
}
}
?>

In View (facebook_test.php),

<html>
<head>
<!– Call Javascript For FBML –>

<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US&quot; type="text/javascript"></script><script type="text/javascript">FB.init("<?= $appapikey ?>");</script>

<title>Facebook Connect</title>
</head>
<body>
<?php
if(!$fb_user)
{
?>
<!– Login Button –>
<fb:login-button v="2" size="medium" onlogin="window.location.reload(true);">Connect with Facebook</fb:login-button>

<?php
}
else
{
echo "Facebook";

//User
//http://wiki.developers.facebook.com/index.php/Users.getInfo
//can retrive many user information except user’s email , check wiki
$user_details=$fb->api_client->users_getInfo($fb_user, array(‘uid’,’last_name’,’first_name’,’proxied_email’,’email_hashes’));
$uid=$user_details[0][‘uid’];
$firstName=$user_details[0][‘first_name’];
$lastName=$user_details[0][‘last_name’];
$proxemail=$user_details[0][‘proxied_email’];
$email=$user_details[0][‘email_hashes’];
echo $uid;
echo "<br>";
echo $email;
echo "<br>";
echo $proxemail;
echo "<br>";
echo $firstName;
echo "<br>";
echo $lastName;

//Friend
//http://wiki.developers.facebook.com/index.php/Friends.get
echo "<p>Friends:";
$friends = $fb->api_client->friends_get();
//slice the friend list
$friends = array_slice($friends, 0, 5);
foreach ($friends as $friend) {
echo "<br>$friend";
}
echo "</p>";
?>

<!–
http://developers.facebook.com/tools.php?connect_wizard
–>

<!– Profile Picture –>
<fb:profile-pic uid="<?= $uid ?>" size="square" facebook-logo="true"></fb:profile-pic>
<br/>
<!– Friend Pic –>
<div id="profile_pics"></div>
<script type="text/javascript">
var widget_div = document.getElementById("profile_pics");
FB.ensureInit(function () {
FB.Facebook.get_sessionState().waitUntilReady(function() {
FB.Facebook.apiClient.friends_get(null, function(result) {
var markup = "";
var num_friends = result ? Math.min(5, result.length) : 0;
if (num_friends > 0) {
for (var i=0; i<num_friends; i++) {
markup +=
‘<fb:profile-pic size="square" uid="’+result[i]+’" facebook-logo="true"></fb:profile-pic>’;
}
}
widget_div.innerHTML = markup;
FB.XFBML.Host.parseDomElement(widget_div);
});
});
});
</script>

<?

//status

$status=$fb->api_client->call_method("facebook.status.get", array(‘uid’=>$uid, ‘limit’=>’5’));

for($i=0;$i<5;$i++)
{
echo $status[$i][‘message’];
echo "<hr>";
}
//you can try
//print_r($status);

echo "<br>";

//Check Permission
$permission = $fb->api_client->Users_hasAppPermission(‘status_update’);
if($permission)
{
//If permission have status update
$msg=$fb->api_client->call_method("facebook.status.set",array(‘status’=>’I can update stataus from fb connect’));
}
else
{
?>
<script>
/
extended permission are
status_update, photo_upload, video_upload, create_note, and share_item
/

//Call facebook and return to ornagai.com
//if not get permission , next for Allow, next_cancel for not Allow
location.href="http://www.facebook.com/authorize.php?api_key=<?= $appapikey ?>&v=1.0&ext_perm=status_update&next=http://www.yoursite.com/fbconnect.php&next_cancel=http://www.yoursite.com&quot;

//You can also use dialog messagebox
/
FB.Connect.showPermissionDialog("status_update", function(perms) {
if (!perms) {
continue_without_permission();
} else {
save_session();
}
});
/
</script>
<?
}
?>
<!–
with PHP
$fb->logout( "http://site.com/returnAfterLogout.php&quot; )
–>

Logout
<?
}//End Facebook user
?>
</body>
</html>

mail() function setup in php.ini

Today, I setup mail() function for php. One of the Developer told me,

can’t send mail with mail() function. Are you enable in php.ini ?

Oh! I think I need to setup mail() in our server.

I reply like that. But I don’t know how to enable it. I don’t worry about that because our server is Ubuntu. I found following link and I setup and then it’s OK. How nice ? Just take 30 minutes and problem has been solove. I love Ubuntu and opensource,too.

https://help.ubuntu.com/community/Exim4

First we need to install exim4 look like that

$sudo apt-get install exim4

after that we need to config exim4

sudo dpkg-reconfigure exim4-config

I made setup by step with wizard. It’s easy. After that, I change php.ini in look like that.

sendmail_path = /usr/sbin/sendmail

OK. Done. Restart the apache and it’s working.

Harbari to Wordpress PHP Code

Copy the following code and create wp_convert.php in habari folder. It's using habari class for connection database, tag,comment,etc. You need to add wordpress database name,username and password in there. After that run wp_convert.php and convert all post to the wordpress database. Habari don't have category and all the post will under categorize.  I got that code from [http://chrismeller.com/user/files/2009/08/wp_migrate_script.php](http://chrismeller.com/user/files/2009/08/wp_migrate_script.php)
<?php

// config info
$import = array(
‘wp_host’ => ‘localhost’,    // hostname
‘wp_user’ => ‘wordpress’,    // username
‘wp_pass’ => ‘wordpress’,    // password
‘wp_name’ => ‘wordpress’,    // name of the database
‘wpprefix’ => ‘wp‘,        // table prefix
);

echo ‘<pre>’;

// keep habari from executing
define( ‘UNIT_TEST’, true );

// bootstrap it
include( ‘index.php’ );

// create a connection to our wordpress database
try {
$wpdb = DatabaseConnection::ConnectionFactory( "mysql:host=" . $import[‘wp_host’] . ";dbname=" . $import[‘wp_name’] );
$wpdb->connect( "mysql:host=" . $import[‘wp_host’] . ";dbname=" . $import[‘wp_name’], $import[‘wp_user’], $import[‘wp_pass’] );
}
catch( Exception $e ) {
die(‘Unable to connect to WordPress database. ‘ . $e->getMessage());
}

// users

$habari_users = Users::get();    // get all the habari users
$wp_users = array();

foreach ( $habari_users as $habari_user ) {

// see if the user exists already in WordPress
$wp_user = $wpdb->get_row( ‘select id, user_login from ‘ . $import[‘wp_prefix’] . ‘users where user_login = ?’, array( $habari_user->username ) );

// if it doesn’t, create it
if ( !$wp_user ) {

$wpdb->query( ‘insert into ‘ . $import[‘wp_prefix’] . ‘users (
user_login,
user_pass,
user_nicename,
user_email,
user_registered,
display_name
) values ( ?, ?, ?, ?, UTC_TIMESTAMP(), ? ) ‘, array(
$habari_user->username,
$habari_user->password,
$habari_user->username,
$habari_user->email,
$habari_user->info->displayname
)
);

$wp_users[ $habari_user->username ] = $wpdb->last_insert_id();

echo ‘Created user ‘ . $habari_user->username . ‘ with id ‘ . $wpdb->last_insert_id() . "n";

}
else {

echo ‘Found existing user ‘ . $wp_user->user_login . ‘ with id ‘ . $wp_user->id . "n";

$wp_users[ $wp_user->user_login ] = $wp_user->id;

}

}

// posts and pages

// get the total number of posts, either published or draft
$total_posts = Posts::get( array( ‘count’ => true, ‘ignore_permissions’ => true, ‘content_type’ => array( ‘entry’, ‘page’ ), ‘status’ => array( ‘published’, ‘draft’ ) ) );
$wp_posts = array();

echo ‘Total Posts: ‘ . $total_posts . "n";

for ( $i = 0; $i < $total_posts / 10; $i++ ) {

$posts = Posts::get( array( ‘limit’ => 10, ‘offset’ => $i  10, ‘ignore_permissions’ => true, ‘content_type’ => array( ‘entry’, ‘page’ ), ‘status’ => array( ‘published’, ‘draft’ ) ) );

echo ‘Got ‘ . count( $posts ) . ‘ posts’ . "n";

foreach ( $posts as $post ) {

$insert_query = ‘insert into ‘ . $import[‘wp_prefix’] . ‘posts (
post_author,
post_date,
post_date_gmt,
post_content,
post_title,
post_status,
comment_status,
post_name,
post_modified,
post_modified_gmt,
guid,
post_type
) values (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)’;

$insert_params = array(
$wp_users[ $post->author->username ],                    // post author ID, converted to the WordPress user’s
$post->pubdate->format( ‘Y-m-d H-i-s’ ),
gmdate( ‘Y-m-d H-i-s’, $post->pubdate->int ),
$post->content,
$post->title,
Post::status_name( $post->status ) == ‘published’ ? ‘publish’ : Post::status_name( $post->status ),                        // post status (published or draft, basically), converted to a string
$post->info->comments_disabled ? ‘closed’ : ‘open’,
$post->slug,
$post->modified->format( ‘Y-m-d H-i-s’ ),
gmdate( ‘Y-m-d H-i-s’, $post->modified->int ),
$post->guid,
Post::type_name( $post->content_type ) == ‘entry’ ? ‘post’ : ‘page’
);

$result = $wpdb->query( $insert_query, $insert_params );

if ( !$result ) {
echo ‘Failed to insert post ‘ . $post->slug . "n";
}
else {
$wp_posts[ $post->id ] = $wpdb->last_insert_id();
}

}

}

// tags

// here we don’t use the habari API because it would be cumbersome - if only it were as robust as the Posts API is…
$total_tags = DB::get_value( ‘select count() from {tags}’ );
$wp_tax = array();

echo ‘Total Tags: ‘ . $total_tags . "n";

for ( $i = 0; $i < $total_tags / 10; $i++ ) {

$tags = DB::get_results( ‘select id, tag_text, tag_slug from {tags} order by id limit 10 offset ‘ . $i  10 );

echo ‘Got ‘ . count( $tags ) . ‘ tags’ . "n";

foreach ( $tags as $tag ) {

// first, see if it already exists as a term in WordPress
$wp_tag = $wpdb->get_row( ‘select term_id, name, slug from ‘ . $import[‘wp_prefix’] . ‘terms where slug = ?’, array( $tag->tag_slug ) );

if ( !$wp_tag ) {

$insert_query = ‘insert into ‘ . $import[‘wp_prefix’] . ‘terms ( name, slug ) values ( ?, ? )’;
$insert_params = array( $tag->tag_text, $tag->tag_slug );

$wpdb->query( $insert_query, $insert_params );

//$wp_tags[ $tag->tag_slug ] = $wpdb->last_insert_id();

$tag_id = $wpdb->last_insert_id();

}
else {

//$wp_tags[ $wp_tag->slug ] = $wp_tag->term_id;

$tag_id = $wp_tag->term_id;

}

// and either way, make sure it’s actually specified as a part of the tag taxonomy
$wp_taxonomy = $wpdb->get_row( ‘select term_taxonomy_id from ‘ . $import[‘wp_prefix’] . ‘term_taxonomy where taxonomy = ? and term_id = ?’, array( ‘post_tag’, $tag_id ) );

if ( !$wp_taxonomy ) {

$insert_query = ‘insert into ‘ . $import[‘wp_prefix’] . ‘term_taxonomy ( term_id, taxonomy ) values ( ?, ? )’;
$insert_params = array( $tag_id, ‘post_tag’ );

$wpdb->query( $insert_query, $insert_params );

$wp_tax[ $tag->id ] = $wpdb->last_insert_id();

}
else {

$wp_tax[ $tag->id ] = $wp_taxonomy->term_taxonomy_id;

}

}

}

// now we have to link all our tags and posts

$total_joins = DB::get_value( ‘select count() from {tag2post}’ );

echo ‘Total Tag to Post relationships: ‘ . $total_joins . "n";

for ( $i = 0; $i < $total_joins / 10; $i++ ) {

// get the joins
$joins = DB::get_results( ‘select tag_id, post_id from {tag2post} order by tag_id, post_id limit 10 offset ‘ . $i  10 );

echo ‘Got ‘ . count( $joins ) . ‘ relationships’ . "n";

foreach ( $joins as $join ) {

// if it’s not in the list of posts we imported earlier, skip it - it’s probably a different content type
if ( !array_key_exists( $join->post_id, $wp_posts ) ) {
continue;
}

$insert_query = ‘insert into ‘ . $import[‘wp_prefix’] . ‘term_relationships ( object_id, term_taxonomy_id ) values ( ?, ? )’;
$insert_params = array( $wp_posts[ $join->post_id ], $wp_tax[ $join->tag_id ] );

$wpdb->query( $insert_query, $insert_params );

}

}

// update the tag counts
$wpdb->query( ‘update ‘ . $import[‘wp_prefix’] . ‘term_taxonomy t set count = ( select count(object_id) from ‘ . $import[‘wp_prefix’] . ‘term_relationships where term_taxonomy_id = t.term_taxonomy_id )’);

// comments

$total_comments = DB::get_value ( ‘select count() from {comments}’ );

echo ‘Total comments: ‘ . $total_comments . "n";

// get the comment types from habari
$comment_types = Comment::list_comment_types();
$comment_statuses = Comment::list_comment_statuses();

for ( $i = 0; $i < $total_comments / 10; $i++ ) {

// get the comments
$comments = DB::get_results( ‘select post_id, name, email, url, ip, date, content, status, type from {comments} order by id limit 10 offset ‘ . $i  10 );

echo ‘Got ‘ . count( $comments ) . ‘ comments’ . "n";

foreach ( $comments as $comment ) {

$comment->date = HabariDateTime::date_create( $comment->date );

$comment_status = $comment_statuses[ $comment->status ];
$comment_type = $comment_types[ $comment->type ];

if ( $comment_status == ‘approved’ ) {
$comment_approved = true;
}
else {
$comment_approved = false;
}

$insert_query = ‘insert into ‘ . $import[‘wp_prefix’] . ‘comments (
comment_post_id,
comment_author,
comment_author_email,
comment_author_url,
comment_author_ip,
comment_date,
comment_date_gmt,
comment_content,
comment_approved,
comment_type
) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )’;

$insert_params = array(
$wp_posts[ $comment->post_id ],
$comment->name,
$comment->email,
$comment->url,
long2ip( $comment->ip ),
$comment->date->format( ‘Y-m-d H-i-s’ ),
gmdate( ‘Y-m-d H-i-s’, $comment->date->int ),
$comment->content,
$comment_approved,
$comment_type
);

$wpdb->query( $insert_query, $insert_params );

}

}

// update comment user_id links based on user display names
$wpdb->query( ‘update ‘ . $import[‘wp_prefix’] . ‘comments c set user_id = ( select ID from ‘ . $import[‘wp_prefix’] . ‘users where display_name = c.comment_author ) where comment_author in ( select distinct display_name from ‘ . $import[‘wp_prefix’] . ‘users )’);

// update post comment counts
$wpdb->query( ‘update ‘ . $import[‘wp_prefix’] . ‘posts p set comment_count = ( select count() from ‘ . $import[‘wp_prefix’] . ‘comments where comment_post_ID = p.ID and comment_approved = ‘1’ )’ );

echo ‘</pre>’;

?>

Done MVC

Today, I done my MVC framework raw, not include library file.

Just call Controller -> Model -> View

I told about my controller in yesterday. Here my view design

class simpleModel extends Model {

function simpleModel()
{
parent::Model();
}
function printit()
{
$data[‘a’]=1;
$this->load->view("Test",$data);
}
}

Here test view

<h3>Title</h3><?= $a; ?>

how nice. Haha… you may told, imm… totally look like CI. Of course, I’m crazy in CI and my MVC look like CI but code is not same. I also used extract method for array key to variable. Now, I just need to combine with my old apiwork. After that, apiwork will be MVC framework.

load model look like CI

<div class="CodeRay">
Abstract class Controller extends Loader {
public $load;
function Controller()
{
        $this->load=$this;
}
abstract function index();
}

So, I can call like this

class IndexController extends Controller {

    function IndexController()
    {
        parent::Controller();
    }

        function index()
        {
                $this->load->model("simple");
                print_r($this->simple);
        }
}

This is my first time setting look like this and I never thinking before

$this->load=$this ;

In the old, I calling like this

$this->load->simple->function();

hmm… How stupid it!!! Cheer!!!! Saturngod

Apiwork

I gave my framework name is apiwork because it just design for api. I made api library for Google,Twitter, Facebook and I hope i will add more in my framework. It’s my private framework and it just use in my work. In the previous two week, I tooke a time for Google API with oAuth. Now, I totally understand and I made my own class for Google API and gxml_parser class. Gxml parser class for retrive the data from atom and xml for google api. 

My framework totally look like CodeIgniter because I’m cary on CI. haha… it true… let check this code.

$this->load->model(“mymodel”);

I got this idea from CodeIgniter and I’m making MVC and totally base on CI frarmework design. So, syntax look like CI. By the way, today I new codaset git hosting. I can use private hosting in there. Codaset still beta and it will be charge for host. I host my apiwork code in there. Github is nice for Git Hosting but private hosting price is not cheap. I just use for personal and not for business. So, I can give money to github and I use codaset :)

I also think about should I release my MV framework Draft ? No library. Just framework. So, some begineer may learn how to make MVC framework. Another thing, my framework’s library should use in native PHP like Zend. So, my co-worker can use in other project easily. Imm… need to do many things in my apiwork.

 

Busy with oAuth

In this days, I am so busy with oAuth. I host my mediatemple and trying oAuth playground. All are ok but post and put have been error. I try, try… but same error. So, I try to change in Google oAuth Playground and it’s working. It’s a problem or hosting or what that ? I lost 2 days with oAuth Playground in mediatemple. Now, trying oAuth in google code playground and I need to soultion in my hosting mediatemple.

Working with date() in PHP

Date is a import for repoarting system. Sometime, we forget about the date(). I forget about some function in date() and I have many problem occur in reporting system.

First thing is minute. I though, minute is a m and so I use like that date(“h:m:s”); It was wrong. Minute use i not m.  You can check in there. Another problem is a time zon. date() also retrive the UTF time zone and we need to config it. I found that in php.net. It’s very suitable but I use like that.

putenv(“TZ=Singapore”); $sg_day=date(“m-d-y H:i”);

It’s a short and easy to understand. So, I use like that and config timezone to singapore. Now, my reporting system corrected and I was happy for learning about date() function.

 

Cheer!!