aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
authorArthur <arthur@hoa.ro>2015-07-15 11:05:07 +0200
committerArthur <arthur@hoa.ro>2015-07-15 11:05:07 +0200
commit874f858b8fbacfcd31cd64e21555f03927d4e15a (patch)
treebe5ad2fcfeb31136e7afca0603a3cd3da3d76b57 /application
parent5b0ebbc5de06b8a0e9679b78b45d0dc755db7986 (diff)
parentd1e2f8e52c931f84c11d4f54f32959710d528182 (diff)
downloadShaarli-874f858b8fbacfcd31cd64e21555f03927d4e15a.tar.gz
Shaarli-874f858b8fbacfcd31cd64e21555f03927d4e15a.tar.zst
Shaarli-874f858b8fbacfcd31cd64e21555f03927d4e15a.zip
Merge pull request #271 from virtualtam/php53
PHP: ensure 5.3 compatibility
Diffstat (limited to 'application')
-rwxr-xr-xapplication/Config.php6
-rw-r--r--application/TimeZone.php110
-rw-r--r--application/Utils.php23
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 @@
19function writeConfig($config, $isLoggedIn) 19function 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 **/
14function 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 .= '&nbsp;&nbsp;&nbsp;&nbsp;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 */
99function 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 */
49function nl2br_escaped($html) 49function nl2br_escaped($html)
50{ 50{
51 return str_replace('>','&gt;',str_replace('<','&lt;',nl2br($html))); 51 return str_replace('>', '&gt;', str_replace('<', '&lt;', 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 */
129function 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?>