Example #1
0
    def add(self, email, password, ip):
        if self.get(email=email, fields='1') is not None:
            raise self.DeplicateUser('duplicate username')

        now = time.time()
        if isinstance(ip, basestring):
            ip = utils.ip2int(ip)
        userkey = umsgpack.unpackb(crypto.password_hash(password))[0]

        insert = dict(
                email = email,
                email_verified = 0,
                password = crypto.aes_encrypt(
                    crypto.password_hash(password), userkey),
                userkey = crypto.aes_encrypt(userkey),
                nickname = None,
                role = None,
                ctime = now,
                mtime = now,
                atime = now,
                cip = ip,
                mip = ip,
                aip = ip,
                )
        return self._insert(**insert)
Example #2
0
    def add(self, email, password, ip):
        if self.get(email=email, fields='1') is not None:
            raise self.DeplicateUser('duplicate username')

        now = time.time()
        if isinstance(ip, basestring):
            ip = utils.ip2int(ip)
        userkey = umsgpack.unpackb(crypto.password_hash(password))[0]

        insert = dict(
                email = email,
                email_verified = 0,
                password = crypto.aes_encrypt(
                    crypto.password_hash(password), userkey),
                userkey = crypto.aes_encrypt(userkey),
                nickname = None,
                role = None,
                ctime = now,
                mtime = now,
                atime = now,
                cip = ip,
                mip = ip,
                aip = ip,
                )
        return self._insert(**insert)
Example #3
0
    def add(self, email, password, ip):
        if self.get(email=email, fields='1') is not None:
            raise self.DeplicateUser('duplicate username')

        now = time.time()
        if isinstance(ip, str):
            ip = utils.ip2int(ip)
        userkey = umsgpack.unpackb(crypto.password_hash(password))[0]

        hash = MD5.new()
        hash.update(password.encode('utf-8'))
        password_md5 = hash.hexdigest()

        insert = dict(
            email=email,
            email_verified=0,
            password=crypto.aes_encrypt(crypto.password_hash(password),
                                        userkey),
            userkey=crypto.aes_encrypt(userkey),
            nickname=None,
            role=None,
            ctime=now,
            mtime=now,
            atime=now,
            cip=ip,
            mip=ip,
            aip=ip,
            password_md5=password_md5,
        )
        return self._insert(**insert)
Example #4
0
    def mod(self, id, **kwargs):
        assert 'id' not in kwargs, 'id not modifiable'
        assert 'email' not in kwargs, 'email not modifiable'
        assert 'userkey' not in kwargs, 'userkey not modifiable'

        if 'password' in kwargs:
            kwargs['password'] = self.encrypt(id, crypto.password_hash(kwargs['password']))
            
        if 'token' in kwargs:
            kwargs['token'] = self.encrypt(id, crypto.password_hash(kwargs['token']))

        return self._update(where="id=%s" % self.placeholder, where_values=(id, ), **kwargs)
Example #5
0
File: user.py Project: zdqd/qiandao
    async def post(self,userid):
        try:
            log = u'设置成功'
            envs = {}
            for k, _  in self.request.body_arguments.items():
                envs[k] = self.get_body_argument(k)

            adminuser = self.db.user.get(email=envs['adminmail'], fields=('role', 'email'))
            newPWD = envs['newpwd']
            if self.db.user.challenge_MD5(envs['adminmail'], envs['adminpwd']) and (adminuser['role'] == 'admin'):
                if (len(newPWD) >= 6):
                    self.db.user.mod(userid, password=newPWD)
                    user = self.db.user.get(userid, fields=('email','password','password_md5'))
                    hash = MD5.new()
                    hash.update(newPWD.encode('utf-8'))
                    tmp = crypto.password_hash(hash.hexdigest(), self.db.user.decrypt(userid, user['password']))
                    if (user['password_md5'] != tmp):
                        self.db.user.mod(userid, password_md5=tmp)
                    if not (self.db.user.challenge(envs['usermail'], newPWD)):
                        raise Exception(u'修改失败')
                else:
                    raise Exception(u'密码长度要大于6位')    
            else:
                raise Exception(u'管理员用户名/密码错误')
        except Exception as e:
            traceback.print_exc()
            await self.render('utils_run_result.html', log=str(e), title=u'设置失败', flg='danger')
            return

        await self.render('utils_run_result.html', log=log, title=u'设置成功', flg='success')
        return
