Recaptcha2 'non-object' error in Zend - zend-framework

I'm trying to implement a Recaptcha2 in an old project that uses Zend. I'm using this library for it https://packagist.org/packages/cgsmith/zf1-recaptcha-2
Initially I was getting this error
Uncaught Zend_Exception: No entry is registered for key 'application'
In my _initRecaptcha, I changed the first line from
$config = \Zend_Registry::get('application');
to
$config = \Zend_Registry::get('config')->application;
This is my _initRecaptcha now
public function _initRecaptcha()
{
$config = \Zend_Registry::get('config')->application;
$params = $config->recaptcha->toArray();
$params['messageTemplates'] = [
\Cgsmith\Validate\Recaptcha::INVALID_CAPTCHA => 'The captcha was invalid', // set custom/translated message
\Cgsmith\Validate\Recaptcha::CAPTCHA_EMPTY => 'The captcha must be completed'
];
\Zend_Registry::set('recaptcha', $params);
}
But now I am getting this error
Trying to get property of non-object in /var/www/my-site/application/Bootstrap.php on line 79
Line 79 is the first line in _initRecaptcha
Can anyone what I need to change?
Thanks.

The library seems to be assuming your config is stored in the registry under the key 'application' (which seems questionable to me). You could either add a method to your bootstrap that sets this up (see their example: https://github.com/cgsmith/zf1-recaptcha-2/blob/master/example/application/Bootstrap.php#L13-L18), or change the code to not require it:
public function _initRecaptcha()
{
$params = $this->getOptions()['recaptcha'];
$params['messageTemplates'] = [
\Cgsmith\Validate\Recaptcha::INVALID_CAPTCHA => 'The captcha was invalid', // set custom/translated message
\Cgsmith\Validate\Recaptcha::CAPTCHA_EMPTY => 'The captcha must be completed'
];
\Zend_Registry::set('recaptcha', $params);
}
(It's a long time since I used ZF1 but I think that should work.)
Both cases assume you've added recaptcha lines to your application.ini per the library instructons.

Related

Zend 1.12 Zend/Form/Element -> addError() gives warning on line 2247 of Zend/FormI/Element

Environment:
Apache 2.4
Windows 8
PHP 5.4.14
Zend 1.12
I am doing a very very very simple action on my form, adding an error message on one of my elements with:
$form->getElement('elemetnid')->addError('error');
It works wonders for everyone else, but on my case it also gives out a warning that reads:
Warning: Invalid argument supplied for foreach() in ...\library\Zend\Form\Element.php on line 2247
This only happens when I try to set the error on my MultiSelect element, but if I do so on another element like Text, then everything is great and no warning is given. So I went to inspect the line on the warning, and realized that the problem is that "getValue()" for that element is returning "NULL" and that is not a correct value for the foreach loop inside the framework´s code. But how is that my responsability?
My question is, if anyone knows how to add an error message for a multiSelect without this warning popping up.
Thanks
I can't recreate your error with: Apache 2.2.16, Php 5.3.7 and Zf 1.12.5.
The behaviour is rather odd though. If I invalidate an element before validating the form (by adding an error to an element), it looks like it short circuits the form validation and the error message does not get applied to the form output.
If I try and force in an invalid value via the URL, a value not in my multi-select, I get repeat error messages, and my previously set error messages override the 'X' was not found in the haystack message.
$form = new Zend_Form;
$form->setMethod('GET');
$listOptions = array('one','two', 'three');
$select = new Zend_Form_Element_Multiselect('options', array(
'multiOptions' => $listOptions,
'validators' => array(
array('InArray',
false,
array(array_keys($listOptions)))
)
));
$submit = new Zend_Form_Element_Submit('submit', array(
'label' => 'Do something'
));
$form->addElements(array($select, $submit));
if(isset($_GET) && count($_GET)) {
if(true)
$select
->addError('The form will never validate.')
->addError('Two wrongs don\'t make a right.');
$form->isValid($_GET);
}
$form->setView(new Zend_View);
echo $form;
if ($form->isErrors()) {
echo 'Form did not validate';
var_dump($form->getErrors());
}

Class Sofzo_From not found error when implementing Sozfo TinyMCE solution

I'm trying to implement TinyMCE to text areas using the solution mentioned in Sofzo. But when I try to extend the Sofzo_Form I get the following error :
Fatal error: Class 'Sozfo_Form' not found in /home/foldername/public_html/application/forms/PageForm.php on line 4
What I have done so far -
Uploaded the Sofzo files to library with following directory structure
/library
../Sozfo
../Form.php
../../Form
../../../Element
../../../../TinyMce.php
../../View
../../../Helper
../../../Exception.php
../../../../FormTinyMce.php
../../../../TinyMce.php
Loaded the classes in application.ini as
Autoloadnamaspaces[] = "Sofzo_"
And in bootstrap as
$autoLoader = Zend_Loader_Autoloader::getInstance();
$autoLoader->registerNamespace('Zend_');
$autoLoader->registerNamespace('SF_');
$autoLoader->registerNamespace('CMS_');
$autoLoader->registerNamespace('Sofzo_');
$loader = new Zend_Loader_PluginLoader();
$loader->addPrefixPath('Zend_View_Helper', 'Zend/View/Helper/')
->addPrefixPath('Storefront_View_Helper',
'application/modules/storefront/views/helpers')
->addPrefixPath('Sozfo_Form', 'Sozfo/');
$view=new Zend_View();
$view->addHelperPath('Sozfo/View/Helper', 'Sozfo_View_Helper');
But when I try to extent the Sofzo_Form in Page_Form as
class Form_PageForm extends Sozfo_Form { }
This issue was solved thanks to Tim Fountain. But now when I load an element as
$this->addElement('tinyMce', 'message', array(
'label' => 'Message',
'required' => true,
'cols' => '50',
'rows' => '10',
'editorOptions' => new Zend_Config_Ini(APPLICATION_PATH . '/configs/tinymce.ini', 'moderator')
));
I get the following error
Plugin by name 'FormTinyMce' was not found in the registry
Read through several comments in original site and they are said to add
$view->addHelperPath('Sozfo/View/Helper', 'Sozfo_View_Helper');
to bootstrap. I've already done that, but I'm guessing I'm not doing something right. Help is much appreciated.
I think the issue is ZF can't find the class because it doesn't know about the Sozfo_ namespace. You've attempted to register this namespace in two different ways, but both of them are incorrect.
In application.ini, you have:
Autoloadnamaspaces[] = "Sofzo_"
But this should be:
autoloaderNamespaces[] = "Sozfo_"
Then in the bootstrap you've tried to register it with:
$autoLoader->registerNamespace('Sofzo_');
but presumably this should be:
$autoLoader->registerNamespace('Sozfo_');
(note spelling). Which ever fix you apply you should only use one of these methods, as they do the same thing.
If it still doesn't work after that then there's an issue with your include_path.
Edit: To fix the view helper path, try this instead of the two lines you currently have:
$view = new Zend_View();
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
$stack = Zend_Controller_Action_HelperBroker::getStack();
$stack->push($viewRenderer);
$view->addHelperPath('Sozfo/View/Helper', 'Sozfo_View_Helper');
This adds the helper path to a view object like you have but also supplies it to the view renderer (which is what renders all the view scripts). If you don't do this then the view renderer uses its own view object, so the view object you setup in the bootstrap is never used for anything.
If this doesn't work, try passing a full path as the first parameter to addHelperPath instead:
$view->addHelperPath(APPLICATION_PATH.'/../library/Sozfo/View/Helper', 'Sozfo_View_Helper');

SugarCRM SOAP test call fails

I am attempting to test a SugarCRM Soap connection using the following code:
<?
define('sugarEntry', TRUE);
require_once('include/nusoap/nusoap.php');
$sugarclient = new nusoapclient('http://www.mycrmurl.com/soap.php?wsdl',true);
echo $sugarclient->call('test', 'test string');
?>
Unfortunately, the test call returns NULL. Thoughts on how to begin troubleshooting?
I'm not familiar with a SugarCRM SOAP method called test, so unless it's a custom method you made yourself, I'd try with some simple valid calls. (Tested with Sugar CE 6.2).
<?php
require_once('include/nusoap/lib/nusoap.php');
$myWsdl = 'http://www.mycrmurl.com/soap.php?wsdl';
$myAuth = array(
'user_name' => 'will',
'password' => MD5('will'),
'version' => '0.1'
);
$soapClient = new nusoap_client($myWsdl,true);
$loginParams = array('user_auth' => $myAuth, 'application_name' => 'MyApp');
$loginResult = $soapClient->call('login', $loginParams);
$sessionId = $loginResult['id'];
echo $sessionId;
?>
If the above still gives you problems, try the following:
Look in the web server log (Is the call getting through)
Enable the SugarCRM logging and set the level to debug
Either enable PHP error output or make PHP log errors to a log file
Use e.g. SoapUI to test SOAP call
See question 5396302 for a more thorough SOAP example
Check the SugarCRM SOAP documentation
Do this:
$result = $sugarclient->call('test', 'test string');
echo print_r ($result);
It will print the array result, if you just want to see the error description do this:
$result = $sugarclient->call('test', 'test string');
echo $result['error']['description'];
The result is a multidimensional array.

Custom error message for input validators (using the array syntax)

ZF 1.11.2
I've tried most of the syntaxes. They didn't click.
$validators = array('product_name' => array('alnum'));
//...
$input = new Zend_Filter_Input($filters, $validators, $_POST);
How in the world do you set a custom error message for alnum with the syntax above? Using 'messages' => array('Not alnum!!')? Yeah, well... How? I must've tried 100 nested arrays.
Use the built in translator.
For example, configure the translator in your config file to use a simple array
; Translations
resources.translate.data = APPLICATION_PATH "/lang"
resources.translate.adapter = "Array"
resources.translate.options.scan = "directory"
resources.translate.options.disableNotices = "1"
This tells the Translate application resource plugin that you want to
keep your translations under APPLICATION_PATH/lang
use the Array adapter (simplest)
scan the translation directory for languages / locales
ignore errors about unknown translations (ie user preferes en_AU but you don't have a specific translation file for that language)
Now, create folders for any languages you want to support. At a minimum, you'll want application/lang/en. For example
application
lang
en
en_AU
en_US
In each language folder, create a translate.php file. This file will contain (and return) an array of key / value pairs for each translation. You can find the keys for each validator message in the validator class. Here's an example for the Alnum validator
<?php
// application/lang/en/translate.php
return array(
Zend_Validate_Alnum::NOT_ALNUM => 'Not alnum!!',
Zend_Validate_Alnum::INVALID => 'Not valid!!'
);
For all Zend validators, you can also use the %value% placeholder in your message, eg
Zend_Validate_Alnum::NOT_ALNUM => "'%value%' is not alpha-numeric"
If you are simply trying to change the validation messages for a form element, I have always done it like this (inside a class that extends Zend_Form):
$this->addElement('text', 'myTextField', array(
'label' => 'The Label',
'description' => 'The description for the field...',
'filters' => array(
'StringTrim',
// etc
),
'validators' => array(
array('NotEmpty', true, array(
'messages' => 'This field is required',
)),
array('AnotherValidator', true, array(
'messages' => 'Bad value',
)),
// etc
),
));
Are you saying that this didn't work? Or are you using your validator in a more general context, in which case #Phil Brown's (awesome!) answer will do the job.
Disabling the translator on the element will disable the translation of all the validator messages. It is not possible to use a translator on the form or element and overwrite just one validator message. When the element is validated the translator is injected to every validator. The validator will use the translator if it is set. Thereby the custom error message won't be used.
Zend_Validate_Abstract::_createMessage()
// $message is your custom error message
$message = $this->_messageTemplates[$messageKey];
if (null !== ($translator = $this->getTranslator())) {
// your custom error message gets overwritten because the messageKey can be translated
if ($translator->isTranslated($messageKey)) {
$message = $translator->translate($messageKey);
} else {
$message = $translator->translate($message);
}
}
I think it is only possible to use a custom error message by disable the translator on the element.
$element->setDisableTranslator(true)
Use setMessage and disable translator if you have one.
$alnum = new Zend_Validate_Alnum();
$alnum->setDisableTranslator(true);
$alnum->setMessage(
'Not alnum!!',
Zend_Validate_Alnum::NOT_ALNUM
);
$validators = array('product_name' => array($alnum));
If you use your validator on a form element, you have to disable the translator on the element.

Image Upload with Zend_Service_Nirvanix

I can't seem to upload an image using Zend_Service_Nirvanix. Is it even possible?
I have a feeling that my problem has something to do with not being able to figure out how to set the UploadHost on the Transfer Service.
Any help is greatly appreciated! My deadline is July 16th!
Here is my code:
$nirvanix = new Zend_Service_Nirvanix(array('appKey' => $key,
'username' => $user,
'password' => pass));
$NSImfs = $nirvanix->getService('IMFS');
$options = array('sizeBytes' => filesize($source));
$storageNode = $NSImfs->getStorageNode($options);
$NSTransfer = $nirvanix->getService('Transfer');
$options = array('uploadToken' => $storageNode->getStorageNode->UploadToken,
'path' => $original,
'fileData' => file_get_contents($source));
$result = $NSTransfer->uploadFile($options);
Here is the error I keep getting:
Zend_Service_Nirvanix_Exception: XML
could not be parsed from response:
Server Error in '/' Application. The
resource cannot be found. Description:
HTTP 404. The resource you are looking
for (or one of its dependencies) could
have been removed, had its name
changed, or is temporarily
unavailable. Please review the
following URL and make sure that it is
spelled correctly.
Requested URL:
/ws/Transfer/UploadFile.ashx
in
/Applications/MAMP/bin/php5/lib/php/Zend/Service/Nirvanix/Response.php
on line 119
You're getting a 404?
Have you checked for an updated version of that library?
Try going into the libray and changing UploadFile.ashx to UploadFile.aspx. I don't think ashx is not a standard extension.
Maybe that will fix it.
There's a commercial upload tool from Aurigma that has support for file and image upload to Nirvanix. Here's the link (see Uploading to Nirvanix section there) to the help topic to check.
To do a local upload (rather than a web upload via the browser) you just have to call the putContents method passing the files data.
Example:
$nirvanix = new Zend_Service_Nirvanix(array('appKey' => $key,
'username' => $user,
'password' => pass));
$NSImfs = $nirvanix->getService('IMFS');
$response = $NSImfs->putContents($destination_file_and_path,
file_get_contents($source_file));
if($response->ResponseCode != 0)
{
echo 'Fail!';
}
You would only call GetStorageNode if you want to generate a token to pass a browser the upload token.