Wednesday, September 11, 2013

Install Asterisk and FreePBX easily by 1 script

To make a VoIP call center, Asterisk & FreePBX is a top choice. This article will show you how to install them just by 1 script. Below are steps & the script file (shell).
  • Step 1: Install Linux CentOS
  • Step 2: Create a script file, named as install_Asterisk_CentOS.sh enter content as following text.
  • Step 3: Run chmod +x install_Asterisk_CentOS.sh  - set executable to script file
  • Step 4: Run  install_Asterisk_CentOS.sh

install_Asterisk_CentOS.sh  file
----------------------------------------------------------
 #!/bin/bash

function pear_db_install(){
    echo -e "\e[32mStarting Install php-pear-DB\e[m"
    cd /usr/src
    if [ ! -e ./DB-1.7.13.tgz ]; then
        wget http://download.pear.php.net/package/DB-1.7.13.tgz
    fi
    pear install DB-1.7.13.tgz
}

function AMP_install() {
    echo -e "\e[32mStarting Install Apache+PHP+MySQL\e[m"
    yum -y install php-gd kernel-headers kernel-devel kernel-PAE kernel-PAE-devel httpd php php-mysql php-pear-DB doxygen mysql-server libtermcap-devel php-gd gcc gcc-c++ libxml2-devel php-pear php-posix sox make
    #Install LAMP (Apache, PHP and MySQL in Linux) using yum.
    echo -e "\e[32mAMP Install OK!\e[m"
    sed -i "s/post_max_size = 8M/post_max_size = 20M/" /etc/php.ini
    sed -i "s/upload_max_filesize = 2M/upload_max_filesize = 20M/" /etc/php.ini
    sed -i "s/short_open_tag = Off/short_open_tag = On/" /etc/php.ini
    sed -i "s/memory_limit = 16M /memory_limit = 128M /" /etc/php.ini
    service mysqld start
}


function asterisk_install() {
    echo -e "\e[32mStarting Install Asterisk\e[m"
    useradd -c "Asterisk PBX" -d /var/lib/asterisk asterisk
    #Define a user called asterisk.
    mkdir /var/run/asterisk /var/log/asterisk /var/spool/asterisk -p
    #Change the owner of this file to asterisk.
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
    setenforce 0
    #shutdown selinux
    sed -i 's/^User apache/User asterisk/;s/^Group apache/Group asterisk/' /etc/httpd/conf/httpd.conf
    sed -i "s/AllowOverride None/AllowOverride All/" /etc/httpd/conf/httpd.conf
    service httpd restart
    #Change User apache and Group apache to User asterisk and Group asterisk.
    #Change the default AllowOverride All to AllowOverride None to prevent .htaccess permission problems.
    cd /usr/src
    if [ ! -e ./asterisk-$asteriskver.tar.gz ]; then
        wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-$asteriskver.tar.gz
        if [ ! -e ./asterisk-$asteriskver.tar.gz ]; then
            wget http://downloads.asterisk.org/pub/telephony/asterisk/old-releases/asterisk-$asteriskver.tar.gz
        fi
    fi
    tar xf asterisk-$asteriskver.tar.gz
    if [ $? != 0 ]; then
        echo "fatal: dont have valid asterisk tar package"
        exit 1
    fi

    cd asterisk-$asteriskver
    ./configure '-disable-xmldoc'
    make
    make install
    make samples
    #This command will  install the default configuration files.
    #make progdocs
    #This command will create documentation using the doxygen software from comments placed within the source code by the developers.
    make config
    #This command will install the startup scripts and configure the system (through the use of the chkconfig command) to execute Asterisk automatically at startup.
    echo -e "\e[32mAsterisk Install OK!\e[m"
}

