Enable Plesk Webp PHP 7.4 GD Support

Today i want to add on my website the support for webp images, and so i have found a plugin for wordpress that is free WebP Converter for Media

But for this plugin we must to active the support webp on the php module GD.
On My VPS i have installed PHP 7.4 on Plesk, the simple way is to change the modulo gd of plesk php version with a custom module in witch the webp support is enabled.

I have find this useful guide that i rewrite with php 7.4 that i have used on my VPS. 

Install the necessary package

# yum install make gcc plesk-php74-devel libjpeg-turbo-devel libpng-devel libXpm-devel freetype-devel libwebp-devel

Download PHP source. Note! Version of PHP source should be the same as your Plesk PHP version! For example, for PHP 7.4.4

# wget http://be2.php.net/get/php-7.4.4.tar.gz/from/this/mirror
# tar -xzf mirror
# cd php-7.4.4/ext/gd

Compile gd.so module

# /opt/plesk/php/7.4/bin/phpize
# ./configure --with-php-config=/opt/plesk/php/7.4/bin/php-config --with-webp --with-freetype --with-jpeg --with-xpm
# make

Replace original Plesk PHP gd.so module with compiled

# mv /opt/plesk/php/7.4/lib64/php/modules/gd.so /opt/plesk/php/7.4/lib64/php/modules/gd.so_orig
# cp modules/gd.so /opt/plesk/php/7.4/lib64/php/modules/
# plesk bin php_handler --reread

Check that all is OK

# /opt/plesk/php/7.4/bin/php -m | grep gd
gd

Do not forget to to make fake removal of original plesk-php74-gd package! Otherwise, your custom gd.so will be overwritten with next update.

# rpm -e --justdb plesk-php74-gd

 

At last remember that you must to go on
Plesk -> Tool & Setting -> PHP Settings
Choose your PHP Verison in this case PHP 7.4.4 and

  • Remove GD module and save
  • Add GD module and save

So Plesk rebuild php for the domains.

CodeAnywhere Laravel Php Container Setup

I use for a long time CodeAnywhere to develop my application via FTP, or to use shell server console via SSH.
Now i want to use the Container and try to create a Laravel PHP application.
But i have encountered some errors that i want to report you.

Now start with the creation of the container, with the “Connection Wizard” is very simple:

If we want to run the Laravel php application, we must to use this command, and set the ip 0.0.0.0 to use app from remote container url.

https://phpbox-CODEOFCODEANYWHERE.codeanyapp.com/

[cabox@PHPBOX app]$ php artisan serve --host=0.0.0.0 --port=8000
Laravel development server started: http://0.0.0.0:8000

Some possible error

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

Edit your app/providers/AppServiceProvider.php file and inside the boot method set a default string length:

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}

SQLSTATE[42S01]: Base table or view already exists: 1050 Table ‘users’ already exists

Solution is to run con shell

[cabox@PHPBOX app]$ php artisan migrate:fresh
[cabox@PHPBOX app]$ php artisan migrate

PHP Script Import Mysql Dump Gzip .sql.gz

In many case we have the necessity to import a mysql dump in fast way on our database, to see some old data or to restore.

Now i want to show you a simple but powerful php script to import a mysql dump gzipped.

We use the function zcat with shell_exec

shell_exec("zcat <url file> | mysql -u <user_db> -p<password db> <database>")

This is a simple php script that we can use on our application, to restore in fast way our gzipped mysql dump

We create a select, to show a list of file, and then choose one and click “load”, then with our zcat shell execution function, load it.

<?php
if(isset($_POST['action']) &amp;&amp; $_POST['action'] == 'execute')
{  
  $file_name = $_POST['filename'];
  $out = shell_exec("zcat ".$file_name." | mysql -u user_db -pPasswordDB db_name");
  echo "Mysql Dump Imported Successfull!";
}


function getDirContents($dir, &amp;$results = array()){
    $files = scandir($dir);
    foreach($files as $key => $value){
        $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
        if(!is_dir($path)) {
            $results[] = $path;
        } else if($value != "." &amp;&amp; $value != "..") {
            getDirContents($path, $results);
            $results[] = $path;
        }
    }

    return $results;
}

