Subversion Repositories PHPX

Rev

Rev 2 | Rev 22 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 PointedEar 1
<?php
2
 
3
/* @section Helper functions */
4
 
5
/**
6
 * Retrieves the value of an element of an associative array.
7
 *
8
 * This is designed for $_GET and other HTTP variables but
9
 * also works for common associative arrays.
10
 *
11
 * @param $key: string
12
 *   Key identifier.  The default is the empty string.
13
 * @param $array: string
14
 *   Array identifier.  The default is '_GET'.  If there is
15
 *   no such array, the identifier is prefixed with 'HTTP'
16
 *   and suffixed with '_VARS' (to support the deprecated
17
 *   HTTP_GET_VARS etc. arrays as of PHP &lt; 4.1).  If
18
 *   there is still no array with that identifier, return
19
 *   the empty string.
20
 * @param $default: string
21
 *   Default return value if the element specified with
22
 *   $key is not available in the array.  The default
23
 *   is the empty string.
24
 * @return
25
 *   The value of the element of that array with that key or
26
 *   the empty string if there is no such element or array.
27
 * @author
28
 *  Copyright (C) 2004, 2005  Thomas Lahn &lt;php@PointedEars.de&gt;
29
 */
30
function getVars($key = '', $array = '_GET', $default = '', $noEntities = false)
31
{
32
  global ${$array};
33
  if (!isset(${'HTTP'.$array.'_VARS'})) global ${'HTTP'.$array.'_VARS'};
34
/*
35
  echo "<pre>getVars: \$$array"."['$key']: return '"
36
    .(isset(${$array}) && isset(${$array}[$key])
37
    ? ${$array}[$key]
38
    : (isset(${'HTTP'.$array.'_VARS'}) && isset(${'HTTP'.$array.'_VARS'}[$key])
39
       ? ${'HTTP'.$array.'_VARS'}[$key]
40
       : $default)) . "'</pre><br>\n";
41
*/
42
  $result = (isset(${$array}) && isset(${$array}[$key])
43
    ? ${$array}[$key]
44
    : (isset(${'HTTP'.$array.'_VARS'}) && isset(${'HTTP'.$array.'_VARS'}[$key])
45
       ? ${'HTTP'.$array.'_VARS'}[$key]
46
       : $default));
47
 
48
// TODO: Escape HTML entities
49
/*
50
  if (!$noEntities)
51
  {
52
    $result = htmlentities($result);
53
  }
54
*/
55
  return $result;
56
}
57
 
58
/**
59
 * Converts the argument to a visible (X)HTML hyperlink
60
 * where its URI target is created from the argument.
61
 * Supported are e-mail addresses, domain names with
62
 * optional paths, and valid URIs.
63
 *
64
 * @param $text
65
 *   Argument to be converted.
66
 * @return
67
 *   The converted argument if it applies to a supported
68
 *   scheme, the unconverted argument otherwise.
69
 *
70
 * @author (C) 2001-04-04T02:03
71
 *   mark.young@vdhinc.com at http://php.net/manual/en/ref.strings.php
72
 *
73
 * Minor correction to my HTMLEncode function.
74
 *
75
 * @author 2002-08-29T09:00
76
 *   Thomas Lahn &lt;PointedEars@selfhtml.de&gt; at localhost
77
 *
78
 * - Added target="_blank"
79
 * - Added support for ftp(s)-URIs: (ht|f)
80
 * - Added support for search strings (?...=...&...=...), either
81
 *   with or without HTML entities: \?=(&\w;|&)
82
 * - Removed enclosing nl2br call because of preformatted display
83
 *
84
 * @author 2003-12-30T14:18
85
 *   Thomas Lahn &lt;PointedEars@selfhtml.de&gt; at localhost
86
 *
87
 * - Removed target="_blank".
88
 * - Added PHPdoc.
89
 *
90
 * @author 2004-01-12T12:45
91
 *   Thomas Lahn &lt;PointedEars@selfhtml.de&gt; at localhost
92
 *
93
 * - Added support for #fragment_identifiers in URIs.
94
 * - Added support for bugs and comments.
95
 *
96
 * @author 2004-01-13T01:29
97
 *   Thomas Lahn &lt;PointedEars@selfhtml.de&gt; at localhost
98
 *
99
 * - Added support for bug aliases.
100
 *
101
 * @author 2004-01-26T20:43
102
 *   Thomas Lahn &lt;PointedEars@selfhtml.de&gt; at localhost
103
 *
104
 * - Do not convert URIs in A elements.
105
 * - Do not allow URIs with "&" before search-string.
106
 * - camelCased function identifier.  Only classes
107
 *   and constructors should start with uppercase.
108
 *
109
 * @author 2004-01-27T14:07
110
 *   Thomas Lahn &lt;PointedEars@selfhtml.de&gt; at localhost
111
 *
112
 * - Allow to convert URIs preceded by "&gt;" but not followed by "&lt;/a&gt;".
113
 * - Allow ";" to be part of the search string
114
 *
115
 * @author 2004-01-29T14:10
116
 *   Thomas Lahn &lt;PointedEars@selfhtml.de&gt; at localhost
117
 *
118
 * - Require valid domain name for "bar" on conversion of "foo@bar" and
119
 *   "www.bar".
120
 * - Be case-insensitive except of bug aliases
121
 * - Escaped "-" in character classes if not meant as range metacharacter.
122
 * - Corrected year.
123
 *
124
 * @author 2004-02-14T17:37
125
 *   Thomas Lahn &lt;PointedEars@selfhtml.de&gt; at localhost
126
 *
127
 * - Accept only valid Internet domain names
128
 * - Accept "%" within path and query part (to escape ASCII characters)
129
 *
130
 * @author 2004-02-27T19:21
131
 *   Thomas Lahn &lt;PointedEars@selfhtml.de&gt; at localhost
132
 *
133
 * - Allow unescaped ":" in URI components, since it apparently does not
134
 *   "conflict with the reserved purpose" (RFC 2396, section 2.2.; here:
135
 *   scheme component)
136
 * - Allow slashes, dots and dashes in URI components
137
 * - Removed invalid [...(...|...)...]
138
 *
139
 * @author 2004-03-01T21:48
140
 *   Thomas Lahn &lt;PointedEars@selfhtml.de&gt; at localhost
141
 *
142
 * - Allow IPv4 addresses
143
 *
144
 * @author 2004-03-08T02:20
145
 *   Thomas Lahn &lt;PointedEars@selfhtml.de&gt; at localhost
146
 *
147
 * - Allow "+" and "," in query part
148
 * - Optimized character classes
149
 *
150
 * @author (C) 2004-04-23T10:03
151
 *   Thomas Lahn &lt;PointedEars@selfhtml.de&gt; at localhost
152
 *
153
 * - Rewrite to use RFC 2822 and 2369 syntax
154
 */
155
function htmlEncode($text)
156
{
157
  // see RFC 2822 "Internet Message Format"
158
  $local_part    = '[^()<>@,;:\\"\-\[\] \x00-\x1A\x7F]+';
159
 
160
  // see RFC 2396 "Uniform Resource Identifiers (URI): Generic Syntax"
161
  // $digit      = '\d';          // 0-9; def. not required
162
  // $hex        = '\da-f';     // "hex" for case-insensitive match; def. N/R
163
  $alpha         = 'a-z';         // "alpha" for case-insensitive match
164
  $alphanum      = $alpha.'\d';   // "alphanum" for case-insensitive match
165
  $mark          = "\-_.!~*\'()";
166
  $reserved      = ';/?:@&=+$,';
167
  $unreserved    = $alphanum.$mark;
168
  $escaped       = '%[\da-f]';    // contains $hex
169
 
170
  // added (?!gt;) to allow "&lt;URI&gt;"
171
  $uric          = '(['.$reserved.$alphanum.$mark.'](?!gt;)|'.$escaped.')';
172
  $uric_no_slash = '(['.$unreserved.';\?:@&=+$,](?!gt;)|'.$escaped.')';
173
  $pchar         = '(['.$unreserved.':@&=+$,](?!gt;)|'.$escaped.')';
174
  $param         = $pchar;
175
  $segment       = $pchar.'*(;'.$param.')*';
176
  $abs_path      = '/' . $segment . '(/'.$segment.')*';
177
  $userinfo      = '(['.$unreserved.';:&=+$,](?!gt;)|'.$escaped.')*';
178
  $domainlabel   = '(['.$alphanum.']'
179
                   . '|['.$alphanum.'](['.$alphanum.']|-)*['.$alphanum.']'
180
                   . ')';
181
  $toplabel      = '(['.$alpha.']'
182
                   . '|['.$alpha.'](['.$alphanum.']|-)*['.$alphanum. '])';
183
  $hostname      = '('.$domainlabel. '\.)*' . $toplabel . '\.?';
184
  $ipv4_address  = '\d+\.\d+\.\d+\.\d+';
185
  $host          = '(' . $hostname . '|' . $ipv4_address . ')';
186
  $port          = '\d*';
187
  $hostport      = $host . '(:' . $port . ')?';
188
  $server_req    = '(' . $userinfo . ')?' . $hostport; // server is required
189
  $reg_name      = '([' . $unreserved . '$,;:@&=+](?!gt;)|' . $escaped . ')+';
190
  $authority     = '(' . $server_req . '|' . $reg_name . ')';
191
  $net_path      = '//' . $authority . '('.$abs_path.')?';
192
  $query         = $uric.'*';
193
  $scheme        = '(ht|f)tps?';
194
  $hier_part     = '(' . $net_path  .'|' . $abs_path . ')(\?' . $query . ')?';
195
  $opaque_part   = $uric_no_slash . $uric.'*';
196
  $absolute_uri  = $scheme . ':(' . $hier_part . '|' . $opaque_part . ')';
197
  $fragment      = $uric.'*';
198
 
199
  // absolute URIs only
200
  $uri_reference = $absolute_uri . '(#' . $fragment . ')?';
201
  // echo '<br>'.htmlentities($local_part . '@' . $host).'<br>';
202
 
203
  $searcharray = array(
204
    "'(?i)(" . $local_part . '@' . $host . ")'i",
205
    "'(?i)((?:(?!://).{3}|^.{0,2}))(www\." . $hostname
206
      . '|' . $ipv4_address . ")'i",
207
    "'(?i)(?<!href=\")(" . $uri_reference . ")(?!</a>)'i",
208
    "'(((?i)bug)\s+#?([\dA-Z_]+)\s+((?i)comment|Kommentar)\s+#?(\d+))'",
209
    "'(((?i)bug)\s+#?([\dA-Z_]+))'",
210
    "'(((?i)comment|Kommentar)\s+#?(\d+))'"
211
  );
212
 
213
  $replacearray = array(
214
    "<a href=\"mailto:\\1\">\\1</a>",
215
    "\\1http://\\2",
216
    "<a href=\"\\1\">\\1</a>",
217
    "<a href=\"./?bug=\\3#c\\5\">\\1</a>",
218
    "<a href=\"./?bug=\\3#details\">\\1</a>",
219
    "<a href=\"#c\\3\">\\1</a>"
220
  );
221
 
222
  return preg_replace($searcharray, $replacearray, $text);
223
}
224
 
