public function indexAjax() { $input = Input::requireParams('username', 'pass', 'r'); if (($user = User::auth($input->username, $input->pass)) !== false) { $sessionData = $user->getData(); $sessionData['previous_login'] = $user->last_login; $user->updateLoginStats(); $sessionData['last_login'] = gmdate('Y-m-d H:i:s'); Cookie::set('username', $input->username, time() + 3600 * 24 * 30); Log::info("User {$input->username} logged in with " . Request::userAgent()); // some stats $sessionData['stats'] = array('new_stack_traces' => \Stack\Trace::getNewCount($sessionData['previous_login'])); Session::set('user', $sessionData); return BootstrapUI::formResponse()->redirect(base64_decode($input->r)); } else { Log::info("Invalid auth for '{$input->username}' with '{$input->pass}'"); return BootstrapUI::formResponse()->failed('Wrong username or password'); } }
/** * Do actual e-mail test by sending the e-mail to the given e-mail address * @return \Bootstrap\Response\Form */ public function testEmailAjax() { $validator = Validator::create(array('to' => 'required|email')); if ($validator->failed()) { return BootstrapUI::formResponse()->failedOn($validator); } if (!Mail::isEnabled()) { return BootstrapUI::formResponse()->message('Mail sender is not enabled!'); } $params = $validator->getParamsObj(); $mail = Mail::create()->from('no-replay@' . Request::hostNameDomain(), Request::hostName())->to($params->to)->subject('Configuration test mail')->body("If you got this e-mail, then your mail configuration is just fine!\n\n" . Url::current()); try { if ($mail->send()) { Log::info("Test e-mail is sent to {$params->to}"); return BootstrapUI::formResponse()->message('Test e-mail is sent!'); } } catch (Exception $e) { Log::info("Couldn\\'t send test e-mail to {$params->to}"); Log::exception($e); // we'll log this if needed, just to make sure it won't be forgotten return BootstrapUI::formResponse()->failed('E-mail wasn\'t sent:<br/>' . $e->getMessage()); // show user the actual error } }
/** * Update login stats */ public function updateLoginStats() { $this->last_login = gmdate('Y-m-d H:i:s'); $this->last_login_ip = Request::ipWithProxy(); $this->save(); }
/** * Get the message that will be logged into file * * @param string $level * @param string $message * @return string */ protected function getMessage($level, $message) { if ($this->getMessageFunction !== null) { return call_user_func($this->getMessageFunction, $level, $message); } $ip = Request::ip(); return date('Y-m-d H:i:sO') . "\t{$level}\t{$ip}\t{$message}\n"; }
/** * Send e-mail report if system detected that e-mail should be sent * * @return boolean|null true if mail was sent and null if mail shouldn't be sent */ protected function sendEmailReport() { if ($this->emailReport === true && $this->config['email'] !== null) { $body = implode('', $this->messages); /* this doesn't have sense any more ::: $body .= "\n\n---------- debug_backtrace:\n"; foreach (debug_backtrace() as $r) { if (isset($r['file']) && isset($r['line'])) { $body .= "{$r['file']}:{$r['line']} "; } if (isset($r['function'])) { $body .= "{$r['function']} "; } if (isset($r['args'])) { $body .= implode(', ', $r['args']); } $body .= "\n"; } */ $body .= "\n----------\n"; $body .= sprintf("server: %s (%s)\n", Request::serverIp(), Request::hostName()); if (PHP_SAPI != 'cli') { $body .= 'URI: ' . $_SERVER['REQUEST_METHOD'] . '=' . Application::getConfig('application', 'site_url') . Application::getUri() . "\n"; $body .= sprintf("User IP: %s (%s)%s", Request::ip(), Request::host(), Request::hasProxy() ? sprintf(" via %s for %s\n", Request::proxySignature(), Request::httpXForwardedFor()) : "\n"); $body .= sprintf("UAS: %s\n", isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'no user agent set'); } else { $body .= 'CLI Name: ' . Application::getCliName() . "\n"; $body .= 'CLI Script: ' . Application::getCliScript() . "\n"; $params = Cli::getParameters(); if (count($params) > 0) { $body .= 'CLI Params: ' . print_r($params, true) . "\n"; } } $body .= sprintf("Server load: %s\n", Server::getServerLoad()); $peak = memory_get_peak_usage(true); $memoryLimit = ini_get('memory_limit'); $body .= sprintf("Memory: %s; peak: %s; limit: %s; spent: %s%%\n", Convert::bytesToString(memory_get_usage(true)), Convert::bytesToString($peak), $memoryLimit, $memoryLimit !== false && $memoryLimit > 0 ? round($peak * 100 / Convert::stringToBytes($memoryLimit), 2) : 'null'); $body .= sprintf("included files: %s\n", print_r(get_included_files(), true)); $mail = Mail::create(); $mail->from('alert@' . Request::hostName(), Request::hostName())->subject('Log report')->body($body); if (!is_array($this->config['email']) && strpos($this->config['email'], ',') !== false) { $this->config['email'] = explode(',', $this->config['email']); } if (is_array($this->config['email'])) { foreach ($this->config['email'] as $toEmail) { $mail->to(trim($toEmail)); } } else { $mail->to(trim($this->config['email'])); } if (!$mail->send()) { $this->error("Can not send alert mail to {$this->config['email']}: {$mail->getError()}\n{$mail->getException()->getTraceAsString()}"); return false; } return true; } return null; }
public static function processRequest(array $params) { $data = $params; $params = array(); foreach ($data as $key => $value) { $params[strtolower($key)] = $value; } unset($data); $where = array(); $bindings = null; if (isset($params['package_name']) && $params['package_name'] !== null && trim($params['package_name']) != '') { $where[] = '(package IS NULL OR package LIKE :package_name)'; $bindings['package_name'] = "%{$params['package_name']}%"; } if (isset($params['app_version_name']) && $params['app_version_name'] !== null && trim($params['app_version_name']) != '') { $where[] = '(package_version IS NULL OR package_version LIKE :package_version)'; $bindings['package_version'] = "%{$params['app_version_name']}%"; } if (isset($params['android_version']) && $params['android_version'] !== null && trim($params['android_version']) != '') { $where[] = '(os_version IS NULL OR os_version LIKE :os_version)'; $bindings['os_version'] = "%{$params['android_version']}%"; } if (isset($params['brand']) && $params['brand'] !== null && trim($params['brand']) != '') { $where[] = '(brand IS NULL OR brand LIKE :brand)'; $bindings['brand'] = "%{$params['brand']}%"; } if (isset($params['phone_model']) && $params['phone_model'] !== null && trim($params['phone_model']) != '') { $where[] = '(model IS NULL OR model LIKE :model)'; $bindings['model'] = "%{$params['phone_model']}%"; } if (isset($params['product']) && $params['product'] !== null && trim($params['product']) != '') { $where[] = '(product IS NULL OR product LIKE :product)'; $bindings['product'] = "%{$params['product']}%"; } if (isset($params['country']) && $params['country'] !== null && trim($params['country']) != '') { $where[] = '(country IS NULL OR country LIKE :country)'; $bindings['country'] = "%{$params['country']}%"; } if (sizeof($where) == 0) { return false; } $where = implode(' AND ', $where); $query = "\n\t\t\tSELECT\n\t\t\t\tid,\n\t\t\t\tname,\n\t\t\t\tto_emails,\n\t\t\t\tlast_email,\n\t\t\t\temail_delay_minutes\n\t\t\tFROM\n\t\t\t\temail_trigger\n\t\t\tWHERE\n\t\t\t\t{$where}\n\t\t\t\tAND state = 'waiting'\n\t\t"; $records = static::getAdapter()->query($query, $bindings); foreach ($records as $r) { $send = false; $now = gmdate('Y-m-d H:i:s'); if ($r->last_email === null) { $send = true; } else { $then = new \DateTime($r->last_email); $then->modify("+{$r->email_delay_minutes} minute"); // Log::debug("{$then->format('Y-m-d H:i:s')} < {$now}"); if ($then->format('Y-m-d H:i:s') < $now) { $send = true; } } if ($send) { static::update(array('state' => 'sending'), $r->id); $email = Mail::create(); foreach (explode(',', $r->to_emails) as $address) { $email->to($address); } $email->subject($r->name); $body = array("DATE AND TIME (GMT): {$now}"); $body[] = "PACKAGE: {$params['package_name']} {$params['app_version_name']}"; unset($params['package_name'], $params['app_version_name']); // first append one line data foreach ($params as $key => $value) { $value = trim($value); if ($value != '') { if (strpos($value, "\n") === false) { $body[] = strtoupper($key) . ": {$value}"; unset($params[$key]); } } } if (isset($params['stack_trace'])) { $body[] = "STACK TRACE\n" . str_repeat('=', 30) . "\n" . $params['stack_trace']; unset($params['stack_trace']); } // append multiple line data foreach ($params as $key => $value) { $value = trim($value); if ($value != '') { $body[] = strtoupper($key) . "\n" . str_repeat('=', 30) . "\n" . $value; unset($params[$key]); } } $email->body(implode("\n\n", $body)); $email->from('no-reply@' . Request::hostName()); if ($email->send()) { Log::notice("Sent e-mail alert '{$r->name}'"); static::update(array('last_email' => $now, 'last_update' => $now, 'state' => 'waiting'), $r->id); } else { Log::error("Can not send e-mail alert '{$r->name}', sender returned false"); static::update(array('state' => 'waiting'), $r->id); } } } }
/** * Process the request * @param string $os 'Android','iOS' or 'Windows' * * TODO: This logic shouldn't be in the model */ public static function processRequest($os) { if (!isset($_POST['PACKAGE_NAME'])) { Log::warning('Package name is not set! UAS=' . Request::userAgent() . ' POST=' . Json::encode($_POST)); } $time = time(); $ip = $_SERVER['REMOTE_ADDR']; $host = gethostbyaddr($ip); $country = null; $provider = null; if ($host != $ip && strpos($host, '.') !== false) { $country = strtolower(substr($host, strrpos($host, '.') + 1)); if (strlen($country) != 2) { $country = null; } $provider = \Provider::normalizeHostName($host); } Log::notice("Got request time={$time} host={$host} country={$country}"); $values = array('created_at', 'package_name', 'app_version_code', 'app_version_name', 'brand', 'phone_model', 'product', 'stack_trace', 'android_version', 'file_path', 'total_mem_size', 'available_mem_size', 'user_comment', 'user_app_start_date', 'user_crash_date', 'installation_id', 'report_id', 'user_email'); $data = array(); $vals = $_POST; foreach ($values as $key) { $key = strtoupper($key); if (isset($vals[$key])) { $data[$key] = trim($vals[$key]); unset($vals[$key]); } } if (isset($data['user_email']) && ($data['user_email'] == 'N/A' || trim($data['user_email']) == '')) { $data['user_email'] = null; } $data['created_at'] = gmdate('Y-m-d H:i:s'); $data['country'] = $country; $data['provider'] = $provider; $data['os'] = $os; $secureCount = 0; do { $submit = static::create($data); if ($submit === false) { Db::getAdapter()->close(); Log::info("Failed to insert crash submit report for time={$time}, waiting 5 seconds"); set_time_limit(60); sleep(5); Db::getAdapter()->reconnect(); Log::info("Retry {$secureCount} insert crash submit report for time={$time}"); } } while ($submit === false && $secureCount++ < 3); if ($submit !== false) { foreach ($vals as $metaName => $metaValue) { if ($metaValue !== null) { $metaValue = trim($metaValue); if (strlen($metaValue) > 0) { $secureCount = 0; do { $dbMeta = $submit->insertMeta(strtolower($metaName), trim($metaValue)); if ($dbMeta === false) { Db::getAdapter()->close(); Log::info("Failed to insert submit meta for meta={$metaName} time={$time}, waiting 5 seconds"); set_time_limit(60); sleep(5); Db::getAdapter()->reconnect(); Log::info("Retry {$secureCount} meta insert for meta={$metaName} time={$time}"); } } while ($dbMeta === false && $secureCount++ < 2); } } } } \Email\Trigger::processRequest($_POST); Log::notice("Done request time={$time}"); }