Including and using Zend Service ReCaptcha in ZF2 (v2.3.3) - forms

how to include Recaptcha service in zend framework 2?
I tried to do like this:
public function contactAction()
{
$formContact = new ContactForm();
$pubKey = 'mypubkey';
$privKey = 'myprivkey';
$recaptcha = new ZendService\ReCaptcha\ReCaptcha($pubKey, $privKey);
return array ('formContact' => $formContact, 'recaptcha' => $recaptcha);
}
but I discovered that ZendService\ReCaptcha is not present by default when you download the framework.
So, I downloaded it from here
https://github.com/zendframework/ZendService_ReCaptcha
and I placed it into vendor\zendframework\zendframework\library\zend together with the other parts of the library.
I tried to refresh the page but doesn't work again because it can't find the zend service recaptcha.
Fatal error: Class 'Application\Controller\ZendService\ReCaptcha\ReCaptcha' not found in C:\Program Files (x86)\xampp\htdocs\Zf-tutorial\module\Application\src\Application\Controller\IndexController.php on line 79
can someone help me? I thought it was simple to implement recaptcha, but it is not so ! thanks!

Add the zendservice-recaptcha module to your composer.json file and run an update:
{
...
"repositories": [
{
"type": "composer",
"url": "http://packages.zendframework.com/"
}
],
...
"require": {
...
"zendframework/zendservice-recaptcha": "*",
...
}
...
}
update composer :
php composer.phar update
This will install the module and configure the relevant class mapping and you will be able to access the classes by adding the use statements as with any other classes you use.

Even i tried recaptcha but with no success so implemented something different to refresh captcha and worked very well, try this once
resetCaptcha function:
$form = $this->getServiceLocator()->get('zfcuser_register_form');
$captcha = $form->get('captcha')->getCaptcha();
$data = array();
$data['id'] = $captcha->generate();
$data['src'] = $captcha->getImgUrl() .
$captcha->getId() .
$captcha->getSuffix();
return $data;
ajax request :
$(document).ready(function() {
$('#refreshcaptcha').click(function() {
var data = [];
var form = <?php $this->registerForm; ?>
data.push({name: "action", value: 'resetCaptcha'});
data.push({name: "params[form]", value: form});
$.post("<?php echo BASE_URL ?>/user/iajax", data,
function(data) {
$('#form_reg img').attr('src', data.src);
$('#captcha-id-hidden').attr('value', data.id);
}, 'json');
});
});
Html call :
<p class="refresh_captcha"><?php echo $this->formCaptcha($form->get('captcha')); ?>
<input type="button" id="refreshcaptcha" value="refresh">
</p>

You do not properly install the library ZendService\ReCaptcha.
your system write:
Class 'Application\Controller\ZendService\ReCaptcha\ReCaptcha' not found
You must:
placed it into vendor\zendframework\zendframework\library
In the file vendor/ZF2/library/Zend/Loader/StandardAutoloader.php insert string
$this->registerNamespace('ZendService', dirname(dirname((__DIR__)))
. '/ZendService');
in case self::AUTOREGISTER_ZF:
in the file init_autoloader.php insert string
$loader->add('ZendService', $zf2Path);.

Related

Drupal 9 : Ajax process to display an entity form in a popup

