Concrete5 error with no result - php

I am using concrete to render my site, it uses the following error system;
if (isset($error) && $error != '') {
if ($error instanceof Exception) {
$_error[] = $error->getMessage();
} else if ($error instanceof ValidationErrorHelper) {
$_error = $error->getList();
} else if (is_array($error)) {
$_error = $error;
} else if (is_string($error)) {
$_error[] = $error;
}
?>
<?php if ($format == 'block') { ?>
<div class="alert-message error">
<?php foreach($_error as $e): ?>
<?php echo $e?><br/>
<?php endforeach; ?>
</div>
<?php } else { ?>
<ul class="ccm-error">
<?php foreach($_error as $e): ?>
<li><?php echo $e?></li>
<?php endforeach; ?>
</ul>
<?php } ?>
<?php } ?>
This is used to detect errors in the system, or validation errors.
My problem is that the error div is being displayed but there is no errors, e.g.:
I have tried dumping the variables and this is the result (in this case):
Array object(ValidationErrorHelper)#89 (1) { ["error:protected"]=> array(0) { } }
This is the ValidationErrorHelper system:
<?php
defined('C5_EXECUTE') or die("Access Denied.");
class ValidationErrorHelper {
protected $error = array();
public function reset() {
$this->error = array();
}
public function add($e) {
if ($e instanceof ValidationErrorHelper) {
$this->error = array_merge($e->getList(), $this->error);
} else if (is_object($e) && ($e instanceof Exception)) {
$this->error[] = $e->getMessage();
} else {
$this->error[] = $e;
}
}
public function getList() {
return $this->error;
}
public function has() {
return (count($this->error) > 0);
}
public function output() {
if ($this->has()) {
print '<ul class="ccm-error">';
foreach($this->getList() as $error) {
print '<li>' . $error . '</li>';
}
print '</ul>';
}
}
}
?>
Is there any way to remove this? or diagnose it?

