forked from kvarq/odk_planner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.php
170 lines (149 loc) · 4.68 KB
/
util.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
// src: https://github.com/Kicksend/mailcheck/wiki/String-Distance-Algorithms
if (!defined('MAGIC')) die('!?');
function levenshteinDistance($s, $t) {
// Determine the Levenshtein distance between s and t
if (!$s || !$t) {
return 99;
}
$m = strlen($s);
$n = strlen($t);
/* For all i and j, d[i][j] holds the Levenshtein distance between
* the first i characters of s and the first j characters of t.
* Note that the array has (m+1)x(n+1) values.
*/
$d = Array();
for ($i = 0; $i <= $m; $i++) {
$d[$i] = Array();
$d[$i][0] = $i;
}
for ($j = 0; $j <= $n; $j++) {
$d[0][$j] = $j;
}
// Determine substring distances
for ($j = 1; $j <= $n; $j++) {
for ($i = 1; $i <= $m; $i++) {
// Subtract one to start at strings' index zero instead of index one
$cost = (substr($s, $i-1, 1) == substr($t, $j-1, 1)) ? 0 : 1;
$d[$i][$j] = min($d[$i][$j-1] + 1, // insertion
min($d[$i-1][$j] + 1, // deletion
$d[$i-1][$j-1] + $cost)); // substitution
}
}
// Return the strings' distance
return $d[$m][$n];
}
function pre_print_r($x, $return = false) {
if ($return) {
return '<pre>' . htmlentities(print_r($x, true)) . '</pre>';
} else {
echo '<pre>', htmlentities(print_r($x, true)), '</pre>';
}
}
### file system {{{1
function assert_permissions($instancedir) {
foreach(array('forms', 'config', 'log') as $dir) {
$dir = "$instancedir/$dir";
$good = is_dir($dir) && is_writable($dir);
if ($good) {
$dh = opendir($dir);
while ($good === true && ($file = readdir($dh)) !== false) {
$good = substr($file, 0, 1) === '.' || is_writable($dir . '/' . $file) ||
' as well as the file "' . $file;
}
closedir($dh);
}
if ($good !== true) {
fatal_error('directory "' . $dir . '" ' . $good .
' must be writeable by the user running this PHP script');
}
}
}
function sanitize($x) {
return preg_replace('/[^a-z0-9_-]/i', '', $x);
}
### html generation {{{1
function targeting($domain) {
global $config;
if ($config->get_setting('opentabs', 'no') == 'yes') {
return " target=\"odk_planner_$domain\" ";
} else {
return '';
}
}
### alerts {{{1
$alerts = array();
# classes : success, info, danger, error
function render_alert($html, $class, $times = 1) {
if ($class === 'warning') $class = 'danger';
if ($times > 1) {
$html .= " ($times times)";
}
print "<div class=\"alert alert-$class\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\">×</button>$html</div>\n";
}
function render_alerts() {
global $alerts;
print "<div class=alerts>";
foreach($alerts as $alert) {
render_alert($alert[0], $alert[1], $alert[2]);
}
print "</div>\n";
$alerts = array();
}
function alerts_to_log($logname) {
global $alerts;
foreach($alerts as $alert) {
$msg = strtoupper($alert[1]) . " : " . $alert[0];
if ($alert[2] > 1) {
$msg .= " ({$alert[2]} times)";
}
log_add($logname, $msg);
}
}
function render_alerts_delayed() {
global $alerts;
if (count($alerts) === 0) {
return;
}
print "<script>\n";
foreach($alerts as $alert) {
$html = $alert[0];
$times = $alert[2];
if ($times > 1) {
$html .= " ($times times)";
}
$html_j = json_encode($html);
$class = htmlentities($alert[1]);
print "$('.alerts').append($('<div class=\"alert alert-$class\">' + $html_j ".
"+ '<button type=button class=close data-dismiss=alert>×</button></div>'));\n";
}
print "</script>\n";
}
function alert($html, $class = 'info', $delayed = true, $admin = false) {
global $alerts, $user;
if ($admin && !in_array('admin', $user['rights'])) {
return;
}
if ($delayed) {
$found = false;
foreach($alerts as $i=>$alert) {
if ($alert[0] === $html && $alert[1] === $class) {
$alerts[$i][2]++;
$found = true;
break;
}
}
if (!$found) {
array_push($alerts, array($html, $class, 1));
}
} else {
render_alert($html, $class);
}
}
function fatal_error($html) {
echo '<style>.alert-error { width: 60%; left: 20%; background: red; padding: 1rem; color: white; } .alert-error button { display: none; }</style>';
alert('fatal error : ' . $html, 'error');
render_alerts();
die;
}
?>