]> _ Git - tortuga-home.git/commitdiff
.
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 13 Oct 2022 05:58:37 +0000 (07:58 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 13 Oct 2022 05:58:37 +0000 (07:58 +0200)
.docker/phpdocker/php-fpm/Dockerfile
.idea/deployment.xml
.idea/workspace.xml
index.php
lib/caldav-client-v2.php [new file with mode: 0644]
scripts/core [deleted file]
scripts/ical.php [new file with mode: 0644]
scripts/lib/ical.php [new file with mode: 0644]
scripts/lib/lib.php
scripts/lib/redis.php
scripts/lib/switchbot.php

index de2f591f4ddb7ad3ec5f67c88c592b9ad447c9d8..44835485d0b26f0ab10267882d35e687c36b1723 100644 (file)
@@ -2,8 +2,9 @@ FROM phpdockerio/php:7.4-fpm
 WORKDIR "/application"
 
 RUN apt-get update
-RUN apt-get -y --no-install-recommends install php7.4-bz2 php7.4-gd php7.4-igbinary php7.4-imap php7.4-xsl php7.4-curl php7.4-json php7.4-imagick php7.4-intl php7.4-memcached php7.4-mysql php7.4-redis php7.4-soap php7.4-ssh2 php7.4-tidy
+RUN apt-get -y --no-install-recommends install php7.4 php7.4-cli php7.4-bz2 php7.4-gd php7.4-igbinary php7.4-imap php7.4-xsl php7.4-curl php7.4-json php7.4-imagick php7.4-intl php7.4-memcached php7.4-mysql php7.4-redis php7.4-soap php7.4-ssh2 php7.4-tidy
 RUN apt-get -y --no-install-recommends install monit less nano wakeonlan lynx at xplanet ffmpeg wget curl composer adb git
+RUN apt-get -y --no-install-recommends install libawl-php
 RUN apt-get clean;rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
 
 COPY startup /usr/bin/startup
index e9db52ae189338418a1bea117b3e42f10cfe1a7d..62e9429d046c07f04d0df03453483215be26b9b7 100644 (file)
@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
-  <component name="PublishConfigData" autoUpload="On explicit save action" serverName="home.tortuga.enhydra.fr" confirmBeforeUploading="false" autoUploadExternalChanges="true" showAutoUploadSettingsWarning="false">
+  <component name="PublishConfigData" autoUpload="On explicit save action" serverName="home.tortuga.enhydra.fr" confirmBeforeUploading="false" confirmBeforeDeletion="false" autoUploadExternalChanges="true" showAutoUploadSettingsWarning="false">
+    <option name="confirmBeforeDeletion" value="false" />
     <option name="confirmBeforeUploading" value="false" />
     <serverData>
       <paths name="alien.cubedesigners.com">
index fbb1144a34b60bae93a546ad482f1f832dfb9993..8ec9af622f472be64dee656fc567afa7a50da932 100644 (file)
@@ -2,8 +2,17 @@
 <project version="4">
   <component name="ChangeListManager">
     <list default="true" id="352ce63a-b52a-41a2-979b-becda7920939" name="Default" comment=".">
-      <change beforePath="$PROJECT_DIR$/.docker/dockerterminal.bat" beforeDir="false" afterPath="$PROJECT_DIR$/.docker/dockerterminal.bat" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/lib/caldav-client-v2.php" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/scripts/ical.php" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/scripts/lib/ical.php" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/.docker/phpdocker/php-fpm/Dockerfile" beforeDir="false" afterPath="$PROJECT_DIR$/.docker/phpdocker/php-fpm/Dockerfile" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/.idea/deployment.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/deployment.xml" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/index.php" beforeDir="false" afterPath="$PROJECT_DIR$/index.php" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/scripts/core" beforeDir="false" />
+      <change beforePath="$PROJECT_DIR$/scripts/lib/lib.php" beforeDir="false" afterPath="$PROJECT_DIR$/scripts/lib/lib.php" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/scripts/lib/redis.php" beforeDir="false" afterPath="$PROJECT_DIR$/scripts/lib/redis.php" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/scripts/lib/switchbot.php" beforeDir="false" afterPath="$PROJECT_DIR$/scripts/lib/switchbot.php" afterDir="false" />
     </list>
     <option name="SHOW_DIALOG" value="false" />
     <option name="HIGHLIGHT_CONFLICTS" value="true" />
       <recent name="T:\Drive\Works\home\images" />
     </key>
     <key name="MoveFile.RECENT_KEYS">
+      <recent name="D:\Works\tortuga-home\lib" />
       <recent name="C:\Users\vince\Drive\Works\home\.docker\config\php" />
       <recent name="C:\Users\vince\Drive\Works\home\images" />
       <recent name="T:\Drive\Works\home\images" />
       <recent name="T:\Drive\Works\home\images\rooms" />
-      <recent name="T:\Drive\Works\home\js\mmenu" />
     </key>
   </component>
   <component name="RunManager" selected="Shell Script.Docker terminal">
       <workItem from="1664987779048" duration="543000" />
       <workItem from="1664988360545" duration="634000" />
       <workItem from="1665630775990" duration="1219000" />
-      <workItem from="1665632309048" duration="519000" />
-    </task>
-    <task id="LOCAL-00480" summary=".">
-      <created>1620028289049</created>
-      <option name="number" value="00480" />
-      <option name="presentableId" value="LOCAL-00480" />
-      <option name="project" value="LOCAL" />
-      <updated>1620028289049</updated>
+      <workItem from="1665632309048" duration="8175000" />
     </task>
     <task id="LOCAL-00481" summary=".">
       <created>1620207485516</created>
       <option name="project" value="LOCAL" />
       <updated>1665632759492</updated>
     </task>
-    <option name="localTasksCounter" value="529" />
+    <task id="LOCAL-00529" summary=".">
+      <created>1665632976124</created>
+      <option name="number" value="00529" />
+      <option name="presentableId" value="LOCAL-00529" />
+      <option name="project" value="LOCAL" />
+      <updated>1665632976124</updated>
+    </task>
+    <option name="localTasksCounter" value="530" />
     <servers />
   </component>
   <component name="TypeScriptGeneratedFilesManager">
index 3282370cd71639c5bb5b60fdbbfe73ecf7f756ec..2acdefef66aecf2a4fdc490675deb2306e0b10d6 100644 (file)
--- a/index.php
+++ b/index.php
@@ -2,6 +2,7 @@
 ob_start();
 $j = time();
 ini_set('memory_limit', '1G');
+
 define('TIMELIMIT', 10);
 define('DISPLAYINTERFACE', true);
 
diff --git a/lib/caldav-client-v2.php b/lib/caldav-client-v2.php
new file mode 100644 (file)
index 0000000..d280eb0
--- /dev/null
@@ -0,0 +1,1018 @@
+<?php
+/**
+* A Class for connecting to a caldav server
+*
+* @package   awl
+*
+* @subpackage caldav-client-v2
+* @author Andrew McMillan <andrew@mcmillan.net.nz>
+* @copyright Andrew McMillan
+* @license   http://www.gnu.org/licenses/lgpl-3.0.txt  GNU LGPL version 3 or later
+*/
+
+require_once('XMLDocument.php');
+
+/**
+ * A class for holding basic calendar information
+ * @package awl
+ */
+class CalendarInfo {
+  public $url;
+  public $displayname;
+  public $getctag;
+
+  function __construct( $url, $displayname = null, $getctag = null ) {
+    $this->url = $url;
+    $this->displayname = $displayname;
+    $this->getctag = $getctag;
+  }
+
+  function __toString() {
+    return( '(URL: '.$this->url.'   Ctag: '.$this->getctag.'   Displayname: '.$this->displayname .')'. "\n" );
+  }
+}
+
+if(!defined("_FSOCK_TIMEOUT")){
+  define("_FSOCK_TIMEOUT", 10);
+}
+
+/**
+* A class for accessing DAViCal via CalDAV, as a client
+*
+* @package   awl
+*/
+class CalDAVClient {
+  /**
+  * Server, username, password, calendar
+  *
+  * @var string
+  */
+  protected $base_url, $user, $pass, $entry, $protocol, $server, $port;
+
+  /**
+  * The principal-URL we're using
+  */
+  protected $principal_url;
+
+  /**
+  * The calendar-URL we're using
+  */
+  protected $calendar_url;
+
+  /**
+  * The calendar-home-set we're using
+  */
+  protected $calendar_home_set;
+
+  /**
+  * The calendar_urls we have discovered
+  */
+  protected $calendar_urls;
+
+  /**
+  * The useragent which is send to the caldav server
+  *
+  * @var string
+  */
+  public $user_agent = 'DAViCalClient';
+
+  protected $headers = array();
+  protected $body = "";
+  protected $requestMethod = "GET";
+  protected $httpRequest = "";  // for debugging http headers sent
+  protected $xmlRequest = "";   // for debugging xml sent
+  protected $xmlResponse = "";  // xml received
+  protected $httpResponseCode = 0; // http response code
+  protected $httpResponseHeaders = "";
+  protected $httpParsedHeaders;
+  protected $httpResponseBody = "";
+
+  protected $parser; // our XML parser object
+
+  private $debug = false; // Whether we are debugging
+
+  /**
+  * Constructor, initialises the class
+  *
+  * @param string $base_url  The URL for the calendar server
+  * @param string $user      The name of the user logging in
+  * @param string $pass      The password for that user
+  */
+  function __construct( $base_url, $user, $pass ) {
+    $this->user = $user;
+    $this->pass = $pass;
+    $this->headers = array();
+
+    if ( preg_match( '#^(https?)://([a-z0-9.-]+)(:([0-9]+))?(/.*)$#', $base_url, $matches ) ) {
+      $this->server = $matches[2];
+      $this->base_url = $matches[5];
+      if ( $matches[1] == 'https' ) {
+        $this->protocol = 'ssl';
+        $this->port = 443;
+      }
+      else {
+        $this->protocol = 'tcp';
+        $this->port = 80;
+      }
+      if ( $matches[4] != '' ) {
+        $this->port = intval($matches[4]);
+      }
+    }
+    else {
+      trigger_error("Invalid URL: '".$base_url."'", E_USER_ERROR);
+    }
+  }
+
+
+  /**
+   * Call this to enable / disable debugging.  It will return the prior value of the debugging flag.
+   * @param boolean $new_value The new value for debugging.
+   * @return boolean The previous value, in case you want to restore it later.
+   */
+  function SetDebug( $new_value ) {
+    $old_value = $this->debug;
+    if ( $new_value )
+      $this->debug = true;
+    else
+      $this->debug = false;
+    return $old_value;
+  }
+
+
+
+  /**
+  * Adds an If-Match or If-None-Match header
+  *
+  * @param bool $match to Match or Not to Match, that is the question!
+  * @param string $etag The etag to match / not match against.
+  */
+  function SetMatch( $match, $etag = '*' ) {
+    $this->headers['match'] = sprintf( "%s-Match: \"%s\"", ($match ? "If" : "If-None"), trim($etag,'"'));
+  }
+
+  /**
+  * Add a Depth: header.  Valid values are 0, 1 or infinity
+  *
+  * @param int $depth  The depth, default to infinity
+  */
+  function SetDepth( $depth = '0' ) {
+    $this->headers['depth'] = 'Depth: '. ($depth == '1' ? "1" : ($depth == 'infinity' ? $depth : "0") );
+  }
+
+  /**
+  * Add a Depth: header.  Valid values are 1 or infinity
+  *
+  * @param int $depth  The depth, default to infinity
+  */
+  function SetUserAgent( $user_agent = null ) {
+    if ( !isset($user_agent) ) $user_agent = $this->user_agent;
+    $this->user_agent = $user_agent;
+  }
+
+  /**
+  * Add a Content-type: header.
+  *
+  * @param string $type  The content type
+  */
+  function SetContentType( $type ) {
+    $this->headers['content-type'] = "Content-type: $type";
+  }
+
+  /**
+  * Set the calendar_url we will be using for a while.
+  *
+  * @param string $url The calendar_url
+  */
+  function SetCalendar( $url ) {
+    $this->calendar_url = $url;
+  }
+
+  /**
+  * Split response into httpResponse and xmlResponse
+  *
+  * @param string Response from server
+   */
+  function ParseResponse( $response ) {
+    $pos = strpos($response, '<?xml');
+    if ($pos !== false) {
+      $this->xmlResponse = trim(substr($response, $pos));
+      $this->xmlResponse = preg_replace('{>[^>]*$}s', '>',$this->xmlResponse );
+      $parser = xml_parser_create_ns('UTF-8');
+      xml_parser_set_option ( $parser, XML_OPTION_SKIP_WHITE, 1 );
+      xml_parser_set_option ( $parser, XML_OPTION_CASE_FOLDING, 0 );
+
+      if ( xml_parse_into_struct( $parser, $this->xmlResponse, $this->xmlnodes, $this->xmltags ) === 0 ) {
+        printf( "XML parsing error: %s - %s\n", xml_get_error_code($parser), xml_error_string(xml_get_error_code($parser)) );
+//        debug_print_backtrace();
+//        echo "\nNodes array............................................................\n"; print_r( $this->xmlnodes );
+//        echo "\nTags array............................................................\n";  print_r( $this->xmltags );
+        printf( "\nXML Reponse:\n%s\n", $this->xmlResponse );
+      }
+
+      xml_parser_free($parser);
+    }
+  }
+
+  /**
+  * Split httpResponseHeaders into an array of headers
+  *
+  * @return array of arrays of header lines
+   */
+  function ParseResponseHeaders() {
+    if ( empty($this->httpResponseHeaders) ) return array();
+    if ( !isset($this->httpParsedHeaders) ) {
+      $this->httpParsedHeaders = array();
+      $headers = str_replace("\r\n", "\n", $this->httpResponseHeaders);
+      $ar_headers = explode("\n", $headers);
+      $last_header = '';
+      foreach ($ar_headers as $cur_headers) {
+        if( preg_match( '{^\s*\S}', $cur_headers) )  $header_name = $last_header;
+        else if ( preg_match( '{^(\S*):', $cur_headers, $matches) ) {
+          $header_name = $matches[1];
+          $last_header = $header_name;
+          if ( empty($this->httpParsedHeaders[$header_name]) ) $this->httpParsedHeaders[$header_name] = array();
+        }
+        $this->httpParsedHeaders[$header_name][] = $cur_headers;
+      }
+    }
+    return $this->httpParsedHeaders;
+  }
+
+  /**
+   * Output http request headers
+   *
+   * @return HTTP headers
+   */
+  function GetHttpRequest() {
+      return $this->httpRequest;
+  }
+  /**
+   * Output http response headers
+   *
+   * @return HTTP headers
+   */
+  function GetResponseHeaders() {
+      return $this->httpResponseHeaders;
+  }
+  /**
+   * Output http response body
+   *
+   * @return HTTP body
+   */
+  function GetResponseBody() {
+      return $this->httpResponseBody;
+  }
+  /**
+   * Output xml request
+   *
+   * @return raw xml
+   */
+  function GetXmlRequest() {
+      return $this->xmlRequest;
+  }
+  /**
+   * Output xml response
+   *
+   * @return raw xml
+   */
+  function GetXmlResponse() {
+      return $this->xmlResponse;
+  }
+
+  /**
+  * Send a request to the server
+  *
+  * @param string $url The URL to make the request to
+  *
+  * @return string The content of the response from the server
+  */
+  function DoRequest( $url = null ) {
+    $headers = array();
+
+    if ( !isset($url) ) $url = $this->base_url;
+    $this->request_url = $url;
+    $url = preg_replace('{^https?://[^/]+}', '', $url);
+    // URLencode if it isn't already
+    if ( preg_match( '{[^%?&=+,.-_/a-z0-9]}', $url ) ) {
+      $url = str_replace(rawurlencode('/'),'/',rawurlencode($url));
+      $url = str_replace(rawurlencode('?'),'?',$url);
+      $url = str_replace(rawurlencode('&'),'&',$url);
+      $url = str_replace(rawurlencode('='),'=',$url);
+      $url = str_replace(rawurlencode('+'),'+',$url);
+      $url = str_replace(rawurlencode(','),',',$url);
+    }
+    $headers[] = $this->requestMethod." ". $url . " HTTP/1.1";
+    $headers[] = "Authorization: Basic ".base64_encode($this->user .":". $this->pass );
+    $headers[] = "Host: ".$this->server .":".$this->port;
+
+    if ( !isset($this->headers['content-type']) ) $this->headers['content-type'] = "Content-type: text/plain";
+    foreach( $this->headers as $ii => $head ) {
+      $headers[] = $head;
+    }
+    $headers[] = "Content-Length: " . strlen($this->body);
+    $headers[] = "User-Agent: " . $this->user_agent;
+    $headers[] = 'Connection: close';
+    $this->httpRequest = join("\r\n",$headers);
+    $this->xmlRequest = $this->body;
+
+    $this->xmlResponse = '';
+
+    $fip = fsockopen( $this->protocol . '://' . $this->server, $this->port, $errno, $errstr, _FSOCK_TIMEOUT); //error handling?
+    if ( !(get_resource_type($fip) == 'stream') ) return false;
+    if ( !fwrite($fip, $this->httpRequest."\r\n\r\n".$this->body) ) { fclose($fip); return false; }
+    $response = "";
+    while( !feof($fip) ) { $response .= fgets($fip,8192); }
+    fclose($fip);
+
+    list( $this->httpResponseHeaders, $this->httpResponseBody ) = preg_split( '{\r?\n\r?\n}s', $response, 2 );
+    if ( preg_match( '{Transfer-Encoding: chunked}i', $this->httpResponseHeaders ) ) $this->Unchunk();
+    if ( preg_match('/HTTP\/\d\.\d (\d{3})/', $this->httpResponseHeaders, $status) )
+      $this->httpResponseCode = intval($status[1]);
+    else
+      $this->httpResponseCode = 0;
+
+    $this->headers = array();  // reset the headers array for our next request
+    $this->ParseResponse($this->httpResponseBody);
+    return $response;
+  }
+
+
+  /**
+  * Unchunk a chunked response
+  */
+  function Unchunk() {
+    $content = '';
+    $chunks = $this->httpResponseBody;
+    // printf( "\n================================\n%s\n================================\n", $chunks );
+    do {
+      $bytes = 0;
+      if ( preg_match('{^((\r\n)?\s*([ 0-9a-fA-F]+)(;[^\n]*)?\r?\n)}', $chunks, $matches ) ) {
+        $octets = $matches[3];
+        $bytes = hexdec($octets);
+        $pos = strlen($matches[1]);
+        // printf( "Chunk size 0x%s (%d)\n", $octets, $bytes );
+        if ( $bytes > 0 ) {
+          // printf( "---------------------------------\n%s\n---------------------------------\n", substr($chunks,$pos,$bytes) );
+          $content .= substr($chunks,$pos,$bytes);
+          $chunks = substr($chunks,$pos + $bytes + 2);
+          // printf( "+++++++++++++++++++++++++++++++++\n%s\n+++++++++++++++++++++++++++++++++\n", $chunks );
+        }
+      }
+      else {
+        $content .= $chunks;
+      }
+    }
+    while( $bytes > 0 );
+    $this->httpResponseBody = $content;
+    // printf( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n%s\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", $content );
+  }
+
+
+  /**
+  * Send an OPTIONS request to the server
+  *
+  * @param string $url The URL to make the request to
+  *
+  * @return array The allowed options
+  */
+  function DoOptionsRequest( $url = null ) {
+    $this->requestMethod = "OPTIONS";
+    $this->body = "";
+    $this->DoRequest($url);
+    $this->ParseResponseHeaders();
+    $allowed = '';
+    foreach( $this->httpParsedHeaders['Allow'] as $allow_header ) {
+      $allowed .= preg_replace( '/^(Allow:)?\s+([a-z, ]+)\r?\n.*/is', '$1,', $allow_header );
+    }
+    $options = array_flip( preg_split( '/[, ]+/', trim($allowed, ', ') ));
+    return $options;
+  }
+
+
+
+  /**
+  * Send an XML request to the server (e.g. PROPFIND, REPORT, MKCALENDAR)
+  *
+  * @param string $method The method (PROPFIND, REPORT, etc) to use with the request
+  * @param string $xml The XML to send along with the request
+  * @param string $url The URL to make the request to
+  *
+  * @return array An array of the allowed methods
+  */
+  function DoXMLRequest( $request_method, $xml, $url = null ) {
+    $this->body = $xml;
+    $this->requestMethod = $request_method;
+    $this->SetContentType("text/xml");
+    return $this->DoRequest($url);
+  }
+
+
+
+  /**
+  * Get a single item from the server.
+  *
+  * @param string $url The URL to GET
+  */
+  function DoGETRequest( $url ) {
+    $this->body = "";
+    $this->requestMethod = "GET";
+    return $this->DoRequest( $url );
+  }
+
+
+  /**
+  * Get the HEAD of a single item from the server.
+  *
+  * @param string $url The URL to HEAD
+  */
+  function DoHEADRequest( $url ) {
+    $this->body = "";
+    $this->requestMethod = "HEAD";
+    return $this->DoRequest( $url );
+  }
+
+
+  /**
+  * PUT a text/icalendar resource, returning the etag
+  *
+  * @param string $url The URL to make the request to
+  * @param string $icalendar The iCalendar resource to send to the server
+  * @param string $etag The etag of an existing resource to be overwritten, or '*' for a new resource.
+  *
+  * @return string The content of the response from the server
+  */
+  function DoPUTRequest( $url, $icalendar, $etag = null ) {
+    $this->body = $icalendar;
+
+    $this->requestMethod = "PUT";
+    if ( $etag != null ) {
+      $this->SetMatch( ($etag != '*'), $etag );
+    }
+    $this->SetContentType('text/calendar; charset="utf-8"');
+    $this->DoRequest($url);
+
+    $etag = null;
+    if ( preg_match( '{^ETag:\s+"([^"]*)"\s*$}im', $this->httpResponseHeaders, $matches ) ) $etag = $matches[1];
+    if ( !isset($etag) || $etag == '' ) {
+      if ( $this->debug ) printf( "No etag in:\n%s\n", $this->httpResponseHeaders );
+      $save_request = $this->httpRequest;
+      $save_response_headers = $this->httpResponseHeaders;
+      $this->DoHEADRequest( $url );
+      if ( preg_match( '{^Etag:\s+"([^"]*)"\s*$}im', $this->httpResponseHeaders, $matches ) ) $etag = $matches[1];
+      if ( !isset($etag) || $etag == '' ) {
+        if ( $this->debug ) printf( "Still No etag in:\n%s\n", $this->httpResponseHeaders );
+      }
+      $this->httpRequest = $save_request;
+      $this->httpResponseHeaders = $save_response_headers;
+    }
+    return $etag;
+  }
+
+
+  /**
+  * DELETE a text/icalendar resource
+  *
+  * @param string $url The URL to make the request to
+  * @param string $etag The etag of an existing resource to be deleted, or '*' for any resource at that URL.
+  *
+  * @return int The HTTP Result Code for the DELETE
+  */
+  function DoDELETERequest( $url, $etag = null ) {
+    $this->body = "";
+
+    $this->requestMethod = "DELETE";
+    if ( $etag != null ) {
+      $this->SetMatch( true, $etag );
+    }
+    $this->DoRequest($url);
+    return $this->httpResponseCode;
+  }
+
+
+  /**
+  * Get a single item from the server.
+  *
+  * @param string $url The URL to PROPFIND on
+  */
+  function DoPROPFINDRequest( $url, $props, $depth = 0 ) {
+    $this->SetDepth($depth);
+    $xml = new XMLDocument( array( 'DAV:' => '', 'urn:ietf:params:xml:ns:caldav' => 'C' ) );
+    $prop = new XMLElement('prop');
+    foreach( $props AS $v ) {
+      $xml->NSElement($prop,$v);
+    }
+
+    $this->body = $xml->Render('propfind',$prop );
+
+    $this->requestMethod = "PROPFIND";
+    $this->SetContentType("text/xml");
+    $this->DoRequest($url);
+    return $this->GetXmlResponse();
+  }
+
+
+  /**
+  * Get/Set the Principal URL
+  *
+  * @param $url string The Principal URL to set
+  */
+  function PrincipalURL( $url = null ) {
+    if ( isset($url) ) {
+      $this->principal_url = $url;
+    }
+    return $this->principal_url;
+  }
+
+
+  /**
+  * Get/Set the calendar-home-set URL
+  *
+  * @param $url array of string The calendar-home-set URLs to set
+  */
+  function CalendarHomeSet( $urls = null ) {
+    if ( isset($urls) ) {
+      if ( ! is_array($urls) ) $urls = array($urls);
+      $this->calendar_home_set = $urls;
+    }
+    return $this->calendar_home_set;
+  }
+
+
+  /**
+  * Get/Set the calendar-home-set URL
+  *
+  * @param $urls array of string The calendar URLs to set
+  */
+  function CalendarUrls( $urls = null ) {
+    if ( isset($urls) ) {
+      if ( ! is_array($urls) ) $urls = array($urls);
+      $this->calendar_urls = $urls;
+    }
+    return $this->calendar_urls;
+  }
+
+
+  /**
+  * Return the first occurrence of an href inside the named tag.
+  *
+  * @param string $tagname The tag name to find the href inside of
+  */
+  function HrefValueInside( $tagname ) {
+    foreach( $this->xmltags[$tagname] AS $k => $v ) {
+      $j = $v + 1;
+      if ( $this->xmlnodes[$j]['tag'] == 'DAV::href' ) {
+        return rawurldecode($this->xmlnodes[$j]['value']);
+      }
+    }
+    return null;
+  }
+
+
+  /**
+  * Return the href containing this property.  Except only if it's inside a status != 200
+  *
+  * @param string $tagname The tag name of the property to find the href for
+  * @param integer $which Which instance of the tag should we use
+  */
+  function HrefForProp( $tagname, $i = 0 ) {
+    if ( isset($this->xmltags[$tagname]) && isset($this->xmltags[$tagname][$i]) ) {
+      $j = $this->xmltags[$tagname][$i];
+      while( $j-- > 0 && $this->xmlnodes[$j]['tag'] != 'DAV::href' ) {
+//        printf( "Node[$j]: %s\n", $this->xmlnodes[$j]['tag']);
+        if ( $this->xmlnodes[$j]['tag'] == 'DAV::status' && $this->xmlnodes[$j]['value'] != 'HTTP/1.1 200 OK' ) return null;
+      }
+//      printf( "Node[$j]: %s\n", $this->xmlnodes[$j]['tag']);
+      if ( $j > 0 && isset($this->xmlnodes[$j]['value']) ) {
+//        printf( "Value[$j]: %s\n", $this->xmlnodes[$j]['value']);
+        return rawurldecode($this->xmlnodes[$j]['value']);
+      }
+    }
+    else {
+      if ( $this->debug ) printf( "xmltags[$tagname] or xmltags[$tagname][$i] is not set\n");
+    }
+    return null;
+  }
+
+
+  /**
+  * Return the href which has a resourcetype of the specified type
+  *
+  * @param string $tagname The tag name of the resourcetype to find the href for
+  * @param integer $which Which instance of the tag should we use
+  */
+  function HrefForResourcetype( $tagname, $i = 0 ) {
+    if ( isset($this->xmltags[$tagname]) && isset($this->xmltags[$tagname][$i]) ) {
+      $j = $this->xmltags[$tagname][$i];
+      while( $j-- > 0 && $this->xmlnodes[$j]['tag'] != 'DAV::resourcetype' );
+      if ( $j > 0 ) {
+        while( $j-- > 0 && $this->xmlnodes[$j]['tag'] != 'DAV::href' );
+        if ( $j > 0 && isset($this->xmlnodes[$j]['value']) ) {
+          return rawurldecode($this->xmlnodes[$j]['value']);
+        }
+      }
+    }
+    return null;
+  }
+
+
+  /**
+  * Return the <prop> ... </prop> of a propstat where the status is OK
+  *
+  * @param string $nodenum The node number in the xmlnodes which is the href
+  */
+  function GetOKProps( $nodenum ) {
+    $props = null;
+    $level = $this->xmlnodes[$nodenum]['level'];
+    $status = '';
+    while ( $this->xmlnodes[++$nodenum]['level'] >= $level ) {
+      if ( $this->xmlnodes[$nodenum]['tag'] == 'DAV::propstat' ) {
+        if ( $this->xmlnodes[$nodenum]['type'] == 'open' ) {
+          $props = array();
+          $status = '';
+        }
+        else {
+          if ( $status == 'HTTP/1.1 200 OK' ) break;
+        }
+      }
+      elseif ( !isset($this->xmlnodes[$nodenum]) || !is_array($this->xmlnodes[$nodenum]) ) {
+        break;
+      }
+      elseif ( $this->xmlnodes[$nodenum]['tag'] == 'DAV::status' ) {
+        $status = $this->xmlnodes[$nodenum]['value'];
+      }
+      else {
+        $props[] = $this->xmlnodes[$nodenum];
+      }
+    }
+    return $props;
+  }
+
+
+  /**
+  * Attack the given URL in an attempt to find a principal URL
+  *
+  * @param string $url The URL to find the principal-URL from
+  */
+  function FindPrincipal( $url=null ) {
+    $xml = $this->DoPROPFINDRequest( $url, array('resourcetype', 'current-user-principal', 'owner', 'principal-URL',
+                                  'urn:ietf:params:xml:ns:caldav:calendar-home-set'), 1);
+
+    $principal_url = $this->HrefForProp('DAV::principal');
+
+    if ( !isset($principal_url) ) {
+      foreach( array('DAV::current-user-principal', 'DAV::principal-URL', 'DAV::owner') AS $href ) {
+        if ( !isset($principal_url) ) {
+          $principal_url = $this->HrefValueInside($href);
+        }
+      }
+    }
+
+    return $this->PrincipalURL($principal_url);
+  }
+
+
+  /**
+  * Attack the given URL in an attempt to find a principal URL
+  *
+  * @param string $url The URL to find the calendar-home-set from
+  */
+  function FindCalendarHome( $recursed=false ) {
+    if ( !isset($this->principal_url) ) {
+      $this->FindPrincipal();
+    }
+    if ( $recursed ) {
+      $this->DoPROPFINDRequest( $this->principal_url, array('urn:ietf:params:xml:ns:caldav:calendar-home-set'), 0);
+    }
+
+    $calendar_home = array();
+    foreach( $this->xmltags['urn:ietf:params:xml:ns:caldav:calendar-home-set'] AS $k => $v ) {
+      if ( $this->xmlnodes[$v]['type'] != 'open' ) continue;
+      while( $this->xmlnodes[++$v]['type'] != 'close' && $this->xmlnodes[$v]['tag'] != 'urn:ietf:params:xml:ns:caldav:calendar-home-set' ) {
+//        printf( "Tag: '%s' = '%s'\n", $this->xmlnodes[$v]['tag'], $this->xmlnodes[$v]['value']);
+        if ( $this->xmlnodes[$v]['tag'] == 'DAV::href' && isset($this->xmlnodes[$v]['value']) )
+          $calendar_home[] = rawurldecode($this->xmlnodes[$v]['value']);
+      }
+    }
+
+    if ( !$recursed && count($calendar_home) < 1 ) {
+      $calendar_home = $this->FindCalendarHome(true);
+    }
+
+    return $this->CalendarHomeSet($calendar_home);
+  }
+
+
+  /**
+  * Find the calendars, from the calendar_home_set
+  */
+  function FindCalendars( $recursed=false ) {
+    if ( !isset($this->calendar_home_set[0]) ) {
+      $this->FindCalendarHome();
+    }
+    $this->DoPROPFINDRequest( $this->calendar_home_set[0], array('resourcetype','displayname','http://calendarserver.org/ns/:getctag'), 1);
+
+    $calendars = array();
+    if ( isset($this->xmltags['urn:ietf:params:xml:ns:caldav:calendar']) ) {
+      $calendar_urls = array();
+      foreach( $this->xmltags['urn:ietf:params:xml:ns:caldav:calendar'] AS $k => $v ) {
+        $calendar_urls[$this->HrefForProp('urn:ietf:params:xml:ns:caldav:calendar', $k)] = 1;
+      }
+
+      foreach( $this->xmltags['DAV::href'] AS $i => $hnode ) {
+        $href = rawurldecode($this->xmlnodes[$hnode]['value']);
+
+        if ( !isset($calendar_urls[$href]) ) continue;
+
+//        printf("Seems '%s' is a calendar.\n", $href );
+
+        $calendar = new CalendarInfo($href);
+        $ok_props = $this->GetOKProps($hnode);
+        foreach( $ok_props AS $v ) {
+//          printf("Looking at: %s[%s]\n", $href, $v['tag'] );
+          switch( $v['tag'] ) {
+            case 'http://calendarserver.org/ns/:getctag':
+              $calendar->getctag = $v['value'];
+              break;
+            case 'DAV::displayname':
+              $calendar->displayname = $v['value'];
+              break;
+          }
+        }
+        $calendars[] = $calendar;
+      }
+    }
+
+    return $this->CalendarUrls($calendars);
+  }
+
+
+  /**
+  * Find the calendars, from the calendar_home_set
+  */
+  function GetCalendarDetails( $url = null ) {
+    if ( isset($url) ) $this->SetCalendar($url);
+
+    $calendar_properties = array( 'resourcetype', 'displayname', 'http://calendarserver.org/ns/:getctag', 'urn:ietf:params:xml:ns:caldav:calendar-timezone', 'supported-report-set' );
+    $this->DoPROPFINDRequest( $this->calendar_url, $calendar_properties, 0);
+
+    $hnode = $this->xmltags['DAV::href'][0];
+    $href = rawurldecode($this->xmlnodes[$hnode]['value']);
+
+    $calendar = new CalendarInfo($href);
+    $ok_props = $this->GetOKProps($hnode);
+    foreach( $ok_props AS $k => $v ) {
+      $name = preg_replace( '{^.*:}', '', $v['tag'] );
+      if ( isset($v['value'] ) ) {
+        $calendar->{$name} = $v['value'];
+      }
+/*      else {
+        printf( "Calendar property '%s' has no text content\n", $v['tag'] );
+      }*/
+    }
+
+    return $calendar;
+  }
+
+
+  /**
+  * Get all etags for a calendar
+  */
+  function GetCollectionETags( $url = null ) {
+    if ( isset($url) ) $this->SetCalendar($url);
+
+    $this->DoPROPFINDRequest( $this->calendar_url, array('getetag'), 1);
+
+    $etags = array();
+    if ( isset($this->xmltags['DAV::getetag']) ) {
+      foreach( $this->xmltags['DAV::getetag'] AS $k => $v ) {
+        $href = $this->HrefForProp('DAV::getetag', $k);
+        if ( isset($href) && isset($this->xmlnodes[$v]['value']) ) $etags[$href] = $this->xmlnodes[$v]['value'];
+      }
+    }
+
+    return $etags;
+  }
+
+
+  /**
+  * Get a bunch of events for a calendar with a calendar-multiget report
+  */
+  function CalendarMultiget( $event_hrefs, $url = null ) {
+
+    if ( isset($url) ) $this->SetCalendar($url);
+
+    $hrefs = '';
+    foreach( $event_hrefs AS $k => $href ) {
+      $href = str_replace( rawurlencode('/'),'/',rawurlencode($href));
+      $hrefs .= '<href>'.$href.'</href>';
+    }
+    $this->body = <<<EOXML
+<?xml version="1.0" encoding="utf-8" ?>
+<C:calendar-multiget xmlns="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
+<prop><getetag/><C:calendar-data/></prop>
+$hrefs
+</C:calendar-multiget>
+EOXML;
+
+    $this->requestMethod = "REPORT";
+    $this->SetContentType("text/xml");
+    $this->DoRequest( $this->calendar_url );
+
+    $events = array();
+    if ( isset($this->xmltags['urn:ietf:params:xml:ns:caldav:calendar-data']) ) {
+      foreach( $this->xmltags['urn:ietf:params:xml:ns:caldav:calendar-data'] AS $k => $v ) {
+        $href = $this->HrefForProp('urn:ietf:params:xml:ns:caldav:calendar-data', $k);
+//        echo "Calendar-data:\n"; print_r($this->xmlnodes[$v]);
+        $events[$href] = $this->xmlnodes[$v]['value'];
+      }
+    }
+    else {
+      foreach( $event_hrefs AS $k => $href ) {
+        $this->DoGETRequest($href);
+        $events[$href] = $this->httpResponseBody;
+      }
+    }
+
+    return $events;
+  }
+
+
+  /**
+  * Given XML for a calendar query, return an array of the events (/todos) in the
+  * response.  Each event in the array will have a 'href', 'etag' and '$response_type'
+  * part, where the 'href' is relative to the calendar and the '$response_type' contains the
+  * definition of the calendar data in iCalendar format.
+  *
+  * @param string $filter XML fragment which is the <filter> element of a calendar-query
+  * @param string $url The URL of the calendar, or empty/null to use the 'current' calendar_url
+  *
+  * @return array An array of the relative URLs, etags, and events from the server.  Each element of the array will
+  *               be an array with 'href', 'etag' and 'data' elements, corresponding to the URL, the server-supplied
+  *               etag (which only varies when the data changes) and the calendar data in iCalendar format.
+  */
+  function DoCalendarQuery( $filter, $url = '' ) {
+
+    if ( !empty($url) ) $this->SetCalendar($url);
+
+    $this->body = <<<EOXML
+<?xml version="1.0" encoding="utf-8" ?>
+<C:calendar-query xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
+  <D:prop>
+    <C:calendar-data/>
+    <D:getetag/>
+  </D:prop>$filter
+</C:calendar-query>
+EOXML;
+
+    $this->requestMethod = "REPORT";
+    $this->SetContentType("text/xml");
+    $this->DoRequest( $this->calendar_url );
+
+    $report = array();
+    foreach( $this->xmlnodes as $k => $v ) {
+      switch( $v['tag'] ) {
+        case 'DAV::response':
+          if ( $v['type'] == 'open' ) {
+            $response = array();
+          }
+          elseif ( $v['type'] == 'close' ) {
+            $report[] = $response;
+          }
+          break;
+        case 'DAV::href':
+          $response['href'] = basename( rawurldecode($v['value']) );
+          break;
+        case 'DAV::getetag':
+          $response['etag'] = preg_replace('/^"?([^"]+)"?/', '$1', $v['value']);
+          break;
+        case 'urn:ietf:params:xml:ns:caldav:calendar-data':
+          $response['data'] = $v['value'];
+          break;
+      }
+    }
+    return $report;
+  }
+
+
+  /**
+  * Get the events in a range from $start to $finish.  The dates should be in the
+  * format yyyymmddThhmmssZ and should be in GMT.  The events are returned as an
+  * array of event arrays.  Each event array will have a 'href', 'etag' and 'event'
+  * part, where the 'href' is relative to the calendar and the event contains the
+  * definition of the event in iCalendar format.
+  *
+  * @param timestamp $start The start time for the period
+  * @param timestamp $finish The finish time for the period
+  * @param string    $relative_url The URL relative to the base_url specified when the calendar was opened.  Default ''.
+  *
+  * @return array An array of the relative URLs, etags, and events, returned from DoCalendarQuery() @see DoCalendarQuery()
+  */
+  function GetEvents( $start = null, $finish = null, $relative_url = '' ) {
+    $filter = "";
+    if ( isset($start) && isset($finish) )
+        $range = "<C:time-range start=\"$start\" end=\"$finish\"/>";
+    else
+        $range = '';
+
+    $filter = <<<EOFILTER
+  <C:filter>
+    <C:comp-filter name="VCALENDAR">
+      <C:comp-filter name="VEVENT">
+        $range
+      </C:comp-filter>
+    </C:comp-filter>
+  </C:filter>
+EOFILTER;
+
+    return $this->DoCalendarQuery($filter, $relative_url);
+  }
+
+
+  /**
+  * Get the todo's in a range from $start to $finish.  The dates should be in the
+  * format yyyymmddThhmmssZ and should be in GMT.  The events are returned as an
+  * array of event arrays.  Each event array will have a 'href', 'etag' and 'event'
+  * part, where the 'href' is relative to the calendar and the event contains the
+  * definition of the event in iCalendar format.
+  *
+  * @param timestamp $start The start time for the period
+  * @param timestamp $finish The finish time for the period
+  * @param boolean   $completed Whether to include completed tasks
+  * @param boolean   $cancelled Whether to include cancelled tasks
+  * @param string    $relative_url The URL relative to the base_url specified when the calendar was opened.  Default ''.
+  *
+  * @return array An array of the relative URLs, etags, and events, returned from DoCalendarQuery() @see DoCalendarQuery()
+  */
+  function GetTodos( $start, $finish, $completed = false, $cancelled = false, $relative_url = "" ) {
+
+    if ( $start && $finish ) {
+$time_range = <<<EOTIME
+                <C:time-range start="$start" end="$finish"/>
+EOTIME;
+    }
+
+    // Warning!  May contain traces of double negatives...
+    $neg_cancelled = ( $cancelled === true ? "no" : "yes" );
+    $neg_completed = ( $cancelled === true ? "no" : "yes" );
+
+    $filter = <<<EOFILTER
+  <C:filter>
+    <C:comp-filter name="VCALENDAR">
+          <C:comp-filter name="VTODO">
+                <C:prop-filter name="STATUS">
+                        <C:text-match negate-condition="$neg_completed">COMPLETED</C:text-match>
+                </C:prop-filter>
+                <C:prop-filter name="STATUS">
+                        <C:text-match negate-condition="$neg_cancelled">CANCELLED</C:text-match>
+                </C:prop-filter>$time_range
+          </C:comp-filter>
+    </C:comp-filter>
+  </C:filter>
+EOFILTER;
+
+    return $this->DoCalendarQuery($filter, $relative_url);
+  }
+
+
+  /**
+  * Get the calendar entry by UID
+  *
+  * @param uid
+  * @param string    $relative_url The URL relative to the base_url specified when the calendar was opened.  Default ''.
+  * @param string    $component_type The component type inside the VCALENDAR.  Default 'VEVENT'.
+  *
+  * @return array An array of the relative URL, etag, and calendar data returned from DoCalendarQuery() @see DoCalendarQuery()
+  */
+  function GetEntryByUid( $uid, $relative_url = '', $component_type = 'VEVENT' ) {
+    $filter = "";
+    if ( $uid ) {
+      $filter = <<<EOFILTER
+  <C:filter>
+    <C:comp-filter name="VCALENDAR">
+          <C:comp-filter name="$component_type">
+                <C:prop-filter name="UID">
+                        <C:text-match icollation="i;octet">$uid</C:text-match>
+                </C:prop-filter>
+          </C:comp-filter>
+    </C:comp-filter>
+  </C:filter>
+EOFILTER;
+    }
+
+    return $this->DoCalendarQuery($filter, $relative_url);
+  }
+
+
+  /**
+  * Get the calendar entry by HREF
+  *
+  * @param string    $href         The href from a call to GetEvents or GetTodos etc.
+  *
+  * @return string The iCalendar of the calendar entry
+  */
+  function GetEntryByHref( $href ) {
+    $href = str_replace( rawurlencode('/'),'/',rawurlencode($href));
+    return $this->DoGETRequest( $href );
+  }
+
+}
diff --git a/scripts/core b/scripts/core
deleted file mode 100644 (file)
index c19fed7..0000000
Binary files a/scripts/core and /dev/null differ
diff --git a/scripts/ical.php b/scripts/ical.php
new file mode 100644 (file)
index 0000000..0b2c1ca
--- /dev/null
@@ -0,0 +1,5 @@
+<?php
+header('Content-type: text/plain');
+require_once "import.php";
+
+isJerome();
\ No newline at end of file
diff --git a/scripts/lib/ical.php b/scripts/lib/ical.php
new file mode 100644 (file)
index 0000000..fda2c00
--- /dev/null
@@ -0,0 +1,82 @@
+<?php
+set_include_path(get_include_path() . ':/usr/share/awl/inc');
+require_once ROOT . '/lib/caldav-client-v2.php';
+
+
+$calendarServer = 'https://nextcloud.home.tortuga.enhydra.fr';
+$calendarBase = '/remote.php/dav/calendars/vincent@enhydra.fr/';
+
+/**
+ * @return CalDAVClient
+ */
+function icalClient()
+{
+    global $calendarServer, $calendarBase;
+    $res = new CalDAVClient($calendarServer . $calendarBase, 'vincent@enhydra.fr', '4zJZ6hbT6xwyL4oE3JEA');
+    //$res->SetDebug(true);
+    $res->SetDepth('1');
+    return $res;
+}
+
+/**
+ * @param $cal string
+ * @param $start DateTime|null
+ * @param $end DateTime|null
+ * @return array
+ */
+function searchIcalEvent($cal, $start = null, $end = null)
+{
+    global $calendarBase;
+
+    $client = icalClient();
+    $client->SetCalendar($calendarBase . '/' . trim($cal, '/') . '/');
+
+    $events = $client->GetEvents(getIcalTimestamp($start), getIcalTimestamp($end));
+    $res = [];
+    foreach ($events as $event) {
+
+        $lines = explode("\n", $event['data']);
+        $data = [];
+        foreach ($lines as $line) {
+            $line = trim($line);
+            if (!$line) {
+                continue;
+            }
+            list($k, $v) = explode(':', $line);
+            $data[$k] = $v;
+        }
+        $event['data'] = $data;
+        $res[] = $event;
+
+    }
+    return $res;
+}
+
+/**
+ * @param $cal string
+ * @param $day DateTime|null
+ * @return array
+ */
+function searchICalEventDay($cal, $day = null)
+{
+    $start = $day;
+    $end = clone $day;
+
+    $start->setTime(0, 0, 0);
+    $end->setTime(23, 59, 59);
+
+    return searchIcalEvent($cal, $start, $end);
+}
+
+/**
+ * @param $date DateTime|null
+ * @return string
+ */
+function getIcalTimestamp($date = null)
+{
+    if (null === $date) {
+        $date = new DateTime('now');
+    }
+    $date->setTimezone(new DateTimeZone('GMT'));
+    return $date->format('Ymd') . 'T' . $date->format('His') . 'Z';
+}
\ No newline at end of file
index 8035451542469515cb79110138a05ee4c57e08b6..40a9f49158dc0c2e0b706a2942bf59ad008829cc 100644 (file)
@@ -5,6 +5,7 @@ $insteonProcessingQueue = false;
 $harmonyClient = null;
 $endQueue = [];
 
+
 require_once ROOT . '/vendor/autoload.php';
 require_once ROOT . '/scripts/lib/profile.php';
 require_once ROOT . "/config/global.php";
@@ -50,6 +51,7 @@ require_once ROOT . '/scripts/lib/climacell.php';
 require_once ROOT . '/scripts/lib/homeconnect.php';
 require_once ROOT . '/scripts/lib/wol.php';
 require_once ROOT . '/scripts/lib/switchbot.php';
+require_once ROOT . '/scripts/lib/ical.php';
 
 
 profile('Loaded libraries', __FILE__, __LINE__);
@@ -313,9 +315,9 @@ function shortcut($s, $hash)
     } elseif ($s['type'] == 'media') {
         $attrs['href'] = isset($s['path']) ? $s['path'] : $s['url'];
         $attrs['class'] = 'media';
-    } elseif ($s['type'] == 'freebox' || $s['type'] === 'shield' || $s['type'] === 'netflix' || $s['type']==='amazonprime' || $s['type']==='nintendoswitch') {
+    } elseif ($s['type'] == 'freebox' || $s['type'] === 'shield' || $s['type'] === 'netflix' || $s['type'] === 'amazonprime' || $s['type'] === 'nintendoswitch') {
         $attrs['href'] = $s['url'];
-        $attrs['class'] = 'ajax play '.$s['type'];
+        $attrs['class'] = 'ajax play ' . $s['type'];
         $attrs['data-remote'] = "1";
     } else if ($s['type'] == 'map') {
         $rand = rand(1, 1000000);
index ccbe566194674b6bf57411bb070ba5c8c29f7a13..bb0406fd7fe590afc0b63bdf391fed157127e163 100644 (file)
@@ -3,7 +3,6 @@
 use Predis\Client;
 
 
-
 class StringSetIb
     extends Predis\Command\StringSet
 {
@@ -107,3 +106,21 @@ function redisEventListener($channel, $callback)
     }
 }
 
