PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/cron.php

https://github.com/Cha0sgr/DNS-Registry
PHP | 614 lines | 387 code | 122 blank | 105 comment | 69 complexity | 4e24a95dee010f57dd89b43add496a46 MD5 | raw file
Possible License(s): GPL-3.0
  1. #!/usr/bin/php -q
  2. <?php
  3. /*-----------------------------------------------------------------------------
  4. * Domain Registry Control Panel *
  5. * *
  6. * Main Author: Vaggelis Koutroumpas vaggelis@koutroumpas.gr (c)2014 for AWMN *
  7. * Credits: see CREDITS file *
  8. * *
  9. * This program is free software: you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License as published by *
  11. * the Free Software Foundation, either version 3 of the License, or *
  12. * (at your option) any later version. *
  13. * *
  14. * This program is distributed in the hope that it will be useful, *
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  17. * GNU General Public License for more details. *
  18. * *
  19. * You should have received a copy of the GNU General Public License *
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  21. * *
  22. *-----------------------------------------------------------------------------*/
  23. // Check if we are run from cli
  24. if(php_sapi_name() != "cli"){
  25. echo "This program requires to be run via terminal (php-cli)\n";
  26. exit(-1);
  27. }
  28. //error_reporting(E_ALL ^ E_NOTICE ^ E_STRICT);
  29. //Include system files
  30. require_once(dirname(__FILE__)."/../includes/config.php");
  31. require_once(dirname(__FILE__)."/../includes/functions.php");
  32. require_once 'Net/DNS.php';
  33. //Check users that haven't logged in for LAST_LOGIN_MAX
  34. $time = time() - ($CONF['LAST_LOGIN_MAX'] * 86400);
  35. $SELECT_USERS = mysql_query("SELECT username, last_login, id FROM users WHERE last_login > '0' AND last_login < '".$time. "' AND active = '1' AND suspended = '0' AND admin_level != 'admin' ", $db);
  36. while ($USERS = mysql_fetch_array($SELECT_USERS)){
  37. //Suspend account
  38. mysql_query("UPDATE records SET disabled = '1' WHERE user_id = '".$USERS['id']."' ", $db);
  39. mysql_query("UPDATE users SET suspended = UNIX_TIMESTAMP() WHERE id = '".$USERS['id']."' ", $db);
  40. //Send email notification that the account just got suspended
  41. //no need to send mail here, it will be sent further down on suspend checks
  42. echo "User " . $USERS['username'] . " hasn't logged in for over " . $CONF['LAST_LOGIN_MAX'] . " days. Last recorded login at: ". date("d-m-Y g:i a", $USERS['last_login']) ." Account suspended.\n";
  43. }
  44. //Check suspended accounts that have passed LAST_LOGIN_MAX_SUSPEND
  45. $SELECT_USERS = mysql_query("SELECT username, last_login, id, suspended FROM users WHERE last_login > '0' AND active = '1' AND suspended > '0' AND admin_level != 'admin' ", $db);
  46. while ($USERS = mysql_fetch_array($SELECT_USERS)){
  47. $days_suspended = round((time() - $USERS['suspended']) / 86400);
  48. if ($days_suspended > $CONF['LAST_LOGIN_MAX_SUSPEND']){
  49. //DELETE ACCOUNT
  50. //Delete user hosted domains
  51. $SELECT_USER_DOMAINS = mysql_query("SELECT domain_id FROM records WHERE user_id = '".$USERS['id']."' AND type = 'SOA' ", $db);
  52. while ($USER_DOMAINS = mysql_fetch_array($SELECT_USER_DOMAINS)){
  53. mysql_query("DELETE FROM domains WHERE id = '".$USER_DOMAINS['domain_id']."' ", $db);
  54. }
  55. //Delete user delegated domains
  56. mysql_query("DELETE FROM records WHERE user_id = '".$USERS['id']."' ", $db);
  57. //Delete user account
  58. mysql_query("DELETE FROM users WHERE id = '".$USERS['id']."' ", $db);
  59. //Delete user notifications
  60. mysql_query("DELETE FROM users_notifications WHERE user_id = '".$USERS['id']."' ", $db);
  61. //Send email notification that the account just got suspended
  62. echo "User " . $USERS['username'] . " hasn't logged in for over " . ($CONF['LAST_LOGIN_MAX'] + $CONF['LAST_LOGIN_MAX_SUSPEND']) . " days. Account was already suspended and passed the suspend period. DELETING. Sending email.\n";
  63. }
  64. }
  65. //Check users that are getting close to LAST_LOGIN_MAX to notify them before account suspend
  66. $time = time() - (($CONF['LAST_LOGIN_MAX'] - $CONF['LAST_LOGIN_MAX_START_ALERTS']) * 86400);
  67. $SELECT_USERS = mysql_query("SELECT username, last_login, id FROM users WHERE last_login > '0' AND last_login < '".$time. "' AND active = '1' AND suspended = '0' AND admin_level != 'admin' ", $db);
  68. while ($USERS = mysql_fetch_array($SELECT_USERS)){
  69. $last_login_days = round(( time() - $USERS['last_login'] ) / 86400 );
  70. $days = $CONF['LAST_LOGIN_MAX_START_ALERTS'] - round ( $last_login_days - ($CONF['LAST_LOGIN_MAX'] - $CONF['LAST_LOGIN_MAX_START_ALERTS']) );
  71. //now check if user has already had an email sent in the last month.
  72. //check in users_notifications table
  73. $SELECT_PAST_NOTIF = mysql_query("SELECT * FROM users_notifications WHERE user_id = '".$USERS['id']."' AND type = 'LAST_LOGIN_MAX_START_ALERTS' ORDER BY time DESC", $db);
  74. $PAST_NOTIF = mysql_fetch_array($SELECT_PAST_NOTIF);
  75. $TOTAL_SENT_NOTIF = mysql_num_rows($SELECT_PAST_NOTIF);
  76. if ($TOTAL_SENT_NOTIF > 0){
  77. $TOTAL_NOTIF = round ($CONF['LAST_LOGIN_MAX_START_ALERTS'] / $CONF['LAST_LOGIN_MAX_START_ALERTS_INTERVAL']);
  78. }else{
  79. $TOTAL_NOTIF = 1;
  80. }
  81. $last_sent_notif = round(( time() - $PAST_NOTIF['time'] ) / 86400 );
  82. if ( ( $PAST_NOTIF['time'] < (time() - ($CONF['LAST_LOGIN_MAX_START_ALERTS_INTERVAL'] * 86400) ) && $TOTAL_SENT_NOTIF < $TOTAL_NOTIF ) || $days <= '1' && $last_sent_notif >= '1' ){
  83. //SEND EMAIL NOTIFICATION
  84. mysql_query("INSERT INTO users_notifications (`user_id`, `type`, `time`) VALUES ('".$USERS['id']."', 'LAST_LOGIN_MAX_START_ALERTS' , UNIX_TIMESTAMP() ) ", $db);
  85. echo "User " . $USERS['username'] . " hasn't logged in for " . $last_login_days . " days. Last recorded login at: ". date("d-m-Y g:i a",$USERS['last_login']) .". User has ".$days." days before account suspend. Sending email.\n";
  86. }else{
  87. //DONT SEND EMAIL - IT'S NOT TIME YET
  88. echo "User " . $USERS['username'] . " was already informed. Last notification sent at: ". date("d-m-Y g:i a",$PAST_NOTIF['time']) .". User has ".$days." days before account suspend.\n";
  89. }
  90. }
  91. //Check users that suspended and notify them that their account will be deleted soon
  92. $SELECT_USERS = mysql_query("SELECT username, last_login, id, suspended FROM users WHERE last_login > '0' AND active = '1' AND suspended > '0' AND admin_level != 'admin' ", $db);
  93. while ($USERS = mysql_fetch_array($SELECT_USERS)){
  94. $suspend_days = round(( time() - $USERS['suspended'] ) / 86400 );
  95. $days = $CONF['LAST_LOGIN_MAX_SUSPEND'] - $suspend_days;
  96. //now check if user has already had an email sent in the last month.
  97. //check in users_notifications table
  98. $SELECT_PAST_NOTIF = mysql_query("SELECT * FROM users_notifications WHERE user_id = '".$USERS['id']."' AND type = 'LAST_LOGIN_MAX_SUSPEND' ORDER BY time DESC", $db);
  99. $PAST_NOTIF = mysql_fetch_array($SELECT_PAST_NOTIF);
  100. $TOTAL_SENT_NOTIF = mysql_num_rows($SELECT_PAST_NOTIF);
  101. if ($TOTAL_SENT_NOTIF > 0){
  102. $TOTAL_NOTIF = round ($CONF['LAST_LOGIN_MAX_SUSPEND'] / $CONF['LAST_LOGIN_MAX_SUSPEND_ALERT_INTERVAL']);
  103. }else{
  104. $TOTAL_NOTIF = 1;
  105. }
  106. $last_sent_notif = round(( time() - $PAST_NOTIF['time'] ) / 86400 );
  107. if ( ( $PAST_NOTIF['time'] < (time() - ($CONF['LAST_LOGIN_MAX_SUSPEND_ALERT_INTERVAL'] * 86400) ) && $TOTAL_SENT_NOTIF < $TOTAL_NOTIF ) || $days <= '1' && $last_sent_notif >= '1' ){
  108. //SEND EMAIL NOTIFICATION
  109. mysql_query("INSERT INTO users_notifications (`user_id`, `type`, `time`) VALUES ('".$USERS['id']."', 'LAST_LOGIN_MAX_SUSPEND' , UNIX_TIMESTAMP() ) ", $db);
  110. echo "User " . $USERS['username'] . " is supended! User has ".$days." days before account delete. Sending email.\n";
  111. }else{
  112. //DONT SEND EMAIL - IT'S NOT TIME YET
  113. echo "User " . $USERS['username'] . " was already informed. Last notification sent at: ". date("d-m-Y g:i a",$PAST_NOTIF['time']) .". User has ".$days." days before account delete.\n";
  114. }
  115. }
  116. // Check for new domains that havent been activated and passed NEW_DOMAIN_ENABLE_PERIOD
  117. $SELECT_DOMAINS = mysql_query("SELECT * FROM records WHERE type = 'NS' AND user_id > '0' AND disabled = '1' GROUP BY name ", $db);
  118. while ($DOMAINS = mysql_fetch_array($SELECT_DOMAINS)){
  119. //Check if domain is disabled due to periodic validation failed checks and ignore because it's not newly registered
  120. $SELECT_OLD_DOMAIN = mysql_query("SELECT 1 FROM users_notifications WHERE user_id = '".$DOMAINS['user_id']."' AND domain = '".$DOMAINS['name']."' AND type = 'DOMAIN_REP_FAILED_VAL' ", $db);
  121. if (!mysql_num_rows($SELECT_OLD_DOMAIN)){
  122. $SELECT_ISHOSTED = mysql_query("SELECT id FROM domains WHERE name = '".$DOMAINS['name']."' ", $db);
  123. $HOSTEDID = mysql_fetch_array($SELECT_ISHOSTED);
  124. $ISHOSTED = mysql_num_rows($SELECT_ISHOSTED);
  125. $SELECT_LAST_UPDATED = mysql_query("SELECT `change_date` FROM `records` WHERE name LIKE '%".$DOMAINS['name']."' ORDER BY change_date DESC LIMIT 0, 1",$db);
  126. $LAST_UPDATED = mysql_fetch_array($SELECT_LAST_UPDATED);
  127. $o=0;
  128. $ns='';
  129. $SELECT_ROOT_NS = mysql_query("SELECT name FROM root_ns WHERE active = '1' ", $db);
  130. $ROOT_NS_TOTAL = mysql_num_rows($SELECT_ROOT_NS);
  131. while ($ROOT_NS = mysql_fetch_array($SELECT_ROOT_NS)){
  132. $o++;
  133. $ns .= "'".$ROOT_NS['name']."'";
  134. if ($o < $ROOT_NS_TOTAL){
  135. $ns .=", ";
  136. }
  137. }
  138. $SELECT_DOMAIN_RECORDS = mysql_query("SELECT 1 FROM records WHERE domain_id = '".$HOSTEDID['id']."' AND type != 'SOA' AND content NOT IN (".$ns.") AND user_id > '0' " , $db);
  139. $DOMAIN_RECORDS = mysql_num_rows($SELECT_DOMAIN_RECORDS);
  140. // If domain is new and disabled for more that NEW_DOMAIN_ENABLE_PERIOD days then delete.
  141. $days_disabled = round((time() - $LAST_UPDATED['change_date']) / 86400);
  142. //echo "Days disabled: " .$days_disabled ."\n";
  143. $days_disabled_left = round(( time() - $LAST_UPDATED['change_date'] ) / 86400 );
  144. $days_left = $CONF['NEW_DOMAIN_ENABLE_PERIOD'] - $days_disabled_left;
  145. //echo "Days left: " .$days_left ."\n";
  146. if ($days_disabled> $CONF['NEW_DOMAIN_ENABLE_PERIOD'] ){
  147. //Domain did not get enabled in time. Deleting...
  148. mysql_query("DELETE FROM records WHERE name = '".$DOMAINS['name']."' ", $db);
  149. mysql_query("DELETE FROM records WHERE name LIKE '%.".$DOMAINS['name']."' ", $db);
  150. mysql_query("DELETE FROM domains WHERE name = '".$DOMAINS['name']."' ", $db);
  151. mysql_query("DELETE FROM users_notifications WHERE domain = '".$DOMAINS['name']."' ", $db);
  152. //Send mail to user about domain delete
  153. echo "New Domain " . $DOMAINS['name'] . " was DELETED! User did not enable it before automatic delete. Sending email.\n";
  154. }else{
  155. //check in users_notifications table
  156. $SELECT_PAST_NOTIF = mysql_query("SELECT * FROM users_notifications WHERE user_id = '".$DOMAINS['user_id']."' AND domain = '".$DOMAINS['name']."' AND type = 'NEW_DOMAIN_ENABLE_PERIOD' ORDER BY time DESC", $db);
  157. $PAST_NOTIF = mysql_fetch_array($SELECT_PAST_NOTIF);
  158. $TOTAL_SENT_NOTIF = mysql_num_rows($SELECT_PAST_NOTIF);
  159. $TOTAL_NOTIF = 1;
  160. $last_sent_notif = round(( time() - $PAST_NOTIF['time'] ) / 86400 );
  161. //If time has come or it's last day send notification
  162. if ( ( $DOMAINS['change_date'] < (time() - (($CONF['NEW_DOMAIN_ENABLE_PERIOD'] - $CONF['NEW_DOMAIN_ENABLE_PERIOD_START_ALERT']) * 86400) ) && $TOTAL_SENT_NOTIF < $TOTAL_NOTIF ) || $days_left <= '1' && $last_sent_notif == '1' ){
  163. //SEND EMAIL NOTIFICATION
  164. mysql_query("INSERT INTO users_notifications (`user_id`, `domain`, `type`, `time`) VALUES ('".$DOMAINS['user_id']."', '".$DOMAINS['name']."', 'NEW_DOMAIN_ENABLE_PERIOD' , UNIX_TIMESTAMP() ) ", $db);
  165. echo "Domain " . $DOMAINS['name'] . " is about to be deleted! User has ".$days_left." days to enable it before automatic delete. Sending email.\n";
  166. }
  167. }
  168. }
  169. }
  170. // Re-validate all domains and suspend those that fail for DOMAIN_REP_FAILED_VAL days in a row
  171. $SELECT_DOMAINS = mysql_query("SELECT * FROM records WHERE type = 'NS' AND user_id > '0' AND disabled = '0' GROUP BY name ", $db);
  172. while ($DOMAINS = mysql_fetch_array($SELECT_DOMAINS)){
  173. $SELECT_ISHOSTED = mysql_query("SELECT id FROM domains WHERE name = '".$DOMAINS['name']."' ", $db);
  174. $HOSTEDID = mysql_fetch_array($SELECT_ISHOSTED);
  175. $ISHOSTED = mysql_num_rows($SELECT_ISHOSTED);
  176. if (!$ISHOSTED){
  177. //echo "Enabled domain to validate again: " . $DOMAINS['name'] . "\n";
  178. // VALIDATION CHECKS
  179. $DOMAIN_FAILED = false;
  180. $d = mysql_real_escape_string($DOMAINS['name'], $db);
  181. $resolver = new Net_DNS_Resolver();
  182. //Set resolver options
  183. $resolver->debug = 0; // Turn on debugging output to show the query
  184. $resolver->usevc = 0; // Force the use of TCP instead of UDP
  185. $resolver->port = 53; // DNS Server port
  186. $resolver->recurse = 0; // Disable recursion
  187. $resolver->retry = $CONF['DNS_VALIDATE_WAIT']; // How long to wait for answer
  188. $resolver->retrans = $CONF['DNS_VALIDATE_RETRY']; // How many times to retry for answer
  189. $SELECT_DOMAIN_NS = mysql_query("SELECT content FROM records WHERE name = '".$d."' AND type = 'NS' ORDER BY content ASC", $db);
  190. $r=0;
  191. $all_domain_errors = array();
  192. while($DOMAIN_NS = mysql_fetch_array($SELECT_DOMAIN_NS)){
  193. $r++;
  194. $domain_errors = array();
  195. $SELECT_NS_IP = mysql_query("SELECT content FROM records WHERE name = '".$DOMAIN_NS['content']."' AND type = 'A' ", $db);
  196. $NS_IP = mysql_fetch_array($SELECT_NS_IP);
  197. $OUR_TLD = getTLD($DOMAIN_NS['content']);
  198. if ($OUR_TLD){
  199. //echo "<h4><img src='images/ico_info.png' align='absmiddle'> &nbsp;Checking Nameserver: <strong class='blue'>" .$DOMAIN_NS['content'] ." (".$NS_IP['content'].")</strong></h4>\n";
  200. }else{
  201. //echo "<h4><img src='images/ico_info.png' align='absmiddle'> &nbsp;Checking Nameserver: <strong class='blue'>" .$DOMAIN_NS['content'] ." (3rd Party TLD)</strong></h4>\n";
  202. $NS_IP['content'] = $DOMAIN_NS['content'];
  203. }
  204. //echo "<div style='margin-left: 40px;'>\n";
  205. if ($DOMAIN_NS['content'] != 'unconfigured'){
  206. //Set resolver nameserver IP to use for lookup
  207. $resolver->nameservers = array($NS_IP['content']);
  208. // Get SOA record from nameserver
  209. $response = $resolver->rawQuery($d, 'SOA');
  210. $aa = $response->header->aa;
  211. $rcode = $response->header->rcode;
  212. $ancount = $response->header->ancount;
  213. //print_r($response);
  214. // Check if we got a response at all from the nameserver
  215. if ($response){
  216. // Check if we got a valid response from the nameserver (SERVFAIL?)
  217. if ($rcode == 'NOERROR'){
  218. // Validate if response has the aa bit (authoritative)
  219. if ($aa == 1 ){
  220. // Check if we got an answers from the nameserver (NXDOMAIN?)
  221. if ($ancount > 0){
  222. // GET NS RECORDS
  223. $response = $resolver->rawQuery($d, 'NS');
  224. //print_r($response);
  225. for ($i = 0; $i <= count($response->answer); $i++) {
  226. $NS[$i] = $response->answer[$i];
  227. }
  228. //echo "<img src='images/ico_valid.png' align='absmiddle' > <strong class='blue'>SOA</strong> record check <strong class='green'>[OK]</strong><br /><br />\n";
  229. //print_r($NS);
  230. //Compare received NS to registry NS
  231. $ns_error = false;
  232. for ($i = 0; $i <= count($NS)-2; $i++) {
  233. $NS_AR = (array) $NS[$i];
  234. $NS_IN_DB = mysql_num_rows(mysql_query("SELECT 1 FROM records WHERE name = '".$d."' AND content = '".mysql_real_escape_string($NS_AR['nsdname'])."' ", $db));
  235. if (!$NS_IN_DB){
  236. $ns_error = true;
  237. }
  238. }
  239. //Validate NS records and move to further checks
  240. if ($ns_error == false){
  241. $NS_TOTAL = mysql_num_rows(mysql_query("SELECT 1 FROM records WHERE name = '".$d."' AND type = 'NS' " .$user_id, $db));
  242. //Check if we got the same quantity of NS records as configured in registry
  243. if ($NS_TOTAL == count($NS)-1){
  244. //echo "<img src='images/ico_valid.png' align='absmiddle' > <strong class='blue'>NS</strong> records check <strong class='green'>[OK]</strong><br /><br />\n";
  245. $glue_errors = array();
  246. //If NS records are TLD of ours, check Glue Records if needed
  247. if ($OUR_TLD){
  248. $SELECT_DOMAIN_NS_GLUES = mysql_query("SELECT content FROM records WHERE name = '".$d."' AND type = 'NS' ORDER BY content ASC", $db);
  249. while($DOMAIN_NS_GLUES = mysql_fetch_array($SELECT_DOMAIN_NS_GLUES)){
  250. $local_ns_found = false;
  251. //Check if db NS record is part of the domain (so that we need to check it's A/Glue records)
  252. $dbns_parts = explode(".", $DOMAIN_NS_GLUES['content']);
  253. //print_r($dbns_parts);
  254. $dbns_parts[0] = false;
  255. $ns_parent_domain = implode(".", $dbns_parts);
  256. $ns_parent_domain = substr($ns_parent_domain, 1);
  257. //$dbns_parts = array_reverse($dbns_parts);
  258. //$ns_parent_domain = $dbns_parts[1] . ".". $dbns_parts[0];
  259. //echo $ns_parent_domain;
  260. //Check for A records on the proper nameservers because not always the nameserver we are iterating now is authoritative for the A/glue record.
  261. $SELECT_NS_PARENT = mysql_query("SELECT content FROM records WHERE name = '".$ns_parent_domain."' AND type ='NS' ORDER BY content ASC", $db);
  262. while ($NS_PARENT = mysql_fetch_array($SELECT_NS_PARENT)){
  263. if ($NS_PARENT['content'] == $DOMAIN_NS_GLUES['content'] ){
  264. $RESOLVER_IP = $NS_IP['content'];
  265. $local_ns_found = true;
  266. break;
  267. }else{
  268. $SELECT_NS_PARENT_IP = mysql_query("SELECT content FROM records WHERE name = '".$NS_PARENT['content']."' AND type = 'A' ", $db);
  269. $NS_PARENT_IP = mysql_fetch_array($SELECT_NS_PARENT_IP);
  270. $RESOLVER_IP = $NS_PARENT_IP['content'];
  271. $RESOLVER_NAME = $NS_PARENT['content'];
  272. if ($local_ns_found){
  273. break;
  274. }
  275. }
  276. }
  277. //Skip check if A record is not under our TLDs
  278. if (getTLD($DOMAIN_NS_GLUES['content'])){
  279. //Set resolver nameserver IP to use for lookup
  280. $resolver->nameservers = array($RESOLVER_IP);
  281. //echo $RESOLVER_IP . "<br>";
  282. // Get A records from nameserver
  283. $response = $resolver->rawQuery($DOMAIN_NS_GLUES['content'], 'A');
  284. //echo "<pre>";
  285. //print_r($response);
  286. //echo "</pre>";
  287. $SELECT_NS_GLUE = mysql_query("SELECT content FROM records WHERE name = '".$DOMAIN_NS_GLUES['content']."' AND type = 'A' ", $db);
  288. $NS_GLUE = mysql_fetch_array($SELECT_NS_GLUE);
  289. if ($response->header->rcode != 'NXDOMAIN'){
  290. if ($response->answer[0]->address == $NS_GLUE['content']){
  291. //echo "<img src='images/ico_valid.png' align='absmiddle' > Glue <strong class='blue'>".$NS_GLUE['content']."</strong> for A record <strong class='blue'>".$DOMAIN_NS_GLUES['content']."</strong> check <strong class='green'>[OK]</strong><br />\n";
  292. }else{
  293. //echo "<img src='images/ico_invalid.png' align='absmiddle' > Glue response: <strong class='red'>".$response->answer[0]->address."</strong> for A record <strong class='blue'>".$DOMAIN_NS_GLUES['content']."</strong> does not match the glue record in registry (<strong class='blue'>".$NS_GLUE['content']."</strong>)<br />\n";
  294. $glue_errors[] = true;
  295. }
  296. }else{
  297. //echo "<img src='images/ico_invalid.png' align='absmiddle' > Glue <strong class='blue'>".$NS_GLUE['content']."</strong> for A record <strong class='blue'>".$DOMAIN_NS_GLUES['content']."</strong> does not exist (<strong class='red'>NXDOMAIN</strong>)<br />\n";
  298. $glue_errors[] = true;
  299. }
  300. if ($RESOLVER_IP != $NS_IP['content']){
  301. //echo "<span class='small' style='margin-left: 20px;'>(Authoritative NS for ".$DOMAIN_NS_GLUES['content']." > ".$RESOLVER_IP." - ".$RESOLVER_NAME.")</span><br /><br />\n";
  302. }else{
  303. //echo "<br />\n";
  304. }
  305. }
  306. }
  307. }
  308. }elseif($NS_TOTAL > count($NS)-2){
  309. //echo "\n<img src='images/ico_invalid.png' align='absmiddle' > Nameserver <strong class='blue'>".$NS_IP['content']." (".$DOMAIN_NS['content'].")</strong> responded with less NS Records than configured in Registry's Database.<br /><br />\n";
  310. $domain_errors[] = true;
  311. }else{
  312. //echo "\n<img src='images/ico_invalid.png' align='absmiddle' > Nameserver <strong class='blue'>".$NS_IP['content']." (".$DOMAIN_NS['content'].")</strong> responded with more NS Records than configured in Registry's Database.<br /><br />\n";
  313. $domain_errors[] = true;
  314. }
  315. }else{
  316. //echo "\n<img src='images/ico_invalid.png' align='absmiddle' > Nameserver <strong class='blue'>".$NS_IP['content']." (".$DOMAIN_NS['content'].")</strong> responded with NS Records not configured in Registry's Database.<br /><br />\n";
  317. $domain_errors[] = true;
  318. }
  319. }else{
  320. //echo "\n<img src='images/ico_invalid.png' align='absmiddle' > Nameserver <strong class='blue'>".$NS_IP['content']." (".$DOMAIN_NS['content'].")</strong> did not reply (<strong class='red'>NO ANSWER</strong>).<br /><br />\n";
  321. $domain_errors[] = true;
  322. }
  323. }else{
  324. //echo "\n<img src='images/ico_invalid.png' align='absmiddle' > Nameserver <strong class='blue'>".$NS_IP['content']." (".$DOMAIN_NS['content'].")</strong> is <strong class='red'>not authoritative</strong> for this domain.<br /><br />\n";
  325. $domain_errors[] = true;
  326. }
  327. }else{
  328. //echo "\n<img src='images/ico_invalid.png' align='absmiddle' > Nameserver <strong class='blue'>".$NS_IP['content']." (".$DOMAIN_NS['content'].")</strong> did not answer properly (<strong class='red'>SERVFAIL</strong>).<br /><br />\n";
  329. $domain_errors[] = true;
  330. }
  331. }else{
  332. //echo "\n<img src='images/ico_invalid.png' align='absmiddle' > Nameserver <strong class='blue'>".$NS_IP['content']." (".$DOMAIN_NS['content'].")</strong> did not respond (<strong class='red'>TIMEOUT</strong>).<br /><br />\n";
  333. $domain_errors[] = true;
  334. }
  335. }else{
  336. //echo "\n<img src='images/ico_invalid.png' align='absmiddle' > You haven't configured any nameservers for your domain.<br /><br />\n";
  337. $domain_errors[] = true;
  338. }
  339. if (count($glue_errors) == false && count($domain_errors) == 0){
  340. //echo "\n<h3><img src='images/ico_valid_medium.png' align='absmiddle' > Domain <strong class='blue'>".$d."</strong> passed validation on NS: <strong class='blue'>".$DOMAIN_NS['content']." (".$NS_IP['content'].")</strong></h3>\n";
  341. }else{
  342. //echo "\n<h3><img src='images/ico_invalid_medium.png' align='absmiddle' > Domain <strong class='blue'>".$d."</strong> failed validation on NS: <strong class='red'>".$DOMAIN_NS['content']." (".$NS_IP['content'].")</strong></h3>\n";
  343. $all_domain_errors[] = true;
  344. }
  345. //echo "</div>\n";
  346. //echo "\n\n<hr height='1' width='90%' align='left'><br />\n\n\n";
  347. }
  348. //echo "<div style='margin-left:40px;'>\n";
  349. if (count($all_domain_errors) || $r==0){
  350. //check in users_notifications table
  351. $SELECT_PAST_FAILS = mysql_query("SELECT * FROM users_notifications WHERE user_id = '".$DOMAINS['user_id']."' AND domain = '".$DOMAINS['name']."' AND type = 'DOMAIN_REP_FAILED_VAL' ORDER BY time ASC", $db);
  352. $PAST_FAILS = mysql_fetch_array($SELECT_PAST_FAILS);
  353. $TOTAL_PAST_FAILS = mysql_num_rows($SELECT_PAST_FAILS);
  354. if ($PAST_FAILS['time']){
  355. $first_fail = round(( time() - $PAST_FAILS['time'] ) / 86400 );
  356. }else{
  357. $first_fail = 0;
  358. }
  359. //echo "First fail was " . $first_fail . " days before\n";
  360. //echo $TOTAL_PAST_FAILS . " fails in a row\n";
  361. if ($first_fail < $CONF['DOMAIN_REP_FAILED_VAL'] && $TOTAL_PAST_FAILS < $CONF['DOMAIN_REP_FAILED_VAL'] ){
  362. echo "Domain ".$DOMAINS['name']." failed, but hasn't reached fail limit to be suspended yet\n";
  363. mysql_query("INSERT INTO users_notifications (`user_id`, `domain`, `type`, `time`) VALUES ('".$DOMAINS['user_id']."', '".$DOMAINS['name']."', 'DOMAIN_REP_FAILED_VAL' , UNIX_TIMESTAMP() ) ", $db);
  364. }else{
  365. if ($first_fail >= $CONF['DOMAIN_REP_FAILED_VAL'] ){
  366. echo "Domain ".$DOMAINS['name']." has failed for ".$first_fail." days in a row! SUSPENDING. Sending email.\n";
  367. //mysql_query("DELETE FROM users_notifications WHERE user_id = '".$DOMAINS['user_id']."' AND domain = '".$DOMAINS['name']."' AND type = 'DOMAIN_REP_FAILED_VAL' ", $db);
  368. mysql_query("UPDATE records SET `disabled` = '1' WHERE `name` = '".$DOMAINS['name']."' AND user_id = '".$DOMAINS['user_id']."' ",$db);
  369. mysql_query("UPDATE records SET `disabled` = '1' WHERE `name` LIKE '%.".$DOMAINS['name']."' AND user_id = '".$DOMAINS['user_id']."' ",$db);
  370. }else{
  371. echo "Domain ".$DOMAINS['name']." reached ".$TOTAL_PAST_FAILS." notifications but first notification date (".$first_fail.") is not older than " . $CONF['DOMAIN_REP_FAILED_VAL']. " ! Not suspending yet. Maybe cron was out of date (run more than 1 time per day)\n";
  372. }
  373. }
  374. //echo "Domain " . $DOMAINS['name'] . " has FAILED the validations! \n";
  375. }else{
  376. echo "Domain ".$DOMAINS['name']." has passed Validation. Resetting any previous fails.\n";
  377. mysql_query("DELETE FROM users_notifications WHERE user_id = '".$DOMAINS['user_id']."' AND domain = '".$DOMAINS['name']."' AND type = 'DOMAIN_REP_FAILED_VAL' ", $db);
  378. mysql_query("DELETE FROM users_notifications WHERE user_id = '".$DOMAINS['user_id']."' AND domain = '".$DOMAINS['name']."' AND type = 'DOMAIN_REP_FAILED_VAL_DIS_PER' ", $db);
  379. //echo "Domain " . $DOMAINS['name'] . " has passed the validations!\n";
  380. }
  381. }
  382. }
  383. //Check for suspended domains that have passed DOMAIN_REP_FAILED_VAL_DIS_PER and DELETE them
  384. $SELECT_DOMAINS = mysql_query("SELECT * FROM records WHERE type = 'NS' AND user_id > '0' AND disabled = '1' GROUP BY name ", $db);
  385. while ($DOMAINS = mysql_fetch_array($SELECT_DOMAINS)){
  386. //Check if domain is disabled due to periodic validation failed checks
  387. $SELECT_OLD_DOMAIN = mysql_query("SELECT time FROM users_notifications WHERE user_id = '".$DOMAINS['user_id']."' AND domain = '".$DOMAINS['name']."' AND type = 'DOMAIN_REP_FAILED_VAL' ORDER BY time DESC", $db);
  388. if (mysql_num_rows($SELECT_OLD_DOMAIN)){
  389. //check in users_notifications table
  390. $LAST_FAIL = mysql_fetch_array($SELECT_OLD_DOMAIN);
  391. $days_suspended = round((time() - $LAST_FAIL['time']) / 86400);
  392. if ($days_suspended > $CONF['DOMAIN_REP_FAILED_VAL_DIS_PER']){
  393. //DELETE DOMAIN
  394. mysql_query("DELETE FROM `records` WHERE `name`= '".$DOMAINS['name']."' AND type = 'NS' AND user_id = '".$DOMAINS['user_id']."' ",$db);
  395. mysql_query("DELETE FROM `records` WHERE `name` LIKE '%.".$DOMAINS['name']."' AND type = 'A' AND user_id = '".$DOMAINS['user_id']."' ",$db);
  396. mysql_query("DELETE FROM `users_notifications` WHERE `domain`= '".$DOMAINS['name']."' AND user_id = '".$DOMAINS['user_id']."' ",$db);
  397. //Send email notification that the account just got suspended
  398. echo "Domain " . $DOMAINS['name'] . " was suspended in for over " . ($CONF['DOMAIN_REP_FAILED_VAL_DIS_PER'] + $CONF['DOMAIN_REP_FAILED_VAL']) . " days. DELETING. Sending email.\n";
  399. }
  400. }
  401. }
  402. // Check for suspended domains that havent been activated and are near permanent deletion!
  403. $SELECT_DOMAINS = mysql_query("SELECT * FROM records WHERE type = 'NS' AND user_id > '0' AND disabled = '1' GROUP BY name ", $db);
  404. while ($DOMAINS = mysql_fetch_array($SELECT_DOMAINS)){
  405. //Check if domain is disabled due to periodic validation failed checks
  406. $SELECT_OLD_DOMAIN = mysql_query("SELECT time FROM users_notifications WHERE user_id = '".$DOMAINS['user_id']."' AND domain = '".$DOMAINS['name']."' AND type = 'DOMAIN_REP_FAILED_VAL' ORDER BY time DESC", $db);
  407. if (mysql_num_rows($SELECT_OLD_DOMAIN)){
  408. //check in users_notifications table
  409. $LAST_FAIL = mysql_fetch_array($SELECT_OLD_DOMAIN);
  410. $suspend_days = round(( time() - $LAST_FAIL['time'] ) / 86400 );
  411. $days = $CONF['DOMAIN_REP_FAILED_VAL_DIS_PER'] - $suspend_days;
  412. echo "Has been suspended for " . $suspend_days . " days\n";
  413. echo "Days left until delete:" . $days . "\n";
  414. //now check if domain user has already had an email sent in the last month.
  415. //check in users_notifications table
  416. $SELECT_PAST_NOTIF = mysql_query("SELECT * FROM users_notifications WHERE user_id = '".$DOMAINS['user_id']."'AND domain = '".$DOMAINS['name']."' AND type = 'DOMAIN_REP_FAILED_VAL_DIS_PER' ORDER BY time DESC", $db);
  417. $PAST_NOTIF = mysql_fetch_array($SELECT_PAST_NOTIF);
  418. $TOTAL_SENT_NOTIF = mysql_num_rows($SELECT_PAST_NOTIF);
  419. if ($TOTAL_SENT_NOTIF > 0){
  420. $TOTAL_NOTIF = round ($CONF['DOMAIN_REP_FAILED_VAL_DIS_PER'] / $CONF['DOMAIN_REP_FAILED_VAL_DIS_PER_ALERT_INTERVAL']);
  421. }else{
  422. $TOTAL_NOTIF = 1;
  423. }
  424. $last_sent_notif = round(( time() - $PAST_NOTIF['time'] ) / 86400 );
  425. echo "Last notification sent: " .$last_sent_notif. " days ago\n";
  426. if ( ( $PAST_NOTIF['time'] < (time() - ($CONF['DOMAIN_REP_FAILED_VAL_DIS_PER_ALERT_INTERVAL'] * 86400) ) && $TOTAL_SENT_NOTIF < $TOTAL_NOTIF ) || $days <= '1' && $last_sent_notif >= '1' ){
  427. //SEND EMAIL NOTIFICATION
  428. mysql_query("INSERT INTO users_notifications (`user_id`, `domain`, `type`, `time`) VALUES ('".$DOMAINS['user_id']."', '".$DOMAINS['name']."', 'DOMAIN_REP_FAILED_VAL_DIS_PER' , UNIX_TIMESTAMP() ) ", $db);
  429. echo "Domain " . $DOMAINS['name'] . " is supended! User has ".$days." days before domain delete. Sending email.\n";
  430. }else{
  431. //DONT SEND EMAIL - IT'S NOT TIME YET
  432. echo "Domain " . $DOMAINS['name'] . " was already informed. Last notification sent at: ". date("d-m-Y g:i a",$PAST_NOTIF['time']) .". User has ".$days." days before domain delete.\n";
  433. }
  434. }
  435. }
  436. // cleanup old sessions to keep database small
  437. mysql_query("DELETE FROM sessions WHERE access < ".(time()-86400)." ", $db);
  438. ?>