PHP Parse HLS M3U8 TS Links

Input

encoded-03-31-16-thu-jun-2017.m3u8

#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=654000,RESOLUTION=568×320,CODECS=”avc1.4d0029,mp4a.40.2″
1080p/encoded-03-31-16-thu-jun-2017.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2849000,RESOLUTION=568×320,CODECS=”avc1.42001f,mp4a.40.2″
720p/encoded-03-31-16-thu-jun-2017.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1033000,RESOLUTION=568×320,CODECS=”avc1.42001e,mp4a.40.2″
360p/encoded-03-31-16-thu-jun-2017.m3u8

encoded-03-31-16-thu-jun-2017.360.m3u8

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-ALLOW-CACHE:YES
#EXT-X-TARGETDURATION:13
#EXT-X-KEY:METHOD=AES-128,URI=”encoded-03-31-16-thu-jun-2017.key”,IV=0x5fd83c919c46159f14ec8da38a4d9ca7
#EXTINF:12.046444,
encoded-03-31-16-thu-jun-201700000.ts
#EXTINF:9.000000,
encoded-03-31-16-thu-jun-201700001.ts
#EXTINF:9.000000,
encoded-03-31-16-thu-jun-201700002.ts
#EXTINF:12.000000,
encoded-03-31-16-thu-jun-201700003.ts
.
.
.
#EXT-X-ENDLIST

Code

<?php

function parseHLS($file, $tag=".ts") {
$return = array();
$i = 0;
$handle = fopen($file, "r");
if($handle) {
while(($line = fgets($handle)) !== FALSE) {
if(strpos($line,"EXT-X-STREAM-INF") !== FALSE) {
if ($c=preg_match_all ("/.*?(BANDWIDTH)(.*?)(,)(RESOLUTION)(.*?)(,)/is", $line, $matches)) {
$return['data'][$i]['bandwidth'] = str_replace("=","",$matches[2][0]);
$return['data'][$i]['resolution'] = str_replace("=","",$matches[5][0]);
}
}

if($tag==".ts") {
if(strpos($line,".ts") !== FALSE) {
$return['data'][$i]['url'] = str_replace(array("\r","\n"),"",$line);
$i++;
}
}

if($tag==".key") {
if(strpos($line,".key") !== FALSE) {  

preg_match( '/URI="([^"]*)"/i', $line, $array );
//print_r( $array[1] ) ;

//$return['data'][$i]['url'] = str_replace(array("\r","\n"),"",$line);
$return['data'][$i]['url'] = str_replace(array("\r","\n"),"",$array[1]);

$i++;
}
}

if($tag==".m3u8") {
if(strpos($line,".m3u8") !== FALSE) {
$return['data'][$i]['url'] = str_replace(array("\r","\n"),"",$line);
$i++;
}
}

}
fclose($handle);
}
return $return;
}


echo "<pre>";

// Get other m3u8 files from m3u8 master manifest 
$u = parseHLS("encoded-03-31-16-thu-jun-2017.m3u8", ".m3u8");

print_r($u);

// Get TS segment files from m3u8 manifest 
$u = parseHLS("encoded-03-31-16-thu-jun-2017.360.m3u8", ".ts");

print_r($u);

// Get key file from m3u8 manifest 
$u = parseHLS("encoded-03-31-16-thu-jun-2017.360.m3u8", ".key");

print_r($u);

echo "</pre>";

?>

Output

Array
(
[data] => Array
(
[0] => Array
(
[bandwidth] => 654000
[resolution] => 568x320
[url] => 1080p/encoded-03-31-16-thu-jun-2017.m3u8
)

[1] => Array
(
[bandwidth] => 2849000
[resolution] => 568x320
[url] => 720p/encoded-03-31-16-thu-jun-2017.m3u8
)

[2] => Array
(
[bandwidth] => 1033000
[resolution] => 568x320
[url] => 360p/encoded-03-31-16-thu-jun-2017.m3u8
)

)

)

Array
(
[data] => Array
(
[0] => Array
(
[url] => encoded-03-31-16-thu-jun-201700000.ts
)

[1] => Array
(
[url] => encoded-03-31-16-thu-jun-201700001.ts
)

[2] => Array
(
[url] => encoded-03-31-16-thu-jun-201700002.ts
)

[3] => Array
(
[url] => encoded-03-31-16-thu-jun-201700003.ts
)
....
.....
)
)

Array
(
[data] => Array
(
[0] => Array
(
[url] => encoded-03-31-16-thu-jun-2017.key
)

)

)

