Including the node_modules folder for socket.io code.
[KiwiIRC.git] / class_session.php
1 <?php
2
3 class SESSIONS {
4 static function exists($session_id){
5 global $config;
6
7 if($config['memcache_use']){
8 $ret = GLOB::$mc->get('sid_'.$session_id) ? true : false;
9 return $ret;
10 } else {
11 $session_sok = $config['sok_dir'].$config['sok_prefix'].$session_id;
12 return file_exists($session_sok);
13 }
14 }
15
16
17
18 static function create($client_key){
19 global $config;
20
21 $temp_id = md5(microtime().rand());
22
23 if($config['memcache_use']){
24 $host_key = 'hostcount_'.$client_key;
25 $count = (int)GLOB::$mc->get($host_key);
26 if($count > $config['connections_per_host']) return false;
27
28 // Save the sid into memcached for the ircbot to pick up
29 GLOB::$mc->add('sid_'.$temp_id, $temp_id);
30 }
31
32 $session_cmd = "{$config['php_path']} {$config['session_script_path']} $temp_id $host_key -h$client_key";
33 //die($session_cmd);
34 $session_status = `$session_cmd`;
35
36 if($session_status != 'ok'){
37 debug("Failed creating session socket with: $session_status");
38 GLOB::$mc->delete('sid_'.$temp_id);
39 return false;
40 }
41
42 if($config['memcache_use']){
43 GLOB::$mc->add($host_key, 0);
44 GLOB::$mc->increment($host_key);
45 }
46
47 return $temp_id;
48 }
49
50
51
52 static function open($session_id){
53 global $config;
54
55 if($config['memcache_use']){
56 $session_sok = GLOB::$mc->get('sid_'.$session_id);
57 if(!$session_sok) return false;
58 } else {
59 $session_sok = $session_id;
60 }
61 $session_sok = $config['sok_dir'].$config['sok_prefix'].$session_sok;
62
63 $sok = @stream_socket_client('unix://'.$session_sok, $errno, $errstr);
64
65 if(!$sok) return false;
66 return $sok;
67 }
68
69
70
71 static function close($session){
72 fclose($session);
73 }
74
75
76 static function read($session, $block=true){
77 fwrite($session, json_encode(array('method'=>'read')));
78
79 if(!$block){
80 stream_set_timeout($session, 0, 100000);
81 } else {
82 stream_set_timeout($session, 120, 0);
83 }
84 $data = fgets($session);
85
86 return $data;
87 }
88
89
90
91
92
93 static function clStart($session_id){
94 global $config;
95
96 if($config['memcache_use']){
97 $session_sok = GLOB::$mc->get('sid_'.$session_id);
98 if(!$session_sok) return false;
99 } else {
100 $session_sok = $session_id;
101 }
102 $session_sok = $config['sok_dir'].$config['sok_prefix'].$session_sok;
103
104 $sok = stream_socket_server('unix://'.$session_sok, $errno, $errstr);
105 if(!$sok) return false;
106
107 return $sok;
108
109 }
110
111
112 static function clStop($session){
113 fclose($session);
114 }
115 }