Exemple #1
0
def openid_base_params(extra=None):
    # 登录验证, 注册新用户, 更新用户信息
    # token 保留字段;
    # username 用户名;
    # pwd 密码;
    # appid 来自何种应用的注册请求;
    # clientId 用户设备唯一号;
    # timestamp 注册请求到达应用的时间戳;
    # login_channel 用户渠道标识符; iOS pc android
    # extra_payload 保留字典字段;
    # user_ip 客户端ip;
    # remote_ip 服务端ip;
    params = {
        # 'token': '',
        # 'username': username,
        # 'email': email,
        # 'pwd': password,
        'request_id': str(uuid.uuid1()),
        'appid': 'RGBlog',
        'clientId': request_value(request, 'clientId', default=''),
        'timestamp': RGTimeUtil.timestamp(),
        'login_channel': request_value(request, 'channel', default=''),
        # 'extra_payload': '用户名',
        'user_ip': request_ip(request, default=''),
        'remote_ip': RGPublicHost
    }
    if extra is not None:
        params = dict(params, **extra)
    return params
def get_base64_username_email(username, email, expire=None):
    if expire is None:
        info = username + user.salt + ' ' + email
    else:
        info = username + user.salt + ' ' + email + ' {}'.format(
            RGTimeUtil.timestamp() + expire * 1000)
    info = info.encode("utf-8")
    info = base64.urlsafe_b64encode(info)
    info = str(info, encoding="utf-8")
    return info
Exemple #3
0
    def is_allow_resend_email(self, now_time=None):
        _type = self.get_payload(key='type', default=None)
        if _type is None:
            return True

        if _type != RGVerifyType.forget_pwd and self.is_full_active():
            return False
        if now_time is None:
            now_time = RGTimeUtil.timestamp()
        if now_time > self.get_payload(key='timestamp',
                                       default=0) + 5 * 60 * 1000:
            return True
        return False
Exemple #4
0
def collect_read_count(art_id, user_id):
    timestamp = RGTimeUtil.timestamp()
    remote_addr = request_ip(request, default='')

    if art_id not in ReadCountMap:
        read_item = {
            'count': 1,
            'readers': {
                remote_addr: {
                    'time': timestamp,
                }
            }
        }
        if user_id is not None:
            read_item['readers'][user_id] = {'time': timestamp}
        ReadCountMap[art_id] = read_item
    else:

        read_item = ReadCountMap[art_id]
        readers = read_item['readers']
        reader = None

        if user_id is not None:
            if user_id in readers:
                reader = readers[user_id]
            else:
                reader = {'time': 0}
                readers[user_id] = reader

        if remote_addr in readers:
            reader_ip = readers[remote_addr]
        else:
            reader_ip = {'time': 0}
            readers[remote_addr] = reader_ip

        read_time = timestamp
        if reader is not None:
            read_time = reader['time']
        elif reader_ip is not None:
            read_time = reader_ip['time']

        if timestamp - read_time > 60 * 60 * 1000:
            read_item['count'] = read_item['count'] + 1
            reader_ip['time'] = timestamp
            if reader is not None:
                reader['time'] = timestamp
Exemple #5
0
def check_and_new_user_file_with_hash(user_id,
                                      directory_id=-1,
                                      file_hash=None,
                                      name=''):
    conn = None
    user_file = None
    from RGUtil.RGCodeUtil import RGResCode
    code = RGResCode.server_error
    try:
        conn = dao.get()
        file_info = check_file(file_hash=file_hash, conn=conn)
        if file_info is None:
            code = RGResCode.not_existed
            raise Exception(code)

        if file_info is not None:
            if not capacity_enough(user_id=user_id,
                                   new_file_size=file_info['size'],
                                   conn=conn):
                code = RGResCode.full_size
                raise Exception('capacity not enough')

            time = RGTimeUtil.timestamp()
            user_file = new_user_file(conn=conn,
                                      user_id=user_id,
                                      file_id=file_info['id'],
                                      type=0,
                                      directory_id=directory_id,
                                      personal_name=name,
                                      add_timestamp=time,
                                      update_timestamp=time,
                                      combined_file_info=file_info)
            if user_file is None:
                code = RGResCode.insert_fail
                raise Exception('new_user_file failed')
        code = RGResCode.ok
        conn.commit()
    except Exception as e:
        print(e)
        conn.rollback()
        conn.commit()
    finally:
        if conn:
            conn.close()
        return user_file, code
