static function getLoginUser($field = '', $auth = '', $saltKey = '') { if (!self::$userInfo['uid']) { if (!$auth && !$saltKey && myCookie('auth') && myCookie('saltkey')) { $auth = myCookie('auth'); $saltKey = myCookie('saltkey'); } if ($auth) { $auth = explode("\t", getDecode($auth, self::getAuthKey($saltKey))); list($uid, $aid) = empty($auth) || count($auth) < 2 ? array(0, 0) : $auth; if ($uid) { self::$userInfo = self::getUserById($uid); if (!self::$userInfo) { showError('抱歉,你的帐号存在异常,无法登陆'); } switch (self::$userInfo['status']) { case 0: //异常 self::setUserLoginOut(); showError('抱歉,你的帐号存在异常,无法登陆'); break; case 1: //帐号正常 break; case 2: //冻结 self::setUserLoginOut(); showError('抱歉,你的帐号已被冻结,无法登陆'); break; case 3: //取消关注 self::setUserLoginOut(); break; default: //未知情况 self::setUserLoginOut(); showError('抱歉,你的帐号存在异常,无法登陆'); } //附加登陆来源 self::$userInfo['loginFrom'] = isHave($auth[2]) ? $auth[2] : 'wx'; if (getUserAgent() == 'weixin' && self::$userInfo['loginFrom'] != 'wx') { self::setUserLoginOut(); } } } } return $field && isset(self::$userInfo[$field]) ? self::$userInfo[$field] : self::$userInfo; }
function file_post_contents($url, $array) { $postdata = http_build_query($array); $eol = "\r\n"; $header = array('User-Agent: ' . getUserAgent(), "Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($postdata)); $opts = array('http' => array('method' => 'POST', 'header' => implode($eol, $header), 'content' => $postdata)); $context = stream_context_create($opts); return file_get_contents($url, false, $context); }
function getBrowser() { $agent = getUserAgent(); $browser = 'unknown'; $version = 'unknown'; if ($agent) { if (strpos($agent, 'MSIE') !== false || strpos($agent, 'rv:11.0')) { //ie11判断 $browser = 'ie'; } else { if (strpos($agent, 'Firefox') !== false) { $browser = 'firefox'; } else { if (strpos($agent, 'Chrome') !== false) { $browser = 'chrome'; } else { if (strpos($agent, 'Opera') !== false) { $browser = 'opera'; } else { if (strpos($agent, 'Chrome') == false && strpos($agent, 'Safari') !== false) { $browser = 'safari'; } } } } } if (preg_match('/MSIE\\s(\\d+)\\..*/i', $agent, $regs)) { $version = $regs[1]; } elseif (preg_match('/FireFox\\/(\\d+)\\..*/i', $agent, $regs)) { $version = $regs[1]; } elseif (preg_match('/Opera[\\s|\\/](\\d+)\\..*/i', $agent, $regs)) { $version = $regs[1]; } elseif (preg_match('/Chrome\\/(\\d+)\\..*/i', $agent, $regs)) { $version = $regs[1]; } elseif (strpos($agent, 'Chrome') == false && preg_match('/Safari\\/(\\d+)\\..*$/i', $agent, $regs)) { $version = $regs[1]; } } return array('browser' => ucfirst($browser), 'version' => $version); }
function isMaple() { return preg_match('/Maple/', getUserAgent()) == 1; }
<?php /* * MWS Admin v2.1 - Wizard Demo PHP * This file is part of MWS Admin, an Admin template build for sale at ThemeForest. * All copyright to this file is hold by Mairel Theafila <*****@*****.**> a.k.a nagaemas on ThemeForest. * Last Updated: * December 08, 2012 * */ function getUserAgent() { return isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null; } function getUserHostAddress() { return isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1'; } $data = $_POST['wizard']; echo print_r($data, true) . "\nIP Address:\n\t" . getUserHostAddress() . "\n\nBrowser:\n\t" . getUserAgent();
function curl($urls, $settings = array()) { $return = array(); if (is_string($urls)) { $urls = array($urls); } $urls = array_values(array_unique($urls)); $modifyContent = isset($settings['modifyContent']) ? $settings['modifyContent'] : null; $retry = isset($settings['retry']) ? $settings['retry'] : 1; $verbose = isset($settings['verbose']) ? intval($settings['verbose']) : false; $threads = isset($settings['threads']) ? intval($settings['threads']) : 5; if ($threads <= 0) { $threads = 1; } if ($threads >= 100) { $threads = 100; } $sleep = isset($settings['sleep']) ? $settings['sleep'] : 5; $delay = isset($settings['delay']) ? $settings['delay'] : 0; $format = isset($settings['format']) ? $settings['format'] : (count($urls) > 1 ? 'array' : 'simple'); $checker = isset($settings['checker']) ? $settings['checker'] : false; $ignoreErrors = isset($settings['ignoreErrors']) ? $settings['ignoreErrors'] : array(); if (is_numeric($checker)) { $checker = array(intval($checker)); } if (!is_callable($checker) and is_array($_ = $checker)) { $checker = function ($url, $ch) use($_) { $info = curl_getinfo($ch); $code = intval($info['http_code']); return in_array($code, array_map('intval', $_)); }; } $handleReturn = function (&$return) use($checker, $verbose, $modifyContent, $ignoreErrors) { $fail = array(); foreach (array_keys($return) as $key) { $value =& $return[$key]; if (!is_array($value) or !array_key_exists('ch', $value) or !is_resource($value['ch'])) { continue; } $ch = $value['ch']; $error = curl_error($ch); $errno = curl_errno($ch); if ($error and in_array($errno, $ignoreErrors)) { $error = ""; } if ($error or $checker and !$checker($value['url'], $ch)) { unset($return[$key]); curl_close($ch); $fail[$key] = $value['url']; if ($verbose) { _log("{$value['url']} .. ERR!" . ($error ? " ({$error})" : ''), E_USER_WARNING); } continue; } $info = curl_getinfo($ch); $content = curl_multi_getcontent($ch); $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); if ($verbose and $value['url'] === $info['url']) { _log("{$value['url']} .. OK!"); } elseif ($verbose) { _log("{$value['url']} .. {$info['url']} .. OK!"); } if (intval($headerSize) > 0) { $header = substr($content, 0, $headerSize); $content = substr($content, $headerSize); $headers = array(); $_ = explode("\r\n\r\n", $header); for ($index = 0; $index < count($_) - 1; $index++) { foreach (explode("\r\n", $_[$index]) as $i => $line) { if ($i === 0) { $line = explode(' ', $line); $headers[$index]['http-code'] = $line[1]; } else { $line = explode(': ', $line, 2); if (count($line) != 2) { continue; } list($k, $v) = $line; $headers[$index][strtolower($k)] = $v; } } } } else { $header = ''; } $return[$key]['content'] = $content; $return[$key]['header'] = $header; if ($modifyContent and is_callable($modifyContent)) { $return[$key]['content'] = $modifyContent($value['url'], $content); } $return[$key]['info'] = $info; if (isset($headers)) { $return[$key]['headers'] = $headers; } unset($value['ch']); curl_close($ch); } return $fail; }; $getCH = function ($url, $settings) { $ch = curl_init(); $opts = array(); $setopt = function ($arr) use(&$ch, &$opts) { $opts = array_replace($opts, $arr); curl_setopt_array($ch, $arr); }; $setopt(array(CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_ENCODING => "", CURLOPT_USERAGENT => getUserAgent(), CURLOPT_AUTOREFERER => true, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 10, CURLOPT_MAXREDIRS => 5, CURLOPT_SSL_VERIFYPEER => 0, CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_HEADER => true)); $acceptCallable = array(CURLOPT_HEADERFUNCTION, CURLOPT_PROGRESSFUNCTION, CURLOPT_READFUNCTION, CURLOPT_WRITEFUNCTION); if (defined("CURLOPT_PASSWDFUNCTION")) { $acceptCallable[] = CURLOPT_PASSWDFUNCTION; } if (is_string($url) and host($url)) { $setopt(array(CURLOPT_URL => $url)); } $constants = array_keys(get_defined_constants()); $constantsStrings = array_values(array_filter($constants, function ($constant) { return strpos($constant, 'CURLOPT_') === 0; })); $constantsValues = array_map('constant', $constantsStrings); foreach ($settings as $key => $value) { if (in_array($key, $constantsStrings)) { $key = constant($key); } if (!in_array($key, $constantsValues)) { continue; } if (is_callable($value) and !in_array($key, $acceptCallable)) { $value = $value($url); } $setopt(array($key => $value)); } if (host($opts[CURLOPT_URL])) { return $ch; } return null; }; do { $fails = array(); while ($urls) { if ($threads == 1 or count($urls) == 1) { $single = curl_init(); $multi = null; } else { $single = null; $multi = curl_multi_init(); } for ($i = 0; $i < $threads and $urls; $i++) { $key = key($urls); $ch = $getCH($urls[$key], $settings); if (is_null($ch)) { unset($urls[$key]); $i--; continue; } $return[$key] = array('url' => $urls[$key], 'ch' => $ch); if ($multi) { curl_multi_add_handle($multi, $ch); } else { $single = $ch; } unset($urls[$key]); } if ($multi) { do { curl_multi_exec($multi, $running); usleep(200000); } while ($running > 0); curl_multi_close($multi); } else { curl_exec($single); } $fails[] = $handleReturn($return); if ($urls and $delay) { sleep($delay); } } foreach ($fails as $fail) { foreach ($fail as $k => $v) { $urls[$k] = $v; } } } while ($urls and $retry-- and sleep($sleep) === 0); if ($format === "simple") { return implode("\n\n", array_values(array_column($return, 'content'))); } elseif ($format === "array") { return array_column($return, 'content', 'url'); } elseif ($format === "complex") { return array_column($return, null, 'url'); } else { return; } }
public function testCurl() { $ua = getUserAgent(); $content = curl('http://ejz.ru/ua', array(CURLOPT_USERAGENT => $ua)); // preg_match("~<textarea[^>]*>([^<]+)</textarea>~i", $content, $match); $_ua = trim($content); $this->assertTrue($ua === $_ua); // $result = curl($_ = 'http://ejz.ru/', array('format' => 'array')); $this->assertTrue(isset($result[$_])); }