PHP in Amazon Lambda

Clone or Download this GitHub Repo
https://github.com/svnlabs/aws-lambda-php-template

Make new folder lambda in current working directory of EC2 server
# mkdir lambda

Upload 2 files from zip to Amazon EC2 server Instance in folder lambda

lambda
– index.js
– php (binary)

index.js

'use strict';
exports.handler = function(event, context, callback) {
var exec = require('child_process').exec;

  exec('./php -v', function (error, stdout, stderr) {
    if(stdout){
      console.log('stdout: ' + stdout);
    }
    if(stderr){
      console.log('stderr: ' + stderr);
    }
    if (error !== null) {
      console.log('Exec error: ' + error);
    }
    context.succeed(stdout);
    console.log(stdout);
  });
};

Go inside folder lambda

# cd lambda
# zip -r ../lambda.zip *

Copy lambda.zip for AWS S3 bucket

# aws s3 cp ../lambda.zip s3://bucket-name/

How to install AWS CLI?

Create new Lambda Function from AWS Console with NodeJS Runtime*

Name*: phpv
Runtime*: Node.js 4.3
Code entry type: Upload a file from Amazon S3
S3 link URL*: https://s3.amazonaws.com/bucket-name/lambda.zip

Handler*: index.handler
Role*: Lambda Basic Execution

All other info will be default**

Save & Test

“PHP 5.6.5 (cli) (built: Jul 21 2015 18:56:08) \nCopyright (c) 1997-2014 The PHP Group\nZend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies\n”

Free SSL Certbot

Certbot, It was Let’s Encrypt Client previously. Automatically enable HTTPS on your website with EFF’s Certbot, deploying Let’s Encrypt certificates.

Server Support

– Apache
– Nginx
– Haproxy
– Plesk

OS Support

– Web Hosting Service
– Debian 7 (wheezy)
– Debian 8 (jessie)
– Debian 9 (stretch)
– Debian testing/unstable
– Debian (other)
– Ubuntu 17.04 (zesty)
– Ubuntu 16.10 (yakkety)
– Ubuntu 16.04 (xenial)
– Ubuntu 14.04 (trusty)
– Ubuntu (other)
– Gentoo
– Arch Linux
– Fedora 24+
– CentOS 6
– RHEL 6
– CentOS/RHEL 7
– FreeBSD
– OpenBSD 5.9
– OpenBSD 6.0+
– OpenBSD (other)
– macOS
– Devuan Jessie 1.0
– Devuan (other)
– Other UNIX
– Non-UNIX

Install on Ubuntu 17.04 (zesty) Apache

$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install python-certbot-apache

Get Started https://certbot.eff.org/#ubuntutzesty-apache

$ sudo certbot --apache

$ sudo certbot --apache certonly

Automating renewal

$ sudo certbot renew --dry-run

$ sudo crontab -e

30 1 * * * /usr/bin/certbot renew --quiet

It will run renewal command at 1:30 am, every day.

PHP Warning: PHP Startup: Unable to load dynamic library /usr/lib64/php/ modules/zip.so

CentOS 7 with PHP

# php --ini
PHP Warning: PHP Startup: Unable to load dynamic library ‘/usr/lib64/php/modules/zip.so’ – /usr/lib64/php/modules/zip.so: undefined symbol: php_pcre_exec in Unknown on line 0
Configuration File (php.ini) Path: /etc
Loaded Configuration File: /etc/php.ini
Scan for additional .ini files in: /etc/php.d
Additional .ini files parsed: /etc/php.d/10-opcache.ini,
/etc/php.d/20-bcmath.ini,
/etc/php.d/20-bz2.ini,
/etc/php.d/20-calendar.ini,
.
.
.

Install Apache, MySQL, PHP 5.6 on Centos 7

Solution

# yum install php-zip

# service httpd restart
Redirecting to /bin/systemctl restart httpd.service

PHP Check Loaded Modules

# php -m
[PHP Modules]
apc
apcu
bcmath
bz2
calendar
Core
ctype
curl
date
dom
ereg
exif
fileinfo
filter
ftp
gd
gettext
gmp
hash
iconv
igbinary
json
libxml
mbstring
mcrypt
memcache
memcached
mhash
mongo
msgpack
mysql
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_pgsql
pdo_sqlite
pgsql
Phar
posix
readline
recode
redis
Reflection
session
shmop
SimpleXML
sockets
SPL
SQLite
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tidy
tokenizer
wddx
xml
xmlreader
xmlwriter
xsl
Zend OPcache
zip
zlib

