Class: PageTemplate::FileSource
- Defined in:
- lib/PageTemplate/parser.rb
Overview
FileSource provides access to files within the ‘include_paths’ argument.
It attempts to be secure by not allowing any access outside of The directories detailed within include_paths
Instance Attribute Summary collapse
-
#paths ⇒ Object
Returns the value of attribute paths.
Instance Method Summary collapse
- #cache(name, cmds) ⇒ Object
-
#get(name) ⇒ Object
Return the contents of the file
name
, which must be within the include_paths. - #get_filename(file) ⇒ Object
-
#initialize(args = {}) ⇒ FileSource
constructor
initialize looks for the following in the args Hash: * include_paths = a list of file paths * include_path = a single file path string (kept for backwards compatibility).
Constructor Details
#initialize(args = {}) ⇒ FileSource
initialize looks for the following in the args Hash:
* include_paths = a list of file paths
* include_path = a single file path string
(kept for backwards compatibility)
228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/PageTemplate/parser.rb', line 228 def initialize(args = {}) @args = args if @args['include_paths'] @paths = @args['include_paths'] elsif @args['include_path'] @paths = [ @args['include_path'] ] else @paths = [Dir.getwd,'/'] end @paths = @paths.compact @cache = Hash.new(nil) @mtime = Hash.new(0) end |
Instance Attribute Details
#paths ⇒ Object
Returns the value of attribute paths.
222 223 224 |
# File 'lib/PageTemplate/parser.rb', line 222 def paths @paths end |
Instance Method Details
#cache(name, cmds) ⇒ Object
261 262 263 264 265 |
# File 'lib/PageTemplate/parser.rb', line 261 def cache(name,cmds) fn = get_filename(name) @cache[fn] = cmds.dup @mtime[fn] = Time.now.to_i end |
#get(name) ⇒ Object
Return the contents of the file name
, which must be within the include_paths.
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/PageTemplate/parser.rb', line 244 def get(name) fn = get_filename(name) begin case when fn.nil? nil when @cache.has_key?(fn) && (@mtime[fn] > File.mtime(fn).to_i) cmds = @cache[fn] cmds.clear_cache cmds else IO.read(fn) end rescue Exception => er return "[ Unable to open file #{fn} because of #{er.} ]" end end |
#get_filename(file) ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/PageTemplate/parser.rb', line 266 def get_filename(file) # Check for absolute filepaths if file == File.(file) if File.exists?(file) return file end end file = file.gsub(/\.\.\//,'') @paths.each do |path| fn = File.join(path,file) fn.untaint if File.exists?(fn) return fn end end return nil end |