| 1 | <?php |
|---|
| 2 | class ical { |
|---|
| 3 | |
|---|
| 4 | var $db; |
|---|
| 5 | var $userinfo = array(); |
|---|
| 6 | var $vEvents = array(); |
|---|
| 7 | |
|---|
| 8 | function ical($db,$userid){ |
|---|
| 9 | $this->db = $db; |
|---|
| 10 | |
|---|
| 11 | $this->_getUserInfo($userid); |
|---|
| 12 | |
|---|
| 13 | } |
|---|
| 14 | |
|---|
| 15 | function verifyUser($hash){ |
|---|
| 16 | return (md5("phpBMS".$this->userinfo["firstname"].$this->userinfo["lastname"].$this->userinfo["id"].$this->userinfo["decpass"]) == $hash); |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | function _toCalDate($sqldate){ |
|---|
| 20 | $phpDate = stringToDate($sqldate,"SQL"); |
|---|
| 21 | return date("Ymd",$phpDate); |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | function _toCalTime($sqltime){ |
|---|
| 25 | $phpTime = stringToTime($sqltime,"24 Hour"); |
|---|
| 26 | return date("His",$phpTime); |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | function _getUserInfo($userid){ |
|---|
| 30 | |
|---|
| 31 | $querystatement = "SELECT id,firstname,lastname, DECODE(password,'".ENCRYPTION_SEED."') as decpass FROM users WHERE id=".((int) $userid); |
|---|
| 32 | $queryresult = $this->db->query($querystatement); |
|---|
| 33 | |
|---|
| 34 | $this->userinfo = $this->db->fetchArray($queryresult); |
|---|
| 35 | |
|---|
| 36 | }//end method |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | function _sendHeaders(){ |
|---|
| 40 | header( 'Content-Type: text/calendar; charset=utf-8' ); |
|---|
| 41 | header("Content-Disposition: inline; filename=phpBMScalendar.ics"); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | function _startCal(){ |
|---|
| 45 | ?>BEGIN:VCALENDAR |
|---|
| 46 | VERSION:2.0 |
|---|
| 47 | PRODID:-//phpBMS v0.81//EN |
|---|
| 48 | X-WR-CALNAME:<?php echo APPLICATION_NAME; ?> For user <?php echo $this->userinfo["firstname"]." ".$this->userinfo["lastname"]."\n"?> |
|---|
| 49 | CALSCALE:GREGORIAN |
|---|
| 50 | METHOD:PUBLISH |
|---|
| 51 | <?php |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | function addEvents($startdate,$enddate = NULL){ |
|---|
| 56 | $querystatement = "SELECT id, subject, content, startdate, starttime, enddate, endtime, |
|---|
| 57 | DATE_FORMAT(creationdate,'%Y%m%dT%H%i%sZ') AS creationdate, location, |
|---|
| 58 | DATE_FORMAT(modifieddate,'%Y%m%dT%H%i%sZ') AS modifieddate, |
|---|
| 59 | repeating, repeatevery, repeattype, repeateachlist, repeatontheday, repeatontheweek, repeattimes, |
|---|
| 60 | repeatuntil |
|---|
| 61 | FROM notes |
|---|
| 62 | WHERE (notes.private = 0 OR notes.createdby=".$this->userinfo["id"].") AND notes.type='EV' AND |
|---|
| 63 | (startdate <= '".datetoString($startdate,"SQL")."'"; |
|---|
| 64 | if($enddate) |
|---|
| 65 | $querystatement .= " AND enddate >= '".datetoString($startdate,"SQL")."'"; |
|---|
| 66 | |
|---|
| 67 | $querystatement .= " OR repeating = 1)"; |
|---|
| 68 | |
|---|
| 69 | $queryresult = $this->db->query($querystatement); |
|---|
| 70 | |
|---|
| 71 | while($therecord = $this->db->fetchArray($queryresult)) |
|---|
| 72 | $this->vEvents[] = $therecord; |
|---|
| 73 | |
|---|
| 74 | }//end method |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | function _showEvents(){ |
|---|
| 78 | foreach($this->vEvents as $event){ |
|---|
| 79 | ?>BEGIN:VEVENT |
|---|
| 80 | CREATED:<?php echo $event["creationdate"]."\n"?> |
|---|
| 81 | LAST-MODIFIED:<?php echo $event["modifieddate"]."\n"?> |
|---|
| 82 | DTSTAMP:<?php echo $event["creationdate"]."\n"?> |
|---|
| 83 | UID: |
|---|
| 84 | <?php echo "BB-".$event["id"]."\n"?> |
|---|
| 85 | SUMMARY:<?php echo $event["subject"]."\n"?> |
|---|
| 86 | DTSTART:<?php |
|---|
| 87 | echo $this->_toCalDate($event["startdate"]); |
|---|
| 88 | if($event["starttime"]) |
|---|
| 89 | echo "T".$this->_toCalTime($event["starttime"])."\n"; |
|---|
| 90 | else |
|---|
| 91 | echo "T000000"."\n"; |
|---|
| 92 | ?> |
|---|
| 93 | DTEND:<?php |
|---|
| 94 | echo $this->_toCalDate($event["enddate"]); |
|---|
| 95 | if($event["starttime"]) |
|---|
| 96 | echo "T".$this->_toCalTime($event["endtime"])."\n"; |
|---|
| 97 | else |
|---|
| 98 | echo "T000000"."\n"; |
|---|
| 99 | |
|---|
| 100 | if($event["location"])echo "LOCATION:".$event["location"]."\n"; |
|---|
| 101 | if($event["content"])echo "DESCRIPTION:".str_replace("\n","",$event["content"])."\n"; |
|---|
| 102 | |
|---|
| 103 | if($event["repeating"]){ |
|---|
| 104 | $rrule = "RRULE:FREQ=".strtoupper($event["repeattype"]).";INTERVAL=".$event["repeatevery"].";"; |
|---|
| 105 | |
|---|
| 106 | switch($event["repeattype"]){ |
|---|
| 107 | |
|---|
| 108 | case "Weekly": |
|---|
| 109 | $rrule .= "BYDAY="; |
|---|
| 110 | $bydayArray = explode("::",$event["repeateachlist"]); |
|---|
| 111 | foreach($bydayArray as $day){ |
|---|
| 112 | $tempday = ($day != 7)?($day+1):(1); |
|---|
| 113 | $rrule .= strtoupper(substr(nl_langinfo(constant("ABDAY_".$tempday)), 0 ,2)).", "; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | $rrule = substr($rrule,0,(strlen($rrule)-2) ).";"; |
|---|
| 117 | break; |
|---|
| 118 | |
|---|
| 119 | case "Monthly": |
|---|
| 120 | if($event["repeateachlist"]){ |
|---|
| 121 | $rrule .= "BYMONTHDAY="; |
|---|
| 122 | $bydayArray = explode("::",$event["repeateachlist"]); |
|---|
| 123 | |
|---|
| 124 | foreach($bydayArray as $day) |
|---|
| 125 | $rrule .= $day.", "; |
|---|
| 126 | |
|---|
| 127 | $rrule = substr($rrule,0,(strlen($rrule)-2) ).";"; |
|---|
| 128 | } else { |
|---|
| 129 | if($event["repeatontheweek"] == 5) |
|---|
| 130 | $event["repeatontheweek"] = -1; |
|---|
| 131 | |
|---|
| 132 | $tempday = ($event["repeatontheday"] != 7)?($event["repeatontheday"]+1):(1); |
|---|
| 133 | |
|---|
| 134 | $rrule .= "BYDAY=".$event["repeatontheweek"].strtoupper(substr(nl_langinfo(constant("ABDAY_".$tempday)), 0 ,2)).";"; |
|---|
| 135 | }//end if |
|---|
| 136 | break; |
|---|
| 137 | |
|---|
| 138 | case "Yearly": |
|---|
| 139 | $rrule .= "BYMONTH="; |
|---|
| 140 | $bymonthArray = explode("::",$event["repeateachlist"]); |
|---|
| 141 | |
|---|
| 142 | foreach($bymonthArray as $month) |
|---|
| 143 | $rrule .= $month.", "; |
|---|
| 144 | |
|---|
| 145 | $rrule = substr($rrule,0,(strlen($rrule)-2) ).";"; |
|---|
| 146 | if($event["repeatontheday"]){ |
|---|
| 147 | if($event["repeatontheweek"] == 5) |
|---|
| 148 | $event["repeatontheweek"] = -1; |
|---|
| 149 | |
|---|
| 150 | $tempday = ($event["repeatontheday"] != 7)?($event["repeatontheday"]+1):(1); |
|---|
| 151 | |
|---|
| 152 | $rrule .= "BYDAY=".$event["repeatontheweek"].strtoupper(substr(nl_langinfo(constant("ABDAY_".$tempday)), 0 ,2)).";"; |
|---|
| 153 | }//endif |
|---|
| 154 | break; |
|---|
| 155 | |
|---|
| 156 | }//endcase |
|---|
| 157 | |
|---|
| 158 | if($event["repeattimes"]) |
|---|
| 159 | $rrule .= "COUNT=".$event["repeattimes"].";"; |
|---|
| 160 | elseif($event["repeatuntil"]) |
|---|
| 161 | $rrule = "UNTIL=".$this->_toCalDate($event["repeatuntil"])."T000000;"; |
|---|
| 162 | |
|---|
| 163 | echo substr($rrule,0,strlen($rrule)-1)."\n"; |
|---|
| 164 | }//endif repeating |
|---|
| 165 | ?> |
|---|
| 166 | END:VEVENT |
|---|
| 167 | <?php |
|---|
| 168 | }//endforeach |
|---|
| 169 | }//end method |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | function showCalendar(){ |
|---|
| 173 | $this->_sendHeaders(); |
|---|
| 174 | |
|---|
| 175 | $this->_startCal(); |
|---|
| 176 | |
|---|
| 177 | $this->_showEvents(); |
|---|
| 178 | |
|---|
| 179 | $this->_endCal(); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | function _endCal(){ |
|---|
| 184 | ?>END:VCALENDAR |
|---|
| 185 | <?php |
|---|
| 186 | }//end methdo |
|---|
| 187 | |
|---|
| 188 | }//end class |
|---|
| 189 | |
|---|
| 190 | //============================================================================================================== |
|---|
| 191 | if (isset($_GET["u"]) && isset($_GET["h"]) && !isset($dontProcess)){ |
|---|
| 192 | $loginNoKick=true; |
|---|
| 193 | $loginNoDisplayError=true; |
|---|
| 194 | |
|---|
| 195 | include("../../include/session.php"); |
|---|
| 196 | |
|---|
| 197 | $theCal = new ical($db,$_GET["u"]); |
|---|
| 198 | |
|---|
| 199 | if($theCal->verifyUser($_GET["h"])) |
|---|
| 200 | |
|---|
| 201 | $theCal->addEvents(strtotime("-1 month",mktime(0,0,0))); |
|---|
| 202 | |
|---|
| 203 | $theCal->showCalendar(); |
|---|
| 204 | |
|---|
| 205 | } |
|---|
| 206 | ?> |
|---|