Example #6
0
 def challenge(self, email, password):
     user = self.get(email=email, fields=('id', 'password'))
     if not user:
         return False
     password_hash = self.decrypt(user['id'], user['password'])
     if password_hash == crypto.password_hash(password, password_hash):
         return True
     return False
Example #7
0
 def challenge(self, email, password):
     user = self.get(email=email, fields=('id', 'password'))
     if not user:
         return False
     password_hash = self.decrypt(user['id'], user['password'])
     if password_hash == crypto.password_hash(password, password_hash):
         return True
     return False
Example #8
0
    def mod(self, id, **kwargs):
        assert 'id' not in kwargs, 'id not modifiable'
        assert 'email' not in kwargs, 'email not modifiable'
        assert 'userkey' not in kwargs, 'userkey not modifiable'

        if 'password' in kwargs:
            kwargs['password'] = self.encrypt(id, crypto.password_hash(kwargs['password']))

        return self._update(where="id=%s", where_values=(id, ), **kwargs)
Example #9
0
 def challenge_MD5(self, email, password_md5):
     user = self.get(email=email, fields=('id', 'password', 'password_md5'))
     if not user:
         return False
     else:
         if (user['password_md5'] == ''):
             pass
         else:
             password_hash = self.decrypt(user['id'], user['password'])
             if crypto.password_hash(password_md5, password_hash) == user['password_md5']:
                 return True
     return False
Example #10
0
    async def post(self):
        email = self.get_argument('email')
        password = self.get_argument('password')
        siteconfig = self.db.site.get(1, fields=('MustVerifyEmailEn'))
        regFlg = False if self.db.site.get(
            1, fields=('regEn'))['regEn'] == 0 else True
        if not email or not password:
            await self.render('login.html',
                              password_error=u'请输入用户名和密码',
                              email=email,
                              regFlg=regFlg)
            return

        user_try = self.db.user.get(email=email,
                                    fields=('id', 'role', 'status'))
        if (user_try):
            if (user_try['status'] != 'Enable') and (user_try['role'] !=
                                                     'admin'):
                await self.render('login.html',
                                  password_error=u'账号已被禁用,请联系管理员',
                                  email=email,
                                  regFlg=regFlg)
                return
        else:
            await self.render('login.html',
                              password_error=u'不存在此邮箱或密码错误',
                              email=email,
                              regFlg=regFlg)
            return

        if self.db.user.challenge(email, password):
            user = self.db.user.get(email=email,
                                    fields=('id', 'email', 'nickname', 'role',
                                            'email_verified'))
            if not user:
                await self.render('login.html',
                                  password_error=u'不存在此邮箱或密码错误',
                                  email=email,
                                  regFlg=regFlg)
                return

            if (siteconfig['MustVerifyEmailEn'] !=
                    0) and (user['email_verified'] == 0):
                await self.render('login.html',
                                  password_error=u'未验证邮箱,请点击注册重新验证邮箱',
                                  email=email,
                                  regFlg=regFlg)
                return

            setcookie = dict(
                expires_days=config.cookie_days,
                httponly=True,
            )
            if config.https:
                setcookie['secure'] = True
            self.set_secure_cookie('user', umsgpack.packb(user), **setcookie)
            self.db.user.mod(user['id'],
                             atime=time.time(),
                             aip=self.ip2varbinary)

            # 如果用户MD5不一致就更新MD5
            user = self.db.user.get(email=email,
                                    fields=('id', 'password', 'password_md5'))
            hash = MD5.new()
            hash.update(password.encode('utf-8'))
            tmp = crypto.password_hash(
                hash.hexdigest(),
                self.db.user.decrypt(user['id'], user['password']))
            if (user['password_md5'] != tmp):
                self.db.user.mod(user['id'], password_md5=tmp)

            next = self.get_argument('next', '/my/')
            self.redirect(next)
        else:
            self.evil(+5)
            await self.render('login.html',
                              password_error=u'不存在此邮箱或密码错误',
                              email=email,
                              regFlg=regFlg)
