2016年4月26日 星期二

FreePBX 12 + Asterisk 13 安裝備忘

sudo -i
apt-get update && apt-get upgrade -y

apt-get install -y build-essential linux-headers-`uname -r` openssh-server apache2 mysql-server mysql-client bison flex php5 php5-curl php5-cli php5-mysql php-pear php5-gd curl sox libncurses5-dev libssl-dev libmysqlclient-dev mpg123 libxml2-dev libnewt-dev sqlite3 libsqlite3-dev pkg-config automake libtool autoconf git unixodbc-dev uuid uuid-dev libasound2-dev libogg-dev libvorbis-dev libcurl4-openssl-dev libical-dev libneon27-dev libsrtp0-dev libspandsp-dev libmyodbc

pear install Console_Getopt

A.         Install iksemel



cd /usr/src
wget https://iksemel.googlecode.com/files/iksemel-1.4.tar.gz
tar xf iksemel-1.4.tar.gz
rm iksemel-1.4.tar.gz
cd iksemel-*
./configure
或使用
./configure --with-libgnutls-prefix=/usr
make
make install



B.          Download Asterisk source files.



cd /usr/src
wget http://downloads.asterisk.org/pub/telephony/libpri/libpri-1.4-current.tar.gz
wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-13-current.tar.gz
wget -O jansson.tar.gz https://github.com/akheron/jansson/archive/v2.7.tar.gz
wget http://www.pjsip.org/release/2.4/pjproject-2.4.tar.bz2



Compile and install pjproject


cd /usr/src
tar -xjvf pjproject-2.4.tar.bz2
rm -f pjproject-2.4.tar.bz2
cd pjproject-2.4
CFLAGS='-DPJ_HAS_IPV6=1'./configure --enable-shared --disable-sound --disable-resample --disable-video --disable-opencore-amr
make dep
make
make install



Compile and install libsrtp


cd /usr/src/
wget http://srtp.sourceforge.net/srtp-1.4.2.tgz
tar zxvf srtp-1.4.2.tgz
cd
srtp
autoconf
./configure
make
make
install
cp
/usr/local/lib/libsrtp.a /lib



C.         Compile and Install jansson



cd /usr/src
tar vxfz jansson.tar.gz
rm -f jansson.tar.gz
cd jansson-*
autoreconf -i
./configure
make
make install



D.        Compile and install Asterisk



cd /usr/src
tar xvfz asterisk-13-current.tar.gz
rm -f asterisk-13-current.tar.gz
cd asterisk-*
contrib/scripts/install_prereq install
./configure -with-crypto -with-ssl -with-srtp=/usr/local/lib
contrib/scripts/get_mp3_source.sh
make menuselect
make
make install
make config
ldconfig
update-rc.d -f asterisk remove
chkconfig asterisk on
apt-get install sysv-rc-conf
sysv-rc-conf asterisk on



# mkdir /etc/asterisk/keys
# cd /usr/src/asterisk-13.7.2/contrib/scripts
# ./ast_tls_cert -C sipim.tibtrade.net -O "tibtrade" -d /etc/asterisk/keys

Next, we generate a client certificate for our SIP device.
# ./ast_tls_cert -m client -c /etc/asterisk/keys/ca.crt -k /etc/asterisk/keys/ca.key -C sipim.tibtrade.net -O "tibtrade" -d /etc/asterisk/keys -o malcolm

Install Asterisk Soundfiles.
The 'make install' above installs a standard low-quality base sound file by default. This is suitable if you are on a small, underpowered system (such as a Rasberry Pi), but on a larger system you should install higher quality soundfiles.  Note that this installs the (8khz) 'wav' soundfiles and G722 (High Definition 'Wideband') audio.


cd /var/lib/asterisk/sounds
wget http://downloads.asterisk.org/pub/telephony/sounds/asterisk-core-sounds-en-wav-current.tar.gz
wget http://downloads.asterisk.org/pub/telephony/sounds/asterisk-extra-sounds-en-wav-current.tar.gz
tar xvf asterisk-core-sounds-en-wav-current.tar.gz
rm -f asterisk-core-sounds-en-wav-current.tar.gz
tar xfz asterisk-extra-sounds-en-wav-current.tar.gz
rm -f asterisk-extra-sounds-en-wav-current.tar.gz
# Wideband Audio download
wget http://downloads.asterisk.org/pub/telephony/sounds/asterisk-core-sounds-en-g722-current.tar.gz
wget http://downloads.asterisk.org/pub/telephony/sounds/asterisk-extra-sounds-en-g722-current.tar.gz
tar xfz asterisk-extra-sounds-en-g722-current.tar.gz
rm -f asterisk-extra-sounds-en-g722-current.tar.gz
tar xfz asterisk-core-sounds-en-g722-current.tar.gz
rm -f asterisk-core-sounds-en-g722-current.tar.gz



