由樹的節點取得他的路徑

因為案子的需要..
所以在 stackoverflow 找到了以下這個方法
可以直接整合進之前的 Tree Class 中..
記錄一下

  1.  <?php
  2.  function get_node_path($from_array, $lookup) {
  3.      if (array_key_exists($lookup, $from_array) === true) {
  4.          return array($lookup);
  5.      }else{
  6.          foreach ($from_array as $key => $sub_array) {
  7.              if (is_array($sub_array) === true) {
  8.                  $ret = get_node_path($sub_array, $lookup);
  9.  
  10.                  if ($ret) {
  11.                      $ret[] = $key;
  12.                      return $ret;
  13.                  }
  14.              }
  15.          }
  16.      }
  17.  
  18.      return null;
  19.  }
  20.  ?>

處理 PayPal 的 IPN 返回

這程式早在十月份時已經完成.
是寫給當時應一位朋友用於學習和請教的.
原來還沒記錄在這裡..代碼節錄如下:

  1.  <?php
  2.  // 載入必要的檔案,如 init.php / config.php 之類的 ....
  3.  
  4.  $paypal_url = $config['payment']['paypal_url'];
  5.  
  6.  // Parse url
  7.  $parsed_paypal_url = parse_url($paypal_url);
  8.  
  9.  // Build post string
  10.  $post_data = http_build_query($_POST)."&cmd=_notify-validate";
  11.  
  12.  // Set the port number
  13.  if($parsed_paypal_url['scheme'] == "https") {
  14.      $ssl = "ssl://";
  15.      $parsed_paypal_url['port'] = "443";
  16.  } else {
  17.      $parsed_paypal_url['port'] = "80";
  18.  }
  19.  
  20.  //Create paypal connection
  21.  $fp = @fsockopen($ssl.$parsed_paypal_url['host'], $parsed_paypal_url['port'], $error_number, $error_string, 30);
  22.  
  23.  //Error checking
  24.  if (!$fp) {
  25.      file_put_contents(CACHE_ROOT.'/ipn.txt', $error_number.' - '.$error_string);
  26.  }else{
  27.      fputs($fp, "POST {$parsed_paypal_url['path']} HTTP/1.1\r\n");
  28.      fputs($fp, "Host: {$parsed_paypal_url['host']}\r\n");
  29.      fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
  30.      fputs($fp, "Content-length: ".strlen($post_data)."\r\n");
  31.      fputs($fp, "Connection: close\r\n\r\n");
  32.      fputs($fp, $post_data . "\r\n\r\n");
  33.     
  34.      // Response from the server
  35.      while(!feof($fp)) {
  36.          $info[] = @fgets($fp, 1024);
  37.      }
  38.  
  39.      fclose($fp);
  40.  
  41.      $info = implode(",", $info);
  42.  
  43.      // Get back the invoice info (Request::post like $_POST['KEYWORD'])
  44.      $item_name = Request::post('item_name');
  45.      $item_number = Request::post('item_number');
  46.      $payment_status = Request::post('payment_status');
  47.      $payment_amount = Request::post('mc_gross');
  48.      $payment_currency = Request::post('mc_currency');
  49.      $txn_id = Request::post('txn_id');
  50.      $receiver_email = Request::post('receiver_email');
  51.      $payer_email = Request::post('payer_email');
  52.      $invoice_number = Request::post('invoice');
  53.  
  54.      // Check the response info have or not verified keyword
  55.      if (preg_match("/VERIFIED/i", $info)) {
  56.          file_put_contents(CACHE_ROOT.'/ipn.txt', 'Verified'); // Save to log
  57.  
  58.          $table = new Table("payment", $invoice_number, "invoice_number");
  59.          $table->status = 'completed';
  60.          $table->update_at = time();
  61.          $table->renew();
  62.      }else{
  63.          file_put_contents(CACHE_ROOT.'/ipn.txt', 'Not Verified');
  64.  
  65.          Session::set("payment_status", false);
  66.      }
  67.  }
  68.  ?>

