diff options
author | VirtualTam <virtualtam@flibidi.net> | 2015-07-11 01:29:12 +0200 |
---|---|---|
committer | VirtualTam <virtualtam@flibidi.net> | 2015-07-13 13:06:06 +0200 |
commit | d1e2f8e52c931f84c11d4f54f32959710d528182 (patch) | |
tree | be5ad2fcfeb31136e7afca0603a3cd3da3d76b57 /index.php | |
parent | 5b0ebbc5de06b8a0e9679b78b45d0dc755db7986 (diff) | |
download | Shaarli-d1e2f8e52c931f84c11d4f54f32959710d528182.tar.gz Shaarli-d1e2f8e52c931f84c11d4f54f32959710d528182.tar.zst Shaarli-d1e2f8e52c931f84c11d4f54f32959710d528182.zip |
PHP: ensure 5.3 compatibility, refactor timezone utilities
Relates to #250
Modifications
- supported version
- bump required version from 5.1.0 to 5.3.x
- update README
- add PHP 5.3 to Travis environments
- rewrite array declarations: explicitely use array() instead of []
- move checkPHPVersion to application/Utils.php
- move timezone functions to application/TimeZone.php
- cleanup code
- improve test coverage
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
Diffstat (limited to 'index.php')
-rw-r--r-- | index.php | 109 |
1 files changed, 24 insertions, 85 deletions
@@ -3,7 +3,7 @@ | |||
3 | // The personal, minimalist, super-fast, no-database Delicious clone. By sebsauvage.net | 3 | // The personal, minimalist, super-fast, no-database Delicious clone. By sebsauvage.net |
4 | // http://sebsauvage.net/wiki/doku.php?id=php:shaarli | 4 | // http://sebsauvage.net/wiki/doku.php?id=php:shaarli |
5 | // Licence: http://www.opensource.org/licenses/zlib-license.php | 5 | // Licence: http://www.opensource.org/licenses/zlib-license.php |
6 | // Requires: PHP 5.1.x (but autocomplete fields will only work if you have PHP 5.2.x) | 6 | // Requires: PHP 5.3.x |
7 | // ----------------------------------------------------------------------------------------------- | 7 | // ----------------------------------------------------------------------------------------------- |
8 | // NEVER TRUST IN PHP.INI | 8 | // NEVER TRUST IN PHP.INI |
9 | // Some hosts do not define a default timezone in php.ini, | 9 | // Some hosts do not define a default timezone in php.ini, |
@@ -59,7 +59,6 @@ ini_set('max_input_time','60'); // High execution time in case of problematic i | |||
59 | ini_set('memory_limit', '128M'); // Try to set max upload file size and read (May not work on some hosts). | 59 | ini_set('memory_limit', '128M'); // Try to set max upload file size and read (May not work on some hosts). |
60 | ini_set('post_max_size', '16M'); | 60 | ini_set('post_max_size', '16M'); |
61 | ini_set('upload_max_filesize', '16M'); | 61 | ini_set('upload_max_filesize', '16M'); |
62 | checkphpversion(); | ||
63 | error_reporting(E_ALL^E_WARNING); // See all error except warnings. | 62 | error_reporting(E_ALL^E_WARNING); // See all error except warnings. |
64 | //error_reporting(-1); // See all errors (for debugging only) | 63 | //error_reporting(-1); // See all errors (for debugging only) |
65 | 64 | ||
@@ -70,9 +69,19 @@ if (is_file($GLOBALS['config']['CONFIG_FILE'])) { | |||
70 | 69 | ||
71 | // Shaarli library | 70 | // Shaarli library |
72 | require_once 'application/LinkDB.php'; | 71 | require_once 'application/LinkDB.php'; |
72 | require_once 'application/TimeZone.php'; | ||
73 | require_once 'application/Utils.php'; | 73 | require_once 'application/Utils.php'; |
74 | require_once 'application/Config.php'; | 74 | require_once 'application/Config.php'; |
75 | 75 | ||
76 | // Ensure the PHP version is supported | ||
77 | try { | ||
78 | checkPHPVersion('5.3', PHP_VERSION); | ||
79 | } catch(Exception $e) { | ||
80 | header('Content-Type: text/plain; charset=utf-8'); | ||
81 | echo $e->getMessage(); | ||
82 | exit; | ||
83 | } | ||
84 | |||
76 | include "inc/rain.tpl.class.php"; //include Rain TPL | 85 | include "inc/rain.tpl.class.php"; //include Rain TPL |
77 | raintpl::$tpl_dir = $GLOBALS['config']['RAINTPL_TPL']; // template directory | 86 | raintpl::$tpl_dir = $GLOBALS['config']['RAINTPL_TPL']; // template directory |
78 | raintpl::$cache_dir = $GLOBALS['config']['RAINTPL_TMP']; // cache directory | 87 | raintpl::$cache_dir = $GLOBALS['config']['RAINTPL_TMP']; // cache directory |
@@ -164,21 +173,7 @@ function setup_login_state() { | |||
164 | 173 | ||
165 | return $userIsLoggedIn; | 174 | return $userIsLoggedIn; |
166 | } | 175 | } |
167 | //================================================================================================== | ||
168 | $userIsLoggedIn = setup_login_state(); | 176 | $userIsLoggedIn = setup_login_state(); |
169 | //================================================================================================== | ||
170 | //================================================================================================== | ||
171 | |||
172 | // Check PHP version | ||
173 | function checkphpversion() | ||
174 | { | ||
175 | if (version_compare(PHP_VERSION, '5.1.0') < 0) | ||
176 | { | ||
177 | header('Content-Type: text/plain; charset=utf-8'); | ||
178 | echo 'Your PHP version is obsolete! Shaarli requires at least php 5.1.0, and thus cannot run. Sorry. Your PHP version has known security vulnerabilities and should be updated as soon as possible.'; | ||
179 | exit; | ||
180 | } | ||
181 | } | ||
182 | 177 | ||
183 | // Checks if an update is available for Shaarli. | 178 | // Checks if an update is available for Shaarli. |
184 | // (at most once a day, and only for registered user.) | 179 | // (at most once a day, and only for registered user.) |
@@ -982,7 +977,7 @@ function showDaily() | |||
982 | $linksToDisplay = $LINKSDB->filterDay($day); | 977 | $linksToDisplay = $LINKSDB->filterDay($day); |
983 | } catch (Exception $exc) { | 978 | } catch (Exception $exc) { |
984 | error_log($exc); | 979 | error_log($exc); |
985 | $linksToDisplay = []; | 980 | $linksToDisplay = array(); |
986 | } | 981 | } |
987 | 982 | ||
988 | // We pre-format some fields for proper output. | 983 | // We pre-format some fields for proper output. |
@@ -1288,7 +1283,7 @@ function renderPage() | |||
1288 | if (!tokenOk($_POST['token'])) die('Wrong token.'); // Go away! | 1283 | if (!tokenOk($_POST['token'])) die('Wrong token.'); // Go away! |
1289 | $tz = 'UTC'; | 1284 | $tz = 'UTC'; |
1290 | if (!empty($_POST['continent']) && !empty($_POST['city'])) | 1285 | if (!empty($_POST['continent']) && !empty($_POST['city'])) |
1291 | if (isTZvalid($_POST['continent'],$_POST['city'])) | 1286 | if (isTimeZoneValid($_POST['continent'],$_POST['city'])) |
1292 | $tz = $_POST['continent'].'/'.$_POST['city']; | 1287 | $tz = $_POST['continent'].'/'.$_POST['city']; |
1293 | $GLOBALS['timezone'] = $tz; | 1288 | $GLOBALS['timezone'] = $tz; |
1294 | $GLOBALS['title']=$_POST['title']; | 1289 | $GLOBALS['title']=$_POST['title']; |
@@ -1322,8 +1317,8 @@ function renderPage() | |||
1322 | $PAGE->assign('token',getToken()); | 1317 | $PAGE->assign('token',getToken()); |
1323 | $PAGE->assign('title', empty($GLOBALS['title']) ? '' : $GLOBALS['title'] ); | 1318 | $PAGE->assign('title', empty($GLOBALS['title']) ? '' : $GLOBALS['title'] ); |
1324 | $PAGE->assign('redirector', empty($GLOBALS['redirector']) ? '' : $GLOBALS['redirector'] ); | 1319 | $PAGE->assign('redirector', empty($GLOBALS['redirector']) ? '' : $GLOBALS['redirector'] ); |
1325 | list($timezone_form,$timezone_js) = templateTZform($GLOBALS['timezone']); | 1320 | list($timezone_form, $timezone_js) = generateTimeZoneForm($GLOBALS['timezone']); |
1326 | $PAGE->assign('timezone_form',$timezone_form); // FIXME: Put entire tz form generation in template? | 1321 | $PAGE->assign('timezone_form', $timezone_form); |
1327 | $PAGE->assign('timezone_js',$timezone_js); | 1322 | $PAGE->assign('timezone_js',$timezone_js); |
1328 | $PAGE->renderPage('configure'); | 1323 | $PAGE->renderPage('configure'); |
1329 | exit; | 1324 | exit; |
@@ -2059,9 +2054,11 @@ function install() | |||
2059 | if (!empty($_POST['setlogin']) && !empty($_POST['setpassword'])) | 2054 | if (!empty($_POST['setlogin']) && !empty($_POST['setpassword'])) |
2060 | { | 2055 | { |
2061 | $tz = 'UTC'; | 2056 | $tz = 'UTC'; |
2062 | if (!empty($_POST['continent']) && !empty($_POST['city'])) | 2057 | if (!empty($_POST['continent']) && !empty($_POST['city'])) { |
2063 | if (isTZvalid($_POST['continent'],$_POST['city'])) | 2058 | if (isTimeZoneValid($_POST['continent'], $_POST['city'])) { |
2064 | $tz = $_POST['continent'].'/'.$_POST['city']; | 2059 | $tz = $_POST['continent'].'/'.$_POST['city']; |
2060 | } | ||
2061 | } | ||
2065 | $GLOBALS['timezone'] = $tz; | 2062 | $GLOBALS['timezone'] = $tz; |
2066 | // Everything is ok, let's create config file. | 2063 | // Everything is ok, let's create config file. |
2067 | $GLOBALS['login'] = $_POST['setlogin']; | 2064 | $GLOBALS['login'] = $_POST['setlogin']; |
@@ -2087,8 +2084,11 @@ function install() | |||
2087 | } | 2084 | } |
2088 | 2085 | ||
2089 | // Display config form: | 2086 | // Display config form: |
2090 | list($timezone_form,$timezone_js) = templateTZform(); | 2087 | list($timezone_form, $timezone_js) = generateTimeZoneForm(); |
2091 | $timezone_html=''; if ($timezone_form!='') $timezone_html='<tr><td><b>Timezone:</b></td><td>'.$timezone_form.'</td></tr>'; | 2088 | $timezone_html = ''; |
2089 | if ($timezone_form != '') { | ||
2090 | $timezone_html = '<tr><td><b>Timezone:</b></td><td>'.$timezone_form.'</td></tr>'; | ||
2091 | } | ||
2092 | 2092 | ||
2093 | $PAGE = new pageBuilder; | 2093 | $PAGE = new pageBuilder; |
2094 | $PAGE->assign('timezone_html',$timezone_html); | 2094 | $PAGE->assign('timezone_html',$timezone_html); |
@@ -2097,67 +2097,6 @@ function install() | |||
2097 | exit; | 2097 | exit; |
2098 | } | 2098 | } |
2099 | 2099 | ||
2100 | // Generates the timezone selection form and JavaScript. | ||
2101 | // Input: (optional) current timezone (can be 'UTC/UTC'). It will be pre-selected. | ||
2102 | // Output: array(html,js) | ||
2103 | // Example: list($htmlform,$js) = templateTZform('Europe/Paris'); // Europe/Paris pre-selected. | ||
2104 | // Returns array('','') if server does not support timezones list. (e.g. PHP 5.1 on free.fr) | ||
2105 | function templateTZform($ptz=false) | ||
2106 | { | ||
2107 | if (function_exists('timezone_identifiers_list')) // because of old PHP version (5.1) which can be found on free.fr | ||
2108 | { | ||
2109 | // Try to split the provided timezone. | ||
2110 | if ($ptz==false) { $l=timezone_identifiers_list(); $ptz=$l[0]; } | ||
2111 | $spos=strpos($ptz,'/'); $pcontinent=substr($ptz,0,$spos); $pcity=substr($ptz,$spos+1); | ||
2112 | |||
2113 | // Display config form: | ||
2114 | $timezone_form = ''; | ||
2115 | $timezone_js = ''; | ||
2116 | // The list is in the form "Europe/Paris", "America/Argentina/Buenos_Aires"... | ||
2117 | // We split the list in continents/cities. | ||
2118 | $continents = array(); | ||
2119 | $cities = array(); | ||
2120 | foreach(timezone_identifiers_list() as $tz) | ||
2121 | { | ||
2122 | if ($tz=='UTC') $tz='UTC/UTC'; | ||
2123 | $spos = strpos($tz,'/'); | ||
2124 | if ($spos!==false) | ||
2125 | { | ||
2126 | $continent=substr($tz,0,$spos); $city=substr($tz,$spos+1); | ||
2127 | $continents[$continent]=1; | ||
2128 | if (!isset($cities[$continent])) $cities[$continent]=''; | ||
2129 | $cities[$continent].='<option value="'.$city.'"'.($pcity==$city?' selected':'').'>'.$city.'</option>'; | ||
2130 | } | ||
2131 | } | ||
2132 | $continents_html = ''; | ||
2133 | $continents = array_keys($continents); | ||
2134 | foreach($continents as $continent) | ||
2135 | $continents_html.='<option value="'.$continent.'"'.($pcontinent==$continent?' selected':'').'>'.$continent.'</option>'; | ||
2136 | $cities_html = $cities[$pcontinent]; | ||
2137 | $timezone_form = "Continent: <select name=\"continent\" id=\"continent\" onChange=\"onChangecontinent();\">${continents_html}</select>"; | ||
2138 | $timezone_form .= " City: <select name=\"city\" id=\"city\">${cities[$pcontinent]}</select><br />"; | ||
2139 | $timezone_js = "<script>"; | ||
2140 | $timezone_js .= "function onChangecontinent(){document.getElementById(\"city\").innerHTML = citiescontinent[document.getElementById(\"continent\").value];}"; | ||
2141 | $timezone_js .= "var citiescontinent = ".json_encode($cities).";" ; | ||
2142 | $timezone_js .= "</script>" ; | ||
2143 | return array($timezone_form,$timezone_js); | ||
2144 | } | ||
2145 | return array('',''); | ||
2146 | } | ||
2147 | |||
2148 | // Tells if a timezone is valid or not. | ||
2149 | // If not valid, returns false. | ||
2150 | // If system does not support timezone list, returns false. | ||
2151 | function isTZvalid($continent,$city) | ||
2152 | { | ||
2153 | $tz = $continent.'/'.$city; | ||
2154 | if (function_exists('timezone_identifiers_list')) // because of old PHP version (5.1) which can be found on free.fr | ||
2155 | { | ||
2156 | if (in_array($tz, timezone_identifiers_list())) // it's a valid timezone? | ||
2157 | return true; | ||
2158 | } | ||
2159 | return false; | ||
2160 | } | ||
2161 | if (!function_exists('json_encode')) { | 2100 | if (!function_exists('json_encode')) { |
2162 | function json_encode($data) { | 2101 | function json_encode($data) { |
2163 | switch ($type = gettype($data)) { | 2102 | switch ($type = gettype($data)) { |