Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repaired a couple of notice level errors #12

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
21 changes: 20 additions & 1 deletion blocks/SG_iCal_VCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ public function getDescription() {
return null;
}
}


/**
* Returns the timezone of the calendar
* known, NULL will be returned.
* @return string
*
* @since 9.13.13
* @author Mat Lipe
*/
public function getTimezone() {
if( isset($this->data['x-wr-timezone']) ) {
return $this->data['x-wr-timezone'];
} else {
return null;
}
}



/**
* @see IteratorAggregate.getIterator()
Expand All @@ -53,4 +72,4 @@ public function getIterator() {
}
}

?>
?>
2 changes: 1 addition & 1 deletion blocks/SG_iCal_VTimeZone.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @license http://creativecommons.org/licenses/by-sa/2.5/dk/deed.en_GB CC-BY-SA-DK
*/
class SG_iCal_VTimeZone {
protected $tzid;
public $tzid;
protected $daylight;
protected $standard;
protected $cache = array();
Expand Down
73 changes: 61 additions & 12 deletions helpers/SG_iCal_Freq.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
*
* @package SG_iCalReader
* @author Morten Fangel (C) 2008
*
* @license http://creativecommons.org/licenses/by-sa/2.5/dk/deed.en_GB CC-BY-SA-DK
*/
class SG_iCal_Freq {
public $debug = false;


protected $weekdays = array('MO'=>'monday', 'TU'=>'tuesday', 'WE'=>'wednesday', 'TH'=>'thursday', 'FR'=>'friday', 'SA'=>'saturday', 'SU'=>'sunday');
protected $knownRules = array('month', 'weekno', 'day', 'monthday', 'yearday', 'hour', 'minute'); //others : 'setpos', 'second'
protected $ruleModifiers = array('wkst');
Expand All @@ -32,7 +36,7 @@ class SG_iCal_Freq {
protected $start = 0;
protected $freq = '';

protected $excluded; //EXDATE
protected $excluded = array(); //EXDATE
protected $added; //RDATE

protected $cache; // getAllOccurrences()
Expand All @@ -46,7 +50,6 @@ class SG_iCal_Freq {
*/
public function __construct( $rule, $start, $excluded=array(), $added=array()) {
$this->start = $start;
$this->excluded = array();

$rules = array();
foreach( explode(';', $rule) AS $v) {
Expand All @@ -57,6 +60,7 @@ public function __construct( $rule, $start, $excluded=array(), $added=array()) {
if( isset($this->rules['until']) && is_string($this->rules['until']) ) {
$this->rules['until'] = strtotime($this->rules['until']);
}

$this->freq = strtolower($this->rules['freq']);

foreach( $this->knownRules AS $rule ) {
Expand Down Expand Up @@ -98,7 +102,12 @@ public function __construct( $rule, $start, $excluded=array(), $added=array()) {
$this->cache = array_values($cache);
}

$this->excluded = $excluded;
if( !is_array( $excluded ) ){
$this->excluded = array();
} else {
$this->excluded = $excluded;
}

$this->added = $added;
}

Expand All @@ -107,20 +116,24 @@ public function __construct( $rule, $start, $excluded=array(), $added=array()) {
* Returns all timestamps array(), build the cache if not made before
* @return array
*/
public function getAllOccurrences() {
public function getAllOccurrences($limit = false) {
if (empty($this->cache)) {
//build cache
$next = $this->firstOccurrence();
$count = 1;
while ($next) {
$cache[] = $next;
$next = $this->findNext($next);
$count++;
if( $limit && $limit == $count ) break;
}
if (!empty($this->added)) {
$cache = $cache + $this->added;
asort($cache);
}
$this->cache = $cache;
}

return $this->cache;
}

Expand Down Expand Up @@ -183,6 +196,11 @@ public function lastOccurrence() {
//return last timestamp in cache
return end($this->cache);
}


function debug( $bool = true ){
$this->debug = $bool;
}

/**
* Calculates the next time after the given offset that the rule
Expand Down Expand Up @@ -215,7 +233,7 @@ public function findNext($offset) {
}
}

$debug = false;
$debug = $this->debug;

//make sure the offset is valid
if( $offset === false || (isset($this->rules['until']) && $offset > $this->rules['until']) ) {
Expand All @@ -228,11 +246,18 @@ public function findNext($offset) {
//set the timestamp of the offset (ignoring hours and minutes unless we want them to be
//part of the calculations.
if($debug) echo 'O: ' . date('r', $offset) . "\n";


$hour = (in_array($this->freq, array('hourly','minutely')) && $offset > $this->start) ? date('H', $offset) : date('H', $this->start);


$minute = (($this->freq == 'minutely' || isset($this->rules['byminute'])) && $offset > $this->start) ? date('i', $offset) : date('i', $this->start);

$t = mktime($hour, $minute, date('s', $this->start), date('m', $offset), date('d', $offset), date('Y',$offset));
if($debug) echo 'START: ' . date('r', $t) . "\n";



if( $this->simpleMode ) {
if( $offset < $t ) {
$ts = $t;
Expand All @@ -249,13 +274,17 @@ public function findNext($offset) {

$eop = $this->findEndOfPeriod($offset);
if($debug) echo 'EOP: ' . date('r', $eop) . "\n";




foreach( $this->knownRules AS $rule ) {
foreach( $this->knownRules AS $rule ) {
if( $found && isset($this->rules['by' . $rule]) ) {
if( $this->isPrerule($rule, $this->freq) ) {
$subrules = explode(',', $this->rules['by' . $rule]);
$_t = null;
foreach( $subrules AS $subrule ) {
foreach( $subrules AS $subrule) {

$imm = call_user_func_array(array($this, 'ruleBy' . $rule), array($subrule, $t));
if( $imm === false ) {
break;
Expand Down Expand Up @@ -288,6 +317,7 @@ public function findNext($offset) {
if($debug) echo 'Not found' . "\n";
$ts = $this->findNext( $this->findStartingPoint( $offset, $this->rules['interval'] ) );
}

if ($ts && in_array($ts, $this->excluded))
return $this->findNext($ts);

Expand All @@ -304,20 +334,26 @@ public function findNext($offset) {
*/
private function findStartingPoint( $offset, $interval, $truncate = true ) {
$_freq = ($this->freq == 'daily') ? 'day__' : $this->freq;

$t = '+' . $interval . ' ' . substr($_freq,0,-2) . 's';
if( $_freq == 'monthly' && $truncate ) {

if( $interval > 1) {
$offset = strtotime('+' . ($interval - 1) . ' months ', $offset);
}
$t = '+' . (date('t', $offset) - date('d', $offset) + 1) . ' days';

}

$sp = strtotime($t, $offset);



if( $truncate ) {

$sp = $this->truncateToPeriod($sp, $this->freq);
}

return $sp;
}

Expand Down Expand Up @@ -373,6 +409,8 @@ private function truncateToPeriod( $time, $freq ) {
* @param string $rule
* @param int $t
* @return int
*
* @since 10.14.13
*/
private function ruleByday($rule, $t) {
$dir = ($rule{0} == '-') ? -1 : 1;
Expand All @@ -390,9 +428,19 @@ private function ruleByday($rule, $t) {
$_t = strtotime($s, $t);

if( $_t == $t && in_array($this->freq, array('monthly', 'yearly')) ) {
// Yes. This is not a great idea.. but hey, it works.. for now
$s = 'next ' . $d . ' ' . date('H:i:s',$t);
$_t = strtotime($s, $_t);

if( $this->freq == 'monthly' ){
while( date( 'm', $_t) == date('m', $t ) ){
$_t = strtotime($s, $_t);
}
} elseif( $this->freq == 'yearly' ){
while( date( 'y', $_t) == date('y', $t ) ){
$_t = strtotime($s, $_t);
}

}
}

return $_t;
Expand Down Expand Up @@ -484,9 +532,10 @@ private function validDate( $t ) {
return false;
}

if (in_array($t, $this->excluded)) {
return false;
}
if (in_array($t, $this->excluded)) {
return false;
}


if( isset($this->rules['bymonth']) ) {
$months = explode(',', $this->rules['bymonth']);
Expand Down
8 changes: 6 additions & 2 deletions helpers/SG_iCal_Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ protected static function Fetch( $resource ) {
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $resource);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
if( !ini_get('safe_mode') && !ini_get('open_basedir') ){
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
}
$content = curl_exec($c);

$ct = curl_getinfo($c, CURLINFO_CONTENT_TYPE);
$enc = preg_replace('/^.*charset=([-a-zA-Z0-9]+).*$/', '$1', $ct);
if( $ct != '' && strtolower(str_replace('-','', $enc)) != 'utf8' ) {
Expand Down Expand Up @@ -82,6 +82,7 @@ protected static function UnfoldLines($content) {
}
$data[] = $line;
}

return $data;
}

Expand All @@ -93,7 +94,7 @@ protected static function UnfoldLines($content) {
*/
private static function _Parse( $content, SG_iCal $ical ) {
$main_sections = array('vevent', 'vjournal', 'vtodo', 'vtimezone', 'vcalendar');
$array_idents = array('exdate','rdate');
$array_idents = array('exdate','rdate','attach');
$sections = array();
$section = '';
$current_data = array();
Expand All @@ -120,6 +121,7 @@ private static function _Parse( $content, SG_iCal $ical ) {
if( array_search($s, $sections) !== false ) {
// This section is in the main section
if( $section == $s ) {

// It _is_ the main section else
if (in_array($line->getIdent(), $array_idents))
//exdate could appears more that once
Expand All @@ -136,6 +138,8 @@ private static function _Parse( $content, SG_iCal $ical ) {
}
}
}


$current_data = array();
}

Expand Down