]> 4ch.mooo.com Git - test.git/blob - lib/lib_pio.php
modified: config.php
[test.git] / lib / lib_pio.php
1 <?php\r
2 /*\r
3 PIO - Pixmicat! data source I/O\r
4 */\r
5 \r
6 // 協助設定 status 旗標的類別\r
7 class FlagHelper{\r
8         var $_status;\r
9 \r
10         function FlagHelper($status=''){\r
11                 $this->_write($status);\r
12         }\r
13 \r
14         function _write($status=''){\r
15                 $this->_status = $status;\r
16         }\r
17 \r
18         function toString(){\r
19                 return $this->_status;\r
20         }\r
21 \r
22         function get($flag){\r
23                 $result = preg_match('/_('.$flag.'(\:(.*))*)_/U', $this->toString(), $match);\r
24                 return $result ? $match[1] : false;\r
25         }\r
26 \r
27         function exists($flag){\r
28                 return $this->get($flag) !== false;\r
29         }\r
30 \r
31         function value($flag){\r
32                 $wholeflag = $this->get($flag);\r
33                 if($scount = substr_count($wholeflag, ':')){\r
34                         $wholeflag = preg_replace('/^'.$flag.'\:/', '', $wholeflag);\r
35                         return ($scount > 1 ? explode(':', $wholeflag) : $wholeflag);\r
36                 }else return $wholeflag !== false;\r
37         }\r
38 \r
39         function add($flag, $value=null){\r
40                 return $this->update($flag, $value);\r
41         }\r
42 \r
43         function update($flag, $value=null){\r
44                 if($value===null){\r
45                         $ifexist = $this->get($flag);\r
46                         if($ifexist !== $flag) $this->_write($this->toString()."_${flag}_");\r
47                 }else{\r
48                         if(is_array($value)) $value = $this->join($value); // Array Flatten\r
49                         $ifexist = $this->get($flag);\r
50                         if($ifexist !== $flag.':'.$value){\r
51                                 if($ifexist) $this->_write($this->replace($ifexist, "$flag:$value")); // 已立flag,不同值\r
52                                 else $this->_write($this->toString()."_$flag:${value}_"); // 無flag\r
53                         }\r
54                 }\r
55                 return $this;\r
56         }\r
57 \r
58         function replace($from, $to){\r
59                 return str_replace("_${from}_", "_${to}_", $this->toString());\r
60         }\r
61 \r
62         function remove($flag){\r
63                 $wholeflag = $this->get($flag);\r
64                 $this->_write(str_replace("_${wholeflag}_", '', $this->toString()));\r
65                 return $this;\r
66         }\r
67 \r
68         function toggle($flag){\r
69                 return ($this->get($flag) ? $this->remove($flag) : $this->add($flag));\r
70         }\r
71 \r
72         function offsetValue($flag, $d=0){\r
73                 $v = intval($this->value($flag));\r
74                 return $this->update($flag, $v + $d);\r
75         }\r
76 \r
77         function plus($flag){ return $this->offsetValue($flag, 1); }\r
78         function minus($flag){ return $this->offsetValue($flag, -1); }\r
79 \r
80         function join(){\r
81                 $arg = func_get_args();\r
82                 $newval = array();\r
83                 foreach($arg as $a){\r
84                         if(is_array($a)) array_push($newval, implode(':', $a));\r
85                         else array_push($newval, $a);\r
86                 }\r
87                 return implode(':', $newval);\r
88         }\r
89 }\r
90 \r
91 // 文章自動刪除機制\r
92 include(PHP_DIRECTORY.'lib/lib_pio.cond.php');\r
93 class PIOSensor{\r
94         /*public static */function check($type, $condobj){\r
95                 foreach($condobj as $i => $j){\r
96                         // 有其中一個需要處理\r
97                         if(call_user_func_array(array($i, 'check'), array($type, $j))===true) return true;\r
98                 }\r
99                 return false;\r
100         }\r
101 \r
102         /*public static */function listee($type, $condobj){\r
103                 $tmparray = array(); // 項目陣列\r
104                 foreach($condobj as $i => $j){\r
105                         // 結果併進 $tmparray\r
106                         $tmparray = array_merge($tmparray, call_user_func_array(array($i, 'listee'), array($type, $j)));\r
107                 }\r
108                 sort($tmparray); // 由舊排到新 (小到大)\r
109                 return array_unique($tmparray);\r
110         }\r
111 \r
112         /*public static */function info($condobj){\r
113                 $sensorinfo='';\r
114                 foreach($condobj as $i => $j){\r
115                         $sensorinfo .= call_user_func_array(array($i, 'info'), array($j))."\n";\r
116                 }\r
117                 return $sensorinfo;\r
118         }\r
119 }\r
120 \r
121 // 分析連線字串\r
122 if(preg_match('/^(.*):\/\//i', CONNECTION_STRING, $backend)) define('PIXMICAT_BACKEND', $backend[1]);\r
123 \r
124 // 引入必要函式庫\r
125 $pio_file = PHP_DIRECTORY.'lib/pio/pio.'.PIXMICAT_BACKEND.'.php';\r
126 $PIOEnv = array( // PIO 環境常數\r
127         'BOARD' => '.',\r
128         'LUTCACHE' => './'.LUT_CACHE,\r
129         'NONAME' => DEFAULT_NONAME,\r
130         'NOTITLE' => DEFAULT_NOTITLE,\r
131         'NOCOMMENT' => DEFAULT_NOCOMMENT,\r
132         'PERIOD.POST' => RENZOKU,\r
133         'PERIOD.IMAGEPOST' => RENZOKU2\r
134 );\r
135 if(is_file($pio_file)) include_once($pio_file);\r
136 \r
137 // PIO Kernel Switcher\r
138 $pioSwitch = 'PIO'.PIXMICAT_BACKEND;\r
139 $PIO = new $pioSwitch(CONNECTION_STRING, $PIOEnv);\r
140 ?>