コード例 #1
0
ファイル: reroute.php プロジェクト: EGreg/PHP-On-Pie
function users_after_pie_reroute($params, &$stop_dispatch)
{
    $uri = Pie_Dispatcher::uri();
    $app = Pie_Config::expect('pie', 'app');
    $ma = $uri->module . '/' . $uri->action;
    $requireComplete = Pie_Config::get('users', 'requireComplete', array());
    if (isset($requireComplete[$ma])) {
        $redirect_action = is_string($requireComplete[$ma]) ? $requireComplete[$ma] : "{$app}/login";
        $test_complete = true;
    } else {
        $requireLogin = Pie_Config::get('users', 'requireLogin', array());
        if (!isset($requireLogin[$ma])) {
            // We don't have to require complete or login here
            return;
        }
        $redirect_action = is_string($requireLogin[$ma]) ? $requireLogin[$ma] : "{$app}/login";
    }
    // First, try to get the user
    $user = Users::loggedInUser();
    if (!$user) {
        // Try authenticating with facebook
        $module = Pie_Dispatcher::uri()->module;
        $app_id = Pie_Config::expect('users', 'facebookApps', $module, 'appId');
        $user = Users::authenticate('facebook', $app_id);
    }
    if (!$user) {
        $uri->onSuccess = $uri->module . '/' . $uri->action;
        $uri->onCancel = "{$app}/welcome";
        if ($uri->onSuccess === $redirect_action) {
            // avoid a redirect loop
            $uri->onSuccess = "{$app}/home";
        }
        $parts = explode('/', $redirect_action);
        $uri->action = $parts[0];
        $uri->action = $parts[1];
    }
    // If have requireLogin but not requireComplete, then
    // simply change the underlying URI without redirecting
    if (empty($test_complete)) {
        return;
    }
    // If we are here, we should check if the user account is complete
    $complete = Pie::event('users/account/complete');
    if ($complete) {
        // good, nothing else to complete
        return;
    }
    // redirect to account page
    $account_action = Pie_Config::expect('users', 'accountAction', $uri->module);
    if ($ma != $account_action) {
        // Make the user launch into setting up their account.
        // If they want to return to this URL later, they can do it on their own.
        Pie_Response::redirect($account_action);
        $stop_dispatch = true;
        return;
    }
}
コード例 #2
0
ファイル: response.php プロジェクト: EGreg/PHP-On-Pie
/**
 * Default pie/response handler.
 * 1. Gets some slots, depending on what was requested.
 * 2. Renders them in a layout
 *    The layout expects "title", "dashboard" and "contents" slots to be filled.
 */
function pie_response($params)
{
    extract($params);
    /**
     * @var Exception $exception
     * @var array $errors
     */
    // Redirect to success page, if requested.
    $is_ajax = Pie_Request::isAjax();
    if (empty($errors) and empty($exception)) {
        if (!$is_ajax and isset($_REQUEST['_pie']['onSuccess'])) {
            $on_success = $_REQUEST['_pie']['onSuccess'];
            if (Pie_Config::get('pie', 'response', 'onSuccessShowFrom', true)) {
                $on_success = Pie_Uri::url($on_success . '?_pie[fromSuccess]=' . Pie_Dispatcher::uri());
            }
            Pie_Response::redirect($on_success);
            return;
        }
    }
    // Get the requested module
    $uri = Pie_Dispatcher::uri();
    if (!isset($module)) {
        $module = $uri->module;
        if (!isset($module)) {
            $module = 'pie';
            Pie_Dispatcher::uri()->module = 'pie';
        }
    }
    // Get the main module (the app)
    $app = Pie_Config::expect('pie', 'app');
    // Add some javascript to inform the front end of important URLs
    Pie::event('pie/addScriptLines');
    // What to do if this is an AJAX request
    if ($is_ajax) {
        $slot_names = Pie_Request::slotNames();
        if (!isset($slot_names)) {
            $slot_names = Pie_Config::get($module, 'response', 'slotNames', array('content' => null, 'dashboard' => null, 'title' => null, 'notices' => null));
        }
        $slots = array();
        $stylesheets = array();
        $stylesInline = array();
        $scripts = array();
        $scriptLines = array();
        if (is_array($slot_names)) {
            foreach ($slot_names as $slot_name => $v) {
                $slots[$slot_name] = Pie_Response::fillSlot($slot_name, 'default');
                $stylesheets[$slot_name] = Pie_Response::stylesheetsArray($slot_name);
                $stylesInline[$slot_name] = Pie_Response::stylesInline($slot_name);
                $scripts[$slot_name] = Pie_Response::scriptsArray($slot_name);
                $scriptLines[$slot_name] = Pie_Response::scriptLines($slot_name);
            }
        }
        $timestamp = microtime(true);
        $echo = Pie_Request::contentToEcho();
        // Render a JSON layout for ajax
        $to_encode = compact('slots', 'stylesheets', 'stylesInline', 'scripts', 'scriptLines', 'timestamp', 'echo');
        // Cut down on the response size
        foreach (array('slots', 'stylesheets', 'stylesInline', 'scripts', 'scriptLines') as $f) {
            $is_empty = true;
            if (is_array($to_encode[$f])) {
                foreach ($to_encode[$f] as $k => $v) {
                    if (isset($v)) {
                        $is_empty = false;
                    } else {
                        unset($to_encode[$f][$k]);
                    }
                }
            } else {
                if (!empty($to_encode[$f])) {
                    $is_empty = false;
                }
            }
            if ($is_empty) {
                unset($to_encode[$f]);
            }
        }
        switch (strtolower($is_ajax)) {
            case 'json':
            default:
                $json = json_encode($to_encode);
                $callback = Pie_Request::callback();
                echo $callback ? "{$callback}({$json})" : $json;
        }
        return;
    }
    // If this is a request for a regular webpage,
    // fill the usual slots and render a layout.
    // Attach stylesheets and scripts
    if (Pie_Request::accepts('text/fbml')) {
        Pie_Response::addStylesheet("css/fbml.css");
        Pie_Response::addScript('plugins/pie/fbjs/Pie.fb.js');
    } else {
        Pie_Response::addStylesheet("css/html.css");
        Pie_Response::addScript('plugins/pie/js/Pie.js');
    }
    // Get all the usual slots for a webpage
    $slot_names = Pie_Config::get($module, 'response', 'slotNames', array('content' => null, 'dashboard' => null, 'title' => null, 'notices' => null));
    $slots = array();
    foreach ($slot_names as $sn => $v) {
        $slots[$sn] = Pie_Response::fillSlot($sn, 'default');
    }
    $output = Pie_Response::output();
    if (isset($output)) {
        if ($output === true) {
            return;
        }
        if (is_string($output)) {
            echo $output;
        }
        return;
    }
    if (Pie_Request::accepts('text/fbml')) {
        // Render a full FBML layout
        $layout_view = Pie_Config::get($app, 'response', 'layout_fbml', "{$app}/layout/fbml.php");
        echo Pie::view($layout_view, $slots);
    } else {
        // Render a full HTML layout
        $layout_view = Pie_Config::get($app, 'response', 'layout_html', "{$app}/layout/html.php");
        echo Pie::view($layout_view, $slots);
    }
}