RSS

Facebook Never Note アプリ(Windows のNotepadみたいなの)

18 8月

<ソース> 


Facebook Never Note アプリ(Windows のNotepadみたいなの)


<本文>


Facebook Never Note アプリ(Windows のNotepadみたいなの)



  • このアプリケーションはノートを書いて保存できるアプリです。

  • このアプリケーションは、Facebook ユーザアカウントを使用して、ユーザ毎にデータ保存、検索できるアプリケーションです。このアプリケーションで、Facebookのログイン、ログアウト、データ保存方法がわかります。 
    ※データの保存、検索にははフェイスブックユーザID(long型)をテーブルに紐づけていますのでそのユーザしかデータがみえません。

  • まずはアプリケーションを動かしてみよう。
    Facebook内側で表示
    http://apps.facebook.com/exoneapptest/

    Facebook外側で表示
    http://facebook.exceedone.co.jp/appstest/ 

 


 


ダウンロード – プログラムソース(PHP), DB作成用SQL(mysql)


参考にどうぞ


プログラムソース(PHP), DB作成用SQL(mysql)です。
※すべてのファイルはUTF-8ですので、編集するときはUTF-8のEditorで開いてください。(Eclipse等)



  1. 20110819日(V1)

    1. プログラムソース(PHP)
      appstest20110819.zip

    2. DB作成用SQL(mysql)
      appstest20110819_SQL.zip

 


基本機能の説明



  • 以下のURLをブラウザーでひらきます。
    フェイスブックにログインされていないと 右上に「Log in(Facebook)」ボタンが現れますのでクリックします。
    http://facebook.exceedone.co.jp/appstest/


     

  • このアプリケーションを初めて実行するときは、実行許可を求められますので右下の「許可する」をクリックします。
     
     

  • ログインできますと、「Welcom Top Page」と表示されます。
    右のほうの「Nevernote」をクリックします。
     
     

  • 「Add」ボタンをクリックします。(新規作成できます。)
     
     

  • データを入力して「Add」ボタンを押すとDB(MySQL)にデータが追加されます。
     
     

  • データが追加されと以下のように一覧に表示されます。
    編集したい場合は「編集」ボタンをクリックしてください。
    削除したい場合は「削除」ボタンをクリックしてください。

 


 


フェイスブックのアプリケーション登録




  1. フェイスブックアプリケーションの初期設定方法は以下のリンクの真ん中あたりの「Facebookにアプリケーションを登録するを参考にしてください。
    Facebookにアプリケーションを登録する。
     

  2. フェイスブックでログイン後以下のURLでアプリを登録します。
    https://www.facebook.com/developers/apps.php
     

    1. アプリ名称
      appstest

    2. [Web Site][サイトURL][キャンバスページ]
      ※フェイスブックの内側で動作する。
      http://apps.facebook.com/exoneapptest/
        

    3. [Web Site][サイトURL][キャンバスページURL]
      ※フェイスブックの外側で動作する。
      http://facebook.exceedone.co.jp/appste
       st/

    4. [詳細設定][サンドボックス]
      有効にする。

 


フォルダー構成

フォルダー構成(PHP,TPL(Smarty), CSS)
appstest-+- .htaccess (一覧不可, この拡張子のアクセス不可(ht,tpl,crt)) |- index.php (ログインのコントロール) |- logout.php (ログアウトのコントロール) |- nevernote.php (Nevernoteのコントロール) |- top.php (ログイン後の初期ページ) |- css/ (CSSファイル) |- libs/ -+- .htaccess (このフォルダーのアクセス不可) | |- base_facebook.php (Facebook提供のベースクラス) | |- common.application.php(共通クラスアプリケーション用) | |- common.index.php (共通クラスIndex用) | |- common.php (共通クラスCommon用) | |- DBConnect.class.php (MySQL DB接続クラス) | |- facebook.php (Facebook提供のクラス) | |- fb_ca_chain_bundle.crt(Facebook提供のcrt) | |- Nevernote.class.php (Nevernoteのクラス) | |- SmartyEx.class.php (Smartyの設定クラス) |- properties/ -+- — .htaccess (このフォルダーのアクセス不可) | |- config.inc.php (サーバ等の環境の設定ファイル) |- smarty/ (Smarty-2.6.26) |- smartytemplate/-+- .htaccess (このフォルダーのアクセス不可) |- configs/ (今回は使用しない)             |- templates/-+- foot.tpl (共通フッターtemplate)                            |             |- head.tpl (共通ヘッダーtemplate)                           |             |- login.tpl (ログインtemplate)                         |             |- nevernotedetail.tpl(明細template)                         |             |- nevernotemain.tpl (メインtemplate)                         |             |- top.tpl (Toptemplate)                           |             |- transfer.tpl (URL転送時template) +- templates_c/ (重要:Apacheの書き込み権限が必要です)
※template_c の権限の変更コマンド




