/** * This method finds the location of a template file.If the file is not an absolute file path, it searches the class path 'XML' * * This method searches the template directory path from the global configuration array * for the given template. If it exists it returns the full path to the file and if not * it returns false. It seaches the path backwards so that later directories * can override package versions of files. * @param string $template The name of the template file. * @param boolean $raise_error Defaults to true. Raise error if template is not found * @return mixed */ public function findTemplate($template, $raise_error = true) { if (I2CE_FileSearch::isAbsolut($template)) { return $template; } $template_file = I2CE::getFileSearch()->search('XML', $template); if ($template_file) { return $template_file; } else { if ($raise_error) { $this->raiseError("Couldn't find template file: {$template}.\nSearch Path is:\n" . print_r(I2CE::getFileSearch()->getSearchPath('TEMPLATES'), true), E_USER_NOTICE); } return false; } }
/** * Checks to see if the locales for a given module are up to date * @param string $shortname The module * @param string $file -- defaults to null which means use the currently loaded file location for the config file * @returns booolean. True if all is up to date. */ public function checkLocalesUptoDate($shortname, $file = null) { if ($file == null) { if (!$this->config->setIfIsSet($file, "data/{$shortname}/file")) { return false; // the module has never been loaded } $r_file = I2CE_FileSearch::realPath($file); if (!file_exists($r_file) || !is_readable($r_file)) { return false; // the friggin file does not even exist. why are you asking me? } } else { $r_file = I2CE_FileSearch::realPath($file); if (!$r_file) { return false; //invalid file was set. } if (!$this->config->setIfIsSet($saved_file, "data/{$shortname}/file")) { return false; // the module has never been loaded } if (I2CE_FileSearch::realPath($saved_file) != $r_file) { //the loaded config file is different than the one we are looking at return false; } } $locales = I2CE_Locales::getAvailableLocales(); $dirs = array(); if (!$this->config->setIfIsSet($dirs, "data/{$shortname}/paths/CONFIGS", true)) { return true; } $localized = array(); $this->config->setIfIsSet($localized, "status/localized/{$shortname}", true); $basename = basename($r_file); $dirname = dirname($r_file); //I2CE::raiseError("Checking $shortname in " . implode(',',$dirs) . " for locales " . implode(',',$locales) ); foreach ($dirs as $dir) { foreach ($locales as $locale) { if (I2CE_FileSearch::isAbsolut($dir)) { //the config path in theconfig file is absolut. $t_file = $dir . DIRECTORY_SEPARATOR . $locale . DIRECTORY_SEPARATOR . $basename; $localized_file = I2CE_FileSearch::realPath($t_file); $dir = I2CE_FileSearch::relativePath($dir); } else { //the config path in theconfig file is relative to the module file $t_file = $dirname . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . $locale . DIRECTORY_SEPARATOR . $basename; $localized_file = I2CE_FileSearch::realPath($t_file); } if (!$localized_file || !is_file($localized_file) || !is_readable($localized_file)) { continue; } if (!array_key_exists($locale, $localized) || !is_array($localized[$locale])) { I2CE::raiseError("Localization data {$locale} for module {$shortname} has never been loaded"); return false; //we have never examined this one before } foreach (array('file', 'mtime', 'hash') as $key) { if (!array_key_exists($key, $localized[$locale])) { return false; } } //we have examined this one before. $l_file = I2CE_FileSearch::realPath($localized[$locale]['file']); if ($localized_file !== $l_file) { return false; } @($mtime = filemtime($localized_file)); if (!$mtime || !$localized[$locale]['mtime']) { return false; } if ($mtime > $localized[$locale]['mtime']) { return false; } $contents = file_get_contents($localized_file); if (!$contents) { return false; } if ($localized[$locale]['hash'] != md5($contents)) { return false; } } } return true; }
public function getFileData($form, $as_array = false) { if (($uri = $this->getFileURIType($form)) === false) { if ($as_array) { return array(); } else { return false; } } $file = $this->_getFile($form); if ($uri == 'mdn') { return $this->getFileData_mdn($file, $as_array); } else { if ($uri == 'file') { $abs_file = null; if (!I2CE_FileSearch::isAbsolut($file)) { $abs_file = I2CE::getFileSearch()->search($this->getSearchCategory($form), $file); } else { $abs_file = $file; } if (!$abs_file || !is_readable($abs_file)) { I2CE::raiseError("Could not find readable {$file} ({$abs_file})"); return false; } } else { $abs_file = $file; } return $this->getFileData_stream($abs_file, $as_array); } }
/** *Process any script directives on the page. * *@param I2CE_Template $template * *Basically we move any <script> tags from the body (which were inserted by templates) *to the header. There is some additional functionality: *if the body attribute is set, then that script is the javascript for the event with that value. * e.g. <script body='onscoll'>alert('annoying hello')</script> * will result in an alert box being displayed anytime the page is scrolled. Specifically * we set the body node to have attribute 'event' with value the content of the script. * <p/> * If the src attribute is set with a relative file path, then it will serve up the file * with the file dump utility. Nothing is done with the value of a body attribute if the * src attribute is present. */ protected function processScripts() { //we move and <script = src nodes to the header $qry = '//head//script'; //find all script elements in the document that are in the body. $template = self::$page->getTemplate(); $results = $template->query($qry); $head_ids = array(); for ($indx = 0; $indx < $results->length; $indx++) { $script = $results->item($indx); if (!$script->hasAttribute('id')) { continue; } $id = $script->getAttribute('id'); if (!$id) { continue; } $head_ids[$id] = $script; } $head_node = $template->doc->getElementsByTagName("head")->item(0); $body_node = $template->doc->getElementsByTagName("body")->item(0); $qry = '//body//script'; //find all script elements in the document that are in the body. $results = $template->query($qry); for ($indx = 0; $indx < $results->length; $indx++) { $node = $results->item($indx); if ($node->hasAttribute('src')) { $script_src = $node->getAttribute('src'); $attrs = array(); for ($i = 0; $i < $node->attributes->length; $i++) { $attr = $node->attributes->item(0); if ($attr->name == 'src') { continue; } $attrs[$attr->name] = $attr->value; } $use_file_dump = !I2CE_FileSearch::isAbsolut($script_src); $node->parentNode->removeChild($node); $template->addHeaderLink($script_src, $attr, $use_file_dump); } else { //we have some script defined if ($node->hasAttribute('body')) { $body_event = $node->getAttribute('body'); $body_attr_node = $body_node->getAttributeNode($body_event); if (!$body_attr_node) { $body_attr_node = new DOMAttr($body_event); $body_node->setAttributeNode($body_attr_node); } $body_attr_node->value .= $node->textContent; } else { if ($node->hasAttribute('id')) { $id = $node->getAttribute('id'); if (array_key_exists($id, $head_ids)) { $append_node = $head_ids[$id]; foreach ($node->childNodes as $child) { $append_node->appendChild($child); } } else { $head_ids[$id] = $node; $head_node->appendChild($node); } } else { $head_node->appendChild($node); } } } } }
/** * Makes a relative file path or file url absolute relative * to the directory that the function was called in. * * @param string $path a path or URL * @param int $indx the index, from the top level, to * consider the calling file defaults to 0 which is * the file that is calling this. * * @returns string the absolute path or URL */ public static function absolut($path, $indx = 0) { if (!I2CE_FileSearch::isAbsolut($path)) { //it is not an absolute path. make it into one. $dbg_bt = debug_backtrace(); $path = dirname($dbg_bt[$indx]['file']) . DIRECTORY_SEPARATOR . $path; } return realpath($path); }
public function importLocalizedTemplates($localized = array()) { $imported = array(); $i2ceConfigNodeList = $this->template->query('/I2CEConfiguration'); if ($i2ceConfigNodeList->length != 1) { I2CE::raiseError("No configuration template loaded"); return $imported; } else { $configNode = $i2ceConfigNodeList->item(0); } $shortname = trim($configNode->getAttribute('name')); if (!$shortname) { I2CE::raiseError("Could not find module name"); return $imported; } $config = $this->storage->config->data->{$shortname}; $file = false; $config->setIfIsSet($file, "file"); if (!$file) { I2CE::raiseError("Could not source file"); return $imported; } $dirs = array(); $basedir = dirname($file); $basename = basename($file); $config->setIfIsSet($dirs, "paths/CONFIGS", true); if (!is_array($localized)) { $localized = array(); } foreach ($dirs as $dir) { foreach ($this->locales as $locale) { if (array_key_exists($locale, $imported)) { continue; } if (I2CE_FileSearch::isAbsolut($dir)) { //the config path in theconfig file is absolut. $localized_file = I2CE_FileSearch::realPath($dir . DIRECTORY_SEPARATOR . $locale . DIRECTORY_SEPARATOR . $basename); $dir = I2CE_FileSearch::relativePath($dir); } else { //the config path in theconfig file is relative to the module file $localized_file = I2CE_FileSearch::realPath($basedir . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . $locale . DIRECTORY_SEPARATOR . $basename); } if (!$localized_file || !is_file($localized_file) || !is_readable($localized_file)) { continue; } I2CE::raiseError("Loading {$localized_file}"); $loc_template = new I2CE_MagicDataTemplate(); $loc_template->loadRootFile($localized_file); $loc_node = $loc_template->doc->documentElement; if (!$loc_node instanceof DOMNODE) { continue; } $results = $loc_template->query('./configurationGroup', $loc_node); $localenode = null; if ($results->length == 1) { $localenode = $results->item(0); if ($localenode->getAttribute('locale') !== $locale) { I2CE::raiseError("Locale mismatch on {$localized_file}"); continue; } } $results = $loc_template->query('./metadata/version', $loc_node); if ($results->length != 1) { I2CE::raiseError("No version on {$localized_file}"); continue; } $new_vers = trim($results->item(0)->textContent); if (!array_key_exists($locale, $localized) || !is_array($localized[$locale]) || !array_key_exists('vers', $localized[$locale]) || !is_string($localized[$locale]['vers']) || strlen($localized[$locale]['vers']) == 0) { $old_vers = '0'; } else { $old_vers = $localized[$locale]['vers']; } if ($localenode) { $localenode_imported = $this->template->doc->importNode($localenode, true); I2CE::raiseError("Adding in localizations of {$shortname} in {$locale} to process"); $configNode->appendChild($localenode_imported); } $data = array('file' => I2CE_FileSearch::relativePath($localized_file), 'mtime' => @filemtime($localized_file), 'hash' => md5(file_get_contents($localized_file)), 'old_vers' => $old_vers, 'vers' => $new_vers); $imported[$locale] = $data; } } if (count($imported) > 0) { I2CE::raiseError("Found localized config files for " . $shortname . ": " . implode(',', array_keys($imported))); } return $imported; }
/** * Makes a relative file path or file url absolute relative * to the directory that the function was called in. * * @param string $path a path or URL * @param int $indx the index, from the top level, to * consider the calling file defaults to 0 which is * the file that is calling this. * * @returns string the absolute path or URL */ public static function absolut($path, $indx = 0) { if (!I2CE_FileSearch::isAbsolut($path)) { //it is not an absolute path. make it into one. $dbg_bt = debug_backtrace(); $path = dirname($dbg_bt[$indx]['file']) . DIRECTORY_SEPARATOR . $path; } $real_path = realpath($path); if ($real_path === false) { I2CE::raiseError("File does not exist: {$path}"); return $path; } else { return $real_path; } }