Checking form submission against a word list - forms

I have some code to check is a form is being spammed and if so then stop the email.
It includes a section like this:
if(strpos($messagefield, " cialis") !== false){
$noemail = true;
}
if(strpos($messagefield, " viagra") !== false){
$noemail = true;
}
etc for as many words as we have in the bad word list
This works fine, but is clumsy and difficult to easily add new words to check.
It would be easier if I could create an array and check any field against the array, but I am strugling to find an example to use (most examples still specify the text to search for which defeats the object in this case)
Can anyone help with code to check $messagefield against an array?
(I know there are better ways maybe but this works for us at the moment!)

$i = 0;
$wordlist = array(' cialis', ' viagra');
while ($i < count($wordlist) && $noemail == false) {
if (strpos($messagefield, $wordlist[$i]) !== false) {
$noemail = true;
}
$i++;
}

It is better to use stripos (case-insensitive version of strpos).
Try following code:
$a = array(' cialis', ' viagra');
for ($i = 0; $i < count($a); $i++)
if(stripos($messagefield, $a[$i]) !== false){
$noemail = true;
break;
}
}

Related

Show All Product in Magento 2

I want to show all product if it is enable or disabled doesn't matter.
with this
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
return $collection;
I only get enabled product please help me with that to get disabled product also.
Found two solution on this, please try first one, if it does not work for you, then you can try 2nd one.
You can use disable stock check on your collection by this:
$productCollection = $this->_productFactory->create()->getCollection();
$productCollection->setFlag('has_stock_status_filter', false);
Or else you can use this:
$collection = $this->_productCollectionFactory->create()
->addAttributeToSelect('*')
->load();
// Patch to alter load and get disabled products too
$collection->clear();
$where = $collection->getSelect()->getPart('where');
foreach ($where as $key => $condition)
{
if(strpos($condition, 'stock_status_index.stock_status = 1') !== false){
$updatedWhere[] = 'AND (stock_status_index.stock_status IN (1,0))';
} else {
$updatedWhere[] = $condition;
}
}
$collection->getSelect()->setPart('where', $updatedWhere);
$collection->load();

CodeIgniter PHP Parsing Error unexpected '{', expecting '('

