Can't submit Typo3 forms when not logged in - php

I have made a simple plugin with a form but it won't post when I'm not logged in.
Here is the file class.tx_gctest_pi1.php, created with Kickstarter.
require_once(PATH_tslib.'class.tslib_pibase.php');
class tx_gctest_pi1 extends tslib_pibase {
var $prefixId = 'tx_gctest_pi1'; // Same as class name
var $scriptRelPath = 'pi1/class.tx_gctest_pi1.php'; // Path to this script relative to the extension dir.
var $extKey = 'gc_test'; // The extension key.
var $pi_checkCHash = true;
function main($content, $conf) {
$this->conf = $conf;
$this->pi_setPiVarDefaults();
$this->pi_loadLL();
if($_POST) {
echo 'test';
}
$content='
<strong>This is a few paragraphs:</strong><br />
<p>This is line 1</p>
<p>This is line 2</p>
<h3>This is a form:</h3>
<form action="'.$this->pi_getPageLink($GLOBALS['TSFE']->id).'" method="POST">
<input type="text" name="'.$this->prefixId.'[input_field]" value="'.htmlspecialchars($this->piVars['input_field']).'">
<input type="submit" name="'.$this->prefixId.'[submit_button]" value="'.htmlspecialchars($this->pi_getLL('submit_button_label')).'">
</form>
<br />
<p>You can click here to '.$this->pi_linkToPage('get to this page again',$GLOBALS['TSFE']->id).'</p>
';
return $this->pi_wrapInBaseClass($content);
}
}
if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/gc_test/pi1/class.tx_gctest_pi1.php']) {
include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/gc_test/pi1/class.tx_gctest_pi1.php']);
}
?>
This will output test when logged in and nothing when not logged in.
The page is reloaded but no post is sent

I think this has not much to do with logged in / logged out. TYPO3 caches content unless you tell it to not cache.
echo var_dump print_r debug are methods that directly display things through php. TYPO3 doesn't catch them. If you want to have something displayed, add it to e.g. $content and return $content at the end of main(). The return value of main() gets cached.
You can try this by clearing your cache in backend and refresh the page. echo etc is displayed. after a new reload, it is gone.
So, how to solve this? There are a few possibilities
make the page that contains your plugin uncached
make the plugin itself uncached
I would suggest you find out what you really want to do and then write your code that is is using caching.

Related

How do I use PHP to detect a button click and open an new page?

