TYPO3 6.1 - how can I login a fe_user via code? - typo3

I am using TYPO3 6.1 and try to login a FE user via Code (included via PHP and USER_INT).
This code does not work for me. :( Any ideas why?
// try to login the user now
$loginData = array(
'uname' => trim($postData['username']), //usernmae
'uident' => trim($postData['password']), //password
'status' => 'login'
);
// $GLOBALS['TSFE']->fe_user-checkPid=0; //do not use a particular pid
$GLOBALS['TSFE']->fe_user->checkPid = $this->feUserPid; // ''; //do not use a particular pid
$info = $GLOBALS['TSFE']->fe_user->getAuthInfoArray();
//
$user = $GLOBALS['TSFE']->fe_user->fetchUserRecord(
$info['db_user'], $loginData['uname']
);
$ok = $GLOBALS['TSFE']->fe_user->compareUident($user, $loginData);
//
if($ok)
{
// login successfull
$GLOBALS['TSFE']->fe_user->createUserSession($user);
//
$content = '<h3 class="green">LOGIN OK!</h3>';
$content .= '<p>Sie sind eingeloggt als ' . $feUsername . ' und wurden nun weitergeleitet...</p>';
}
else
{
// login failed
$content = '<h3 class="red">LOGIN FAILED!</h3>';
}
Thanks for any hint!

Starting with TYPO3 4.7, you must use $loginData['uident_text'] instead of $loginData['uident'] for the password.
If the problem persists, please give more information what exactly is going wrong, what your log says etc.

Does compareUident() return TRUE for you?
I, fx have a case, where I automatically log in user that has just created themselves. And compareUident() didn't work for me for some reason, but as I know this same website visitor just created the user, I skip that function and continue on initiating all the session variables. The following is a working code on TYPO3 6.1:
$loginData = array(
'uname' => $postdata['username'],
'uident' => $postdata['password'],
'status' => 'login',
);
$GLOBALS['TSFE']->fe_user->checkPid = 0;
$info = $GLOBALS['TSFE']->fe_user->getAuthInfoArray();
$user = $GLOBALS['TSFE']->fe_user->fetchUserRecord($info['db_user'], $loginData['uname']);
//if($GLOBALS['TSFE']->fe_user->compareUident($user, $loginData)) {
$GLOBALS['TSFE']->fe_user->user = $GLOBALS['TSFE']->fe_user->fetchUserSession();
$GLOBALS['TSFE']->loginUser = 1;
$GLOBALS['TSFE']->fe_user->fetchGroupData();
$GLOBALS['TSFE']->fe_user->start();
$GLOBALS['TSFE']->fe_user->createUserSession($user);
$GLOBALS['TSFE']->fe_user->loginSessionStarted = TRUE;
//}
Inspired by:
https://stackoverflow.com/a/7738383/445364
https://typo3.org/extensions/repository/view/dix_easylogin

Related

How to upload a PDF file and send via email in TYPO3?

I'm Having a form with the following fields.
Name
Address
Contact
PDF (File Attachment)
This PDF is to be uploaded from the system and send these details together with the uploaded PDF via email. I want these details to be in the custom email template using standalone view and send.
I'm using TYPO3 v7.6.23
I'm new to TYPO3. How to do This?
I'm adding my code here
$addJobsInfo = GeneralUtility::makeInstance('MNU\\MnuJobs\\Domain\\Model\\Jobs');
$addJobsInfo->setName($arguments['name']);
$addJobsInfo->setAddress($arguments['address']);
$addJobsInfo->setContact($arguments['contact']);
$this->jobsRepository->add($addJobsInfo);
$persistenceManager = $this->objectManager->get('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager');
$persistenceManager->persistAll();
$lastID = $addJobsInfo->getUid();
$getPDF = $_FILES;
if(!empty($getPDF)){
$pdfName = $getPDF['tx_mnujobs_mnujobs']['name']['jobsDOC'];
$pdfTemp = $getPDF['tx_mnujobs_mnujobs']['tmp_name']['jobsDOC'];
$resourceFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
$storage = $resourceFactory->getDefaultStorage();
$folder = $storage->getFolder('user_upload');
$pdfFinalName = 'Jobs_'.time().'_'.$pdfName;
$uploadPDF = $storage->addFile($pdfTemp, $folder, $pdfFinalName);
if($lastID){
$newId = 'NEW'.$lastID;
$data = array();
$data['sys_file_reference'][$newId] = array(
'table_local' => 'sys_file',
'uid_local' => $uploadPDF->getUid(),
'tablenames' => 'tx_mnujobs_domain_model_jobs',
'uid_foreign' => $lastID,
'fieldname' => 'pdfattachment',
'pid' => $this->settings['pageUid']
);
$dataHandler = GeneralUtility::makeInstance(DataHandler::class);
$dataHandler->start($data, array());
$dataHandler->process_datamap();
}
}
//E-mail Sending
$mail = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');
$mail->setSubject('Contact Details');
$mail->setTo('sample#gmail.com']);
$mail->setBody('Hi, This is a sample. Please Find the Attachment', 'text/html');
$attachment = Swift_Attachment::fromPath($folder->getIdentifier().$pdfFinalName);
$attachment->setFilename('Document.pdf');
$mail->attach($attachment);
$sendMail = $mail->send();
I'm getting this Exception
Unable to open file for reading
[/user_upload/Jobs_1525757243_sample.pdf]
So I've replaced this
$attachment = Swift_Attachment::newInstance($folder->getIdentifier().$pdfFinalName, 'Document.pdf', 'application/pdf');
$mail->attach($attachment);
Both are not working, Where I've gone wrong?
What worked for me was adding fileadmin in front of the identifier:
$filePath = 'fileadmin' . $file->getOriginalResource()->getIdentifier();
$attachment = \Swift_Attachment::fromPath($filePath);
$mail->attach($attachment);
Use ext:powermail, it is feature rich, actively maintained and very well documented