function libpri_install() {
    echo -e "\e[32mStarting Install LibPRI\e[m"
    cd /usr/src
    if [ ! -e ./libpri-$libpriver.tar.gz ]; then
        wget http://downloads.asterisk.org/pub/telephony/libpri/releases/libpri-$libpriver.tar.gz
    fi
    tar xf libpri-$libpriver.tar.gz
    if [ $? != 0 ]; then
        echo -e "fatal: dont have valid libpri tar package\n"
        exit 1
    fi

    cd libpri-$libpriver
    make
    make install
    echo -e "\e[32mLibPRI Install OK!\e[m"
}

function dahdi_install() {
    echo -e "\e[32mStarting Install DAHDI\e[m"
    cd /usr/src
    if [ ! -e ./dahdi-linux-complete-$dahdiver.tar.gz ]; then
        wget http://downloads.asterisk.org/pub/telephony/dahdi-linux-complete/dahdi-linux-complete-$dahdiver.tar.gz
        if [ ! -e ./dahdi-linux-complete-$dahdiver.tar.gz ]; then
            wget http://downloads.asterisk.org/pub/telephony/dahdi-linux-complete/releases/dahdi-linux-complete-$dahdiver.tar.gz
        fi
    fi
    tar xf dahdi-linux-complete-$dahdiver.tar.gz
    if [ $? != 0 ]; then
        echo -e "fatal: dont have valid dahdi tar package\n"
        exit 1
    fi

    cd dahdi-linux-complete-$dahdiver
    make
    if [ $? != 0 ]; then
        yum -y upgrade
        echo -e "\e[32mplease reboot your server and run this script again\e[m\n"
        exit 1
    fi

    make install
    make config
    /usr/sbin/dahdi_genconf
    service dahdi start
    echo -e "\e[32mDAHDI Install OK!\e[m"
}


function freepbx_install() {
    echo -e "\e[32mStarting Install FreePBX\e[m"
    cd /usr/src
    if [ ! -e ./freepbx-$freepbxver.tar.gz ]; then
        wget http://mirror.freepbx.org/freepbx-$freepbxver.tar.gz
    fi
    tar xf freepbx-$freepbxver.tar.gz
    if [ $? != 0 ]; then
        echo -e "fatal: dont have valid freepbx tar package\n"
        exit 1
    fi

    cd freepbx-$freepbxver
    . /tmp/.mysql_root_pw.$$

    #Set mysql initial password.
    mysqladmin create asterisk -uroot -p$mysql_root_pw
    if [ $? != 0 ]; then
        echo -e "fatal: failed to create asterisk database\n"
        exit 1
    fi

    mysqladmin create asteriskcdrdb -uroot -p$mysql_root_pw
    mysql asterisk < SQL/newinstall.sql -uroot -p$mysql_root_pw
    mysql asteriskcdrdb < SQL/cdr_mysql_table.sql -uroot -p$mysql_root_pw
mysql -uroot -p$mysql_root_pw <<EOF
GRANT ALL PRIVILEGES ON asteriskcdrdb.* TO asterisk@localhost IDENTIFIED BY 'amp109';
GRANT ALL PRIVILEGES ON asterisk.* TO asterisk@localhost IDENTIFIED BY 'amp109';
flush privileges;
EOF
    #Create databases and import tables.
    ./start_asterisk start
    echo -e "\e[32mYour IP Is `ifconfig|grep -oP \(\\\\d+?\\\\.\){3}\\\\d+|head -n1`,Password is $mysql_root_pw\nRemember This And press 'Enter' to start installation of FreePBX!\e[m"
    read pause
    ./install_amp --username=asterisk --password=amp109 --webroot=/var/www/html
    #chmod -R 777 /var/www/html;
    #Set permissions or HTTP error 403 forbidden.
    echo `wc -l /etc/asterisk/manager.conf` | awk '{system("head -n "$1-2" "$2">manager.tmp&&mv manager.tmp /etc/asterisk/manager.conf")}'
    #Delete last two lines of manager.conf or freepbx can't connect to the asterisk manager.
    sed -i 's/read = system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate/read = system,call,agent/' /etc/asterisk/manager.conf
    touch /etc/asterisk/sip_general_additional.conf
    touch sip_general_custom.conf
    touch /etc/asterisk/sip_general_custom.conf
    touch /etc/asterisk/sip_nat.conf
    touch /etc/asterisk/sip_registrations_custom.conf
    touch /etc/asterisk/sip_custom.conf
    touch /etc/asterisk/sip_custom_post.conf
    service asterisk restart
    sleep 1
    asterisk -rx 'module load chan_sip.so'
    asterisk -rx 'manager reload'
    #Load sip module then you can use sip command.
    asterisk -rx 'core set verbose 10'
    #Set level of verboseness.
    echo -e "\e[32mFreePBX Install OK!\e[m"
}