def get_username_email(info, has_expire=False):
    info = str(base64.b64decode(info), "utf-8")
    salt = user.salt + ' '

    if has_expire:
        index = info.rfind(' ')
        expire = info[index + 1:]
        expire = int(expire)
        if expire < RGTimeUtil.timestamp():
            return None, None
        info = info[0:index]

    index = info.rfind(salt)
    if index == -1:
        return None, None

    username = info[0:index]
    email = info[index + len(salt):]
    return username, email
Exemple #7
0
def change_relation(my_id, other_id, relation=0):
    timestamp = RGTimeUtil.timestamp()

    sql = """INSERT INTO user_relation (m_user_id, o_user_id, relation, addtime)
        VALUES (%(my_id)s, %(other_id)s, %(relation)s, %(timestamp)s) 
        ON DUPLICATE KEY UPDATE relation=%(relation)s, addtime=%(timestamp)s"""

    result, count, new_id = dao.execute_sql(sql=sql,
                                            ret=False,
                                            args={
                                                'my_id': my_id,
                                                'other_id': other_id,
                                                'relation': relation,
                                                'timestamp': timestamp
                                            })

    if count > 0:
        return True, relation
    else:
        return False, relation
Exemple #8
0
def generate_token_ifneed(user_id, token_type):
    new_token = RGTokenUtil.generate_token(str(user_id))
    new_time = RGTimeUtil.timestamp()
    token_type = int(token_type)
    sql = "INSERT INTO tokens (user_id, type, token, timestamp) \
            VALUES (%(user_id)s, %(token_type)s, %(new_token)s, %(new_time)s) \
            ON DUPLICATE KEY UPDATE token=%(new_token)s, timestamp=%(new_time)s"

    result, count, new_id = dao.execute_sql(sql,
                                            new_id=True,
                                            args={
                                                'user_id': user_id,
                                                'token_type': token_type,
                                                'new_token': new_token,
                                                'new_time': new_time,
                                            })

    print(count)

    if count > 0:
        return new_token
    else:
        return None
Exemple #9
0
def new_directory(user_id, directory_id=-1, name=''):
    conn = None
    try:
        conn = dao.get()
        time = RGTimeUtil.timestamp()
        file = new_user_file(conn=conn,
                             user_id=user_id,
                             file_id=None,
                             type=1,
                             directory_id=directory_id,
                             personal_name=name,
                             add_timestamp=time,
                             update_timestamp=time)
        conn.commit()
        return file
    except Exception as e:
        print(e)
        conn.rollback()
        conn.commit()
        return None
    finally:
        if conn:
            conn.close()
Exemple #10
0
def new_file(conn=None,
             filename='',
             mime='',
             exif_time=0,
             exif_info=None,
             exif_lalo=None,
             file_hash=None,
             size=0,
             forever=0):
    timestamp = RGTimeUtil.timestamp()
    sql = "INSERT INTO `file` \
    (filename, mime, exif_timestamp, `timestamp`, `exif_info`, `exif_lalo`, `hash`, `size`, `forever`) \
    VALUES \
    (%(filename)s, %(mime)s, %(exif_timestamp)s, %(timestamp)s, \
    %(exif_info)s, %(exif_lalo)s, %(hash)s, %(size)s, %(forever)s)"

    args = {
        'filename': filename,
        'mime': mime,
        'exif_timestamp': exif_time,
        'timestamp': timestamp,
        'exif_info': exif_info,
        'exif_lalo': exif_lalo,
        'hash': file_hash,
        'size': str(size),
        'forever': forever
    }
    result, count, new_id, err = dao.do_execute_sql(sql=sql,
                                                    conn=conn,
                                                    new_id=True,
                                                    args=args,
                                                    commit=False)
    if count > 0:
        args['id'] = new_id
        return args
    else:
        return None
Exemple #11
0
def new_album(user_id,
              title='',
              desc='',
              cover=None,
              level=0,
              timestamp=None,
              conn=None,
              commit=True):
    if timestamp is None:
        timestamp = RGTimeUtil.timestamp()

    sql = new_album_sql(user_id_key='user_id',
                        title_key='title',
                        desc_key='desc',
                        cover_key='cover',
                        level_key='level',
                        timestamp_key='timestamp')
    args = {
        'desc': desc,
        'title': title,
        'user_id': user_id,
        'cover': cover,
        'level': level,
        'timestamp': timestamp
    }

    result, count, new_id, error = dao.do_execute_sql(sql,
                                                      new_id=True,
                                                      conn=conn,
                                                      commit=commit,
                                                      args=args)

    if count > 0:
        return album(new_id, user_id, title, desc, cover, level, timestamp)
    else:
        return None
