/**
  * diagnose hiweb_tpl setup
  * If $errors is secified, the diagnostic report will be appended to the array, rather than being output.
  *
  * @param  hiweb_tpl $smurty hiweb_tpl instance to test
  * @param  array  $errors array to push results into rather than outputting them
  *
  * @return bool   status, true if everything is fine, false else
  */
 public static function testInstall(hiweb_tpl $smurty, &$errors = null)
 {
     $status = true;
     if ($errors === null) {
         echo "<PRE>\n";
         echo "hiweb_tpl Installation test...\n";
         echo "Testing template directory...\n";
     }
     $_stream_resolve_include_path = function_exists('stream_resolve_include_path');
     // test if all registered template_dir are accessible
     foreach ($smurty->getTemplateDir() as $template_dir) {
         $_template_dir = $template_dir;
         $template_dir = realpath($template_dir);
         // resolve include_path or fail existence
         if (!$template_dir) {
             if ($smurty->use_include_path && !preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $_template_dir)) {
                 // try PHP include_path
                 if ($_stream_resolve_include_path) {
                     $template_dir = stream_resolve_include_path($_template_dir);
                 } else {
                     $template_dir = Smurty_Internal_Get_Include_Path::getIncludePath($_template_dir);
                 }
                 if ($template_dir !== false) {
                     if ($errors === null) {
                         echo "{$template_dir} is OK.\n";
                     }
                     continue;
                 } else {
                     $status = false;
                     $message = "FAILED: {$_template_dir} does not exist (and couldn't be found in include_path either)";
                     if ($errors === null) {
                         echo $message . ".\n";
                     } else {
                         $errors['template_dir'] = $message;
                     }
                     continue;
                 }
             } else {
                 $status = false;
                 $message = "FAILED: {$_template_dir} does not exist";
                 if ($errors === null) {
                     echo $message . ".\n";
                 } else {
                     $errors['template_dir'] = $message;
                 }
                 continue;
             }
         }
         if (!is_dir($template_dir)) {
             $status = false;
             $message = "FAILED: {$template_dir} is not a directory";
             if ($errors === null) {
                 echo $message . ".\n";
             } else {
                 $errors['template_dir'] = $message;
             }
         } elseif (!is_readable($template_dir)) {
             $status = false;
             $message = "FAILED: {$template_dir} is not readable";
             if ($errors === null) {
                 echo $message . ".\n";
             } else {
                 $errors['template_dir'] = $message;
             }
         } else {
             if ($errors === null) {
                 echo "{$template_dir} is OK.\n";
             }
         }
     }
     if ($errors === null) {
         echo "Testing compile directory...\n";
     }
     // test if registered compile_dir is accessible
     $__compile_dir = $smurty->getCompileDir();
     $_compile_dir = realpath($__compile_dir);
     if (!$_compile_dir) {
         $status = false;
         $message = "FAILED: {$__compile_dir} does not exist";
         if ($errors === null) {
             echo $message . ".\n";
         } else {
             $errors['compile_dir'] = $message;
         }
     } elseif (!is_dir($_compile_dir)) {
         $status = false;
         $message = "FAILED: {$_compile_dir} is not a directory";
         if ($errors === null) {
             echo $message . ".\n";
         } else {
             $errors['compile_dir'] = $message;
         }
     } elseif (!is_readable($_compile_dir)) {
         $status = false;
         $message = "FAILED: {$_compile_dir} is not readable";
         if ($errors === null) {
             echo $message . ".\n";
         } else {
             $errors['compile_dir'] = $message;
         }
     } elseif (!is_writable($_compile_dir)) {
         $status = false;
         $message = "FAILED: {$_compile_dir} is not writable";
         if ($errors === null) {
             echo $message . ".\n";
         } else {
             $errors['compile_dir'] = $message;
         }
     } else {
         if ($errors === null) {
             echo "{$_compile_dir} is OK.\n";
         }
     }
     if ($errors === null) {
         echo "Testing plugins directory...\n";
     }
     // test if all registered plugins_dir are accessible
     // and if core plugins directory is still registered
     $_core_plugins_dir = realpath(dirname(__FILE__) . '/../plugins');
     $_core_plugins_available = false;
     foreach ($smurty->getPluginsDir() as $plugin_dir) {
         $_plugin_dir = $plugin_dir;
         $plugin_dir = realpath($plugin_dir);
         // resolve include_path or fail existence
         if (!$plugin_dir) {
             if ($smurty->use_include_path && !preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $_plugin_dir)) {
                 // try PHP include_path
                 if ($_stream_resolve_include_path) {
                     $plugin_dir = stream_resolve_include_path($_plugin_dir);
                 } else {
                     $plugin_dir = Smurty_Internal_Get_Include_Path::getIncludePath($_plugin_dir);
                 }
                 if ($plugin_dir !== false) {
                     if ($errors === null) {
                         echo "{$plugin_dir} is OK.\n";
                     }
                     continue;
                 } else {
                     $status = false;
                     $message = "FAILED: {$_plugin_dir} does not exist (and couldn't be found in include_path either)";
                     if ($errors === null) {
                         echo $message . ".\n";
                     } else {
                         $errors['plugins_dir'] = $message;
                     }
                     continue;
                 }
             } else {
                 $status = false;
                 $message = "FAILED: {$_plugin_dir} does not exist";
                 if ($errors === null) {
                     echo $message . ".\n";
                 } else {
                     $errors['plugins_dir'] = $message;
                 }
                 continue;
             }
         }
         if (!is_dir($plugin_dir)) {
             $status = false;
             $message = "FAILED: {$plugin_dir} is not a directory";
             if ($errors === null) {
                 echo $message . ".\n";
             } else {
                 $errors['plugins_dir'] = $message;
             }
         } elseif (!is_readable($plugin_dir)) {
             $status = false;
             $message = "FAILED: {$plugin_dir} is not readable";
             if ($errors === null) {
                 echo $message . ".\n";
             } else {
                 $errors['plugins_dir'] = $message;
             }
         } elseif ($_core_plugins_dir && $_core_plugins_dir == realpath($plugin_dir)) {
             $_core_plugins_available = true;
             if ($errors === null) {
                 echo "{$plugin_dir} is OK.\n";
             }
         } else {
             if ($errors === null) {
                 echo "{$plugin_dir} is OK.\n";
             }
         }
     }
     if (!$_core_plugins_available) {
         $status = false;
         $message = "WARNING: hiweb_tpl's own libs/plugins is not available";
         if ($errors === null) {
             echo $message . ".\n";
         } elseif (!isset($errors['plugins_dir'])) {
             $errors['plugins_dir'] = $message;
         }
     }
     if ($errors === null) {
         echo "Testing cache directory...\n";
     }
     // test if all registered cache_dir is accessible
     $__cache_dir = $smurty->getCacheDir();
     $_cache_dir = realpath($__cache_dir);
     if (!$_cache_dir) {
         $status = false;
         $message = "FAILED: {$__cache_dir} does not exist";
         if ($errors === null) {
             echo $message . ".\n";
         } else {
             $errors['cache_dir'] = $message;
         }
     } elseif (!is_dir($_cache_dir)) {
         $status = false;
         $message = "FAILED: {$_cache_dir} is not a directory";
         if ($errors === null) {
             echo $message . ".\n";
         } else {
             $errors['cache_dir'] = $message;
         }
     } elseif (!is_readable($_cache_dir)) {
         $status = false;
         $message = "FAILED: {$_cache_dir} is not readable";
         if ($errors === null) {
             echo $message . ".\n";
         } else {
             $errors['cache_dir'] = $message;
         }
     } elseif (!is_writable($_cache_dir)) {
         $status = false;
         $message = "FAILED: {$_cache_dir} is not writable";
         if ($errors === null) {
             echo $message . ".\n";
         } else {
             $errors['cache_dir'] = $message;
         }
     } else {
         if ($errors === null) {
             echo "{$_cache_dir} is OK.\n";
         }
     }
     if ($errors === null) {
         echo "Testing configs directory...\n";
     }
     // test if all registered config_dir are accessible
     foreach ($smurty->getConfigDir() as $config_dir) {
         $_config_dir = $config_dir;
         $config_dir = realpath($config_dir);
         // resolve include_path or fail existence
         if (!$config_dir) {
             if ($smurty->use_include_path && !preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $_config_dir)) {
                 // try PHP include_path
                 if ($_stream_resolve_include_path) {
                     $config_dir = stream_resolve_include_path($_config_dir);
                 } else {
                     $config_dir = Smurty_Internal_Get_Include_Path::getIncludePath($_config_dir);
                 }
                 if ($config_dir !== false) {
                     if ($errors === null) {
                         echo "{$config_dir} is OK.\n";
                     }
                     continue;
                 } else {
                     $status = false;
                     $message = "FAILED: {$_config_dir} does not exist (and couldn't be found in include_path either)";
                     if ($errors === null) {
                         echo $message . ".\n";
                     } else {
                         $errors['config_dir'] = $message;
                     }
                     continue;
                 }
             } else {
                 $status = false;
                 $message = "FAILED: {$_config_dir} does not exist";
                 if ($errors === null) {
                     echo $message . ".\n";
                 } else {
                     $errors['config_dir'] = $message;
                 }
                 continue;
             }
         }
         if (!is_dir($config_dir)) {
             $status = false;
             $message = "FAILED: {$config_dir} is not a directory";
             if ($errors === null) {
                 echo $message . ".\n";
             } else {
                 $errors['config_dir'] = $message;
             }
         } elseif (!is_readable($config_dir)) {
             $status = false;
             $message = "FAILED: {$config_dir} is not readable";
             if ($errors === null) {
                 echo $message . ".\n";
             } else {
                 $errors['config_dir'] = $message;
             }
         } else {
             if ($errors === null) {
                 echo "{$config_dir} is OK.\n";
             }
         }
     }
     if ($errors === null) {
         echo "Testing sysplugin files...\n";
     }
     // test if hiweb-core-tpl are available
     $source = HIWEB_CORE_HTML_ASSETS;
     if (is_dir($source)) {
         $expected = array("smurty_cacheresource.php" => true, "smurty_cacheresource_custom.php" => true, "smurty_cacheresource_keyvaluestore.php" => true, "smurty_config_source.php" => true, "smurty_internal_cacheresource_file.php" => true, "smurty_internal_compile_append.php" => true, "smurty_internal_compile_assign.php" => true, "smurty_internal_compile_block.php" => true, "smurty_internal_compile_break.php" => true, "smurty_internal_compile_call.php" => true, "smurty_internal_compile_capture.php" => true, "smurty_internal_compile_config_load.php" => true, "smurty_internal_compile_continue.php" => true, "smurty_internal_compile_debug.php" => true, "smurty_internal_compile_eval.php" => true, "smurty_internal_compile_extends.php" => true, "smurty_internal_compile_for.php" => true, "smurty_internal_compile_foreach.php" => true, "smurty_internal_compile_function.php" => true, "smurty_internal_compile_if.php" => true, "smurty_internal_compile_include.php" => true, "smurty_internal_compile_include_php.php" => true, "smurty_internal_compile_insert.php" => true, "smurty_internal_compile_ldelim.php" => true, "smurty_internal_compile_nocache.php" => true, "smurty_internal_compile_private_block_plugin.php" => true, "smurty_internal_compile_private_function_plugin.php" => true, "smurty_internal_compile_private_modifier.php" => true, "smurty_internal_compile_private_object_block_function.php" => true, "smurty_internal_compile_private_object_function.php" => true, "smurty_internal_compile_private_print_expression.php" => true, "smurty_internal_compile_private_registered_block.php" => true, "smurty_internal_compile_private_registered_function.php" => true, "smurty_internal_compile_private_special_variable.php" => true, "smurty_internal_compile_rdelim.php" => true, "smurty_internal_compile_section.php" => true, "smurty_internal_compile_setfilter.php" => true, "smurty_internal_compile_while.php" => true, "smurty_internal_compilebase.php" => true, "smurty_internal_config.php" => true, "smurty_internal_config_file_compiler.php" => true, "smurty_internal_configfilelexer.php" => true, "smurty_internal_configfileparser.php" => true, "smurty_internal_data.php" => true, "smurty_internal_debug.php" => true, "smurty_internal_filter_handler.php" => true, "smurty_internal_function_call_handler.php" => true, "smurty_internal_get_include_path.php" => true, "smurty_internal_nocache_insert.php" => true, "smurty_internal_parsetree.php" => true, "smurty_internal_resource_eval.php" => true, "smurty_internal_resource_extends.php" => true, "smurty_internal_resource_file.php" => true, "smurty_internal_resource_registered.php" => true, "smurty_internal_resource_stream.php" => true, "smurty_internal_resource_string.php" => true, "smurty_internal_smurtytemplatecompiler.php" => true, "smurty_internal_template.php" => true, "smurty_internal_templatebase.php" => true, "smurty_internal_templatecompilerbase.php" => true, "smurty_internal_templatelexer.php" => true, "smurty_internal_templateparser.php" => true, "smurty_internal_utility.php" => true, "smurty_internal_write_file.php" => true, "smurty_resource.php" => true, "smurty_resource_custom.php" => true, "smurty_resource_recompiled.php" => true, "smurty_resource_uncompiled.php" => true, "smurty_security.php" => true);
         $iterator = new DirectoryIterator($source);
         foreach ($iterator as $file) {
             if (!$file->isDot()) {
                 $filename = $file->getFilename();
                 if (isset($expected[$filename])) {
                     unset($expected[$filename]);
                 }
             }
         }
         if ($expected) {
             $status = false;
             $message = "FAILED: files missing from libs/hiweb-core-tpl: " . join(', ', array_keys($expected));
             if ($errors === null) {
                 echo $message . ".\n";
             } else {
                 $errors['hiweb-core-tpl'] = $message;
             }
         } elseif ($errors === null) {
             echo "... OK\n";
         }
     } else {
         $status = false;
         $message = "FAILED: " . HIWEB_CORE_HTML_ASSETS . ' is not a directory';
         if ($errors === null) {
             echo $message . ".\n";
         } else {
             $errors['sysplugins_dir_constant'] = $message;
         }
     }
     if ($errors === null) {
         echo "Testing plugin files...\n";
     }
     // test if core plugins are available
     $source = HIWEB_TPL_PLUGINS_DIR;
     if (is_dir($source)) {
         $expected = array("block.textformat.php" => true, "function.counter.php" => true, "function.cycle.php" => true, "function.fetch.php" => true, "function.html_checkboxes.php" => true, "function.html_image.php" => true, "function.html_options.php" => true, "function.html_radios.php" => true, "function.html_select_date.php" => true, "function.html_select_time.php" => true, "function.html_table.php" => true, "function.mailto.php" => true, "function.math.php" => true, "modifier.capitalize.php" => true, "modifier.date_format.php" => true, "modifier.debug_print_var.php" => true, "modifier.escape.php" => true, "modifier.regex_replace.php" => true, "modifier.replace.php" => true, "modifier.spacify.php" => true, "modifier.truncate.php" => true, "modifiercompiler.cat.php" => true, "modifiercompiler.count_characters.php" => true, "modifiercompiler.count_paragraphs.php" => true, "modifiercompiler.count_sentences.php" => true, "modifiercompiler.count_words.php" => true, "modifiercompiler.default.php" => true, "modifiercompiler.escape.php" => true, "modifiercompiler.from_charset.php" => true, "modifiercompiler.indent.php" => true, "modifiercompiler.lower.php" => true, "modifiercompiler.noprint.php" => true, "modifiercompiler.string_format.php" => true, "modifiercompiler.strip.php" => true, "modifiercompiler.strip_tags.php" => true, "modifiercompiler.to_charset.php" => true, "modifiercompiler.unescape.php" => true, "modifiercompiler.upper.php" => true, "modifiercompiler.wordwrap.php" => true, "outputfilter.trimwhitespace.php" => true, "shared.escape_special_chars.php" => true, "shared.literal_compiler_param.php" => true, "shared.make_timestamp.php" => true, "shared.mb_str_replace.php" => true, "shared.mb_unicode.php" => true, "shared.mb_wordwrap.php" => true, "variablefilter.htmlspecialchars.php" => true);
         $iterator = new DirectoryIterator($source);
         foreach ($iterator as $file) {
             if (!$file->isDot()) {
                 $filename = $file->getFilename();
                 if (isset($expected[$filename])) {
                     unset($expected[$filename]);
                 }
             }
         }
         if ($expected) {
             $status = false;
             $message = "FAILED: files missing from libs/plugins: " . join(', ', array_keys($expected));
             if ($errors === null) {
                 echo $message . ".\n";
             } else {
                 $errors['plugins'] = $message;
             }
         } elseif ($errors === null) {
             echo "... OK\n";
         }
     } else {
         $status = false;
         $message = "FAILED: " . HIWEB_TPL_PLUGINS_DIR . ' is not a directory';
         if ($errors === null) {
             echo $message . ".\n";
         } else {
             $errors['plugins_dir_constant'] = $message;
         }
     }
     if ($errors === null) {
         echo "Tests complete.\n";
         echo "</PRE>\n";
     }
     return $status;
 }
