Ejemplo n.º 1
0
def profile(username):
    if (username.lower() == "unendingpattern") or (username.lower()
                                                   == "unswp"):
        identibooru.flash("no such profile exists", "danger")
        return identibooru.redirect(identibooru.url_for('index'))
    parsed_bio = ""
    user = Users.query.filter_by(username=username).first()
    if user:
        tags = Tags.query.filter_by(uid=user.uid).order_by(Tags.tag).all()
        opts = UserOpts.query.filter_by(uid=user.uid).first()
        opts.views = opts.views + 1
        identibooru.db.session.commit()
        tag_list = render_tag_list(html=True,
                                   tags=tags,
                                   is_profile=get_user_options(user.username),
                                   is_map=False)
        return identibooru.render_template(
            'profile.html',
            title=user.username + ' - profile',
            user=user,
            tags=tags,
            user_options=get_user_options(username),
            tag_list=tag_list)
    identibooru.flash("no such profile exists", "danger")
    return identibooru.redirect(identibooru.url_for('index'))
Ejemplo n.º 2
0
def function_login():
    if get_user_options(identibooru.current_user.get_username())['rank'] != 0:
        return identibooru.redirect(identibooru.url_for('index'))
    email = identibooru.request.form['email']

    # this might stop automated requests
    if email != "":
        identibooru.app.logger.info(
            f'potential bot: %s entered an email address when email addresses aren\'t used',
            email)
        return "error: email address entered"

    username = identibooru.request.form['username']
    password = identibooru.request.form['password']

    user = Users.query.filter_by(username=username).first()
    if user:
        validity = check_password_hash(user.password, password)
        if validity:
            user.is_authenticated = True
            identibooru.login_user(user)
            if identibooru.current_user.is_authenticated:
                return identibooru.redirect("/" + user.username + "/edit")
    identibooru.flash("authentication failed", "danger")
    return identibooru.redirect(identibooru.url_for('account_login'))
Ejemplo n.º 3
0
def account_register():
    if get_user_options(identibooru.current_user.get_username())['rank'] != 0:
        return identibooru.redirect(identibooru.url_for('index'))
    if identibooru.disable_registration:
        identibooru.flash("registration is currently disabled", "danger")
        return identibooru.redirect(identibooru.url_for('index'))
    return identibooru.render_template('account-register.html',
                                       title='register')
Ejemplo n.º 4
0
def search():
    terms = identibooru.request.args.get('t', False)
    confirm_adult = identibooru.request.args.get('confirm_adult', False)
    search_tags = []
    remove_tags = []

    relevant_tags = []
    relevant_tag_list = []
    if not terms:
        identibooru.flash("no search terms specified", "danger")
        return identibooru.redirect(identibooru.url_for('index'))
    if identibooru.disable_search and get_user_options(identibooru.current_user.get_username())['rank'] != 99:
        identibooru.flash("not enough tags to form search results yet, please contribute by registering and tagging yourself!", "success")
        return identibooru.redirect(identibooru.url_for('index'))

    if " " in terms:
        terms_list = terms.split(" ")
    else:
        terms_list = [terms]
    if get_user_options(identibooru.current_user.get_username())['adult'] == 0 and not confirm_adult:
        adult_tags = ["-rating:explicit", "-rating:questionable"]
        fullpath = identibooru.request.full_path

        for adult_tag in adult_tags:
            if not adult_tag in terms_list:
                terms_list.append(adult_tag)
    
    tags = Tags.query.all()
    for term in terms_list:
        for tag in tags:
            if fnmatch.fnmatch(tag.tag, term):
                search_tags.append(tag)
        if term[0] is "-":
                remove_tags.append(term.strip("-"))

    
    search_results = render_search_results(html=True, search_tags=search_tags, remove_tags=remove_tags, users_class=Users)

    for result in search_results.values():
        relevant_tags.append(Tags.query.filter_by(uid=result['user'].uid).all())
            
    tags = Tags.query.all()
    for term in terms_list:
        for tag in tags:
            if fnmatch.fnmatch(tag.tag, term):
                relevant_tag_list.append(tag)
    for tag in list(itertools.chain(*relevant_tags)):
        relevant_tag_list.append(tag.tag)

    tag_list = render_tag_list(html=True, tags=relevant_tag_list, is_profile=False, is_map=False, remove_tags=remove_tags)
    return identibooru.render_template('search.html', title=terms + ' - search', results=search_results, tags=tag_list, keywords=terms)
