hoolux /system/expressionengine/helpers/compat_helper.php

Language PHP Lines 297
MD5 Hash d846ddbc5b61c2e30bd97de6e1d3fb91 Estimated Cost $4,464 (why?)
Repository https://bitbucket.org/tdevonshire/hoolux.git View Raw File View Project SPDX
  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
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * ExpressionEngine - by EllisLab
 *
 * @package		ExpressionEngine
 * @author		EllisLab Dev Team
 * @copyright	Copyright (c) 2003 - 2012, EllisLab, Inc.
 * @license		http://ellislab.com/expressionengine/user-guide/license.html
 * @link		http://ellislab.com
 * @since		Version 2.0
 * @filesource
 */
 
// ------------------------------------------------------------------------

/**
 * ExpressionEngine Compat Helper
 *
 * @package		ExpressionEngine
 * @subpackage	Helpers
 * @category	Helpers
 * @author		EllisLab Dev Team
 * @link		http://ellislab.com
 */

// ------------------------------------------------------------------------

/**
 * Fixes a bug in PHP 5.2.9 where sort flags defaulted
 * to SORT_REGULAR, resulting in some very odd removal
 * behavior. And since we have now started using it,
 * we stupidly need to compat the whole thing. -pk
 */
function ee_array_unique(array $arr, $sort_flags = SORT_STRING)
{
	// 5.2.9 introduced both the flags and the bug
	if (is_php('5.2.9'))
	{
		return array_unique($arr, $sort_flags);
	}

	// before 5.2.9 sort_string was the default
	if ($sort_flags === SORT_STRING)
	{
		return array_unique($arr);
	}

	// no point uniquing an array of 1 or nil
	if (count($arr) < 2)
	{
		return $arr;
	}

	// to be in parity with php's solution,
	// we need to keep the original key positions
	$key_pos = array_flip(array_keys($arr));
	$ret_arr = $arr;

	asort($arr, $sort_flags);

	$last_kept = reset($arr);
	$last_kept_k = key($arr);

	next($arr); // skip ahead

	while (list($k, $v) = each($arr))
	{
		if ($sort_flags === SORT_NUMERIC)
		{
			$keep = (bool) ((float) $last_kept - (float) $v);
		}
		else // SORT_REGULAR
		{
			$keep = ($last_kept != $v);
		}

		if ($keep)
		{
			$last_kept = $v;
			$last_kept_k = $k;
		}
		else
		{
			// This is the mind boggling and in my opinion buggy part of
			// the algorithm php uses. We unset any duplicates that follow
			// the first value. Which is fine thinking narrowly about unique
			// array values.
			// However, it's extremely unintuitive when thinking about php
			// arrays, where duplicate keys override earlier keys.
			// It also makes this algorithm unstable, so that moving a boolean
			// true value to different spots in the array vastly changes the
			// output.
			$unset = $k;
			if ($key_pos[$last_kept_k] > $key_pos[$k])
			{
				$unset = $last_kept_k;

				$last_kept = $v;
				$last_kept_k = $k;
			}

			unset($arr[$unset]);
			unset($ret_arr[$unset]);
		}
	}

	unset($arr);
	return $ret_arr;
}

// ------------------------------------------------------------------------

/**
 * Based on the c implementation from the ISC
 */
function ee_inet_ntop($ip)
{
	// unpack the binary
	$hex = unpack('H*', $ip);
	$hex = current($hex);

	$len = strlen($hex);

	// ipv4
	if ($len == 8)
	{
		$parts = str_split($hex, 2);
		$parts = array_map('hexdec', $parts);
		return implode('.', $parts);
	}

	// ipv6
	if ($len != 32)
	{
		show_error('Invalid IP address.');
	}

	$parts = str_split($hex, 4);

	// find the longest run of zeros
	$start = -1;
	$len = 0;
	$best_start = -1;
	$best_len = 0;

	foreach ($parts as $i => &$part)
	{
		if ($part == '0000')
		{
			if ($start == -1)
			{
				$start = $i;
				$len = 0;
			}

			$len++;
		}
		elseif ($start != -1 && ($best_start == -1 OR $len > $best_len))
		{
			$best_start = $start;
			$best_len = $len;
			$start = -1;
		}
	}

	// didn't move best?
	if ($start != -1 && ($best_start == -1 OR $len > $best_len))
	{
		$best_start = $start;
		$best_len = $len;
	}

	// print out the result
	$out = '';

	foreach ($parts as $i => &$part)
	{
		if ($best_start != -1)
		{
			if ($i >= $best_start && $i < ($best_start + $best_len))
			{
				if ($i == $best_start)
				{
					$out .= ':';
				}
				continue;
			}
		}

		if ($i)
		{
			$out .= ':';
		}

		// ipv4 mapped?
		if ($i == 6 && $best_start == 0 && 
			($best_len == 6 OR ($best_len == 5 && $parts[5] == 'ffff')))
		{
			$out .= ee_inet_ntop(pack('H4H4', $parts[6], $parts[7]));
			break;
		}

		// collapse the hex string
		$out .= sprintf('%x', hexdec($part));
	}

	return $out;
}

// ------------------------------------------------------------------------

/**
 * Based loosely on the c implementation from the ISC
 */
function ee_inet_pton($str)
{
	$pad_off = -1;

	if (strpos($str, ':') !== FALSE)
	{
		$parts = explode(':', $str);

		foreach ($parts as $i => &$part)
		{
			if ($part === '' && $pad_off == -1)
			{
				$pad_off = $i;
				continue;
			}

			// ipv4 mapped?
			if (strpos($part, '.'))
			{
				if ($i + 1 != count($parts))
				{
					show_error('Invalid IP address.');
				}

				// convert the ipv4 one, unpack as hex, pad, and add
				// I'm sure there is a way to just concatenate the binary
				// but I can't figure it out right now
				$ipv4 = unpack('H*', ee_inet_pton($part));
				list($cur, $next) = str_split(current($ipv4), 4);

				$part = str_pad($cur, 4, '0', STR_PAD_LEFT);
				$parts[$i + 1] = str_pad($next, 4, '0', STR_PAD_LEFT);
				break;
			}

			$part = str_pad($part, 4, '0', STR_PAD_LEFT);
		}

		if ($pad_off != -1)
		{
			$pad_len = 9 - count($parts);
			$zeros = array_fill(0, $pad_len, '0000');
			array_splice($parts, $pad_off, 1, $zeros);
		}

		$args = $parts;
		array_unshift($args, str_repeat('H4', 8));
		return call_user_func_array('pack', $args);
	}
	
	if (strpos($str, '.'))
	{
		return pack('N', ip2long($str));
	}

	show_error('Invalid IP address.');
}

// ------------------------------------------------------------------------

/**
 * Some windows servers don't have the inet_* functions
 */
if ( ! function_exists('inet_ntop'))
{
	function inet_ntop($in)
	{
		return ee_inet_ntop($in);
	}
}

if ( ! function_exists('inet_pton'))
{
	function inet_pton($in)
	{
		return ee_inet_pton($in);
	}
}


/* End of file  */
/* Location: ./system/expressionengine/helpers/compat_helper.php */
Back to Top