Example #1
4
 /**
  * Method to read email from imap extension and return Zend Mail Message object.
  *
  * This is bridge while migrating to Zend Mail package supporting reading from imap extension functions.
  *
  * @param resource $mbox
  * @param integer $num
  * @param array $info connection information about connection
  * @return ImapMessage
  */
 public static function createFromImap($mbox, $num, $info)
 {
     // check if the current message was already seen
     list($overview) = imap_fetch_overview($mbox, $num);
     $headers = imap_fetchheader($mbox, $num);
     $content = imap_body($mbox, $num);
     // fill with "\Seen", "\Deleted", "\Answered", ... etc
     $knownFlags = array('recent' => Zend\Mail\Storage::FLAG_RECENT, 'flagged' => Zend\Mail\Storage::FLAG_FLAGGED, 'answered' => Zend\Mail\Storage::FLAG_ANSWERED, 'deleted' => Zend\Mail\Storage::FLAG_DELETED, 'seen' => Zend\Mail\Storage::FLAG_SEEN, 'draft' => Zend\Mail\Storage::FLAG_DRAFT);
     $flags = array();
     foreach ($knownFlags as $flag => $value) {
         if ($overview->{$flag}) {
             $flags[] = $value;
         }
     }
     $message = new self(array('root' => true, 'headers' => $headers, 'content' => $content, 'flags' => $flags));
     // set MailDate to $message object, as it's not available in message headers, only in IMAP itself
     // this likely "message received date"
     $imapheaders = imap_headerinfo($mbox, $num);
     $header = new GenericHeader('X-IMAP-UnixDate', $imapheaders->udate);
     $message->getHeaders()->addHeader($header);
     $message->mbox = $mbox;
     $message->num = $num;
     $message->info = $info;
     return $message;
 }
 /**
  * Initializes rows and fields from CSV file
  * @param resource $fp File pointer, obtained with {@link fopen()}
  * @param SQLICSVOptions $options
  * @return SQLICSVRowSet
  */
 public static function fromCSVFile($fp, SQLICSVOptions $options = null)
 {
     if (!$options instanceof SQLICSVOptions) {
         $options = new SQLICSVOptions();
     }
     $set = new self();
     $i = 0;
     while ($data = fgetcsv($fp, $options['csv_line_length'], $options['delimiter'], $options['enclosure'])) {
         //echo "Working on CSV row #$i\n";
         if ($i == 0) {
             $set->setRowHeaders($data);
             $i++;
             unset($data);
             continue;
         }
         $aRowData = array();
         $headers = $set->getHeaders();
         for ($j = 0, $jMax = count($headers); $j < $jMax; ++$j) {
             $aRowData[$headers[$j]] = $data[$j];
         }
         unset($headers, $data);
         $row = new SQLICSVRow($aRowData);
         $set->rows[] = $row;
         unset($aRowData);
         $i++;
     }
     $set->initIterator();
     return $set;
 }