Install and Configure FreePBX

A.       Create the Asterisk user and set base file permissions.


useradd -m asterisk
chown asterisk. /var/run/asterisk
chown -R asterisk. /etc/asterisk
chown -R asterisk. /var/{lib,log,spool}/asterisk
chown -R asterisk. /usr/lib/asterisk
rm -rf /var/www/html



B.         A few small modifications to Apache.



sed -i 's/\(^upload_max_filesize = \).*/\120M/' /etc/php5/apache2/php.ini
cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf_orig
sed -i 's/^\(User\|Group\).*/\1 asterisk/' /etc/apache2/apache2.conf
sed -i 's/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf
service apache2 restart



C.         Configure ODBC

Edit /etc/odbcinst.ini and add the following. Note that this command assumes you are installing to a new machine, and that the file is empty. If this is not a freshly installed machine, please manually verify the contents of the file, rather than just copying and pasting the lines below. The 'EOF' does no go in the file, it simply signals to the 'cat' command that you have finished pasting.


cat >> /etc/odbcinst.ini << EOF
[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib/x86_64-linux-gnu/odbc/libmyodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libodbcmyS.so
FileUsage = 1
EOF


You may need to verify these paths, if you're not on a x86_64 machine. You can use the command `find / -name libmyodbc.so` to verify the location
Edit or create /etc/odbc.ini and add the following section. Note that, again, this command assumes you are installing to a new machine, and the file is empty. Please manually verify the contents of the files if this is not the case.


cat >> /etc/odbc.ini << EOF
[MySQL-asteriskcdrdb]
Description=MySQL connection to 'asteriskcdrdb' database
driver=MySQL
server=localhost
database=asteriskcdrdb
Port=3306
Socket=/var/run/mysqld/mysqld.sock
option=3 
EOF


D.        Download and install FreePBX.



cd /usr/src
wget http://mirror.freepbx.org/modules/packages/freepbx/freepbx-13.0-latest.tgz
tar vxfz freepbx-13.0-latest.tgz
rm -f freepbx-13.0-latest.tgz
cd freepbx
chmod +x /usr/sbin/fwconsole
./start_asterisk start
./install -n

2016年4月14日 星期四

Redmine 啟動背景執行 Shell Script

最近VirtualPC再Windows重啟後透過排程設定背景執行,所以往常Redmine手動啟動變得沒有途徑可以輸入,寫入vc.local啟動命令列方式看來也殘念了,找到以下這腳本目前運作OK~~

#!/bin/bash # Modify it to your configuration DIR=/var/www/html/redmine/ # Start Redmine in daemon mode. start(){ cd $DIR ruby script/rails server webrick -e production -d > log/redmine.log } # Stop Redmine daemon stop(){ RUBYPID=`ps aux | grep "rails" | grep -v grep | awk '{print $2}'` if [ "x$RUBYPID" != "x" ]; then kill -2 $RUBYPID fi } # Check if Redmine is running status(){ RUBYPID=`ps aux | grep "rails" | grep -v grep | awk '{print $2}'` if [ "x$RUBYPID" = "x" ]; then echo "* Redmine is not running" else echo "* Redmine is running" fi
}

case "$1" in
  start)
    start
    status
    ;;
  stop)
    stop
    sleep 2
    status
    ;;
  status)
    status
    ;;
  restart|force-reload)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|force-reload|status}"
    exit 1
esac

2016年4月12日 星期二

RSYNC 同步資料備忘...

雙邊伺服器將SSL認證互相設定後,就不用進行輸入密碼驗證之程序。

指令 rsync -avz --rsh='ssh -p 1022' /來源目錄 Account@Host:/目的路徑
將"來源目錄"複製到"目的路徑"下

指令 rsync -avz --rsh='ssh -p 1022' /來源目錄/ Account@Host:/目的路徑
將"來源目錄/*.*"複製到"目的路徑"下

選項與參數:
-v :觀察模式,可以列出更多的資訊,包括鏡像時的檔案檔名等;
-q :與 -v  相反,安靜模式,略過正常資訊,僅顯示錯誤訊息;
-r :遞迴複製!可以針對『目錄』來處理!很重要!
-u :僅更新 (update),若目標檔案較新,則保留新檔案不會覆蓋;
-l :複製連結檔的屬性,而非連結的目標原始檔案內容;
-p :複製時,連同屬性 (permission) 也保存不變!
-g :保存原始檔案的擁有群組;
-o :保存原始檔案的擁有人;
-D :保存原始檔案的裝置屬性 (device)
-t :保存原始檔案的時間參數;
-I :忽略更新時間 (mtime) 的屬性,檔案比對上會比較快速;
-z :在資料傳輸時,加上壓縮的參數!
-e :使用的通道協定,例如使用 ssh 通道,則 -e ssh
-a :相當於 -rlptgoD ,所以這個 -a 是最常用的參數了!

