예제 #1
0
 public static function getConnection($configName, $alternateHostname = null, $alternateDatabase = null, $storeInInstance = true)
 {
     $config = \Nf\Registry::get('config');
     if (!isset($config->db->{$configName})) {
         throw new \Exception('The adapter "' . $configName . '" is not defined in the config file');
     }
     $defaultHostname = $config->db->{$configName}->params->hostname;
     $defaultDatabase = $config->db->{$configName}->params->database;
     $hostname = $alternateHostname !== null ? $alternateHostname : $defaultHostname;
     $database = $alternateDatabase !== null ? $alternateDatabase : $defaultDatabase;
     // if the connection has already been created and if we store the connection in memory for future use
     if (isset(self::$_connections[$configName . '-' . $hostname . '-' . $database]) && $storeInInstance) {
         return self::$_connections[$configName . '-' . $hostname . '-' . $database];
     } else {
         // optional profiler config
         $profilerConfig = isset($config->db->{$configName}->profiler) ? (array) $config->db->{$configName}->profiler : null;
         if ($profilerConfig != null) {
             $profilerConfig['name'] = $configName;
         }
         // or else we create a new connection
         $dbConfig = array('adapter' => $config->db->{$configName}->adapter, 'params' => array('hostname' => $hostname, 'username' => $config->db->{$configName}->params->username, 'password' => $config->db->{$configName}->params->password, 'database' => $database, 'charset' => $config->db->{$configName}->params->charset), 'profiler' => $profilerConfig);
         // connection with the factory method
         $dbConnection = self::factory($dbConfig);
         if ($storeInInstance) {
             self::$_connections[$configName . '-' . $hostname . '-' . $database] = $dbConnection;
         }
         return $dbConnection;
     }
 }
예제 #2
0
 public function __construct($config)
 {
     require_once realpath(Registry::get('libraryPath') . '/php/classes/FirePHPCore/FirePHP.class.php');
     $this->firephp = \FirePHP::getInstance(true);
     $this->label = static::LABEL_TEMPLATE;
     $front = \Nf\Front::getInstance();
     $this->dbName = $config['name'];
     $front->registerMiddleware($this);
     $this->payload = array(array('Duration', 'Query', 'Time'));
 }
예제 #3
0
 public function testDeepReplace()
 {
     $iniContent = 'testReplace=aaa';
     $filename = Registry::get('applicationPath') . '/cache/test.' . date('YmdHis') . rand(1, 1000) . '.initmp';
     file_put_contents($filename, $iniContent);
     $iniTest = Ini::parse($filename);
     $iniReplaced = Ini::deepReplace($iniTest, 'a', 'z');
     unlink($filename);
     $this->assertTrue($iniReplaced->testReplace === 'zzz');
 }
예제 #4
0
 public function loadLabels($locale, $force = false)
 {
     if (!$this->labelsLoaded || $force) {
         if (file_exists(\Nf\Registry::get('applicationPath') . '/labels/' . $locale . '.ini')) {
             $this->labels = parse_ini_file(\Nf\Registry::get('applicationPath') . '/labels/' . $locale . '.ini', true);
             $this->labelsLoaded = true;
         } else {
             throw new \Exception('Cannot load labels for this locale (' . $locale . ')');
         }
     }
 }
예제 #5
0
 public static function setErrorDisplaying()
 {
     if (\Nf\Registry::isRegistered('config')) {
         $config = \Nf\Registry::get('config');
         if (isset($config->error->displayPHPErrors) && (strtolower($config->error->displayPHPErrors) == 'off' || $config->error->displayPHPErrors == 0)) {
             ini_set('display_errors', 0);
             // don't display the errors
         } else {
             ini_set('display_errors', 1);
             // display the errors
         }
     } else {
         ini_set('display_errors', 1);
     }
 }
예제 #6
0
 public function getNamedUrl($name, $params = array(), $version = null, $locale = null)
 {
     if ($version == null) {
         $version = Registry::get('version');
     }
     if ($locale == null) {
         $locale = Registry::get('locale');
     }
     $foundRoute = false;
     if (isset($this->allRoutesByVersionAndLocale[$version][$locale][$name])) {
         $url = $this->allRoutesByVersionAndLocale[$version][$locale][$name]['regexp'];
         $foundRoute = true;
     } elseif (isset($this->allRoutesByVersionAndLocale[$version]['*'][$name])) {
         $url = $this->allRoutesByVersionAndLocale[$version]['*'][$name]['regexp'];
         $foundRoute = true;
     } elseif (isset($this->allRoutesByVersionAndLocale['*']['*'][$name])) {
         $url = $this->allRoutesByVersionAndLocale['*']['*'][$name]['regexp'];
         $foundRoute = true;
     }
     if ($foundRoute) {
         preg_match_all('/\\(([\\w_]+):([^)]+)\\)/im', $url, $result, PREG_SET_ORDER);
         for ($matchi = 0; $matchi < count($result); $matchi++) {
             if (isset($params[$result[$matchi][1]])) {
                 $url = str_replace($result[$matchi][0], $params[$result[$matchi][1]], $url);
             }
         }
         return $url;
     } else {
         throw new \Exception('Cannot find route named "' . $name . '" (version=' . $version . ', locale=' . $locale . ')');
     }
 }
예제 #7
0
 public function getNamedUrl($name, $params = array(), $version = null, $locale = null, $getFullUrl = true)
 {
     if ($version == null) {
         $version = Registry::get('version');
     }
     if ($locale == null) {
         $locale = Registry::get('locale');
     }
     // get the actual domain name from the url.ini
     $domainName = '';
     if ($getFullUrl) {
         if (!isset($this->allVersionsUrls[$version][$locale])) {
             $urlIni = Registry::get('urlIni');
             $localeSuffix = $urlIni->suffixes->{$locale};
             $versionPrefix = $urlIni->versions->{$version};
             if (strpos($versionPrefix, '|') !== false) {
                 $arrVersionPrefix = explode('|', $versionPrefix);
                 $versionPrefix = $arrVersionPrefix[0];
                 if ($versionPrefix == '<>') {
                     $versionPrefix = '';
                 }
             }
             $domainName = str_replace('[version]', $versionPrefix, $localeSuffix);
             if (!isset($this->allVersionsUrls[$version])) {
                 $this->allVersionsUrls[$version] = array();
                 $this->allVersionsUrls[$version][$locale] = $domainName;
             }
         } else {
             $domainName = $this->allVersionsUrls[$version][$locale];
         }
     }
     $foundRoute = false;
     if (isset($this->allRoutesByVersionAndLocale[$version][$locale][$name])) {
         $url = $this->allRoutesByVersionAndLocale[$version][$locale][$name]['regexp'];
         $foundRoute = true;
     } elseif (isset($this->allRoutesByVersionAndLocale[$version]['*'][$name])) {
         $url = $this->allRoutesByVersionAndLocale[$version]['*'][$name]['regexp'];
         $foundRoute = true;
     } elseif (isset($this->allRoutesByVersionAndLocale['*']['*'][$name])) {
         $url = $this->allRoutesByVersionAndLocale['*']['*'][$name]['regexp'];
         $foundRoute = true;
     }
     if ($foundRoute) {
         preg_match_all('/\\(([\\w_]+):([^)]+)\\)/im', $url, $result, PREG_SET_ORDER);
         for ($matchi = 0; $matchi < count($result); $matchi++) {
             if (isset($params[$result[$matchi][1]])) {
                 $url = str_replace($result[$matchi][0], $params[$result[$matchi][1]], $url);
             }
         }
         if ($getFullUrl) {
             return $domainName . '/' . $url;
         } else {
             return $url;
         }
     } else {
         throw new \Exception('Cannot find route named "' . $name . '" (version=' . $version . ', locale=' . $locale . ')');
     }
 }
예제 #8
0
 private function setTimezone()
 {
     $config = Registry::get('config');
     if (isset($config->date->timezone)) {
         try {
             date_default_timezone_set($config->date->timezone);
         } catch (\Exception $e) {
             echo 'timezone set failed (' . $config->date->timezone . ') is not a valid timezone';
         }
     }
 }
예제 #9
0
 public function setApplicationNamespace($namespace)
 {
     $this->_applicationNamespace = $namespace;
     \Nf\Registry::set('applicationNamespace', $namespace);
 }
예제 #10
0
 public function setBasePath($path)
 {
     $config = \Nf\Registry::get('config');
     // configuration de Smarty
     $this->_smarty->setTemplateDir(array(\Nf\Registry::get('applicationPath') . '/application/' . \Nf\Registry::get('version') . '/' . $path . '/views/', \Nf\Registry::get('libraryPath') . '/php/application/' . \Nf\Registry::get('version') . '/' . $path . '/views/'));
     // répertoire du cache Smarty
     $cacheDirectory = realpath(\Nf\Registry::get('applicationPath')) . '/cache/smarty/' . \Nf\Registry::get('version') . '/' . \Nf\Registry::get('locale') . '/' . $path . '/';
     // répertoire des templates compilés
     $compileDirectory = realpath(\Nf\Registry::get('applicationPath')) . '/cache/templates_c/' . \Nf\Registry::get('version') . '/' . \Nf\Registry::get('locale') . '/' . $path . '/';
     $configDirectory = realpath(\Nf\Registry::get('applicationPath')) . '/configs/' . \Nf\Registry::get('version') . '/' . \Nf\Registry::get('locale') . '/' . $path . '/';
     $pluginsDirectories = array(realpath(\Nf\Registry::get('applicationPath') . '/plugins/'), realpath(\Nf\Registry::get('libraryPath') . '/php/plugins/'), realpath(\Nf\Registry::get('libraryPath') . '/php/classes/Smarty/plugins/'));
     \Nf\File::mkdir($cacheDirectory, 0755, true);
     \Nf\File::mkdir($compileDirectory, 0755, true);
     $this->_smarty->setUseSubDirs(true);
     // répertoire de cache de smarty
     $this->_smarty->setCacheDir($cacheDirectory);
     // répertoire de compilation
     $this->_smarty->setCompileDir($compileDirectory);
     // répertoire des configs smarty des applis
     $this->_smarty->setConfigDir($configDirectory);
     // répertoire des plugins
     foreach ($pluginsDirectories as $pluginsDirectory) {
         $this->_smarty->addPluginsDir($pluginsDirectory);
     }
     $this->_smarty->left_delimiter = $config->view->smarty->leftDelimiter;
     $this->_smarty->right_delimiter = $config->view->smarty->rightDelimiter;
     // dev : we disable Smarty's caching
     if (\Nf\Registry::get('environment') == 'dev') {
         $this->_smarty->caching = false;
         $this->_smarty->force_compile = true;
         $this->_smarty->setCompileCheck(true);
     }
     // only one file generated for each rendering
     $this->_smarty->merge_compiled_includes = true;
     // send the registry to the view
     $this->_smarty->assign('_registry', \Nf\Registry::getInstance());
     // send the label Manager to the view
     $this->_smarty->assign('_labels', \Nf\LabelManager::getInstance());
     // $this->_smarty->testInstall();
 }
예제 #11
0
 public function redirectForTrailingSlash()
 {
     $config = \Nf\Registry::get('config');
     $redirectionUrl = false;
     $requestParams = '';
     $requestPage = '/' . $this->_uri;
     // we don't redirect for the home page...
     if ($requestPage != '/' && mb_strpos($requestPage, '/?') !== 0) {
         // the url without the params is :
         if (mb_strpos($requestPage, '?') !== false) {
             $requestParams = mb_substr($requestPage, mb_strpos($requestPage, '?'), mb_strlen($requestPage) - mb_strpos($requestPage, '?'));
             $requestPage = mb_substr($requestPage, 0, mb_strpos($requestPage, '?'));
         }
         if (isset($config->trailingSlash->needed) && $config->trailingSlash->needed == true) {
             if (mb_substr($requestPage, -1, 1) != '/') {
                 $redirectionUrl = 'http://' . $_SERVER['HTTP_HOST'] . $requestPage . '/' . $requestParams;
             }
         } else {
             if (mb_substr($requestPage, -1, 1) == '/') {
                 $redirectionUrl = 'http://' . $_SERVER['HTTP_HOST'] . rtrim($requestPage, '/') . $requestParams;
             }
         }
         if ($redirectionUrl !== false) {
             $response = new \Nf\Front\Response\Http();
             $response->redirect($redirectionUrl, 301);
             $response->sendHeaders();
             return true;
         }
     }
     return false;
 }
예제 #12
0
$autoloader = new \Nf\Autoloader();
$autoloader->addNamespaceRoot($applicationNamespace, $applicationPath . '/models');
$autoloader->addNamespaceRoot('Nf', $libraryPath . '/Nf');
$autoloader->addNamespaceRoot('Library', $libraryPath . '/php/models');
$autoloader->addNamespaceRoot('', $applicationPath . '/models');
$autoloader->register();
\Nf\Registry::set('libraryPath', $libraryPath);
\Nf\Registry::set('applicationPath', $applicationPath);
$urlIni = \Nf\Ini::parse(\Nf\Registry::get('applicationPath') . '/configs/url.ini', true);
\Nf\Registry::set('urlIni', $urlIni);
\Nf\Registry::set('environment', 'test');
\Nf\Registry::set('locale', $urlIni->i18n->defaultLocale);
\Nf\Registry::set('version', 'cli');
$config = \Nf\Ini::parse(\Nf\Registry::get('applicationPath') . '/configs/config.ini', true, \Nf\Registry::get('locale') . '_' . \Nf\Registry::get('environment') . '_' . \Nf\Registry::get('version'));
\Nf\Registry::set('config', $config);
\Nf\Error\Handler::setErrorDisplaying();
$front = \Nf\Front::getInstance();
$request = new \Nf\Front\Request\Cli('/');
$front->setRequest($request);
$response = new \Nf\Front\Response\Cli();
$front->setResponse($response);
$front->setApplicationNamespace($applicationNamespace);
// routing
$router = \Nf\Router::getInstance();
$front->setRouter($router);
$router->addAllRoutes();
$labelManager = \Nf\LabelManager::getInstance();
$labelManager->loadLabels(\Nf\Registry::get('locale'));
$localization = \Nf\Localization::getInstance();
$localization->setLocale(\Nf\Registry::get('locale'));
예제 #13
0
 public function setBasePath($path)
 {
     $this->_templateDirectory = Registry::get('applicationPath') . '/application/' . Registry::get('version') . '/' . $path . '/views/';
     $this->_configDirectory = Registry::get('applicationPath') . '/configs/' . Registry::get('version') . '/' . Registry::get('locale') . '/' . $path . '/';
 }
예제 #14
-1
 public function log($err)
 {
     $config = Registry::get('config');
     // We need a transport - UDP via port 12201 is standard.
     $transport = new \Gelf\Transport\UdpTransport($config->error->logger->gelf->ip, $config->error->logger->gelf->port, \Gelf\Transport\UdpTransport::CHUNK_SIZE_LAN);
     // While the UDP transport is itself a publisher, we wrap it in a real Publisher for convenience
     // A publisher allows for message validation before transmission, and it calso supports to send messages
     // to multiple backends at once
     $publisher = new \Gelf\Publisher();
     $publisher->addTransport($transport);
     $fullMessage = \Nf\Front\Response\Cli::displayErrorHelper($err);
     // Now we can create custom messages and publish them
     $message = new \Gelf\Message();
     $message->setShortMessage(Handler::recursiveArrayToString($err['message']))->setLevel(\Psr\Log\LogLevel::ERROR)->setFile($err['file'])->setLine($err['line'])->setFullMessage($fullMessage);
     if (php_sapi_name() == 'cli') {
         global $argv;
         $message->setAdditional('url', 'su ' . $_SERVER['LOGNAME'] . ' -c "php ' . Registry::get('applicationPath') . '/html/' . implode(' ', $argv) . '"');
     } else {
         $message->setAdditional('url', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
     }
     if (isset($config->error->logger->additionals)) {
         foreach ($config->error->logger->additionals as $additionalName => $additionalValue) {
             $message->setAdditional($additionalName, $additionalValue);
         }
     }
     if ($publisher->publish($message)) {
         return true;
     } else {
         return false;
     }
 }