PEAR_Frontend_Web
[ class tree: PEAR_Frontend_Web ] [ index: PEAR_Frontend_Web ] [ all elements ]

Source for file Web.php

Documentation is available at Web.php

  1. <?php
  2. /*
  3.   +----------------------------------------------------------------------+
  4.   | PHP Version 4                                                        |
  5.   +----------------------------------------------------------------------+
  6.   | Copyright (c) 1997-2003 The PHP Group                                |
  7.   +----------------------------------------------------------------------+
  8.   | This source file is subject to version 2.02 of the PHP license,      |
  9.   | that is bundled with this package in the file LICENSE, and is        |
  10.   | available at through the world-wide-web at                           |
  11.   | http://www.php.net/license/2_02.txt.                                 |
  12.   | If you did not receive a copy of the PHP license and are unable to   |
  13.   | obtain it through the world-wide-web, please send a note to          |
  14.   | license@php.net so we can mail you a copy immediately.               |
  15.   +----------------------------------------------------------------------+
  16.   | Author: Christian Dickmann <dickmann@php.net>                        |
  17.   +----------------------------------------------------------------------+
  18.  
  19.   $Id: Web.php,v 1.27 2004/06/06 22:37:52 pajoye Exp $
  20. */
  21.  
  22. require_once "PEAR.php";
  23. require_once "PEAR/Remote.php";
  24. require_once "HTML/Template/IT.php";
  25. require_once "Net/UserAgent/Detect.php";
  26.  
  27. /**
  28. * PEAR_Frontend_Web is a HTML based Webfrontend for the PEAR Installer
  29. *
  30. * The Webfrontend provides basic functionality of the Installer, such as
  31. * a package list grouped by categories, a search mask, the possibility
  32. * to install/upgrade/uninstall packages and some minor things.
  33. * PEAR_Frontend_Web makes use of the PEAR::HTML_IT Template engine which
  34. * provides the possibillity to skin the Installer.
  35. *
  36. @author  Christian Dickmann <dickmann@php.net>
  37. @package PEAR_Frontend_Web
  38. @access  private
  39. */
  40.  
  41. class PEAR_Frontend_Web extends PEAR
  42. {
  43.     // {{{ properties
  44.  
  45.     /**
  46.      * What type of user interface this frontend is for.
  47.      * @var string 
  48.      * @access public
  49.      */
  50.     var $type 'Web';
  51.  
  52.     /**
  53.      * Container, where values can be saved temporary
  54.      * @var array 
  55.      * @access private
  56.      */
  57.     var $_data = array();
  58.  
  59.     // }}}
  60.     var $config;
  61.  
  62.     var $_no_delete_pkgs = array(
  63.         'PEAR',
  64.         'PEAR_Frontend_Web',
  65.         'Archive_Tar',
  66.         'Console_Getopt',
  67.         'XML_RPC',
  68.         'Net_UserAgent_Detect',
  69.         'Pager');
  70.  
  71.     // {{{ constructor
  72.  
  73.     function PEAR_Frontend_Web()
  74.     {
  75.         parent::PEAR();
  76.         $GLOBALS['_PEAR_Frontend_Web_log''';
  77.         $this->config $GLOBALS['_PEAR_Frontend_Web_config'];
  78.     }
  79.  
  80.     // }}}
  81.     // {{{ displayLine()
  82.  
  83.     /* XXX some methods from CLI following. should be deleted in the near future */
  84.     function displayLine($text)
  85.     {
  86.         trigger_error("Frontend::display deprecated"E_USER_ERROR);
  87.     }
  88.  
  89.     // }}}
  90.     // {{{ display()
  91.  
  92.     function display($text)
  93.     {
  94.         trigger_error("Frontend::display deprecated"E_USER_ERROR);
  95.     }
  96.  
  97.     // }}}
  98.     // {{{ userConfirm()
  99.  
  100.     function userConfirm($prompt$default 'yes')
  101.     {
  102.         trigger_error("Frontend::display deprecated"E_USER_ERROR);
  103.         return false;
  104.     }
  105.  
  106.     // }}}
  107.     // {{{ displayStart(prompt, [default])
  108.  
  109.     function displayStart()
  110.     {
  111.         $tpl $this->_initTemplate("start.tpl.html"'PEAR Installer');
  112.         $tpl->setVariable('Version''0.5');
  113.         $tpl->show();
  114.         exit;
  115.     }
  116.  
  117.     // }}}
  118.     // {{{ _initTemplate()
  119.  
  120.     /**
  121.      * Initialize a TemplateObject, add a title, and icon and add JS and CSS for DHTML
  122.      *
  123.      * @param string  $file     filename of the template file
  124.      * @param string  $title    (optional) title of the page
  125.      * @param string  $icon     (optional) iconhandle for this page
  126.      * @param boolean $useDHTML (optional) add JS and CSS for DHTML-features
  127.      *
  128.      * @access private
  129.      *
  130.      * @return object Object of HTML/IT - Template - Class
  131.      */
  132.  
  133.     function _initTemplate($file$title ''$icon ''$useDHTML = true)
  134.     {
  135.         $tpl = new HTML_Template_IT(dirname(__FILE__)."/Web");
  136.         $tpl->loadTemplateFile($file);
  137.         $tpl->setVariable("InstallerURL"$_SERVER["PHP_SELF"]);
  138.         $tpl->setVariable("ImgPEAR"$_SERVER["PHP_SELF"].'?img=pear');
  139.         if ($title{
  140.             $tpl->setVariable("Title"$title);
  141.         }
  142.         if ($icon{
  143.             $tpl->setCurrentBlock("TitleBlock");
  144.             $tpl->setVariable("_InstallerURL"$_SERVER["PHP_SELF"]);
  145.             $tpl->setVariable("_Title"$title);
  146.             $tpl->setVariable("_Icon"$icon);
  147.             $tpl->parseCurrentBlock();
  148.         }
  149.  
  150.         $tpl->setCurrentBlock();
  151.  
  152.         if ($useDHTML && Net_UserAgent_Detect::getBrowser('ie5up'== 'ie5up'{
  153.             $dhtml = true;
  154.         else {
  155.             $dhtml = false;
  156.         }
  157.  
  158.         if ($dhtml{
  159.             $tpl->setVariable("JS"'dhtml');
  160.             $css '<link rel="stylesheet" href="'.$_SERVER['PHP_SELF'].'?css=dhtml" />';
  161.             $tpl->setVariable("DHTMLcss"$css);
  162.         else {
  163.             $tpl->setVariable("JS"'nodhtml');
  164.         }
  165.  
  166.         if (!isset($_SESSION['_PEAR_Frontend_Web_js']|| $_SESSION['_PEAR_Frontend_Web_js'== false{
  167.             $tpl->setCurrentBlock('JSEnable');
  168.             $tpl->setVariable('RedirectURL'$_SERVER['REQUEST_URI'](!empty($_GET'&' '?'.'enableJS=1');
  169.             $tpl->parseCurrentBlock();
  170.             $tpl->setCurrentBlock();
  171.         }
  172.  
  173.         return $tpl;
  174.     }
  175.  
  176.     // }}}
  177.     // {{{ displayError()
  178.  
  179.     /**
  180.      * Display an error page
  181.      *
  182.      * @param mixed   $eobj  PEAR_Error object or string containing the error message
  183.      * @param string  $title (optional) title of the page
  184.      * @param string  $img   (optional) iconhandle for this page
  185.      * @param boolean $popup (optional) popuperror or normal?
  186.      *
  187.      * @access public
  188.      *
  189.      * @return null does not return anything, but exit the script
  190.      */
  191.  
  192.     function displayError($eobj$title 'Error'$img 'error'$popup = false)
  193.     {
  194.         $msg '';
  195.         if (isset($GLOBALS['_PEAR_Frontend_Web_log']&& trim($GLOBALS['_PEAR_Frontend_Web_log'])) {
  196.             $msg trim($GLOBALS['_PEAR_Frontend_Web_log'])."\n\n";
  197.         }
  198.  
  199.         if (PEAR::isError($eobj)) {
  200.             $msg .= trim($eobj->getMessage());
  201.         else {
  202.             $msg .= trim($eobj);
  203.         }
  204.  
  205.         $msg nl2br($msg."\n");
  206.  
  207.         $tplfile ($popup "error.popup.tpl.html" "error.tpl.html");
  208.         $tpl $this->_initTemplate($tplfile$title$img);
  209.  
  210.         $tpl->setVariable("Error"$msg);
  211.         $command_map = array(
  212.             "install"   => "list-all",
  213.             "uninstall" => "list-all",
  214.             "upgrade"   => "list-all",
  215.             );
  216.         if (isset($_GET['command'])) {
  217.             if (isset($command_map[$_GET['command']])) {
  218.                 $_GET['command'$command_map[$_GET['command']];
  219.             }
  220.             $tpl->setVariable("param"'?command='.$_GET['command']);
  221.         }
  222.  
  223.         $tpl->show();
  224.         exit;
  225.     }
  226.  
  227.     // }}}
  228.     // {{{ displayFatalError()
  229.  
  230.     /**
  231.      * Alias for PEAR_Frontend_Web::displayError()
  232.      *
  233.      * @see PEAR_Frontend_Web::displayError()
  234.      */
  235.  
  236.     function displayFatalError($eobj$title 'Error'$img 'error')
  237.     {
  238.         $this->displayError($eobj$title$img);
  239.     }
  240.  
  241.     function displayErrorImg($eobj)
  242.     {
  243.         $msg '';
  244.         if (isset($GLOBALS['_PEAR_Frontend_Web_log']&& trim($GLOBALS['_PEAR_Frontend_Web_log']))
  245.             $msg trim($GLOBALS['_PEAR_Frontend_Web_log'])."\n\n";
  246.  
  247.         $_SESSION['_PEAR_Frontend_Web_LastError']     $eobj;
  248.         $_SESSION['_PEAR_Frontend_Web_LastError_log'$msg;
  249.         echo '<script language="javascript">';
  250.         printf('window.open("%s?command=show-last-error", "PEAR", "width=600, height=400");',
  251.             $_SERVER["PHP_SELF"]);
  252.         echo ' </script>';
  253.         printf('<img src="%s?img=install_fail" border="0">'$_SERVER['PHP_SELF']);
  254.         exit;
  255.     }
  256.  
  257.     // }}}
  258.     // {{{ _outputListAll()
  259.  
  260.     /**
  261.      * Output a list of packages, grouped by categories. Uses Paging
  262.      *
  263.      * @param array   $data     array containing all data to display the list
  264.      * @param string  $title    (optional) title of the page
  265.      * @param string  $img      (optional) iconhandle for this page
  266.      * @param boolean $useDHTML (optional) add JS and CSS for DHTML-features
  267.      * @param boolean $paging   (optional) use Paging or not
  268.      *
  269.      * @access private
  270.      *
  271.      * @return boolean true (yep. i am an optimist)
  272.      */
  273.  
  274.     function _outputListAll($data$title 'Install / Upgrade / Remove PEAR Packages'$img 'pkglist'$useDHTML = false$paging = true)
  275.     {
  276.         $tpl $this->_initTemplate("package.list.tpl.html"$title$img$useDHTML);
  277.  
  278.         if (!isset($data['data'])) {
  279.             $data['data'= array();
  280.         }
  281.  
  282.         $pageId = isset($_GET['from'])?$_GET['from']:0;
  283.         $paging_data $this->__getData($pageId5count($data['data'])false);
  284.  
  285.         $data['data'array_slice($data['data']$pageId5);
  286.  
  287.         $links = array();
  288.         $from $paging_data['from'];
  289.         $to $paging_data['next'];
  290.  
  291.         // Generate Linkinformation to redirect to _this_ page after performing an action
  292.         $link_str '<a href="?command=%s&from=%s&mode=%s" class="paging_link">%s</a>';
  293.  
  294.  
  295.  
  296.         $command = isset($_GET['command']$_GET['command']:'list-all';
  297.         $mode = isset($_GET['mode'])?$_GET['mode']:'';
  298.  
  299.  
  300.  
  301.         if ($paging_data['from']>1{
  302.             $links['back'sprintf($link_str$command$paging_data['prev']$mode'&lt;&lt;');
  303.         else {
  304.             $links['back''';
  305.         }
  306.  
  307.         if $paging_data['next']{
  308.             $links['next'sprintf($link_str$command$paging_data['next']$mode'&gt;&gt;');
  309.         else {
  310.             $links['next''';
  311.         }
  312.  
  313.         $links['current''&from=' $paging_data['from']  '&mode=' $mode;
  314.  
  315.         if (isset($_GET['command']&& $_GET['command'== 'search'{
  316.             $links['current'.= '&redirect=search&0='.$_REQUEST[0].'&1='.$_REQUEST[1];
  317.         }
  318.  
  319.         $modes = array(
  320.             'installed'    => 'list installed packages',
  321.             ''             => 'list all packages',
  322.             'notinstalled' => 'list not installed packages',
  323.             'upgrades'     => 'list avail. upgrades',
  324.             );
  325.  
  326.         $i = 1;
  327.         foreach($modes as $mode => $text{
  328.             $tpl->setVariable('mode' $i !empty($mode'&mode='.$mode '');
  329.             $tpl->setVariable('mode' $i.'text'$text);
  330.             $i++;
  331.         }
  332.  
  333.         $tpl->setVariable('Prev'$links['back']);
  334.         $tpl->setVariable('Next'$links['next']);
  335.         $tpl->setVariable('PagerFrom'$from);
  336.         $tpl->setVariable('PagerTo'$to);
  337.         $tpl->setVariable('PagerCount'$paging_data['numrows']);
  338.  
  339.         foreach($data['data'as $category => $packages{
  340.             foreach($packages as $row{
  341.                 list($pkgName$pkgVersionLatest$pkgVersionInstalled$pkgSummary$row;
  342.                 $tpl->setCurrentBlock("Row");
  343.                 $tpl->setVariable("ImgPackage"$_SERVER["PHP_SELF"].'?img=package');
  344.                 $images = array(
  345.                     'install' => '<img src="'.$_SERVER["PHP_SELF"].'?img=install" width="13" height="13" border="0" alt="install">',
  346.                     'uninstall' => '<img src="'.$_SERVER["PHP_SELF"].'?img=uninstall" width="18" height="17"  border="0" alt="uninstall">',
  347.                     'upgrade' => '<img src="'.$_SERVER["PHP_SELF"].'?img=install" width="13" height="13" border="0" alt="upgrade">',
  348.                     'info' => '<img src="'.$_SERVER["PHP_SELF"].'?img=info"  width="17" height="19" border="0" alt="info">',
  349.                     'infoExt' => '<img src="'.$_SERVER["PHP_SELF"].'?img=infoplus"  width="18" height="19" border="0" alt="extended info">',
  350.                     );
  351.                 $urls   = array(
  352.                     'install' => sprintf('%s?command=install&pkg=%s%s',
  353.                         $_SERVER["PHP_SELF"]$pkgName$links['current']),
  354.                     'uninstall' => sprintf('%s?command=uninstall&pkg=%s%s',
  355.                         $_SERVER["PHP_SELF"]$pkgName$links['current']),
  356.                     'upgrade' => sprintf('%s?command=upgrade&pkg=%s%s',
  357.                         $_SERVER["PHP_SELF"]$pkgName$links['current']),
  358.                     'info' => sprintf('%s?command=remote-info&pkg=%s',
  359.                         $_SERVER["PHP_SELF"]$pkgName),
  360.                     'infoExt' => sprintf('%s?package=%s',
  361.                         'http://pear.php.net/package-info.php'$row[0]),
  362.                     );
  363.  
  364.                 $compare version_compare($pkgVersionLatest$pkgVersionInstalled);
  365.                 $id 'id="'.$pkgName.'_href"';
  366.                 if (!$pkgVersionInstalled || $pkgVersionInstalled == "- no -"{
  367.                     $inst sprintf('<a href="%s" onClick="return perform(\'%s\');" %s>%s</a>',
  368.                         $urls['install']$pkgName$id$images['install']);
  369.                     $del '';
  370.                 else if ($compare == 1{
  371.                     $inst sprintf('<a href="%s" onClick="return perform(\'%s\');" %s>%s</a>',
  372.                         $urls['upgrade']$pkgName$id$images['upgrade']);
  373.                     $del sprintf('<a href="%s" onClick="return deletePkg(\'%s\');" %s >%s</a>',
  374.                         $urls['uninstall']$pkgName$id$images['uninstall']);
  375.                 else {
  376.                     $del sprintf('<a href="%s" onClick="return deletePkg(\'%s\');" %s >%s</a>',
  377.                         $urls['uninstall']$pkgName$id$images['uninstall']);
  378.                     $inst '';
  379.                 }
  380.                 $info    sprintf('<a href="%s">%s</a>'$urls['info'],    $images['info']);
  381.                 $infoExt sprintf('<a href="%s">%s</a>'$urls['infoExt']$images['infoExt']);
  382.  
  383.                 if (in_array($pkgName$this->_no_delete_pkgs)) {
  384.                     $del '';
  385.                 }
  386.  
  387.                 $tpl->setVariable("Latest"$pkgVersionLatest);
  388.                 $tpl->setVariable("Installed"$pkgVersionInstalled);
  389.                 $tpl->setVariable("Install"$inst);
  390.                 $tpl->setVariable("Delete"$del);
  391.                 $tpl->setVariable("Info"$info);
  392.                 $tpl->setVariable("InfoExt"$infoExt);
  393.                 $tpl->setVariable("Package"$pkgName);
  394.                 $tpl->setVariable("Summary"nl2br($pkgSummary));
  395.                 $tpl->parseCurrentBlock();
  396.             }
  397.             $tpl->setCurrentBlock("Category");
  398.             $tpl->setVariable("categoryName"$category);
  399.             $tpl->setVariable("ImgCategory"$_SERVER["PHP_SELF"].'?img=category');
  400.             $tpl->parseCurrentBlock();
  401.         }
  402.         $tpl->show();
  403.  
  404.         return true;
  405.     }
  406.  
  407.     function _getPackageDeps($deps)
  408.     {
  409.         if (count($deps== 0{
  410.             return "<i>No dependencies registered.</i>\n";
  411.         else {
  412.             $lastversion '';
  413.             $rel_trans = array(
  414.                 'lt' => 'older than %s',
  415.                 'le' => 'version %s or older',
  416.                 'eq' => 'version %s',
  417.                 'ne' => 'any version but %s',
  418.                 'gt' => 'newer than %s',
  419.                 'ge' => '%s or newer',
  420.                 );
  421.             $dep_type_desc = array(
  422.                 'pkg'    => 'PEAR Package',
  423.                 'ext'    => 'PHP Extension',
  424.                 'php'    => 'PHP Version',
  425.                 'prog'   => 'Program',
  426.                 'ldlib'  => 'Development Library',
  427.                 'rtlib'  => 'Runtime Library',
  428.                 'os'     => 'Operating System',
  429.                 'websrv' => 'Web Server',
  430.                 'sapi'   => 'SAPI Backend',
  431.                 );
  432.             $result "      <dl>\n";
  433.             foreach($deps as $row{
  434.  
  435.                 // Print link if it's a PEAR package
  436.                 if ($row['type'== 'pkg'{
  437.                     $row['name'sprintf('<a class="green" href="%s?command=remote-info&pkg=%s">%s</a>',
  438.                         $_SERVER['PHP_SELF']$row['name']$row['name']);
  439.                 }
  440.  
  441.                 if (isset($rel_trans[$row['relation']])) {
  442.                     $rel sprintf($rel_trans[$row['relation']]$row['version']);
  443.                     $result .= sprintf("%s: %s %s",
  444.                            $dep_type_desc[$row['type']]$row['name']$rel);
  445.                 else {
  446.                     $result .= sprintf("%s: %s"$dep_type_desc[$row['type']]$row['name']);
  447.                 }
  448.                 $lastversion $row['version'];
  449.                 $result .= '<br>';
  450.             }
  451.             if ($lastversion{
  452.             }
  453.             $result .= "      </dl>\n";
  454.         }
  455.         return $result;
  456.     }
  457.  
  458.     /**
  459.      * Output details of one package
  460.      *
  461.      * @param array $data array containing all information about the package
  462.      *
  463.      * @access private
  464.      *
  465.      * @return boolean true (yep. i am an optimist)
  466.      */
  467.  
  468.     function _outputPackageInfo($data)
  469.     {
  470.         $tpl $this->_initTemplate("package.info.tpl.html"'Package Management :: '.$data['name']'pkglist');
  471.  
  472.         $tpl->setVariable("Latest"$data['stable']);
  473.         $tpl->setVariable("Installed"$data['installed']);
  474.         $tpl->setVariable("Package"$data['name']);
  475.         $tpl->setVariable("License"$data['license']);
  476.         $tpl->setVariable("Category"$data['category']);
  477.         $tpl->setVariable("Summary"nl2br($data['summary']));
  478.         $tpl->setVariable("Description"nl2br($data['description']));
  479.         $deps $data['releases'][$data['stable']]['deps'];
  480.         $tpl->setVariable("Dependencies"$this->_getPackageDeps($deps));
  481.  
  482.         $compare version_compare($data['stable']$data['installed']);
  483.  
  484.         $images = array(
  485.             'install' => '<img src="'.$_SERVER["PHP_SELF"].'?img=install" width="13" height="13" border="0" alt="install">',
  486.             'uninstall' => '<img src="'.$_SERVER["PHP_SELF"].'?img=uninstall" width="18" height="17"  border="0" alt="uninstall">',
  487.             'upgrade' => '<img src="'.$_SERVER["PHP_SELF"].'?img=install" width="13" height="13" border="0" alt="upgrade">',
  488.             );
  489.  
  490.         $opt_img = array();
  491.         $opt_text = array();
  492.         if (!$data['installed'|| $data['installed'== "- no -"{
  493.             $opt_img[sprintf(
  494.                 '<a href="%s?command=install&pkg=%s&redirect=info">%s</a>',
  495.                 $_SERVER["PHP_SELF"]$data['name']$images['install']);
  496.             $opt_text[sprintf(
  497.                 '<a href="%s?command=install&pkg=%s&redirect=info" class="green">Install package</a>',
  498.                 $_SERVER["PHP_SELF"]$data['name']);
  499.         else if ($compare == 1{
  500.             $opt_img[sprintf(
  501.                 '<a href="%s?command=upgrade&pkg=%s&redirect=info">%s</a><br>',
  502.                 $_SERVER["PHP_SELF"]$data['name']$images['upgrade']);
  503.             $opt_text[sprintf(
  504.                 '<a href="%s?command=upgrade&pkg=%s&redirect=info" class="green">Upgrade package</a>',
  505.                 $_SERVER["PHP_SELF"]$data['name']);
  506.             if (!in_array($data['name']$this->_no_delete_pkgs)) {
  507.                 $opt_img[sprintf(
  508.                     '<a href="%s?command=uninstall&pkg=%s&redirect=info" %s>%s</a>',
  509.                     $_SERVER["PHP_SELF"]$data['name'],
  510.                     'onClick="return confirm(\'Do you really want to uninstall \\\''.$data['name'].'\\\'?\')"',
  511.                     $images['uninstall']);
  512.                 $opt_text[sprintf(
  513.                     '<a href="%s?command=uninstall&pkg=%s&redirect=info" class="green" %s>Uninstall package</a>',
  514.                     $_SERVER["PHP_SELF"]$data['name'],
  515.                     'onClick="return confirm(\'Do you really want to uninstall \\\''.$data['name'].'\\\'?\')"');
  516.            }
  517.         else {
  518.             if (!in_array($data['name']$this->_no_delete_pkgs)) {
  519.                 $opt_img[sprintf(
  520.                     '<a href="%s?command=uninstall&pkg=%s&redirect=info" %s>%s</a>',
  521.                     $_SERVER["PHP_SELF"]$data['name'],
  522.                     'onClick="return confirm(\'Do you really want to uninstall \\\''.$data['name'].'\\\'?\')"',
  523.                     $images['uninstall']);
  524.                 $opt_text[sprintf(
  525.                     '<a href="%s?command=uninstall&pkg=%s&redirect=info" class="green" %s>Uninstall package</a>',
  526.                     $_SERVER["PHP_SELF"]$data['name'],
  527.                     'onClick="return confirm(\'Do you really want to uninstall \\\''.$data['name'].'\\\'?\')"');
  528.            }
  529.         }
  530.  
  531.         if (isset($opt_img[0]))
  532.         {
  533.             $tpl->setVariable("Opt_Img_1"$opt_img[0]);
  534.             $tpl->setVariable("Opt_Text_1"$opt_text[0]);
  535.         }
  536.         if (isset($opt_img[1]))
  537.         {
  538.             $tpl->setVariable("Opt_Img_2"$opt_img[1]);
  539.             $tpl->setVariable("Opt_Text_2"$opt_text[1]);
  540.         }
  541.  
  542.         $tpl->show();
  543.         return true;
  544.     }
  545.  
  546.     /**
  547.      * Output all kinds of data depending on the command which called this method
  548.      *
  549.      * @param mixed  $data    datastructure containing the information to display
  550.      * @param string $command (optional) command from which this method was called
  551.      *
  552.      * @access public
  553.      *
  554.      * @return mixed highly depends on the command
  555.      */
  556.  
  557.     function outputData($data$command '_default')
  558.     {
  559.         switch ($command{
  560.             case 'config-show':
  561.                 $prompt  = array();
  562.                 $default = array();
  563.                 foreach($data['data'as $group{
  564.                     foreach($group as $row{
  565.                         $prompt[$row[1]]  $row[0];
  566.                         $default[$row[1]] $row[2];
  567.                     }
  568.                 }
  569.                 $title 'Configuration :: '.$GLOBALS['pear_user_config'];
  570.                 $GLOBALS['_PEAR_Frontend_Web_Config'=
  571.                     $this->userDialog($command$promptarray()$default$title'config');
  572.                 return true;
  573.             case 'list-all':
  574.                 return $this->_outputListAll($data);
  575.             case 'search':
  576.                 return $this->_outputListAll($data'Package Search :: Result''pkgsearch'falsefalse);
  577.             case 'remote-info':
  578.                 return $this->_outputPackageInfo($data);
  579.             case 'install':
  580.             case 'upgrade':
  581.             case 'uninstall':
  582.                 return true;
  583.             case 'login':
  584.                 if ($_SERVER["REQUEST_METHOD"!= "POST")
  585.                     $this->_data[$command$data;
  586.                 return true;
  587.             case 'logout':
  588.                 $this->displayError($data'Logout''logout');
  589.                 break;
  590.             case 'package':
  591.                 echo $data;
  592.                 break;
  593.             default:
  594.                 echo $data;
  595.         }
  596.  
  597.         return true;
  598.     }
  599.  
  600.     /**
  601.      * Display a formular and return the given input (yes. needs to requests)
  602.      *
  603.      * @param string $command  command from which this method was called
  604.      * @param array  $prompts  associative array. keys are the inputfieldnames
  605.      *                          and values are the description
  606.      * @param array  $types    (optional) array of inputfieldtypes (text, password,
  607.      *                          etc.) keys have to be the same like in $prompts
  608.      * @param array  $defaults (optional) array of defaultvalues. again keys have
  609.      *                          to be the same like in $prompts
  610.      * @param string $title    (optional) title of the page
  611.      * @param string $icon     (optional) iconhandle for this page
  612.      *
  613.      * @access public
  614.      *
  615.      * @return array input sended by the user
  616.      */
  617.  
  618.     function userDialog($command$prompts$types = array()$defaults = array()$title ''$icon '')
  619.     {
  620.         // If this is an POST Request, we can return the userinput
  621.         if (isset($_GET["command"]&& $_GET["command"]==$command
  622.             && $_SERVER["REQUEST_METHOD"== "POST"{
  623.             $result = array();
  624.             foreach($prompts as $key => $prompt{
  625.                 $result[$key$_POST[$key];
  626.             }
  627.             return $result;
  628.         }
  629.  
  630.         // If this is an Answer GET Request , we can return the userinput
  631.         if (isset($_GET["command"]&& $_GET["command"]==$command
  632.             && isset($_GET["userDialogResult"]&& $_GET["userDialogResult"]=='get'{
  633.             $result = array();
  634.             foreach($prompts as $key => $prompt{
  635.                 $result[$key$_GET[$key];
  636.             }
  637.             return $result;
  638.         }
  639.  
  640.         // Assign title and icon to some commands
  641.         if ($command == 'login'{
  642.             $title 'Login';
  643.             $icon 'login';
  644.         }
  645.  
  646.         $tpl $this->_initTemplate("userDialog.tpl.html"$title$icon);
  647.         $tpl->setVariable("Command"$command);
  648.         if (isset($this->_data[$command])) {
  649.             $tpl->setVariable("Headline"nl2br($this->_data[$command]));
  650.         }
  651.  
  652.         if (is_array($prompts)) {
  653.             $maxlen = 0;
  654.             foreach($prompts as $key => $prompt{
  655.                 if (strlen($prompt$maxlen{
  656.                     $maxlen strlen($prompt);
  657.                 }
  658.             }
  659.  
  660.             foreach($prompts as $key => $prompt{
  661.                 $tpl->setCurrentBlock("InputField");
  662.                 $type    (isset($types[$key])    $types[$key]    'text');
  663.                 $default (isset($defaults[$key]$defaults[$key'');
  664.                 $tpl->setVariable("prompt"$prompt);
  665.                 $tpl->setVariable("name"$key);
  666.                 $tpl->setVariable("default"$default);
  667.                 $tpl->setVariable("type"$type);
  668.                 if ($maxlen > 25{
  669.                     $tpl->setVariable("width"'width="275"');
  670.                 }
  671.                 $tpl->parseCurrentBlock();
  672.             }
  673.         }
  674.         $tpl->show();
  675.         exit;
  676.     }
  677.  
  678.     /**
  679.      * Write message to log
  680.      *
  681.      * @param string $text message which has to written to log
  682.      *
  683.      * @access public
  684.      *
  685.      * @return boolean true
  686.      */
  687.  
  688.     function log($text)
  689.     {
  690.         $GLOBALS['_PEAR_Frontend_Web_log'.= $text."\n";
  691.         return true;
  692.     }
  693.  
  694.     /**
  695.      * Sends the required file along with Headers and exits the script
  696.      *
  697.      * @param string $handle handle of the requested file
  698.      * @param string $group  group of the requested file
  699.      *
  700.      * @access public
  701.      *
  702.      * @return null nothing, because script exits
  703.      */
  704.  
  705.     function outputFrontendFile($handle$group)
  706.     {
  707.         $handles = array(
  708.             "css" => array(
  709.                 "style" => "style.css",
  710.                 "dhtml" => "dhtml.css",
  711.                 ),
  712.             "js" => array(
  713.                 "dhtml" => "dhtml.js",
  714.                 "nodhtml" => "nodhtml.js",
  715.                 ),
  716.             "image" => array(
  717.                 "logout" => array(
  718.                     "type" => "gif",
  719.                     "file" => "logout.gif",
  720.                     ),
  721.                 "login" => array(
  722.                     "type" => "gif",
  723.                     "file" => "login.gif",
  724.                     ),
  725.                 "config" => array(
  726.                     "type" => "gif",
  727.                     "file" => "config.gif",
  728.                     ),
  729.                 "pkglist" => array(
  730.                     "type" => "png",
  731.                     "file" => "pkglist.png",
  732.                     ),
  733.                 "pkgsearch" => array(
  734.                     "type" => "png",
  735.                     "file" => "pkgsearch.png",
  736.                     ),
  737.                 "package" => array(
  738.                     "type" => "jpeg",
  739.                     "file" => "package.jpg",
  740.                     ),
  741.                 "category" => array(
  742.                     "type" => "jpeg",
  743.                     "file" => "category.jpg",
  744.                     ),
  745.                 "install" => array(
  746.                     "type" => "gif",
  747.                     "file" => "install.gif",
  748.                     ),
  749.                 "install_wait" => array(
  750.                     "type" => "gif",
  751.                     "file" => "install_wait.gif",
  752.                     ),
  753.                 "install_ok" => array(
  754.                     "type" => "gif",
  755.                     "file" => "install_ok.gif",
  756.                     ),
  757.                 "install_fail" => array(
  758.                     "type" => "gif",
  759.                     "file" => "install_fail.gif",
  760.                     ),
  761.                 "uninstall" => array(
  762.                     "type" => "gif",
  763.                     "file" => "trash.gif",
  764.                     ),
  765.                 "info" => array(
  766.                     "type" => "gif",
  767.                     "file" => "info.gif",
  768.                     ),
  769.                 "infoplus" => array(
  770.                     "type" => "gif",
  771.                     "file" => "infoplus.gif",
  772.                     ),
  773.                 "pear" => array(
  774.                     "type" => "gif",
  775.                     "file" => "pearsmall.gif",
  776.                     ),
  777.                 "error" => array(
  778.                     "type" => "gif",
  779.                     "file" => "error.gif",
  780.                     ),
  781.                 "manual" => array(
  782.                     "type" => "gif",
  783.                     "file" => "manual.gif",
  784.                     ),
  785.                 "download" => array(
  786.                     "type" => "gif",
  787.                     "file" => "download.gif",
  788.                     ),
  789.                 ),
  790.             );
  791.  
  792.         $file $handles[$group][$handle];
  793.         switch ($group{
  794.             case 'css':
  795.                 header("Content-Type: text/css");
  796.                 readfile(dirname(__FILE__).'/Web/'.$file);
  797.                 exit;
  798.             case 'image':
  799.                 $filename dirname(__FILE__).'/Web/'.$file['file'];
  800.                 header("Content-Type: image/".$file['type']);
  801.                 header("Expires: ".gmdate("D, d M Y H:i:s \G\M\T"time(+ 60*60*24*100));
  802.                 header("Last-Modified: ".gmdate("D, d M Y H:i:s \G\M\T"filemtime($filename)));
  803.                 header("Cache-Control: public");
  804.                 header("Pragma: ");
  805.                 readfile($filename);
  806.                 exit;
  807.             case 'js':
  808.                 header("Content-Type: text/javascript");
  809.                 readfile(dirname(__FILE__).'/Web/'.$file);
  810.                 exit;
  811.         }
  812.     }
  813.  
  814.     /*
  815.      * From DB::Pager. Removing Pager dependency.
  816.      * @private
  817.      */
  818.     function __getData($from$limit$numrows$maxpages = false)
  819.     {
  820.         if (empty($numrows|| ($numrows < 0)) {
  821.             return null;
  822.         }
  823.         $from (empty($from)) ? 0 : $from;
  824.  
  825.         if ($limit <= 0{
  826.             return false;
  827.         }
  828.  
  829.         // Total number of pages
  830.         $pages ceil($numrows/$limit);
  831.         $data['numpages'$pages;
  832.  
  833.         // first & last page
  834.         $data['firstpage'= 1;
  835.         $data['lastpage']  $pages;
  836.  
  837.         // Build pages array
  838.         $data['pages'= array();
  839.         for ($i=1; $i <= $pages$i++{
  840.             $offset $limit ($i-1);
  841.             $data['pages'][$i$offset;
  842.             // $from must point to one page
  843.             if ($from == $offset{
  844.                 // The current page we are
  845.                 $data['current'$i;
  846.             }
  847.         }
  848.         if (!isset($data['current'])) {
  849.             return PEAR::raiseError (null'wrong "from" param'null,
  850.                                      nullnull'DB_Error'true);
  851.         }
  852.               // Limit number of pages (Goole algorithm)
  853.         if ($maxpages{
  854.             $radio floor($maxpages/2);
  855.             $minpage $data['current'$radio;
  856.             if ($minpage < 1{
  857.                 $minpage = 1;
  858.             }
  859.             $maxpage $data['current'$radio - 1;
  860.             if ($maxpage $data['numpages']{
  861.                 $maxpage $data['numpages'];
  862.             }
  863.             foreach (range($minpage$maxpageas $page{
  864.                 $tmp[$page$data['pages'][$page];
  865.             }
  866.             $data['pages'$tmp;
  867.             $data['maxpages'$maxpages;
  868.         else {
  869.             $data['maxpages'= null;
  870.         }
  871.  
  872.         // Prev link
  873.         $prev $from $limit;
  874.         $data['prev'($prev >= 0$prev : null;
  875.  
  876.         // Next link
  877.         $next $from $limit;
  878.         $data['next'($next $numrows$next : null;
  879.  
  880.         // Results remaining in next page & Last row to fetch
  881.         if ($data['current'== $pages{
  882.             $data['remain'= 0;
  883.             $data['to'$numrows;
  884.         else {
  885.             if ($data['current'== ($pages - 1)) {
  886.                 $data['remain'$numrows ($limit*($pages-1));
  887.             else {
  888.                 $data['remain'$limit;
  889.             }
  890.             $data['to'$data['current'$limit;
  891.         }
  892.         $data['numrows'$numrows;
  893.         $data['from']    $from + 1;
  894.         $data['limit']   $limit;
  895.  
  896.         return $data;
  897.     }
  898.  
  899.     // }}}
  900. }
  901.  
  902. ?>

Documentation generated on Mon, 11 Mar 2019 10:17:18 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.