Skip to content

Commit b38c7e7

Browse files
xh3n1Sam Tuke
authored andcommitted
Introduce new notification for updates
Signed-off-by: Xheni Myrtaj <[email protected]>
1 parent 2398fdb commit b38c7e7

File tree

2 files changed

+138
-2
lines changed

2 files changed

+138
-2
lines changed

public_html/lists/admin/index.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,10 +519,36 @@ function mb_strtolower($string)
519519
if (TEST) {
520520
echo Info($GLOBALS['I18N']->get('Running in testmode, no emails will be sent. Check your config file.'));
521521
}
522+
522523
$updaterdir = __DIR__ . '/../updater';
523-
if(file_exists($updaterdir) && ALLOW_UPDATER){
524-
echo Info(s('Try automatic updater').' <a href="?page=redirecttoupdater" title="'.s('automatic updater').'">'.s('here').'</a>'.' ('.s('beta').')');
524+
525+
include 'updatelib.php';
526+
527+
if (showUpdateNotification()) {
528+
529+
try {
530+
531+
$updateNotif = checkForUpdate('init.php');
532+
533+
} catch (Exception $e) {
534+
535+
echo s('Error: '), $e->getMessage(), "\n";
536+
537+
}
538+
539+
$moreInfo = '<a href="https://www.phplist.com/download?utm_source=pl'.VERSION.'&amp;utm_medium=updatedownload&amp;utm_campaign=phpList" title="'.s('Download the new version').'" target="_blank">'.s('Download the new version').'</a>';
540+
541+
if (file_exists($updaterdir) && ALLOW_UPDATER) {
542+
543+
$moreInfo.= s(' or update').' <a href="?page=redirecttoupdater" title="'.s('automatic updater').'">'.s('here.').'</a>';
544+
}
545+
546+
if ($updateNotif!== '') {
547+
548+
Info($updateNotif.''.$moreInfo);
549+
}
525550
}
551+
526552
if (version_compare(PHP_VERSION, '5.3.3', '<') && WARN_ABOUT_PHP_SETTINGS) {
527553
Error(s('Your PHP version is out of date. phpList requires PHP version 5.3.3 or higher.'));
528554
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
/**
3+
* Get Current phpList Version.
4+
*
5+
* @param string $path Production version location
6+
* @return mixed
7+
* @throws Exception
8+
*/
9+
function getCurrentphpListVersion($path = '')
10+
{
11+
$version = file_get_contents($path);
12+
$matches = array();
13+
preg_match_all('/define\(\"VERSION\",\"(.*)\"\);/', $version, $matches);
14+
15+
if (isset($matches[1][0])) {
16+
17+
return $matches[1][0];
18+
}
19+
20+
throw new Exception(s('No production version found.'));
21+
}
22+
23+
/**
24+
* Get response from server.
25+
*
26+
* @param string $path Production version location
27+
* @return mixed
28+
* @throws Exception
29+
*/
30+
function getResponse($path = '')
31+
{
32+
$serverUrl = "https://download.phplist.org/version.json";
33+
$updateUrl = $serverUrl . '?version=' . getCurrentphpListVersion($path);
34+
35+
$ch = curl_init();
36+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
37+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
38+
curl_setopt($ch, CURLOPT_URL, $updateUrl);
39+
$responseFromServer = curl_exec($ch);
40+
curl_close($ch);
41+
42+
$responseFromServer = json_decode($responseFromServer, true);
43+
44+
return $responseFromServer;
45+
}
46+
47+
/**
48+
* Check for update and return a message only if there is an update available.
49+
*
50+
* @param string $path Production version location
51+
* @return string
52+
* @throws Exception
53+
*/
54+
function checkForUpdate($path = '')
55+
{
56+
$serverResponse = getResponse($path);
57+
$version = isset($serverResponse['version']) ? $serverResponse['version'] : '';
58+
$versionString = isset($serverResponse['versionstring']) ? $serverResponse['versionstring'] : '';
59+
60+
if ($version !== '' && $version !== getCurrentphpListVersion($path) && version_compare(getCurrentphpListVersion($path), $version)) {
61+
$updateMessage = s('Update to ' . htmlentities($versionString) . ' is available. ');
62+
} else {
63+
$updateMessage = '';
64+
}
65+
66+
SaveConfig('lastcheckupdate', date('m/d/Y h:i:s', time()), 0, true);
67+
68+
return $updateMessage;
69+
}
70+
71+
/**
72+
* Check every 3 days for a new update
73+
*
74+
* @return bool
75+
*/
76+
function lastTimeCheck()
77+
{
78+
79+
$doCheck = false;
80+
81+
$currentTime = date('m/d/Y h:i:s', time());
82+
$lastCheckTime = getConfig('lastcheckupdate');
83+
$lastTimeFormattedDateTime = new DateTime($lastCheckTime);
84+
$currentTimeFormattedDateTime = new DateTime($currentTime);
85+
86+
$interval = $currentTimeFormattedDateTime->diff($lastTimeFormattedDateTime);
87+
$dDiff = $interval->format('%d');
88+
89+
if ($dDiff >= '3') {
90+
91+
$doCheck = true;
92+
}
93+
94+
return $doCheck;
95+
}
96+
97+
/**
98+
* Show notification every 3 days only
99+
* @return bool
100+
*/
101+
function showUpdateNotification()
102+
{
103+
104+
if (lastTimeCheck()) {
105+
106+
return true;
107+
}
108+
109+
return false;
110+
}

0 commit comments

Comments
 (0)