//directory of our backup file
$listFile = getDirContents('/var/www/vhosts/<our domain>/backups');
echo "<form method=POST>";
echo "<select name='filename'>";
foreach ($listFile as &amp;$value) {
  echo "<option>".$value."</option>";
}
echo "</select>";
echo "<input type='hidden' name='action' value='execute'>";
echo "<input type='submit' name='Load Mysql Dump'>";
echo "</form>";
?>

Plesk Nginx WP Super Cache Expert Mode Settings

Have you got a wordpress site and you want to optimize it?
The simple way to take fast your website is:

  • Install WPSuper Cache Plugin
  • Setting Server side Nginx

Install WP Super Cache Plugin

The installation of WPSuper Cache is easy, just download the plugin and then upload it on our wordpress website.

Setting WP Super Cache

Now i show to you how to set wp super cache in “Expert mode”, remember that this mode require the change on the server for nginx configuration.

Enable Caching ON

Then we must to set the Expert Mode, and then suggest some setting:

Settings Server side Nginx

Now we need to set the configuration of nginx on our server, in this case i configure a Plesk Server.
We must go in the Domain Setting -> Apache & nginx Settings
In the bottom we find “Additional nginx directives

We must to enable:

  • GZIP compression for the page that we serve on the client
  • Header Cache Control to declase a cache browser for the js, css, image etc..
  • Location to find wpsupercache generated file
  • Security setting 
### WP Super Cache Below ###
set $cache_uri $request_uri;

# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
	set $cache_uri 'null cache';
}

# GZIP Compression
gzip on;
gzip_disable "MSIE [1-6]\\.(?!.*SV1)";
gzip_min_length 1100;
gzip_buffers 4 32k;
gzip_proxied any;
gzip_comp_level 9;
gzip_types text/plain text/css application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/bmp image/svg+xml;
gzip_vary on;

# NGINX Caching
location ~* \.(?:ico|css|js|gif|jpe?g|png|svg|woff)$ {
	expires 14d;
	add_header Cache-Control "public, no-transform";
	log_not_found off;
}

location ~* \.(jpg|jpeg|gif|png)$ {
	expires 14d;
	add_header Cache-Control "public, no-transform";
	log_not_found off;
}

location ~* \.(pdf|css|html|js|swf)$ {
	expires 14d;
	add_header Cache-Control "public, no-transform";
	log_not_found off;
}

location ~ \.css {
	add_header  Content-Type    text/css;
}
location ~ \.js {
	add_header  Content-Type    application/x-javascript;
}

if ($query_string != "") {
	set $cache_uri 'null cache';
}

# Don't cache uris containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
	set $cache_uri 'null cache';
}

# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
	set $cache_uri 'null cache';
}

# Use cached or actual file if they exists, otherwise pass request to WordPress
location ~ / {
	try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php ;
}

# WORDPRESS PERMALINKS
if (!-e $request_filename) {
	rewrite ^(.+)$ /index.php?q=$1 last;
}

# SECURITY
location ~* wp-config.php { deny all; }
location ~* "^/wp-content/(?!plugins/).*\.php" { deny all; }

Now you can test your site on pagespeed and see the difference!
If you want much performance you can use Autoptimize WordPress plugin to minify js, css and html.

WordPress Plugin Creation Tutorial – Simplest way

Today I asked myself how to explain to a junior, how to create a wordpress plugin, the simplest way. Without the inclusion of complex structures with public folder, includes, ….., but something very very simple.

Below I will explain how to create a plugin to manage some date, in this example is a Sport Race:
– Create Sport Race
– Insert members of a Sport Race
– Show table with Sport Race, and manage data (bulk delete)
– Show table with Sport Race, and manage data (bulk delete)
– Upload media using WordPress System (media manager of wordpress)

You can download it on: GITHUB

The structure of the project in the Plugin name directory is:

As you can see, a simple list of file with no directory

My project as two section: Sport Race and Members
Each section is composed by two file: _page.php and _table.php
And a file of system: plugin_activation.php

For example now analyze two file: sportrace_page.php and sportrace_table.php

For the sportrace_table.php will explain in the next article how to create a table, now take a look at file, is very simple.
For now i will explain how to use it in the sportrace_page.php

At top of page we put

<?php 
global $wpdb;

// jQuery
wp_enqueue_script('jquery');
// This will enqueue the Media Uploader script
wp_enqueue_media();
?>

For the inclusion of table put:

<?php 
echo '<form method="post">';
require_once 'sportrace_table.php';
$class = new Sportrace_Table();
$class->prepare_items();
$class->display();
echo '</form>';
?>

In the plugin_activation.php you can find the mysql create query and the menu inclusion!

Let’s start to create your wordpress plugin! Simple and Fast!

Ebay OAuth 2 Generate Token and Refresh – PHP

Today i will show to you how to use a OAuth 2 on Ebay, so how you can take your token, and then use it.

At first you must to register on https://developer.ebay.com

Then you must to set your Application on (Sandbox/Test or Production, in this guide i show to you Production procedure) https://developer.ebay.com/my/keys

After you must to have a situation like this:

Now we have:
– Client ID
– Dev ID
– Client Secret

We need:
– Authorization Code
– Access Token
– Refresh Token

Authorization Code

At first we must to generate the Authorization Code, and for this procedure we must to paste an url on the browser and copy the query string “code”.
So click on the “User token link”

Click on “Get a Token from eBay via Your Application”

Then you must to “Add eBay Redirect URL”
So you generate your app details, you can see the
“Your branded eBay Production Sign In (OAuth)” or if sandbox (test)
“Your branded eBay SandboxSign In (OAuth)”

Copy the link, that is like:

https://auth.ebay.com/oauth2/authorize?client_id=App-PRD-hhhhhf72-3hhb&response_type=code&redirect_uri=YourName-App–okllp&scope=https://api.ebay.com/oauth/api_scope https://api.ebay.com/oauth/api_scope/sell.marketing.readonly https://api.ebay.com/oauth/api_scope/sell.marketing https://api.ebay.com/oauth/api_scope/sell.inventory.readonly https://api.ebay.com/oauth/api_scope/sell.inventory https://api.ebay.com/oauth/api_scope/sell.account.readonly https://api.ebay.com/oauth/api_scope/sell.account https://api.ebay.com/oauth/api_scope/sell.fulfillment.readonly

Put this link on your browser, and login with your ebay account, so then you must to redirect on another page that sayd to you that the application has grant, and in the url bar you find your authorization code.

https://signin.ebay.com/ws/eBayISAPI.dll?ThirdPartyAuthSucessFailure&isAuthSuccessful=true&code=v%5K1.1HH%…..w&expires_in=299

Access Token and Refresh Token

Now with our authorization code, we call this url to take our access and refresh token.
For this call we need the authorization header, and create a base64 encode with clientID and certID.
And then in the parameter set Authorization code and RuName