Facebook OAuth Issue with CakePHP 2.9

Hi Guys first of all let me tell you guys that I am a newbie with CakePHP and MVC also but one of my old client requested me some favor which I took as a challenge. So If anyone can help me I will appreciate a lot.
I have a CakePHP 2.9 application where OAuth is not working properly following is the code for app/Controller/UsersController.php
public function facebookLogin(){
$this->layout = false;
App::import('Vendor', 'Facebook/facebook');
/*$facebook = new Facebook(array(
'appId' => '1867587306812489', // Facebook App ID
'secret' => '9fb41a07d3da3406dbab5861c3498764', // Facebook App Secret
'cookie' => false,
)
); */
$facebook = new Facebook(array(
'appId' => '1887387108164606', // Facebook App ID
'secret' => '8d733aa6a4501ce27fde9626429baab9', // Facebook App Secret
'cookie' => false,
)
);
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me?fields=first_name,last_name,email');
$usersArr = $this->User->find('first',array('conditions'=>array('User.email'=>$user_profile['email'],'User.social_id'=>$user_profile['id'],'User.role_id'=>0)));
$user_count = $this->User->find('first',array('conditions'=>array(),'order'=>'User.id DESC','limit'=>1));
if(empty($usersArr)){
$saveUserArr['User']['username'] = trim($user_profile['first_name']).' '.trim($user_profile['last_name']);
$saveUserArr['User']['firstname'] = $user_profile['first_name'];
$saveUserArr['User']['lastname'] = $user_profile['last_name'];
/* $user_n = $saveUserArr['User']['first_name'].'.'.$saveUserArr['User']['last_name'];
$user_cnt = $user_count['User']['id'] + 1;
$user_n = str_replace(' ','_',$user_n).'.'.$user_cnt;
$saveUserArr['User']['user_unique_id'] = $user_n; */
$saveUserArr['User']['email'] = $user_profile['email'];
$saveUserArr['User']['password'] = '123456';
$saveUserArr['User']['social_id'] = $user_profile['id'];
$saveUserArr['User']['login_from'] = 'facebook';
$saveUserArr['User']['role_id'] = 0;
$this->User->save($saveUserArr['User'],false);
}else{
$this->User->id = $usersArr['User']['id'];
$this->User->saveField('social_id', $user_profile['id']);
$this->User->saveField('login_from', 'facebook');
}
$this->request->data['User']['email'] = $user_profile['email'];
$this->request->data['User']['login_from'] = 'facebook';
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email', // Permissions to request from the user
));
return $this->redirect($loginUrl);
}
}
private function deleteFacebookSession(){
Configure::write('debug', 0);
if(!$this->Session->check('Auth.User')){
foreach( $this->Session->read() as $key => $value ) {
if( strpos( $key, 'fb_' ) === 0 ) {
$this->Session->delete($key);
}
}
}
}
It throws following error:
The oauth script url is not properly configured in your fb app.
You may go to your fb app settings and look for APP DOMAIN.
Include your domain there:
If you are developing in your localhost, you may want to create a server.local rec in your hosts file so you can access your localserver as server.local.
Also, you can create test versions of the main fb app, and allows you to have a copy of your fb app that works in other environments, it is called "create test app"