I was wondering if someone can help me. I am trying to write some PHP code so that when I click on a button in one page, upload.php, the button click is detected and I am redirected to another page, processing.php. I am following along another tutorial and I have triple checked, I don't see what I have done wrong, but the button click is not being detected as it is in the video.
This is the code for my upload.php file:
<?php include_once('includes/header.php');?>
<?php include_once('includes/classes/VideoDetailsFormProvider.php');?>
<div class="column">
<!-- //calling PHP function to create upload form -->
<?php
//create variable and assign value
$formProvier = new VideoDetailsFormProvider($con);
//call function
echo $formProvier->createUploadForm();
?>
</div>
<?php include_once('includes/footer.php');?>
This is the relevant code from my additional class VideoDetailsFormProvider.php:
class VideoDetailsFormProvider{
private $con;
//create constructor and pass $con variable to it
public function __construct($con){
$this->con = $con;
}
//creating a function to create the upload form
public function createUploadForm(){
$fileInput = $this->createFileInput();
$titleInput = $this->createTitleInput();
$descriptionInput = $this->createDescriptionInput();
$privacyInput = $this->createPrivacyInput();
$categoryInput = $this->createCategoryInput();
$uploadButton = $this->createUploadButton();
return "
<form action='processing.php' method='POST'>
$fileInput
$titleInput
$descriptionInput
$privacyInput
$categoryInput
$uploadButton
</form>
";
}
private function createUploadButton(){
$html = "<button name='uploadButton' class='btn btn-primary'>Upload Video</button>";
return $html;
}
And this is what I have in my processing.php file:
<?php include_once('includes/header.php');
//check for submission of the form or button is pressed
if(!isset($_POST['uploadButton'])){
echo "No form data has been set";
}else{
}
?>
When I click the button object, nothing happens. In the video I am transferred to processing.php and the echo message is not displayed. Or at least I should be, but that doesn't happen. I did try to check here to see if I could find some answers, a few things I tried didn't work out. Does anyone have any ideas about something I might be missing? Thanks in advance
The problem is with your upload button, when you use an html form it has to be submitted to the php processing page, try:-
private function createUploadButton(){
$html = "<button type='submit' name='uploadButton' class='btn btn-primary'>Upload Video</button>";
return $html;
}

Why isnt my PHP function firing from another file?

I have this input element in index.php
<input type="submit" name="OUs" value="Get all OU's">
This is where I fire the function in a PHP codeblock in index.php:
require_once('./OU.php');
$ou = new \Google\OU\OU();
if(isset($_POST['OUs'])) {
echo $ou->getOUs($response['access_token']);
}
I try to run a function in ou.php
namespace Google\OU;
define("APIURL_DIRECTORY","https://www.googleapis.com/admin/directory/v1/customer/"); // For Google
Directory actions
class OU
{
// Get OU's
function getOUs($token){
$url = "https://www.googleapis.com/admin/directory/v1/customer/my_customer/orgunits?orgUnitPath=/&type=all";
$method = "GET";
echo exeCurl($url,$method);
}
What is the reason that nothing happens when I click on that submit button? It should execute the Curl Call, shouldnt it?
I cant really figure it out. Thanks in advance.

sugarCRM: How to redirect to external page after logout?

I am trying to redirect to a page on my external page, what I have tried till now is. Manually I added some files.
In /custom/modules/Users/logic_hooks.php
<?php
$hook_version = 1;
$hook_array = Array();
$hook_array['after_logout'] = Array();
$hook_array['after_logout'][] = Array(
//Processing index. For sorting the array.
1,
//Label. A string value to identify the hook.
'after_logout example',
//The PHP file where your class is located.
'custom/modules/Users/logic_hooks_class.php',
//The class the method is in.
'logic_hooks_class',
//The method to call.
'after_logout_method'
);
?>
And another file in In /custom/modules/Users/logic_hooks_class.php
<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class logic_hooks_class
{
function after_logout_method($bean, $event, $arguments)
{
header('Location: http://raviranjan.info/');
}
function AfterLogout(&$bean, $event, $arguments)
{
SugarApplication::redirect('http://raviranjan.info/');
}
}
?>
So is there any other way to redirecting or just show something on screen before or after logging out from SugarCRM application.
Advance Thanks for any help.
There are multiple ways of doing this. Read following details:
show alert of any other type of message by handling click event of logout link. e.g. when user click that link then show alert of any other type of message.
after logout redirect user to your custom page( you can build custom entry point which will be accessible with auth=>false).
you can add any button or java-script to show message then redirect it to your target page.
Simple jquery selector, see following:
$("a.utilsLink").click(function(){
var r = confirm("Are you sure to logout?");
if (r == true) {
console.log("yes is clicked");
} else {
console.log("cancel is clicked...");
return false;
}
});

Print output result on same page

<?php
class myDemo
{
function __construct()
{
return strip_tags(file_get_contents("http://example.com/bitcoin/checker.php?method=gLink&secret=xxxxxx&user_id=".$_SESSION['user_id']));
}
}
$o = new myDemo;
$data = $o->__construct();
$data = json_decode($data);
echo $data->random; ?>
this script show to user a bitcoin address for submit payment . i want the code that make a button and when user click on the button he get bitcoin address on same page below the button . i mean user get the output of this script on same page after click on the button .
You may implement an AJAX request to do that.
HTML
<input type="submit" id="getBitCoinAddr" value="Get Address">
<div id="BitCoinAddr"></div>
jQuery
$("getBitCoinAddr").click(function(){
$("#BitCoinAddr").load("BitCoinAddr.php?getResult=1");
});
PHP
if($_GET['getResult==1'])
{
return $data->random;
}
However, it's better to separate side effects and declarations (PSR-1).

Content of class property gets cleared out before outputting to browser

I have this inside a class named site among other harmless stuff:
private
$notice_type = '',
$notice_msg = '';
public function setNotice($type,$msg){
$this->notice_type=$type;
$this->notice_msg=$msg;
}
public function notice($what){
switch($what){
case 'type': return $this->notice_type; break;
case 'msg': return $this->notice_msg; break;
}
}
public function clearNotice(){
$this->notice_type='';
$this->notice_msg='';
}
I've set this class to a session like this:
$_SESSION['site'] = new site();
Here's a scenario of how I use it:
After submitting a form; I set the notification like this : $_SESSION['site']->setNotice('success','success message');, or error if that's the case and redirect the user somewehere using header().
Then I output the message like this on the landing page:
echo $_SESSION['site']->notice('msg');
$_SESSION['site']->clearNotice();.
But; When I use the clearNotice()-function - the contents of both $notice_type and $notice_msg is cleared out before it gets outputted to the browser.
I need for it to stay untill the user navigates away from the page somehow. What am I missing here?
I don't know what happend. But somehow this script started to work as expected.
I've rewritten the code over and over for a while now, and as far as I know it's just about the same as before. But anyway; This is what works now:
site()-class:
This class controlls notifications as well as settings set by the user - like preffered sorting direction of data and selections worth remembering for better user experience and such.
<?php
class site {
private
$notice_type = '',
$notice_msg = '';
public function newNotice($type,$msg){
$this->notice_type=$type;
$this->notice_msg=$msg;
}
public function notice($what){
switch($what){
case 'type': return $this->notice_type; break;
case 'msg': return $this->notice_msg; break;
}
}
public function clearNotice(){
$this->notice_type='';
$this->notice_msg='';
}
}
?>
I have a document where I configurate the entire site by setting a couple of variables to Yes or No - As in this case : $_SITE_CLASS_site.
<?php
# check to see if session is started
if(!isset($_SESSION)){session_start();}
//
// check if site()-class should be activated for this site
if($_SITE_CLASS_site=='Yes'){
# if Yes; prevent resetting the class if it has already been started.
if(!isset($_SESSION['site'])){$_SESSION['site']=new site();}
//
}
//
?>
I've created a template where I have this code before the page content is outputted:
basically it just checks if there is any message to display
<?php if ($_SITE_CLASS_site=='Yes'&&$_SESSION['site']->notice('msg')!=''): ?>
<div id="site-notice-<?=$_SESSION['site']->notice('type')?>" class="grid_12"><p><?=$_SESSION['site']->notice('msg')?></p></div>
<?php endif; ?>
Then I load the page content, and at the end I have this:
the notice should be viewable untill the user closes it, or leaving the page. I don't want, or need, to keep the message
<?php
if ($_SITE_CLASS_site=='Yes'&&$_SESSION['site']->notice('msg')!=''):
$_SESSION['site']->clearNotice();
endif;
?>
Now; Whenever I need to give the user a feedback on their actions - after submitting a form successfully for example - I can just do this at the end of the script:
$_SESSION['site']->newNotice('success','<b>Success!</b> Your request was submitted successfully...');
header('Location '.$_SERVER['HTTP_REFERER']);
exit;
It works like a charm...

Categories