EHCP-Force-Edition/ehcp/localutils.php

1653 lines
42 KiB
PHP
Raw Normal View History

2018-04-02 22:26:17 +00:00
<?php
@ini_set("date.timezone", "UTC");
2018-04-02 22:26:17 +00:00
function imgextension($f)
{
return isextension($f, 'img');
2018-04-02 22:26:17 +00:00
}
function isoextension($f)
{
return isextension($f, 'iso');
2018-04-02 22:26:17 +00:00
}
function isextension($f, $e)
{
$f = basename($f);
2018-04-02 22:26:17 +00:00
$ext = explode(".", strtolower($f));
$ext = array_pop($ext);
2018-04-02 22:26:17 +00:00
#echo "isleniyor: $f : ext:($ext) \n";
return ($ext == $e);
2018-04-02 22:26:17 +00:00
}
function mymkdir($dirname)
{
$dirname = trim($dirname);
if ($dirname <> '.' and $dirname <> '..') {
if (!is_dir($dirname)) {
if (mkdir($dirname, 777, true))
echo "\ndirectory is made: ($dirname)\n";
else
"\nerror occured while making directory: ($dirname)\n";
2018-04-02 22:26:17 +00:00
}
}
}
if (!function_exists("print_r2")) {
function print_r2($array)
{
if (is_array($array))
return "<pre>Array:\n" . str_replace(array("\n", " "), array('<br>', '&nbsp;'), print_r($array, true)) . '</pre>';
elseif ($array === null)
return "(null) ";
elseif ($array === "")
return "(bosluk= \"\")";
elseif ($array === false)
return "(bool-false)";
elseif ($array === true)
return "(bool-true)";
else {
return "Array degil:<br>(normal gosterim:$array)<br>print_r:(" . print_r($array, true) . ") <br>var_dump:" . var_dump($array);
}
2018-04-02 22:26:17 +00:00
}
}
if (!function_exists('print_r3')) {
function print_r3($ar, $header = '')
{
if (!$ar)
return "(BOS-EMPTY)";
if (!is_array($ar))
return "Not Array:" . $ar;
2018-04-02 22:26:17 +00:00
$sayi = count($ar);
$tr = "<tr class='list'>";
$td = "<td class='list'>";
2018-04-02 22:26:17 +00:00
$res .= "<table border=1 class='list'> $header";
2018-04-02 22:26:17 +00:00
foreach ($ar as $key => $val) {
$res .= "$tr$td" . $key . "</td>$td" . $val . "</td></tr>";
}
2018-04-02 22:26:17 +00:00
$res .= "</table>";
return $res;
2018-04-02 22:26:17 +00:00
/*
ic ice (recursive) yapmak icin,
en basa, if(!is_array($ar)) return $ar;
$res.="<tr><td>".print_r3(key($ar))."</td><td>".print_r3($val)."</td></tr>";
*/
}
2018-04-02 22:26:17 +00:00
}
if (!function_exists("andle")) {
function andle($s1, $s2)
{ //iki string'in andlenmi halini bulur. bir bosa "and" kullanlmaz. delphiden aldim..:)
if ($s1 == '')
$s1 = $s2;
elseif ($s2 <> '')
$s1 = $s1 . ' and ' . $s2;
return $s1;
}
2018-04-02 22:26:17 +00:00
}
function to_array($ar)
{ # convert a variable to array if it is not already,
if (is_array($ar))
return $ar; # if array, dont do anything
if (!$ar)
return array(); # bos ise, bos array dondur.
if (!is_array($ar))
return array($ar); # array olmayan bir degisken ise, arraya dondur ve return et.
return "(arraya cevirme yapilamadi.)"; # hicbiri degilse hata var zaten.
2018-04-02 22:26:17 +00:00
}
function array_merge2($ar1, $ar2)
{
return array_merge(to_array($ar1), to_array($ar2));
2018-04-02 22:26:17 +00:00
}
if (!function_exists("writeoutput")) {
function writeoutput($file, $string, $mode = "w", $log = true)
{
2018-04-02 22:26:17 +00:00
mymkdir(dirname($file)); # auto make the dir of filename
2018-04-02 22:26:17 +00:00
if (!($fp = fopen($file, $mode))) {
2018-04-02 22:26:17 +00:00
echo "hata: dosya acilamadi: $file (writeoutput) !";
return false;
}
if (!fputs($fp, $string . "\n")) {
2018-04-02 22:26:17 +00:00
fclose($fp);
echo "hata: dosyaya yazilamadi: $file (writeoutput) !";
return false;
}
2018-04-02 22:26:17 +00:00
fclose($fp);
if ($log)
echo "\n" . basename(__FILE__) . ": file written successfully: $file, mode:$mode \n";
return true;
}
2018-04-02 22:26:17 +00:00
}
if (!function_exists("writeoutput2")) {
function writeoutput2($file, $string, $mode = "w", $debug = true)
{
$file = removeDoubleSlash($file);
2018-04-02 22:26:17 +00:00
if ($debug) {
echo "\n" . __FUNCTION__ . ":*** Writing to file ($file) the contents:\n\n$string\n\n";
}
2018-04-02 22:26:17 +00:00
mymkdir(dirname($file)); # auto make the dir of filename
2018-04-02 22:26:17 +00:00
if (!($fp = fopen($file, $mode))) {
2018-04-02 22:26:17 +00:00
echo "hata: dosya acilamadi: $file (writeoutput) !";
return false;
}
if (!fputs($fp, $string . "\n")) {
2018-04-02 22:26:17 +00:00
fclose($fp);
echo "hata: dosyaya yazilamadi: $file (writeoutput) !";
return false;
}
fclose($fp);
return true;
2018-04-02 22:26:17 +00:00
}
}
if (!function_exists("alanlarial")) {
function alanlarial($db2, $tablo)
{ // adodb de calsyor.
foreach ($db2->MetaColumnNames($tablo) as $alan)
$alanlar[] = $alan;
return $alanlar;
}
2018-04-02 22:26:17 +00:00
}
if (!function_exists("strop")) {
function strop($str, $bas, $son)
{
return $bas . $str . $son;
}
2018-04-02 22:26:17 +00:00
}
if (!function_exists("arrayop")) {
function arrayop($arr, $op)
{
foreach ($arr as $ar)
$ret[] = $op($ar, "{", "}");
return $ret;
}
2018-04-02 22:26:17 +00:00
}
if (!function_exists("executeprog2")) {
function executeprog2($prog)
{ // echoes output.
passthru($prog, $val);
return ($val == 0);
}
2018-04-02 22:26:17 +00:00
}
if (!function_exists('executeProg3')) {
function executeProg3($prog, $echooutput = False)
{
# executes program and return output
if ($echooutput)
echo "\n" . __FUNCTION__ . ": executing: ($prog)\n";
exec($prog, $topcmd);
if (!is_array($topcmd))
return "";
foreach ($topcmd as $t)
$topoutput .= $t . "\n";
$out = trim($topoutput);
if ($echooutput and ($out <> ''))
echo "\n$out\n";
return $out;
}
2018-04-02 22:26:17 +00:00
}
if (!function_exists("executeprog")) {
function executeprog($prog)
{ // does not echo output. only return it.
$fp = popen("$prog", 'r');
if (!$fp) {
return "<br>Cannot Execute: $prog " . __FUNCTION__;
}
$read = fread($fp, 8192);
pclose($fp);
return $read;
}
}
if (!function_exists('degiskenal')) {
function degiskenal($degiskenler)
{
$alansayisi = count($degiskenler);
for ($i = 0; $i < $alansayisi; $i++) {
global ${$degiskenler[$i]};
if ($_POST[$degiskenler[$i]] <> "")
${$degiskenler[$i]} = $_POST[$degiskenler[$i]];
else
${$degiskenler[$i]} = $_GET[$degiskenler[$i]];
$degerler[] = ${$degiskenler[$i]};
}
;
return $degerler;
}
2018-04-02 22:26:17 +00:00
}
if (!function_exists('replacelineinfile')) {
function replacelineinfile($find, $replace, $where, $addifnotexists = false)
{
// edit a line starting with $find, to edit especially conf files..
2018-04-02 22:26:17 +00:00
debugecho("\nreplaceline: ($find -> $replace) in ($where) \n ");
$bulundu = false;
2018-04-02 22:26:17 +00:00
$filearr = @file($where);
//if($find=='$dbrootpass=') print_r($filearr);
2018-04-02 22:26:17 +00:00
if (!$filearr) {
echo "cannot open file... returning...\n";
return false;
} //else print_r($file);
2018-04-02 22:26:17 +00:00
$len = strlen($find);
$newfile = array();
2018-04-02 22:26:17 +00:00
foreach ($filearr as $line) {
$line = trim($line) . "\n";
$sub = substr($line, 0, $len);
if ($sub == $find) {
$line = $replace . "\n";
$bulundu = true;
}
$newfile[] = $line;
2018-04-02 22:26:17 +00:00
}
if ($addifnotexists and !$bulundu) {
echo "Line not found, adding at end: ($replace)\n";
$newfile[] = $replace;
}
2018-04-02 22:26:17 +00:00
return arraytofile($where, $newfile);
2018-04-02 22:26:17 +00:00
}
function replaceOrAddLineInFile($find, $replace, $where)
{
return replacelineinfile($find, $replace, $where, true);
}
2018-04-02 22:26:17 +00:00
}
if (!function_exists("addifnotexists")) {
function addifnotexists($what, $where)
{
debugecho("\naddifnotexists: ($what) -> ($where) \n ", 4);
2018-05-01 17:15:40 +00:00
#bekle(__FUNCTION__." basliyor..");
$what .= "\n";
$filearr = @file($where);
if (!$filearr) {
2018-05-01 17:15:40 +00:00
echo "cannot open file, trying to setup: ($where)\n";
$fp = fopen($where, 'w');
2018-05-01 17:15:40 +00:00
fclose($fp);
$filearr = file($where);
2018-04-02 22:26:17 +00:00
2018-05-01 17:15:40 +00:00
} //else print_r($file);
2018-04-02 22:26:17 +00:00
if (array_search($what, $filearr) === false) {
2018-05-01 17:15:40 +00:00
echo "dosyada bulamadı ekliyor: $where -> $what \n";
$filearr[] = $what;
arraytofile($where, $filearr);
2018-04-02 22:26:17 +00:00
2018-05-01 17:15:40 +00:00
} else {
//echo "buldu... sorun yok. \n";
// already found, so, do not add
}
2018-05-01 17:15:40 +00:00
#bekle(__FUNCTION__." bitti...");
2018-04-02 22:26:17 +00:00
2018-05-01 17:15:40 +00:00
}
2018-04-02 22:26:17 +00:00
}
2018-05-01 17:15:40 +00:00
if (!function_exists("removeifexists")) {
function removeifexists($what, $where)
{
debugecho("\nremoveifexists: ($what) -> ($where) \n ", 4);
$filearr = @file($where);
if (!$filearr) {
2018-05-01 17:15:40 +00:00
echo "cannot open file, trying to setup: ($where)\n";
$fp = fopen($where, 'w');
2018-05-01 17:15:40 +00:00
fclose($fp);
$filearr = file($where);
2018-05-01 17:15:40 +00:00
}
if (is_array($filearr) && count($filearr) > 0) {
2018-05-01 17:15:40 +00:00
$newFileArr = array();
foreach ($filearr as $line) {
if (!startsWith($line, $what)) {
2018-05-01 17:15:40 +00:00
$newFileArr[] = $line;
}
}
if (is_array($newFileArr) && count($newFileArr) > 0) {
arraytofile($where, $newFileArr);
2018-05-01 17:15:40 +00:00
}
}
}
2018-04-02 22:26:17 +00:00
}
if (!function_exists('getlocalip')) {
function getlocalip($interface = 'eth0')
{
global $localip;
2018-04-02 22:26:17 +00:00
$ipline = exec("ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | head -1");
2018-04-02 22:26:17 +00:00
if (!isset($ipline) || empty($ipline)) {
$ipline = "127.0.0.1";
}
2018-04-02 22:26:17 +00:00
$localip = $ipline;
return $ipline;
}
2018-04-02 22:26:17 +00:00
}
if (!function_exists("debugecho")) {
function debugecho($str, $level = 0)
{
$currentlevel = 4;
if ($level >= $currentlevel)
echo $str;
2018-04-02 22:26:17 +00:00
}
2018-04-02 22:26:17 +00:00
}
if (!function_exists("arraytofile")) {
function arraytofile($file, $lines)
{
$new_content = join('', $lines);
$fp = fopen($file, 'w');
$write = fwrite($fp, $new_content);
fclose($fp);
}
2018-04-02 22:26:17 +00:00
}
function inputform5ForTableConfig($tableConfig, $addArray, $isAdmin = false)
{
2018-04-02 22:26:17 +00:00
// $isAdmin can be used in combo with another parameter in an input array to determine if a field should be enabled for administrators or disabled for non-administators including resellers.
// It's an optional parameter that can be used to limit things further during form construction depending on which user submitted the request to build a form, so to speak
// You have to tell this function how to use it, as it really doesn't have any purpose other than being a value you can use should you need it.
2018-04-02 22:26:17 +00:00
# written for compatibility with inputform5 general function.
# convert a table config (like in start of classapp.php, 'subdomainstable'=>array....) to an array that is acceptable by function inputform5 and call inputform5
$fields = $tableConfig['insertfields'];
$fields2 = array();
$say = count($fields);
2018-04-02 22:26:17 +00:00
for ($i = 0; $i < $say; $i++) {
if (is_array($fields[$i]))
$newitem = $fields[$i]; # accept fields both arrays and non-arrays
else
$newitem = array($fields[$i]);
if ($tableConfig['insertfieldlabels'][$i] <> '')
$newitem['lefttext'] = $tableConfig['insertfieldlabels'][$i];
$fields2[] = $newitem;
2018-04-02 22:26:17 +00:00
}
#$out.="Say:$say, <br>insertFields".print_r2($fields).print_r2($fields2);
$fields2 = array_merge($fields2, $addArray);
2018-04-02 22:26:17 +00:00
#$out.=print_r2($fields2);
return $out . inputform5($fields2, '', $isAdmin);
2018-04-02 22:26:17 +00:00
}
function inputform5($alanlar, $action = '', $isAdmin = false)
{
2018-04-02 22:26:17 +00:00
// $isAdmin can be used in combo with another parameter in an input array to determine if a field should be enabled for administrators or disabled for non-administators including resellers.
// It's an optional parameter that can be used to limit things further during form construction depending on which user submitted the request to build a form, so to speak
// You have to tell this function how to use it, as it really doesn't have any purpose other than being a value you can use should you need it.
global $debuglevel, $output;
/*
* general purpose input form generator. examples below.
*
sadece echo yapmaz.
degistirildi. artik textarea gosterebiliyor.
$res.="alanlar:".print_r2($alan);
$res.="degerler:".print_r2($deger);
*/
if (!is_array($alanlar))
$alanlar = array($alanlar); # convert to array if not , i.e, you dont need to use an array if you only has one input element,
$alanlar[] = array('_insert', 'tip' => 'hidden', 'varsayilan' => '1');
$alansayisi = count($alanlar);
$res .= "
2018-04-02 22:26:17 +00:00
<script> // script for pass generate
var keylist='abcdefghijklmnopqrstuvwxyz123456789'
var temp=''
function generatepass(){
temp=''
for (i=0;i<6;i++)
temp+=keylist.charAt(Math.floor(Math.random()*keylist.length))
return temp
}
</script>
<form method=post enctype='multipart/form-data' ";
if ($action <> "") {
$res .= " action='$action'";
}
;
$res .= "><table class='inputform'>";
2018-04-02 22:26:17 +00:00
if ($debuglevel > 2)
$output .= print_r2($alanlar);
2018-04-02 22:26:17 +00:00
foreach ($alanlar as $alan)
$res .= inputelement2($alan, $isAdmin);
$res .= "</table>";
if (strstr($res, "input type='submit' ") === false)
$res .= "<input type=submit>";
$res .= "</form>";
2018-04-02 22:26:17 +00:00
return $res;
/* this function is very flexible, cok esnek yani... ingilizce yazdik diye yanlis anlasilmasin, anadoluda yazildi bu...;)
* example usages:
* echo inputform5('name') # displays only an input form with field name
* echo inputform5(array('name','surname')) # input form with name, surname
* echo inputform5(array(array('name','varsayilan'=>'defaultname'),'surname')) # using default value
* etc...
*/
2018-04-02 22:26:17 +00:00
}
function inputelement2($alan, $isAdmin = false)
{
2018-04-02 22:26:17 +00:00
// $isAdmin can be used in combo with another parameter in an input array to determine if a field should be enabled for administrators or disabled for non-administators including resellers.
// It's an optional parameter that can be used to limit things further during form construction depending on which user submitted the request to build a form, so to speak
// You have to tell this function how to use it, as it really doesn't have any purpose other than being a value you can use should you need it.
if (!is_array($alan))
$alan = array($alan); # convert to array if not
2018-04-02 22:26:17 +00:00
$solyazi = $alan['solyazi'] . $alan['lefttext'];
$alanadi = $alan['alanadi'] . $alan['name'];
$alantipi = $alan['tip'] . $alan['type'];
$sagyazi = $alan['sagyazi'] . $alan['righttext'];
2018-04-02 22:26:17 +00:00
// CSS Class for row
$cssclass = $alan['cssclass'];
if (!isset($cssclass) || empty($cssclass)) {
2018-04-02 22:26:17 +00:00
$cssclass = "";
}
$cols = $alan['cols'];
$rows = $alan['rows'];
$cols = ($cols == "" ? 40 : $cols);
$rows = ($rows == "" ? 10 : $rows);
2018-04-02 22:26:17 +00:00
if (!$alantipi or $alantipi == '')
$alantipi = $alan[1]; # second array element is field type
if (!$alantipi or $alantipi == '')
$alantipi = 'text';
if ($alanadi == '')
$alanadi = $alan[0]; # fieldname is the first element, if not defined as 'alanadi'=>'fieldname_example'
2018-04-02 22:26:17 +00:00
// Left text handling
if (!$solyazi and !in_array($alantipi, array('hidden', 'comment', 'submit'))) {
$solyazi = ucwords($alanadi);
$lastCharacter = substr($solyazi, -1);
if ($lastCharacter != ":") {
2018-04-02 22:26:17 +00:00
// Append a Colon
$solyazi = $solyazi . ":";
}
} else if ($solyazi && !in_array($alantipi, array('hidden', 'comment', 'submit'))) {
if (strpos($solyazi, ' ') != false) {
$wordsInLeftText = explode(' ', $solyazi);
if (count($wordsInLeftText) <= 5) {
$solyazi = ucwords($solyazi);
}
}
$lastCharacter = substr($solyazi, -1);
if ($lastCharacter != ":" && (!isset($alan['skip-ending-colon']) || $alan['skip-ending-colon'] != true)) {
2018-04-02 22:26:17 +00:00
// Append a Colon
$solyazi = $solyazi . ":";
}
} else if (in_array($alantipi, array('hidden', 'comment', 'submit'))) {
2018-04-02 22:26:17 +00:00
$solyazi = "";
}
if ($alantipi == 'comment')
$span = " colspan=3 "; # no 3 columns for comment type
2018-04-02 22:26:17 +00:00
$varsayilan = $alan['varsayilan'];
if (!$varsayilan)
$varsayilan = $alan['default'];
2018-04-02 22:26:17 +00:00
if (!$varsayilan and $alan['value'] <> '')
$varsayilan = $alan['value'];
if (!$varsayilan and $alan['deger'] <> '')
$varsayilan = $alan['deger']; # ister varsayilan, ister value, ister deger de, gine de calisir..
if ($deger == '')
$deger = $value = $varsayilan;
2018-04-02 22:26:17 +00:00
if ($alan['readonly'] <> '')
$readonly = 'readonly="yes"';
2018-04-02 22:26:17 +00:00
$res .= "<tr class='inputform";
if (!empty($cssclass)) {
2018-04-02 22:26:17 +00:00
$res .= " " . $cssclass;
}
$res .= "'><td class='inputform' $span>";
if ($span == '')
$res .= $solyazi . "</td>\n<td class='inputform'>"; # no need to a new td if there is a col span
switch ($alantipi) {
2018-04-02 22:26:17 +00:00
case 'password_with_generate':
#$alantipi='password';
#$alantipi='text';
/* Password generator by cs4fun.lv */
$res .= "<input id='$alanadi' type='text' name='$alanadi' value='$varsayilan'></td><td>
2018-04-02 22:26:17 +00:00
<input type=\"button\" value=\"Generate:\" onClick=\"$('#$alanadi').val(generatepass());\">
$sagyazi</td>\n";
break;
/* END Password generator by cs4fun.lv */
2018-04-02 22:26:17 +00:00
case 'comment':
$res .= "$varsayilan</td>\n";
2018-04-02 22:26:17 +00:00
break;
case 'hidden&text':
$res .= "<input id='$alanadi' type='hidden' name='$alanadi' value='$varsayilan'>$varsayilan</td>\n";
2018-04-02 22:26:17 +00:00
break;
case 'password':
case 'text':
case 'hidden':
$res .= "<input id='$alanadi' type='$alantipi' name='$alanadi' value='$varsayilan'></td>\n";
2018-04-02 22:26:17 +00:00
break;
case 'textarea':
$res .= "<textarea id='$alanadi' cols=$cols name='$alanadi' rows=$rows $readonly>$varsayilan</textarea> <br></td>\n";
2018-04-02 22:26:17 +00:00
break;
case 'checkbox':
if ($alan['checked'])
$checked = "checked=" . $alan['checked'];
else
$checked = '';
if ($alan['disabled'] == 'disabled' || (!empty($alan['requires_admin']) && ($alan['requires_admin'] == true) && !$isAdmin))
$disabledInput = "disabled";
else
$disabledInput = '';
if ($deger == '')
$deger = $alanadi;
$res .= "<input type='checkbox' name='$alanadi' value='$varsayilan' $checked $disabledInput>" . $alan['secenekyazisi'] . "</td>\n";
break;
2018-04-02 22:26:17 +00:00
case 'radio':
foreach ($alan['secenekler'] as $deger2 => $yazi2)
$res .= "<input type=radio name='$alanadi' value='$deger2' " . ($varsayilan == $deger2 ? 'checked' : '') . ">$yazi2<br>";
$res .= "</td>";
/*
echo print_r2($alan);
echo "<br>(varsayilan:$varsayilan)<br>";
*/
break;
case 'select':
$res .= "<select id='$alanadi' name='$alanadi'>\n\r";
if (!is_array($alan['secenekler']))
$alan['secenekler'] = $varsayilan;
foreach ($alan['secenekler'] as $deger2 => $yazi2) {
if ($varsayilan == $deger2) {
$sel = " selected='yes'";
2018-04-02 22:26:17 +00:00
}
$res .= "<option value='$deger2'$sel>$yazi2</option>\n\r";
2018-04-02 22:26:17 +00:00
$sel = "";
}
#for ($j=0;$j<$sayi;$j++) $res.="<option value='".$varsayilan[$j]."'>".$varsayilan[$j]."</option>\n\r";
$res .= "</select></td>\n";
break;
2018-04-02 22:26:17 +00:00
case 'fileupload':
$res .= "\n<td><input type='file' id='$alanadi' name='$alanadi'></td>\n";
break;
2018-04-02 22:26:17 +00:00
case 'submit':
if ($deger == "No/Yes") { // Special no yes confirm case
$res .= "\n<input type='submit' id='$alanadi' name='$alanadi' value='No'>&nbsp; <input type='submit' id='$alanadi' name='$alanadi' value='Yes'>\n";
} else {
$res .= "\n<input type='submit' id='$alanadi' name='$alanadi' value='$deger'>\n";
}
break;
2018-04-02 22:26:17 +00:00
default:
$res .= "<input type='text' id='$alanadi' name='$alanadi' value='$deger'></td>\n";
2018-04-02 22:26:17 +00:00
}
if ($span == '' and $alantipi <> 'password_with_generate')
$res .= "<td>$sagyazi</td>";
2018-04-02 22:26:17 +00:00
#$res.="<td>($alantipi)</td></tr>\n";
$res .= "</tr>\n";
2018-04-02 22:26:17 +00:00
return $res;
}
if (!function_exists("tablobaslikyaz")) {
function tablobaslikyaz($alan, $baslik, $extra)
{ // tablolistelede kullanilmak icin yazildi.
$tr = "<tr class='list'>";
$td = "<td class='list'>";
$th = "<th class='list'>";
2018-04-02 22:26:17 +00:00
if ($baslik === null || !is_array($baslik)) {
$baslik = array();
}
2022-04-20 18:57:51 +00:00
$alansayisi = count($alan);
2018-04-02 22:26:17 +00:00
$result2 = " \n $tr";
if (count($baslik) > 0) {
for ($i = 0; $i < $alansayisi; $i++) {
if ($baslik[$i] <> "") {
$yaz = $baslik[$i];
2018-09-24 17:59:53 +00:00
} else {
$yaz = $alan[$i];
2018-09-24 17:59:53 +00:00
}
$result2 .= "$th$yaz</th>";
}
} else {
for ($i = 0; $i < $alansayisi; $i++) {
$yaz = $alan[$i];
$result2 .= "$th$yaz</th>";
2018-09-24 17:59:53 +00:00
}
;
2018-09-24 17:59:53 +00:00
}
// Handle extra
if (is_array($extra)) {
for ($i = 0; $i < count($extra); $i++) {
2022-04-21 21:47:08 +00:00
$indexToStart = count($baslik) - count($extra) + $i;
if ($alansayisi + count($extra) == count($baslik)) {
$result2 .= "$th" . (isset($baslik) && is_array($baslik) && array_key_exists($indexToStart, $baslik) && !empty($baslik[$indexToStart]) ? $baslik[$indexToStart] : "") . "</th>";
} else {
$result2 .= $th . "</th>";
2022-04-21 21:47:08 +00:00
}
2018-09-24 17:59:53 +00:00
}
2018-09-10 18:45:46 +00:00
}
$result2 .= "</tr>\n ";
return $result2;
}
2018-04-02 22:26:17 +00:00
}
function timediffhrs($timein, $timeout)
{
$timeinsec = strtotime($timein);
$timeoutsec = strtotime($timeout);
2018-04-02 22:26:17 +00:00
$timetot = $timeoutsec - $timeinsec;
$timehrs = intval($timetot / 3600);
$timehrsi = (($timetot / 3600) - $timehrs) * 60;
$timemins = intval(($timetot / 60) - $timehrs * 60);
2018-04-02 22:26:17 +00:00
return $timehrs;
}
function getFirstPart($str, $splitter)
{
$position = strpos($str, $splitter);
if ($position === false)
return $str;
else
return substr($str, 0, $position);
2018-04-02 22:26:17 +00:00
}
function getLastPart($str, $splitter)
{
$position = strrpos($str, $splitter);
2018-04-02 22:26:17 +00:00
return substr($str, $position + 1);
}
function get_filename_from_url($url)
{
$lastslashposition = strrpos($url, "/");
$filename = substr($url, $lastslashposition + 1);
return $filename;
2018-04-02 22:26:17 +00:00
}
function removeDoubleSlash($str)
{
2018-04-02 22:26:17 +00:00
# why this function?: some directory names contain trailing slash like /example/this/, and some portions of existing codes uses that. Until fixed, new codes are written using this, to let both style work..
# this function may be removed after all trailing slashes removed..
return str_replace("//", "/", $str);
2018-04-02 22:26:17 +00:00
}
function get_filename_extension($filename)
{
2018-04-02 22:26:17 +00:00
$lastdotposition = strrpos($filename, ".");
2018-04-02 22:26:17 +00:00
if ($lastdotposition === 0) {
$extension = substr($filename, 1);
} elseif ($lastdotposition == "") {
$extension = $filename;
} else {
$extension = substr($filename, $lastdotposition + 1);
}
2018-04-02 22:26:17 +00:00
return strtolower($extension);
2018-04-02 22:26:17 +00:00
}
if (!function_exists('securefilename')) {
function securefilename($fn)
{
$ret = str_replace(array("\\", '..', '%', '&'), array('', '', ''), $fn);
#$ret=escapeshellarg($ret);
return $ret;
}
2018-04-02 22:26:17 +00:00
}
function passthru2($cmd, $no_remove = false, $no_escape = false)
{
$cmd1 = $cmd;
if (!$no_remove)
$cmd = removeDoubleSlash($cmd);
if (!$no_escape)
$cmd = escapeshellcmd($cmd);
2018-04-02 22:26:17 +00:00
echo "\nexecuting command: $cmd1 \n(escapedcmd: $cmd)\n";
passthru($cmd);
return true;
}
function escapeDollarSignsBash($cmd)
{
return str_replace('$', '\$', $cmd);
}
function passthru2_silent($cmd, $no_remove = false, $no_escape = false)
{
$cmd1 = $cmd;
if (!$no_remove)
$cmd = removeDoubleSlash($cmd);
if (!$no_escape)
$cmd = escapeshellcmd($cmd);
2018-04-02 22:26:17 +00:00
passthru($cmd);
return true;
}
function passthru3($cmd, $source = '')
{
$cmd = removeDoubleSlash($cmd);
2018-04-02 22:26:17 +00:00
# Echoes command and execute, does not escapeshellcmd
echo "\n$source:Executing command: ($cmd) \n";
passthru($cmd);
}
function date_tarih()
{
2018-04-02 22:26:17 +00:00
return date('Y-m-d h:i:s');
}
function my_shell_exec($cmd, $source = '')
{
echo "\n$source: " . date_tarih() . " Executing command: ($cmd)";
echo shell_exec($cmd);
2018-04-02 22:26:17 +00:00
}
function trimstrip($str)
{
2018-04-02 22:26:17 +00:00
return trim(stripslashes($str));
}
function isNumericField($f)
{
return (substr_count($f, 'int') > 0 or substr_count($f, 'float') > 0);
2018-04-02 22:26:17 +00:00
}
function stripslashes_deep($value)
{
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
2018-04-02 22:26:17 +00:00
return $value;
}
function validateIpAddress($ip_addr)
{
2018-08-08 20:36:47 +00:00
return filter_var($ip_addr, FILTER_VALIDATE_IP);
2018-04-02 22:26:17 +00:00
}
if (!function_exists('buildoption2')) {
function buildoption2($adi, $arr, $selected)
{
$res = "<select name='$adi'><option value=''>Select/Sec</option>";
foreach ($arr as $ar)
$res .= "<option value='$ar' " . (($ar == $selected) ? "selected" : "") . ">$ar</option>";
$res .= "</select>";
return $res;
}
2018-04-02 22:26:17 +00:00
}
if (!function_exists("debug_print_backtrace2")) {
function debug_print_backtrace2()
{
echo "<pre>";
debug_print_backtrace();
echo "</pre>";
}
2018-04-02 22:26:17 +00:00
}
if (!function_exists("debug_backtrace2")) {
function debug_backtrace2()
{
$ar = debug_backtrace();
$out = "<br>";
array_shift($ar); # enson cagrilan zaten bu. ona gerek yok.
$ar = array_reverse($ar);
foreach ($ar as $a) {
$f = $a['file'];
$f = explode("/", $f);
$f = array_pop($f);
#$nf=array();
#$nf[]=array_pop($f);
#$nf[]=array_pop($f);
#$nf[]=array_pop($f); # son uc elemani al. cok uzun dosya adi/yolu olmasin diye
#$nf=array_reverse($nf);
#$f=implode("/",$nf);
$out .= "(" . $f . ':' . $a['line'] . ':' . $a['function'] . ")->";
#$out.="(".$f.'->'.$a['function'].")->";
}
return $out . "<br>";
2018-04-02 22:26:17 +00:00
}
}
function textarea_to_array($area, $start = array(), $end = array())
{
$templ = array();
$templates = explode("\n", $area);
2018-04-02 22:26:17 +00:00
#echo print_r2($templates);
$templates = array_merge($start, $templates, $end);
foreach ($templates as $t) {
$t = trim($t);
$templ[$t] = $t;
2018-04-02 22:26:17 +00:00
#echo "$t -> $t ekleniyor <br>";
}
#echo print_r2($templ);
# bu çalışmadı, bug var veya anlamadım: $templ=array_merge($start,$templ,$end);
#array_push($templ,$end); # bunlar da çalışmadı.
#array_unshift($templ,$start);
#echo print_r2($templ);
return $templ;
/*
çok ilginç, yukardaki array_merge fonksiyonları, array'ın indexlerini değiştiriyor:
çıktısı:
* Array gosteriliyor:
Array
(
[4096] => 4096
[2048] => 2048
[256] => 256
[512] => 512
[1024] => 1024
[1536] => 1536
)
Array gosteriliyor:
Array
(
[0] => Array
(
[0] => seç
)
[1] => 4096
[2] => 2048
[3] => 256
[4] => 512
[5] => 1024
[6] => 1536
[7] => Array
(
)
)
*
*
*/
2018-04-02 22:26:17 +00:00
}
/* Ubuntu Specific Functions */
function getUbuntuVersion()
{
2018-04-02 22:26:17 +00:00
exec("lsb_release -r | awk '{ print $2 }'", $version);
if (!empty($version) && is_array($version)) {
2018-04-02 22:26:17 +00:00
return $version[0];
}
return false;
}
function getUbuntuReleaseYear()
{
2018-04-02 22:26:17 +00:00
$version = getUbuntuVersion();
return substr($version, 0, stripos($version, "."));
}
function getUbuntuReleaseMonth()
{
2018-04-02 22:26:17 +00:00
$version = getUbuntuVersion();
return substr($version, stripos($version, ".") + 1);
}
function getIsUbuntu()
{
2020-02-21 00:31:26 +00:00
exec("cat /etc/issue | awk '{ print $1 }' | head -n 1", $distro);
if (is_array($distro) && !empty($distro)) {
if (strtolower(trim($distro[0])) == "ubuntu") {
2018-04-02 22:26:17 +00:00
return true;
}
}
return false;
}
function getIsDebian()
{
2020-02-21 00:31:26 +00:00
exec("cat /etc/issue | awk '{ print $1 }' | head -n 1", $distro);
if (is_array($distro) && !empty($distro)) {
if (strtolower($distro[0]) == "debian") {
2018-04-02 22:26:17 +00:00
return true;
}
}
return false;
}
/* End Ubuntu Specific Functions */
/* Start OS Specific Functions */
function sysIsUsingSystemD()
{
2018-04-02 22:26:17 +00:00
exec("ps -p 1 | awk '{print $4}' | tail -n 1", $sysd);
if (!empty($sysd) && is_array($sysd)) {
if (!empty($sysd[0]) && $sysd[0] == "systemd") {
2018-04-02 22:26:17 +00:00
return true;
}
}
return false;
}
function serviceExists($service)
{
2018-04-02 22:26:17 +00:00
// Neat: http://stackoverflow.com/questions/2427913/how-can-i-grep-for-a-string-that-begins-with-a-dash-hyphen
if (isset($service) && !empty($service)) {
2018-04-02 22:26:17 +00:00
// Below command is too slow
// $serviceExists = shell_exec('service --status-all 2>&1 | grep -F -- "' . $service . '" | awk \'{print $4}\' | tr -d \'\n\'');
2020-03-26 04:42:30 +00:00
$serviceExists = shell_exec('ls /etc/init.d 2>/dev/null | grep -F -- "' . $service . '" | head -n 1');
if (isset($serviceExists) && !empty($serviceExists)) {
2018-04-02 22:26:17 +00:00
return true;
}
2020-03-26 04:42:30 +00:00
$serviceExists = shell_exec('find /lib/systemd/system -name "*' . $service . '*" -exec basename {} .service \; 2>/dev/null | head -n 1');
if (isset($serviceExists) && !empty($serviceExists)) {
2018-04-02 22:26:17 +00:00
return true;
}
2020-03-26 04:42:30 +00:00
$serviceExists = shell_exec('find /etc/systemd/system -name "*' . $service . '*" -exec basename {} .service \; 2>/dev/null | head -n 1');
if (isset($serviceExists) && !empty($serviceExists)) {
2018-04-02 22:26:17 +00:00
return true;
}
}
return false;
}
function determinePHPFPMName()
{
2018-04-02 22:26:17 +00:00
// Below command takes too long
// $serviceExists = shell_exec('service --status-all 2>&1 | grep -F -- "-fpm" | awk \'{print $4}\' | grep "php" | tr -d \'\n\'');
2020-03-26 04:42:30 +00:00
$serviceExists = shell_exec('ls /etc/init.d 2>/dev/null | grep -F -- "-fpm" | head -n 1');
if (isset($serviceExists) && !empty($serviceExists) && stripos($serviceExists, 'php') !== false) {
2018-04-02 22:26:17 +00:00
return $serviceExists;
}
2020-03-26 04:42:30 +00:00
$serviceExists = shell_exec('find /lib/systemd/system -name "*-fpm*" -exec basename {} .service \; 2>/dev/null | head -n 1');
2018-04-02 22:26:17 +00:00
if (isset($serviceExists) && !empty($serviceExists) && stripos($serviceExists, 'php') !== false) {
2018-04-02 22:26:17 +00:00
return $serviceExists;
}
2020-03-26 04:42:30 +00:00
$serviceExists = shell_exec('find /etc/systemd/system -name "*-fpm*" -exec basename {} .service \; 2>/dev/null | head -n 1');
if (isset($serviceExists) && !empty($serviceExists) && stripos($serviceExists, 'php') !== false) {
2018-04-02 22:26:17 +00:00
return $serviceExists;
}
2018-04-02 22:26:17 +00:00
return false;
}
function determineFTPUserFromCMD()
{
2018-04-02 22:26:17 +00:00
exec("cat /etc/passwd | grep vsftpd", $vsftpd_user_exists);
if (!empty($vsftpd_user_exists) && is_array($vsftpd_user_exists)) {
if (!empty($vsftpd_user_exists[0])) {
2018-04-02 22:26:17 +00:00
return "vsftpd";
}
}
return "ftp";
}
function determineBindUserFromCMD()
{
2018-04-02 22:26:17 +00:00
$bindUser = "bind";
// Try bind - which is standard for Ubuntu
exec('cat /etc/passwd | grep -o "^bind.*"', $bind_user_exists);
if (!empty($bind_user_exists) && is_array($bind_user_exists)) {
if (!empty($bind_user_exists[0])) {
2018-04-02 22:26:17 +00:00
return "bind";
}
}
2018-04-02 22:26:17 +00:00
// Unset bind user array
unset($bind_user_exists);
2018-04-02 22:26:17 +00:00
// Try named which may be used for other distros
exec('cat /etc/passwd | grep -o "^named.*"', $bind_user_exists);
if (!empty($bind_user_exists) && is_array($bind_user_exists)) {
if (!empty($bind_user_exists[0])) {
2018-04-02 22:26:17 +00:00
return "named";
}
}
2018-04-02 22:26:17 +00:00
// Return bind9 user.
return $bindUser;
}
/* End OS Specific Functions */
function startsWith($haystack, $needle)
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
2018-04-02 22:26:17 +00:00
}
function endsWith($haystack, $needle)
{
$length = strlen($needle);
if ($length == 0) {
return true;
}
2018-04-02 22:26:17 +00:00
return (substr($haystack, -$length) === $needle);
2018-04-02 22:26:17 +00:00
}
function removeInvalidCharsFromDomainName($string, $regexPattern)
{
2018-04-02 22:26:17 +00:00
$string = strtolower($string);
if (stripos($string, "http://") !== false) {
2018-04-02 22:26:17 +00:00
$string = str_replace("http://", "", $string);
}
if (stripos($string, "https://") !== false) {
2018-04-02 22:26:17 +00:00
$string = str_replace("https://", "", $string);
}
if (stripos($string, "www.") !== false) {
2018-04-02 22:26:17 +00:00
$string = str_replace("www.", "", $string);
}
2018-04-02 22:26:17 +00:00
// Need to replace invalid characters now!!!!
if (isset($regexPattern) && !empty($regexPattern)) {
2018-04-02 22:26:17 +00:00
$string = preg_replace($regexPattern, "", $string);
}
2018-04-02 22:26:17 +00:00
// Break the domain name into parts (name and TLD)
$positionOfFirstDot = stripos($string, ".");
if ($positionOfFirstDot !== false) {
2018-04-02 22:26:17 +00:00
$domainNameWithoutTLD = substr($string, 0, $positionOfFirstDot);
$domainTLD = substr($string, $positionOfFirstDot);
}
2018-04-02 22:26:17 +00:00
// Remove hyphens from front
while (isset($domainNameWithoutTLD) && startsWith($domainNameWithoutTLD, "-")) {
2018-04-02 22:26:17 +00:00
$domainNameWithoutTLD = substr($domainNameWithoutTLD, 1);
}
2018-04-02 22:26:17 +00:00
// Remove hyphens from front of tld
while (isset($domainTLD) && startsWith($domainTLD, "-")) {
2018-04-02 22:26:17 +00:00
$domainTLD = substr($domainTLD, 1);
}
2018-04-02 22:26:17 +00:00
// Remove hyphens from back
while (isset($domainNameWithoutTLD) && endsWith($domainNameWithoutTLD, "-")) {
2018-04-02 22:26:17 +00:00
$domainNameWithoutTLD = substr($domainNameWithoutTLD, 0, strlen($domainNameWithoutTLD) - 1);
}
2018-04-02 22:26:17 +00:00
// Remove hyphens from back of tld
while (isset($domainTLD) && endsWith($domainTLD, "-")) {
2018-04-02 22:26:17 +00:00
$domainTLD = substr($domainTLD, 0, strlen($domainTLD) - 1);
}
if (isset($domainNameWithoutTLD) && isset($domainTLD)) {
2018-04-02 22:26:17 +00:00
$string = $domainNameWithoutTLD . $domainTLD;
}
2018-04-02 22:26:17 +00:00
return $string;
}
function removeAllTrailingSlashes($str)
{
while (isset($str) && endsWith($str, "/")) {
2018-04-02 22:26:17 +00:00
$str = substr($str, 0, strlen($str) - 1);
}
return $str;
}
function removeInvalidChars($string, $mode)
{
switch ($mode) {
2018-04-02 22:26:17 +00:00
case "directory":
$pattern = "/[^A-Za-z0-9\/_\-]/i";
$string = preg_replace($pattern, "", $string);
break;
case "database":
$pattern = "/[^A-Za-z0-9_]/i";
$string = preg_replace($pattern, "", $string);
break;
2018-04-02 22:26:17 +00:00
case "title":
$pattern = "/[^A-Za-z0-9_\-\s']/i";
break;
case "strictTitle":
$pattern = "/[^A-Za-z0-9_\-\s]/i";
break;
case "name":
$pattern = "/[^A-Za-z0-9_\-]/i";
break;
case "properName":
$pattern = "/[^A-Za-z0-9_\-\s]/i";
break;
case "email":
$pattern = "/[^A-Za-z0-9_\-@\.]/i";
break;
2018-08-08 20:36:47 +00:00
case "lettersandnumbers":
$pattern = "/[^A-Za-z0-9]/i";
break;
2018-04-02 22:26:17 +00:00
case "domainname":
// Lowercase for domain names only!!!
$pattern = "/[^a-z0-9\-\.]/i";
$string = removeInvalidCharsFromDomainName($string, $pattern);
break;
case "domainnamewithseparatorchar":
// Lowercase for domain names only!!!
$pattern = "/[^a-z0-9\-\.,=;]/i";
$string = removeInvalidCharsFromDomainName($string, $pattern);
break;
case "domainnameport":
// Lowercase for domain names only!!!
// Allow port in the domain name for custom ports (example: ehcpforce.tk:8777)
$pattern = "/[^a-z0-9:\-\.]/i";
$string = removeInvalidCharsFromDomainName($string, $pattern);
2018-04-02 22:26:17 +00:00
// Make sure we only have one : port colon
$positionOfFirstColon = stripos($string, ":");
if ($positionOfFirstColon !== false) {
2018-04-02 22:26:17 +00:00
// count the colon characters
$colonSplit = explode(":", $string);
if (is_array($colonSplit)) {
2018-04-02 22:26:17 +00:00
array_filter($colonSplit); // Remove empty records
if (count($colonSplit) != 2) {
2018-04-02 22:26:17 +00:00
// Remove all colons since there is more than one
$string = str_replace(":", "", $string);
}
}
}
2018-04-02 22:26:17 +00:00
break;
case "subdomainname":
2018-04-02 22:26:17 +00:00
// Lowercase for subdomains too
$pattern = "/[^a-z0-9\-]/i";
$string = strtolower($string);
if (stripos($string, "http://") !== false) {
2018-04-02 22:26:17 +00:00
$string = str_replace("http://", "", $string);
}
if (stripos($string, "https://") !== false) {
2018-04-02 22:26:17 +00:00
$string = str_replace("https://", "", $string);
}
if (stripos($string, "www.") !== false) {
2018-04-02 22:26:17 +00:00
$string = str_replace("www.", "", $string);
}
2018-04-02 22:26:17 +00:00
// Need to replace invalid characters now!!!!
if (isset($pattern) && !empty($pattern)) {
2018-04-02 22:26:17 +00:00
$string = preg_replace($pattern, "", $string);
}
2018-04-02 22:26:17 +00:00
// Remove hyphens from front
while (startsWith($string, "-")) {
2018-04-02 22:26:17 +00:00
$string = substr($string, 1);
}
2018-04-02 22:26:17 +00:00
// Remove hyphens from back
while (endsWith($string, "-")) {
2018-04-02 22:26:17 +00:00
$string = substr($string, 0, strlen($string) - 1);
}
2018-04-02 22:26:17 +00:00
break;
default:
return $string;
}
if (isset($pattern) && !empty($pattern)) {
2018-04-02 22:26:17 +00:00
return preg_replace($pattern, "", $string);
}
2018-04-02 22:26:17 +00:00
return $string;
}
function setOwner($file, $owner)
{
if (file_exists($file)) {
if (chown($file, $owner)) {
2018-04-02 22:26:17 +00:00
return true;
}
}
return false;
}
function setPermissions($file, $mode)
{
if (file_exists($file)) {
if (chmod($file, $mode)) {
2018-04-02 22:26:17 +00:00
return true;
}
}
return false;
}
function domainNameValid($string, $skipRegex = false)
{
2018-04-02 22:26:17 +00:00
$valid = true;
if (empty($string)) {
2018-04-02 22:26:17 +00:00
$valid = false;
}
if (stripos($string, "http://") !== false) {
2018-04-02 22:26:17 +00:00
$valid = false;
}
if (stripos($string, "https://") !== false) {
2018-04-02 22:26:17 +00:00
$valid = false;
}
if (stripos($string, "www.") !== false) {
2018-04-02 22:26:17 +00:00
$valid = false;
}
2018-04-02 22:26:17 +00:00
// If it's still valid, run regex to see if it's a valid domain
if ($valid && !$skipRegex) {
2018-04-02 22:26:17 +00:00
$valid = preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $string) //valid chars check
&& preg_match("/^.{1,253}$/", $string) //overall length check
&& preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $string);
2018-04-02 22:26:17 +00:00
}
2018-04-02 22:26:17 +00:00
// If it's still valid, make sure there is a domain ending and TLD
if ($valid) {
2018-04-02 22:26:17 +00:00
$positionOfLastDot = strrpos($string, ".");
// Must have period in the domainname
if ($positionOfLastDot === false) {
2018-04-02 22:26:17 +00:00
$valid = false;
} else {
2018-04-02 22:26:17 +00:00
// If we have a period, make sure the length following it is greater than 2
$remainingChars = substr($string, $positionOfLastDot);
if (strlen($remainingChars) < 3) {
2018-04-02 22:26:17 +00:00
$valid = false;
}
}
}
2018-04-02 22:26:17 +00:00
return $valid;
}
function getIPAddress()
{
2018-08-10 01:11:17 +00:00
$ip = "";
if (isset($_SERVER['HTTP_CF_CONNECTING_IP']) && !empty($_SERVER['HTTP_CF_CONNECTING_IP'])) {
2018-08-10 01:11:17 +00:00
$ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
} else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
2018-08-10 01:11:17 +00:00
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else if (isset($_SERVER['HTTP_X_REAL_IP']) && !empty($_SERVER['HTTP_X_REAL_IP'])) {
2018-08-10 01:11:17 +00:00
$ip = $_SERVER['HTTP_X_REAL_IP'];
} else {
2018-08-10 01:11:17 +00:00
$ip = $_SERVER['REMOTE_ADDR'];
}
if (!isValidIPAddress($ip)) {
2018-08-10 01:11:17 +00:00
return "";
}
2018-08-10 01:11:17 +00:00
return $ip;
}
function isValidIPAddress($ip, $allowLocalIPs = false)
{
$valid = false;
if (!$allowLocalIPs) {
2018-08-10 01:11:17 +00:00
$valid = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
} else {
2018-08-10 01:11:17 +00:00
$valid = filter_var($ip, FILTER_VALIDATE_IP);
}
return $valid;
}
function inputValid($string, $mode)
{
2018-04-02 22:26:17 +00:00
$valid = true;
switch ($mode) {
2018-04-02 22:26:17 +00:00
case "domainname":
$valid = domainNameValid($string);
break;
case "domainnameport":
// Do normal domain name validation
$valid = domainNameValid($string, true);
2018-04-02 22:26:17 +00:00
// Check colon count
$colonParts = explode(":", $string);
if (count($colonParts) > 2) {
2018-04-02 22:26:17 +00:00
$valid = false;
}
break;
case "certificate_key":
if (strpos($string, "PRIVATE KEY") === FALSE) {
2018-04-02 22:26:17 +00:00
$valid = false;
}
break;
case "certificate":
if (strpos($string, "-----BEGIN CERTIFICATE-----") === FALSE) {
2018-04-02 22:26:17 +00:00
$valid = false;
}
if (strpos($string, "-----END CERTIFICATE-----") === FALSE) {
2018-04-02 22:26:17 +00:00
$valid = false;
}
break;
case "directory_at_least_two_levels":
if (substr_count($string, '/') < 2) {
2018-04-02 22:26:17 +00:00
$valid = false;
}
$protectedPaths = array("/var/www/vhosts", "/var/www/new", "/var/www/php_sessions", "/var/www/webalizer", "/var/www/passivedomains");
if (in_array($string, $protectedPaths)) {
2018-04-02 22:26:17 +00:00
$valid = false;
}
break;
case "email_address":
$valid = filter_var($string, FILTER_VALIDATE_EMAIL);
break;
case "url":
$valid = filter_var($string, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED);
break;
}
return $valid;
}
function testCertificateAndPrivateKeyHashMatch($cert, $key)
{
2018-04-02 22:26:17 +00:00
$testCertPath = "/var/www/new/ehcp/test/test.crt";
$testCertKeyPath = "/var/www/new/ehcp/test/test.key";
writeoutput2($testCertPath, $cert, "w+", false);
writeoutput2($testCertKeyPath, $key, "w+", false);
2018-04-02 22:26:17 +00:00
// Unset our arrays if they were set at some point
if (isset($errorsWithCert)) {
2018-04-02 22:26:17 +00:00
unset($errorsWithCert);
}
if (isset($errorsWithCertKey)) {
2018-04-02 22:26:17 +00:00
unset($errorsWithCertKey);
}
if (isset($openSSLCertHash)) {
2018-04-02 22:26:17 +00:00
unset($openSSLCertHash);
}
if (isset($openSSLCertKeyHash)) {
2018-04-02 22:26:17 +00:00
unset($openSSLCertKeyHash);
}
2018-04-02 22:26:17 +00:00
// Check to make sure we have a valid cert and cert key file
exec("openssl x509 -noout -modulus -in " . $testCertPath . " 2>&1 | grep -o -e error -e unable", $errorsWithCert);
exec("openssl rsa -noout -modulus -in " . $testCertKeyPath . " 2>&1 | grep -o -e error -e unable", $errorsWithCertKey);
if (is_array($errorsWithCert) && count($errorsWithCert) > 0) {
2018-04-02 22:26:17 +00:00
return false;
}
if (is_array($errorsWithCertKey) && count($errorsWithCertKey) > 0) {
2018-04-02 22:26:17 +00:00
return false;
}
2018-04-02 22:26:17 +00:00
// Now that we know the keys are OK, check the hashes and make sure they match...
exec("openssl x509 -noout -modulus -in " . $testCertPath . " | openssl md5", $openSSLCertHash);
exec("openssl rsa -noout -modulus -in " . $testCertKeyPath . " | openssl md5", $openSSLCertKeyHash);
if (is_array($openSSLCertHash) && is_array($openSSLCertKeyHash)) {
if (trim($openSSLCertHash[0]) == trim($openSSLCertKeyHash[0])) {
2018-04-02 22:26:17 +00:00
return true;
}
}
return false;
}
function makeSureSSLTestFileMatches($cert, $key)
{
2018-04-02 22:26:17 +00:00
$testCertPath = "/var/www/new/ehcp/test/test.crt";
$testCertKeyPath = "/var/www/new/ehcp/test/test.key";
2018-04-02 22:26:17 +00:00
// Make sure the files contain what has been sent in...
$inTestCrt = trim(file_get_contents($testCertPath));
$inTestKey = trim(file_get_contents($testCertKeyPath));
if ($cert != $inTestCrt || $key != $inTestKey) {
2018-04-02 22:26:17 +00:00
return false;
}
return true;
}
function testCertificateChainValid($chain)
{
2018-04-02 22:26:17 +00:00
// Unset variable if set before...
if (isset($openSSLResultsChainValid)) {
2018-04-02 22:26:17 +00:00
unset($openSSLResultsChainValid);
}
2018-04-02 22:26:17 +00:00
// Check to see if the chain certificate entered is valid
$testCertChainPath = "/var/www/new/ehcp/test/chain.crt";
writeoutput2($testCertChainPath, $chain, "w+", false);
2018-04-02 22:26:17 +00:00
exec("openssl verify $testCertChainPath 2>&1 | grep OK", $openSSLResultsChainValid);
if (is_array($openSSLResultsChainValid) && count($openSSLResultsChainValid) > 0) {
if (stripos($openSSLResultsChainValid[0], "OK") !== false) {
2018-04-02 22:26:17 +00:00
return true;
}
}
return false;
}
function makeSureSSLTestChainFileMatches($chain)
{
2018-04-02 22:26:17 +00:00
$testCertChainPath = "/var/www/new/ehcp/test/chain.crt";
$inTestChain = trim(file_get_contents($testCertChainPath));
if ($chain != $inTestChain) {
2018-04-02 22:26:17 +00:00
return false;
}
return true;
}
function manageService($service, $action)
{
2018-04-02 22:26:17 +00:00
passthru2("service $service $action", true, true);
}
function getServiceActionStr($service, $action)
{
2018-04-02 22:26:17 +00:00
return "service $service $action";
}
function getCustomApache2ListenPorts()
{
2018-04-02 22:26:17 +00:00
// Get the ports Apache2 is listening on
$originalBindPorts = shell_exec('cat "/etc/apache2/ports.conf" | grep "Listen"');
if (isset($originalBindPorts) && !empty($originalBindPorts)) {
2018-04-02 22:26:17 +00:00
// Split each Listen match into an array
if (stripos($originalBindPorts, "\n") != False) {
2018-04-02 22:26:17 +00:00
$originalBindPorts = explode("\n", $originalBindPorts);
} else {
2018-04-02 22:26:17 +00:00
// Must be only one port, so add it to our array.
$originalBindPorts[] = $originalBindPorts;
}
2018-04-02 22:26:17 +00:00
// Remove any empty values in our array
$originalBindPorts = array_filter($originalBindPorts);
2018-04-02 22:26:17 +00:00
// We want to ignore these ports, as the replacement file will handle the correct base ports based on web server configuration
$ignorePorts = array("80", "443");
2018-04-02 22:26:17 +00:00
// Loop through each listen entry, get only the port, and add it to the list of custom ports if it's not in our ignore ports array.
foreach ($originalBindPorts as $port) {
$port = preg_replace("/[^0-9]/", "", $port);
if (!in_array($port, $ignorePorts)) {
2018-04-02 22:26:17 +00:00
$realPorts[] = $port;
}
}
if (isset($realPorts) && is_array($realPorts)) {
2018-04-02 22:26:17 +00:00
return $realPorts;
}
}
2018-04-02 22:26:17 +00:00
// Default return
return false;
}
function addCustomPortsToApache($ports)
{
if (is_array($ports)) {
foreach ($ports as $port) {
2018-04-02 22:26:17 +00:00
writeoutput2("/etc/apache2/ports.conf", "Listen " . $port, "a+");
}
}
}
if (!function_exists("stripContentsAfterLine")) {
function stripContentsAfterLine($firstMatch, $content)
{
2018-04-02 22:26:17 +00:00
$finalContent = "";
foreach (preg_split("/((\r?\n)|(\r\n?))/", $content) as $line) {
if (trim($line) == trim($firstMatch) || startsWith(trim($line), $firstMatch)) {
2018-04-02 22:26:17 +00:00
return $finalContent . $firstMatch . "\n";
}
$finalContent .= $line . "\n";
}
return $finalContent;
2018-04-02 22:26:17 +00:00
}
}
if (!function_exists("getContentsAfterLine")) {
function getContentsAfterLine($firstMatch, $content)
{
2018-04-02 22:26:17 +00:00
$finalContent = "";
$foundMatch = false;
foreach (preg_split("/((\r?\n)|(\r\n?))/", $content) as $line) {
if (trim($line) == trim($firstMatch) || startsWith(trim($line), $firstMatch) || $foundMatch) {
if ($foundMatch === true) {
2018-04-02 22:26:17 +00:00
$finalContent .= $line . "\n";
}
$foundMatch = true;
}
}
return $finalContent;
2018-04-02 22:26:17 +00:00
}
}
/* LEFT OVERS FROM DBUTIL */
function selectstring($alanlar)
{
2018-04-02 22:26:17 +00:00
//if(count($alanlar)==0) return false;
$res = $alanlar[0];
$alansayisi = count($alanlar);
2018-04-02 22:26:17 +00:00
for ($i = 1; $i < $alansayisi; $i++) {
if (trim($alanlar[$i]) <> "")
$res .= "," . $alanlar[$i];
2018-04-02 22:26:17 +00:00
}
return $res;
2018-04-02 22:26:17 +00:00
}
function buildquery2($select, $filtre, $orderby)
{ // v1.0
$res = $select;
if ($filtre <> "") {
$res .= " where $filtre";
}
;
2018-04-02 22:26:17 +00:00
if ($sirala <> "") {
$res .= " order by $sirala";
}
;
return $res;
2018-04-02 22:26:17 +00:00
}
2022-02-18 20:19:30 +00:00
if (!function_exists('add_line_if_not_exists')) {
function add_line_if_not_exists($findLine, $file)
{
2022-02-18 20:19:30 +00:00
$lineExists = false;
if (file_exists($file)) {
2022-02-18 20:19:30 +00:00
$handle = fopen($file, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
if (trim($line) == trim($findLine)) {
2022-02-18 20:19:30 +00:00
$lineExists = true;
break;
}
}
fclose($handle);
}
if (!$lineExists) {
file_put_contents($file, $findLine . PHP_EOL, FILE_APPEND | LOCK_EX);
2022-02-18 20:19:30 +00:00
}
}
}
}
if (!function_exists("isValidHostname")) {
function isValidHostname($host)
{
if (filter_var(gethostbyname($host), FILTER_VALIDATE_IP)) {
return true;
}
return false;
}
}
?>