My wp-ShowGithubFile plugin is accepted by WordPress today!

Though I wrote the plugin initially for personal use, now the same is available for you to use in your WordPress sites.

Link: https://wordpress.org/plugins/wp-showgithubfile/

You can either download it from the directory, or install directly within your WordPress installation. Here is a live demo of the plugin:

001 package com.company;
002
003 import java.util.Arrays;
004
005 public class Main {
006
007     public static void main(String[] args) {
008
009         // New Customer wants to buy a bottle
010         Customer customer = new Customer();
011
012         // Customer Registration
013         customer.RegisterMe("+919999999999"PhoneType.APP ); // App or SMS?
014
015         // Assuming Registration successful
016
017         // Customer requests the token using a mobile app, or by sending SMS
018         String CustomerToken =  customer.giveMeAToken("695000"); // Pin code
019
020         if (CustomerToken != null) {
021             //  Assuming Customer gets the token and he goes to the Outlet
022         } else {
023             // Seems there are no slots available. Try again later.
024             return;
025         }
026
027
028         // At the Outlet, sales guy is scanning the token
029         Boolean OutletVerification = new Outlet().isTheTokenShownByCustomerValid(CustomerToken);
030
031         if (OutletVerification) {
032             // Assuming the validation is success
033             System.out.println("Customer says: Wow, I got the bottle!");
034         } else {
035             System.out.println("Sales guy says: Get lost!");
036         }
037     }
038
039     public enum PhoneType APPSMS };
040
041     private static class VQServer {
042         // Database
043         public String[] tokenDB = { "A""B","C","D","E" };
044         public String[] Outlets = {"Outlet1""Outlet2""Outlet3"};
045
046         public void registerCustomer(String phoneNumberPhoneType phonetype) {
047             // Register customer
048
049         }
050         public String requestNewTokenByCustomer(String pinCode) {
051             // Logic for - Token generation logic
052             // Logic for - Find nearest outlet using pinCode
053
054             // Send token by SMS if PhoneType is SMS
055
056             return "C"// Sample token
057         }
058
059         public Boolean validateTokenByOutlet(String token) {
060             return Arrays.asList(tokenDB).contains(token); // Validate
061         }
062     };
063
064     static VQServer vqServer = new VQServer();
065
066     private static class Customer {
067
068         public String giveMeAToken(String pinCode) {
069             return vqServer.requestNewTokenByCustomer(pinCode);
070         }
071         public void RegisterMe(String phoneNumberPhoneType phonetype) {
072             // Customer Registration
073             vqServer.registerCustomer(phoneNumberphonetype);
074         }
075     }
076     private static class Outlet {
077         public Boolean isTheTokenShownByCustomerValid(String token) {
078             return vqServer.validateTokenByOutlet(token);
079         }
080     }
081 }

View in GitHub | Made with wp-showgithubfile plugin

My Groovy HelloScript contribution to community

Here goes my contribution to HelloScript project by Praseed Pai.

GitHub: https://github.com/praseedpai/HelloScript_files/blob/master/Groovy/HelloScript.groovy

Source Code:

001 /**
002  * HelloScript.groovy
003  */
004
005 //  A Simple Console Output
006
007     println "Hello World"
008
009 //  Some more
010     print ("Hello World")
011     System.out.println("K-Mug"); // Java style also works
012
013 // Declare variables
014     def a 100 // a number, def is about scope
015     100// another number
016     year "Twenty Twenty" // a string
017
018 // String formatting & interpolation
019     println "This is ${a}${b}, and " year;
020
021 // Reassign a variable
022     year 2008// dynamic typing
023
024 // if-else
025     if (year 2008)
026         println "Welcome to the future - yes, we have flying cars!"
027     else if (year 2008)
028         printLN "The past - please don't change anything. Don't step on any butterflies. And for the sake of all thats good and holy, stay away from your parents!"
029     else
030         println "Anything wrong with your time machine? You have not gone anywhere, kiddo."
031
032
033 // Range based for loop
034 for (i in 0..3) {
035     println "$i Hi there!"
036 }
037
038 // Copying range value to a variable
039     range_array = (0..10)
040     print range_array;
041
042 // Array demo
043     rules = ['Do no harm','Obey','Continue Living']
044     println rules;
045
046     rules << "Be Honest" // add one more item
047     println rules
048
049 // Array demo, with mixed types
050     more_rules = ['Do no harm','Obey','Continue Living'404403500100f]
051     println more_rules;
052
053 // Loop through Array
054     0
055     while (rules.size()) {
056         print "Rule " + (1) + " : " rules[i]
057         += 1
058     }
059
060     println() // just a newline
061
062 // Associating array
063     associated = [
064         'hello'    :    'world',
065         'foo'    :    'bar',
066         'lorem'    :    'ipsum'
067     ]
068
069     for (ele in associated) {
070         print ele.key " : " ele.value
071     }
072
073     println() // just a newline
074
075 // Example of a Nested Loop
076 // To calculate Pythagorean Triplets
077     10
078     println "-------------------------------------"
079     for (va in (1..n)) {
080         for (vb in (va..n)) {
081             Integer c_square va**vb**2
082             Integer vc Math.sqrt(c_square)
083             if ((c_square vc**2) == 0) {
084                 println va " " vb " " vc
085             }
086         }
087     }
088     println "----------------------------------"
089
090 // Iterating over a list using range and size
091     println "-------------------------------------"
092     fibonacci = [0,1,1,2,3,5,8,13,21]
093     for (i in (0..fibonacci.size()-1)) {
094         println i " " fibonacci[i]
095     }
096     println "---------------------------------------"
097
098 // Parsing a line - split and join
099     csv_values "hello,world,how,are,you".split(",")
100     println csv_values;
101     println csv_values.join(":")
102
103 // A Single Argument Function
104     def hello(name) {
105         return "Hello ${name}!"
106     }
107     println hello("Praveen")
108
109 // A simple class
110     class Movie {
111         String name ""
112         Integer rating 0
113
114         def Movie(movieName) {
115             this.name movieName
116             this.rateMovie()
117         }
118
119         def rateMovie() {
120             this.rating = (this.name.length() % 10) + 1     // IMDBs rating algorithm. True story!
121         }
122         def printMovieDetails() {
123             println "Movie : " +   this.name
124             println "Rating : " '*'.multiply(this.rating)  +  "(" this.rating +")"
125         }
126     }
127
128 // Create the Object
129     ncfom = new Movie("New Country for Old Men");     // It's a sequel!
130     ncfom.printMovieDetails()
131
132 // Closures in action
133     myList = ["Hello","My","World","What's","Up"]
134     myList.each {
135         print it "-" // it is special
136     }
137
138 // Multiplication table
139     (1..10).each {
140         println "${it} x 2 = ${it*2}"
141     }
142
143     println() // just a newline
144
145 // Tuples
146     myTuple = new Tuple(1'two'3'four')
147     println myTuple
148
149 // File IO - Create a new file
150     myFile = new File("myfile.txt");
151     myFile.text "Hello World! - from the file"
152     myFile.createNewFile()
153
154 // File IO - Read from file
155     println myFile.text
156
157 // File IO - With closure
158     myFile.eachLine {
159         -> println l.toUpperCase()
160     }
161
162 // Regular Expressions
163     import java.util.regex.Pattern
164     Pattern pattern = ~/World/
165     str "Hello World, this is Universe, not your World!"
166     println "Found " pattern.matcher(str).size() + " mathes."

View in GitHub | Made with wp-showgithubfile plugin [Code generated by wp-ShowGithubFile plugin]

WordPress plugin – Show your source code directly from GitHub in your blog/website

Show a file, preferably source code file content in a WordPress blog post or page.  This plugin shows always the latest code from GitHub.

Visit my GitHub repository to download the plugin for free – https://github.com/ninethsense/wp-ShowGithubFile

screenshot

Source Code: (Live Example directly from GitHub 🙂

001 <?php
002 /**
003  * Plugin Name: wp-ShowGithubFile
004  * Description:       Show a file, preferably source code file content in a WordPress blog post or page
005  * Version:           1.0.0
006  * Author:            NinethSense
007  * Author URI:        https://blog.ninethsense.com/
008  * License:           GPL v2 or later
009  */
010
011
012     add_shortcode"GitHub""ShowGitHub" );
013     function ShowGitHub($atts) {
014         if (!isset($atts["file"]) && !strpos(strtolower($atts["file"]), "https://raw.githubusercontent.com/" )) {
015             // Also, I want only files from GitHub. Modify this to show file from any URL
016             return "[Invalid GitHub Raw file]";
017         }
018         
019         $fh = @get_headers($atts["file"]);
020         
021         if (strpos($fh[0],"200") == 0) {
022             return "[Invalid file]";
023         }
024         
025         $fcontents file_get_contents(trim($atts["file"]));
026         $notphp false;
027         
028         if (strpos($fcontents"?php") == false) {
029             $fcontents "<?php ".$fcontents;
030             $notphp true;
031         }
032         
033         // Make use of PHP syntax highlighing. Something is better than nothing.
034         $fcontents highlight_string($fcontentsTRUE);
035         
036         if ($notphp) {
037             $fcontents str_replace("&lt;?php&nbsp;""",$fcontents);
038             $notphp false;
039         }
040
041         $fcontents str_replace(["<code>""</code>"], "",$fcontents);
042         
043         $fcontents explode("<br />",$fcontents);
044         
045         $style = (isset($atts["style"])) ?$atts["style"]:"";
046         $ret "<div style='font-family:monospace;background-color:#dcd7ca;width:100%;overflow:auto;white-space:nowrap;border:solid 1px #aaa;font-size:10pt;$style'>";
047         for ($i=0;$i<sizeof($fcontents);$i++) {
048             $line $fcontents[$i];
049             $ret .= "<span style='font-size:10pt;display:block;width:40px;background-color:#aaa;float:left'>" 
050                 str_pad($i+13,'0',STR_PAD_LEFT) ." </span><span style='margin-left:10px;width:100%;inline-block'>" $line "</span><br />";
051         }
052         $ret .= "</div>";
053        
054         file_put_contents("D:\\test.txt"$ret);
055         return $ret;
056
057         
058     }
059 ?>

