Passed
Branch master (c73d10)
by Stefan
02:51 queued 55s
created

PNTestServer.php (2 issues)

Labels
Severity
1
<?php
2
require_once 'autoloader.php';
3
4
use SKien\PNServer\PNDataProvider;
5
use SKien\PNServer\PNDataProviderSQLite;
6
use SKien\PNServer\PNDataProviderMySQL;
7
use SKien\PNServer\PNSubscription;
8
use SKien\PNServer\PNVapid;
9
use SKien\PNServer\PNPayload;
10
use SKien\PNServer\PNServer;
11
12
	// check, if PHP version is sufficient and all required extensions are installed
13
	$bExit = false;
14
	if (version_compare(phpversion(), '7.1', '<')) {
15
		trigger_error('At least PHP Version 7.1 is required (current Version is ' . phpversion() . ')!', E_USER_WARNING);
16
		$bExit = true;
17
	}
18
	$aExt = array('curl', 'gmp', 'mbstring', 'openssl', 'bcmath');
19
	foreach ($aExt as $strExt) {
20
		if (!extension_loaded($strExt)) {
21
			trigger_error('Extension ' . $strExt . ' must be installed!', E_USER_WARNING);
22
			$bExit = true;
23
		}
24
	}
25
	if ($bExit) {
26
		exit();
27
	}
28
29
	// for test use SQLite database - will be cretaed at first instantiation
30
	$oDP = new PNDataProviderSQLite();
31
	
32
	// or may use any MySQL database
33
	// $oDP = new PNDataProviderMySQL('localhost', 'username', 'password', 'db-name');
34
	
35
	if (!$oDP->isConnected()) {
36
	    echo $oDP->getError();
37
		exit();
38
	}
39
40
	// just add expired (unsubscribed) subscription to demonstrate response of
41
	// push service and the auto-remove option
42
	if (!$oDP->saveSubscription(
43
			'{'
44
			.'	"endpoint": "https://fcm.googleapis.com/fcm/send/f8PIq7EL6xI:APA91bFgD2qA0Goo_6sWgWVDKclh5Sm1Gf1BtYZw3rePs_GHqmC9l2N92I4QhLQtPmyB18HYYseFHLhvMbpq-oGz2Jtt8AVExmNU9R3K9Z-Gaiq6rQxig1WT4ND_5PSXTjuth-GoGggt",'
45
			.'	"expirationTime": "1589291569000",'
46
			.'	"keys": {'
47
			.'	"p256dh": "BEQrfuNX-ZrXPf0Mm-IdVMO1LMpu5N3ifgcyeUD2nYwuUhRUDmn_wVOM3eQyYux5vW2B8-TyTYco4-bFKKR02IA",'
48
			.'	"auth": "jOfywakW_srfHhMF-NiZ3Q"'
49
			.'	}'
50
			.'}'
51
	    )) {
52
	    echo $oDP->getError();
53
	    exit();
54
	}
55
	
56
	echo 'Count of subscriptions: ' . $oDP->count() . '<br/><br/>' . PHP_EOL;
57
	if ($oDP->init()) {
58
		while (($strJsonSub = $oDP->fetch()) !== false) {
59
			echo 'UA: ' . $oDP->getColumn(PNDataProvider::COL_USERAGENT);
60
			echo ' (Lastupdated: ' . date('Y-m-d H:i:s', $oDP->getColumn(PNDataProvider::COL_LASTUPDATED)) . ')<br/>' . PHP_EOL;			
0 ignored issues
show
$oDP->getColumn(SKien\PN...vider::COL_LASTUPDATED) of type string is incompatible with the type integer expected by parameter $timestamp of date(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
			echo ' (Lastupdated: ' . date('Y-m-d H:i:s', /** @scrutinizer ignore-type */ $oDP->getColumn(PNDataProvider::COL_LASTUPDATED)) . ')<br/>' . PHP_EOL;			
Loading history...
61
			$strPrintable = json_encode(json_decode($strJsonSub), JSON_PRETTY_PRINT);
0 ignored issues
show
It seems like $strJsonSub can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
			$strPrintable = json_encode(json_decode(/** @scrutinizer ignore-type */ $strJsonSub), JSON_PRETTY_PRINT);
Loading history...
62
			$strPrintable = str_replace("\n", '<br/>', $strPrintable);
63
			$strPrintable = str_replace(" ", '&nbsp;', $strPrintable);
64
			echo '<span style="font-size: 10pt; font-family: courier; overflow:scroll; white-space: nowrap">' . $strPrintable . '</span><br/><br/>' . PHP_EOL;			
65
		}
66
	} else {
67
    	echo $oDP->getError();
68
    	exit();
69
	}
70
	
71
	// the server to handle all
72
	$oServer = new PNServer($oDP);
73
74
	// set the VAPID key
75
	/*
76
	$oVapid = new PNVapid(
77
			"mailto:[email protected]",
78
			"the-generated-public-key",
79
			"the-generated-private-key"
80
	   );
81
	*/
82
	$oVapid = new PNVapid(
83
	       "mailto:[email protected]",
84
	       "BDtOCcUUTYvuUzx9ktgYs3mB6tQCjFLNfOkuiaIi_2LNosLbHQY6P91eMzQ8opTDLK_PjJHsjMSiJ-MUOeSjV8E",
85
	       "juLDCbPNbObvn-89_o0SEbnBZLMWxlVEjGypyxHEh2M"
86
	   );
87
	        
88
	$oServer->setVapid($oVapid);
89
	
90
	// create payload
91
	// - we don't set a title - so service worker uses default
92
	// - URL to icon can be
93
	//    * relative to the origin location of the service worker
94
	//    * absolute from the homepage (begining with a '/')
95
	//    * complete URL (beginning with https://) 
96
	$oPayload = new PNPayload('', "...first text to display.", './elephpant.png');
97
	// set tag to group the notifications but always show the popup 
98
	$oPayload->setTag('news', true);
99
	// and lead the user to thr page of your choice
100
	$oPayload->setURL('/where-to-go.php');
101
	
102
	$oServer->setPayload($oPayload);
103
104
	// load subscriptions from database (incluing the expired one created above...) 
105
	if (!$oServer->loadSubscriptions()) {
106
	    echo $oDP->getError();
107
	    exit();
108
	}
109
	
110
	// ... and finally push !
111
	if (!$oServer->push()) {
112
		echo '<h2>' . $oServer->getError() . '</h2>' . PHP_EOL;
113
	} else {
114
		$aLog = $oServer->getLog();
115
		echo '<h2>Push - Log:</h2>' . PHP_EOL;
116
		foreach ($aLog as $strEndpoint => $aMsg ) {
117
			echo '<h3>' . PNSubscription::getOrigin($strEndpoint) . '</h3>' . PHP_EOL;
118
			echo $aMsg['msg'] . '<br/>resonse code: ' . $aMsg['curl_response_code']  . ' (' . $aMsg['curl_response'] . ')';	
119
		}
120
	}
121