public function testConfig() { $this->assertFalse(defined('SITE_ROOT')); $configFile = dirname(__FILE__) . '/files/siteConfig.ini'; $this->assertTrue(file_exists($configFile)); $x = new SiteConfig($configFile, null); $this->assertTrue(is_object($x)); // $this->assertTrue(is_array($x->config)); $this->assertTrue(is_array($GLOBALS)); $myFs = new FileSystem(dirname(__FILE__)); // so... set things as constants and GLOBALS foreach ($x->get_valid_sections() as $sectionName) { $x->make_section_constants($sectionName); $x->make_section_globals($sectionName); } $this->assertEquals(ToolBox::resolve_path_with_dots(dirname($configFile) . '/..'), $GLOBALS['SITE_ROOT']); $this->assertEquals($GLOBALS['SITE_ROOT'], $GLOBALS['SITEROOT']); //Test to make sure the constant and the global are identical. $this->assertEquals(constant('SITE_ROOT'), $GLOBALS['SITE_ROOT']); $this->assertEquals('CS_SESSID', constant('SESSION_NAME')); $this->assertTrue(isset($GLOBALS['SESSION_NAME'])); $this->assertFalse(isset($GLOBALS['API_AUTHTOKEN'])); $testProjectSection = $x->get_section('cs-project'); $this->assertEquals(1, count($testProjectSection)); $a = $x->get_fullConfig(); $this->assertEquals($a['cs-project']['api_authtoken'], $a['test']['TOKEN']); $this->assertEquals($a['cs-project']['api_authtoken'], $a['test']['TOKEN2']); $keys = array_keys($a); $this->assertEquals($x->get_valid_sections(), $keys); foreach ($keys as $name) { $this->assertEquals($a[$name], $x->get_section($name)); } }
protected function parse_value($value, array $replacements = null) { //remove double-slashes (//) $value = preg_replace('/[\\/]{2,}/', '\\/', $value); //remove leading slash for string replaces (i.e. "{/MAIN/SITE_ROOT}" becomes "{MAIN/SITE_ROOT}") $value = preg_replace('/{\\//', '{', $value); //replace special vars. $value = ToolBox::mini_parser($value, $replacements, '{', '}'); if (strlen($value)) { $value = ToolBox::resolve_path_with_dots($value); } return $value; }
/** * Initializes some internal objects, variables, and so on. */ protected function initialize_locals($mainTemplateFile) { //replace multiple slashes with a single one to avoid confusing other logic... $mainTemplateFile = preg_replace('/(\\/){2,}/', '/', $mainTemplateFile); $showMatches = array(); $numMatches = preg_match_all('/\\//', $mainTemplateFile, $showMatches); if ($numMatches == 1 && preg_match('/^/', $mainTemplateFile)) { $mainTemplateFile = preg_replace('/^\\//', '', $mainTemplateFile); } if (isset($mainTemplateFile) && strlen($mainTemplateFile) && is_dir(dirname($mainTemplateFile)) && dirname($mainTemplateFile) != '.') { $this->siteRoot = dirname($mainTemplateFile); if (preg_match('/\\//', $this->siteRoot) && preg_match('/templates/', $this->siteRoot)) { $this->siteRoot .= "/.."; } } elseif (defined('SITE_ROOT') && is_dir(constant('SITE_ROOT'))) { $this->siteRoot = constant('SITE_ROOT'); } elseif (is_dir($_SERVER['DOCUMENT_ROOT'] . '/templates')) { $this->siteRoot = $_SERVER['DOCUMENT_ROOT'] . '/templates'; } else { throw new exception(__METHOD__ . ": cannot locate siteRoot from main template file (" . $mainTemplateFile . ")"); } $fs = new FileSystem(dirname(__FILE__)); // $this->siteRoot = $fs->resolve_path_with_dots($this->siteRoot); $this->siteRoot = ToolBox::resolve_path_with_dots($this->siteRoot); $this->tmplDir = $this->siteRoot . '/templates'; if (defined('CS_TEMPLATE_BASE_DIR')) { $this->tmplDir = constant('CS_TEMPLATE_BASE_DIR'); } $this->libDir = $this->siteRoot . '/lib'; if (!is_dir($this->tmplDir)) { throw new Exception(__METHOD__ . ": invalid templates folder (" . $this->tmplDir . "), siteRoot=(" . $this->siteRoot . ")"); } //if there have been some global template vars (or files) set, read 'em in here. if (isset($GLOBALS['templateVars']) && is_array($GLOBALS['templateVars']) && count($GLOBALS['templateVars'])) { foreach ($GLOBALS['templateVars'] as $key => $value) { $this->add_template_var($key, $value); } } if (isset($GLOBALS['templateFiles']) && is_array($GLOBALS['templateFiles'])) { foreach ($GLOBALS['templateFiles'] as $key => $value) { $this->templateFiles[$key] = $value; } } unset($GLOBALS['templateVars'], $GLOBALS['templateFiles']); if (!preg_match('/^\\//', $mainTemplateFile)) { $mainTemplateFile = $this->tmplDir . "/" . $mainTemplateFile; } $this->mainTemplate = $mainTemplateFile; //load the default layout }
/** * Turns out there's no real good way to view the parsed data without * evaluating GLOBALS and constants. Ick. */ public function testConfig() { $this->assertFalse(defined('SITE_ROOT')); $configFile = dirname(__FILE__) . '/files/siteConfig.xml'; $this->assertTrue(file_exists($configFile)); $x = new cs_siteConfig($configFile, null); $this->assertTrue(is_object($x)); $this->assertTrue(is_array($x->config)); foreach ($x->get_valid_sections() as $section) { $x->make_section_constants($section); $x->make_section_globals($section); } $this->assertTrue(is_array($GLOBALS)); $this->assertEquals(ToolBox::resolve_path_with_dots(dirname($configFile) . '/..'), $GLOBALS['SITE_ROOT']); $this->assertEquals($GLOBALS['SITE_ROOT'], $GLOBALS['SITEROOT']); //BUG!!!! see https://github.com/crazedsanity/cs-webapplibs/issues/26 $this->assertEquals(constant('SITE_ROOT'), $GLOBALS['SITE_ROOT']); $this->assertEquals('CS_SESSID', constant('SESSION_NAME')); $this->assertTrue(!empty($GLOBALS['SESSION_NAME'])); $this->assertEquals(constant('session_db_host'), constant('DB_PG_HOST')); $this->assertEquals(constant('cs_webupgradedb-RWDIR'), constant('CS_RWDIR')); $this->assertFalse(isset($GLOBALS['API_AUTHTOKEN'])); $this->assertFalse(defined('API_AUTHTOKEN')); }
/** * Takes the given filename & returns the ABSOLUTE pathname: checks to see if the given * string already has the absolute path in it. */ protected function filename2absolute($filename) { clearstatcache(); $filename = ToolBox::resolve_path_with_dots($filename); //If it's a single filename beginning with a slash, strip the slash. $x = array(); $numSlashes = preg_match_all('/\\//', $filename, $x); if (preg_match('/^\\/[\\w]/', $filename) && !preg_match('/^\\/\\./', $filename) && $numSlashes == 1) { $filename = preg_replace('/^\\//', '', $filename); } if (preg_match("/^\\//", $filename)) { $retval = $filename; } else { $retval = $this->realcwd . '/' . $filename; $retval = ToolBox::resolve_path_with_dots($retval); } if (!$this->check_chroot($retval, FALSE)) { ToolBox::debug_print(func_get_args()); throw new exception(__METHOD__ . ": file (" . $retval . ") is outside of allowed directory"); } return $retval; }
/** * @expectedException InvalidArgumentException */ public function test_resolvePathException() { ToolBox::resolve_path_with_dots(NULL); }
/** * Fixes issues with extra slashes and such. * * @param $path <str> path to fix * * @return <str> PASS: this is the fixed path */ public static function fix_path($path) { $retval = preg_replace('~/$~', '', ToolBox::resolve_path_with_dots('/' . $path)); return $retval; }