Example #11
0
    def ConvertNewType(self, path=config.sqlite3.path):
        if config.db_type == 'sqlite3':
            import sqlite3_db as db
        else:
            import db

        class DB(object):
            user = db.UserDB()
            tpl = db.TPLDB()
            task = db.TaskDB()
            tasklog = db.TaskLogDB()
            site = db.SiteDB()
            pubtpl = db.PubTplDB()

        self.db = DB

        if config.db_type == 'sqlite3':
            self._execute('''CREATE TABLE IF NOT EXISTS `%s` (
                    `id` INTEGER NOT NULL PRIMARY KEY,
                    `regEn` INT UNSIGNED NOT NULL DEFAULT 1,
                    `MustVerifyEmailEn` INT UNSIGNED NOT NULL DEFAULT 0
                    )''' % 'site')
        else:
            self.db.site._execute('''CREATE TABLE IF NOT EXISTS `user` (
            `id` INTEGER NOT NULL PRIMARY KEY  AUTO_INCREMENT,
            `email` VARCHAR(256) NOT NULL,
            `email_verified` TINYINT(1) NOT NULL DEFAULT 0,
            `password` VARBINARY(128) NOT NULL,
            `password_md5` VARBINARY(128) NOT NULL DEFAULT '',
            `userkey` VARBINARY(128) NOT NULL,
            `nickname` VARCHAR(64) NULL,
            `role` VARCHAR(128) NULL,
            `ctime` INT UNSIGNED NOT NULL,
            `mtime` INT UNSIGNED NOT NULL,
            `atime` INT UNSIGNED NOT NULL,
            `cip` VARBINARY(16) NOT NULL,
            `mip` VARBINARY(16) NOT NULL,
            `aip` VARBINARY(16) NOT NULL,
            `skey` VARBINARY(128) NOT NULL DEFAULT '',
            `barkurl` VARBINARY(128) NOT NULL DEFAULT '',
            `wxpusher` VARBINARY(128) NOT NULL DEFAULT '',
            `noticeflg` INT UNSIGNED NOT NULL DEFAULT 1,
            `logtime`  VARBINARY(1024) NOT NULL DEFAULT '{"en":false,"time":"20:00:00","ts":0,"schanEn":false,"WXPEn":false}',
            `status`  VARBINARY(1024) NOT NULL DEFAULT 'Enable',
            `notepad` TEXT NULL,
            `diypusher` VARBINARY(1024) NOT NULL DEFAULT '',
            `qywx_token` VARBINARY(1024) NOT NULL DEFAULT '',
            `tg_token` VARBINARY(1024) NOT NULL DEFAULT '',
            `dingding_token` VARBINARY(1024) NOT NULL DEFAULT '',
            `push_batch`  VARBINARY(1024) NOT NULL DEFAULT '{"sw":false,"time":0,"delta":86400}'
            );''')
            self.db.site._execute('''CREATE TABLE IF NOT EXISTS `tpl` (
            `id` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
            `userid` INT UNSIGNED NULL,
            `siteurl` VARCHAR(256) NULL,
            `sitename` VARCHAR(128) NULL,
            `banner` VARCHAR(1024) NULL,
            `disabled` TINYINT(1) NOT NULL DEFAULT 0,
            `public` TINYINT(1) NOT NULL DEFAULT 0,
            `lock` TINYINT(1) NOT NULL DEFAULT 0,
            `fork` INT UNSIGNED NULL,
            `har` MEDIUMBLOB NULL,
            `tpl` MEDIUMBLOB NULL,
            `variables` TEXT NULL,
            `interval` INT UNSIGNED NULL,
            `note` VARCHAR(1024) NULL,
            `success_count` INT UNSIGNED NOT NULL DEFAULT 0,
            `failed_count` INT UNSIGNED NOT NULL DEFAULT 0,
            `last_success` INT UNSIGNED NULL,
            `ctime` INT UNSIGNED NOT NULL,
            `mtime` INT UNSIGNED NOT NULL,
            `atime` INT UNSIGNED NOT NULL,
            `tplurl` VARCHAR(1024) NULL DEFAULT '',
            `updateable` INT UNSIGNED NOT NULL DEFAULT 0,
            `_groups` VARCHAR(256) NOT NULL DEFAULT 'None'
            );''')
            self.db.site._execute('''CREATE TABLE IF NOT EXISTS `task` (
            `id` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
            `tplid` INT UNSIGNED NOT NULL,
            `userid` INT UNSIGNED NOT NULL,
            `disabled` TINYINT(1) NOT NULL DEFAULT 0,
            `init_env` BLOB NULL,
            `env` BLOB NULL,
            `session` BLOB NULL,
            `retry_count` INT NOT NULL DEFAULT 8,
            `retry_interval` INT UNSIGNED NULL,
            `last_success` INT UNSIGNED NULL,
            `last_failed` INT UNSIGNED NULL,
            `success_count` INT UNSIGNED NOT NULL DEFAULT 0,
            `failed_count` INT UNSIGNED NOT NULL DEFAULT 0,
            `last_failed_count` INT UNSIGNED NOT NULL DEFAULT 0,
            `next` INT UNSIGNED NULL DEFAULT NULL,
            `note` VARCHAR(256) NULL,
            `ctime` INT UNSIGNED NOT NULL,
            `mtime` INT UNSIGNED NOT NULL,
            `ontimeflg` INT UNSIGNED NOT NULL DEFAULT 0,
            `ontime` VARCHAR(256) NOT NULL DEFAULT '00:10:00',
            `_groups` VARCHAR(256) NOT NULL DEFAULT 'None',
            `pushsw`  VARBINARY(128) NOT NULL DEFAULT '{"logen":false,"pushen":true}',
            `newontime`  VARBINARY(256) NOT NULL DEFAULT '{"sw":false,"time":"00:10:10","randsw":false,"tz1":0,"tz2":0}'
            );''')
            self.db.site._execute('''CREATE TABLE IF NOT EXISTS `tasklog` (
            `id` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
            `taskid` INT UNSIGNED NOT NULL,
            `success` TINYINT(1) NOT NULL,
            `ctime` INT UNSIGNED NOT NULL,
            `msg` TEXT NULL
            );''')
            self.db.site._execute(
                '''CREATE TABLE IF NOT EXISTS `push_request` (
            `id` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
            `from_tplid` INT UNSIGNED NOT NULL,
            `from_userid` INT UNSIGNED NOT NULL,
            `to_tplid` INT UNSIGNED NULL,
            `to_userid` INT UNSIGNED NULL,
            `status` TINYINT NOT NULL DEFAULT 0,
            `msg` VARCHAR(1024) NULL,
            `ctime` INT UNSIGNED NOT NULL,
            `mtime` INT UNSIGNED NOT NULL,
            `atime` INT UNSIGNED NOT NULL
            );''')
            self.db.site._execute('''CREATE TABLE IF NOT EXISTS `site` (
            `id` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
            `regEn` INT UNSIGNED NOT NULL DEFAULT 1,
            `MustVerifyEmailEn` INT UNSIGNED NOT NULL DEFAULT 0,
            `logDay` INT UNSIGNED NOT NULL DEFAULT 365,
            `repos` TEXT NOT NULL
            );''')

            self.db.site._execute('''CREATE TABLE IF NOT EXISTS `pubtpl` (
                `id` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
                `name` TEXT ,
                `author` TEXT ,
                `comments` TEXT ,
                `content` TEXT ,
                `filename` TEXT,
                `date` TEXT,
                `version` TEXT,
                `url` TEXT,
                `update` TEXT,
                `reponame` TEXT,
                `repourl`  TEXT,
                `repoacc`  TEXT,
                `repobranch`  TEXT,
                `commenturl`  TEXT
            )''')

        if config.db_type == 'sqlite3':
            exec_shell = self._execute
        else:
            exec_shell = self.db.task._execute

        try:
            self.db.task.get("1", fields=('retry_count'))
        except:
            exec_shell(
                "ALTER TABLE `task` ADD `retry_count` INT NOT NULL DEFAULT 8 ")

        try:
            self.db.task.get("1", fields=('retry_interval'))
        except:
            exec_shell(
                "ALTER TABLE `task` ADD `retry_interval` INT UNSIGNED NULL ")

        try:
            self.db.task.get("1", fields=('ontimeflg'))
        except:
            exec_shell(
                "ALTER TABLE `task` ADD `ontimeflg` INT UNSIGNED NOT NULL DEFAULT 0 "
            )

        try:
            self.db.task.get("1", fields=('ontime'))
        except:
            exec_shell(
                "ALTER TABLE `task` ADD `ontime` VARCHAR(256) NOT NULL DEFAULT '00:10:00' "
            )

        try:
            self.db.user.get("1", fields=('skey'))
        except:
            exec_shell(
                "ALTER TABLE `user` ADD `skey` VARBINARY(128) NOT NULL DEFAULT '' "
            )

        try:
            self.db.user.get("1", fields=('barkurl'))
        except:
            exec_shell(
                "ALTER TABLE `user` ADD `barkurl` VARBINARY(128) NOT NULL DEFAULT '' "
            )

        try:
            self.db.user.get("1", fields=('wxpusher'))
        except:
            exec_shell(
                "ALTER TABLE `user` ADD `wxpusher` VARBINARY(128) NOT NULL DEFAULT '' "
            )

        try:
            self.db.user.get("1", fields=('noticeflg'))
        except:
            exec_shell(
                "ALTER TABLE `user` ADD `noticeflg` INT UNSIGNED NOT NULL DEFAULT 1 "
            )

        try:
            self.db.user.get("1", fields=('push_batch'))
        except:
            exec_shell(
                "ALTER TABLE `user` ADD `push_batch` VARBINARY(1024) NOT NULL DEFAULT '{\"sw\":false,\"time\":0,\"delta\":86400}' "
            )

        try:
            for user in self.db.user.list(fields=('id', 'push_batch')):
                push_batch_i = json.loads(user['push_batch'])
                if not push_batch_i.get('delta'):
                    push_batch_i['delta'] = 86400
                    self.db.user.mod(user['id'],
                                     push_batch=json.dumps(push_batch_i))
        except Exception as e:
            raise e

        try:
            self.db.tpl.get("1", fields=('tplurl'))
        except:
            exec_shell(
                "ALTER TABLE `tpl` ADD `tplurl` VARCHAR(1024) NULL DEFAULT '' "
            )

        try:
            self.db.tpl.get("1", fields=('updateable'))
        except:
            exec_shell(
                "ALTER TABLE `tpl` ADD `updateable` INT UNSIGNED NOT NULL DEFAULT 0 "
            )

        try:
            self.db.task.get("1", fields=('pushsw'))
        except:
            exec_shell(
                "ALTER TABLE `task` ADD `pushsw` VARBINARY(128) NOT NULL DEFAULT '{\"logen\":false,\"pushen\":true}' "
            )

        try:
            self.db.task.get("1", fields=('newontime'))
        except:
            exec_shell(
                "ALTER TABLE  `task` ADD `newontime`  VARBINARY(256) NOT NULL DEFAULT '{\"sw\":false,\"time\":\"00:10:10\",\"randsw\":false,\"tz1\":0,\"tz2\":0 }' "
            )

        try:
            self.db.user.get("1", fields=('logtime'))
        except:
            exec_shell(
                "ALTER TABLE `user` ADD `logtime` VARBINARY(128) NOT NULL DEFAULT '{\"en\":false,\"time\":\"20:00:00\",\"ts\":0,\"schanEn\":false,\"WXPEn\":false}' "
            )

        try:
            self.db.user.get("1", fields=('status'))
        except:
            exec_shell(
                "ALTER TABLE `user` ADD `status`  VARBINARY(1024) NOT NULL DEFAULT 'Enable' "
            )

        try:
            temp = self.db.site.get(1, fields=('regEn'))
            if not (temp):
                raise Exception("new")
        except Exception as e:
            insert = dict(
                regEn=1,
                repos=
                '{"repos":[{"reponame":"default","repourl":"https://github.com/qiandao-today/templates","repobranch":"master","repoacc":true}], "lastupdate":0}'
            )
            self.db.site._insert(**insert)

        try:
            self.db.site.get("1", fields=('MustVerifyEmailEn'))
        except:
            exec_shell(
                "ALTER TABLE `site` ADD `MustVerifyEmailEn`  INT UNSIGNED NOT NULL DEFAULT 0 "
            )

        try:
            groups = self.db.task.get("1", fields=('`groups`'))
            if groups:
                exec_shell("ALTER TABLE `task` RENAME TO `taskold`")
                if config.db_type == 'sqlite3':
                    autokey = 'AUTOINCREMENT'
                else:
                    autokey = 'AUTO_INCREMENT'
                exec_shell('''CREATE TABLE IF NOT EXISTS `task` (
                `id` INTEGER PRIMARY KEY %s,
                `tplid` INT UNSIGNED NOT NULL,
                `userid` INT UNSIGNED NOT NULL,
                `disabled` TINYINT(1) NOT NULL DEFAULT 0,
                `init_env` BLOB NULL,
                `env` BLOB NULL,
                `session` BLOB NULL,
                `retry_count` INT NOT NULL DEFAULT 8,
                `retry_interval` INT UNSIGNED NULL,
                `last_success` INT UNSIGNED NULL,
                `last_failed` INT UNSIGNED NULL,
                `success_count` INT UNSIGNED NOT NULL DEFAULT 0,
                `failed_count` INT UNSIGNED NOT NULL DEFAULT 0,
                `last_failed_count` INT UNSIGNED NOT NULL DEFAULT 0,
                `next` INT UNSIGNED NULL DEFAULT NULL,
                `note` VARCHAR(256) NULL,
                `ctime` INT UNSIGNED NOT NULL,
                `mtime` INT UNSIGNED NOT NULL,
                `ontimeflg` INT UNSIGNED NOT NULL DEFAULT 0,
                `ontime` VARCHAR(256) NOT NULL DEFAULT '00:10:00',
                `_groups` VARCHAR(256) NOT NULL DEFAULT 'None',
                `pushsw`  VARBINARY(128) NOT NULL DEFAULT '{\"logen\":false,\"pushen\":true}',
                `newontime`  VARBINARY(256) NOT NULL DEFAULT '{\"sw\":false,\"time\":\"00:10:10\",\"randsw\":false,\"tz1\":0,\"tz2\":0}'
                );''' % autokey)
                exec_shell(
                    "INSERT INTO `task` SELECT `id`,`tplid`,`userid`,`disabled`,`init_env`,`env`,`session`,`retry_count`,`retry_interval`,`last_success`,`last_failed`,`success_count`,`failed_count`,`last_failed_count`,`next`,`note`,`ctime`,`mtime`,`ontimeflg`,`ontime`,`groups`,`pushsw`,`newontime` FROM `taskold` "
                )
                exec_shell("DROP TABLE `taskold` ")
        except:
            pass

        try:
            self.db.task.get("1", fields=('_groups'))
        except:
            exec_shell(
                "ALTER TABLE `task` ADD `_groups` VARBINARY(128) NOT NULL DEFAULT 'None' "
            )

        try:
            groups = self.db.tpl.get("1", fields=('`groups`'))
            if groups:
                exec_shell("ALTER TABLE `tpl` RENAME TO `tplold`")
                if config.db_type == 'sqlite3':
                    autokey = 'AUTOINCREMENT'
                else:
                    autokey = 'AUTO_INCREMENT'
                exec_shell('''CREATE TABLE IF NOT EXISTS `tpl` (
                `id` INTEGER NOT NULL PRIMARY KEY %s,
                `userid` INT UNSIGNED NULL,
                `siteurl` VARCHAR(256) NULL,
                `sitename` VARCHAR(128) NULL,
                `banner` VARCHAR(1024) NULL,
                `disabled` TINYINT(1) NOT NULL DEFAULT 0,
                `public` TINYINT(1) NOT NULL DEFAULT 0,
                `lock` TINYINT(1) NOT NULL DEFAULT 0,
                `fork` INT UNSIGNED NULL,
                `har` MEDIUMBLOB NULL,
                `tpl` MEDIUMBLOB NULL,
                `variables` TEXT NULL,
                `interval` INT UNSIGNED NULL,
                `note` VARCHAR(1024) NULL,
                `success_count` INT UNSIGNED NOT NULL DEFAULT 0,
                `failed_count` INT UNSIGNED NOT NULL DEFAULT 0,
                `last_success` INT UNSIGNED NULL,
                `ctime` INT UNSIGNED NOT NULL,
                `mtime` INT UNSIGNED NOT NULL,
                `atime` INT UNSIGNED NOT NULL,
                `tplurl` VARCHAR(1024) NULL DEFAULT '',
                `updateable` INT UNSIGNED NOT NULL DEFAULT 0,
                `_groups` VARCHAR(256) NOT NULL DEFAULT 'None'
                );''' % autokey)
                exec_shell(
                    "INSERT INTO `tpl` SELECT `id`,`userid`,`siteurl`,`sitename`,`banner`,`disabled`,`public`,`lock`,`fork`,`har`,`tpl`,`variables`,`interval`,`note`,`success_count`,`failed_count`,`last_success`,`ctime`,`mtime`,`atime`,`tplurl`,`updateable`,`groups` FROM `tplold` "
                )
                exec_shell("DROP TABLE `tplold` ")
        except:
            pass

        try:
            self.db.tpl.get("1", fields=('_groups'))
        except:
            exec_shell(
                "ALTER TABLE `tpl` ADD `_groups` VARBINARY(128) NOT NULL DEFAULT 'None' "
            )

        try:
            tmp = self.db.site.get("1", fields=('logDay'))
            tmp = tmp['logDay']
        except Exception as e:
            if (str(e).find('no such column') > -1):
                exec_shell(
                    "ALTER TABLE `site` ADD `logDay`  INT UNSIGNED NOT NULL DEFAULT 365 "
                )
            else:
                if config.db_type == 'sqlite3':
                    autokey = ''
                else:
                    autokey = 'AUTO_INCREMENT'
                exec_shell('''CREATE TABLE IF NOT EXISTS `newsite` (
                            `id` INTEGER NOT NULL PRIMARY KEY {0},
                            `regEn` INT UNSIGNED NOT NULL DEFAULT 1,
                            `MustVerifyEmailEn` INT UNSIGNED NOT NULL DEFAULT 0,
                            `logDay` INT UNSIGNED NOT NULL DEFAULT 365
                            );'''.format(autokey))
                exec_shell(
                    'INSERT INTO `newsite` SELECT id,regEn,MustVerifyEmailEn,LogDay FROM `site`'
                )
                exec_shell("DROP TABLE `site`")
                exec_shell('CREATE TABLE `site` as select * from `newsite`')
                exec_shell("DROP TABLE `newsite`")

        try:
            self.db.user.get("1", fields=('notepad'))
        except:
            if config.db_type == 'sqlite3':
                exec_shell(
                    "ALTER TABLE `user` ADD `notepad` TEXT NOT NULL DEFAULT '' "
                )
            else:
                exec_shell("ALTER TABLE `user` ADD `notepad` TEXT NULL")

        try:
            self.db.user.get("1", fields=('diypusher'))
        except:
            exec_shell(
                "ALTER TABLE `user` ADD `diypusher`  VARCHAR(1024) NOT NULL DEFAULT '' "
            )

        try:
            self.db.user.get("1", fields=('qywx_token'))
        except:
            exec_shell(
                "ALTER TABLE `user` ADD `qywx_token`  VARCHAR(1024) NOT NULL DEFAULT '' "
            )

        try:
            self.db.user.get("1", fields=('tg_token'))
        except:
            exec_shell(
                "ALTER TABLE `user` ADD `tg_token`  VARCHAR(1024) NOT NULL DEFAULT '' "
            )

        try:
            self.db.user.get("1", fields=('dingding_token'))
        except:
            exec_shell(
                "ALTER TABLE `user` ADD `dingding_token`  VARCHAR(1024) NOT NULL DEFAULT '' "
            )

        try:
            self.db.user.get("1", fields=('password_md5'))
            for user in self.db.user.list(fields=('id', 'password',
                                                  'password_md5')):
                if isinstance(user['password_md5'], str) and re.match(
                        r'^[a-z0-9]{32}$', user['password_md5']):
                    self.db.user.mod(user['id'],
                                     password_md5=crypto.password_hash(
                                         user['password_md5'],
                                         self.db.user.decrypt(
                                             user['id'], user['password'])))
        except:
            exec_shell(
                "ALTER TABLE `user` ADD  `password_md5` VARBINARY(128) NOT NULL DEFAULT '' "
            )

        try:
            if self.db.user.get(
                    "1", fields=('cip', 'mip', 'aip')) and str.isdigit(
                        str(list(self.db.user.list(fields='cip'))[0]['cip'])):
                exec_shell("ALTER TABLE `user` RENAME TO `userold`")
                if config.db_type == 'sqlite3':
                    autokey = 'AUTOINCREMENT'
                else:
                    autokey = 'AUTO_INCREMENT'
                exec_shell('''CREATE TABLE IF NOT EXISTS `user` (
                    `id` INTEGER NOT NULL PRIMARY KEY  %s,
                    `email` VARCHAR(256) NOT NULL,
                    `email_verified` TINYINT(1) NOT NULL DEFAULT 0,
                    `password` VARBINARY(128) NOT NULL,
                    `password_md5` VARBINARY(128) NOT NULL DEFAULT '',
                    `userkey` VARBINARY(128) NOT NULL,
                    `nickname` VARCHAR(64) NULL,
                    `role` VARCHAR(128) NULL,
                    `ctime` INT UNSIGNED NOT NULL,
                    `mtime` INT UNSIGNED NOT NULL,
                    `atime` INT UNSIGNED NOT NULL,
                    `cip` VARBINARY(16) NOT NULL,
                    `mip` VARBINARY(16) NOT NULL,
                    `aip` VARBINARY(16) NOT NULL,
                    `skey` VARBINARY(128) NOT NULL DEFAULT '',
                    `barkurl` VARBINARY(128) NOT NULL DEFAULT '',
                    `wxpusher` VARBINARY(128) NOT NULL DEFAULT '',
                    `noticeflg` INT UNSIGNED NOT NULL DEFAULT 1,
                    `logtime`  VARBINARY(1024) NOT NULL DEFAULT '{"en":false,"time":"20:00:00","ts":0,"schanEn":false,"WXPEn":false}',
                    `status`  VARBINARY(1024) NOT NULL DEFAULT 'Enable',
                    `notepad` TEXT NULL,
                    `diypusher` VARBINARY(1024) NOT NULL DEFAULT '',
                    `qywx_token` VARBINARY(1024) NOT NULL DEFAULT '',
                    `tg_token` VARBINARY(1024) NOT NULL DEFAULT '',
                    `dingding_token` VARBINARY(1024) NOT NULL DEFAULT '',
                    `push_batch`  VARBINARY(1024) NOT NULL DEFAULT '{"sw":false,"time":0,"delta":86400}'
                    );''' % autokey)
                exec_shell(
                    "INSERT INTO `user` SELECT `id`,`email`,`email_verified`,`password`,`password_md5`,`userkey`,`nickname`,`role`,`ctime`,`mtime`,`atime`,`cip`,`mip`,`aip`,`skey`,`barkurl`,`wxpusher`,`noticeflg`,`logtime`,`status`,`notepad`,`diypusher`,`qywx_token`,`tg_token`,`dingding_token`,`push_batch` FROM `userold` "
                )
                exec_shell("DROP TABLE `userold` ")
        except:
            pass

        try:
            self.db.site.get("1", fields=('repos'))
        except:
            if config.db_type == 'sqlite3':
                exec_shell(
                    '''ALTER TABLE `site` ADD  `repos` TEXT NOT NULL DEFAULT '{"repos":[{"reponame":"default","repourl":"https://github.com/qiandao-today/templates","repobranch":"master","repoacc":true}], "lastupdate":0}' '''
                )
            else:
                exec_shell('''ALTER TABLE `site` ADD  `repos` TEXT ''')
                exec_shell(
                    '''UPDATE `site` SET `repos` = '{"repos":[{"reponame":"default","repourl":"https://github.com/qiandao-today/templates","repobranch":"master","repoacc":true}], "lastupdate":0}' WHERE `site`.`id` = 1 '''
                )

        try:
            tmp = self.db.site.get("1", fields=('repos'))['repos']
            if tmp == None or tmp == '':
                exec_shell(
                    '''UPDATE `site` SET `repos` = '{"repos":[{"reponame":"default","repourl":"https://github.com/qiandao-today/templates","repobranch":"master","repoacc":true}], "lastupdate":0}' WHERE `site`.`id` = 1 '''
                )
        except:
            pass

        try:
            self.db.pubtpl.get("1", fields=('commenturl'))
        except:
            if config.db_type == 'sqlite3':
                exec_shell(
                    '''ALTER TABLE `pubtpl` ADD  `commenturl` TEXT NOT NULL DEFAULT ''; '''
                )
            else:
                exec_shell('''ALTER TABLE `pubtpl` ADD  `commenturl` TEXT ''')
                exec_shell(
                    '''UPDATE `pubtpl` SET `commenturl` = '' WHERE 1=1 ''')

        return