miniblog

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

commit 277df22276278c469a4d3a1072453848fd29a1e0
Author: Jan Dankert <devnull@localhost>
Date:   Mon, 12 Dec 2011 00:38:22 +0100

Initiale Version mit Login am konfigurierbaren Server.

Diffstat:
.project | 22++++++++++++++++++++++
client/OpenratClient.php | 123+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
config.ini | 11+++++++++++
index.php | 105+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 261 insertions(+), 0 deletions(-)

diff --git a/.project b/.project @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>BlogForm</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/client/OpenratClient.php b/client/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/config.ini b/config.ini @@ -0,0 +1,11 @@ + +[server] + +host="localhost" +port="80" +path="/~dankert/cms-test/09/dispatcher.php" +database="pg_test" + +[project] +rootfolderid=4 +urlschema=daily diff --git a/index.php b/index.php @@ -0,0 +1,105 @@ +<?php + +$config = parse_ini_file('./config.ini',true); + +require('./client/OpenratClient.php'); +$client = new OpenratClient(); + +if (!isset($_SERVER['PHP_AUTH_USER'])) { + header('WWW-Authenticate: Basic realm="Blog Upload"'); + header('HTTP/1.0 401 Unauthorized'); + echo 'sorry, authentication required to blog something'; + exit; +} + +$username = $_SERVER['PHP_AUTH_USER']; +$password = $_SERVER['PHP_AUTH_PW']; + +?> + +<html> +<head> +<title>Blog Form</title> +</head> +<body> + +<?php if ( !empty($_POST['text']) ) { + + ?><h1>Result</h1><?php + + $client->host = $config['server']['host']; + $client->port = $config['server']['port']; + $client->path = $config['server']['path']; + $client->type ="application/json"; + + $client->parameter = array(); + $client->parameter['action'] = 'login'; + $client->parameter['subaction'] = 'login'; + + $client->method = 'GET'; + $client->request(); + + + if ( $client->status != '200') + { + echo '<span style="background-color:'.($client->status=='200'?'green':'red').'">HTTP-Status '.$client->status.'</span>'; + } + + $response = json_decode($client->response,true); + $token = $response['session']['token']; + $client->cookie =$response['session']['name'].'='.$response['session']['id']; + ?><pre><?php print_r($client) ?></pre><hr /><?php + + $client->parameter = array(); + $client->parameter['action'] = 'login'; + $client->parameter['subaction'] = 'login'; + + $client->method = 'POST'; + $client->parameter['token' ] = $token; + $client->parameter['dbid' ] = $config['server']['database']; + $client->parameter['login_name' ] = $username; + $client->parameter['login_password'] = $password; + + $client->request(); + $response = json_decode($client->response,true); + ?><pre><?php print_r($client); ?></pre><?php + + ?> + + + <?php } ?> + +<h1>Blog</h1> +<form action="<?php echo $_SERVER['SCRIPT_NAME'] ?>" method="post"> + +<div class="line"> +<div class="label">Benutzer</div> +<div class="value"><input type="text" name="username" readonly="readonly" + value="<?php echo $username ?>" /></div> +</div> +<div class="line"> +<div class="label">Titel</div> +<div class="value"><input type="text" name="title" value="" /></div> +</div> +<div class="line"> +<div class="label">Text</div> +<div class="value"><textarea name="text"></textarea></div> +</div> +</div> +<div class="line"> +<div class="label">File</div> +<div class="value"><input type="file" name="image"></textarea></div> +</div> + +<div class="line"> +<div class="label">Options</div> +<div class="value"><select name="option"><option name="public" >test</option></select></div> +</div> + +<br> + +<input type="submit"></form> +<hr> + +</body> +</html>