+/**
+ * @param $key string
+ * @param $ttl int
+ * @param $closure Closure
+ * @return mixed
+ */
+function remember($key, $ttl, $closure)
+{
+    $client = getRedisClient();
+    if (!$client->exists($key)) {
+        $data = call_user_func($closure);
+        $client->set($key, $data);
+        $client->expire($key, $ttl);
+    } else {
+        $data = $client->get($key);
+    }
+    return $data;
+}
index 2e28b2855a88f8482ea8d7adc7d4acaebda41ded..f5f5f5f90eaa71156820a2a63f2ae1a16f63cb90 100644 (file)
@@ -34,15 +34,21 @@ function hotwaterCheckMode()
         $isWeek = $d < 6;
 
         $ecomode = (int)getState('ecomode', '0');
-        if ($ecomode == 0) {
-            $hot = ($isWeek && $h > 8 && $h < 21) ? '0' : '1';
-        } else if ($ecomode == 1) {
-            $hot = ($isWeek && $h >= 6 && $h <= 7) ? '1' : '0';
-        } else if ($ecomode == 2) {
+
+        if (isJerome()) {
+            $weekColdHours = [0, 1, 2, 3, 4, 5, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 23];
+            if ($ecomode == 0) {
+                $hot = ($isWeek && in_array($h, $weekColdHours)) ? '0' : '1';
+            } else if ($ecomode == 1) {
+                $hot = ($isWeek && $h >= 6 && $h <= 7) ? '1' : '0';
+            } else if ($ecomode == 2) {
+                $hot = '0';
+            }
+        } else {
             $hot = '0';
         }
 
-        echo "\n\n".'H:' . $h . '/D:' . $d . '/W:' . $hot."\n\n";
+        echo "\n\n" . 'J:' . isJerome() . 'H:' . $h . '/D:' . $d . '/W:' . $hot . "\n\n";
     }
     hotwater($hot);
 }
@@ -81,4 +87,42 @@ function hotwater($newState)
     setState('hotwater', $newState);
     setState('hotwater_change', '0');
     return $res;
+}
+
+function isVincent()
+{
+
+}
+
+function isJerome()
+{
+    return remember('jerome_in_paris', 3600, function () {
+        return _isJerome();
+    });
+}
+
+function _isJerome()
+{
+    $today = new DateTime('now');
+    $yesterday = new DateTime('now');
+    $yesterday->sub(new DateInterval('P1D'));
+    $tomorrow = new DateTime('now');
+    $tomorrow->add(new DateInterval('P1D'));
+
+    $dates = [$yesterday, $today, $tomorrow];
+
+    foreach ($dates as $date) {
+        $events = searchIcalEventDay('dplacements-jrme', $today);
+        $count = 0;
+        foreach ($events as $event) {
+            if (stristr($event['data']['SUMMARY'], '(V)')) {
+                continue;
+            }
+            $count++;
+        }
+        if (!$count) {
+            return true;
+        }
+    }
+    return false;
 }
\ No newline at end of file