Personal Project

Monday, April 11, 2016

How to cache UIWebView webpage for offline viewing ?

There is an issue when invoking UIWebView API on iOS that webpage won`t be displayed 
immediately.  It will take around 2 seconds to load the html page from the server and display
it properly. 
In order to optimize the UI, i suggest that  preloading the webpage in cache and it could be displayed for offline viewing at anytime.  

This solution has integrated UnityAds SDK for displaying video ad and the source codes can be seen at github.


Monday, March 7, 2016

Configuring Tomcat To Use SSL

Setting up SSL for Tomcat can be pided into two main tasks: creating a functional keystore, and configuring the Tomcat connectors and applications. Let's tackle them one at a time.  

Step 1 - Creating the Keystore

$JAVA_HOME/bin/keytool -genkey -alias 6waves -keyalg RSA -keystore /key/keystore

$JAVA_HOME/bin/keytool -genkey -alias tomcat -keyalg RSA -keystore /key/keystore


 

Step 2- Creating the Certificate Signing Request


$JAVA_HOME/bin/keytool -certreq -keyalg RSA -alias 6waves -file 6wavesCerificate.csr -keystore /key/keystore
$JAVA_HOME/bin/keytool -certreq -keyalg RSA -alias [youralias] -file [yourcertificatname].csr -keystore [path/to/your/keystore]

 

Step 3
Download CA



 

Step 4

To import the Root Certificate -
keytool -import -alias root -keystore /key/keystore -trustcacerts -file GeoTr                                                      ust_Global_CA.cer
To import your new Certificate -
keytool -import -alias 6waves -keystore /key/keystore -file
keytool -import -alias [youralias] -keystore [path/to/your/keystore] -file [path/to/your_keystore]

Test URL
https://your server ip:8443/test.jsp

Enable root user password to login EC2 on Ubuntu


Edit your SSHd config to allow password based logins:


    sudo nano -w /etc/ssh/sshd_config

Add the following line to /etc/ssh/sshd_config:
    PasswordAuthentication yes

Copy your authorized SSH keys if you want to log in via keys as well:
    sudo mkdir /root/.ssh
    sudo cp /home/ubuntu/.ssh/authorized_keys /root/.ssh/

Reload your SSHd config:
    sudo reload ssh

Now you can login as root!

How to install Tomcat and Deploy war on Ubuntu ?

Install JDK

sudo apt-get install openjdk-7-*

Install tomcat

sudo apt-get install tomcat7*

Setup tomcat

vim /etc/tomcat7/tomcat-users.xml
sudo vim /etc/tomcat7/server.xml

Setup JAVA HOME

sudo vim /etc/profile JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64 export JAVA_HOME PATH=$PATH:$JAVA_HOME/bin export PATH

Install tomcat7

sudo apt-get install tomcat7

Start tomcat7

sudo service tomcat7 start

Stop tomcat7

sudo service tomcat7 stop

Restart tomcat7

sudo service tomcat7 restart

Deploy war

Upload war to folder /var/lib/tomcat7/webapp

How to install LAMP on Ubuntu ?

1. Install Apache



2. Install MySQL



3. Install PHP


Restart Server



Check PHP

You can check your PHP by executing any PHP file in folder /var/www/html












Wednesday, March 2, 2016

Send Email in PHP with Japanese subject and body


   Before, we get started, you must have an SMTP server that can receive the emails from your machines 

   and send them to the recipient

    (i.e. your corporate exchange or Gmail).

 

    How to Setup Email on Linux Using Gmail and SMTP

  • Install ssmtp 

    sudo apt-get install ssmtp

    Edit ssmtp file

    sudo vim /etc/ssmtp/ssmtp.conf
  • root=username@gmail.com
    mailhub=smtp.gmail.com:465
    rewriteDomain=gmail.com
    AuthUser=username@gmail.com
    AuthPass=password
    FromLineOverride=YES
    UseTLS=YES

In order to make the default (root) “from” field be the server name, edit the/etc/ssmtp/revaliases file:
sudo vim /etc/ssmtp/revaliases
And add into it the desired translation which in our Gmail examples case will be:
root:username@gmail.com:smtp.gmail.com:465
Incredibly this is all you have to do to enable the ability. From now on, the machine will Email you when something is up.

Test Email Command

Lets test that our ssmtp setup was correct by sending an Email:

echo "Test message from Linux server using ssmtp" | sudo ssmtp -vvv your-email@some-domain.com

 

 

Use the following codes to send email with PHP

 

 

    //  $to  : receiver email address
    //  $subject :  Japanese subject
    //  $message : Japanese body
    function sendEmail($to, $subject, $message)
    {
        if(!isset($to) || !isset($subject) || !isset($message)) {
            return;
        }
   
         // Set up environment
        mb_language('Japanese');
        mb_internal_encoding('UTF-8');
   
        // Set up subject
        $headers ='From: '.mb_encode_mimeheader("ありがとうございます").' <test@123.jp>' . "\r\n";
                'Reply-To: test@123.jp' . "\r\n" .
                    'X-Mailer: PHP/' . phpversion();
   
          // Set up body
        $message = mb_convert_encoding($message, "JIS", "UTF-8");
       
         //  Send email  
        if(mb_send_mail($to, $subject, $message, $headers))
        {
                error_log("#### Send mail OK ####");
   
        }else {
                 error_log("#### Send mail Error ####");
      }
       
    } // End Send Email

 




Create a dropdown Date using JavaScript

1. Include jquery, moment.js and combodate.js on your page
  1. <script src="js/jquery.js"></script>
  2. <script src="js/moment.min.js"></script>
  3. <script src="js/combodate.js"></script>
2. Markup input elements to be used for date and time values
  1. <input name="playerDate" value="15-05-1984" data-format="DD-MM-YYYY" data-template="D MMM YYYY">  
3.Markup Javascript
 <script type="text/javascript">
  // Date 
  $(function(){
   $('#playerDate').combodate();  });
</script> 

 
Download source code: 
https://github.com/gcoolmaneric/jquery-php-html5/tree/master/dropdownDate

 

Upload images using JQuery and PHP for different device

Do you want to create a form that could allow you to upload images on different device ?
You can use jquery and php to make it done.

Add the following codes to html page. 

<body>
     <form id="upload" method="post" action="upload.php" enctype="multipart/form-data">
            <div id="drop">
                Drop Here

                <a>Browse</a>
                <input type="file" name="upl" multiple />
            </div>

            <ul>
                <!-- The file uploads will be shown here -->
            </ul>

        </form>

        <!-- JavaScript Includes -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script src="assets/js/jquery.knob.js"></script>

        <!-- jQuery File Upload Dependencies -->
        <script src="assets/js/jquery.ui.widget.js"></script>
        <script src="assets/js/jquery.iframe-transport.js"></script>
        <script src="assets/js/jquery.fileupload.js"></script>
       
        <!-- Our main JS file -->
        <script src="assets/js/script.js"></script>
</body>

PHP Code

upload.php
<?php

// A list of permitted file extensions
$allowed = array('png', 'jpg', 'gif','zip');

if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){

    $extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);

    if(!in_array(strtolower($extension), $allowed)){
        echo '{"status":"error"}';
        exit;
    }

    if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.$_FILES['upl']['name'])){
        echo '{"status":"success"}';
        exit;
    }
}

echo '{"status":"error"}';
exit;

?>

Download source codes:
https://github.com/gcoolmaneric/jquery-php-html5/tree/master/uploadImage


Change the maximum upload file size


You need to set the value of upload_max_filesize and post_max_size in your php.ini :
 vim /etc/php5/apache2/php.ini 
; Maximum allowed size for uploaded files.
upload_max_filesize = 20M

; Must be greater than or equal to upload_max_filesize
post_max_size = 20M

Tuesday, March 1, 2016

How to clear browser cache with S3 on AWS ?

If you use S3 CDN on AWS to speed up website`s performance, your updated files will not take effect immediately.  The cause of this problem is the browser usually tries to save and read downloaded files like SWF at local cache.

