function import($filename, $rawtext = "")
 {
     @ini_set("max_execution_time", 600);
     echo JText::sprintf("Importing events from ical file %s", $filename) . "<br/>";
     $cfg =& JEVConfig::getInstance();
     $option = JEV_COM_COMPONENT;
     // resultant data goes here
     if ($filename != "") {
         $file = $filename;
         if (!@file_exists($file)) {
             $file = JPATH_SITE . "/components/{$option}/" . $filename;
         }
         if (!file_exists($file)) {
             echo "I hope this is a URL!!<br/>";
             $file = $filename;
         }
         // get name
         $isFile = false;
         if (isset($_FILES['upload']) && is_array($_FILES['upload'])) {
             $uploadfile = $_FILES['upload'];
             // MSIE sets a mime-type of application/octet-stream
             if ($uploadfile['size'] != 0 && ($uploadfile['type'] == "text/calendar" || $uploadfile['type'] == "text/csv" || $uploadfile['type'] == "application/octet-stream" || $uploadfile['type'] == "text/html")) {
                 $this->srcURL = $uploadfile['name'];
                 $isFile = true;
             }
         }
         if ($this->srcURL == "") {
             $this->srcURL = $file;
         }
         // $this->rawData = iconv("ISO-8859-1","UTF-8",file_get_contents($file));
         if (!$isFile && is_callable("curl_exec")) {
             $ch = curl_init();
             curl_setopt($ch, CURLOPT_URL, ($isFile ? "file://" : "") . $file);
             curl_setopt($ch, CURLOPT_VERBOSE, 1);
             curl_setopt($ch, CURLOPT_POST, 0);
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
             $this->rawData = curl_exec($ch);
             curl_close($ch);
             // try file_get_contents as a backup
             if ($isFile && $this->rawData === false) {
                 $this->rawData = @file_get_contents($file);
             }
         } else {
             $this->rawData = @file_get_contents($file);
         }
         if ($this->rawData === false) {
             // file_get_contents: no or blocked wrapper for $file
             JError::raiseNotice(0, 'file_get_contents() failed, try fsockopen');
             $parsed_url = parse_url($file);
             if ($parsed_url === false) {
                 JError::raiseWarning(0, 'url not parsed: ' . $file);
             } else {
                 if ($parsed_url['scheme'] == 'http' || $parsed_url['scheme'] == 'https' || $parsed_url['scheme'] == 'webcal') {
                     // try socked connection
                     $fsockhost = $parsed_url['host'];
                     $fsockport = 80;
                     if ($parsed_url['scheme'] == 'https') {
                         $fsockhost = 'ssl://' . $fsockhost;
                         $fsockport = 443;
                     }
                     if (array_key_exists('port', $parsed_url)) {
                         $fsockport = $parsed_url['port'];
                     }
                     $fh = @fsockopen($fsockhost, $fsockport, $errno, $errstr, 3);
                     if ($fh === false) {
                         // fsockopen: no connect
                         JError::raiseWarning(0, 'fsockopen: no connect for ' . $file . ' - ' . $errstr);
                         return false;
                     } else {
                         $fsock_path = (array_key_exists('path', $parsed_url) ? $parsed_url['path'] : '') . (array_key_exists('query', $parsed_url) ? $parsed_url['query'] : '') . (array_key_exists('fragment', $parsed_url) ? $parsed_url['fragment'] : '');
                         fputs($fh, "GET {$fsock_path} HTTP/1.0\r\n");
                         fputs($fh, "Host: " . $parsed_url['host'] . "\r\n\r\n");
                         while (!feof($fh)) {
                             $this->rawData .= fread($fh, 4096);
                         }
                         fclose($fh);
                         $this->rawData = JString::substr($this->rawData, JString::strpos($this->rawData, "\r\n\r\n") + 4);
                     }
                 }
             }
         }
         // Returns true if $string is valid UTF-8 and false otherwise.
         /*
         $isutf8 = $this->detectUTF8($this->rawData);
         if ($isutf8) {
         $this->rawData = iconv("ISO-8859-1","UTF-8",$this->rawData);
         }
         */
     } else {
         $this->srcURL = "n/a";
         $this->rawData = $rawtext;
     }
     // get rid of spurious carriage returns and spaces
     //$this->rawData = preg_replace("/[\r\n]+ ([:;])/","$1",$this->rawData);
     // simplify line feed
     $this->rawData = str_replace("\r\n", "\n", $this->rawData);
     // remove spurious lines before calendar start
     if (!JString::stristr($this->rawData, 'BEGIN:VCALENDAR')) {
         // check for CSV format
         $firstLine = JString::substr($this->rawData, 0, JString::strpos($this->rawData, "\n") + 1);
         if (JString::stristr($firstLine, 'SUMMARY') && JString::stristr($firstLine, 'DTSTART') && JString::stristr($firstLine, 'DTEND') && JString::stristr($firstLine, 'CATEGORIES') && JString::stristr($firstLine, 'TIMEZONE')) {
             $timezone = date_default_timezone_get();
             $csvTrans = new CsvToiCal($file);
             $this->rawData = $csvTrans->getRawData();
             date_default_timezone_set($timezone);
         } else {
             JError::raiseWarning(0, 'Not a valid VCALENDAR data file: ' . $this->srcURL);
             //JError::raiseWarning(0, 'Not a valid VCALENDAR or CSV data file: ' . $this->srcURL);
             // return false so that we don't remove a valid calendar because of a bad URL load!
             return false;
         }
     }
     $begin = JString::strpos($this->rawData, "BEGIN:VCALENDAR", 0);
     $this->rawData = JString::substr($this->rawData, $begin);
     //		$this->rawData = preg_replace('/^.*\n(BEGIN:VCALENDAR)/s', '$1', $this->rawData, 1);
     // unfold content lines according the unfolding procedure of rfc2445
     $this->rawData = str_replace("\n ", "", $this->rawData);
     $this->rawData = str_replace("\n\t", "", $this->rawData);
     // TODO make sure I can always ignore the second line
     // Some google calendars has spaces and carriage returns in their UIDs
     // Convert string into array for easier processing
     $this->rawData = explode("\n", $this->rawData);
     $skipuntil = null;
     foreach ($this->rawData as $vcLine) {
         //$vcLine = trim($vcLine); // trim one line
         if (!empty($vcLine)) {
             // skip unhandled block
             if ($skipuntil) {
                 if (trim($vcLine) == $skipuntil) {
                     // found end of block to skip
                     $skipuntil = null;
                 }
                 continue;
             }
             $matches = explode(":", $vcLine, 2);
             if (count($matches) == 2) {
                 list($this->key, $value) = $matches;
                 //$value = str_replace('\n', "\n", $value);
                 //$value = stripslashes($value);
                 $append = false;
                 // Treat Accordingly
                 switch ($vcLine) {
                     case "BEGIN:VTODO":
                         // start of VTODO section
                         $this->todoCount++;
                         $parent = "VTODO";
                         break;
                     case "BEGIN:VEVENT":
                         // start of VEVENT section
                         $this->eventCount++;
                         $parent = "VEVENT";
                         break;
                     case "BEGIN:VCALENDAR":
                     case "BEGIN:DAYLIGHT":
                     case "BEGIN:VTIMEZONE":
                     case "BEGIN:STANDARD":
                         $parent = $value;
                         // save tu array under value key
                         break;
                     case "END:VTODO":
                     case "END:VEVENT":
                     case "END:VCALENDAR":
                     case "END:DAYLIGHT":
                     case "END:VTIMEZONE":
                     case "END:STANDARD":
                         $parent = "VCALENDAR";
                         break;
                     default:
                         // skip unknown BEGIN/END blocks
                         if ($this->key == 'BEGIN') {
                             $skipuntil = 'END:' . $value;
                             break;
                         }
                         // Generic processing
                         $this->add_to_cal($parent, $this->key, $value, $append);
                         break;
                 }
             } else {
                 // ignore these lines go
             }
         }
     }
     // Sort the events into start date order
     // there's little point in doing this id an RRULE is present!
     //	usort($this->cal['VEVENT'], array("iCalImport","comparedates"));
     // Populate vevent class - should do this first trawl through !!
     if (array_key_exists("VEVENT", $this->cal)) {
         foreach ($this->cal["VEVENT"] as $vevent) {
             // trap for badly constructed all day events
             if (isset($vevent["DTSTARTRAW"]) && isset($vevent["DTENDRAW"]) && $vevent["DTENDRAW"] == $vevent["DTSTARTRAW"]) {
                 $vevent["DTEND"] += 86400;
             }
             $this->vevents[] = iCalEvent::iCalEventFromData($vevent);
         }
     }
     return $this;
 }