安裝 node.js

記錄一下於 FreeBSD 中安裝 node.js

記: check for dl 會出現 no 是正常的,因為是 freebsd

  1.  cd /usr/ports/devel/libexecinfo
  2.  make install clean
  3.  
  4.  cd /usr/ports/security/openssl
  5.  make install clean
  6.  
  7.  cd /downloads
  8.  http://nodejs.org/dist/v0.6.x/node-v0.6.x.tar.gz
  9.  tar zxvf node-v0.6.x.tar.gz
  10.  cd node-v0.6.x
  11.  ./configure --prefix=/usr/local/nodejs
  12.  gmake
  13.  gmake install
  14.  
  15.  var http = require('http');
  16.  http.createServer(function (req, res) {
  17.    res.writeHead(200, {'Content-Type': 'text/plain'});
  18.    res.end('Hello World\n');
  19.  }).listen(1337, "111.253.253.253");
  20.  console.log('Server running at http://111.253.253.253:1337/');

Android 模擬中使用 AndroidMarket

應該十一月底發的,只是一直沒時間整裡進來
來筆記一下,不過使用後體驗不太好,還是買支手機好

  1.  1. 之後下載 Emulator Files.rar [註1]
  2.  2. 切換到 android_sdk/tools 執行 ./android
  3.  2. 更新 Android SDK 到 2.2 的 API 8
  4.  3. 關閉後再執行 ./android
  5.  4. 解壓 Emulator Files.rar
  6.  5. 將裡面的檔案都放入到 android_sdk/platform-tools (同目錄內有 ./adb)
  7.  4. 打開 AVDs 界面經由 MenuBar > Tools > Manage AVDs
  8.  7. 新建 AVDs 經由 New
  9.  -- (Name: AndroidMarket, Target: Android 2.2 API 8, SD Card Size: 100)
  10.  8. 之後切換到執行模擬器 ./emulator @AndroidMarket -partition-size 96
  11.  9. 完全載入後切換到 ../platform-tools 執行下面的指令
  12.  
  13.  # 取出 build.prop, 用編輯器打開, 註解掉 ro.config.nocheckin=yes
  14.  ./adb pull /system/build.prop
  15.  
  16.  ./adb remount
  17.  ./adb push build.prop /system/build.prop
  18.  ./adb install ./GoogleServicesFramework.apk
  19.  ./adb install ./Vending.apk
  20.  ./adb install ./Gmail.apk
  21.  
  22.  ./adb shell mount -o rw,remount -t yaffs2 /dev/block/mtdblock03 /system
  23.  ./adb push ./su /system/xbin/su
  24.  ./adb shell chmod 06755 /system
  25.  ./adb shell chmod 06755 /system/xbin/su
  26.  ./adb install ./uperuser.apk
  27.  
  28.  # 或者簡單一點的 (可能需要修改 sh 內的 path)
  29.  chmod 755 *.sh
  30.  ./Linux.sh
  31.  
  32.  * 註
  33.  1. http://forum.xda-developers.com/showthread.php?t=821742

VMware Fusion 安裝 Mint 失敗

因為想要測試一些新的東西,
所以就在 VMware Fusion 上安裝 LinuxMint,
結果失敗,無論選擇什麼環境都失敗,
都得到下面的提問視窗,無論選擇誰都會 Crash 掉

  1.  mintMenu" has quit unexpectedly

最後問了大神,才知道原來是記憶體問題,解決方法

  1.  Settings -> Processor & RAM -> 將 512MB 調成 1G -> 重開

設定過後就可以順利安裝了

AppFog 的小試用

昨天收到 AppFog 的邀請信
所以作了簡單的試用,記錄一下步驟