Beispiel #2
0
 /**
  * build template filepath by traversing the template_dir array
  *
  * @param  Smurty_Template_Source   $source    source object
  * @param  Smurty_Internal_Template $_template template object
  *
  * @return string                   fully qualified filepath
  * @throws SmurtyException          if default template handler is registered but not callable
  */
 protected function buildFilepath(Smurty_Template_Source $source, Smurty_Internal_Template $_template = null)
 {
     $file = $source->name;
     if ($source instanceof Smurty_Config_Source) {
         $_directories = $source->smurty->getConfigDir();
         $_default_handler = $source->smurty->default_config_handler_func;
     } else {
         $_directories = $source->smurty->getTemplateDir();
         $_default_handler = $source->smurty->default_template_handler_func;
     }
     // go relative to a given template?
     $_file_is_dotted = $file[0] == '.' && ($file[1] == '.' || $file[1] == '/' || $file[1] == "\\");
     if ($_template && $_template->parent instanceof Smurty_Internal_Template && $_file_is_dotted) {
         if ($_template->parent->source->type != 'file' && $_template->parent->source->type != 'extends' && !$_template->parent->allow_relative_path) {
             throw new SmurtyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'");
         }
         $file = dirname($_template->parent->source->filepath) . DIR_SEPARATOR . $file;
         $_file_exact_match = true;
         if (!preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $file)) {
             // the path gained from the parent template is relative to the current working directory
             // as expansions (like include_path) have already been done
             $file = getcwd() . DIR_SEPARATOR . $file;
         }
     }
     // resolve relative path
     if (!preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $file)) {
         // don't we all just love windows?
         $_path = DIR_SEPARATOR . trim($file, '/');
         $_was_relative = true;
     } else {
         // don't we all just love windows?
         $_path = str_replace('\\', '/', $file);
     }
     $_path = $this->normalizePath($_path, false);
     if (DIR_SEPARATOR != '/') {
         // don't we all just love windows?
         $_path = str_replace('/', '\\', $_path);
     }
     // revert to relative
     if (isset($_was_relative)) {
         $_path = substr($_path, 1);
     }
     // this is only required for directories
     $file = rtrim($_path, '/\\');
     // files relative to a template only get one shot
     if (isset($_file_exact_match)) {
         return $this->fileExists($source, $file) ? $file : false;
     }
     // template_dir index?
     if (preg_match('#^\\[(?P<key>[^\\]]+)\\](?P<file>.+)$#', $file, $match)) {
         $_directory = null;
         // try string indexes
         if (isset($_directories[$match['key']])) {
             $_directory = $_directories[$match['key']];
         } elseif (is_numeric($match['key'])) {
             // try numeric index
             $match['key'] = (int) $match['key'];
             if (isset($_directories[$match['key']])) {
                 $_directory = $_directories[$match['key']];
             } else {
                 // try at location index
                 $keys = array_keys($_directories);
                 $_directory = $_directories[$keys[$match['key']]];
             }
         }
         if ($_directory) {
             $_file = substr($file, strpos($file, ']') + 1);
             $_filepath = $_directory . $_file;
             if ($this->fileExists($source, $_filepath)) {
                 return $_filepath;
             }
         }
     }
     $_stream_resolve_include_path = function_exists('stream_resolve_include_path');
     // relative file name?
     if (!preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $file)) {
         foreach ($_directories as $_directory) {
             $_filepath = $_directory . $file;
             if ($this->fileExists($source, $_filepath)) {
                 return $this->normalizePath($_filepath);
             }
             if ($source->smurty->use_include_path && !preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $_directory)) {
                 // try PHP include_path
                 if ($_stream_resolve_include_path) {
                     $_filepath = stream_resolve_include_path($_filepath);
                 } else {
                     $_filepath = Smurty_Internal_Get_Include_Path::getIncludePath($_filepath);
                 }
                 if ($_filepath !== false) {
                     if ($this->fileExists($source, $_filepath)) {
                         return $this->normalizePath($_filepath);
                     }
                 }
             }
         }
     }
     // try absolute filepath
     if ($this->fileExists($source, $file)) {
         return $file;
     }
     // no tpl file found
     if ($_default_handler) {
         if (!is_callable($_default_handler)) {
             if ($source instanceof Smurty_Config_Source) {
                 throw new SmurtyException("Default config handler not callable");
             } else {
                 throw new SmurtyException("Default template handler not callable");
             }
         }
         $_return = call_user_func_array($_default_handler, array($source->type, $source->name, &$_content, &$_timestamp, $source->smurty));
         if (is_string($_return)) {
             $source->timestamp = @filemtime($_return);
             $source->exists = !!$source->timestamp;
             return $_return;
         } elseif ($_return === true) {
             $source->content = $_content;
             $source->timestamp = $_timestamp;
             $source->exists = true;
             return $_filepath;
         }
     }
     // give up
     return false;
 }
