~ubuntu-branches/ubuntu/wily/steam/wily

« back to all changes in this revision

Viewing changes to server/classes/Script.pike

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2013-10-29 19:51:18 UTC
  • mfrom: (1.1.4) (0.1.4 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20131029195118-b9bxciz5hwx5z459
Tags: 1:1.0.0.39-2ubuntu1
Add an epoch to the version number as there was an unrelated steam package
in the archive with a higher version number.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2004  Thomas Bopp, Thorsten Hampel, Ludger Merkens
2
 
 *
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.
7
 
 *
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.
12
 
 *
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
16
 
 * 
17
 
 * $Id: Script.pike,v 1.1.1.1 2006/03/27 12:40:08 exodusd Exp $
18
 
 */
19
 
 
20
 
constant cvs_version="$Id: Script.pike,v 1.1.1.1 2006/03/27 12:40:08 exodusd Exp $";
21
 
 
22
 
 
23
 
//! the Script class is for web scripts with helper function to 
24
 
//! generate html.
25
 
 
26
 
inherit "/classes/Object";
27
 
inherit httplib;
28
 
inherit "/base/xml_parser";
29
 
 
30
 
#include <macros.h>
31
 
#include <database.h>
32
 
#include <exception.h>
33
 
#include <classes.h>
34
 
#include <attributes.h>
35
 
#include <types.h>
36
 
 
37
 
static void init_script() { }
38
 
static void create_script() { }
39
 
 
40
 
static mapping mLanguages = ([ ]);
41
 
 
42
 
/**
43
 
 * Initialization.
44
 
 */
45
 
static void init() 
46
 
{
47
 
    ::init();
48
 
    init_script();
49
 
}
50
 
 
51
 
static void load_object() 
52
 
{
53
 
    object lobj = do_query_attribute(SCRIPT_LANGUAGE_OBJ);
54
 
    if ( objectp(lobj) )
55
 
        init_languages(lobj);
56
 
}
57
 
 
58
 
string get_language_term(string term, string language)
59
 
{
60
 
    if ( mappingp(mLanguages) ) {
61
 
        if ( mappingp(mLanguages[language]) )
62
 
            return mLanguages[language][term];
63
 
    }
64
 
    return "!! unknown internationalization for " + term;
65
 
}
66
 
 
67
 
void init_languages(object languageObj)
68
 
{
69
 
    set_attribute(SCRIPT_LANGUAGE_OBJ, languageObj);
70
 
 
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;
76
 
            if ( !stringp(lang) ) 
77
 
                steam_error("Uninitialized language in language document !");
78
 
 
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();
84
 
            }
85
 
        }
86
 
    }
87
 
}
88
 
 
89
 
static void create_object()
90
 
{
91
 
    create_script();
92
 
}
93
 
 
94
 
static object find_document(string path)
95
 
{
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();
101
 
    }
102
 
    return _FILEPATH->resolve_path(env, path);
103
 
}
104
 
 
105
 
static string nav_link(object id, string name) {
106
 
    if ( !objectp(id) )
107
 
        return "";
108
 
    return "<a href=\"/scripts/navigate.pike?object="+id->get_object_id()+"\">"+name+"</a>";
109
 
}
110
 
 
111
 
/**
112
 
 *
113
 
 *  
114
 
 * @param 
115
 
 * @return 0 upon successfull checking or string (html code)
116
 
 * @author <a href="mailto:astra@upb.de">Thomas Bopp</a>) 
117
 
 * @see 
118
 
 */
119
 
static string check_variables(mapping req, mapping vars)
120
 
{
121
 
    foreach ( indices(req), string key ) {
122
 
        if ( !vars[key] ) 
123
 
            THROW("The variable '"+key+"' was not found, but is "+
124
 
                  "required !", E_ERROR);
125
 
        switch(req[key]) {
126
 
        case CMD_TYPE_INT:
127
 
            int v;
128
 
            if ( sscanf(vars[key], "%d", v) == 0 )
129
 
                return error_page(
130
 
                    "The variable '"+key+"' needs an integer.. <br/>"+
131
 
                    sprintf("%O",vars[key]), 
132
 
                    "JavaScript:history.back();");
133
 
            break;
134
 
        case CMD_TYPE_STRING:
135
 
            if ( !stringp(vars[key]) )
136
 
                return error_page(
137
 
                    "The variable '"+key+"' needs a string.. <br/>"+
138
 
                    sprintf("%O",vars[key]), 
139
 
                    "JavaScript:history.back();");
140
 
            break;
141
 
        default:
142
 
        break;
143
 
        }
144
 
    }
145
 
    return 0;
146
 
}
147
 
 
148
 