View in GitHub | Made with wp-showgithubfile plugin

PHP PortPing script – check host connectivity

Here is a script in PHP I wrote to check if a remote host and port are open for you to connect. Do not mistake this as an equivalent to ping command because ping uses ICMP and I used TCP using PHP’s famous fsockopen() function, and a small piece of AJAX.

PortPing

You can get the latest source code from my GitHub code share page – https://github.com/ninethsense/code-share/

PHP:

function ping($host, $port) {
         $startTime = time();
         if ($fs = fsockopen($host, $port, $errCode, $errStr, 1)) {
             $timeTaken = (time()-$startTime)/1000;
         echo "Reply from $host:$port time={$timeTaken}ms";
             fclose($fs);
         } else {   
             echo "Request timed out.";
         }
         echo "<br>";
     }

JavaScript:

var count = 0;
             var intervalID = setInterval(function PortPing() {
                 var xhttp = new XMLHttpRequest();
                 xhttp.onreadystatechange = function() {
                     if (this.readyState == 4 && this.status == 200) {
                         document.getElementById("console").innerHTML += this.responseText;
                     }
                 };
                 xhttp.open("GET", "PortPing.php?source=self&host=<?=$host?>&port=<?=$port?>", true);
                 xhttp.send();
                 if (++count > <?=$t-1?>) {
                     clearInterval(intervalID);
                 }
             },2);

How to setup AWS SES / Simple Email Service

This is a tutorial on how to setup AWS SES / Simple Email Service.

Step 1 – Login to your AWS Console, then choose Services –> Customer Engagement –> Simple Email Service

image

Step 2 – Verify a New Domain

Choose “Domains” from left navigation panel, then click –> Verify a New Domain button

image

Step 2.2 – Enter Domain name

image

Also click checkbox Generate DKIM settings, though optional, I will be using this.

You will be displayed with a list of DNS settings in the next screen.

image

Once you completed the Step 3, click close.

Step 3 – Edit DNS records

You will have to login to your domain control panel and add these.

I will be using cPanel for this exercise. Steps are almost same with any domain/hosting control panel.

image

Step 4 – Wait for the domain to get verified. Click refresh icon on the right-top whenever you are impatient.

image

You have nothing to do here. Just wait for some 2-5-10 minutes.

image

Step 4.1 – Verify Email Address

You cannot even send a test mail, unless you have a email id verified.

Choose ‘Email Addresses’ from the left-navigation panel and click “Verify a New Email Address” button.

image

Make sure you enter an already existing email id.

image

A verification mail will be sent to your inbox.

There will be a URL you are expected to click, and you are verified.

image

Once verified, you should be able to see the verification status updated in AWS console too.

image

Step 5 – Send test mail.

Go back to Domains –> “Send a Test Mail” button.

image

A simple Folder Watcher for windows

Here is a simple file system watcher for windows. I wrote a script to monitor a folder for a specific purpose, but then thought to make this a generic tool so someone will benefit if I host in GitHub.

FolderWatcher

 

Find full project at https://github.com/ninethsense/FolderWatcher

 

Source Code:

private void Form1_Load(object sender, EventArgs e)
{
    toolStripLabel.Text = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

    StartMonitoring();
}

private void StartMonitoring()
{
    watcher = new FileSystemWatcher(toolStripLabel.Text)
    {
        IncludeSubdirectories = true,
        EnableRaisingEvents = true
    };
    watcher.Created += Watcher_Handler;
    watcher.Deleted += Watcher_Handler;
    watcher.Renamed += Watcher_Handler;
    watcher.Changed += Watcher_Handler;
}

private void Watcher_Handler(object sender, FileSystemEventArgs e)
{
    Invoke(new Action(() =>
    {
        listBoxLog.Items.Add($"[{DateTime.Now}][{e.ChangeType}] {e.FullPath}");
    }));
}

