-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.php
More file actions
executable file
·486 lines (419 loc) · 15.4 KB
/
array.php
File metadata and controls
executable file
·486 lines (419 loc) · 15.4 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
<?php
//a collection of functions that return arrays
//code reviewed 10/16/2010
error_debug('including array.php', __file__, __line__);
function array_2d($array) {
//takes a normal array and makes it associative by doubling, eg (1, 2, 'foo') becomes (1=>1, 2=>2, 'foo'=>'foo'), used by draw_form_date() and elsewhere
$return = array();
foreach ($array as $a) $return[$a] = $a;
return $return;
}
function array_ajax($required_values=false) {
//returns an array of ajax-posted content
//set error output mode to plain text
global $_josh;
$_josh['error_mode_html'] = false;
$array = url_query_parse(file_get_contents('php://input'));
foreach ($array as $key=>$value) $array[$key] = format_quotes($value);
if ($required_values) {
$required_values = array_separated($required_values);
foreach ($required_values as $r) if (!isset($array[$r])) exit; //intended to just quietly bomb. lots of spiders seem to want to post empty to ajax urls for some reason.
}
return $array;
}
function array_argument(&$array, $value, $key='class', $separator=' ') {
//for appending class values, eg class="foo bar". you don't need to know if it exists when you array_argument it. used by several draw_ functions
if (empty($value)) return false;
if (empty($array[$key])) {
$array[$key] = $value;
} else {
$array[$key] .= $separator . $value;
}
}
function array_arguments($arguments=false) {
//for constructing an array of tag arguments, used by draw_ functions.
if (!$arguments) return array();
if (is_string($arguments)) {
if (substr($arguments, 0, 1) == '#') {
return array('id'=>substr($arguments, 1));
} else {
return array('class'=>$arguments);
}
}
return $arguments;
}
function array_checkboxes($name, $array='post') {
//finds checkbox values in an array, default POSTDATA. used by db_checkboxes()
if ($array == 'post') $array = $_POST;
if (empty($array)) return false;
$return = array();
foreach ($array as $key=>$value) {
$parts = explode('-', $key);
if ((count($parts) == 3) && ($parts[0] == 'chk') && ($parts[1] == $name)) $return[] = $parts[2];
}
return $return;
}
function array_chunk_html($string, $length) {
//split an html string into an array of strings while not breaking inside a tag, used by language_translate()
if (strlen($string) < $length) return array($string);
$words = array_explode_html($string, ' ');
$wordcount = count($words);
//iterator variables
$lengthcounter = 0;
$wordcounter = 0;
$array_position = 0;
$return = array($array_position=>'');
foreach ($words as $w) {
$wordcounter++;
if ($wordcounter != $wordcount) $w .= ' ';
$wordlength = strlen($w);
if ($wordlength > $length) {
//todo - just break the word up anyway?
error_handle('word too long', 'array_chunk_html has a word ' . $w . ' that longer than ' . $length . ' characters, which is set as its max', __file__, __line__);
return '';
}
if (($lengthcounter + $wordlength) <= $length) {
//add to current array position
$return[$array_position] .= $w;
$lengthcounter += $wordlength;
} else {
//add to new array position, reset counter
$array_position++;
$return[$array_position] = $w;
$lengthcounter = $wordlength;
}
}
return $return;
}
function array_csv($content, $delimiter=',', $optional_fields=false) {
//makes an associative array out of a delimited file, assumes first line is header
//set up header
$rows = array_separated(trim($content), NEWLINE);
$cols = explode($delimiter, array_shift($rows));
error_debug(count($rows) . ' rows and ' . count($cols) . ' cols found in content', __file__, __line__);
foreach ($cols as &$c) $c = format_text_code($c);
//optional fields will always be present in returned array, even if they are not present in spreadsheet
if (is_array($optional_fields)) {
foreach ($optional_fields as $o) {
if (!in_array($o, $cols)) $cols[] = $o;
}
}
$count = count($cols);
//do rows
foreach ($rows as &$r) {
$cells = array_separated($r, $delimiter, true);
$thisrow = array();
for ($i = 0; $i < $count; $i++) {
$thisrow[$cols[$i]] = @$cells[$i];
//strip quotes if surrounding cell content
if ((substr($thisrow[$cols[$i]], 0, 1) == '"') && (substr($thisrow[$cols[$i]], 0, 1) == '"')) $thisrow[$cols[$i]] = substr($thisrow[$cols[$i]], 1, -1);
}
$r = $thisrow;
}
return $rows;
}
function array_explode_html($string, $separator) {
//splits an HTML string into an array like explode() but keeps HTML tags together
//adapted from stefan at NOSPAM dot elakpistol dot com http://theserverpages.com/php/manual/en/function.explode.php
$return = array();
for ($i = 0, $j = 0; $i < strlen($string); $i++) {
if ($string[$i] == $separator) {
while ($string[$i + 1] == $separator) $i++;
$j++;
continue;
}
if (!isset($return[$j])) $return[$j] = '';
if ($string[$i] == '<') {
if (strlen($return[$j]) > 0) $j++;
$pos = strpos($string, '>', $i);
if (isset($return[$j])) {
$return[$j] .= substr($string, $i, $pos - $i + 1);
} else {
$return[$j] = substr($string, $i, $pos - $i + 1);
}
$i += ($pos - $i);
$j++;
continue;
}
if ((($string[$i] == "\n") || ($string[$i] == "\r")) && (strlen($return[$j]) == 0)) continue;
$return[$j] .= $string[$i];
}
return $return;
}
function array_ics($file) {
if (url($file)) $file = url_get($file); //file can be a url of a file, or contents
$file = str_replace("\r\n ", '', $file); //take care of orphans
$file = explode(NEWLINE, $file);
$events = array();
$event_counter = $line_counter = $are_tracking = 0;
foreach ($file as $line) {
$line_counter++;
if (stristr($line, ':')) {
list($key, $value) = explode(':', trim($line));
if ($value == 'VEVENT') {
if ($key == 'BEGIN') {
$are_tracking = true;
} elseif ($key == 'END') {
$event_counter++;
$are_tracking = false;
}
} elseif ($are_tracking) {
$events[$event_counter][$key] = $value;
}
} else {
error_handle(__function__ . ' had issue where line ' . $line_counter . ' contained no spaces (content was "' . $line . '")', __file__, __line__);
}
}
return $events;
}
function array_insert($array, $position, $value) {
//inserts a $value into a flat $array at a particular $position
$array_clip = array_splice($array, $position);
$array[] = $value;
return array_merge($array, $array_clip);
}
function array_insert_assoc($array, $position, $key, $value) {
//insert a $value into an associative $array at a particular $position
//todo possibly combine with array_insert where value is an array(key=>value)
$array_clip = array_splice($array, $position);
$array[$key] = $value;
return array_merge($array, $array_clip);
}
function array_instances($array, $needle) {
//returns a count of all the instances of needle in array
$count = 0;
foreach ($array as $value) if ($needle == $value) $count++;
return $count;
}
function array_key_filter($array, $key, $value) {
//returns an $array's keys of a particular value. used by PLC
//todo deprecate once PLC is backended
$return = array();
foreach ($array as $element) if ($element[$key] == $value) $return[] = $element;
return $return;
}
function array_key_group($array, $key='group') {
//take and array and
//used on living cities new home page for member list
$return = array();
foreach ($array as $a) {
if (!$key) $key = array_shift(array_keys($a));
if (isset($return[$a[$key]])) {
$return[$a[$key]][] = $a;
} else {
$return[$a[$key]] = array($a);
}
}
return $return;
}
function array_key_promote($array) {
//makes a two-column resultset into an associative array. used by draw_nav()
if (!is_array($array)) return false;
$return = array();
foreach ($array as $a) {
$keys = array_keys($a);
$return[$a[$keys[0]]] = $a[$keys[1]];
}
return $return;
}
function array_key_values($array, $key) {
//take an associative array's values for particular key and return a 1-d array of it
$return = array();
foreach ($array as $a) $return[] = $a[$key];
return $return;
}
function array_format_quotes($array) {
//beta function. using it to escape quotes coming from array_csv for upag
$return = array();
foreach ($array as $key=>$value) {
if (is_array($value)) {
$return[$key] = array_format_quotes($value);
} elseif (is_string($value)) {
$return[$key] = format_quotes($value);
}
}
return $return;
}
function array_object($object) {
//converts an object to an associative array recursively. used by array_xml()
if (is_object($object)) $object = get_object_vars($object);
return is_array($object) ? array_map(__FUNCTION__, $object) : $object;
}
function array_post_checkboxes($field_name) {
error_deprecated(__function__ . ' was deprecated on 10/20/2010 because it has been generalized into array_checkboxes');
return array_checkboxes($field_name);
}
function array_range($start, $end, $increment=1) {
//returns an array of numeric values. used by draw_form_date()
$return = array();
$increment = abs($increment);
if ($start < $end) {
while ($start <= $end) {
$return[] = $start;
$start += $increment;
}
} elseif ($start > $end) {
while ($start >= $end) {
$return[] = $start;
$start -= $increment;
}
}
return $return;
}
function array_query_string($string, $defaults=false, $separator='&') {
//coverts a key/value string eg key=value&foo=bar and returns an array
//separator is a variable because cookie strings are separated with semicolons
$return = array();
$pairs = array_separated($string, $separator);
foreach ($pairs as $p) {
list($key, $value) = array_separated(urldecode($p), '=');
$return[$key] = $value;
}
return $return;
}
function array_receive() {
//receive json data. is a pair with array_send()
return json_decode(file_get_contents('php://input'), true);
}
function array_remove($needle, $haystack) {
//removes an array element with a specific key. arguments should probably be reversed?
//is the opposite of array_filter?
$return = array();
foreach ($haystack as $value) if ($value != $needle) $return[] = $value;
return $return;
}
function array_searchwords($string) {
//return a sanitized array of words to search with or use format_highlight on
global $_josh;
//sanitize string
$string = strip_tags($string);
$string = format_accents_convert($string);
$string = preg_replace("/[^a-zA-Z0-9]+/", ' ', $string);
$string = strtolower($string);
//create array
$words = array_diff(array_unique(array_separated($string, ' ')), $_josh['ignored_words']);
return (!count($words)) ? false : $words;
}
function array_send($array, $target) {
//POST an array as a JSON post request to a remote site
global $_josh;
//prepare POSTdata
if (is_array($array)) {
//must have JSON
if (!function_exists('json_encode')) return error_handle('JSON Not Installed', 'You need the JSON library for array_send to work.', __file__, __line__);
$postdata = utf8_encode(json_encode($array));
} else {
$postdata = $array;
}
$target = url_parse($target);
//check to make sure is REMOTE host -- can't be posting to yrself
//if ($target['host'] == $_josh['request']['host']) return;
if ($pointer = fsockopen($target['host'], 80, $errno, $errstr, 30)) {
error_debug('<b>' . __function__ . '</b> has a stream to ' . $target['host'], __file__, __line__);
fputs($pointer, 'POST ' . $target['path_query'] . ' HTTP/1.0' . NEWLINE);
fputs($pointer, 'Host: ' . $target['host'] . NEWLINE);
fputs($pointer, 'Content-type: application/json; charset=utf-8' . NEWLINE);
fputs($pointer, 'Content-length: ' . strlen($postdata) . NEWLINE);
fputs($pointer, 'User-agent: Mozilla/4.0 (compatible: MSIE 7.0; Windows NT 6.0)' . NEWLINE);
fputs($pointer, 'Connection: close' . NEWLINE . NEWLINE);
fputs($pointer, $postdata);
//get server response and strip it of http headers
$response = '';
while (!feof($pointer)) $response .= fgets($pointer, 128);
fclose($pointer);
error_debug('<b>' . __function__ . '</b> was returned ' . $response, __file__, __line__);
$response = substr($response, strpos($response, '\r\n\r\n') + 4);
}
return $response;
}
function array_separated($content, $separator=',', $preserve_nulls=false) {
//returns an array from a string like explode does, but it trims values. used widely.
if (is_array($content)) return $content; //might not need splitting
$return = array();
$fields = explode($separator, $content);
foreach ($fields as $f) {
$f = trim($f);
if (!empty($f) || $preserve_nulls) $return[] = $f;
}
return $return;
}
function array_sets($items, $length=2) {
//split an array into an array of sub-arrays of $length length
$sets = array();
$count = ceil(count($items) / $length);
for ($i = 0; $i < $count; $i++) $sets[] = array_slice($items, $i * $length, $length);
return $sets;
}
function array_slice_assoc($array, $start=false, $length=false) {
//returns a subsection of an array, like array_slice, but works with associative arrays. used in NBI and PSA
$keys = array_slice(array_keys($array), $start, $length);
return array_intersect_key($array, array_2d($keys));
}
function array_sort($array, $direction='asc', $key=false) {
//sorts an associative array by its values. used by file_folder()
if (!function_exists('array_sort_asc')) {
function array_sort_asc($a, $b) {
global $_josh;
return strcmp($a[$_josh['sort_key']], $b[$_josh['sort_key']]);
}
}
if (!function_exists('array_sort_desc')) {
function array_sort_desc($a, $b) {
global $_josh;
return strcmp($b[$_josh['sort_key']], $a[$_josh['sort_key']]);
}
}
//$key defaults to be the first key
global $_josh;
$_josh['sort_key'] = ($key) ? $key : array_shift(array_keys($array[0]));
error_debug('<b>' . __function__ . '</b> running for ' . $key, __file__, __line__);
usort($array, 'array_sort_' . strToLower($direction));
unset($_josh['sort_key']);
return $array;
}
function array_to_lower($array) {
//formats a one-dimensional array in lowercase. used in format_title()
if (!is_array($array)) return false;
foreach ($array as &$a) $a = strToLower($a);
return $array;
}
function array_twitter($handle) {
if (!$xml = array_rss('https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=' . $handle)) return false;
if (!isset($xml['channel'])) return false;
foreach ($xml['channel']['item'] as &$status) {
$status['title'] = format_html_links(substr($status['title'], strpos($status['title'], ':') + 1));
//link twitter handles
$words = explode(' ', $status['title']);
foreach ($words as &$w) {
if (substr($w, 0, 1) == '@') $w = draw_link('https://twitter.com/#!/' . substr($w, 1), $w);
if (substr($w, 0, 1) == '#') $w = draw_link('https://twitter.com/#!/search?q=%23' . substr($w, 1), $w);
}
$status['title'] = implode(' ', $words);
$status['date'] = $status['pubDate'];
$status['twitter_id'] = substr($status['guid'], strrpos($status['guid'], '/') + 1);
unset($status['description']);
unset($status['guid']);
unset($status['pubDate']);
}
//die(draw_array($xml['channel']['item']));
return $xml['channel']['item'];
}
function array_random($array) {
//returns a random value from a one-dimensional array
return $array[rand(0, count($array)-1)];
}
function array_rss($url) {
//returns an associative array from a $url
if ($xml = url_get($url)) return array_xml($xml);
return false;
}
function array_value_exists($array, $needle) {
//for naomi, checks a given 1d array and returns first match
foreach ($array as $key=>$value) if ($needle == $value) return $key;
return false;
}
function array_xml($string) {
//reads xml data into an associative array
$data = new SimpleXMLElement($string);
if (is_object($data)) return array_object($data->children());
}