/**
149
 
 * Get the value for a variable set by the web-interface which is always
150
 
 * a string.
151
 
 *  
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>) 
155
 
 */
156
 
mixed
157
 
get_value(mixed v)
158
 
{
159
 
    mixed val;
160
 
    int     i;
161
 
 
162
 
    if ( v == 0 ) return 0;
163
 
    if ( intp(v) ) return v;
164
 
    
165
 
    val = v / "\0";
166
 
    if ( sizeof(val) > 1 )
167
 
        return val;
168
 
 
169
 
    if ( sscanf(v, "%d", val) == 1 && ((string)val) == v )
170
 
        return val;
171
 
    if ( sscanf(v, "%f", val) == 1 && search((string)val, v) == 0 )
172
 
        return val;
173
 
    return v;
174
 
}
175
 
 
176
 
 
177
 
/**
178
 
 * If multiple values are assigned to a single variable it is
179
 
 * converted to an array in this function.
180
 
 *  
181
 
 * @param v - the original string value
182
 
 * @return the resulting array
183
 
 * @author <a href="mailto:astra@upb.de">Thomas Bopp</a>) 
184
 
 * @see get_value
185
 
 */
186
 
array get_values(string v)
187
 
{
188
 
    array res = v / "\0";
189
 
    array result = ({ });
190
 
 
191
 
    foreach ( res, string str ) {
192
 
        if ( stringp(str) && strlen(str) > 0 )
193
 
            result += ({ get_value(str) });
194
 
    }
195
 
    return result;
196
 
}
197
 
 
198
 
/**
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
202
 
 *  
203
 
 * @param str - the source string
204
 
 * @return an array of objects
205
 
 * @author <a href="mailto:astra@upb.de">Thomas Bopp</a>) 
206
 
 * @see get_values
207
 
 */
208
 
static array(object) extract_objects(string str)
209
 
{
210
 
    array(object) result = ({ });
211
 
    
212
 
    if ( !stringp(str) )
213
 
        return result;
214
 
 
215
 
    array(string) res = str / "\0";
216
 
    foreach( res, string oid ) {
217
 
        object obj;
218
 
        obj = find_object((int)oid);
219
 
        if ( objectp(obj) )
220
 
            result += ({ obj });
221
 
    }
222
 
    LOG("Result="+sprintf("%O\n",result));
223
 
    return result;
224
 
}
225
 
 
226
 
/**
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
229
 
 * object ids.
230
 
 *  
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>) 
235
 
 * @see 
236
 
 */
237
 
array(object) get_objects(mapping vars, string index)
238
 
{
239
 
    array(object) result = ({ });
240
 
    
241
 
    array(string) res;
242
 
    if ( arrayp(vars[index]) )
243
 
        res = vars[index];
244
 
    else if ( !stringp(vars[index]) )
245
 
        return result;
246
 
    else
247
 
        res = vars[index] / "\0";
248
 
 
249
 
    foreach( res, string oid ) {
250
 
        object obj;
251
 
        obj = find_object((int)oid);
252
 
        if ( objectp(obj) )
253
 
            result += ({ obj });
254
 
    }
255
 
    return result;
256
 
}
257
 
 
258
 
/**
259
 
 * Replace all variables in a html template.
260
 
 *  
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>) 
265
 
 */
266
 
string html_template(string|object templ, mapping vars)
267
 
{
268
 
    mapping templ_vars = ([ ]);
269
 
 
270
 
    if ( objectp(templ) ) {
271
 
      if ( !(templ->get_object_class() & CLASS_DOCUMENT) )
272
 
        THROW("Template object is not a document.", E_ERROR);
273
 
 
274
 
      templ = templ->get_content();
275
 
    }
276
 
 
277
 
    foreach(indices(vars), string key) {
278
 
        mixed val = vars[key];
279
 
        if ( stringp(val) )
280
 
            templ_vars["{"+key+"}"] = val;
281
 
    }
282
 
    templ_vars["{SCRIPT}"] = "/scripts/execute.pike?script="+get_object_id();
283
 
 
284
 
    return replace(templ, indices(templ_vars), values(templ_vars));
285
 
}
286
 
 
287
 
/**
288
 
 * Create a new instance of a CLASS_
289
 
 *  
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>) 
294
 
 */
295
 
object new_instance(int cid, mapping vars)
296
 