function lame_install(){
    echo -e "\e[32mStarting Install Lame for mp3 monitor\e[m"
    cd /usr/src
    if [ ! -e ./lame-$lamever.tar.gz ]; then
        wget http://sourceforge.net/projects/lame/files/lame/$lamever/lame-$lamever.tar.gz/download
    fi
    tar xf lame-$lamever.tar.gz
    if [ $? != 0 ]; then
        echo -e "\e[32mdont have valid lame tar package, you may lose the feature to check recordings on line\e[m\n"
        return 1
    fi

    cd lame-$lamever
    ./configure && make && make install
    if [ $? != 0 ]; then
        echo -e "\e[32mfailed to install lame, you may lose the feature to check recordings on line\e[m\n"
        return 1
    fi
    ln -s /usr/local/bin/lame /usr/bin/
    return 0;
}

function get_mysql_passwd(){
    service mysqld start
    while true;do
        echo -e "\e[32mplease enter your mysql root passwd\e[m";
        read mysql_passwd;
        # make sure it's not a empty passwd
        if [ "X${mysql_passwd}" != "X" ]; then
            mysqladmin -uroot -p$mysql_passwd password $mysql_passwd    # try empty passwd
            if [ $? == 0  ]; then
                break;
            fi

            mysqladmin password "$mysql_passwd"
            if [ $? == 0  ]; then
                break;
            fi

            echo -e "\e[32minvalid password,please try again\e[m"
        fi
    done
    echo mysql_root_pw=$mysql_passwd > /tmp/.mysql_root_pw.$$
}


function run() {

    asteriskver=1.6.2.20
    libpriver=1.4.12
    freepbxver=2.9.0
    dahdiver=2.6.2+2.6.2
    lamever=3.99

    AMP_install
    libpri_install
    dahdi_install
    asterisk_install
    pear_db_install
    get_mysql_passwd
    freepbx_install
    lame_install

    #Run all system after install.
    chkconfig dahdi on && chkconfig httpd on && chkconfig mysqld on && chkconfig asterisk on
    #Run all automatically at linux startup.
    service iptables stop && chkconfig iptables off
    #Stop the internal firewall now and forever.
    service asterisk restart
    chown asterisk.asterisk /var/lib/php -R
    /bin/rm -rf /tmp/.mysql_root_pw.$$
    # update index.html
cat > /var/www/html/index.html << EOF
<HTML>
<HEAD>
<head>
    <title>FreePBX</title>
    <meta http-equiv="Content-Type" content="text/html">
    <link href="mainstyle.css" rel="stylesheet" type="text/css">
</head>

<body>
<div id="page">

<div class="header">

    <a href="index.php"><img src="admin/images/freepbx.png"/></a>

</div>

<div class="message">
    Welcome
</div>

<div class="content">

<h4><a href="recordings/">Voicemail & Recordings (ARI)</a></h4>
<h4><a href="panel/">Flash Operator Panel (FOP)</a></h4>
<h4><a href="admin/">FreePBX Administration</a></h4>

<br><br><br><br><br><br>
</div>

    </div>

</body>
</html>
EOF

}

run

Wednesday, August 21, 2013

PHP: step by step example to do a download manager web page