What's with this PHP Parsing Error Unexpected '{', expecting '('
No backtrace, no any other error message, just one line in the controller, that's it -_-
I kept looking for solutions and reading many links related to this.
What could be the reason for the error in my code below..
This was my controller code (which was working fine):
if (isset($filter) && !empty($search)) {
$data['users'] = $this->model_search->searchTutor($field, $search);
}
elseif (($filter == 'subjName') && !empty($search)) {
$data['users'] = $this->model_search->searchBySubj($field, $search);
}
else {
$data['users'] = $this->model_search->getlist($field);
}
//later i wanted to add a code that will show No Result Found
The view file of my page started giving this error when I added an elseif statement in my controller (Search.php):
if (isset($filter) && !empty($search)) {
$data['users'] = $this->model_search->searchTutor($field, $search);
}
elseif (($filter == 'subjName') && !empty($search)) {
$data['users'] = $this->model_search->searchBySubj($field, $search);
}
//so I added another elseif
elseif (isset($filter) && empty($search)) {
$data['users'] = $this->model_search->getlist($field);
}
//and put the No Result last
else {
$this->session->set_flashdata('nores','<div class="alert text-center">No result matched your search.</div>');
}
Is it because of the multiple elseif condition or am I really missing something here? Please help..
elseif {
Your new elseif has no condition. When do you expect it to run? You need to add a condition.

POSTMAN jetpacks TESTING within for loop

Hitting API w/ GET request, checking if item has been deleted by globals.id variable, have test inside forloop and when I run test returns 0/0 tests passed. All of my console logs within the for loop work, the objects contain values matching what I have as well. Anyone know how to do this?
var data = JSON.parse(responseBody);
for (var i = 0; i < data.length; i++){
tests["id has been deleted"] = data[i].id !== globals.id;
if(data[i].id !== globalID){
tests["id has been deleted"] = data[i].id !== globals.id;
return true;
}
}
I could make test with for loop.
My Json:
{
"rows": [
{
"id": "2804",
"title": "Some title",
...
},
...
],
"total": "2788"
}
My test:
for (var i in data.rows){
var obj = data.rows[i];
tests["title of row #" + i + " is not null"] = !!obj.title;
tests["title of row #" + i + " is not empty"] = obj.title !== "";
}
But if I use "return true" Postman shows "Tests (0/0)"
You can make use of forEach loop for iterating the result set returned in response. It returns the objects of specified type which can be used for processing.
var data = JSON.parse(responseBody);
data.forEach(function(process){
var processId = "Id" + process.id;
//console.log("processId" + processId);
})
Yes Ranson Namba! I'm experiencing the same thing - even attempting to write to the console is ignored within my For loops. What's more, it would be ever so helpful to be able to specify arrays within the For loop, but apparently Postman doesn't allow that either. What's the solution to all this limiting BS?
var responseData = JSON.parse(responseBody);
for (i = 0; i < responseData.scoringResults.length; i++) {
var scores = responseData[i].score;
for (j; j < scores.length; j++){ //scores.length = 4 at this point
var scoreValues = scores[j].split(",");
tests["Verify number of score dimensions"] = scoreValues.length === 4;
}
}
tests["Status code is 200"] = responseCode.code === 200;
The last tests works fine, just nothing you put inside either for loop works, even a simple console.log("whatever"). A little help? Thx

Is this a valid way to check if db_row exists?

I am working with Zend and I needed to check whether a row in the DB already exists (A simple solution to get rid of the duplicate key error I was getting). I tried several things but nothing seemed to work... (for example the Zend_Validate_Db_NoRecordExists method)
So I wrote the following the code and I was wondering if this is a valid way to do it, or if I should do things differently:
In the model:
$where = $condition = array(
'user_id = ' . $user_id,
'page_id = ' . $page_id
);
$check = $this->fetchRow($where);
if(count($check) > 0) {
return null;
}else{
// Here I create a new row, fill it with data, save and return it.
}
And then in my view:
if($this->result != null) { /* do stuff */ }else{ /* do other stuff */ }
It does work but it does seem to take more time (duh, because of the extra query) and I am a bit unsure whether I should stick with this..
Any recommendation is welcome :)
Assuming you have coded your function in your controller
$row = $this->fetchRow($where); //If no row is found then $row is null .
if(!$row)
{
$row = $dbTb->createNew($insert); //$insert an associative array where it keys map cols of table
$row->save();
$this->view->row_not_found = true;
}
return $row;
In your view you can do this
if($this->row_not_found)
{
}else {
}

Any Way to Streamline this Logic?

I've written some code that translates an Entity Framework collection to some fixed fields. I ended up with the following snippet but isn't there a slicker way to accomplish this?
var numbers = c.ContactPhoneNumbers.OrderByDescending(n => n.IsPrimary);
int count = 0;
foreach (var number in numbers)
{
if (count == 0)
{
hc.PrimaryPhone = number.PhoneNumber;
hc.PrimaryPhoneType = number.PhoneNumberType;
}
else if (count == 1)
{
hc.SecondaryPhone = number.PhoneNumber;
hc.SecondaryPhoneType = number.PhoneNumberType;
}
else break;
count++;
}
c is an Entity Framework entity and c.ContactPhoneNumbers represents entries in a related table. Seems like this code could be made a little more straight forward and less awkward.
Since you are iterating the phone enumeration right away, might be better to use ToList() so you can use the indexer:
var numbers = c.ContactPhoneNumbers.OrderByDescending(n => n.IsPrimary).ToList();
if(numbers.Count > 0)
{
hc.PrimaryPhone = numbers[0].PhoneNumber;
hc.PrimaryPhoneType = number[0].PhoneNumberType;
}
if(numbers.Count > 1)
{
hc.SecondaryPhone = numbers[1].PhoneNumber;
hc.SecondaryPhoneType = numbers[1].PhoneNumberType;
}