由於線上的 Web Console 個人認為不太好用
所以直接用套件方式進行佈置好了

  1.  # 先安裝套件
  2.  gem install af
  3.  
  4.  # 登入帳戶
  5.  af login
  6.  
  7.  # 切換到專案中
  8.  cd ~/Desktop/appfog
  9.  
  10.  # 建立 Appname 和 網址
  11.  # appname : test
  12.  # site url: test.aws.af.cm
  13.  af push test --url test.aws.af.cm
  14.  
  15.  # 之後會再要求你設定這個 App 的資料
  16.  ---
  17.  Would you like to deploy from the current directory? [Yn]: Y
  18.  Detected a PHP Application, is this correct? [Yn]: Y
  19.  Memory Reservation [Default:128M] (64M, 128M, 256M, 512M, 1G or 2G)
  20.  Creating Application: OK
  21.  Would you like to bind any services to 'test'? [yN]: y
  22.  Would you like to use an existing provisioned service [yN]? y
  23.  The following provisioned services are available:
  24.  1. mysql-XXXXX
  25.  Please select one you wish to provision: 1
  26.  Binding Service: OK
  27.  Uploading Application:
  28.    Checking for available resources: OK
  29.    Packing application: OK
  30.    Uploading (0K): OK   
  31.  Push Status: OK
  32.  Staging Application: OK                                                         
  33.  Starting Application: OK
  34.  ---
  35.  
  36.  # 備注
  37.  * appfog 本身支持 git 和類似於 ftp 的佈署
  38.  * 這裡使用的是後者,因為不想特別去建立 Public Git
  39.  * 如果本身已有 Public Git 的可以通過 Web Console 來建立較方便
  40.  
  41.  * 以套件操作方式,想得知 MySQL 的 addon 連線資料
  42.  * 可以通過以下方式查看
  43.  
  44.      echo "< ?php print_r( getenv('VCAP_SERVICES') ); ?>" > test.php
  45.  
  46.  * 之後再通過以下指令提交更新到 appfog
  47.  
  48.      af update test
  49.  
  50.  * 提交後就可以在網址中查看 test.php 得知訊息

no.de 中使用 MongoDB

先參照之前的那一篇 no.de 佈署文
再依據下面方法來安裝 MongoDB 後就可以使用了
其實下面的資料在官方裡的 wiki 有記載

  1.  # 以 admin 身份登入
  2.  ssh admin@[YOUR].no.de
  3.  
  4.  # 切為 root 身份
  5.  sudo bash
  6.  
  7.  # 以自動安裝方式進行
  8.  # 先取得安裝 Script
  9.  curl -O http://wiki.joyent.com/download/attachments/1639170/mongodbnode.sh
  10.  
  11.  # 再通過安裝 Script 指定要安裝的版本,進行安裝
  12.  bash mongodbnode.sh 2.0.0
  13.  
  14.  # 在目前的 Session 中重新加載 bash shell runtime
  15.  source ~/.bashrc
  16.  
  17.  # 再執行以下指令,使其可以在機器重開時可自動開啟 MongoDB
  18.  pfexec svcadm enable mongodb
  19.  
  20.  # 之後就可以直接在程式裡面用 localhost 連接了

connect-form 上傳文件失敗

  1.  如果失敗的情況是: 無限迴圈 Loop/一直保持空白
  2.  這問題記錄於推特上,但由於推特字數有限,所以補充和記錄一下:
  3.  
  4.  1. 東西是由 ExpressJS Framework 來開發
  5.  2. 又用了 connect-form 上傳檔案
  6.  
  7.  最終在追查和測試過程中發現,發生這情況的原因是:
  8.  - 用了自家制的 middleware(中間件) 而導致的
  9.  
  10.  既然知道問題就來問問大神
  11.  原來在六月份已有人提出過,於根據找到的討論文中提示,得到真正原因是:
  12.  - 自制的 middleware(中間件) 內的 Async Function 發生了 Losting data event.
  13.  - 使到 req.form.complete(function) 一直沒收到觸發事件,導致了交接不能
  14.  - 而我的 Async Function 主要是用於檢查用戶已登入,並在登入後取得他的個人資料
  15.    - 簡單來說是於 MongoDB 中取得用戶資料,而這個讀取用作是 ASync 來的
  16.  
  17.  所以解決方法就是使自制的 middleware(中間件) 在得到結果後才繼續執行後續動作就解題了
  18.  
  19.  再於討論串中向下翻有人提到可以參考 connect 套件中裡 utils.js 的 pause() 函數
  20.  於是再去追查套件,那就得到了真相,一如 pause() 的例子:
  21.  
  22.      var pause = utils.pause(req);
  23.      fs.readFile(path, function(){
  24.          next();
  25.          pause.resume();
  26.      });
  27.  
  28.  最後最後將上面的 pause 放到自制的 middleware(中間件) 就大功告成了
  29.  當然有空也應該來看看 connect 這東西的代碼

