No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

692 líneas
24 KiB

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Data.SqlClient;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Management; //�ڹ���(project)�������У��Ҽ�����������
  13. using System.DirectoryServices; //�ڹ���(project)�������У��Ҽ�����������
  14. using Microsoft.JScript; //��ȡ�ű�����������
  15. using System.Diagnostics; //�ٶȲ�����
  16. using System.IO;
  17. /// <summary>
  18. /// Summary description for BasicPage
  19. /// </summary>
  20. ///
  21. namespace Basic
  22. {
  23. public class BasicPage : System.Web.UI.Page
  24. {
  25. public string newString;//���ر���ȡ���ַ���
  26. public string connstring;
  27. public string strSql;
  28. public SqlConnection myconn;
  29. public SqlCommand mycommand;
  30. public SqlDataReader myreader;
  31. public SqlDataAdapter myadapter;
  32. public DataSet ds;
  33. public DataView dv;
  34. public DataTable tzDataTable;
  35. //
  36. public string strTitle = null;
  37. public string strDescription = null;
  38. public string strKeys = null;
  39. //�������ÿ�ʼ
  40. public string strServer = System.Configuration.ConfigurationManager.AppSettings["Server"].ToString();
  41. public string strDatabase = System.Configuration.ConfigurationManager.AppSettings["Database"].ToString();
  42. public string strUid = System.Configuration.ConfigurationManager.AppSettings["UID"].ToString();
  43. public string strPwd = System.Configuration.ConfigurationManager.AppSettings["PWD"].ToString();
  44. public void connection()
  45. {
  46. connstring = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString();
  47. myconn = new SqlConnection(connstring);
  48. }
  49. public void dbOpen()
  50. {
  51. connection();
  52. myconn.Open();
  53. }
  54. public void dbClose()
  55. {
  56. myconn.Close();
  57. }
  58. public void MessageBox(string url, string message, int type)
  59. {
  60. if (type == 0)
  61. {
  62. System.Web.HttpContext.Current.Response.Write("<script>alert('" + message + "');location.href='" + url + "';</script>");
  63. }
  64. else if (type == 1)
  65. {
  66. System.Web.HttpContext.Current.Response.Write("<script>alert('" + message + "');parent.location.href='" + url + "';</script>");
  67. }
  68. }
  69. /// <summary>
  70. ///�������ݼ�DataSet
  71. /// </summary>
  72. /// <param name="tableName"></param>
  73. /// <param name="tempStrSQL"></param>
  74. /// <returns></returns>
  75. public DataSet SelectDataBase(string tableName, string tempStrSQL)
  76. {
  77. connection();
  78. ds = new DataSet();
  79. this.myadapter = new SqlDataAdapter(tempStrSQL, this.myconn);
  80. this.myadapter.Fill(ds, tableName);
  81. return ds;
  82. }
  83. /// <summary>
  84. ///�������ݼ�DataTable
  85. /// </summary>
  86. /// <param name="tableName"></param>
  87. /// <param name="tempStrSQL"></param>
  88. /// <returns></returns>
  89. public DataTable SelectDataTable(string tempStrSQL)
  90. {
  91. connection();
  92. myconn.Open();
  93. SqlCommand cmd = new SqlCommand(tempStrSQL, this.myconn);
  94. SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  95. DataTable dt = new DataTable();
  96. dt.Load(reader);
  97. reader.Dispose();
  98. return dt;
  99. }
  100. /// <summary>
  101. /// ���ص�������
  102. /// </summary>
  103. /// <param name="tempStrSql"></param>
  104. /// <returns></returns>
  105. public SqlDataReader getRead(string tempStrSql)
  106. {
  107. connection();
  108. SqlCommand mycommand = new SqlCommand(tempStrSql, myconn);
  109. myconn.Open();
  110. SqlDataReader myreader = mycommand.ExecuteReader(CommandBehavior.CloseConnection);
  111. return myreader;
  112. }
  113. /// <summary>
  114. /// ִ�и��²���
  115. /// </summary>
  116. /// <param name="tempStrSql"></param>
  117. /// <returns></returns>
  118. public bool doExecute(string tempStrSql)
  119. {
  120. connection();
  121. SqlCommand mycommand = new SqlCommand(tempStrSql, myconn);
  122. myconn.Open();
  123. try
  124. {
  125. mycommand.ExecuteNonQuery();
  126. return true;
  127. }
  128. finally
  129. {
  130. mycommand.Dispose();
  131. myconn.Close();
  132. myconn.Dispose();
  133. }
  134. }
  135. /// <summary>
  136. /// ��ȡ�ַ���
  137. /// </summary>
  138. /// <param name="str"></param>
  139. /// <param name="length"></param>
  140. /// <returns></returns>
  141. public string CutString(string str, int length)
  142. {
  143. if (str != "")
  144. {
  145. if (str.Length > length)
  146. {
  147. newString = str.Substring(0, length) + "...";
  148. }
  149. else
  150. {
  151. newString = str;
  152. }
  153. }
  154. return newString;
  155. }
  156. /// <summary>
  157. /// �����������ͻ��˵Ļ���
  158. /// </summary>
  159. public bool ClearClientPageCache()
  160. {
  161. HttpContext.Current.Response.Buffer = true;
  162. HttpContext.Current.Response.Expires = 0;
  163. HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
  164. HttpContext.Current.Response.AddHeader("pragma", "no-cache");
  165. HttpContext.Current.Response.AddHeader("cache-control", "private");
  166. HttpContext.Current.Response.CacheControl = "no-cache";
  167. return true;
  168. }
  169. /// <summary>
  170. /// ����Jmail4.3����������FSO�ı���д
  171. /// </summary>
  172. /// <param name="_obj"></param>
  173. /// <returns></returns>
  174. public bool IsObjInstalled(string _obj)
  175. {
  176. bool IsFSOInstalled = false;
  177. try
  178. {
  179. Server.CreateObject(_obj);
  180. IsFSOInstalled = true;
  181. }
  182. catch (Exception ex)
  183. {
  184. IsFSOInstalled = false;
  185. }
  186. return IsFSOInstalled;
  187. }
  188. /// <summary>
  189. /// ��ȡ�ű���������
  190. /// </summary>
  191. /// <returns></returns>
  192. public string JiaoBenYinqing()
  193. {
  194. return GlobalObject.ScriptEngine() + "/" + GlobalObject.ScriptEngineMajorVersion() + "." + GlobalObject.ScriptEngineMinorVersion() + "." + GlobalObject.ScriptEngineBuildVersion();
  195. }
  196. /// <summary>
  197. /// ��ȡ������ip
  198. /// </summary>
  199. /// <returns></returns>
  200. public string ServerIp()
  201. {
  202. string stringMAC = "";
  203. string stringIP = "";
  204. ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");
  205. ManagementObjectCollection MOC = MC.GetInstances();
  206. foreach (ManagementObject MO in MOC)
  207. {
  208. if ((bool)MO["IPEnabled"] == true)
  209. {
  210. stringMAC += MO["MACAddress"].ToString();
  211. //TextMAC.Text = stringMAC.ToString();
  212. string[] IPAddresses = (string[])MO["IPAddress"];
  213. if (IPAddresses.Length > 0) stringIP = IPAddresses[0];
  214. }
  215. }
  216. return stringIP;
  217. }
  218. /// <summary>
  219. /// ��ȡ����ϵͳ
  220. /// </summary>
  221. /// <returns></returns>
  222. public string ServerOS()
  223. {
  224. return System.Environment.OSVersion.ToString();
  225. }
  226. /// <summary>
  227. /// ���������ٶȲ���
  228. /// </summary>
  229. /// <returns></returns>
  230. public string IntTest()
  231. {
  232. string Value;
  233. Stopwatch timer = new Stopwatch();
  234. long total = 0;
  235. timer.Start();
  236. for (int i = 1; i <= 500000; i++)
  237. {
  238. total += i;
  239. }
  240. timer.Stop();
  241. decimal micro = timer.Elapsed.Ticks / 10m;
  242. Value = "�����������ԣ����ڽ���50���μӷ�����......�����ɣ�<font color=red>" + micro.ToString() + "΢��" + "</font>";
  243. return Value;
  244. }
  245. /// <summary>
  246. /// ʵ�������ٶȲ���
  247. /// </summary>
  248. /// <returns></returns>
  249. public string FloatTest()
  250. {
  251. string Value;
  252. Stopwatch timer = new Stopwatch();
  253. long total = 0;
  254. timer.Start();
  255. for (int i = 1; i <= 200000; i++)
  256. {
  257. total *= total;
  258. }
  259. timer.Stop();
  260. decimal micro = timer.Elapsed.Ticks / 10m;
  261. Value = "�������������ԣ����ڽ���20���γ˷�����......�����ɣ�<font color=red>" + micro.ToString() + "΢��" + "</font>";
  262. return Value;
  263. }
  264. /// <summary>
  265. /// �������ݿ�
  266. /// </summary>
  267. /// <param name="strDbName"></param>
  268. /// <param name="strFileName"></param>
  269. /// <param name="strRname"></param>
  270. /// <returns></returns>
  271. public string DbBackup(string strDbName, string strFileName, string strRname)
  272. {
  273. try
  274. {
  275. SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
  276. SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
  277. oSQLServer.LoginSecure = false;
  278. oSQLServer.Connect(strServer, strUid, strPwd);
  279. oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
  280. oBackup.Database = strDbName;
  281. string Path = strFileName + strRname;
  282. oBackup.Files = Path;
  283. oBackup.BackupSetName = strRname;
  284. oBackup.BackupSetDescription = "�������ݿ�";
  285. oBackup.Initialize = true;
  286. oBackup.SQLBackup(oSQLServer);
  287. oSQLServer.DisConnect();
  288. return "ok";
  289. }
  290. catch (Exception ex)
  291. {
  292. return ex.Message;
  293. }
  294. }
  295. /// <summary>
  296. /// ��ԭ���ݿ�
  297. /// </summary>
  298. /// <param name="strDbName"></param>
  299. /// <param name="strFileName"></param>
  300. /// <param name="strRname"></param>
  301. /// <returns></returns>
  302. public bool DbRestore(string strDbName, string strFileName, string strRname)
  303. {
  304. strDbName = strDatabase;
  305. strFileName = "";
  306. SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
  307. SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
  308. try
  309. {
  310. oSQLServer.LoginSecure = false;
  311. oSQLServer.Connect(strServer, strUid, strPwd);
  312. oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
  313. oRestore.Database = strDbName;
  314. oRestore.Files = strFileName + strRname;
  315. oRestore.FileNumber = 1;
  316. oRestore.ReplaceDatabase = true;
  317. oRestore.SQLRestore(oSQLServer);
  318. return true;
  319. }
  320. catch
  321. {
  322. return false;
  323. }
  324. }
  325. /// <summary>
  326. /// ��ȡϵͳʹ��������
  327. /// </summary>
  328. /// <returns></returns>
  329. public string readConame()
  330. {
  331. string strConame;
  332. SqlDataReader Myreader;
  333. Myreader = getRead("select webname from tbset where id=1");
  334. if (Myreader.Read())
  335. {
  336. strConame = Myreader["webname"].ToString();
  337. }
  338. else
  339. {
  340. strConame = "<font color=red>��������(���Ƚ���ϵͳ��������)</font>";
  341. }
  342. Myreader.Close();
  343. return strConame;
  344. }
  345. /// <summary>
  346. /// ͳ�Ʒ������ռ�ռ������
  347. /// </summary>
  348. /// <param name="dirPath"></param>
  349. /// <returns></returns>
  350. public long GetDirectoryLength(string dirPath)
  351. {
  352. //�жϸ�����·���Ƿ�����,�������������˳�
  353. if (!Directory.Exists(dirPath))
  354. return 0;
  355. long len = 0;
  356. //����һ��DirectoryInfo����
  357. DirectoryInfo di = new DirectoryInfo(dirPath);
  358. //ͨ��GetFiles����,��ȡdiĿ¼�е������ļ��Ĵ�С
  359. foreach (FileInfo fi in di.GetFiles())
  360. {
  361. len += fi.Length;
  362. }
  363. //��ȡdi�����е��ļ���,���浽һ���µĶ���������,�Խ��еݹ�
  364. DirectoryInfo[] dis = di.GetDirectories();
  365. if (dis.Length > 0)
  366. {
  367. for (int i = 0; i < dis.Length; i++)
  368. {
  369. len += GetDirectoryLength(dis[i].FullName);
  370. }
  371. }
  372. return len;
  373. }
  374. /// <summary>
  375. /// ����Ҫ������IP
  376. /// </summary>
  377. /// <param name="strAddIp"></param>
  378. /// <returns></returns>
  379. public int lockip(string strAddIp)
  380. {
  381. int returnId = 0;
  382. int intId = 1;
  383. bool bsign = false;
  384. string strIpGroup;
  385. string strIp = strAddIp;
  386. SqlDataReader myreader = getRead("select ip from TbIp where id=1");
  387. if (myreader.Read())
  388. {
  389. string[] strIPzu = myreader["ip"].ToString().Split('|');
  390. for (int i = 0; i < strIPzu.Length; i++)
  391. {
  392. if (strIp == strIPzu[i])
  393. {
  394. bsign = true;
  395. }
  396. }
  397. strIpGroup = myreader["ip"].ToString();
  398. myreader.Close();
  399. if (bsign == false)
  400. {
  401. if (strIpGroup == "")
  402. {
  403. strIpGroup = strIp;
  404. }
  405. else
  406. {
  407. strIpGroup = strIpGroup + "|" + strIp;
  408. }
  409. if (doExecute("update tbIp set ip='" + strIpGroup + "' where id=1"))
  410. {
  411. //Response.Redirect("Success.aspx?message=IP�����ɹ���&url=IpManage.aspx");
  412. returnId = 1;
  413. }
  414. }
  415. else if (bsign == true)
  416. {
  417. //Response.Redirect("Error.aspx?message=����ʧ�ܣ���IP�ѱ�������&url=IpManage.aspx");
  418. returnId = 2;
  419. }
  420. }
  421. else
  422. {
  423. myreader.Close();
  424. if (doExecute("insert into tbIp(id,ip)values(" + intId + ",'" + strIp + "')"))
  425. {
  426. //Response.Redirect("Success.aspx?message=IP�����ɹ���&url=IpManage.aspx");
  427. returnId = 3;
  428. }
  429. }
  430. return returnId;
  431. }
  432. /// <summary>
  433. /// ��ʾ������IP
  434. /// </summary>
  435. /// <returns></returns>
  436. public string showLockIp()
  437. {
  438. string strIp = "";
  439. SqlDataReader myreader = getRead("select ip from tbIp where id=1");
  440. if (myreader.Read())
  441. {
  442. strIp = myreader["ip"].ToString();
  443. }
  444. myreader.Close();
  445. return strIp.Trim();
  446. }
  447. /// <summary>
  448. /// ����������IP�б�
  449. /// </summary>
  450. /// <param name="strIps"></param>
  451. /// <returns></returns>
  452. public bool updateIp(string strIps)
  453. {
  454. bool sign = false;
  455. SqlDataReader myreader = getRead("select * from tbIp where id=1");
  456. if (myreader.Read())
  457. {
  458. if (doExecute("update tbIp set ip='" + strIps + "' where id=1"))
  459. {
  460. sign = true;
  461. }
  462. }
  463. else
  464. {
  465. int id = 1;
  466. if (doExecute("insert into tbIp(id,ip)values(" + id + ",'" + strIps + "')"))
  467. {
  468. sign = true;
  469. }
  470. }
  471. myreader.Close();
  472. return sign;
  473. }
  474. /// <summary>
  475. /// �ж��Ƿ��ر���վ
  476. /// </summary>
  477. /// <returns></returns>
  478. public void ShowWebState()
  479. {
  480. string state = "1";
  481. SqlDataReader myreader = getRead("select state from TBset where id=1");
  482. if (myreader.Read())
  483. {
  484. state = myreader["state"].ToString();
  485. if (state == "0")
  486. {
  487. System.Web.HttpContext.Current.Response.Write("<script>parent.location.href='Close.htm'</script>");
  488. }
  489. }
  490. myreader.Close();
  491. }
  492. /// <summary>
  493. /// ��ȡTitle,Description,Keys
  494. /// </summary>
  495. public void showWebTitle()
  496. {
  497. int intId = 1;
  498. SqlDataReader Myreader = getRead("select * from TBset where id=" + intId + "");
  499. if (Myreader.Read())
  500. {
  501. strTitle = Myreader["webname"].ToString();
  502. strDescription = Myreader["webdescription"].ToString();
  503. strKeys = Myreader["webkeys"].ToString();
  504. }
  505. Myreader.Close();
  506. StartProcessRequest();//�����û������Ƿ�����
  507. ShowWebState();//�ж���վ�Ƿ��ر�
  508. }
  509. /// <summary>
  510. /// �����û������Ƿ�����
  511. /// </summary>
  512. ///
  513. public static void StartProcessRequest()
  514. {
  515. try
  516. {
  517. string getkeys = "";
  518. //string sqlErrorPage = System.Configuration.ConfigurationSettings.AppSettings["CustomErrorPage"].ToString();
  519. if (System.Web.HttpContext.Current.Request.QueryString != null)
  520. {
  521. for (int i = 0; i < System.Web.HttpContext.Current.Request.QueryString.Count; i++)
  522. {
  523. getkeys = System.Web.HttpContext.Current.Request.QueryString.Keys[i];
  524. if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys], 0))
  525. {
  526. //System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true");
  527. System.Web.HttpContext.Current.Response.Write("<script>alert('�����Ƿ��ύ��');history.back();</script>");
  528. System.Web.HttpContext.Current.Response.End();
  529. }
  530. }
  531. }
  532. if (System.Web.HttpContext.Current.Request.Form != null)
  533. {
  534. for (int i = 0; i < System.Web.HttpContext.Current.Request.Form.Count; i++)
  535. {
  536. getkeys = System.Web.HttpContext.Current.Request.Form.Keys[i];
  537. if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys], 1))
  538. {
  539. //System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true");
  540. System.Web.HttpContext.Current.Response.Write("<script>alert('�����Ƿ��ύ��');history.back();</script>");
  541. System.Web.HttpContext.Current.Response.End();
  542. }
  543. }
  544. }
  545. }
  546. catch
  547. {
  548. // ��������: �����û��ύ��Ϣ!
  549. }
  550. }
  551. /**/
  552. /// <summary>
  553. /// �����û������Ƿ�����
  554. /// </summary>
  555. /// <param name="Str">�����û��ύ����</param>
  556. /// <returns>�����Ƿ�����SQLע��ʽ��������</returns>
  557. private static bool ProcessSqlStr(string Str, int type)
  558. {
  559. string SqlStr;
  560. if (type == 1)
  561. SqlStr = "exec|insert|select|delete|update|count|chr|mid|master|truncate|char|declare";
  562. else
  563. SqlStr = "'|and|exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare|>|<";
  564. bool ReturnValue = true;
  565. try
  566. {
  567. if (Str != "")
  568. {
  569. string[] anySqlStr = SqlStr.Split('|');
  570. foreach (string ss in anySqlStr)
  571. {
  572. if (Str.IndexOf(ss) >= 0)
  573. {
  574. ReturnValue = false;
  575. }
  576. }
  577. }
  578. }
  579. catch
  580. {
  581. ReturnValue = false;
  582. }
  583. return ReturnValue;
  584. }
  585. /// <summary>
  586. /// �ж��Ƿ�������
  587. /// </summary>
  588. /// <param name="oText"></param>
  589. /// <returns>bool</returns>
  590. public bool IsNumberic(string oText)//�ж��Ƿ�������
  591. {
  592. try
  593. {
  594. int var1 = System.Convert.ToInt32(oText);
  595. if (var1 < 0)
  596. {
  597. return false;
  598. }
  599. return true;
  600. }
  601. catch
  602. {
  603. return false;
  604. }
  605. }
  606. /// <summary>
  607. /// ��ȡ�ַ���
  608. /// </summary>
  609. /// <param name="inputString"></param>
  610. /// <param name="length"></param>
  611. /// <returns>string</returns>
  612. public string SubString(string inputString, int length)
  613. {
  614. if (Encoding.UTF8.GetByteCount(inputString) <= length * 2)
  615. {
  616. return inputString;
  617. }
  618. ASCIIEncoding ascii = new ASCIIEncoding();
  619. int tempLen = 0;
  620. string tempString = "";
  621. byte[] s = ascii.GetBytes(inputString);
  622. for (int i = 0; i < s.Length; i++)
  623. {
  624. if ((int)s[i] == 63)
  625. {
  626. tempLen += 2;
  627. }
  628. else
  629. {
  630. tempLen += 1;
  631. }
  632. tempString += inputString.Substring(i, 1);
  633. if (tempLen >= (length - 1) * 2)
  634. break;
  635. }
  636. //�����ع������ϰ���ʡ�Ժ�
  637. if (System.Text.Encoding.Default.GetBytes(inputString).Length > length)
  638. tempString += "";
  639. return tempString;
  640. }
  641. /// <summary>
  642. /// ��ʾ��¼״̬
  643. /// </summary>
  644. public void ShowLoginState()
  645. {
  646. if (Session["j95fn3839vfn93h9n29n"] == null)
  647. {
  648. System.Web.HttpContext.Current.Response.Write("<script>parent.location.href='Default.aspx'</script>");
  649. }
  650. }
  651. #region JS��ʾ============================================
  652. /// <summary>
  653. /// ���ӱ༭ɾ����ʾ
  654. /// </summary>
  655. /// <param name="msgtitle">��ʾ����</param>
  656. /// <param name="url">���ص�ַ</param>
  657. /// <param name="msgcss">CSS��ʽ</param>
  658. public void JscriptMsg(string msgtitle, string url, string msgcss)
  659. {
  660. string msbox = "parent.jsprint(\"" + msgtitle + "\", \"" + url + "\", \"" + msgcss + "\")";
  661. ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", msbox, true);
  662. }
  663. /// <summary>
  664. /// ���ش����������ӱ༭ɾ����ʾ
  665. /// </summary>
  666. /// <param name="msgtitle">��ʾ����</param>
  667. /// <param name="url">���ص�ַ</param>
  668. /// <param name="msgcss">CSS��ʽ</param>
  669. /// <param name="callback">JS�ص�����</param>
  670. public void JscriptMsg(string msgtitle, string url, string msgcss, string callback)
  671. {
  672. string msbox = "parent.jsprint(\"" + msgtitle + "\", \"" + url + "\", \"" + msgcss + "\", " + callback + ")";
  673. ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", msbox, true);
  674. }
  675. #endregion
  676. }
  677. }