Jump to content

RobertP

Members
  • Posts

    287
  • Joined

  • Last visited

Everything posted by RobertP

  1. not tested Options +FollowSymLinks RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^/?([a-zA-Z0-9-]+)(?:/(.*))?$ index.php?$1
  2. This is not mine, i just found it on my hd and thought it might help. not sure if it even works. i came across it once, and thought i should save it. <?php error_reporting(E_ALL); set_time_limit(0); ob_implicit_flush(); date_default_timezone_set('America/Chicago'); $master = WebSocket('192.168.1.29', 12345); $sockets = array($master); $users = array(); while(true) { $changed = $sockets; socket_select($changed,$write=NULL,$except=NULL,NULL); foreach($changed as $socket) { if($socket == $master) { $client = socket_accept($master); if($client < 0) { server_log('socket_accept() failed', 2); continue; } else { connect($client); } } else { $bytes = socket_recv($socket,$buffer,2048,0); if($bytes==0) { disconnect($socket); } else { $user = getuserbysocket($socket); if(!$user->handshake) { dohandshake($user,$buffer); } else { // Decode the WebSocket data and process accordingly $data = hybi10Decode($buffer); if($data['type'] == 'text') { process($user, $data['payload']); } elseif($data['type'] == 'ping') { } elseif($data['type'] == 'pong') { } elseif($data['type'] == 'close') { } } } } } } //--------------------------------------------------------------- function server_log($msg, $sev = 1) { echo '[' . date('G:i:s') . "] $msg\n"; } function process($user, $msg) { server_log("Message received: ". $msg); } function send($client,$msg) { server_log("> ".$msg, 1); $msg = hybi10Encode($msg, 'close'); socket_write($client,$msg,strlen($msg)); } function WebSocket($address,$port) { $master=socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("socket_create() failed"); socket_set_option($master, SOL_SOCKET, SO_REUSEADDR, 1) or die("socket_option() failed"); socket_bind($master, $address, $port) or die("socket_bind() failed"); socket_listen($master,20) or die("socket_listen() failed"); server_log('Server Started : ' . date('Y-m-d H:i:s'), 1); server_log("Master socket : $master", 1); server_log("Listening on : {$address}:{$port}", 1); return $master; } function connect($socket) { global $sockets,$users; $user = new User(); $user->id = uniqid(); $user->socket = $socket; array_push($users,$user); array_push($sockets,$socket); server_log('New client connected', 1); } function disconnect($socket){ global $sockets,$users; $found=null; $n=count($users); for($i=0;$i<$n;$i++){ if($users[$i]->socket==$socket){ $found=$i; break; } } if(!is_null($found)){ array_splice($users,$found,1); } $index = array_search($socket,$sockets); socket_close($socket); console($socket." DISCONNECTED!"); if($index>=0){ array_splice($sockets,$index,1); } } function dohandshake($user, $buffer) { server_log('Requesting handshake...', 1); // Determine which version of the WebSocket protocol the client is using if(preg_match("/Sec-WebSocket-Version: (.*)\r\n/ ", $buffer, $match)) $version = $match[1]; else return false; if($version == { // Extract header variables if(preg_match("/GET (.*) HTTP/" ,$buffer,$match)){ $r=$match[1]; } if(preg_match("/Host: (.*)\r\n/" ,$buffer,$match)){ $h=$match[1]; } if(preg_match("/Sec-WebSocket-Origin: (.*)\r\n/",$buffer,$match)){ $o=$match[1]; } if(preg_match("/Sec-WebSocket-Key: (.*)\r\n/",$buffer,$match)){ $k = $match[1]; } // Generate our Socket-Accept key based on the IETF specifications $accept_key = $k . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'; $accept_key = sha1($accept_key, true); $accept_key = base64_encode($accept_key); $upgrade = "HTTP/1.1 101 Switching Protocols\r\n" . "Upgrade: websocket\r\n" . "Connection: Upgrade\r\n" . "Sec-WebSocket-Accept: $accept_key\r\n\r\n"; socket_write($user->socket, $upgrade, strlen($upgrade)); $user->handshake = true; return true; } else { server_log("Client is trying to use an unsupported WebSocket protocol ({$version})", 1); return false; } } function getuserbysocket($socket) { global $users; $found = null; foreach($users as $user) { if($user->socket==$socket) { $found = $user; break; } } return $found; } function hybi10Encode($payload, $type = 'text', $masked = true) { $frameHead = array(); $frame = ''; $payloadLength = strlen($payload); switch($type) { case 'text': // first byte indicates FIN, Text-Frame (10000001): $frameHead[0] = 129; break; case 'close': // first byte indicates FIN, Close Frame(10001000): $frameHead[0] = 136; break; case 'ping': // first byte indicates FIN, Ping frame (10001001): $frameHead[0] = 137; break; case 'pong': // first byte indicates FIN, Pong frame (10001010): $frameHead[0] = 138; break; } // set mask and payload length (using 1, 3 or 9 bytes) if($payloadLength > 65535) { $payloadLengthBin = str_split(sprintf('%064b', $payloadLength), ; $frameHead[1] = ($masked === true) ? 255 : 127; for($i = 0; $i < 8; $i++) { $frameHead[$i+2] = bindec($payloadLengthBin[$i]); } // most significant bit MUST be 0 (close connection if frame too big) if($frameHead[2] > 127) { $this->close(1004); return false; } } elseif($payloadLength > 125) { $payloadLengthBin = str_split(sprintf('%016b', $payloadLength), ; $frameHead[1] = ($masked === true) ? 254 : 126; $frameHead[2] = bindec($payloadLengthBin[0]); $frameHead[3] = bindec($payloadLengthBin[1]); } else { $frameHead[1] = ($masked === true) ? $payloadLength + 128 : $payloadLength; } // convert frame-head to string: foreach(array_keys($frameHead) as $i) { $frameHead[$i] = chr($frameHead[$i]); } if($masked === true) { // generate a random mask: $mask = array(); for($i = 0; $i < 4; $i++) { $mask[$i] = chr(rand(0, 255)); } $frameHead = array_merge($frameHead, $mask); } $frame = implode('', $frameHead); // append payload to frame: $framePayload = array(); for($i = 0; $i < $payloadLength; $i++) { $frame .= ($masked === true) ? $payload[$i] ^ $mask[$i % 4] : $payload[$i]; } return $frame; } function hybi10Decode($data) { $payloadLength = ''; $mask = ''; $unmaskedPayload = ''; $decodedData = array(); // estimate frame type: $firstByteBinary = sprintf('%08b', ord($data[0])); $secondByteBinary = sprintf('%08b', ord($data[1])); $opcode = bindec(substr($firstByteBinary, 4, 4)); $isMasked = ($secondByteBinary[0] == '1') ? true : false; $payloadLength = ord($data[1]) & 127; // close connection if unmasked frame is received: if($isMasked === false) { $this->close(1002); } switch($opcode) { // text frame: case 1: $decodedData['type'] = 'text'; break; // connection close frame: case 8: $decodedData['type'] = 'close'; break; // ping frame: case 9: $decodedData['type'] = 'ping'; break; // pong frame: case 10: $decodedData['type'] = 'pong'; break; default: // Close connection on unknown opcode: $this->close(1003); break; } if($payloadLength === 126) { $mask = substr($data, 4, 4); $payloadOffset = 8; } elseif($payloadLength === 127) { $mask = substr($data, 10, 4); $payloadOffset = 14; } else { $mask = substr($data, 2, 4); $payloadOffset = 6; } $dataLength = strlen($data); if($isMasked === true) { for($i = $payloadOffset; $i < $dataLength; $i++) { $j = $i - $payloadOffset; $unmaskedPayload .= $data[$i] ^ $mask[$j % 4]; } $decodedData['payload'] = $unmaskedPayload; } else { $payloadOffset = $payloadOffset - 4; $decodedData['payload'] = substr($data, $payloadOffset); } return $decodedData; } class User{ var $id; var $socket; var $handshake; }
  3. i am having an issue with a prepared statement. if i replace my '?' with a 4, it works as expected. only 1 issue is that the variable $max is dynamic. example code <?php ini_set('display_errors',1); ini_set('date.timezone','America/Toronto'); $connection = new PDO('mysql:host=127.0.0.1;port=3390;dbname=gludoe','user','pass'); $connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC); $max = 4; $stmt = $connection->prepare('select * from plugin_simsmanager_items order by add_date desc limit ?;'); $stmt->execute(array($max)); print_r($stmt->fetchAll()); ?> output Array ( ) expected output Array ( [0] => Array ( [item_id] => 1 [item_name] => Test 1 [image_id] => commons/plugins/simsManager/test1.jpg [add_date] => 0.0000 [item_description] => [file_id] => [age_group] => ) [1] => Array ( [item_id] => 2 [item_name] => Test 2 [image_id] => commons/plugins/simsManager/test2.jpg [add_date] => 0.0000 [item_description] => [file_id] => [age_group] => ) [2] => Array ( [item_id] => 3 [item_name] => Test 3 [image_id] => commons/plugins/simsManager/test3.jpg [add_date] => 0.0000 [item_description] => [file_id] => [age_group] => ) [3] => Array ( [item_id] => 4 [item_name] => Test 4 [image_id] => commons/plugins/simsManager/test4.jpg [add_date] => 0.0000 [item_description] => [file_id] => [age_group] => ) )
  4. This is quite simple, however i would like your opinions... When should i use switch instead of ifelse ? after 2 or more possibilities? 5 or more? Currently i am using this for the most simplistic design: else if($command=='content'&&count($commands)>0){ $content = 'n/a'; if($commands[0]=='dashboard'){ $content = 'Dashboard'; } elseif($commands[0]=='settings'&&isset($commands[1])){ if($commands[1]=='general'){ $content = 'Configuration Settings = General Settings'; } elseif($commands[1]=='server'){ $content = 'Configuration Settings = Server Settings'; } elseif($commands[1]=='member'){ $content = 'Configuration Settings = Member Settings'; } elseif($commands[1]=='page'){ $content = 'Configuration Settings = Page Settings'; } elseif($commands[1]=='plugin'){ $content = 'Configuration Settings = Plugin Settings'; } elseif($commands[1]=='file'){ $content = 'Configuration Settings = File Settings'; } } styler::validExit($content); } now i think i should switch to 'switch' statements; but this is just the beginning. ps: this is the core of my administration panel for my cms.
  5. tbh i usually install each one at a time, not as a service and start via cmd.
  6. solved js $(".tooltip").live("mouseover",function(e){ tip = $(this).attr("title"); if(!tip) return; $(this).attr("title",""); $("body").append("<div id=\"tooltip\">"+tip+"</div>"); }).mousemove(function(e){ $("#tooltip").css("top",e.pageY+10); $("#tooltip").css("left",e.pageX+20); }).mouseout(function(){ $(this).attr("title",$("#tooltip").html()); $("div#tooltip").remove(); });
  7. Not sure if this is a javascript or a css problem. but when hovering on my tr's in my list, there is padding or something added to the table that makes it jump a little. example: http://bend.gludoe.com/ caused by the tooltip i presume. css div#tooltip{ position:absolute; z-index:9999; color:#FFF; font-size:10px; width:180px; background-color:#000; padding:8px 12px; border-radius:8px; } js $(".tooltip").live("mouseover",function(e){ tip = $(this).attr("title"); if(!tip) return; $(this).attr("title",""); $(this).append("<div id=\"tooltip\">"+tip+"</div>"); }).mousemove(function(e){ $("#tooltip").css("top",e.pageY+10); $("#tooltip").css("left",e.pageX+20); }).mouseout(function(){ $(this).attr("title",$("#tooltip").html()); $(this).children("div#tooltip").remove(); }); if you can help, i am completely stumped on this :S
  8. with my application, i have nothing valuable in my initial index.php file ( security reasons of course ); from there i call my engine file ( located out of my web root ) which initiates everything. something like this: /index.php (check php version, make sure /sources & /commons exit, include engine.php) /sources (outside of my web root, also contains engine.php) /commons (hold my js/css/image files, anything that needs to be accessed public) i try to code things once. if i plan to use it more then once, then i modify the existing code ( create a function or class if needed ) so if any error arise later on, i have 1 code block to blame, not 10+.
  9. simply amazing, thank you very much!
  10. http://jsfiddle.net/JcLvr/2/ my issue is displaying the div's after creating them (to display all at once). i wish to have the divs in an array so i can remove them anytime aswell.
  11. on the server side just make sure you have a good error handler, to catch everything and share nothing. the most i use on the client side is chromes dev console. (however i am starting to think of moving to firefox)
  12. thank you so much (did'nt know about the out-keyword)
  13. << thinkin the same thing ..
  14. RobertP

    Hello!

    Welcome, there is only 1 thing i have learnt here, and that is don't expect help if you don't give any. (good luck)
  15. just simply add session_start(); to the top of all your pages..
  16. $_SESSION[username] = $row[username]; the above is take from your code offtopic: taken from your code above. if you want users to logout.. session_destory();
  17. as long as you set the $_SESSION['myValue'] and don't unset it until the users want to logout, then correct; no guests.
  18. $connection->exec('update plugin_advertisements set out = out+1 where id = '.intval($id).';'); $connection->execute('update plugin_advertisements set out = out+1 where id = ?;',array($id)); no luck with either :S if anyone has had this problem and found a viable solution, maybe you can point me in the right direction?
  19. http://php.net/manual/en/function.set-time-limit.php nevermind, i can see you have attempted this already :-\
  20. I'm not sure what you mean. I'm new to scripts like this. add this to the top of all your pages <?php session_start(); ....
  21. not working, still multiple paths. edit: i originally had it set to one slash, and i was still having this issue, but now its working perfectly. thank you very much scootstah!
  22. i have a few small issues with pdo, make sure you have the corresponding extensions enabled via your php.ini not sure how many others use it, but i recently switch to it and it is perfect for my use. allows different database servers with ease, and also has support for prepared statements and transactions. (i am a former java dev; so its like home cooking )
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.