GithubHelp home page GithubHelp logo

icalendar's Introduction

Zap Calendar iCalendar Library

(https://github.com/zcontent/icalendar)

The Zap Calendar iCalendar Library is a PHP library for supporting the iCalendar (RFC 5545) standard.

This PHP library is for reading and writing iCalendar formatted feeds and files. Features of the library include:

  • Read AND write support for iCalendar files
  • Object based creation and manipulation of iCalendar files
  • Supports expansion of RRULE to a list of repeating dates
  • Supports adding timezone info to iCalendar file

All iCalendar data is stored in a PHP object tree. This allows any property to be added to the iCalendar feed without requiring specialized library function calls. With power comes responsibility. Missing or invalid properties can cause the resulting iCalendar file to be invalid. Visit iCalendar.org to view valid properties and test your feed using the site's iCalendar validator tool.

Library API documentation can be found at http://icalendar.org/zapcallibdocs

See the examples folder for programs that read and write iCalendar files. At its simpliest, you need to include the library at the top of your program:

require_once($path_to_library . "/zapcallib.php");

Create an ical object using the ZCiCal object:

$icalobj = new ZCiCal();

Add an event object:

$eventobj = new ZCiCalNode("VEVENT", $icalobj->curnode);

Add a start and end date to the event:

// add start date
$eventobj->addNode(new ZCiCalDataNode("DTSTART:" . ZCiCal::fromSqlDateTime("2020-01-01 12:00:00")));

// add end date
$eventobj->addNode(new ZCiCalDataNode("DTEND:" . ZCiCal::fromSqlDateTime("2020-01-01 13:00:00")));

Write the object in iCalendar format using the export() function call:

echo $icalobj->export();

This example will not validate since it is missing some required elements. Look at the simpleevent.php example for the minimum # of elements needed for a validated iCalendar file.

To create a multi-event iCalendar file, simply create multiple event objects. For example:

$icalobj = new ZCiCal();
$eventobj1 = new ZCiCalNode("VEVENT", $icalobj->curnode);
$eventobj1->addNode(new ZCiCalDataNode("SUMMARY:Event 1"));
...
$eventobj2 = new ZCiCalNode("VEVENT", $icalobj->curnode);
$eventobj2->addNode(new ZCiCalDataNode("SUMMARY:Event 2"));
...

To read an existing iCalendar file/feed, create the ZCiCal object with a string representing the contents of the iCalendar file:

$icalobj = new ZCiCal($icalstring);

Large iCalendar files can be read in chunks to reduce the amount of memory needed to hold the iCalendar feed in memory. This example reads 500 events at a time:

$icalobj = null;
$eventcount = 0;
$maxevents = 500;
do
{
	$icalobj = newZCiCal($icalstring, $maxevents, $eventcount);
	...
	$eventcount +=$maxevents;
}
while($icalobj->countEvents() >= $eventcount);

You can read the events from an imported (or created) iCalendar object in this manner:

foreach($icalobj->tree->child as $node)
{
	if($node->getName() == "VEVENT")
	{
		foreach($node->data as $key => $value)
		{
			if($key == "SUMMARY")
			{
				echo "event title: " . $value->getValues() . "\n";
			}
		}
	}
}

Known Limitations

  • Since the library utilizes objects to read and write iCalendar data, the size of the iCalendar data is limited to the amount of available memory on the machine. The ZCiCal() object supports reading a range of events to minimize memory space.
  • The library ignores timezone info when importing files, instead utilizing PHP's timezone library for calculations (timezones are supported when exporting files). Imported timezones need to be aliased to a PHP supported timezone.
  • At this time, the library does not support the "BYSETPOS" option in RRULE items.
  • At this time, the maximum date supported is 2036 to avoid date math issues with 32 bit systems.
  • Repeating events are limited to a maximum of 5,000 dates to avoid memory or infinite loop issues

icalendar's People

Contributors

biswajitdas avatar cogliano avatar russellmann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

icalendar's Issues

Calendar Name

Apologies if this is already implemented, though I cannot find it anywhere...

Is there a way to set a name for the calendar?
NAME:My Calendar Name

Thank you

ZCiCalNode::addNode() incorrectly handles multiple nodes with same name

Consider the following code:

$ical = new ZCiCal();
$vevent = new ZCiCalNode( 'VEVENT', $ical->curnode );
$vevent->addNode( new ZCiCalDataNode( 'RDATE:20180310' ) );
$vevent->addNode( new ZCiCalDataNode( 'RDATE:20180324' ) );
$ical-export();

The above results in output like:

BEGIN:VCALENDAR
PRODID:-//ZContent.net//ZapCalLib 1.0//EN
VERSION:2.0
BEGIN:VEVENT
RDATE:20180324
RDATE:20180324
END:VEVENT
END:VCALENDAR

which is incorrect: notice that both RDATE nodes in the output contain the value of last RDATE node added.

The correct output should be:

BEGIN:VCALENDAR
PRODID:-//ZContent.net//ZapCalLib 1.0//EN
VERSION:2.0
BEGIN:VEVENT
RDATE:20180310
RDATE:20180324
END:VEVENT
END:VCALENDAR

I've got a fix to ZCiCalNode::addNode() that results in the correct output for this particular case. I can submit a PR. However, since I'm really new to writing code to generate iCalendar feeds I'm not sure whether my fix will break other cases, and since you don't have any unit tests it's hard for me to tell.

Should I submit the PR anyway?

Handling multiple events

How do we handle multiple events? The documentation is not clear on that.

By handling multiple events I mean this. Let's say that I have a PHP script which, after being triggered, performs a database query, which returns pairs of dates (from-to). How can I use your PHP library to create a calendar which includes all of these dates?

The dates are not reccuring. Instead they refer to unique events.

Multibyte strings support

Hi,

The printDataLine function defined on line 435 of includes/ical.php uses substr and strlen functions. I noticed that this can cause issues with multibyte characters: in some cases, the substr can split a multibyte character, which can keep the client application from reading the output.
Replacing calls to substr and strlen with mb_substr and mb_strlen respectively would avoid splitting multibyte characters, but lines would then be 75 characters long instead of 75 bytes long, and the iCalendar standard requires lines to be 75 bytes long.

Thank you for your consideration.

Reading from Google Calendar .ics Exception: Call to a member function getParameters() on array

Hi,

This code (as in example folder):

$icalfeed = 'myGoogleCalendarPrivateICSUrl';
$icalfeed = file_get_contents($icalfile);
$icalobj = new ZCiCal($icalfeed);

echo "Number of events found: " . $icalobj->countEvents() . "\n";

$ecount = 0;

// read back icalendar data that was just parsed
if(isset($icalobj->tree->child))
{
	foreach($icalobj->tree->child as $node)
	{
		if($node->getName() == "VEVENT")
		{
			$ecount++;
			echo "<br /><br />Event $ecount:\n";
			foreach($node->data as $key => $value)
			{
				if(is_array($value))
				{
					for($i = 0; $i < count($value); $i++)
					{
						$p = $value[$i]->getParameters();
						echo "  $key: " . $value[$i]->getValues() . "\n";
					}
				}
				else
				{
					echo "  $key: " . $value->getValues() . "\n";
				}
			}
		}
	}
}

Produces this error:

Error Object
(
    [message:protected] => Call to a member function getParameters() on array
    [string:Error:private] => 
    [code:protected] => 0
    [file:protected] => /test_scripts/cal.php
    [line:protected] => 31
    [trace:Error:private] => Array
        (
        )

    [previous:Error:private] => 
)

Please help.

ZCiCal::toUnixDateTime - missing return statement

The ZCiCal::toUnixDateTime() does not return anything.

/**
 * Read date/time in iCal formatted string
 *
 * @param string iCal formated date/time string
 *
 * @return int Unix timestamp
 * @deprecated Use ZDateHelper::toUnixDateTime() instead
 */

function toUnixDateTime($datetime){
	$year = substr($datetime,0,4);
	$month = substr($datetime,4,2);
	$day = substr($datetime,6,2);
	$hour = 0;
	$minute = 0;
	$second = 0;
	if(strlen($datetime) > 8 && $datetime{8} == "T") {
		$hour = substr($datetime,9,2);
		$minute = substr($datetime,11,2);
		$second = substr($datetime,13,2);
	}
	$d1 = mktime($hour, $minute, $second, $month, $day, $year);

}

All-day events

Hi,

is there a possibility to create all-day events?

Greetings

Invalid ics generates uncaught exception

Passing "foobar" as ICS content to the constructor generates an uncaught exception:

Uncaught Error: Call to a member function getName() on null in icalendar/includes/ical.php:582
Stack trace:
#0 import_vacances.php(26): ZCiCal->__construct('foobar\\n')
#1 {main}\n  thrown in icalendar/includes/ical.php on line 582

Regular try…catch didn't work and I had to use set_exception_handler() to catch it… it should really pass it, or test for proper format first.

ZCiCal::fromSqlDateTime deprecated, no replacement

The method ZCiCal::fromSqlDateTime() is deprecated but the suggested replacement method ZDateHelper::fromSqlDateTime() does not exist.

class ZCiCal {
...
/**
 * Format into iCal time format from SQL date or SQL date-time format
 * 
 * @param string $datetime SQL date or SQL date-time string
 *
 * @return string iCal formatted string
 * @deprecated Use ZDateHelper::fromSqlDateTime() instead
 */
static function fromSqlDateTime($datetime = ""){

Handling subnode

Can we create a subnode like this ?

BEGIN:VEVENT
BEGIN:VALARM
END:VALARM
END:VEVENT

I can't find anything about this.

Thank's

does not support multiple ATTENDEE

If i add two ATTENDEE

$ical = new ZCiCal();
$event = new ZCiCalNode("VEVENT", $ical->curnode);
$event->addNode(new ZCiCalDataNode("ATTENDEE;CN=Luke;ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:[email protected]");
$event->addNode(new ZCiCalDataNode("ATTENDEE;CN=Json;ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:[email protected]");

echo $ical->export();

then i get

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//ZContent.net//Zap Calendar 1.0//EN
CALSCALE:GREGORIAN
BEGIN:VEVENT
ATTENDEE;CN=Json;ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:[email protected]
END:VEVENT
END:VCALENDAR

It dose not support multiple ATTENDEE

Composer package

Hi,

I've forked your nice library and converted it to a Composer package so it can easily be included in other Composer projects. Have you maybe considering doing this yourself (if so, we could create a PR to merge changes from my fork https://github.com/liliumdev/icalendar) ? Also, if you have something against me doing this (the package is here https://packagist.org/packages/liliumdev/icalendar ), please let me know so I can take it down. Well, if you do this under your namespace anyway, I am definitely going to delete "my" package, but this is helping me out for now so I can use the autoloader in my projects. Also the author email in the composer.json file is my own at the moment because I wasn't able to find ZContent's email or someone who is indeed the author of this package.

Thanks for the awesome work and please let me know if you're OK with this!

Uncaught Error: Call to a member function getName() on null

Hi, I am getting the following error when trying to read an ical.

Fatal error: Uncaught Error: Call to a member function getName() on null in ..../icalendar-master/includes/ical.php on line 581

Simply copied the code from the documentation.

require_once(WP_PLUGIN_DIR . '/stemon-calendar/icalendar-master/zapcallib.php');
$icalstring = 'https://www.lodgify.com/da268dd0-4648-49dd-8250-ac67a8d5929d.ics';
$icalobj = new ZCiCal($icalstring);

If any help on figuring out why of the error, please.

It works in the validator: https://icalendar.org/validator.html?url=https://www.lodgify.com/da268dd0-4648-49dd-8250-ac67a8d5929d.ics

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.