[include_global]

Description

Link: [include_global]
Author: Johan Sölve
Category: File
Version: 7.x
License: Public Domain
Posted: Mar. 03, 2007
Updated: Jan. 01, 0001
More by this author...
1. Look for file in  web root         
2. Look for file one level above web root
3. Look for file in 0_Global_ somewhere above web root

Parameters

none


Sample Usage

include_global: 'path/to/filename.inc'; // normal use with relative path
include_global: '/path/to/filename.inc'; // normal use with absolute path
include_global: 'path/to/filename.inc', -raw; // don´t process the included file, corresponds to include_raw
						

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?LassoScript

define_tag: 'include_global',
	-priority='replace',
	-required='param1', 
	-optional='raw',
	-Privileged;
	
	// Johan Sölve 2004-02-15
	/*	Usage: 
			include_global: 'path/to/filename.inc'; // normal use with relative path
			include_global: '/path/to/filename.inc'; // normal use with absolute path
			include_global: 'path/to/filename.inc', -raw; // don´t process the included file, 
				corresponds to include_raw
			1. Look for file in  web root 		
			2. Look for file one level above web root
			3. Look for file in 0_Global_ somewhere above web root
	*/
	// 2004-10-22	JS	added error reporting from process tag
	// 2004-10-27	JS	added charset sniffing to match LP7 plain include behavior
	// todo: add support for ../ paths
	
	local: 'includepath'=(params -> get: 1),
		'process'=true;
	local: 'includepath_in'=#includepath;
	if: (params -> (find: '-raw') -> size > 0);
		local: 'process'=false;
	/if;
	
	local: 'localpath'=(response_localpath),
		'webpath'=(response_path),
		'includeContent'='';
	if: !(#includepath -> beginswith: '/');
		// not absolute path
		#includepath = #webpath + #includepath;
	/if;
	
	// inline to authenticate file tags not needed since we run as process
		
	// 1. Look for file in  web root 
	if: file_exists: #includepath;
		#includeContent = file_read: #includePath;

	else;
		// 2. Look for file one level above web root
		#localpath -> removetrailing: (#localpath -> (split: '/') -> last);
		// This ends up with ///a/very/deep/path/to/webroot/and/to/file/
		
		local: 'webroot'=#localpath;
		#webroot -> (removetrailing: #webpath);
		// #webroot += '/'; // we don´t want a trailing slash on webroot
		// This ends up with ///a/very/deep/path/to/webroot
		
		local: 'includeOutsideOfRoot'=#webroot;
		#includeOutsideOfRoot -> (removetrailing: '/' + (#includeOutsideOfRoot -> (split: '/') ->last));
		#includeOutsideOfRoot += #includepath;
		// This ends up with ///a/very/deep/path/to/includefile.inc
		
		if: file_exists: #includeOutsideOfRoot;
			#includeContent = file_read: #includeOutsideOfRoot;

		else;
			// 3. Look for file in 0_Global_ somewhere above web root
			local: 'includeGlobal'=#webroot;
			
			// look for globals level for level up from web root
			while: #includeGlobal != '//' && loop_count < 20; // for safety
				if: #includeGlobal != '//'; // make sure we are not at the top already
					#includeGlobal -> (removetrailing: '/' + (#includeGlobal -> (split: '/') ->last));
					if: file_exists: (#includeGlobal + '/0_Global_/');
						// we found the global folder - exit the loop
						loop_abort;
					/if;
				/if;
			/while;

			#includeGlobal += '/0_Global_' + #includepath;
			// This ends up with ///a/very/0_Global_/includefile.inc

			if: file_exists: #includeGlobal;
				#includeContent = file_read: #includeGlobal;

			else;
				// file not found in any location
				// throw an error
				if: (file_currenterror: -ErrorCode) != 0;
					fail: (file_currenterror: -ErrorCode), 
						file_currenterror + ' Include_global ' 
							+ #includeOutsideOfRoot;
				else: (error_currenterror: -ErrorCode) != 0;
					fail: (error_currenterror: -ErrorCode), 
						error_currenterror + ' Include_global ' 
							+ #includeOutsideOfRoot;
				else;
					fail: (error_filenotfound: -errorcode), error_filenotfound + ' Include_global ' 
							+ #includepath
							+ ', ' + #includeOutsideOfRoot
							+ ', ' + #includeGlobal;
				/if;
			/if;
		/if;
	/if;


	if: #process;
		// -raw was not specified so process the include
		if: (integer: (lasso_version: -lassoversion)) >= 7 && #includeContent->type == 'bytes';
			// make charset assumptions if we run on a Unicode aware Lasso
			// first try to detect a UTF BOM
			if (#includeContent->(get: 1) == 0xEF && #includeContent->(get: 2) == 0xBB && #includeContent->(get: 3) == 0xBF);
				// UTF with BOM
				#includeContent->(remove: 1, 3);
				#includeContent = string: #includeContent;
			else: (lasso_version: -lassoplatform) >> 'Mac OS';
				// assume file was MacRoman
				#includeContent = #includeContent->(exportstring: 'Macintosh');
			else;
				// assume file was ISO
				#includeContent = #includeContent->(exportstring: 'ISO-8859-1');
			/if;			
		/if;
		#includeContent = (process: #includeContent);
		if: #includeContent == '' && (error_currenterror: -errorcode) != 0;
			fail: (error_currenterror: -errorcode),  (error_currenterror  + ' in ' + #includepath_in);
		/if;
	/if;

	// return by reference to disable default encoding when calling the tag in LP7 
	//   (must still use -encodenone in LP6)
 	return: @#includeContent;
/define_tag;

?>

 

Related Tags



Comments

none

Email:


Password:



Newest

Most Popular