Sometimes the error object is set, but doesn't have any errors in it. If you look at the very top "if" statement, you'll see this:
if (isset($error) && $error != '') {
So what's happening is the $error variable is an object or an array and has been set by the system, but it doesn't have any errors in it. But your code is always outputting the container <div> as long as the $error variable is set (or not an empty string). What you need to do is add an additional check on the $_error variable (the one that is created in the first dozen or so lines of the code) to see if it's empty or not.
Try this:
if (isset($error) && $error != '') {
if ($error instanceof Exception) {
$_error[] = $error->getMessage();
} else if ($error instanceof ValidationErrorHelper) {
$_error = $error->getList();
} else if (is_array($error)) {
$_error = $error;
} else if (is_string($error)) {
$_error[] = $error;
}
if (!empty($_error)) {
if ($format == 'block') { ?>
<div class="alert-message error">
<?php foreach($_error as $e): ?>
<?php echo $e?><br/>
<?php endforeach; ?>
</div>
<?php } else { ?>
<ul class="ccm-error">
<?php foreach($_error as $e): ?>
<li><?php echo $e?></li>
<?php endforeach; ?>
</ul>
<?php }
}
}

Related

Why do I get undefined index error on my PHP function code

I even tried using "isset", but sadly it is not working.
function pageBanner($args=NULL){
if(!$args['title']){
$args['title']=get_the_title();
}
if (!$args['subtitle']){
$args['subtitle'] = get_field('page_banner_subtitle');
}
if (!$args['photo']){
if (get_field('page_banner_background_image')){
$args['photo'] = get_field('page_banner_background_image')['sizes']['pageBanner'];
}
else {
$args['photo']=get_theme_file_uri('images/pages.jpg');
}
}?>
I didn't know the problem on my if(!$args['title']){
$args['title']=get_the_title(); it is working, but the subtitle and photo are undefined index.
Try something like this:
function pageBanner($args = null)
{
if ($args === null) {
$args = [];
}
if (!isset($args['title'])) {
$args['title'] = get_the_title();
}
if (!isset($args['subtitle'])) {
$args['subtitle'] = get_field('page_banner_subtitle');
}
if (!isset($args['photo'])) {
$field = get_field('page_banner_background_image');
if ($field && isset($field['sizes']['pageBanner'])) {
$args['photo'] = $field['sizes']['pageBanner'];
} else {
$args['photo'] = get_theme_file_uri('images/pages.jpg');
}
}
}

PHP undefined variable when url identifier is present

I apologize for this being a bit lengthy but I wanted to show the code and not leave anything out.
The Goal: The user submits a registration form that submits the form to a database which adds the info plus a unique string to the database. Once the user submits an email is sent to the user that contains a link to a php page that has that unique string as a URL identifier at the end like so:
xyz.com/activate.php?uid=292ca78b727593baad9a
When the user clicks that link they are taken that page where they will fill out another form and when they click submit it will activate their account.
The Problem: When the user goes to the link provided they receive an error from my validation page like so:
Undefined index: variable in process.php
BUT when the user deletes the URL identifier so the URL shows as:
xyz.com/activate.php
The user does not receive an error and the validation page (process.php) works properly. I have attempted to use the identifier with a $_GET['uid'] and checking if it exists before running the code but the result was the same. I cannot find an answer when attempting to google this issue so I apologize if this has been asked before.
The Question: Why does this work without the URL identifier but not with it? I do realize the URL identifier basically does a $_REQUEST when the page first loads which is what runs the process.php. Is there a way to prevent that?
So that you guys know the code I am working with I have posted it below.
activate.php:
<?php
// validate the form.
require_once('process.php');
$validation_rules = array(
'company_name' => array(
'required' => true,
'min-length' => 2
)
);
$db_error = '';
$validate = new validator();
$validate->validate($validation_rules);
if ($validate->validate_result()) {
// the validation passed!
}
?>
<div class="bg v-100 cm-hero-activate">
<div class="v-100">
<div class="v-set v-mid">
<form class="v-mid v-bg-white <?php if(!$validate->validate_result() && $_POST || !empty($error)) { echo "shake"; } ?>" name="activation" action="" method="post">
<fieldset>
<div class="form-content">
<div id="content">
<div class="form-inner in" data-id="1" data-name="activate">
<h1>ACTIVATE YOUR ACCOUNT</h1>
<div class="form-section">
<h2>Personal Info</h2>
<?php if (!empty($db_error)) {
echo $db_error;
} ?>
<div class="field-group">
<input type="text" id="company_name" class="field required" name="company_name" value="<?php $validate->form_value('company_name'); ?>">
<label for="company_name" class="placeholder">Company Name</label>
<?php $validate->error(array('field' => 'company_name', 'display_error' => 'single')); ?>
</div>
</div>
<div class="field-bottom">
<button name="submit" data-name="activate" class="bttn btn-dark btn-hover-gloss">ACTIVATE MY ACCOUNT</button>
</div>
</div>
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>
process.php:
<?php
class validator {
private $validation_rules;
private $errors = array();
private $validate_result = array();
public function validate($rules) {
$this->validation_rules = $rules;
if($this->validation_rules && $_REQUEST) {
foreach($this->validation_rules as $field => $rules) {
$result = $this->process_validation($field, $rules);
if($result == false) {
$this->validate_result[] = 0;
} elseif($result == true) {
$this->validate_result[] = 1;
}
}
}
}
public function form_value($field_name = '') {
if($this->validation_rules) {
if($_REQUEST && $_REQUEST[$field_name]) {
if(!$this->validate_result()) {
echo $_REQUEST[$field_name];
}
}
}
}
public function validate_result() {
if($this->validation_rules) {
if($_REQUEST) {
$final_result = true;
$length = count($this->validate_result);
for($i=0;$i < $length; $i++) {
if($this->validate_result[$i] == 0) {
$final_result = false;
}
}
return $final_result;
}
}
}
private function process_validation($field, $rules) {
$result = true;
$error = array();
foreach($rules as $rule => $value) {
if($rule == 'required' && $value == true) {
if(!$this->required($field, $value)) {
$error[] = "$field - required";
$result = false;
}
} elseif($rule == 'min-length') {
if(!$this->minlength($field, $value)) {
$error[] = "$field - minimun length is $value";
$result = false;
}
}
}
$this->errors[] = array($field => $error);
return $result;
}
public function error($data = '') {
if($this->validation_rules) {
if($_REQUEST) {
foreach($this->errors as $err) {
if(isset($data['field'])) {
foreach($err as $field => $field_error) {
if($data['field'] == $field) {
foreach($field_error as $error_data) {
if(isset($data['display_error']) == 'single') {
echo '<p class="error">' . $error_data . '</p>';
goto next;
} else {
echo '<p class="error">' . $error_data . '</p>';
}
}
next:
}
}
} else {
foreach($err as $field => $field_error) {
foreach($field_error as $error_data) {
if(isset($data['display_error']) == 'single') {
echo '<p class="error">' . $error_data . '</p>';
goto next1;
} else {
echo '<p class="error">' . $error_data . '</p>';
}
}
next1:
}
}
}
}
}
}
private function required($field, $value) {
if(empty($_REQUEST[$field])) {
return false;
} else {
return true;
}
}
private function minlength($field, $value) {
if(strlen($_REQUEST[$field]) < $value) {
return false;
} else {
return true;
}
}
?>
EDIT: it seems that the error is happening on the if statement in the min-length function: if(strlen($_REQUEST[$field]) < $value)
EDIT 2: This may also be of some use. The textbox is being filled with this: <br /><font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan=
and underneath that I receive this error: Notice: Undefined index: company_name in C:\...\process.php on line 25 Call Stack #TimeMemoryFunctionLocation 10.0005369632{main}( )...\activate.php:0 20.0029403928validator->form_value( )...\activate.php:80 ">
Your $field_name doesn't exist in the $_REQUEST array which is why you are getting your undefined index error.
You are not checking if the value is set - just accessing it via $_REQUEST[$field_name] in your if statement.
Change
public function form_value($field_name = '') {
if($this->validation_rules) {
if($_REQUEST && $_REQUEST[$field_name]) {
if(!$this->validate_result()) {
echo $_REQUEST[$field_name];
}
}
}
}
To
public function form_value($field_name = '') {
if($this->validation_rules) {
if($_REQUEST && isset($_REQUEST[$field_name])) {
if(!$this->validate_result()) {
echo $_REQUEST[$field_name];
}
}
}
}
Unrelated Tips.
Aim for cleaner - and more concise readable code.
$input = [
'name' => 'Matthew',
];
$rules = [
'name' => 'required|string',
];
$v = (new Validator())->validate($input, $rules);
if ($v->fails()) {
// Do something with $v->errors();
return;
}
// Do something with validated input
Try and avoid using else or elseif wherever possible. This can be achieved by looking for the negative first, and exiting early. Try not to have too many levels of nesting. It makes your code extremely difficult to read and increases cyclomatic complexity.
Also - take a look at an MVC framework which should help you to structure your code. A good start would be something like Laravel https://laravel.com/ there are good tutorials on https://laracasts.com/
You are getting this error because you have logic like
if($this->validation_rules && $_REQUEST) {
In PHP, an empty array is falsy and a non-empty one, truthy.
$_REQUEST is a combination of $_GET, $_POST and $_COOKIE.
When you add the uid query parameter, $_REQUEST will not be empty and therefore not falsy and your validator will attempt to run but without the expected POST data (such as company_name).
If you're only wanting to validate POST data, you should only be inspecting $_POST.
See Gravy's answer for safer ways to check for the existence of array keys.

Allow access only member's group and administrator

I'm trying to allow access for certain content only for specific member's user and administration.
With the following code I can't view the content when I'm logged as admin:
<?php
if (is_category(4)) {
if (!current_user_can('my-group') || !current_user_can('add_users')) {
echo '<h1 class="entry-title">You can not access</h1><p>This content is only for Administrator and group\'s members.</p>';
}
} else {
?>
Also I tryed each condition individually, but as admin I can't view the content.
I tryed this example code:
<?php if (is_category(8)) {
if (!current_user_can('mio-gruppo') || !user_can('administrator')) {
echo 'you can not pass';
}
} else {
echo 'you pass';
}
?>
But the two conditional statement goes in conflict. If I try both separately works right
I guess it should be && instead of ||:
<?php
if (is_category(4)) {
if (!current_user_can('my-group') && !current_user_can('add_users')) {
echo '<h1 class="entry-title">You can not access</h1><p>This content is only for Administrator and group\'s members.</p>';
}
} else {
?>
Alternatively make it the other way round:
<?php
if (is_category(4)) {
if (current_user_can('my-group') || current_user_can('add_users')) {
echo 'access granted';
} else {
echo 'no access';
}
}
?>
This is what I need:
<?php
if (is_category(8) && (current_user_can(array(1,2,3,4,5,6,7)) || !is_user_logged_in())) {
if (!current_user_can('mio-gruppo')) {
echo 'you can not pass';
}
} else {
echo ' you can pass';
}
?>

error returning information using php

I am steps away from finishing this project, I seem to have a problem with my logic in my fetchMessage() function, I am passing in the session_id however I am getting nothing returned here is my function code
function fetchMessages($session)
{
$get = ("SELECT * FROM chatRoom WHERE session_id = '$session'");
$hold = mysql_query($get, $con);
if($hold)
{
return mysql_fetch_array($hold);
}
}
the code calling this function is
<?php
include 'core/conection.php';
include 'function.php';
if(isset($_POST['method']) === true && empty($_POST['method']) === false)
{
$method = trim($_POST['method']);
$session = trim($_POST['session']);
if($method === 'fetch')
{
$messages = fetchMessages($session);
if(empty($messages) === true)
{
echo 'A representative will be with you shortly';
echo '<br />';
echo $session;
}else
{
foreach($messages as $message)
{
$ts = $message['timestamp'];
?>
<div class = "message">
<?php echo date('n-j-Y h:i:s a', $ts); ?>
<?php echo $message['username']; ?>
says:<p><?php echo nl2br($message['message']); ?></p>
</div>
<?php
}
}
}
}
?>
I know it is making it at least this far because at this line
if(empty($messages) === true)
{
echo 'A representative will be with you shortly';
echo '<br />';
echo $session;
}
it displays the correct session_id i have a fealing it is either my fetchMessages function that is wrong or the html code to display the results that is wrong.

PHP if statement errmsg

I have this if statment
if(!empty($URL) && ($safe===true)){
//lots of code
}
Is it possible to show different error messages depending on what condition failed?
For example if $URL is empty echo "URL empty";
and if $safe===false echo "GTFO";
Just add this to your code
else if(empty($URL)
{
echo "url empty";
}
else if($safe===false)
echo "Get Out"; // be polite ;)
if (empty($url))
{
echo "URL empty";
}
elseif ($safe === false)
{
echo "GTFO";
}
else
{
//lots of code
}
} else {
if($safe === false){
die("GTFO");
}
if (empty($url)){
echo "URL Empty.";
}
}
Yes; you could make use of an else if statement.
if (!empty($URL) && ($safe===true)) {
//lots of code
} else if (empty($URL)) {
// report that url is empty
} else if ($safe === false) {
// report that safe is false
}
Alternatively, you could just use an else statement to report that the if condition was false.
I propose the following solution. It will allow you to show multiple errors and set each condition only once (instead of having so many conditions and anti-conditions as other solutions proposed).
$errors = array();
if(empty($URL) {
$errors[] = 'URL empty';
}
if($safe !== true) {
$errors[] = 'GTFO';
}
if(empty($errors)) {
//lots of code
} else {
echo '<ul>';
foreach($errors as $error_message) {
echo '<li>' . $error_message . '</li>';
}
echo '</ul>';
}

Categories