2016年4月8日 星期五

備忘~JavaScript export Table to CSV File

中文處理上透過encodeURIComponent()編碼成UTF 8,直接用EXCEL開啟時若中文是亂碼那先檢查(可用NotePad++或UltraEdit之類編輯器檢視)檔案格式是否為 UTF-8 ( 有 BOM )

用PHPExcel()來匯出效果更好寫,不過在部分情況下不允許就將就著用。

1. JavaScript區塊
<script type="text/javascript">
function exportTableToCSV($table, filename) {
var $headers = $table.find('tr:has(th)')
,$rows = $table.find('tr:has(td)')

// Temporary delimiter characters unlikely to be typed by keyboard
// This is to avoid accidentally splitting the actual contents
,tmpColDelim = String.fromCharCode(11) // vertical tab character
,tmpRowDelim = String.fromCharCode(0) // null character

// actual delimiter characters for CSV format
,colDelim = '","'
,rowDelim = '"\r\n"';

// Grab text from table into CSV formatted string
var csv = '"';
csv += formatRows($headers.map(grabRow));
csv += rowDelim;
csv += formatRows($rows.map(grabRow)) + '"';

// 若欲輸出的檔案為 UTF-8 ( 無 BOM )
var csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);

// 若欲輸出的檔案為 UTF-8 ( 有 BOM )
var csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent('\uFEFF' + csv);

$(this)
.attr({
'download': filename
,'href': csvData
//,'target' : '_blank' //if you want it to open in a new window
});

//------------------------------------------------------------
// Helper Functions
//------------------------------------------------------------
// Format the output so it has the appropriate delimiters
function formatRows(rows){
return rows.get().join(tmpRowDelim)
.split(tmpRowDelim).join(rowDelim)
.split(tmpColDelim).join(colDelim);
}
// Grab and format a row from the table
function grabRow(i,row){

var $row = $(row);
//for some reason $cols = $row.find('td') || $row.find('th') won't work...
var $cols = $row.find('td');
if(!$cols.length) $cols = $row.find('th');

return $cols.map(grabCol)
.get().join(tmpColDelim);
}
// Grab and format a column from the table
function grabCol(j,col){
var $col = $(col),
$text = $col.text();

return $text.replace('"', '""'); // escape double quotes

}
}
 
//$('#export').click(function() {
$(".export").on('click', function (event) {
var outputFile = window.prompt("請輸入匯出之檔名") || 'export';
outputFile = outputFile.replace('.csv','') + '.csv'

// CSV
exportTableToCSV.apply(this, [$('#dvData>table'), outputFile]);
});
</script>

2. HTML區塊
<a href='#' class='export'>匯出</a>

<div style="display:none" id="dvData">
  <table>
      <tr><th>標題A</th><th>標題B</th></tr>
      <tr><td>資料A1</td><td>資料B1</td></tr>
      <tr><td>資料A2</td><td>資料B2</td></tr>
  </table>
</div>

2016年4月6日 星期三

FaceBook oAuth for WebSite

Main Url : https://developers.facebook.com/apps 

1. 先進行註冊後即可於個人FB左下角看到開發人員項目,點選"管理應用程式"

2. 建立新的FaceBook APP取得App ID


3. APP Dashboard 

   a.來取得 App Secret,程式呼叫時需要帶入
   b. 在Settings頁設定該APP相關資訊 
   c. 在App Review設定Public為Yes <- 請先檢查相關設定均已完成。

<?php
  $tokenurl='https://graph.facebook.com/oauth/access_token';
  $client_id='170145......';
  $redirect_uri='http://www.xxx.net/zz.php';
  $client_secret="6f5cbdea2bc......";

  //取得Login的Access_token
  $tokenurl=$tokenurl."?client_id=".$client_id."&redirect_uri=".$redirect_uri."&client_secret=".$client_secret."&code=".$_GET["code"]; 
  $xml = file_get_contents($tokenurl);

 $NewString = preg_split("/[&,]+/", $xml);
 $appsecret_proof = hash_hmac('sha256', $NewString[0], $client_secret);

  //取得Login_id 
  $GetUserUrl='https://graph.facebook.com/me?access_token='.$access_token.'&appsecret_proof='.$appsecret_proof;  

  $res = file_get_contents($GetUserUrl);  
  $arr = json_decode($res);

  echo $arr->id. $arr->name; 

?>