Facebook Php API: RSVP users to public event

I'm trying to use the facebook php api to rsvp users to a public event.
So far I can only get it to work for users who have already been invited.
I've tried:
$path = $event_id.'/attending';
$method = 'POST';
try
{
$this->facebook->api($path, $method);
}
catch(FacebookApiException $e){}
Which does nothing.
My code as it stands is:
function rsvpEvent($event_id)
{
$fb_config = array(
'appId' => APP_ID,
'secret' => APP_SECRET,
'cookie' => true,
);
$this->load->library('facebook', $fb_config);
$me = $this->facebook->api('/me/');
$user_id = $me['id'];
$path = $event_id.'/invited/'.$user_id;
$status = $this->facebook->api($path, 'GET');
$status = $status['data'][0]['rsvp_status'];
if($status === 'not_replied')
{
$path = $event_id.'/attending';
$method = 'POST';
try
{
$this->facebook->api($path, $method);
}
catch(FacebookApiException $e){}
}
}
Has anyone got any ideas how I can get this to work?
$path = $event_id.'/attending?access_token='.$SOME_ACCESS_TOKEN;
You're not using the access token in your code, so I am uncertain of whether you skipped it when you copied it, If it's not in the code, then it may be that.
I retrieved the information here

Facebook connect works fine but troubles inside Facebook iframe

I actually tried to find a solution but nothing and I hope I will find a solution here !
I use facebook connect in my website, everything works fine
when I am logged on facebook, I am automatically logged into my site through facebook connect, and vice versa.
Now i tried to make it a facebook application but I have the following problem:
I try to run inside the facebook iframe but unfortunately I'm not automatically logged in. sometimes it works, sometimes nothing happens , Sometimes I get this message: (Could not get the Facebook session. Your server may not Be Able Securely Facebook to connect to the user to retrieve information.)
some code im using :
include 'includes/facebook.php';
$facebook = new Facebook(array(
'appId' => $setting['facebook_appid'],
'secret' => $setting['facebook_secret'],
'cookie' => true, // enable optional cookie support
));
if ($facebook->getUser()) {
$facebook_session = 1;
try {
$fb_user = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
function get_facebook_cookie($app_id, $application_secret) {
$args = array();
parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
ksort($args);
$payload = '';
foreach ($args as $key => $value) {
if ($key != 'sig') {
$payload .= $key . '=' . $value;
}
}
if (md5($payload . $application_secret) != $args['sig']) {
return null;
}
return $args;
}
$cookie = get_facebook_cookie($setting['facebook_appid'], $setting['facebook_secret']);
$open = #file_get_contents('https://graph.facebook.com/me?access_token='.$cookie['access_token']);
if ($open != FALSE) {
$fbdata = json_decode($open);
$fb_user = array();
foreach($fbdata as $key => $fbdata2) {
$fb_user[$key] = $fbdata2;
}
}
}
else {
$facebook_session = 0;
}
ps: im using of course a Secure Canvas URL
Please help !
Thanks in advance
My Facebook Connect login worked fine, but not inside the Facebook Canvas/iFrame it didn't work at all. I had to change my login method (now that facebook users have to be able to autologin in your application) and what basically worked for me was this: How to authorize Facebook app using redirect in canvas?
This is what I had before (I use CakePHP):
$params = array(
'scope' => 'email'
);
$loginUrl = $facebook->getLoginUrl($params);
$this->redirect($loginUrl);
And I changed it to this which worked quite well:
$loginUrl = $facebook->getLoginUrl(
array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => 'publish_stream'
)
);
echo '<script>top.location="'.$loginUrl.'";</script>';
I'm not sure if that really helps you as your app seems a bit more complicated than mine, but maybe you can solve your issue with this answer or the post I linked to.