I need help I am new in Drupal 9 Ajax development. I have developed a piece of code but it doesn't work.
To sumarise, I want to display an entity form in a popup. The popup is built dynamically with jQuery, I use Ajax to fill the popup and the form is built in an ajax command (the code is : \Drupal::service('entity.form_builder')->getForm($livraison, 'edit');)
The form is well displayed but I think I miss something in ajax developpement because when I submit the form, the entity is not saved. When I look at the javascript console log, I see 3 others command responses plus the command I have created in server side (modifLivraison).
result: Array(4) [ {…}, {…}, {…}, {…} ]
​0: Object { command: "settings", settings: {…}, merge: true }
1: Object { command: "insert", method: "prepend", selector: "head", … }
2: Object { command: "insert", method: "append", selector: "body", … }
3: Object { command: "modifLivraison", modif: "...
I think that the Entity form needs some javascript and that's why these 3 commands have been added.
I don't know if my ajax process is correct.
Here my code :
In the command class:
class ModifLivraisonCommand implements CommandInterface
{ protected livraisonId; ...
/**
* Implements Drupal\Core\Ajax\CommandInterface:render().
*/
public function render()
{
...
$entitytype_manager = \Drupal::service('entity_type.manager');
$storage = $entitytype_manager->getStorage('livraison');
$livraison = $storage->load($this->livraisonId);
...
$livraison_form = \Drupal::service('entity.form_builder')->getForm($livraison, 'edit');
$livraison_form['#cache']['max-age'] =0;
$output = \Drupal::service('renderer')->render($livraison_form);
return [
'command' => 'modifLivraison',
'modif' => $output
];
...
The controller :
public function modifLivraison($livraison) {
$response = new AjaxResponse();
$response->addCommand(new ModifLivraisonCommand($livraison));
return $response;
}
and the javascript in the client side :
create_popup('0,0,0,0')
...
jQuery.ajax('/livraisons/modif-livraison/' + livraison, {
success: function(result,textStatus, jqXHR) {
jQuery(".spinner").remove();
console.log(result);
var ajax;
Drupal.AjaxCommands.prototype.settings(ajax,result[0],200)
jQuery(result[1].selector)[result[1].method](result[1].data);
jQuery(result[2].selector)[result[2].method](result[2].data);
...
jQuery("#modif_livraison").append(result[3].modif);
...
}
})
I think the error is in the way of ajax calling of the url (jQuery.ajax('/livraisons/modif-livraison/' + livraison ...)
My problem is the entity is not saved when the form is submitted. What's wrong ?

Using Symfony Form component standalone with security-csrf - error on submission

I have a question regarding symfony/form using as a standalone component and security-csrf running with PHP build-in server. I hardly remember having such issue with the Symfony framework.
When setting symfony/form as a standalone component I tried this code for both v4.2 and v5.1 https://github.com/xmgcoyi/standalone-forms/tree/4.2+twig. A rewrite of webmozart's example mentioned here https://symfony.com/doc/current/components/form.html
The csrf token is generated with twig-bridge, but when submitting the form - on calling$form->isValid() - invalid csrf error appears.
By default csrf protection is enabled, setting to false - the form submits.
Tried CSRF component with both setups with NativeSessionTokenStorage and SessionTokenStorage + Session of HttpFoundation.
Could you give any hint on what I'm doing wrong and where to look at?
P.S.
Code samples with csrf error on submission:
https://github.com/xmgcoyi/standalone-forms/tree/4.2+twig
https://github.com/liorchamla/pratique-symfony-form/tree/06-protection-csrf
UPD
The apps above work well, the problem was in browser storage filled with garbage.
Setting to false in $formFactory->createBuilder(FormType::class, null, ['csrf_protection' => false]) submits the form
This is a bit of a guess but the 4.2 linked repo has:
$csrfManager = new CsrfTokenManager($csrfGenerator, $csrfStorage);
$csrfTokenManager = new CsrfTokenManager();
Two token managers. One is used in the twig form engine and one is used in the form factory extension. Does not seem like a reasonable thing to do.
Here is an updated 5.1 working example. I stripped it down even more from your linked repo. But the only thing that I really changed was the token manager.
# index.php
require_once '../vendor/autoload.php';
$app = new App();
$app->run();
final class App
{
public function run()
{
$csrfGenerator = new UriSafeTokenGenerator();
$csrfStorage = new NativeSessionTokenStorage();
$csrfManager = new CsrfTokenManager($csrfGenerator, $csrfStorage);
$twig = new Environment(new FilesystemLoader([
'../templates',
'../vendor/symfony/twig-bridge/Resources/views/Form',
]));
$formEngine = new TwigRendererEngine(['form_div_layout.html.twig'], $twig);
$twig->addRuntimeLoader(new FactoryRuntimeLoader([
FormRenderer::class => function () use ($formEngine,$csrfManager) {
return new FormRenderer($formEngine, $csrfManager);
},
]));
$twig->addExtension(new TranslationExtension());
$twig->addExtension(new FormExtension());
$formFactory = Forms::createFormFactoryBuilder()
->addExtension(new CsrfExtension($csrfManager))
//->addExtension(new ValidatorExtension($validator))
->getFormFactory();
$form = $formFactory->createBuilder()
->add('firstName', TextType::class)
->getForm();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$form->submit($_POST[$form->getName()]); // form
if ($form->isValid()) {
dump('form is valid');
}
}
echo $twig->render('index.html.twig', [
'form' => $form->createView(),
]);
}
}
The composer.json is simply:
{
"require": {
"symfony/form": "^5.1",
"symfony/twig-bridge": "^5.1",
"symfony/translation": "^5.1",
"symfony/security-csrf": "^5.1"
},
"require-dev": {
"symfony/var-dumper": "^5.1"
}
}
If you still have trouble then I would suggest tracking down where the sessions are stored and then verifying the that csrf token is being properly stored. It should look something like:
_csrf|a:1:{s:4:"form";s:43:"9v1tUNe3J3eYVOmEPwVdz5_iISfzBg8Qa9pLMV8tSN4";}
This was actually kind of an interesting exercise in using the twig system for standalone forms. Thanks.

Laravel 5.4 Locale restores to default when using redirect()

I'm struggling with the Laravel's locale on Fresh new Project. I googled many times, but they didn't solve my problem.
Then I followed locale in laravel 5.4 returns to the pervious locale after refresh source, but it works only when I call pages by view
return view('home');
And it does not work when I use routes
return redirect()->route('home');
Here are my files:
web.php
Route::get('/lang/{locale}', function ($locale) {
App::setLocale($locale);
Session::put('locale', $locale);
//return view('home'); ###### This one works
return redirect()->route('home'); ###### Where as this does NOT work
});
Route::get('/', function () {
return view('welcome');
});
Route::get('/home', 'HomeController#index')->name('home');
home.blade.php
<div class="panel-body">
You are logged in!
{{__('auth.success')}}
<br>
Rus
<br>
Kgz
</div>
ChangeLocale.php Middleware (I call it for every request of pages)
public function handle($request, Closure $next)
{
if(Session::has('locale')) {
app()->setLocale(Session::get('locale'));
}
return $next($request);
}
Thanks in advance ;)
Ok, after I changed
Route::get('/home', 'HomeController#index')->name('home');
to
Route::get('/home', 'HomeController#index')->name('home')->middleware('changeLocale');
It started to work.
So, why my Middleware, is not working?
Should I assign middleware seperately to all my routes?
You may add your changeLocale middleware to web group in app/Http/Kernel.php:
protected $middlewareGroups = [
'web' => [
...
\Your\ChangeLocateMiddleware::class
],
...
]
As you may see in app/Providers/RouteServiceProvider.php, this middleware group applies to all your web routes.

Twitter Typeahead.js with Yahoo Finance in AJAX

I am trying to couple the new version of Typeahead.js and using it with JSON that needs to be pulled from AJAX and not from a JSON file like they have in their examples. I just can't get it to work, I don't want to cache the JSON result or anything, I want to pull it live from Yahoo.
My HTML input is <input type="text" id="symbol" name="symbol" autofocus autocomplete="off" placeholder="Symbol" onkeyup="onSymbolChange(this.value)" />
My AJAX/PHP file has this to retrieve data (this part work, I tested it with Firebug)
header('Content-type:text/html; charset=UTF-8;');
$action = (isset($_GET['action'])) ? $_GET['action'] : null;
$symbol = (isset($_GET['symbol'])) ? $_GET['symbol'] : null;
switch($action) {
case 'autocjson':
getYahooSymbolAutoComplete($symbol);
break;
}
function getYahooSymbolAutoCompleteJson($symbolChar) {
$data = #file_get_contents("http://d.yimg.com/aq/autoc?callback=YAHOO.util.ScriptNodeDataSource.callbacks&query=$symbolChar");
// parse yahoo data into a list of symbols
$result = [];
$json = json_decode(substr($data, strlen('YAHOO.util.ScriptNodeDataSource.callbacks('), -1));
foreach ($json->ResultSet->Result as $stock) {
$result[] = '('.$stock->symbol.') '.$stock->name;
}
echo json_encode(['symbols' => $result]);
}
The JS file (this is where I'm struggling)
function onSymbolChange(symbolChar) {
$.ajax({
url: 'yahoo_autocomplete_ajax.php',
type: 'GET',
dataType: 'json',
data: {
action: 'autocjson',
symbol: symbolChar
},
success: function(response) {
$('#symbol').typeahead({
name: 'symbol',
remote: response.symbols
});
}
});
}
I don't think that I'm suppose to attach a typeahead inside an AJAX success response, but I don't see much examples with AJAX (except for a previous version of typeahead)... I see the JSON response with Firebug after typing a character but the input doesn't react so good. Any guidance would really be appreciated, I'm working on a proof of concept at this point... It's also worth to know that I'm using AJAX because I am in HTTPS and using a direct http to Yahoo API is giving all kind of problems with Chrome and new Firefox for insecure page.
UPDATE
To make it to work, thanks to Hieu Nguyen, I had to modify the AJAX JSON response from this echo json_encode(['symbols' => $result]); to instead this echo json_encode($result); and modify the JS file to use the code as suggested here:
$('#symbol').typeahead({
name: 'symbol',
remote: 'yahoo_autocomplete_ajax.php?action=autocjson&symbol=%QUERY'
});
I have to do it in reverse, i.e: hook the ajax call inside typeahead remote handler. You can try:
$('#symbol').typeahead({
name: 'symbol',
remote: '/yahoo_autocomplete_ajax.php?action=autocjson&symbol=%QUERY'
});
You don't have to create onSymbolChange() function since typeahead will take care of that already.
You can also filter and debug the response from backend by using:
$('#symbol').typeahead({
name: 'symbol',
remote: {
url: '/yahoo_autocomplete_ajax.php?action=autocjson&symbol=%QUERY',
filter: function(resp) {
var dataset = [];
console.log(resp); // debug the response here
// do some filtering if needed with the response
return dataset;
}
}
});
Hope it helps!

JSON object parsing error using jQuery Form Plugin

Environment: JQuery Form Plugin, jQuery 1.7.1, Zend Framework 1.11.11.
Cannot figure out why jQuery won't parse my json object if I specify an url other than a php file.
The form is as follows:
<form id="imageform" enctype="multipart/form-data">
Upload your image <input type="file" name="photoimg" id="photoimg" />
<input type="submit" id ="button" value="Send" />
</form>
The javascript triggering the ajax request is:
<script type="text/javascript" >
$(document).ready(function() {
var options = {
type: "POST",
url: "<?php $this->baseURL();?>/contact/upload",
dataType: 'json',
success: function(result) {
console.log(result);
},
error: function(ob,errStr) {
console.log(ob);
alert('There was an error processing your request. Please try again. '+errStr);
}
};
$("#imageform").ajaxForm(options);
});
</script>
The code in my zend controller is:
class ContactController extends BaseController {
public function init() {
/* Initialize action controller here */
}
public function indexAction() {
}
public function uploadAction() {
if (isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") {
$image = $_FILES['photoimg']['tmp_name'];
$im = new imagick($image);
$im->pingImage($image);
$im->readImage($image);
$im->thumbnailImage(75, null);
$im->writeImage('userImages/test/test_thumb.jpg');
$im->destroy();
echo json_encode(array("status" => "success", "message" => "posted successfully"));
}
else
echo json_encode(array("status" => "fail", "message" => "not posted successfully"));
}
}
When I create an upload.php file with the above code, and modify the url from the ajax request to
url: "upload.php",
i don't run into that parsing error, and the json object is properly returned. Any help to figure out what I'm doing wrong would be greatly appreciated! Thanks.
You need either to disable layouts, or using an action helper such as ContextSwitch or AjaxContext (even better).
First option:
$this->_helper->viewRenderer->setNoRender(true);
$this->_helper->layout->disableLayout();
And for the second option, using AjaxContext, you should add in your _init() method:
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('upload', 'json')
->initContext();
This will disable automatically disable layouts and send a json header response.
So, instead of your two json_encode lines, you should write:
$this->status = "success";
$this->message = "posted successfully";
and
$this->status = "fail";
$this->message = "not posted successfully";
In order to set what to send back to the client, you simply have to assign whatever content you want into view variables, and these variables will be automatically convert to json (through Zend_Json).
Also, in order to tell your controller which action should be triggered, you need to add /format/json at the end of your URL in your jQuery script as follow:
url: "<?php $this->baseURL();?>/contact/upload/format/json",
More information about AjaxContext in the manual.
Is the Content-type header being properly set as "application/json" when returning your JSON?