Typo3 Extbase: get domain in CommandController - dns

I'm trying to access the base url of my site inside a command action like this:
namespace Vendor\TxTest\Command;
class TestCommandController extends \TYPO3\CMS\Extbase\Mvc\Controller\CommandController
{
/**
* logger
*
* #var \TYPO3\CMS\Core\Log\LogManager
*/
protected $logger;
/**
* Class constructor
*/
public function __construct()
{
$this->logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( 'TYPO3\\CMS\\Core\\Log\\LogManager' )->getLogger( __CLASS__ );
}
/**
* Test command
*/
public function testCommand()
{
$homeUrl = \TYPO3\CMS\Core\Utility\GeneralUtility::locationHeaderUrl( '/' );
$this->logger->info( 'url: ' . $homeUrl );
$this->logger->info( "\n\r\n\r" );
}
}
When I run the command from the Scheduler backend module, the domain looks ok, but when it runs automatically, the result is:
Mon, 10 Mar 2014 ... component="Vendor.TxNews.Command.TestCommandController": url: http:///
What is the correct way to get the domain in this context?

PHP knows the domain from the server-call. If your site is on a specific server, you might have several urls pointing to this server. Your PHP does not know by itself which domain he has. Only from the request that the user is doing PHP is getting this information in the $_SERVER-var that Typo3/Extbase can read. I assume your script is running on different servers if you want to get the url? Can you put a configuration on the server that is different for each server?
One approach to do this would be to store the url from a user-call and read this in your Background-Module.

to put it clear: If you run the scheduler automatically and therefore trigger PHP in CLI mode, there is no request URL / it is empty, as like the name already suggests u run in command line interface mode.
Typoscript has the baseURL settable and switchable, but even there the domain of the call is undefined, which is perfectly right.

Related

selenium webdriver get from partial url in python

I want to get driver.get from a partial url so for example:
usually we do this:
driver.get('www.dummy.com/event/rocknroll/1234')
but now I dont know the event name so is there any way I can do something like this:
driver.get('www.dummy.com/event/*/1234')
Thanks
As per my understanding this will not work, as the get method expects a string as the parameter.
From docs,
void get(String url);
/**
* Get a string representing the current URL that the browser is looking at.
*
* #return The URL of the page currently loaded in the browser
*/
However, have you tried hitting the url with some dummy tests for this?

CSRF in grails, withForm{}.invalidToken{} not working fine

As I've to use csrf in my grails application, I'm doing it by encapsulating my action logic under withForm{...}.invalidToken{....}, also I'm adding an attribute as: g:formRemote useToken="true" under g:formRemote tag in gsp.
The problem is, I'm always getting inside the invalidToken{...} block on submit and hence my form is not getting saved.
How should I make it working properly?
Example:
def action = {
withForm{
......
}.invalidToken{
println "Invalid Token code"
}
}
gsp ex:
<g:formRemote useToken="true" ...>
...
...
</g:formRemote>
useToken is not supported with formRemote. Please take a look on https://github.com/grails/grails-core/blob/2.5.x/grails-plugin-gsp/src/main/groovy/org/codehaus/groovy/grails/plugins/web/taglib/JavascriptTagLib.groovy#L349
The supported attributes for formRemote are:
* #attr name REQUIRED The form name
* #attr url REQUIRED The url to submit to as either a map (containing values for the controller, action, id, and params) or a URL string
* #attr action The action to execute as a fallback, defaults to the url if non specified
* #attr update Either a map containing the elements to update for 'success' or 'failure' states, or a string with the element to update in which cause failure events would be ignored
* #attr before The javascript function to call before the remote function call
* #attr after The javascript function to call after the remote function call
* #attr asynchronous Whether to do the call asynchronously or not (defaults to true)
* #attr method The method to use the execute the call (defaults to "post")
With that said. You can keep using the withForm in your controller and implement the form submission using jQuery or other JS library and send the token. Please take a look at this question: Grails - Is there a recommended way of dealing with CSRF attacks in AJAX forms?
Also I would recommend you to move on from tags like formRemote. I'm assuming you're using at least Grails 2.x. In case you want to migrate to Grails 3.x in the future, the Ajax-related tags are deprecated.
Hope this helps.

Hosting project in IIS appends project name to the URL and breaks my routing

I have a web api 2 project, and in my code I do some routing stuff myself.
I have all my actions going to a single route, so hitting localhost/<anything> will always go to one route.
In that route I am doing some custom pattern matching.
If the user goes to /Kittens/AdoptAKitten/12345
It will match against a template I have using regex, defined as /Kittens/AdoptAKitten/{something}
The problem is when I host my project locally, it ends up at localhost/KITTENCORP.ADOPTION/ which is the name of my project. As a result the route matching doesn't work.
I am not sure how to take into account this 'root' address. Previously I was just looking at the domain part of the Uri object but I need to include this part in the comparison to make it work (or disregard/remove it).
This code will however also be deployed to a server somewhere at which point it will probably be hosted on adoptionservice.kittens.org and thus adoptionservice.kittens.org/Kittens/AdoptAKitten/12345 will be the url. So it has to account for both situations.
Any ideas how I can resolve this problem?
For anyone stumbling across this and wondering the same thing, I fixed it with this code:
// When hosted in IIS it may get a virtual path such as localhost/KittenLibrary that needs including in comparisons
if (!string.IsNullOrEmpty(HttpRuntime.AppDomainAppVirtualPath))
{
urlStart = UrlCombine(urlStart, HttpRuntime.AppDomainAppVirtualPath);
}
UrlCombine is a function I pinched from another SO question:
private static string UrlCombine(string url1, string url2)
{
if (url1.Length == 0)
{
return url2;
}
if (url2.Length == 0)
{
return url1;
}
url1 = url1.TrimEnd('/', '\\');
url2 = url2.TrimStart('/', '\\');
return string.Format("{0}/{1}", url1, url2);
}

