Redirect doesn't work in cake 3 when I use copy command before - redirect

I tried this in cake 3:
$path = '/some/path/on/myserver';
$dir = new Folder($path, true, 0775);
copy('https://www.domain.com/image',$path.$imgName);
return $this->redirect('redirectPath');
$this->redirect doesn't work. But if I command out the copy command redirect works fine. What is the problem or did I miss something?

If any error or warning happend before $this->redirect() it will not work, it will show
Header already sent error
if you want to force to redirect then you may use
$this->response = $this->redirect(['action' => 'index']) ;
$this->response->send () ;
die ();

Try Like this,
$this->redirect($this->referer(['action' => 'index']));

Thank you for all answers and please don't mind. But I think there is no easy solution!
Cause after sending this copy(..) statement the http request will be finished. And the browser will not get any response and will wait till Skt. Nevermind... ;-)
The only solution is to call another view and from there start an ajax request.
In this ajax request you may start your copy statement. Or if you have gearman running start a worker to do this.
Thanks anyway.
Michael

Related

Email Queuing with OctoberCMS

Can anyone help with some tips on how to queue email using an OctoberCMS ajax page?
function sendRecipientMsg($dataset, $sendCounter, $recipients){
$template = $dataset['template'];
Mail::queue($template, $dataset, function($message) use($dataset, $recipients){
$message->to('piggy#teamprema.co.nz','MissPiggy');
$message->subject('Have a good day');
$message->from('us#prema.co.nz', 'Mike and Stephie');
$message->sender('us#prema.co.nz', 'Mike and Stephie');
trace_log('$message');
$message->cc($address, $name = null);
$message->bcc('systems#safe.org.nz', 'SAFE Campaigns Feedlots ECards');
});
}
This code works when we use Mail::send but not with Mail::queue
Any help or tips very welcome
In your config/queue.php file, which driver do you have set as the default?
For example: 'default' => env('QUEUE_DRIVER', 'sync')
(if you are using DotEnv then check the .env file in your docroot).
If you are using sync, it should send right away as sync is really only for development and will still block.
If you are using another method, like database, then you do have to ensure that your queues are configured to process how you expect.
Try running php artisan queue:work, then trigger your ajax call and it should send.

How to login with user but still stay admin?

I want to implement feature when operator/admin may login as user. Do something under user's credentials and then return back and continue as operator/admin
I try to mount whole application under /as_user/:user_id route. So when request come I adjust session to :user_id.
I try detour
$app->routes->route( '/as_user/:app_user' )->detour( app => $app );
But in this case when GET /as_user/17/packages request come the application fall into infinite loop
Also I think to append ?U=17 query parameter. But I do not know how and where rewrite code in such way: All link should be rendered with ?U=17 appended.
Please advice how to login with another user but still stay admin.
Seems I found the answer:
$r->under( '/as_user/:user_id', sub{
# FIX THE SESSION HERE. Just like:
# $_[0]->session->{ user_id } = _[0]->match->stack->[-1]->{ user_id };
return 1; # Required to not break the dispatch chain
})->route('/')->detour( 'App' );
Instead of application instance you should pass application class and Mojolicious will instantiate it itself.
PS. Infinite loop maybe because of cyclic refs. (But Mojolicious check refs here)
UPD
Infinite loop because of bug

Nginx module redirect

