orb._pike
Go to the documentation of this file.
1 /* Copyright (C) 2000-2005 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: orb.pike,v 1.3 2010/08/20 20:42:25 astra Exp $
18  */
19 #include <classes.h>
20 #include <database.h>
21 #include <attributes.h>
22 #include <access.h>
23 #include <macros.h>
24 class orb {
25 public:
26 
27 
28 
29 
30 object resolve_path(object|string uid, string path);
31 object path_to_object(string path, void|bool reslv);
32 string object_to_path(object obj);
33 object path_to_environment(string url);
34 
35  mapping mVirtualPath = ([ ]);
36 
37 void add_virtual_path(string path, object module)
38 {
39  if ( !_Server->is_module(module) )
40  steam_error("Invalid module in filepath: add_virtual_path() !");
41  mVirtualPath[module] = path;
42 }
43 
44 /**
45  * get detailed information about a file
46  *
47  * @param url - pathname or object-pointer for an object
48  * @return file information for the object
49  */
50 array identify_file(string|object url)
51 {
52  string dtype;
53  int last_modified;
54  object obj;
55  array res;
56 
57  if ( !objectp(url) ) {
58  obj = path_to_object(url);
59  if ( !objectp(obj) )
60  return 0;
61  }
62  else {
63  LOG("identify_file("+url+")");
64  obj = url;
65  url = object_to_path(obj);
66  }
67 
68  if ( obj->get_object_class() & CLASS_DOCUMENT )
69  {
70  dtype = obj->query_attribute(DOC_MIME_TYPE);
71  LOG("Mime-type is " + dtype);
72  if ( !stringp(dtype) ) {
73  string doctype;
74 
75  doctype = obj->query_attribute(DOC_TYPE);
76  if ( stringp(doctype) )
77  dtype = _TYPES->query_mime_type(doctype);
78  else
79  dtype = "text/html";
80  }
81  last_modified = obj->query_attribute(DOC_LAST_MODIFIED);
82  if ( last_modified == 0 )
83  last_modified = time();
84  }
85  else {
86  dtype = "text/html";
87  last_modified = time();
88  }
89 
90  res = ({ obj,
91  url,
92  dtype, // type of document
93  obj->get_content_size(), // size
94  last_modified, // last modified
95  obj->get_object_class(), // the class type of document
96  obj->stat(),
97  obj->query_sanction(_WORLDUSER)
98  });
99  LOG("Identified object=#"+obj->get_object_id()+ " from url="+url +
100  ",size="+obj->get_content_size()+" array-size="+sizeof(res));
101  LOG("res="+sprintf("%O", res));
102  return res;
103 }
104 
105 /**
106  * Get stats of some file identified by 'f'
107  *
108  * @param string f - the filename
109  * @return file_stat information (usually an array like Stdio.File->stat())
110  */
111 mixed stat_file(string f, void|object id)
112 {
113  object obj = path_to_object(f, true);
114  if ( objectp(obj) )
115  return obj->stat();
116  return 0; // not found
117 }
118 
119 /**
120  * Get the mimetype for a given url.
121  *
122  * @param string f - the filename
123  * @return mimetype like text/plain
124  */
125 string get_mimetype(string f)
126 {
127  object obj = path_to_object(f);
128  if ( objectp(obj) )
129  return obj->stat()[7];
130  return "application/x-unknown";
131 }
132 
133 /**
134  * create a new directory with name "name" in "path"
135  *
136  * @param path - the path to create the directory
137  * @param name - the name for the new container
138  * @return the container-object
139  */
140 object make_directory(string path, string name)
141 {
142  object obj, env, factory;
143  env = path_to_object(path, true);
144  if ( !objectp(env) )
145  return null;
146  obj = resolve_path(path, name);
147 
148  if ( objectp(obj) )
149  return obj;
150  if (path == "/home") {
151  steam_error("Cannot create objects in /home!");
152  }
153  else if ( _Server->query_config("default_container_type") == "room" )
154  factory = _Server->get_factory(CLASS_ROOM);
155  else
156  factory = _Server->get_factory(CLASS_CONTAINER);
157  obj = factory->execute((["name":name,]));
158  if ( !objectp(obj) )
159  return 0;
160  // now move the container in the appropriate place
161  obj->move(env);
162  return obj;
163 }
164 
165 array get_directory(string dir)
166 {
167  array directory = ({ });
168  object cont = path_to_object(dir, true);
169  if ( objectp(cont) ) {
170  array inv = cont->get_inventory();
171  if ( !arrayp(inv) )
172  return directory;
173 
174  foreach(inv, object obj) {
175  if ( objectp(obj) && obj->status() >= 0 &&
176  !(obj->get_object_class() & CLASS_USER) )
177  {
178  int cl = obj->get_object_class();
179  if ( !stringp(obj->get_identifier()) )
180  continue;
181  if ( cl & CLASS_EXIT || cl & CLASS_DOCUMENT ||
182  cl & CLASS_CONTAINER || cl & CLASS_LINK )
183  directory += ({ obj->get_identifier() });
184  }
185  }
186  }
187  return directory;
188 
189 }
190 
191 /**
192  * return the result of a directory query as a mapping filename:file_stat
193  *
194  * @param url - the container to get the directory
195  * @return the directory for the container
196  */
197 mapping(string:array) query_directory(string|object url)
198 {
199  object cont;
200  array inv;
201  mapping(string:array) res;
202  int i, sz;
203 
204  LOG("query_directory...");
205  res = ([ ]);
206  if ( stringp(url) )
207  cont = path_to_object(url, true);
208  else
209  cont = url;
210  inv = cont->get_inventory();
211  for ( i = 0, sz = sizeof(inv); i < sz; i++ ) {
212  int cl = inv[i]->get_object_class();
213  if ( objectp(inv[i]) &&
214  stringp(inv[i]->get_identifier()) &&
215  strlen(inv[i]->get_identifier()) > 0 &&
216  !(cl & CLASS_USER) &&
217  (cl & CLASS_EXIT || cl & CLASS_DOCUMENT || cl & CLASS_CONTAINER ))
218  {
219  res[inv[i]->get_identifier()] = inv[i]->stat();
220  }
221  }
222  LOG("mapping:size="+sizeof(indices(res)));
223  return res;
224 }
225 
226 /**
227  * get the filename and path of a given path
228  *
229  * @param fname - the file name to process
230  * @return path and filename of a file
231  */
232 array get_filename(string fname)
233 {
234  int sz;
235  array tokens;
236 
237  tokens = fname / "/";
238  if ( !arrayp(tokens) || (sz=sizeof(tokens)) <= 1 )
239  return ({ fname, "" });
240  return ({ tokens[sz-1], (tokens[..sz-2] * "/") + "/"});
241 }
242 
243 /**
244  * Get a path of objects (array) for a given url.
245  *
246  * @param string url - the given url
247  * @return array of objects
248  */
249 array get_path(object obj)
250 {
251  array path = ({ });
252  while ( objectp(obj) ) {
253  path = ({ obj }) + path;
254  obj = obj->get_environment();
255  }
256  return path;
257 }
258 
259 
260 
261 };