Ejemplo n.º 5
0
def account_settings(username):
    if (username
            == identibooru.current_user.get_username()) or (get_user_options(
                identibooru.current_user.get_username())['rank'] == 99):
        user = Users.query.filter_by(username=username).first()
        if not user:
            identibooru.flash("no such user exists", "danger")
            return identibooru.redirect(identibooru.url_for('index'))
        return identibooru.render_template(
            'account-settings.html',
            title=user.username + ' - account settings',
            user=user,
            user_options=get_user_options(username))
    identibooru.flash("authentication failed", "danger")
    return identibooru.redirect("/" + username)
Ejemplo n.º 6
0
def map():
    search_results = False
    search_user_results = False
    terms = identibooru.request.args.get('t', False)

    search_users = ""

    if not terms:
        identibooru.flash("no search terms specified", "danger")
        return identibooru.redirect(identibooru.url_for('index'))
    if terms:
        if " " in terms:
            terms_list = terms.split(" ")
        else:
            terms_list = [terms]

        terms_list = [terms]
        if get_user_options(identibooru.current_user.get_username())['adult'] == 0:
            adult_tags = ["-rating:explicit", "-rating:questionable"]
            fullpath = identibooru.request.full_path

            for adult_tag in adult_tags:
                if not adult_tag in terms_list:
                    terms_list.append(adult_tag)

        search_tags = []
        remove_tags = []

        relevant_tags = []
        relevant_tag_list = []

        tags = Tags.query.all()
        for term in terms_list:
            groups = term.split(":")
            if len(groups) > 1:
                if groups[0] == "user":
                    search_user_results = Users.query.filter_by(username=groups[1].strip(" "))
            elif len(groups) == 1 and not identibooru.disable_search or get_user_options(identibooru.current_user.get_username())['rank'] == 99:
                for tag in tags:
                    if fnmatch.fnmatch(tag.tag, term):
                        search_tags.append(tag)
                if term[0] is "-":
                        remove_tags.append(term.strip("-"))
        search_results = render_search_results(html=False, search_tags=search_tags, remove_tags=remove_tags, users_class=Users)

    #tag_list = render_tag_list(html=True, tags=relevant_tag_list, is_profile=False, is_map=True, remove_tags=remove_tags)
    return identibooru.render_template('map.html', title='map', tag_results=search_results, search_users=search_user_results, keywords=terms)
Ejemplo n.º 7
0
def function_change_password():
    username = identibooru.request.form['username']
    password = identibooru.request.form['password']

    if (username
            == identibooru.current_user.get_username()) or (get_user_options(
                identibooru.current_user.get_username())['rank'] == 99):
        user = Users.query.filter_by(username=username).first()
        if user:
            hash = generate_password_hash(password)
            user.password = hash
            identibooru.db.session.commit()
            identibooru.flash("password changed", "success")
            identibooru.cache.delete_memoized(
                identibooru.models.users.get_user_options)
            return identibooru.redirect("/" + user.username + "/settings")
    identibooru.flash("authentication failed", "danger")
    return identibooru.redirect("/" + user.username + "/settings")
Ejemplo n.º 8
0
def function_change_options():
    username = identibooru.request.form['username']

    if (username == identibooru.current_user.get_username()) or (get_user_options(identibooru.current_user.get_username())['rank'] == 99):
        user = Users.query.filter_by(username=username).first()
        opts = UserOpts.query.filter_by(uid=user.uid).first()

        if identibooru.request.form.get('options'):
            options = identibooru.request.form.getlist('options')
            if "toggleSharing" in options:
                opts.sharing = 1
            else:
                opts.sharing = 0
            if "toggleTracking" in options:
                opts.tracking = 1
            else:
                opts.tracking = 0
            if "toggleAvatar" in options:
                opts.avatar = 1
            else:
                opts.avatar = 0
            if "toggleMap" in options:
                opts.map = 1
            else:
                opts.map = 0
            if "toggleAdult" in options:
                opts.adult = 1
            else:
                opts.adult = 0
        else:
            opts.sharing = 0
            opts.tracking = 0
            opts.avatar = 0
            opts.map = 0
            opts.adult = 0

        identibooru.db.session.commit()
        identibooru.cache.delete_memoized(identibooru.models.users.get_user_options)
        identibooru.cache.delete_memoized(identibooru.models.users.user_is_checked)
        identibooru.flash("settings changed", "success")
        return identibooru.redirect("/" + username + "/settings")
    identibooru.flash("authentication failed", "danger")
    return identibooru.redirect("/")
