Facebook PHP SDK get always 0 - facebook-php-sdk

I want to work with Facebook on server side. Therefore I use Facebook SDK API.
I used tutorial to quick start and added curl options which fix often problems.
But getUser return 0 again. I made print_r($SESSION). And see there are fb_[FACEBOOK_APP_ID]_state var. Facebook redirects me back to my page with no errors, but getUser is 0.
define('FACEBOOK_APP_ID',"387123884707291");
define('FACEBOOK_SECRET',"REMOVED");
$user = null;
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true
));
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = 0;
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 0;
Facebook::$CURL_OPTS[CURLOPT_CONNECTTIMEOUT] = 30;
$user = $facebook->getUser();
print_r($_SESSION); //here line!
if($user == 0) {
$login_url = $facebook->getLoginUrl(array('scope' => 'email'));
$content = "<script> top.location.href='".$login_url."'</script>";
echo "bb";
} else
echo "logined";
die;

Okay. I have solved issue by enabling "Facebook application" tab (see screenshot).

Related

Why I can't send cookie in Mercure with Symfony?

I make an online auction site and I want the user who previously had the highest bid to be able to receive a real-time notification, when another user has submitted a higher bid than his. So I want to target my user using cookies.
And I tried this:
public function returnNotification(HubInterface $hub, string $message, string $userIdd){
$userId = Uuid::fromString($userIdd);
$userIdCookie = new Cookie('user_id', $userId, 0, '/', null, true, true, false, Cookie::SAMESITE_NONE);
$update = new Update(
"https://127.0.0.1:8000/instrumenthol",
json_encode(['status' => $message]),
true,
$userIdCookie
);
$hub->publish($update);
return new Response('published!');
}
And on the client side I get the notification with:
<script>const url= new URL("https://localhost/.well-known/mercure");
urlToAppend = 'https://127.0.0.1:8000/instrumenthol';
const eventSource = new EventSource(url,{
withCredentials: true,
});
eventSource.onmessage = (e) => {
console.log(e.data);
document.getElementById("message").insertAdjacentHTML('afterend', '<div class="alert alert-success">Ping</div>');
window.setTimeout(()=>{
const alert = document.querySelector('.alert');
alert.parentNode.removeChild(alert);
}, 50000);
}
</script>
My browser recovers the cookie but refuses it, firefox displays the error in the console:
GET https://localhost/.well-known/mercure
[HTTP/1.1 400 Bad Request 54ms]
Firefox can’t establish a connection to the server at https://localhost/.well-known/mercure.
firefox error
firefox error
I don't know how to solve the problem, can you help me solve the problem please?

Symfony3 add locale in deeplink