private void ToolStripClearButton_Click(object sender, EventArgs e)
{
    listBoxLog.Items.Clear();
}

private void ToolStripChooseFolderButton_Click(object sender, EventArgs e)
{
    watcher.EnableRaisingEvents = false;
    folderBrowserDialog.SelectedPath = toolStripLabel.Text;
    var dlg = folderBrowserDialog.ShowDialog();

    if (dlg == DialogResult.OK)
    {
        string OldPath = toolStripLabel.Text;

        try
        {
            watcher.Path = folderBrowserDialog.SelectedPath;
            watcher.EnableRaisingEvents = true;
        } catch (FileNotFoundException ex)
        {
            toolStripLabel.Text = OldPath;
            watcher.Path = OldPath;

            MessageBox.Show($"Seems you don't have permission to access folder        {folderBrowserDialog.SelectedPath}. Try starting this app as Administrator\n\nDetails:\n{ex.Message}", "Error", MessageBoxButtons.OK);
            watcher.EnableRaisingEvents = true;
        }
            toolStripLabel.Text = watcher.Path;

    }

}

Microsoft Build 2020 – Highlights

Find my notes on Microsoft 2020 Build – virtual event, held on 19,20 & 21st May 2020.

Build Home – https://mybuild.microsoft.com/

Microsoft 365

  • Fluid Framework – A new framework to build distributed web applications. (in preview) [Link]
  • Project Cortex – Classify organization content using AI to topics. Knowledge base to improve employees’ organizational intelligence and productivity (in preview). Microsoft Graph based APIs will be available.[Link]
  • Microsoft Lists – Will have templates for quick lists, color formatting, and the ability to create alerts, track issues, assets, routines, contacts, and inventory for companies [Link]
  • Office Scripting [Link]
  • Outlook – Compose email messages with text prediction
  • Excel – New features – new capabilities, contextual experiences [Link]
  • Misc. – Office Add-In debugger Extension for VS Code

Teams

  • Teams to get customizable templates
  • Easily create Chatbots
  • Power Platform Integration
  • Developers can build and publish Teams app using Visual Studio and VS Code
  • Easy integration of custom apps and Power Apps using single click
  • Share Power BI reports in Teams
  • Schedule virtual appoints in Teams
  • Broadcast events and create studio productions from virtual stage

Reference: [Link]

Power Platform

  • Trying to bring in low-code RPA into Power Automate with the acquisition of Softomotive [Link]
  • New “Add to Teams” experience

Azure

  • Azure Synapse Link
    • Bringing database services and analytics together in real time [Link]
  • Azure ML
    • New “responsible machine learning” tools in AML. This will help developers understand, protect and control their models throughout the machine learning lifecycle. [Link][Link]
  • AI Supercomputer
    • New infrastructure available in Azure to train extremely large AI models [Link]
  • Azure Arc & Azure Stack Hub (updates)
    • Azure Arc – For customers who want to simplify complex and distributed environments across on-premises, edge and multicloud, Azure Arc enables deployment of Azure services anywhere and extends Azure management to any infrastructure [Link]
    • Azure Stack Hub – Azure Stack Hub broadens Azure to let you run apps in an on-premises environment and deliver Azure services in your datacenter [Link]
  • Azure SQL Edge (in preview)
    • Combines new capabilities such as data streaming and time series with in-database machine learning and graph features [Link]
  • Azure Quantum
    • One-stop-shop in Azure that gives you the freedom to create your own path to scalable quantum computing. [Link]

Developers

  • Visual Studio Codespaces (formerly Visual Studio Online) [Link]
    • Cloud-hosted, managed dev environments accessible from anywhere
  • Project Reunion
    • Unifying app development across the billion windows 10 devices [Link]
    • Breaking the barrier between Win32 and UWP APIs.
  • WSL / Windows Subsystem for Linux [Link]
    • Support for Windows Subsystem for Linux 2 distros
    • Run all Linux apps in WSL
    • GPU support
  • .NET
    • C# 9 – preview is available [Link]
  • Blazor
    • WebAssembly is out of preview
  • Microsoft Graph Services
    • productivity-focused services powered by Microsoft 365 and Azure, that are designed specifically for developers who are building high-value applications [Link]
  • Windows Package Manager
    • Native Package Manager for Windows 10 (in preview) [Link]

Certifications

  • Azure IoT Developer Specialty Certification

Misc.

  • Microsoft Cloud for Healthcare
    • Microsoft Cloud for Healthcare brings together trusted and integrated
      capabilities for customers and partners that enrich patient engagement and
      connects health teams to help improve collaboration, decision-making, and
      operational efficiencies [Link]
  • New Microsoft Edge