Exemplo n.º 1
0
	function saveAvatar()
	{
		//echo "i m here";
		$mainframe= JFactory::getApplication(); 
		$user = &JFactory::getUser();
		$userId = $user->id;
		$itemId =  AwdwallHelperUser::getComItemId();
		$db =& JFactory::getDBO();
		if((int)$userId > 0){
			require_once (JPATH_COMPONENT . DS . 'models' . DS . 'wall.php');			
			$arrExt 	= explode(',', AwdwallHelperUser::getImageExt());
			$image 	= JRequest::getVar('avatar', null, 'files', 'array');
			$gender = JRequest::getInt('gender', null);
			$birthday = JRequest::getString('birthday', null);
			$aboutme  = JRequest::getString('aboutme', null);
				$max_upload = (int)(ini_get('upload_max_filesize'));
				$inputsize=$image['size'];
				$inputsize=AwdwallHelperUser::mbFormat($inputsize,1);
				//echo $max_upload.'<br>'.$inputsize;
				//exit;
				if($inputsize > $max_upload )
				{
					$msg=JText::_('UPLOAD FILE SIZE');
					$mainframe->Redirect(JRoute::_('index.php?option=com_awdwall&task=uploadavatar&Itemid='.$itemId, false), JText::_('UPLOAD FILE SIZE'));
				}
			if($image['name'] != ''){
				jimport('joomla.filesystem.file');
				jimport('joomla.filesystem.folder');
				$time = time();
				
				//Clean up filename to get rid of strange characters like spaces etc
				$fileName = $time . '_' . JFile::makeSafe($image['name']);
				$fileName = str_replace(' ', '_', $fileName);
				$src 	= $image['tmp_name'];
				$dest 	= 'images' . DS . 'wallavatar' . DS . $userId . DS . 'original' . DS . $fileName; 
				
				if(in_array(strtolower(JFile::getExt($fileName)), $arrExt)){
				   require_once (JPATH_COMPONENT . DS . 'libraries' . DS . 'class.upload.php');	
				   $handle = new upload($_FILES['avatar']);
				   if ($handle->uploaded) {
				   
						$filename = preg_replace("/\\.[^.\\s]{3,4}$/", "", $fileName);
						$filename1 = preg_replace("/\\.[^.\\s]{3,4}$/", "", $fileName);
						$folder='images' . DS . 'wallavatar' . DS . $userId . DS . 'original';
						processthumb($handle,$filename,$folder);
						
						$handle->image_resize          = true;
						$handle->image_ratio_y        = true;
						$handle->image_x               = 133;
						
						$folder='images' . DS . 'wallavatar' . DS . $userId . DS . 'thumb';
						$filename='tn133'.$filename1;
						processthumb($handle,$filename,$folder);
						
						$handle->image_resize          = true;
						$handle->image_ratio_crop      = true;
						$handle->image_x               = 51;
						$handle->image_y               = 51;
						$folder='images' . DS . 'wallavatar' . DS . $userId . DS . 'thumb';
						$filename='tn51'.$filename1;
						processthumb($handle,$filename,$folder);
						
						$handle->image_resize          = true;
						$handle->image_ratio_crop      = true;
						$handle->image_x               = 40;
						$handle->image_y               = 40;
						$folder='images' . DS . 'wallavatar' . DS . $userId . DS . 'thumb';
						$filename='tn40'.$filename1;
						processthumb($handle,$filename,$folder);
						
						$handle->image_resize          = true;
						$handle->image_ratio_crop      = true;
						$handle->image_x               = 32;
						$handle->image_y               = 32;
						$folder='images' . DS . 'wallavatar' . DS . $userId . DS . 'thumb';
						$filename='tn32'.$filename1;
						processthumb($handle,$filename,$folder);
						
						$handle->image_resize          = true;
						$handle->image_ratio_crop      = true;
						$handle->image_x               = 19;
						$handle->image_y               = 19;
						$folder='images' . DS . 'wallavatar' . DS . $userId . DS . 'thumb';
						$filename='tn19'.$filename1;
						processthumb($handle,$filename,$folder);
						
				   }
					
					
					
					
				}else {
					//Redirect and notify user file is not right extension
					$this->setRedirect(JRoute::_('index.php?option=com_awdwall&&view=awdwall&layout=main&Itemid=' . $itemId, false));
				}
			}
			
			// check if user is exist or not
			$sql = 'SELECT user_id FROM #__awd_wall_users WHERE user_id = ' . (int)$userId;
			$db->setQuery($sql);
			$result = $db->loadResult();
			if($result){
				// update into wall table first
				if($fileName != '')
					$sql = 'UPDATE #__awd_wall_users SET avatar = "' . $fileName . '", gender = ' . $gender . ', birthday = "' . $birthday . '" WHERE user_id = ' . (int)$userId;
				else
					$sql = 'UPDATE #__awd_wall_users SET gender = ' . $gender . ', birthday = "' . $birthday . '" WHERE user_id = ' . (int)$userId;
				$db->setQuery($sql);
				$db->query();
			}else{
				// save into wall table first
				$sql = 'INSERT INTO #__awd_wall_users (user_id, avatar, gender, birthday) VALUES("' . $userId . '","'.$fileName . '", "' . $gender . '", "' . $birthday . '")';
				$db->setQuery($sql);
				$db->query();
			}
			
			
			if($fileName)
			{
			// store into wall
			$Itemid = AwdwallHelperUser::getComItemId();
			$wall 				=& JTable::getInstance('Wall', 'Table');						
			$wall->user_id		= $userId;
			$wall->type			= 'text';
			$wall->commenter_id	= $userId;
			$wall->user_name	= '';
			$wall->avatar		= '';
			$wall->message		= JText::_('UPDATED AVATAR');
			$wall->reply		= 0;
			$wall->is_read		= 0;
			$wall->is_pm		= 0;
			$wall->is_reply		= 0;
			$wall->posted_id	= NULL;
			$wall->wall_date	= time();
			// store wall to database
				if (!$wall->store()){				
		
					$this->setRedirect(JRoute::_('index.php?option=com_awdwall&&view=awdwall&layout=main&Itemid='.$itemId , false ), JText::_('Post Failed'));
				}
			}
			else
			{
				// store into wall
				$Itemid = AwdwallHelperUser::getComItemId();
				$wall 				=& JTable::getInstance('Wall', 'Table');						
				$wall->user_id		= $userId;
				$wall->type			= 'text';
				$wall->commenter_id	= $userId;
				$wall->user_name	= '';
				$wall->avatar		= '';
				$wall->message		= JText::_('UPDATED PROFILE');
				$wall->reply		= 0;
				$wall->is_read		= 0;
				$wall->is_pm		= 0;
				$wall->is_reply		= 0;
				$wall->posted_id	= NULL;
				$wall->wall_date	= time();
	
				// store wall to database
					if (!$wall->store()){				
			
						$this->setRedirect(JRoute::_('index.php?option=com_awdwall&&view=awdwall&layout=main&Itemid='.$itemId , false ), JText::_('Post Failed'));
					}
			}
		}else{
			$this->setRedirect(JRoute::_('index.php?option=com_awdwall&&view=awdwall&layout=main&Itemid=' . $itemId, false));
		}
			$this->setRedirect(JRoute::_('index.php?option=com_awdwall&&view=awdwall&layout=mywall&Itemid=' . $itemId, false));
	}