forked from nikkiii/vbulletin-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.php
155 lines (137 loc) · 4.32 KB
/
utils.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
<?php
date_default_timezone_set('Europe/Madrid');
/*
* Converts CSV to JSON
* Example uses Google Spreadsheet CSV feed
* csvToArray function I think I found on php.net
*/
// Function to convert CSV into associative array
function csvToArray($file, $delimiter) {
if (($handle = fopen($file, 'r')) !== FALSE) {
$i = 0;
while (($lineArray = fgetcsv($handle, 4000, $delimiter, '"')) !== FALSE) {
for ($j = 0; $j < count($lineArray); $j++) {
$arr[$i][$j] = $lineArray[$j];
}
$i++;
}
fclose($handle);
}
return $arr;
}
function getData($feed) {
// Arrays we'll use later
$keys = array();
$newArray = array();
// Do it
$data = csvToArray($feed, ',');
// Set number of elements (minus 1 because we shift off the first row)
$count = count($data) - 1;
//Use first row for names
$labels = array_shift($data);
foreach ($labels as $label) {
$keys[] = $label;
}
// Add Ids, just in case we want them later
$keys[] = 'id';
for ($i = 0; $i < $count; $i++) {
$data[$i][] = $i;
}
// Bring it all together
for ($j = 0; $j < $count; $j++) {
$d = array_combine($keys, $data[$j]);
$newArray[$j] = $d;
}
return $newArray;
}
function getCalendarWeek($date_string) {
$my_dateTime = new DateTime($date_string, new DateTimeZone('Europe/Madrid'));
return $my_dateTime->format("W");
}
// Credits: http://www.velvetcache.org/2007/01/22/simple-bbcode-to-html-function-in-php
function bbc2html($content) {
$search = array (
'/(\[b\])(.*?)(\[\/b\])/',
'/(\[i\])(.*?)(\[\/i\])/',
'/(\[u\])(.*?)(\[\/u\])/',
'/(\[ul\])(.*?)(\[\/ul\])/',
'/(\[li\])(.*?)(\[\/li\])/',
'/(\[url=)(.*?)(\])(.*?)(\[\/url\])/',
'/(\[url\])(.*?)(\[\/url\])/'
);
$replace = array (
'<strong>$2</strong>',
'<em>$2</em>',
'<u>$2</u>',
'<ul>$2</ul>',
'<li>$2</li>',
'<a href="$2" target="_blank">$4</a>',
'<a href="$2" target="_blank">$2</a>'
);
return preg_replace($search, $replace, $content);
}
// needs bbcode extension installed
function bbcode2html($bbcode_text) {
$arrayBBCode = array(
''=> array('type'=>BBCODE_TYPE_ROOT, 'childs'=>'!i'),
'i'=> array('type'=>BBCODE_TYPE_NOARG, 'open_tag'=>'<i>',
'close_tag'=>'</i>', 'childs'=>'b'),
'url'=> array('type'=>BBCODE_TYPE_OPTARG,
'open_tag'=>'<a href="{PARAM}">', 'close_tag'=>'</a>',
'default_arg'=>'{CONTENT}',
'childs'=>'b,i'),
'img'=> array('type'=>BBCODE_TYPE_NOARG,
'open_tag'=>'<img src="', 'close_tag'=>'" />',
'childs'=>''),
'b'=> array('type'=>BBCODE_TYPE_NOARG, 'open_tag'=>'<b>',
'close_tag'=>'</b>'),
);
$BBHandler = bbcode_create($arrayBBCode);
return bbcode_parse($BBHandler, $bbcode_text);
}
function checkAbsoluteURL(&$url_string) {
$valid_url = strpos($url_string, "http");
if ($valid_url === false) {
$url_string = "http://" . $url_string;
}
}
// adapted from http://www.algorithmist.com/index.php/Quicksort_non-recursive.php
function quickSort(&$array, $fieldname) {
$cur = 1;
$stack[1]['l'] = 0;
$stack[1]['r'] = count($array)-1;
do {
$l = $stack[$cur]['l'];
$r = $stack[$cur]['r'];
$cur--;
do {
$i = $l;
$j = $r;
$tmp = $array[(int)( ($l+$r)/2 )];
// partion the array in two parts.
// left from $tmp are with smaller values,
// right from $tmp are with bigger ones
do {
while($array[$i][$fieldname] < $tmp[$fieldname])
$i++;
while($tmp[$fieldname] < $array[$j][$fieldname])
$j--;
// swap elements from the two sides
if($i <= $j) {
$w = $array[$i];
$array[$i] = $array[$j];
$array[$j] = $w;
$i++;
$j--;
}
} while ($i <= $j);
if($i < $r) {
$cur++;
$stack[$cur]['l'] = $i;
$stack[$cur]['r'] = $r;
}
$r = $j;
} while ($l < $r);
} while ($cur != 0);
}
?>