3

I am trying to block a recent "Contact Us" form spammer. I figured the best way to do this is by adding a form key and validating it in the controller.

This works when I modify core files, which is obviously not what the end solution should be,

I have read several questions and tutorials on overriding a controller, and I have tried them with no luck, the core controller always gets used, and the validation never happens. What am I doing wrong? (note: I have not created a module in Magento as of yet, this is my first)

Here's what I have:

New file: app/code/local/ACS/Contacts/controllers/IndexController.php

This is a copy of the core file app/code/core/Mage/Contacts/controllers/IndexController.php with the addition of

if (!$this->_validateFormKey()) {
    $error = true;
}

in the try block of the postAction() function.

2nd New File: app/code/local/ACS/Contacts/etc/config.xml

Which looks like this:

<?xml version="1.0"?>
<config>
  <modules>
    <acs_contacts>
      <vesion>0.1.0</version>
    </acs_contacts>
  </modules>
  <frontend>
    <routers>
      <contacts>
        <args>
          <modules>
            <contacts before="Mage_Contacts">ACS_Contacts</contacts>
          </modules>
        </args>
      </contacts>
    </routers>
  </frontend>
</config>

And finally, last new file app/etc/modules/ACS_Contacts.xml:

<?xml version="1.0"?>
<config>
  <modules>
    <ACS_Contacts>
      <active>true</active>
      <codePool>local</codePool>
   </ACS_Contacts>
 </modules>
</config>

And of course I have added the form_key to app/design/frontend/theme/template/contacts/form.phtml

2
+50

First you need to fix your app/code/local/ACS/Contacts/etc/config.xml file:

<?xml version="1.0"?>
<config>
  <modules>
    <ACS_Contacts>
      <version>0.1.0</version>
    </ACS_Contacts>
  </modules>
  <frontend>
    <routers>
      <contacts>
        <args>
          <modules>
            <ACS_Contacts before="Mage_Contacts">ACS_Contacts</ACS_Contacts>
          </modules>
        </args>
      </contacts>
    </routers>
  </frontend>
</config>

Then your app/etc/modules/ACS_Contacts.xml needs a little update:

<?xml version="1.0"?>
<config>
  <modules>
    <ACS_Contacts>
      <active>true</active>
      <codePool>local</codePool>
      <depends>
          <Mage_Contacts/>
      </depends>
   </ACS_Contacts>
 </modules>
</config>

Finally, don't simply copy app/code/core/Mage/Contacts/controllers/IndexController.php into app/code/local/ACS/Contacts/controllers/IndexController.php, make your new controller extend the original controller and just create your own postAction method:

<?php
require_once 'Mage/Contacts/controllers/IndexController.php';

class ACS_Contacts_IndexController extends Mage_Contacts_IndexController
{
    public function postAction()
    {
         // Here you can copy the original method and modify it to fit your needs
    }
}
  • Okay, I have made the changes you suggested, "ACS_Contacts" is now the same case everywhere and my controller extends the core and only defines the postAction function including the key validation. Unfortunately, it still appears to be using the unmodified controller. – David Wilkins Apr 26 '16 at 13:04
  • @DavidWilkins did you copy my entire definition of the config.xml ? Because you also had a typo vesion instead of version . – Raphael at Digital Pianism Apr 26 '16 at 13:06
  • 1
    Ah! I totally missed the typo in "version". When I corrected that, I (correctly) got an internal server error, because I also missed the include_once in the controller. After correcting those, it works! THANKS!~ – David Wilkins Apr 26 '16 at 13:34
  • @DavidWilkins glad you figured it out =D – Raphael at Digital Pianism Apr 26 '16 at 13:36
2

In config.xml file

<acs_contacts>
      <vesion>0.1.0</version>
</acs_contacts>

changed to:

<ACS_Contacts>
      <vesion>0.1.0</version>
</ACS_Contacts>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.