Skip to content
Newer
Older
100644 908 lines (905 sloc) 23.9 KB
fd2e8ef m
wis authored Nov 14, 2006
1 <?
2 include("qwebdom4.php");
3 # vim:foldcolumn=3 foldnestmax=2 foldlevel=0:
4 #
5 # Written by Antony Lesuisse (al@udev.org) in 2004-2005.
6 # Public domain. The author disclaims copyright to this
7 # source code.
8 #
9 #------------------------------------------------------
10 # lib: many useful functions for PHP
11 #------------------------------------------------------
12 # missing var_export function for php < 4.2
13 if (!function_exists("var_export")) {
14 function var_export() {
15 $args = func_get_args();
16 $indent = (isset($args[2])) ? $args[2] : '';
17 if (is_array($args[0])) {
18 $output = 'array ('."\n";
19 foreach ($args[0] as $k => $v) {
20 if (is_numeric($k))
21 $output .= $indent.' '.$k.' => ';
22 else
23 $output .= $indent.' \''.str_replace('\'', '\\\'', str_replace('\\', '\\\\', $k)).'\' => ';
24 if(is_array($v))
25 $output .= var_export($v, true, $indent.' ');
26 else {
27 if (gettype($v) != 'string' && !empty($v))
28 $output .= $v.','."\n";
29 else
30 $output .= '\''.str_replace('\'', '\\\'', str_replace('\\', '\\\\', $v)).'\','."\n";
31 }
32 }
33 $output .= ($indent != '') ? $indent.'),'."\n" : ')';
34 } else
35 $output = $args[0];
36 if ($args[1] == true)
37 return $output;
38 else
39 echo $output;
40 }
41 }
42 function lib_http_404() {
43 header("HTTP/1.0 404 Not Found");
44 echo "<h1>404 Not Found</h1>";
45 exit;
46 }
47 function lib_http_302($url="http://www.google.com/search?q=love") {
48 header('HTTP/1.0 302 Moved Temporarily');
49 header("Location: $url");
50 exit;
51 }
52 function lib_microtime() {
53 list($usec,$sec)=explode(" ",microtime());
54 return (float)$usec+(float)$sec;
55 }
56 function lib_pwgen($min=6,$max=8) {
57 $char="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
58 $char="aaabcdeeefghiiijklmnooopqrstuuuvwxyz";
59 $pwd="";
60 while(strlen($pwd)<rand($min,$max)) {
61 $pwd.=$char{rand(0,strlen($char)-1)};
62 }
63 return $pwd;
64 }
65 function lib_split($nbr,$l1) {
66 $n=sizeof($l1);
67 $k=0;
68 $l2=array();
69 for($i=0;$i<$n;$i++) {
70 if($i >= (($k+1)*($n/$nbr))) {
71 $k+=1;
72 }
73 $l2[$k][]=$l1[$i];
74 }
75 return $l2;
76 }
77 function lib_readfile($name) {
78 $f=fopen($name,"r");
79 $r="";
80 while(!feof($f))
81 $r.=fread($f,65536);
82 fclose($f);
83 return $r;
84 }
85 function lib_xml_print($n) {
86 $nt=$n->type;
87 $nn=$n->tagname;
88 $nc=$n->children();
89 $nc=$nc?$nc:array();
90 if($nt == XML_DOCUMENT_NODE) {
91 foreach($nc as $i)
92 lib_xml_print($i);
93 } if($nt == XML_TEXT_NODE) {
94 echo str_replace(">","&gt;",str_replace("<","&lt;",$n->content));
95 } elseif($nt == XML_ELEMENT_NODE) {
96 $na=$n->attributes();
97 $na=$na?$na:array();
98 $att="";
99 foreach($na as $i) {
100 $an=$i->name;
101 $av=$i->value;
102 $att.=" $an=\"$av\"";
103 }
104 if(sizeof($nc)) {
105 echo "<$nn$att>";
106 foreach($nc as $i)
107 lib_xml_print($i);
108 echo "</$nn>";
109 } else {
110 echo "<$nn$att/>";
111 }
112 }
113 }
114 function lib_str_l1toa($s) {
115 return strtr($s,
116 "サ請唾津毒班碧麺力佰厶壞嶷掣桀毯珀矣粤肄蓍裨跋鈿韵鴦��ŠŒšœžŸ",
117 "YuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyySOZsozY");
118 }
119 function lib_stra($s) {
120 return preg_replace("/[^0-9A-Za-z _-]/"," ",lib_str_l1toa($s));
121 }
122 function lib_stru($s) {
123 return preg_replace("/[^0-9A-Za-z_]/","_",lib_str_l1toa($s));
124 }
125 function lib_download($fname,$fstr=0,$fpath="") {
126 if(is_string($fstr) and $fsize=strlen($fstr)) {
127 header("Content-Type: application/octet-stream");
128 header("Content-Type: application/force-download");
129 header("Content-Type: application/download");
130 header("Content-Disposition: attachment; filename=\"$fname\"");
131 header("Content-Transfer-Encoding: binary");
132 header("Content-Length: ".$fsize);
133 echo $fstr;
134 exit;
135 } elseif($fsize=filesize($fpath)) {
136 #$temp=explode("=",$HTTP_RANGE);
137 #$temp2=explode("-",$temp[1]);
138 #$offset=$temp2[0];
139 if(!$offset)
140 $offset=0;
141 $fd=fopen($fpath,"rb");
142 if($offset==0) {
143 header("Content-Disposition: attachment; filename=\"$fname\"");
144 header("Content-Transfer-Encoding: binary");
145 header("Content-Length: ".$fsize);
146 } else {
147 header("HTTP/1.1 206 Partial Content");
148 header("Content-Range: bytes $offset-".($fsize-1)."/".$fsize);
149 header("Content-Length: ".($fsize-$offset));
150 header("Connection: close" );
151 fseek($fd,$offset);
152 }
153 header("Content-Type: application/octet-stream");
154 header("Content-Type: application/force-download");
155 header("Content-Type: application/download");
156 fpassthru($fd);
157 exit;
158 }
159 return 1;
160 }
161 function lib_system() {
162 $arg=func_get_args();
163 if(is_array($arg[0]))
164 $arg=$arg[0];
165 $cmd=array_shift($arg);
166 foreach($arg as $i) {
167 $cmd.=" ''".escapeshellarg($i);;
168 }
169 system($cmd);
170 }
171 function lib_popen($arg,$mode) {
172 $cmd=array_shift($arg);
173 foreach($arg as $i) {
174 $cmd.=" ''".escapeshellarg($i);;
175 }
176 return popen($cmd,$mode);
177 }
178 # attchments depends on mime-construct(1)
179 function lib_mail($mail,$type="text",$fpath="",$fname="",$ftype="application/octet-stream") {
180 list($h,$b)=explode("\n\n",trim($mail),2);
181 $hl=explode("\n",$h);
182 foreach($hl as $i) {
183 if(preg_match("/^From: .*?([A-Za-z0-9_.-]+@[A-Za-z0-9.-]+)/",$i,$m))
184 $sender=$m[1];
185 }
186 if($type=="text")
187 $type='text/plain; charset="iso-8859-1"';
188 elseif($type="html")
189 $type='text/html; charset="iso-8859-1"';
190 $msg="";
191 if(strlen($fpath)==0) {
192 $hl[]="MIME-Version: 1.0";
193 $hl[]="Content-Type: $type";
194 $hl[]="Content-Transfer-Encoding: 8bit";
195 foreach($hl as $i)
196 $msg.="$i\n";
197 $msg.="\n$b";
198 } else {
199 $c=array("mime-construct","--output","--type",$type,"--encoding","8bit");
200 foreach($hl as $i)
201 array_splice($c,count($c),0,array("--header",$i));
202 array_splice($c,count($c),0,array("--string",$b));
203 if(strlen($fname)==0)
204 $fname=basename($fpath);
205 array_splice($c,count($c),0,array("--type",$ftype,"--encoding","base64","--attachment",$fname,"--file",$fpath));
206 $p=lib_popen($c,"r");
207 while($t=fread($p,8192))
208 $msg.=$t;
209 pclose($p);
210 }
211 if($sender)
212 $p=lib_popen(array("/usr/sbin/sendmail","-t","-f",$sender),"w");
213 else
214 $p=lib_popen(array("/usr/sbin/sendmail","-t"),"w");
215 # echo "<pre>$msg</pre>";
216 fwrite($p,$msg);
217 pclose($p);
218 }
219 # TODO fix $k should not increment for %%, usr i for src, j for arg i++,j++ right place
220 function lib_query_printf() {
221 $arg=func_get_args();
222 $src=explode("%",array_shift($arg));
223 $q=array_shift($src);
224 foreach($src as $k=>$v) {
225 if($v{0}=="s") {
226 $q.=addslashes($arg[$k]).substr($v,1);
227 } elseif($v{0}=="r") {
228 $q.=$arg[$k].substr($v,1);
229 } elseif($v{0}=="a") {
230 $tmp="";
231 foreach($arg[$k] as $key=>$val)
232 $tmp.="$key='".addslashes($val)."',";
233 $q.=substr($tmp,0,-1).substr($v,1);
234 } elseif(strlen($v)==0) {
235 $q.="%";
236 } else {
237 $q.=$v;
238 }
239 }
240 return $q;
241 }
242 function lib_query() {
243 $arg=func_get_args();
244 return mysql_query(call_user_func_array('lib_query_printf',$arg));
245 }
246 function lib_queryt() {
247 $arg=func_get_args();
248 echo call_user_func_array('lib_query_printf',$arg);
249 }
250 function lib_query_fetch() {
251 $arg=func_get_args();
252 $a=array();
253 $q=mysql_query(call_user_func_array('lib_query_printf',$arg));
254 while($r=mysql_fetch_assoc($q))
255 $a[]=$r;
256 return $a;
257 }
258 function lib_trim($a) {
259 $r=array();
260 foreach($a as $k=>$v)
261 $r[$k]=trim($v);
262 return $r;
263 }
264 function lib_pre() {
265 $arg=func_get_args();
266 $pre=array_shift($arg);
267 $r=array();
268 foreach($arg as $a) {
269 foreach($a as $k=>$v)
270 $r[$pre."_".$k]=$v;
271 }
272 return $r;
273 }
274 function lib_suffix() {
275 $arg=func_get_args();
276 $pre=array_shift($arg)."_";
277 $r=array();
278 foreach($arg as $a) {
279 foreach($a as $k=>$v) {
280 $key = strncmp($pre,$k,strlen($pre))==0 ? substr($k,strlen($pre)) : $k;
281 $r[$key]=$v;
282 }
283 }
284 return $r;
285 }
286 function lib_merge($a,$b,$pre) {
287 }
288 # zipfile handling
289 function lib_zip_open($zip,$name) {
290 return lib_popen(array("unzip","-p",$zip,$name),"r");
291 }
292 function lib_zip_readfile($zip,$name) {
293 $f=lib_zip_open($zip,$name);
294 $r="";
295 while(!feof($f))
296 $r.=fread($f,65536);
297 pclose($f);
298 return $r;
299 }
300 function lib_zip_print($zip,$name) {
301 $f=lib_zip_open($zip,$name);
302 $c=0;
303 while(!feof($f)) {
304 $s=fread($f,4096);
305 $c+=strlen($s);
306 echo $s;
307 }
308 pclose($f);
309 return $c;
310 }
311 function lib_zip_serve($zip,$name) {
312 if(strlen($name)==0)
313 $name="index.html";
314 if(preg_match("/\\.(gif)$/i",$name,$m)) {
315 header("Content-Type: image/gif");
316 } elseif(preg_match("/\\.(jpg|jpeg|png)$/i",$name,$m)) {
317 header("Content-Type: image/jpeg");
318 } elseif(preg_match("/\\.(png)$/i",$name,$m)) {
319 header("Content-Type: image/png");
320 } elseif(preg_match("/\\.(txt)$/i",$name,$m)) {
321 header("Content-Type: text/plain");
322 }
323 if(lib_zip_print($zip,$name)==0) {
324 header("HTTP/1.0 404 Not Found");
325 echo "<h1>404 Not Found</h1>";
326 exit();
327 }
328 }
329 function lib_zip_example() {
330 $s=$_SERVER["PATH_INFO"];
331 if(preg_match("!^/(actualite[0-9-]+|dossier[0-9-]+)/(.*)$!",$s,$m)) {
332 $zip=$m[1];
333 $name=$m[2];
334 lib_zip_serve("files/$zip.zip",$name);
335 }
336 }
337 #----------------------------------------------------------
338 # qweb: Quick Web Toolkit, url, xml-template, form, smvc
339 # globals $qweb_u (url), $qweb_x (template)
340 #----------------------------------------------------------
341 class qweb_smvc {
342 # public
343 var $state="";
344 # deprecated
345 var $state_arg=array();
346 # private
347 var $q_run=array(),$q_run_i=0,$q_sub=array(),$q_sel;
348 # new view context
349 var $q_vc;
350 function view($a=array()) {
351 $a=array_merge($a,$this->q_vc);
352 $this->q_vc=0;
353 foreach($this->q_state_split($this->state) as $i) {
354 $m="s{$i}_view";
355 if(method_exists($this,$m)) {
356 $args=$this->state_arg;
357 array_unshift($args,$a);
358 $a=call_user_func_array(array(&$this,$m),$args);
359 if(is_string($a))
360 return $a;
361 }
362 }
363 }
364 function control($arg=0,$vc=array()) {
365 if(!is_array($arg))
366 $arg=array_merge($_GET,$_POST);
367 $this->q_vc=$vc;
368 $pri=0;
369 $this->q_sel=0;
370 foreach($this->q_sub as $k=>$v) {
371 $o=& $this->q_sub[$k]["obj"];
372 if($o->control($arg) and $v["pri"]>$pri) {
373 $this->q_sel=$k;
374 $pri=$v["pri"];
375 }
376 }
377 $rc=0;
378 $this->q_run=$this->q_state_split($this->state);
379 for($i=0;$i<count($this->q_run);$i++) {
380 $s1=$this->q_run[$i];
381 $m="s{$s1}_control";
382 if(method_exists($this,$m)) {
383 $this->q_run_i=$i;
384 $ret=$this->$m($arg,&$this->q_vc);
385 if(is_int($ret) or is_bool($ret)) {
386 $rc=$ret;
387 } elseif(is_array($ret)) {
388 $arg=$r;
389 }
390 }
391 }
392 return $rc;
393 }
394 function q_state_split($state) {
395 $r=array("");
396 if($state=="")
397 return $r;
398 $s=explode("_",$state);
399 for($i=0;$i<sizeof($s);$i++)
400 $r[]="_".join("_",array_slice($s,0,$i+1));
401 return $r;
402 }
403 function q_state_set() {
404 $arg=func_get_args();
405 $s=array_shift($arg);
406 if(count($arg))
407 $this->state_arg=$arg;
408 $this->state=$s;
409 $prev=array_slice($this->q_run,0,$this->q_run_i+1);
410 $next=array();
411 foreach($this->q_state_split($s) as $i) {
412 if(!in_array($i,$prev))
413 $next[]=$i;
414 }
415 array_splice($prev,count($prev),count($prev),$next);
416 # array_splice($this->q_run,$this->q_run_i,count($this->q_run),array()); To stop
417 $this->q_run=$prev;
418 }
419 function q_sub_add($name,&$wid,$pri=0) {
420 $this->q_sub[$name]=array("obj"=>$wid,"pri"=>$pri);
421 }
422 function q_sub_del($name) {
423 unset($this->q_sub[$name]);
424 }
425 function q_sub_select() {
426 return $this->q_sel;
427 }
428 function q_sub_view($name=0,$a=array()) {
429 if(!is_string($name))
430 $name=$this->q_sel;
431 if(array_key_exists($name,$this->q_sub)) {
432 $o=& $this->q_sub[$name]["obj"];
433 return $o->view($a);
434 }
435 }
436 function &q_sub_get($name) {
437 if(array_key_exists($name,$this->q_sub)) {
438 return $this->q_sub[$name]["obj"];
439 }
440 }
441 }
442 class qweb_url {
443 var $base,$args;
444 function qweb_url($base="index.php",$arg="") {
445 $this->base=$base;
446 $this->param=array();
447 $this->param_reg($arg);
448 }
449 function param_add($s,$p=0) {
450 if($p==0)
451 $p=$this->param;
452 if(strlen($s)) {
453 foreach(explode("&",$s) as $key=>$val) {
454 $a=explode("=",$val);
455 $p[$a[0]]=$a[1];
456 }
457 }
458 return $p;
459 }
460 function param_reg($s) {
461 $this->param=$this->param_add($s);
462 }
463 function param_str($p=0) {
464 if($p==0)
465 $p=$this->param;
466 foreach($p as $key=>$val)
467 $s.="$key=".rawurlencode($val)."&amp;";
468 return substr($s,0,strlen($s)-5);
469 }
470 function href($i="") {
471 return 'href="'.$this->base.'?'.$this->param_str($this->param_add($i)).'"';
472 }
473 function str($i="",$class="",$title="") {
474 if($class)
475 $class=" class=\"$class\"";
476 if($title)
477 $class=" title=\"$title\"";
478 return '<a '.$this->href($i)."$class$title>";
479 }
480 function form_action() {
481 return "action=\"".$this->base."\"";
482 }
483 function form_input($i="") {
484 $t=$this->param_add($i);
485 foreach($t as $key=>$val)
486 $s.="<input type=\"hidden\" name=\"$key\" value=\"".rawurlencode($val)."\">\n";
487 return $s;
488 }
489 function form_get($i="") {
490 return "<form method=\"get\" ".$this->form_action().">\n".$this->form_input($i);
491 }
492 function form_post($i="") {
493 return "<form method=\"post\" ".$this->form_action().">\n".$this->form_input($i);
494 }
495 function form_multi($i="") {
496 return "<form enctype=\"multipart/form-data\" method=\"post\" action=\"".$this->base."\">\n".$this->form_input($i);
497 }
498 function request() {
499 return array_merge($_GET,$_POST);
500 }
501 }
502 # TODO add input SUMIBT -> submitted, Missing-defined ?
503 class qweb_form {
504 function qweb_form($name,$def=0,$pre="") {
505 $this->v_sumitted=0;
506 $this->v_def=array();
507 $this->v_input=array();
508 $this->v_error=array();
509 $this->process($name);
510 if(is_array($def))
511 $this->default_fill($def,$pre);
512 }
513 function default_empty() {
514 return sizeof($this->v_input)==0;
515 }
516 function default_set($k,$v) {
517 echo "";
518 $this->v_def[$k]=$v;
519 }
520 function default_fill($a,$pre="") {
521 foreach($a as $name=>$v)
522 $this->default_set($pre.$name,$v);
523 }
524 function process_node($n,$v) {
525 $nt=$n->type;
526 $nn=$n->tagname;
527 $nc=$n->children();
528 $nc=$nc?$nc:array();
529 if($nt == XML_ELEMENT_NODE) {
530 $na=$n->attributes();
531 $na=$na?$na:array();
532 $tv=array();
533 foreach($na as $i) {
534 $an=$i->name;
535 $av=$i->value;
536 if(strncmp("t-",$an,2)==0)
537 $tv[substr($an,2)]=$av;
538 }
539 if($value=$tv["value"]) {
540 if($name=$tv["name"] and ($tv["type"]=="checkbox" or $tv["type"]=="radio")) {
541 if($tv["selected"])
542 $this->v_def[$name]=$value;
543 } elseif($name=$tv["select"]) {
544 if($tv["selected"])
545 $this->v_def[$name]=$value;
546 } elseif($name=$tv["name"]) {
547 $this->v_def[$name]=$value;
548 }
549 }
550 if($name=$tv["name"]) {
551 if(array_key_exists($name,$v)) {
552 $this->v_submitted=1;
553 if(!($check=$tv["check"]))
554 $check="";
555 if($check=="email")
556 $check='/^[^@#!& ]+@[A-Za-z0-9-][.A-Za-z0-9-]{0,64}\\.[A-Za-z]{2,5}$/';
557 if($tv["notrim"])
558 $val=$v[$name];
559 else
560 $val=trim($v[$name]);
561 $this->v_input[$name]=$val;
562 if($check and (!preg_match($check,$val))) {
563 $this->v_error[$name]=1;
564 }
565 }
566 }
567 foreach($nc as $i)
568 $this->process_node($i,$v);
569 }
570 }
571 function process($name,$v=0) {
572 global $qweb_x;
573 if(!$v)
574 $v = array_merge($_GET,$_POST);
575 $x=$qweb_x->xml_template($name);
576 $this->process_node($x,$v);
577 }
578 function error_set($k) {
579 $this->v_error[$k]=1;
580 }
581 function error_clear($k) {
582 unset($this->v_error[$k]);
583 }
584 function error_get($k) {
585 return $this->v_error[$k];
586 }
587 function error_any() {
588 return (sizeof($this->v_error)!=0) and $this->v_submitted;
589 }
590 function input_set($k,$v) {
591 $this->v_input[$k]=$v;
592 }
593 function input_get($k) {
594 return $this->v_input[$k];
595 }
596 function input_valid() {
597 return (sizeof($this->v_error)==0) and $this->v_submitted;
598 }
599 function input_collect($pre="") {
600 $n=strlen($pre);
601 $x=array();
602 foreach($this->v_input as $name=>$v)
603 $x[substr($name,$n)]=$v;
604 return $x;
605 }
606 function display_get($name) {
607 if(array_key_exists($name,$this->v_input))
608 return $this->v_input[$name];
609 else
610 return $this->v_def[$name];
611 }
612 }
613 class qweb_xml {
614 var $template=array();
615 function qweb_xml($xml="") {
616 if($xml)
617 $this->load($xml);
618 }
619 function load($xml) {
620 if(strncmp($xml,"<?xml",5)==0)
621 $n0=xmldoc($xml);
622 else
623 $n0=xmldocfile($xml);
624 $n1=$n0->children();
625 foreach($n1 as $j) {
626 $n2=$j->children();
627 foreach($n2 as $i) {
628 if($i->type==XML_ELEMENT_NODE and $i->tagname=="t") {
629 $ia=$i->attributes();
630 if(($ia[0]->name=="name") or ($ia[0]->name=="form")) {
631 $this->template[$ia[0]->value]=$i;
632 }
633 }
634 }
635 }
636 }
637 function xml_template($te) {
638 if(array_key_exists($te,$this->template))
639 return $this->template[$te];
640 else
641 die("template: $te missing.");
642 }
643 function xml_eval($dic,$_expr) {
644 extract($dic);
645 return eval("return $_expr;");
646 }
647 function xml_element($nn,$att,$nc,$dic,$trim=0,$pre="") {
648 $g_inner="";
649 foreach($nc as $i)
650 $g_inner.=$this->xml_node($i,$dic);
651 if($trim)
652 $g_inner=trim($g_inner);
653 if($nn=="t") {
654 return $g_inner;
655 } elseif(sizeof($nc) or strlen($pre)) {
656 return "<$nn$att>$pre$g_inner</$nn>";
657 } else {
658 return "<$nn$att/>";
659 }
660 }
661 function xml_node($n,$dic) {
662 global $qweb_u;
663 $r="";
664 $nt=$n->type;
665 $nn=$n->tagname;
666 $nc=$n->children();
667 $nc=$nc?$nc:array();
668 if($nt == XML_TEXT_NODE) {
669 $r.=utf8_decode($n->content);
670 } elseif($nt == XML_ELEMENT_NODE) {
671 $na=$n->attributes();
672 $na=$na?$na:array();
673 $att="";
674 $tv=array();
675 $trim=0;
676 foreach($na as $i) {
677 $an=$i->name;
678 $av=$i->value;
679 if(strncmp("t-",$an,2)==0) {
680 if(strncmp("t-att-",$an,6)==0) {
681 $an=substr($an,6);
682 $av=$this->xml_eval($dic,$av);
683 $av=htmlspecialchars($av);
684 $att.=" $an=\"$av\"";
685 } elseif(strncmp("t-href",$an,6)==0) {
686 $att.=' '.$qweb_u->href($this->xml_eval($dic,"\"$av\""));
687 } elseif(strncmp("t-trim",$an,6)==0) {
688 $trim=$av;
689 } else {
690 $tv[substr($an,2)]=$av;
691 }
692 } else {
693 $att.=" $an=\"".htmlspecialchars(utf8_decode($av)).'"';
694 }
695 }
696 if($tv) {
697 if($tv["raw"]=="0") {
698 $r.=$dic[0];
699 } elseif($e=$tv["rawf"]) {
700 $r.=sprintf($e,$this->xml_eval($dic,$tv["raw"]));
701 } elseif($e=$tv["raw"]) {
702 $r.=$this->xml_eval($dic,$e);
703 } elseif($e=$tv["escf"]) {
704 $r.=htmlentities(sprintf($e,$this->xml_eval($dic,$tv["esc"])));
705 } elseif($e=$tv["esc"]) {
706 $r.=htmlentities($this->xml_eval($dic,$e));
707 } elseif($e=$tv["action"]) {
708 $att.=' '.$qweb_u->form_action();
709 $input=$qweb_u->form_input($this->xml_eval($dic,"\"$e\""));
710 $r.=$this->xml_element($nn,$att,$nc,$dic,$trim,$input);
711 } elseif($e=$tv["if"]) {
712 if($this->xml_eval($dic,$e))
713 $r.=$this->xml_element($nn,$att,$nc,$dic,$trim);
714 } elseif($e=$tv["foreach"]) {
715 if(!array_key_exists($e,$dic)) {
716 echo "template: var $e not found.";
717 } else {
718 foreach($dic[$e] as $i)
719 $r.=$this->xml_element($nn,$att,$nc,array_merge($dic,$i),$trim);
720 }
721 } elseif($e=$tv["call"]) {
722 $tmp="";
723 $arg=array();
724 foreach($nc as $i) {
725 if($i->type==XML_ELEMENT_NODE and $i->tagname=="t") {
726 $ia=$i->attributes();
727 $ia=$ia?$ia:array();
728 foreach($ia as $j) {
729 if($j->name=="t-arg") {
730 $ic=$i->children();
731 $ic=$ic?$ic:array();
732 $arg[$j->value]=$this->xml_element("t","",$ic,$dic);
733 }
734 }
735 }
736 $tmp.=$this->xml_node($i,$dic);
737 }
738 if($p=$tv["prefix"]) {
739 $dic=lib_suffix($p,$dic);
740 }
741 $dic=array_merge($dic,$arg);
742 $dic[0]=$tmp;
743 $r.=$this->render($e,array_merge($dic,$arg));
744 } elseif($e=$tv["inc"]) {
745 $f=fopen($e,"r");
746 while(!feof($f))
747 $r.=fread($f,8192);
748 fclose($f);
749 # Forms attributes
750 } elseif($e=$tv["type"]) {
751 $form=$dic[$tv["form"]] or $form=$dic["form"];
752 $name=$tv["name"];
753 $class=$form->error_get($name) ? "form_error" : "form_valid";
754 if($e=="text" or $e=="password") {
755 $value=htmlentities($form->display_get($name));
756 $att.=" type=\"$e\" name=\"$name\" value=\"$value\" class=\"$class\"";
757 $r.=$this->xml_element($nn,$att,$nc,$dic);
758 } elseif($e=="textarea") {
759 $value=htmlentities($form->display_get($name));
760 $att.=" name=\"$name\" class=\"$class\"";
761 $r.="<$nn$att>$value</$nn>";
762 } elseif($e=="checkbox" or $e=="radio") {
763 $value=$tv["value"];
764 $checked="";
765 if($value==$form->display_get($name))
766 $checked=" checked=\"checked\"";
767 $att.=" type=\"$e\" name=\"$name\" value=\"$value\" class=\"$class\"$checked";
768 $r.=$this->xml_element($nn,$att,$nc,$dic);
769 }
770 } elseif($e=$tv["select"]) {
771 $name=$e;
772 $form=$dic[$tv["form"]] or $form=$dic["form"];
773 $value=$tv["value"];
774 $selected="";
775 if($value==$form->display_get($name))
776 $selected=" selected=\"selected\"";
777 $att.=" value=\"$value\"$selected";
778 $r.=$this->xml_element($nn,$att,$nc,$dic);
779 } elseif($e=$tv["name"]) {
780 $att.=" name=\"$e\"";
781 $r.=$this->xml_element($nn,$att,$nc,$dic);
782 } elseif($e=$tv["error"]) {
783 $form=$tv["form"] ? $dic[$tv["form"]] : $dic["form"];
784 if($form->error_get($e))
785 $r.=$this->xml_element($nn,$att,$nc,$dic);
786 } elseif($e=$tv["invalid"]) {
787 $invalid=0;
788 if($form=$dic[$e] and $form->error_any())
789 $r.=$this->xml_element($nn,$att,$nc,$dic);
790 }
791 } else {
792 $r.=$this->xml_element($nn,$att,$nc,$dic,$trim);
793 }
794 }
795 return $r;
796 }
797 function render($name,$dic=array()) {
798 return $this->xml_node($this->xml_template($name),$dic);
799 }
800 }
801 function qweb_xml_render($name,$dic=array()) {
802 global $qweb_x;
803 return $qweb_x->render($name,$dic);
804 }
805 function qweb_xml_enum($l) {
806 if($s=sizeof($l)) {
807 $l[0]['enum_first']=1;
808 $l[$s-1]['enum_last']=1;
809 $l[$s-1]['enum_lastonly']=($s!=1);
810 for($i=0;$i<$s;$i++) {
811 $l[$i]['enum']=$i;
812 $l[$i]['enum_size']=$s;
813 $l[$i]['enum_middle']=(($i!=0)and($i!=$s-1));
814 }
815 $l[(int)(($s-1)/2)]['enum_half']=1;
816 }
817 return $l;
818 }
819 function qweb_xml_pager($num,$step,$cur=0,$max=5) {
820 $a=array();
821 $a["pager_tot_val"]=$num;
822 $a["pager_tot_page"]=$tot_page=(int)ceil($num/$step);
823 $a["pager_cur_val"]=$cur;
824 $a["pager_cur_val1"]=$num?$cur+1:0;
825 $a["pager_cur_page"]=$cur_page=(int)floor($cur/$step);
826 $a["pager_cur_page1"]=$cur_page+1;
827 $a["pager_cur_end"]=max(0,min($cur+$step-1,$num-1));
828 $a["pager_cur_end1"]=min($cur+$step,$num);
829 if($cur_page==0) {
830 $a["pager_prev"]=0;
831 } else {
832 $a["pager_prev"]=1;
833 $a["pager_prev_page"]=$cur_page-1;
834 $a["pager_prev_val"]=($cur_page-1)*$step;
835 }
836 if($tot_page<=$cur_page+1) {
837 $a["pager_next"]=0;
838 } else {
839 $a["pager_next"]=1;
840 $a["pager_next_page"]=$cur_page+1;
841 $a["pager_next_val"]=($cur_page+1)*$step;
842 }
843 $l=array();
844 $begin=$cur_page-$max;
845 $end=$cur_page+$max;
846 if($begin<0)
847 $end-=$begin;
848 if($end>$tot_page)
849 $begin-=($end-$tot_page);
850 for($i=max(0,$begin); $i<min($end,$tot_page); $i++) {
851 $b=array();
852 $b["pager_page"]=$i;
853 $b["pager_page1"]=$i+1;
854 $b["pager_val"]=$i*$step;
855 $b["pager_sel"]=($cur_page==$i);
856 $b["pager_sep"]=($tot_page!=$i+1);
857 $l[]=$b;
858 }
859 $a["pager_active"]=sizeof($l)>1;
860 $a["pager_list"]=$l;
861 return $a;
862 }
863 function qweb_url_init($base,$arg) {
864 global $qweb_u;
865 $qweb_u=new qweb_url($base,$arg);
866 }
867 function qweb_xml_init($xml) {
868 global $qweb_x;
869 $qweb_x=new qweb_xml($xml);
870 }
871
872 class qweb_control {
873 function control($start,&$argo) {
874 $arg=array();
875 for($i=0;$i<count($argo);$i++) {
876 $s=&$argo[$i];
877 array_push($arg,&$s);
878 }
879 $done=array();
880 $todo=null;
881 while(1) {
882 if(!is_array($todo)) {
883 $tmp="";
884 $todo=array();
885 foreach(explode("_",$start) as $i) {
886 $tmp.="${i}_";
887 if(!array_key_exists(substr($tmp,0,-1),$done))
888 $todo[]=substr($tmp,0,-1);
889 }
890 } elseif (count($todo)) {
891 $i=array_shift($todo);
892 $done[$i]=1;
893 if(method_exists($this,$i)) {
894 $a=call_user_func_array(array(&$this,$i),$arg);
895 if(is_string($a)) {
896 $todo=null;
897 $start=$a;
898 }
899 }
900 } else {
901 break;
902 }
903 }
904 }
905 }
906
907 ?>
Something went wrong with that request. Please try again.