225
/**
226
 * Converts the argument into a visible (X)HTML hyperlink if a condition
227
 * applies.
228
 *
229
 * @author
230
 *   (C) 2003, 2004  Thomas Lahn &lt;selfhtml.de@PointedEars.de&gt;
231
 * @param $s
232
 *   Content to be converted.  Required.
233
 * @param $sCond
234
 *   Condition to be true for the content to be converted.
235
 *   The default is <code>true</code>.
236
 * @param $sURI
237
 *   Target URI of the hyperlink.  The default is the
238
 *   value of $s.
239
 * @param $sName
240
 *   Value of the <code>name</code> attribute of the
241
 *   <code>a</code> element.  Unused if not provided
242
 *   or empty.
243
 * @param $sTitle
244
 *   Value of the <code>title</code> attribute of the
245
 *   <code>a</code> element.  Unused if not provided
246
 *   or empty.
247
 * @param $sClass
248
 *   Value of the <code>class</code> attribute of the
249
 *   <code>a</code> element.  Unused if not provided
250
 *   or empty.
251
 * @return
252
 *   The converted argument if the condition applies,
253
 *   the unconverted argument otherwise.
254
 */
255
function makeLinkIf(
256
  $s,
257
  $sCond = true,
258
  $sURI = NULL,
259
  $sTarget = '',
260
  $sName = '',
261
  $sTitle = '',
262
  $sClass = '')
263
{
264
  return ($sCond || $sName
265
          ? '<a' . ($sCond
266
                    ? " href=\"" . (is_null($sURI) ? $s : $sURI)
267
                      . "\"".($sTarget ? " target=\"$sTarget\"" : '')
268
                    : ''
269
                   )
270
            . ($sName  ? " name=\"$sName\""   : '')
271
            . ($sTitle ? " title=\"$sTitle\"" : '')
272
            . ($sClass ? " class=\"$sClass\"" : '')
273
            . '>'
274
          : ''
275
         )
276
         . $s
277
         . ($sCond || $sName ? '</a>' : '');
278
}
279
 
280
/**
281
 * Returns a visible (X)HTML hyperlink that uses the mailto: URI scheme.
282
 *
283
 * @author (C) 2003  Thomas Lahn &lt;selfhtml.de@PointedEars.de&gt;
284
 *
285
 * @author (C) 2003-12-30  Thomas Lahn &lt;selfhtml.de@PointedEars.de&gt;
286
 * - Corrected `$email ($name)'.
287
 * - Now uses rawurlencode(...).
288
 * - Added PHPdoc.
289
 *
290
 * @author (C) 2004-11-30  Thomas Lahn &lt;selfhtml.de@PointedEars.de&gt;
291
 * - Don't rawurlencode(...) parens for comments.
292
 *
293
 * @param $sAddress
294
 *   E-mail address.  The default is <selfhtml.de@PointedEars.de>.
295
 * @param $sTitle
296
 *   Value of the <code>title</code> attribute of the
297
 *   <code>a</code> element.  Unused if not provided
298
 *   or empty.  The default is 'PointedEars'.
299
 * @param $sToName
300
 *   Name to be used in the To header of the e-mail.
301
 *   Note that @link{rawurlencode()} is used to escape
302
 *   special characters automatically.
303
 *   The default is "Thomas 'PointedEars' Lahn".
304
 *
305
 * @return
306
 *   The converted argument if the condition applies,
307
 *   the unconverted argument otherwise.
308
 */