[Zend Modules]
Zend OPcache

PHP Check Loaded ini files
# php --ini

# php -v
PHP 5.6.31 (cli) (built: Jul 6 2017 08:06:11)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

Install Apache, MySQL, PHP 5.6 on Centos 7

Install Apache

# yum update
# yum install httpd
# systemctl start httpd.service

Now Browser your Apache Server http://Server_IP_address/

Enable Apache to start at server boot

# systemctl enable httpd.service

Install Mariadb MySQL

# yum install mariadb-server mariadb
# systemctl start mariadb
# mysql_secure_installation

Enable MySQL to start at server boot

# systemctl enable mariadb.service

Install PHP 5.6

Install EPEL repository

# rpm -Uvh http://vault.centos.org/7.0.1406/extras/x86_64/Packages/epel-release-7-5.noarch.rpm

Note: Make sure you installed “epel-release-7-5.noarch.rpm” else google it for different location …. search on Google “index of” + epel-release-7-5.noarch.rpm

Install remi repository

# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

Note: Make sure you installed “remi-release-7.rpm” else google it for different location

Enable remi

# yum --enablerepo=remi,remi-php56 install php php-common

Install php 5.6 on Centos 7

# yum --enablerepo=remi,remi-php56 install php-cli php-pear php-pdo php-mysql php-mysqlnd php-pgsql php-sqlite php-gd php-mbstring php-mcrypt php-xml php-simplexml php-curl php-zip

# systemctl restart httpd.service
# chown -R apache:apache /var/www/html/

# chmod -R 775 /var/www/

<?php phpinfo(); ?>

http://Server_IP_address/phpinfo.php

CentOS 7 + SELinux + Apache + PHP write/access permission

CentOS 7 have SELinux, it is security enhancement to Linux which allows users more control over access control.

SELinux make problem to access or write files or directories

– Can’t serve files on directory
– Can’t write to file

SELinux is blocking the read/write operations

# chcon -Rv --type=httpd_sys_rw_content_t /var/www/html/ (if you want to allow full web root)

# chcon -Rv --type=httpd_sys_rw_content_t /var/www/html/uploads/ (Or just uploads folder)

Ownership
# chown apache:apache -R /var/www/html/

Recursive File permissions
# find . -type f -exec chmod 0644 {} \;

Recursive Directory permissions
# find . -type d -exec chmod 0755 {} \;

Resursive SELinux serve files off Apache
# chcon -t httpd_sys_content_t /var/www/html/ -R

Allow write to specific directories
# chcon -t httpd_sys_rw_content_t /var/www/html/logs/ -R
# chcon -t httpd_sys_rw_content_t /var/www/html/uploads/ -R

httpd_sys_content_t – allow Apache to serve contents
httpd_sys_rw_content_t – allow Apache to write to those path.

Facebook Delight Text Keywords

Facebook Delight Text display celebration balloons on Facebook Page / Post. Facebook have selected Greeting Text Keywords from different languages. Facebook Delight Text give celebration feel to Facebook Users.

Keywords:[“Congrats”,”تهانينا”,”مبروك”,”ألف مبروك”,”مبارك”,”Glückwunsch”,”Gratuliere”,”Enhorabuena”,”Felicitaciones”,”Félicitations”,”बधाई”,”Selamat”,”Congratulazioni”,”おめでとう”,”축하해”,”축하해요”,”Мои поздравления”,”Поздравляем”,”Поздравляю”,”ยินดีด้วย”,”Tebrikler”,”恭喜”,”Geluk”,”অভিনন্দন”,”Gratulace!”,”Til lykke”,”Tillykke”,”Συγχαρητήρια”,”Paljon onnea”,”Onnea”,”અભિનંદન”,”מזל טוב”,”Gratula”,”Čestitam”,”Čestitke”,”Čestitamo”,”അഭിനന്ദനങ്ങൾ”,”ಶುಭಾಶಯ”,”Tahniah”,”Grattis”,”अभिनंदन”,”ਵਧਾਈਆਂ”,”Parabéns”,”Gratki”,”Gratulacje”,”Felicitări!”,”Gratulujem”,”Blahoželám”,”Gefeliciteerd”,”வாழ்த்துகள்”,”வாழ்த்துக்கள்”,”అభినందనలు”,”مبارکباد”,”Chúc mừng”,”Честитам”,”Hongera”]

What is a Self-XSS scam?

