+
+ // The setCreatedBy sets created_by field for both group and user to passed in user_id.
+ private function setCreatedBy($user_id) {
+ if ($this->err->yes()) return false; // There are errors, do not proceed.
+
+ global $i18n;
+ $mdb2 = getConnection();
+
+ // Update group.
+ $sql = "update tt_groups set created_by = $user_id where id = $this->group_id and org_id = $this->org_id";
+ $affected = $mdb2->exec($sql);
+ if (is_a($affected, 'PEAR_Error')) {
+ $this->err->add($i18n->get('error.db'));
+ return false;
+ }
+
+ // Update top manager.
+ $sql = "update tt_users set created_by = $user_id where id = $this->user_id and group_id = $this->group_id and org_id = $this->org_id";
+ $affected = $mdb2->exec($sql);
+ if (is_a($affected, 'PEAR_Error')) {
+ $this->err->add($i18n->get('error.db'));
+ return false;
+ }
+
+ return true;
+ }
+
+ // registeredRecently determines if we already have successful recent registration(s) from user IP.
+ // "recent" means the following:
+ // - 2 or more registrations during last 10 minutes, or
+ // - 1 registration during last minute.
+ //
+ // This offers some level of protection from bot registrations.
+ function registeredRecently() {
+ $mdb2 = getConnection();
+
+ $ip_part = ' created_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']);
+ $sql = 'select count(*) as cnt from tt_groups where '.$ip_part.' and created > now() - interval 10 minute';
+ $res = $mdb2->query($sql);
+ if (is_a($res, 'PEAR_Error'))
+ return false;
+ $val = $res->fetchRow();
+ if ($val['cnt'] == 0)
+ return false; // No registrations in last 10 minutes.
+ if ($val['cnt'] >= 2)
+ return true; // 2 or more registrations in last 10 mintes.
+
+ // If we are here, there was exactly one registration during last 10 minutes.
+ // Determine if it occurred within the last minute in a separate query.
+ $sql = 'select created from tt_groups where '.$ip_part.' and created > now() - interval 1 minute';
+ $res = $mdb2->query($sql);
+ if (is_a($res, 'PEAR_Error'))
+ return false;
+ $val = $res->fetchRow();
+ if ($val)
+ return true;
+
+ return false;
+ }