#chmod -R 777 templates_c/
 

 


実行環境について(Apache, PHP, MySQLバージョン)


#httpd -v
Server version: Apache/2.2.3
Server built:   May 4 2011 06:51:15


# apachectl -v
Server version: Apache/2.2.3
Server built:   May 4 2011 06:51:15


# rpm -qa php*
php-cli-5.1.6-27.el5_5.3
php-pdo-5.1.6-27.el5_5.3
php-mcrypt-5.1.6-15.el5.centos.1
php-xml-5.1.6-27.el5_5.3
php-mbstring-5.1.6-27.el5_5.3
php-mysql-5.1.6-27.el5_5.3
php-pear-1.4.9-6.el5
php-common-5.1.6-27.el5_5.3
php-5.1.6-27.el5_5.3
php-devel-5.1.6-27.el5_5.3


[root@www001 ~]# rpm -qa mysql*
mysql-devel-5.0.77-4.el5_6.6
mysql-5.0.77-4.el5_6.6
mysql-server-5.0.77-4.el5_6.6


データベースとテーブルの構造 (MySQL)





  1. データベース作成
    mysql>CREATE DATABASE IF NOT EXISTS appstest;
     

  2. テーブル作成
    mysql>USE appstest;

    mysql>CREATE TABLE `nevernote` (
                              `idnevenote` int(10) unsigned NOT NULL AUTO_INCREMENT,
                              `facebookuserid` bigint(20) unsigned NOT NULL,
                              `title` text NOT NULL,
                              `detail` text NOT NULL,
                              PRIMARY KEY (`idnevenote`),
                              KEY `Index_1` (`facebookuserid`)
                             ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1; 
     


  3. testuser (localhostから)が appstest のテーブルがアクセスできるようにします。
    grant all privileges on appstest.* to testuser@localhost identified by ‘testuser123’;

Smartyについて





  1. Smartyとは

    1. 簡単に説明しますと、PHPプログラムから、「Smarty->Display(xxx.tpl)」という命令を実行するととxxx.tplを読み込み、htmlとして出力する機能です。 
       

    2. Smarty自体もPHPプログラムですので、サーバの任意の場所に設置して使用します。
       ApacheのDocument Folder配下でなくてもOKです。
      Apacheに読み取り権限があるフォルダーであればOKです。
       

    3. 注意としては、実際には、Smartyは 「template_c」 というところに、事前にtplからコンパイルした、phpを出力しますのでApacheにはこのフォルダーの書き込み権限が必要です。
       

  2. 「appstest/libs/SmartyEx.class.php」 の説明
    以下はSmartyのtemplateの場所を指定しています。




    <?php

    require_once(_ROOT_DOCUMENT . “smarty/libs/Smarty.class.php”);


    class SmartyEx extends Smarty{
     function SmartyEx(){
     $this->Smarty();
     $this->template_dir= _ROOT_DOCUMENT . “smartytemplate/templates/”;
     $this->compile_dir = _ROOT_DOCUMENT . “smartytemplate/templates_c/”;
     $this->config_dir = _ROOT_DOCUMENT . “smartytemplate/configs/”;
     }
    }
    $smarty = new SmartyEx();
    ?>




 


Facebookログイン、ログアウトの仕組み



  • 「appstest/index.php」 の説明
    indexなので最初によびだされるphpです。Facebookユーザがログインされているか確認しています。
    ログインされている場合は「top.php」を呼び出します。




    //——————————————————————
    //Include common.index.php class
    require_once(“libs/common.index.php”);
    //——————————————————————

    //Include Facebook class(フェイスブックのクラスをインクルードする。)
    require_once(_ROOT_DOCUMENT . ‘libs/facebook.php’);


    //Get Facebook Object with Facebook ApplicationID (フェイスブックのアプリケーションIDでオブジェクトを取得する。
    $facebook = new Facebook(array(
       ‘appId’ => _FACEBOOK_APPLICATIONID,
       ‘secret’ => _FACEBOOK_SECRETKEY,
    ));


    //Get Facebook userid (フェイスブックのユーザIDを取得する)
    $user = $facebook->getUser();


    //Check login Facebook(フェイスブックにログイン済みか確認する。)
    if ($user) {
     try {
     //Get Facebook user profile (フェイスブックのユーザプロフィールを取得する。)
     $user_profile = $facebook->api(‘/me’);
     } catch (FacebookApiException $e) {
     error_log($e);
     $user = null;
     }
    }


    if($user)
    {
     //フェイスブックにログイン済み


     //フェイスブックのユーザ情報をセッションに保存する。
     $_SESSION[‘FB’][‘user’]=$user;
     $_SESSION[‘FB’][‘name’]=$user_profile[‘name’];


     $_SESSION[‘FB’][‘user_profile’]=$user_profile;
     $_SESSION[‘FB’][‘logoutUrl’]=$facebook->getLogoutUrl();


     //top画面表示する。
     $smarty->assign(‘transferurl’, ‘./top.php’);
     $smarty->display(‘transfer.tpl’);


    }
    else
    {
     //フェイスブックにログインしていない。


     //login画面表示する。
     $loginUrl=$facebook->getLoginUrl();
     $smarty->assign(‘loginUrl’, $loginUrl);
     $smarty->display(“login.tpl”);
    }


    ?>


     


  • 「appstest/logout.php」 の説明
    ログアウト時にはフェイスブックユーザのログアウトとセッションの削除をしています。




    //——————————————————————
    //Include common.application.php class
    require_once(‘libs/common.application.php’);
    //——————————————————————

    //Display Logout Screen (logout画面表示)
    $smarty->assign(‘transferurl’, $_SESSION[‘FB’][‘logoutUrl’]);
    $smarty->display(‘transfer.tpl’);


    //Clear Session (セッションクリアする。)
    session_destroy();


    ?>




Nevernoteの仕組み


「appstest/nevernote.php」 の説明
Nevernoteの制御をしているPHPで、状況により、いろんな tpl(Smartyテンプレート)を呼び出しています。





//——————————————————————
//Include common.application.php class
require_once(‘libs/common.application.php’);
//——————————————————————

//Include Nevernote.class for using db(DBの操作Classの呼び出し)
require_once(_ROOT_DOCUMENT.’libs/Nevernote.class.php’);
$objNevernote = new Nevernote($objDBConnect);


if (isset($_REQUEST[“submit_main”]))
{
 //nevernotemain.tpl, [Search button] or [Add button] が押されたとき。


 if (isset($_REQUEST[“submit_search”]))
 {
 //nevernotemain.tpl, [Search button] が押されたとき。
 displaylist($objNevernote, $smarty);
 }
 elseif (isset($_REQUEST[“submit_add”]))
 {
 //nevernotemain.tpl, [Add button] が押されたとき。
 $smarty->assign(‘detail_mode’, ‘add’);
 $smarty->display(‘nevernotedetail.tpl’);
 }
}
elseif (isset($_REQUEST[“submit_main_list”]))
{
 //nevernotemain.tpl, [Edit button] or [Delete button] が押されたとき。
 if (isset($_REQUEST[“submit_edit”]))
 {
 //nevernotemain.tpl, [Edit button] が押されたとき。
 $nidnevenote = getArrayKeyFirst($_REQUEST[“submit_edit”]);
 $dataNevernote = $objNevernote->GetOne($nidnevenote);
 $smarty->assign(‘dataNevernote’, $dataNevernote);
 $smarty->assign(‘detail_mode’, ‘edit’);
 $smarty->display(‘nevernotedetail.tpl’);
 }
 elseif (isset($_REQUEST[“submit_delete”]))
 {
 //nevernotemain.tpl, [delete button] が押されたとき。
 $nidnevenote = getArrayKeyFirst($_REQUEST[“submit_delete”]);
 $dataNevernote = $objNevernote->Delete($nidnevenote);


 displaylist($objNevernote, $smarty);
 }
}
elseif (isset($_REQUEST[“submit_detail”]))
{
 //nevernotedetail.tpl, [Add button] or [Update button] or [Cancel button] が押されたとき。


 if (isset($_REQUEST[“submit_detail_Add”]))
 {
 //nevernotedetail.tpl, [Add button] が押されたとき。
 $data[“title”]=$_REQUEST[“title”];
 $data[“detail”]=$_REQUEST[“detail”];
 $objNevernote->Insert($_SESSION[‘FB’][‘user’], $data);
 }
 elseif (isset($_REQUEST[“submit_detail_Update”]))
 {
 //nevernotedetail.tpl, [Update button] が押されたとき。
 $nidnevenote=$_REQUEST[“idnevenote”];
 $data[“title”]=$_REQUEST[“title”];
 $data[“detail”]=$_REQUEST[“detail”];
 $objNevernote->Update($nidnevenote, $data);


 }
 elseif (isset($_REQUEST[“submit_detail_Cancel”]))
 {
 //nevernotedetail.tpl, [Cancel button] が押されたとき。
 }


 displaylist($objNevernote, $smarty);
}
else
{
 //Init (最初)
 displaylist($objNevernote, $smarty);
}


//一覧表示
function displaylist($objNevernote, $smarty)
{
 $listNevernote = $objNevernote->GetList($_SESSION[‘FB’][‘user’]);
 $smarty->assign(‘listNevernote’, $listNevernote);
 $smarty->display(‘nevernotemain.tpl’);
}


?>


DBクラスの説明(mysql)


「appstest/libs/DBConnect.class.php」 の説明
MySQLのDBにアクセスするクラスです。





class DBConnect {

 var $dbname;
 var $host;
 var $port;
 var $user;
 var $password;
 var $socket;
 var $con      = false;


 function DBConnect($DBNAME, $HOST, $PORT, $USER, $PASSWORD, $SOCKET) {
 $this->dbname = $DBNAME;
 $this->host   = $HOST;
 $this->port   = $PORT;
 $this->user   = $USER;
 $this->password = $PASSWORD;
 $this->socket = $SOCKET;


 $this->GetConnection();
 }


 function GetConnection(){
 if($this->con == false) {
   return($this->_DBConnect());
 }
 return($this->con);
 }


 function _DBConnect(){
 $result = mysql_pconnect($this->host.”:”.$this->socket, $this->user, $this->password);
 if($result==false)
 {
   printf(“Error DB Connect:”.mysql_error());
   return false;
 }


 $result = mysql_select_db($this->dbname);


 if($result==false)
 {
   printf(“Error DB Select:”.mysql_error());
   return false;
 }
 }


 function DBClose(){
 if($this->con != false){
   mysql_close($this->con);
   $this->con = false;
 }
 }


 function Query($sql){
 $result = mysql_query($sql);
 if(!$this->CheckQuery($sql,$result)) return false;
 return $result;
 }


 function FetchOne($sql){
 $result = mysql_query($sql);
 if(!$this->CheckQuery($sql,$result)) return false;


 if($result != false){
   $num = mysql_num_rows($result);
   if($num > 0){
    $data = mysql_fetch_object($result);
    settype($data, “array”);
    return $data;
   }else{
    return false;
   }
 }else{
   return false;
 }
 }


 function FetchAll($sql)
 {
 $result = mysql_query($sql);
 if(!$this->CheckQuery($sql,$result)) return false;


 if($result != false)
 {
   $num = mysql_num_rows($result);
   if($num > 0)
   {
    for($i=0;$i<$num;$i )
    {
     $list[$i] = mysql_fetch_object($result);
     settype($list[$i], “array”);
    }
    return $list;
   }else
   {
    return false;
   }
 }else
 {
   return false;
 }
 }


 function CheckQuery($sql, $result)
 {
 if($result == false){
   printf(“Error Query; SQL=”.$sql.”; Error Detail=”.mysql_error());
   return false;
 }
 return true;
 }
}
?>




Commonクラスの説明



  • 「appstest/libs/common.php.php」 の説明
    commonの共通ルーチン




    <?php
    define(‘_ROOT_DOCUMENT’, dirname(__FILE__).’/../’);

    require_once(_ROOT_DOCUMENT.’properties/config.inc.php’);
    require_once(_SMARTYLOCATION);


    if (!isset($_SESSION)) {
     session_start();
    }


    $smarty->assign(‘site_title’, _SITE_TITLE);


    // Common function
    function getArrayKeyFirst($array)
    {
     $arrykeys = array_keys($array);
     return $arrykeys[0];


    }
    ?>


     


  • 「appstest/libs/common.index.php」 の説明
    appstest/index.php にインクルードされるヘッダー




    <?php
    require_once(dirname(__FILE__).’/../’.’libs/common.php’);
    ?>

     


  • 「appstest/libs/common.application.php」 の説明
    appstest/index.php 以外にインクルードされるヘッダー




    <?php
    require_once(dirname(__FILE__).’/../’.’libs/common.php’);

    //ログインしていない場合はLogin画面へ戻る
    if (!isset($_SESSION[‘FB’][‘user’]))
    {
     //index画面表示する。
     $smarty->assign(‘transferurl’, ‘./index.php’);
     $smarty->display(‘transfer.tpl’);
     exit;
    }


    $smarty->assign(‘name’, $_SESSION[‘FB’][‘name’]);
    $smarty->assign(‘user’, $_SESSION[‘FB’][‘user’]);
    $smarty->assign(‘logoutUrl’, $_SESSION[‘FB’][‘logoutUrl’]);


    // DB
    require_once(_ROOT_DOCUMENT.’libs/DBConnect.class.php’);
    $objDBConnect = new DBConnect(_DBNAME, _HOST, _PORT, _USER, _PASSWORD, _SOCKET);


    ?>

 

コメントを残す