How do you  tell the browser to clear its cache while updating files ?
You can add the following header to solve this problem.

Add Cache Clear header manually with S3 UI

1. Upload files to S3 

2.  Select your uploaded files and Add "Cache-Control" header to enforce the browser not to cache any files and to read the latest ones.

header : Cache-Control
value:     max-age=0, no-cache, no-store, must-revalidate
 

3. Push a button to make your uploaded files public 


Upload files to S3 and add Cache Clear header automatically

1. Install aws and s3cmd
// Install aws
apt-get install aws
// Download and install s3cmd
http://s3tools.org/repositories

// Install
tar xvfz s3cmd-1.0.0-rc1.tar.gz
cd s3cmd-1.0.0-rc1/
python setup.py install

// Set up s3cmd
./s3cmd --configure

 

2.Deployment 
Copy the following commands in your script file. Then you can execute this task automatically while updating files.

// Upload Files to s3
aws s3 sync /var/www/html/yourproject/client s3://yourbucket/client --cache-control max-age=0 --acl public-read

// Add Cache Clear header
s3cmd --recursive modify --add-header="Cache-Control:max-age=0, no-cache, no-store, must-revalidate" s3://yourbucket/client

// Make files Public
s3cmd setacl s3://yourbucket/client --acl-public --recursive

Monday, February 29, 2016

Development Tool for Quick-Cocos2dx-Community Lua


I have tried many IDE tools for programming Lua. Cocos IDE v1.2 only supports Quick-cocos2d v3.3 and Sublime is nothing but an editor. I want to find an IDE that could make break point for debugging and easy to use. ZeroBraneStudio is the most easiest IDE i recommend you use.

// Download ZeroBraneStudio - https://studio.zerobrane.com/
1.Extract zip ZeroBraneStudio-1.30-38


2.Execute zbstudio.exe

3.Import source codes into zbstudio

4. Add ZeroBrane Studio [ZeroBrane]/lualibs/mobdebug/mobdebug.lua)to src/

