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

Source for file pear-format-html.php

Documentation is available at pear-format-html.php

  1. <?php
  2. /*
  3.    +----------------------------------------------------------------------+
  4.    | PEAR Web site version 1.0                                            |
  5.    +----------------------------------------------------------------------+
  6.    | Copyright (c) 2001-2005 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.    | Authors:                                                             |
  17.    +----------------------------------------------------------------------+
  18.    $Id: pear-format-html.php,v 1.192 2006/10/21 18:08:27 cellog Exp $
  19. */
  20.  
  21. PEAR::setErrorHandling(PEAR_ERROR_CALLBACK'error_handler');
  22.  
  23. function extra_styles($new = null{
  24.     static $extra_styles = array();
  25.     if (!is_null($new)) {
  26.         $extra_styles[$new;
  27.     }
  28.     return $extra_styles;
  29. }
  30.  
  31. // needed for error reporting handling in startup
  32. $GLOBALS['_NODB'= true;
  33. require_once 'pear-cache.php';
  34. // $dbh is initialized in pear-cache
  35. $GLOBALS['_NODB'= false;
  36.  
  37. $self htmlspecialchars($_SERVER['PHP_SELF']);
  38.  
  39. // Handling things related to the manual
  40. $in_manual = false;
  41.  
  42. if (substr($self07== '/manual'{
  43.     if (substr($self710!= "/index.php"{
  44.         $in_manual = true;
  45.     }
  46.  
  47.     require_once 'pear-manual.php';
  48.  
  49.     extra_styles('/css/manual.css');
  50. }
  51.  
  52. $GLOBALS['_style''';
  53. $_style '';
  54.  
  55. /**
  56.  * Prints out the XHTML headers and top of the page.
  57.  *
  58.  * @param string $title  a string to go into the header's <title>
  59.  * @param string $style 
  60.  * @return void 
  61.  */
  62. function response_header($title 'The PHP Extension and Application Repository'$style = false$extraHeaders '')
  63. {
  64.     global $_style$_header_done$SIDEBAR_DATA$self$auth_user;
  65.  
  66.     $extra_styles extra_styles();
  67.  
  68.     if ($_header_done{
  69.         return;
  70.     }
  71.  
  72.     $_header_done    = true;
  73.     $_style          $style;
  74.     $rts             rtrim($SIDEBAR_DATA);
  75.  
  76.     if (substr($rts-1== '-'{
  77.         $SIDEBAR_DATA substr($rts0-1);
  78.     else {
  79.  
  80.         $SIDEBAR_DATA .= draw_navigation('main_menu''Main:');
  81.         $SIDEBAR_DATA .= draw_navigation('docu_menu''Documentation:');
  82.         $SIDEBAR_DATA .= draw_navigation('downloads_menu''Downloads:');
  83.         $SIDEBAR_DATA .= draw_navigation('proposal_menu''Package Proposals:');
  84.         if (!$GLOBALS['_NODB']{
  85.             // if reporting an error, no database access will be performed
  86.             include_once 'pear-auth.php';
  87.             init_auth_user();
  88.         else {
  89.             $auth_user = null;
  90.         }
  91.         if (!empty($auth_user)) {
  92.             if (!empty($auth_user->registered)) {
  93.                 if (auth_check('pear.dev')) {
  94.                     global $developer_menu;
  95.                     $SIDEBAR_DATA .= draw_navigation('developer_menu''Developers:');
  96.                 }
  97.             }
  98.             if ($auth_user->isAdmin()) {
  99.                 global $admin_menu;
  100.                 $SIDEBAR_DATA .= draw_navigation('admin_menu''Administrators:');
  101.             }
  102.         else {
  103.             $SIDEBAR_DATA .= draw_navigation('developer_menu_public''Developers:');
  104.         }
  105.     }
  106.  
  107.     if ($GLOBALS['in_manual'== false{
  108.         /* The manual-related code takes care of sending the right
  109.          * headers.
  110.          */
  111.         header('Content-Type: text/html; charset=ISO-8859-15');
  112.         echo '<?xml version="1.0" encoding="ISO-8859-15" ?>';
  113.     }
  114. ?>
  115.  
  116. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  117. <html>
  118. <head>
  119. <?php
  120. echo $extraHeaders;
  121. ?>
  122.  <title>PEAR :: <?php echo $title?></title>
  123.  <link rel="shortcut icon" href="/gifs/favicon.ico" />
  124.  <link rel="stylesheet" href="/css/style.css" />
  125. <?php
  126.  
  127.     foreach ($extra_styles as $style_file{
  128.         echo ' <link rel="stylesheet" href="' $style_file "\" />\n";
  129.     }
  130. ?>
  131.  <link rel="alternate" type="application/rss+xml" title="RSS feed" href="http://<?php echo htmlspecialchars($_SERVER['HTTP_HOST'])?>/feeds/latest.rss" />
  132. </head>
  133.  
  134. <body <?php
  135.     if (!empty($GLOBALS['ONLOAD'])) {
  136.         print 'onload="' $GLOBALS['ONLOAD']'"';
  137.     }
  138. ?>>
  139. <div>
  140. <a id="TOP"></a>
  141. </div>
  142.  
  143. <!-- START HEADER -->
  144.  
  145. <table id="head-menu" class="head" cellspacing="0" cellpadding="0">
  146.  <tr>
  147.   <td class="head-logo">
  148.    <?php print_link('/'make_image('pearsmall.gif''PEAR'falsefalsefalsefalse'margin: 5px;') )?><br />
  149.   </td>
  150.   <td class="head-menu">
  151.    <?php
  152.  
  153.     if (!$auth_user{
  154.         print_link('/account-request.php''Register'false,
  155.                    'class="menuBlack"');
  156.         echo delim();
  157.         if ($_SERVER['QUERY_STRING'&& $_SERVER['QUERY_STRING'!= 'logout=1'{
  158.             print_link('/login.php?redirect=' urlencode(
  159.                        "{$self}?{$_SERVER['QUERY_STRING']}"),
  160.                        'Login'false'class="menuBlack"');
  161.         else {
  162.             print_link('/login.php?redirect=' $self,
  163.                        'Login'false'class="menuBlack"');
  164.         }
  165.     else {
  166.         print '<small class="menuWhite">';
  167.         print 'Logged in as ' strtoupper($auth_user->handle' (';
  168.         print '<a class="menuWhite" href="/user/' $auth_user->handle . '">Info</a> | ';
  169.         print '<a class="menuWhite" href="/account-edit.php?handle=' $auth_user->handle . '">Profile</a> | ';
  170.         print '<a class="menuWhite" href="/bugs/search.php?handle=' $auth_user->handle . '&amp;cmd=display">Bugs</a>';
  171.         print ")</small><br />\n";
  172.  
  173.         if (empty($_SERVER['QUERY_STRING'])) {
  174.             print_link('?logout=1''Logout'false'class="menuBlack"');
  175.         else {
  176.             print_link('?logout=1&amp;'
  177.                             . htmlspecialchars($_SERVER['QUERY_STRING']),
  178.                        'Logout'false'class="menuBlack"');
  179.         }
  180.     }
  181.  
  182.     echo delim();
  183.     print_link('/manual/''Documentation'false'class="menuBlack"');
  184.     echo delim();
  185.     print_link('/packages.php''Packages'false'class="menuBlack"');
  186.     echo delim();
  187.     print_link('/support/','Support',false'class="menuBlack"');
  188.     echo delim();
  189.     print_link('/bugs/','Bugs',false'class="menuBlack"');
  190.     ?>
  191.  
  192.   </td>
  193.  </tr>
  194.  
  195.  <tr>
  196.   <td class="head-search" colspan="2">
  197.    <form method="get" action="/search.php">
  198.     <p class="head-search"><span class="accesskey">S</span>earch for
  199.     <input class="small" type="text" name="q" value="" size="20" accesskey="s" />
  200.     in the
  201.     <select name="in" class="small">
  202.         <option value="packages">Packages</option>
  203.         <option value="site">This site (using Yahoo!)</option>
  204.         <option value="users">Developers</option>
  205.         <option value="pear-dev">Developer mailing list</option>
  206.         <option value="pear-general">General mailing list</option>
  207.         <option value="pear-cvs">CVS commits mailing list</option>
  208.     </select>
  209.     <input type="image" src="/gifs/small_submit_white.gif" alt="search" style="vertical-align: middle;" />
  210.     </p>
  211.    </form>
  212.   </td>
  213.  </tr>
  214. </table>
  215.  
  216. <!-- END HEADER -->
  217. <!-- START MIDDLE -->
  218.  
  219. <table class="middle" cellspacing="0" cellpadding="0">
  220.  <tr>
  221.  
  222.     <?php
  223.  
  224.     if (isset($SIDEBAR_DATA)) {
  225.         ?>
  226.  
  227. <!-- START LEFT SIDEBAR -->
  228.   <td class="sidebar_left">
  229.    <span id="sidebar">
  230.    <?php echo $SIDEBAR_DATA ?>
  231.    </span>
  232.   </td>
  233. <!-- END LEFT SIDEBAR -->
  234.  
  235.         <?php
  236.     }
  237.  
  238.     ?>
  239.  
  240. <!-- START MAIN CONTENT -->
  241.  
  242.   <td class="content">
  243.  
  244.     <?php
  245. }
  246.  
  247.  
  248. function response_footer($style = false)
  249. {
  250.     global $LAST_UPDATED$MIRRORS$MYSITE$COUNTRIES,$SCRIPT_NAME$RSIDEBAR_DATA;
  251.  
  252.     static $called;
  253.     if ($called{
  254.         return;
  255.     }
  256.     $called = true;
  257.     if (!$style{
  258.         $style $GLOBALS['_style'];
  259.     }
  260.  
  261.     ?>
  262.  
  263.   </td>
  264.  
  265. <!-- END MAIN CONTENT -->
  266.  
  267.     <?php
  268.  
  269.     if (isset($RSIDEBAR_DATA)) {
  270.         ?>
  271.  
  272. <!-- START RIGHT SIDEBAR -->
  273.   <td class="sidebar_right">
  274.    <?php echo $RSIDEBAR_DATA?>
  275.   </td>
  276. <!-- END RIGHT SIDEBAR -->
  277.  
  278.         <?php
  279.     }
  280.  
  281.     ?>
  282.  
  283.  </tr>
  284. </table>
  285.  
  286. <!-- END MIDDLE -->
  287. <!-- START FOOTER -->
  288.  
  289. <table class="foot" cellspacing="0" cellpadding="0">
  290.  <tr>
  291.   <td class="foot-bar" colspan="2">
  292. <?php
  293. print_link('/about/privacy.php''PRIVACY POLICY'false'class="menuBlack"');
  294. echo delim();
  295. print_link('/about/credits.php''CREDITS'false'class="menuBlack"');
  296. ?>
  297.   </td>
  298.  </tr>
  299.  
  300.  <tr>
  301.   <td class="foot-copy">
  302.    <small>
  303.     <?php print_link('/copyright.php',
  304.                      'Copyright &copy; 2001-2006 The PHP Group')?><br />
  305.     All rights reserved.
  306.    </small>
  307.   </td>
  308.   <td class="foot-source">
  309.    <small>
  310.     Last updated: <?php echo $LAST_UPDATED?><br />
  311.     Bandwidth and hardware provided by:
  312.     <?php
  313.      if ($_SERVER['SERVER_NAME'== 'pear.php.net'{
  314.          print_link('http://www.pair.com/''pair Networks');
  315.      elseif ($_SERVER['SERVER_NAME'== PEAR_CHANNELNAME{
  316.          print PEAR_CHANNELNAME;
  317.      else {
  318.          print '<i>This is an unofficial mirror!</i>';
  319.      }
  320.     ?>
  321.  
  322.    </small>
  323.   </td>
  324.  </tr>
  325. </table>
  326.  
  327. <!-- END FOOTER -->
  328.  
  329. </body>
  330. </html>
  331.  
  332.     <?php
  333. }
  334.  
  335.  
  336. function draw_navigation($type$menu_title='')
  337. {
  338.     global $self;
  339.  
  340.     switch ($type{
  341.         case 'main_menu':
  342.             $data = array(
  343.                 '/index.php'           => 'Home',
  344.                 '/news/'               => 'News',
  345.                 '/qa/'                 => 'Quality Assurance',
  346.                 '/group/'              => 'The PEAR Group',
  347.             );
  348.             break;
  349.         case 'docu_menu':
  350.             $data = array(
  351.                 '/manual/en/about-pear.php' => 'About PEAR',
  352.                 '/manual/index.php'    => 'Manual',
  353.                 '/manual/en/faq.php'   => 'FAQ',
  354.                 '/support/'            => 'Support',
  355.             );
  356.             break;
  357.         case 'downloads_menu':
  358.             $data = array(
  359.                 '/packages.php'        => 'List Packages',
  360.                 '/search.php'          => 'Search Packages',
  361.                 '/package-stats.php'   => 'Statistics'
  362.             );
  363.             break;
  364.         case 'developer_menu':
  365.             $data= array(
  366.                 '/accounts.php'        => 'List Accounts',
  367.                 '/release-upload.php'  => 'Upload Release',
  368.                 '/package-new.php'     => 'New Package'
  369.             );
  370.             break;
  371.         case 'developer_menu_public':
  372.             $data= array(
  373.                 '/accounts.php'        => 'List Accounts'
  374.             );
  375.             break;
  376.         case 'proposal_menu':
  377.             $data = array(
  378.                         '/pepr/'       => 'Browse Proposals',
  379.                         '/pepr/pepr-proposal-edit.php'  => 'New Proposal'
  380.                     );
  381.             break;
  382.         case 'admin_menu':
  383.             $data = array(
  384.                 '/admin/'                     => 'Overview'
  385.             );
  386.             break;
  387.         default:
  388.             return '';
  389.     }
  390.  
  391.     $html "\n";
  392.     if (!empty($menu_title)) {
  393.         $html .= "<strong>$menu_title</strong>\n";
  394.     }
  395.  
  396.     $html .= '<ul class="side_pages">' "\n";
  397.     foreach ($data as $url => $tit{
  398.         $html .= ' <li class="side_page">';
  399.         if ($url == $self{
  400.             $html .= '<strong>' $tit '</strong>';
  401.         else {
  402.             $html .= '<a href="' $url '">' $tit '</a>';
  403.         }
  404.         $html .= "</li>\n";
  405.     }
  406.     $html .= "</ul>\n\n";
  407.  
  408.     return $html;
  409. }
  410.  
  411. function menu_link($text$url{
  412.     echo "<p>\n";
  413.     print_link($urlmake_image('pear_item.gif'$text) );
  414.     echo '&nbsp;';
  415.     print_link($url'<strong>' $text '</strong>' );
  416.     echo "</p>\n";
  417. }
  418.  
  419. /**
  420.  * Display errors or warnings as a <ul> inside a <div>
  421.  *
  422.  * Here's what happens depending on $in:
  423.  *   + string: value is printed
  424.  *   + array:  looped through and each value is printed.
  425.  *             If array is empty, nothing is displayed.
  426.  *             If a value contains a PEAR_Error object,
  427.  *   + PEAR_Error: prints the value of getMessage() and getUserInfo()
  428.  *                 if DEVBOX is true, otherwise prints data from getMessage().
  429.  *
  430.  * @param string|array|PEAR_Error$in  see long description
  431.  * @param string $class  name of the HTML class for the <div> tag.
  432.  *                         ("errors", "warnings")
  433.  * @param string $head   string to be put above the message
  434.  *
  435.  * @return bool  true if errors were submitted, false if not
  436.  */
  437. function report_error($in$class 'errors'$head 'ERROR:')
  438. {
  439.     if (PEAR::isError($in)) {
  440.         if (DEVBOX == true{
  441.             $in = array($in->getMessage('... ' $in->getUserInfo());
  442.         else {
  443.             $in = array($in->getMessage());
  444.         }
  445.     elseif (!is_array($in)) {
  446.         $in = array($in);
  447.     elseif (!count($in)) {
  448.         return false;
  449.     }
  450.  
  451.     echo '<div class="' $class '">' $head '<ul>';
  452.     foreach ($in as $msg{
  453.         if (PEAR::isError($msg)) {
  454.             if (DEVBOX == true{
  455.                 $msg $msg->getMessage('... ' $msg->getUserInfo();
  456.             else {
  457.                 $msg $msg->getMessage();
  458.             }
  459.         }
  460.         echo '<li>' htmlspecialchars($msg"</li>\n";
  461.     }
  462.     echo "</ul></div>\n";
  463.     return true;
  464. }
  465.  
  466. /**
  467.  * Forwards warnings to report_error()
  468.  *
  469.  * For use with PEAR_ERROR_CALLBACK to get messages to be formatted
  470.  * as warnings rather than errors.
  471.  *
  472.  * @param string|array|PEAR_Error$in  see report_error() for more info
  473.  *
  474.  * @return bool  true if errors were submitted, false if not
  475.  *
  476.  * @see report_error()
  477.  */
  478. function report_warning($in)
  479. {
  480.     return report_error($in'warnings''WARNING:');
  481. }
  482.  
  483. /**
  484.  * Generates a complete PEAR web page with an error message in it then
  485.  * calls exit
  486.  *
  487.  * For use with PEAR_ERROR_CALLBACK error handling mode to print fatal
  488.  * errors and die.
  489.  *
  490.  * @param string|array|PEAR_Error$in  see report_error() for more info
  491.  * @param string $title  string to be put above the message
  492.  *
  493.  * @return void 
  494.  *
  495.  * @see report_error()
  496.  */
  497. function error_handler($errobj$title 'Error')
  498. {
  499.     response_header($title);
  500.     report_error($errobj);
  501.     response_footer();
  502.     exit;
  503. }
  504.  
  505. /**
  506.  * Displays success messages inside a <div>
  507.  *
  508.  * @param string $in  the message to be displayed
  509.  *
  510.  * @return void 
  511.  */
  512. function report_success($in)
  513. {
  514.     echo '<div class="success">';
  515.     echo htmlspecialchars($in);
  516.     echo "</div>\n";
  517. }
  518.  
  519.  
  520. class BorderBox {
  521.     function BorderBox($title$width '90%'$indent ''$cols = 1,
  522.                        $open = false)
  523.     {
  524.         $this->title  $title;
  525.         $this->width  $width;
  526.         $this->indent $indent;
  527.         $this->cols   $cols;
  528.         $this->open   $open;
  529.         $this->start();
  530.     }
  531.  
  532.     function start()
  533.     {
  534.         $title $this->title;
  535.         if (is_array($title)) {
  536.             $title implode('</th><th>'$title);
  537.         }
  538.         $i $this->indent;
  539.         print "<!-- border box starts -->\n";
  540.         print "$i<table cellpadding=\"0\" cellspacing=\"1\" style=\"width: $this->width; border: 0px;\">\n";
  541.         print "$i <tr>\n";
  542.         print "$i  <td style=\"background-color: #000000;\">\n";
  543.         print "$i   <table cellpadding=\"2\" cellspacing=\"1\" style=\"width: 100%; border: 0px;\">\n";
  544.         print "$i    <tr style=\"background-color: #CCCCCC;\">\n";
  545.         print "$i     <th";
  546.         if ($this->cols > 1{
  547.             print " colspan=\"$this->cols\"";
  548.         }
  549.         print ">$title</th>\n";
  550.         print "$i    </tr>\n";
  551.         if (!$this->open{
  552.             print "$i    <tr style=\"background-color: #FFFFFF;\">\n";
  553.             print "$i     <td>\n";
  554.         }
  555.     }
  556.  
  557.     function end()
  558.     {
  559.         $i $this->indent;
  560.         if (!$this->open{
  561.             print "$i     </td>\n";
  562.             print "$i    </tr>\n";
  563.         }
  564.         print "$i   </table>\n";
  565.         print "$i  </td>\n";
  566.         print "$i </tr>\n";
  567.         print "$i</table>\n";
  568.         print "<!-- border box ends -->\n";
  569.     }
  570.  
  571.     function horizHeadRow($heading /* ... */)
  572.     {
  573.         $i $this->indent;
  574.         print "$i    <tr>\n";
  575.         print "$i     <th style=\"vertical-align: top; background-color: #CCCCCC;\">$heading</th>\n";
  576.         for ($j = 0; $j $this->cols-1; $j++{
  577.             print "$i     <td style=\"vertical-align: top; background-color: #E8E8E8\">";
  578.             $data @func_get_arg($j + 1);
  579.             if (!isset($data)) {
  580.                 print "&nbsp;";
  581.             else {
  582.                 print $data;
  583.             }
  584.             print "</td>\n";
  585.         }
  586.         print "$i    </tr>\n";
  587.  
  588.     }
  589.  
  590.     function headRow()
  591.     {
  592.         $i $this->indent;
  593.         print "$i    <tr>\n";
  594.         for ($j = 0; $j $this->cols$j++{
  595.             print "$i     <th style=\"vertical-align: top; background-color: #FFFFFF;\">";
  596.             $data @func_get_arg($j);
  597.             if (empty($data)) {
  598.                 print '&nbsp;';
  599.             else {
  600.                 print $data;
  601.             }
  602.             print "</th>\n";
  603.         }
  604.         print "$i    </tr>\n";
  605.     }
  606.  
  607.     function plainRow(/* ... */)
  608.     {
  609.         $i $this->indent;
  610.         print "$i    <tr>\n";
  611.         for ($j = 0; $j $this->cols$j++{
  612.             print "$i     <td style=\"vertical-align: top; background-color: #FFFFFF;\">";
  613.             $data @func_get_arg($j);
  614.             if (empty($data)) {
  615.                 print '&nbsp;';
  616.             else {
  617.                 print $data;
  618.             }
  619.             print "</td>\n";
  620.         }
  621.         print "$i    </tr>\n";
  622.     }
  623.  
  624.     function fullRow($text)
  625.     {
  626.         $i $this->indent;
  627.         print "$i    <tr>\n";
  628.         print "$i     <td style=\"background-color: #E8E8E8;\"";
  629.         if ($this->cols > 1{
  630.             print " colspan=\"$this->cols\"";
  631.         }
  632.         print ">$text</td>\n";
  633.         print "$i    </tr>\n";
  634.  
  635.     }
  636. }
  637.  
  638. /**
  639.  * prints "urhere" menu bar
  640.  * Top Level :: XML :: XML_RPC
  641.  * @param bool $link_lastest If the last category should or not be a link
  642.  */
  643. function html_category_urhere($id$link_lastest = false)
  644. {
  645.     $html '<a href="/packages.php">Top Level</a>';
  646.     if ($id !== null{
  647.         global $dbh;
  648.         $res $dbh->query("SELECT c.id, c.name
  649.                             FROM categories c, categories cat
  650.                             WHERE cat.id = $id
  651.                             AND c.cat_left <= cat.cat_left
  652.                             AND c.cat_right >= cat.cat_right");
  653.         $nrows $res->numRows();
  654.         $i = 0;
  655.         while ($res->fetchInto($rowDB_FETCHMODE_ASSOC)) {
  656.             if (!$link_lastest && $i >= $nrows -1{
  657.                 break;
  658.             }
  659.             $html .= "  :: ".
  660.                      "<a href=\"/packages.php?catpid={$row['id']}&amp;catname={$row['name']}\">".
  661.                      "{$row['name']}</a>";
  662.             $i++;
  663.         }
  664.         if (!$link_lastest{
  665.             $html .= '  :: <strong>' $row['name''</strong>';
  666.         }
  667.     }
  668.     print $html;
  669. }
  670.  
  671. /**
  672.  * Returns an absolute URL using Net_URL
  673.  *
  674.  * @param  string $url All/part of a url
  675.  * @return string      Full url
  676.  */
  677. function getURL($url)
  678. {
  679.     include_once 'Net/URL.php';
  680.     $obj = new Net_URL($url);
  681.     return $obj->getURL();
  682. }
  683.  
  684. /**
  685.  * Redirects to the given full or partial URL.
  686.  * will turn the given url into an absolute url
  687.  * using the above getURL() function. This function
  688.  * does not return.
  689.  *
  690.  * @param string $url Full/partial url to redirect to
  691.  * @param  bool  $keepProtocol Whether to keep the current protocol or to force HTTP
  692.  */
  693. function localRedirect($url$keepProtocol = true)
  694. {
  695.     $url getURL($url$keepProtocol);
  696.     if  ($keepProtocol == false{
  697.         $url preg_replace("/^https/""http"$url);
  698.     }
  699.     header('Location: ' $url);
  700.     exit;
  701. }
  702.  
  703. /**
  704.  * Get URL to license text
  705.  *
  706.  * @todo  Add more licenses here
  707.  * @param string Name of the license
  708.  * @return string Link to license URL
  709.  */
  710. function get_license_link($license "")
  711. {
  712.     switch ($license{
  713.  
  714.         case 'PHP License 3.01' :
  715.         case 'PHP 3.01' :
  716.             $link 'http://www.php.net/license/3_01.txt';
  717.             break;
  718.  
  719.         case 'PHP License' :
  720.         case 'PHP 2.02' :
  721.             $link 'http://www.php.net/license/2_02.txt';
  722.             break;
  723.  
  724.         case 'GPL' :
  725.         case 'GNU General Public License' :
  726.             $link 'http://www.gnu.org/licenses/gpl.html';
  727.             break;
  728.  
  729.         case 'LGPL' :
  730.         case 'GNU Lesser General Public License' :
  731.             $link 'http://www.gnu.org/licenses/lgpl.html';
  732.             break;
  733.  
  734.         case 'BSD' :
  735.         case 'BSD License' :
  736.         case 'New BSD License' :
  737.         case 'New BSD' :
  738.             $link 'http://www.opensource.org/licenses/bsd-license.php';
  739.             break;
  740.  
  741.         case 'MIT' :
  742.         case 'MIT License' :
  743.             $link 'http://www.opensource.org/licenses/mit-license.php';
  744.             break;
  745.  
  746.         default :
  747.             $link '';
  748.             break;
  749.     }
  750.  
  751.     return ($link != '' '<a href="' $link '">' $license "</a>\n" $license);
  752. }
  753.  
  754. function display_user_notes($user$width '50%')
  755. {
  756.     global $dbh;
  757.     $bb = new BorderBox("Notes for user $user"$width);
  758.     $notes $dbh->getAssoc("SELECT id,nby,ntime,note FROM notes
  759.                 WHERE uid = ? ORDER BY ntime"truearray($user));
  760.     if (!empty($notes)) {
  761.         print '<table cellpadding="2" cellspacing="0" style="border: 0px;">' "\n";
  762.         foreach ($notes as $nid => $data{
  763.         print " <tr>\n";
  764.         print "  <td>\n";
  765.         print "   <strong>{$data['nby']} {$data['ntime']}:</strong>";
  766.         print "<br />\n";
  767.         print "   ".htmlspecialchars($data['note'])."\n";
  768.         print "  </td>\n";
  769.         print " </tr>\n";
  770.         print " <tr><td>&nbsp;</td></tr>\n";
  771.         }
  772.         print "</table>\n";
  773.     else {
  774.         print 'No notes.';
  775.     }
  776.     $bb->end();
  777.     return sizeof($notes);
  778. }
  779.  
  780. // {{{ user_link()
  781.  
  782. /**
  783.  * Create link to the account information page and to the user's wishlist
  784.  *
  785.  * @param string User's handle
  786.  * @param bool   Should the wishlist link be skipped?
  787.  * @return mixed False on error, otherwise string
  788.  */
  789. function user_link($handle$compact = false)
  790. {
  791.     global $dbh;
  792.  
  793.     $query "SELECT name, wishlist FROM users WHERE handle = '" $handle "'";
  794.     $row $dbh->getRow($queryDB_FETCHMODE_ASSOC);
  795.  
  796.     if (!is_array($row)) {
  797.         return false;
  798.     }
  799.  
  800.     return sprintf("<a href=\"/user/%s\">%s</a>&nbsp;%s\n",
  801.                    $handle,
  802.                    $row['name'],
  803.                    ($row['wishlist'!= "" && $compact == false ? '['.make_link('http://' htmlspecialchars($_SERVER['HTTP_HOST']'/wishlist.php/' $handle'Wishlist').']' '')
  804.                    );
  805. }
  806.  
  807. // }}}
  808.  
  809. /**
  810.  * Returns a hyperlink to something
  811.  */
  812. function make_link($url$linktext ''$target ''$extras '')
  813. {
  814.     return sprintf('<a href="%s"%s%s>%s</a>',
  815.         $url,
  816.         ($target ' target="'.$target.'"' ''),
  817.         ($extras ' '.$extras ''),
  818.         ($linktext != '' $linktext $url)
  819.     );
  820. }
  821.  
  822. /**
  823.  * Echos a hyperlink to something
  824.  */
  825. function print_link($url$linktext ''$target ''$extras '')
  826. {
  827.     echo make_link($url$linktext$target$extras);
  828. }
  829.  
  830. /**
  831.  * Creates a link to the bug system
  832.  */
  833. function make_bug_link($package$type 'list'$linktext '')
  834. {
  835.     switch ($type{
  836.         case 'list':
  837.             if (!$linktext{
  838.                 $linktext 'Package Bugs';
  839.             }
  840.             return make_link('/bugs/search.php?cmd=display&amp;status=Open&amp;package_name[]=' urlencode($package)$linktext);
  841.         case 'report':
  842.             if (!$linktext{
  843.                 $linktext 'Report a new bug';
  844.             }
  845.             return make_link('/bugs/report.php?package=' urlencode($package)$linktext);
  846.     }
  847. }
  848.  
  849. /**
  850.  * Turns the provided email address into a "mailto:" hyperlink.
  851.  *
  852.  * The link and link text are obfuscated by alternating Ord and Hex
  853.  * entities.
  854.  *
  855.  * @param string $email     the email address to make the link for
  856.  * @param string $linktext  a string for the visible part of the link.
  857.  *                            If not provided, the email address is used.
  858.  * @param string $extras    a string of extra attributes for the <a> element
  859.  *
  860.  * @return string  the HTML hyperlink of an email address
  861.  */
  862. function make_mailto_link($email$linktext ''$extras '')
  863. {
  864.     $tmp '';
  865.     for ($i = 0$l strlen($email)$i<$l$i++{
  866.         if ($i % 2{
  867.             $tmp .= '&#' ord($email[$i]';';
  868.         else {
  869.             $tmp .= '&#x' dechex(ord($email[$i])) ';';
  870.         }
  871.     }
  872.  
  873.     return '<a ' $extras ' href="&#x6d;&#97;&#x69;&#108;&#x74;&#111;&#x3a;'
  874.            . $tmp '">' ($linktext != '' $linktext $tmp'</a>';
  875. }
  876.  
  877. /**
  878.  * Prints an IMG tag for a sized spacer GIF
  879.  */
  880. function spacer($width = 1$height = 1$align ''$extras '')
  881. {
  882.     printf('<img src="/gifs/spacer.gif" width="%d" height="%d" style="border: 0px;" alt="" %s%s />',
  883.         $width,
  884.         $height,
  885.         ($align 'align="'.$align.'" ' ''),
  886.         ($extras $extras '')
  887.     );
  888. }
  889.  
  890. /**
  891.  * Tags the output of make_image() and resize it manually
  892.  */
  893. function resize_image($img$width = 1$height = 1)
  894. {
  895.     $str preg_replace('/width=\"([0-9]+?)\"/i'''$img );
  896.     $str preg_replace('/height=\"([0-9]+?)\"/i'''$str );
  897.     $str substr($str,0,-1sprintf(' height="%s" width="%s" />'$height$width );
  898.     return $str;
  899. }
  900.  
  901. /**
  902.  * Returns an IMG tag for a given file (relative to the images dir)
  903.  */
  904. function make_image($file$alt ''$align ''$extras ''$dir '',
  905.                     $border = 0$styles '')
  906. {
  907.     if (!$dir{
  908.         $dir '/gifs';
  909.     }
  910.     if ($size @getimagesize($_SERVER['DOCUMENT_ROOT'].$dir.'/'.$file)) {
  911.         $image sprintf('<img src="%s/%s" style="border: %d;%s%s" %s alt="%s" %s />',
  912.             $dir,
  913.             $file,
  914.             $border,
  915.             ($styles ' '.$styles            ''),
  916.             ($align  ' float: '.$align.';'  ''),
  917.             $size[3],
  918.             ($alt    $alt ''),
  919.             ($extras ' '.$extras            '')
  920.         );
  921.     else {
  922.         $image sprintf('<img src="%s/%s" style="border: %d;%s%s" alt="%s" %s />',
  923.             $dir,
  924.             $file,
  925.             $border,
  926.             ($styles ' '.$styles            ''),
  927.             ($align  ' float: '.$align.';'  ''),
  928.             ($alt    $alt ''),
  929.             ($extras ' '.$extras            '')
  930.         );
  931.     }
  932.     return $image;
  933. }
  934.  
  935. /**
  936.  * Prints an IMG tag for a given file
  937.  */
  938. function print_image($file$alt ''$align ''$extras ''$dir '',
  939.                      $border = 0)
  940. {
  941.     print make_image($file$alt$align$extras$dir);
  942. }
  943.  
  944. /**
  945.  * Print a pipe delimiter
  946.  */
  947. function delim($color = false$delimiter '&nbsp;|&nbsp;')
  948. {
  949.     if (!$color{
  950.         return $delimiter;
  951.     }
  952.     return sprintf('<span style="color: %s;">%s</span>'$color$delimiter);
  953. }
  954.  
  955. /**
  956.  * Prints a horizontal delimiter
  957.  */
  958. function hdelim()
  959. {
  960.     return '<hr />';
  961. }
  962.  
  963. /**
  964.  * Prints a tabbed navigation bar based on the parameter $items
  965.  */
  966. function print_tabbed_navigation($items)
  967. {
  968.     global $self;
  969.  
  970.     $page basename($self);
  971.  
  972.     echo '<div id="nav">' "\n";
  973.     echo "  <ul>\n";
  974.     foreach ($items as $title => $item{
  975.         echo "    <li>";
  976.         echo '<a href="' $item['url']
  977.              . '" title="' $item['title''"';
  978.         if ($page == $item['url']{
  979.             echo ' class="active"';
  980.         }
  981.         echo '>' $title "</a>";
  982.         echo "</li>\n";
  983.     }
  984.     echo "  </ul>\n";
  985.     echo "</div>\n";
  986. }
  987.  
  988. /**
  989.  * Prints a tabbed navigation bar for the various package pages.
  990.  *
  991.  * @param int    $pacid   the id number of the package being viewed
  992.  * @param string $name    the name of the package being viewed
  993.  * @param string $action  the indicator of the current page view
  994.  *
  995.  * @return void 
  996.  */
  997. function print_package_navigation($pacid$name$action)
  998. {
  999.     global $auth_user;
  1000.  
  1001.     $nav_items = array('Main'          => array('url'   => '',
  1002.                                                 'title' => ''),
  1003.                        'Download'      => array('url'   => 'download',
  1004.                                                 'title' => 'Download releases of this package'),
  1005.                        'Documentation' => array('url'   => 'docs',
  1006.                                                 'title' => 'Read the available documentation'),
  1007.                        'Bugs'          => array('url'   => 'bugs',
  1008.                                                 'title' => 'View/Report Bugs'),
  1009.  
  1010.                        'Trackbacks'    => array('url'   => 'trackbacks',
  1011.                                                 'title' => 'Show Related Sites'),
  1012. /*
  1013.                'Wiki'          => array('url'   => 'wiki',
  1014.                                         'title' => 'View wiki area')*/
  1015.                        );
  1016.  
  1017.     if (isset($auth_user&& is_object($auth_user)) {
  1018.         $nav_items['Edit']             = array('url'   => '/package-edit.php?id='.$pacid,
  1019.                                                'title' => 'Edit this package');
  1020.         $nav_items['Edit Maintainers'= array('url'   => '/admin/package-maintainers.php?pid='.$pacid,
  1021.                                                'title' => 'Edit the maintainers of this package');
  1022.         $nav_items['Delete']           = array('url'   => '/package-delete.php?id='.$pacid,
  1023.                                                'title' => 'Delete this package');
  1024.     }
  1025.  
  1026.     print '<div id="nav">';
  1027.  
  1028.     foreach ($nav_items as $title => $item{
  1029.         if (!empty($item['url']&& $item['url']{0== '/'{
  1030.             $url $item['url'];
  1031.         else {
  1032.             $url '/package/' htmlspecialchars($name'/' $item['url'];
  1033.         }
  1034.         print '<a href="' $url '"'
  1035.             . ' title="' $item['title''" '
  1036.             . ($action == $item['url'' class="active" ' '')
  1037.             . '>'
  1038.             . $title
  1039.             . '</a> ';
  1040.     }
  1041.  
  1042.     print '</div>';
  1043. }
  1044.  
  1045. /**
  1046.  * Sets <var>$_SESSION['captcha']</var> and
  1047.  * <var>$_SESSION['captcha_time']</var> then prints the XHTML that
  1048.  * displays a CAPTCHA image and a form input element
  1049.  *
  1050.  * Only generate a new <var>$_SESSION['captcha']</var> if it doesn't exist
  1051.  * yet.  This avoids the problem of the CAPTCHA value being changed but the
  1052.  * old image remaining in the browser's cache.  This is necessary because
  1053.  * caching can not be reliably disabled.
  1054.  *
  1055.  * Use upper case letters to reduce confusion with some of these fonts.
  1056.  * Input is passed through strtoupper() before comparison.
  1057.  *
  1058.  * Don't use "I" or "O" to avoid confusion with numbers.  Don't use digits
  1059.  * because some of the fonts don't handle them.
  1060.  *
  1061.  * @return string  the CAPTCHA image and form intut
  1062.  *
  1063.  * @see validate_captcha(), captcha-image.php
  1064.  */
  1065. function generate_captcha()
  1066. {
  1067.     if (session_id(== ''{
  1068.         session_start();
  1069.     }
  1070.  
  1071.     if (!isset($_SESSION['captcha'])) {
  1072.         $_SESSION['captcha''';
  1073.         $useable 'ABCDEFGHJKLMNPQRSTUVWXYZ';
  1074.         for ($i = 0; $i < 4; $i++{
  1075.             $_SESSION['captcha'.= substr($useablemt_rand(023)1);
  1076.         }
  1077.         $_SESSION['captcha_time'time();
  1078.     }
  1079.     return 'Type <img src="/captcha-image.php?x=' time()
  1080.            . '" alt="If you are unable to'
  1081.            . ' read this image, click the help link to the right of'
  1082.            . ' the input box" align="top" /> into this box...'
  1083.            . ' <input type="text" size="4" maxlength="4" name="captcha" />'
  1084.            . ' (<a href="/captcha-help.php" target="_blank">help</a>)'
  1085.            . ' <br />If this image is hard to read, reload the page.';
  1086.  
  1087. }
  1088.  
  1089. /**
  1090.  * Check if the CAPTCHA value submitted by the user in
  1091.  * <var>$_POST['captcha']</var> matches <var>$_SESSION['captcha']</var>
  1092.  * and that the submission was made within the allowed time frame
  1093.  * of the CAPTCHA being generated
  1094.  *
  1095.  * If the two values aen't the same or the length of time between CAPTCHA
  1096.  * generation and form submission is too long, this function will unset()
  1097.  * <var>$_SESSION['captcha']</var>.  Unsetting it will cause
  1098.  * generate_captcha() to come up with a new CAPTCHA value and image.
  1099.  * This prevents brute force attacks.
  1100.  *
  1101.  * Similarly, if the submission is correct <var>$_SESSION['captcha']</var>
  1102.  * is unset() in order to keep robots from making multiple requests with
  1103.  * a correctly guessed CAPTCHA value.
  1104.  *
  1105.  * @param int $max_age  the length of time in seconds since the CAPTCHA was
  1106.  *                       generated during which a submission should be
  1107.  *                       considered valid.  Default is 300 seconds
  1108.  *                       (aka 5 minutes).
  1109.  *
  1110.  * @return bool  true if input matches captcha, false if not
  1111.  *
  1112.  * @see generate_captcha(), captcha-image.php
  1113.  */
  1114. function validate_captcha($max_age = 300)
  1115. {
  1116.     if (session_id(== ''{
  1117.         session_start();
  1118.     }
  1119.  
  1120.     if (!isset($_POST['captcha']||
  1121.         !isset($_SESSION['captcha']||
  1122.         (time($_SESSION['captcha_time']$max_age ||
  1123.         $_SESSION['captcha'!= strtoupper($_POST['captcha']))
  1124.     {
  1125.         unset($_SESSION['captcha']);
  1126.         unset($_SESSION['captcha_time']);
  1127.         return false;
  1128.     else {
  1129.         unset($_SESSION['captcha']);
  1130.         unset($_SESSION['captcha_time']);
  1131.         return true;
  1132.     }
  1133. }
  1134.  
  1135. /**
  1136.  * Turns bug/feature request numbers into hyperlinks
  1137.  *
  1138.  * If the bug number is prefixed by the word "PHP," the link will
  1139.  * go to bugs.php.net.  Otherwise, the bug is considered a PEAR bug.
  1140.  *
  1141.  * @param string $text  the text to check for bug numbers
  1142.  *
  1143.  * @return string  the string with bug numbers hyperlinked
  1144.  */
  1145. function make_ticket_links($text)
  1146. {
  1147.     $text preg_replace('/(?<=php)\s*(bug|request)\s+#?([0-9]+)/i',
  1148.                          ' <a href="http://bugs.php.net/\\2">\\1 \\2</a>',
  1149.                          $text);
  1150.     $text preg_replace('/(?<![>a-z])(bug|request)\s+#?([0-9]+)/i',
  1151.                          '<a href="/bugs/\\2">\\0</a>'$text);
  1152.     return $text;
  1153. }
  1154. ?>

Documentation generated on Tue, 24 Oct 2006 21:32:28 -0400 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.