Ejemplo n.º 1
0
function users_contact_post()
{
    Pie_Session::start();
    Pie_Valid::nonce(true);
    extract($_REQUEST);
    $user = Users::loggedInUser();
    if (!$user) {
        throw new Users_Exception_NotLoggedIn();
    }
    $app = Pie_Config::expect('pie', 'app');
    $subject = "Welcome! Activate your email.";
    $view = "{$app}/email/setEmail.php";
    $fields = array();
    $p = array();
    $p['subject'] =& $subject;
    $p['view'] =& $view;
    $p['fields'] =& $fields;
    Pie::event('users/setEmail', $p, 'before');
    // may change the fields
    if (isset($first_name)) {
        $user->first_name = $first_name;
    }
    if (isset($last_name)) {
        $user->last_name = $last_name;
    }
    $user->addEmail($_REQUEST['email_address'], $subject, $view, true, $fields);
    // If no exceptions were throw, save this user row
    if (isset($first_name) or isset($last_name)) {
        $user->save();
    }
}
Ejemplo n.º 2
0
function pie_response_content()
{
    $serve_fbml = Pie_Request::accepts('text/fbml');
    if ($serve_fbml) {
        // add more fbjs files here
    } else {
        // the js files for your app
        Pie_Response::addScript('plugins/pie/js/Pie.js');
        Pie_Response::addScript("http://cdn.jquerytools.org/1.2.3/jquery.tools.min.js");
        Pie_Response::addScript('plugins/users/js/Users.js');
        // See views/layout/html.php for a facebook script at the top of the <body>
    }
    Pie_Response::addStylesheet('plugins/pie/css/Ui.css');
    $app = Pie_Config::expect('pie', 'app');
    $url = Pie_Request::url();
    $module = Pie_Dispatcher::uri()->module;
    if (empty($module)) {
        return Pie::event("{$app}/notFound/response/content");
    }
    $action = Pie_Dispatcher::uri()->action;
    $event = "{$module}/{$action}/response/content";
    if (!Pie::canHandle($event)) {
        return Pie::event("{$app}/notFound/response/content");
    }
    // Go ahead and fire the event, returning the result.
    return Pie::event($event);
}
Ejemplo n.º 3
0
/**
 * Override pie/notFound handler.
 * just goes on to render our app's response,
 * which will echo a 404 view.
 */
function pie_notFound($params)
{
    if (!Pie_Dispatcher::uri()->facebook) {
        header("HTTP/1.0 404 Not Found");
    }
    Pie_Dispatcher::uri()->module = Pie_Config::expect('pie', 'app');
    Pie_Dispatcher::uri()->action = 'notFound';
    Pie::event('pie/response', $params);
}
Ejemplo n.º 4
0
/**
 * Override pie/noModule handler.
 * just goes on to render our app's response,
 * which will echo a 404 view.
 */
function pie_noModule($params)
{
    if (!Pie_Request::accepts('text/fbml')) {
        header("HTTP/1.0 404 Not Found");
    }
    Pie_Dispatcher::uri()->module = Pie_Config::expect('pie', 'app');
    Pie_Dispatcher::uri()->action = '';
    Pie::event('pie/response', $params);
}
Ejemplo n.º 5
0
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;
    }
}
Ejemplo n.º 6
0
/**
 * This is a tool for selecting photos (to possibly add)
 * @param $facebook
 *  Optional. You can provide instance of the Facebook class.
 * @param $upload
 *  Defaults to false. If true, shows an option to upload, as well.
 * @param $action_uri
 *  Defaults to 'items/addPhoto'. The URI to submit the form to.
 * @param $filter_visible
 *  Optional string. Set to 'everyone' to only display albums visible to everyone.
 * @param $on_success
 *  Optional string. The url to redirect to after a photo is added or uploaded.
 */
