initial commit
This commit is contained in:
312
archive/version_02/php/CaptchasDotNet.php
Executable file
312
archive/version_02/php/CaptchasDotNet.php
Executable file
@@ -0,0 +1,312 @@
|
||||
<?php
|
||||
//
|
||||
// PHP module for easy utilization of the free captchas.net CAPTCHA service
|
||||
//
|
||||
// For documentation look at http://captchas.net/sample/php/
|
||||
//
|
||||
// Written by
|
||||
// Sebastian Wilhelmi <seppi@seppi.de> and
|
||||
// Felix Holderied <felix@holderied.de>
|
||||
// This file is in the public domain.
|
||||
//
|
||||
// ChangeLog:
|
||||
//
|
||||
// 2006-08-16: New optional features integrated
|
||||
//
|
||||
// 2006-03-01: Only delete the random string from the repository in
|
||||
// case of a successful verification.
|
||||
//
|
||||
// 2006-02-14: Add new image() method returning an HTML/JavaScript
|
||||
// snippet providing a fault tolerant service.
|
||||
//
|
||||
// 2005-06-02: Initial version.
|
||||
//
|
||||
|
||||
class CaptchasDotNet
|
||||
{
|
||||
function CaptchasDotNet ($client, $secret,
|
||||
$random_repository = '/tmp/captchasnet-random-strings',
|
||||
$cleanup_time = 3600,
|
||||
$alphabet = 'abcdefghijklmnopqrstuvwxyz',
|
||||
$letters = 6,
|
||||
$width = 240,
|
||||
$height = 80
|
||||
)
|
||||
{
|
||||
$this->__client = $client;
|
||||
$this->__secret = $secret;
|
||||
$this->__random_repository = $random_repository;
|
||||
$this->__cleanup_time = $cleanup_time;
|
||||
$this->__time_stamp_file = $random_repository . '/__time_stamp__';
|
||||
$this->__alphabet = $alphabet;
|
||||
$this->__letters = $letters;
|
||||
$this->__width = $width;
|
||||
$this->__height = $height;
|
||||
}
|
||||
|
||||
function __random_string ()
|
||||
{
|
||||
// The random string shall consist of small letters, big letters
|
||||
// and digits.
|
||||
$letters = "abcdefghijklmnopqrstuvwxyz";
|
||||
$letters .= strtoupper ($letters) + "0123456789";
|
||||
|
||||
// The random starts out empty, then 40 random possible characters
|
||||
// are appended.
|
||||
$random_string = '';
|
||||
for ($i = 0; $i < 40; $i++)
|
||||
{
|
||||
$random_string .= $letters{rand (0, strlen ($letters) - 1)};
|
||||
}
|
||||
|
||||
// Return the random string.
|
||||
return $random_string;
|
||||
}
|
||||
|
||||
// Create a new random string and register it.
|
||||
function random ()
|
||||
{
|
||||
// If the repository directory is does not yet exist, create it.
|
||||
if (!is_dir ($this->__random_repository))
|
||||
{
|
||||
mkdir ($this->__random_repository);
|
||||
}
|
||||
|
||||
// If the time stamp file does not yet exist, create it.
|
||||
if (!is_file ($this->__time_stamp_file))
|
||||
{
|
||||
touch ($this->__time_stamp_file);
|
||||
}
|
||||
|
||||
// Get the current time.
|
||||
$now = time ();
|
||||
|
||||
// Determine the time, before which to remove random strings.
|
||||
$cleanup_time = $now - $this->__cleanup_time;
|
||||
|
||||
// If the last cleanup is older than specified, cleanup the
|
||||
// directory.
|
||||
if (filemtime ($this->__time_stamp_file) < $cleanup_time)
|
||||
{
|
||||
$handle = opendir ($this->__random_repository);
|
||||
while (true)
|
||||
{
|
||||
$filename = readdir ($handle);
|
||||
if (!$filename)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if ($filename != '.' && $filename != '..')
|
||||
{
|
||||
$filename = $this->__random_repository . '/' . $filename;
|
||||
if (filemtime ($filename) < $cleanup_time)
|
||||
{
|
||||
unlink ($filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir ($handle);
|
||||
|
||||
touch ($this->__time_stamp_file);
|
||||
}
|
||||
|
||||
// loop until a valid random string has been found and registered,
|
||||
// but at most 20 times. If no valid random has been found during
|
||||
// that time, there is something really wrong. Also show the error
|
||||
// in the last run.
|
||||
for ($remaining = 20; $remaining > 0; $remaining--)
|
||||
{
|
||||
// generate a new random string.
|
||||
$random = $this->__random_string ();
|
||||
|
||||
// open a file with the corresponding name in the repository
|
||||
// directory in such a way, that the creation fails, when the
|
||||
// file already exists. That should be near to impossible with
|
||||
// good seeding of the random number generator, but it's better
|
||||
// to play safe. If this is the last run, show the possible
|
||||
// error message.
|
||||
$filename = $this->__random_repository . '/' . $random;
|
||||
|
||||
if ($remaining == 1)
|
||||
{
|
||||
$file = fopen ($filename, 'x');
|
||||
}
|
||||
else
|
||||
{
|
||||
$file = @fopen ($filename, 'x');
|
||||
}
|
||||
|
||||
if ($file)
|
||||
{
|
||||
fclose ($file);
|
||||
break;
|
||||
}
|
||||
|
||||
// if the file already existed, rerun the loop to try the next
|
||||
// string.
|
||||
}
|
||||
|
||||
// return the successfully registered random string.
|
||||
$this->__random = $random;
|
||||
return $random;
|
||||
}
|
||||
|
||||
//
|
||||
// Generates image-URL Parameters are only atached if different from default
|
||||
//
|
||||
function image_url ($random = False, $base = 'http://image.captchas.net/')
|
||||
{
|
||||
if (!$random)
|
||||
{
|
||||
$random = $this->__random;
|
||||
}
|
||||
$image_url = $base;
|
||||
$image_url .= '?client=' . $this->__client;
|
||||
$image_url .= '&random=' . $random;
|
||||
if ($this->__alphabet!='abcdefghijklmnopqrstuvwxyz') {$image_url .= '&alphabet=' . $this->__alphabet;};
|
||||
if ($this->__letters!=6) {$image_url .= '&letters=' . $this->__letters;};
|
||||
if ($this->__width!=240) {$image_url .= '&width=' . $this->__width;};
|
||||
if ($this->__height!=80) {$image_url .= '&height=' . $this->__height;};
|
||||
return $image_url;
|
||||
}
|
||||
|
||||
//
|
||||
// Same as image_url but without width and height
|
||||
//
|
||||
function audio_url ($random = False, $base = 'http://audio.captchas.net/')
|
||||
{
|
||||
if (!$random)
|
||||
{
|
||||
$random = $this->__random;
|
||||
}
|
||||
$audio_url = $base;
|
||||
$audio_url .= '?client=' . $this->__client;
|
||||
$audio_url .= '&random=' . $random;
|
||||
if ($this->__alphabet!='abcdefghijklmnopqrstuvwxyz') {$audio_url .= '&alphabet=' . $this->__alphabet;};
|
||||
if ($this->__letters!=6) {$audio_url .= '&letters=' . $this->__letters;};
|
||||
return $audio_url;
|
||||
}
|
||||
|
||||
//
|
||||
// Generates complete html-sample with javascript to reload image from
|
||||
// backup server
|
||||
//
|
||||
function image ($random = False, $id = 'captchas.net')
|
||||
{
|
||||
$image = <<<EOT
|
||||
<a href="http://captchas.net"><img
|
||||
style="border: none; vertical-align: bottom"
|
||||
id="@ID@" src="@URL@" width="@WIDTH@" height="@HEIGHT@"
|
||||
alt="The Captcha image" /></a>
|
||||
<script type="text/javascript">
|
||||
<!--
|
||||
function captchas_image_error (image)
|
||||
{
|
||||
if (!image.timeout) return true;
|
||||
image.src = image.src.replace (/^http:\/\/image\.captchas\.net/,
|
||||
'http://image.backup.captchas.net');
|
||||
return captchas_image_loaded (image);
|
||||
}
|
||||
|
||||
function captchas_image_loaded (image)
|
||||
{
|
||||
if (!image.timeout) return true;
|
||||
window.clearTimeout (image.timeout);
|
||||
image.timeout = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
var image = document.getElementById ('@ID@');
|
||||
image.onerror = function() {return captchas_image_error (image);};
|
||||
image.onload = function() {return captchas_image_loaded (image);};
|
||||
image.timeout
|
||||
= window.setTimeout(
|
||||
"captchas_image_error (document.getElementById ('@ID@'))",
|
||||
10000);
|
||||
image.src = image.src;
|
||||
//-->
|
||||
</script>
|
||||
EOT;
|
||||
$image = str_replace ('@HEIGHT@', $this->__height, $image);
|
||||
$image = str_replace ('@WIDTH@', $this->__width, $image);
|
||||
$image = str_replace ('@ID@', $id, $image);
|
||||
$image = str_replace ('@URL@', $this->image_url (), $image);
|
||||
return $image;
|
||||
}
|
||||
|
||||
function validate ($random)
|
||||
{
|
||||
$this->__random = $random;
|
||||
|
||||
$file_name = $this->__random_repository . '/' . $random;
|
||||
|
||||
// Find out, whether the file exists
|
||||
$result = is_file ($file_name);
|
||||
|
||||
// if the file exists, remember it.
|
||||
if ($result)
|
||||
{
|
||||
$this->__random_file = $file_name;
|
||||
}
|
||||
|
||||
// the random string was valid, if and only if the corresponding
|
||||
// file existed.
|
||||
return $result;
|
||||
}
|
||||
|
||||
function verify ($input, $random = False)
|
||||
{
|
||||
if (!$random)
|
||||
{
|
||||
$random = $this->__random;
|
||||
}
|
||||
$password_letters = $this->__alphabet;
|
||||
$password_length = $this->__letters;
|
||||
|
||||
// If the user input has the wrong lenght, it can't be correct.
|
||||
if (strlen ($input) != $password_length)
|
||||
{
|
||||
return False;
|
||||
}
|
||||
|
||||
// Calculate the MD5 digest of the concatenation of secret key and
|
||||
// random string. The digest is a hex string.
|
||||
$encryption_base = $this->__secret . $random;
|
||||
// This extension is needed for secure use of optional parameters
|
||||
// In case of standard use we do not append the values, to be
|
||||
// compatible to existing implementations
|
||||
if(($password_letters != 'abcdefghijklmnopqrstuvwxyz') || ($password_length != '6'))
|
||||
{
|
||||
$encryption_base = $encryption_base . ':' . $password_letters . ':' . $password_length;
|
||||
}
|
||||
$digest = md5 ($encryption_base);
|
||||
|
||||
// Check the password according to the rules from the first
|
||||
// positions of the digest.
|
||||
for ($pos = 0; $pos < $password_length; $pos++)
|
||||
{
|
||||
$letter_num
|
||||
= hexdec (substr ($digest, 2 * $pos, 2)) % strlen ($password_letters);
|
||||
|
||||
// If the letter at the current position is wrong, the user
|
||||
// input isn't correct.
|
||||
if ($input[$pos] != $password_letters[$letter_num])
|
||||
{
|
||||
return False;
|
||||
}
|
||||
}
|
||||
|
||||
// if the file exists, remove it.
|
||||
if ($this->__random_file)
|
||||
{
|
||||
unlink ($this->__random_file);
|
||||
unset ($this->__random_file);
|
||||
}
|
||||
|
||||
// The user input was correct.
|
||||
return True;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
221
archive/version_02/php/func_xml.php
Executable file
221
archive/version_02/php/func_xml.php
Executable file
@@ -0,0 +1,221 @@
|
||||
<?php
|
||||
#################################################################################
|
||||
# #
|
||||
# XML functions #
|
||||
# written by benjamin ramey #
|
||||
# bsr@rameydesign.net #
|
||||
# #
|
||||
# best viewed with: #
|
||||
# resolution = 1280x960 #
|
||||
# tab = 4 spaces #
|
||||
# indent = 8 spaces #
|
||||
# #
|
||||
#################################################################################
|
||||
|
||||
##################################################### FUNCTION: xml_parse_file #
|
||||
# parameters: path to file to parse
|
||||
# returns: associative array of xml file data
|
||||
# DOES NOT: handle CDATA, doing anything with WSDL,XSL,namespaces, or numerous
|
||||
# other cool XML stuff
|
||||
function xml_parse_file($xml_file)
|
||||
{
|
||||
$xml = '';
|
||||
$data = array();
|
||||
|
||||
# first we don't want to waste time, so make sure the file to open exists
|
||||
if(file_exists($xml_file))
|
||||
{
|
||||
# put entire file contents in $xml
|
||||
$xml = file_get_contents($xml_file);
|
||||
# if the file is empty, return and error
|
||||
if(empty($xml)) {
|
||||
return('XML document is empty.');
|
||||
}
|
||||
}
|
||||
# if the file doesn't exist return an error
|
||||
else { return("Could not find xml file."); }
|
||||
|
||||
# this pattern matches the first line of every xml doc, the <?xml line
|
||||
$patt = "/^<\?xml([^\?]*)\?>/";
|
||||
|
||||
# try to find this mandatory opening <?xml line
|
||||
if(preg_match($patt,$xml,$matches))
|
||||
{
|
||||
# get the xml specs inside the <?xml line
|
||||
$xml_specs = explode(' ',$matches[1]);
|
||||
foreach($xml_specs as $spec)
|
||||
{
|
||||
# if a spec is empty for some reason, don't attempt to work with it,
|
||||
# just continue to next one
|
||||
if(empty($spec)) { continue; }
|
||||
# specs are always in the name='value' format
|
||||
list($name,$value) = explode('=',$spec);
|
||||
# strip away any quotes from the value side of the '='
|
||||
$value = str_replace("'",'',$value);
|
||||
$value = str_replace('"','',$value);
|
||||
|
||||
# assign these xml specs to the data array
|
||||
$data[$name] = $value;
|
||||
}
|
||||
$xml = substr($xml,strlen($matches[0]));
|
||||
}
|
||||
# if we don't find that mandatory opening <?xml line, return an error
|
||||
else { return('XML document missing opening xml tag.'); }
|
||||
|
||||
# xml_left is a heap, initialize with all xml at first
|
||||
$xml_left = array($xml);
|
||||
# tmp_refs is also a heap, holding references to points in the xml document
|
||||
$tmp_refs[] =& $data;
|
||||
# we will use this pattern to recognize an opening xml tag
|
||||
$patt = "/^<(\w+)([^>]*)>/";
|
||||
|
||||
# we continue through the xml until nothing is left in $xml_left
|
||||
while(!empty($xml_left))
|
||||
{
|
||||
# the last element of $xml_left is always the high priority,
|
||||
# so process it next
|
||||
$to_process = array_pop($xml_left);
|
||||
# trim fore and aft spaces from the xml to process
|
||||
$to_process = trim($to_process);
|
||||
# the last index of our tmp_refs array, important later
|
||||
$last_index = count($tmp_refs) - 1;
|
||||
|
||||
# attempt the match the pattern that finds an opening xml tag
|
||||
if(preg_match($patt,$to_process,$matches)) {
|
||||
$element = $matches[1];
|
||||
$parameters = isset($matches[2]) ? $matches[2] : '';
|
||||
|
||||
if(isset($tmp_refs[$last_index][$element]))
|
||||
{
|
||||
if(is_array($tmp_refs[$last_index][$element]))
|
||||
{
|
||||
# case example: [biography]=>[0],[1]...[x]; $element = biography
|
||||
# endsup: [biography]=>[0],[1]...[x+1]
|
||||
if(isset($tmp_refs[$last_index][$element][0]))
|
||||
{
|
||||
array_push($tmp_refs[$last_index][$element],'');
|
||||
$tmp_index = count($tmp_refs[$last_index][$element]) - 1;
|
||||
$tmp_refs[] =& $tmp_refs[$last_index][$element][$tmp_index];
|
||||
}
|
||||
# case example: [biography]=>[paragraph]; $element = biography
|
||||
# endsup: [biography]=>[0]=>[paragraph]
|
||||
# [1]=>''
|
||||
else
|
||||
{
|
||||
$tmp_ary = $tmp_refs[$last_index][$element];
|
||||
$tmp_refs[$last_index][$element] = array($tmp_ary,'');
|
||||
$tmp_refs[] =& $tmp_refs[$last_index][$element][1];
|
||||
}
|
||||
}
|
||||
# case example: [biography]=>"string"; $element = biography
|
||||
# endsup: [biography]=>[0]=>"string"
|
||||
# [1]=>''
|
||||
else
|
||||
{
|
||||
$tmp_refs[$last_index][$element] = array($tmp_refs[$last_index][$element],'');
|
||||
$tmp_refs[] =& $tmp_refs[$last_index][$element][1];
|
||||
}
|
||||
}
|
||||
# case example: [biography]=> NULL; $element = biography
|
||||
# endsup: [biography]=>""
|
||||
else
|
||||
{
|
||||
$tmp_refs[$last_index][$element] = '';
|
||||
$tmp_refs[] =& $tmp_refs[$last_index][$element];
|
||||
}
|
||||
|
||||
# if there are parameters, add them as if they were imbedded elements
|
||||
if(!empty($parameters))
|
||||
{
|
||||
$pairs = explode(' ',$parameters);
|
||||
$last_index = count($tmp_refs) - 1;
|
||||
|
||||
# we can do this, because we know the last element added
|
||||
# is always fresh and new
|
||||
$tmp_refs[$last_index] = array();
|
||||
|
||||
foreach($pairs as $pair)
|
||||
{
|
||||
# if a parameter is empty, just continue to next one
|
||||
if(empty($pair)) { continue; }
|
||||
|
||||
list($name,$value) = explode('=',$pair);
|
||||
$value = str_replace("'",'',$value);
|
||||
$value = str_replace('"','',$value);
|
||||
|
||||
$tmp_refs[$last_index][$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
# we've already found an opening tag, now find the closing tag
|
||||
$end_tag = strpos($to_process,"</$element>");
|
||||
|
||||
# if there is not closing tag--oops! badly formatted xml doc
|
||||
if($end_tag === FALSE)
|
||||
{
|
||||
return("XML document improperly formed.
|
||||
No closing tag found for element: $element");
|
||||
}
|
||||
|
||||
# get the string length of the opening tag and all the paramters
|
||||
# inside, plus the opening and closing brackets
|
||||
$element_len = strlen($matches[0]);
|
||||
# then, the contents of this xml tag start after the above
|
||||
# measured length to the end of the $to_process string, minus
|
||||
# the length of the closing tag
|
||||
$contents = substr($to_process,$element_len,$end_tag - $element_len);
|
||||
|
||||
# if there is text in the $to_process string past this closing
|
||||
# tag that we just found, put it on top of the $xml_left tag
|
||||
# it's a sister element of the current one
|
||||
if($extra = substr($to_process,$end_tag + strlen("</$element>")))
|
||||
{
|
||||
array_push($xml_left,$extra);
|
||||
}
|
||||
# if not sister elements are found, we are at the end of this
|
||||
# generation of elements, so place an END indicator in the
|
||||
# $xml_left array
|
||||
else
|
||||
{
|
||||
array_push($xml_left,'!~~END~~!');
|
||||
}
|
||||
array_push($xml_left,$contents);
|
||||
}
|
||||
# we place the identifier END to indicate the end of an xml tag that
|
||||
# has nested tags and no content itself, if this is NOT the case
|
||||
# we have content to assign the current xml tag
|
||||
else if($to_process != '!~~END~~!')
|
||||
{
|
||||
$tmp_refs[$last_index] = $to_process;
|
||||
array_pop($tmp_refs);
|
||||
}
|
||||
# if we miss the above two ifs, we are at the end of an element
|
||||
# but don't have content to assign, so step back one generation
|
||||
# in the xml document
|
||||
else
|
||||
{
|
||||
$tmp = array_pop($tmp_refs);
|
||||
}
|
||||
}
|
||||
|
||||
# if all went well, we return an associative array with all the xml
|
||||
# data in it, properly nested
|
||||
return($data);
|
||||
}
|
||||
|
||||
##################################################### FUNCTION: xml_force_array #
|
||||
# parameters: path to file to parse
|
||||
# returns: associative array of xml file data
|
||||
function xml_force_array($element)
|
||||
{
|
||||
$array = array();
|
||||
if(is_array($element))
|
||||
{
|
||||
if(isset($element[0])) { $array = $element; }
|
||||
else { array_push($array,$element); }
|
||||
}
|
||||
else { array_push($array,$element); }
|
||||
|
||||
return($array);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user