Page MenuHomeDevCentral

No OneTemporary

diff --git a/finger/FingerClient.php b/finger/FingerClient.php
index 9442fcb..26deb83 100644
--- a/finger/FingerClient.php
+++ b/finger/FingerClient.php
@@ -1,232 +1,232 @@
<?php
/**
* Finger client
*/
class FingerClient {
/**
* The finger remote server
* @var string
*/
public $server;
/**
* The finger remote user
* @var string
*/
public $user;
/**
* The finger raw result
* @var string
*/
public $rawResult;
/**
* The finger structured result
* @var array
*/
public $structuredResult;
/**
* Timeout time to connect to finger server
* @var int
*/
public $timeout = 10;
/**
* The last error
* @var string
*/
public $lastError;
/**
- * Blacklist of blocked hosts, where you can't finger
+ * Blocked hosts, where you can't finger
* @var array
*/
private $blockedHosts = [];
/**
* Initializes a new instance of the FingerClient object
*
* @param string server The finger remote server
* @param string user The finger remote user
*/
public function __construct ($server, $user) {
$this->server = $server;
$this->user = $user;
}
/**
* Initializes a new instance of the FingerClient object from an address
*
* @param string $address The finger address
* @return FingerClient a FingerClient instance; if the address is invalid, returns null.
*/
public static function FromAddress ($address) {
if (self::IsValid($address)) {
$data = explode('@', $address, 2);
return new self($data[1], $data[0]);
}
return null;
}
/**
* Runs the finger client
* After this method have been called, the rawResult member will be available.
*
* @return bool If the session gets finger information, true; otherwise, false.
*/
public function Run () {
if (in_array($this->server, $this->blockedHosts)) {
- $this->lastError = "This server $this->server has been blacklisted (probably because of frequent timeouts).";
+ $this->lastError = "This server $this->server has been blocked (probably because of frequent timeouts).";
return false;
}
$fp = @fsockopen($this->server, 79, $errno, $errstr, $this->timeout);
if ($fp === false) {
$this->lastError = "Can't connect to $this->server. $errstr";
return false;
}
fwrite($fp, $this->user);
fwrite($fp, "\n");
$this->rawResult = '';
while(!feof($fp)) {
$this->rawResult .= fgets($fp, 4096);
}
fclose($fp);
if (trim($this->rawResult) == "finger: $this->user: no such user.") {
$this->lastError = "No such user on this server.";
return false;
}
return true;
}
/**
* Parses the finger result to get structured data
*
* After this method have been called, the structuredResult member will be available.
*/
public function Parse () {
$lines = explode("\n", $this->rawResult);
$fields = array();
$n = count($lines);
$mode = ''; //Parsing mode ('' for Plan or simpler fields), 'project' for the project)
for ($i = 0 ; $i < $n ; $i++) {
$line = $lines[$i];
if ($mode == 'project') {
if ($i == $n - 1 || trim($line) == 'Plan:' || trim($line) == 'No Plan.') {
//Ends project mode
$mode = '';
$fields[] = ['field' => 'Project', 'data' => $project];
} else {
$project .= $line;
continue;
}
}
if (strpos($line, "\t") !== false) {
//This is probably a line with two fields (A: ... B: ...).
$data = explode("\t", $line, 2);
for ($j = 0 ; $j < 2 ; $j++) {
$fields[] = self::ParseBlock($data[$j]);
}
continue;
}
if (trim($line) == 'Project:') {
$mode = 'project';
$project = '';
continue;
}
if (trim($line) == 'No Mail.') {
$fields[] = ['field' => 'Mail', 'data' => ''];
continue;
}
if (trim($line) == 'No Plan.') {
$fields[] = ['field' => 'Plan', 'data' => ''];
continue;
}
if (trim($line) == 'No Project.') {
$fields[] = ['field' => 'Project', 'data' => ''];
continue;
}
if (trim($line) == 'Plan:') {
$fields[] = [
'field' => 'Plan',
'data' => implode("\n", array_slice($lines, ++$i))
];
break;
}
if (trim($line) !== "") {
$data = self::ParseBlock($line);
$fields[] = $data;
}
}
$this->structuredResult = $fields;
}
/**
* Parses a string to get field and data information
*
* @param string $The finger line
*/
private static function ParseBlock ($data) {
$info = explode(': ', trim($data), 2);
if (count($info) == 1) {
return ['field' => '', 'data' => $info[0]];
}
return ['field' => $info[0], 'data' => $info[1]];
}
/**
* Gets the specified field parsing the Finger raw result
*
* @param string $field The field to retrieve
* @return string The field data.
*/
public function Get ($field) {
if ($this->structuredResult === null) return null;
foreach ($this->structuredResult as $info) {
if ($info['field'] == $field) {
return $info['data'];
}
}
return null;
}
/**
* Determines if an address is syntactically valid
*
* @param string $address The finger address
* @return bool true if the address is valid; otherwise, false.
*/
public static function IsValid ($address) {
return preg_match("/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/", $address);
}
/**
- * Adds the specified hosts into the blacklist
+ * Adds the specified hosts into the blocklist
*
* @param array the list of hosts to add to the blacklist
*/
- public function AddToBlacklist ($blockedHosts) {
+ public function AddToBlocklist ($blockedHosts) {
$this->blockedHosts = array_merge($this->blockedHosts, $blockedHosts);
}
/**
- * Clears the blacklist
+ * Clears the blocklist
*/
- public function ClearBlacklist () {
+ public function ClearBlocklist () {
$this->blockedHosts = [];
}
}
diff --git a/finger/blacklist.txt b/finger/blocklist.txt
similarity index 100%
rename from finger/blacklist.txt
rename to finger/blocklist.txt
diff --git a/finger/thimbl.php b/finger/thimbl.php
index 56b05b2..0c5ba35 100644
--- a/finger/thimbl.php
+++ b/finger/thimbl.php
@@ -1,145 +1,145 @@
<?php
/**
* Represents a controller to process the Thimbl client request
*/
class ThimblController {
///
/// Constants
///
/**
* The path to the server to not finger
*
* @const
*/
- const BLACKLIST_FILE = 'finger/blacklist.txt';
+ const BLOCKLIST_FILE = 'finger/blocklist.txt';
///
/// Private properties
///
/**
* Thimbl document from a plan file
*
* @var ThimblDocument|null
*/
private $thimblDocument = null;
/**
* A finger client instance used to fetch the plan file containing the Thimbl feed
*
* @var FingerClient
*/
private $client;
/**
* Errors occured during request processing
*
* @var array
*/
private $errors = [];
/**
* Runs the controller logic
*/
public function run () {
if (array_key_exists('user', $_REQUEST)) {
$this->processThimblClientRequest();
}
if (count($this->errors)) {
$this->printErrors();
}
$this->printInputForm();
if ($this->thimblDocument !== null) {
$this->printThimblFeed();
}
}
/**
* Processes Thimbl client request
*/
public function processThimblClientRequest() {
require_once('FingerClient.php');
$this->client = FingerClient::fromAddress($_REQUEST['user']);
if ($this->client === null) {
$this->errors[] = 'Invalid Finger address format.';
return;
}
- $this->handleBlackList();
+ $this->handleBlockList();
if (!$this->client->Run()) {
$this->errors[] = $this->client->lastError;
return;
}
$this->client->Parse();
if (!$planField = $this->client->Get('Plan')) {
$this->errors[] = 'Finger connection successful, but there is no plan file.';
return;
}
require_once('ThimblDocument.php');
if (!$this->thimblDocument = ThimblDocument::FromJSON($planField)) {
$this->printDebugPlanMessage($planField);
}
}
/**
* Prints debug .plan message
*
* @param string $planField
*/
private function printDebugPlanMessage ($planField) {
echo '<div class="alert-box alert">Finger connection successful, but the plan file format is not a Thimbl one.<br />';
echo 'JSON error returned by the parser: ', json_last_error_msg();
echo '</div>';
echo "<h2>Plan file for $_REQUEST[user]</h2>";
echo '<pre>', clean_string($planField), '</pre>';
}
/**
- * Handles blacklist
+ * Handles blocklist
*/
- private function handleBlackList () {
- if (file_exists(self::BLACKLIST_FILE)) {
- $blackListedServers = explode(
+ private function handleBlockList () {
+ if (file_exists(self::BLOCKLIST_FILE)) {
+ $blockListedServers = explode(
"\n",
trim(
- file_get_contents(self::BLACKLIST_FILE)
+ file_get_contents(self::BLOCKLIST_FILE)
)
);
- $this->client->AddToBlacklist($blackListedServers);
+ $this->client->AddToBlocklist($blockListedServers);
}
}
/**
* Prints errors
*/
public function printErrors () {
foreach ($this->errors as $error) {
echo '<div class="alert-box alert">', $error, '</div>';
}
}
/**
* Prints input form
*/
public function printInputForm () {
include('thimbl_inputForm.html');
}
public function printThimblFeed () {
$plan = $this->thimblDocument;
echo "<h2>Thimbl feed for $_REQUEST[user]</h2>";
require_once('thimbl_feed.php');
}
}
(new ThimblController())->run();

File Metadata

Mime Type
text/x-diff
Expires
Mon, Sep 15, 04:44 (10 h, 42 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2983711
Default Alt Text
(11 KB)

Event Timeline