Migrating to WebClientPrint 4.0 for PHP
Product WebClientPrint for PHP Published 12/22/2017 Updated 03/07/2018 Author Neodynamic
Overview
The brand new WebClientPrint 4.0 for PHP is compatible with Version 3.0 but a few modifications must be done to get v4.0 working on an existing project using v3.0. Please use this article as a general guide to upgrading your server-side code to v4.0
In this article:
• WebClientPrint.php Update
In your project where old v3 is being used and you want to upgrade to v4, then UPDATE the WebClientPrint.php file to latest v4. Do it by downloading the package installer from our website's Download section
• WebClientPrint Processor (WCPP) Update
Any project using WebClientPrint.php v4 will require that ANY Client that wants to print from them, to install WebClientPrint Processor (WCPP) v4 which can be downloaded and installed from our website's Download section
• WebClientPrintAPIController Update
In v4.0, WebClientPrint brings new features that require you to update the code you used to have to v3.0 in the WebClientPrintAPIController.php file.
<?php
//*********************************
// IMPORTANT NOTE
// 1. In this sample we store users related stuff (like
// the list of printers and whether they have the WCPP
// client utility installed) in the wcpcache folder BUT
// you can change it to another different storage (like a DB)!
// which will be required in Load Balacing scenarios
//
// 2. If your website requires user authentication, then
// THIS FILE MUST be set to ALLOW ANONYMOUS access!!!
//
//*********************************
include 'WebClientPrint.php';
use Neodynamic\SDK\Web\WebClientPrint;
//IMPORTANT SETTINGS:
//===================
//Set wcpcache folder RELATIVE to WebClientPrint.php file
//FILE WRITE permission on this folder is required!!!
WebClientPrint::$wcpCacheFolder = getcwd().'/wcpcache/';
if (file_exists(WebClientPrint::$wcpCacheFolder) == false) {
//create wcpcache folder
$old_umask = umask(0);
mkdir(WebClientPrint::$wcpCacheFolder, 0777);
umask($old_umask);
}
//===================
// Clean built-in Cache
// NOTE: Remove it if you implement your own cache system
WebClientPrint::cacheClean(30); //in minutes
// Process WebClientPrint Request
$urlParts = parse_url($_SERVER['REQUEST_URI']);
if (isset($urlParts['query'])){
$query = $urlParts['query'];
parse_str($query, $qs);
//get session id from querystring if any
$sid = NULL;
if (isset($qs[WebClientPrint::SID])){
$sid = $qs[WebClientPrint::SID];
}
try{
//get request type
$reqType = WebClientPrint::GetProcessRequestType($query);
if($reqType == WebClientPrint::GenPrintScript ||
$reqType == WebClientPrint::GenWcppDetectScript){
//Let WebClientPrint to generate the requested script
//Get Absolute URL of this file
$currentAbsoluteURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
$currentAbsoluteURL .= $_SERVER["SERVER_NAME"];
if($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443")
{
$currentAbsoluteURL .= ":".$_SERVER["SERVER_PORT"];
}
$currentAbsoluteURL .= $_SERVER["REQUEST_URI"];
$currentAbsoluteURL = substr($currentAbsoluteURL, 0, strrpos($currentAbsoluteURL, '?'));
ob_start();
ob_clean();
header('Content-type: text/javascript');
echo WebClientPrint::generateScript($currentAbsoluteURL, $query);
return;
}
else if ($reqType == WebClientPrint::ClientSetWcppVersion)
{
//This request is a ping from the WCPP utility
//so store the session ID indicating this user has the WCPP installed
//also store the WCPP Version if available
if(isset($qs[WebClientPrint::WCPP_SET_VERSION]) && strlen($qs[WebClientPrint::WCPP_SET_VERSION]) > 0){
WebClientPrint::cacheAdd($sid, WebClientPrint::WCP_CACHE_WCPP_VER, $qs[WebClientPrint::WCPP_SET_VERSION]);
}
return;
}
else if ($reqType == WebClientPrint::ClientSetInstalledPrinters)
{
//WCPP Utility is sending the installed printers at client side
//so store this info with the specified session ID
WebClientPrint::cacheAdd($sid, WebClientPrint::WCP_CACHE_PRINTERS, strlen($qs[WebClientPrint::WCPP_SET_PRINTERS]) > 0 ? $qs[WebClientPrint::WCPP_SET_PRINTERS] : '');
return;
}
else if ($reqType == WebClientPrint::ClientSetInstalledPrintersInfo)
{
//WCPP Utility is sending the installed printers at client side with detailed info
//so store this info with the specified session ID
//Printers Info is in JSON format
$printersInfo = $_POST['printersInfoContent'];
WebClientPrint::cacheAdd($sid, WebClientPrint::WCP_CACHE_PRINTERSINFO, $printersInfo);
return;
}
else if ($reqType == WebClientPrint::ClientGetWcppVersion)
{
//return the WCPP version for the specified Session ID (sid) if any
ob_start();
ob_clean();
header('Content-type: text/plain');
echo WebClientPrint::cacheGet($sid, WebClientPrint::WCP_CACHE_WCPP_VER);
return;
}
else if ($reqType == WebClientPrint::ClientGetInstalledPrinters)
{
//return the installed printers for the specified Session ID (sid) if any
ob_start();
ob_clean();
header('Content-type: text/plain');
echo base64_decode(WebClientPrint::cacheGet($sid, WebClientPrint::WCP_CACHE_PRINTERS));
return;
}
else if ($reqType == WebClientPrint::ClientGetInstalledPrintersInfo)
{
//return the installed printers with detailed info for the specified Session ID (sid) if any
ob_start();
ob_clean();
header('Content-type: text/plain');
echo base64_decode(WebClientPrint::cacheGet($sid, WebClientPrint::WCP_CACHE_PRINTERSINFO));
return;
}
}
catch (Exception $ex)
{
throw $ex;
}
}
- If your website uses CSRF Protection feature, then you exclude this Controller from it! In the sample project, we've modified the App\Http\Middleware\VerifyCsrfToken.php file with this setting: protected $except = [ 'WebClientPrintController' ];
- This WebClientPrintAPIController now requires to accept both GET & POST requests, so the new setting for it in the routes\web.php file must be modified to Route::any('WebClientPrintController', 'WebClientPrintController@processRequest');
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
//*********************************
// IMPORTANT NOTE
// 1. In this sample we store users related stuff (like
// the list of printers and whether they have the WCPP
// client utility installed) in the wcpcache folder BUT
// you can change it to another different storage (like a DB)!
// which will be required in Load Balacing scenarios
//
// 2. If your website requires user authentication, then
// THIS FILE MUST be set to ALLOW ANONYMOUS access!!!
//
// 3. EXCLUDE this Controller from CSRF Protection
// Ref https://laravel.com/docs/5.5/csrf#csrf-excluding-uris
//*********************************
//Includes WebClientPrint classes
include_once(app_path() . '\WebClientPrint\WebClientPrint.php');
use Neodynamic\SDK\Web\WebClientPrint;
class WebClientPrintController extends Controller
{
public function processRequest(Request $request){
//IMPORTANT SETTINGS:
//===================
//Set wcpcache folder RELATIVE to WebClientPrint.php file
//FILE WRITE permission on this folder is required!!!
WebClientPrint::$wcpCacheFolder = app_path() . '/WebClientPrint/wcpcache/';
//===================
// Clean built-in Cache
// NOTE: Remove it if you implement your own cache system
WebClientPrint::cacheClean(30); //in minutes
//get session id from querystring if any
$sid = NULL;
if ($request->has(WebClientPrint::SID)){
$sid = $request->input(WebClientPrint::SID);
}
try{
//get query string from url
$query = substr($request->fullUrl(), strpos($request->fullUrl(), '?')+1);
//get request type
$reqType = WebClientPrint::GetProcessRequestType($query);
if($reqType == WebClientPrint::GenPrintScript ||
$reqType == WebClientPrint::GenWcppDetectScript){
//Let WebClientPrint to generate the requested script
//Get Absolute URL of this file
$currentAbsoluteURL = substr($request->fullUrl(), 0, strrpos($request->fullUrl(), '?'));
// Return WebClientPrint's javascript code
return response(WebClientPrint::generateScript($currentAbsoluteURL, $query))
->header('Content-Type', 'text/javascript');
}
else if ($reqType == WebClientPrint::ClientSetWcppVersion)
{
//This request is a ping from the WCPP utility
//so store the session ID indicating this user has the WCPP installed
//also store the WCPP Version if available
if($request->has(WebClientPrint::WCPP_SET_VERSION) && strlen($request->input(WebClientPrint::WCPP_SET_VERSION)) > 0){
WebClientPrint::cacheAdd($sid, WebClientPrint::WCP_CACHE_WCPP_VER, $request->input(WebClientPrint::WCPP_SET_VERSION));
}
return;
}
else if ($reqType == WebClientPrint::ClientSetInstalledPrinters)
{
//WCPP Utility is sending the installed printers at client side
//so store this info with the specified session ID
WebClientPrint::cacheAdd($sid, WebClientPrint::WCP_CACHE_PRINTERS, strlen($request->input(WebClientPrint::WCPP_SET_PRINTERS)) > 0 ? $request->input(WebClientPrint::WCPP_SET_PRINTERS) : '');
return;
}
else if ($reqType == WebClientPrint::ClientSetInstalledPrintersInfo)
{
//WCPP Utility is sending the installed printers at client side with detailed info
//so store this info with the specified session ID
//Printers Info is in JSON format
$printersInfo = $request->input('printersInfoContent');
WebClientPrint::cacheAdd($sid, WebClientPrint::WCP_CACHE_PRINTERSINFO, $printersInfo);
return;
}
else if ($reqType == WebClientPrint::ClientGetWcppVersion)
{
//return the WCPP version for the specified Session ID (sid) if any
return response(WebClientPrint::cacheGet($sid, WebClientPrint::WCP_CACHE_WCPP_VER))
->header('Content-Type', 'text/plain');
}
else if ($reqType == WebClientPrint::ClientGetInstalledPrinters)
{
//return the installed printers for the specified Session ID (sid) if any
return response(base64_decode(WebClientPrint::cacheGet($sid, WebClientPrint::WCP_CACHE_PRINTERS)))
->header('Content-Type', 'text/plain');
}
else if ($reqType == WebClientPrint::ClientGetInstalledPrintersInfo)
{
//return the installed printers with detailed info for the specified Session ID (sid) if any
return response(base64_decode(WebClientPrint::cacheGet($sid, WebClientPrint::WCP_CACHE_PRINTERSINFO)))
->header('Content-Type', 'text/plain');
}
}
catch (Exception $ex)
{
throw $ex;
}
}
}
That's all. Just be sure that your project is now referencing new v4.0 of WebClientPrint.php file.
As always, do not hesitate to contact our dev team at http://neodynamic.com/support