Thursday, November 30, 2023

They all need to go after 7th of October

 


Tuesday, November 28, 2023

List of software and apps that does not require registration and just works

List of software and apps that does not require registration and just works out of the box without too many ads and without need to registrations. The list of applications for Android and Linux desktop that just do the work they need to do. Nothing more. Nothing less.


  1. K-9 Email Mail for Android
  2. Otomusic - music mp3 player application for Android

 

 

Wednesday, November 15, 2023

Writing a json to smtp email proxy in go (golang) json2smtp

I wrote a json2smtp proxy server that can be self hosted in Go (golang)


See here:

https://www.c2kb.com/json2smtp

https://github.com/caviv/json2smtp

 

The reason why I needed to write an email proxy was because I was called to do some maintenance on an old legacy system. This system was running on Windows Server 2012, with php version 5.5.6 and Apache 2.4. Very old versions of software. The system was sending emails using the php smtp package and old libssl.dll which supports only TLSv1.0. The smtp service they were using (AWS SES Simple Email Service) has recently dropped the support for SMTP connection via TLS version 1.0. Now in order to support TLS v1.2 for emails on smtp I have to upgrade the whole system. Windows version, php version, Apache version and more - this would have taken me a very long time with many obstacles on the way.



The solution I have chosen was to write this proxy, so calls from the php to send email will be sent as JSON object with attachments to the json2smtp proxy and from there will be sent to the smtp server using TLS 1.2

I have checked some other options that were recommended by Chat-GPT and Bard. But all of them were actually much more complicated to implement than writing my own solution which took me less than a few hours to code. If you know other similar tools and services please let me know.

Here are the tools that did not work for me:
MailSlurp is a cloud-based email proxy that allows you to receive emails in JSON format. It also provides a number of other features, such as the ability to create and manage email addresses, send emails, and track email activity. | This is an online service that costs money.
SMTP2GO is a cloud-based email service that provides a variety of features, including email proxying. It allows you to receive emails in JSON format and send emails through its own SMTP servers.  | This is an online service that costs money.
Mailgun is another cloud-based email service that provides email proxying. It allows you to receive emails in JSON format and send emails through its own SMTP servers.   | This is an online service that costs money.
SendGrid is a cloud-based email service that provides email proxying. It allows you to receive emails in JSON format and send emails through its own SMTP servers.   | This is an online service that costs money.

In addition to these cloud-based services, there are also a number of self-hosted email proxy tools that can be used. Some of these tools include:
Postfix is a popular open-source mail server that can be used as an email proxy. It can be configured to receive emails in JSON format and send emails through its own SMTP servers. | might be ok but very hard to configure.
Exim is another popular open-source mail server that can be used as an email proxy. It can be configured to receive emails in JSON format and send emails through its own SMTP servers.  | might be ok but very hard to configure.
Mailcow Dockerized is a Docker image that contains a number of email server components, including Postfix, Dovecot, and MySQL. It can be configured to receive emails in JSON format and send emails through its own SMTP servers.   | Couldn't make it work

An email proxy: input: json, output: smtp call

For a legacy project I needed to have a proxy email that reads json input and execute a smtp calls in order to send emails. So I created a small proxy for emails in go (golang)
See the code on github: https://github.com/caviv/json2smtp

Download json2smtp binaries executables

Ubuntu linux: json2smtp
Windows amd64: json2smtp-amd64.exe

How it works:

Simple calling diagram:
Simple architecture of calling the json2smtp email proxy server with json and smtp calls

The json struct object

Simple object:

curl -X POST \
 -H "Content-Type: application/json" \
 -d '{ \
"from": "john doe <john@example.com&rt;", \
"to": ["kermit@muppets.com", "oneperson@example.com"], \
"cc": ["email1@example.com"], \
"bcc": ["secret@example.com"], \
"subject": "email subject line", \
"message": "message body in text/html to be sent", \
"attachments": {"filename.pdf": "base64 file encoded", 
                "anotherfilename.txt": "base64 file encoded"}, \
 }' \
 http://localhost:8080/
		

Full object with smtp data:

curl -X POST \
 -H "Content-Type: application/json" \
 -d '{ \
"from": "john doe <john@example.com&rt;", \
"to": ["kermit@muppets.com", "oneperson@ex", \
"to": ["kermit@muppets.com", "oneperson@example.com"], \
"cc": ["email1@example.com"], \
"bcc": ["secret@example.com"], \
"subject": "email subject line", \
"message": "message body in text/html to be sent", \
"attachments": {"filename.pdf": "base64 file encoded", 
                "anotherfilename.txt": "base64 file encoded"}, \
