ZF2 CurrencyFormat on Default locale not working - locale

In my application I use serval languages. I'n my module.php I set the locale of a user by the follow methode:
$translator = $e->getApplication()->getServiceManager()->get('translator');
$translator ->setLocale(\Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']))
->setFallbackLocale($this->setDomainLocale());
There are running server domain.tdl's on this application (different languages) so I use a function in my fallback to set the fallback locale per domain, anyway...
I want to make use of currencyFormat but I don't get it work to use it with the locale by user. I tried the code below with and without '\Locale::getDefault())';
A number like '1509053' and should be returned as '€ 1.509.053.00' or ',' depends on locale, I get just '€ 1509053.00'.
$this->plugin("currencyformat")->setCurrencyCode("EUR")->setLocale(\Locale::getDefault());
output of \Locale::getDefault())
string(11) "en_US_POSIX"
output of module.php $translator
class Zend\I18n\Translator\Translator#193 (8) { protected $messages => array(0) { }
protected $files => array(0) { } protected $patterns => array(1) { 'default' => array(1) { [0] => array(3) { 'type' => string(7) "gettext" 'baseDir' => string(119) "..../module/.../config/../language" 'pattern' => string(5) "%s.mo" } } } protected $remote => array(0) { } protected $locale => string(35) "nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4" protected $fallbackLocale => string(5) "nl_NL" protected $cache => NULL protected $pluginManager => NULL }
Hope someone can put me in the right direction :) Thnx

To use the function '\Locale::getDefault();' we need to set first '\Locale::setDefault(language_code);' $translate->setLocale doesn't set Locale::getDefault();.

Related

What is the correct way to map and translate cookies in logstash?

My input is a log from IIS server with cookies included. I want my output (elasticsearch) to have a field like this:
"cookies": {
"cookie_name": "cookie_value"
}
Also for some cookies I want their values to be replaced with some other values from a dictionary.
Basically, I think the following filter config solves my problem:
kv {
source => "cookie"
target => "cookies"
trim => ";"
include_keys => [ "cookie_name1","cookie_name2" ]
}
translate {
field => "cookies.cookie_name1"
destination => "cookies.cookie_name1"
dictionary_path => "/etc/logstash/dict.yaml"
override => "true"
fallback => "%{cookies.cookie_name1}"
}
The problem is that I don't know if it’s the right way to do this, and whether it will work at all (especially the cookies.cookie_name part).
The correct way to do this is:
kv {
source => "cookie"
target => "cookies"
field_split => ";+"
include_keys => [ "cookie_name1","cookie_name2" ]
}
translate {
field => "[cookies][cookie_name1]"
destination => "[cookies][cookie_name1]"
dictionary_path => "/etc/logstash/dict.yaml"
override => "true"
fallback => "%{[cookies][cookie_name1]}"
}
https://www.elastic.co/guide/en/logstash/current/event-dependent-configuration.html#logstash-config-field-references
https://www.elastic.co/guide/en/logstash/7.4/plugins-filters-kv.html
https://www.elastic.co/guide/en/logstash/7.4/plugins-filters-translate.html

How to call another filter from within a ruby filter in logstash.

I'm building out logstash and would like to build functionality to anonymize fields as specified in the message.
Given the message below, the field fta is a list of fields to anonymize. I would like to just use %{fta} and pass it through to the anonymize filter, but that doesn't seem to work.
{ "containsPII":"True", "fta":["f1","f2"], "f1":"test", "f2":"5551212" }
My config is as follows
input {
stdin { codec => json }
}
filter {
if [containsPII] {
anonymize {
algorithm => "SHA1"
key => "123456789"
fields => %{fta}
}
}
}
output {
stdout {
codec => rubydebug
}
}
The output is
{
"containsPII" => "True",
"fta" => [
[0] "f1",
[1] "f2"
],
"f1" => "test",
"f2" => "5551212",
"#version" => "1",
"#timestamp" => "2016-07-13T22:07:04.036Z",
"host" => "..."
}
Does anyone have any thoughts? I have tried several permutations at this point with no luck.
Thanks,
-D
EDIT:
After posting in the Elastic forums, I found out that this is not possible using base logstash functionality. I will try using the ruby filter instead. So, to ammend my question, How do I call another filter from within the ruby filter? I tried the following with no luck and honestly can't even figure out where to look. I'm very new to ruby.
filter {
if [containsPII] {
ruby {
code => "event['fta'].each { |item| event[item] = LogStash::Filters::Anonymize.execute(event[item],'12345','SHA1') }"
add_tag => ["Rubyrun"]
}
}
}
You can execute the filters from ruby script. Steps will be:
Create the required filter instance in the init block of inline ruby script.
For every event call the filter method of the filter instance.
Following is the example for above problem statement. It will replace my_ip field in event with its SHA1.
Same can be achieved using ruby script file.
Following is the sample config file.
input { stdin { codec => json_lines } }
filter {
ruby {
init => "
require 'logstash/filters/anonymize'
# Create instance of filter with applicable parameters
#anonymize = LogStash::Filters::Anonymize.new({'algorithm' => 'SHA1',
'key' => '123456789',
'fields' => ['my_ip']})
# Make sure to call register
#anonymize.register
"
code => "
# Invoke the filter
#anonymize.filter(event)
"
}
}
output { stdout { codec => rubydebug {metadata => true} } }
Well, I wasn't able to figure out how to call another filter from within a ruby filter, but I did get to the functional goal.
filter {
if [fta] {
ruby {
init => "require 'openssl'"
code => "event['fta'].each { |item| event[item] = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, '123456789', event[item] ) }"
}
}
}
If the field FTA exists, it will SHA2 encode each of the fields listed in that array.

