-
Notifications
You must be signed in to change notification settings - Fork 127
/
Horde.php
1217 lines (1093 loc) · 41.6 KB
/
Horde.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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Provides the base functionality shared by all Horde applications.
*
* Copyright 1999-2015 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Jon Parise <jon@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/lgpl21 LGPL
* @package Core
*/
class Horde
{
const SSL_NEVER = 0;
const SSL_ALWAYS = 1;
const SSL_AUTO = 2;
const SSL_ONLY_LOGIN = 3;
/**
* The current buffer level.
*
* @var integer
*/
protected static $_bufferLevel = 0;
/**
* Has content been sent at the base buffer level?
*
* @var boolean
*/
protected static $_contentSent = false;
/**
* The labels already used in this page.
*
* @var array
*/
protected static $_labels = array();
/**
* Are accesskeys supported on this system.
*
* @var boolean
*/
protected static $_noAccessKey;
/**
* The access keys already used in this page.
*
* @var array
*/
protected static $_used = array();
/**
* Shortcut to logging method.
*
* @see Horde_Core_Log_Logger
*/
public static function log($event, $priority = null,
array $options = array())
{
$options['trace'] = isset($options['trace'])
? ($options['trace'] + 1)
: 1;
$log_ob = new Horde_Core_Log_Object($event, $priority, $options);
/* Chicken/egg: we must wait until we have basic framework setup
* before we can start logging. Otherwise, queue entries. */
if (isset($GLOBALS['injector']) &&
Horde_Core_Factory_Logger::available()) {
$GLOBALS['injector']->getInstance('Horde_Log_Logger')->logObject($log_ob);
} else {
Horde_Core_Factory_Logger::queue($log_ob);
}
}
/**
* Shortcut to logging method.
*
* @deprecated Use log() instead
* @see log()
*/
public static function logMessage($event, $priority = null,
array $options = array())
{
$options['trace'] = isset($options['trace'])
? ($options['trace'] + 1)
: 1;
self::log($event, $priority, $options);
}
/**
* Debug method. Allows quick shortcut to produce debug output into a
* temporary file.
*
* @param mixed $event Item to log.
* @param string $fname Filename to log to. If empty, logs to
* 'horde_debug.txt' in the PHP temporary
* directory.
* @param boolean $backtrace Include backtrace information?
*/
public static function debug($event = null, $fname = null,
$backtrace = true)
{
if (is_null($fname)) {
$fname = self::getTempDir() . '/horde_debug.txt';
}
try {
$logger = new Horde_Log_Logger(new Horde_Log_Handler_Stream($fname));
} catch (Exception $e) {
return;
}
$html_ini = ini_set('html_errors', 'Off');
self::startBuffer();
if (!is_null($event)) {
echo "Variable information:\n";
var_dump($event);
echo "\n";
}
if (is_resource($event)) {
echo "Stream contents:\n";
rewind($event);
fpassthru($event);
echo "\n";
}
if ($backtrace) {
echo "Backtrace:\n";
echo strval(new Horde_Support_Backtrace());
}
$logger->log(self::endBuffer(), Horde_Log::DEBUG);
ini_set('html_errors', $html_ini);
}
/**
* Add a signature + timestamp to a query string and return the signed
* query string.
*
* @param mixed $queryString The query string (or Horde_Url object)
* to sign.
* @param integer $now The timestamp at which to sign. Leave blank
* for generating signatures; specify when
* testing.
*
* @return mixed The signed query string (or Horde_Url object).
*/
public static function signQueryString($queryString, $now = null)
{
if (!isset($GLOBALS['conf']['secret_key'])) {
return $queryString;
}
if (is_null($now)) {
$now = time();
}
if ($queryString instanceof Horde_Url) {
$queryString->setRaw(true)->add(array('_t' => $now, '_h' => ''));
$parse_url = parse_url($queryString);
$queryString->add('_h', Horde_Url::uriB64Encode(hash_hmac('sha1', $parse_url['query'] . '=', $GLOBALS['conf']['secret_key'], true)));
return $queryString;
}
$queryString .= '&_t=' . $now . '&_h=';
return $queryString . Horde_Url::uriB64Encode(hash_hmac('sha1', $queryString, $GLOBALS['conf']['secret_key'], true));
}
/**
* Verify a signature and timestamp on a query string.
*
* @param string $data The signed query string.
* @param integer $now The current time (can override for testing).
*
* @return boolean Whether or not the string was valid.
*/
public static function verifySignedQueryString($data, $now = null)
{
if (is_null($now)) {
$now = time();
}
$pos = strrpos($data, '&_h=');
if ($pos === false) {
return false;
}
$pos += 4;
$queryString = substr($data, 0, $pos);
$hmac = substr($data, $pos);
if ($hmac != Horde_Url::uriB64Encode(hash_hmac('sha1', $queryString, $GLOBALS['conf']['secret_key'], true))) {
return false;
}
// String was not tampered with; now validate timestamp
parse_str($queryString, $values);
return !($values['_t'] + $GLOBALS['conf']['urls']['hmac_lifetime'] * 60 < $now);
}
/**
* Do necessary escaping to output JSON.
*
* @param mixed $data The data to JSON-ify.
* @param array $options Additional options:
* - nodelimit: (boolean) Don't add security delimiters?
* DEFAULT: false
* - urlencode: (boolean) URL encode the json string
* DEFAULT: false
*
* @return string The escaped string.
*/
public static function escapeJson($data, array $options = array())
{
$json = Horde_Serialize::serialize($data, Horde_Serialize::JSON);
if (empty($options['nodelimit'])) {
$json = '/*-secure-' . $json . '*/';
}
return empty($options['urlencode'])
? $json
: '\'' . rawurlencode($json) . '\'';
}
/**
* Is the current HTTP connection considered secure?
* @TODO Move this to the request classes!
*
* @return boolean
*/
public static function isConnectionSecure()
{
global $browser, $conf, $registry;
if ($browser->usingSSLConnection()) {
return true;
}
if (!empty($conf['safe_ips'])) {
if (reset($conf['safe_ips']) == '*') {
return true;
}
/* $_SERVER['HTTP_X_FORWARDED_FOR'] is user data and not
* reliable. We don't consult it for safe IPs. We also have to
* assume that if it is present, the user is coming through a proxy
* server. If so, we don't count any non-SSL connection as safe, no
* matter the source IP. */
$remote = $registry->remoteHost();
if (!$remote->proxy) {
foreach ($conf['safe_ips'] as $safe_ip) {
$safe_ip = preg_replace('/(\.0)*$/', '', $safe_ip);
if (strpos($remote->addr, $safe_ip) === 0) {
return true;
}
}
}
}
return false;
}
/**
* Throws an exception if not using a secure connection.
*
* @throws Horde_Exception
*/
public static function requireSecureConnection()
{
if (!self::isConnectionSecure()) {
throw new Horde_Exception(Horde_Core_Translation::t("The encryption features require a secure web connection."));
}
}
/**
* Returns the driver parameters for the specified backend.
*
* @param mixed $backend The backend system (e.g. 'prefs', 'categories',
* 'contacts') being used.
* The used configuration array will be
* $conf[$backend]. If an array gets passed, it will
* be $conf[$key1][$key2].
* @param string $type The type of driver. If null, will not merge with
* base config.
*
* @return array The connection parameters.
*/
public static function getDriverConfig($backend, $type = 'sql')
{
global $conf;
if (!is_null($type)) {
$type = Horde_String::lower($type);
}
if (is_array($backend)) {
$c = Horde_Array::getElement($conf, $backend);
} elseif (isset($conf[$backend])) {
$c = $conf[$backend];
} else {
$c = null;
}
if (!is_null($c) && isset($c['params'])) {
$c['params']['umask'] = $conf['umask'];
return (!is_null($type) && isset($conf[$type]))
? array_merge($conf[$type], $c['params'])
: $c['params'];
}
return (!is_null($type) && isset($conf[$type]))
? $conf[$type]
: array();
}
/**
* Checks if all necessary parameters for a driver configuration
* are set and throws a fatal error with a detailed explanation
* how to fix this, if something is missing.
*
* @param array $params The configuration array with all parameters.
* @param string $driver The key name (in the configuration array) of
* the driver.
* @param array $fields An array with mandatory parameter names for
* this driver.
* @param string $name The clear text name of the driver. If not
* specified, the application name will be used.
* @param string $file The configuration file that should contain
* these settings.
* @param string $variable The name of the configuration variable.
*
* @throws Horde_Exception
*/
public static function assertDriverConfig($params, $driver, $fields,
$name = null,
$file = 'conf.php',
$variable = '$conf')
{
global $registry;
// Don't generate a fatal error if we fail during or before
// Registry instantiation.
if (is_null($name)) {
$name = isset($registry) ? $registry->getApp() : '[unknown]';
}
$fileroot = isset($registry) ? $registry->get('fileroot') : '';
if (!is_array($params) || !count($params)) {
throw new Horde_Exception(
sprintf(Horde_Core_Translation::t("No configuration information specified for %s."), $name) . "\n\n" .
sprintf(Horde_Core_Translation::t("The file %s should contain some %s settings."),
$fileroot . '/config/' . $file,
sprintf("%s['%s']['params']", $variable, $driver)));
}
foreach ($fields as $field) {
if (!isset($params[$field])) {
throw new Horde_Exception(
sprintf(Horde_Core_Translation::t("Required \"%s\" not specified in %s configuration."), $field, $name) . "\n\n" .
sprintf(Horde_Core_Translation::t("The file %s should contain a %s setting."),
$fileroot . '/config/' . $file,
sprintf("%s['%s']['params']['%s']", $variable, $driver, $field)));
}
}
}
/**
* Returns a session-id-ified version of $uri.
* If a full URL is requested, all parameter separators get converted to
* "&", otherwise to "&".
*
* @param mixed $uri The URI to be modified (either a string or any
* object with a __toString() function).
* @param boolean $full Generate a full (http://server/path/) URL.
* @param mixed $opts Additional options. If a string/integer, it is
* taken to be the 'append_session' option. If an
* array, one of the following:
* - app: (string) Use this app for the webroot.
* DEFAULT: current application
* - append_session: (integer) 0 = only if needed [DEFAULT], 1 = always,
* -1 = never.
* - force_ssl: (boolean) Ignore $conf['use_ssl'] and force creation of
* a SSL URL?
* DEFAULT: false
*
* @return Horde_Url The URL with the session id appended (if needed).
*/
public static function url($uri, $full = false, $opts = array())
{
if (is_array($opts)) {
$append_session = isset($opts['append_session'])
? $opts['append_session']
: 0;
if (!empty($opts['force_ssl'])) {
$full = true;
}
} else {
$append_session = $opts;
$opts = array();
}
$puri = parse_url($uri);
/* @todo Fix for PHP < 5.3.6 */
if (isset($puri['fragment']) && !isset($puri['path'])) {
$pos = strpos(
$uri,
'/',
strpos($uri, $puri['host']) + strlen($puri['host'])
);
$puri['path'] = substr($uri, $pos, strpos($uri, '#', $pos) - $pos);
}
/* End fix */
$url = '';
$schemeRegexp = '|^([a-zA-Z][a-zA-Z0-9+.-]{0,19})://|';
$webroot = ltrim($GLOBALS['registry']->get('webroot', empty($opts['app']) ? null : $opts['app']), '/');
if ($full &&
!isset($puri['scheme']) &&
!preg_match($schemeRegexp, $webroot) ) {
/* Store connection parameters in local variables. */
$server_name = $GLOBALS['conf']['server']['name'];
$server_port = isset($GLOBALS['conf']['server']['port']) ? $GLOBALS['conf']['server']['port'] : '';
$protocol = 'http';
switch ($GLOBALS['conf']['use_ssl']) {
case self::SSL_ALWAYS:
$protocol = 'https';
break;
case self::SSL_AUTO:
if ($GLOBALS['browser']->usingSSLConnection()) {
$protocol = 'https';
}
break;
case self::SSL_ONLY_LOGIN:
if (!empty($opts['force_ssl'])) {
$protocol = 'https';
$server_port = '';
}
break;
}
/* If using a non-standard port, add to the URL. */
if (!empty($server_port) &&
((($protocol == 'http') && ($server_port != 80)) ||
(($protocol == 'https') && ($server_port != 443)))) {
$server_name .= ':' . $server_port;
}
$url = $protocol . '://' . $server_name;
} elseif (isset($puri['scheme'])) {
$url = $puri['scheme'] . '://' . $puri['host'];
/* If using a non-standard port, add to the URL. */
if (isset($puri['port']) &&
((($puri['scheme'] == 'http') && ($puri['port'] != 80)) ||
(($puri['scheme'] == 'https') && ($puri['port'] != 443)))) {
$url .= ':' . $puri['port'];
}
}
if (isset($puri['path']) &&
(substr($puri['path'], 0, 1) == '/') &&
(!preg_match($schemeRegexp, $webroot) ||
(preg_match($schemeRegexp, $webroot) && isset($puri['scheme'])))) {
$url .= $puri['path'];
} elseif (isset($puri['path']) && preg_match($schemeRegexp, $webroot)) {
if (substr($puri['path'], 0, 1) == '/') {
$pwebroot = parse_url($webroot);
$url = $pwebroot['scheme'] . '://' . $pwebroot['host']
. $puri['path'];
} else {
$url = $webroot . '/' . $puri['path'];
}
} else {
$url .= '/' . ($webroot ? $webroot . '/' : '') . (isset($puri['path']) ? $puri['path'] : '');
}
if (isset($puri['query'])) {
$url .= '?' . $puri['query'];
}
if (isset($puri['fragment'])) {
$url .= '#' . $puri['fragment'];
}
$ob = new Horde_Url($url, $full);
if (empty($GLOBALS['conf']['session']['use_only_cookies']) &&
(($append_session == 1) ||
(($append_session == 0) && !isset($_COOKIE[session_name()])))) {
$ob->add(session_name(), session_id());
}
return $ob;
}
/**
* Returns an external link passed through the dereferrer to strip session
* IDs from the referrer.
*
* @param string $url The external URL to link to.
* @param boolean $tag If true, a complete <a> tag is returned, only the
* url otherwise.
*
* @return string The link to the dereferrer script.
*/
public static function externalUrl($url, $tag = false)
{
if (!isset($_GET[session_name()]) ||
Horde_String::substr($url, 0, 1) == '#' ||
Horde_String::substr($url, 0, 7) == 'mailto:') {
$ext = $url;
} else {
$ext = self::signQueryString($GLOBALS['registry']->getServiceLink('go', 'horde')->add('url', $url));
}
if ($tag) {
$ext = self::link($ext, $url, '', '_blank');
}
return $ext;
}
/**
* Returns an anchor tag with the relevant parameters
*
* @param Horde_Url|string $url The full URL to be linked to.
* @param string $title The link title/description.
* @param string $class The CSS class of the link.
* @param string $target The window target to point to.
* @param string $onclick JavaScript action for the 'onclick' event.
* @param string $title2 The link title (tooltip) (deprecated - just
* use $title).
* @param string $accesskey The access key to use.
* @param array $attributes Any other name/value pairs to add to the
* <a> tag.
* @param boolean $escape Whether to escape special characters in the
* title attribute.
*
* @return string The full <a href> tag.
*/
public static function link($url = '', $title = '', $class = '',
$target = '', $onclick = '', $title2 = '',
$accesskey = '', $attributes = array(),
$escape = true)
{
if (!($url instanceof Horde_Url)) {
$url = new Horde_Url($url);
}
if (!empty($title2)) {
$title = $title2;
}
if (!empty($onclick)) {
$attributes['onclick'] = $onclick;
}
if (!empty($class)) {
$attributes['class'] = $class;
}
if (!empty($target)) {
$attributes['target'] = $target;
}
if (!empty($accesskey)) {
$attributes['accesskey'] = $accesskey;
}
if (!empty($title)) {
if ($escape) {
$title = str_replace(
array("\r", "\n"), '',
htmlspecialchars(nl2br(htmlspecialchars($title))));
/* Remove double encoded entities. */
$title = preg_replace('/&([a-z]+|(#\d+));/i', '&\\1;', $title);
}
$attributes['title.raw'] = $title;
}
return $url->link($attributes);
}
/**
* Uses DOM Tooltips to display the 'title' attribute for link() calls.
*
* @param string $url The full URL to be linked to
* @param string $status The JavaScript mouse-over string
* @param string $class The CSS class of the link
* @param string $target The window target to point to.
* @param string $onclick JavaScript action for the 'onclick' event.
* @param string $title The link title (tooltip). Most not contain
* HTML data other than <br>, which will
* be converted to a linebreak.
* @param string $accesskey The access key to use.
* @param array $attributes Any other name/value pairs to add to the
* <a> tag.
*
* @return string The full <a href> tag.
*/
public static function linkTooltip($url, $status = '', $class = '',
$target = '', $onclick = '',
$title = '', $accesskey = '',
$attributes = array())
{
if (strlen($title)) {
$attributes['nicetitle'] = Horde_Serialize::serialize(explode("\n", preg_replace('/<br\s*\/?\s*>/', "\n", $title)), Horde_Serialize::JSON);
$title = null;
$GLOBALS['injector']->getInstance('Horde_PageOutput')->addScriptFile('tooltips.js', 'horde');
}
return self::link($url, $title, $class, $target, $onclick, null, $accesskey, $attributes, false);
}
/**
* Returns an anchor sequence with the relevant parameters for a widget
* with accesskey and text.
*
* @param array $params A hash with widget options (other options will be
* passed as attributes to the link tag):
* - url: (string) The full URL to be linked to.
* - title: (string) The link title/description.
* - nocheck: (boolean, optional) Don't check if the accesskey already
* already has been used.
* Defaults to false (= check).
*
* @return string The full <a href>Title</a> sequence.
*/
public static function widget($params)
{
$params = array_merge(
array(
'class' => '',
'target' => '',
'onclick' => '',
'nocheck' => false),
$params
);
$url = ($params['url'] instanceof Horde_Url)
? $params['url']
: new Horde_Url($params['url']);
$title = $params['title'];
$params['accesskey'] = self::getAccessKey($title, $params['nocheck']);
unset($params['url'], $params['title'], $params['nocheck']);
return $url->link($params)
. self::highlightAccessKey($title, $params['accesskey'])
. '</a>';
}
/**
* Returns a session-id-ified version of $SCRIPT_NAME resp. $PHP_SELF.
*
* @param boolean $script_params Include script parameters like
* QUERY_STRING and PATH_INFO?
* (Deprecated: use Horde::selfUrlParams()
* instead.)
* @param boolean $nocache Include a cache-buster parameter in the
* URL?
* @param boolean $full Return a full URL?
* @param boolean $force_ssl Ignore $conf['use_ssl'] and force creation
* of a SSL URL?
*
* @return Horde_Url The requested URL.
*/
public static function selfUrl($script_params = false, $nocache = true,
$full = false, $force_ssl = false)
{
if (!strncmp(PHP_SAPI, 'cgi', 3)) {
// When using CGI PHP, SCRIPT_NAME may contain the path to
// the PHP binary instead of the script being run; use
// PHP_SELF instead.
$url = $_SERVER['PHP_SELF'];
} else {
$url = isset($_SERVER['SCRIPT_NAME']) ?
$_SERVER['SCRIPT_NAME'] :
$_SERVER['PHP_SELF'];
}
if (isset($_SERVER['REQUEST_URI'])) {
$url = Horde_String::common($_SERVER['REQUEST_URI'], $url);
}
if (substr($url, -9) == 'index.php') {
$url = substr($url, 0, -9);
}
if ($script_params) {
if ($pathInfo = Horde_Util::getPathInfo()) {
if (substr($url, -1) == '/') {
$pathInfo = substr($pathInfo, 1);
}
$url .= $pathInfo;
}
if (!empty($_SERVER['QUERY_STRING'])) {
$url .= '?' . $_SERVER['QUERY_STRING'];
}
}
$url = self::url($url, $full, array('force_ssl' => $force_ssl));
return ($nocache && $GLOBALS['browser']->hasQuirk('cache_same_url'))
? $url->unique()
: $url;
}
/**
* Create a self URL of the current page, building the parameter list from
* the current Horde_Variables object (or via another Variables object
* passed as an optional argument) rather than the original request data.
*
* @since 2.3.0
*
* @param array $opts Additional options:
* - force_ssl: (boolean) Force creation of an SSL URL?
* DEFAULT: false
* - full: (boolean) Return a full URL?
* DEFAULT: false
* - nocache: (boolean) Include a cache-buster parameter in the URL?
* DEFAULT: true
* - vars: (Horde_Variables) Use this Horde_Variables object instead of
* the Horde global object.
* DEFAULT: Use the Horde global object.
*
* @return Horde_Url The self URL.
*/
public static function selfUrlParams(array $opts = array())
{
$vars = isset($opts['vars'])
? $opts['vars']
: $GLOBALS['injector']->createInstance('Horde_Variables');
$url = self::selfUrl(
false,
(!array_key_exists('nocache', $opts) || empty($opts['nocache'])),
!empty($opts['full']),
!empty($opts['force_ssl'])
)->add(iterator_to_array($vars));
if (!isset($opts['vars'])) {
$url->remove(array_keys($_COOKIE));
}
return $url;
}
/**
* Determines the location of the system temporary directory. If a specific
* configuration cannot be found, it defaults to /tmp.
*
* @return string A directory name that can be used for temp files.
* Returns false if one could not be found.
*/
public static function getTempDir()
{
global $conf;
/* If one has been specifically set, then use that */
if (!empty($conf['tmpdir'])) {
$tmp = $conf['tmpdir'];
}
/* Next, try sys_get_temp_dir(). */
if (empty($tmp)) {
$tmp = sys_get_temp_dir();
}
/* If it is still empty, we have failed, so return false;
* otherwise return the directory determined. */
return empty($tmp)
? false
: $tmp;
}
/**
* Creates a temporary filename for the lifetime of the script, and
* (optionally) registers it to be deleted at request shutdown.
*
* @param string $prefix Prefix to make the temporary name more
* recognizable.
* @param boolean $delete Delete the file at the end of the
* request?
* @param string $dir Directory to create the temporary file
* in.
* @param boolean $secure If deleting file, should we securely
* delete the file?
* @param boolean $session_remove Delete this file when session is
* destroyed?
*
* @return string Returns the full path-name to the temporary file or
* false if a temporary file could not be created.
*/
public static function getTempFile($prefix = 'Horde', $delete = true,
$dir = '', $secure = false,
$session_remove = false)
{
if (empty($dir) || !is_dir($dir)) {
$dir = self::getTempDir();
}
$tmpfile = Horde_Util::getTempFile($prefix, $delete, $dir, $secure);
if ($session_remove) {
$gcfiles = $GLOBALS['session']->get('horde', 'gc_tempfiles', Horde_Session::TYPE_ARRAY);
$gcfiles[] = $tmpfile;
$GLOBALS['session']->set('horde', 'gc_tempfiles', $gcfiles);
}
return $tmpfile;
}
/**
* Returns the Web server being used.
* PHP string list built from the PHP 'configure' script.
*
* @return string A web server identification string.
* @see php_sapi_name()
*/
public static function webServerID()
{
switch (PHP_SAPI) {
case 'apache':
return 'apache1';
case 'apache2filter':
case 'apache2handler':
return 'apache2';
default:
return PHP_SAPI;
}
}
/**
* Returns an un-used access key from the label given.
*
* @param string $label The label to choose an access key from.
* @param boolean $nocheck Don't check if the access key already has been
* used?
* @param boolean $shutdown Is this called as a shutdown function?
*
* @return string A single lower case character access key, or an empty
* string if no key can be found.
*/
public static function getAccessKey(
$label, $nocheck = false, $shutdown = false
)
{
/* Shutdown call for translators? */
if ($shutdown) {
if (!count(self::$_labels)) {
return;
}
$script = basename($_SERVER['PHP_SELF']);
$labels = array_keys(self::$_labels);
sort($labels);
$used = array_keys(self::$_used);
sort($used);
$remaining = str_replace($used, array(), 'abcdefghijklmnopqrstuvwxyz');
self::log('Access key information for ' . $script);
self::log('Used labels: ' . implode(',', $labels));
self::log('Used keys: ' . implode('', $used));
self::log('Free keys: ' . $remaining);
return;
}
/* Use access keys at all? */
if (!isset(self::$_noAccessKey)) {
self::$_noAccessKey = !$GLOBALS['browser']->hasFeature('accesskey') || !$GLOBALS['prefs']->getValue('widget_accesskey');
}
if (self::$_noAccessKey ||
!preg_match('/_(\w)/u', $label, $match)) {
return '';
}
$key = Horde_String_Transliterate::toAscii($match[1]);
/* Has this key already been used? */
if (isset(self::$_used[strtolower($key)]) &&
!($nocheck && isset(self::$_labels[$label]))) {
return '';
}
/* Save key and label. */
self::$_used[strtolower($key)] = true;
self::$_labels[$label] = true;
return $key;
}
/**
* Strips an access key from a label.
*
* For multibyte charset strings the access key gets removed completely,
* otherwise only the underscore gets removed.
*
* @param string $label The label containing an access key.
*
* @return string The label with the access key being stripped.
*/
public static function stripAccessKey($label)
{
$replace = $GLOBALS['registry']->nlsconfig->curr_multibyte &&
preg_match('/[\x80-\xff]/', $label)
? ''
: '$1';
return preg_replace('/_(\w)/u', $replace, $label);
}
/**
* Highlights an access key in a label.
*
* @param string $label The label to highlight the access key in.
* @param string $accessKey The access key to highlight.
*
* @return string The HTML version of the label with the access key
* highlighted.
*/
public static function highlightAccessKey($label, $accessKey)
{
$stripped_label = self::stripAccessKey($label);
if (empty($accessKey)) {
return $stripped_label;
}
if ($GLOBALS['registry']->nlsconfig->curr_multibyte) {
/* Prefix parenthesis with the UTF-8 representation of the LRO
* (Left-to-Right-Override) Unicode codepoint U+202D. */
return $stripped_label . "\xe2\x80\xad" .
'(<span class="accessKey">' . strtoupper($accessKey) .
'</span>' . ')';
}
return preg_replace(
'/_(\w)/u',
'<span class="accessKey">$1</span>',
$label
);
}
/**
* Returns the appropriate "accesskey" and "title" attributes for an HTML
* tag and the given label.
*
* @param string $label The title of an HTML element
* @param boolean $nocheck Don't check if the access key already has
* been used?
* @param boolean $return_array Return attributes as a hash?
*
* @return string The title, and if appropriate, the accesskey attributes
* for the element.
*/
public static function getAccessKeyAndTitle($label, $nocheck = false,
$return_array = false)
{
$ak = self::getAccessKey($label, $nocheck);
$attributes = array('title' => self::stripAccessKey($label));
if (!empty($ak)) {
$attributes['title'] .= sprintf(Horde_Core_Translation::t(" (Accesskey %s)"), strtoupper($ak));
$attributes['accesskey'] = $ak;
}
if ($return_array) {
return $attributes;
}
$html = '';
foreach ($attributes as $attribute => $value) {
$html .= sprintf(' %s="%s"', $attribute, $value);
}
return $html;
}
/**
* Returns a label element including an access key for usage in conjuction
* with a form field. User preferences regarding access keys are respected.
*
* @param string $for The form field's id attribute.
* @param string $label The label text.
* @param string $ak The access key to use. If null a new access key
* will be generated.
*
* @return string The html code for the label element.
*/
public static function label($for, $label, $ak = null)
{
if (is_null($ak)) {
$ak = self::getAccessKey($label, 1);
}
$label = self::highlightAccessKey($label, $ak);
return sprintf('<label for="%s"%s>%s</label>',
$for,
!empty($ak) ? ' accesskey="' . $ak . '"' : '',
$label);