<?php
define('_DTOEXEC', 1); // entry point

require_once 'common.php';

if (!isset($_GET['netid'])) {
  $foundNetid = false;
} else {
  $foundNetid = true;
  $uid = preg_replace('/[^a-z0-9]+/', '', strtolower($_GET['netid']));
  
 // if (in_array($uid, array('bpelham', 'pmorton', 'cmknight'))) {
    $foundNetid = false;
 // } else {
/*    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://webscript.princeton.edu/~danli/ldap_fetch.php?id=$uid");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  // return as string
    $lookup = curl_exec($ch);
    curl_close($ch);*/
 // }

  if (preg_match('/^error/i', $lookup)) {
    $displayError = $lookup;
    $foundNetid = false;
  }
  if ($lookup == 'netid not found') {
    $foundNetid = false;
  }
}

if ($foundNetid) {
  $lookup = unserialize($lookup);
  if ($lookup === false) {
    err(504);
  }
  
  function parseLDAPdate($datestr) {
    if (strlen($datestr) != 15 || $datestr[14] != 'Z') {
      err(356);
    }
    $date = DateTime::createFromFormat('YmdHis', substr($datestr, 0, 14), new DateTimeZone('UTC'));
    $date->setTimezone(new DateTimeZone('America/New_York'));
    return $date;
  }
  
  // see http://docs.sun.com/app/docs/doc/820-0384/6nc4k4sq3?l=en&a=view or RFC2252
  function parseLDAPaddress($str) {
    return str_replace(array("\\5c", "\\5C"), "\\", str_replace("\\24", '$', str_replace('$', "\n", $str)));
  }
  
  $uid = $lookup['uid'];
  $name = $lookup['name'];
  $firstName = $lookup['firstName'];
  $vacationStartDate = $lookup['vacationStartDate'] ? parseLDAPdate($lookup['vacationStartDate']) : false;
  $vacationEndDate = $lookup['vacationEndDate'] ? parseLDAPdate($lookup['vacationEndDate']) : false;
  $autoReply = $lookup['autoReply'] ? parseLDAPaddress($lookup['autoReply']) : false;
  $autoReplyInternal = $lookup['autoReplyInternal'] ? parseLDAPaddress($lookup['autoReplyInternal']) : false; // TODO
  $autoReplyEnabled = $lookup['autoReplyEnabled'];
  $campusAddress = $lookup['campusAddress'];
  $otherEmails = $lookup['otherEmails'];
  
  $isForwarding = !empty($otherEmails);
  $onVacation = $autoReplyEnabled;
}
?>
<?php require 'header.php'; ?>
      <div class="messagesection">
        <div class="sectionheader">thanks for your attention</div>
        <div style="padding-top: 5px;">looks like i've had some success in getting people's attention. since this site was never supposed to facilitate burglary, i've stopped the display of personal information, but do check out the <a href="about.php">about page</a> describing the problem i'm trying to raise awareness about.</div>
      </div>
      <div class="contentsection">
        <div class="sectionheader">who do you want to investigate?</div>
        <div style="padding-top: 5px; font-size: 92%"><a class="showrelax" href="about.php">[ hey, i don't want to investigate anybody! what is going on here? ]</a></div>
        <div id="relaxmsg" style="display: none;">relax, nobody's going to get hurt. humor me and try at least one netid? or if you insist, <a href="about.php">cut straight to the chase</a>.</div>
        <form action="<?php echo $scriptName; ?>" method="get">
          <div class="subsection">
            <div>
              <input name="netid" id="netid" type="text" size="8" maxlength="30" value="<?php echo (isset($uid) ? $uid : ''); ?>" placeholder="netid" />
              <label for="netid" class="pton" style="font-size: 115%">@princeton.edu</label>
            </div>
            <div style="padding: 5px 0px 3px 80px; font-size: 85%">
            <img alt="^^" src="black_arrow_2.png" height="30" width="42" style="position: relative; top: 3px; left: 2px;" /> if you aren't sure, try <a class="fillinlink" href="<?php echo $scriptName; ?>?netid=danli">danli</a> or <a class="fillinlink" href="<?php echo $scriptName; ?>?netid=cwest">cwest</a> for starters
            </div>
          </div>
          <div class="subsection">
            <input type="submit" value="lookup" />
          </div>
        </form>
      </div>
<?php if (isset($displayError)) { ?>
<div class="contentsection">
<div class="sectionheader"><span class="bitswasted">oops,</span> something went wrong</div>
<div>well, this is embarrassing. would you mind emailing <?php echo "<a href='mailto:$adminEmail'>$adminEmail</a>"; ?> or making a post at <a href="http://danli.uservoice.com">http://danli.uservoice.com</a> with the following information?</div>
<pre><?php echo (isset($uid) ? "$uid | " : '') . $displayError; ?></pre>
<div>thanks a bunch!</div>
</div>
<?php } else if (isset($uid)) { ?>
  <?php if ($foundNetid) { ?>
    <table id="resultstable"><tr>
      <td class="contentsection">
          <div class="sectionheader">email check: <?php echo $isForwarding ? '<span class="bitswasted">failed</span>' : '<span class="allclear">passed</span>'; ?></div>
          <?php
            if ($isForwarding) {
              echo "<div>oh my! emails sent to $uid@princeton.edu are forwarded to the following addresses:</div>";
              echo '<pre>' . implode("\n", $otherEmails) . '</pre>';
            } else {
              echo "<div>good news! $uid@princeton.edu is not forwarding emails to another address.</div>";
            }
          ?>
      </td>
      <td class="spacer">&nbsp;</td>
      <td class="contentsection">
          <div class="sectionheader">burglary check: <?php echo $onVacation ? '<span class="allclear">now</span>' : '<span class="bitswasted">wait</span>'; ?></div>
          <?php
            $startend = '';
            $startendParen = '';
            if ($vacationStartDate || $vacationEndDate) {
              $startend = ' ' . ($vacationStartDate ? 'starting ' . strtolower($vacationStartDate->format('M j Y \a\t g:i a')) . ' ' : '');
              $startend .= ($vacationEndDate ? 'until ' . strtolower($vacationEndDate->format('M j Y \a\t g:i a')) : '');
              $startendParen = ' (' . ($vacationStartDate ? 'starting ' . strtolower($vacationStartDate->format('M j Y \a\t g:i a')) . ' ' : '');
              $startendParen .= ($vacationEndDate ? 'until ' . strtolower($vacationEndDate->format('M j Y \a\t g:i a')) : '') . ')';
            }
            if ($onVacation) {
              echo "<div class='subsection'>";
              echo "<div>great timing! looks like " . strtolower($firstName) . " is on vacation$startend, with the following auto-reply message:</div>";
              echo "<pre>" . ($autoReplyInternal ? $autoReplyInternal : $autoReply) . "</pre>";
              if ($autoReplyInternal) {
                echo "<div>though if you aren't a princetonian, you'd receive this instead:</div>";
                echo "<pre>$autoReply</pre>";
              }
              echo "</div>";
            } else {
              echo "<div class='subsection'>darn! looks like " . strtolower($firstName) . " isn't on vacation, so you'll have to come back at a later date.</div>";
              if ($autoReply || $autoReplyInternal) {
                echo "<div class='subsection'>";
                echo "<div>for what it's worth, this was " . strtolower($firstName) . "'s last vacation message$startendParen:</div>";
                echo "<pre>" . ($autoReplyInternal ? $autoReplyInternal : $autoReply) . "</pre>";
                if ($autoReplyInternal) {
                  echo "<div>though non-princetonians received this instead:</div>";
                  echo "<pre>$autoReply</pre>";
                }
                echo "</div>";
              }
            }
          ?>
      </td>
    </tr></table>
  <?php } ?>
      <div class="contentsection">
        <?php
          if(!$foundNetid) {
        
          //<div class="sectionheader">conclusion: <span class="notfound">not found</span></div>
          //<div class="subsection">hmmm&#8230;somehow we weren't able to (or have been asked not to) find that netid. we suspect you'll have a hard time robbing someone who doesn't exist, but if you believe otherwise, please let us know at the feedback link below.</div>
        ?>
        <div class="sectionheader">conclusion: <span class="notfound">mission accomplished?</span></div>
        <div class="subsection">seems like awareness has been raised, so there's no need to expose people's emails and vacation auto-responders any more. stay tuned for the university's reaction&#8230;</div>
        <?php
          } else {
            $burgleNowLater = 'burgle ' . ($onVacation ? 'now' : 'later');
            $whenToBurgle = ($onVacation ? 'now' : 'when the time is ripe');
            $burgleTarget = $campusAddress ? "<a href='http://maps.google.com/maps?q=" . urlencode("$campusAddress, Princeton, NJ") . "'>" . htmlspecialchars(strtolower($campusAddress)) . "</a>" : (strtolower($firstName) . "'s home.");
          
            if ($isForwarding) {
              echo "<div class='sectionheader'>conclusion: <span class='bitswasted'>guilty, $burgleNowLater</span></div>";
              echo "<div class='subsection'>" . htmlspecialchars(strtolower($name)) . " is disrespecting princeton by forwarding emails. ";
              echo "serve justice by breaking into $burgleTarget $whenToBurgle.</div>";
            } else {
              echo "<div class='sectionheader'>conclusion: <span class='allclear'>$burgleNowLater, if you like</span></div>";
              echo "<div class='subsection'>" . htmlspecialchars(strtolower($name)) . " is sticking to the $uid@princeton.edu email address. ";
              echo "but should you have other reasons for robbing " . strtolower($firstName) . ", feel free to break into $burgleTarget $whenToBurgle.</div>";
            }
          }
        ?>
        <div class="sectionheader" style="font-size: 225%">&#8594; <a href="about.php">mkay, but what if i'm not a robber?</a></div>
      </div>
<?php /*        <!--            <div><a class="emailfriend" href="<?php echo htmlspecialchars(sprintf("mailto:$uid@princeton.edu?subject=%s&body=%s", rawurlencode("congratulations from save the bits @ princeton"), rawurlencode("congratulations! since you don't forward your email and you haven't set an auto-reply message, you aren't wasting any bits!\n\nsee $fullRequestURL"))); ?>">congratulate <?php echo strtolower($firstName); ?> by email on this fine achievement</a></div> -->
          <!--<a class="emailfriend" href="<?php
              $warns = array();
              if ($autoReply || $autoReplyInternal) { $warns[] = 'you have a vacation auto-reply set'; }
              if (!empty($otherEmails)) { $warns[] = 'you are forwarding your email to another account'; }
              echo htmlspecialchars(sprintf("mailto:$uid@princeton.edu?subject=%s&body=%s", rawurlencode("a friendly warning from save the bits @ princeton"), rawurlencode("it looks like " . implode(' and ', $warns) . ". you may be wasting bits!\n\nsee $fullRequestURL"))); ?>">send <?php echo strtolower($firstName); ?> a friendly warning email</a></div>--> */ ?>
<?php } ?>
<?php require 'footer.php'; ?>