Good day
I am writing a module for Nginx, that should redirect user to certain (local) URLs, if several conditions apply - something like ngx_http_rewrite_module (Though I couldn't find redirection code in that module).
My code successfully runs on required events, but I am unable to redirect user to another page.
I've tried ngx_http_internal_redirect, but it didn't work:
static ngx_str_t ngx_redirect_script = ngx_string("/dst.php");
return ngx_http_internal_redirect(r, &ngx_redirect_script , &r->args);
Perhaps somebody knows how to do that?
Thank you!
Interesting, this worked:
ngx_http_internal_redirect(r, &ngx_redirect_script , &r->args);
return NGX_HTTP_OK;

PHPUnit and Zend Framework assertRedirectTo() issue

I'm having an issue with assertRedirectTo() in a test I have created, below is the code I have used:
public function testLoggedInIndexAction() {
$this->dispatch('/');
$this->assertController('index');
$this->resetResponse();
$this->request->setPost(array(
'type' => 'login',
'username' => 'root',
'password' => 'asdasd',
));
$this->request->setMethod('POST');
$this->dispatch('/');
$this->assertRedirectTo('/feed/');
}
You log in through / (index.php/) and submit post details there and the it redirects you to /feed/ (index.php/feed/). The details I have supplied are correct and should work however I am having issues whereby PHPUnit is saying they are incorrect:
There was 1 failure:
1) IndexControllerTest::testLoggedInIndexAction
Failed asserting response redirects to "/feed/"
/home/public_html/mashhr/library/Zend/Test/PHPUnit/Constraint/Redirect.php:190
/home/public_html/mashhr/library/Zend/Test/PHPUnit/ControllerTestCase.php:701
/home/public_html/mashhr/tests/application/controllers/UserControllerTest.php:36
#poelinca: No, it is simply a case of Zend_Test being unreliable in registering a redirect (even if it has been called correctly!)
In his case, the real app is no doubt redirecting the user properly, but the Zend_Test environment is having trouble registering properly called redirects. The best response I can think of is to omit any failing assertRedirect which actually works in the application.
This is not an optimal situation, but unless you're prepared to dig into the Zend code to see where the problem is, this may be your best bet for efficiency. This is an example of what causes unit testing to get a bad name: Having to alter code to pass tests which actually work already.
See http://framework.zend.com/issues/browse/ZF-7496 Which is misleadingly specific in its title: the problem relates to all redirects, especially those which must set headers and exit instead of dispatching the original controller.
For whatever reason, this behavior causes Redirects not to always fail, but to be highly unreliable instead! If anyone knows a better workaround to this problem (which is general, and probably unrelated to the OP's code) please let us know.
stumbled on this question while having the same problem. I ended up doing the following:
$this->assertRedirect();
$responseHeaders = $this->response->getHeaders();
$this->assertTrue(count($responseHeaders) != 0);
$this->assertArrayHasKey('value', $responseHeaders[0]);
// in my case I'm redirecting to another module
$this->assertEquals('/module/controller/action', $responseHeaders[0]['value']);
I've responded this answer in http://zend-framework-community.634137.n4.nabble.com/Zend-Test-failing-on-AssertRedirectTo-td3325845.html#a4451217
I'm having this same issue... A possible way to assert it could be in
PHPUnit and Zend Framework assertRedirectTo() issue
But the problem is there.. My example is (wich actually works if done manually):
// controller modules/picking/orders/product
$orderId = $this->_getParam('oId');
if (empty($orderId)) {
return $this->_redirect('picking/orders/browse/invalid/empty');
}
// test
$this->dispatch('picking/orders/product');
$this->assertRedirect(); // ok
$this->assertRedirectTo('picking/orders/browse'); // error
$this->assertRedirectTo('picking/orders/browse/invalid/empty'); // error
After I've found the error!
Actually, following the example above i've found that the string comparizon in my example has an error:
'.../public//picking/orders/browse/invalid/empty'
'.../public/picking/orders/browse/invalid/empty'
... fixing the preprended slash solve the problem! ;)
So if i understand right , you wrote a test that fails ( id say this is perfect ) .
In order to make the test pass you need to debug you're app and see where the problem is , in this case you need to have a look at the actual redirection ( or eaven the post fields sent by the form ) , maybe check the routing too . I gues nobody here will be able to answer you're question unless you post the code in you're index controller/form/view and feed controller .
For future reference, I had this issue today and it was caused by the Url class failing to build a valid url (I was passing the wrong parameters) but not reporting an error to PHPUnit (probably because PHPUnit masks the error).
Correcting the url parameters fixes the url and with it the assertion.

Powershell - Increase the timeout for retrieving XML from a URL

I'm trying to retrieve an XML stream from a URL. For most URLs my code below works fine. But, I have a couple URLs that timeout. The URLs in question do work from Internet Explorer.
$webclient=New-Object "System.Net.WebClient"
[xml]$data=$webclient.DownloadString($url)
So, I went searching for a way to increase the timeout period. From what I've read, I believe I cannot do this using System.Net.WebClient. I think I need to use System.Net.WebRequest instead, but I cannot get it to work. The code I've been working on is below:
$myHttpWebRequest = [system.net.WebRequest]::Create($url)
$myHttpWebRequest.Timeout = 600000
$myHttpWebResponse = $myHttpWebRequest.GetResponse()
$sr = New-Object System.IO.StreamReader($response.GetResponseStream())
[xml]$xml = [xml]$sr.ReadToEnd()
The URLs I'm trying to access are internal to my company, so I can't post them. But, they do work in IE and the actual URL should be irrelevant.
Ideas?
EDIT: Preliminary testing shows that adding $myHttpWebRequest.AuthenticationLevel = "None" works. Thanks Scott Saad.
By default the WebRequest.AuthenticationLevel is set to MutualAuthRequested, therefore it will wait for some type of authentication response. Therefore, a timeout is probably being exceeded while waiting for the authentication to occur. It didn't look like you were messing with the Credentials so unless you require authentication, you probably won't need this. Try something like the following after you create your WebRequest:
$myHttpWebRequest.AuthenticationLevel = "None"
I hope this helps solve the problem.