# 參考
- http://groups.google.com/group/express-js/browse_thread/thread/889305468e89f2b6/3de16078d14cfb00
- https://github.com/senchalabs/connect/blob/master/lib/utils.js#L273

佈署到 PHPCloud.Com

前天才提交申請,誰不知道昨天起床就有邀請函
所以就來測試下,以 PHPInfo 看來是最新的 PHP 5.3.8
這個東西好像是 PHP Zend 官方和某商家合作的 Cloud 服務
記錄一下

  1.  1. 先登入 phpcloud.com 網站
  2.  2. 左手的 My Containers
  3.  3. 右邊的 CREATE CONTAINER 按鍵
  4.  4. 填入子域名和密碼 (GIT 提交時的 Username / Password)
  5.  5. 左邊的 My Account
  6.  6. 可以上傳自己的 SSH Public Key 又或者下載 PEM 證書
  7.  7. 完成後就可以 GIT 了
  8.  8. 在左邊的 My Containers 下面點擊剛建立的 子域名
  9.  9. 右邊的 git access 按鈕取得 git 庫位置
  10.  
  11.  * 如果是空專案,可以直接將 phpcloud 拉回來再寫
  12.  
  13.  cd ~/ && mkidr phpcloud && cd phpcloud
  14.  git init
  15.  git remote add phpcloud https://[YOUR-SUB-DOMAIN].my.phpcloud.com/git/container-root.git
  16.  git pull
  17.  
  18.  * 如果不是空專案,可以將 phpcloud 拉下來,再將檔案放到 public 目錄 (視乎需要)
  19.  * 因為 public 才可見,其他目錄不可見
  20.  
  21.  cd ~/project
  22.  git init
  23.  git remote add phpcloud https://[YOUR-SUB-DOMAIN].my.phpcloud.com/git/container-root.git
  24.  git pull
  25.  git add .
  26.  git commit -m 'merge phpcloud file'
  27.  
  28.  = 如此一來就已經合併了,之後看需要移動檔案/目錄,也可以移動後再合位吧

將 node.js 的程式佈署到 no.de 上

帳號一早有了 只是都無法開新機器
前幾天再試發現已經可以正常開新機器了
所也來記錄一下

  1.  1. 登入 no.de 上
  2.  2. 點擊 Order A MACHINE 輸入子域名
  3.  3. 點擊右上方的 ACCOUNT SETTINGS -> SSH KEYS 新增
  4.  4. 點擊 MACHINES 選擇剛才新增的域名
  5.  5. vim ~.ssh/config 加入下面內容並保存退出
  6.  
  7.      Host [YOUR].no.de
  8.          Port [PORT]
  9.          User node
  10.          ForwardAgent yes
  11.         
  12.  6. 切到自的 Project 如 ~/project
  13.  7. git remote add [YOUR].no.de [YOUR].no.de:repo
  14.  8. git push [YOUR].no.de master
  15.  9. 打開網址看看
  16.  
  17.  = 如果出現錯誤可以查看 log
  18.  ssh xx.no.de
  19.  node-service-log
  20.  
  21.  = 如果出現 Internal Server Error
  22.  1. 將 app.js 命名為 server.js
  23.  2. 將 server port 設為 80
Page 1 of 4112345»102030...Last »
Fork me on GitHub