Example #3
0
 /**
  * @param string $requestString
  * @return wfWAFRequest
  */
 public static function parseString($requestString)
 {
     if (!is_string($requestString)) {
         throw new InvalidArgumentException(__METHOD__ . ' expects a string for first parameter, recieved ' . gettype($requestString));
     }
     if (version_compare(phpversion(), '5.3.0') > 0) {
         $class = get_called_class();
         $request = new $class();
     } else {
         $request = new self();
     }
     $request->setAuth(array());
     $request->setBody(array());
     $request->setCookies(array());
     $request->setFileNames(array());
     $request->setFiles(array());
     $request->setHeaders(array());
     $request->setHost('');
     $request->setIP('');
     $request->setMethod('');
     $request->setPath('');
     $request->setProtocol('');
     $request->setQueryString(array());
     $request->setTimestamp('');
     $request->setURI('');
     list($headersString, $bodyString) = explode("\n\n", $requestString, 2);
     $headersString = trim($headersString);
     $bodyString = trim($bodyString);
     $headers = explode("\n", $headersString);
     // Assume first is method
     if (preg_match('/^([a-z]+) (.*?) HTTP\\/1.[0-9]/i', $headers[0], $matches)) {
         $request->setMethod($matches[1]);
         $uri = $matches[2];
         $request->setUri($uri);
         if (($pos = wfWAFUtils::strpos($uri, '?')) !== false) {
             $queryString = wfWAFUtils::substr($uri, $pos + 1);
             parse_str($queryString, $queryStringArray);
             $request->setQueryString($queryStringArray);
             $path = wfWAFUtils::substr($uri, 0, $pos);
             $request->setPath($path);
         } else {
             $request->setPath($uri);
         }
     }
     $kvHeaders = array();
     for ($i = 1; $i < count($headers); $i++) {
         $headerString = $headers[$i];
         list($header, $headerValue) = explode(':', $headerString, 2);
         $header = trim($header);
         $headerValue = trim($headerValue);
         $kvHeaders[$header] = $headerValue;
         switch (wfWAFUtils::strtolower($header)) {
             case 'authorization':
                 if (preg_match('/basic ([A-Za-z0-9\\+\\/=]+)/i', $headerValue, $matches)) {
                     list($authUser, $authPass) = explode(':', base64_decode($matches[1]), 2);
                     $auth['user'] = $authUser;
                     $auth['password'] = $authPass;
                     $request->setAuth($auth);
                 }
                 break;
             case 'host':
                 $request->setHost($headerValue);
                 break;
             case 'cookie':
                 $cookieArray = array();
                 $cookies = explode(';', $headerValue);
                 foreach ($cookies as $cookie) {
                     if (wfWAFUtils::strpos($cookie, '=') !== false) {
                         list($cookieName, $cookieValue) = explode('=', $cookie, 2);
                         $cookieArray[trim($cookieName)] = urldecode(trim($cookieValue));
                     }
                 }
                 $request->setCookies($cookieArray);
                 break;
         }
     }
     $request->setHeaders($kvHeaders);
     if (wfWAFUtils::strlen($bodyString) > 0) {
         if (preg_match('/^multipart\\/form\\-data; boundary=(.*?)$/i', $request->getHeaders('Content-Type'), $boundaryMatches)) {
             $body = '';
             $files = array();
             $fileNames = array();
             $boundary = $boundaryMatches[1];
             $bodyChunks = explode("--{$boundary}", $bodyString);
             foreach ($bodyChunks as $chunk) {
                 if (!$chunk || $chunk == '--') {
                     continue;
                 }
                 list($chunkHeaders, $chunkData) = explode("\n\n", $chunk, 2);
                 $chunkHeaders = explode("\n", $chunkHeaders);
                 $param = array('value' => wfWAFUtils::substr($chunkData, 0, -1));
                 foreach ($chunkHeaders as $chunkHeader) {
                     if (wfWAFUtils::strpos($chunkHeader, ':') !== false) {
                         list($chunkHeaderKey, $chunkHeaderValue) = explode(':', $chunkHeader, 2);
                         $chunkHeaderKey = trim($chunkHeaderKey);
                         $chunkHeaderValue = trim($chunkHeaderValue);
                         switch ($chunkHeaderKey) {
                             case 'Content-Disposition':
                                 $dataAttributes = explode(';', $chunkHeaderValue);
                                 foreach ($dataAttributes as $attr) {
                                     $attr = trim($attr);
                                     if (preg_match('/^name="(.*?)"$/i', $attr, $attrMatch)) {
                                         $param['name'] = $attrMatch[1];
                                         continue;
                                     }
                                     if (preg_match('/^filename="(.*?)"$/i', $attr, $attrMatch)) {
                                         $param['filename'] = $attrMatch[1];
                                         continue;
                                     }
                                 }
                                 break;
                             case 'Content-Type':
                                 $param['type'] = $chunkHeaderValue;
                                 break;
                         }
                     }
                 }
                 if (array_key_exists('name', $param)) {
                     if (array_key_exists('filename', $param)) {
                         $files[$param['name']] = array('name' => $param['filename'], 'type' => $param['type'], 'size' => wfWAFUtils::strlen($param['value']), 'content' => $param['value']);
                         $fileNames[$param['name']] = $param['filename'];
                     } else {
                         $body .= urlencode($param['name']) . '=' . urlencode($param['value']) . '&';
                     }
                 }
             }
             if ($body) {
                 parse_str($body, $postBody);
                 if (is_array($postBody)) {
                     $request->setBody($postBody);
                 } else {
                     $request->setBody($body);
                 }
             }
             if ($files) {
                 $request->setFiles($files);
             }
             if ($fileNames) {
                 $request->setFileNames($fileNames);
             }
         } else {
             parse_str($bodyString, $postBody);
             if (is_array($postBody)) {
                 $request->setBody($postBody);
             } else {
                 $request->setBody($bodyString);
             }
         }
     }
     return $request;
 }
