Monthly Archives: March 2019

Legends in Morris Chart

With help from this article https://github.com/morrisjs/morris.js/issues/346

This is about adding Legends to Morris charts. Though Morris chart shows legend on hovering on the graphs, but I wanted to show a legend permanently on one corner of the chart area.

Below are the codes. It is mostly plug and play code

The div in which the chart will be displayed

<div class="box-body chart-responsive">
    <div id="visitor_legend" class="bar-chart-legend"></div> <!-- the legend area -->
    <div class="chart" id="bar-chart" style="height: 300px;"></div> <!-- the chart area -->
</div>

The CSS

<style>
.bar-chart-legend {
	display: inline-block;
	right: 25px;
	position: absolute;
	top: 8px;
	font-size: 10px;
}

.bar-chart-legend .legend-item {
	display: block;
}

.bar-chart-legend .legend-color {
	width: 12px;
	height: 12px;
	margin: 3px 5px;
	display: inline-block;
}
</style>

The JS code

// Legend for Bar chart
bar.options.labels.forEach(function(label, i) {
	var legendItem = $('<span class="legend-item"></span>').text( label).prepend('<span class="legend-color">&nbsp;</span>');
	legendItem.find('span').css('backgroundColor', bar.options.barColors[i]);
	$('#visitor_legend').append(legendItem) // ID pf the legend div declared above
});

Morris charts documentation https://morrisjs.github.io/morris.js/lines.html

PHP substr_replace

Want to mask part of a string – say a API key or the first 12 digits of a credit card?

Here is a PHP function for doing the job easily

substr_replace(API_USER],"xxxxxxxxxxxx",6,12); //this will replace 12 characters starting from 6th position

substr_replace(API_PASS,"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",6,28); //this will replace 28 characters starting from 6th position of the string

 

Some WordPress Tricks

Redirect some or users to a particular URL after logging in

function login_redirect( $redirect_to, $request, $user ) 
{ 
   return ( is_array( $user->roles ) && in_array( 'sln_staff', $user->roles ) ) ? "https://mydevfactory.com/~appfactory/salonBooking/wp-admin/admin.php?page=salon" : admin_url(); /* here sln_staff is the admin group for which a redirect will happen after logging in */
}

add_filter( 'login_redirect', 'login_redirect', 10, 3 );

 

Remove the Black Admin bar that appears on top of site when the user is logged in

function remove_admin_bar() 
{ 
   show_admin_bar(false);
}
add_action('after_setup_theme', 'remove_admin_bar');

Remove the WordPress update notice on top of the Dashboard

function hide_update_notice_to_all_but_admin_users() 
{ 
   if (!current_user_can('update_core')) { 
     remove_action( 'admin_notices', 'update_nag', 3 ); 
   } 
   $user = wp_get_current_user(); 
   /*echo ($user['roles'][0]); 
   if(in_array( 'sln_staff', $user->roles )) 
     echo "SLN_STAFF"; 
   else 
     echo "WHOEVER";*/
}

add_action( 'admin_head', 'hide_update_notice_to_all_but_admin_users', 1 );

 

Remove the Dashboard items using the Screen Options and then remove the Screen Options and Help tab

function remove_help_tabs($old_help, $screen_id, $screen)
{ 
   $screen->remove_help_tabs(); 
   return $old_help;
}

add_filter( 'contextual_help', 'remove_help_tabs', 999, 3 );
add_filter('screen_options_show_screen', '__return_false');

 

Amazon AWS SDK Passing Credentials

As per the the sample codes the AWS API access key and secret is to be stored in the a file called credentials under a folder named .aws 

The location of that .aws folder can be set in the code through this config statement

putenv('HOME=/path/that/you/want/to/set');

But I am more comfortable in passing the credentials through my code so that I can make things more flexible and need not worry about write permissions.

To pass the credentials through code the first thing needed is to remove or not to use the config statement

'profile' => 'default'

The credentials can then be passed like below

$xxxxxxClient = new xxxxxxClient([
   'version' => "<CLIENT VERSION>", /* can be set to 'latest' */
   'region' => "<REGION>",
   'credentials' => array(
     'key' => "<API USER>",
     'secret' => "<API PASS>"
    )
]);

 

RTMP and audio broadcasting — DRAFT

FFMPEG Commands

  1. https://gist.github.com/protrolium/e0dbd4bb0f1a396fcb55
  2. https://ffmpeg.org/ffmpeg-formats.html#toc-mov_002c-mp4_002c-ismv
  3. http://www.mediaentertainmentinfo.com/2015/06/5-technical-series-handy-ffmpeg-commands-to-get-your-video-processing-done.html/
  4. https://github.com/arut/nginx-rtmp-module/wiki/Examples
  5. https://github.com/arut/nginx-rtmp-module/wiki/Directives#exec_options

 

FFMPEG RTSP Commands

  1. https://trac.ffmpeg.org/wiki/StreamingGuide
  2. https://stackoverflow.com/questions/16658873/how-to-minimize-the-delay-in-a-live-streaming-with-ffmpeg

 