Exemplo n.º 2
0
 function save($array, &$queryModel, $rrule, $dryrun = false)
 {
     $cfg =& JEVConfig::getInstance();
     $db =& JFactory::getDBO();
     $user = JFactory::getUser();
     // Allow plugins to check data validity
     $dispatcher =& JDispatcher::getInstance();
     JPluginHelper::importPlugin("jevents");
     $res = $dispatcher->trigger('onBeforeSaveEvent', array(&$array, &$rrule, $dryrun));
     // TODO do error and hack checks here
     $ev_id = intval(JArrayHelper::getValue($array, "evid", 0));
     $newevent = $ev_id == 0;
     $data = array();
     // TODO add UID to edit form
     $data["UID"] = JArrayHelper::getValue($array, "uid", md5(uniqid(rand(), true)));
     $data["X-EXTRAINFO"] = JArrayHelper::getValue($array, "extra_info", "");
     $data["LOCATION"] = JArrayHelper::getValue($array, "location", "");
     $data["allDayEvent"] = JArrayHelper::getValue($array, "allDayEvent", "off");
     $data["CONTACT"] = JArrayHelper::getValue($array, "contact_info", "");
     $data["DESCRIPTION"] = JArrayHelper::getValue($array, "jevcontent", "");
     $data["publish_down"] = JArrayHelper::getValue($array, "publish_down", "2006-12-12");
     $data["publish_up"] = JArrayHelper::getValue($array, "publish_up", "2006-12-12");
     $data["SUMMARY"] = JArrayHelper::getValue($array, "title", "");
     $data["URL"] = JArrayHelper::getValue($array, "url", "");
     // If user is jevents can deleteall or has backend access then allow them to specify the creator
     $jevuser = JEVHelper::getAuthorisedUser();
     $creatorid = JRequest::getInt("jev_creatorid", 0);
     if ($creatorid > 0) {
         if (JVersion::isCompatible("1.6.0")) {
             //$access = JAccess::check($user->id, "core.deleteall","com_jevents");
             $access = $user->authorise('core.admin', 'com_jevents');
         } else {
             // Get an ACL object
             $acl =& JFactory::getACL();
             $grp = $acl->getAroGroup($user->get('id'));
             $access = $acl->is_group_child_of($grp->name, 'Public Backend');
         }
         if ($jevuser && $jevuser->candeleteall || $access) {
             $data["X-CREATEDBY"] = $creatorid;
         }
     }
     $ics_id = JArrayHelper::getValue($array, "ics_id", 0);
     if ($data["allDayEvent"] == "on") {
         $start_time = "00:00";
     } else {
         $start_time = JArrayHelper::getValue($array, "start_time", "08:00");
     }
     $publishstart = $data["publish_up"] . ' ' . $start_time . ':00';
     $data["DTSTART"] = JevDate::strtotime($publishstart);
     if ($data["allDayEvent"] == "on") {
         $end_time = "00:00";
     } else {
         $end_time = JArrayHelper::getValue($array, "end_time", "15:00");
     }
     $publishend = $data["publish_down"] . ' ' . $end_time . ':00';
     if (isset($array["noendtime"]) && $array["noendtime"]) {
         $publishend = $data["publish_down"] . ' 23:59:59';
     }
     $data["DTEND"] = JevDate::strtotime($publishend);
     // iCal for whole day uses 00:00:00 on the next day JEvents uses 23:59:59 on the same day
     list($h, $m, $s) = explode(":", $end_time . ':00');
     if ($h + $m + $s == 0 && $data["allDayEvent"] == "on" && $data["DTEND"] > $data["DTSTART"]) {
         //if (($h+$m+$s)==0 && $data["allDayEvent"]=="on" && $data["DTEND"]>=$data["DTSTART"]) {
         //$publishend = JevDate::strftime('%Y-%m-%d 23:59:59',($data["DTEND"]-86400));
         $publishend = JevDate::strftime('%Y-%m-%d 23:59:59', $data["DTEND"]);
         $data["DTEND"] = JevDate::strtotime($publishend);
     }
     $data["RRULE"] = $rrule;
     $data["MULTIDAY"] = JArrayHelper::getValue($array, "multiday", "1");
     $data["NOENDTIME"] = JArrayHelper::getValue($array, "noendtime", "0");
     $data["X-COLOR"] = JArrayHelper::getValue($array, "color", "");
     $data["LOCKEVENT"] = JArrayHelper::getValue($array, "lockevent", "0");
     // Add any custom fields into $data array
     foreach ($array as $key => $value) {
         if (strpos($key, "custom_") === 0) {
             $data[$key] = $value;
         }
     }
     $vevent = iCalEvent::iCalEventFromData($data);
     $vevent->catid = JArrayHelper::getValue($array, "catid", 0);
     if (is_array($vevent->catid)) {
         JArrayHelper::toInteger($vevent->catid);
     }
     // if catid is empty then use the catid of the ical calendar
     if (is_string($vevent->catid) && $vevent->catid <= 0 || is_array($vevent->catid) && count($vevent->catid) == 0) {
         $query = "SELECT catid FROM #__jevents_icsfile WHERE ics_id={$ics_id}";
         $db->setQuery($query);
         $vevent->catid = $db->loadResult();
     }
     $vevent->access = intval(JArrayHelper::getValue($array, "access", 0));
     if (!JVersion::isCompatible("1.6.0")) {
         $vevent->access = $vevent->access > $user->aid ? $user->aid : $vevent->access;
     }
     $vevent->state = intval(JArrayHelper::getValue($array, "state", 0));
     // Shouldn't really do this like this
     $vevent->_detail->priority = intval(JArrayHelper::getValue($array, "priority", 0));
     // FRONT END AUTO PUBLISHING CODE
     $frontendPublish = JEVHelper::isEventPublisher();
     if (!$frontendPublish) {
         $frontendPublish = JEVHelper::canPublishOwnEvents($ev_id);
     }
     // Always unpublish if no Publisher otherwise publish automatically (for new events)
     // Should we always notify of new events
     $notifyAdmin = $cfg->get("com_notifyallevents", 0);
     if (!JFactory::getApplication()->isAdmin()) {
         if ($frontendPublish && $ev_id == 0) {
             $vevent->state = 1;
         } else {
             if (!$frontendPublish) {
                 $vevent->state = 0;
                 // In this case we send a notification email to admin
                 $notifyAdmin = true;
             }
         }
     }
     $vevent->icsid = $ics_id;
     if ($ev_id > 0) {
         $vevent->ev_id = $ev_id;
     }
     $rp_id = intval(JArrayHelper::getValue($array, "rp_id", 0));
     if ($rp_id > 0) {
         // I should be able to do this in one operation but that can come later
         $testevent = $queryModel->listEventsById(intval($rp_id), 1, "icaldb");
         if (!JEVHelper::canEditEvent($testevent)) {
             JError::raiseError(403, JText::_('ALERTNOTAUTH'));
         }
     }
     $db =& JFactory::getDBO();
     $success = true;
     //echo "class = ".get_class($vevent);
     if (!$dryrun) {
         if (!$vevent->store()) {
             echo $db->getErrorMsg() . "<br/>";
             $success = false;
             JError::raiseWarning(101, JText::_('COULD_NOT_SAVE_EVENT_'));
         }
     } else {
         // need a value for eventid to pretend we have saved the event so we can get the repetitions
         if (!isset($vevent->ev_id)) {
             $vevent->ev_id = 0;
         }
         $vevent->rrule->eventid = $vevent->ev_id;
     }
     // Only update the repetitions if the event edit says the reptitions will have changed or a new event
     if ($newevent || JRequest::getInt("updaterepeats", 1)) {
         $repetitions = $vevent->getRepetitions(true);
         if (!$dryrun) {
             if (!$vevent->storeRepetitions()) {
                 echo $db->getErrorMsg() . "<br/>";
                 $success = false;
                 JError::raiseWarning(101, JText::_('COULD_NOT_SAVE_REPETITIONS'));
             }
         }
     }
     $res = $dispatcher->trigger('onAfterSaveEvent', array(&$vevent, $dryrun));
     if ($dryrun) {
         return $vevent;
     }
     // If not authorised to publish in the frontend then notify the administrator
     if (!$dryrun && $success && $notifyAdmin && !JFactory::getApplication()->isAdmin()) {
         JLoader::register('JEventsCategory', JEV_ADMINPATH . "/libraries/categoryClass.php");
         $cat = new JEventsCategory($db);
         $cat->load($vevent->catid);
         $adminuser = $cat->getAdminUser();
         $adminEmail = $adminuser->email;
         $config = new JConfig();
         $sitename = $config->sitename;
         $subject = JText::_('JEV_MAIL_ADDED') . ' ' . $sitename;
         $subject = $vevent->state == '1' ? '[Info] ' . $subject : '[Approval] ' . $subject;
         $Itemid = JEVHelper::getItemid();
         // reload the event to get the reptition ids
         $evid = intval($vevent->ev_id);
         $testevent = $queryModel->getEventById($evid, 1, "icaldb");
         $rp_id = $testevent->rp_id();
         list($year, $month, $day) = JEVHelper::getYMD();
         //http://joomlacode1.5svn/index.php?option=com_jevents&task=icalevent.edit&evid=1&Itemid=68&rp_id=72&year=2008&month=09&day=10&lang=cy
         $uri =& JURI::getInstance(JURI::base());
         $root = $uri->toString(array('scheme', 'host', 'port'));
         $modifylink = '<a href="' . $root . JRoute::_('index.php?option=' . JEV_COM_COMPONENT . '&task=icalevent.edit&evid=' . $evid . '&rp_id=' . $rp_id . '&Itemid=' . $Itemid . "&year={$year}&month={$month}&day={$day}") . '"><b>' . JText::_('JEV_MODIFY') . '</b></a>' . "\n";
         $viewlink = '<a href="' . $root . JRoute::_('index.php?option=' . JEV_COM_COMPONENT . '&task=icalrepeat.detail&evid=' . $rp_id . '&Itemid=' . $Itemid . "&year={$year}&month={$month}&day={$day}&login=1") . '"><b>' . JText::_('JEV_VIEW') . '</b></a>' . "\n";
         $created_by = $user->name;
         if ($created_by == null) {
             $created_by = "Anonymous";
             if (JRequest::getString("custom_anonusername", "") != "") {
                 $created_by = JRequest::getString("custom_anonusername", "") . " (" . JRequest::getString("custom_anonemail", "") . ")";
             }
         }
         JEV_CommonFunctions::sendAdminMail($sitename, $adminEmail, $subject, $testevent->title(), $testevent->content(), $created_by, JURI::root(), $modifylink, $viewlink);
     }
     if ($success) {
         return $vevent;
     }
     return $success;
 }