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
|
<?php
# Lifter002: TODO
# Lifter005: TODO
# Lifter007: TODO
# Lifter003: TODO
# Lifter010: TODO
/*
* Assets.class.php - assets helper
*
* Copyright (C) 2007 - Marcus Lunzenauer <mlunzena@uos.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*/
/**
* This class is used to construct URLs for static content like images,
* stylesheets or javascripts. As the URL to the "assets" directory is
* configurable one always has to construct the above mentioned URLs
* dynamically.
*
* Example:
*
* # construct the URL for the image "blank.gif"
* $url = Assets::image_path('blank.gif');
*
* @package studip
*
* @author mlunzena
* @copyright (c) Authors
*/
class Assets
{
const NUMBER_OF_ALIASES = 2;
/**
* @ignore
*/
private static $assets_url;
private static $assets_path;
private static $dynamic;
private static $counter_cache;
/**
* This method sets the URL to your assets.
*
* @param string $path the path to the assets
*/
public static function set_assets_path(string $path): void
{
self::$assets_path = $path;
}
/**
* This method sets the URL to your assets.
*
* @param string the URL to the assets
*
* @return void
*/
public static function set_assets_url($url)
{
self::$assets_url = $url;
self::$counter_cache = NULL;
self::$dynamic = mb_strpos($url, '%d') !== FALSE;
}
/**
* This class method is an accessor to the URL "prefix" for all things "asset"
* Prepend the return value of this method to the relative path of the wanted
* static content.
*
* Additionally if the ASSETS_URL contains the string '%d', it will be
* replaced with a random number between 0 and 3. If you passed an argument
* this number will not be random but specific to that argument thus being
* referentially transparent.
*
* Example:
*
* # static ASSETS_URL
* $ASSETS_URL = 'http://www.example.com/public/';
* echo Assets::url() . 'javascripts/prototype.js' . "\n";
* echo Assets::url('javascripts/prototype.js') . "\n";
*
* # output
* http://www.example.com/public/javascripts/prototype.js
* http://www.example.com/public/javascripts/prototype.js
*
*
* # dynamic ASSETS_URL
* $ASSETS_URL = 'http://www%d.example.com/public/';
* echo Assets::url() . 'javascripts/prototype.js' . "\n";
* echo Assets::url() . 'javascripts/prototype.js' . "\n";
* echo Assets::url() . 'javascripts/prototype.js' . "\n";
* echo Assets::url('javascripts/prototype.js') . "\n";
* echo Assets::url('javascripts/prototype.js') . "\n";
* echo Assets::url('javascripts/prototype.js') . "\n";
*
* # output
* http://www0.example.com/public/javascripts/prototype.js
* http://www1.example.com/public/javascripts/prototype.js
* http://www2.example.com/public/javascripts/prototype.js
* http://www1.example.com/public/javascripts/prototype.js
* http://www1.example.com/public/javascripts/prototype.js
* http://www1.example.com/public/javascripts/prototype.js
*
*
* @param string an optional suffix which is used to construct a number if
* ASSETS_URL is dynamic (contains '%d')
*
* @return string the URL "prefix"
*/
public static function url($to = '')
{
if (!self::$dynamic) {
return self::$assets_url . $to;
}
# dynamic ASSETS_URL
return sprintf(self::$assets_url,
$to == ''
? self::$counter_cache++ % self::NUMBER_OF_ALIASES
# alternative implementation
# : hexdec(mb_substr(sha1($to),-1)) & 3)
: ord($to[1]) & (self::NUMBER_OF_ALIASES - 1))
. $to;
}
/**
* This class method is an accessor to the path "prefix" for all things "asset"
*/
public static function path($to = ''): string
{
return self::$assets_path . $to;
}
/**
* Returns an image tag using options as html attributes on the
* tag, but with these special cases:
*
* 'alt' - If no alt text is given, the file name part of the $source is used
* (capitalized and without the extension)
* * 'size' - Supplied as "X@Y", so "30@45" becomes width="30" and height="45"
*
* The source can be supplied as a...
* * full path, like "/my_images/image.gif"
* * file name, like "rss.png", that gets expanded to "/images/rss.png"
* * file name without extension, like "logo", that gets expanded to "/images/logo.png"
*
* Do not use this to render icons. Use the more appropiate class
* Icon for this.
*/
public static function img($source, $opt = [])
{
if (!$source) {
return '';
}
$size = $opt['size'] ?? null;
$opt = self::parse_attributes($opt);
$opt['src'] = self::image_path($source);
if (!isset($opt['alt'])) {
$opt['alt'] = ucfirst(current(explode('.', basename($opt['src']))));
}
if (isset($size) && !isset($opt['width'])) {
$size = explode('@', $size, 2);
$opt['width'] = $size[0];
$opt['height'] = $size[1] ?? null;
unset($opt['size']);
}
return self::tag('img', $opt);
}
/**
* Returns an input tag using options as html attributes on the
* tag, but with these special cases:
*
* * 'size' - Supplied as "X@Y", so "30@45" becomes width="30" and height="45"
*
* The source can be supplied as a...
* * full path, like "/my_images/image.gif"
* * file name, like "rss.png", that gets expanded to "/images/rss.png"
* * file name without extension, like "logo", that gets expanded to "/images/logo.png"
*
* Do not use this to render icons. Use the more appropiate class
* Icon for this.
*/
public static function input($source, $opt = [])
{
if (!$source) {
return '';
}
$parts = explode('/', $source);
$size = $opt['size'];
$opt = self::parse_attributes($opt);
$opt['src'] = self::image_path($source);
$opt['type'] = 'image';
if (isset($size) && !isset($opt['width'])) {
[$opt['width'], $opt['height']] = explode('@', $size, 2);
unset($opt['size']);
}
return self::tag('input', $opt);
}
/**
* Returns path to an image asset.
*
* Example:
*
* The src can be supplied as a...
*
* full path,
* like "/my_images/image.gif"
*
* file name,
* like "rss.png", that gets expanded to "/images/rss.png"
*
* file name without extension,
* like "logo", that gets expanded to "/images/logo.png"
*
* Note: This function should be private/depracated for the use in other
* scripts, as we would like to always generate the complete <image> oder
* <input> tag. Please use Assets::img or Assets::input instead.
*/
public static function image_path($source, $respect_retina = false)
{
$path = self::compute_public_path($source, 'images', 'png');
return $path;
}
/**
* Returns a script include tag per source given as argument.
*
* Examples:
*
* Assets::script('prototype') =>
* <script src="/javascript/prototype.js"></script>
*
* Assets::script('common.javascript', '/elsewhere/cools') =>
* <script src="/js/common.javascript"></script>
* <script src="/elsewhere/cools.js"></script>
*/
public static function script($atLeastOneArgument)
{
$html = '';
foreach (func_get_args() as $source) {
$source = self::javascript_path($source);
$html .= self::content_tag('script', '',
['src' => $source]);
$html .= "\n";
}
return $html;
}
/**
* Returns path to a javascript asset.
*
* Example:
*
* Assets::javascript_path('ajax') => /javascripts/ajax.js
*/
public static function javascript_path($source)
{
return self::compute_public_path($source, 'javascripts', 'js');
}
/**
* Returns a css link tag per source given as argument.
*
* Examples:
*
* Assets::stylesheet('style') =>
* <link href="/stylesheets/style.css" media="screen" rel="stylesheet">
*
* Assets::stylesheet('style', array('media' => 'all')) =>
* <link href="/stylesheets/style.css" media="all" rel="stylesheet">
*
* Assets::stylesheet('random.styles', '/css/stylish') =>
* <link href="/stylesheets/random.styles" media="screen" rel="stylesheet">
* <link href="/css/stylish.css" media="screen" rel="stylesheet">
*/
public static function stylesheet($atLeastOneArgument)
{
$sources = func_get_args();
$sourceOptions = (func_num_args() > 1 &&
is_array($sources[func_num_args() - 1]))
? array_pop($sources)
: [];
$html = '';
foreach ($sources as $source) {
$source = self::stylesheet_path($source);
$opt = array_merge(['rel' => 'stylesheet',
'media' => 'screen',
'href' => $source],
$sourceOptions);
$html .= self::tag('link', $opt) . "\n";
}
return $html;
}
/**
* Returns path to a stylesheet asset.
*
* Example:
*
* stylesheet_path('style') => /stylesheets/style.css
*/
public static function stylesheet_path($source)
{
return self::compute_public_path($source, 'stylesheets', 'css');
}
/**
* This function computes the public path to the given source by using default
* dir and ext if not specified by the source. If source is not an absolute
* URL, the assets url is incorporated.
*
* @ignore
*/
private static function compute_public_path($source, $dir, $ext)
{
# add extension if not present
if ('' == mb_substr(mb_strrchr($source, "."), 1))
$source .= ".$ext";
# if source is not absolute
if (FALSE === mb_strpos($source, ':')) {
# add dir if url does not contain a path
if ('/' !== $source[0])
$source = "$dir/$source";
# consider asset host
$source = self::url(ltrim($source, '/'));
}
return $source;
}
/**
* Constructs an html tag.
*
* @ignore
*
* @param string tag name
* @param array tag options
* @param boolean true to leave tag open
*
* @return string
*/
private static function tag($name, $options = [], $open = FALSE)
{
if (!$name) {
return '';
}
ksort($options);
return '<' . $name . ' ' . arrayToHtmlAttributes($options) . ($open ? '>' : '>');
}
/**
* Helper function for content tags.
*
* @param string $name tag name
* @param string $content tag content
* @param array $options tag options
*
* @return string
*/
private static function content_tag($name, $content = '', $options = [])
{
if (!$name) {
return '';
}
return '<' . $name . ' ' . arrayToHtmlAttributes($options) . '>' .
$content .
'</' . $name . '>';
}
/**
* Parse a HTML attribute string into an array.
*
* @ignore
*/
private static function parse_attributes($stringOrArray)
{
if (is_array($stringOrArray))
return $stringOrArray;
preg_match_all('/
\s*(\w+) # key \\1
\s*=\s* # =
(\'|")? # values may be included in \' or " \\2
(.*?) # value \\3
(?(2) \\2) # matching \' or " if needed \\4
\s*(?:
(?=\w+\s*=) | \s*$ # followed by another key= or the end of the string
)
/x', $stringOrArray, $matches, PREG_SET_ORDER);
$attributes = [];
foreach ($matches as $val)
$attributes[$val[1]] = $val[3];
return $attributes;
}
}
|