FFMPEG Latency

  1. https://www.wowza.com/blog/streaming-protocols-latency
  2. https://www.wowza.com/docs/how-to-set-up-low-latency-applications-in-wowza-streaming-engine-for-rtmp-streaming

 

Installing Amazon SDK on ISPConfig systems

  1. First install Composer and PHP for the jailed user. This article has the steps
  2. composer require aws/aws-sdk-php  — as the jailed user
  3. There might be an error –
    fopen(/usr/share/php/Composer/Autoload/../../../doc/composer/copyright): failed to open stream: No such file or directory
  4. You can copy the file using the root login
    1. mkdir -p /var/www/clients/client1/web1/usr/share/doc/composer
    2. cp /usr/share/doc/composer/copyright  /var/www/clients/client1/web1/usr/share/doc/composer/
    3. chown web1:client1 /var/www/clients/client1/web1/usr/share/doc/composer/copyright

ISPConfig 3.1 Automated Installation

This article is about Automated Installation of ISPConfig3.1 on Ubuntu 18.04 and the issue with Roundcube.

The installation steps can be found here and very well laid down – https://www.howtoforge.com/tutorial/ubuntu-ispconfig-automated-install-script/

I installed it on a Linode instance (the $10 instance).

The first thing I found is the softwares is better updated manually prior to the automated installation of the ISPConfig. In my case the installer script couldn’t install the updates.

The biggest problem I faced was after the installation Roundcube was neither sending or receiving any mails. Though the mailbox is opening and I can compose mails but on sending the mail it failed with a “Timed Out” error. Neither any incoming mails were received.

Upon searching and going through lots of suggestions (even a re-installation of Roundcube) ultimately found the issue was with two lines in the postfix master file (marked with red color and bold below)

#
# Postfix master process configuration file.  For details on the format
# of the file, see the master(5) manual page (command: "man 5 master").
#
# Do not forget to execute "postfix reload" after editing this file.
#
# ==========================================================================
# service type  private unpriv  chroot  wakeup  maxproc command + args
#               (yes)   (yes)   (yes)   (never) (100)
# ==========================================================================
smtp      inet  n       -       -       -       -       smtpd
#smtp      inet  n       -       -       -       1       postscreen
#smtpd     pass  -       -       -       -       -       smtpd
#dnsblog   unix  -       -       -       -       0       dnsblog
#tlsproxy  unix  -       -       -       -       0       tlsproxy
#submission inet n       -       -       -       -       smtpd
  -o syslog_name=postfix/submission
  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_reject_unlisted_recipient=no
#  -o smtpd_client_restrictions=$mua_client_restrictions
#  -o smtpd_helo_restrictions=$mua_helo_restrictions
#  -o smtpd_sender_restrictions=$mua_sender_restrictions
#  -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
#  -o milter_macro_daemon_name=ORIGINATING
#smtps     inet  n       -       -       -       -       smtpd
  -o syslog_name=postfix/smtps
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_reject_unlisted_recipient=no
#  -o smtpd_client_restrictions=$mua_client_restrictions
#  -o smtpd_helo_restrictions=$mua_helo_restrictions
#  -o smtpd_sender_restrictions=$mua_sender_restrictions
#  -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
#  -o milter_macro_daemon_name=ORIGINATING

The # infront of the submission and smtps needs to be removed.

Thanks to Till and this post on HowtoForge


One more important issue was this line in the main.cf file

mydestination = domain-name.com, localhost, localhost.localdomain

Though by default the domain name was added to the config but it prevented mails from being received. After removing the domain name and leaving only the localhost entries everything worked fine


While trying to fix the problem I found in the process – for ISPConfig based setup it is better to leave the SSL Certificate settings that the script has set. I tried changing to a purchased SSL and incoming mails stopped. It was about 4:30 AM in the morning and was too tired to do anything more. So reverted back to the original files and everything was fine.


While trying different possible solutions I had enabled the necessary ports in UFW. So is  not sure if that is really needed or not. But worth giving a try if mails are not delivered or received even after correcting the settings in the /etc/postfix/master.cf file


Roundcube errors can be found here – /var/log/roundcube/errors

DNS Records and Godaddy

I have no idea why Godaddy have to make things difficult by trying to be simple (different).

  1. Whenever the main domain needs to be referred @ is needed and a must. I had used “domian.com.” but it didn’t work – the DNS records were not propagating
  2. Where there is a subdomain, the domain should not be a part of the Host Entry – that is a must. For example – “mail.domain.com.” should be written as “mail” only.
  3. Similarly when referring to the main domain @ is needed
  4. For reference below are some examples
a 	@ 	17x.xxx.xxx.xx5
a 	mail 	17x.xxx.xxx.xx5
cname 	www 	@
mx 	@ 	mail.gxxxxxxxxxxxxxl.com (Priority: 10)
txt 	@ 	v=spf1 mx a ~all
txt 	_dmarc 	v=DMARC1; p=quarantine  # the quarantine or reject is needed.
txt 	default._domainkey 	v=DKIM1; t=s; p=MIxxxxxxxxxxxxw0xxQE


It needs the name of two nameservers with the same domain name for setting custom Nameservers.