function items_addPhoto_tool($params)
{
    if (isset(Users::$facebook)) {
        $facebook = Users::$facebook;
    } else {
        $app = Pie_Config::expect('pie', 'app');
        if (!isset(Users::$facebooks[$app])) {
            throw new Pie_Exception_MissingObject(array('name' => 'Users::$facebooks[' . $app . ']'));
        }
        $facebook = Users::$facebooks[$app];
    }
    $defaults = array('facebook' => $facebook, 'upload' => false, 'action_uri' => 'items/addPhoto', 'on_success' => Pie_Request::url());
    extract(array_merge($defaults, $params));
    if (!$facebook instanceof Facebook) {
        throw new Pie_Exception_WrongType(array('field' => '$facebook', 'type' => 'Facebook'));
    }
    if (isset($_REQUEST['_pie']['onSuccess'])) {
        $on_success = $_REQUEST['_pie']['onSuccess'];
    }
    $sn = Pie_Session::name();
    $sid = Pie_Session::id();
    $photos = array();
    if (isset($aid)) {
        $photos = Items::facebookPhotos($facebook, $aid);
        return Pie::view('items/tool/addPhotoList.php', compact('photos'));
    }
    $facebook->require_login();
    $album_rows = Items::facebookAlbums($facebook);
    $albums = array();
    foreach ($album_rows as $ar) {
        if (isset($filter_visible) and $ar['visible'] != $filter_visible) {
            continue;
        }
        $albums[$ar['aid']] = $ar['name'];
    }
    $albums = $albums;
    if (count($album_rows)) {
        $row = reset($album_rows);
        $photos = Items::facebookPhotos($facebook, $row['aid']);
    }
    $throbber_url = Pie_Html::themedUrl('plugins/items/img/anim/throbber.gif');
    $url_json = json_encode(Pie_Uri::url($action_uri));
    Pie_Response::addStylesheet('plugins/items/css/Items.css');
    if (Pie_Request::accepts('text/fbml')) {
        Pie_Response::addScript('plugins/items/fbjs/Items.fb.js');
    } else {
        Pie_Response::addScript('plugins/items/js/Items.js');
    }
    if (is_bool($upload)) {
        $upload = uniqid('up.', false);
    }
    $addPhoto_url_json = json_encode(Pie_Uri::url('items/addPhoto'));
    Pie_Response::addScriptLine("\tPie.Items.urls['items/addPhoto'] = {$addPhoto_url_json};");
    return Pie::view('items/tool/addPhoto.php', compact('action_uri', 'on_success', 'on_added', 'albums', 'photos', 'throbber_url', 'upload'));
}
Ejemplo n.º 7
0
function users_after_pie_addScriptLines()
{
    $app = Pie_Config::expect('pie', 'app');
    $app_json = json_encode($app);
    $fb_app_info = Pie_Config::get('users', 'facebookApps', $app, array());
    if ($fb_app_info) {
        unset($fb_app_info['secret']);
        $fb_app_info_json = json_encode($fb_app_info);
        Pie_Response::addScriptLine("// users {{ \n" . "\t\tif (!Pie) Pie = {}; if (!Pie.Users) Pie.Users = {};\n" . "\t\tif (!Pie.Users.facebookApps) Pie.Users.facebookApps = {};\n" . "\t\tPie.Users.facebookApps[{$app_json}] = {$fb_app_info_json}\n" . "// }} users \n");
    }
}
Ejemplo n.º 8
0
 function beforeSave($updated_fields)
 {
     parent::beforeSave($updated_fields);
     if (isset($updated_fields['username'])) {
         $app = Pie_Config::expect('pie', 'app');
         $unique = Pie_Config::get('users', 'model', $app, 'username_unique', true);
         if ($unique) {
             $criteria = array('username' => $updated_fields['username']);
             if (isset($this->id)) {
                 $criteria['id != '] = $this->id;
             }
             $row = Users_User::db()->select('COUNT(1)', Users_User::table())->where($criteria)->limit(1)->execute()->fetch();
             if ($row[0] > 0) {
                 throw new Users_Exception_UsernameExists(null, 'username');
             }
         }
     }
     return $updated_fields;
 }