309
function mailto_link(
310
  $sAddress = 'selfhtml.de@PointedEars.de',
311
  $sTitle = 'PointedEars',
312
  $sToName = "Thomas 'PointedEars' Lahn",
313
  $sSubject = 'SELFbug',
314
  $sImgPath = '../../media/mail.gif'
315
)
316
{
317
//width="14" height="15" // image size detection not yet implemented
318
  return ($sImgPath != ''
319
          ? '<img src="' . $sImgPath . '" border="0" alt="@">&nbsp;'
320
          : '')
321
    . '<a href="mailto:'
322
    . ($sAddress != ''
323
       ? $sAddress
324
       : 'selfhtml.de@PointedEars.de')
325
    . ($sToName != ''
326
       ? ' (' . rawurlencode($sToName) . ')'
327
       : '')
328
    . ($sSubject != ''
329
       ? '?subject=' . rawurlencode($sSubject)
330
       : '')
331
    . '">' . (($sTitle != '') ? $sTitle : $sAddress) . '</a>';
332
}
333
 
334
// map bug states to numbers so that they become comparable
335
function array_values_to_keys($a)
336
{
337
  $aNew = array();
338
  foreach ($a as $key => $value)
339
  {
340
    $aNew[$value] = count($aNew);
341
  }
342
  return $aNew;
343
}
344
 
345
/**
346
 * Converts a string or an array of strings to an associative
347
 * bitmask array with the string(s) as key(s).
348
 *
349
 * Converts the argument to a bitmask array where each member's
350
 * value is a power of 2, so that arbitrary member values can be
351
 * added to an integer on which bitwise operations with the member
352
 * value or a combination of member values are possible.
353
 *
354
 * @author (c) 2003 Thomas Lahn &lt;SELFbug@PointedEars.de&gt;
355
 * @param $aArray
356
 *   String or array of strings to be converted.
357
 */
358
function getBitmaskArray($aArray)
359
{
360
  $a = array();
361
 
362
  if (is_array($aArray))
363
  {
364
    for ($i = 0; $i < count($aArray); $i++)
365
    {
366
      $a[$aArray[$i]] = pow(2, $i);
367
    }
368
  }
369
  else
370
    $a[$aArray] = 1;
371
 
372
  return $a;
373
}
374
 
375
/**
376
 * Returns the contents of a file as if include() was used.
377
 *
378
 * @param string $filename Path of the file to retrieve
379
 * @return string File contents
380
 */
381
function get_include_content($filename)
382
{
383
  if (is_file($filename))
384
  {
385
    ob_start();
386
    include $filename;
387
    $contents = ob_get_contents();
388
    ob_end_clean();
389
    return $contents;
390
  }
391
 
392
  return '';
393
}
394
 
395
/**
396
 * Replaces each group of expressions in a string with the same
397
 * corresponding string.
398
 *
399
 * @param Array[Array[string] | string, string] $map
400
 * @param string                                $subject
401
 * @return string
402
 *   A copy of $subject with the provided mapping applied.
403
 */
404
function preg_replace_group($map = array(), $subject = '')
405
{
406
  if ($subject)
407
  {
408
    for ($i = 0, $len = count($map); $i < $len; $i++)
409
    {
410
      $subject = preg_replace($map[$i][0], $map[$i][1], $subject);
411
    }
412
  }
413
 
414
  return $subject;
415
}
416
 
417
/**
418
 * Randomly encodes a string of characters.
419
 *
420
 * @param string $s
421
 *   String to be encoded
422
 * @param string $format = 'sgml'
423
 *   Encoding format.  Currently only SGML-based encoding of
424
 *   ASCII characters with character references is supported.
425
 * @return string
426
 */
427
function randomEsc($s = '', $format = 'sgml')
428
{
429
  $f = function_exists('mt_rand') ? 'mt_rand' : 'rand';
430
 
431
  return preg_replace_callback('/[\\x00-\\x7F]/',
432
    create_function('$m', "return $f(0, 1)" . '? $m[0] : "&#" . ord($m[0]) . ";";'),
433
    $s);
434
}
435
 
13 PointedEar 436
/**
437
 * Reduces sequences of two or more consecutive white-space characters
438
 * in an input to a single space.
439
 *
440
 * @param string $s
441
 * @return string
442
 */
443
function reduceWhitespace($s)
444
{
445
  return preg_replace('/\s{2,}/', ' ', $s);
446
}
447
 
2 PointedEar 448
?>