Setting a fe_users session from extbase

As I have to verify the login-data in a way, normal login extensions can not handle, I have to develop a custom login-form. This login box is part of an ext. written in extbase.
But I stuck with a basic question: How can i handle fe_users sessions in extbase?
I wrote this function, maybe it helps:
/** #var $fe_user tslib_feUserAuth */
$fe_user = $GLOBALS['TSFE']->fe_user;
$fe_user->createUserSession(array('uid' => $uid));
$fe_user->user = $fe_user->getRawUserByUid($uid);
$fe_user->fetchGroupData();
$GLOBALS['TSFE']->loginUser = 1;
It's inspired by Tx_Phpunit_Framework::loginFrontEndUser($userId) :
// Instead of passing the actual user data to createUserSession, we
// pass an empty array to improve performance (e.g. no session record
// will be written to the database).
$GLOBALS['TSFE']->fe_user->createUserSession(array());
$GLOBALS['TSFE']->fe_user->user = $GLOBALS['TSFE']->fe_user->getRawUserByUid($userId);
$GLOBALS['TSFE']->fe_user->fetchGroupData();
$GLOBALS['TSFE']->loginUser = 1;
None of the answers provided seemed to work for me in TYPO3 6.2. The fix is described in a Bugreport in the Forge, https://forge.typo3.org/issues/62194 . Basically, the SessionCookie isn't placed automatically anymore, you need to do that on your own now, adding
$reflection = new \ReflectionClass($GLOBALS['TSFE']->fe_user);
$setSessionCookieMethod = $reflection->getMethod('setSessionCookie');
$setSessionCookieMethod->setAccessible(TRUE);
$setSessionCookieMethod->invoke($GLOBALS['TSFE']->fe_user);
after calling the createUserSession().
I finally made it, by combining lots of snippets. Thats how it worked out for me:
$loginData = array(
'uname' => $loginData['user'],
'uident' => $loginData['passw'],
'status' => 'login'
);
$GLOBALS['TSFE']->fe_user->checkPid = 0;
$info = $GLOBALS['TSFE']->fe_user->getAuthInfoArray();
$user = $GLOBALS['TSFE']->fe_user->fetchUserRecord( $info['db_user'], $loginData['uname'] );
if ( $GLOBALS['TSFE']->fe_user->compareUident($user,$loginData) )
{
$GLOBALS["TSFE"]->fe_user->user = $GLOBALS["TSFE"]->fe_user->fetchUserSession();
$GLOBALS['TSFE']->loginUser = 1;
$GLOBALS['TSFE']->fe_user->fetchGroupData();
$GLOBALS['TSFE']->fe_user->start();
$GLOBALS["TSFE"]->fe_user->createUserSession($user);
$GLOBALS["TSFE"]->fe_user->loginSessionStarted = TRUE;
}