Example #1
0
 public function __construct($username, $password)
 {
     // 为本次请求生成一个随机 IP 地址,避免同 IP 上的猪队友
     $this->ip = join('.', [220, mt_rand(50, 250), mt_rand(50, 250), mt_rand(50, 250)]);
     // 初始化 CURL 共享载体
     if (is_null($this->curls)) {
         $this->curls = curl_share_init();
         curl_share_setopt($this->curls, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
     }
     // 第一步:获取登录页面代码,提取登录用的 ONCE 参数
     $html = $this->request('/signin', '/');
     preg_match('@<input type="hidden" value="(.+?)" name="once" />@', $html, $match);
     $once = $match[1];
     // 第二步:请求登录,获取当前金币数量
     $data = ['u' => $username, 'p' => $password, 'once' => $once, 'next' => '/'];
     $html = $this->request('/signin', '/signin', $data);
     $balance['before'] = $this->balance($html) or die('登录失败!');
     // 第三步:进入每日任务页面,提取金币领取地址
     $html = $this->request('/mission/daily', '/');
     preg_match('@/mission/daily/redeem\\?once=\\d+@', $html, $match) or die('当日已经领取');
     $url = $match[0];
     // 第四步:领取每日奖励,获取新的金币数量
     $html = $this->request($url, '/mission/daily');
     $balance['after'] = $this->balance($html);
     // 判断领取是否成功
     stripos($html, '每日登录奖励已领取') or die('领取金币失败');
     // 连续登录天数
     preg_match('@已连续登录 \\d+ 天@', $html, $match);
     $days = $match[0];
     // 输出结果
     echo $days . ',领取 ' . ($balance['after'] - $balance['before']) . ' 金币,账户余额 ' . $balance['after'] . ' 金币';
 }
Example #2
0
 /**
  * @codeCoverageIgnore
  */
 public static function setup($ch)
 {
     static $sh;
     if (!function_exists('curl_share_init')) {
         return;
     }
     if (!$sh) {
         $sh = curl_share_init();
         curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
         curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
         curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
     }
     curl_setopt($ch, CURLOPT_SHARE, $sh);
 }
 /**
  * @param Package\PackageInterface[] $packages
  * @param array $pluginConfig
  * @return void
  */
 public function download(array $packages, array $pluginConfig)
 {
     $mh = curl_multi_init();
     $unused = array();
     $maxConns = $pluginConfig['maxConnections'];
     for ($i = 0; $i < $maxConns; ++$i) {
         $unused[] = curl_init();
     }
     /// @codeCoverageIgnoreStart
     if (function_exists('curl_share_init')) {
         $sh = curl_share_init();
         curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
         foreach ($unused as $ch) {
             curl_setopt($ch, CURLOPT_SHARE, $sh);
         }
     }
     if (function_exists('curl_multi_setopt')) {
         if ($pluginConfig['pipeline']) {
             curl_multi_setopt($mh, CURLMOPT_PIPELINING, true);
         }
     }
     /// @codeCoverageIgnoreEnd
     $cachedir = rtrim($this->config->get('cache-files-dir'), '\\/');
     $chFpMap = array();
     $running = 0;
     //ref type
     $remains = 0;
     //ref type
     $this->totalCnt = count($packages);
     $this->successCnt = 0;
     $this->failureCnt = 0;
     $this->io->write("    Prefetch start: <comment>success: {$this->successCnt}, failure: {$this->failureCnt}, total: {$this->totalCnt}</comment>");
     do {
         // prepare curl resources
         while ($unused && $packages) {
             $package = array_pop($packages);
             $filepath = $cachedir . DIRECTORY_SEPARATOR . static::getCacheKey($package);
             if (file_exists($filepath)) {
                 ++$this->successCnt;
                 continue;
             }
             $ch = array_pop($unused);
             // make file resource
             $fp = CurlRemoteFilesystem::createFile($filepath);
             $chFpMap[(int) $ch] = compact('fp', 'filepath');
             // make url
             $url = $package->getDistUrl();
             $request = new Aspects\HttpGetRequest(parse_url($url, PHP_URL_HOST), $url, $this->io);
             $request->verbose = $pluginConfig['verbose'];
             if (in_array($package->getName(), $pluginConfig['privatePackages'])) {
                 $request->maybePublic = false;
             } else {
                 $request->maybePublic = preg_match('%^(?:https|git)://github\\.com%', $package->getSourceUrl());
             }
             $onPreDownload = Factory::getPreEvent($request);
             $onPreDownload->notify();
             $opts = $request->getCurlOpts();
             unset($opts[CURLOPT_ENCODING]);
             unset($opts[CURLOPT_USERPWD]);
             curl_setopt_array($ch, $opts);
             curl_setopt($ch, CURLOPT_FILE, $fp);
             curl_multi_add_handle($mh, $ch);
         }
         // start multi download
         do {
             $stat = curl_multi_exec($mh, $running);
         } while ($stat === CURLM_CALL_MULTI_PERFORM);
         // wait for any event
         do {
             switch (curl_multi_select($mh, 5)) {
                 case -1:
                     usleep(10);
                     do {
                         $stat = curl_multi_exec($mh, $running);
                     } while ($stat === CURLM_CALL_MULTI_PERFORM);
                     continue 2;
                 case 0:
                     continue 2;
                 default:
                     do {
                         $stat = curl_multi_exec($mh, $running);
                     } while ($stat === CURLM_CALL_MULTI_PERFORM);
                     do {
                         if ($raised = curl_multi_info_read($mh, $remains)) {
                             $ch = $raised['handle'];
                             $errno = curl_errno($ch);
                             $info = curl_getinfo($ch);
                             curl_setopt($ch, CURLOPT_FILE, STDOUT);
                             $index = (int) $ch;
                             $fileinfo = $chFpMap[$index];
                             unset($chFpMap[$index]);
                             $fp = $fileinfo['fp'];
                             $filepath = $fileinfo['filepath'];
                             fclose($fp);
                             if (CURLE_OK === $errno && 200 === $info['http_code']) {
                                 ++$this->successCnt;
                             } else {
                                 ++$this->failureCnt;
                                 unlink($filepath);
                             }
                             $this->io->write($this->makeDownloadingText($info['url']));
                             curl_multi_remove_handle($mh, $ch);
                             $unused[] = $ch;
                         }
                     } while ($remains);
                     if ($packages) {
                         break 2;
                     }
             }
         } while ($running);
     } while ($packages);
     $this->io->write("    Finished: <comment>success: {$this->successCnt}, failure: {$this->failureCnt}, total: {$this->totalCnt}</comment>");
     foreach ($unused as $ch) {
         curl_close($ch);
     }
     curl_multi_close($mh);
 }
Example #4
0
 protected function shareInit()
 {
     $this->shareDescriptor = curl_share_init();
     curl_share_setopt($this->shareDescriptor, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
 }
Example #5
0
 /**
  * @see curl_share_setopt
  * @link http://php.net/manual/en/function.curl-share-setopt.php
  * @param $option
  * @param $value
  */
 public function setShareOptions($option, $value)
 {
     if (!isset($this->sh)) {
         $this->sh = curl_share_init();
         $this->setCurlOption(array(CURLOPT_SHARE => $this->sh));
     }
     curl_share_setopt($this->sh, $option, $value);
 }
Example #6
0
 /**
  * @see curl_share_setopt
  *
  * @param int $opt
  * @param mixed $val
  * @return boolean
  */
 public function setOpt($opt, $val)
 {
     return curl_share_setopt($this->handle, $opt, $val);
 }
Example #7
0
 public function __construct()
 {
     $this->resource = curl_share_init();
     curl_share_setopt($this->resource, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
 }
 /**
  * @param Package\PackageInterface[] $packages
  * @param array $pluginConfig
  * @return void
  */
 public function download(array $packages, array $pluginConfig)
 {
     $mh = curl_multi_init();
     $unused = array();
     $maxConns = $pluginConfig['maxConnections'];
     for ($i = 0; $i < $maxConns; ++$i) {
         $unused[] = curl_init();
     }
     // @codeCoverageIgnoreStart
     if (function_exists('curl_share_init')) {
         $sh = curl_share_init();
         curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
         foreach ($unused as $ch) {
             curl_setopt($ch, CURLOPT_SHARE, $sh);
         }
     }
     if (function_exists('curl_multi_setopt')) {
         if ($pluginConfig['pipeline']) {
             curl_multi_setopt($mh, CURLMOPT_PIPELINING, true);
         }
     }
     // @codeCoverageIgnoreEnd
     $cachedir = rtrim($this->config->get('cache-files-dir'), '\\/');
     $chFpMap = array();
     $running = 0;
     //ref type
     $remains = 0;
     //ref type
     $this->totalCnt = count($packages);
     $this->successCnt = 0;
     $this->failureCnt = 0;
     $this->io->write("    Prefetch start: <comment>success: {$this->successCnt}, failure: {$this->failureCnt}, total: {$this->totalCnt}</comment>");
     EVENTLOOP:
     // prepare curl resources
     while (count($unused) > 0 && count($packages) > 0) {
         $package = array_pop($packages);
         $filepath = $cachedir . DIRECTORY_SEPARATOR . static::getCacheKey($package);
         if (file_exists($filepath)) {
             ++$this->successCnt;
             continue;
         }
         $ch = array_pop($unused);
         // make file resource
         $chFpMap[(int) $ch] = $outputFile = new OutputFile($filepath);
         // make url
         $url = $package->getDistUrls();
         if (count($url) > 0) {
             $url = $url[0];
         } else {
             $url = $package->getDistUrl();
         }
         $host = parse_url($url, PHP_URL_HOST) ?: '';
         $request = new Aspects\HttpGetRequest($host, $url, $this->io);
         $request->verbose = $pluginConfig['verbose'];
         if (in_array($package->getName(), $pluginConfig['privatePackages'])) {
             $request->maybePublic = false;
         } else {
             $request->maybePublic = (bool) preg_match('%^(?:https|git)://github\\.com%', $package->getSourceUrl());
         }
         $onPreDownload = Factory::getPreEvent($request);
         $onPreDownload->notify();
         $opts = $request->getCurlOpts();
         if ($pluginConfig['insecure']) {
             $opts[CURLOPT_SSL_VERIFYPEER] = false;
         }
         if (!empty($pluginConfig['userAgent'])) {
             $opts[CURLOPT_USERAGENT] = $pluginConfig['userAgent'];
         }
         if (!empty($pluginConfig['capath'])) {
             $opts[CURLOPT_CAPATH] = $pluginConfig['capath'];
         }
         unset($opts[CURLOPT_ENCODING]);
         unset($opts[CURLOPT_USERPWD]);
         // ParallelDownloader doesn't support private packages.
         curl_setopt_array($ch, $opts);
         curl_setopt($ch, CURLOPT_FILE, $outputFile->getPointer());
         curl_multi_add_handle($mh, $ch);
     }
     // wait for any event
     do {
         $runningBefore = $running;
         while (CURLM_CALL_MULTI_PERFORM === curl_multi_exec($mh, $running)) {
         }
         SELECT:
         $eventCount = curl_multi_select($mh, 5);
         if ($eventCount === -1) {
             usleep(200 * 1000);
             continue;
         }
         if ($eventCount === 0) {
             continue;
         }
         while (CURLM_CALL_MULTI_PERFORM === curl_multi_exec($mh, $running)) {
         }
         if ($running > 0 && $running === $runningBefore) {
             goto SELECT;
         }
         do {
             if ($raised = curl_multi_info_read($mh, $remains)) {
                 $ch = $raised['handle'];
                 $errno = curl_errno($ch);
                 $info = curl_getinfo($ch);
                 curl_setopt($ch, CURLOPT_FILE, STDOUT);
                 $index = (int) $ch;
                 $outputFile = $chFpMap[$index];
                 unset($chFpMap[$index]);
                 if (CURLE_OK === $errno && 200 === $info['http_code']) {
                     ++$this->successCnt;
                 } else {
                     ++$this->failureCnt;
                     $outputFile->setFailure();
                 }
                 unset($outputFile);
                 $this->io->write($this->makeDownloadingText($info['url']));
                 curl_multi_remove_handle($mh, $ch);
                 $unused[] = $ch;
             }
         } while ($remains > 0);
         if (count($packages) > 0) {
             goto EVENTLOOP;
         }
     } while ($running > 0);
     $this->io->write("    Finished: <comment>success: {$this->successCnt}, failure: {$this->failureCnt}, total: {$this->totalCnt}</comment>");
     foreach ($unused as $ch) {
         curl_close($ch);
     }
     curl_multi_close($mh);
 }
Example #9
0
 /**
  * Set an option for the cURL share handle
  *
  * @param integer $option
  * @param mixed $value
  * @return boolean
  */
 public function setopt($option, $value)
 {
     return curl_share_setopt($this->_sh, $option, $value);
 }
 /**
  * Soap __doRequest() Method with CURL Implementation
  *
  * @param string $request
  * @param string $location
  * @param string $action
  * @param int    $version
  * @param int    $one_way
  *
  * @return string
  * @throws SoapFault
  */
 public function __doRequest($request, $location, $action, $version, $one_way = 0)
 {
     $action = static::$action;
     $actionCommand = static::$action;
     $actionMethod = $action;
     // print xml for debugging testing
     if ($actionCommand != static::GET_RESULT && self::$printSoapRequest) {
         // debug the request here
         echo $this->prettyXml($request);
         if (self::$exitAfterPrint) {
             exit;
         }
         self::$printSoapRequest = false;
         self::$exitAfterPrint = false;
     }
     // some .NET Servers only accept action method with ns url!! uncomment it if you get error wrong command
     // $actionMethod  = str_ireplace(['http://tempuri.org/IFlightAPI/', 'https://tempuri.org/IFlightAPI/'], '', $action);
     /** return the xml response as its coming from normal soap call */
     if ($actionCommand == static::GET_RESULT && static::$xmlResponse) {
         return static::$xmlResponse;
     } else {
     }
     /** return the xml response as its coming from normal soap call */
     if ($action == static::GET_RESULT && static::$xmlResponse) {
         return static::$xmlResponse;
     }
     $soapResponses =& static::$soapResponses;
     $soapRequests =& static::$soapRequests;
     /** @var $id string represent hashId of each request based on the request body to avoid multiple calls for the same request if exists */
     $id = sha1($location . $request);
     /** if curl is not enabled use parent::__doRequest */
     if (!in_array('curl', get_loaded_extensions())) {
         if (isset($soapResponses[$id])) {
             unset($soapResponses[$id]);
             return parent::__doRequest($request, $location, $action, $version, $one_way = 0);
         }
         $soapRequests[$id] = true;
         return "";
     }
     /** return response if soap method called for second time with same parameters */
     if (isset($soapResponses[$id])) {
         $data = $soapResponses[$id];
         unset($soapResponses[$id]);
         if ($data instanceof SoapFault) {
             throw $data;
         }
         return $data;
     }
     /** @var $headers array of headers to be sent with request */
     $headers = ['Content-type: text/xml', 'charset=utf-8', "Accept: text/xml", 'SOAPAction: "' . $action . '"', "Content-length: " . strlen($request)];
     // ssl connection sharing
     if (empty(static::$sharedCurlData[$location])) {
         $shOpt = curl_share_init();
         curl_share_setopt($shOpt, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
         curl_share_setopt($shOpt, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
         curl_share_setopt($shOpt, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
         static::$sharedCurlData[$location] = $shOpt;
     }
     $sh = static::$sharedCurlData[$location];
     $ch = curl_init();
     /** CURL_OPTIONS  */
     curl_setopt($ch, CURLOPT_URL, $location);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_ENCODING, "");
     curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
     curl_setopt($ch, CURLOPT_TIMEOUT, 50);
     curl_setopt($ch, CURLOPT_TIMECONDITION, 50);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_VERBOSE, static::$debug);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
     curl_setopt($ch, CURLOPT_SHARE, $sh);
     $soapRequests[$id] = $ch;
     static::$requestIds[$id] = $id;
     static::$actions[$id] = $actionMethod;
     static::$requestXml[$id] = $request;
     static::$lastRequestId = $id;
     return "";
 }
Example #11
0
require_once __DIR__ . '/credentials.incl.php';
date_default_timezone_set('UTC');
if (count($argv) < 3) {
    echo "Run curseupdater.sh\n";
    exit(1);
}
fwrite(STDERR, "Starting WoWI Updater..\n");
$zipPath = $argv[1];
$version = $argv[2];
if (!file_exists($zipPath)) {
    fwrite(STDERR, 'File does not exist: ' . $zipPath . "\n");
    exit(1);
}
$sh = curl_share_init();
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
$curl = curl_init();
curl_setopt_array($curl, [CURLOPT_SHARE => $sh, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_TIMEOUT => 10, CURLOPT_POSTFIELDS => ['vb_login_username' => WOWI_USERNAME, 'vb_login_password' => WOWI_PASSWORD, 'do' => 'login', 'cookieuser' => 1], CURLOPT_URL => 'https://secure.wowinterface.com/forums/login.php']);
curl_exec($curl);
// sets login cookies
curl_close($curl);
$curl = curl_init();
curl_setopt_array($curl, [CURLOPT_SHARE => $sh, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 10, CURLOPT_URL => 'http://www.wowinterface.com/downloads/editfile.php?id=19662']);
$html = curl_exec($curl);
curl_close($curl);
if (preg_match('/name="securitytoken" value="([^"]+)"/', $html, $res) == 0) {
    fwrite(STDERR, "Could not get security token\n");
    curl_share_close($sh);
    exit(1);
}
$securityToken = $res[1];