$link = "https://api.ebay.com/identity/v1/oauth2/token";
        $codeAuth = base64_encode(':');
        $ch = curl_init($link);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/x-www-form-urlencoded',
            'Authorization: Basic '.$codeAuth
        ));
        curl_setopt($ch, CURLHEADER_SEPARATE, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=authorization_code&amp;code==&amp;redirect_uri=");
        $response = curl_exec($ch);
        $json = json_decode($response, true);
        $info = curl_getinfo($ch);
        curl_close($ch);
        if($json != null)
        {
            $this->authToken = $json["access_token"];
            $this->refreshToken = $json["refresh_token"];
        }

Why we have access token and refresh token?

We use Access Token for all the api request on eBay service.
We use Refresh Token to regenerate a Access Token.

Access token is valid 2 hour, but Refresh Token is valid 18 months.

How can i refresh my access token?

You must to take a call to this service, passing your refresh code, and the service return to you your new access token

$link = "https://api.ebay.com/identity/v1/oauth2/token";
        $codeAuth = base64_encode(':');
        $ch = curl_init($link);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/x-www-form-urlencoded',
            'Authorization: Basic '.$codeAuth
        ));
        curl_setopt($ch, CURLHEADER_SEPARATE, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=refresh_token&amp;refresh_token&amp;scope=https://api.ebay.com/oauth/api_scope https://api.ebay.com/oauth/api_scope/sell.marketing.readonly https://api.ebay.com/oauth/api_scope/sell.marketing https://api.ebay.com/oauth/api_scope/sell.inventory.readonly https://api.ebay.com/oauth/api_scope/sell.inventory https://api.ebay.com/oauth/api_scope/sell.account.readonly https://api.ebay.com/oauth/api_scope/sell.account https://api.ebay.com/oauth/api_scope/sell.fulfillment.readonly https://api.ebay.com/oauth/api_scope/sell.fulfillment https://api.ebay.com/oauth/api_scope/sell.analytics.readonly");
        $response = curl_exec($ch);
        $json = json_decode($response, true);
        $info = curl_getinfo($ch);
        curl_close($ch);
        if($json != null)
        {
            $this->authToken = $json["access_token"];
        }

I suggest to save your Access Token and Refresh Token on Database.

At the end my eBay API Simple Class

<?php

class EbayAPI
{
    protected $devID;
    protected $appID;
    protected $certID;
    protected $clientID;
    protected $serverUrl;
    public $userToken;
    protected $paypalEmailAddress;
    protected $ruName;


    public function __construct()
    {
        $this->devID = '<your-dev-id>'; // these prod keys are different from sandbox keys
        $this->appID = '<your-app-id>';
        $this->certID = '<your-cert-id>';
        $this->clientID = '<your-client-id>';
        //set the Server to use (Sandbox or Production)
        $this->serverUrl = 'https://api.ebay.com/ws/api.dll';      // server URL different for prod and sandbox
        //the token representing the eBay user to assign the call with

        $this->authCode = '<paste here your authorization code>'; 
        $this->authToken ="";
        $this->refreshToken ="";
        $this->ruName= "";

        $this->paypalEmailAddress= 'PAYPAL_EMAIL_ADDRESS';
        
    }

    public function firstAuthAppToken() {
        $url = "https://auth.ebay.com/oauth2/authorize?client_id=".$this->clientID."&amp;response_type=code&amp;redirect_uri=".$this->ruName."&amp;scope=https://api.ebay.com/oauth/api_scope https://api.ebay.com/oauth/api_scope/sell.marketing.readonly https://api.ebay.com/oauth/api_scope/sell.marketing https://api.ebay.com/oauth/api_scope/sell.inventory.readonly https://api.ebay.com/oauth/api_scope/sell.inventory https://api.ebay.com/oauth/api_scope/sell.account.readonly https://api.ebay.com/oauth/api_scope/sell.account https://api.ebay.com/oauth/api_scope/sell.fulfillment.readonly https://api.ebay.com/oauth/api_scope/sell.fulfillment https://api.ebay.com/oauth/api_scope/sell.analytics.readonly"; 
return $url;      
    }

    public function authorizationToken()
    {
        $link = "https://api.ebay.com/identity/v1/oauth2/token";
        $codeAuth = base64_encode($this->clientID.':'.$this->certID);
        $ch = curl_init($link);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/x-www-form-urlencoded',
            'Authorization: Basic '.$codeAuth
        ));
        curl_setopt($ch, CURLHEADER_SEPARATE, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=authorization_code&amp;code=".$this->authCode."&amp;redirect_uri=".$this->ruName);
        $response = curl_exec($ch);
        $json = json_decode($response, true);
        $info = curl_getinfo($ch);
        curl_close($ch);
        if($json != null)
        {
            $this->authToken = $json["access_token"];
            $this->refreshToken = $json["refresh_token"]; 
        } 
    }

    public function refreshToken()
    {
        $link = "https://api.ebay.com/identity/v1/oauth2/token";
        $codeAuth = base64_encode($this->clientID.':'.$this->certID);
        $ch = curl_init($link);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/x-www-form-urlencoded',
            'Authorization: Basic '.$codeAuth
        ));
        echo $this->refreshToken;
        curl_setopt($ch, CURLHEADER_SEPARATE, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=refresh_token&amp;refresh_token=".$this->refreshToken."&amp;scope=https://api.ebay.com/oauth/api_scope https://api.ebay.com/oauth/api_scope/sell.marketing.readonly https://api.ebay.com/oauth/api_scope/sell.marketing https://api.ebay.com/oauth/api_scope/sell.inventory.readonly https://api.ebay.com/oauth/api_scope/sell.inventory https://api.ebay.com/oauth/api_scope/sell.account.readonly https://api.ebay.com/oauth/api_scope/sell.account https://api.ebay.com/oauth/api_scope/sell.fulfillment.readonly https://api.ebay.com/oauth/api_scope/sell.fulfillment https://api.ebay.com/oauth/api_scope/sell.analytics.readonly");
        $response = curl_exec($ch);
        $json = json_decode($response, true);
        $info = curl_getinfo($ch);
        curl_close($ch);
        if($json != null)
        {
            $this->authToken = $json["access_token"];
        } 
    }
}