PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/php/timers/subscribe2.php

https://gitlab.com/kukla21305/fhq
PHP | 143 lines | 107 code | 15 blank | 21 comment | 18 complexity | 7e53f08109a318b50f9006a37734638c MD5 | raw file
  1. <?
  2. class SMTPClient
  3. {
  4. function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body)
  5. {
  6. $this->SmtpServer = $SmtpServer;
  7. $this->SmtpUser = base64_encode ($SmtpUser);
  8. $this->SmtpPass = base64_encode ($SmtpPass);
  9. $this->from = $from;
  10. $this->to = $to;
  11. $this->subject = $subject;
  12. $this->body = $body;
  13. if ($SmtpPort == "")
  14. {
  15. $this->PortSMTP = 25;
  16. }
  17. else
  18. {
  19. $this->PortSMTP = $SmtpPort;
  20. }
  21. }
  22. function SendMail ()
  23. {
  24. if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP))
  25. {
  26. fputs ($SMTPIN, "EHLO localhost\r\n");
  27. $talk["hello"] = fgets ( $SMTPIN, 1024 );
  28. fputs($SMTPIN, "auth login\r\n");
  29. $talk["res"]=fgets($SMTPIN,1024);
  30. fputs($SMTPIN, $this->SmtpUser."\r\n");
  31. $talk["user"]=fgets($SMTPIN,1024);
  32. fputs($SMTPIN, $this->SmtpPass."\r\n");
  33. $talk["pass"]=fgets($SMTPIN,256);
  34. fputs ($SMTPIN, "MAIL FROM: <".$this->from.">\r\n");
  35. $talk["From"] = fgets ( $SMTPIN, 1024 );
  36. fputs ($SMTPIN, "RCPT TO: <".$this->to.">\r\n");
  37. $talk["To"] = fgets ($SMTPIN, 1024);
  38. fputs($SMTPIN, "DATA\r\n");
  39. $talk["data"]=fgets( $SMTPIN,1024 );
  40. fputs($SMTPIN, "To: <".$this->to.">\r\nFrom: <".$this->from.">\r\nSubject:".$this->subject."\r\n\r\n\r\n".$this->body."\r\n.\r\n");
  41. $talk["send"]=fgets($SMTPIN,256);
  42. //ЗАКРЫВАЕМ СОЕДИНЕНИЕ И ВЫХОДИМ ...
  43. fputs ($SMTPIN, "QUIT\r\n");
  44. fclose($SMTPIN);
  45. //
  46. }
  47. return $talk;
  48. }
  49. }
  50. // ------- func ends
  51. require_once "config.php";
  52. $error = '';
  53. $info = '';
  54. $email = $_GET['email'];
  55. // echo ":::[".$email."] :: ";
  56. // Проверка заполнения поля с адресом email
  57. if ($email == '' || $email == 'your@email.com') {
  58. $error = $config['messages']['no_email'];
  59. echo $error;
  60. }
  61. // Проверка формата адреса email
  62. else if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
  63. {
  64. $error = $config['messages']['email_invalid'];
  65. echo $error;
  66. }
  67. // Соединенияе с базой
  68. else if (!@mysql_connect($config['database']['host'], $config['database']['username'], $config['database']['password'])) {
  69. $info = $config['messages']['technical'];
  70. echo $info;
  71. }
  72. else if (!@mysql_select_db($config['database']['database']))
  73. {
  74. $error = $config['messages']['technical'];
  75. echo $error;
  76. }
  77. else
  78. {
  79. // Вставляем подписку
  80. $q = "INSERT INTO `keva_precour` (email, subscribed_at) VALUES ('".$email."', NOW())";
  81. @mysql_query($q);
  82. $good = "no";
  83. if (mysql_error())
  84. {
  85. // $error = $config['messages']['technical'];
  86. echo "Такой адрес уже есть в базе данных"; //$error;
  87. }
  88. else
  89. {
  90. // Готово.
  91. $info = $config['messages']['thank_you'];
  92. echo $info;
  93. $good = "yes";
  94. }
  95. if($good == "yes")
  96. {
  97. $username = base64_encode($email);
  98. $password = substr(md5(rand().rand()), 0, 7);
  99. $password_hash = md5($password);
  100. $nick = mysql_fetch_array(mysql_query("select name from nicknames where used = '0' order by rand() limit 0,1;"));
  101. mysql_query("update nicknames set used = 1 where name = '{$nick['name']}'");
  102. //print_r($nick);
  103. //error_reporting(E_ALL);
  104. $nick = mysql_real_escape_string($nick['name']);
  105. $sql = "insert into user values (NULL, '{$username}', '{$password_hash}', 0, 'user', '{$nick}')";
  106. mysql_select_db("freehackquest") or die(mysql_error());
  107. mysql_query($sql);
  108. $body = "Thank you for your subscription on our news!
  109. Before start our lessons we would to suggest to participate in mini-CTF game, which we already created account for you.
  110. login: {$email}
  111. password: {$password}
  112. random nick: {$nick['name']}
  113. link: http://free-hack-quest.keva.su/";
  114. $subj = "Keva CTF team";
  115. $SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, "noreply@keva.su", $email, $subj, $body);
  116. $SMTPChat = $SMTPMail->SendMail();
  117. }
  118. }
  119. /*
  120. if ($_GET['json']) {
  121. if ($error) {
  122. echo '{"ошибка": "'.$error.'"}';
  123. } else if ($info) {
  124. echo '{"сообщение": "'.$info.'"}';
  125. }
  126. }
  127. */
  128. ?>