aboutsummaryrefslogtreecommitdiff
path: root/lib/helper.php
blob: 67c84aabefe68fbf87ee7687363e7405077a934b (plain)
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
<?php

function ssl()
{
	return isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on');
}

function proto()
{
	return ssl() ? 'https' : 'http';
}

function baseurl()
{
	if(isset($GLOBALS['CONFIG']['BASEURL']))
	{
		$base = $GLOBALS['CONFIG']['BASEURL'];
		if(startswith('//', $base))
			$base = proto().':'.$base;

		return forceslash($base);
	}

	$base  = ssl() ? 'https://' : 'http://';
	$base .= $_SERVER['HTTP_HOST'];
	$base .=  forceslash(dirname($_SERVER['SCRIPT_NAME']));

	return forceslash($base);
}

function joinpath($parts)
{
	$parts = array_map('forceslash', $parts);
	return rtrim(implode('', $parts), '/');
}

function forceslash($url)
{
	$url =  rtrim($url, '/');
	if(strlen($url) > 0)
		$url .= '/';

	return $url;
}

function forceproto($url)
{
	if(startswith('//', $url))
		$url = proto().':'.$url;

	return $url;
}

function startswith($needle, $haystack)
{
	return substr($haystack, 0, strlen($needle)) == $needle;
}

function handle_lesscss_request($lessfile, $relative_path)
{
	$dir = forceslash(sys_get_temp_dir());

	$css_file = Less_Cache::Get([
		$lessfile => $relative_path,
	], [
		'sourceMap' => true,
		'compress' => true,
		'relativeUrls' => true,

		'cache_dir' => $dir,
	]);

	$css = file_get_contents($dir.$css_file);
	header('Content-Type: text/css');
	header('Content-Length: '.strlen($css));
	print($css);
}

function days_diff($date)
{
	$seconds = strtotime( $date ) - time();
	$days = intval(ceil($seconds / 60 / 60 / 24));
	return $days;
}

function days_diff_readable($date)
{
	$days = days_diff($date);
	if($days == -1)
		return 'yesterday';

	if($days == 0)
		return 'today';

	if($days == 1)
		return 'tomorrow';

	if($days < -60)
		return round(-$days / 30)." months ago";

	if($days < 0)
		return (-$days)." days ago";

	if($days > 60)
		return 'in '.round($days / 30)." months";

	return "in $days days";
}

function url_params()
{
	if($GLOBALS['forceopen'])
		return '?forceopen=yess';

	return '';
}

/**
 * returns the fielst element matching $predicate or null, if none matched.
 * $predicate is a callable that receives one array value at a time and can
 * return a bool'ish value
 */
function array_filter_first($array, $predicate)
{
	foreach ($array as $value) {
		if( $predicate($value) ) {
			return $value;
		}
	}

	return null;
}
/**
 * returns the fielst element matching $predicate or null, if none matched.
 * $predicate is a callable that receives one array value at a time and can
 * return a bool'ish value
 */
function array_filter_last($array, $predicate)
{
	foreach (array_reverse($array) as $value) {
		if( $predicate($value) ) {
			return $value;
		}
	}

	return null;
}

function slugify($text)
{
	// replace non letter or digits by -
	$text = preg_replace('~[^\pL\d]+~u', '-', $text);

	// transliterate
	$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);

	// remove unwanted characters
	$text = preg_replace('~[^-\w]+~', '', $text);

	// trim
	$text = trim($text, '-');

	// remove duplicate -
	$text = preg_replace('~-+~', '-', $text);

	// lowercase
	$text = strtolower($text);

	if (empty($text)) {
		return 'none';
	}

	return $text;
}