/** * Set the location for language files. * * @param String $location Path to locale files * * @return void */ public function set_locales_location($location) { if (file_exists($location) === TRUE) { $this->locales_location = $location; } else { $this->logger->warning('Invalid locales location: ' . $location); } }
public function shutdownAllChildren() { foreach ($this->children as $child) { posix_kill($child, SIGTERM); } $time = microtime(true); do { foreach ($this->children as $child) { $pid = pcntl_waitpid($child, $status, WNOHANG); if ($pid) { unset($this->children[$pid]); $this->childrenCount--; } } if ($this->childrenCount === 0) { break; } usleep(1000); } while (microtime(true) - $time < $this->childExitTimeout); if ($this->childrenCount) { $this->logger->warning(sprintf('%d children failed to exit within the %ds limit, going to kill them', $this->childrenCount, $this->childExitTimeout)); foreach ($this->children as $child) { posix_kill($child, SIGKILL); } } }
/** * Constructor. * * @param Boolean $response Response of the Mail Class. * @param LoggerInterface $logger Shared instance of a Logger. * @param String $email The email address that the message was sent to. */ public function __construct($response, $logger, $email) { if ($response === TRUE) { $this->status = PushNotificationStatus::SUCCESS; } else { $this->status = PushNotificationStatus::ERROR; $context = ['endpoint' => $email]; $logger->warning('Sending email notification to {endpoint} failed.', $context); } }
/** * Fetch and parse results as though they were a json string. * * @param String $url API URL * @param Array $params Array of parameters for the API request * * @return Array $parts Array of return values */ protected function get_json_results($url, $params = []) { $this->curl->set_option('CURLOPT_FAILONERROR', FALSE); $response = $this->curl->get_request($url . '?' . http_build_query($params)); $result = json_decode($response->get_result(), TRUE); if ($response->http_code !== 200) { $context = ['message' => $result['message'], 'request' => $url, 'id' => $result['sys']['id']]; $this->logger->warning('Contentful API Request ({request}) failed with id "{id}": {message}', $context); $result['total'] = 0; } unset($response); return $result; }
/** * Verify that every index in the input set has been checked. * * @param String $error_prefix Prefix string for the error logging * * @return Boolean $return TRUE if everything has been checked, FALSE otherwise */ protected function is_fully_checked($error_prefix) { // Check that input matches with the defined ruleset $data_indexes = array_keys($this->data); $checked_indexes = array_keys($this->result); $unhandled_elements = array_diff($data_indexes, $checked_indexes); if ($unhandled_elements == array()) { return TRUE; } $message = $error_prefix . "Unhandled Index '{index}'!"; $context = array(); foreach ($unhandled_elements as $value) { $context['index'] = $value; $this->logger->warning($message, $context); } return FALSE; }
/** * 断片的なHTMLをbody要素の子として挿入し解析します。 * @param string $input * @return \DOMElement|null body要素。 */ protected function parse(string $input) { $prependedRandom = bin2hex(random_bytes(10)); $appendedRandom = bin2hex(random_bytes(10)); $html = "<!DOCTYPE html>\n <html>\n <head>\n <title>Filter</title>\n </head>\n <body><!--{$prependedRandom}-->{$input}<!--{$appendedRandom}--></body>\n </html>\n "; $html5 = new html5\HTML5(); $document = $html5->loadHTML($html); foreach ($html5->getErrors() as $error) { $this->logger->warning($error); } $body = $document->getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'body')->item(0); if ($body && $body->hasChildNodes() && $body->firstChild->nodeType === \XML_COMMENT_NODE && $body->firstChild->data === $prependedRandom && $body->lastChild->nodeType === \XML_COMMENT_NODE && $body->lastChild->data === $appendedRandom) { $body->removeChild($body->firstChild); $body->removeChild($body->lastChild); return $body; } $this->logger->error(_('HTMLの解析に失敗しました。')); }
/** * Creates a directory with the given name, if it does not exist. * * This method is a wrapper of the php mkdir. * * @param String $pathname The directory path/name * @param Integer $mode The access mode (0755 by default) * @param Boolean $recursive Allows the creation of nested directories specified in the pathname * * @return Boolean TRUE when directory is created or FALSE in failure. */ public function mkdir($pathname, $mode = 0755, $recursive = FALSE) { if (is_string($mode)) { $this->logger->warning('String representation of access mode is not supported. Please, try using an integer.'); return FALSE; } if (decoct(octdec(strval($mode))) != $mode && $mode > 0) { $mode = octdec((string) $mode); } //this is the octal range (0000 - 2777 and 4000 - 4777) in decimal if (!($mode >= 0 && $mode <= 1535 || $mode >= 2048 && $mode <= 2559)) { $this->logger->warning('Access mode value ' . $mode . ' is invalid.'); return FALSE; } if (mkdir($pathname, $mode, $recursive) === FALSE) { $this->logger->warning('Failed to create directory: ' . $pathname); return FALSE; } return TRUE; }
/** * Set notification status information. * * @param String $endpoint The notification endpoint that was used. * @param LoggerInterface $logger Shared instance of a Logger. * * @return void */ private function set_status($endpoint, $logger) { switch ($this->http_code) { case 200: $this->status = PushNotificationStatus::SUCCESS; break; case 400: $this->status = PushNotificationStatus::ERROR; break; case 401: $this->status = PushNotificationStatus::INVALID_ENDPOINT; break; case 503: $this->status = PushNotificationStatus::ERROR; break; default: $this->status = PushNotificationStatus::UNKNOWN; break; } if ($this->status !== PushNotificationStatus::SUCCESS) { $context = ['endpoint' => $endpoint, 'code' => $this->status, 'description' => $this->result]; $message = 'Push notification delivery status for endpoint {endpoint}: '; $message .= 'failed with an error: {description}. Error #{code}'; $logger->warning($message, $context); } else { $failures = $this->parse_gcm_failures(); if ($failures['failure'] != 0) { $context = ['failure' => $failures['failure'], 'errors' => json_encode($failures['messages'])]; $message = '{failure} push notification(s) failed with the following '; $message .= 'error information {errors}.'; $logger->warning($message, $context); } } }
/** * Receive request and create a Response. * * This is the core method implementing basic parts of JSKOS API. * The method handles HTTP request method, request headers and * [query modifiers](https://gbv.github.io/jskos-api/#query-modifiers), * passes valid GET and HEAD requests to the served Service and wraps * the result as Response. * * @return Response */ public function response() { $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'; $params = $_GET; if ($method == 'OPTIONS') { $this->logger->info("Received OPTIONS request"); return $this->optionsResponse(); } # get query modifier: callback if (isset($params['callback'])) { $callback = $params['callback']; if (!preg_match('/^[$A-Z_][0-9A-Z_$.]*$/i', $callback)) { unset($callback); } unset($params['callback']); } $language = $this->extractRequestLanguage($params); # TODO: extract more query modifiers # TODO: header: Allow/Authentication $error = null; # conflicting parameters if (isset($params['uri']) and isset($params['search'])) { $error = new Error('422', 'request_error', 'Conflicting request parameters uri & search'); } if (!$error and ($method == 'GET' or $method == 'HEAD')) { $this->logger->info("Received {$method} request", $params); $answer = $this->queryService($params); if ($answer instanceof Page) { // TODO: if unique $response = $this->basicResponse(200, $answer); // TODO: Add Link header with next/last/first $response->headers['X-Total-Count'] = $answer->totalCount; if ($method == 'HEAD') { $response->emptyBody = true; } } elseif ($answer instanceof Error) { $error = $answer; } } elseif (!$error) { $error = new Error(405, '???', 'Method not allowed'); } if (isset($error)) { $this->logger->warning($error->message, ['error' => $error]); $response = $this->basicResponse($error->code, $error); } if (isset($callback)) { $response->callback = $callback; } return $response; }
/** * Set notification status information. * * @param String $endpoint The notification endpoint that was used. * @param LoggerInterface $logger Shared instance of a Logger. * * @return void */ private function set_status($endpoint, $logger) { switch ($this->http_code) { case 200: if ($this->pap_response['code'] == 0) { $this->status = PushNotificationStatus::SUCCESS; } else { if ($this->pap_response['code'] == 2002) { $this->status = PushNotificationStatus::INVALID_ENDPOINT; } elseif ($this->pap_response['code'] < 4000) { $this->status = PushNotificationStatus::ERROR; } elseif ($this->pap_response['code'] <= 4502) { $this->status = PushNotificationStatus::TEMPORARY_ERROR; } else { $this->status = PushNotificationStatus::UNKNOWN; } } break; case 400: $this->status = PushNotificationStatus::ERROR; break; case 401: $this->status = PushNotificationStatus::INVALID_ENDPOINT; break; case 503: $this->status = PushNotificationStatus::ERROR; break; default: $this->status = PushNotificationStatus::UNKNOWN; break; } if ($this->status !== PushNotificationStatus::SUCCESS) { $error_description = isset($this->pap_response['message']) ? $this->pap_response['message'] : $this->result; $context = ['endpoint' => $endpoint, 'code' => $this->status, 'description' => $error_description]; $message = 'Push notification delivery status for endpoint {endpoint}: '; $message .= 'failed with an error: {description}. Error #{code}'; $logger->warning($message, $context); } }
/** * Set notification status information. * * @param String $endpoint The notification endpoint that was used. * @param LoggerInterface $logger Shared instance of a Logger. * * @return void */ private function set_status($endpoint, $logger) { switch ($this->response_code) { case APNSStatus::APN_SUCCESS: $this->status = PushNotificationStatus::SUCCESS; break; case APNSStatus::APN_ERR_TOKEN_IS_NOT_SET: case APNSStatus::APN_ERR_TOKEN_INVALID: $this->status = PushNotificationStatus::INVALID_ENDPOINT; break; case APNSStatus::APN_ERR_PROCESSING_ERROR: $this->status = PushNotificationStatus::TEMPORARY_ERROR; break; case APNSStatus::APN_ERR_UNKNOWN: $this->status = PushNotificationStatus::UNKNOWN; break; default: $this->status = PushNotificationStatus::ERROR; break; } if ($this->status !== PushNotificationStatus::SUCCESS) { $context = ['endpoint' => $endpoint, 'code' => $this->status, 'description' => $this->result]; $message = 'Push notification delivery status for endpoint {endpoint}: '; $message .= 'failed with an error: {description}. Error #{code}'; $logger->warning($message, $context); } }
/** * Set notification status information. * * @param String $endpoint The notification endpoint that was used. * @param LoggerInterface $logger Shared instance of a Logger. * * @return void */ private function set_status($endpoint, $logger) { switch ($this->http_code) { case 200: if ($this->headers['X-Notificationstatus'] === 'Received') { $this->status = PushNotificationStatus::SUCCESS; } elseif ($this->headers['X-Notificationstatus'] === 'QueueFull') { $this->status = PushNotificationStatus::TEMPORARY_ERROR; } else { $this->status = PushNotificationStatus::CLIENT_ERROR; } break; case 404: $this->status = PushNotificationStatus::INVALID_ENDPOINT; break; case 400: case 401: case 405: $this->status = PushNotificationStatus::ERROR; break; case 406: case 412: case 503: $this->status = PushNotificationStatus::TEMPORARY_ERROR; break; default: $this->status = PushNotificationStatus::UNKNOWN; break; } if ($this->status !== PushNotificationStatus::SUCCESS) { $context = ['endpoint' => $endpoint, 'nstatus' => $this->headers['X-Notificationstatus'], 'dstatus' => $this->headers['X-Deviceconnectionstatus'], 'sstatus' => $this->headers['X-Subscriptionstatus']]; $message = 'Push notification delivery status for endpoint {endpoint}: '; $message .= '{nstatus}, device {dstatus}, subscription {sstatus}'; $logger->warning($message, $context); } }