"smtphost": "smtp.example.com - optional parameter", \
"smtpport": 587 - optional paramater, \
"smtpuser": "username - optional parameter", \
"smtppassword": "password - optional parameter" \
 }' \
 http://localhost:8080/
		

Attachments

In order to send attachments with your json email struct you need to construct an object of base64 encoded string of your binary file.

How to install email proxy server:

Download the code and run it

git clone https://github.com/caviv/json2smtp.git
go run ./
go run ./ --help
	

Build and compile

Download the code compile it and run with help command

git clone https://github.com/caviv/json2smtp.git
go build ./
./json2smtp --help
		

Execute smtp2json emal proxy and samples

Command line help:

json2smtp utility https://www.c2kb.com/json2smtp v1.0.1 2023-11-13
Get json input and calls smtp - function as a json to smtp proxy
Options:
  -port int
    	the port to listen on (default 8080)
  -smtphost string
    	smtp host, e.g. smtp.example.com
  -smtpoverride
    	true - allows to pass smtp parameters in the json call,
    	       false will always use the config smtp data (default true)
  -smtppassword string
    	password for the smtp user
  -smtpport int
    	the port to listen on (default 587)
  -smtpuser string
    	username for the smtp
		

Example:

json2smtp --port=8200 --smtphost='smtp.example.com' 
          --smtpport=587 --smtpuser='username' 
          --smtppassword='password'
          --smtpoverride=false
	

Run service in the background:

nohup json2smtp --port=8200 --smtphost='smtp.example.com'
                --smtpport=587 --smtpuser='username'
                 --smtppassword='password' >> logfile.log 2>&1 &
	

Simple execute smtp2json service:

In this way the calling client will have to pass the smtp server details in each call because we don't set the smtp default server for the proxy. The default port to listen on is 8080.

json2smtp
		

Recommended architecture to run the service:

Calling json2smtp proxy behind a caddy web server for https / tls

Libraries used

This external libraries are used in the project:

require gopkg.in/mail.v2 v2.3.1
require gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0

Sunday, November 5, 2023

Taking a screenshot and creating thumbnail of webpage html as an image via command line

There are a few options to take screenshot of a webpage or save a webpage as an image on the disk via the command line. This can allow you to create a thumbnail of a web site easily by one command on the terminal.

 

Using chrome to save a webpage as an image:

chrome --screenshot=google.png --headless --window-size=1024,768 https://www.google.com/ 

 

Using cutycapt to screenshot a webpage:

https://cutycapt.sourceforge.net/

 

Using webkit2png to create site thumbnail:

https://paulhammond.org/webkit2png

 

You can also use Midori browser



Tuesday, October 17, 2023

Linux Ubuntu Installing and Using Fake Camera Stream

If you wish to create a virtual camera in Linux and stream "fake" videos during calls:


To install the loopback camera:

sudo apt update sudo apt install v4l2loopback-dkms v4l2loopback-utils

To add the loopback camera module:

sudo modprobe v4l2loopback card_label="My Fake Webcam" exclusive_caps=1

or

sudo modprobe v4l2loopback devices=1 video_nr=1 card_label='MyWebCam' exclusive_caps=1

Now to stream the video:

First find the list of videos device available:

v4l2-ctl --list-devices



You can check the videos by playing:

ffplay /dev/videoX ffplay Rick_Astley_Never_Gonna_Give_You_Up.mpg

Now you can stream the video to the free one:

ffmpeg -re -i Rick_Astley_Never_Gonna_Give_You_Up.mpg -map 0:v -f v4l2 /dev/videoX

Or

ffmpeg -stream_loop -1 -re -i video.mp4 -vcodec rawvideo -threads 0 -f v4l2 /dev/videoX

To remove the module from the kernel:

sudo modprobe --remove v4l2loopback

Software for streaming:

sudo apt-add-repository ppa:obsproject/obs-studio
sudo apt-get update
sudo apt-get install ffmpeg obs-studio

Monday, July 31, 2023

Go (golang) howto not to export fields of a struct when Unmarshal to json object

Use:

