function get_user($token = null) { $db = new Database(df_db()); if (!isset($token)) { $token = $_POST['token']; } if (!$token) { return null; } $user = $db->getObject("select username from sessions where token_id=:token_id and expires > :now", array('token_id' => $token, 'now' => time())); if ($user) { return $user->username; } return null; }
public static function createPortletWithSQL($sql, $cols = null, $opts = array()) { $db = new Database(df_db()); if (is_array($sql)) { if (count($sql) > 1) { $queryParams = $sql[1]; } else { $queryParams = array(); } $sql = $sql[0]; } $rows = $db->query($sql, (object) $queryParams); return new Portlet($rows, $cols, $opts); }
function update_profile($token = null, $values) { global $error_message; $db = new Database(df_db()); $user = get_user(); if (!$user) { $error_message = "You are not logged in"; return false; } if (@$values['avatar']) { $filename = 'avatar.' . time() . '.png'; $imgPath = 'uploads/' . sha1($user) . '/' . $filename; @mkdir('uploads/' . sha1($user)); @unlink($imgPath); move_uploaded_file($values['avatar'], $imgPath); $values['avatar'] = $filename; } if (count($values) === 0) { $error_message = "There were no values specified to update in the profile."; return false; } try { $res = $db->updateObject('profiles', (object) $values, array('username' => $user)); return true; } catch (Exception $ex) { $error_message = $ex->getMessage(); return false; } }