Example #4
0
 /**
  * Proxy/resend the current request to different url/server.
  *
  * @param string   $endpoint Url
  * @param callback $filter   Callback to change the generated Curl options.
  */
 public static function proxy($endpoint, $filter = null)
 {
     $url = new Url($endpoint);
     $url->query = $_GET;
     $options = array(CURLOPT_URL => (string) $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => $_SERVER['REQUEST_METHOD'], CURLOPT_FAILONERROR => false, CURLOPT_HEADER => true, CURLOPT_HTTPHEADER => []);
     // Proxy HTTP headers
     foreach ($_SERVER as $name => $value) {
         if (substr($name, 0, 5) === 'HTTP_' && in_array($name, array('HTTP_HOST', 'HTTP_CONNECTION')) === false) {
             $options[CURLOPT_HTTPHEADER][] = substr($name, 5) . ':' . $value;
         }
     }
     // Proxy POST/PUT contents
     if (in_array($_SERVER['REQUEST_METHOD'], array('POST', 'PUT'))) {
         if (empty($_POST) === false) {
             $options[CURLOPT_POSTFIELDS] = $_POST;
         } elseif (empty($_PUT) === false) {
             $options[CURLOPT_POSTFIELDS] = $_PUT;
         } elseif (isset($HTTP_RAW_POST_DATA)) {
             $options[CURLOPT_POSTFIELDS] = $HTTP_RAW_POST_DATA;
         } else {
             $options[CURLOPT_POSTFIELDS] = file_get_contents('php://input');
         }
     }
     // Custom curl options
     if ($filter !== null) {
         $options = call_user_func($filter, $options);
     }
     // Send request
     $response = new self($options);
     $headers = $response->getHeaders();
     unset($headers['Transfer-Encoding']);
     $headers['Status'] = $response->http_code;
     \Sledgehammer\send_headers($headers);
     echo $response->getBody();
     exit;
 }
Example #5
0
 public static function fromArray(array $array)
 {
     $message = new self($array['subject']);
     if (null !== $array['sender']) {
         $message->setSender($array['sender']);
     }
     if (null !== $array['from']) {
         $message->setFrom($array['from']);
     }
     if (null !== $array['to']) {
         $message->setTo($array['to']);
     }
     if (null !== $array['replyTo']) {
         $message->setReplyTo($array['replyTo']);
     }
     if (null !== $array['cc']) {
         $message->setCc($array['cc']);
     }
     if (null !== $array['bcc']) {
         $message->setBcc($array['bcc']);
     }
     if (null !== $array['text']) {
         $message->setBodyWithAlternative($array['text'], $array['html']);
     }
     foreach ($array['customHeaders'] as $label => $valueList) {
         foreach ($valueList as $value) {
             $message->getHeaders()->addTextHeader($label, $value);
         }
     }
     return $message;
 }