typedef MyData struct {

    Data            string    `json:"data"`   

    FieldName   *int64   `json:"-"` 

}

Adding the `json:"-"` will make the Unmarshal command disregard this field

Saturday, July 22, 2023

Cleaning and resizing VM VirtualBox VDI Virtual Machine Disk

 You have to do the following steps:

Run defrag in the guest (Windows only)
Nullify free space:

With a Linux Guest run this:

dd if=/dev/zero of=/var/tmp/bigemptyfile bs=4096k ; rm /var/tmp/bigemptyfile
Or:

telinit 1
mount -o remount,ro /dev/sda1
zerofree -v /dev/sda1
With a Windows Guest, download SDelete from Sysinternals and run this:

sdelete.exe c: -z
(replace C: with the drive letter of the VDI)

Shutdown the guest VM

Now run VBoxManage's modifymedium command with the --compact option:

With a Linux Host run this:

vboxmanage modifymedium --compact /path/to/thedisk.vdi
With a Windows Host run this:

VBoxManage.exe modifymedium --compact c:\path\to\thedisk.vdi
With a Mac Host run this:

VBoxManage modifymedium --compact /path/to/thedisk.vdi
This reduces the vdi size.

https://superuser.com/questions/529149/how-to-compact-virtualboxs-vdi-file-size

Thursday, May 25, 2023

Some notes about caddy webserver installation on Linux

 

caddy

https://caddyserver.com/docs/install#debian-ubuntu-raspbian

sudo apt update

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https

curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo tee /etc/apt/trusted.gpg.d/caddy-stable.asc

curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list

sudo apt update

sudo apt install caddy


# the code sits in /var/www

# create link to the local directory

ln -s /var/www/ /home/xxx/myprojects/web


# set the user to be xxx and the group caddy

sudo chown -R xxx:caddy ./www  


# add us to caddy groups

sudo usermod -a -G caddy xxx

sudo usermod -a -G www-data xxx


Install php-fpm

sudo apt install php-cli php-fpm php-mysql php-mbstring php-curl

https://www.howtoforge.com/tutorial/ubuntu-caddy-web-server-installation/


configure caddy with php

sudo nano /etc/php/8.1/fpm/pool.d/www.conf

#change users to caddy (user, group, listen.owner, listen.group)

#take the socket name: listen = /run/php/php8.1-fpm.sock

nano php.ini # set E_ALL view all errors

sudo systemctl restart php8.1-fpm

#set socket in Caddyfile: php_fastcgi unix//run/php/php8.1-fpm.sock

sudo service caddy stop && sudo service caddy start

 

