2012-06-13 17:59:22 +00:00
|
|
|
package zall;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
|
import java.sql.SQLException;
|
2012-06-14 20:39:22 +00:00
|
|
|
import java.util.HashMap;
|
2012-06-13 17:59:22 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.logging.Level;
|
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
2012-06-14 20:39:22 +00:00
|
|
|
import javax.servlet.ServletConfig;
|
2012-06-13 17:59:22 +00:00
|
|
|
import javax.servlet.ServletException;
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
|
|
2012-06-14 20:39:22 +00:00
|
|
|
import zall.action.*;
|
|
|
|
|
import zall.action.media.*;
|
|
|
|
|
import zall.action.user.*;
|
|
|
|
|
import zall.bean.*;
|
2012-06-13 17:59:22 +00:00
|
|
|
import zall.util.Email;
|
|
|
|
|
import zall.util.Email.ContentType;
|
|
|
|
|
import zall.util.msg.UserMessage;
|
|
|
|
|
import zall.util.msg.UserMessage.MessageType;
|
|
|
|
|
import zutil.db.DBConnection;
|
|
|
|
|
import zutil.log.LogUtil;
|
|
|
|
|
|
|
|
|
|
public class ZalleryAjax extends HttpServlet{
|
|
|
|
|
public static final Logger logger = LogUtil.getLogger();
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
2012-06-14 20:39:22 +00:00
|
|
|
private HashMap<String,ZalleryAction> actions;
|
|
|
|
|
|
|
|
|
|
public void init(ServletConfig config) throws ServletException {
|
|
|
|
|
super.init(config);
|
|
|
|
|
|
|
|
|
|
// General
|
|
|
|
|
registerAction(new LoginAction());
|
|
|
|
|
registerAction(new RegisterAction());
|
|
|
|
|
|
|
|
|
|
// User Actions
|
|
|
|
|
registerAction(new ModifyUserAction());
|
|
|
|
|
registerAction(new ModifyUserStatusAction());
|
|
|
|
|
registerAction(new RemoveUserAction());
|
|
|
|
|
registerAction(new SendVerificationEmailAction());
|
|
|
|
|
registerAction(new VerifyEmailAction());
|
|
|
|
|
|
|
|
|
|
// Media Actions
|
|
|
|
|
registerAction(new CommentAction());
|
|
|
|
|
registerAction(new CreateFolderAction());
|
|
|
|
|
registerAction(new ModifyMediaAction());
|
|
|
|
|
registerAction(new RemoveFolderAction());
|
|
|
|
|
registerAction(new RemoveMediaAction());
|
|
|
|
|
registerAction(new TogglePrivateAction());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void registerAction(ZalleryAction action){
|
|
|
|
|
if(actions == null)
|
|
|
|
|
actions = new HashMap<String,ZalleryAction>();
|
|
|
|
|
actions.put(action.getActionId().toLowerCase(), action);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException{
|
2012-06-13 17:59:22 +00:00
|
|
|
try {
|
|
|
|
|
doGet(request, response, response.getWriter());
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new ServletException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-14 20:39:22 +00:00
|
|
|
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException{
|
2012-06-13 17:59:22 +00:00
|
|
|
doGet(request, response, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param out is the PrintStream that will be used, no output will be generated if it is null
|
|
|
|
|
*/
|
2012-06-14 20:39:22 +00:00
|
|
|
public void doGet(HttpServletRequest request, HttpServletResponse response, PrintWriter out) throws ServletException{
|
2012-06-13 17:59:22 +00:00
|
|
|
DBConnection db = null;
|
|
|
|
|
try {
|
2012-06-14 20:39:22 +00:00
|
|
|
String actionStr = request.getParameter("action").toLowerCase();
|
2012-06-13 17:59:22 +00:00
|
|
|
HttpSession session = request.getSession();
|
|
|
|
|
User user = (User) session.getAttribute("user");
|
|
|
|
|
db = Zallery.getDB();
|
|
|
|
|
UserMessage msgs = UserMessage.getUserMessage(session);
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 20:39:22 +00:00
|
|
|
ZalleryAction action = actions.get( actionStr );
|
|
|
|
|
if( action != null ){
|
|
|
|
|
if( (action.requireUser() && user != null) || !action.requireUser() ){
|
|
|
|
|
action.handleRequest(db, request, response, session, out, user, msgs);
|
2012-06-13 20:05:58 +00:00
|
|
|
}
|
2012-06-14 20:39:22 +00:00
|
|
|
else{
|
|
|
|
|
// Unauthorized
|
|
|
|
|
if( out != null ){
|
|
|
|
|
out.print( "{\"error\":\"Unauthorized user!\"}" );
|
|
|
|
|
response.setStatus( 401 );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
msgs.add(MessageType.ERROR, "Unauthorized user!");
|
|
|
|
|
logger.severe("Unauthorized user!");
|
2012-06-13 17:59:22 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
// Unauthorized
|
|
|
|
|
if( out != null ){
|
2012-06-14 20:39:22 +00:00
|
|
|
out.print( "{\"error\":\"Unknown action!\"}" );
|
|
|
|
|
response.setStatus( 404 );
|
2012-06-13 17:59:22 +00:00
|
|
|
}
|
|
|
|
|
else
|
2012-06-14 20:39:22 +00:00
|
|
|
msgs.add(MessageType.ERROR, "Unknown action: '"+actionStr+"'!");
|
|
|
|
|
logger.severe("Unknown action: '"+actionStr+"'!");
|
2012-06-13 17:59:22 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
if( out != null ){
|
|
|
|
|
out.println("{\"error\":\""+e.getMessage().replaceAll("\"", "\\\"")+"\"}");
|
|
|
|
|
logger.log(Level.SEVERE, "Exception in ajax page!", e);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw new ServletException(e);
|
|
|
|
|
} finally{
|
|
|
|
|
if(db != null) db.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-06-14 20:39:22 +00:00
|
|
|
|
|
|
|
|
|
2012-06-13 17:59:22 +00:00
|
|
|
public static void sendEmailVerification(User user) throws IOException{
|
|
|
|
|
// Email
|
|
|
|
|
Email smtpEmail = new Email("admin@koc.se", user.getEmail());
|
|
|
|
|
smtpEmail.setNiceFrom("Koc.se Admin");
|
|
|
|
|
smtpEmail.setSubject("Registration at "+Zallery.getWebsiteName());
|
|
|
|
|
smtpEmail.setContentType(ContentType.HTML);
|
|
|
|
|
smtpEmail.setMessage("You receive this message because you have requested an account" +
|
2012-06-13 20:05:58 +00:00
|
|
|
"<br>at "+Zallery.getWebsiteName()+". Please click the link to verify your email address: " +
|
2012-06-13 17:59:22 +00:00
|
|
|
"<p><a href='"+Zallery.getWebsiteURL()+"?action=verfemail&id="+user.getId()+"&hash="+user.getEmailVerificationHash()+"'>"+Zallery.getWebsiteURL()+"?action=verfemail&id="+user.getId()+"&hash="+user.getEmailVerificationHash()+"</a>" +
|
|
|
|
|
"<p> You will have to wait for an admin to activate your account after you have verified your email.");
|
|
|
|
|
smtpEmail.send();
|
|
|
|
|
}
|
2012-06-14 20:39:22 +00:00
|
|
|
|
2012-06-13 17:59:22 +00:00
|
|
|
public static void sendEmailNewUserToAdmin(User newuser, DBConnection db) throws SQLException, IOException{
|
|
|
|
|
// Email the admin about new user
|
|
|
|
|
Email email = new Email("admin@koc.se", "admin@koc.se");
|
|
|
|
|
email.setNiceFrom("Koc.se Admin");
|
|
|
|
|
email.setSubject("New user activation request at "+Zallery.getWebsiteName());
|
|
|
|
|
email.setContentType(ContentType.HTML);
|
|
|
|
|
email.setMessage("A new user has registered for an account at " +
|
|
|
|
|
"<a href='"+Zallery.getWebsiteURL()+"'>"+Zallery.getWebsiteName()+"</a>:" +
|
|
|
|
|
"<p>Email: <b>" + newuser.getEmail() + "</b>" +
|
|
|
|
|
"<br>Name: <b>" + newuser.getName() + "</b>" +
|
|
|
|
|
"<br>Facebook: <a href='http://www.facebook.com/profile.php?id="+newuser.getFacebookUid()+"'>"+newuser.getFacebookUid()+"</a>");
|
|
|
|
|
List<User> admins = User.loadSuperUsers(db);
|
|
|
|
|
for(User admin : admins){
|
|
|
|
|
if( admin.isEmailVerified() ){
|
|
|
|
|
email.setTo( admin.getEmail() );
|
|
|
|
|
email.send();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|