KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > OptionsModule


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.quercus.lib;
31
32 import com.caucho.quercus.Quercus;
33 import com.caucho.quercus.QuercusModuleException;
34 import com.caucho.quercus.annotation.Optional;
35 import com.caucho.quercus.env.*;
36 import com.caucho.quercus.lib.file.FileModule;
37 import com.caucho.quercus.module.AbstractQuercusModule;
38 import com.caucho.quercus.program.QuercusProgram;
39 import com.caucho.util.L10N;
40
41 import java.io.IOException JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.Map JavaDoc;
45
46 /**
47  * PHP options
48  */

49 public class OptionsModule extends AbstractQuercusModule {
50   private static final L10N L = new L10N(OptionsModule.class);
51
52   // php/1a0q (phpMyAdmin)
53
public static final String JavaDoc PHP_OS
54     = System.getProperty("os.name").toUpperCase();
55
56   public static final int ASSERT_ACTIVE = 1;
57   public static final int ASSERT_CALLBACK = 2;
58   public static final int ASSERT_BAIL = 3;
59   public static final int ASSERT_WARNING = 4;
60   public static final int ASSERT_QUIET_EVAL = 5;
61
62   public static final int INFO_GENERAL = 1;
63   public static final int INFO_CREDITS = 2;
64   public static final int INFO_CONFIGURATION = 4;
65   public static final int INFO_MODULES = 8;
66   public static final int INFO_ENVIRONMENT = 16;
67   public static final int INFO_VARIABLES = 32;
68   public static final int INFO_LICENSE = 64;
69
70   private static final Value _SERVER = StringValue.create("_SERVER");
71
72   private static final HashMap JavaDoc<String JavaDoc,StringValue> _iniMap
73     = new HashMap JavaDoc<String JavaDoc,StringValue>();
74
75   /**
76    * Returns the default quercus.ini values.
77    */

78   public Map JavaDoc<String JavaDoc,StringValue> getDefaultIni()
79   {
80     return _iniMap;
81   }
82
83   /**
84    * Checks the assertion
85    */

86   public static boolean quercus_assert(Env env, String JavaDoc code)
87   {
88     try {
89       Quercus quercus = env.getQuercus();
90       
91       QuercusProgram program = quercus.parseCode(code);
92       
93       Value value = program.execute(env);
94       
95       return value.toBoolean();
96     } catch (IOException JavaDoc e) {
97       throw new QuercusModuleException(e);
98     }
99   }
100
101   /**
102    * Checks the assertion
103    */

104   public static Value assert_options(Env env,
105                      int code,
106                      @Optional("-1") Value value)
107   {
108     Value result = NullValue.NULL;
109     
110     if (value.equals(LongValue.MINUS_ONE)) {
111       switch (code) {
112       case ASSERT_ACTIVE:
113     result = env.getIni("assert.active");
114     break;
115       case ASSERT_WARNING:
116     result = env.getIni("assert.warning");
117     break;
118       case ASSERT_BAIL:
119     result = env.getIni("assert.bail");
120     break;
121       case ASSERT_QUIET_EVAL:
122     result = env.getIni("assert.quiet_eval");
123     break;
124       case ASSERT_CALLBACK:
125     result = env.getIni("assert.callback");
126     break;
127       default:
128     result = BooleanValue.FALSE;
129     break;
130       }
131     }
132     else {
133       switch (code) {
134       case ASSERT_ACTIVE:
135     result = env.setIni("assert.active", value.toString());
136     break;
137       case ASSERT_WARNING:
138     result = env.setIni("assert.warning", value.toString());
139     break;
140       case ASSERT_BAIL:
141     result = env.setIni("assert.bail", value.toString());
142     break;
143       case ASSERT_QUIET_EVAL:
144     result = env.setIni("assert.quiet_eval", value.toString());
145     break;
146       case ASSERT_CALLBACK:
147     result = env.setIni("assert.callback", value.toString());
148     break;
149       default:
150     result = BooleanValue.FALSE;
151     break;
152       }
153     }
154
155     if (result == null)
156       result = NullValue.NULL;
157
158     return result;
159   }
160
161   /**
162    * Returns true if the given extension is loaded
163    */

164   public static boolean extension_loaded(Env env, String JavaDoc ext)
165   {
166     return env.isExtensionLoaded(ext);
167   }
168
169   /**
170    * Returns true if the given extension is loaded
171    */

172   public static Value get_loaded_extensions(Env env)
173   {
174     ArrayValue value = new ArrayValueImpl();
175
176     for (String JavaDoc ext : env.getLoadedExtensions()) {
177       value.put(ext);
178     }
179
180     return value;
181   }
182
183   /**
184    * Stubs the dl.
185    */

186   public static boolean dl(Env env, String JavaDoc dl)
187   {
188     env.stub("dl is stubbed for dl(" + dl + ")");
189
190     return false;
191   }
192
193   /**
194    * Sets an environment name/value pair.
195    */

196   public static BooleanValue putenv(Env env, StringValue settings)
197   {
198     int eqIndex = settings.indexOf('=');
199
200     if (eqIndex < 0)
201       return BooleanValue.FALSE;
202
203     StringValue key = settings.substring(0, eqIndex);
204     StringValue val = settings.substring(eqIndex + 1);
205
206     env.getQuercus().setServerEnv(key, val);
207
208     return BooleanValue.TRUE;
209   }
210
211   /**
212    * Gets an environment value.
213    */

214   public static Value getenv(Env env, StringValue key)
215   {
216     Value val = env.getQuercus().getServerEnv(key);
217
218     if (val == null) {
219       ArrayValue serverVars = env.getGlobalVar("_SERVER").toArrayValue(env);
220       val = serverVars.get(key);
221     }
222
223     if (val == null || ! val.isset())
224       return BooleanValue.FALSE;
225
226     return val;
227   }
228
229   /**
230    * Returns the configuration value of a configuration.
231    */

232   public static Value get_cfg_var(Env env, String JavaDoc name)
233   {
234     Value value = env.getConfigVar(name);
235
236     if (value != null)
237       return value;
238     else
239       return NullValue.NULL;
240   }
241
242   /**
243    * Returns the owner of the current script.
244    */

245   public static String JavaDoc get_current_user(Env env)
246   {
247     env.stub("get_current_user");
248     
249     return String.valueOf(env.getSelfPath().getOwner());
250   }
251
252   /**
253    * Returns the constants as an array
254    */

255   public static Value get_defined_constants(Env env)
256   {
257     return env.getDefinedConstants();
258   }
259
260   /**
261    * Returns the include path
262    */

263   public static Value get_include_path(Env env)
264   {
265     Value value = env.getIni("include_path");
266
267     if (value != null)
268       return value;
269     else
270       return StringValue.EMPTY;
271   }
272
273   /**
274    * Returns extension function with a given name.
275    */

276   public static Value get_extension_funcs(Env env, String JavaDoc name)
277   {
278     return env.getExtensionFuncs(name);
279   }
280
281   /**
282    * Gets the magic quotes value.
283    */

284   public static Value get_magic_quotes_gpc(Env env)
285   {
286     return (env.getIniBoolean("magic_quotes_gpc")
287         ? BooleanValue.TRUE
288         : BooleanValue.FALSE);
289   }
290
291   /**
292    * Gets the magic quotes runtime value.
293    */

294   public static Value get_magic_quotes_runtime(Env env)
295   {
296     return BooleanValue.FALSE; // PHP 6 removes, so we don't support
297
}
298
299   /**
300    * Gets the magic quotes value.
301    */

302   public static Value magic_quotes_runtime(Env env)
303   {
304     return BooleanValue.FALSE; // PHP 6 removes, so we don't support
305
}
306
307   /**
308    * Returns the gid for the script path.
309    */

310   public static Value getlastmod(Env env)
311   {
312     return FileModule.filemtime(env, env.getSelfPath());
313   }
314
315   /**
316    * Returns the gid for the script path.
317    */

318   public static Value getmygid(Env env)
319   {
320     return FileModule.filegroup(env, env.getSelfPath());
321   }
322
323   /**
324    * Returns the inode for the script path.
325    */

326   public static Value getmyinode(Env env)
327   {
328     return FileModule.fileinode(env, env.getSelfPath());
329   }
330
331   /**
332    * Returns the uid for the script path.
333    */

334   public static Value getmyuid(Env env)
335   {
336     return FileModule.fileowner(env, env.getSelfPath());
337   }
338
339   /**
340    * Returns the thread for the script.
341    */

342   public static long getmypid(Env env)
343   {
344     return Thread.currentThread().getId();
345   }
346
347
348   /**
349    * Sets an initialization value.
350    */

351   public static Value ini_alter(Env env, String JavaDoc varName, String JavaDoc value)
352   {
353     return ini_set(env, varName, value);
354   }
355
356   /**
357    * Returns an initialization value.
358    */

359   public static String JavaDoc ini_get(Env env, String JavaDoc varName)
360   {
361     StringValue v = env.getIni(varName);
362
363     if (v != null)
364       return v.toString();
365     else
366       return null;
367   }
368
369   /**
370    * Returns all initialization values.
371    * XXX: access levels dependent on PHP_INI, PHP_INI_PERDIR, PHP_INI_SYSTEM.
372    *
373    * @param extension assumes ini values are prefixed by extension names.
374    */

375   public static Value ini_get_all(Env env,
376                        @Optional() String JavaDoc extension)
377   {
378     if (extension.length() > 0) {
379       if (! env.isExtensionLoaded(extension)) {
380         env.warning(L.l("extension '" + extension + "' not loaded."));
381         return BooleanValue.FALSE;
382       }
383       extension += ".";
384     }
385   
386     return getAllDirectives(env, extension);
387   }
388
389   private static Value getAllDirectives(Env env, String JavaDoc prefix)
390   {
391     ArrayValue directives = new ArrayValueImpl();
392
393     Value global = new StringValueImpl("global_value");
394     Value local = new StringValueImpl("local_value");
395     Value access = new StringValueImpl("access");
396
397     Value level = new LongValue(7);
398
399     HashMap JavaDoc<String JavaDoc, StringValue> iniMap =
400         env.getQuercus().getIniAll(prefix);
401
402     for (Map.Entry JavaDoc<String JavaDoc,StringValue> entry : iniMap.entrySet()) {
403       ArrayValue inner = new ArrayValueImpl();
404       
405       String JavaDoc key = entry.getKey();
406       Value globalVal = entry.getValue();
407
408       if (globalVal == null)
409         inner.put(NullValue.NULL);
410       else {
411         globalVal = formatIniValue(globalVal);
412         inner.put(global, globalVal);
413       }
414
415       Value localVal = env.getIni(key);
416       if (localVal == null)
417         inner.put(local, globalVal);
418       else
419         inner.put(local, formatIniValue(localVal));
420
421       inner.put(access, level);
422       directives.put(new StringValueImpl(key), inner);
423     }
424
425     return directives;
426   }
427   
428   private static Value formatIniValue(Value val)
429   {
430     String JavaDoc string = val.toString().toLowerCase();
431     if ("on".equals(string))
432       return new StringValueImpl("1");
433     else if ("off".equals(string))
434       return StringValue.EMPTY;
435
436     return val;
437   }
438
439   /**
440    * Restore the initial configuration value
441    */

442   public static Value ini_restore(Env env, String JavaDoc name)
443   {
444     Value value = env.getConfigVar(name);
445
446     if (value != null)
447       env.setIni(name, value.toString());
448
449     return NullValue.NULL;
450   }
451
452   /**
453    * Sets an initialization value.
454    */

455   public static Value ini_set(Env env, String JavaDoc varName, String JavaDoc value)
456   {
457     Value oldValue = env.setIni(varName, value);
458
459     if (oldValue != null)
460       return oldValue;
461     else
462       return NullValue.NULL;
463   }
464
465   /**
466    * Returns the sapi type.
467    */

468   public static String JavaDoc php_sapi_name()
469   {
470     return "apache";
471   }
472
473   public static void phpinfo(Env env, @Optional("-1") int what)
474   {
475     if ((what & INFO_GENERAL) != 0)
476       phpinfoGeneral(env);
477   }
478
479   private static void phpinfoGeneral(Env env)
480   {
481     env.print("<h1>Quercus</h1>");
482     env.print("<pre>");
483     env.println("PHP Version => " + phpversion("std"));
484     env.println("System => " + System.getProperty("os.name") + " "
485           + System.getProperty("os.version") + " "
486           + System.getProperty("os.arch"));
487     env.println("Build Date => " + env.getQuercus().getVersionDate());
488     env.println("Configure Command => n/a");
489     env.println("Server API => CGI");
490     env.println("Virtual Directory Support => disabled");
491     env.println("Configuration File (php.ini) Path => WEB-INF/php.ini");
492     env.println("PHP API => 20031224");
493     env.println("PHP Extension => 20041030");
494     env.println("Debug Build => no");
495     env.println("Thread Safety => enabled");
496     env.println("Registered PHP Streams => php, file, http, https");
497     env.println("</pre>");
498   }
499
500   /**
501    * Returns system information
502    */

503   public static String JavaDoc php_uname(@Optional("'a'") String JavaDoc mode)
504   {
505     // XXX: stubbed
506

507     if (mode == null || mode.equals(""))
508       mode = "a";
509
510     switch (mode.charAt(0)) {
511     case 's':
512       return PHP_OS;
513
514     case 'n':
515       return "localhost";
516
517     case 'r':
518       return "2.4.0";
519
520     case 'v':
521       return "Version 2.4.0";
522
523     case 'm':
524       return "i386";
525
526     case 'a':
527     default:
528       return (php_uname("s") + " " +
529               php_uname("n") + " " +
530               php_uname("r") + " " +
531               php_uname("v") + " " +
532               php_uname("m"));
533     }
534   }
535
536   /**
537    * Returns the quercus version.
538    */

539   public static String JavaDoc phpversion(@Optional String JavaDoc module)
540   {
541     return "5.0.4";
542   }
543
544   /**
545    * Sets the include path
546    */

547   public static String JavaDoc set_include_path(Env env, String JavaDoc includePath)
548   {
549     return env.setIncludePath(includePath);
550   }
551
552   /**
553    * Sets the include path
554    */

555   public static Value restore_include_path(Env env)
556   {
557     env.restoreIncludePath();
558
559     return NullValue.NULL;
560   }
561
562   /**
563    * Sets the magic quotes value.
564    */

565   public static Value set_magic_quotes_runtime(Env env, Value value)
566   {
567     return BooleanValue.FALSE; // PHP 6 removes magic_quotes
568
}
569
570   /**
571    * Sets the time limit
572    */

573   public static Value set_time_limit(Env env, long seconds)
574   {
575     env.setTimeLimit(seconds * 1000L);
576
577     return NullValue.NULL;
578   }
579
580   /**
581    * Compares versions
582    */

583   public static Value version_compare(String JavaDoc version1,
584                                       String JavaDoc version2,
585                                       @Optional("cmp") String JavaDoc op)
586   {
587     ArrayList JavaDoc<Value> expanded1 = expandVersion(version1);
588     ArrayList JavaDoc<Value> expanded2 = expandVersion(version2);
589
590     int cmp = compareTo(expanded1, expanded2);
591
592     if ("eq".equals(op) || "==".equals(op) || "=".equals(op))
593       return cmp == 0 ? BooleanValue.TRUE : BooleanValue.FALSE;
594     else if ("ne".equals(op) || "!=".equals(op) || "<>".equals(op))
595       return cmp != 0 ? BooleanValue.TRUE : BooleanValue.FALSE;
596     else if ("lt".equals(op) || "<".equals(op))
597       return cmp < 0 ? BooleanValue.TRUE : BooleanValue.FALSE;
598     else if ("le".equals(op) || "<=".equals(op))
599       return cmp <= 0 ? BooleanValue.TRUE : BooleanValue.FALSE;
600     else if ("gt".equals(op) || ">".equals(op))
601       return cmp > 0 ? BooleanValue.TRUE : BooleanValue.FALSE;
602     else if ("ge".equals(op) || ">=".equals(op))
603       return cmp >= 0 ? BooleanValue.TRUE : BooleanValue.FALSE;
604     else {
605       if (cmp == 0)
606         return new LongValue(0);
607       else if (cmp < 0)
608         return new LongValue(-1);
609       else
610         return new LongValue(1);
611     }
612   }
613
614   public static String JavaDoc quercus_quercus_version(Env env)
615   {
616     return env.getQuercus().getVersion();
617   }
618
619   public static String JavaDoc zend_version()
620   {
621     return "2.0.4";
622   }
623
624   private static ArrayList JavaDoc<Value> expandVersion(String JavaDoc version)
625   {
626     ArrayList JavaDoc<Value> expand = new ArrayList JavaDoc<Value>();
627
628     int len = version.length();
629     int i = 0;
630
631     while (i < len) {
632       char ch = version.charAt(i);
633
634       if ('0' <= ch && ch <= '9') {
635         int value = 0;
636
637         for (; i < len && '0' <= (ch = version.charAt(i)) && ch <= '9'; i++) {
638           value = 10 * value + ch - '0';
639         }
640
641         expand.add(new LongValue(value));
642       }
643       else if (Character.isLetter((char) ch)) {
644         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
645
646         for (; i < len && Character.isLetter(version.charAt(i)); i++) {
647           sb.append((char) ch);
648         }
649
650         String JavaDoc s = sb.toString();
651
652         if (s.equals("dev"))
653           s = "a";
654         else if (s.equals("alpha") || s.equals("a"))
655           s = "b";
656         else if (s.equals("beta") || s.equals("b"))
657           s = "c";
658         else if (s.equals("RC"))
659           s = "d";
660         else if (s.equals("pl"))
661           s = "e";
662         else
663           s = "z" + s;
664
665         expand.add(new StringValueImpl(s));
666       }
667       else
668         i++;
669     }
670
671     return expand;
672   }
673
674   private static int compareTo(ArrayList JavaDoc<Value> a, ArrayList JavaDoc<Value> b)
675   {
676     int i = 0;
677
678     while (true) {
679       if (a.size() <= i && b.size() <= i)
680         return 0;
681       else if (a.size() <= i)
682         return -1;
683       else if (b.size() <= i)
684         return 1;
685
686       int cmp = compareTo(a.get(i), b.get(i));
687
688       if (cmp != 0)
689         return cmp;
690
691       i++;
692     }
693   }
694
695   private static int compareTo(Value a, Value b)
696   {
697     if (a.equals(b))
698       return 0;
699     else if (a.isLongConvertible() && ! b.isLongConvertible())
700       return -1;
701     else if (b.isLongConvertible() && ! a.isLongConvertible())
702       return 1;
703     else if (a.lt(b))
704       return -1;
705     else
706       return 1;
707   }
708
709   static {
710     addIni(_iniMap, "assert.active", "1", PHP_INI_ALL);
711     addIni(_iniMap, "assert.bail", "0", PHP_INI_ALL);
712     addIni(_iniMap, "assert.warning", "1", PHP_INI_ALL);
713     addIni(_iniMap, "assert.callback", null, PHP_INI_ALL);
714     addIni(_iniMap, "assert.quiet_eval", "0", PHP_INI_ALL);
715     addIni(_iniMap, "enable_dl", "1", PHP_INI_SYSTEM);
716     addIni(_iniMap, "max_execution_time", "30", PHP_INI_ALL);
717     addIni(_iniMap, "max_input_time", "-1", PHP_INI_PERDIR);
718     addIni(_iniMap, "magic_quotes_gpc", "1", PHP_INI_PERDIR);
719     // magic_quotes is ignored in PHP 6
720
addIni(_iniMap, "magic_quotes_runtime", "0", PHP_INI_ALL);
721
722     // basic
723
addIni(_iniMap, "track_vars", "On", PHP_INI_ALL);
724     addIni(_iniMap, "arg_separator.output", "&", PHP_INI_ALL);
725     addIni(_iniMap, "arg_separator.input", "&", PHP_INI_ALL);
726     addIni(_iniMap, "variables_order", "EGPCS", PHP_INI_ALL);
727     addIni(_iniMap, "auto_globals_jit", "1", PHP_INI_ALL);
728     // register_globals is ignored in PHP 6
729
addIni(_iniMap, "register_globals", "0", PHP_INI_ALL);
730     addIni(_iniMap, "register_argc_argv", "1", PHP_INI_ALL);
731     addIni(_iniMap, "register_long_arrays", "1", PHP_INI_ALL);
732     addIni(_iniMap, "post_max_size", "8M", PHP_INI_ALL);
733     addIni(_iniMap, "gpc_order", "GPC", PHP_INI_ALL);
734     addIni(_iniMap, "auto_prepend_file", null, PHP_INI_ALL);
735     addIni(_iniMap, "auto_append_file", null, PHP_INI_ALL);
736     addIni(_iniMap, "default_mimetype", "text/html", PHP_INI_ALL);
737     addIni(_iniMap, "default_charset", "", PHP_INI_ALL);
738     addIni(_iniMap, "always_populate_raw_post_data", "0", PHP_INI_ALL);
739     addIni(_iniMap, "allow_webdav_methods", "0", PHP_INI_ALL);
740
741     // file uploads
742
addIni(_iniMap, "file_uploads", "1", PHP_INI_SYSTEM);
743     addIni(_iniMap, "upload_tmp_dir", null, PHP_INI_SYSTEM);
744     addIni(_iniMap, "upload_max_filesize", "2M", PHP_INI_SYSTEM);
745     
746     addIni(_iniMap, "memory_limit", "-1", PHP_INI_ALL);
747   }
748
749   //@todo mixed assert_options(int what [, mixed value])
750
//@todo boolean assert(mixed assertion)
751
//@todo int dl(string library)
752
//@todo boolean extension_loaded(string name)
753
//@todo string get_cfg_var(string varname)
754
//@todo string get_current_user()
755
//@todo array get_extension_funcs(string module_name)
756
//@todo string get_include_path()
757
//@todo array get_included_files()
758
//@todo array get_loaded_extensions()
759
//@todo int get_magic_quotes_runtime()
760
//@todo array get_required_files() ALIAS of get_included_files
761
//@todo int getlastmod()
762
//@todo int getmygid()
763
//@todo int getmyinode()
764
//@todo int getmypid()
765
//@todo int getmyuid()
766
//@todo array getopt(string options [,array longopts])
767
//@todo array getrusage([int who])
768
//@todo string ini_alter(string varname, string newvalue) ALIAS of ini_set
769
//@todo array ini_get_all([string extension])
770
//@todo void ini_restore(string varname)
771
//XXX main is Dummy for main()
772
//@todo int memory_get_usage()
773
//@todo string quercus_ini_scanned_files()
774
//@todo string quercus_logo_guid()
775
//@todo string quercus_uname([string mode])
776
//@todo boolean quercuscredits([int flag])
777
//@todo boolean quercusinfo([int what])
778
//@todo boolean pupenv(string setting)
779
//@todo void restore_include_path()
780
//@todo string set_include_path(string new_include_path)
781
//@todo string zend_logo_guid()
782

783
784 }
785
786
Popular Tags