public function logAction() { Log::debug('AE1'); Log::notice('AE2'); Log::notice('AE3'); echo "OK"; }
/** * Initialize the application :) * * @throws Exception */ protected static function init() { // second, check all requirements if (!function_exists('spl_autoload_register')) { throw new Exception('SPL is missing! Can not register autoload function'); } // set the error reporting in development mode if (static::inDevelopment()) { error_reporting(E_ALL | E_STRICT); } $config = static::getConfig('application'); // this is just shorthand for Directory Separator defined('DS') || define('DS', DIRECTORY_SEPARATOR); date_default_timezone_set($config['timezone']); // Register Autoload function spl_autoload_register(function ($className) { $classes = \Koldy\Application::$classAliases; if (isset($classes[$className])) { class_alias($classes[$className], $className); } else { $classPath = str_replace('\\', DS, $className); $path = "{$classPath}.php"; include $path; } }); // set the include path to the framework folder (to Koldy and any other // framework(s) located in framework folder with same namespacing style) $includePaths = array(substr(dirname(__FILE__), 0, -6)); $basePath = static::getApplicationPath(); // auto registering modules if there are any defined if (isset($config['auto_register_modules'])) { if (!is_array($config['auto_register_modules'])) { throw new Exception('Invalid config for auto_register_modules in config/application.php'); } foreach ($config['auto_register_modules'] as $moduleName) { $includePaths[] = $basePath . 'modules' . DS . $moduleName . DS . 'controllers'; $includePaths[] = $basePath . 'modules' . DS . $moduleName . DS . 'models'; $includePaths[] = $basePath . 'modules' . DS . $moduleName . DS . 'library'; } } // adding additional include paths if there are any if (isset($config['additional_include_path'])) { if (!is_array($config['additional_include_path'])) { throw new Exception('Invalid config for additional_include_path in config/application.php'); } // so, we need to include something more foreach ($config['additional_include_path'] as $path) { $includePaths[] = $path; } } // register include path of application itself $includePaths[] = $basePath . 'controllers'; $includePaths[] = $basePath . 'library'; $includePaths[] = $basePath . 'models'; // set the include path set_include_path(implode(PATH_SEPARATOR, $includePaths) . PATH_SEPARATOR . get_include_path()); // set the error handler if (isset($config['error_handler']) && $config['error_handler'] instanceof \Closure) { set_error_handler($config['error_handler']); } else { set_error_handler(function ($errno, $errstr, $errfile, $errline) { if (!(error_reporting() & $errno)) { // This error code is not included in error_reporting return; } switch ($errno) { case E_USER_ERROR: \Koldy\Log::error("PHP [{$errno}] {$errstr} in file {$errfile}:{$errline}"); break; case E_USER_WARNING: case E_DEPRECATED: case E_STRICT: \Koldy\Log::warning("PHP [{$errno}] {$errstr} in file {$errfile}:{$errline}"); break; case E_USER_NOTICE: \Koldy\Log::notice("PHP [{$errno}] {$errstr} in file {$errfile}:{$errline}"); break; default: \Koldy\Log::error("PHP Uknown [{$errno}] {$errstr} in file {$errfile}:{$errline}"); break; } /* Don't execute PHP internal error handler */ return true; }); } // register PHP fatal errors register_shutdown_function(function () { if (!defined('KOLDY_FATAL_ERROR_HANDLER')) { define('KOLDY_FATAL_ERROR_HANDLER', true); // to prevent possible recursion if you run into problems with logger $fatalError = error_get_last(); if ($fatalError !== null && $fatalError['type'] == E_ERROR) { $errno = E_ERROR; $errstr = $fatalError['message']; $errfile = $fatalError['file']; $errline = $fatalError['line']; $config = \Koldy\Application::getConfig('application'); if (isset($config['error_handler']) && $config['error_handler'] instanceof \Closure) { call_user_func($config['error_handler'], $errno, $errstr, $errfile, $errline); } else { \Koldy\Log::error("PHP [{$errno}] Fatal error: {$errstr} in {$errfile} on line {$errline}"); } } } }); // all execeptions will be caught in run() method }
/** * (non-PHPdoc) * @see \Koldy\Application\Route\AbstractRoute::exec() */ public function exec() { if (method_exists($this->controllerInstance, 'before')) { $response = $this->controllerInstance->before(); // if "before" method returns anything, then we should not continue if ($response !== null) { return $response; } } $method = $this->getActionMethod(); if (method_exists($this->controllerInstance, $method) || method_exists($this->controllerInstance, '__call')) { // get the return value of your method (json, xml, view object, download, string or nothing) return $this->controllerInstance->{$method}(); } else { // the method we need doesn't exists, so, there is nothing we can do about it any more Log::notice("Can not find method={$method} in class={$this->getControllerClass()} on path={$this->controllerPath} for URI=" . Application::getUri()); static::error(404); } }
/** * The PDO will be initialized only if needed, not on adapter initialization * * @return PDO */ public function getAdapter() { if ($this->pdo === null) { try { $this->tryConnect($this->config); } catch (PDOException $e) { $this->lastException = $e; $this->lastError = $e->getMessage(); $this->pdo = null; if (isset($this->config['backup_connections']) && is_array($this->config['backup_connections'])) { $sizeof = count($this->config['backup_connections']); for ($i = 0; $i < $sizeof && $this->pdo === null; $i++) { $config = $this->config['backup_connections'][$i]; if (isset($config['log_error']) && $config['log_error'] === true) { Log::error("Error connecting to primary database connection on key={$this->configKey}, will now try backup_connection #{$i} {$config['username']}@{$config['host']}"); Log::exception($e); // log exception and continue } else { Log::notice("Error connecting to primary database connection on key={$this->configKey}, will now try backup_connection #{$i} {$config['username']}@{$config['host']}"); } $this->pdo = null; if (isset($config['wait_before_connect'])) { usleep($config['wait_before_connect'] * 1000); } try { $this->tryConnect($config); Log::notice("Connected to backup connection #{$i} ({$config['type']}:{$config['username']}@{$config['host']})"); } catch (PDOException $e) { $this->lastException = $e; $this->lastError = $e->getMessage(); $this->pdo = null; } } } if ($this->pdo === null) { throw new Exception('Error connecting to database'); } } } return $this->pdo; }
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}"); }