Some info should be concerned before you begin to do a download manager program:
1. Bandwidth of your host
2. Download speed
3. Capacity of server
4. File is stored in folder or Database? If it is store in DB, should concern about capacity of DB
5. Download statistic

The first program
//file download.php
<?php
 $filename = "document.zip";
 $fp = fopen($filename, "rb");

 header("Content-type: application/octet-stream");
 header("Content-length: " . filesize($filename));

 fpassthru($fp);
 fclose($fp);
 ?>

It's too simple, sure! The first program will read document.zip file in server then save it on client (PC of user) as download.php file as default. You must rename it with correct extension name to open it in client PC.

The second program
Next step, we'll make the program download a file on a specific folder on server then save it in the same name in client, e.g. we want to download and save file with name document.zip on server, we will call download.php?file=document.zip
//file download.php
<?php
 //default folder on server that contains files which are allowed to download
 $upload_dir = "../upload/";

 //get download file name
 $filename = isset($_GET['file'])?$_GET['file']:'';

 //check the file
 if ( !preg_match('/^[a-z0-9\_\-][a-z0-9\_\-\. ]*$/i', $filename) )
         || !is_file($upload_dir.$filename) || !is_readable($upload_dir.$filename) ) {
     echo "Error: invalid file name or not existing!";
     exit(-1);
 } //end if

 //open in binary mode and read the file
 $fp = fopen($upload_dir.$filename, "rb");

 //send header to the browser in client side
 header('Content-type: application/octet-stream');
 header('Content-disposition: attachment; filename="'.$filename.'"');
 header('Content-length: ' . filesize($upload_dir.$filename));

 //get and save file
 fpassthru($fp);
 fclose($fp);
?>

It is still too easy, sure!

Where I can use this download manager
-Use it in the case of you don't want publish the direct location to download the file
-Use it in next step of a login progress, in which you allow user download a file after paying credit
-Use it if you want to track activity of user. This requires small modification to save time stamp + user + client ip + download file name to a log file or DB.

When we shouldn't use it
-Server is overloading
-File is too big
-File is not important

Hope it can help you start a bigger program -:)

Tuesday, August 6, 2013

Solving error "MySQL server has gone away"

If you have time, you can read the article "MySQL server has gone away". OR just do 3 steps as the following:
1. Find my.ini file if you are using Windows (it can locate in C:\ProgramData\MySQL\MySQL Server 5.5\). OR my.cnf file if you are using Linux (it can locate in /etc/mysql/my.cnf)

2. Increase max_allowed_packet value below [mysqld] section (by default max_allowed_packet =1M or not defined), for example you change it to 32M:
[mysqld]
max_allowed_packet=32M

3. Restart mysql service

Friday, August 2, 2013

Joomla 2.5 convert utf8 string to lower case

The following function can help you convert an UTF8 string to lower case:
utf8_strtolower(string_value);

Happy coding!

Sunday, July 7, 2013

Joomla 2.5: overwrite com_search layout

As overwriting layout of any component, e.g. overwrite com_content blog layout, to customize layout of com_search, you may copy its original 2 files below:
/components/com_search/views/search/tmpl/default_form.php: display search box and search filters / conditions
/components/com_search/views/search/tmpl/default_results.php: display search results
to the html folder of your template:
/templates/your_template/html/com_search/search/default_form.php
/templates/your_template/html/com_search/search/default_results.php

Let do a sample, in which we only search in Articles and display every search result with an intro image of the article if any.

Below is an example for an article with intro image:


The following image is before this customization:

Here are the image after this customization:

To do this sample, we modify the file /modules/mod_search/tmpl/default.php of mod_search to search Articles only. Add the following line to this file:
<input type="hidden" name="areas[]" value="content" />
before line:
<input type="hidden" name="task" value="search" />

And download this file and unpack, then copy folder html/com_search to your template and plugins/search/content/content.php to joomla content search plugin.

Base on this post, you can create your search layout as you want.



Subscribe to RSS Feed Follow me on Twitter!