A Self-XSS scam usually works by promising to help you access somebody else’s account. Instead, the scammer tricks you into gaining access to your account for fraud, spam and tricking more people into a scam.
Scammers will usually target your friends by posting to your Timeline.
To avoid Self-XSS attacks, never copy and paste suspicious links.
(Source: Facebook)

This is a browser feature intended for developers. If someone told you to copy-paste something here to enable a Facebook feature or “hack” someone’s account, it is a scam and will give them access to your Facebook account.

ZRUYR1S8XUa.js: https://www.facebook.com/rsrc.php/v3/y0/r/ZRUYR1S8XUa.js

See https://www.facebook.com/selfxss for more information.
(Source: Chrome Console)

Amazon DynamoDB Formatted JSON using JQ

Linux sed is useful tool to format and transform plain text. But sed is not so useful for structured data like JSON. JQ is a sed-like tool easily deal with JSON.

./jq

is a lightweight and flexible command-line JSON processor.

Install jq on OS X:
brew install jq

Install jq on Ubuntu:
apt-get install jq

Others:
https://stedolan.github.io/jq/download/

UserPoints.json data exported from Amazon DynamoDB

# AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXXXXXXXXX AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX /usr/local/bin/aws dynamodb scan –table-name UserPoints –region us-east-1 –output json > UserPoints.json

# jq “[.Items[] | { userId: .userId.S, CompanyCode: .CompanyCode.S, date: .date.S, points: .points.S, Id: .Id.S }]” UserPoints.json

It will output like this…

[
{
“userId”: “user1@gmail.com”,
“CompanyCode”: “ABC107”,
“date”: “2017/04/08”,
“point”: “16”,
“Id”: “1496238999”
},
{
“userId”: “user2@gmail.com”,
“CompanyCode”: “ABC107”,
“date”: “2017/02/07”,
“point”: “45”,
“Id”: “1496239218”
}
]

Access Skype SQLite Database

I was working for Online Radio Server setup with Icecast and Skype. This Radio Station was using Skype Group Calls with real listeners of station then I have to feed Live Skype Audio to Icecast Radio Server as Input.

Radio Setup Tools Online Radio Schedule with Skype & Icecast

– SAM Broadcaster
– Icecast Server
– Skype account as Radio Station
– Virtual Audio Cable to manage Icecast / Skype Patch

Radio Listeners normally connect to Radio Server Skype from Website. Listeners can send contact request to Skype then Radio Server Manager Schedule some calls based on Radio Schedule. Radio Server Skype Auto Connect new Skype requests

Radio Station with Icecast Skype Features

– Listeners can chat live on Skype
– Selected Listeners can participate in Live Talk

Now I need to show real status of Radio Server Skype on the Radio Station Website??

I know Skype use SQLite to save Database on computer here “C:/Users/[Radio-Server]/AppData/Roaming/Skype/[Radio-Skype]/main.db”

So, I just have query some tables to get REAL Status of Radio Server Skype

– Radio Server Skype is busy in any CALL?
– Radio Server Skype is Free for next CALL?
– Any Live Radio Schedule is running using Skype Calls?

Skype-SQLite

SQLite Tools

DB Browser for SQLite
Skyperious is a Skype database viewer and merger in Python

Skype-SQLite

PHP with SQLite extension to access Skype Database

PHP-PDO-SQLite

PHP-SQLite

<?php
$skype_path = "C:/Users/[Radio-Server]/AppData/Roaming/Skype/[Radio-Skype]/main.db";

// Path for skype main.db file
$db = new SQLite3($skype_path);

$query = "SELECT id, name FROM Calls where is_active='1';";

$stmt = $db->prepare($query);
$results = $stmt->execute();

$call 	= $results->fetchArray();


// Get all Mamabers for current Skype CALL
$query = "SELECT id, identity, guid FROM CallMembers where call_name='".$call['name']."';";

$stmt = $db->prepare($query);
$results = $stmt->execute();

echo "<pre>";

while($data = $results->fetchArray()){

// Print all records in current Skype CALL
print_r($data); 

}

echo "</pre>";

$db->close();

?>

SQLite-CMD

JavaScript Detecting Internet Connection

Offline.js automatically alerts users for their internet connection using AJAX requests.

Download offline.min.js

offline.html

<script src="offline.min.js"></script>
<script>

Offline.options = {checks: {xhr: {url: 'offline.html'}}};

var run = function(){

// check internet connection
Offline.check();

// return internet connection state  
console.log(Offline.state);

document.getElementById("status").innerHTML = Offline.state; 
 
}

setInterval(run, 5000);

</script>

<div id="status">up</div>