home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / modules / news / index.php next >
PHP Script  |  2004-03-08  |  14KB  |  315 lines

  1. <?php
  2. /*
  3.  * NNTP-Interface in weblog-style
  4.  *
  5.  * Author: Markus Schabel <markus.schabel@tgm.ac.at>
  6.  *
  7.  * TODO add support for multiple servers
  8.  * TODO remove hard-coded servers
  9.  * TODO add multiple language code
  10.  */
  11.  
  12. // Require main configuration file
  13. require( "../../Group-Office.php" );
  14.  
  15. // Include NNTP class, all communication to the NNTP server is done over
  16. // this class. It also includes a class for a nntpMessage, so that each
  17. // message has the same format.
  18. require_once( "nntp.class.inc" );
  19.  
  20. // Get language-file for this module...
  21. // TODO create language-file for this module
  22. // require( $GO_LANGUAGE->get_language_file( 'news' ) );
  23.  
  24. // Check if a user is logged in. If not try to login via cookies. If that
  25. // also fails then show the login-screen.
  26. $GO_SECURITY->authenticate();
  27.  
  28. // Check if the user is allowed to access this module.
  29. $GO_MODULES->authenticate( 'news' );
  30.  
  31. // This is the title of this page. Needed in header.inc for displaying the
  32. // correct title in the titlebar of the browser.
  33. $page_title = "news";
  34.  
  35. // Require theme-header, most times this will be the navigation with some
  36. // design issues.
  37. require( $GO_THEME->theme_path."header.inc" );
  38.  
  39. // If this module is loaded without any parameters we display all groups
  40. // we have access to. So we set the default task to show_groups if it has
  41. // not been set before.
  42. $task = isset( $_REQUEST['task'] ) ? $_REQUEST['task'] : 'show_groups';
  43.  
  44. // This file implements the navigation of this module. So if you need more
  45. // functions in this module you probably have to add them there.
  46. require( "tasks.inc" );
  47.  
  48. // All output should be aligned in a table to have correct distances
  49. // between the window-borders and our output.
  50. echo "<table border='0' cellpadding='10'><tr><td>";
  51.  
  52. // Initialize the NNTP class. As parameters give the server we want to connect
  53. // to, the current user and the password he used to login.
  54. // TODO in the future this will be probably be configurable and stored in
  55. // the database... This will probably break authentication on some public
  56. // NNTP servers!
  57. $nntp = new nntp(
  58.                 "dot.tgm.ac.at",
  59.                 $_SESSION['GO_SESSION']["username"],
  60.                 $_SESSION['GO_SESSION']["user_auth_id"]
  61.                 );
  62.  
  63. // Open the connection to the newsserver. We also could open a connection
  64. // each time we query the server for data, but that would be a performance
  65. // impact, so we decided to open the connection once and keep it open.
  66. $nntp->open();
  67.  
  68. // Decide what output we should generate...
  69. switch( $task ) {
  70.   // Display all groups the user is allowed to see or the specific group
  71.   // the user likes to see if he is allowed to.
  72.   case 'show_groups':
  73.     // If the user requested a newsgroup we will only display this one.
  74.     // Since the implementation in showMessages needs an array as parameter
  75.     // we convert the newsgroup to an array.
  76.     if ( isset( $_REQUEST['newsgroup'] ) ) {
  77.       // Generate a new array.
  78.       $list = array();
  79.       // Put the newsgroup in this array.
  80.       array_push( $list, $_REQUEST['newsgroup'] );
  81.     } else {
  82.       // If the user didn't request a newsgroup we clear the array and
  83.       // display all groups this user is able to see.
  84.       $list = 0;
  85.     }
  86.     // Get all messages of the groups that are listed in $list. If $list
  87.     // is 0 then return all messages.
  88.     $msgs = $nntp->getMessages( $list );
  89.     // If we got any messages we'll sort them and print them in reverse
  90.     // (that means newest first) order. If we got no messages we do not
  91.     // need to do anything.
  92.     if ( count( $msgs ) > 0 ) {
  93.       // Sort and reverse the list of messages (sort by timestamp).
  94.     $omsg = $msgs;
  95.       // Proceed for each message:
  96.       foreach( $omsg as $msg ) {
  97.         // and print it in commenting-enabled view.
  98.     $msg->show( "comment" );
  99.         // Since the messages will be put directly one after another we'll
  100.         // provide an additional space between them.
  101.         echo "<br>";
  102.       }
  103.     }
  104.     break;
  105.  
  106.   // Post the new generated message to the server. No need to break after
  107.   // this becase we can print the thread after the mail got posted.
  108.   case 'post':
  109.     // To post a new message to a newsgroup we need to know to which
  110.     // group we should post it. So first we check if we know something
  111.     // about the destination newsgroup...
  112.     if ( isset( $_POST['newsgroup'] ) ) {
  113.       // Next thing we check if $message is set, which means that the new
  114.       // message is a answer to an old message.
  115.       if ( isset( $_POST['message'] ) ) {
  116.         // So we are answering a message. To fill the References Headers
  117.         // we need to find out details of the original message...
  118.         $msg = $nntp->getMessage( $_POST['newsgroup'], $_POST['message'] );
  119.     // The field that is interesting is the id of the original message.
  120.         $msgid = $msg->long_id;
  121.       } else {
  122.         // Ok, we are generating a new message, so there is no message id
  123.     // of an old message and we set the id to 0.
  124.         $msgid = 0;
  125.       }
  126.       // Next step is to find out who we are to generate the correct
  127.       // from address...
  128.       $sender = $_SESSION['GO_SESSION']["name"]." <".
  129.                 $_SESSION['GO_SESSION']["email"].">";
  130.       // Now we have all information we need to post the new message. So
  131.       // we can do it.
  132.       // TODO add some checks if subject and body is valid.
  133.       // TODO $retval should include the message id of the new message so
  134.       // we can display the correct thread if we just started a new one.
  135.       $retval = $nntp->postMessage(
  136.                                   $_POST['newsgroup'],
  137.                                   $sender,
  138.                                   $msgid,
  139.                                   smartstrip( $_POST['news_subj'] ),
  140.                                   smartstrip( $_POST['news_body'] )
  141.                                   );
  142.       // If the message was posted successfully we can inform the user
  143.       // about this.
  144.       // TODO also inform the user if sending failed
  145.       if ( $retval ) {
  146.         // TODO replace hardcoded message with variable in language-file
  147.         echo "<h1>Nachricht erfolgreich gesendet.</h1>\n";
  148.       }
  149.     }
  150.     // We do not break here because we like to see the thread to which the
  151.     // posted message belongs. For the user this is an additional check if
  152.     // the message was posted correctly...
  153.  
  154.   // Display only one thread of a specific newsgroup.
  155.   case 'show_thread':
  156.     // The thread can only be identified if we know a message of this
  157.     // thread and to which newsgroup it belongs. So first we check if
  158.     // we know some details...
  159.     if ( isset( $_REQUEST['message'] ) && isset( $_REQUEST['newsgroup'] ) ) {
  160.       // OK, we know which thread we should display. So we query this
  161.       // thread from the server and put it into a variable.
  162.       $thread = $nntp->getThread(
  163.                                 $_REQUEST['newsgroup'],
  164.                                 $_REQUEST['message']
  165.                                 );
  166.       // Thread is an array of messages. We proceed for each message
  167.       // (that means we split this array to seperate messages and print
  168.       // each message for it's own)
  169.       foreach ( $thread as $msg ) {
  170.         // We print this message in a table, this is the easiest way to
  171.     // enable a threaded view, because we can add clean cells left
  172.     // of the message to shift the message a bit to the right.
  173.         echo "<table border='0' width='100%' cellspacing='0' cellpadding='1'";
  174.         echo " bgcolor='#FFFFFF'><tr>\n";
  175.     // The eigth value of the message (which is also represented by
  176.     // an array) is the number of messages it references, that means
  177.     // the value we have to shift the message to the right.
  178.     for ( $i=1; $i<$msg->depth; $i++ ) {
  179.       echo "  <td width=20> </td>\n";
  180.     }
  181.     // This is the cell where we print the content of the message.
  182.     // Since we are still in a table we have to add the <td> tags.
  183.         echo "  <td>\n";
  184.     // Print the message. Use the function show of the message, so we
  185.         // do not care about the implementation here.
  186.         $msg->show();
  187.     // Close the cell with the message content...
  188.         echo "  </td>\n";
  189.     // Of course we have to close the table ;-)
  190.     echo "</tr></table>\n";
  191.       }
  192.     }
  193.     break;
  194.  
  195.   // Answer to an existing message or generate a new message. This depends
  196.   // on weather a specific message is known to answer to, and if not we
  197.   // decide the user likes to create a new.
  198.   case 'new_message':
  199.   case 'answer_msgs':
  200.     // Find out if we should generate a new message or answer to an existing
  201.     // and set the status variable $mode depending on this.
  202.     $mode = ( isset( $_REQUEST['newsgroup'] ) &&
  203.               isset( $_REQUEST['message'] ) ) ? "answer" : "new";
  204.     // Since the user should write his message in the webinterface we must
  205.     // provide a form.
  206.     echo "<form name='sendform' method='post' action='";
  207.     echo $_SERVER['PHP_SELF']."'>\n";
  208.     // Depending on the mode we print a drop-box with all accessible groups
  209.     // or the specific group we are answering to.
  210.     if ( $mode == "answer" ) {
  211.       // Find out to which message we are answering. This is needed for
  212.       // correct settings of the References and Follow-Up headers.
  213.       $msg = $nntp->getMessage( $newsgroup, $message );
  214.       // Print the message we are anwering to in a hidden field.
  215.       echo "<input type='hidden' name='message' value='";
  216.       echo $_REQUEST['message']."'/>\n";
  217.       // Print the name of the group we are answering.
  218.       echo "<input type='hidden' name='newsgroup' value='";
  219.       echo $_REQUEST['newsgroup']."'/>\n";
  220.     } else {
  221.       // We generate a new message. So first we find out which groups are
  222.       // present on the server.
  223.       $list = $nntp->getGroups();
  224.       // Since $list is a complex array and we only like to know about the
  225.       // names of the groups we must split them and fill a new array. First
  226.       // create a new array without data in it.
  227.       $ngroups = array();
  228.       // Now we proceed for each group
  229.       foreach ( $list as $ng ) {
  230.         // Get the name of this group and append the name to the array.
  231.         $ngroups[] = substr( $ng->name, strpos( $ng->name, "}")+1 );
  232.       }
  233.       // Sort the array, the user will be happy to see the groups in
  234.       // alphabetical order.
  235.       sort( $ngroups );
  236.       // Now we create our dropbox with all the groups.
  237.       $dropbox = new dropbox();
  238.       // Fill the dropbox with data (add each groupname to it), but prcess
  239.       // the groups to enable a threaded view of them
  240.       foreach ( $ngroups as $ng ) {
  241.         // We say that a group is not a subgroup of another group.
  242.         $indent = "";
  243.         // Since the groups are sorted, we can compare the actual group with
  244.         // the group we processed last, and all fields that are equal in this
  245.         // group and the last group indicate that we can indent this group.
  246.         // We can only do this if we already processed the first group.
  247.         if ( is_array( $last ) ) {
  248.           // Fill the array with the seperate parts of the name of the group.
  249.           $parts = explode( ".", $ng );
  250.           // We process each part of the shorter group.
  251.           for ( $i=1; ($i<count($parts)) && ($i<count($last)); $i++ ) {
  252.             // If the actual processed part is equal in this group and tha
  253.             // last one, we can indent it.
  254.             if ( $parts[$i] == $last[$i] )
  255.               $indent = $indent."   ";
  256.           }
  257.         }
  258.         // Text and value of dropbox entry should be the same: the name
  259.         // of the group.
  260.         $dropbox->add_value( $ng, $indent.$ng );
  261.         // We finished with processing this group. Now we update this variable
  262.         // To be able to compare the next group with this one.
  263.         $last = explode( ".", $ng );
  264.       }
  265.       // Print the dropbox. It's called "newsgroup" without any parameters.
  266.       $dropbox->print_dropbox('newsgroup','','');
  267.       echo "<br/>";
  268.     }
  269.     // Now we print an input field for the subject of the message.
  270.     echo "<input type='text' class='textbox' name='news_subj' size='110' ";
  271.     echo "value='";
  272.     // If we are answering to an message we print the old subject with an
  273.     // additional "Re: " in front of it:
  274.     if ( $mode == "answer" ) {
  275.       // Print an "Re: " and the encoded subject of the old message.
  276.       echo "Re: ".utf8_decode( imap_utf8( $msg->subject ) );
  277.     }
  278.     // Close the input field. If we're not answering the value of the input
  279.     // field will be clear and the user has to choose a subject.
  280.     echo "'><br/>\n";
  281.     // Print a textbox for the body of the new message
  282.     echo "<textarea class='textbox' name='news_body' cols='110' rows='12'>";
  283.     // If we are answering a message we print the old body quoted in the box.
  284.     if ( $mode == "answer" ) {
  285.       // Print the quoted text.
  286.       echo quote( $msg->body );
  287.     }
  288.     // And close the message-body textbox again.
  289.     echo "</textarea><br/>";
  290.     // Add a hidden field to tell us that the user is posting a new message.
  291.     echo "<input type='hidden' name='task' value='post'/>";
  292.     // Print the "send" button
  293.     $button = new button( "send", "document.sendform.submit();" );
  294.     // Close the form.
  295.     echo "</form>";
  296.     break;
  297.  
  298.   // This really should not happen since our default-action is show_groups!
  299.   default:
  300.     break;
  301. }
  302.  
  303. // Since all our output goes into a table we have to close the following tags
  304. echo "</td></tr></table>";
  305.  
  306. // Load theme-footer, this is probably some kind of "Group-Office Version..."
  307. require( $GO_THEME->theme_path."footer.inc" );
  308.  
  309. // That's it, we've printed what the user wanted to do and can now exit.
  310. // Maybe that would be the correct place to close database connections...
  311.  
  312. // We're finished and able to close the connection to the newsserver,
  313. $nntp->close();
  314. ?>
  315.