Upload File Using cPanel API

cPanel is a web hosting control panel which is using for various aspects of website and server administration with a nice graphical interface. cPanel also has well-documented APIs to manage all kinds cPanel activities through API. Here I have shared a code snipped to uploading files using cPanel API. (Fileman::upload_files)

Parsing XML With Multiple Namespaces Using PHP

Yesterday, I had a very ugly XML data which had to parse. It has multiple namespaces and namespaces are irrelevant with DOM – I just wasn’t getting the nodeValue from the Element. It was parsing partially not all nodeValues due to namespaces are irrelevant with DOM. Then I applied little tricks onto it through XML children options. And I thought I need to share this code so other developers can save his time to get out this annoying issue.

Remove Default Search Box and Add Custom Box in DataTable

DataTable displaying a default search box while initializing it. So by default DataTable has a search box. But sometimes you might have to remove that default search box and add a custom search box. And place that somewhere in the page. This article will help you to do that.

$('#example').DataTable();

See the above snippet, If your DataTable initializing it like that, then default search box will display. You might think, I can just use “bFilter: false” option to remove that search box. Yes you can! but the problem is if you do that, then DataTable will disable/turn off the search functionality. But you have to only remove the search box not search functionality.

In this case, you can use "dom: 't'" option. This will keep the default functionality of DataTable and just remove the search box, pagination (just hide it). It will just show the table elements.

Here I have shared a code snippet, you can get the idea to accomplish it.

$(document).ready(function() {
  var users = $('#users').DataTable({
    "dom": "t"
  });

  $('#customSearchBox').keyup(function() {
    users.search($(this).val()).draw();
  })
});

See the full jsFiddle

Install Elasticsearch in Ubuntu 16.04

Elasticsearch is a distributed, real-time, full-text search engine. Elasticsearch developer by JAVA (based on Lucene). It has an HTTP web interface and schema-free JSON documents. In this article, I will describe how to install and then configure elasticsearch in ubuntu 16.04. Let’s see

Before you install elasticsearch, please ensure java is installed on your machine. If not then install java first before doing anything.

Install Elasticsearch in Ubuntu 16.04
Install Elasticsearch in Ubuntu 16.04

Installing Java 8

 First Add the Java PPA to apt package database.

sudo add-apt-repository -y ppa:webupd8team/java

Then run update commands to update your apt package database.

sudo apt-get update
Then install Java 8 and verify it is installed, run following command.

sudo apt-get -y install oracle-java8-installer
java -version
After successfully install Java, now you can install  Elasticsearch on your machine.

Installing and Configuring Elasticsearch

You can download elasticsearch from their official website elastic.co. It would be always good to download deb (Debian) version for the ubuntu, cause it has everything integrated to successfully run elasticsearch. You can also download it through the command line:

sudo wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.2.deb
Then install this as usual ubuntu way with the dpkg command:

sudo dpkg -i elasticsearch-1.7.2.deb

After execution this command your installation will be completed, now you have to configure elasticsearch. By default all configuration files of elasticsearch stored into the /etc/elasticsearch directory. Here you can get two files elasticsearch.yml and logging.yml. To configure the elasticsearch you have to open elasticsearch.yml file in your text editor.

sudo nano /etc/elasticsearch/elasticsearch.yml

