|
| 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