Esempio n. 1
0
 /// <summary>
 /// registration
 /// </summary>
 /// <param name="userid"></param>
 /// <param name="password"></param>
 /// <param name="power">power</param>
 /// state = 0:to be confirmed 1:confirmed but disabled 2:normal
 /// <returns></returns>
 public bool Registration(UserInfo userinfo)
 {
     Mutex.WaitAll(M);
     SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
     cmd.Connection = conn;
     string sql = "INSERT INTO users VALUES('" + userinfo.Id + "','" + userinfo.Password + "','" + ((int)userinfo.Power).ToString() + "','" + "2','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "', '0')";
     cmd.CommandText = sql;
     try
     {
         cmd.ExecuteNonQuery();
         cmd.Dispose();
         Read.ReleaseMutex();
         Write.ReleaseMutex();
         return true;
     }
     catch (Exception e)
     {
         log.Error(e.ToString());
         cmd.Dispose();
         Read.ReleaseMutex();
         Write.ReleaseMutex();
         return false;
     }
 }
Esempio n. 2
0
 public bool Update_Count(ref List<Corpus> list_corpus, UserInfo userinfo)
 {
     Mutex.WaitAll(M);
     SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
     cmd.Connection = conn;
     string sql = "SELECT count FROM users " + "WHERE username = '******'";
     try
     {
         cmd.CommandText = sql;
         SQLiteDataReader reader = cmd.ExecuteReader();
         if (reader.Read())
         {
             int count = list_corpus.Count + Int32.Parse(reader.GetString(0));
             sql = "UPDATE users SET count = '" + count.ToString() + "' WHERE username = '******';";
             sql = sql + "INSERT INTO users_tid VALUES ";
             string tmp = "";
             foreach (Corpus item in list_corpus)
             {
                 tmp = tmp + "('" + item.Id + "', '" + userinfo.Id + "'),";
             }
             sql = sql + tmp.Substring(0, tmp.Length - 1) + ";";
             cmd.CommandText = sql;
             cmd.ExecuteNonQuery();
             cmd.Dispose();
             Read.ReleaseMutex();
             Write.ReleaseMutex();
             return true;
         }
         cmd.Dispose();
         Read.ReleaseMutex();
         Write.ReleaseMutex();
         return false;
     }
     catch (Exception e)
     {
         cmd.Dispose();
         Read.ReleaseMutex();
         Write.ReleaseMutex();
         log.Debug(e.ToString());
         throw;
     }
 }
Esempio n. 3
0
 public List<UserInfo> GetAllInfo()
 {
     Write.WaitOne();
     SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
     cmd.Connection = conn;
     string sql = "SELECT * FROM users";
     try
     {
         cmd.CommandText = sql;
         SQLiteDataReader reader = cmd.ExecuteReader();
         List<UserInfo> List_u = new List<UserInfo>();
         while (reader.Read())
         {
             UserInfo tmp = new UserInfo(reader.GetString(0), reader.GetString(1), (power)Int32.Parse((reader.GetString(2))), reader.GetString(3), reader.GetString(4), Int32.Parse(reader.GetString(5)));
             List_u.Add(tmp);
         }
         cmd.Dispose();
         Write.ReleaseMutex();
         return List_u;
     }
     catch (Exception e)
     {
         log.Debug(e.ToString());
         cmd.Dispose();
         Write.ReleaseMutex();
         throw;
     }
 }
