I'm trying to get an example from the dropbox sdk to work.
Here is some of the code that is in index.php in the root.
$req = $_SERVER['SCRIPT_NAME'];
if ($req === "/") {
//a bunch of code and elseif:s
}else{
echo renderHtmlPage("Bad URL", "No handler for $req");
exit;
}
I get the following output:
Bad URL
No handler for /index.php
How come req is set to index and how could it be set to / for example?
Full code
$dbxClient = getClient();
if ($dbxClient === false) {
header("Location: /dropbox-auth-start");
exit;
}
$path = "/";
if (isset($_GET['path'])) $path = $_GET['path'];
if (isset($_GET['dl'])) {
passFileToBrowser($dbxClient, $path);
}
else {
$entry = $dbxClient->getMetadataWithChildren($path);
if ($entry['is_dir']) {
echo renderFolder($entry);
}
else {
echo renderFile($entry);
}
}
}
else if ($req === "/dropbox-auth-start") {
$authorizeUrl = getWebAuth()->start();
header("Location: $authorizeUrl");
}
else if ($req === "/dropbox-auth-finish") {
try {
list($accessToken, $userId, $urlState) = getWebAuth()->finish($_GET);
assert($urlState === null);
unset($_SESSION['dropbox-auth-csrf-token']);
}
catch (dbx\WebAuthException_BadRequest $ex) {
error_log("/dropbox-auth-finish: bad request: " . $ex->getMessage());
// Respond with an HTTP 400 and display error page...
exit;
}
catch (dbx\WebAuthException_BadState $ex) {
// Auth session expired. Restart the auth process.
header('Location: /dropbox-auth-start');
exit;
}
catch (dbx\WebAuthException_Csrf $ex) {
error_log("/dropbox-auth-finish: CSRF mismatch: " . $ex->getMessage());
// Respond with HTTP 403 and display error page...
exit;
}
catch (dbx\WebAuthException_NotApproved $ex) {
echo renderHtmlPage("Not Authorized?", "Why not, bro?");
exit;
}
catch (dbx\WebAuthException_Provider $ex) {
error_log("/dropbox-auth-finish: unknown error: " . $ex->getMessage());
exit;
}
catch (dbx\Exception $ex) {
error_log("/dropbox-auth-finish: error communicating with Dropbox API: " . $ex->getMessage());
exit;
}
// NOTE: A real web app would store the access token in a database.
$_SESSION['access-token'] = $accessToken;
echo renderHtmlPage("Authorized!", "Auth complete, <a href='/'>click here</a> to browse");
}
else if ($req === "/unlink") {
// "Forget" the access token.
unset($_SESSION['access-token']);
header("Location: /");
}
else if ($req === "/upload") {
if (empty($_FILES['file']['name'])) {
echo renderHtmlPage("Error", "Please choose a file to upload");
exit;
}
if (!empty($_FILES['file']['error'])) {
echo renderHtmlPage("Error", "Error ".$_FILES['file']['error']." uploading file. See <a href='http://php.net/manual/en/features.file-upload.errors.php'>the docs</a> for details");
exit;
}
$dbxClient = getClient();
$remoteDir = "/";
if (isset($_POST['folder'])) $remoteDir = $_POST['folder'];
$remotePath = rtrim($remoteDir, "/")."/".$_FILES['file']['name'];
$fp = fopen($_FILES['file']['tmp_name'], "rb");
$result = $dbxClient->uploadFile($remotePath, dbx\WriteMode::add(), $fp);
fclose($fp);
$str = print_r($result, TRUE);
echo renderHtmlPage("Uploading File", "Result: <pre>$str</pre>");
}
Related
So i started learning facebook sdk and i wanted to get attening_count/list from my event page as an array, but i'm unable to get it working. It only displays me id, name etc.
if (isset($_SESSION['access_tok'])) {
echo 'Out!<br>';
try {
$response = $fb->get('me/accounts?fields=', $_SESSION['access_tok']);
$page = $response->getGraphEdge();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
foreach($page as $pages)
{
$name = $pages['name'];
$cc = $pages['access_token'];
if($name == 'Testing')
{
try {
$response1 = $fb->get('/eventid', $cc);
$page1 = $response1->getGraphNode()->asArray();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$page1 ['id'];
}
else
{
}
}
}
I'm having an issue catching the proper exception, no matter what i do i get the same response. This is the response i get in the view
Error validating access token: The user has not authorized application FacebookResponseException in FacebookResponseException.php line 89:
The issue arrises after a user de-authorizes my app from Facebook after have created an access_token , what I ant to do is catch the exception and automatically log them out and flush the session: But i can't seem to find the right exception to catch
I've tested with all these: I narrowed the problem to being inside this catch block: I included the catch block, then the full code I'm using in my route's function after.
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=permissions');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookAuthenticationException $e) {
echo 'Facebook Dan1 returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookAuthorizationException $e) {
echo 'Facebook Dan2 returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookClientException $e) {
echo 'Facebook Dan3 returned an error: ' . $e->getMessage();
exit;
} catch(Exception $e) {
echo 'Facebook Dan4 returned an error: ' . $e->getMessage();
exit;
}
now my full code for this page:
public function getHomeProfile(Request $request, LaravelFacebookSdk $fb)
{
$user = Auth::user();
$token = Session::get('fb_user_access_token');
$twitterToken = Session::get('access_token');
// $fb->setDefaultAccessToken($token);
// $resp = $fb->get('/debug_token?=input_token=$token');
// dd($resp);
$permissionsToRequest = ([
'public_profile',
'publish_actions'
]);
$login_link_for_public_actions = $fb->getLoginUrl($permissionsToRequest, 'http://pms.dev:8000/facebook/publicactions/callback');
if (isset($token)) {
$fb->setDefaultAccessToken($token);
// $debugToken = $fb->get('/debug_token?input_token=' . $token);
// $debugTokenResponse = $debugToken->getGraphNode()->asArray();
// echo "<pre>";
// print_r($debugTokenResponse);
// echo "<pre>";
// die();
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=permissions');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookAuthenticationException $e) {
echo 'Facebook Dan1 returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookAuthorizationException $e) {
echo 'Facebook Dan2 returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookClientException $e) {
echo 'Facebook Dan3 returned an error: ' . $e->getMessage();
exit;
} catch(Exception $e) {
echo 'Facebook Dan4 returned an error: ' . $e->getMessage();
exit;
}
// Returns a `Facebook\GraphNodes\GraphUser` collection
$facebookuser = $response->getGraphUser();
//$ty= json_decode($facebookuser);
$permissions = $facebookuser['permissions'];
$checked = '';
foreach ($permissions as $p) {
if ($p['permission'] == 'publish_actions' && $p['status'] == 'granted' ) {
$checked = 'checked';
}
}
} else {
$checked = null;
}
if (isset($twitterToken)) {
$twitterChecked = 'checked';
} else {
$twitterChecked = null;
}
$userPlugsCountry = $user->plugsCountry()->setPath($request->url());
$user_plugs_list = Auth::user()->plugs()->lists('id');
if (Auth::check()) {
$statuses = Status::where(function($query) {
return $query->where('user_id', Auth::user()->id)->where('parent_id', NULL)
->orWhereIn('user_id', Auth::user()->plugs()->lists('id'));
})->orderBy('created_at', 'desc')->paginate(7);
}
if (array_key_exists('REQUEST_SCHEME', $_SERVER)) {
$cors_location = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["SERVER_NAME"] .
dirname($_SERVER["SCRIPT_NAME"]) . "/cloudinary_cors.html";
} else {
$cors_location = "http://" . $_SERVER["HTTP_HOST"] . "/cloudinary_cors.html";
}
if ($request->ajax()) {
return [
'statuses' => view('ajax.status')->with('user', $user)->with(compact('statuses'))->render(),
'next_page' => $statuses->nextPageUrl(),
'countries' => view('ajax.next_countries')->with('user', $user)->with(compact('userPlugsCountry'))->render(),
'next_countries_page' => $userPlugsCountry->nextPageUrl(),
'prev_countries_page' => $userPlugsCountry->previousPageUrl(),
];
}
return view('profile.home')->with('user', $user)->with('cors_location', $cors_location)->with('statuses',$statuses)->with('userPlugsCountry',$userPlugsCountry)->with('checked', $checked)->with('login_link_for_public_actions',$login_link_for_public_actions)->with('twitterChecked', $twitterChecked);
}
OK i figured out whet i was doing wrong. I'm using sammyk laravelfacebooksdk https://github.com/SammyK/LaravelFacebookSdk and was only including:
use SammyK\LaravelFacebookSdk\LaravelFacebookSdk;
I didn't realize i also needed to add
use Facebook;
to the namespace.
easy fix, that gave me a lot of trouble.
<?php
ob_start();
session_start();
require ('openid.php');
function logoutbutton() {
echo "<form action=\"steamauth/logout.php\" method=\"post\"><input value=\"Logout\" type=\"submit\" /></form>"; //logout button
}
function steamlogin()
{
try {
require("settings.php");
$openid = new LightOpenID($steamauth['']);
$button['small'] = "small";
$button['large_no'] = "large_noborder";
$button['large'] = "large_border";
$button = $button[$steamauth['buttonstyle']];
if(!$openid->mode) {
if(isset($_GET['login'])) {
$openid->identity = 'http://steamcommunity.com/openid';
header('Location: ' . $openid->authUrl());
}
return "<form action=\"?login\" method=\"post\"> <input type=\"image\" src=\"http://cdn.steamcommunity.com/public/images/signinthroughsteam/sits_".$button.".png\"></form>";
}
elseif($openid->mode == 'cancel') {
echo 'User has canceled authentication!';
} else {
if($openid->validate()) {
$id = $openid->identity;
$ptn = "/^http:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/";
preg_match($ptn, $id, $matches);
$_SESSION['steamid'] = $matches[1];
// First determine of the $steamauth['loginpage'] has been set, if yes then redirect there. If not redirect to where they came from
if($steamauth['loginpage'] !== "") {
$returnTo = $steamauth['loginpage'];
} else {
//Determine the return to page. We substract "login&"" to remove the login var from the URL.
//"file.php?login&foo=bar" would become "file.php?foo=bar"
$returnTo = str_replace('login&', '', $_GET['openid_return_to']);
//If it didn't change anything, it means that there's no additionals vars, so remove the login var so that we don't get redirected to Steam over and over.
if($returnTo === $_GET['openid_return_to']) $returnTo = str_replace('?login', '', $_GET['openid_return_to']);
}
header('Location: '.$returnTo);
} else {
echo "User is not logged in.\n";
}
}
} catch(ErrorException $e) {
echo $e->getMessage();
}
}
?>
"Not Found The requested URL /login was not found on this server." idk how i do that.. search here and here but nothing found :c i tried all time but i get the error with /login not found on the server "yes i know my english is maybe to bad :D"
I started on an ftp project and I'm having trouble getting the directory to reload. My code says that it successfully changed, but I am assume that it only changes server-side. I need the browser to change the directory as well.
PS: How can I download via FTP through the browser? When I test locally it writes the files to the root directory but when connected remotely I don't know where they go. There is no indication of files being downloaded.
Any help would be greatly appreciated! And please, if you have any tips for me that would be great. I'm still pretty new at this but I'm trying my best.
<?php
session_id('logon');
session_start();
if (isset($_POST['connect']))
{
$_SESSION['port'] = $_POST['port'];
$_SESSION['server'] = $_POST['server'];
$_SESSION['user'] = $_POST['user'];
$_SESSION['password'] = $_POST['password'];
}
$port = $_SESSION['port'];
$server = $_SESSION['server'];
$user = $_SESSION['user'];
$pass = $_SESSION['password'];
$connection = ftp_connect($server)
or die("Couldn't connect!");
$logon = ftp_login($connection,$user,$pass)
or die("Couldn't login!" . $server ."<br>". $port);
$workingDir = ftp_pwd($connection);
echo "You are in $workingDir<br><br>";
$dirList = ftp_nlist($connection, ".");
foreach($dirList as $item)
{
$res = ftp_size($connection, $item);
if ($res != "-1")
{
echo "<a href='?download=$item'>$item</a><br>";
if (isset($_GET['download']))
{
if ($_GET['download'] == $item)
{
include('include/download.php');
}
}
}
else
{
$directory = $item;
echo "<a href='?change=$directory'>$directory</a><br>";
if ($_GET['change'] == $directory)
{
if (ftp_chdir($connection, $directory))
{
echo "Changed to " . ftp_pwd($connection) . "!<br>";
$dirList = ftp_nlist($connection, ".");
header("Refresh:0");
}
else
{
echo "Failed to change to $directory";
}
}
}
}
ini_set('error_reporting', E_ALL);
ftp_quit($connection);
?>
Solved! Added a directory.php file with
$getChange = $_GET['change'];
if (ftp_chdir($connection, $getChange))
{
echo "Changed to " . ftp_pwd($connection) . "!<br>";
$dirList = ftp_nlist($connection, ".");
}
else
{
echo "Failed to change to $getChange";
}
$workingDir = ftp_pwd($connection);
echo "You are in $workingDir<br><br>";
$dirList = ftp_nlist($connection, ".");
foreach($dirList as $item)
{
$res = ftp_size($connection, $item);
if ($res != "-1")
{
echo "<a href='?download=$item'>$item</a><br>";
if (isset($_GET['download']))
{
if ($_GET['download'] == $item)
{
include('include/download.php');
}
}
}
else
{
echo "<a href='directory.php?change=$item'>$item</a><br>";
}
}
If i have this:
function valid($valor) {
foreach($valor as $valor){
if (!empty($valor)) {
if (preg_match("/[^A-Za-z0-9-áàçéúâôóã ]|(\d+)/", $valor)) {
$error = "invalid";
throw new Exception($error);
}
}
}
}
and
if (isset($_POST['myform'])){
if ($val_->valid($form1['new'])) {
echo "ok";
}
else
echo "bad";
}
but i got: Fatal error: Uncaught exception 'Exception
What i want is basically something like that (pseudo code):
if (exception true) {
echo "problem";
}
else
echo "ok";
How can i do that ?
You have to handle the exception.
try {
...
//statements
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
EDIT:
try{
if ($val_->valid($form1['new'])) {
echo "ok";
}
}catch(Exception $e){
echo "bad";
}