How do I convert an integer to string in CakePHP?

In my CakePHP app when I try to get data like this:
$this->loadModel('Radio');
$posts = $this->Radio->find('all');
the integers are displayed like strings (in debug) :
'Radio' => array(
'idSong' => '4',
'name' => 'batman',
'title' => 'Batman Theme Song'
),
why? the type is int in the DB. I need integers correctly displayed in my JSON files
Not sure if there's a straightforward solution, but you could change the model data using afterfind
Something like
public function afterFind($results, $primary = false) {
foreach ($results as $key => $val) {
if (isset($val['Radio']['idSong'])) {
$results[$key]['Radio']['idSong'] = (int)$results[$key]['Radio']['idSong'];
}
}
return $results;
}

Kohana auth model

I'm new to kohana 3.2 and i couldnt find any answer regrading the auth module.
this is my code and forsome reason ever since i changed the user model to extend model_auth_user
the validation isnt being done prooperly. The password field can be inserted empty and no excpetion will be caught and same if the password_confirm and password fields are different:
public function action_new()
{
if ($_POST){
try
{
$user = ORM::factory('user')
->values(array(
'username' => $_POST['username'],
'email' => $_POST['email'],
'password' => $_POST['password'],
'password_confirm' => $_POST['password_confirm']));
$user->save();
$user->add('roles', ORM::factory('role', array('name' => 'login')));
$this->request->redirect('user/index');
}
catch (ORM_Validation_Exception $e)
{
$errors = $e->errors();
}
}
$view = View::factory('user/new')
->bind('errors',$errors); //pass the info to the view
$this->response->body($view); //show the view
}
thanks
You can override run_filter() method to force Kohana ignore password filtering in case of empty value. For example, put this code to your User_Model:
protected function run_filter($field, $value)
{
if ($field === "password" AND $value === "")
return "";
parent::run_filter($field, $value);
}
Try code sample from Model_Auth_User::create_user();
$user->save(Model_User::get_password_validation($_POST)->rule('password', 'not_empty'));
This validation execute before filters(hashing password). After hashing - blank password becomes not empty string.

Custom error message for validation rules in Kohana 3.2

I have a controller=product.php,a model=category.php, and a custom error message file category.php. When I am submitting the empty form , I am getting error messages for rule "not_empty, but I am not getting error message for rule "unique_categoryname"
Please help!
class Controller_Product extends Controller_Application
{
public function action_addcategory()
{
$errors='';
$cat = new Model_Category();
$validation=Validation::factory($this->request->post())
->rule('cat_name',array($cat,'unique_categoryname'));
if ($validation->check())
{
if (HTTP_Request::POST == $this->request->method())
{
try
{
$cat_name=$_POST['cat_name'];
$cat_description=$_POST['cat_description'];
if (isset($_FILES['cat_image']))
{
$filename = $cat->upload_photo($_FILES['cat_image']);
}
$cat->InsertCategory($cat_name,$cat_description,$filename);
Request::current()->redirect('product/listcategory');
}
catch (ORM_Validation_Exception $ex)
{
$errors = $ex->errors('models');
}
}
}
$view = new View('product/addcategory');
$view->set("categories",$cat);
$view->set('errors',$errors);
$this->template->content=$view;
}
}
class Model_Category extends ORM
{
public function rules()
{
return array(
'cat_name' => array(
array('not_empty'),
array('min_length', array(':value', 4)),
array('max_length', array(':value', 32)),
),
'cat_description' => array(
array('not_empty'),
array('min_length', array(':value', 10)),
),
);
}
}
//Custom error messages page : messages/models/category.php
return array(
'cat_name' => array(
'not_empty' => 'You must provide a category name.',
'min_length' => 'The category name must be at least :param2 characters long.',
'max_length' => 'The category name must be less than :param2 characters long.',
'unique_categoryname'=> 'Category Name already exists, please change',
),
'cat_description' => array(
'not_empty' => 'You must provide category description .',
'min_length' => 'The category description must be at least :param2 characters long.',
),
);
You need to pass your $validation to the ORM save/update method.
Assuming InsertCategory is a wrapper for saving, you should pass it there:
$cat->InsertCategory($cat_name, $cat_description, $filename, $validation);
Then in InsertCategory method:
public function InsertCategory(....)
{
// .....
$this->save($validation);
}
Be aware that there is a bug in Kohana 3.2 and external validation messages go to _external.php file.
You can find more information here: http://kohanaframework.org/3.2/guide/orm/validation#external-validation

Resources