Your browser must have JavaScript enabled in order to view this page.
 >  >
 
Welcome Guest#10138 Login/register    Go to Bottom
Go to Top

Source for file dpclient.php

Documentation is available at dpclient.php

  1. <?php
  2. /**
  3.  * Serves as a gateway between the browser and the PHP DutchPIPE server
  4.  *
  5.  * Passes the HTTP request to the PHP server along with its environment and user
  6.  * variables, and returns the info retrieved from the PHP server back to the
  7.  * user's browser. Used to serve pages, and by the AJAX engine in
  8.  * dpclient-js.php. It talks to the PHP server using a fast file socket
  9.  * connection.
  10.  *
  11.  * DutchPIPE version 0.4; PHP version 5
  12.  *
  13.  * LICENSE: This source file is subject to version 1.0 of the DutchPIPE license.
  14.  * If you did not receive a copy of the DutchPIPE license, you can obtain one at
  15.  * http://dutchpipe.org/license/1_0.txt or by sending a note to
  16.  * license@dutchpipe.org, in which case you will be mailed a copy immediately.
  17.  *
  18.  * @package    DutchPIPE
  19.  * @subpackage lib
  20.  * @author     Lennert Stock <ls@dutchpipe.org>
  21.  * @copyright  2006, 2007 Lennert Stock
  22.  * @license    http://dutchpipe.org/license/1_0.txt  DutchPIPE License
  23.  * @version    Subversion: $Id: dpclient.php 307 2007-09-01 17:16:09Z ls $
  24.  * @link       http://dutchpipe.org/manual/package/DutchPIPE
  25.  * @see        dpserver-ini.php, dpserver.php, dpclient.css
  26.  */
  27.  
  28. /**
  29.  * Gets server settings
  30.  */
  31. if (!defined('DPSERVER_HOST_URL')) {
  32.     require_once(realpath(dirname($_SERVER['SCRIPT_FILENAME']'/..')
  33.         . '/config/dpserver-ini.php');
  34. }
  35.  
  36. /**
  37.  * Universe paths used by templates
  38.  */
  39. require_once(DPSERVER_DPUNIVERSE_CONFIG_PATH 'dpuniverse-ini.php');
  40.  
  41. /**
  42.  * Common functions for templates available to universe objects and dpclient.php
  43.  */
  44. require_once(DPSERVER_LIB_PATH 'dptemplates.php');
  45.  
  46. /**
  47.  * Provides alternatives to multibyte string functions if not supported
  48.  */
  49. require_once(DPSERVER_LIB_PATH 'dpmbstring_'
  50.     . (!DPSERVER_ENABLE_MBSTRING || !function_exists('mb_strlen')
  51.     ? 'disabled' 'enabled''.php');
  52.  
  53.  
  54. /**
  55.  * Contains a message describing the last error, if any
  56.  */
  57. $gLastErrorMsg NULL;
  58.  
  59. /* Deal with AJAX requests and normal page requests */
  60. if ((!isset($_GET['ie6']|| $_GET['ie6'!== 'yes'&& isset($_GET&& (isset($_GET['ajax']|| isset($_GET['method']))) {
  61.     /* Output XML or '1' to "keep-alive" */
  62. else {
  63.     /* Output XHTML */
  64. }
  65.  
  66. /**
  67.  * Talks to the DutchPIPE server, returns response
  68.  *
  69.  * @return     boolean|string output from the DutchPIPE server, FALSE for error
  70.  */
  71. function talk2server()
  72. {
  73.     global $gLastErrorMsg$header_data;
  74.  
  75.     if (DPSERVER_SOCKET_TYPE === AF_UNIX{
  76.         /* Creates a file socket and connects to it */
  77.         if (FALSE === ($socket socket_create(AF_UNIXSOCK_STREAM0))) {
  78.             $gLastErrorMsg '<h1>' DPSERVER_SOCKERR_MSG '</h1>'
  79.                 . sprintf(dp_text(
  80.                 "socket_create(): unable to create socket [%u]: %s\n"),
  81.                 socket_last_error()socket_strerror(socket_last_error()));
  82.             return FALSE;
  83.         }
  84.         if (FALSE === @socket_connect($socketDPSERVER_SOCKET_PATH)) {
  85.             $gLastErrorMsg '<h1>' DPSERVER_SOCKERR_MSG '</h1>'
  86.                 . sprintf(dp_text(
  87.                 "socket_create(): unable to connect [%u]: %s\n"),
  88.                 socket_last_error()socket_strerror(socket_last_error()));
  89.             return FALSE;
  90.         }
  91.     elseif (DPSERVER_SOCKET_TYPE === AF_INET{
  92.         /* Creates a TCP/IP socket and connects to it */
  93.         if (FALSE === ($socket socket_create(AF_INETSOCK_STREAM,
  94.                 SOL_TCP))) {
  95.             $gLastErrorMsg '<h1>' DPSERVER_SOCKERR_MSG '</h1>'
  96.                 . sprintf(dp_text(
  97.                 "socket_create(): unable to create socket [%u]: %s\n"),
  98.                 socket_last_error()socket_strerror(socket_last_error()));
  99.             return FALSE;
  100.         }
  101.         if (FALSE === @socket_connect($socketDPSERVER_SOCKET_ADDRESS,
  102.                 DPSERVER_SOCKET_PORT)) {
  103.             $gLastErrorMsg '<h1>' DPSERVER_SOCKERR_MSG '</h1>'
  104.                 . sprintf(dp_text(
  105.                 "socket_create(): unable to connect [%u]: %s\n"),
  106.                 socket_last_error()socket_strerror(socket_last_error()));
  107.             return FALSE;
  108.         }
  109.     else {
  110.         $gLastErrorMsg '<h1>' DPSERVER_SOCKERR_MSG'</h1>'
  111.             . dp_text('Invalid socket protocol');
  112.         return FALSE;
  113.     }
  114.  
  115.     /* Talk to the DutchPIPE server and write user variables to it */
  116.     if (!isset($_SESSION)) {
  117.         $_SESSION array();
  118.     }
  119.  
  120.     if (isset($_FILES['dpuploadimg'])
  121.             && isset($_FILES['dpuploadimg']['tmp_name'])) {
  122.         @chmod($_FILES['dpuploadimg']['tmp_name']0777);
  123.     }
  124.  
  125.     /* Serialize $_SERVER, $_SESSION, ... variables so they can be sent */
  126.     $in serialize(array($_SERVER$_SESSION$_COOKIE$_GET$_POST,
  127.         $_FILES));
  128.     if (TRUE === DPSERVER_BASE64_CLIENT2SERVER{
  129.         $in base64_encode($in);
  130.     }
  131.     $in "<vars>$in</vars>\r\nquit\r\n";
  132.  
  133.     socket_write($socket$instrlen($in));
  134.     /* Read and process server reply, filter header info given by the server,
  135.       put the remainder in $output */
  136.     $cookie_set $remove_guest_cookie $remove_registered_cookie FALSE;
  137.  
  138.     for ($output ''$buf @socket_read($socketDPSERVER_CLIENT_CHUNK){
  139.         $output .= $buf;
  140.     }
  141.  
  142.     $arroutput explode(chr(0)$output);
  143.     $output '';
  144.     foreach ($arroutput as $buf{
  145.         if (!dp_strlen($buf|| isset($newlocation)) {
  146.             continue;
  147.         }
  148.         $bufdec TRUE === DPSERVER_BASE64_SERVER2CLIENT base64_decode($buf)
  149.             : $buf;
  150.         if (dp_strlen($bufdec11
  151.                 && dp_substr($bufdec011== "Set-Login: "{
  152.             $cookie_data dp_substr($bufdec11);
  153.             setcookie(DPSERVER_COOKIE_NAME$cookie_dataFALSE'/');
  154.             $cookie_set TRUE;
  155.         }
  156.  
  157.         elseif (dp_strlen($bufdec17 && FALSE !== ($pos1 dp_strpos($bufdec,
  158.                 "<header><![CDATA[")) && FALSE !== ($pos2 dp_strpos($bufdec,
  159.                 "]]></header>")) && $pos2 $pos1 12{
  160.             $header_data dp_substr($bufdec0$pos2);
  161.             $header_data dp_substr($header_data$pos1 17);
  162.             header($header_data);
  163.         }
  164.  
  165.         elseif (dp_strlen($bufdec17 && FALSE !== ($pos1 dp_strpos($bufdec,
  166.                 "<cookie><![CDATA[")) && FALSE !== ($pos2 dp_strpos($bufdec,
  167.                 "]]></cookie>")) && $pos2 $pos1 12{
  168.             $cookie_data dp_substr($bufdec0$pos2);
  169.             $cookie_data dp_substr($cookie_data$pos1 17);
  170.             if ($cookie_data == 'removeguest'{
  171.                 $remove_guest_cookie TRUE;
  172.             }
  173.             elseif ($cookie_data == 'removeregistered'{
  174.                 $remove_registered_cookie TRUE;
  175.             }
  176.             else {
  177.                 setcookie(DPSERVER_COOKIE_NAME$cookie_datatime()
  178.                     + 630720000'/');
  179.                 $cookie_set TRUE;
  180.             }
  181.         }
  182.  
  183.         elseif (dp_strlen($bufdec19 && FALSE !== ($pos1 dp_strpos($bufdec,
  184.                 "<location><![CDATA[")) && FALSE !== ($pos2 dp_strpos($bufdec,
  185.                 "]]></location>")) && $pos2 $pos1 14{
  186.             $bufdec dp_substr($bufdec0$pos2);
  187.             $bufdec dp_substr($bufdec$pos1 19);
  188.             $newlocation $bufdec;
  189.             $newlocation $newlocation == '' DPSERVER_CLIENT_DIR
  190.                 : DPSERVER_CLIENT_URL "?location=$newlocation";
  191.             $output "<location><![CDATA[$newlocation]]></location>";
  192.             continue;
  193.         }
  194.         else {
  195.             $output .= TRUE === DPSERVER_BASE64_SERVER2CLIENT
  196.                 ? base64_decode($buf$buf;
  197.         }
  198.     }
  199.  
  200.     /* Close cocket, return server reply */
  201.     socket_close($socket);
  202.  
  203.     if (FALSE === $cookie_set{
  204.         if (FALSE !== $remove_guest_cookie{
  205.             setcookie(DPSERVER_COOKIE_NAMEFALSEFALSE'/');
  206.         }
  207.         if (FALSE !== $remove_registered_cookie{
  208.             setcookie(DPSERVER_COOKIE_NAMEFALSEtime(3600'/');
  209.         }
  210.     }
  211.  
  212.     if (isset($newlocation)) {
  213.         if (!isset($_GET|| !isset($_GET['ajax'])) {
  214.             header("Location: $newlocation");
  215.             exit;
  216.         }
  217.     }
  218.  
  219.     return $output;
  220. }
  221.  
  222. /**
  223.  * Handles AJAX requests from dpclient-js.php
  224.  *
  225.  * @param   string  $output     The output from talk2server()
  226.  */
  227. function handle_ajax_request($output)
  228. {
  229.     if (FALSE === $output || $output == '1'{
  230.         if (is_integer(DPSERVER_APACHE_GZIP_MIN)
  231.                 && DPSERVER_APACHE_GZIP_MIN 1{
  232.             apache_setenv('no-gzip''1');
  233.         }
  234.         echo '1';
  235.     }
  236.     elseif (FALSE === $output || $output == '2'{
  237.         if (is_integer(DPSERVER_APACHE_GZIP_MIN)
  238.                 && DPSERVER_APACHE_GZIP_MIN 1{
  239.             apache_setenv('no-gzip''1');
  240.         }
  241.         echo '2';
  242.     }
  243.     else {
  244.         if (isset($_GET&& isset($GET['standalone']&& isset($GET['_seq'])
  245.                 && $GET['_seq'== '0'{
  246.             $xml2 simplexml_load_string($str =
  247.                 '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'
  248.                 . '<dutchpipe>' $output '</dutchpipe>');
  249.  
  250.             if (FALSE !== $xml2{
  251.                 handle_cookies($xml2);
  252.             }
  253.         }
  254.         if (is_integer(DPSERVER_APACHE_GZIP_MIN)
  255.                 && DPSERVER_APACHE_GZIP_MIN dp_strlen($output)) {
  256.             apache_setenv('no-gzip''1');
  257.         }
  258.  
  259.         header('Content-Type: text/xml');
  260.         echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>
  261. <dutchpipe>$output</dutchpipe>\n";
  262.         exit;
  263.     }
  264. }
  265.  
  266. /**
  267.  * Sets cookies transmitted with the xml we got from the server
  268.  *
  269.  * @param   string  &$xml       XML of the output from talk2server()
  270.  */
  271. function handle_cookies(&$xml)
  272. {
  273.     foreach ($xml->cookie as $id => $cookie{
  274.         foreach($xml->cookie->attributes(as $attname => $attval{
  275.             switch ($attname{
  276.             case 'name':
  277.                 $name $attval;
  278.                 break;
  279.             case 'expire':
  280.                 $expire dp_strlen($attval$attval FALSE;
  281.                 break;
  282.             case 'path':
  283.                 $path $attval;
  284.                 break;
  285.             case 'domain':
  286.                 $domain $attval;
  287.                 break;
  288.             case 'secure':
  289.                 $secure $attval;
  290.                 break;
  291.             default:
  292.                 break;
  293.             }
  294.         }
  295.  
  296.         if (!isset($name)) {
  297.             break;
  298.         }
  299.  
  300.         if (!isset($secure)) {
  301.             if (!isset($domain)) {
  302.                 if (!isset($path)) {
  303.                     if (!isset($expire)) {
  304.                         setcookie($name$cookieFALSE'/');
  305.                     else {
  306.                         setcookie($name$cookie$expire'/');
  307.                     }
  308.                 else {
  309.                     setcookie($name$cookie,
  310.                         (!isset($expireFALSE $expire)$path);
  311.                 }
  312.             else {
  313.                 setcookie($name$cookie(!isset($expireFALSE $expire),
  314.                     (!isset($path'' $path)$domain);
  315.             }
  316.         else {
  317.             setcookie($name$cookie(!isset($expireFALSE $expire),
  318.                 (!isset($path'' $path)(!isset($domain'' $domain),
  319.                 $secure);
  320.         }
  321.     }
  322. }
  323.  
  324. /**
  325.  * Handles a normal page request
  326.  *
  327.  * @param   string  $output     The output from talk2server()
  328.  */
  329. function handle_normal_request($output)
  330. {
  331.     global $gLastErrorMsg;
  332.  
  333.     if (isset($_GET['ie6']&& 'yes' === $_GET['ie6']{
  334.         return;
  335.     }
  336.     if (FALSE === $output{
  337.         $body $gLastErrorMsg;
  338.  
  339.         /* Otherwise serve the page with the retrieved content in it */
  340.         echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  341.         require_once(DPSERVER_TEMPLATE_PATH DPSERVER_TEMPLATE_DOWN_FILE);
  342.         exit;
  343.     }
  344.     $xml simplexml_load_string($str '<?xml version="1.0" encoding="UTF-8" '
  345.         . 'standalone="yes" ?><dutchpipe>' $output '</dutchpipe>');
  346.  
  347.     if (FALSE === $xml{
  348.         echo '<pre>' htmlentities($str'</pre>';
  349.         exit;
  350.     }
  351.  
  352.     $messages $windows array();
  353.     $body $dpelements $scripts '';
  354.     $closetext dp_text('close');
  355.     $inputpersistent $template_file FALSE;
  356.  
  357.     handle_cookies($xml);
  358.  
  359.     foreach ($xml->event as $e{
  360.         foreach ($e as $type => $data{
  361.             switch ($type{
  362.             case 'message':
  363.                 $messages[$data;
  364.                 break;
  365.             case 'addDpElement':
  366.                 $dpelements .= $data->asXML();
  367.                 break;
  368.             case 'removeDpElement':
  369.                 $dpelements .= $data->asXML();
  370.                 break;
  371.             case 'changeDpElement':
  372.                 $dpelements .= $data->asXML();
  373.                 break;
  374.             case 'moveDpElement':
  375.                 $dpelements .= $data->asXML();
  376.                 break;
  377.             case 'stylesheet':
  378.                 $dpelements .= $data->asXML();
  379.                 break;
  380.             case 'script':
  381.                 $tmp $data->asXML();
  382.                 $pos1 dp_strpos($tmp'<![CDATA[');
  383.                 $pos2 dp_strpos($tmp']]>');
  384.                 if (FALSE !== $pos1 && FALSE !== $pos2 && $pos1 $pos2{
  385.                     $tmp dp_substr($tmp,  0$pos1)
  386.                         . dp_substr($tmp$pos1 9$pos2 $pos1 9)
  387.                         . dp_substr($tmp$pos2 3);
  388.                 }
  389.                 $scripts .= $tmp;
  390.                 break;
  391.             case 'div':
  392.                 foreach ($data->attributes(as $a => $b{
  393.                     if ('template' === $a{
  394.                         $template_file $b;
  395.  
  396.                     }
  397.                 }
  398.                 $body .= str_replace(' template="' (!$template_file ''
  399.                     : $template_file'"'''$data);
  400.                 break;
  401.             case 'window':
  402.                 $windows['<div class="dpwindow_default" id="dpwindow">'
  403.                     . $data '<p align="right"><a href="javascript:'
  404.                     . 'close_dpwindow()">' $closetext '</a></p></div>';
  405.                 break;
  406.             case 'inputpersistent':
  407.                 foreach ($data->attributes(as $a => $b{
  408.                     if ('persistent' === $a{
  409.                         $inputpersistent $b;
  410.                     }
  411.                 }
  412.                 break;
  413.             default:
  414.                 break;
  415.             }
  416.         }
  417.     }
  418.  
  419.     if (=== dp_strlen($body)) {
  420.         $body '<h1>' dp_text('Error fetching page. Invalid page XML.')
  421.             . '</h1>';
  422.     }
  423.  
  424.     if (dp_strlen($dpelements)) {
  425.         $dpelements "        <script type=\"text/javascript\">
  426.             function dp_load_xml(text)
  427.             {
  428.                 var xmlDoc = '';
  429.                 if (window.ActiveXObject) {
  430.                     xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
  431.                     xmlDoc.async='false';
  432.                     xmlDoc.loadXML(text);
  433.                 }
  434.                 else if (document.implementation &&
  435.                         document.implementation.createDocument) {
  436.                         var parser = new DOMParser();
  437.                     var xmlDoc=parser.parseFromString(text,'text/xml');
  438.                 }
  439.                 else {
  440.                     /* alert('Your browser cannot handle this script'); */
  441.                     return false;
  442.                 }
  443.                 return xmlDoc;
  444.             }
  445.  
  446.             function dp_load_elements()
  447.             {
  448.                 var content = '<?xml version=\"1.0\"?><dutchpipe>"
  449.             . "<event count=\"-1\" time=\"-1\">" addslashes($dpelements)
  450.             . "</event></dutchpipe>'; handle_response(dp_load_xml(content));
  451.             }
  452.             </script>\n";
  453.     }
  454.  
  455.     $messages_style !sizeof($messages''
  456.         : ' style="display: block; padding-top: 12px"';
  457.  
  458.     $windows implode("\n"$windows);
  459.     $messages implode("\n"$messages);
  460.  
  461.     $subtemplates dp_get_subtemplates(array('input''input_say'),
  462.         $template_file);
  463.     ob_start();
  464.     include($subtemplates['input']);
  465.     if ('always' == $inputpersistent{
  466.         $dpinput_say ob_get_contents();
  467.     else {
  468.         $dpinput ob_get_contents();
  469.     }
  470.     ob_end_clean();
  471.  
  472.     ob_start();
  473.     include($subtemplates['input_say']);
  474.     if ('always' == $inputpersistent{
  475.         $dpinput ob_get_contents();
  476.     else {
  477.         $dpinput_say ob_get_contents();
  478.     }
  479.     ob_end_clean();
  480.  
  481.     /* Otherwise serve the page with the retrieved content in it */
  482.     echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  483.     require_once(!$template_file
  484.         ? DPSERVER_TEMPLATE_PATH DPSERVER_TEMPLATE_FILE $template_file);
  485. }
  486. ?>

Documentation generated on Mon, 03 Sep 2007 22:18:55 +0200 by phpDocumentor 1.3.0RC6

Click me!
Guest#10138
 
 
 
  Go to Top
 
 
Input Field OptionsClose Input Field Go to Top
 
Legal Notices | Copyright © 2006, 2007 Lennert Stock. All rights reserved. Last update: Mon Sep 03 2007, 21:50 CET.