Writing to extConf in extbase controller - typo3

I have a litte extbase extension that changes my color settings (e.g. css, cookiebar, etc.), and I also want to change the color of the backend plugin button, in the backend sysext in my controller.
Getting the value:
$var = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['backend']);
$var["loginHighlightColor"]="#444444";
But now, how do I save the value?
When trying the following statement, it sets the value correctly but it doesn't get persisted:
$GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['backend'] = serialize($var);
Even trying to persist manually with the PersistentManager doesn't work.

Thanks to Bernd Wilke I got it:
$var = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['backend']);
$var["loginHighlightColor"]="#444444";
$GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['backend'] = serialize($var);
$configurationUtility = $this->objectManager->get(\TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility::class);
$newConfiguration = $configurationUtility->getCurrentConfiguration("backend");
\TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($newConfiguration, $var);
$configurationUtility->writeConfiguration(
$configurationUtility->convertValuedToNestedConfiguration($newConfiguration),
"backend"
);

This is how it works inside my AdditionalConfiguration.php. Maybe you can adapt it:
$resourcePath = 'EXT:' . $extKey . '/Resources/Public/Images/';
$GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['backend'] = serialize(
[
'loginLogo' => $resourcePath . 'logo.svg',
'loginHighlightColor' => '#c0c0c0',
'loginBackgroundImage' => $resourcePath . 'login-bg.svg',
]
);

you propably need to do the same than found here:
http://api.typo3.org/typo3cms/current/html/_configuration_controller_8php_source.html#l00156
as that function is protected you can't call it from your code.
there are two public functions you possibly can use:
public function saveAction(array $config, $extensionKey) (line 109)
public function saveAndCloseAction(array $config, $extensionKey) (line 131)

Related

zend session namespace not working

I am unable to access zend session in included file via layout.
what i have done so far -
//bootstrap
public function _initSession()
{
Zend_Session::start();
$test = new Zend_Session_Namespace('test');
}
//controller
public function init(){
$test = new Zend_Session_Namespace('test');
$test->abc = 'defghi';
}
//layout include file
<?php include_once( APPLICATION_PATH . '/data/ga_test.php');?>
//ga_test.php
$test = new Zend_Session_Namespace('test');
echo 'this is ' . $test->abc;
I am not able to access the variable in ga_test file. I am getting an empty variable. But if I include ga_test end of each view file then it works. Obviously I don't want to go to every view file and include ga_test.php. Can I do this via layout.
I am sure, I am doing something wrong here. Any help would be really appreciated.
Thanks

mojolicious helper storing an elasticsearch connection

i'm experimenting with elasticsearch within mojolicious.
I'm reasonably new at both.
I wanted to create a helper to store the ES connection and I was hoping to pass the helper configuration relating to ES (for example the node info, trace_on file etc).
If I write the following very simple helper, it works;
has elasticsearch => sub {
return Search::Elasticsearch->new( nodes => '192.168.56.21:9200', trace_to => ['File','/tmp/elasticsearch.log'] );
};
and then in startup
$self->helper(es => sub { $self->app->elasticsearch() });
however if I try to extend that to take config - like the following -
it fails. I get an error "cannot find index on package" when the application calls $self->es->index
has elasticsearch => sub {
my $config = shift;
my $params->{nodes} = '192.168.56.21:' . $config->{port};
$params->{trace_to} = $config->{trace_to} if $config->{trace_to};
my $es = Search::Elasticsearch->new( $params );
return $es;
};
and in startup
$self->helper(es => sub { $self->app->elasticsearch($self->config->{es}) });
I assume I'm simply misunderstanding helpers or config or both - can someone enlighten me?
Just fyi, in a separate controller file I use the helper as follows;
$self->es->index(
index => $self->_create_index_name($index),
type => 'crawl_data',
id => $esid,
body => {
content => encode_json $data,
}
);
that works fine if I create the helper using the simple (1st) form above.
I hope this is sufficient info? please let me know if anything else is required?
First of all, has and helper are not the same. has is a lazily built instance attribute. The only argument to an attribute constructor is the instance. For an app, it would look like:
package MyApp;
has elasticsearch => sub {
my $app = shift;
Search::ElasticSearch->new($app->config->{es});
};
sub startup {
my $app = shift;
...
}
This instance is then persistent for the life of the application after first use. I'm not sure if S::ES has any reconnect-on-drop logic, so you might need to think about it a permanent object is really what you want.
In contrast a helper is just a method, available to the app, all controllers and all templates (in the latter case, as a function). The first argument to a helper is a controller instance, whether the current one or a new one, depending on context. Therefore you need to build your helper like:
has (elasticsearch => sub {
my ($c, $config) = #_;
$config ||= $c->app->config->{es};
Search::ElasticSearch->new($config);
});
This mechanism will build the instance on demand and can accept pass-in arguments, perhaps for optional configuration override as I have shown in that example.
I hope this answers your questions.

