18 function is_assoc($array)
20 return (
bool) count(array_filter(array_keys($array),
'is_string'));
29 function string_or_implode_array($vData)
33 return implode(
' ', $vData);
35 return (
string) $vData;
47 function array2xml($aArray, $sRoot =
'root', $sCustomHeader =
'<?xml version="1.0" encoding="UTF-8"?>')
49 $sRootNode =
"$sCustomHeader<$sRoot/>";
50 $oXML = new \SimpleXMLElement($sRootNode);
51 array2xml_recursive($aArray, $oXML, null);
52 $sData = $oXML->asXML();
63 function array2xml_recursive($aArray, &$oSimpleXMLElement)
65 foreach ($aArray as $sNodeName => $vValue)
67 if (is_array($vValue))
69 if (is_assoc($vValue))
71 $oXML = $oSimpleXMLElement->addChild($sNodeName);
72 array2xml_recursive($vValue, $oXML);
76 foreach ($vValue as $vValue2)
78 if (is_array($vValue2))
80 $oXML = $oSimpleXMLElement->addChild($sNodeName);
81 array2xml_recursive($vValue2, $oXML);
85 $oSimpleXMLElement->addChild($sNodeName, $vValue2);
92 $oSimpleXMLElement->addChild($sNodeName, $vValue);
103 function xml2json($sXMLString)
105 $oXML = simplexml_load_string($sXMLString);
106 $sJSON = json_encode($oXML);
116 function xml2array($sXMLString)
118 $sJSON = xml2json($sXMLString);
119 $aArray = json_decode($sJSON, TRUE);
133 function arr_get($array, $nKey, $default = null, $bCheckEmpty =
false)
135 if (!is_array($array) and ! $array instanceof \ArrayAccess)
149 foreach ($nKey as $k)
151 $return[$k] = arr_get($array, $k, $default);
156 is_object($nKey) and $nKey = (
string) $nKey;
158 if (array_key_exists($nKey, $array))
160 if ($bCheckEmpty && (empty($array[$nKey]) || (is_string($array[$nKey]) && empty(trim($array[$nKey])))))
164 return $array[$nKey];
167 foreach (explode(
'.', $nKey) as $nKey_part)
169 if ((is_a($array,
'ArrayAccess') and isset($array[$nKey_part])) ===
false)
171 if (!is_array($array) or ! array_key_exists($nKey_part, $array))
177 $array = $array[$nKey_part];
180 if ($bCheckEmpty && empty($array))
194 function starts_with($sHaystack, $sNeedle)
197 return $sNeedle ===
"" || strrpos($sHaystack, $sNeedle, -strlen($sHaystack)) !== FALSE;
207 function ends_with($sHaystack, $sNeedle)
210 return $sNeedle ===
"" || (($sTemp = strlen($sHaystack) - strlen($sNeedle)) >= 0 && strpos($sHaystack, $sNeedle, $sTemp) !== FALSE);
220 function remove_prefix($sStr, $sPrefix)
222 return substr($sStr, strlen($sPrefix));
232 function remove_suffix($sStr, $sPrefix)
234 return substr($sStr, 0, -1 * strlen($sPrefix));
243 function seconds_to_time($nSeconds)
245 $oZeroDT =
new DateTime(
"@0");
246 $oSecondsDT =
new DateTime(
"@$nSeconds");
247 return $oZeroDT->diff($oSecondsDT)->format(
'%a days, %h hours, %i minutes and %s seconds');
257 function load_config($sFileName, $fErrorCallback = null)
259 if (file_exists($sFileName))
261 $aConfigData = include $sFileName;
264 elseif (is_callable($fErrorCallback))
266 call_user_func($fErrorCallback);
278 function load_view($sFileName, $aVariables = [])
282 if (file_exists($sFileName))
284 extract($aVariables);
286 $sContents = ob_get_contents();
297 function has_html($sText)
299 return strpos($sText,
'<') !==
false || strpos($sText,
'>') !==
false;
310 system(
'ipconfig /all');
311 $sConsoleOutput = ob_get_contents();
313 $sMAC = substr($sConsoleOutput, (strpos($sConsoleOutput,
"Physical") + 36), 17);
323 function quote_ini_value($vValue)
325 if (is_numeric($vValue))
327 return $vValue .
"\n";
329 return "\"" . $vValue .
"\"\n";
340 function write_ini_file($aSettings, $sPath, $bHasSections = FALSE)
345 foreach ($aSettings as $nKey => $vElement)
347 $sContent .=
"[" . $nKey .
"]\n";
348 foreach ($vElement as $nKey2 => $vElement2)
350 if (is_array($vElement2))
352 for ($i = 0; $i < count($vElement2); $i++)
354 $sContent .= $nKey2 .
"[] = " . quote_ini_value($vElement2[$i]);
357 else if ($vElement2 ==
"")
358 $sContent .= $nKey2 .
" = \n";
360 $sContent .= $nKey2 .
" = " . quote_ini_value($vElement2);
366 foreach ($aSettings as $nKey => $vElement)
368 if (is_array($vElement))
370 for ($i = 0; $i < count($vElement); $i++)
372 $sContent .= $nKey .
"[] = " . quote_ini_value($vElement[$i]);
375 else if ($vElement ==
"")
376 $sContent .= $nKey .
" = \n";
378 $sContent .= $nKey .
" = " . quote_ini_value($vElement);
382 if (!$oHandle = fopen($sPath,
'w'))
387 $bSuccess = fwrite($oHandle, $sContent);
399 function sse_header()
401 header(
"Content-Type: text/event-stream");
402 header(
"Cache-Control: no-cache");
403 header(
"Connection: keep-alive");
412 function sse_message($vData)
414 if (is_array($vData))
416 echo
"data: " . json_encode($vData) .
"\n";
420 echo
"data: $vData\n";
435 sse_message(
'CLOSE');