Beispiel #3
0
 /**
  * Takes unknown classes and loads plugin files for them
  * class name format: Smurty_PluginType_PluginName
  * plugin filename format: plugintype.pluginname.php
  *
  * @param  string $plugin_name class plugin name to load
  * @param  bool   $check       check if already loaded
  *
  * @throws SmurtyException
  * @return string |boolean filepath of loaded file or false
  */
 public function loadPlugin($plugin_name, $check = true)
 {
     // if function or class exists, exit silently (already loaded)
     if ($check && (is_callable($plugin_name) || class_exists($plugin_name, false))) {
         return true;
     }
     // Plugin name is expected to be: Smurty_[Type]_[Name]
     $_name_parts = explode('_', $plugin_name, 3);
     // class name must have three parts to be valid plugin
     // count($_name_parts) < 3 === !isset($_name_parts[2])
     if (!isset($_name_parts[2]) || strtolower($_name_parts[0]) !== 'smurty') {
         throw new SmurtyException("plugin {$plugin_name} is not a valid name format");
     }
     // if type is "internal", get plugin from hiweb-core-tpl
     if (strtolower($_name_parts[1]) == 'internal') {
         $file = HIWEB_CORE_HTML_ASSETS . strtolower($plugin_name) . '.php';
         if (file_exists($file)) {
             require_once $file;
             return $file;
         } else {
             return false;
         }
     }
     // plugin filename is expected to be: [type].[name].php
     $_plugin_filename = "{$_name_parts[1]}.{$_name_parts[2]}.php";
     $_stream_resolve_include_path = function_exists('stream_resolve_include_path');
     // loop through plugin dirs and find the plugin
     foreach ($this->getPluginsDir() as $_plugin_dir) {
         $names = array($_plugin_dir . $_plugin_filename, $_plugin_dir . strtolower($_plugin_filename));
         foreach ($names as $file) {
             if (file_exists($file)) {
                 require_once $file;
                 return $file;
             }
             if ($this->use_include_path && !preg_match('/^([\\/\\\\]|[a-zA-Z]:[\\/\\\\])/', $_plugin_dir)) {
                 // try PHP include_path
                 if ($_stream_resolve_include_path) {
                     $file = stream_resolve_include_path($file);
                 } else {
                     $file = Smurty_Internal_Get_Include_Path::getIncludePath($file);
                 }
                 if ($file !== false) {
                     require_once $file;
                     return $file;
                 }
             }
         }
     }
     // no plugin loaded
     return false;
 }