1
/* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
* $Id: Script.pike,v 1.1.1.1 2006/03/27 12:40:08 exodusd Exp $
20
constant cvs_version="$Id: Script.pike,v 1.1.1.1 2006/03/27 12:40:08 exodusd Exp $";
23
//! the Script class is for web scripts with helper function to
26
inherit "/classes/Object";
28
inherit "/base/xml_parser";
32
#include <exception.h>
34
#include <attributes.h>
37
static void init_script() { }
38
static void create_script() { }
40
static mapping mLanguages = ([ ]);
51
static void load_object()
53
object lobj = do_query_attribute(SCRIPT_LANGUAGE_OBJ);
58
string get_language_term(string term, string language)
60
if ( mappingp(mLanguages) ) {
61
if ( mappingp(mLanguages[language]) )
62
return mLanguages[language][term];
64
return "!! unknown internationalization for " + term;
67
void init_languages(object languageObj)
69
set_attribute(SCRIPT_LANGUAGE_OBJ, languageObj);
71
NodeXML n = parse_data(languageObj->get_content());
72
array nodes = n->get_nodes("language");
73
if ( arrayp(nodes) ) {
74
foreach ( nodes, object node ) {
75
string lang = node->attributes->name;
77
steam_error("Uninitialized language in language document !");
79
mLanguages[lang] = ([ ]);
80
foreach( node->get_nodes("term"), object term ) {
81
if ( !stringp(node->attributes->name) )
82
steam_error("Broken language file - missing name attribute on term.");
83
mLanguages[lang][term->attributes->name] = term->get_data();
89
static void create_object()
94
static object find_document(string path)
96
object env = get_environment();
97
if ( !objectp(env) ) {
98
env = do_query_attribute(OBJ_SCRIPT);
99
if ( objectp(env) && !(env->get_object_class() & CLASS_CONTAINER) )
100
env = env->get_environment();
102
return _FILEPATH->resolve_path(env, path);
105
static string nav_link(object id, string name) {
108
return "<a href=\"/scripts/navigate.pike?object="+id->get_object_id()+"\">"+name+"</a>";
115
* @return 0 upon successfull checking or string (html code)
116
* @author <a href="mailto:astra@upb.de">Thomas Bopp</a>)
119
static string check_variables(mapping req, mapping vars)
121
foreach ( indices(req), string key ) {
123
THROW("The variable '"+key+"' was not found, but is "+
124
"required !", E_ERROR);
128
if ( sscanf(vars[key], "%d", v) == 0 )
130
"The variable '"+key+"' needs an integer.. <br/>"+
131
sprintf("%O",vars[key]),
132
"JavaScript:history.back();");
134
case CMD_TYPE_STRING:
135
if ( !stringp(vars[key]) )
137
"The variable '"+key+"' needs a string.. <br/>"+
138
sprintf("%O",vars[key]),
139
"JavaScript:history.back();");
149
* Get the value for a variable set by the web-interface which is always
152
* @param the original string value
153
* @return integer or float or still a string
154
* @author <a href="mailto:astra@upb.de">Thomas Bopp</a>)
162
if ( v == 0 ) return 0;
163
if ( intp(v) ) return v;
166
if ( sizeof(val) > 1 )
169
if ( sscanf(v, "%d", val) == 1 && ((string)val) == v )
171
if ( sscanf(v, "%f", val) == 1 && search((string)val, v) == 0 )
178
* If multiple values are assigned to a single variable it is
179
* converted to an array in this function.
181
* @param v - the original string value
182
* @return the resulting array
183
* @author <a href="mailto:astra@upb.de">Thomas Bopp</a>)
186
array get_values(string v)
188
array res = v / "\0";
189
array result = ({ });
191
foreach ( res, string str ) {
192
if ( stringp(str) && strlen(str) > 0 )
193
result += ({ get_value(str) });
199
* Extract an array of objects from a given string value.
200
* An object-array is provided by the web-interface as a string of
201
* integers separated by \0
203
* @param str - the source string
204
* @return an array of objects
205
* @author <a href="mailto:astra@upb.de">Thomas Bopp</a>)
208
static array(object) extract_objects(string str)
210
array(object) result = ({ });
215
array(string) res = str / "\0";
216
foreach( res, string oid ) {
218
obj = find_object((int)oid);
222
LOG("Result="+sprintf("%O\n",result));
227
* Get an array of objects which are in the vars mapping passed to
228
* steam by the web server. The mapping will contain an array of
231
* @param mapping vars - the parameter mapping.
232
* @param string index - the index to lookup inthe mapping.
233
* @return array of objects found in the mapping.
234
* @author <a href="mailto:astra@upb.de">Thomas Bopp</a>)
237
array(object) get_objects(mapping vars, string index)
239
array(object) result = ({ });
242
if ( arrayp(vars[index]) )
244
else if ( !stringp(vars[index]) )
247
res = vars[index] / "\0";
249
foreach( res, string oid ) {
251
obj = find_object((int)oid);
259
* Replace all variables in a html template.
261
* @param string templ - the html template string
262
* @param mapping vars - variables to exchange
263
* @return the exchanged template
264
* @author <a href="mailto:astra@upb.de">Thomas Bopp</a>)
266
string html_template(string|object templ, mapping vars)
268
mapping templ_vars = ([ ]);
270
if ( objectp(templ) ) {
271
if ( !(templ->get_object_class() & CLASS_DOCUMENT) )
272
THROW("Template object is not a document.", E_ERROR);
274
templ = templ->get_content();
277
foreach(indices(vars), string key) {
278
mixed val = vars[key];
280
templ_vars["{"+key+"}"] = val;
282
templ_vars["{SCRIPT}"] = "/scripts/execute.pike?script="+get_object_id();
284
return replace(templ, indices(templ_vars), values(templ_vars));
288
* Create a new instance of a CLASS_
290
* @param int cid - the class flag
291
* @param mapping vars - vars mapping for the factory.
292
* @return newly created object
293
* @author <a href="mailto:astra@upb.de">Thomas Bopp</a>)
295
object new_instance(int cid, mapping vars)
297
object factory = _Server->get_factory(cid);
298
object obj = factory->execute(vars);
307
* @author <a href="mailto:astra@upb.de">Thomas Bopp</a>)
310
string html_template_block(string|object templ, string block, array bvars)
312
mapping templ_vars = ([ ]);
315
if ( objectp(templ) ) {
316
if ( !(templ->get_object_class() & CLASS_DOCUMENT) )
317
THROW("Template object is not a document.", E_ERROR);
319
templ = templ->get_content();
323
templ = replace(templ, "<!-- END " + block + " -->",
324
"<!-- BEGIN " + block + " -->");
325
array(string) blocks = templ / ("<!-- BEGIN " + block + " -->");
326
string block_html = "";
327
foreach ( bvars, mapping vars ) {
329
foreach( indices(vars), string index )
330
templ_vars["{"+index+"}"] = (string)vars[index];
331
block_html += replace(blocks[1], indices(templ_vars), values(templ_vars));
333
return blocks[0] + block_html + blocks[2];
337
* Create a selection field for an object. The parameter passed has to be a container
338
* or a Group and will list the content as selections.
342
* @author <a href="mailto:astra@upb.de">Thomas Bopp</a>)
345
string make_selection(string name, object obj, object|void val, int|void classbit)
347
string html = "<select name=\""+name+"\">\n";
349
classbit = CLASS_OBJECT;
351
array(object) res = ({ });
353
if ( obj->get_object_class() & CLASS_CONTAINER ) {
354
foreach( obj->get_inventory(), object o )
355
if ( o->get_object_class() & classbit )
358
else if ( obj->get_object_class() & CLASS_GROUP ) {
359
foreach( obj->get_members(), object m )
360
if ( m->get_object_class() & classbit )
363
foreach(res, object r) {
364
html += sprintf("<option value=\"%d\""+
365
(objectp(val) && val == r ? " selected='true'":"")+
368
(r->get_object_class() & CLASS_USER ?
369
r->query_attribute(USER_FULLNAME) :
370
r->get_identifier()));
372
html += "</select>\n";
376
string image(object obj, mapping args)
378
return "<image src=\"/scripts/get.pike?object="+obj->get_object_id()+"\""+
379
(args->xsize ? " xsize=\""+args->xsize+"\"":"")+
380
(args->ysize ? " xsize=\""+args->ysize+"\"":"")+
385
* Upgrade callback function - the the creation time to current timestamp.
387
* @author <a href="mailto:astra@upb.de">Thomas Bopp</a>)
391
do_set_attribute(OBJ_CREATION_TIME, time());
394
final object get_source_object()
396
program prg = object_program(this_object());
397
string desc = master()->describe_program(prg);
399
if ( !sscanf(desc, "%*s#%d", id) )
400
sscanf(desc, "%*s#%d", id);
402
return find_object(id);
407
* Describe the Script in a backtrace.
409
* @return description string of the object
410
* @author <a href="mailto:astra@upb.de">Thomas Bopp</a>)
414
return master()->describe_program(object_program(this_object())) + " ("+
415
get_identifier()+", #"+get_object_id()+")";
418
int get_object_class() { return ::get_object_class() | CLASS_SCRIPT; }
419
string describe() { return _sprintf(); }
423
if ( !floatp(get_value("1.1")) )
424
error("1.1 should be float!");
425
if ( get_value("1test") != "1test" )
426
error("String mismatch !");
427
if ( !arrayp(get_value("a\0b")) )
428
error("\\0 separated string does not return array !");
429
if ( !intp(get_value("22")) || get_value("42") != 42 )
430
error("get_value() does not handle integers correctly!");