Exemple #12
0
def new_user(username, pwd, title='Title', desc='Desc', nick='Nickname'):
    timestamp = RGTimeUtil.timestamp()
    pwd = sha256_key(username, pwd)

    _user = None

    conn = None
    cursor = None
    try:
        conn = dao.get()
        cursor = conn.cursor()
        # 某些时候获取到重复的ID
        # sql = "SELECT auto_increment FROM  information_schema.`TABLES` \
        #         WHERE TABLE_SCHEMA='rg_database'AND TABLE_NAME='user'"
        # cursor.execute(sql)
        new_user_id = -1

        from Model import album
        _album = album.new_album(user_id=new_user_id,
                                 title='日志相册',
                                 desc='默认相册',
                                 cover=None,
                                 level=2,
                                 timestamp=timestamp,
                                 conn=conn,
                                 commit=False)

        if _album is None:
            raise Exception

        new_album_id = _album.ID

        sql = "INSERT INTO user (username, pwd, addtime, title, description, nickname, default_album_id) VALUES \
        (%(username)s, %(pwd)s, %(timestamp)s, %(title)s, %(desc)s, %(nick)s, %(new_album_id)s)"

        count = cursor.execute(sql,
                               args={
                                   'username': username,
                                   'pwd': pwd,
                                   'timestamp': timestamp,
                                   'title': title,
                                   'desc': desc,
                                   'nick': nick,
                                   'new_album_id': new_album_id,
                                   'is_active': 1
                               })

        if count == 0:
            raise Exception

        new_user_id = dao.get_last_insert(cursor=cursor)[0]
        flag = album.update_owner(album_id=new_album_id,
                                  user_id=new_user_id,
                                  conn=conn,
                                  commit=False)
        if not flag:
            raise Exception

        conn.commit()
        return get_user_with_username(username, need_email=True)
    except Exception as e:
        print(e)
        conn.rollback()
        conn.commit()
        return None
    finally:
        dao.close(conn, cursor)
Exemple #13
0
def verify_user(username,
                email,
                pwd,
                verify_code,
                verify_type,
                title='Title',
                desc='Desc',
                nick='Nickname'):
    check_time = RGTimeUtil.timestamp()
    res_code = RGResCode.server_error

    conn = None
    try:
        conn = dao.get()
        sql = "SELECT * FROM user where username=%(username)s"
        result, count, new_id, err = dao.do_execute_sql_with_connect(
            sql=sql,
            ret=True,
            commit=False,
            conn=conn,
            args={'username': username})

        if err:
            res_code = RGResCode.server_error
            raise Exception

        if count <= 0:
            res_code = RGResCode.not_existed
            raise Exception

        _user = user_with_db_result(result[0], need_pwd=True, need_email=True)
        if verify_type != RGVerifyType.forget_pwd and _user.is_full_active():
            res_code = RGResCode.has_existed
            raise Exception

        # 超时
        if _user.is_time_out(check_time=check_time):
            res_code = RGResCode.timeout
            raise Exception

        # 校验验证码
        if verify_type == RGVerifyType.new or verify_type == RGVerifyType.forget_pwd:
            email = _user.get_payload(key='email')
        if not _user.verify_code(
                code=verify_code, email=email, v_type=verify_type):
            res_code = RGResCode.verify_code_incorrect
            raise Exception

        # 创建默认相册
        user_id = _user.ID
        if verify_type == RGVerifyType.new:
            from Model import album
            _album = album.new_album(user_id=user_id,
                                     title='日志相册',
                                     desc='默认相册',
                                     cover=None,
                                     level=2,
                                     timestamp=check_time,
                                     conn=conn,
                                     commit=False)

            if _album is None:
                res_code = RGResCode.server_error
                raise Exception
            new_album_id = _album.ID
            _user.addTime = check_time
        else:
            new_album_id = _user.defaultAlbumId

        sha256_pwd = sha256_key(username, pwd)

        args = {
            'pwd': sha256_pwd,
            'add_time': _user.addTime,
            'title': title,
            'desc': desc,
            'nick': nick,
            'new_album_id': new_album_id,
            'id': user_id,
            'is_active': 1,
            'info_payload': ''
        }

        if verify_type == RGVerifyType.new:
            sql = "UPDATE user SET " \
                  "pwd=%(pwd)s, " \
                  "addtime=%(add_time)s, " \
                  "title=%(title)s, " \
                  "description=%(desc)s, " \
                  "nickname=%(nick)s, " \
                  "default_album_id=%(new_album_id)s, " \
                  "is_active=%(is_active)s, " \
                  "info_payload=%(info_payload)s " \
                  "where id=%(id)s"
        elif verify_type == RGVerifyType.bind:
            if sha256_pwd != _user.pwd:
                res_code = RGResCode.password_incorrect
                raise Exception
            sql = "UPDATE user SET " \
                  "info_payload=%(info_payload)s " \
                  "where id=%(id)s"
        elif verify_type == RGVerifyType.forget_pwd:
            sql = "UPDATE user SET " \
                  "pwd=%(pwd)s, " \
                  "info_payload=%(info_payload)s " \
                  "where id=%(id)s"

        result, count, new_id, err = dao.do_execute_sql_with_connect(
            sql=sql,
            args=args,
            conn=conn,
            commit=False,
            ret=True,
        )

        if err:
            res_code = RGResCode.server_error
            raise Exception

        if verify_type == RGVerifyType.new:
            code = RGOpenIdController.user_new(username=username,
                                               email=email,
                                               password=pwd)
        elif verify_type == RGVerifyType.bind:
            code = RGOpenIdController.user_new(username=username,
                                               email=email,
                                               password=pwd)
        elif verify_type == RGVerifyType.forget_pwd:
            code = RGOpenIdController.user_update(username=username,
                                                  password=pwd)
        else:
            code = RGResCode.lack_param

        if code != RGResCode.ok:
            res_code = code
            raise Exception

        res_code = RGResCode.ok
        conn.commit()
        return _user.ID, res_code
    except Exception as e:
        print(e)
        conn.rollback()
        conn.commit()
        return None, res_code
    finally:
        dao.close(conn, None)
