/** * Get newsfeed item as HTML * * @param Model_NewsfeedItem $item * @return string */ public static function get(Model_NewsfeedItem $item) { $link = $item->is_aggregate() ? implode('<br>', self::get_links($item)) : self::get_link($item); if (!$link) { return ''; } $text = ''; switch ($item->type) { case self::TYPE_COMMENT: $text = $item->is_aggregate() ? __('commented photos') : __('commented a photo'); break; case self::TYPE_COMMENT_FLYER: $text = $item->is_aggregate() ? __('commented flyers') : __('commented a flyer'); break; case self::TYPE_FLYER_EDIT: $text = $item->is_aggregate() ? __('updated flyers') : __('updated a flyer'); break; case self::TYPE_NOTE: $user = Model_User::find_user($item->data['user_id']); if ($link && $user->loaded()) { $text = __('tagged :user to a photo', array(':user' => HTML::user($user))); } break; case self::TYPE_UPLOAD: $text = __('added new photos to a gallery'); break; } return $text . '<br />' . $link; }
/** * Get newsfeed item as HTML * * @param Model_NewsfeedItem $item * @return string */ public static function get(Model_NewsfeedItem $item) { $text = ''; switch ($item->type) { case self::TYPE_COMMENT: $gallery = Model_Gallery::factory($item->data['gallery_id']); $image = Model_Image::factory($item->data['image_id']); if ($gallery->loaded() && $image->loaded()) { $text = __('commented to an image<br />:gallery', array(':gallery' => HTML::anchor(Route::url('gallery_image', array('gallery_id' => Route::model_id($gallery), 'id' => $image->id, 'action' => '')), '<i class="icon-camera icon-white"></i> ' . HTML::chars($gallery->name), array('class' => 'hoverable')))); } break; case self::TYPE_COMMENT_FLYER: $flyer = Model_Flyer::factory($item->data['flyer_id']); if ($flyer->loaded()) { $text = __('commented to a flyer<br />:flyer', array(':flyer' => HTML::anchor(Route::url('flyer', array('id' => $flyer->id)), '<i class="icon-picture icon-white"></i> ' . ($flyer->name ? HTML::chars($flyer->name) : __('flyer')), array('class' => 'hoverable')))); } break; case self::TYPE_FLYER_EDIT: $flyer = Model_Flyer::factory($item->data['flyer_id']); if ($flyer->loaded()) { $text = __('updated flyer<br />:flyer', array(':flyer' => HTML::anchor(Route::url('flyer', array('id' => $flyer->id)), '<i class="icon-picture icon-white"></i> ' . ($flyer->name ? HTML::chars($flyer->name) : __('flyer')), array('class' => 'hoverable')))); } break; case self::TYPE_NOTE: $gallery = Model_Gallery::factory($item->data['gallery_id']); $image = Model_Image::factory($item->data['image_id']); $user = Model_User::find_user($item->data['user_id']); if ($gallery->loaded() && $image->loaded() && $user->loaded()) { $text = __('tagged :user to an image<br />:gallery', array(':user' => HTML::user($user), ':gallery' => HTML::anchor(Route::url('gallery_image', array('gallery_id' => Route::model_id($gallery), 'id' => $image->id, 'action' => '')), '<i class="icon-tag icon-white"></i> ' . HTML::chars($gallery->name), array('class' => 'hoverable')))); } break; } return $text; }
/** * Get anchor to newsfeed item target. * * @static * @param Model_NewsfeedItem $item * @return string */ public static function get_link(Model_NewsfeedItem $item) { $text = ''; switch ($item->type) { case self::TYPE_FRIEND: if ($friend = Model_User::find_user($item->data['friend_id'])) { $text = HTML::user($friend); } break; } return $text; }
/** * Create new view. * * @param Model_Forum_Post $forum_post * @param Model_Forum_Topic $forum_topic */ public function __construct(Model_Forum_Post $forum_post, Model_Forum_Topic $forum_topic) { parent::__construct(); $this->forum_post = $forum_post; $this->forum_topic = $forum_topic; // Get post author $this->author = Model_User::find_user($this->forum_post->author_id); // Viewer's post $this->my = self::$_user && $this->author && $this->author->id == self::$_user_id; // Topic author's post $this->owner = $this->author ? $this->author->id == $this->forum_topic->author_id : $this->forum_post->author_name == $this->forum_topic->author_name; $this->id = 'post-' . $this->forum_post->id; $this->class = 'media permalink post' . ($this->owner ? ' owner' : '') . ($this->my ? ' my' : ''); }
/** * Get notification as HTML. * * @static * @param Model_Notification * @return string */ public static function get(Model_Notification $notification) { $text = ''; switch ($notification->type) { case self::TYPE_FRIEND: $friend = Model_User::find_user($notification->user_id); if ($friend->loaded()) { $text = __(':friend added you as a friend', array(':friend' => HTML::user($friend))); } else { $notification->delete(); } break; } return $text; }
/** * Check permission * * @param string $permission * @param Model_User $user * @return boolean */ public function has_permission($permission, $user) { switch ($permission) { case self::PERMISSION_READ: return true; case self::PERMISSION_CREATE: return (bool) $user; case self::PERMISSION_COMMENT: case self::PERMISSION_COMMENTS: $author = Model_User::find_user($this->author_id); return $user && ($this->author_id == $user->id || !$author->is_ignored($user) && !$author->is_ignored($user, true)); case self::PERMISSION_DELETE: case self::PERMISSION_UPDATE: return $user && ($this->author_id == $user->id || $user->has_role('admin')); } return false; }
/** * Check permission for object * * @static * @param Permission_Interface $model Object implemeneting permission interface * @param string $permission * @param mixed $user Defaults to session user * @return boolean */ public static function has(Permission_Interface $model, $permission = Permission_Interface::PERMISSION_READ, $user = false) { if (Kohana::$profiling === true && class_exists('Profiler', false)) { $benchmark = Profiler::start('Anqh', __METHOD__ . '(' . get_class($model) . ')'); } // Make sure we have a valid user, if any $user = Model_User::find_user($user); // Create unique permission id for caching $permission_id = sprintf('%s:%d:%s:%d', get_class($model), $model->id(), $permission, $user && $user->loaded() ? $user->id : 0); // If permission check not found from cache ask the model if (!isset(self::$_permissions[$permission_id])) { self::$_permissions[$permission_id] = $model->has_permission($permission, $user); } if (isset($benchmark)) { Profiler::stop($benchmark); } return self::$_permissions[$permission_id]; }
/** * Get URL for user * * @param mixed $user * @param string $action * @return string */ public static function user($user, $action = null) { // User id given if (is_numeric($user) && (int) $user > 0) { $user = Model_User::find_user($user); } // Model_User given if ($user instanceof Model_User) { $user = $user->username; } else { if (is_array($user) && isset($user['username'])) { $user = $user['username']; } } // Username given if (is_string($user)) { return Route::get('user')->uri(array('username' => urlencode($user), 'action' => $action)); } return null; }
/** * Handle forum quotations * * @param BBCode $bbcode * @param string $action * @param string $name * @param string $default * @param array $params * @param string $content * @return string */ public function bbcode_quote($bbcode, $action, $name, $default, $params, $content) { // Pass all to 2nd phase if ($action == BBCODE_CHECK) { return true; } // Parse parameters foreach ($params['_params'] as $param) { switch ($param['key']) { // Parent post id case 'post': $post = Jelly::select('forum_post', (int) $param['value']); break; // Parent post author // Parent post author case 'author': $author_name = $param['value']; $author = Model_User::find_user($author_name); break; } } // Add parent post if (isset($post) && $post->loaded()) { $quote = '<blockquote cite="' . URL::site(Route::model($post->topic)) . '?post=' . $post->id . '#post-' . $post->id . '">'; // Override author $author = $post->author; } else { $quote = '<blockquote>'; } $quote .= '<p>' . trim($content) . '</p>'; // Post author if (isset($author) && $author->loaded()) { $quote .= '<cite>' . __('-- :author', array(':author' => HTML::user($author))) . '</cite>'; } else { if (isset($author_name)) { $quote .= '<cite>' . __('-- :author', array(':author' => HTML::chars($author_name))) . '</cite>'; } } $quote .= '</blockquote>'; return $quote; }
/** * Get newsfeed item as HTML * * @static * @param Model_NewsfeedItem $item * @return string */ public static function get(Model_NewsFeedItem $item) { $text = ''; switch ($item->type) { case self::TYPE_DEFAULT_IMAGE: $image = Model_Image::factory($item->data['image_id']); if ($image->loaded()) { $text = __('changed their default image'); } break; case self::TYPE_FRIEND: $friend = Model_User::find_user($item->data['friend_id']); if ($friend->loaded()) { $text = __('added :friend as a friend', array(':friend' => HTML::user($friend))); } break; case self::TYPE_LOGIN: $text = __('logged in'); break; } return $text; }
/** * Get URL for user * * @param mixed $user true for session user * @param string $action * @return string */ public static function user($user, $action = null) { static $_visitor; if (is_numeric($user) && (int) $user > 0) { // User id given if ($user = Model_User::find_user($user)) { $user = $user->username; } } else { if ($user instanceof Model_User) { // Model_User given $user = $user->username; } else { if (is_array($user) && isset($user['username'])) { // Light user array given $user = $user['username']; } else { if ($user === true) { // Use session user if ($_visitor === null) { if ($user = Visitor::instance()->get_user()) { $_visitor = $user->username; } else { // No session user available $_visitor = false; } } $user = $_visitor; } } } } // Username available if (is_string($user)) { return Route::url('user', array('username' => urlencode($user), 'action' => $action)); } return null; }
/** * Action: Password lost */ public function action_password() { $this->history = false; $email = $message = ''; // Handle request if ($_POST && ($email = trim(Arr::get($_POST, 'email', '')))) { $message = new View_Alert(__('We could not find any user or the user is missing email address, sorry.'), __('Uh oh,')); // Find the user, accept only strings $user = Valid::digit($email) ? false : Model_User::find_user(trim($email)); // Send email if ($user && Valid::email($user->email)) { $subject = __('Your new :site password', array(':site' => Kohana::$config->load('site.site_name'))); $mail = __("Forgot your password, :username?\n\nWe received a request to generate a new password for your :site account, please sign in and change your password. You should also delete this email.\n\nUsername: :username\nPassword: :password", array(':site' => Kohana::$config->load('site.site_name'), ':username' => Text::clean($user->username), ':password' => Visitor::generate_password($user->password))); if (Email::send($user->email, Kohana::$config->load('site.email_invitation'), $subject, $mail)) { $message = new View_Alert(__(':email should soon receive the generated password in their inbox.', array(':email' => $email)), __('Mission accomplished!'), View_Alert::SUCCESS); $email = ''; } } } // Build page $this->view = View_Page::factory(__('Misplaced your password?')); $this->view->add(View_Page::COLUMN_MAIN, $this->section_password($message, $email)); }
/** * Validate callback wrapper for checking email uniqueness * * @static * @param Validate $array * @param string $field */ public static function _unique(Validate $array, $field) { if (Model_User::find_user($array[$field])) { $array->error($field, 'unique', array('param1' => $field)); } }
/** * Report image. * * @static * @param Model_User $user * @param Model_Image $image * @param string $reason */ public static function image_removal_request(Model_User $user, Model_Image $image, $reason = null) { if ($user && $image) { $author = Model_User::find_user($image->author_id); if ($author) { parent::add($user, $author, self::CLASS_GALLERIES, self::TYPE_IMAGE_REPORT, $image->id, $reason); } } }
/** * Make sure given user is a proper user object * * @param mixed $user * @return Model_User */ protected function _get_user($user) { static $current; if (!is_object($current) && (is_string($user) || is_int($user))) { $current = Model_User::find_user($user); } if ($user instanceof Model_User && $user->loaded()) { $current = $user; } return $current; }
/** * Get user or redirect to user list * * @param boolean $redirect * @return Model_User */ protected function _get_user($redirect = true) { // Get our user, default to logged in user if no username given $username = urldecode((string) $this->request->param('username')); $user = $username == '' ? self::$user : Model_User::find_user($username); if (!$user && $redirect) { $this->request->redirect(Route::get('users')->uri()); } return $user; }
/** * Action: add note */ public function action_note() { $this->history = false; /** @var Model_Gallery $gallery */ $gallery_id = (int) $this->request->param('gallery_id'); $gallery = new Model_Gallery($gallery_id); if (!$gallery->loaded()) { throw new Model_Exception($gallery, $gallery_id); } /** @var Model_Image $image */ $image_id = $this->request->param('id'); $image = new Model_Image($image_id); if (!$image->loaded()) { throw new Model_Exception($image, $image_id); } // Permission check Permission::required($image, Model_Image::PERMISSION_NOTE, self::$user); // Create note if (isset($_POST['name']) && trim($_POST['name'] != '')) { // Get note user $username = trim($_POST['name']); $user = Model_User::find_user($username); if (!$user && ($user_id = Arr::get($_POST, 'user_id'))) { $user = Model_User::find_user($user_id); } try { $position = Arr::intersect($_POST, array('x', 'y', 'width', 'height'), true); $image->add_note(self::$user->id, count($position) == 4 ? $position : null, $user ? $user : $username); // Newsfeed if ($user) { NewsfeedItem_Galleries::note(self::$user, $gallery, $image, $user); } } catch (Validation_Exception $e) { } } // Redirect back to image // @todo: ajaxify for more graceful approach $this->request->redirect(Route::get('gallery_image')->uri(array('gallery_id' => Route::model_id($gallery), 'id' => $image->id, 'action' => ''))); }
/** * Action: Redirected from 3rd party. */ public function action_redirect() { $provider = $this->consumer->get_provider(); if ($provider != 'facebook') { // Unsupported provider $this->view->add(View_Page::COLUMN_CENTER, new View_Alert(__('We are not entirely sure what 3rd party service redirected you here'), __('Failed to load your profile :('), View_Alert::ERROR)); Kohana::$log->add(Log::NOTICE, 'OAuth2: Unsupported provider: :provider', array(':provider' => $provider)); return; } if ($response = Arr::get($_REQUEST, OAuth2::RESPONSE_TYPE_CODE)) { // Code received, change it to access token try { $token = $this->consumer->request_token(array(OAuth2::RESPONSE_TYPE_CODE => $response)); if (Visitor::$user) { // Already logged in $external = Model_User_External::factory()->find_by_user_id(Visitor::$user->id, $provider); if ($this->_update_token($external, $token)) { // Already paired with local user $this->request->redirect(URL::user(Visitor::$user, 'settings')); //Request::back(); } else { // Not paired with local user, do so if ($response = $this->consumer->api_call('/me', array('fields' => 'id,email'))) { // Received a response from 3rd party if ($error = Arr::get($response, 'error')) { // .. but it was an error $this->view->add(View_Page::COLUMN_CENTER, new View_Alert(__('They said ":error"', array(':error' => HTML::chars($error->message))), __('Failed to load your profile :('), View_Alert::ERROR)); Kohana::$log->add(Log::NOTICE, 'OAuth2: Failed to load Facebook profile: :error', array(':error' => $error->message)); } else { // Received required information $external = new Model_User_External(); $external->set_fields(array('token' => $token['access_token'], 'user_id' => Visitor::$user->id, 'external_user_id' => Arr::get($response, 'id'), 'created' => time(), 'expires' => time() + (int) $token['expires'], 'provider' => $provider)); $external->save(); $this->request->redirect(URL::user(Visitor::$user, 'settings')); //Request::back(); } } else { // No data received, this should be handled by exceptions } } } else { // No signed in user available if ($response = $this->consumer->api_call('/me')) { // Received a response from 3rd party if ($error = Arr::get($response, 'error')) { // .. but it was an error $this->view->add(View_Page::COLUMN_CENTER, new View_Alert(__('They said ":error"', array(':error' => HTML::chars($error->message))), __('Failed to load your profile :('), View_Alert::ERROR)); Kohana::$log->add(Log::NOTICE, 'OAuth2: Failed to load Facebook profile: :error', array(':error' => $error->message)); } else { // Received required information $external_user_id = Arr::get($response, 'id'); $external = Model_User_External::factory()->find_by_external_user_id($external_user_id, $provider); if ($this->_update_token($external, $token)) { // Already paired with local user, login Kohana::$log->add(Log::DEBUG, 'OAuth2: Attempting to login :external_user_id => :user_id', array(':external_user_id' => $external->external_user_id, ':user_id' => $external->user_id)); if ($this->_login($external)) { Request::back(); } Kohana::$log->add(Log::WARNING, 'OAuth2: Login failed'); } else { // Not paired with a local user, check if we have unpaired user available $email = Arr::get($response, 'email'); // Store external user id in session data, token should be stored in OAuth2 Session::instance()->set('oauth2.' . $provider . '.id', $external_user_id); if ($user = Model_User::find_user($email)) { // User with same email found, ask to sign in Kohana::$log->add(Log::DEBUG, 'OAuth2: Existing user with same email found'); $this->view->add(View_Page::COLUMN_CENTER, $this->section_signin($user, $response)); } else { // No user with same email found, start registering Kohana::$log->add(Log::DEBUG, 'OAuth2: Starting new user registration'); Session::instance()->set('oauth2.' . $provider . '.response', $response); $this->request->redirect(Route::url('sign', array('action' => 'up')) . '?provider=' . $provider); } } } } else { // No data received, this should be handled by exceptions } } } catch (OAuth2_Exception_InvalidGrant $e) { $this->view->add(View_Page::COLUMN_CENTER, new View_Alert(HTML::chars($e->getMessage()), __('Failed to load your profile :('), View_Alert::ERROR)); Kohana::$log->add(Log::NOTICE, 'OAuth2: Invalid grant: :error', array(':error' => $e->getMessage())); } catch (Kohana_Exception $e) { $this->view->add(View_Page::COLUMN_CENTER, new View_Alert(HTML::chars($e->getMessage()), __('Failed to load your profile :('), View_Alert::ERROR)); Kohana::$log->add(Log::NOTICE, 'OAuth2: Exception: :error', array(':error' => $e->getMessage())); } } else { $this->view->add(View_Page::COLUMN_CENTER, new View_Alert(__('Did not receive required code from 3rd party'), __('Failed to load your profile :('), View_Alert::ERROR)); Kohana::$log->add(Log::NOTICE, 'OAuth2: No code received'); } }
/** * Action: Artist profile */ public function action_profile() { $user = Model_User::find_user(urldecode((string) $this->request->param('username'))); if (!$user) { $this->request->redirect(Route::url('charts')); return; } // Build page $this->view = Controller_User::_set_page($user); $this->view->tab = 'music'; // Browse $tracks = Model_Music_Track::factory()->find_by_user($user->id, Model_Music_Track::TYPE_MIX, 0); if ($count = count($tracks)) { $this->view->add(View_Page::COLUMN_LEFT, $this->section_browse($tracks, __('Mixtapes') . ' <small>(' . $count . ')</small>')); } $tracks = Model_Music_Track::factory()->find_by_user($user->id, Model_Music_Track::TYPE_TRACK, 0); if ($count = count($tracks)) { $this->view->add(View_Page::COLUMN_RIGHT, $this->section_browse($tracks, __('Tracks') . ' <small>(' . $count . ')</small>')); } }
/** * Returns user link * * @param mixed $user Model_User, uid or username * @param string $nick * @param array $attributes * @return string */ public static function user($user, $nick = null, array $attributes = null) { static $viewer = false; // Load current user for friend styling if ($viewer === false) { $viewer = Visitor::instance()->get_user(); } $class = array('user', 'hoverable'); if ($user instanceof Model_user || $user && ($user = Model_User::find_user($user))) { if ($user->loaded()) { $nick = $user->username; if ($viewer && $viewer->is_friend($user)) { $class[] = 'friend '; } if ($user->gender) { $class[] = $user->gender == 'f' ? 'female ' : 'male '; } } } $class[] = Arr::get($attributes, 'class'); $attributes['class'] = trim(implode(' ', $class)); return empty($nick) ? __('Unknown') : HTML::anchor(URL::user($nick), $nick, $attributes); }
/** * Action: search */ public function action_search() { $images = array(); $username = Arr::get($_GET, 'user'); $this->view = View_Page::factory(__('Search')); if ($username) { // Search user's images if ($user = Model_User::find_user($username)) { $this->view->title = __("Search results for ':query'", array(':query' => HTML::chars($user->username))); $images = Model_Image::factory()->find_by_user($user->id); } else { $this->view->title = __("Search results for ':query'", array(':query' => HTML::chars($username))); } // Build page $this->view->subtitle = __(':count images', array(':count' => count($images))); $this->_set_page_actions(Permission::has(new Model_Gallery(), Model_Gallery::PERMISSION_CREATE)); if (count($images)) { $this->view->add(View_Page::COLUMN_CENTER, $this->section_search_results($images)); } else { $this->view->add(View_Page::COLUMN_CENTER, new View_Alert(__('No images found.'), null, View_Alert::INFO)); } return; } // No results }
/** * Action: user's blog */ public function action_user() { $user = Model_User::find_user(urldecode((string) $this->request->param('username'))); if (!$user) { $this->request->redirect(Route::url('blogs')); return; } $blogs = Model_Blog_Entry::factory()->find_by_user($user); if ($months = $this->_build_months($blogs)) { // Default to last month $year = (int) $this->request->param('year'); $month = (int) $this->request->param('month'); if (!$year) { $year = max(array_keys($months)); $month = max(array_keys($months[$year])); } else { if (!$month) { $month = isset($months[$year]) ? min(array_keys($months[$year])) : 1; } } $year = min($year, date('Y')); $month = min(12, max(1, $month)); // Build page $this->view = Controller_User::_set_page($user); $this->view->tab = 'blog'; $this->view->add(View_Page::COLUMN_CENTER, '<h2>' . HTML::chars(date('F Y', mktime(null, null, null, $month, 1, $year))) . '</h2>'); // Pagination $params = array('username' => urlencode($user->username)); $this->view->add(View_Page::COLUMN_CENTER, $this->section_month_pagination($months, 'blog_user', $params, $year, $month)); // Entries if (isset($months[$year]) && isset($months[$year][$month])) { foreach ($months[$year][$month] as $entry) { $this->view->add(View_Page::COLUMN_CENTER, $this->section_entry($entry, true)); } } // Month browser $this->view->add(View_Page::COLUMN_RIGHT, $this->section_month_browser($months, 'blog_user', $params, $year, $month)); } else { // No entires found $this->view->add(View_Page::COLUMN_CENTER, new View_Alert(__('Alas, the quill seems to be dry, no blog entries found.'), null, View_Alert::INFO)); } }
<?php defined('SYSPATH') or die('No direct access allowed.'); /** * Forum post * * @package Forum * @author Antti Qvickström * @copyright (c) 2010 Antti Qvickström * @license http://www.opensource.org/licenses/mit-license.php MIT license */ // Post author // @todo Fix this idiocracy if ($author = Model_User::find_user_light($post->author_id)) { $author_full = Model_User::find_user($author['id']); } // Viewer's post $my = $user && $author && $author['id'] == $user->id; // Topic author's post $owners = $author ? $author['id'] == $topic->author_id : $post->author_name == $topic->author_name; ?> <article id="post-<?php echo $post->id; ?> " class="post <?php echo $owners ? 'owner ' : '', $my ? 'my ' : '', Text::alternate('', 'alt'); ?> "> <section class="author grid2 first">
/** * Gets the currently logged in user from the session or null. * * @return Model_User */ public function get_user() { if (self::$user === false) { if ($user_id = $this->_session->get($this->_config['session_key'], null)) { self::$user = Model_User::find_user($user_id); } else { self::$user = null; } } return self::$user; }
/** * Get user id from data * * @static * @param mixed $user * @return integer */ public static function user_id($user) { if (is_int($user) || is_numeric($user)) { // Already got id return (int) $user; } else { if (is_array($user)) { // Got user array return (int) Arr::get($user, 'id'); } else { if ($user instanceof Model_User) { // Got user model return $user->id; } else { if (is_string($user)) { // Got user name $username = Text::clean($user); if (!($id = (int) Anqh::cache_get('user_uid_' . $username))) { if ($user = Model_User::find_user($user)) { $id = $user->id; Anqh::cache_set('user_uid_' . $username, $id, Date::DAY); } } return $id; } } } } return 0; }
/** * Update copyright info * * @return Model_Gallery */ public function update_copyright() { $copyrights = $authors = array(); // Load author ids foreach ($this->images() as $image) { if ($image->author_id) { $authors[$image->author_id] = ''; } } // Load usernames foreach ($authors as $author_id => $author) { if ($author = Model_User::find_user($author_id)) { $copyrights[$author->username_clean] = $author->username; } } ksort($copyrights); $this->copyright = implode(', ', $copyrights); return $this; }