function decode() { $params = array('crlf' => "\r\n", 'charset' => $this->charset, 'include_bodies' => $this->include_bodies, 'decode_headers' => $this->decode_headers, 'decode_bodies' => $this->decode_bodies); $info = array('raw' => &$this->mime_message); Signal::send('mail.received', $this, $info); $this->splitBodyHeader(); $decoder = new Mail_mimeDecode($this->mime_message); $this->struct = $decoder->decode($params); if (PEAR::isError($this->struct)) { return false; } $info = array('raw_header' => &$this->header, 'headers' => &$this->struct->headers, 'body' => &$this->struct->parts, 'type' => $this->struct->ctype_primary . '/' . $this->struct->ctype_secondary, 'mail' => $this->struct, 'decoder' => $decoder); // Allow signal handlers to interact with the processing Signal::send('mail.decoded', $decoder, $info); // Handle wrapped emails when forwarded if ($this->struct && $this->struct->parts) { $outer = $this->struct; $ctype = $outer->ctype_primary . '/' . $outer->ctype_secondary; if (strcasecmp($ctype, 'message/rfc822') === 0) { // Capture Delivered-To header from the outer mail $dt = $this->struct->headers['delivered-to']; // Capture Message-Id from outer mail $mid = $this->struct->headers['message-id']; $this->struct = $outer->parts[0]; // Add (clobber) delivered to header from the outer mail if ($dt) { $this->struct->headers['delivered-to'] = $dt; } // Ensure the nested mail has a Message-Id if (!isset($this->struct->headers['message-id'])) { $this->struct->headers['message-id'] = $mid; } // Use headers of the wrapped message $headers = array(); foreach ($this->struct->headers as $h => $v) { $headers[mb_convert_case($h, MB_CASE_TITLE)] = $v; } $this->header = Format::array_implode(": ", "\n", $headers); } } // Look for application/tnef attachment and process it if ($this->struct && $this->struct->parts) { foreach ($this->struct->parts as $i => $part) { if (!@$part->parts && $part->ctype_primary == 'application' && $part->ctype_secondary == 'ms-tnef') { try { $tnef = new TnefStreamParser($part->body); $this->tnef = $tnef->getMessage(); // No longer considered an attachment unset($this->struct->parts[$i]); } catch (TnefException $ex) { // TNEF will remain an attachment $this->notes[] = 'TNEF parsing exception: ' . $ex->getMessage(); } } } } return count($this->struct->headers) > 1; }
function _request($command, $args = array()) { $url = str_replace('{command}', $command, self::$crowdin_api_url); $args += array('key' => $this->key); foreach ($args as &$a) { $a = urlencode($a); } unset($a); $url .= '?' . Format::array_implode('=', '&', $args); return $this->_http_get($url); }
function createTicket($data) { # Pull off some meta-data $alert = $data['alert'] ? $data['alert'] : true; $autorespond = $data['autorespond'] ? $data['autorespond'] : true; $data['source'] = $data['source'] ? $data['source'] : 'API'; # Create the ticket with the data (attempt to anyway) $errors = array(); $ticket = Ticket::create($data, $errors, $data['source'], $autorespond, $alert); # Return errors (?) if (count($errors)) { if (isset($errors['errno']) && $errors['errno'] == 403) { return $this->exerr(403, 'Ticket denied'); } else { return $this->exerr(400, "Unable to create new ticket: validation errors:\n" . Format::array_implode(": ", "\n", $errors)); } } elseif (!$ticket) { return $this->exerr(500, "Unable to create new ticket: unknown error"); } return $ticket; }
function create($format) { $this->requireApiKey(); # Parse request body $data = $this->getRequest($format); if ($format == "xml") { $data = $data["ticket"]; } # Pull off some meta-data $alert = $data['alert'] ? $data['alert'] : true; $autorespond = $data['autorespond'] ? $data['autorespond'] : true; $source = $data['source'] ? $data['source'] : 'API'; $attachments = $data['attachments'] ? $data['attachments'] : array(); # TODO: Handle attachment encoding (base64) foreach ($attachments as $filename => &$info) { if ($info["encoding"] == "base64") { # XXX: May fail on large inputs. See # http://us.php.net/manual/en/function.base64-decode.php#105512 if (!($info["data"] = base64_decode($info["data"], true))) { Http::response(400, sprintf("%s: Poorly encoded base64 data", $filename)); } } $info['size'] = strlen($info['data']); } # Create the ticket with the data (attempt to anyway) $errors = array(); $ticket = Ticket::create($data, $errors, $source, $autorespond, $alert); # Return errors (?) if (count($errors)) { Http::response(400, "Unable to create new ticket: validation errors:\n" . Format::array_implode(": ", "\n", $errors)); } elseif (!$ticket) { Http::response(500, "Unable to create new ticket: unknown error"); } # Save attachment(s) foreach ($attachments as &$info) { $ticket->saveAttachment($info, $ticket->getLastMsgId(), "M"); } # All done. Return HTTP/201 --> Created Http::response(201, $ticket->getExtId()); }
function exerr($code, $error = '') { global $ost; if ($error && is_array($error)) { $error = Format::array_implode(": ", "\n", $error); } //Log the error as a warning - include api key if available. $msg = $error; if ($_SERVER['HTTP_X_API_KEY']) { $msg .= "\n*[" . $_SERVER['HTTP_X_API_KEY'] . "]*\n"; } $ost->logWarning("API Error ({$code})", $msg, false); $this->response($code, $error); //Responder should exit... return false; }
function createTicket($data) { # Pull off some meta-data $alert = (bool) (isset($data['alert']) ? $data['alert'] : true); $autorespond = (bool) (isset($data['autorespond']) ? $data['autorespond'] : true); # Assign default value to source if not defined, or defined as NULL $data['source'] = isset($data['source']) ? $data['source'] : 'API'; # Create the ticket with the data (attempt to anyway) $errors = array(); $ticket = Ticket::create($data, $errors, $data['source'], $autorespond, $alert); # Return errors (?) if (count($errors)) { if (isset($errors['errno']) && $errors['errno'] == 403) { return $this->exerr(403, __('Ticket denied')); } else { return $this->exerr(400, __("Unable to create new ticket: validation errors") . ":\n" . Format::array_implode(": ", "\n", $errors)); } } elseif (!$ticket) { return $this->exerr(500, __("Unable to create new ticket: unknown error")); } // $ticket->setStaffId(0); return $ticket; }
function exerr($code, $error = '') { global $ost; if ($error && is_array($error)) { $error = Format::array_implode(": ", "\n", $error); } //Log the error as a warning - include api key if available. $msg = $error; if ($_SERVER['HTTP_X_API_KEY']) { $msg .= "\n*[" . $_SERVER['HTTP_X_API_KEY'] . "]*\n"; } $ost->logWarning(__('API Error') . " ({$code})", $msg, false); if (PHP_SAPI == 'cli') { fwrite(STDERR, "({$code}) {$error}\n"); } else { $this->response($code, $error); //Responder should exit... } return false; }