Exemple #14
0
def new_user_and_save_verify_code(username,
                                  email,
                                  verify_code,
                                  verify_type=RGVerifyType.new):

    if username is None or email is None:
        return RGResCode.lack_param
    if len(username) <= 0 or len(email) <= 0:
        return RGResCode.lack_param

    timestamp = RGTimeUtil.timestamp()

    conn = None
    res_code = RGResCode.server_error
    try:
        conn = dao.get()

        # OPEN ID
        res_code, data = User.RGOpenIdController.user_list(email=email)
        if res_code != RGResCode.ok:
            raise Exception

        if verify_type == RGVerifyType.forget_pwd:
            if data is not None and len(data) > 0:
                if data[0]['username'] != username:
                    res_code = RGResCode.auth_fail
                    raise Exception
            else:
                res_code = RGResCode.not_existed
                raise Exception
        else:
            if data is not None and len(data) > 0:
                res_code = RGResCode.has_existed
                raise Exception

        # OPEN ID
        if verify_code == RGVerifyType.bind or verify_code == RGVerifyType.new:
            res_code, data = User.RGOpenIdController.user_list(
                username=username)
            if res_code != RGResCode.ok:
                raise Exception
            if data is not None and len(data) > 0:
                res_code = RGResCode.has_existed
                raise Exception

        sql = "SELECT * FROM user where username=%(username)s"
        result, count, new_id, err = dao.do_execute_sql_with_connect(
            sql=sql,
            ret=True,
            commit=False,
            conn=conn,
            args={'username': username})
        if err:
            res_code = RGResCode.server_error
            raise Exception
        if count > 0:
            _user = user_with_db_result(result[0], need_email=True)
            if verify_type == RGVerifyType.new:
                if _user.is_full_active() or _user.has_email():
                    res_code = RGResCode.user_has_existed
                    raise Exception
            elif verify_type == RGVerifyType.bind:
                if _user.has_email():
                    res_code = RGResCode.user_has_existed
                    raise Exception
            elif verify_type == RGVerifyType.forget_pwd:
                pass
            else:
                res_code = RGResCode.lack_param
                raise Exception

            if _user.is_allow_resend_email(now_time=timestamp) is False:
                res_code = RGResCode.frequent
                raise Exception

        sql = "INSERT INTO user (username, addtime, pwd, is_active, info_payload) VALUES \
            (%(username)s, %(timestamp)s, %(pwd)s, %(is_active)s, %(info_payload)s) \
            ON DUPLICATE KEY UPDATE info_payload=%(info_payload)s"

        payload = json.dumps({
            'code': verify_code,
            'email': email,
            'type': verify_type,
            'timestamp': RGTimeUtil.timestamp()
        })
        args = {
            'username': username,
            'timestamp': timestamp,
            'pwd': '',
            'is_active': 0,
            'info_payload': payload
        }

        result, count, new_id, err = dao.do_execute_sql_with_connect(
            sql=sql, args=args, conn=conn, commit=False)

        if err:
            res_code = RGResCode.server_error
            raise Exception

        conn.commit()
        return res_code
    except Exception as e:
        print(e)
        conn.rollback()
        conn.commit()
        return res_code
    finally:
        dao.close(conn, None)
