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 /application | |
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 'application')
-rwxr-xr-x | application/Config.php | 6 | ||||
-rw-r--r-- | application/TimeZone.php | 110 | ||||
-rw-r--r-- | application/Utils.php | 23 |
3 files changed, 135 insertions, 4 deletions
diff --git a/application/Config.php b/application/Config.php index 0b01b524..ec799d7f 100755 --- a/application/Config.php +++ b/application/Config.php | |||
@@ -19,10 +19,10 @@ | |||
19 | function writeConfig($config, $isLoggedIn) | 19 | function writeConfig($config, $isLoggedIn) |
20 | { | 20 | { |
21 | // These fields are required in configuration. | 21 | // These fields are required in configuration. |
22 | $MANDATORY_FIELDS = [ | 22 | $MANDATORY_FIELDS = array( |
23 | 'login', 'hash', 'salt', 'timezone', 'title', 'titleLink', | 23 | 'login', 'hash', 'salt', 'timezone', 'title', 'titleLink', |
24 | 'redirector', 'disablesessionprotection', 'privateLinkByDefault' | 24 | 'redirector', 'disablesessionprotection', 'privateLinkByDefault' |
25 | ]; | 25 | ); |
26 | 26 | ||
27 | if (!isset($config['config']['CONFIG_FILE'])) { | 27 | if (!isset($config['config']['CONFIG_FILE'])) { |
28 | throw new MissingFieldConfigException('CONFIG_FILE'); | 28 | throw new MissingFieldConfigException('CONFIG_FILE'); |
@@ -126,4 +126,4 @@ class UnauthorizedConfigException extends Exception | |||
126 | { | 126 | { |
127 | $this->message = 'You are not authorized to alter config.'; | 127 | $this->message = 'You are not authorized to alter config.'; |
128 | } | 128 | } |
129 | } \ No newline at end of file | 129 | } |
diff --git a/application/TimeZone.php b/application/TimeZone.php new file mode 100644 index 00000000..ccbef918 --- /dev/null +++ b/application/TimeZone.php | |||
@@ -0,0 +1,110 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * Generates the timezone selection form and JavaScript. | ||
4 | * | ||
5 | * Note: 'UTC/UTC' is mapped to 'UTC' to form a valid option | ||
6 | * | ||
7 | * Example: preselect Europe/Paris | ||
8 | * list($htmlform, $js) = templateTZform('Europe/Paris'); | ||
9 | * | ||
10 | * @param string $preselected_timezone preselected timezone (optional) | ||
11 | * | ||
12 | * @return an array containing the generated HTML form and Javascript code | ||
13 | **/ | ||
14 | function generateTimeZoneForm($preselected_timezone='') | ||
15 | { | ||
16 | // Select the first available timezone if no preselected value is passed | ||
17 | if ($preselected_timezone == '') { | ||
18 | $l = timezone_identifiers_list(); | ||
19 | $preselected_timezone = $l[0]; | ||
20 | } | ||
21 | |||
22 | // Try to split the provided timezone | ||
23 | $spos = strpos($preselected_timezone, '/'); | ||
24 | $pcontinent = substr($preselected_timezone, 0, $spos); | ||
25 | $pcity = substr($preselected_timezone, $spos+1); | ||
26 | |||
27 | // Display config form: | ||
28 | $timezone_form = ''; | ||
29 | $timezone_js = ''; | ||
30 | |||
31 | // The list is in the form 'Europe/Paris', 'America/Argentina/Buenos_Aires'... | ||
32 | // We split the list in continents/cities. | ||
33 | $continents = array(); | ||
34 | $cities = array(); | ||
35 | |||
36 | // TODO: use a template to generate the HTML/Javascript form | ||
37 | |||
38 | foreach (timezone_identifiers_list() as $tz) { | ||
39 | if ($tz == 'UTC') { | ||
40 | $tz = 'UTC/UTC'; | ||
41 | } | ||
42 | $spos = strpos($tz, '/'); | ||
43 | |||
44 | if ($spos !== false) { | ||
45 | $continent = substr($tz, 0, $spos); | ||
46 | $city = substr($tz, $spos+1); | ||
47 | $continents[$continent] = 1; | ||
48 | |||
49 | if (!isset($cities[$continent])) { | ||
50 | $cities[$continent] = ''; | ||
51 | } | ||
52 | $cities[$continent] .= '<option value="'.$city.'"'; | ||
53 | if ($pcity == $city) { | ||
54 | $cities[$continent] .= ' selected="selected"'; | ||
55 | } | ||
56 | $cities[$continent] .= '>'.$city.'</option>'; | ||
57 | } | ||
58 | } | ||
59 | |||
60 | $continents_html = ''; | ||
61 | $continents = array_keys($continents); | ||
62 | |||
63 | foreach ($continents as $continent) { | ||
64 | $continents_html .= '<option value="'.$continent.'"'; | ||
65 | if ($pcontinent == $continent) { | ||
66 | $continents_html .= ' selected="selected"'; | ||
67 | } | ||
68 | $continents_html .= '>'.$continent.'</option>'; | ||
69 | } | ||
70 | |||
71 | // Timezone selection form | ||
72 | $timezone_form = 'Continent:'; | ||
73 | $timezone_form .= '<select name="continent" id="continent" onChange="onChangecontinent();">'; | ||
74 | $timezone_form .= $continents_html.'</select>'; | ||
75 | $timezone_form .= ' City:'; | ||
76 | $timezone_form .= '<select name="city" id="city">'.$cities[$pcontinent].'</select><br />'; | ||
77 | |||
78 | // Javascript handler - updates the city list when the user selects a continent | ||
79 | $timezone_js = '<script>'; | ||
80 | $timezone_js .= 'function onChangecontinent() {'; | ||
81 | $timezone_js .= 'document.getElementById("city").innerHTML ='; | ||
82 | $timezone_js .= ' citiescontinent[document.getElementById("continent").value]; }'; | ||
83 | $timezone_js .= 'var citiescontinent = '.json_encode($cities).';'; | ||
84 | $timezone_js .= '</script>'; | ||
85 | |||
86 | return array($timezone_form, $timezone_js); | ||
87 | } | ||
88 | |||
89 | /** | ||
90 | * Tells if a continent/city pair form a valid timezone | ||
91 | * | ||
92 | * Note: 'UTC/UTC' is mapped to 'UTC' | ||
93 | * | ||
94 | * @param string $continent the timezone continent | ||
95 | * @param string $city the timezone city | ||
96 | * | ||
97 | * @return whether continent/city is a valid timezone | ||
98 | */ | ||
99 | function isTimeZoneValid($continent, $city) | ||
100 | { | ||
101 | if ($continent == 'UTC' && $city == 'UTC') { | ||
102 | return true; | ||
103 | } | ||
104 | |||
105 | return in_array( | ||
106 | $continent.'/'.$city, | ||
107 | timezone_identifiers_list() | ||
108 | ); | ||
109 | } | ||
110 | ?> | ||
diff --git a/application/Utils.php b/application/Utils.php index 658b97bc..cd4724fa 100644 --- a/application/Utils.php +++ b/application/Utils.php | |||
@@ -48,7 +48,7 @@ function endsWith($haystack, $needle, $case=true) | |||
48 | */ | 48 | */ |
49 | function nl2br_escaped($html) | 49 | function nl2br_escaped($html) |
50 | { | 50 | { |
51 | return str_replace('>','>',str_replace('<','<',nl2br($html))); | 51 | return str_replace('>', '>', str_replace('<', '<', nl2br($html))); |
52 | } | 52 | } |
53 | 53 | ||
54 | /** | 54 | /** |
@@ -117,3 +117,24 @@ function generateLocation($referer, $host, $loopTerms = array()) | |||
117 | 117 | ||
118 | return $final_referer; | 118 | return $final_referer; |
119 | } | 119 | } |
120 | |||
121 | /** | ||
122 | * Checks the PHP version to ensure Shaarli can run | ||
123 | * | ||
124 | * @param string $minVersion minimum PHP required version | ||
125 | * @param string $curVersion current PHP version (use PHP_VERSION) | ||
126 | * | ||
127 | * @throws Exception the PHP version is not supported | ||
128 | */ | ||
129 | function checkPHPVersion($minVersion, $curVersion) | ||
130 | { | ||
131 | if (version_compare($curVersion, $minVersion) < 0) { | ||
132 | throw new Exception( | ||
133 | 'Your PHP version is obsolete!' | ||
134 | .' Shaarli requires at least PHP '.$minVersion.', and thus cannot run.' | ||
135 | .' Your PHP version has known security vulnerabilities and should be' | ||
136 | .' updated as soon as possible.' | ||
137 | ); | ||
138 | } | ||
139 | } | ||
140 | ?> | ||