<?php	// TWIG		Commonly used functions.	?>
<?php

if (!$included[common.inc]) {

 if (!$thisYear)	{ $thisYear = 0 + date("Y"); }
 if (!$thisMonth)	{ $thisMonth = 0 + date("m"); }
 if (!$thisDay)		{ $thisDay = 0 + date("d"); }

 $dateQuery = "thisYear=$thisYear&thisMonth=$thisMonth&thisDay=$thisDay";
 $todaysYear	= 0 + date("Y");
 $todaysMonth	= 0 + date("m");
 $todaysDay 	= 0 + date("d");

 $dayNamesShort=array(0=>'S',1=>'M',2=>'T',3=>'W', 4=>'T',5=>'F',6=>'S');
 $dayNames=array(
	0=>'Sunday',
	1=>'Monday',
	2=>'Tuesday',
	3=>'Wednesday',
	4=>'Thursday',
	5=>'Friday',
	6=>'Saturday');
	
 $monthNames=array(
	1=>'January',
	2=>'February',
	3=>'March',
	4=>'April',
	5=>'May',
	6=>'June',
	7=>'July',
	8=>'August',
	9=>'September',
	10=>'October',
	11=>'November',
	12=>'December');

 // Names of Mime Types;
 $mimetype[0]="7bit";
 $mimetype[1]="8bit";
 $mimetype[2]="binary";
 $mimetype[3]="base64";
 $mimetype[4]="quoted-printable";
 $mimetype[5]="unknown";

 // Mailbox Sort Defs
 $sortbyint["date"]=1;
 $sortbyint["from"]=2;
 $sortbyint["subject"]=3;
 $sortbyint["to"]=4;

 Function PrintError ($string) {
  echo "<b>Error</b>: <i>$string</i><br>\n";
 }

 // Create link with some status info
 Function TwigLink ($text,$command,$args="",$path="",$newmailbox="",$target="") {
  global $baseurl, $mailbox, $thisGroup;
  global $sortby, $sortbyway, $startmsg;
  if ($newmailbox) { $str = "$baseurl/$command/$newmailbox/"; }
  else { $str= "$baseurl/$command/$mailbox/"; }
  if ($path) { $str .= "$path/"; }
  $str .= "?thisGroup=$thisGroup";
  if ($sortby) { $str .= "&sortby=$sortby"; }
  if ($sortbyway) { $str .= "&sortbyway=$sortbyway"; }
  if ($startmsg) { $str .= "&startmsg=$startmsg"; }
  if ($args) { $str .= "&$args"; }
  if ($target) { $targetstr = "target=\"$target\""; }
  $str .= "&ts=" . mktime();	// Make each URL unique (screw you n+2.0!)
  return "<a href=\"$str\" $targetstr>$text</a>";
 }

 // Open a mailbox without a lot of hassle
 Function twig_open ($mailbox) {
  global $imap_server, $PHP_AUTH_USER, $PHP_AUTH_PW;
  if (!$imap_server) { $imap_server = "localhost"; }
  if (!$imap_port) { $imap_port = "143"; }
  $mbox = @imap_open("{$imap_server:$imap_port}$mailbox",$PHP_AUTH_USER,$PHP_AUTH_PW);
  if ($mbox) { return $mbox; }
 }

 Function twig_new_msgs($stream = "yourmom") {
  global $mbox;
  if ($stream == "yourmom") { $stream = $mbox; }
  $check=imap_check($mbox);
  return $check->Recent;
 }

 Function twig_total_msgs($stream = "yourmom") {
  global $mbox;
  if ($stream == "yourmom") { $stream = $mbox; }
  $check=imap_check($mbox);
  return $check->Nmsgs;
 }

 Function mboxSelect($stream = "yourmom") {
  global $mbox;
  global $baseurl, $mailbox, $imap_server, $imap_port;
  global $sortby, $sortbyway, $thisGroup, $startmsg;

  if ($stream == "yourmom") { $stream = $mbox; }
  $str = "<form action=\"$baseurl/mbox_changeto/$mailbox\" METHOD=POST>\n";
  $str .= "<input type=hidden name=sortby value=\"$sortby\">\n";
  $str .= "<input type=hidden name=sortbyway value=\"$sortbyway\">\n";
  $str .= "<input type=hidden name=thisGroup value=\"$thisGroup\">\n";
  $str .= "<input type=hidden name=startmsg value=\"$startmsg\">\n";
  $str .= "<input type=hidden name=ts value=\"". mktime() . "\">\n";
  $str .= "<select name=newmailbox>\n";

  $subscribed=imap_listsubscribed($mbox,"{$imap_server:$imap_port}","*");
  $i=0;
  while ($i<count($subscribed)) {
   $mb=ereg_replace("\{.*\}","",$subscribed[$i]);
   if (MailboxEncode($mb) == $mailbox) { $mbselected = "selected"; }
   else { $mbselected = ""; }
   $str .= "<option value=\"$mb\" $mbselected>";
   $str .= MailboxDecode($mb) . "\n";
   $i++;
  }
  $str .= "</select>\n<input type=submit value=\"Go\">\n</form>\n";
  return $str;
 }

 Function isAdmin($username = "$PHP_AUTH_USER") {
  return 0;
 }

 Function groupName($gid) {
  global $dbconfig;
  if ($gid == "*") {
   return "ALL";
  }
  if ($gid == 0) {
   return "Unfiled";
  }
  if ($gid>0) {
   $query="SELECT groupname FROM $dbconfig[groups_table] WHERE id = $gid";
   $result=dbQuery($query);
   return dbResult($result,0,"groupname");
  }
  else { return "Unfiled"; }
 }

 Function memberOf($gid) {
  global $PHP_AUTH_USER, $dbconfig;
  $query="SELECT groupid FROM $dbconfig[members_table] WHERE username = '$PHP_AUTH_USER' AND groupid = '$gid'";
  $result=dbQuery($query);
  for($i=0; $i<dbNumRows($result); $i++) {
   if (dbResult($result,$i,"groupid")) { $groups = "y"; }
  }
  return $groups;
 }

 // Returns members of $gid group
 Function groupMembers($gid) {
  global $dbconfig;
  $query="SELECT username FROM $dbconfig[members_table] WHERE groupid = $gid";
  $result=dbQuery($query);
  for($i=0; $i<dbNumRows($result); $i++) {
   $members[]=dbResult($result,$i,"username");
  }
  return $members;
 }

 // Returns Groups $user is a member of
 Function groupMembership($user) {
  global $dbconfig;
  $query="SELECT * FROM $dbconfig[members_table] WHERE username = '$user'";
  $result=dbQuery($query);
  for($i=0; $i<dbNumRows($result); $i++) {
   $groups[$i]=dbResult($result,$i,"groupid");
  }
  return $groups;
 }

 // Returns owner of $gid group
 Function groupOwner($gid) {
  global $dbconfig;
  $query="SELECT owner FROM $dbconfig[groups_table] WHERE id = '$gid'";
  $result=dbQuery($query);
  for($i=0; $i<dbNumRows($result); $i++) {
   $owner[$gid]=dbResult($result,$i,"owner");
  }
  return $owner[$gid];
 }

 // Returns groups $user is owner of
 Function groupOwnership($user) {
  global $dbconfig;
  $query="SELECT * FROM $dbconfig[groups_table] WHERE owner = '$user'";
  $result=dbQuery($query);
  for($i=0; $i<dbNumRows($result); $i++) {
   $groups[]=dbResult($result,$i,"id");
  }
  return $groups;
 }

 Function groupSelect($username = "$PHP_AUTH_USER", $thisGroup = "") {
  $str .= "<option value=0>Unfiled\n";
  $groups=groupOwnership($username);
  for ($i=0; $i<count($groups); $i++) {
   $selected = "";
   if ($groups[$i] == $thisGroup) { $selected = "selected"; }
   $str .= "<option value=$groups[$i] $selected>" . groupName($groups[$i]) . "\n";
  }
  $groups=groupMembership($username);
  for ($i=0; $i<count($groups); $i++) {
   $selected = "";
   if ($groups[$i] == $thisGroup) { $selected = "selected"; }
   $str .= "<option value=$groups[$i] $selected>" . groupName($groups[$i]) . "\n";
  }
  return $str;
 }

 Function massage($str) { // Fixes fu for things like NetPositive
  return htmlspecialchars(ereg_replace("\r","",$str));
 } 

 Function MailboxEncode ($mb) { return ereg_replace("/","!",$mb); }
 Function MailboxDecode ($mb) { return ereg_replace("!","/",$mb); }

 Function FindHtml ($string) { // Cheesy but works
  global $baseurl, $mailbox;
  $string = ereg_replace("-","hhyypphheenn",$string);
  $string = eregi_replace("([A-Za-z0-9%_=\.]*@[A-Za-z0-9%_\.]*)","<a href=mailto:\\1>\\1</a>",$string);
  $string = eregi_replace("(http://[A-Za-z\._\%&;~=,#0-9/\?]*)","<a href=\\1 target=_new>\\1</a>",$string);
  $string = eregi_replace("(ftp://[A-Za-z\._\%~&;=,#0-9/\?]*)","<a href=\\1 target=_new>\\1</a>",$string);
  $string = ereg_replace("hhyypphheenn","-",$string);
  $string = eregi_replace("=mailto:","=$baseurl/compose/$mailbox?form[to]=",$string);
  return $string;
 }

 Function twig_Auth ($realm = "TWIG", $logout = "") {
  global $PHP_AUTH_USER, $PHP_AUTH_PW, $mailbox;
  // Require a username/password from the user
  if (!$PHP_AUTH_USER || ($logout == $PHP_AUTH_USER)) {
   Header("WWW-authenticate: basic realm=\"$realm\"");
   Header("HTTP/1.0 401 Unauthorized");
   if ($messages["nouser"]) { echo $messages["baduser"]; }
   else { echo "You must enter a username"; }
   exit;
  }
  else {
   $mbox=twig_open($mailbox);
   if (!$mbox) {
    Header("WWW-authenticate: basic realm=\"$realm\"");
    Header("HTTP/1.0 401 Unauthorized");
    echo "Wrong Password";
    exit;
   }
  }
  return $mbox;
 }

 Function zpad($num, $top = 10) {
  if ($num < $top) { return "0" . $num; }
  else { return $num; }
 }

 Function sqltime2nice($str) {
  ereg("^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})",$str,$regs);
  return date("h:i a",mktime($regs[4],$regs[5],00,$regs[2],$regs[3],$regs[1]));
 }

 // Page Title Settings
 $pagetitle["default"] = "TWIG";
 $pagetitle["calendar"] = "TWIG [Calendar]";
 $pagetitle["calendar_edit"] = "TWIG [Calendar]";
 $pagetitle["todo"] = "TWIG [Todo]";
 $pagetitle["todo_edit"] = "TWIG [Todo]";
 $pagetitle["bookmarks"] = "TWIG [Bookmarks]";
 $pagetitle["bookmarks_edit"] = "TWIG [Bookmarks]";
 $pagetitle["contacts"] = "TWIG [Contacts]";
 $pagetitle["contacts_edit"] = "TWIG [Contacts]";
 $pagetitle["schedule"] = "TWIG [Schedule]";
 $pagetitle["msgview"] = "TWIG [Mail]";
 $pagetitle["mbox"] = "TWIG [Mail]";
 $pagetitle["compose"] = "TWIG [Mail]";
 $pagetitle["delete"] = $pagetitle["mbox"];
 $pagetitle["undelete"] = $pagetitle["mbox"];
 $pagetitle["expunge"] = $pagetitle["mbox"];
 $pagetitle["prefs"] = "TWIG [Mail]";
 $pagetitle["changeto"] = "TWIG [Mail]";
 $pagetitle["addressbook"] = "TWIG [Mail]";
 $pagetitle["addressbook_edit"] = "TWIG [Mail]";
 $pagetitle["subscribe"] = "TWIG [Mail]";
 $pagetitle["groups"] = "TWIG [Groups]";
 $pagetitle["groups_edit"] = "TWIG [Groups]";
 $pagetitle["members"] = "TWIG [Groups]";
 $pagetitle["members_edit"] = "TWIG [Groups]";

 $included[common.inc]=1;
}

?>
