Example #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
 }
Example #2
0
 /**
  * (non-PHPdoc)
  * @see \Koldy\Cache\Driver\AbstractDriver::deleteOld()
  */
 public function deleteOld($olderThenSeconds = null)
 {
     if ($olderThenSeconds === null) {
         $olderThenSeconds = $this->defaultDuration;
     }
     clearstatcache();
     /**
      * This is probably not good since lifetime is written in file
      * But going into every file and read might be even worse idea
      * XXX: Think about this
      */
     foreach (Directory::read($this->path) as $fullPath => $fileName) {
         $timeCreated = @filemtime($fullPath);
         if ($timeCreated !== false) {
             // successfully red the file modification time
             if (time() - $olderThenSeconds > $timeCreated) {
                 // it is old enough to be removed
                 if (!@unlink($fullPath)) {
                     Log::warning("Can not delete cached file on path {$fullPath}");
                 }
             }
         }
     }
 }
Example #3
0
 /**
  * If you statically created new record in database to the table with auto
  * incrementing field, then you can use this static method to get the
  * generated primary key
  * 
  * @return integer
  * @example
  * 
  * 		if (User::create(array('first_name' => 'John', 'last_name' => 'Doe'))) {
  *   		echo User::getLastInsertId();
  *   	}
  */
 public static function getLastInsertId()
 {
     if (static::$autoIncrement) {
         return static::getAdapter(static::$connection)->getLastInsertId();
     } else {
         Log::warning('Can not get last insert ID when model ' . get_called_class() . ' doesn\'t have auto_increment field');
         return null;
     }
 }
Example #4
0
 /**
  * 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}");
 }