Exemplo n.º 1
0
func PhotoUploadGET(w http.ResponseWriter, r *http.Request) {
	// Get session
	sess := session.Instance(r)

	// Get the user photos
	photos, err := model.PhotosByUserId(uint64(sess.Values["id"].(uint32)))
	if err != nil {
		log.Println(err)
	}

	verified := false

	for _, v := range photos {
		if v.Status_id == 1 {
			verified = true
			break
		}
	}

	// Only allow access to this page if verified
	if verified {
		// Display the view
		v := view.New(r)
		v.Name = "user_upload"
		v.Render(w)
	} else {
		Error404(w, r)
	}
}
Exemplo n.º 2
0
func isVerifiedPublic(r *http.Request, user_id uint64) bool {
	// Get the user photos
	photos, err := model.PhotosByUserId(user_id)
	if err != nil {
		log.Println(err)
	}

	verified := false

	for _, v := range photos {
		if v.Status_id == 1 && v.Initial == 0 {
			verified = true
			break
		}
	}

	return verified
}
Exemplo n.º 3
0
func isVerified(r *http.Request) bool {
	// Get session
	sess := session.Instance(r)

	// Get the user photos
	photos, err := model.PhotosByUserId(uint64(sess.Values["id"].(uint32)))
	if err != nil {
		log.Println(err)
	}

	verified := false

	for _, v := range photos {
		if v.Status_id == 1 {
			verified = true
			break
		}
	}

	return verified
}
Exemplo n.º 4
0
// Displays the default home page
func PhotoPOST(w http.ResponseWriter, r *http.Request) {
	// Get session
	sess := session.Instance(r)

	// Get the user photos
	photos, err := model.PhotosByUserId(uint64(sess.Values["id"].(uint32)))
	if err != nil {
		sess.AddFlash(view.Flash{"An error with the server occurred. Please try again later.", view.FlashError})
		sess.Save(r, w)
		Index(w, r)
		return
	}

	// Limit the number of photos
	if len(photos) >= photoLimit {
		sess.AddFlash(view.Flash{"You can only have a max of " + fmt.Sprintf("%v", photoLimit) + " photos. Delete old photos and then try again.", view.FlashError})
		sess.Save(r, w)
		Index(w, r)
		return
	}

	// File upload max size
	if r.ContentLength > 1000000*5 {
		sess.AddFlash(view.Flash{"Photo size is too large. Make sure it is under 5MB.", view.FlashError})
		sess.Save(r, w)
		Index(w, r)
		return
	}

	// Get the form photo
	file, _, err := r.FormFile("photo")

	if err != nil {
		sess.AddFlash(view.Flash{"Photo is missing.", view.FlashError})
		sess.Save(r, w)
		Index(w, r)
		return
	}

	defer file.Close()

	ok, filetype, _ := isSupported(file)

	// Is file supported
	if !ok {
		sess.AddFlash(view.Flash{"Photo type is not supported. Try to upload a JPG, GIF, or PNG.", view.FlashError})
		sess.Save(r, w)
		Index(w, r)
		return
	}

	// Get the photo size
	photo_info, err := photo.ImageDimensions(file)
	if err != nil {
		log.Println(err)
		sess.AddFlash(view.Flash{"Could not read the photo dimensions.", view.FlashError})
		sess.Save(r, w)
		Index(w, r)
		return

	}

	// OKCupid 400 x 400
	// ChristianMingle ?

	if photo_info.Width < 300 || photo_info.Height < 300 {
		sess.AddFlash(view.Flash{"Photo is too small. It must be atleast 300x300 pixels.", view.FlashError})
		sess.Save(r, w)
		Index(w, r)
		return
	}

	user_id := fmt.Sprint(sess.Values["id"])
	folder := photoPath + user_id

	// If folder does not exists
	if !fs.FolderExists(folder) {
		err = os.Mkdir(folder, 0777)
		if err != nil {
			log.Println("Unable to create the folder for writing. Check your write access privilege.", err)
			sess.AddFlash(view.Flash{"An error occurred on the server. Please try again later.", view.FlashError})
			sess.Save(r, w)
			Index(w, r)
			return
		}
	}

	filename := time.Now().Format("20060102150405")

	finalOut := folder + "/" + filename + ".jpg"

	if filetype == "image/gif" {
		img, err := photo.GIFToImage(file)
		if err != nil {
			log.Println(err)
			sess.AddFlash(view.Flash{"An error occurred on the server. Please try again later.", view.FlashError})
			sess.Save(r, w)
			Index(w, r)
			return
		}

		err = photo.ImageToJPGFile(img, finalOut)
	} else if filetype == "image/png" {
		img, err := photo.PNGToImage(file)
		if err != nil {
			log.Println(err)
			sess.AddFlash(view.Flash{"An error occurred on the server. Please try again later.", view.FlashError})
			sess.Save(r, w)
			Index(w, r)
			return
		}
		err = photo.ImageToJPGFile(img, finalOut)
	} else {
		err = photo.JPGToFile(file, finalOut)
	}

	if err != nil {
		log.Println("Error uploading file:", err)
		sess.AddFlash(view.Flash{"An error occurred on the server. Please try again later.", view.FlashError})
	} else {
		uid, err := strconv.ParseUint(user_id, 10, 32)
		if err != nil {
			log.Println(err)
			sess.AddFlash(view.Flash{"An error occurred on the server. Please try again later.", view.FlashError})
			sess.Save(r, w)
			Index(w, r)
			return
		}

		initial := false

		if strings.Contains(r.URL.Path, "initial") {
			initial = true
		}

		err = model.PhotoCreate(uid, filename, initial)
		if err != nil {
			log.Println(err)
			sess.AddFlash(view.Flash{"An error occurred on the server. Please try again later.", view.FlashError})
			sess.Save(r, w)
			Index(w, r)
			return
		}

		err = photo.FixRotation(finalOut)
		if err != nil {
			//log.Println("No rotation:", err, finalOut)
		} else {
			//log.Println("Rotation success", finalOut)
		}

		po, err := pushover.New()
		if err == pushover.ErrPushoverDisabled {
			// Nothing
		} else if err != nil {
			log.Println(err)
		} else {
			err = po.Message("User " + user_id + " added a new photo for verification. You can approve the photo here:\nhttps://verified.ninja/admin/user/" + user_id)
			if err != nil {
				log.Println(err)
			}
		}

		//log.Println("File uploaded successfully:", finalOut)
		sess.AddFlash(view.Flash{"Photo uploaded successfully.", view.FlashSuccess})
	}

	sess.Save(r, w)
	Index(w, r)
	return
}
Exemplo n.º 5
0
func InitialPhotoGET(w http.ResponseWriter, r *http.Request) {
	// Get session
	sess := session.Instance(r)

	user_id := uint64(sess.Values["id"].(uint32))

	demo, err := model.DemographicByUserId(user_id)
	if err != sql.ErrNoRows {
		//log.Println(err)
	}

	// Force the user to enter in demographic information
	if len(demo.Gender) < 1 {
		UserInformationGET(w, r)
		return
	}

	// If the user has no photos, show this page
	// If the user has only unverified photos, show the waiting screen

	// Get the user photos
	photos, err := model.PhotosByUserId(uint64(sess.Values["id"].(uint32)))
	if err != nil {
		log.Println(err)
	}

	verified_private := false
	unverified_private := false
	//rejected_private := false
	any_private := false

	for _, v := range photos {
		if v.Initial == 1 {
			if v.Status_id == 1 {
				verified_private = true
			} else if v.Status_id == 2 {
				unverified_private = true
			} else if v.Status_id == 3 {
				//rejected_private = true
			}
			any_private = true
		}
	}

	// Redirect to profile to handle caess where all private photos are rejected
	if len(photos) < 1 || verified_private || !any_private {
		// Get the user verification code
		token_info, err := model.UserTokenByUserId(user_id)
		if err == sql.ErrNoRows {
			token_info.Token = random.Generate(6)
			token_info.User_id = uint32(user_id)
			err = model.UserTokenCreate(user_id, token_info.Token)
		} else if err != nil {
			log.Println(err)
			Error500(w, r)
			return
		}

		// Display the view
		v := view.New(r)
		v.Name = "user_step1"
		v.Vars["user_token"] = token_info.Token
		v.Vars["first_name"] = sess.Values["first_name"]
		v.Render(w)
	} else if unverified_private {
		http.Redirect(w, r, "/profile", http.StatusFound)
	} else {
		//Error404(w, r)
		http.Redirect(w, r, "/profile", http.StatusFound)
	}
}
Exemplo n.º 6
0
// Displays the default home page
func AdminGET(w http.ResponseWriter, r *http.Request) {
	dirs, err := filepath.Glob(photoPath + "*")
	if err != nil {
		log.Println(err)
	}

	users := []User{}

	ds := string(os.PathSeparator)

	for _, v := range dirs {
		u := User{}
		idRaw := v[strings.LastIndex(v, ds)+1:]
		u.Id, err = strconv.Atoi(idRaw)
		if err != nil {
			log.Println(err)
			continue
		}

		info, err := model.UserNameById(u.Id)
		if err == sql.ErrNoRows {
			log.Println("User is not found in database:", u.Id)
			continue
		} else if err != nil {
			log.Println(err)
			continue
		}

		u.FirstName = info.First_name
		u.LastName = info.Last_name

		privateVerifiedCount := 0
		publicVerifiedCount := 0

		// Get the photo information
		user_id := strconv.Itoa(u.Id)
		imagesDB, err := model.PhotosByUserId(uint64(u.Id))
		if err != nil {
			log.Println(err)
			return
		}
		//images := []Image{}
		for _, val := range imagesDB {
			img := Image{}
			img.Name = val.Path
			if val.Status_id == 1 {
				u.VerifiedCount += 1

				if val.Initial == 1 {
					privateVerifiedCount += 1
				} else if val.Initial == 0 {
					publicVerifiedCount += 1
				}

			} else if val.Status_id == 2 {
				u.UnverifiedCount += 1
			}

			img.Path = "image/" + user_id + "/" + val.Path + ".jpg"

			img.Status_id = int(val.Status_id)
			img.Date = val.Updated_at.Format("Jan _2, 2006")
			img.Initial = int(val.Initial)
			u.Images = append(u.Images, img)
		}

		// Get the user verification code
		token_info, err := model.UserTokenByUserId(uint64(u.Id))
		if err == sql.ErrNoRows {
			log.Println(err)
			token_info.Token = "TOKEN IS MISSING"
		} else if err != nil {
			log.Println(err)
			token_info.Token = "TOKEN IS MISSING"
		}
		u.Token = token_info.Token

		// Get the username information
		sites, err := model.UserinfoByUserId(uint64(u.Id))
		if err != nil {
			log.Println(err)
			return
		}
		u.SiteCount = len(sites)

		u.Email = isVerifiedEmail(r, int64(u.Id))

		if u.SiteCount > 0 && privateVerifiedCount > 0 && publicVerifiedCount > 0 && u.Email {
			u.Ninja = true
		}

		users = append(users, u)
	}

	// Display the view
	v := view.New(r)
	v.Name = "admin"
	v.Vars["users"] = users
	v.Render(w)
}
Exemplo n.º 7
0
// Displays the default home page
func AdminAllGET(w http.ResponseWriter, r *http.Request) {
	dirs, err := filepath.Glob(photoPath + "*")
	if err != nil {
		log.Println(err)
	}

	users := []User{}

	ds := string(os.PathSeparator)

	for _, v := range dirs {
		u := User{}
		idRaw := v[strings.LastIndex(v, ds)+1:]
		u.Id, err = strconv.Atoi(idRaw)
		if err != nil {
			log.Println(err)
			continue
		}

		info, err := model.UserNameById(u.Id)
		if err == sql.ErrNoRows {
			log.Println("User is not found in database:", u.Id)
			continue
		} else if err != nil {
			log.Println(err)
			continue
		}

		u.FirstName = info.First_name
		u.LastName = info.Last_name

		/*files, err := filepath.Glob(photoPath + idRaw + "/*")
		if err != nil {
			log.Println(err)
			continue
		}

		for _, v := range files {
			i := Image{}
			i.Name = v[strings.LastIndex(v, ds)+1:]
			iid, _ := strconv.Atoi(strings.Replace(i.Name, `.jpg`, ``, -1))
			i.Id = iid
			i.Path = strings.Replace(v, `\`, `/`, -1)
			u.Images = append(u.Images, i)
		}*/

		// Get the photo information
		user_id := strconv.Itoa(u.Id)
		imagesDB, err := model.PhotosByUserId(uint64(u.Id))
		if err != nil {
			log.Println(err)
			return
		}
		//images := []Image{}
		for _, val := range imagesDB {
			img := Image{}
			img.Name = val.Path
			/*if val.Status_id == 1 {
				img.Path = "image/" + user_id + "/" + val.Path + ".jpg"
			} else {
				img.Path = photoPath + user_id + "/" + val.Path + ".jpg"
			}*/
			img.Path = "image/" + user_id + "/" + val.Path + ".jpg"

			img.Status_id = int(val.Status_id)
			img.Date = val.Updated_at.Format("Jan _2, 2006")
			img.Initial = int(val.Initial)
			u.Images = append(u.Images, img)
		}

		//uid := sess.Values["id"].(uint32)

		// Get the user verification code
		token_info, err := model.UserTokenByUserId(uint64(u.Id))
		if err == sql.ErrNoRows {
			log.Println(err)
			token_info.Token = "TOKEN IS MISSING"
		} else if err != nil {
			log.Println(err)
			token_info.Token = "TOKEN IS MISSING"
		}
		u.Token = token_info.Token
		users = append(users, u)
	}

	// Display the view
	v := view.New(r)
	v.Name = "admin_all"
	v.Vars["users"] = users
	v.Render(w)
}
Exemplo n.º 8
0
// Displays the default home page
func AdminUserGET(w http.ResponseWriter, r *http.Request) {
	var params = context.Get(r, "params").(httprouter.Params)
	userid := params.ByName("userid")
	user_id, _ := strconv.Atoi(userid)

	users := []User{}

	for _, v := range []int{user_id} {
		u := User{}
		u.Id = v

		info, err := model.UserNameById(u.Id)
		if err == sql.ErrNoRows {
			log.Println("User is not found in database:", u.Id)
			continue
		} else if err != nil {
			log.Println(err)
			continue
		}

		u.FirstName = info.First_name
		u.LastName = info.Last_name

		// Get the photo information
		user_id := strconv.Itoa(u.Id)
		imagesDB, err := model.PhotosByUserId(uint64(u.Id))
		if err != nil {
			log.Println(err)
			return
		}
		//images := []Image{}
		for _, val := range imagesDB {
			img := Image{}
			img.Name = val.Path
			img.Path = "image/" + user_id + "/" + val.Path + ".jpg"

			img.Status_id = int(val.Status_id)
			img.Date = val.Updated_at.Format("Jan _2, 2006")
			img.Initial = int(val.Initial)
			u.Images = append(u.Images, img)
		}

		// Get the user verification code
		token_info, err := model.UserTokenByUserId(uint64(u.Id))
		if err == sql.ErrNoRows {
			log.Println(err)
			token_info.Token = "TOKEN IS MISSING"
		} else if err != nil {
			log.Println(err)
			token_info.Token = "TOKEN IS MISSING"
		}
		u.Token = token_info.Token
		users = append(users, u)
	}

	// Display the view
	v := view.New(r)
	v.Name = "admin_all"
	v.Vars["users"] = users
	v.Render(w)
}
Exemplo n.º 9
0
func UserProfileGET(w http.ResponseWriter, r *http.Request) {
	// Get session
	sess := session.Instance(r)

	// Get the user photos
	photos, err := model.PhotosByUserId(uint64(sess.Values["id"].(uint32)))
	if err != nil {
		log.Println(err)
	}

	note := ""
	photo := ""
	status := uint8(0)
	date := time.Now()

	verified_private := false
	unverified_private := false
	rejected_private := false
	verified_public := false

	for _, v := range photos {
		if v.Initial == 1 {
			if v.Status_id == 1 {
				verified_private = true
			} else if v.Status_id == 2 {
				unverified_private = true
				note = v.Note
				photo = v.Path
				status = v.Status_id
				date = v.Updated_at
			} else if v.Status_id == 3 {
				rejected_private = true
				note = v.Note
				photo = v.Path
				status = v.Status_id
				date = v.Updated_at
			}
		} else {
			if v.Status_id == 1 {
				verified_public = true
			}
		}
	}

	user_id := strconv.Itoa(int(sess.Values["id"].(uint32)))

	// Display the view
	v := view.New(r)

	v.Vars["isNinja"] = false

	// If a private photo is verified, show the page
	if verified_private {
		v.Name = "user_profile"

		// Get the photo information
		imagesDB, err := model.PhotosByUserId(uint64(sess.Values["id"].(uint32)))
		if err != nil {
			log.Println(err)
			return
		}
		images := []Image{}
		for _, val := range imagesDB {
			img := Image{}
			img.Name = val.Path
			/*if val.Status_id == 1 {
				img.Path = "image/" + user_id + "/" + val.Path + ".jpg"
			} else {
				img.Path = photoPath + user_id + "/" + val.Path + ".jpg"
			}*/

			img.Path = "image/" + user_id + "/" + val.Path + ".jpg"

			img.Status_id = int(val.Status_id)
			img.Date = val.Updated_at.Format("Jan _2, 2006")
			img.Initial = int(val.Initial)
			img.Note = val.Note

			images = append(images, img)
		}
		v.Vars["images"] = images

		// Get the username information
		sites, err := model.UserinfoByUserId(uint64(sess.Values["id"].(uint32)))
		if err != nil {
			log.Println(err)
			return
		}
		for i, val := range sites {
			sites[i].Profile = strings.Replace(val.Profile, ":name", val.Username, -1)
		}
		v.Vars["sites"] = sites

		if len(sites) > 0 && verified_public {
			v.Vars["isNinja"] = true
		}

	} else {
		if unverified_private {
			// THIS NOTE MAY NOT BE FOR THE CORRECT PICTURE
			v.Vars["note"] = note
			//v.Vars["photo"] = photoPath + user_id + "/" + photo + ".jpg"
			v.Vars["photo"] = "image/" + user_id + "/" + photo + ".jpg"
			v.Vars["status_id"] = status
			v.Vars["date"] = date.Format("Jan _2, 2006")
			v.Vars["photo_id"] = photo
			v.Name = "user_unverified"
		} else if rejected_private {
			// THIS NOTE MAY NOT BE FOR THE CORRECT PICTURE
			v.Vars["note"] = note
			//v.Vars["photo"] = photoPath + user_id + "/" + photo + ".jpg"
			v.Vars["photo"] = "image/" + user_id + "/" + photo + ".jpg"
			v.Vars["status_id"] = status
			v.Vars["date"] = date.Format("Jan _2, 2006")
			v.Vars["photo_id"] = photo
			v.Name = "user_rejected"
		} else {
			http.Redirect(w, r, "/profile/initial", http.StatusFound)
			return
		}
	}

	v.Vars["first_name"] = sess.Values["first_name"]
	v.Render(w)
}
Exemplo n.º 10
0
func PublicUsernameGET(w http.ResponseWriter, r *http.Request) {
	// Get session
	sess := session.Instance(r)

	var params httprouter.Params
	params = context.Get(r, "params").(httprouter.Params)
	site := params.ByName("site")
	username := params.ByName("username")

	// Display the view
	v := view.New(r)

	v.Vars["isSelf"] = false
	v.Vars["verified_email"] = false

	user_info, err := model.UserByUsername(username, site)
	if err == sql.ErrNoRows {
		v.Vars["verified_private"] = false
		v.Vars["verified_public"] = false
		v.Vars["exists"] = false
	} else if err != nil {
		log.Println(err)
		Error500(w, r)
		return
	} else {

		v.Vars["verified_email"] = isVerifiedEmail(r, int64(user_info.Id))

		v.Vars["exists"] = true

		if sess.Values["id"] != nil {
			if sess.Values["id"] == user_info.Id {
				v.Vars["isSelf"] = true
			}
		}

		if isVerifiedPublic(r, uint64(user_info.Id)) && isVerifiedPrivate(r, uint64(user_info.Id)) {
			v.Vars["verified_public"] = true

			// Get the photo information
			//user_id := strconv.Itoa(int(sess.Values["id"].(uint32)))
			user_id_string := strconv.Itoa(int(user_info.Id))
			imagesDB, err := model.PhotosByUserId(uint64(user_info.Id))

			if err != nil {
				log.Println(err)
				return
			}
			images := []Image{}
			for _, val := range imagesDB {
				img := Image{}
				img.Name = val.Path
				/*if val.Status_id == 1 {
					img.Path = "image/" + user_id_string + "/" + val.Path + ".jpg"
				} else {
					img.Path = photoPath + user_id_string + "/" + val.Path + ".jpg"
				}*/

				img.Path = "image/" + user_id_string + "/" + val.Path + ".jpg"

				img.Status_id = int(val.Status_id)
				img.Date = val.Updated_at.Format("Jan _2, 2006")

				// Only allows verified images right now
				if val.Status_id == 1 && val.Initial == 0 {
					images = append(images, img)
				}
			}
			v.Vars["site"] = user_info.Site
			v.Vars["profile"] = strings.Replace(user_info.Profile, ":name", user_info.Username, -1)

			v.Vars["images"] = images

		} else if isVerifiedPrivate(r, uint64(user_info.Id)) {
			v.Vars["verified_private"] = true
		} else {
			v.Vars["verified_private"] = false
		}
	}

	v.Name = "public_username"
	v.Vars["username"] = username
	//v.Vars["site"] = user_info.Site
	//v.Vars["profile"] = user_info.Profile
	v.Vars["home"] = user_info.Home
	v.Render(w)
}
Exemplo n.º 11
0
func APIVerifyUserGET(w http.ResponseWriter, r *http.Request) {
	// Get session
	//sess := session.Instance(r)

	user_id := uint64(0)
	other_user_id := uint64(0)

	userkey := r.URL.Query().Get("userkey")
	token := r.URL.Query().Get("token")

	auth_info, err := model.ApiAuthenticationByKeys(userkey, token)
	if err == sql.ErrNoRows {
		Error401(w, r)
		return
	} else if err != nil {
		log.Println(err)
		Error500(w, r)
		return
	}

	// If the user is logged in
	/*if sess.Values["id"] != nil {
		user_id = uint64(sess.Values["id"].(uint32))
	}*/

	user_id = uint64(auth_info.User_id)

	var params httprouter.Params
	params = context.Get(r, "params").(httprouter.Params)
	site := params.ByName("site")
	username := params.ByName("username")

	vn := VerifiedNinja{}

	user_info, err := model.UserByUsername(username, site)
	if err == sql.ErrNoRows {
	} else if err != nil {
		log.Println(err)
	} else {

		other_user_id = uint64(user_info.Id)

		// Get the user photos
		photos, err := model.PhotosByUserId(uint64(user_info.Id))
		if err != nil {
			log.Println(err)
		}

		for _, v := range photos {
			if v.Initial == 1 {
				if v.Status_id == 1 {
					vn.PrivatePhotoVerified = true
				}
			} else {
				if v.Status_id == 1 {
					vn.PublicPhotoVerified = true
				}
			}
		}

		// If a private photo is verified, show the page
		if vn.PrivatePhotoVerified && vn.PublicPhotoVerified {

			// Get the username information
			sites, err := model.UserinfoByUserId(uint64(user_info.Id))
			if err != nil {
				log.Println(err)
			} else {
				for _, s := range sites {
					if strings.ToLower(s.Site) == strings.ToLower(site) {
						vn.RegisteredUsername = true
						vn.VerifiedNinja = true
						break
					}
				}
			}
		}
	}

	//log.Println("API Check - is Ninja?:", username, site, vn.VerifiedNinja)

	err = model.TrackRequestAPI(user_id, r, other_user_id, vn.VerifiedNinja)
	if err != nil {
		log.Println(err)
	}

	js, err := json.Marshal(vn)
	if err != nil {
		log.Println(err)
		Error500(w, r)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.Write(js)
}