/** * Update the sign-in credentials for the specific user. * * @param UserRecord $user The user to update the credentials for * @return Boolean True on success */ public function assignCredentials(UserRecord $user) { $db = new DatabaseConnection(); // Generate a new salt and hash the password $salt = $this->generateSalt(); // What hashing algorithm to use $ha = config::get('lepton.user.hashalgorithm', 'md5'); $ps = $user->password . $salt; $hp = hash($ha, $ps); if ($user->userid == null) { $uuid = UUID::v4(); try { $id = $db->insertRow("REPLACE INTO " . LEPTON_DB_PREFIX . "users (username,salt,password,email,flags,registered,uuid) VALUES (%s,%s,%s,%s,%s,NOW(),%s)", $user->username, $salt, $hp, $user->email, $user->flags, $uuid); $user->userid = $id; } catch (Exception $e) { throw $e; // TODO: Handle exception } } else { try { $db->updateRow("UPDATE " . LEPTON_DB_PREFIX . "users SET username=%s,salt=%s,password=%s,email=%s,flags=%s WHERE id=%d", $user->username, $salt, $hp, $user->email, $user->flags, $user->userid); } catch (Exception $e) { throw $e; // TODO: Handle exception } } return true; }
function savepost() { $post = new WebForm($this->gbform); if (!$post->isValid()) { // Form is invalid, post it back to the user to allow correction } else { $db = new DatabaseConnection(); $db->insertRow("INSERT INTO guestbook (name,email,website,message) VALUES (%s,%s,%s,%s)", $post->name, $post->email, $post->website, $post->message); } }
public function addEvent(AuditEvent $event) { $event->setComponent($this->_component); $cn = get_class($event); $sev = serialize($event); // Shove into DB -- echo $sev; echo $cn . "\n\n" . $sev . "\n"; $db = new DatabaseConnection(); $db->insertRow("INSERT INTO auditlog " . "eventclass,component,severity,eventdate,data) " . "VALUES (%s,%s,%d,%d,NOW(),%s)", $cn, $event->getComponent(), $event->getSeverity(), $event->getAssociatedUserId(), $sev); }
/** * * * * */ public function alias() { $this->checkData(); console::writeLn("Generating geoalias table"); $db = new DatabaseConnection(); $db->exec('DROP TABLE IF EXISTS geoalias'); $db->exec('CREATE TABLE geoalias (id INT PRIMARY KEY AUTO_INCREMENT, geoid BIGINT, locname VARCHAR(64) CHARSET utf8, INDEX locname(locname(5))) CHARSET utf8'); $rows = $db->getRows("SELECT id,alternatenames FROM geonames WHERE alternatenames!=''"); console::write('%8d / %8d ', 0, count($rows)); foreach ($rows as $row) { $alt = explode(',', $row['alternatenames']); foreach ($alt as $altstr) { $db->insertRow("INSERT INTO geoalias (geoid,locname) VALUES (%d,%s)", $row['id'], $altstr); } $rc++; $rt++; if ($rt >= 100) { $rh++; if ($rh >= 50) { console::write("\n%8d / %8d ", $rc, count($rows)); $rh = 0; } else { console::write('.'); } $rt = 0; } } console::writeLn(' Done!'); }
public function save() { if (!$this->uuid) { $this->uuid = uuid::v4(); } if (count($this->modified) > 0) { // Get a database reference $db = new DatabaseConnection(); // Determine what needs to be updated. $mtable = array('user' => false, 'userdata' => false, 'ambient' => false, 'credentials' => false); foreach ($this->modified as $mod) { switch ($mod) { case 'ambient': $mtable['ambient'] = true; break; case 'username': $mtable['user'] = true; break; case 'password': $mtable['credentials'] = true; break; case 'email': $mtable['user'] = true; break; case 'uuid': $mtable['user'] = true; break; case 'active': $mtable['user'] = true; break; case 'displayname': $mtable['userdata'] = true; break; case 'firstname': $mtable['userdata'] = true; break; case 'lastname': $mtable['userdata'] = true; break; case 'sex': $mtable['userdata'] = true; break; case 'country': $mtable['userdata'] = true; break; case 'flags': $mtable['user'] = true; break; case 'userid': break; default: throw new BadArgumentException("Unknown field modified: {$mod}"); } } $this->modified = array(); if (!$this->userid) { // Check to see if the username already exists if (user::find($this->username)) { throw new UserException("User already exists!"); } // Insert $ambient = serialize($this->ambient); $this->userid = $db->insertRow("INSERT INTO " . LEPTON_DB_PREFIX . "users (username,email,uuid,flags,active,registered) VALUES " . "(%s,%s,%s,%s,%d,NOW())", $this->username, $this->email, $this->uuid, $this->flags, $this->active ? 1 : 0); $db->updateRow("INSERT INTO " . LEPTON_DB_PREFIX . "userdata (displayname,firstname,lastname,sex,country,ambient,id) VALUES " . "(%s,%s,%s,%s,%s,%s,%d)", $this->displayname, $this->firstname, $this->lastname, $this->sex, $this->country, $ambient, $this->userid); // Update credentials $backend = User::getAuthenticationBackend(); $backend->assignCredentials($this); } else { // Update if ($mtable['ambient'] && $mtable['userdata']) { // Update complete userdata table $ambient = serialize($this->ambient); $db->updateRow("Update " . LEPTON_DB_PREFIX . "userdata SET displayname=%s,firstname=%s,lastname=%s,sex=%s,country=%s,ambient=%s WHERE id=%d", $this->displayname, $this->firstname, $this->lastname, $this->sex, $this->country, $ambient, $this->userid); } elseif ($mtable['ambient']) { // Update the ambient column $ambient = serialize($this->ambient); $db->updateRow("UPDATE " . LEPTON_DB_PREFIX . "userdata SET ambient=%s WHERE id=%d ", $ambient, $this->userid); } elseif ($mtable['userdata']) { // Update the userdata columns $db->updateRow("UPDATE " . LEPTON_DB_PREFIX . "userdata SET displayname=%s,firstname=%s,lastname=%s,sex=%s,country=%s WHERE id=%d", $this->displayname, $this->firstname, $this->lastname, $this->sex, $this->country, $this->userid); } if ($mtable['user']) { // Update users table $db->updateRow("UPDATE " . LEPTON_DB_PREFIX . "users SET username=%s,email=%s,uuid=%s,flags=%s,active=%s WHERE id=%d", $this->username, $this->email, $this->uuid, $this->flags, $this->active ? 1 : 0, $this->userid); } if ($mtable['credentials']) { // Update credentials $backend = User::getAuthenticationBackend(); $backend->assignCredentials($this); } } } return true; }
/** * Update the sign-in credentials for the specific user. * * @param UserRecord $user The user to update the credentials for * @return Boolean True on success */ public function assignCredentials(UserRecord $user) { $db = new DatabaseConnection(); $hp = $this->hashPassword($user->password); logger::debug("Updating password has for %s with '%s'", $user->username, $hp); if ($user->userid == null) { $uuid = UUID::v4(); try { $id = $db->insertRow("REPLACE INTO " . LEPTON_DB_PREFIX . "users (username,password,email,flags,registered,uuid) VALUES (%s,%s,%s,%s,%s,NOW(),%s)", $user->username, $hp, $user->email, $user->flags, $uuid); $user->userid = $id; } catch (Exception $e) { throw $e; // TODO: Handle exception } } else { try { $db->updateRow("UPDATE " . LEPTON_DB_PREFIX . "users SET username=%s,password=%s,email=%s,flags=%s WHERE id=%d", $user->username, $hp, $user->email, $user->flags, $user->userid); } catch (Exception $e) { throw $e; // TODO: Handle exception } } return true; }
function execute() { $db = new DatabaseConnection(); $stmt = "INSERT INTO " . $this->table . " "; $rd = array(); foreach ($this->insert[0] as $key => $val) { $rd[] = $key; } $stmt .= "(" . join(',', $rd) . ") VALUES "; $si = array(); foreach ($this->insert as $row) { $sr = array(); foreach ($row as $key => $val) { $sr[] = $db->escape('%s', $val); } $si[] = '(' . join(',', $sr) . ')'; } $stmt .= join(', ', $si); return $db->insertRow($stmt); }
private static function updateSets(callback $callback = null) { // Update the callback if one is present if ($callback) { $callback->call('Updating sets...'); } // Pull the country list $db = new DatabaseConnection(); $rs = $db->getRows("SELECT isocode,capital FROM geonames_countryinfo"); foreach ($rs as $cc) { $f = $db->getSingleRow("SELECT * FROM geonames_datasets WHERE setkey=%s", $cc['isocode']); if (!$f) { $db->insertRow("INSERT INTO geonames_datasets (setkey,setname,url,active) VALUES (%s,%s,%s,0)", $cc['isocode'], $cc['capital'], self::getUrl($cc['isocode'] . '.zip')); } } if ($callback) { $callback->call('All sets updated'); } }