Ejemplo n.º 9
0
def profile_edit(username):
    if (username
            == identibooru.current_user.get_username()) or (get_user_options(
                identibooru.current_user.get_username())['rank'] == 99):
        user = Users.query.filter_by(username=username).first()
        if user:
            tags = Tags.query.filter_by(uid=user.uid).order_by(Tags.tag).all()
            tag_list = render_tag_list(html=False,
                                       tags=tags,
                                       is_profile=get_user_options(
                                           user.username),
                                       is_map=False)
        else:
            identibooru.flash("no such user exists", "danger")
            return identibooru.redirect(identibooru.url_for('index'))
        return identibooru.render_template(
            'profile-edit.html',
            title=user.username + ' - editing profile',
            user=user,
            user_options=get_user_options(username),
            tags=tag_list)
    identibooru.flash("authentication failed", "danger")
    return identibooru.redirect("/" + username)
Ejemplo n.º 10
0
def profile_set(username):
    coords = identibooru.request.args.get('coords', False)
    api = identibooru.request.args.get('api', False)
    if (api == get_user_options(username)['api']) or (
            username
            == identibooru.current_user.get_username()) or (get_user_options(
                identibooru.current_user.get_username())['rank'] == 99):
        user = Users.query.filter_by(username=username).first()
        if user:
            if coords:
                lat_lon = []
                validated_tag = validate_tag(coords)
                coords = coords.split(",")
                if len(coords) == 2:
                    lat = Tags.query.filter(Tags.tag.ilike('lat%')).filter_by(
                        uid=user.uid).first()
                    lon = Tags.query.filter(Tags.tag.ilike('lon%')).filter_by(
                        uid=user.uid).first()

                    if lat and lon:
                        lat.tag = "lat:" + coords[0]
                        lon.tag = "lon:" + coords[1]
                        identibooru.db.session.commit()
                    else:
                        lat = Tags(uid=user.uid, tag="lat:" + coords[0])
                        lon = Tags(uid=user.uid, tag="lon:" + coords[1])
                        identibooru.db.session.add(lat)
                        identibooru.db.session.add(lon)
                    identibooru.db.session.flush()
                    identibooru.db.session.commit()

                    identibooru.cache.delete_memoized(
                        identibooru.models.tags.sort_tags)
                    identibooru.cache.delete_memoized(
                        identibooru.models.tags.render_tag_list)
                    identibooru.cache.delete_memoized(
                        identibooru.models.tags.render_search_results)
                    identibooru.cache.delete_memoized(
                        identibooru.models.users.get_user_options)

                    if api: return "success"
                    identibooru.flash("location set", "success")
                    return identibooru.redirect("/" + user.username +
                                                "/settings")
        if api: return "fail"
        identibooru.flash("failed to set location", "danger")
        return identibooru.redirect("/" + user.username + "/settings")
    if api: return "authentication fail"
    identibooru.flash("authentication fail", "danger")
    return identibooru.redirect("/" + user.username + "/settings")