5. Add the following line of code to  coinflip/scripts/main.lua
require("mobdebug").start()
6. At ZeroBrane Studio, select Project->Start Debugger Server
7.Set up break point
8.Open main.lua by player3
9.Debug

How to buld Quick-Cocos2dx-Community for Android and iPhone ?

Build APK for Android


1.Install


    JDK  

        http://www.oracle.com/technetwork/java/javase/downloads/index.html

    Android SDK 

        http://developer.android.com/sdk/installing/index.html?pkg=tools

    Android NDK 9d

        http://dl.google.com/android/ndk/android-ndk-r9-windows-x86_64.zip

    Apache Ant

        http://ant.apache.org/bindownload.cgi



2.Configure environment 


// Change settings for JAVA_HOME, ANT_ROOT,

//  NDK_ROOT, ANDROID_NDK_ROOT

//  ANDROID_SDK_ROOT,  ANDROID_HOME

$ vim ~/.profile

export JAVA_HOME="$(/usr/libexec/java_home)"

# Add environment variable ANT_ROOT for cocos2d-x

export ANT_ROOT=/usr/local/apache-ant/bin

        

# Add environment variable NDK_ROOT for cocos2d-x

export NDK_ROOT=/Users/Tester/Desktop/cocos2dx/android/android-ndk-r9d

export PATH=$NDK_ROOT:$PATH

export ANDROID_NDK_ROOT=/Users/Tester/Desktop/cocos2dx/android/android-ndk-r9d

export PATH=$ANDROID_NDK_ROOT:$PATH

   

# Add environment variable ANDROID_SDK_ROOT for cocos2d-x

export ANDROID_SDK_ROOT=/Users/Tester/Desktop/cocos2dx/android/android-sdk-macosx

export ANDROID_HOME=/Users/Tester/Desktop/cocos2dx/android/android-sdk-macosx

export PATH=$ANDROID_HOME:$PATH

PATH=/usr/local/bin:$PATH:~/bin:~/bin/python:$ANDROID_SDK_ROOT/tools:$ANDROID_SDK_ROOT/platform-tools:$ANT_ROOT:

# Quick cocos2dx

export QUICK_V3_ROOT=`cat ~/.QUICK_V3_ROOT`


3. Create a project named “dev” by player


// Goto Android folder of dev

cd dev/frameworks/runtime-src/proj.android

// clean files

./clean.sh

// Compile  Quick-Cocos2dx-Community Engine Lib and output libcocos2dlua.so

./ build_native.sh

// Update Android Settings

./android update project -p . -t 1

// Set up properties pointing to Quick-Cocos2d-Community folder

// Relative Path

vim project.properties

android.library.reference.1=../../../../../cocos2dx/Quick-Cocos2dx-Community/cocos/platform/android/java


4. Update Android settings in Quick-cocos2dx-Community folder


 cd cocos2dx/Quick-Cocos2dx-Community/cocos/platform/android/java

./android update project -p . -t 1

5.  Build apk and debug


// Goto Android folder of dev

cd dev/frameworks/runtime-src/proj.android

// Build

// Dev mode

ant debug 

// Release

ant release

//  Clean settings

ant clean

// Deploy Apk to Android device

apk is saved in proj.android/bin/dev-debug.apk

cd proj.android/bin/

adb install dev-debug.apk


Build IPA for iPhone


// Goto iPhone project folder

cd dev/frameworks/runtime-src/proj.ios_mac/

// Open xcode project and build ipa

cd dev/frameworks/runtime-src/proj.ios_mac/


Sunday, February 28, 2016

How to setup PHP development for Couchbase ?

// Install PHP Lib for couchbase

gie clone git@github.com:couchbase/php-ext-couchbase.git


// Install couchbase.so

http://packages.couchbase.com/ubuntu/couchbase.key
wget -O http://packages.couchbase.com/ubuntu/couchbase.key
sudo apt-key add
echo "deb http://packages.couchbase.com/ubuntu trusty trusty/main" >> /etc/apt/sources.list.d/couchbase
apt-get update
sudo apt-get install libcouchbase2-core libcouchbase2-libevent libcouchbase2-devel
pecl install couchbase-1.2.5


// Setup  couchbase.so

cd /etc/php5/apache2/conf.d
ln -s ../../mods-available/couchbase.ini 30-couchbase.ini
cd /etc/php5/cli/conf.d
ln -s ../../mods-available/couchbase.ini 30-couchbase.ini


// Check command

php -i | grep ini

 

Install, Uninstall, Backup, and Restore Couchbase.

Install Couchbase

# sudo dpkg -i couchbase-server-enterprise_3.0.2-ubuntu12.04_amd64.deb 

Uninstall Couchbase

# sudo dpkg -r couchbase-server

 Backup Couchbase

# ./cbbackup http://IP Address:8091 /home/backup-dump

Restore Couchbase

#./cbrestore /home/backup-dump http://username:password@IP Address:8091 --bucket-source=default 

reference : http://docs.couchbase.com/couchbase-manual-2.5/cb-install/