Esempio n. 4
0
 //public bool ConfirmAndEnable_State(UserInfo userinfo)
 //{
 //    Mutex.WaitAll(M);
 //    SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
 //    cmd.Connection = conn;
 //    string sql = "UPDATE users SET state = '2' WHERE username = '******'";
 //    try
 //    {
 //        cmd.CommandText = sql;
 //        cmd.ExecuteNonQuery();
 //        cmd.Dispose();
 //        Read.ReleaseMutex();
 //        Write.ReleaseMutex();
 //        return true;
 //    }
 //    catch (Exception e)
 //    {
 //        log.Error(e.ToString());
 //        cmd.Dispose();
 //        Read.ReleaseMutex();
 //        Write.ReleaseMutex();
 //        return false;
 //    }
 //}
 //public bool ConfirmAndEnable_State(ref List<UserInfo> list_userinfo)
 //{
 //    string value = "";
 //    foreach (UserInfo item in list_userinfo)
 //    {
 //        value = value + "'" + item.Id + "',";
 //    }
 //    value = value.Substring(0, value.Length - 1);
 //    Mutex.WaitAll(M);
 //    SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
 //    cmd.Connection = conn;
 //    try
 //    {
 //        string sql = "UPDATE users SET state = '2' WHERE username IN (" + value + ")";
 //        cmd.CommandText = sql;
 //        cmd.ExecuteNonQuery();
 //        cmd.Dispose();
 //        Read.ReleaseMutex();
 //        Write.ReleaseMutex();
 //        return true;
 //    }
 //    catch (Exception e)
 //    {
 //        log.Error(e.ToString());
 //        cmd.Dispose();
 //        Read.ReleaseMutex();
 //        Write.ReleaseMutex();
 //        return false;
 //    }
 //}
 //public bool Disable_State(UserInfo userinfo)
 //{
 //    Mutex.WaitAll(M);
 //    SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
 //    cmd.Connection = conn;
 //    string sql = "UPDATE users SET state = '1' WHERE username = '******'";
 //    try
 //    {
 //        cmd.CommandText = sql;
 //        cmd.ExecuteNonQuery();
 //        cmd.Dispose();
 //        Read.ReleaseMutex();
 //        Write.ReleaseMutex();
 //        return true;
 //    }
 //    catch (Exception e)
 //    {
 //        log.Error(e.ToString());
 //        cmd.Dispose();
 //        Read.ReleaseMutex();
 //        Write.ReleaseMutex();
 //        return false;
 //    }
 //}
 //public bool Disable_State(ref List<UserInfo> list_userinfo)
 //{
 //    string value = "";
 //    foreach (UserInfo item in list_userinfo)
 //    {
 //        value = value + "'" + item.Id + "',";
 //    }
 //    value = value.Substring(0, value.Length - 1);
 //    Mutex.WaitAll(M);
 //    SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
 //    cmd.Connection = conn;
 //    try
 //    {
 //        string sql = "UPDATE users SET state = '1' WHERE username IN (" + value + ")";
 //        cmd.CommandText = sql;
 //        cmd.ExecuteNonQuery();
 //        cmd.Dispose();
 //        Read.ReleaseMutex();
 //        Write.ReleaseMutex();
 //        return true;
 //    }
 //    catch (Exception e)
 //    {
 //        log.Error(e.ToString());
 //        cmd.Dispose();
 //        Read.ReleaseMutex();
 //        Write.ReleaseMutex();
 //        return false;
 //    }
 //}
 //public bool Enable_State(UserInfo userinfo)
 //{
 //    Mutex.WaitAll(M);
 //    SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
 //    cmd.Connection = conn;
 //    string sql = "UPDATE users SET state = '2' WHERE username = '******'";
 //    try
 //    {
 //        cmd.CommandText = sql;
 //        cmd.ExecuteNonQuery();
 //        cmd.Dispose();
 //        Read.ReleaseMutex();
 //        Write.ReleaseMutex();
 //        return true;
 //    }
 //    catch (Exception e)
 //    {
 //        log.Error(e.ToString());
 //        cmd.Dispose();
 //        return false;
 //    }
 //}
 //public bool Enable_State(ref List<UserInfo> list_userinfo)
 //{
 //    string value = "";
 //    foreach (UserInfo item in list_userinfo)
 //    {
 //        value = value + "'" + item.Id + "',";
 //    }
 //    value = value.Substring(0, value.Length - 1);
 //    Mutex.WaitAll(M);
 //    SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
 //    cmd.Connection = conn;
 //    try
 //    {
 //        string sql = "UPDATE users SET state = '2' WHERE username IN (" + value + ")";
 //        cmd.CommandText = sql;
 //        cmd.ExecuteNonQuery();
 //        cmd.Dispose();
 //        Read.ReleaseMutex();
 //        Write.ReleaseMutex();
 //        return true;
 //    }
 //    catch (Exception e)
 //    {
 //        log.Error(e.ToString());
 //        cmd.Dispose();
 //        Read.ReleaseMutex();
 //        Write.ReleaseMutex();
 //        return false;
 //    }
 //}
 public state Get_State(UserInfo userinfo)
 {
     Write.WaitOne();
     SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
     cmd.Connection = conn;
     string sql = "SELECT state FROM users" + "WHERE username = '******'";
     try
     {
         cmd.CommandText = sql;
         SQLiteDataReader reader = cmd.ExecuteReader();
         if (reader.Read())
         {
             state t = (state)Int32.Parse(reader.GetString(0));
             cmd.Dispose();
             Write.ReleaseMutex();
             return t;
         }
         cmd.Dispose();
         Write.ReleaseMutex();
         return state.tobeconfirm;
     }
     catch (Exception e)
     {
         cmd.Dispose();
         Write.ReleaseMutex();
         log.Debug(e.ToString());
         throw;
     }
 }
Esempio n. 5
0
 public bool Del_User(UserInfo userinfo)
 {
     Mutex.WaitAll(M);
     SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
     cmd.Connection = conn;
     string sql = "DELETE FROM users WHERE username = '******'";
     try
     {
         cmd.CommandText = sql;
         cmd.ExecuteNonQuery();
         cmd.Dispose();
         Read.ReleaseMutex();
         Write.ReleaseMutex();
         return true;
     }
     catch (Exception e)
     {
         log.Error(e.ToString());
         cmd.Dispose();
         Read.ReleaseMutex();
         Write.ReleaseMutex();
         return false;
     }
 }
Esempio n. 6
0
 public bool Check(UserInfo userinfo)
 {
     Mutex.WaitAll(M);
     SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
     cmd.Connection = conn;
     string sql = "UPDATE users SET lastlogin = '******' WHERE username = '******' AND password = '******'";
     try
     {
         cmd.CommandText = sql;
         cmd.ExecuteNonQuery();
         cmd.Dispose();
         Read.ReleaseMutex();
         Write.ReleaseMutex();
         return true;
     }
     catch (Exception e)
     {
         log.Error(e.ToString());
         cmd.Dispose();
         Read.ReleaseMutex();
         Write.ReleaseMutex();
         return false;
     }
 }
        private void Interaction(object oclient)
        {
            int recv;
            byte[] data = new byte[1024];
            Socket client = (Socket)oclient;
            IPEndPoint clientip = (IPEndPoint)client.RemoteEndPoint;

            recv = client.Receive(data);
            string enter_info = Encoding.UTF8.GetString(data, 0, recv);

            UserInfo userinfo = new UserInfo();

            try
            {
                #region login
                if (enter_info.IndexOf("&#LOG") != -1)
                {
                    if (Get_userinfo(enter_info, ref userinfo))
                    {
                        //user has logined at somewhere
                        if (LoginUser2Socket.ContainsKey(userinfo.Id))
                        {
                            string confirm_info = "2";
                            data = Encoding.UTF8.GetBytes(confirm_info);
                            client.Send(data, SocketFlags.None);
                            recv = client.Receive(data);
                            string feedback = Encoding.UTF8.GetString(data, 0, recv);
                            if (feedback == "1")
                            {
                                Socket c = (Socket)LoginUser2Socket[userinfo.Id];
                                c.Dispose();
                                c.Close();
                                LoginUser2Socket.Remove(userinfo.Id);
                            }
                            client.Dispose();
                            client.Close();
                            return;
                        }

                        if (Authenticate(userinfo))
                        {
                            log.Info("client IP: " + clientip.Address + "client port: " + clientip.Port + "userid: " + userinfo.Id + " is connected\n");
                            string confirm_info = "1";
                            data = Encoding.UTF8.GetBytes(confirm_info);
                            client.Send(data, SocketFlags.None);

                            LoginUser2Socket[userinfo.Id] = client;

                            #region translator
                            if (userinfo.Power == power.translator)
                            {
                                while (true)
                                {
                                    data = new byte[1024 * 1024];
                                    recv = client.Receive(data);
                                    if (recv == 0)
                                    {
                                        break;
                                    }
                                    string tmp = Encoding.UTF8.GetString(data, 0, recv);
                                    if (tmp.IndexOf("&#GET") != -1)
                                    {
                                        try
                                        {
                                            int get_textnum = Int32.Parse(tmp.Substring(5));
                                            List<Corpus> list_C = DB.Access_read(get_textnum);
                                            if (list_C != null)
                                            {
                                                string texts = "&#";
                                                foreach (Corpus item in list_C)
                                                {
                                                    texts = texts +
                                                        item.Id + "::" + //text id
                                                         item.Title + "::" + //text title
                                                        item.Body + "&#"; //text body
                                                }
                                                byte[] corpusdata = Encoding.UTF8.GetBytes(texts);
                                                client.Send(corpusdata, SocketFlags.None);
                                            }
                                            else
                                            {
                                                client.Send(Encoding.UTF8.GetBytes("NO data to be translated!"), SocketFlags.None);
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            LoginUser2Socket.Remove(userinfo.Id);
                                            client.Dispose();
                                            client.Close();
                                            log.Error(e.ToString());
                                        }
                                    }
                                    else if (tmp.IndexOf("&#POST") != -1)
                                    {   //protocol:&#POST(NUM)&#corpus1(id::checker::translator::title::body)&#corpus2()&#...&#
                                        string[] texts_data = tmp.Split(new string[] { "&#" }, StringSplitOptions.RemoveEmptyEntries);
                                        int post_textnum = Int32.Parse(texts_data[0].Substring(4));
                                        List<Corpus> list_c = new List<Corpus>();
                                        for (int i = 1; i < post_textnum+1; i++)
                                        {
                                            Corpus C_tmp = new Corpus();
                                            string[] items = texts_data[i].Split(new string[] { "::" }, StringSplitOptions.RemoveEmptyEntries);
                                            C_tmp.Id = items[0];
                                            C_tmp.Checker = items[1];
                                            C_tmp.Translator = items[2];
                                            C_tmp.Title = items[3];
                                            C_tmp.Body = items[4];
                                            C_tmp.State = "2";
                                            C_tmp.Submit = DateTime.Now.ToString();
                                            list_c.Add(C_tmp);
                                        }
                                        DB.Access_write(ref list_c);
                                        UserOP.Update_Count(ref list_c, userinfo);
                                    }
                                }
                                LoginUser2Socket.Remove(userinfo.Id);
                                client.Dispose();
                                client.Close();
                            }
                            #endregion

                            #region administrator
                            else if (userinfo.Power == power.admin)
                            {
                                while (true)
                                {
                                    data = new byte[1024 * 1024];
                                    recv = client.Receive(data);
                                    if (recv == 0)
                                    {
                                        break;
                                    }
                                    string tmp = Encoding.UTF8.GetString(data, 0, recv);
                                    if (tmp.IndexOf("&#GET") != -1)
                                    {
                                        try
                                        {
                                            List<UserInfo> list_u = UserOP.GetAllInfo();
                                            if (list_u != null)
                                            {
                                                string users = "&#";
                                                foreach (UserInfo item in list_u)
                                                {
                                                    string Online = "0";
                                                    if (LoginUser2Socket.ContainsKey(item.Id))
                                                    {
                                                        Online = "1";
                                                    }
                                                    users = users +
                                                        item.Id + "::" + //id
                                                         item.Password + "::" + //password
                                                        ((int)item.Power) + "::" + //power
                                                        item.State+ "::" +
                                                        item.Lastlogin + "::" +
                                                        item.Count + "::" +
                                                        Online + "&#"; //
                                                }
                                                data = Encoding.UTF8.GetBytes(users);
                                                client.Send(data, SocketFlags.None);
                                            }
                                            else
                                            {
                                                client.Send(Encoding.UTF8.GetBytes("NO data to be translated!"), SocketFlags.None);
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            LoginUser2Socket.Remove(userinfo.Id);
                                            client.Dispose();
                                            client.Close();
                                            log.Error(e.ToString());
                                        }
                                    }
                                    else
                                    {
                                        //protocol:&#D(C)sum&#user1(id::password::power::state)&#user2()&#...&#
                                        string[] post_data = tmp.Split(new string[] { "&#" }, StringSplitOptions.RemoveEmptyEntries);
                                        char tag = post_data[0][0];
                                        int post_usernum = Int32.Parse(post_data[0].Substring(1));
                                        List<UserInfo> list_u = new List<UserInfo>();
                                        for (int i = 1; i < post_usernum+1; i++)
                                        {
                                            UserInfo U_tmp = new UserInfo();
                                            string[] li_item = post_data[i].Split(new string[] { "::" }, StringSplitOptions.RemoveEmptyEntries);
                                            U_tmp.Id = li_item[0];
                                            U_tmp.Password = li_item[1];
                                            U_tmp.Power = (power)Int32.Parse(li_item[2]);
                                            U_tmp.State = li_item[3];
                                            list_u.Add(U_tmp);
                                        }

                                        switch (tag)
                                        {
                                            case 'D'://delete user info
                                                if (UserOP.Del_User(ref list_u))
                                                    client.Send(Encoding.UTF8.GetBytes("1"), SocketFlags.None);
                                                else
                                                    client.Send(Encoding.UTF8.GetBytes("0"), SocketFlags.None);
                                                break;
                                            case 'C'://disable user
                                                if (UserOP.Update_State(ref list_u))
                                                {
                                                    client.Send(Encoding.UTF8.GetBytes("1"), SocketFlags.None);
                                                }
                                                else
                                                    client.Send(Encoding.UTF8.GetBytes("0"), SocketFlags.None);
                                                break;
                                            default:
                                                break;
                                        }
                                    }
                                }
                                LoginUser2Socket.Remove(userinfo.Id);
                                client.Dispose();
                                client.Close();
                            }
                            #endregion
                        }
                        else
                        {
                            string Error = "0";
                            data = Encoding.UTF8.GetBytes(Error);
                            client.Send(data, SocketFlags.None);
                            client.Dispose();
                            client.Close();
                        }
                    }
                    else
                    {
                        string Error = "0";
                        data = Encoding.UTF8.GetBytes(Error);
                        client.Send(data, SocketFlags.None);
                        client.Dispose();
                        client.Close();
                    }
                }
                #endregion

                #region user registration
                else if (enter_info.IndexOf("&#REG") != -1)
                {
                    if (Get_userinfo(enter_info, ref userinfo))
                    {
                        if (UserOP.Registration(userinfo))
                        {
                            string Success = "1";
                            data = Encoding.UTF8.GetBytes(Success);
                            client.Send(data, SocketFlags.None);
                            client.Dispose();
                            client.Close();
                        }
                        else
                        {
                            string fail = "0";
                            data = Encoding.UTF8.GetBytes(fail);
                            client.Send(data, SocketFlags.None);
                            client.Dispose();
                            client.Close();
                        }
                    }
                    else
                    {
                        string fail = "0";
                        data = Encoding.UTF8.GetBytes(fail);
                        client.Send(data, SocketFlags.None);
                        client.Dispose();
                        client.Close();
                    }
                }
                #endregion
            }
            catch (Exception exc)
            {
                client.Dispose();
                client.Close();
                log.Debug(exc.ToString());
            }
        }
 /// <summary>
 /// get the user name and password from the string
 /// </summary>
 /// <param name="str">example:&#REG&#IDusera&#PW1234&#PO2&#</param>
 /// <param name="user_name"></param>
 /// <param name="password"></param>
 /// <returns></returns>
 private bool Get_userinfo(string str, ref UserInfo userinfo)
 {
     if (str.IndexOf("&#") == -1)
     {
         return false;
     }
     string[] tmp = str.Split(new string[] { "&#" }, StringSplitOptions.RemoveEmptyEntries);
     userinfo.Id = tmp[1];
     userinfo.Password = tmp[2];
     userinfo.Power = (power)Int32.Parse(tmp[3]);
     return true;
 }
 private bool Authenticate(UserInfo userinfo)
 {
     if (UserOP.Check(userinfo))
     {
         return true;
     }
     return false;
 }