Esempio n. 1
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)
}
Esempio n. 2
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)
}
Esempio n. 3
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)
}