Now find the line node.name and cluster.name in this file and uncomment it (remove # before them) and change the value of these two.

Find the line, network.bind_host uncomment it by removing the # character at the beginning of the line, and change the value to 0.0.0.0 so it looks like this:

network.bind_host: 0.0.0.0

After all done, elasticsearch will be installed on your computer, now just run this command to start elasticsearch.

sudo service elasticsearch start

For testing elasticsearch output. just type http://localhost:9200/ in your browser and see the possible response.

{
  "status" : 200,
  "name" : "sohel-rana-pc",
  "cluster_name" : "sohel-rana-elasticsearch",
  "version" : {
    "number" : "1.7.2",
    "build_hash" : "e43676b1385b8125d647f593f7202acbd816e8ec",
    "build_timestamp" : "2015-09-14T09:49:53Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.4"
  },
  "tagline" : "You Know, for Search"
}

Internationalization & Localization Application with CakePHP 3

CakePHP has one of the coolest features to make application Internationalization & Localization. It’s really easy to translate your application or website into multiple languages with CakePHP 3.  Let’s see it.

First of all, create a directory Locale inside PROJECT_ROOT/src

Create a subdirectory of each language (which language you want to translate your application) inside PROJECT_ROOT/src/Locale/. Subdirectory name can be anything (ex. english, french, etc), but prefer ISO code of the language (ex. en_US, fr_FR, bn_BN etc). So follow this tutorial and make your subdirectory name en_USfr_FR, and bn_BN

Create default.po file inside all subdirectories (en_USfr_FR, and bn_BN). And copy the following code in the respective file.

src/Locale/en_US/default.po

msgid "message"
msgstr "Hello World"

 

src/Locale/fr_FR/default.po

msgid "message"
msgstr "Bonjour le monde"

 

src/Locale/bn_BN/default.po

msgid "message"
msgstr "ওহে বিশ্ব"

Here msgid key of which will use inside the template (view files) and msgstr is the values which will gonna translate.

So now you have three different languages (en_USfr_FR, and bn_BN). and one key (message) which can be translated into these three languages.

Now use <?php echo __(‘message’); ?> in your view (any ctp file of your application).

Then open PROJECT_ROOT/config/app.php file and find

 'defaultLocale' => env('APP_DEFAULT_LOCALE', 'fr_FR')

and replace with your language.

But if you want to translate language in runtime then you can use it

use Cake\I18n\I18n;

I18n::locale('fr_FR');

I hope this article will help you to translate your application in multiple language.

Host Static Website in Google App Engine

This article is for hosting your static website into Google App Engine. This article provides an app.yaml file so you can host your static website into Google App Engine. Before do anything make your website’s folder structure like that (see below image). So put all the static files inside assets folder (css, js, images, fonts) and all HTML files in project’s root directory. Inside assets folder css, js, img, and fonts are sperate folders for holding separate files (ex. css file stylesheets, img for images)

Host Static Website in Google App Engine

Then copy-paste this snipped in your YAML file. And then deploy (gcloud app deploy) your application.

You can also make your project structure your own way, then you have to change YAML content based on your changes. So that means need to change YAML information according to your project’s structure.

Create NGINX Virtual Host with PHP 7.0

Here I have shared a script for creating NGINX virtual host with PHP 7.0. NGINX virtual host with  5.x and 7.0 is very similar but only different is php5-fpm.sock location. To create a NGINX virtual host open the configuration files by running this command.

sudo gedit /etc/nginx/sites-available/default

And then copy-pastes this code snippet and modify it by your configuration (information). Then restart your server. And you are ready to go.

See it for creating virtual host with PHP 5.x

Change an Array Value if Duplicate

I have an array which holds the multiple same values in same categories. And I want to If the data is duplicate and the category is ‘Sales’ I want 1 of them to change to ‘Service’ and vice versa.

From above snippet, you can see ‘Lane Assist’ is duplicate in ‘Sales‘ category and ‘Bandenafdichtset’ is duplicate in ‘Service‘ category and I changed them as vice versa.

Getting Query String Values Without a Hash in the URL

$location.search() returns empty? Angular does not remove the query before the hash bang in URL. According to AngularJS documentation,  hash bang URL format is:

http://example.com/#!/bar?baz=23

So if you have no hash bang in your URL (e.g. http://example.com/bar?baz=23), and try to get query string value, then you can use this snippet.

function getQueryParams(queryParam, url) 
{
        var pageUrl = url || window.location.search.substring(1);
        var urlVariables = pageUrl.split(/[&||?]/);
        var response = null;

        for (var i = 0; i < urlVariables.length; i += 1) {
            var param = urlVariables[i];
            var paramName = (param || '').split('=');

            if (paramName[0] === queryParam) {
                response = paramName[1];
            }
        }
        return response;
}
    
console.log(getQueryParams('baz', 'http://example.com/bar?baz=23'));

This code is also with pure JavaScript (withour AngularJS).

Change Bower Default Directory And Folder

Bower is the client-side dependencies manager. The developer can easily manage their client-side dependencies with the Bower. By default when you install any package (e.g jQyery, Bootstrap etc) through Bower, then a bower_components folder will create in the project’s root directory and store all the files inside it. So the default directory of bower installation is ROOT and folder is bower_components
But if you want to change the Bower default directory and folder then you have to follow these following steps.

Remove bower_components (if exist) from your project root directory

 

Create .bowerrc file in your project’s root directory

 

Paste these code in .bowerrc file and modify DIRECTORY_NAME & FOLDER_NAME

{
  "directory": "DIRECTORY_NAME/FOLDER_NAME"
}

Then finally run bower install command.

Hope these will helpful to configure your Bower directory and folder name.