.
*/
/**
* Methods return this if they succeed
*/
if (@defined('AA_PP_DEBUG')) return;
define('AA_PP_DEBUG', 0);
/**
* Methods return this if they succeed
*/
define('ACLF', chr(13) . chr(10));
register_activation_hook(__FILE__, 'aa_pp_activate');
register_deactivation_hook(__FILE__, 'aa_pp_deactivate');
add_action('admin_menu', 'aa_pp_setup_options');
if (strpos($_SERVER['REQUEST_URI'], basename(__FILE__)) !== false)
{
add_action('admin_head', 'aa_pp_admin_header');
//error_reporting(E_ALL | E_NOTICE);
}else error_reporting(E_NONE);
/**
* aa_pp_admin_header()
*
* @return
*/
function aa_pp_admin_header()
{
global $wpdb, $aa_PP, $aa_SIDS;
if (!current_user_can(8))
die(__("You are not allowed to be here without upload permissions"));
if (!user_can_access_admin_page())
wp_die(__('You do not have sufficient permissions to access this page.'));
$aa_PP = get_option('askapache_password_protect');
$aa_SIDS = get_option('askapache_password_protect_sids');
$f=aa_pp_get_post_values($aa_PP);
update_option('askapache_password_protect', $aa_PP);
}
/**
* Gets the POST values and checks them for errors
*
* @param array v the global aa_PP array
* @return mixed true on success array of errors on fail
*/
function aa_pp_get_post_values($v)
{
global $aa_PP, $aa_SIDS;
$action = 'none';
$errors=array();
if(isset($_POST['notice']))
unset($_POST['notice']);
foreach(array('a_user','a_authdomain','a_authtype','a_algorithm','a_authname','a_authuserfile','a_step') as $k)
{
if(isset($_POST[$k]) && !empty($_POST[$k]) && $_POST[$k] != $v[$k])
$v[substr($k,2)]=$_POST[$k];
}
foreach (array('activate-selected', 'deactivate-selected', 'delete-selected', 'm_move') as $action_key)
{
if (isset($_POST[$action_key]))
{
$action = $action_key;
break;
}
}
if($action=='m_move')
{
check_admin_referer('askapache-move-area');
foreach(array('m_read','m_reset','m_sid','m_setup','m_test','m_welcome','m_contact') as $where)
{
if(isset($_POST[$where]))
{
$aa_PP['step']=substr($where,2);
break;
}
}
return true;
}
if (isset($_GET['deactivate-sid']))
$action = 'deactivate-sid';
if (isset($_GET['activate-sid']))
$action = 'activate-sid';
if (isset($v['authtype']) && !in_array($v['authtype'], array('Digest', 'Basic')))
$errors[]='Incorrect authtype';
if (isset($v['algorithm']) && !in_array($v['algorithm'], array('crypt', 'md5', 'sha1')))
$errors[]='Incorrect algorithm';
if(strpos($v['user'],':')!==false)
$errors[]='Username cannot contain the : character';
if (strlen($v['authname']) > 65)
$errors[]='Authname cannot exceed 65 characters, yours was '.strlen($v['authname']).' characters';
if ($v['authtype'] == 'Digest' && $v['algorithm'] != 'md5')
$errors[]='Digest Authentication can only use the md5 algorithm';
foreach(array($v['authuserfile'],$v['admin_htaccess'],$v['root_htaccess']) as $f)
{
if(strpos(basename($f),'.ht')===false)
$errors[]='File names must start with .ht like .htaccess or .htpasswd3';
if(file_exists($f) && !@is_writable($f))
$errors[]='Please make '.$f.' writable and readable';
}
if(sizeof($errors)==0)
{
$aa_PP=$v;
switch ($action)
{
case 'activate-sid':
$sid = (int)$_GET['activate-sid'];
check_admin_referer('activate-sid_' . $sid);
if(!aa_pp_activate_sid($sid))
$errors[]="Failed to activate sid {$sid}";
echo '';
break;
case 'deactivate-sid':
$sid = (int)$_GET['deactivate-sid'];
check_admin_referer('deactivate-sid_' . $sid);
if(!aa_pp_deactivate_sid($sid))
$errors[]="Failed to deactivate sid {$sid}";
break;
case 'activate-selected':
check_admin_referer('askapache-bulk-sids');
break;
case 'deactivate-selected':
check_admin_referer('askapache-bulk-sids');
break;
}
if (isset($_POST['a_pass']))
{
if(!aa_pp_file_put_c($v['authuserfile'], aa_pp_hashit($v['authtype'], $v['user'], $_POST['a_pass'], $v['authname']), false))
$errors[]='Failed to create '.$v['authuserfile'];
if (isset($_POST['a_wpadmin_protection']))
{
$sid=20030002;
if(!aa_pp_activate_sid($sid, $v['admin_htaccess']))
$errors[]='Failed to activate sid {$sid}';
}
if (isset($_POST['a_wplogin_protection']))
{
$sid=20030001;
if(!aa_pp_activate_sid($sid, $v['root_htaccess']))
$errors[]='Failed to activate sid {$sid}';
}
}
}
if(sizeof($errors)>0)
{
@$_POST['notice']=join("
",$errors);
}
$aa_PP=$v;
return true;
}
/**
* aa_pp_main_page()
*
* @return
*/
function aa_pp_main_page()
{
global $aa_PP, $aa_SIDS;
?>
'.$_POST['notice'].'
'; switch ($aa_PP['step']) { case 'contact': ?>I'm finally mostly happy with the system now used by this plugin to update/modify/and use the different modules. The old code just wasn't future-proofed enough. This new version is based very much off of the WordPress Plugins code, so it is future proofed.
This "Improvements" page is the start of whats to come, Basically each of the security modules (and there are a LOT of great mod_security ones coming) will have their own very Basic settings. So you can tweak the settings. If someone finds an improvement they can send it for review. New ideas and modules can be submitted here also.
Modules are inserted into your server .htaccess configuration files. Once a plugin is installed, you may activate it or deactivate it here.
"; foreach($rules as $line)@$_POST['notice'].=htmlentities($line); @$_POST['notice'].=''; } return true; } function aa_pp_htaccess_file_init($file=false) { global $aa_PP; if(!$file) $files=array($aa_PP['admin_htaccess'],$aa_PP['root_htaccess']); else $files=array($file); foreach($files as $file) { $wordp=$new=$jot=array(); $aapasspro=$wpg=$s=false; $ot = array( '# +ASKAPACHE PASSPRO ' . $aa_PP['plugin_data']['Version'], '#######################################################', '# __ __', '# ____ ______/ /______ _____ ____ ______/ /_ ___', '# / __ `/ ___/ //_/ __ `/ __ \/ __ `/ ___/ __ \/ _ \ ', '# / /_/ (__ ) ,< / /_/ / /_/ / /_/ / /__/ / / / __/', '# \__,_/____/_/|_|\__,_/ .___/\__,_/\___/_/ /_/\___/', '# /_/', '# - - - - - - - - - - - - - - - - - - - - - - - - - - -', '# +APRO SIDS', '# -APRO SIDS', '# - - - - - - - - - - - - - - - - - - - - - - - - - - -', '# __ __', '# ____ ______/ /______ _____ ____ ______/ /_ ___', '# / __ `/ ___/ //_/ __ `/ __ \/ __ `/ ___/ __ \/ _ \ ', '# / /_/ (__ ) ,< / /_/ / /_/ / /_/ / /__/ / / / __/', '# \__,_/____/_/|_|\__,_/ .___/\__,_/\___/_/ /_/\___/', '# /_/', '#######################################################', '# -ASKAPACHE PASSPRO ' . $aa_PP['plugin_data']['Version']); $markerdata = (is_writable(dirname($file)) && touch($file)) ? @explode("\n", @implode('', @file($file))) : false; if($markerdata) { foreach ($markerdata as $line) { if(strpos($line, '# BEGIN WordPress')!==false) $s=$wpg=true; if($s===true) $wordp[]=$line; if(strpos($line, '# END WordPress')!==false) { $s=false; continue; } if(!$s) $new[]=$line; if(strpos($line, '# +ASKAPACHE PASSPRO')!==false) $aapasspro=true; } } if (!$aapasspro) { if($wpg) $jot=array_merge($new,$ot,$wordp); else $jot=array_merge($markerdata,$ot); if (!$f = @fopen($file, 'w'))return aa_pp_debug("aa_pp_htaccess_file_init couldnt fopen {$file}"); $pr = join("\n", $jot); if (!@fwrite($f, $pr, strlen($pr))) return aa_pp_debug("aa_pp_htaccess_file_init couldnt fwrite {$file}"); fclose($f); } } return true; } /** * aa_pp_insert_mark() * * @param mixed $file * @param mixed $marker * @param mixed $insertion * @param mixed $backup * @return */ function aa_pp_insert_mark($file, $marker, $insertion, $backup = false) { global $aa_PP; $file = (@is_readable($file)) ? realpath(rtrim($file, '/')) : rtrim($file, '/'); if (!is_writable($file) && @!chmod($file, 0666) && !@touch($file)) return aa_pp_debug("aa_pp_insert_mark could not write, create, or touch {$file}"); if ($backup) $backedup = aa_pp_backup($file, $file . '-' . time()); aa_pp_notify("Inserting {$marker} array to {$file}"); $oldone = $foundit = false; $out = array(); if (!is_array($insertion) || (is_array($insertion) && count($insertion) < 1)) { aa_pp_notify("aa_pp_insert_mark1 called without array, creating one for {$marker}"); $my = array("# +{$marker}", "", "# -{$marker}"); } else { $my = array(); $my[] = "# +{$marker}"; foreach ($insertion as $l) $my[] = $l; $my[] = "# -{$marker}"; } if (!$f = @fopen($file, 'w')) return aa_pp_debug("aa_pp_insert_mark couldnt fopen {$file}"); $pr = join("\n", $my); if (!@fwrite($f, $pr, strlen($pr))) return aa_pp_debug("aa_pp_insert_mark couldnt fwrite {$file}"); fwrite($f, $out, strlen($out)); fclose($f); return true; } /** * aa_pp_insert_sids() * * @param mixed $file * @param mixed $marker * @param mixed $insertion * @param mixed $backup * @return */ function aa_pp_insert_sids($file, $marker, $insertion, $backup = false) { global $aa_PP; $file = (@is_readable($file)) ? realpath(rtrim($file, '/')) : rtrim($file, '/'); if (!is_writable($file) && @!chmod($file, 0666) && !@touch($file)) return aa_pp_debug("aa_pp_insert_sids could not write, create, or touch {$file}"); if ($backup) $backedup = aa_pp_backup($file, $file . '-' . time()); aa_pp_notify("Inserting {$marker} array to {$file}"); $foundit = false; $out = array(); if (!is_array($insertion) || (is_array($insertion) && count($insertion) < 1)) { aa_pp_notify("aa_pp_insert_sids called without array, creating one for {$marker}"); $my = array("# +SID {$marker}", "", "# -SID {$marker}"); } else { $my = array(); $my[] = "# +SID {$marker}"; foreach ($insertion as $l) $my[] = $l; $my[] = "# -SID {$marker}"; } if ($markerdata = @explode("\n", @implode('', @file($file)))) { if (!$f = @fopen($file, 'w')) return aa_pp_debug("aa_pp_insert_sids couldnt fopen {$file}"); $state = $s = $found = false; foreach ($markerdata as $line) { if (strpos($line, '-ASKAPACHE PASSPRO') !== false) { fwrite($f, $line); continue; } if (strpos($line, "# +APRO SIDS") !== false) { $s = true; fwrite($f, $line . "\n"); continue; } if (strpos($line, "# -APRO SIDS") !== false) { $s = false; if (!$found) { foreach ($my as $in) fwrite($f, $in . "\n"); } fwrite($f, $line . "\n"); continue; } if (!$s) fwrite($f, $line . "\n"); else { if (strpos($line, "# +SID {$marker}") !== false) $state = true; if (!$state)fwrite($f, $line . "\n"); if (strpos($line, "# -SID {$marker}") !== false) { $state = false; $found = true; foreach ($my as $in) fwrite($f, $in . "\n"); } } } fclose($f); } return true; } /** * aa_pp_mkdir() * * @param mixed $dirname * @return */ function aa_pp_mkdir($dirname) { @umask(0); aa_pp_notify("Creating directory {$dirname}"); if (is_dir($dirname) || @wp_mkdir_p($dirname)) return true; if (is_writable($dirname) && @wp_mkdir_p($dirname)) return true; return(@mkdir($dirname, 0777)) ? true : aa_pp_debug("failed to create directory {$dirname}"); } /** * aa_pp_unlink() * * @param mixed $f * @param mixed $backup * @return */ function aa_pp_unlink($f, $backup = false) { @umask(0); $f = (@is_readable($f)) ? realpath(rtrim($f, '/')) : rtrim($f, '/'); aa_pp_notify("Deleting {$f} in aa_pp_unlink"); if (!@file_exists($f)) return true; else { if ($backup) $backedup = aa_pp_backup($f, $f . '-' . time()); return(@chmod($f, 0777) && @unlink($f)) ? true : (@chmod(dirname($f), 0777) && @unlink($f)) ? true : aa_pp_debug("Failed to delete {$f} in aa_pp_unlink"); } } /** * aa_pp_backup() * * @param mixed $f * @param mixed $bf * @return */ function aa_pp_backup($f, $bf) { aa_pp_notify("aa_pp_backup: Backing up {$f} to {$bf}"); if (!@copy($f, $bf)) aa_pp_notify("Failed to backup {$f} to {$bf} using copy"); elseif (!@rename($f, $bf)) return aa_pp_debug("Failed to backup {$f} to {$bf} using rename"); return true; } /** * aa_pp_file_put_c() * * @param mixed $file * @param mixed $content * @param mixed $backup * @return */ function aa_pp_file_put_c($file, $content, $backup = false) { @umask(0); $f = (@is_readable($file)) ? realpath(rtrim($file, '/')) : rtrim($file, '/'); aa_pp_notify("Creating {$f} with aa_pp_file_put_c"); if (file_exists($f) && is_readable($f)) { if ($backup) $backedup = aa_pp_backup($f, $f . '-' . time()); aa_pp_unlink($f, 0); } if (aa_pp_checkfunction("file_put_contents")) return @file_put_contents($f, $content); if (!$fh = @fopen($f, 'wb')) return aa_pp_debug("couldnt fopen {$f}"); if (!@fwrite($fh, $content, strlen($content))) { fclose($fh); return aa_pp_debug("Failed to fwrite to {$f} in aa_pp_file_put_c"); } fclose($fh); return true; } /** * aa_pp_readfile() * * @param mixed $file * @param mixed $g * @return */ function aa_pp_readfile($file, $g = true) { @umask(0); $f = (@is_readable($file)) ? realpath(rtrim($file, '/')) : rtrim($file, '/'); aa_pp_notify("Reading {$f} in aa_pp_readfile"); if (!$fh = @fopen($f, 'rb')) return aa_pp_debug("couldnt fopen {$f}"); if (!$filecontent = @fread($fh, @filesize($f))) { fclose($fh); return aa_pp_debug("Failed to fread {$f} in aa_pp_readfile"); } fclose($fh); if (!$g) return $filecontent; else echo htmlspecialchars($filecontent); } /** * aa_pp_hashit() * * @param mixed $algorithm * @param string $user * @param string $pass * @param string $authname * @return */ function aa_pp_hashit($algorithm, $user = '', $pass = '', $authname = '') { global $aa_PP, $aa_SIDS; $hash = $tmp = ''; aa_pp_notify('Creating ' . $algorithm . ' Hash in aa_pp_hashit'); switch (strtoupper($algorithm)) { case 'DIGEST': $hash = $user . ":" . $authname . ":" . md5($user . ":" . $authname . ":" . $pass); break; case 'CRYPT': $seed = null; for ($i = 0; $i < 8; $i++) $seed .= substr('0123456789abcdef', rand(0, 15), 1); $hash = "{$user}:" . crypt($pass, "$" . $seed); break; case 'SHA1': $hash = $user . ':{SHA}' . base64_encode(pack("H*", sha1($pass))); break; case 'MD5': $saltt = substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"), 0, 8); $len = strlen($pass); $text = $pass . '$apr1$' . $saltt; $bin = pack("H32", md5($pass . $saltt . $pass)); for ($i = $len; $i > 0; $i -= 16) $text .= substr($bin, 0, min(16, $i)); for ($i = $len; $i > 0; $i >>= 1) $text .= ($i &1) ? chr(0) : $pass{0}; $bin = pack("H32", md5($text)); for ($i = 0; $i < 1000; $i++) { $new = ($i &1) ? $pass : $bin; if ($i % 3) $new .= $saltt; if ($i % 7) $new .= $pass; $new .= ($i &1) ? $bin : $pass; $bin = pack("H32", md5($new)); } for ($i = 0; $i < 5; $i++) { $k = $i + 6; $j = $i + 12; if ($j == 16) $j = 5; $tmp = $bin[$i] . $bin[$k] . $bin[$j] . $tmp; } $tmp = chr(0) . chr(0) . $bin[11] . $tmp; $tmp = strtr(strrev(substr(base64_encode($tmp), 2)), "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"); $hash = $user . ':$apr1$' . $saltt . '$' . $tmp; break; } return $hash; } /** * aa_pp_checkfunction() * * @param mixed $func * @return */ function aa_pp_checkfunction($func) { return(!function_exists($func) || @in_array($func, @explode(',', @ini_get('disable_functions')))) ? aa_pp_debug("{$func} disabled or not found") : true; } /** * aa_pp_run_tests() * * @return */ function aa_pp_run_tests() { global $wpdb, $wp_version, $aa_PP, $aa_SIDS; $home = get_option('siteurl'); $hu = str_replace($aa_PP['scheme'] . '://', '', $home); $uri = $aa_PP['scheme'] . '://' . rtrim(str_replace($aa_PP['root_path'], '', $hu), '/'); $test_root_path = $aa_PP['root_path'] . 'wp-content/' . basename($aa_PP['test_dir']) . '/'; $test_url_base = $uri . $test_root_path; $home_path = rtrim(get_home_path(), '/') . '/'; $basic_authuserfile = $aa_PP['test_dir'] . '/.htpasswda1'; $digest_authuserfile = $aa_PP['test_dir'] . '/.htpasswda2'; $img = pack("H*", "47494638396101000100910000000000ffffffffffff00000021f90405140002002c00000000010001000002025401003b"); $aok = '[ ] '; $fail = '[ ] '; $info = '[ ] '; $warn = '[ ] '; $m_s = '
Checks different software to make sure its compatible with this plugin.
The tests performed by this page are currently required to determine your servers capabilities to make sure we don't crash your server. The utmost care was taken to make these tests work for everyone running Apache, which is crazy hard because we are testing server configuration settings programmatically from a php binary without access to server configuration settings.
So we achieve this by modifying your server's .htaccess configuration file and then making special HTTP requests to your server which result in specific HTTP responses which tell us if the configuration changes failed or succeeded. The most widely allowed (by web hosts) and compatible 4+5 php function that provides access to sockets is fsockopen, so it is required. The next version will fallback to curl, but if your web host has disabled fsockopen you can bet you don't have curl.
If any of these checks fail this plugin will not work. Both your /.htaccess and /wp-admin/.htaccess files must be writable for this plugin, those are the only 2 files this plugin absolutely must be able to modify. If any of the other checks fail you will need to manually create a folder named askapache in your /wp-content/ folder and make it writable.
Your php installation should have all of these. The md5 is the only one absolutely required, otherwise I can't create the neccessary password files for you.
These tests determine with a high degree of accuracy whether or not your server is able to handle .htaccess files, and also checks for various Apache modules that extend the functionality of this plugin. The 2 modules you really want to have are mod_rewrite and mod_auth_digest. In future versions of this plugin, we will be utilizing the advanced security features of mod_security more and more, so if you don't have it, bug your web host about it non-stop ;)
sockit("{$test_url_base}test.gif") == 200) ? 1 : 0; $msg=($atest) ? $aok : $fail; echo $m_s . $msg . " .htaccess files allowed" . $m_e; $tester = new AskApache_Net; $aa_PP['mod_alias_support'] = $atest = ($tester->sockit("{$test_url_base}modaliastest") == 305) ? 1 : 0; $msg=($atest) ? $aok : $warn; echo $m_s . $msg . " mod_alias detection" . $m_e; $tester = new AskApache_Net; $aa_PP['mod_rewrite_support'] = $atest = ($tester->sockit("{$test_url_base}test.gif?modrewritetest=1") == 307) ? 1 : 0; $msg=($atest) ? $aok : $warn; echo $m_s . $msg . " mod_rewrite detection" . $m_e; $tester = new AskApache_Net; $aa_PP['mod_security_support'] = $atest = ($tester->sockit("{$test_url_base}test.gif?modsecuritytest") == 503) ? 1 : 0; $msg=($atest) ? $aok : $warn; echo $m_s . $msg . " mod_security detection" . $m_e; $tester = new AskApache_Net; $aa_PP['mod_auth_digest_support'] = $atest = ($tester->sockit("{$test_url_base}digest_check.gif") == 200) ? 1 : 0; $msg=($atest) ? $aok : $warn; echo $m_s . $msg . " mod_auth_digest detection" . $m_e; ?>Now we know the encryption and apache module capabilities of your site. This test literally logs in to your server using Digest Authenticationts, providing the ultimate answer as to if your server supports this scheme.
authtype = ''; $tester->sockit(str_replace('://', '://testDIGEST:testDIGEST@', $test_url_base) . 'digest_auth_test.gif'); $tester->authtype = 'Digest'; $rg = ($tester->sockit(str_replace('://', '://testDIGEST:testDIGEST@', $test_url_base) . 'digest_auth_test.gif') == 200) ? 1 : 0; $tester = new AskApache_Net; $rb = ($tester->sockit($test_url_base . 'digest_auth_test.gif') == 401) ? 1 : 0; $aa_PP['digest_support'] = $atest = ($rb && $rg) ? 1 : 0; $msg=($atest) ? $aok : $fail; echo $m_s . $msg . " Digest Authentication Attempt" . $m_e; } else echo $m_s . $msg . $fail . " Major bummer... you don't have mod_auth_digest! (included in apache since 1.1)" . $m_e; ?>Basic Authentication uses the .htpasswd file to store your encrypted password. These checks perform actual logins to your server using a different .htpasswd encryption each time.
sockit($test_url_base . 'basic_auth_test.gif') == 401) ? 1 : 0; if ($aa_PP['crypt_support'] != 0) { $tester = new AskApache_Net; $rg = ($tester->sockit(str_replace('://', '://testCRYPT:testCRYPT@', $test_url_base) . 'basic_auth_test.gif') == 200) ? 1 : 0; $aa_PP['crypt_support'] = $atest = ($rb && $rg) ? 1 : 0; $msg=($atest) ? $aok : $fail; echo $m_s . $msg . " Basic Authentication Attempt using Crypt Encryption" . $m_e; } if ($aa_PP['md5_support'] != 0) { $tester = new AskApache_Net; $rg = ($tester->sockit(str_replace('://', '://testMD5:testMD5@', $test_url_base) . 'basic_auth_test.gif') == 200) ? 1 : 0; $aa_PP['md5_support'] = $atest = ($rb && $rg) ? 1 : 0; $msg=($atest) ? $aok : $fail; echo $m_s . $msg . " Basic Authentication Attempt using MD5 Encryption" . $m_e; } if ($aa_PP['sha1_support'] != 0) { $tester = new AskApache_Net; $rg = ($tester->sockit(str_replace('://', '://testSHA1:testSHA1@', $test_url_base) . 'basic_auth_test.gif') == 200) ? 1 : 0; $aa_PP['sha1_support'] = $atest = ($rb && $rg) ? 1 : 0; $msg=($atest) ? $aok : $fail; echo $m_s . $msg . " Basic Authentication Attempt using SHA1 Encryption" . $m_e; } $aa_PP['basic_support'] = $atest = ($aa_PP['sha1_support'] != 0 || $aa_PP['md5_support'] != 0 || $aa_PP['crypt_support'] != 0) ? 1 : 0; $msg=($atest) ? $aok : $warn; echo $m_s . $msg . " Basic Authentication Access Scheme Supported" . $m_e; ?>Some information about your php.ini settings. The following settings may need to be tweaked. Likely they are fine.
'; print_r($this->request_headers); print_r($this->response_headers); echo ''; } return(int)$this->response_code; } /** * AskApache_Net::get_response_code() * * @return */ function get_response_code() { $status = null; preg_match('/HTTP.*([0-9]{3}).*/', $this->response_headers[0], $status); $this->response_code = $status[1]; return $this->response_code; } /** * AskApache_Net::get_response_headers() * * @return */ function get_response_headers() { return $this->response_headers; } /** * AskApache_Net::get_response_body() * * @return */ function get_response_body() { return $this->response; } /** * AskApache_Net::set_speed() * * @param mixed $speed * @return */ function set_speed($speed) { $this->_speed = round($speed * 1024); } /** * AskApache_Net::_connect() * * @return */ function _connect() { $fp = null; if (false === ($fp = @fsockopen($this->socket['ip'], $this->socket['port'], $errno, $errstr, $this->_fp_timeout)) || !is_resource($fp)) { switch ($errno) { case - 3: $err = "Socket creation failed"; break; case - 4: $err = "DNS lookup failure"; break; case - 5: $err = "Connection refused or timed out"; break; case 111: $err = "Connection refused"; break; case 113: $err = "No route to host"; break; case 110: $err = "Connection timed out"; break; case 104: $err = "Connection reset by client"; break; default: $err = "Connection failed"; break; } echo "Fsockopen failed! [{$errno}] {$err} ({$errstr})"; return false; } $this->_fp = $fp; return true; } /** * AskApache_Net::_disconnect() * * @return */ function _disconnect() { return(@fclose($this->_fp)); } /** * AskApache_Net::_rx() * * @return */ function _rx() { $buf = $response = ''; while (!feof($this->_fp) && $buf = @fread($this->_fp, $this->_speed)) { $response .= $buf; sleep(1); } $g = strpos($response, ACLF . ACLF); $headers = substr($response, 0, $g); $this->response = substr($response, $g, (strlen($response) - $g)); $this->response_headers = explode(ACLF, $headers); } /** * AskApache_Net::_tx() * * @param mixed $request * @return */ function _tx($request = null) { $g = false; $g = (!is_null($request)) ? @fwrite($this->_fp, $request, strlen($request)) : @fwrite($this->_fp, $this->request, strlen($this->request)); return(bool)$g; } /** * AskApache_Net::_build_sock() * * @param mixed $url * @return */ function _build_sock($url) { if (!$u_bits = parse_url($url)) return false; if (AA_PP_DEBUG) { echo '
'; print_r($u_bits); echo ''; } if (empty($u_bits['url'])) $u_bits['url'] = (!empty($this->socket['url'])) ? $this->socket['url'] : 'url'; if (empty($u_bits['method'])) $u_bits['method'] = (!empty($this->socket['method'])) ? $this->socket['method'] : 'GET'; if (empty($u_bits['protocol'])) $u_bits['protocol'] = (!empty($this->socket['protocol'])) ? $this->socket['protocol'] : '1.0'; if (empty($u_bits['host'])) $u_bits['host'] = (!empty($this->socket['host'])) ? $this->socket['host'] : $_SERVER['HTTP_HOST']; if (empty($u_bits['ip'])) $u_bits['ip'] = (!empty($this->socket['ip'])) ? $this->socket['ip'] : $this->_get_ip($u_bits['host']); if (empty($u_bits['scheme'])) $u_bits['scheme'] = (!empty($this->socket['scheme'])) ? $this->socket['scheme'] : 'http'; if (empty($u_bits['port'])) $u_bits['port'] = (!empty($this->socket['port'])) ? $this->socket['port'] : $_SERVER['SERVER_PORT']; if (empty($u_bits['path'])) $u_bits['path'] = (!empty($this->socket['path'])) ? $this->socket['path'] : '/'; if (empty($u_bits['ua'])) $u_bits['ua'] = (!empty($this->socket['ua'])) ? $this->socket['ua'] : 'Mozilla/5.0 (compatible; AskApache_Net/1.0; http://www.askapache.com)'; if (empty($u_bits['referer'])) $u_bits['referer'] = (!empty($this->socket['referer'])) ? $this->socket['referer'] : 'http://www.askapache.com'; if (!empty($u_bits['user'])) $this->socket['user'] = $u_bits['user']; else $u_bits['user'] = (!empty($this->socket['user'])) ? $this->socket['user'] : ''; if (!empty($u_bits['pass'])) $this->socket['pass'] = $u_bits['pass']; else $u_bits['pass'] = (!empty($this->socket['pass'])) ? $this->socket['pass'] : ''; if (empty($u_bits['fragment'])) $u_bits['fragment'] = (!empty($this->socket['fragment'])) ? $this->socket['fragment'] : ''; if (!empty($u_bits['query'])) $u_bits['path'] .= '?' . $u_bits['query']; else $u_bits['path'] .= (!empty($this->socket['query'])) ? '?' . $this->socket['query'] : ''; if (AA_PP_DEBUG) { echo '
'; print_r($u_bits); echo ''; } $this->socket = $u_bits; return true; } /** * AskApache_Net::_build_request() * * @return */ function _build_request() { $_request_headers = array(); $_request_headers[] = $this->socket['method'] . " " . $this->socket['path'] . " HTTP/" . $this->socket['protocol']; $_request_headers[] = "Host: " . $this->socket['host']; $_request_headers[] = "User-Agent: " . $this->socket['ua']; $_request_headers[] = 'Accept: application/xhtml+xml,text/html;q=0.9,*/*;q=0.5'; $_request_headers[] = 'Accept-Language: en-us,en;q=0.5'; $_request_headers[] = 'Accept-Encoding: none'; $_request_headers[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'; $_request_headers[] = 'Referer: ' . $this->socket['referer']; if (!empty($this->socket['user']) && !empty($this->socket['pass'])) { if ($this->authtype == 'Basic') $_request_headers[] = 'Authorization: Basic ' . base64_encode($this->socket['user'] . ":" . $this->socket['pass']); elseif ($this->authtype == 'Digest') $_request_headers[] = $this->getDigest(); } $this->request_headers = $_request_headers; $this->request = join(ACLF, $_request_headers) . ACLF . ACLF; return true; } /** * AskApache_Net::_get_ip() * * @param mixed $host * @return */ function _get_ip($host) { $hostip = @gethostbyname($host); $ip = ($hostip == $host) ? $host : long2ip(ip2long($hostip)); return $ip; } /** * AskApache_Net::_set_sock_time() * * @return */ function _set_sock_time() { @ini_set("default_socket_timeout", $this->_read_timeout); if (function_exists("socket_set_timeout")) @socket_set_timeout($this->_fp, $this->_read_timeout); elseif (function_exists("stream_set_timeout")) @stream_set_timeout($this->_fp, $this->_read_timeout); return true; } /** * AskApache_Net::getDigest() * * @return */ function getDigest() { foreach ($this->response_headers as $num => $header) { if (preg_match('/WWW-Authenticate: Digest/i', $header)) $this->dh = $dh = substr($header, 25); } $dh = $this->dh; $this->socket['protocol'] = '1.1'; $myDigest = $this->Digests; if (preg_match('/realm="([^"]*?)"/i', $dh, $match)) { if (isset($match[1]) && !empty($match[1])) $myDigest['realm'] = $match[1]; } if (preg_match('/nonce="([^"]*?)"/i', $dh, $match)) { if (isset($match[1]) && !empty($match[1])) $myDigest['nonce'] = $match[1]; } if (preg_match('/cnonce="([^"]*?)"/i', $dh, $match)) { if (isset($match[1]) && !empty($match[1])) $myDigest['cnonce'] = $match[1]; } if (preg_match('/algorithm=(MD5|MD5-sess),/i', $dh, $match)) { if (isset($match[1]) && !empty($match[1])) $myDigest['algorithm'] = $match[1]; } if (preg_match('/domain="([^"]*?)"/i', $dh, $match)) { if (isset($match[1]) && !empty($match[1])) $myDigest['domain'] = $match[1]; } if (preg_match('/opaque="([^"]*?)"/i', $dh, $match)) { if (isset($match[1]) && !empty($match[1])) $myDigest['opaque'] = $match[1]; } if (preg_match('/qop=[\'\"]?(auth|auth-int)[\'\"]?/i', $dh, $match)) { if (isset($match[1]) && !empty($match[1])) $myDigest['qop'] = $match[1]; } if (preg_match('/nc=([0-9]*?)/i', $dh, $match)) { if (isset($match[1]) && !empty($match[1])) $myDigest['nc'] = $match[1]; } $myDigest['uri'] = $this->socket['path']; $myDigest['A1'] = md5($this->socket['user'] . ':' . $myDigest['realm'] . ':' . $this->socket['pass']); $myDigest['A2'] = md5($this->socket['method'] . ':' . $this->socket['path']); $myDigest['response'] = md5($myDigest['A1'] . ':' . $myDigest['nonce'] . ':' . $myDigest['nc'] . ':' . $myDigest['cnonce'] . ':' . $myDigest['qop'] . ':' . $myDigest['A2']); $this->Digests = $myDigest; $ah = 'Authorization: Digest username="' . $this->socket['user'] . '", realm="' . $myDigest['realm'] . '", nonce="' . $myDigest['nonce'] . '", uri="' . $myDigest['uri'] . '"'; $ah .= ', algorithm=' . $myDigest['algorithm'] . ', response="' . $myDigest['response'] . '"'; $ah .= ', qop=' . $myDigest['qop'] . ', nc=' . $myDigest['nc']; if (!empty($myDigest['cnonce'])) $ah .= ', cnonce="' . $myDigest['cnonce'] . '"'; if (!empty($myDigest['opaque'])) $ah .= ', opaque="' . $myDigest['opaque'] . '"'; if (AA_PP_DEBUG) { echo '
'; echo $ah . "\n"; print_r($myDigest); echo ''; } return $ah; } } } ?>