Ejemplo n.º 11
0
def function_register():
    if get_user_options(identibooru.current_user.get_username())['rank'] != 0:
        return identibooru.redirect(identibooru.url_for('index'))

    if identibooru.disable_registration:
        identibooru.flash("registration is currently disabled", "danger")
        return identibooru.redirect(identibooru.url_for('index'))

    email = identibooru.request.form['email']

    # this might stop automated requests
    if email != "":
        identibooru.app.logger.info(
            f'potential bot: %s entered an email address when email addresses aren\'t used',
            email)
        return "error: email address entered"

    if identibooru.hcaptcha:
        response = identibooru.request.form.get('g-recaptcha-response', False)
        data = {
            "secret": identibooru.hcaptcha_secret_key,
            "response": response,
            "remoteip": identibooru.request.environ.get('REMOTE_ADDR')
        }
        r = requests.post("https://hcaptcha.com/siteverify", data=data)
        is_valid = r.json()["success"] if r.status_code == 200 else False

        if not is_valid:
            identibooru.flash("invalid captcha", "danger")
            return identibooru.redirect(
                identibooru.url_for('account_register'))

    username = identibooru.request.form['username']
    password = identibooru.request.form['password']

    if not identibooru.re.search('^[a-z][a-z0-9-_]{2,32}$', username,
                                 identibooru.re.IGNORECASE):
        identibooru.flash("invalid username", "danger")
        return identibooru.redirect(identibooru.url_for('account_register'))

    user = Users.query.filter_by(username=username).first()
    if user:
        identibooru.flash("that username is taken", "danger")
        return identibooru.redirect(identibooru.url_for('account_register'))
    else:
        hash = generate_password_hash(password)
        create_user = Users(username=username, password=hash)
        identibooru.db.session.add(create_user)
        identibooru.db.session.flush()
        identibooru.db.session.commit()

        user_id = str(create_user.uid)
        bio = '''welcome to my profile

{{image}}'''

        create_user_opts = UserOpts(uid=user_id,
                                    bio=bio,
                                    views=0,
                                    avatar=0,
                                    map=0,
                                    tracking=0,
                                    sharing=1,
                                    rank=1,
                                    adult=0)
        identibooru.db.session.add(create_user_opts)
        identibooru.db.session.flush()
        identibooru.db.session.commit()

        create_user_tags = Tags(uid=user_id, tag="new_user")
        identibooru.db.session.add(create_user_tags)
        create_user_tags = Tags(uid=user_id, tag="rating:safe")
        identibooru.db.session.add(create_user_tags)
        identibooru.db.session.flush()
        identibooru.db.session.commit()

        qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L)
        qr.add_data(get_request().url_root + username)
        qr.make(fit=True)

        img = qr.make_image(fill_color="black", back_color="white")

        if img:
            qr_to_save = identibooru.app.static_folder + '/files/{}-qr.png'.format(
                username)
            img.save(qr_to_save)

        user = Users.query.filter_by(uid=user_id).first()
        user.is_authenticated = True
        identibooru.login_user(user)

        return identibooru.redirect("/" + username + "/edit")
    identibooru.flash("registration failed", "danger")
    return identibooru.redirect(identibooru.url_for('account_register'))
Ejemplo n.º 12
0
def function_logout():
    identibooru.logout_user()
    identibooru.flash("logged out", "danger")
    return identibooru.redirect(identibooru.url_for('index'))