{
297
 
    object factory = _Server->get_factory(cid);
298
 
    object obj = factory->execute(vars);
299
 
    return obj;
300
 
}
301
 
 
302
 
/**
303
 
 *
304
 
 *  
305
 
 * @param 
306
 
 * @return 
307
 
 * @author <a href="mailto:astra@upb.de">Thomas Bopp</a>) 
308
 
 * @see 
309
 
 */
310
 
string html_template_block(string|object templ, string block, array bvars)
311
 
{
312
 
    mapping templ_vars = ([ ]);
313
 
    float t;
314
 
 
315
 
    if ( objectp(templ) ) {
316
 
      if ( !(templ->get_object_class() & CLASS_DOCUMENT) )
317
 
        THROW("Template object is not a document.", E_ERROR);
318
 
 
319
 
      templ = templ->get_content();
320
 
    }
321
 
 
322
 
    int count = 0;
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 ) {
328
 
        templ_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));
332
 
    }
333
 
    return blocks[0] + block_html + blocks[2];
334
 
}
335
 
 
336
 
/**
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.
339
 
 *  
340
 
 * @param 
341
 
 * @return 
342
 
 * @author <a href="mailto:astra@upb.de">Thomas Bopp</a>) 
343
 
 * @see 
344
 
 */
345
 
string make_selection(string name, object obj, object|void val, int|void classbit)
346
 
{
347
 
    string html = "<select name=\""+name+"\">\n";
348
 
    if ( classbit == 0 )
349
 
        classbit = CLASS_OBJECT;
350
 
 
351
 
    array(object) res = ({ });
352
 
    
353
 
    if ( obj->get_object_class() & CLASS_CONTAINER ) {
354
 
        foreach( obj->get_inventory(), object o )
355
 
            if ( o->get_object_class() & classbit )
356
 
                res += ({ o });
357
 
    }
358
 
    else if ( obj->get_object_class() & CLASS_GROUP ) {
359
 
        foreach( obj->get_members(), object m )
360
 
            if ( m->get_object_class() & classbit )
361
 
                res += ({ m });
362
 
    }
363
 
    foreach(res, object r) {
364
 
        html += sprintf("<option value=\"%d\""+
365
 
                        (objectp(val) && val == r ? " selected='true'":"")+
366
 
                        ">%s</option>",
367
 
                        r->get_object_id(),
368
 
                        (r->get_object_class() & CLASS_USER ? 
369
 
                         r->query_attribute(USER_FULLNAME) :
370
 
                         r->get_identifier()));
371
 
    }
372
 
    html += "</select>\n";
373
 
    return html;
374
 
}
375
 
 
376
 
string image(object obj, mapping args)
377
 
{
378
 
    return "<image src=\"/scripts/get.pike?object="+obj->get_object_id()+"\""+
379
 
        (args->xsize ? " xsize=\""+args->xsize+"\"":"")+
380
 
        (args->ysize ? " xsize=\""+args->ysize+"\"":"")+
381
 
        " border=\"0\"/>";
382
 
}
383
 
 
384
 
/**
385
 
 * Upgrade callback function - the the creation time to current timestamp.
386
 
 *  
387
 
 * @author <a href="mailto:astra@upb.de">Thomas Bopp</a>) 
388
 
 */
389
 
void upgrade()
390
 
{
391
 
    do_set_attribute(OBJ_CREATION_TIME, time());
392
 
}
393
 
 
394
 
final object get_source_object()
395
 
{
396
 
  program prg = object_program(this_object());
397
 
  string desc = master()->describe_program(prg);
398
 
  int id;
399
 
  if ( !sscanf(desc, "%*s#%d", id) )
400
 
    sscanf(desc, "%*s#%d", id);
401
 
  
402
 
  return find_object(id);
403
 
}
404
 
 
405
 
 
406
 
/**
407
 
 * Describe the Script in a backtrace.
408
 
 *  
409
 
 * @return description string of the object
410
 
 * @author <a href="mailto:astra@upb.de">Thomas Bopp</a>) 
411
 
 */
412
 
string _sprintf()
413
 
{
414
 
    return master()->describe_program(object_program(this_object())) + " ("+
415
 
        get_identifier()+", #"+get_object_id()+")";
416
 
}
417
 
 
418
 
int get_object_class() { return ::get_object_class() | CLASS_SCRIPT; }
419
 
string describe() { return _sprintf(); }
420
 
 
421
 
void test()
422
 
{
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!");
431
 
}
432