Unverified Commit 24fe6820 authored by Nathan Van Overloop's avatar Nathan Van Overloop
Browse files

centralnic/issues#3769: rework to std classes

parent e7e93c4a
<?php
class Net_EPP {
class Net_EPP
{
/**
* load a class by giving it's short name
* @return boolean
*/
static function autoload($class) {
$prefix = __CLASS__.'_';
/**
* load a class by giving it's short name
* @return boolean
*/
public static function autoload($class)
{
$prefix = __CLASS__.'_';
if ($prefix == substr($class, 0, strlen($prefix))) {
$basedir=dirname(dirname(__FILE__));
debug_log("class name is {$class} from dir {$basedir}");
$file = $basedir.'/'.str_replace('_', '/', $class).'.php';
if(!file_exists($file)){
debug_log("the file {$file} does not exist");
return false;
}
return include_once($file);
} else {
return false;
}
}
if ($prefix == substr($class, 0, strlen($prefix))) {
$basedir=dirname(dirname(__FILE__));
syslog(LOG_INFO, "class name is {$class} from dir {$basedir}");
$file = $basedir.'/'.str_replace('_', '/', $class).'.php';
if (!file_exists($file)) {
syslog(LOG_CRIT, "the file {$file} does not exist");
return false;
}
return include_once($file);
} else {
return false;
}
}
}
spl_autoload_register(array('Net_EPP', 'autoload'));
<?php
/* EPP Client class for PHP, Copyright 2005 CentralNic Ltd
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
......@@ -28,137 +28,152 @@
* Low-level functions useful for both EPP clients and servers
* @package Net_EPP
*/
class Net_EPP_Protocol {
static function _fread_nb($socket,$length) {
$result = '';
// Loop reading and checking info to see if we hit timeout
$info = stream_get_meta_data($socket);
$time_start = microtime(true);
$timeout_time=$time_start+$GLOBALS['timeout'];
while (!$info['timed_out'] && !feof($socket)) {
//make sure we don't wait to long
if($timeout_time<microtime(true)){
$time_diff=microtime(true)-$time_start;
throw new exception("Timeout reading from EPP server after $time_diff seconds");
}
// Try read remaining data from socket
$buffer = fread($socket,$length - strlen($result));
// If the buffer actually contains something then add it to the result
if ($buffer !== false) {
$result .= $buffer;
// If we hit the length we looking for, break
if (strlen($result) == $length) {
break;
}
} else {
// Sleep 0.25s
usleep(250000);
}
// Update metadata
$info = stream_get_meta_data($socket);
class Net_EPP_Protocol
{
public static function _fread_nb($socket, $length)
{
$result = '';
// Loop reading and checking info to see if we hit timeout
$info = stream_get_meta_data($socket);
$time_start = microtime(true);
$timeout_time=$time_start+$GLOBALS['timeout'];
while (!$info['timed_out'] && !feof($socket)) {
//make sure we don't wait to long
if ($timeout_time<microtime(true)) {
$time_diff=microtime(true)-$time_start;
throw new exception("Timeout reading from EPP server after $time_diff seconds");
}
// Try read remaining data from socket
$buffer = fread($socket, $length - strlen($result));
// If the buffer actually contains something then add it to the result
if ($buffer !== false) {
$result .= $buffer;
// If we hit the length we looking for, break
if (strlen($result) == $length) {
break;
}
} else {
// Sleep 0.25s
usleep(250000);
}
// Update metadata
$info = stream_get_meta_data($socket);
$time_end = microtime(true);
}
// Check for timeout
if ($info['timed_out']) {
throw new Exception('Timeout while reading data from socket');
}
if ($GLOBALS['debug']) {
$time_diff=(microtime(true)-$time_start)*1000;
debug_log("returning after {$time_diff} ms");
}
return $result;
}
public static function _fwrite_nb($socket, $buffer, $length)
{
// Loop writing and checking info to see if we hit timeout
$info = stream_get_meta_data($socket);
$time_start = microtime(true);
$pos = 0;
while (!$info['timed_out'] && !feof($socket)) {
// Some servers don't like alot of data, so keep it small per chunk
$wlen = $length - $pos;
if ($wlen > 1024) {
$wlen = 1024;
}
// Try write remaining data from socket
$written = @fwrite($socket, substr($buffer, $pos), $wlen);
// If we read something, bump up the position
if ($written && $written !== false) {
$pos += $written;
// If we hit the length we looking for, break
if ($pos == $length) {
break;
}
} else {
// Sleep 0.25s
usleep(250000);
}
// Update metadata
$info = stream_get_meta_data($socket);
$time_end = microtime(true);
}
// Check for timeout
if ($info['timed_out']) {
throw new Exception('Timeout while reading data from socket');
}
if($GLOBALS['debug']){
$time_diff=microtime(true)-$time_start;
debug_log("returning after {$time_diff}");
}
return $result;
}
static function _fwrite_nb($socket,$buffer,$length) {
// Loop writing and checking info to see if we hit timeout
$info = stream_get_meta_data($socket);
$time_start = microtime(true);
$pos = 0;
while (!$info['timed_out'] && !feof($socket)) {
// Some servers don't like alot of data, so keep it small per chunk
$wlen = $length - $pos;
if ($wlen > 1024) { $wlen = 1024; }
// Try write remaining data from socket
$written = @fwrite($socket,substr($buffer,$pos),$wlen);
// If we read something, bump up the position
if ($written && $written !== false) {
$pos += $written;
// If we hit the length we looking for, break
if ($pos == $length) {
break;
}
} else {
// Sleep 0.25s
usleep(250000);
$timeDiff=round(($time_end-$time_start)*1000);
if($GLOBALS['debug']){
debug_log("time to write to socket ${timeDiff}ms");
}
// Update metadata
$info = stream_get_meta_data($socket);
$time_end = microtime(true);
if (($time_end - $time_start) > 10000000) {
throw new exception('Timeout while writing to EPP Server');
}
}
// Check for timeout
if ($info['timed_out']) {
throw new Exception('Timeout while writing data to socket');
}
return $pos;
}
/**
* get an EPP frame from the remote peer
* @param resource $socket a socket connected to the remote peer
* @throws Exception on frame errors.
* @return string the frame
*/
static function getFrame($socket) {
if($GLOBALS['debug']) debug_log("start reading first 4 bytes");
// Read header
$hdr = Net_EPP_Protocol::_fread_nb($socket,4);
if($GLOBALS['debug']) debug_log("read header successfully");
// Unpack first 4 bytes which is our length
$unpacked = unpack('N', $hdr);
$length = $unpacked[1];
if ($length < 5) {
throw new Exception(sprintf('Got a bad frame header length of %d bytes from peer', $length));
} else {
$length -= 4; // discard the length of the header itself
// Read frame
return Net_EPP_Protocol::_fread_nb($socket,$length);
}
}
/**
* send an EPP frame to the remote peer
* @param resource $socket a socket connected to the remote peer
* @param string $xml the XML to send
* @throws Exception when it doesn't complete the write to the socket
* @return the amount of bytes written to the frame
*/
static function sendFrame($socket, $xml) {
// Grab XML length & add on 4 bytes for the counter
if (($time_end - $time_start) > 10000000) {
throw new exception('Timeout while writing to EPP Server');
}
}
// Check for timeout
if ($info['timed_out']) {
throw new Exception('Timeout while writing data to socket');
}
return $pos;
}
/**
* get an EPP frame from the remote peer
* @param resource $socket a socket connected to the remote peer
* @throws Exception on frame errors.
* @return string the frame
*/
public static function getFrame($socket)
{
if ($GLOBALS['debug']) {
debug_log("start reading first 4 bytes");
}
// Read header
$hdr = Net_EPP_Protocol::_fread_nb($socket, 4);
if ($GLOBALS['debug']) {
debug_log("read header successfully containing ".var_dump($hdr));
}
// Unpack first 4 bytes which is our length
$unpacked = unpack('N', $hdr);
$length = $unpacked[1];
if ($length < 5) {
throw new Exception(sprintf('Got a bad frame header length of %d bytes from peer', $length));
} else {
$length -= 4; // discard the length of the header itself
// Read frame
return Net_EPP_Protocol::_fread_nb($socket, $length);
}
}
/**
* send an EPP frame to the remote peer
* @param resource $socket a socket connected to the remote peer
* @param string $xml the XML to send
* @throws Exception when it doesn't complete the write to the socket
* @return the amount of bytes written to the frame
*/
public static function sendFrame($socket, $xml)
{
$length = strlen($xml) + 4;
$res = Net_EPP_Protocol::_fwrite_nb($socket, pack('N',$length) . $xml,$length);
// Check our write matches
if ($length != $res) {
throw new Exception("Short write when sending XML");
}
return $res;
}
if($GLOBALS['debug']){
debug_log("length of the header is ${length} about to write ${xml}");
}
// Grab XML length & add on 4 bytes for the counter
$res = Net_EPP_Protocol::_fwrite_nb($socket, pack('N', $length) . $xml, $length);
// Check our write matches
if ($length != $res) {
throw new Exception("Short write when sending XML");
}
return $res;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment