Esempio n. 1
0
 /**
  * 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
 }
 /**
  * Actually add new user into database
  */
 public function addUserAjax()
 {
     $validator = Validator::create(array('id' => 'is:0', 'username' => "required|min:2|max:32|unique:\\User,username", 'first_name' => 'max:255', 'last_name' => 'max:255', 'pass' => 'required|min:5|max:255', 'pass2' => 'identical:pass', 'account_type' => 'required', 'timezone' => 'required'));
     if ($validator->failed()) {
         return BootstrapUI::formResponse()->failedOn($validator);
     }
     $data = $validator->getParams();
     unset($data['id'], $data['pass2']);
     $data['pass'] = md5($data['pass']);
     $user = User::create($data);
     if ($user === false) {
         Log::error('Can not create new user');
         return BootstrapUI::formResponse()->failed('Something went wrong');
     }
     return BootstrapUI::formResponse()->redirect(Url::href('system', 'users'));
 }
Esempio n. 3
0
 public function logErrorAction()
 {
     Log::error('TEST ERROR');
 }
Esempio n. 4
0
 /**
  * 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;
 }
Esempio n. 5
0
 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);
             }
         }
     }
 }
Esempio n. 6
0
 /**
  * Insert archive metas
  * @param array $metas
  */
 public function insertMeta(array $metas)
 {
     $ok = array('report_id', 'environment', 'build', 'settings_global', 'settings_system', 'settings_secure', 'device_features', 'shared_preferences', 'initial_configuration', 'crash_configuration', 'dumpsys_meminfo', 'display', 'stack_trace', 'logcat', 'tktal_mem_size', '@evice_features', 'installation_id', 'file_path', 'dropbox', 'is_silent', 'custom_data');
     foreach ($metas as $key => $value) {
         if (in_array($key, $ok)) {
             // first, insert into meta_archive to get last ID
             try {
                 $meta = Meta::create(array('crash_id' => $this->id, 'name' => $key, 'value' => $value));
             } catch (\Exception $e) {
                 Log::error("Can not insert meta for={$this->id} key={$key} value={$value}");
                 Log::exception($e);
                 \Status::setCalculationStatus('Died. Failed on meta table');
                 exit(1);
             }
         } else {
             // key is not recognized, but we'll still write it in database
             try {
                 UnknownMeta::create(array('report_id' => $this->id, 'meta_name' => $key, 'meta_value' => $value));
             } catch (\Exception $e) {
                 Log::error("Can not insert unknown meta report_id={$this->id} meta_name={$key} meta_value={$value}");
                 Log::exception($e);
                 \Status::setCalculationStatus('Died. Failed on unknown meta table');
                 exit(1);
             }
         }
     }
 }
Esempio n. 7
0
 /**
  * (non-PHPdoc)
  * @see SessionHandlerInterface::gc()
  */
 public function gc($maxlifetime)
 {
     $delete = new Delete($this->config['table']);
     $timestamp = time() - $maxlifetime;
     if ($delete->where('time', '<', $timestamp)->exec($this->config['connection']) === false) {
         Log::error("Session GC: Error deleting session record from database that are older then maxlifetime={$maxlifetime} (older then timestamp {$timestamp})");
         return false;
     }
     return true;
 }