Correct password_hash using

I am trying to make my own user-authorization php-script to login users which where created by other php-class (not mine).
So, I try to make hash-string from word admin to make it:
$2y$10$trJyrB8x2V/hKKeKJvNF0Otz6OqFgisd0fiLc7B1ssHzSvpE0ADYu
My PHP version is 5.4.4. And I am trying to code it like this:
echo (password_hash("admin", PASSWORD_DEFAULT));
but it outputs nothing.
I found this code in the third-party php-class:
public function make($value, array $options = array())
{
$cost = isset($options['rounds']) ? $options['rounds'] : $this->rounds;
$hash = password_hash($value, PASSWORD_BCRYPT, array('cost' => $cost));
if ($hash === false) {
throw new \RuntimeException("Bcrypt hashing not supported.");
}
return $hash;
}
Does anybody know how to use password_hash correctly?
Your options array probably creates a problem try this:
$hash = password_hash($value, PASSWORD_BCRYPT, ['cost' => $cost]);

Slim Framework - Using a GET route

I'm absolutely new to Slim Framework. I'm working on an Webservice that should provide an interface between an Android App and a Web-Application. I used the Slim Documentation to make my first steps and now I want to create a simple GET route, to receive information from the App. Here is what I have so far:
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$name_outside = '';
$app = new \Slim\Slim();
$app->get('/session/program_name/:name', function ($name) use($app) {
$name_outside = $name;
echo $name;
});
$app->run();
echo $name_outside;
I need to access the variable :name outside the function, but what I get is nothing. What I am doing wrong here?
Btw: I know that GET-routes usually are used to list existing resources, but for my simple case, I decided to use it that way.
Fix your code to hold name as args parameters ,then you can get in in Your function
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$name_outside = '';
$app = new \Slim\Slim();
$app->get('/session/program_name/{name}', function ($args) use($app) {
$name_outside = $args['name'];
echo $args['name'];
});
$app->run();
To acess the $name_outside inside the function context you pass it.
$app->get('/session/program_name/:name', function ($name) use($app, &$name_outside) {
$name_outside = $name;
echo $name;
});
But perhaps you're using Slim in a wrong way. Why you have to access the variable outside of your route?
No code is execyted after the run() call. That's the way slim works, try to put a die where you're echo the variable, it isn't reachable.
You shouldn't have the need to access the context of the route outside of it this way. To transform a request or a response you use middlewares with the hooks.

ZEND tralslations for addMultiOption text in Form for poEdit

I dont have an idea why translations are not working in with Zend_Form.
I would like to be able translate options for selects.
For now i have something like this:
my form class:
(...)
$this->translate = Zend_Registry::get('translate');
Zend_Form::setDefaultTranslator( Zend_Registry::get('translate') );
(...)
$select = new Zend_Form_Element_Select('select');
// $select->addMultiOption('0', $this->translate('Aktywny'));
$select->addMultiOption('0', $this->translate->_('Aktywny'));
$select->addMultiOption('1', 'Nieaktywny');
in my bootstrap file i have something like this:
protected function _initTranslate()
{
Zend_Loader::loadClass('Zend_Translate');
Zend_Loader::loadClass('Zend_Registry');
$translate = new Zend_Translate('gettext', APPLICATION_PATH.'/languages',
'browser',
array('scan' => Zend_Translate::LOCALE_FILENAME));
//changing language and setting it to session if changed
$session = new Zend_Session_Namespace('jezyk');
if(isset($session->language)) {
$translate->setLocale($session->language);
} else
$translate->setLocale('pl');
$registry = Zend_Registry::getInstance();
$registry->set('Zend_Translate', $translate);
}
and it works fine for controllers, phtml files and plugins where i call it by
$this->translate('string to translate');
and in plugins
$this->view->translate('string to translate');
but those methods won't work in form. It throws exception:
Warning: Exception caught by form: No entry is registered for key 'translate' Stack Trace: #0
to make it working as i wrote in comment just have to change line:
$this->translate = Zend_Registry::get('translate');
for
$this->translate = Zend_Registry::get('Zend_Translate');
cause i didn't saw that i'm getting wrong translate from registry. It should be Zend_Translate like in Bootstrap file, not translate as i did.
And this is solution for my problems with translate and now i can make translations in form files :)