/** * リソースリクエスト送信 * * @param string $method リクエストメソッド(CRUD) * @param string $uri URI(クエリー付き) * @param array $values 引数 * * @return mixed BEAR_Ro | array */ public function send($method, $uri, array $values = array()) { $socket = new Net_Socket(); // 接続を確立する $socket->connect($this->_ip, $this->_port, true, 30); $uriWithVal = self::_mergeQueryAndArray($uri, $values); // 改行を含むデータを送信する $request = "{$method} {$uriWithVal}"; $socket->writeLine($request); // 改行が現れるまでデータを受信する $code = $socket->readLine(); // アトリビュート $header = $socket->readLine(); while ($header) { $headers[] = $header; $header = $socket->readLine(); } $body = $socket->readLine(); if ($this->_returnVo && class_exists('BEAR_Ro', false)) { $ro = BEAR::factory('BEAR_Ro'); /* @var $ro BEAR_Ro */ $ro->setBody($body); $ro->setHeaders($headers); $ro->setCode($code); $result = $ro; } else { $result = array('code' => $code, 'headers' => $headers, 'body' => $body); } return $result; }
function save($curpass, $passwd) { $rcmail = rcmail::get_instance(); $vpopmaild = new Net_Socket(); $host = $rcmail->config->get('password_vpopmaild_host'); $port = $rcmail->config->get('password_vpopmaild_port'); $result = $vpopmaild->connect($host, $port, null); if (is_a($result, 'PEAR_Error')) { return PASSWORD_CONNECT_ERROR; } $vpopmaild->setTimeout($rcmail->config->get('password_vpopmaild_timeout'), 0); $result = $vpopmaild->readLine(); if (!preg_match('/^\\+OK/', $result)) { $vpopmaild->disconnect(); return PASSWORD_CONNECT_ERROR; } $vpopmaild->writeLine("slogin " . $_SESSION['username'] . " " . $curpass); $result = $vpopmaild->readLine(); if (!preg_match('/^\\+OK/', $result)) { $vpopmaild->writeLine("quit"); $vpopmaild->disconnect(); return PASSWORD_ERROR; } $vpopmaild->writeLine("mod_user " . $_SESSION['username']); $vpopmaild->writeLine("clear_text_password " . $passwd); $vpopmaild->writeLine("."); $result = $vpopmaild->readLine(); $vpopmaild->writeLine("quit"); $vpopmaild->disconnect(); if (!preg_match('/^\\+OK/', $result)) { return PASSWORD_ERROR; } return PASSWORD_SUCCESS; }
function save($curpass, $passwd) { $rcmail = rcmail::get_instance(); // include('Net/Socket.php'); $vpopmaild = new Net_Socket(); if (PEAR::isError($vpopmaild->connect($rcmail->config->get('password_vpopmaild_host'), $rcmail->config->get('password_vpopmaild_port'), null))) { return PASSWORD_CONNECT_ERROR; } $result = $vpopmaild->readLine(); if (!preg_match('/^\\+OK/', $result)) { $vpopmaild->disconnect(); return PASSWORD_CONNECT_ERROR; } $vpopmaild->writeLine("slogin " . $_SESSION['username'] . " " . $curpass); $result = $vpopmaild->readLine(); if (!preg_match('/^\\+OK/', $result)) { $vpopmaild->writeLine("quit"); $vpopmaild->disconnect(); return PASSWORD_ERROR; } $vpopmaild->writeLine("mod_user " . $_SESSION['username']); $vpopmaild->writeLine("clear_text_password " . $passwd); $vpopmaild->writeLine("."); $result = $vpopmaild->readLine(); $vpopmaild->writeLine("quit"); $vpopmaild->disconnect(); if (!preg_match('/^\\+OK/', $result)) { return PASSWORD_ERROR; } return PASSWORD_SUCCESS; }
function sendCommand($address, $command) { $socket = new Net_Socket(); // open connection $socket->connect("{$address}", 23, true, 3); $socket->writeLine($command); echo "\naddress:: {$address} command {$comando}"; sleep(1); $respuesta = trim($socket->read(200)); echo "\n respuesta: {$respuesta}\n"; $socket->disconnect(); echo "\ndisconnect..\n"; }
function password_save($curpass, $passwd) { $rcmail = rcmail::get_instance(); // include('Net/Socket.php'); $poppassd = new Net_Socket(); $result = $poppassd->connect($rcmail->config->get('password_pop_host'), $rcmail->config->get('password_pop_port'), null); if (PEAR::isError($result)) { return format_error_result(PASSWORD_CONNECT_ERROR, $result->getMessage()); } else { $result = $poppassd->readLine(); if (!preg_match('/^2\\d\\d/', $result)) { $poppassd->disconnect(); return format_error_result(PASSWORD_ERROR, $result); } else { $poppassd->writeLine("user " . $_SESSION['username']); $result = $poppassd->readLine(); if (!preg_match('/^[23]\\d\\d/', $result)) { $poppassd->disconnect(); return format_error_result(PASSWORD_CONNECT_ERROR, $result); } else { $poppassd->writeLine("pass " . $curpass); $result = $poppassd->readLine(); if (!preg_match('/^[23]\\d\\d/', $result)) { $poppassd->disconnect(); return format_error_result(PASSWORD_ERROR, $result); } else { $poppassd->writeLine("newpass " . $passwd); $result = $poppassd->readLine(); $poppassd->disconnect(); if (!preg_match('/^2\\d\\d/', $result)) { return format_error_result(PASSWORD_ERROR, $result); } else { return PASSWORD_SUCCESS; } } } } } }
function save($curpass, $passwd) { $rcmail = rcmail::get_instance(); $poppassd = new Net_Socket(); $port = $rcmail->config->get('password_pop_port', 106); $host = $rcmail->config->get('password_pop_host', 'localhost'); $host = rcube_utils::parse_host($host); $result = $poppassd->connect($host, $port, null); if (is_a($result, 'PEAR_Error')) { return $this->format_error_result(PASSWORD_CONNECT_ERROR, $result->getMessage()); } $result = $poppassd->readLine(); if (!preg_match('/^2\\d\\d/', $result)) { $poppassd->disconnect(); return $this->format_error_result(PASSWORD_ERROR, $result); } $poppassd->writeLine("user " . $_SESSION['username']); $result = $poppassd->readLine(); if (!preg_match('/^[23]\\d\\d/', $result)) { $poppassd->disconnect(); return $this->format_error_result(PASSWORD_CONNECT_ERROR, $result); } $poppassd->writeLine("pass " . $curpass); $result = $poppassd->readLine(); if (!preg_match('/^[23]\\d\\d/', $result)) { $poppassd->disconnect(); return $this->format_error_result(PASSWORD_ERROR, $result); } $poppassd->writeLine("newpass " . $passwd); $result = $poppassd->readLine(); $poppassd->disconnect(); if (!preg_match('/^2\\d\\d/', $result)) { return $this->format_error_result(PASSWORD_ERROR, $result); } return PASSWORD_SUCCESS; }
/** * Send a command to the IMAP server and retrieve * the response * * @param string $command The command to send * * @param string $data If we are issuing a command * that means the server will * consequently expect literal * data sent then this is that data. * * @return mixed Server response string on success * or bool false on error */ function sendCmd($command, $data = null, $fh = false) { if (!is_resource($this->socket->fp)) { $this->lastError = IC_NOT_CONNECTED; return false; } if (feof($this->socket->fp)) { $this->lastError = IC_NOT_CONNECTED; return false; } $cid = $this->cid; $this->socket->writeLine("{$this->cid} {$command}"); $this->cid++; if ($this->debug) { $this->debugOutput("C: {$cid} {$command}\n"); } $resp = ''; $loop = 0; $lines = 0; $bytesRead = 0; $respSize = 0; $code = ''; $fetch = substr($command, 0, 5) == 'FETCH'; $select = substr($command, 0, 6) == 'SELECT'; while (true) { // Check we are still connected if (!is_resource($this->socket->fp)) { return $resp; } if (feof($this->socket->fp)) { $this->lastError = IC_NOT_CONNECTED; return false; } $line = $this->socket->gets(8192); // 8192 same buffer used in Pear Net::POP3 if ($this->debug) { $this->debugOutput("S: {$line}"); } if (preg_match("/^{$cid} (OK|BAD|NO)/", $line, $m)) { $code = $m[1]; } elseif (!$fetch && !$select && preg_match("/^\\* (BAD|NO|BYE)/", $line, $m)) { $code = $m[1]; } // UW-IMAP been brain dead for 'status' command on a mailbox if ($code == 'NO' && preg_match("/NO CLIENT BUG DETECTED/", $line)) { $code = ''; continue; } else { if ($code == 'BAD' || $code == 'NO' || $code == 'BYE') { return false; } else { if ($code == 'OK') { return !empty($resp) ? $resp : $line; } } } $lines++; // If writing to the filehandle, get msg size and skip the first line response from the IMAP server if (is_resource($fh) && preg_match('/^\\* /', $line) && $lines == '1') { preg_match('/\\{(\\d+)\\}/', $line, $m); $respSize = $m[1]; continue; } // Check if we are sending literal data // and server is ready for literal data if ($data && preg_match('/^\\+/', $line)) { $size = $this->socket->write($data); if ($this->debug) { $this->debugOutput("C: {$cid} {$data}\n"); } sleep(1); $this->socket->write("\r\n"); continue; } // Double check that we are not hung on a // command (ie not enough data sent) if ($data && $line == '') { // Send some new lines to try and // reach expected bytes $this->socket->writeLine(''); if ($this->debug) { $this->debugOutput("C: {$cid} \n"); } } // Replace/fix - Under cyrus, multiple IMAP logins, mailbox locks, sockets are closed automatically if ($line == '' && $loop > 1000) { return $resp; } else { if ($line == '') { $loop++; } } // Print to the filehandle only up to $respSize bytes so we do not append the last IMAP header on the message if ($stop) { continue; } elseif (is_resource($fh)) { $bytesRead += strlen($line); // Strip Control-M chars $line = preg_replace('/\\cM+$/', '', $line); fwrite($fh, $line); // Only read up to expected byte size if ($bytesRead == $respSize) { $stop = true; } } else { $resp .= "{$line}"; } } }
/** * Connects to the whois server and retrieves domain information * * @param string $nicServer FQDN of whois server to query * @param string $domain Domain name to query * * @access private * @return mixed returns a PEAR_Error on failure, string of whois data on success */ function _connect($nicServer, $domain) { include_once 'Net/Socket.php'; if (is_null($nicServer) || empty($nicServer)) { return new PEAR_Error($this->_errorCodes[014], 14); } if (PEAR::isError($socket = new Net_Socket())) { return new PEAR_Error($this->_errorCodes[010], 10); } $result = $socket->connect($nicServer, $this->getPort(), null, $this->getTimeout(), $this->getOptions()); if (PEAR::isError($result)) { return new PEAR_Error($this->_errorCodes[011], 11); } $socket->setBlocking(false); // Querying denic.de requires some special coaxing for a domain query. // http://www.denic.de/en/faq-single/2978/1115.html if (substr($domain, -3) == '.de') { if (PEAR::isError($socket->writeLine("-T dn,ace " . $domain))) { return new PEAR_Error($this->_errorCodes[012], 12); } } else { if (PEAR::isError($socket->writeLine($domain))) { return new PEAR_Error($this->_errorCodes[012], 12); } } $nHost = null; $whoisData = $socket->readAll(); if (PEAR::isError($whoisData)) { return new PEAR_Error($this->_errorCodes[013], 13); } $data = explode("\n", $whoisData); foreach ($data as $line) { $line = rtrim($line); // check for whois server redirection if (!isset($nHost)) { $pattern = '/' . $this->_whoisServerID . '([a-z0-9.]+)\\n/i'; if (preg_match($pattern, $line, $matches)) { $nHost = $matches[1]; } elseif ($nicServer == $this->_nicServers['ANICHOST']) { foreach ($this->_ipNicServers as $ipNicServer) { $server = trim($this->_nicServers[$ipNicServer], '.'); if (strstr($line, $server)) { $nHost = $this->_nicServers[$ipNicServer]; } } } } } // this should fail, but we'll call it anyway and ignore the error $socket->disconnect(); if ($nHost && $nHost != $nicServer) { $tmpBuffer = $this->_connect($nHost, $domain); if (PEAR::isError($tmpBuffer)) { return $tmpBuffer; } $whoisData .= $tmpBuffer; } return $whoisData; }
/** * Connects to the whois server and retrieves domain information * * @param $nicServer string FQDN of whois server to query * @param $domain string domain name to query * @access private * @return mixed returns a PEAR_Error on failure, string of whois data on success */ function _connect($nicServer, $domain) { include_once 'Net/Socket.php'; if (PEAR::isError($socket = new Net_Socket())) { return new PEAR_Error($this->_errorCodes[010]); } if (PEAR::isError($socket->connect($nicServer, getservbyname('whois', 'tcp')))) { return new PEAR_Error($this->_errorCodes[011]); } $socket->setBlocking(false); if (PEAR::isError($socket->writeLine($domain))) { return new PEAR_Error($this->_errorCodes[012]); } $nHost = null; $whoisData = $socket->readAll(); if (PEAR::isError($whoisData)) { return new PEAR_Error($this->_errorCodes[013]); } $data = explode("\n", $whoisData); foreach ($data as $line) { $line = rtrim($line); // check for whois server redirection if (!isset($nHost)) { if (preg_match("/" . $this->_whoisServerID . "(.*)/", $line, $matches)) { $nHost = $matches[1]; } elseif ($nicServer == $this->_nicServers["ANICHOST"]) { foreach ($this->_ipNicServers as $ipNicServer) { if (strstr($line, $ipNicServer)) { $nHost = $ipNicServer; } } } } } // this should fail, but we'll call it anyway and ignore the error $socket->disconnect(); if ($nHost) { $tmpBuffer = $this->_connect($nHost, $domain); if (PEAR::isError($tmpBuffer)) { return $tmpBuffer; } $whoisData .= $tmpBuffer; } return $whoisData; }
/** * Used to figure out which Sieve server the script will be run * on, and then open a GSSAPI authenticated socket to said server. * * @param string $username The username. * @param string $password The password. * @param string $hostspec The hostspec. * * @return TODO * @throws Ingo_Exception */ public function sivtestSocket($username, $password, $hostspec) { $command = ''; $error_return = ''; if (strtolower($this->_params['logintype']) == 'gssapi' && isset($_SERVER['KRB5CCNAME'])) { $command .= 'KRB5CCNAME=' . $_SERVER['KRB5CCNAME']; } $domain_socket = 'unix://' . $this->_params['socket']; $command .= ' ' . $this->_params['command'] . ' -m ' . $this->_params['logintype'] . ' -u ' . $username . ' -a ' . $username . ' -w ' . $password . ' -p ' . $this->_params['port'] . ' -X ' . $this->_params['socket'] . ' ' . $hostspec; $conn_attempts = 0; while ($conn_attempts++ < 4) { $attempts = 0; if (!file_exists($this->_params['socket'])) { exec($command . ' > /dev/null 2>&1'); sleep(1); while (!file_exists($this->_params['socket'])) { usleep(200000); if ($attempts++ > 5) { $error_return = ': No socket after 10 seconds of trying!'; continue 2; } } } $socket = new Net_Socket(); $error = $socket->connect($domain_socket, 0, true, 30); if (!$error instanceof PEAR_Error) { break; } // We failed, break this connection. unlink($this->_params['socket']); } if (!empty($error_return)) { throw new Ingo_Exception($error_return); } $status = $socket->getStatus(); if ($status instanceof PEAR_Error || $status['eof']) { throw new Ingo_Exception(_("Failed to write to socket: (connection lost!)")); } $error = $socket->writeLine("CAPABILITY"); if ($error instanceof PEAR_Error) { throw new Ingo_Exception(_("Failed to write to socket: " . $error->getMessage())); } $result = $socket->readLine(); if ($result instanceof PEAR_Error) { throw new Ingo_Exception(_("Failed to read from socket: " . $error->getMessage())); } if (preg_match('|^bye \\(referral "(sieve://)?([^"]+)|i', $result, $matches)) { $socket->disconnect(); $this->sivtestSocket($username, $password, $matches[2]); } else { $socket->disconnect(); exec($command . ' > /dev/null 2>&1'); sleep(1); } }
function sendCommand($address, $command, $port = 23) { if (empty($address)) { return; } $socket = new Net_Socket(); // open connection $respuesta = ''; if ($socket->connect("{$address}", $port, true, 1)) { $socket->writeLine($command); //usleep(1000000); sleep(1); $respuesta = trim($socket->read(200)); $socket->disconnect(); } return $respuesta; }
/** * @brief * @param string $data_file Saveするファイルのパス * @param string $url データのURL * @retval bool */ function fetchTgzFile($data_file, $url) { /* exec('which wget || which curl', $which, $status); $command = strpos($which[0], 'wget')!==false ? sprintf('%s -O %s %s', $which[0], $data_file, $_url) : sprintf('%s -o %s %s', $which[0], $data_file, $_url); exec($command, $result, $status); return $status === 0; */ if ($fp = fopen($data_file, 'w')) { $url = parse_url($url); if (isset($url['port']) && $url['port']) { $port = $url['port']; } else { $port = $url['scheme'] == 'https' ? '443' : '80'; } $path = isset($url['path']) && $url['path'] ? $url['path'] : '/'; $path .= isset($url['query']) && $url['query'] ? $url['query'] : ''; require_once 'Net/Socket.php'; $sock = new Net_Socket(); $connect = $sock->connect($url['host'], $port); if ($connect) { $sock->writeLine("GET {$path} HTTP/1.1"); $sock->writeLine("Host: " . $url['host']); $sock->writeLine("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; ja; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7 (.NET CLR 3.5.30729)"); $sock->writeLine("Keep-Alive: 1000"); $sock->writeLine("Connection: keep-alive"); $sock->writeLine(""); $null_line = false; while (!$sock->eof()) { if ($null_line === false) { $_sock_d = $sock->readLine(); if ($_sock_d == "") { $null_line = true; } } if ($null_line === true) { $_sock_d = $sock->read(1024); fputs($fp, $_sock_d); } } $sock->disconnect(); } fclose($fp); return true; } return false; }