Exemple #15
0
def user_is_timeout(add_time, check_time=None):
    if check_time is None:
        check_time = RGTimeUtil.timestamp()
    if add_time + 10 * 60 * 1000 < check_time:
        return True
    return False
Exemple #16
0
def add_or_update_art(user_id,
                      title=None,
                      content='',
                      cate=0,
                      group_id=None,
                      art_id=None,
                      summary='',
                      cover='',
                      conn=None,
                      commit=True,
                      timestamp=None):
    if timestamp is None:
        timestamp = RGTimeUtil.timestamp()

    g = Goose({
        'browser_user_agent':
        'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)\
         Chrome/57.0.2987.110 Safari/537.36',
        'stopwords_class': StopWordsChinese
    })

    h5_format = \
        '<!DOCTYPE html> \
        <html lang = "zh"> \
        <head> \
        <meta charset = "UTF-8"> \
        <title> blog </title > \
        </head> \
        <body> \
        <article><div> %s </div></article> \
        </body> \
        </html>'

    art_parse = g.extract(raw_html=h5_format % content)
    open_graph = art_parse.opengraph

    p_summary = ''
    p_cover = ''

    if 'description' in open_graph:
        p_summary = open_graph['description']
    if p_summary is not None and len(p_summary) is 0:
        p_summary = art_parse.cleaned_text
    if p_summary is not None and len(p_summary) is not 0:
        summary = p_summary

    if len(summary) >= 100:
        summary = summary[:97] + '...'

    if 'image' in open_graph:
        p_cover = open_graph['image']
    if p_cover is '' and art_parse.top_image is not None:
        p_cover = art_parse.top_image.src
    if p_cover is not None and len(p_cover) is not 0:
        cover = p_cover
    if cover is not None and len(cover) > 0:
        try:
            parsed_tuple = parse.urlparse(cover)
            if parsed_tuple.netloc.endswith(
                    RGDomainName) or parsed_tuple.hostname is None:
                p_cover = parsed_tuple.path
                index = p_cover.find(FilePreFix)
                if index == 0 or index == 1:
                    cover = p_cover
                    index = cover.rfind(RGQualityName + '.')
                    if index != -1:
                        cover = cover[0:index] + RGThumbnailName + cover[
                            index + len(RGQualityName):]
        except Exception as e:
            print(e)

    if group_id is not None and int(group_id) < 0:
        group_id = None

    if art_id is None:
        sql = "INSERT INTO art \
            (title, summary, cate, user_id, group_id, cover, content, addtime, updatetime, create_time) \
             VALUES (%(title)s, %(summary)s, %(cate)s, %(user_id)s, %(group_id)s, %(cover)s, %(content)s, \
             {}, {}, from_unixtime({}))".format(timestamp, timestamp,
                                                timestamp / 1000)
    else:
        art_id = int(art_id)
        sql = "UPDATE art SET \
                title = %(title)s, summary=%(summary)s, cate=%(cate)s, \
                user_id=%(user_id)s, group_id=%(group_id)s, cover=%(cover)s, \
                content=%(content)s, updatetime=%(timestamp)s, group_id=%(group_id)s \
                WHERE id=%(art_id)s and user_id =%(user_id)s"

    args = {
        'title': title,
        'summary': summary,
        'cate': cate,
        'user_id': user_id,
        'group_id': group_id,
        'cover': cover,
        'content': content,
        'timestamp': timestamp,
        'art_id': art_id,
    }

    result, count, new_id, err = dao.do_execute_sql(sql=sql,
                                                    kv=True,
                                                    new_id=True,
                                                    conn=conn,
                                                    commit=commit,
                                                    args=args)

    if count > 0:
        return True, art_id if art_id is not None else new_id
    else:
        return False, 0