/**
  * Create Authentication object
  * This function is overwritable from the custom helpers/auth_helper.php
  *
  * @param  string $id PK value
  * @param  object $data    The user data object (optional). If it is not given, auth_create will load it from db
  *
  * @return object The authenticated user object or FALSE on failure
  */
 function auth_create($id, $data = null)
 {
     $lc_auth = auth_prerequisite();
     $auth = auth_get();
     if (!$auth) {
         $table = db_prefix() . str_replace(db_prefix(), '', $lc_auth['table']);
         $fieldId = $lc_auth['fields']['id'];
         $fieldRole = $lc_auth['fields']['role'];
         if (is_object($data)) {
             $session = $data;
         } else {
             $sql = 'SELECT * FROM ' . $table . ' WHERE ' . $fieldId . ' = :id LIMIT 1';
             if ($result = db_query($sql, array(':id' => $id))) {
                 $session = db_fetchObject($result);
             }
         }
         if (isset($session)) {
             $session->sessId = session_id();
             $session->timestamp = md5(time());
             $session->permissions = auth_permissions($session->{$fieldRole});
             $session->blocks = auth_blocks($session->{$fieldRole});
             auth_set($session);
             return $session;
         }
     } else {
         return $auth;
     }
     return false;
 }
 /**
  * Perform a query on the database and return the result object
  *
  * @return object|null The result object
  *   If the result not found, return null.
  */
 public function getSingleResult()
 {
     $this->limit(1);
     if ($this->result === null) {
         $this->execute();
     }
     if ($row = db_fetchObject($this->result)) {
         return $row;
     }
     return null;
 }
/**
 * Perform a query on the database and return the first result row as object
 *
 * It adds the `LIMIT 1` clause if the query has no record limit
 * This is useful for one-row fetching. No need explicit `db_query()` call as this invokes it internally.
 *
 * @param string $sql The SQL query string
 * @param array $args The array of placeholders and their values
 *
 *      array(
 *          ':placeholder1' => $value1,
 *          ':placeholder2' => $value2
 *      )
 *
 * @return object The result object
 */
function db_fetchResult($sql, $args = array())
{
    if (!preg_match('/LIMIT\\s+[0-9]{1,}\\b/i', $sql)) {
        $sql .= ' LIMIT 1';
    }
    if ($result = db_query($sql, $args)) {
        if ($row = db_fetchObject($result)) {
            return $row;
        }
    }
    return false;
}