Ejemplo n.º 13
0
def profile_save(username):
    if (username
            == identibooru.current_user.get_username()) or (get_user_options(
                identibooru.current_user.get_username())['rank'] == 99):
        user = Users.query.filter_by(username=username).first()
        updated_tags = False

        new_tags = identibooru.request.form['tags'].splitlines()
        new_bio = identibooru.request.form['bio']
        image = identibooru.request.files['image']

        set_rating = "rating:safe"

        if user:
            opts = UserOpts.query.filter_by(uid=user.uid).first()
            tags = Tags.query.filter_by(uid=user.uid).all()

            tag_count = Tags.query.filter_by(uid=user.uid).count()

            if tag_count >= identibooru.profile_tag_limit or len(
                    new_tags) >= identibooru.profile_tag_limit:
                identibooru.flash(
                    "attempting to set more than " +
                    str(identibooru.profile_tag_limit) + " tags", "danger")
                return identibooru.redirect("/" + username + "/edit")

            current_tags = []
            for tag in tags:
                current_tags.append(tag.tag)

            #print(new_tags)

            added = Diff(new_tags, current_tags)
            removed = Diff(current_tags, new_tags)

            #print(added)
            #print(removed)

            for tag in new_tags:
                tag = tag.lower()
                groups = tag.split(":")
                if len(groups) > 1:
                    if groups[0] == "age" and int(groups[1]) < 18:
                        identibooru.flash(
                            "Persons under the age of 18 are not allowed to create an account or otherwise use our Services.",
                            "danger")
                        return identibooru.redirect("/info/terms")

            invalid_entry = ["-", "~", "_"]
            invalid_exit = ["-", "~", "_"]

            if len(added) > 0:
                updated_tags = True
                for item in added:
                    validated_tag = validate_tag(item)
                    if (validated_tag is not False) and (
                            validated_tag[-1]
                            not in invalid_entry) and (validated_tag[-1]
                                                       not in invalid_exit):
                        new_tag = Tags(uid=user.uid, tag=validated_tag)
                        identibooru.db.session.add(new_tag)
            if len(removed) > 0:
                updated_tags = True
                for item in removed:
                    to_remove = Tags.query.filter_by(tag=item).first()
                    if to_remove: identibooru.db.session.delete(to_remove)

            if opts:
                opts.bio = new_bio
            old_bio = get_user_options(user.username)['bio']
            identibooru.db.session.commit()

            new_current_tags = Tags.query.filter_by(uid=user.uid).all()
            for current_tag in new_current_tags:
                if current_tag.tag in identibooru.questionable_tag_list:
                    removed_tags = ["rating:explicit", "rating:safe"]
                    for removed_tag in removed_tags:
                        removed_tag = Tags.query.filter_by(
                            uid=user.uid, tag=removed_tag).first()
                        if removed_tag:
                            identibooru.db.session.delete(removed_tag)
                            identibooru.db.session.commit()
                    set_rating = "rating:questionable"
                if current_tag.tag in identibooru.explicit_tag_list:
                    removed_tags = ["rating:safe", "rating:questionable"]
                    for removed_tag in removed_tags:
                        removed_tag = Tags.query.filter_by(
                            uid=user.uid, tag=removed_tag).first()
                        if removed_tag:
                            identibooru.db.session.delete(removed_tag)
                            identibooru.db.session.commit()
                    set_rating = "rating:explicit"

            if set_rating:
                if set_rating == "rating:safe":
                    removed_tags = ["rating:explicit", "rating:questionable"]
                    for removed_tag in removed_tags:
                        removed_tag = Tags.query.filter_by(
                            uid=user.uid, tag=removed_tag).first()
                        if removed_tag:
                            identibooru.db.session.delete(removed_tag)
                            identibooru.db.session.commit()
                new_tag = Tags(uid=user.uid, tag=validate_tag(set_rating))
                identibooru.db.session.add(new_tag)

            identibooru.db.session.commit()

            if get_qr(user.username) == "":
                qr = qrcode.QRCode(
                    error_correction=qrcode.constants.ERROR_CORRECT_L)
                qr.add_data(get_request().url_root + username)
                qr.make(fit=True)

                img = qr.make_image(fill_color="black", back_color="white")

                if img:
                    qr_to_save = identibooru.app.static_folder + '/files/{}-qr.png'.format(
                        user.username)
                    img.save(qr_to_save)

            if image:
                avatar_to_save = identibooru.app.static_folder + '/files/{}-avatar-full.png'.format(
                    user.username)
                avatar_to_save_thumb = identibooru.app.static_folder + '/files/{}-avatar-thumb.png'.format(
                    user.username)
                image.save(avatar_to_save)

                try:
                    image_maxwidth = 1024 if not identibooru.image_maxwidth else int(
                        identibooru.image_maxwidth)
                    image_maxheight = 2048 if not identibooru.image_maxheight else int(
                        identibooru.image_maxheight)

                    thumb_maxwidth = 256 if not identibooru.thumb_maxwidth else int(
                        identibooru.thumb_maxwidth)
                    thumb_maxheight = 1024 if not identibooru.thumb_maxheight else int(
                        identibooru.thumb_maxheight)

                    img = Image.open(avatar_to_save)
                    img.thumbnail((image_maxwidth, image_maxheight))
                    img.save(avatar_to_save)

                    thumbnail = Image.open(avatar_to_save)
                    thumbnail.thumbnail((thumb_maxwidth, thumb_maxheight))
                    thumbnail.save(avatar_to_save_thumb)
                except IOError:
                    identibooru.flash("image upload failed", "danger")

            identibooru.cache.delete_memoized(identibooru.models.users.get_qr)
            identibooru.cache.delete_memoized(
                identibooru.models.users.get_avatar)
            identibooru.cache.delete_memoized(
                identibooru.models.conversions.sanitize_html)
            identibooru.cache.delete_memoized(
                identibooru.models.users.get_user_options)

            if updated_tags:
                identibooru.cache.delete_memoized(
                    identibooru.models.users.get_stats)
                identibooru.cache.delete_memoized(
                    identibooru.models.tags.sort_tags)
                identibooru.cache.delete_memoized(
                    identibooru.models.tags.count_tags)
                identibooru.cache.delete_memoized(
                    identibooru.models.tags.render_tag_list)
                identibooru.cache.delete_memoized(
                    identibooru.models.tags.render_search_results)

            clean_tags(user.username, Users)

            return identibooru.redirect("/" + user.username + "/edit")
    identibooru.flash("authentication failed", "danger")
    return identibooru.redirect("/" + username)