Beispiel #1
0
 function run(\Db\Connection $db, Logger $logger)
 {
     // "What pages are taking the longest to load?"
     // "What pages spend the most time in PHP as opposed to the database?"
     $report_type = "pages_slow";
     // select the worst URLs
     $q = $db->prepare("SELECT script_name, SUM(time_taken) AS time_taken, COUNT(id) AS page_count,\n        SUM(db_prepare_time) + SUM(db_execute_time) + SUM(db_fetch_time) + SUM(db_fetch_all_time) AS database_time FROM performance_metrics_pages\n        GROUP BY script_name ORDER BY SUM(time_taken) / COUNT(id) LIMIT " . \Openclerk\Config::get('metrics_report_count', 20));
     $q->execute();
     $data = $q->fetchAll();
     $q = $db->prepare("INSERT INTO performance_reports SET report_type=?");
     $q->execute(array($report_type));
     $report_id = $db->lastInsertId();
     foreach ($data as $row) {
         $q = $db->prepare("INSERT INTO performance_report_slow_pages SET report_id=?, script_name=?, page_time=?, page_count=?, page_database=?");
         $q->execute(array($report_id, $row['script_name'], $row['time_taken'], $row['page_count'], $row['database_time']));
     }
     $logger->info("Created report '{$report_type}'");
     // we've processed all the data we want; delete old metrics data
     $q = $db->prepare("DELETE FROM performance_metrics_pages");
     $q->execute();
     $logger->info("Deleted old metric data");
 }
Beispiel #2
0
 /**
  * @throws UserSignupException if the user could not be signed up, with a reason
  * @throws UserAlreadyExistsException if the user already exists in the database
  * @return the created {@link User}
  */
 static function trySignup(\Db\Connection $db, $email, $password)
 {
     if ($email === null) {
         throw new UserAuthenticationException("Email required for password signup.");
     }
     if (!is_valid_email($email)) {
         throw new UserAuthenticationException("That is not a valid email.");
     }
     // does a user already exist with this email?
     $q = $db->prepare("SELECT * FROM users WHERE email=? LIMIT 1");
     $q->execute(array($email));
     if ($q->fetch()) {
         throw new UserAlreadyExistsException("That email is already in use.");
     }
     // create a new user
     $q = $db->prepare("INSERT INTO users SET email=?");
     $q->execute(array($email));
     $user_id = $db->lastInsertId();
     // create a new password
     $q = $db->prepare("INSERT INTO user_passwords SET user_id=?, password_hash=?");
     $q->execute(array($user_id, UserPassword::hash($password)));
     return User::findUser($db, $user_id);
 }
Beispiel #3
0
 /**
  * @throws UserSignupException if the user could not be signed up, with a reason
  * @throws UserAlreadyExistsException if the identity or email already exists in the database
  * @return the created {@link User}
  */
 static function trySignup(\Db\Connection $db, $email, $openid, $redirect)
 {
     if (!$redirect) {
         throw new \InvalidArgumentException("No redirect provided.");
     }
     if ($email || \Openclerk\Config::get('users_require_email', false)) {
         if (!is_valid_email($email)) {
             throw new UserSignupException("That is not a valid email.");
         }
         // does a user already exist with this email?
         $q = $db->prepare("SELECT * FROM users WHERE email=? LIMIT 1");
         $q->execute(array($email));
         if ($q->fetch()) {
             throw new UserAlreadyExistsException("That email is already in use.");
         }
     }
     $light = self::validateOpenID($openid, $redirect);
     // search for existing identities
     $q = $db->prepare("SELECT * FROM user_openid_identities WHERE identity=? LIMIT 1");
     $q->execute(array($light->identity));
     if ($identity = $q->fetch()) {
         throw new UserAlreadyExistsException("An account for the OpenID identity '" . $light->identity . "' already exists.");
     }
     // otherwise create a new account
     // create a new user
     $q = $db->prepare("INSERT INTO users SET email=?");
     $q->execute(array($email));
     $user_id = $db->lastInsertId();
     // create a new identity
     $q = $db->prepare("INSERT INTO user_openid_identities SET user_id=?, identity=?");
     $q->execute(array($user_id, $light->identity));
     return User::findUser($db, $user_id);
 }
Beispiel #4
0
 /**
  * @throws UserSignupException if the user could not be signed up, with a reason
  * @throws UserAlreadyExistsException if the user already exists in the database
  * @return the created {@link User}
  */
 static function trySignup(\Db\Connection $db, OAuth2Providers $provider)
 {
     $identity = UserOAuth2::auth($provider->getProvider());
     if (!$identity) {
         throw new UserSignupException("Could not login with OAuth2.");
     }
     $email = $identity->email;
     if ($email || \Openclerk\Config::get('users_require_email', false)) {
         if (!$email) {
             throw new UserSignupException("No email address found.");
         }
         if (!is_valid_email($email)) {
             throw new UserSignupException("That is not a valid email.");
         }
         // does a user already exist with this email?
         $q = $db->prepare("SELECT * FROM users WHERE email=? LIMIT 1");
         $q->execute(array($email));
         if ($q->fetch()) {
             throw new UserAlreadyExistsException("That email '" . $email . "' is already in use.");
         }
     }
     $uid = $identity->uid;
     if (!$uid) {
         throw new UserSignupException("No UID found");
     }
     // does such an identity already exist?
     $q = $db->prepare("SELECT * FROM user_oauth2_identities WHERE provider=? AND uid=? LIMIT 1");
     $q->execute(array($provider->getKey(), $uid));
     if ($q->fetch()) {
         throw new UserAlreadyExistsException("That OAuth2 identity is already in use.");
     }
     // create a new user
     $q = $db->prepare("INSERT INTO users SET email=?");
     $q->execute(array($email));
     $user_id = $db->lastInsertId();
     // create a new identity
     $q = $db->prepare("INSERT INTO user_oauth2_identities SET user_id=?, provider=?, uid=?");
     $q->execute(array($user_id, $provider->getKey(), $uid));
     return User::findUser($db, $user_id);
 }