I create a new site in symfony3 following the getting started section in the official symfony documentation in https://symfony.com/doc/current/setup.html
Everything is working ok.. if I put mydomain.com as the URL, the framework add /en or the correct local.
My question is if there is a way that if the user do a deeplink to mydomain.com/blog the framework found that the local is not present so it can add and transform the url to mydomain.com/en/blog
I'm not adding the code as it is the default one. Let me know if you need it.
There are multiple ways to do this. Probably the easiest is to have an EventSubscriber or -Listener that catches request without a locale and then handles adding that information. Since you based your project on the demo application you might want to look at their solution: https://github.com/symfony/demo/blob/master/src/EventSubscriber/RedirectToPreferredLocaleSubscriber.php
The steps to perform in your event handler are roughly these:
Listen to kernel.request event
Return early based on some criteria, e.g. homepage, a cookie with the language is set, or something else
Detect the language either by getting the default locale or determining from your available locales and the browser header which language fits best (see: https://github.com/willdurand/Negotiation#language-negotiation)
Redirect, add the locale as attribute to request, write the currently set language to a cookie, or whatever else you need to do to change the route
Thanks to #dbrumann I get to this solution... For sure it can be improve to use less code but it just did the trick.
I updated the onKernelRequest method in RedirectToPreferredLocaleSubscriber class
public function onKernelRequest(GetResponseEvent $event): void
{
$request = $event->getRequest();
$path = explode('/',$request->getPathInfo());
$hasLocale = false;
foreach ($this->locales as $key => $l) {
if($l == $path[1]){
$hasLocale = true;
}
}
if(!$hasLocale){
// Ignore sub-requests and all URLs but the homepage
if (!$event->isMasterRequest() || '/' !== $request->getPathInfo()) {
$preferredLanguage = $request->getPreferredLanguage($this->locales);
if ($preferredLanguage !== $this->defaultLocale) {
$url = "";
foreach ($path as $key => $p) {
if($key > 0){
$url .= "/" . $p;
}
}
//print_r('/' . $preferredLanguage . $url);exit;
$response = new RedirectResponse('/' . $preferredLanguage . $url);
$event->setResponse($response);
}
}
else{
// Ignore requests from referrers with the same HTTP host in order to prevent
// changing language for users who possibly already selected it for this application.
if (0 === mb_stripos($request->headers->get('referer'), $request->getSchemeAndHttpHost())) {
return;
}
$preferredLanguage = $request->getPreferredLanguage($this->locales);
if ($preferredLanguage !== $this->defaultLocale) {
$response = new RedirectResponse($this->urlGenerator->generate('homepage', ['_locale' => $preferredLanguage]));
$event->setResponse($response);
}
}
}
}

if customer logs in wordpress he automatically gets logged in into magento also

if customer logs in wordpress he automatically gets logged in into magento also
although the customer email id and password is entry when sign up in magento as well as word press and work properly in case of sign up but when i want to login its not working in magento.
Here attached the image
I have already try
this code
require_once ( "shop/app/Mage.php" );
umask(0);
Mage::app("default");
$session = Mage::getSingleton("customer/session");
// Check if user is logged in
if($session->isLoggedIn())
{
// Yes user is logged in
echo "Yes user is logged in";
}else{
$session = Mage::getSingleton('customer/session', array('name' => 'frontend'));
$session->login("$email", "$password");
$session->setCustomerAsLoggedIn($session->getCustomer());
}
After you logged into wordpress you can force the user-logged-state with the mail-address.
Mage::getSingleton('core/session', array('name' => 'frontend'));
$_customerExist = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('email', $_mail)
->getFirstItem();
$_customer = Mage::getModel('customer/customer')
->load($_customerExist->getentity_id());
if(!Mage::getSingleton('customer/session')->isLoggedIn()) {
$session = Mage::getSingleton('customer/session', array('name' => 'frontend'));
$session->setCustomer($_customer);
$session->setCustomerAsLoggedIn($_customer);
}
If you want to login with the password you can find a solution here:
See here.
Whats going wrong with your version? Take a look into your logs why the login fails.

Adding a wordpress user to Salesforce Contact

I'm trying to do a relatively simple task by hooking into the Wordpress registration and adding the user that's being registered to a Salesforce db. When I run the Salesforce db code outside of Wordpress it works flawlessly, but when I test this by registering on my wordpress website, I get an error stating: INVALID_LOGIN: Invalid username, password, security token; or user locked out.
Additionally, I receive this error from Wordpress "Cannot modify header information - headers already sent" which doesn't allow me to view the entire object data that's being sent to Salesforce.
This is my code:
$SF_USERNAME = 'test';
$SF_PASSWORD = 'test';
define( 'CD_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
require_once (CD_PLUGIN_PATH . 'Toolkit/soapclient/SforceEnterpriseClient.php');
require_once (CD_PLUGIN_PATH . 'Toolkit/soapclient/SforceHeaderOptions.php');
function add_user_to_SF($user_id) {
$user = get_userdata($user_id);
try {
$mySforceConnection = new SforceEnterpriseClient();
$mySoapClient = $mySforceConnection->createConnection(CD_PLUGIN_PATH . 'Toolkit/soapclient/enterprise.wsdl.xml');
$mylogin = $mySforceConnection->login($SF_USERNAME, $SF_PASSWORD);
print '<pre>';
print_r($mylogin);
print '</pre>';
print '<br/><br/>';
$sObject = new stdclass();
$sObject->FirstName = $user->first_name;
$sObject->LastName = $user->last_name;
$sObject->Email = $user->user_email;
//echo "**** Creating the following:\r\n";
$createResponse = $mySforceConnection->create(array($sObject), 'Contact');
$ids = array();
foreach ($createResponse as $createResult) {
print_r($createResult);
array_push($ids, $createResult->id);
}
} catch (Exception $e) {
$errors->add( 'demo_error', __(print_r($_POST),'mydomain') );
$errors->add( 'demo_error', __($mySforceConnection->getLastRequest(),'mydomain') );
$errors->add( 'demo_error', __($e->faultstring,'mydomain') );
return $errors;
}
}
add_filter( 'registration_errors', 'add_user_to_SF', 10, 3 );
This is a php scope issue.
Adding:
global $SF_USERNAME;
global $SF_PASSWORD;
inside the function fixed the problem.
It looks like you might be missing the security token. It's appended to the end of the password.
This link explains how to generate the token
https://login.salesforce.com/help/doc/en/user_security_token.htm
You need to add your salesforce account security token with password
eg. Password - "testing"
Security Token - "2321njjn32j32"
You need to pass as - "testing2321njjn32j32"
This will work properly.

wordpress wp_signon function not working

I am using wp_signon() function to login the user. I am doing this like
$creds = array();
$creds['user_login'] = $username;
$creds['user_password'] = $password;
$creds['remember'] = true;
$user = wp_signon( $creds, false );
i want to send user to home page after login.
But i Am facing following error:
Warning: Cannot modify header information - headers already sent by (output started at E:\xampp\htdocs\wpmoodle\wp-content\themes\twentyten\header.php:12) in E:\xampp\htdocs\wpmoodle\wp-includes\pluggable.php on line 690.
Thanks in advance.
wp_signon() needs to run before you've sent any of your actual page to the browser.
This is because part of what wp_signon() does is to set your authentication cookies. It does this by outputting a "Set-Cookie: ..." header -- if you look at line 690 of pluggable.php, where your error comes from, you'll see that that line sets a cookie.
So, because wp_signon() outputs headers, you can't already have sent any content -- because headers must always be output before content.
However, the error indicates that you've already sent some output -- on line 12 of header.php, presumably some of the first HTML of the standard WordPress theme.
This basically indicates that you need to move your wp_signon() call to somewhere earlier in the WordPress processing, so it has a chance to output its headers before any page content is sent.
If someone needs it, here is my solution:
function custom_login() {
if (isset($_POST['submit'])) {
$login_data = array();
$login_data['user_login'] = sanitize_user($_POST['username']);
$login_data['user_password'] = esc_attr($_POST['password']);
$user = wp_signon( $login_data, false );
if ( is_wp_error($user) ) {
echo $user->get_error_message();
} else {
wp_clear_auth_cookie();
do_action('wp_login', $user->ID);
wp_set_current_user($user->ID);
wp_set_auth_cookie($user->ID, true);
$redirect_to = $_SERVER['REQUEST_URI'];
wp_safe_redirect($redirect_to);
exit;
}
}
}
add_action( 'after_setup_theme', 'custom_login' );

Resources