Caddyfile
website.com {
#:80 {
tls /var/www/fullchain.pem /var/www/privkey.pem

# Set this path to your site's directory.
root * /var/www/website.com
php_fastcgi unix//run/php/php7.4-fpm.sock

# Enable the static file server.
# file_server
rewrite /symbol/* s.php?s={path}
# rewrite /symbol/ {
# r ^/(\w+)/?$
# to /symbol.php?symbol={1}
# }
file_server

encode gzip

log {
output file /var/log/caddy/example.com.access.log {
roll_size 3MiB
roll_keep 5
roll_keep_for 48h
}
format console
}
# Another common task is to set up a reverse proxy:
# reverse_proxy localhost:8080

# Or serve a PHP site through php-fpm:
# php_fastcgi localhost:9000

#RewriteRule ^symbol/jscss/([^/]*)\.([^/]*)$ /jscss/$1.$2 [L] # no need for those since we used <base href="../" />
#RewriteRule ^symbol/images/([^/]*)\.([^/]*)$ /images/$1.$2 [L]
#rewrite /symbol/* / symbol.php?symbol={query}

 

 

I had the same problem whith my docker php-fpm and I fixed it by modifing parameters

 in these files: /usr/local/etc/php-fpm.d/www.conf /usr/local/etc/php-fpm.d/www.conf.default

The new parameters are :

pm = dynamic
pm.max_children = 25
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
pm.max_requests = 500

 

 

- ADD how to enabled logs in PHP 

 

Monday, February 6, 2023

Linux terminal shell command line to show a list of just the files in the directory

Linux terminal shell command line to show a list of just the files in the directory

find . -type f
 

Wednesday, February 1, 2023

php8 COM Exception Type Mistmtach in paramater #0 when calling MS Word COM object SaveAs

php8 COM Exception Type Mistmtach in paramater #0 when calling MS Word COM object SaveAs

I saw this:

https://bugs.php.net/bug.php?id=73605

 

When calling $word->SaveAs($filename, 8);

You should change to $word->SaveAs($filename, new variant(8, VT_I4);

 

 

php 8 using ldap connectivity configuring php.ini and apache2.4

Got an error that ldap_connect is not a recognize function to use with apache 2.4, php 8 on windows


Here is a solution:

1.

Enable extension=ldap in the php.ini


2.

Copy .dll files from the php installation directory into the apache/bin installation directory


Please note, ldap_sort function was deprecated

Monday, January 16, 2023

Linux Ubuntu Changing the datair of mysql

 

mysqld.cnf

datadir = /new/data/dir

mysqld --user=mysql --initialize

innodb_flush_log_at_trx_commit= 0
ssl=0 #  (5.7 community (self built) might not use SSL for cases. 8.0 always use ssl by default, might be slower)
innodb_doublewrite_pages=120 # (if doublewrite enabled for recent 8.0.x "on HDD")
innodb_undo_log_truncate=OFF
innodb_log_writer_threads=OFF
 

plugin-load-add=auth_socket.so
auth_socket=FORCE_PLUS_PERMANENT

 

sudo systemctl stop apparmor

sudo systemctl disable apparmor



sudo mysqldump --master-data=1 --flush-logs --single-transaction --routines --quick --all-databases > db20230116_1852.sql

CREATE USER 'XXXXX'@'%' IDENTIFIED WITH mysql_native_password BY 'XXXXXX';
GRANT ALL PRIVILEGES ON *.* TO 'XXXXX'@'%' WITH GRANT OPTION;
FLUSH PRIVILIGES;

 

Saturday, January 14, 2023

Monday, December 12, 2022

Enabling php error logging with caddy and php-fpm

How to enable php error logging with caddy and php-fpm

Inside the file:

pool.d/www.conf


Edit at the end:

php_admin_value[error_log] = /var/log/fpm-php.www.log
php_admin_flag[log_errors] = on

catch_workers_output = yes

Saturday, November 19, 2022

Script for creating html bookmarks file for Chrome to import

#!/bin/bash
#
# Run this script on a file named urls.txt with all your URLs and pipe the output to an HTML file.
# Example: ./convert_url_file.sh > bookmarks.html

echo "<!DOCTYPE NETSCAPE-Bookmark-file-1>"
echo '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">'
echo '<TITLE>Bookmarks</TITLE>'
echo '<H1>Bookmarks</H1>'
echo '<DL><p>'
  cat urls.txt |
  while read L; do
    echo -n '    <DT><A HREF="';
        echo ''"$L"'">'"$L"'</A>';
  done
echo "</DL><p>"

 

 

Monday, July 11, 2022

Simple Using SQUID proxy server for Ubuntu 20.04

sudo apt install squid

sudo cp /etc/squid/squid.conf /etc/squid/squid.conf.original
sudo chmod a-w /etc/squid/squid.conf.original

sudo nano /etc/squid/squid.conf
 

inside squid.conf
http_port XXXXX
http_access allow all

sudo update-rc.d squid disable
sudo systemctl restart squid.service
sudo systemctl status squid
sudo systemctl disable squid
sudo systemctl stop squid
 

Saturday, June 25, 2022

The right way to use rsync

 rsync -avW -LK --progress ./from /to

 

Enable snap mysql-workbench to have access to the network

 sudo snap connect mysql-workbench-community:password-manager-service :password-manager-service

Saturday, March 19, 2022

Making sure my linux system does not wake up on suspend

Had this annoying problem in my linux Xubuntu 20.04 when putting on suspend the system then starts even though I put it in suspend and sleep.

Using acpitool to turn off wake-up from unnecessary devices.

But the problem the change is not permanent and does not survive a restart.

So I used cron @reboot to make sure it is always off:

@reboot acpitool -W 3 && acpitool -W 9 && acpitool -W 10 && acpitool -W 17 && acpitool -W 63 && acpitool -W 64 && acpitool -W 66 && acpitool -W 68 && acpitool -W 74
 

 


Thursday, March 17, 2022

Saturday, March 5, 2022

MySQL Test Timezone Script

 CREATE TABLE tzt(
id MEDIUMINT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
ts TIMESTAMP NOT NULL,
dt DATETIME NOT NULL
);

SELECT @@global.time_zone, @@session.time_zone;


SET GLOBAL time_zone = 'Asia/Jerusalem';
SET SESSION time_zone = 'Asia/Jerusalem';

INSERT INTO tzt SET ts='2021-08-22 13:30:00', dt='2021-08-22 13:30:00';

SELECT * FROM tzt\G

SET GLOBAL time_zone = 'America/Sao_Paulo';
SET SESSION time_zone = 'America/Sao_Paulo';


SELECT * FROM tzt\G

Monday, January 10, 2022

Connecting to mysql with TLS 1.2 SSL encrypted connection

In order to be able to connect to MySQL remote server using encrypted connection overt TLS1.2 you will need a .pem certificate of CA.

 You can obtain the certificate here: https://dl.cacerts.digicert.com/DigiCertGlobalRootCA.crt.pem

(DigiCertGlobalRootCA.crt.pem)

Read more here:

https://docs.microsoft.com/en-us/azure/mysql/flexible-server/how-to-connect-tls-ssl

Here is a PHP sample for connection with SSL to MySQL encrypted connection on TLS1.2:

self::$mysqli = mysqli_init();
//self::$mysqli->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);
self::$mysqli->ssl_set(NULL, NULL, "DigiCertGlobalRootCA.crt.pem", NULL, NULL);
self::$mysqli->real_connect(host', 'username', 'password', 'database'); // , 3306, MYSQLI_CLIENT_SSL);

//self::$mysqli = new mysqli('hots', 'username', 'password', 'database');   
self::$mysqli->set_charset('utf8');
return self::$mysqli;

Here is the full certificate for MySQL secure connection

    -----BEGIN CERTIFICATE-----
MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD
QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT
MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j
b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IENBMIIBIjANBgkqhkiG
9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4jvhEXLeqKTTo1eqUKKPC3eQyaKl7hLOllsB
CSDMAZOnTjC3U/dDxGkAV53ijSLdhwZAAIEJzs4bg7/fzTtxRuLWZscFs3YnFo97
nh6Vfe63SKMI2tavegw5BmV/Sl0fvBf4q77uKNd0f3p4mVmFaG5cIzJLv07A6Fpt
43C/dxC//AH2hdmoRBBYMql1GNXRor5H4idq9Joz+EkIYIvUX7Q6hL+hqkpMfT7P
T19sdl6gSzeRntwi5m3OFBqOasv+zbMUZBfHWymeMr/y7vrTC0LUq7dBMtoM1O/4
gdW7jVg/tRvoSSiicNoxBN33shbyTApOB6jtSj1etX+jkMOvJwIDAQABo2MwYTAO
BgNVHQ8BAf8EBAMCAYYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUA95QNVbR
TLtm8KPiGxvDl7I90VUwHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUw
DQYJKoZIhvcNAQEFBQADggEBAMucN6pIExIK+t1EnE9SsPTfrgT1eXkIoyQY/Esr
hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg
06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF
PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls
YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk
CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=
-----END CERTIFICATE-----
  

Saturday, November 20, 2021

Some pitfalls for using PWA and ServiceWorkers I hoped I knew before

Some pitfalls for using PWA and ServiceWorker I hoped I knew before - So I am writing them here so maybe others will get to rid them before or I can find them again when I'll need them again.

This is not a tutorial for PWA, ServiceWorker. Those are just a few points that I encountered while developing a PWA (Progressive Web App) and ServiceWorker using Vanilla JavaScript and caching mechanism for Offline usage. There are plenty of good PWA ServiceWorker Caching tutorial on the internet.

Full Vanilla JavaScript code is attached all the way at the bottom

Double caches:

First, the cache made by the cache mechanism which you as a coder decide what will be saved in this cache using the cache.addAll() command.

And the second cache is the one the browser automatically caches for example images and javascripts code or others.

Hence might be that when you work offline the browser will return cached images which you did not stated that should be cached only because the browser caching mechanism have cached them.


Cache is empty offline from the website {ignoreVary: true}:

You develop and everything works cool from your localhost server but when deploying and installing on production on your website the offline cache of the serviceworker is empty in the devtools of your Chrome browser. When you work Online you do see the cache.

Now weather I think it appears empty it is a Chrome bug because the cache is there - apparently your code does not recognize the cache using caches.match and tries to request it from the network.

There are two things needed to be done:

  1. Use caches.match(e.request, {ignoreVary: true}) in your code telling the cache match comparing mechanism to ignore the request type and use only the URL itself
  2. When installing the serviceWorker it should be by default working under the same directory it is installed from. Unfortunately it doesn't always work. So when installing the serviceWorker use the scope parameter: navigator.serviceWorker.register('sw1.js', {scope: './'}) 

There can be only one instance of the ServiceWorker for the domain:

So no need to reinstall the worker when you go offline and the list of resources need to be relative to the service worker location:

const includeToCache = [
  './',
  'index.php',
//  'loader.js?20211118a', // the serviceWorker loader
//  'sw1.js', // the serviceWorker itself
]; 


The full code of the serviceWorker

// service worker for PWA loaded from loader.js in pwa.class.php

const cacheName = 'pwa_v10';

const includeToCache = [
  './',
  'index.php',
//  'loader.js?20211118a',
//  'sw1.js',
];

// Start the service worker and cache all of the app's content
self.addEventListener('install', e => {
    self.skipWaiting();
    e.waitUntil(
        caches.open(cacheName).then(cache => {
            let c = cache.addAll(includeToCache).catch(err => console.log('SW install addAll FAIL:', err));
            return c;
        })
    );
});

// Serve cached content when offline
self.addEventListener('fetch', e => {
    e.respondWith(
        caches.match(e.request, {ignoreVary: true}).then(response => {
            return response || fetch(e.request);
        })
    );
});

// replacing new cache if needed
self.addEventListener('activate', e => {
    // delete any caches that aren’t in cacheName
    // which will get rid of older versions
    e.waitUntil(
        caches.keys().then(keys => Promise.all(
            keys.map(key => {
                if(cacheName !== key) {
                    return caches.delete(key);
                }
        }))).then(() => {
            console.log(cacheName + ' now ready to handle fetches!');
        })
    );
});

Wednesday, October 7, 2020

Using phpword to merge two Mircrosoft Office Word .docx documents

How to combine or embed and insert another .docx file (Microsoft office docx word document) into another one using PHPWord

Joining two .docx document using php (phpword library)

$mainTemplateProcessor = new \PhpOffice\PhpWord\TemplateProcessor("file1");
//$mainTemplateProcessor ->setValue('var_name', $value);

$innerTemplateProcessor = new \PhpOffice\PhpWord\TemplateProcessor("file2");
//$innerTemplateProcessor->setValue('var2_name', $value2);

// extract internal xml from template that will be merged inside main template
$innerXml = $innerTemplateProcessor->gettempDocumentMainPart();
$innerXml = preg_replace('/^[\s\S]*<w:body>(.*)<\/w:body>.*/', '$1', $innerXml);

// remove tag containing header, footer, images
$innerXml = preg_replace('/<w:sectPr>.*<\/w:sectPr>/', '', $innerXml);

// inject internal xml inside main template
$mainXml = $mainTemplateProcessor->gettempDocumentMainPart();
$mainXml = preg_replace('/<\/w:body>/', '<w:p><w:r><w:br w:type="page" /><w:lastRenderedPageBreak/></w:r></w:p>' . $innerXml . '</w:body>', $mainXml);
$mainTemplateProcessor->settempDocumentMainPart($mainXml);

$mainTemplateProcessor->saveAs($result_file_name);


In order for the above code to work, you need to modify edit and add to the TemplateProcessor.php file those two functions:

public function gettempDocumentMainPart()
{
    return $this->tempDocumentMainPart;
}

public function settempDocumentMainPart($new)
{
    return $this->tempDocumentMainPart = $new;
}


This answer is based on the answer of @pfleu here: https://github.com/PHPOffice/PHPWord/issues/1130



 

Saturday, September 26, 2020

Teu Sonho - Online Interprete Seus Sonhos (teusonho.org)

Teusonho.org - Online Interprete Seus Sonhos

https://teusonho.org é um software de interpretatação online de sonhos usando IA para análise em tempo real de sonhos repetidos e símbolos de sonhos.I took my website for dream analysis using Artificial Intelligence and Machine Learning that I have created in Hebrew https://dreamon.co.il and also in English https://understandmydreams.com and created a version in Portuguese https://teusonho.org with new dream symbols and new dreams.

Peguei meu site para análise de sonhos usando Inteligência Artificial e Aprendizado de Máquina que criei em hebraico https://dreamon.co.il e também em inglês https://understandmydreams.com e criei uma versão em português https://teusonho.org com novos símbolos de sonho e novos sonhos.

Also on twitter: https://twitter.com/OrgTeusonho

 

Online Interprete Seus Sonhos

Sunday, August 30, 2020

nginx rewrite and redirect rule like .htaccess in apache

In order to rewrite for example:

https://www.teusonho.org/symbol/casa to /symbol.php=casa

You can use:

location /symbol {
         rewrite ^/symbol/([^/\.]*)$ /symbol.php?symbol=$1;
    }

Loading (importing) an .sql mysql dumpfile and showing progress bar

When loading and importing a large .sql mysql dump file and you wish to know how long is it going to take and where does the process status is standing using a progress bar from the mysqldump loading you can us pv (pipe viewer)

For example:

pv sqldumpfile.sql | mysql database name

About pv (pipe viewer) utility for Linux:


Tuesday, May 26, 2020

Connecting to MySQL 8 from Old PHP version using mysqli

If you have upgraded your MySQL to MySQL 8 version and still using older version of PHP you might encounter a few problems in the connection using mysqli connector library.

1.
The first problem we are going to solve is:

php mysql 8: "Server sent charset (255) unknown to the client. Please, report to the developers"

The problem is that MySQL have changed the default character set from utf8 to utf8mb4 and it is not supported by mysqli connector (nor PDO as I have checked) in older versions of php.

"The default collation for utf8mb4 differs between MySQL 5.7 and 8.0 ( utf8mb4_general_ci for 5.7, utf8mb4_0900_ai_ci for 8.0). When the 8.0 client requests a character set of utf8mb4 , what it sends to the server is the default 8.0 utf8mb4 collation; that is, the utf8mb4_0900_ai_ci."

The way to solve it easily (there are other ways, such as upgrading php or connectors and more ...) is to force MySQL 8.0 to use UTF8 as default charset for connection.

Add those lines at the end of the my.cnf configuration file. In Ubunutu 20.04 it will be /etc/mysql/mysql.conf.d/mysqld.cnf

So:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

And add at the end (must be at the end of the file):
[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
collation-server = utf8_unicode_ci
character-set-server = utf8
default_authentication_plugin = mysql_native_password

do not forget to restart the server after:
sudo service mysql restart

2.
Second problem is new plugin for MySQL 8 passwords. Instead of the default mysql_native_password it is now using caching_sha2_password so you will get the error:  mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password]

The last line we added to the my.cnf file should fix it but not entirely. When creating the users inside your database make sure to CREATE USER or ALTER USER to use the mysql_native_password like that:

CREATE USER 'username'@`location` IDENTIFIED WITH mysql_native_password BY '[somepassword]'; 

That solved me some problems.

Monday, May 25, 2020

The way I use mysqldump



sudo mysqldump --master-data=1 --flush-logs --single-transaction --routines --quick --all-databases > db.sql

Friday, May 15, 2020

Simple system to track errors in code and find them easily

Hi,

Here is a trick I developed during my years as a developer / programmer and team code leader. This system not just helps you to find the location of the error or warning in the code, but also who wrote it (without the need to open Git Blame or SVN log) and when was it coded in written.

They say that grep (find in all files) is the best friend of the programmer. Hence if you see an error in text or log file, let's say "Error: can't find configuration file". So you copy the text and "grep" it. It might be you would find this string in a few places scattered around the code.

So, we have started to give some Error Codes to the error. For example "Error: can't find configuration file (1029)". This will make it much easier and this unique error code is easy to search inside all the code files.

So how do you keep all the Error Codes unique ? Maybe a new programmer will add the same code number.

Do you keep a file with all Error Codes ?

Here is my system for assigning error codes:

"Error: some error description (ErrorCode: 20200515.1271726)"

And here is the format of the Error Code

YYYYMMDD.XXXHHII

YYYY, MM, DD - is obvious the date the code was written

HH, II - is the hour and minutes

The XXX is a unique number each of the coder team member identifier.

So let's say a programmer called Dani with code 019 have to add a new log  line with some warning or error. He will add 20200515.0191727 as the Error Code.
Next time I will see this code I can easily 'grep' it and I can also know who wrote this piece of code and when.

Believe me, it helps when the team is large.

Cheers.
Cnaan