| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
class HC3_Functions |
| 3 |
{ |
| 4 |
public static function glueArray( array $array ) |
| 5 |
{ |
| 6 |
$return = join( '_', $array ); |
| 7 |
if( $return ){ |
| 8 |
$return = '_' . $return . '_'; |
| 9 |
} |
| 10 |
return $return; |
| 11 |
} |
| 12 |
|
| 13 |
public static function unglueArray( $string ) |
| 14 |
{ |
| 15 |
$return = array(); |
| 16 |
|
| 17 |
$string = trim( $string ); |
| 18 |
$string = trim( $string, '_' ); |
| 19 |
if( strlen($string) ){ |
| 20 |
$return = explode( '_', $string ); |
| 21 |
} |
| 22 |
|
| 23 |
return $return; |
| 24 |
} |
| 25 |
|
| 26 |
public static function wpGetIdByShortcode( $shortcode ) |
| 27 |
{ |
| 28 |
global $wpdb; |
| 29 |
$ret = array(); |
| 30 |
|
| 31 |
$pages = $wpdb->get_results( |
| 32 |
" |
| 33 |
SELECT |
| 34 |
ID, post_content |
| 35 |
FROM $wpdb->posts |
| 36 |
WHERE |
| 37 |
( post_type IN ('post', 'page') ) |
| 38 |
AND |
| 39 |
( post_content LIKE '%[" . $shortcode . "%]%' ) |
| 40 |
AND |
| 41 |
( post_status IN ('publish', 'private') ) |
| 42 |
" |
| 43 |
); |
| 44 |
|
| 45 |
if( $pages ){ |
| 46 |
foreach( $pages as $p ){ |
| 47 |
// find shortcode |
| 48 |
$pos1 = strpos( $p->post_content, '[' . $shortcode ); |
| 49 |
$pos2 = strpos( $p->post_content, ']', $pos1 ); |
| 50 |
|
| 51 |
$shortcodeContent = substr( $p->post_content, $pos1, $pos2 - $pos1 + 1 ); |
| 52 |
$shortcodeContent = substr( $p->post_content, $pos1, $pos2 - $pos1 ) . ' ]'; |
| 53 |
$shortcodeAtts = shortcode_parse_atts( $shortcodeContent ); |
| 54 |
|
| 55 |
unset( $shortcodeAtts[0] ); |
| 56 |
unset( $shortcodeAtts[1] ); |
| 57 |
|
| 58 |
// _print_r( $shortcodeAtts ); |
| 59 |
// echo "$pos1 - $pos2\n$shortcodeContent\n$p->post_content\n\n"; |
| 60 |
$ret[ $p->ID ] = $shortcodeAtts; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return $ret; |
| 65 |
} |
| 66 |
|
| 67 |
public static function removeInvisibleCharacters( $str, $url_encoded = TRUE ) |
| 68 |
{ |
| 69 |
$non_displayables = array(); |
| 70 |
|
| 71 |
// every control character except newline (dec 10) |
| 72 |
// carriage return (dec 13), and horizontal tab (dec 09) |
| 73 |
if ($url_encoded){ |
| 74 |
$non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15 |
| 75 |
$non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31 |
| 76 |
} |
| 77 |
$non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127 |
| 78 |
|
| 79 |
do { |
| 80 |
$str = preg_replace($non_displayables, '', $str, -1, $count); |
| 81 |
} |
| 82 |
while ($count); |
| 83 |
|
| 84 |
return $str; |
| 85 |
} |
| 86 |
|
| 87 |
public static function buildCsv( $array, $separator = ',' ) |
| 88 |
{ |
| 89 |
$processed = array(); |
| 90 |
reset( $array ); |
| 91 |
foreach( $array as $a ){ |
| 92 |
if( strpos($a, '"') !== FALSE ){ |
| 93 |
$a = str_replace( '"', '""', $a ); |
| 94 |
} |
| 95 |
|
| 96 |
$wrap = FALSE; |
| 97 |
|
| 98 |
if( strpos($a, $separator) !== FALSE ){ |
| 99 |
$wrap = TRUE; |
| 100 |
} |
| 101 |
elseif( strpos($a, "\n") !== FALSE ){ |
| 102 |
$wrap = TRUE; |
| 103 |
} |
| 104 |
|
| 105 |
if( $wrap ){ |
| 106 |
$a = '"' . $a . '"'; |
| 107 |
} |
| 108 |
|
| 109 |
$processed[] = $a; |
| 110 |
} |
| 111 |
|
| 112 |
$ret = join( $separator, $processed ); |
| 113 |
return $ret; |
| 114 |
} |
| 115 |
|
| 116 |
public static function pushDownload( $filename, $data ) |
| 117 |
{ |
| 118 |
// Try to determine if the filename includes a file extension. |
| 119 |
// We need it in order to set the MIME type |
| 120 |
if (FALSE === strpos($filename, '.')){ |
| 121 |
return FALSE; |
| 122 |
} |
| 123 |
|
| 124 |
// Grab the file extension |
| 125 |
$x = explode('.', $filename); |
| 126 |
$extension = end($x); |
| 127 |
|
| 128 |
// Load the mime types |
| 129 |
$mimes = array(); |
| 130 |
|
| 131 |
// Set a default mime if we can't find it |
| 132 |
if ( ! isset($mimes[$extension])){ |
| 133 |
$mime = 'application/octet-stream'; |
| 134 |
} |
| 135 |
else { |
| 136 |
$mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension]; |
| 137 |
} |
| 138 |
|
| 139 |
// Generate the server headers |
| 140 |
if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE){ |
| 141 |
header('Content-Type: "'.$mime.'"'); |
| 142 |
header('Content-Disposition: attachment; filename="'.$filename.'"'); |
| 143 |
header('Expires: 0'); |
| 144 |
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
| 145 |
header("Content-Transfer-Encoding: binary"); |
| 146 |
header('Pragma: public'); |
| 147 |
header("Content-Length: ".strlen($data)); |
| 148 |
} |
| 149 |
else { |
| 150 |
header('Content-Type: "'.$mime.'"'); |
| 151 |
header('Content-Disposition: attachment; filename="'.$filename.'"'); |
| 152 |
header("Content-Transfer-Encoding: binary"); |
| 153 |
header('Expires: 0'); |
| 154 |
header('Pragma: no-cache'); |
| 155 |
header("Content-Length: ".strlen($data)); |
| 156 |
} |
| 157 |
|
| 158 |
exit($data); |
| 159 |
} |
| 160 |
|
| 161 |
public static function generateRand( $len = 12, $conf = array() ) |
| 162 |
{ |
| 163 |
$useLetters = isset($conf['letters']) ? $conf['letters'] : TRUE; |
| 164 |
$useHex = isset($conf['hex']) ? $conf['hex'] : FALSE; |
| 165 |
$useDigits = isset($conf['digits']) ? $conf['digits'] : TRUE; |
| 166 |
$useCaps = isset($conf['caps']) ? $conf['caps'] : FALSE; |
| 167 |
|
| 168 |
$salt = ''; |
| 169 |
if( $useHex ){ |
| 170 |
$salt .= 'abcdef'; |
| 171 |
} |
| 172 |
if( $useLetters ) |
| 173 |
$salt .= 'abcdefghijklmnopqrstuvxyz'; |
| 174 |
if( $useDigits ){ |
| 175 |
// $salt .= '0123456789'; |
| 176 |
$salt .= '123456789'; |
| 177 |
} |
| 178 |
if( $useCaps ){ |
| 179 |
$salt .= 'ABCDEFGHIJKLMNOPQRSTUVXYZ'; |
| 180 |
} |
| 181 |
|
| 182 |
// srand( (double) microtime() * 1000000 ); |
| 183 |
$return = ''; |
| 184 |
$i = 1; |
| 185 |
$array = array(); |
| 186 |
while ( $i <= $len ){ |
| 187 |
$num = rand() % strlen($salt); |
| 188 |
$tmp = substr($salt, $num, 1); |
| 189 |
$array[] = $tmp; |
| 190 |
$i++; |
| 191 |
} |
| 192 |
shuffle( $array ); |
| 193 |
$return = join( '', $array ); |
| 194 |
return $return; |
| 195 |
} |
| 196 |
|
| 197 |
static function adjustColorBrightness( $hex, $steps ) |
| 198 |
{ |
| 199 |
// Steps should be between -255 and 255. Negative = darker, positive = lighter |
| 200 |
$steps = max(-255, min(255, $steps)); |
| 201 |
|
| 202 |
// Normalize into a six character long hex string |
| 203 |
$hex = str_replace('#', '', $hex); |
| 204 |
if( strlen($hex) == 3 ){ |
| 205 |
$hex = str_repeat(substr($hex,0,1), 2).str_repeat(substr($hex,1,1), 2).str_repeat(substr($hex,2,1), 2); |
| 206 |
} |
| 207 |
|
| 208 |
// Split into three parts: R, G and B |
| 209 |
$color_parts = str_split($hex, 2); |
| 210 |
$return = '#'; |
| 211 |
|
| 212 |
foreach( $color_parts as $color ){ |
| 213 |
$color = hexdec($color); // Convert to decimal |
| 214 |
$color = max(0,min(255,$color + $steps)); // Adjust color |
| 215 |
$return .= str_pad(dechex($color), 2, '0', STR_PAD_LEFT); // Make two char hex code |
| 216 |
} |
| 217 |
return $return; |
| 218 |
} |
| 219 |
|
| 220 |
static function arithmeticMean( $a ) |
| 221 |
{ |
| 222 |
$sum = 0; |
| 223 |
foreach( $a as $element ){ |
| 224 |
$sum += $element; |
| 225 |
} |
| 226 |
$return = $sum / count($a); |
| 227 |
return $return; |
| 228 |
} |
| 229 |
|
| 230 |
static function geometricMean( $array ) |
| 231 |
{ |
| 232 |
if( ! count($array) ){ |
| 233 |
return 0; |
| 234 |
} |
| 235 |
|
| 236 |
$chunkSize = 10; |
| 237 |
|
| 238 |
$total = count($array); |
| 239 |
$power = 1 / $total; |
| 240 |
|
| 241 |
$chunkProducts = array(); |
| 242 |
$chunks = array_chunk( $array, $chunkSize ); |
| 243 |
|
| 244 |
foreach( $chunks as $chunk ){ |
| 245 |
$chunkProducts[] = pow( array_product($chunk), $power ); |
| 246 |
} |
| 247 |
|
| 248 |
$result = array_product( $chunkProducts ); |
| 249 |
return $result; |
| 250 |
} |
| 251 |
|
| 252 |
static function meanSquareError( $array1, $array2 ) |
| 253 |
{ |
| 254 |
$totals = array(); |
| 255 |
|
| 256 |
$count = count( $array1 ); |
| 257 |
for( $ii = 0; $ii < $count; $ii++ ){ |
| 258 |
$thisOne = pow( ($array2[$ii] - $array1[$ii]), 2 ); |
| 259 |
$totals[] = $thisOne; |
| 260 |
} |
| 261 |
|
| 262 |
$return = array_sum($totals) / count($totals); |
| 263 |
return $return; |
| 264 |
} |
| 265 |
|
| 266 |
static function removeFromArray( $array, $what, $replaceWith = NULL ) |
| 267 |
{ |
| 268 |
$return = $array; |
| 269 |
for( $ii = count($return) - 1; $ii >= 0; $ii-- ){ |
| 270 |
if( ! array_key_exists($ii, $return) ){ |
| 271 |
continue; |
| 272 |
} |
| 273 |
|
| 274 |
if( $return[$ii] == $what ){ |
| 275 |
if( NULL === $replaceWith ){ |
| 276 |
array_splice( $return, $ii, 1 ); |
| 277 |
} |
| 278 |
else { |
| 279 |
array_splice( $return, $ii, 1, array($replaceWith) ); |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
return $return; |
| 284 |
} |
| 285 |
|
| 286 |
static function tempCrudPrepareArgs( $args, $idField ) |
| 287 |
{ |
| 288 |
if( $args && (! is_array($args)) ){ |
| 289 |
$args = array( $args ); |
| 290 |
} |
| 291 |
|
| 292 |
if( isset($args['_PREPARED']) && $args['_PREPARED'] ){ |
| 293 |
return $args; |
| 294 |
} |
| 295 |
|
| 296 |
$return = array( |
| 297 |
'_PREPARED' => TRUE, |
| 298 |
'LIMIT' => array(), |
| 299 |
'SORT' => array(), |
| 300 |
'WHERE' => array(), |
| 301 |
'SEARCH' => NULL, |
| 302 |
); |
| 303 |
|
| 304 |
$allowed_compares = array( '=', '<>', '>=', '<=', '>', '<', 'IN', 'NOTIN', 'LIKE', '&'); |
| 305 |
$special = array( 'limit', 'sort', 'search'); |
| 306 |
|
| 307 |
foreach( $args as $arg ){ |
| 308 |
if( ! is_array($arg) ){ |
| 309 |
$arg = array( $idField, '=', $arg ); |
| 310 |
} |
| 311 |
|
| 312 |
$k = $arg[0]; |
| 313 |
|
| 314 |
if( in_array($k, $special) ){ |
| 315 |
array_shift( $arg ); |
| 316 |
|
| 317 |
switch( $k ){ |
| 318 |
case 'search': |
| 319 |
$v = array_shift( $arg ); |
| 320 |
$return['SEARCH'] = $v; |
| 321 |
break; |
| 322 |
|
| 323 |
case 'limit': |
| 324 |
$v = array_shift( $arg ); |
| 325 |
$offset = array_shift( $arg ); |
| 326 |
$return['LIMIT'] = array( $v, $offset ); |
| 327 |
break; |
| 328 |
|
| 329 |
case 'sort': |
| 330 |
if( is_array($return['SORT']) ){ |
| 331 |
$sort_by = array_shift( $arg ); |
| 332 |
$sort_how = strtolower( $arg ? array_shift( $arg ) : 'asc' ); |
| 333 |
if( ! in_array($sort_how, array('asc', 'desc')) ){ |
| 334 |
echo "SORTING '$sort_how' IS NOT ALLOWED, ONLY ASC OR DESC!<br>"; |
| 335 |
$sort_how = 'asc'; |
| 336 |
} |
| 337 |
$return['SORT'][] = array( $sort_by, $sort_how ); |
| 338 |
} |
| 339 |
break; |
| 340 |
} |
| 341 |
|
| 342 |
continue; |
| 343 |
} |
| 344 |
|
| 345 |
// WHERE |
| 346 |
if( count($arg) < 3 ){ |
| 347 |
echo "FOR WHERE ARGUMENTS REQUIRE 3 PARAMS: '$k'"; |
| 348 |
_print_r( $args ); |
| 349 |
_print_r( $arg ); |
| 350 |
exit; |
| 351 |
} |
| 352 |
|
| 353 |
list( $k, $compare, $v ) = $arg; |
| 354 |
$compare = strtoupper( $compare ); |
| 355 |
if( ! in_array($compare, $allowed_compares) ){ |
| 356 |
echo "COMPARING BY '$compare' IS NOT ALLOWED!<br>"; |
| 357 |
exit; |
| 358 |
} |
| 359 |
|
| 360 |
if( ($k == $idField) && ($idField == 'id') && ( ! is_array($v) ) ){ |
| 361 |
$v = (int) $v; |
| 362 |
} |
| 363 |
|
| 364 |
if( in_array($compare, array('IN', 'NOTIN')) && (! is_array($v)) ){ |
| 365 |
$v = strlen($v) ? $v : 0; |
| 366 |
$v = array($v); |
| 367 |
} |
| 368 |
if( $v == 'null' ){ |
| 369 |
$v = NULL; |
| 370 |
} |
| 371 |
if( ($k == $idField) && ($compare == '=') ){ |
| 372 |
$return['LIMIT'] = array(1, 0); |
| 373 |
$return['SORT'] = NULL; |
| 374 |
} |
| 375 |
|
| 376 |
$return['WHERE'][] = array( $k, $compare, $v ); |
| 377 |
} |
| 378 |
|
| 379 |
return $return; |
| 380 |
} |
| 381 |
|
| 382 |
public static function listFiles( $dir, $type = 'file', $extension = '' ) |
| 383 |
{ |
| 384 |
if( ! is_array($dir) ) |
| 385 |
$dir = array( $dir ); |
| 386 |
|
| 387 |
$return = array(); |
| 388 |
foreach( $dir as $this_dir ){ |
| 389 |
if ( file_exists($this_dir) && ($handle = opendir($this_dir)) ){ |
| 390 |
while ( false !== ($f = readdir($handle)) ){ |
| 391 |
if( substr($f, 0, 1) == '.' ) |
| 392 |
continue; |
| 393 |
|
| 394 |
if( 'file' == $type ){ |
| 395 |
if( is_file( $this_dir . '/' . $f ) ){ |
| 396 |
if( (! $extension ) || ( substr($f, - strlen($extension)) == $extension ) ){ |
| 397 |
$return[] = $f; |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
else { |
| 402 |
if( is_dir( $this_dir . '/' . $f ) ){ |
| 403 |
if( (! $extension ) || ( substr($f, - strlen($extension)) == $extension ) ){ |
| 404 |
$return[] = $f; |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
closedir($handle); |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
sort( $return ); |
| 414 |
return $return; |
| 415 |
} |
| 416 |
|
| 417 |
public static function makeCombos( $array ) |
| 418 |
{ |
| 419 |
$return = array(); |
| 420 |
|
| 421 |
while( $thisOnes = array_shift($array) ){ |
| 422 |
$thisCombos = array(); |
| 423 |
foreach( $thisOnes as $r ){ |
| 424 |
if( $return ){ |
| 425 |
reset( $return ); |
| 426 |
foreach( $return as $combo ){ |
| 427 |
$combo[] = $r; |
| 428 |
$thisCombos[] = $combo; |
| 429 |
} |
| 430 |
} |
| 431 |
else { |
| 432 |
$thisCombos[] = array( $r ); |
| 433 |
} |
| 434 |
} |
| 435 |
if( $thisCombos ){ |
| 436 |
$return = $thisCombos; |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
return $return; |
| 441 |
} |
| 442 |
} |