public function IPCheck($domain) { // lookup NS records $dns = \dns_get_record($domain, DNS_A); //grab domain id $domain_id = \app\models\Domain::findOne(['domain' => $domain])->domain_id; // retrieve last stored records $prev_record = (new \yii\db\Query())->select(['record_value'])->from(['domain_record'])->where(['domain_id' => $domain_id, 'record_type' => 'IP'])->all(); $prev = []; foreach ($prev_record as $re) { $prev[] = $re['record_value']; } // prepare dns records array $record = []; foreach ($dns as $ns) { $record[] = $ns['ip']; } //detect record differences $diff = array_diff($record, $prev); if (!empty($diff)) { $q = new \app\models\MailQueue(); $q->domain_id = $domain_id; $q->message = ''; $q->save(); } // clear old record \app\models\DomainRecord::deleteAll(['domain_id' => $domain_id, 'record_type' => 'IP']); // Insert new records foreach ($record as $key => $value) { $model = new \app\models\DomainRecord(); $model->domain_id = $domain_id; $model->record_type = 'IP'; $model->record_value = $value; $model->last_checked = new \yii\db\Expression('now()'); $model->save(); } }
/** * @return \yii\db\ActiveQuery */ public function getDomainRecords() { return $this->hasMany(DomainRecord::className(), ['domain_id' => 'domain_id']); }