<?php

require '../scripts/authorize.php';
require '../scripts/database_connection.php';
require '../scripts/view.php';

session_start();

// Authorize any user, as long as they're logged in
authorize_user();

// Get the user ID of the user to show
$user_id = $_REQUEST['user_id'];

if (!isset($user_id)) {
  $user_id = $_SESSION['user_id'];
}

// Build the SELECT statement
$select_query = "SELECT * FROM users WHERE user_id = " . $user_id;

// Run the query
$result = mysql_query($select_query);

if ($result) {
  $row = mysql_fetch_array($result);
  $first_name     = $row['first_name'];
  $last_name      = $row['last_name'];
  $bio            = preg_replace("/[\r\n]+/", "</p><p>", $row['bio']);
  $email          = $row['email'];
  $facebook_url   = $row['facebook_url'];
  $twitter_handle = $row['twitter_handle'];
  $user_image     = get_web_path($row['user_pic_path']);

  // Turn $twitter_handle into a URL
  $twitter_url = "http://www.twitter.com/" . substr($twitter_handle, $position + 1);

} else {
  handle_error("there was a problem finding your information in our system.",
               "Error locating user with ID {$user_id}");
}

page_start("User Profile");
?>

  <div id="content">
    <div class="user_profile">
      <h1><?php echo "{$first_name} {$last_name}"; ?></h1>
      <p><img src="<?php echo $user_image; ?>" class="user_pic" />
        <?php echo $bio; ?></p>
      <p class="contact_info">Get in touch with <?php echo $first_name; ?>:</p>
      <ul>
        <li>...by emailing them at 
          <a href="<?php echo $email; ?>"><?php echo $email; ?></a></li>
        <li>...by 
          <a href="<?php echo $facebook_url; ?>">checking them out on Facebook</a></li>
        <li>...by <a href="<?php echo $twitter_url; ?>">following them on Twitter</a></li>
      </ul>
    </div>
  </div>
  <div id="footer"></div>
 </body>
</html>