Way to access ModX database by including ModX core file?

I have a PHP file (that I cant make a snippet out of). I need to connect it to the database and rather then doing it the normal way was wondering if I could just include a header file of some sort that already has a DB connection.
Does anyone know of such a file in the Modx file structure?
Easy...
if you open the main index.php, you can see some hint:
/* define this as true in another entry file, then include this file to simply access the API
* without executing the MODX request handler */
if (!defined('MODX_API_MODE')) {
define('MODX_API_MODE', false);
}
// ...
/* execute the request handler */
if (!MODX_API_MODE) {
$modx->handleRequest();
}
That said, if you have a raw PHP file, let's say as an example: hello.php
<?php
define('MODX_API_MODE', true); // IMPORTANT!!!
require 'index.php'; // or give directory path, according to your need
// let's test it
$startPage = $modx->getObject('modResource', $modx->getOption('site_start', null, 1));
if (!$startPage) {
die('CRAP!');
}
$startPageArray = $startPage->toArray();
echo '<pre>';
print_r($startPageArray);
echo '</pre>';
die('WOOHOO');
And no, you don't have to define $modx again.
It's using the same object in index.php.
you can run any operations against your modx database if you load the modx module in your external script [in fact you can use any modx functions]
https://rtfm.modx.com/revolution/2.x/developing-in-modx/other-development-resources/loading-modx-externally
once you instantiate the modx object it will handle all your database connection details. This will work in any page, not just manager pages.
The only place which allows database connections using system database config are manager pages, yet that would require more work to write a plugin to access its classes and functions.
If you want to be able to use MODX functionality to establish connection, I suggest using it's xPDO to perform queries, for security reasons at least.
Such setup would be:
define('MODX_CORE_PATH', '/path/to/revo/core/');
define('MODX_CONFIG_KEY','config');
require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
$host = 'localhost';
$username = 'your_username';
$password = 'your_password';
$dbname = 'your_database';
$port = 3306;
$charset = 'utf-8';
$dsn = "mysql:host=$host;dbname=$dbname;port=$port;charset=$charset";
$xpdo = new xPDO($dsn, $username, $password);

Dart redstone web application

Let's say I configure redstone as follows
#app.Route("/raw/user/:id", methods: const [app.GET])
getRawUser(int id) => json_about_user_id;
When I run the server and go to /raw/user/10 I get raw json data in a form of a string.
Now I would like to be able to go to, say, /user/10 and get a nice representation of this json I get from /raw/user/10.
Solutions that come to my mind are as follows:
First
create web/user/user.html and web/user/user.dart, configure the latter to run when index.html is accessed
in user.dart monitor query parameters (user.dart?id=10), make appropriate requests and present everything in user.html, i.e.
var uri = Uri.parse( window.location.href );
String id = uri.queryParameters['id'];
new HttpRequest().getString(new Uri.http(SERVER,'/raw/user/${id}').toString() ).then( presentation )
A downside of this solution is that I do not achieve /user/10-like urls at all.
Another way is to additionally configure redstone as follows:
#app.Route("/user/:id", methods: const [app.GET])
getUser(int id) => app.redirect('/person/index.html?id=${id}');
in this case at least urls like "/user/10" are allowed, but this simply does not work.
How would I do that correctly? Example of a web app on redstone's git is, to my mind, cryptic and involved.
I am not sure whether this have to be explained with connection to redstone or dart only, but I cannot find anything related.
I guess you are trying to generate html files in the server with a template engine. Redstone was designed to mainly build services, so it doesn't have a built-in template engine, but you can use any engine available on pub, such as mustache. Although, if you use Polymer, AngularDart or other frameowrk which implements a client-side template system, you don't need to generate html files in the server.
Moreover, if you want to reuse other services, you can just call them directly, for example:
#app.Route("/raw/user/:id")
getRawUser(int id) => json_about_user_id;
#app.Route("/user/:id")
getUser(int id) {
var json = getRawUser();
...
}
Redstone v0.6 (still in alpha) also includes a new foward() function, which you can use to dispatch a request internally, although, the response is received as a shelf.Response object, so you have to read it:
#app.Route("/user/:id")
getUser(int id) async {
var resp = await chain.forward("/raw/user/$id");
var json = await resp.readAsString();
...
}
Edit:
To serve static files, like html files and dart scripts which are executed in the browser, you can use the shelf_static middleware. See here for a complete Redstone + Polymer example (shelf_static is configured in the bin/server.dart file).

Resources