Permalink
Browse files

Added Avatar upload feature

  • Loading branch information...
1 parent dcc8a7f commit 702ec78a74927a95f0fd4fe6d0beeaf5ff2a8d9a @snytkine snytkine committed Apr 18, 2011
View
@@ -105,8 +105,10 @@ SKIP_CACHE = false
COOKIE_DOMAIN = null
;
;system path to temporary directory. Must we writable to php
+; if not sure - leave blank to use system's default temporary directory
+;
;
-TEMP_DIR = /tmp
+TEMP_DIR =
;
;Some configuration constants
;Leave black if you host images, css and js
@@ -120,6 +122,10 @@ JS_SITE = ""
AVATAR_IMG_SITE = ""
;size of square avatar in pixels
AVATAR_SQUARE_SIZE= 40
+;maximum allowed file size of
+; uploaded avatar in bytes.
+; default 1000000 means 1MB
+MAX_AVATAR_UPLOAD_SIZE = 1024000
;
;Name of GeoIP file - must be in the LAMPCMS_DIR (root dir of program)
;Download it from maxmind.com
View
@@ -45,3 +45,4 @@ RewriteRule ^/aa/([0-9]+)/([a-f0-9]+)$ /index.php?a=activate&eid=$1&hash=$2
RewriteRule ^/([a-zA-Z\-]+)/$ /index.php?a=$1
RewriteRule ^/search/(m|r)/(.*)/page([0-9]+)\.html$ /index.php?a=search&ord=$1&q=$2&pageID=$3
RewriteRule ^/tab/(a|q)/([0-9]+)/([a-zA-Z]+)/page([0-9]+)\.html$ /index.php?a=userinfotab&tab=$1&uid=$2&sort=$3&pageID=$4 [L]
+RewriteRule ^/editprofile/([0-9]+) /index.php?a=editprofile&uid=$1 [L]
View
@@ -12,6 +12,7 @@ view_question = A
view_answers = A
view_comments = A
login = A
+read = A
;
;unregistered on not logged in user
@@ -76,6 +77,7 @@ unban_user = A
close_question = A
edit_any_comment = A
+
;
;can do most of the things
@@ -99,7 +101,7 @@ ban_user = A
shred_user = A
change_user_role = A
make_sticky = A
-
+edit_any_profile = A
;
;user caught spamming
@@ -0,0 +1,171 @@
+<?php
+/**
+ *
+ * License, TERMS and CONDITIONS
+ *
+ * This software is lisensed under the GNU LESSER GENERAL PUBLIC LICENSE (LGPL) version 3
+ * Please read the license here : http://www.gnu.org/licenses/lgpl-3.0.txt
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * ATTRIBUTION REQUIRED
+ * 4. All web pages generated by the use of this software, or at least
+ * the page that lists the recent questions (usually home page) must include
+ * a link to the http://www.lampcms.com and text of the link must indicate that
+ * the website\'s Questions/Answers functionality is powered by lampcms.com
+ * An example of acceptable link would be "Powered by <a href="http://www.lampcms.com">LampCMS</a>"
+ * The location of the link is not important, it can be in the footer of the page
+ * but it must not be hidden by style attibutes
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This product includes GeoLite data created by MaxMind,
+ * available from http://www.maxmind.com/
+ *
+ *
+ * @author Dmitri Snytkine <cms@lampcms.com>
+ * @copyright 2005-2011 (or current year) ExamNotes.net inc.
+ * @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU LESSER GENERAL PUBLIC LICENSE (LGPL) version 3
+ * @link http://www.lampcms.com Lampcms.com project
+ * @version Release: @package_version@
+ *
+ *
+ */
+
+
+namespace Lampcms;
+use Lampcms\FS\Path;
+
+class AvatarParser extends LampcmsObject
+{
+
+ public function __construct(Registry $o){
+ $this->oRegistry = $o;
+ }
+
+
+ /**
+ * Parse the avatar file in $tempPath,
+ * by creating small square image from it,
+ * save into file system and then add path to new avatar
+ * in User object as 'avatar' element
+ *
+ *
+ * @param User $oUser
+ * @param unknown_type $tempPath
+ * @throws \Lampcms\Exception
+ *
+ * @return object $this
+ */
+ public function addAvatar(User $oUser, $tempPath){
+
+ d('$tempPath: '.$tempPath);
+ if(empty($tempPath)){
+ d('no avatar to add');
+ return $this;
+ }
+
+ $size = $this->oRegistry->Ini->AVATAR_SQUARE_SIZE;
+
+ $avatarDir = LAMPCMS_DATA_DIR.'img'.DS.'avatar'.DS.'sqr'.DS;
+ d('$avatarDir: '.$avatarDir);
+
+ $savePath = Path::prepare($oUser->getUid(), $avatarDir);
+ d('$savePath: '.$savePath);
+
+ /**
+ * Create avatar and save it
+ * with compression level of 80% (small compression)
+ */
+ try{
+ $ImgParser = \Lampcms\Image\Editor::factory($this->oRegistry)
+ ->loadImage($tempPath)
+ ->makeSquare($size);
+ $savePath .= $ImgParser->getExtension();
+ $ImgParser->save($avatarDir.$savePath, null, 80);
+ d('avatar saved to '.$savePath);
+ } catch(\Lampcms\ImageException $e){
+ e('ImageException caught in: '.$e->getFile().' on line: '.$e->getLine().' error: '.$e->getMessage());
+ throw new \Lampcms\Exception('Unable to process your avatar image at this time');
+ }
+
+ /**
+ * Now remove tempPath file
+ */
+ @\unlink($tempPath);
+
+ /**
+ * Now add the path to avatar
+ * to user object
+ * save() is not invoked on User object here!
+ * Either rely on auto-save (may not work in case User is
+ * actually the Viewer object) or call save()
+ * from a function that invoked this method
+ */
+ $oUser['avatar'] = $savePath;
+
+ return $this;
+ }
+
+
+ /**
+ * Given an path to avatar remove it from User object
+ * if it looks like local avatar also remove it from
+ * the file system
+ *
+ * @todo unfinished
+ *
+ * @param object $oUser object of type User
+ * @param string $src path to avatar
+ *
+ * @return object $this
+ */
+ public function removeAvatar(User $oUser, $src){
+
+ if(0 === \strncmp($src, 'http', 4)){
+ /**
+ * External avatar, just remove from User object and done
+ */
+ $external = $oUser['avatar_external'];
+ if($src == $external){
+ $oUser['avatar_external'] = null;
+ }
+
+ /**
+ * What to do if user wants to remove gravatar image?
+ * We can mark nogravatar = true in user object
+ * and then it will not use gravatar ever again
+ * there will not be any way to undo it...
+ */
+ $oUser['noavatar'] = true;
+
+ return $this;
+ }
+
+ /**
+ * If this was not external avatar then must
+ * remove from File System and
+ * then remove from User object
+ */
+
+
+ return $this;
+ }
+}
View
@@ -266,8 +266,7 @@ public function saveResourceLocation($resourceId = '', $ip = '', array $arrExtra
* which would mean a user does not have appropriate
* access privileges
*/
- public function checkAccessPermission($privilege = null, RoleInterface $role = null, $resource = null)
- {
+ public function checkAccessPermission($privilege = null, RoleInterface $role = null, $resource = null){
d('$privilege: '.$privilege.' '.var_export($privilege, true));
@@ -58,52 +58,132 @@
class Editprofile extends WebPage
{
-
+ /**
+ * Pre-check to deny non-logged in user
+ * access to this page
+ *
+ * @var bool
+ */
protected $membersOnly = true;
+
+ /**
+ *
+ * Viewer must have edit_profile
+ * permission to access this page
+ * @var string
+ */
protected $permission = 'edit_profile';
+
+ /**
+ * $layoutID 1 means no side-column on page
+ *
+ * @var int
+ */
protected $layoutID = 1;
+
+ /**
+ * Form object
+ *
+ * @var object of type \Lampcms\Forms\Form
+ */
protected $oForm;
+ /**
+ * User object of user whose profile
+ * is being edited
+ *
+ * @var object of type User
+ */
+ protected $oUser;
+
+
protected function main(){
+ $this->getUser();
$this->oForm = new \Lampcms\Forms\Profile($this->oRegistry);
$this->oForm->formTitle = $this->aPageVars['title'] = 'Edit profile';
if($this->oForm->isSubmitted() && $this->oForm->validate()){
$this->oRegistry->Dispatcher->post($this->oForm, 'onBeforeProfileUpdate');
$this->saveProfile();
$this->oRegistry->Dispatcher->post($this->oForm, 'onProfileUpdate');
- $this->aPageVars['body'] = \tplProfileSuccess::parse(array('Profile has been updated', $this->oRegistry->Viewer->getProfileUrl(), 'View you new profile'), false);
+ $this->aPageVars['body'] = \tplProfileSuccess::parse(array('Profile has been updated', $this->oUser->getProfileUrl(), 'View the new profile'), false);
} else {
$this->setForm();
$this->aPageVars['body'] = $this->oForm->getForm();
}
}
+ /**
+ * Create $this->oUser User object for user whose
+ * profile is being edited
+ *
+ * @todo unfinished. IT will be possible to
+ * edit user other than Viewer when Viewer has
+ * permission to edit_other_profile
+ * For now this is a Viewe object
+ *
+ * @return object $this
+ */
+ protected function getUser(){
+ $uid = $this->oRequest->get('uid', 'i', null);
+ if($uid && ($uid !== $this->oRegistry->Viewer->getUid())){
+ /**
+ * This is edit profile for another user
+ * check Viewer permission here
+ */
+ $this->checkAccessPermission('edit_any_profile');
+ $this->oUser = \Lampcms\User::factory($this->oRegistry)->by_id($uid);
+ } else {
+ $this->oUser = $this->oRegistry->Viewer;
+ }
+
+ return $this;
+ }
+
+
/**
* Populate form elements with
* values from current user profile
*
* @return object $this
*/
protected function setForm(){
- $Viewer = $this->oRegistry->Viewer;
- $this->oForm->fn = $Viewer['fn'];
- $this->oForm->mn = $Viewer['mn'];
- $this->oForm->ln = $Viewer['ln'];
+ $this->oForm->username = $this->oUser['username'];
+ $this->oForm->usernameLabel = 'Username';
+ $this->oForm->fn = $this->oUser['fn'];
+ $this->oForm->mn = $this->oUser['mn'];
+ $this->oForm->ln = $this->oUser['ln'];
$this->oForm->gender = $this->getGenderOptions();
- $this->oForm->dob = $Viewer['dob'];
+ $this->oForm->dob = $this->oUser['dob'];
$this->oForm->country = $this->getCountryOptions();
- $this->oForm->state = $Viewer['state'];
- $this->oForm->city = $Viewer['city'];
- $this->oForm->url = $Viewer['url'];
- $this->oForm->zip = $Viewer['zip'];
- $this->oForm->description = $Viewer['description'];
+ $this->oForm->state = $this->oUser['state'];
+ $this->oForm->city = $this->oUser['city'];
+ $this->oForm->url = $this->oUser['url'];
+ $this->oForm->zip = $this->oUser['zip'];
+ $this->oForm->description = $this->oUser['description'];
+ $this->oForm->avatarSrc = $this->oUser->getAvatarSrc();
+ $this->oForm->width = $this->oRegistry->Ini->AVATAR_SQUARE_SIZE;
+ $this->oForm->uid = $this->oUser->getUid();
+ $this->oForm->maxAvatarSize = $this->oRegistry->Ini->MAX_AVATAR_UPLOAD_SIZE;
+ /**
+ * @todo translate string
+ */
+ $this->oForm->avatarTos = sprintf('Upload Image. Maximum size of %sMb<br><span class="smaller">By uploading a file you certify that you have the right to distribute this picture and that it does not violate the Terms of Service.</span>', floor($this->oRegistry->Ini->MAX_AVATAR_UPLOAD_SIZE / 1000000) );
+
+ /**
+ * Add ' hide' class to avatar upload
+ * if php does not have gd of support for jpeg
+ * inside gd
+ */
+ if(!\extension_loaded('gd') || !\function_exists('imagecreatefromjpeg')){
+ $this->oForm->hideAvatar = ' hide';
+ }
return $this;
}
@@ -116,7 +196,8 @@ protected function setForm(){
* @return object $this
*/
protected function saveProfile(){
- ProfileParser::factory($this->oRegistry)->save($this->oRegistry->Viewer, new SubmittedProfileWWW($this->oForm));
+ ProfileParser::factory($this->oRegistry)->save($this->oUser, new SubmittedProfileWWW($this->oForm));
+
/**
* Should unset 'welcome' from session
* because it contains user display name in the
Oops, something went wrong.

0 comments on commit 702ec78

Please sign in to comment.