comment2cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 4a70d12786f56ffe34680157774ef45bc0576203
Author: dankert <devnull@localhost>
Date:   Sun, 15 Mar 2015 17:18:32 +0100

Intitiales Checkin mit funktionsfähiger Basis.

Diffstat:
.project | 22++++++++++++++++++++++
cms/OpenRat.class.php | 204+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cms/openrat/OpenratClient.php | 123+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
comment.php | 135+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
profiles/config-myproject.ini | 36++++++++++++++++++++++++++++++++++++
source/Mailbox.class.php | 107+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 627 insertions(+), 0 deletions(-)

diff --git a/.project b/.project @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>comment2cms</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.wst.validation.validationbuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.dltk.core.scriptbuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.php.core.PHPNature</nature> + </natures> +</projectDescription> diff --git a/cms/OpenRat.class.php b/cms/OpenRat.class.php @@ -0,0 +1,203 @@ +<?php + + +class OpenRat { + + public $text = ''; + public $subject; + public $pageId; + public $name; + public $debug = true; + public $timestamp; + public $config; + + private $client; + + private function request( $method,$parameter ) + { + + $this->client->parameter = $parameter; + $this->client->method = $method; + $this->client->request(); + + if ( $this->client->status != '200' || $this->debug) + { + echo '<span style="background-color:'.($this->client->status=='200'?'green':'red').'">HTTP-Status '.$this->client->status.'</span>'; + echo "<h4>".$parameter['action'].'/'.$parameter['subaction'].'</h4>'; + ?><pre><?php print_r(""); ?></pre><pre><?php print_r($this->client->response); ?></pre><?php + } + + $response = json_decode($this->client->response,true); + + if ( $response == null ) + { + echo '<span style="background-color:red">Kein JSON: <pre>'.htmlentities($this->client->response).'</pre></span>'; + exit; + } + + return $response; + } + + + public function push() + { + $filesToPublish = array(); + require_once('./cms/openrat/OpenratClient.php'); + $this->client = new OpenratClient(); + + $this->client->host = $this->config['host']; + $this->client->port = $this->config['port']; + $this->client->path = $this->config['path']; + $this->client->type ="application/json"; + + + $response = $this->request( 'GET', + array('action' =>'login', + 'subaction'=>'login') ); + + $token = $response['session']['token']; + $this->client->cookie =$response['session']['name'].'='.$response['session']['id']; + + + $response = $this->request( 'POST', array( + 'action' => 'login', + 'subaction' => 'login', + 'token' => $token, + 'dbid' => $this->config['database'], + 'login_name' => $this->config['user' ], + 'login_password'=> $this->config['password'] ) ); + + $this->client->cookie =$response['session']['name'].'='.$response['session']['id']; + $token = $response['session']['token']; + + + // Projekt auswählen + $response = $this->request( 'POST', array( + 'action' => 'start', + 'subaction' => 'projectmenu', + 'token' => $token, + 'id' => $this->config['projectid']) ); + + // Seite laden. + $responsePage = $this->request( 'GET', array + ( + 'action' => 'page', + 'subaction' => 'info', + 'id' => $this->pageId, + 'token' => $token + ) ); + + // Inhalt laden und nachschauen, ob es schon einen Kommentare-Ordner gibt. + $responseLink = $this->request( 'GET', array + ( + 'action' => 'pageelement', + 'subaction' => 'edit', + 'id' => $this->pageId.'_'.$this->config['page_elementid_comments'], + 'token' => $token + ) ); + + $commentFolderId = $responseLink['output']['linkobjectid']; + + if ( empty($commentFolderId)) { + + // Der Kommentarordner existiert noch nicht, also müssen wir diesen anlegen. + + // Wo kommen die Kommentarordner rein? + // Kann konfiguriert werden. Falls nicht, dann in den Ordner, der die Seite enthält. + $commentContainerFolderId = intval($this->config['comment_folder_id']); + if ( $commentContainerFolderId == 0 ) + $commentContainerFolderId = $responsePage['output']['parentid']; + + $responseCreate = $this->request( 'POST', array + ( + 'action' => 'folder', + 'subaction' => 'createfolder', + 'id' => $commentContainerFolderId, + 'token' => $token, + 'name' => 'comment-'.$this->pageId + ) ); + $commentFolderId = $responseCreate['output']['objectid']; + + // Ordner für die Kommentare in der Seite speichern + $response = $this->request( 'POST', array + ( + 'action' => 'pageelement', + 'subaction' => 'edit', + 'id' => $this->pageId, + 'elementid' => $this->config['page_elementid_comments'], + 'token' => $token, + 'release' => '1', + 'linkobjectid' => $commentFolderId + ) ); + + } + + + // Seite für den Kommentar anlegen. + $response = $this->request( 'POST', array + ( + 'action' => 'folder', + 'subaction' => 'createpage', + 'id' => $commentFolderId, + 'templateid' => $this->config['comment_templateid'], + 'token' => $token, + 'name' => 'Kommentar: '.$this->subject, + 'filename' => 'comment-'.$this->subject, + ) ); + + $commentpageobjectid = $response['output']['objectid']; + + // Text speichern anlegen. + $response = $this->request( 'POST', array + ( + 'action' => 'pageelement', + 'subaction' => 'edit', + 'id' => $commentpageobjectid, + 'elementid' => $this->config['elementid_text'], + 'token' => $token, + 'release' => '1', + 'text' => $this->text + ) ); + + // Betreff speichern anlegen. + $response = $this->request( 'POST', array + ( + 'action' => 'pageelement', + 'subaction' => 'edit', + 'id' => $commentpageobjectid, + 'elementid' => $this->config['elementid_subject'], + 'token' => $token, + 'release' => '1', + 'text' => $this->subject + ) ); + + // Name speichern anlegen. + $response = $this->request( 'POST', array + ( + 'action' => 'pageelement', + 'subaction' => 'edit', + 'id' => $commentpageobjectid, + 'elementid' => $this->config['elementid_name'], + 'token' => $token, + 'release' => '1', + 'text' => $this->name + ) ); + + + // Veröffentlichen der Seiten, welche jetzt den Kommentar enthält. + $response = $this->request( 'POST', array + ( + 'action' => 'page', + 'subaction' => 'pub', + 'id' => $this->pageId, + 'token' => $token + ) ); + + + mail('jan.2015@jandankert.de','Neuer Kommentar von '.$this->name.': '.$this->subject,'Text:\n\n'.$this->text); + } + + +} + +?>+ \ No newline at end of file diff --git a/cms/openrat/OpenratClient.php b/cms/openrat/OpenratClient.php @@ -0,0 +1,122 @@ +<?php + +class OpenratClient +{ + public $error = ''; + public $status = ''; + + public $host; + public $port; + public $path; + public $method = 'GET'; + public $response; + public $parameter = array(); + public $type = "text/json"; + public $cookie = ""; + + public function request() + { + $errno = 0; + $errstr = ''; + + // Die Funktion fsockopen() erwartet eine Protokollangabe (bei TCP optional, bei SSL notwendig). + if ( $this->port == '443' ) + $prx_proto = 'ssl://'; // SSL + else + $prx_proto = 'tcp://'; // Default + + $fp = fsockopen($prx_proto.$this->host,$this->port, $errno, $errstr, 30); + + if ( !$fp || !is_resource($fp) ) + { + $this->error = "Connection refused: '".$prx_proto.$host.':'.$port." - $errstr ($errno)"; + } + else + { + $lb = "\r\n"; + $http_get = $this->path; + + $parameterString = ''; + + foreach( $this->parameter as $pkey=>$pvalue) + { + if ( strlen($parameterString) > 0) + $parameterString .= '&'; + + $parameterString .= urlencode($pkey) . '=' .urlencode($pvalue); + } + //print_r($parameterString); + + if ( $this->method == 'GET') + if ( !empty($parameterString) ) + $http_get .= '?'.$parameterString; + + if ( $this->method == 'POST' ) + { + $header[] = 'Content-Type: application/x-www-form-urlencoded'; + $header[] = 'Content-Length: '.strlen($parameterString); + } + + $header[] = 'Host: '.$this->host; + $header[] = 'Accept: '.$this->type; + $header[] = 'Cookie: '.$this->cookie; + $request_header = array_merge(array( $this->method.' '.$http_get.' HTTP/1.0'),$header); + //print_r($request_header); + $http_request = implode($lb,$request_header).$lb.$lb; + + + if ( $this->method == 'POST' ) + $http_request .= $parameterString; + + //echo "<textarea>".htmlentities($http_request)."</textarea>"; + + if (!is_resource($fp)) { + $error = 'Connection lost after connect: '.$prx_proto.$host.':'.$port; + return false; + } + fputs($fp, $http_request); // Die HTTP-Anfrage zum Server senden. + + // Jetzt erfolgt das Auslesen der HTTP-Antwort. + $isHeader = true; + + // RFC 1945 (Section 6.1) schreibt als Statuszeile folgendes Format vor + // "HTTP/" 1*DIGIT "." 1*DIGIT SP 3DIGIT SP + if (!is_resource($fp)) { + echo 'Connection lost during transfer: '.$host.':'.$port; + } + elseif (!feof($fp)) { + $line = fgets($fp,1028); + $this->status = substr($line,9,3); + } + else + { + echo 'Unexpected EOF while reading HTTP-Response'; + } + + $body = ''; + + while (!feof($fp)) { + $line = fgets($fp,1028); + if ( $isHeader && trim($line)=='' ) // Leerzeile nach Header. + { + $isHeader = false; + } + elseif( $isHeader ) + { + list($headerName,$headerValue) = explode(': ',$line) + array(1=>''); + //if ( $headerName == 'Set-Cookie' ) + // $this->cookie = $headerValue; + $responseHeader[$headerName] = trim($headerValue); + } + else + { + $body .= $line; + } + } + fclose($fp); // Verbindung brav schliessen. + $this->response = $body; + } + } +} + +?>+ \ No newline at end of file diff --git a/comment.php b/comment.php @@ -0,0 +1,134 @@ +<?php + +header('Content-Type: text/html; charset=utf-8'); + +?> + +<form method="post" action=""> + <input type="submit" value="Neue Kommentare verarbeiten abrufen"> +</form> + +<?php + +if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) +{ + + if ($dh = opendir('./profiles')) + { + while (($file = readdir($dh)) !== false) + { + if ( substr($file,-4) == '.ini' ) + { + $config = parse_ini_file('./profiles/'.$file,true); + + if ( !$config['enabled'] ) + continue; + + $blogger = new Blogger(); + if ( $config['debug'] ) echo "<h1>Profile: $file</h1>"; + + $blogger->config = $config; + $blogger->debug = $config['debug']; + + echo "<h2>Step 1: Pulling</h2>"; + $blogger->pull(); + flush(); + echo "<h2>Step 2: CMS</h2>"; + $blogger->pushToCMS(); + flush(); + } + } + closedir($dh); + } +} + + +class Blogger { + + public $debug = true; + public $config; + + private $blogs = array(); + + public function pull() + { + if ($dh = opendir('./source')) + { + while (($file = readdir($dh)) !== false) + { + if ( substr($file,-4) == '.php' ) + { + require_once('./source/'.$file); + $className = substr($file,0,strlen($file)-10); + + if ( $this->debug ) + echo "<h3>Source-Plugin: ".$className.'</h3>'; + + if ( isset($this->config[strtolower($className)] )) + { + $source = new $className; + + $source->config = $this->config[strtolower($className)]; + $source->debug = $this->debug; + + foreach( $source->pull() as $blog ) + { + $this->blogs[] = $blog; + } + } + } + } + closedir($dh); + + if ( $this->debug ) + { + echo "<h3>Blogs</h3>"; + echo '<pre>'; + print_r($this->blogs); + echo '</pre>'; + } + } + + } + + public function pushToCMS() + { + if ($dh = opendir('./cms')) + { + while (($file = readdir($dh)) !== false) + { + if ( substr($file,-4) == '.php' ) + { + require_once('./cms/'.$file); + $className = substr($file,0,strlen($file)-10); + + if ( $this->debug ) + echo "<h3>CMS-Plugin: ".$className.'</h3>'; + + $cms = new $className; + + if ( isset($this->config[strtolower($className)] )) + { + $cms->config = $this->config[strtolower($className)]; + + foreach( $this->blogs as $blog ) + { + + $cms->text = $blog['text' ]; + $cms->subject = $blog['subject' ]; + $cms->name = $blog['name' ]; + $cms->pageId = $blog['pageid' ]; + $cms->timestamp = $blog['timestamp']; + $cms->debug = $this->debug; + $cms->push(); + } + } + } + } + closedir($dh); + } + + } +} + +?>+ \ No newline at end of file diff --git a/profiles/config-myproject.ini b/profiles/config-myproject.ini @@ -0,0 +1,36 @@ + +enabled=false +debug=false + +[mailbox] + +host = {hostname:143/novalidate-cert}INBOX +username= +password= +archive_folder = Archiv +dry=true + + +[openrat] + +user= +password= + +host="duese" +port="80" +path="/~dankert/cms/cms09/dispatcher.php" +database="pg_prod" + +projectid=33 +languageid=45 +rootfolderid=6182 + +templateid=68 + +elementid_text=277 +elementid_attachment_folder=289 +elementid_date=287 + +keywords_folderid=6200 +publish=6183 + diff --git a/source/Mailbox.class.php b/source/Mailbox.class.php @@ -0,0 +1,106 @@ +<?php + +class Mailbox +{ + public $debug = false; + public $config = array(); + + private $text; + private $html; + + public function pull() + { + + $entrys = array(); + + /* connect to gmail */ + $hostname = $this->config['host']; + $username = $this->config['username']; + $password = $this->config['password']; + + /* try to connect */ + $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to IMAP server: ' . imap_last_error()); + + /* grab emails */ + $emails = imap_search($inbox,'ALL UNDELETED UNSEEN'); + + print_r($emails); + + /* if emails are returned, cycle through each... */ + if($emails) { + + /* put the newest emails on top */ + rsort($emails); + + /* for every email... */ + foreach($emails as $email_number) { + + /* get information specific to this email */ + // $overview = imap_fetch_overview($inbox,$email_number,0); + $headers = imap_headerinfo($inbox,$email_number); + $structure = imap_fetchstructure($inbox,$email_number); + // $message = imap_fetchbody($inbox,$email_number); + + // echo "\nOverview:"; + // print_r($overview); + if ( $this->debug ) { echo '<pre>'; print_r($headers); echo '</pre>'; } + + // Initalize + $this->filenames = array(); + $this->text = ''; + $this->html = ''; + $subject = iconv_mime_decode($headers->subject,0,'UTF-8'); + + $s = imap_fetchstructure($inbox,$email_number); + + // Hier gibt es keinen MIME-Messages, es reicht also der Body. + $this->text = imap_body($inbox,$email_number); + + if ( $this->debug ) echo "\n\nBetreff: ".$subject; + if ( $this->debug ) echo "\n\nText: "; + if ( $this->debug ) print_r($this->text); + + + $header_string = imap_fetchheader($inbox, $email_number); + preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m',$header_string, $matches); + $allHeaders = array_combine($matches[1], $matches[2]); + + if ( $this->debug ) echo "\n\nAlle Mailheader: "; + if ( $this->debug ) echo "<pre>$header_string</pre>"; + if ( $this->debug ) {echo "<pre>"; print_r($allHeaders); echo "</pre>";} + + $entrys[] = array( + 'timestamp' => strtotime($headers->date), + 'subject' => $subject, + 'text' => $this->text, + 'name' => $allHeaders['X-Name'], + 'pageid' => $allHeaders['X-Page-Id'] + ); + + // Aufräumen: + // - Mail als gelesen markieren und in das Archiv verschieben. + if ( $this->config['dry'] ) + ; + else + { + imap_setflag_full($inbox,$email_number,'\\SEEN',0); + + if (isset($this->config['archive_folder'])) + { + imap_mail_move($inbox,$email_number,$this->config['archive_folder']) or die("IMAP: Move did not suceed: "+imap_last_error() ); + + imap_expunge($inbox); + } + } + } + } + + /* close the connection */ + imap_close($inbox); + + return $entrys; + } + +} + +?>+ \ No newline at end of file