Ejemplo n.º 9
0
function pie_addScriptLines()
{
    $app = Pie_Config::expect('pie', 'app');
    $uri = Pie_Dispatcher::uri();
    $proxies_json = json_encode(Pie_Config::get('pie', 'proxies', array()));
    $uri_json = json_encode($uri->toArray());
    $url = Pie_Request::url();
    $url_json = json_encode($url);
    $proxy_url_json = json_encode(Pie_Uri::url($url));
    $base_url = json_encode(Pie_Request::baseUrl());
    Pie_Response::addScriptLine(<<<EOT
// pie {{
\t\tPie.info = {
\t\t\t"proxies": {$proxies_json},
\t\t\t"uri": {$uri_json},
\t\t\t"url": {$url_json},
\t\t\t"proxyUrl": {$proxy_url_json},
\t\t\t"baseUrl": {$base_url}
\t\t};
EOT
);
    $uris = Pie_Config::get('pie', 'javascript', 'uris', array());
    $urls = array();
    foreach ($uris as $u) {
        $urls["{$u}"] = Pie_Uri::url("{$u}");
    }
    $urls_json = json_encode($urls);
    Pie_Response::addScriptLine("\t\tPie.urls = {$urls_json};");
    // Export more variables to inline js
    $app = Pie_Config::expect('pie', 'app');
    $app_json = json_encode($app);
    Pie_Response::addScriptLine("\t\tPie.app = {$app_json};\n" . "// }} pie");
    $snf = Pie_Config::get('pie', 'session', 'nonceField', 'nonce');
    $nonce = isset($_SESSION[$snf]) ? $_SESSION[$snf] : null;
    if ($nonce) {
        $nonce_json = json_encode($nonce);
        Pie_Response::addScriptLine("\t\tPie.nonce = {$nonce_json};");
    }
}
Ejemplo n.º 10
0
 /**
  * Get the status of the logged-in user and their account.
  * @param Users_Email $email
  *  Optional. Pass a reference here to be filled with the email object, if it's loaded.
  *  You can use it in conjunction with the "verify email" status.
  * @return array|boolean
  *  Returns false if the user is not logged in.
  *  Returns true if everything is complete.
  *  Otherwise, returns an array whose keys are the names of the missing fields:
  *  ("first_name", "last_name", "birthday", "gender", "desired_gender", "username",
  *  "email_address")
  *  and the values are "missing" or "unverified"
  */
 static function accountStatus(&$email = null)
 {
     $module = Pie_Dispatcher::uri()->module;
     $user = Users::loggedInUser();
     if (!$user) {
         // Try to authenticate
         $app_id = Pie_Config::expect('users', 'facebookApps', $module, 'appId');
         $user = Users::authenticate('facebook', $app_id);
         if (!$user) {
             return false;
         }
     }
     $result = array();
     if (empty($user->email_address)) {
         // An email address isn't verified for this user yet.
         // If the user hasn't even added an email address, then ask for one.
         if (!isset(self::$email)) {
             self::$email = new Users_Email();
             self::$email->user_id = $user->id;
             self::$email = self::$email->retrieve(null, false, '*', true)->orderBy('time_created', false)->resume();
         }
         $email = self::$email;
         if ($email) {
             // The email could be unverified, sunspended, unsubscribed, etc.
             $result['email_address'] = $email->state;
         } else {
             $result['email_address'] = 'missing';
         }
     }
     $fieldnames = array('first_name', 'last_name', 'username', 'birthday', 'gender', 'desired_gender', 'relationship_status', 'relationship_user_id', 'zipcode');
     foreach ($fieldnames as $k => $v) {
         if (empty($user->{$v})) {
             $result[$v] = 'missing';
         }
     }
     return $result;
 }
Ejemplo n.º 11
0
function items_addPhoto_response_content()
{
    if (isset($_POST['fb_sig_app_id'])) {
        $app_id = $_POST['fb_sig_app_id'];
    } else {
        $app = Pie_Config::expect('pie', 'app');
        $app_id = Pie_Config::expect('users', 'facebookApps', $app, 'appId');
    }
    Users::authenticate('facebook', $app_id);
    return Pie::tool('items/addPhoto', array());
}
Ejemplo n.º 12
0
function pie_response_dashboard()
{
    $app = Pie_Config::expect('pie', 'app');
    $slogan = "Powered by PHP ON PIE.";
    return Pie::view("{$app}/dashboard.php", compact('slogan'));
}
Ejemplo n.º 13
0
/**
 * 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);
    }
}