Location: PHPKode > projects > MaximaPHP > maximaphp/viewers/mpvgenerator.php
<?php

/**
 * mpvgenerator.php, MaximaPHP - Viewer Generator
 *
 * Viewer Generator for MaximaPHP.
 * 
 * This file contains MPVGenerator class.
 * @author Bowo Prasetyo <http://www.my-tool.com/>
 * @version 0.1.3
 * @package MaximaPHP
 */

/**************************************************************
 *
 * Copyright (C) 2007 Bowo Prasetyo
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation below,
 * 
 * http://www.gnu.org/licenses/gpl.html#TOC1
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 **************************************************************/

/**
 * Viewer Generator class for MaximaPHP.
 *
 */
class MPVGenerator 
{

   /**
    * Default text size
    */
   var $defTextSize = 60;

   /**
    * Default column number of textarea
    */
   var $defTextareaCols = 50;

   /**
    * Default row number of textarea
    */
   var $defTextareaRows = 10;

   /**
    * Show the viewer
    */
   function show( $qstring )
   {
      require_once( 'mprewriteurl.php' );

      if ( isset( $_POST['viewer_level'] ) )
         $viewer_level = $_POST['viewer_level'];
      else
         $viewer_level = 1;

      if ( isset( $_POST['viewer_name'] ) )
         $viewer_name = $_POST['viewer_name'];
      else
         $viewer_name = '';

      if ( isset( $_POST['viewer_description'] ) )
         $viewer_description = $_POST['viewer_description'];
      else
         $viewer_description = '';

      if ( isset( $_POST['class_name'] ) )
         $class_name = $_POST['class_name'];
      else
         $class_name = '';

      if ( isset( $_POST['author_name'] ) )
         $author_name = $_POST['author_name'];
      else
         $author_name = '';

      if ( isset( $_POST['author_url_or_email'] ) )
         $author_url_or_email = $_POST['author_url_or_email'];
      else
         $author_url_or_email = '';

      if ( isset( $_POST['view_mathml'] ) )
         $view_mathml = TRUE;
      else
         $view_mathml = FALSE;

      if ( isset( $_POST['view_tex'] ) )
         $view_tex = TRUE;
      else
         $view_tex = FALSE;

      if ( isset( $_POST['view_maxima'] ) )
         $view_maxima = TRUE;
      else
         $view_maxima = FALSE;

      if ( isset( $_POST['simplify'] ) )
         $simplify = TRUE;
      else
         $simplify = FALSE;

      if ( isset( $_POST['numeric'] ) )
         $numeric = TRUE;
      else
         $numeric = FALSE;

      if ( isset( $_POST['example_number'] ) )
         $example_number = $_POST['example_number'];
      else
         $example_number = 1;

      if ( isset( $_POST['form_types'] ) )
         $form_types = $_POST['form_types'];
      else
         $form_types = array( '', '', '' );

      if ( isset( $_POST['form_names'] ) )
         $form_names = $_POST['form_names'];
      else
         $form_names = array( '', '', '' );      

      if ( isset( $_POST['form_texts'] ) )
         $form_texts = $_POST['form_texts'];
      else
         $form_texts = array( '', '', '' );      

      if ( isset( $_POST['form_add'] ) )
      {
         $form_types = array_merge( $form_types, array( '', '', '' ) );
         $form_names = array_merge( $form_names, array( '', '', '' ) );
         $form_texts = array_merge( $form_texts, array( '', '', '' ) );
      }

?>

   <h3><?php echo MPVGENERATOR_TITLE ?></h3>
   <?php echo MPVGENERATOR_MAIN_0 ?><br />

   <?php echo str_replace( '##VERSION2##', MAXIMAPHP_VERSION, str_replace( '##VERSION1##', MAXIMA_VERSION, MPVIEWER_POWERED_BY ) ) ?><br />

   <br />
   <?php echo $view_link ?>
   <form action="" method="post">
   <table>
   <tr><td><?php echo MPVGENERATOR_MAIN_9 ?></td><td><select name="viewer_level"><option <?php echo $viewer_level == 1 ? 'selected="selected"' : '' ?>>1</option><option <?php echo $viewer_level == 2 ? 'selected="selected"' : '' ?>>2</option></select> <?php echo MPVGENERATOR_MAIN_10 ?></td></tr>
   <tr><td><?php echo MPVGENERATOR_MAIN_2 ?></td><td><input type="text" name="class_name" size="30" value="<?php echo $class_name ?>" /> <span class="eg"><?php echo MPVIEWER_EG ?> CellAuto</span></td></tr>
   <tr><td><?php echo MPVGENERATOR_MAIN_1 ?></td><td><input type="text" name="viewer_name" size="30" value="<?php echo $viewer_name ?>" /> <span class="eg"><?php echo MPVIEWER_EG ?> Cellular Automata</span></td></tr>
   <tr><td><?php echo MPVGENERATOR_MAIN_8 ?></td><td><input type="text" name="viewer_description" size="60" value="<?php echo $viewer_description ?>" /><br />
   <span class="eg"><?php echo MPVIEWER_EG ?> Generates and plots population of cellular automata.</span></td></tr>
   <tr><td><?php echo MPVGENERATOR_MAIN_3 ?></td><td><input type="text" name="author_name" size="30" value="<?php echo $author_name ?>" /></td></tr>
   <tr><td><?php echo MPVGENERATOR_MAIN_4 ?></td><td><input type="text" name="author_url_or_email" size="30" value="<?php echo $author_url_or_email ?>" /></td></tr>
   <tr><td><?php echo MPVGENERATOR_MAIN_5 ?><br /></td><td><input type="checkbox" name="view_text" checked="checked" disabled="disabled" /> Text
   <input type="checkbox" name="view_mathml" <?php echo $view_mathml ? 'checked="checked"' : '' ?> /> MathML
   <input type="checkbox" name="view_tex" <?php echo $view_tex ? 'checked="checked"' : '' ?> /> Tex
   <input type="checkbox" name="view_maxima" <?php echo $view_maxima ? 'checked="checked"' : '' ?> /> Maxima</td></tr>
   <tr><td><?php echo MPVGENERATOR_MAIN_11 ?></td><td><select name="example_number"><option <?php echo $example_number == 1 ? 'selected="selected"' : '' ?>>1</option><option <?php echo $example_number == 2 ? 'selected="selected"' : '' ?>>2</option><option <?php echo $example_number == 3 ? 'selected="selected"' : '' ?>>3</option><option <?php echo $example_number == 4 ? 'selected="selected"' : '' ?>>4</option></select>
   <input type="checkbox" name="simplify" <?php echo $simplify ? 'checked="checked"' : '' ?> /> Simplify
   <input type="checkbox" name="numeric" <?php echo $numeric ? 'checked="checked"' : '' ?> /> Numeric
   </td></tr>
   <tr><td colspan="2" style="border: thin solid black; padding: 5px">
   <?php
      echo MPVGENERATOR_MAIN_7 . '<br />';
      for ( $i = 0; $i < count( $form_types ); $i++)
      {
         $type = $form_types[$i];
         $name = $form_names[$i];
         $text = $form_texts[$i];
         echo 'Type <select name="form_types[]"><option value="text" ' . ( $type == 'text' ? 'selected="selected"' : '' ) . '>Text</option><option value="textarea" ' . ( $type == 'textarea' ? 'selected="selected"' : '' ) . '>Textarea</option><option value="checkbox" ' . ( $type == 'checkbox' ? 'selected="selected"' : '' ) . '>Checkbox</option><option value="radio" ' . ( $type == 'radio' ? 'selected="selected"' : '' ) . '>Radio</option><option value="select" ' . ( $type == 'select' ? 'selected="selected"' : '' ) . '>Select</option></select> Name <input type="text" name="form_names[]" size="30" value="' . $name . '" /> Text <input type="text" name="form_texts[]" size="30" value="' . $text . '" /><br />';
      }
   ?>
   </td></tr>
   <tr><td align="left"><input type="submit" name="submit" /></td><td align="right"><input type="submit" name="form_add" value=" <?php echo MPVGENERATOR_MAIN_6 ?> " /></td></tr>
   </table>
   </form>

<?php        
      /** 
       * Generate viewer class file
       */
      if ( isset( $_POST['submit'] ) )
      {
         $upper_class_name = strtoupper( $class_name );
         $lower_class_name = strtolower( $class_name );
         $text_count = 0;
         $text_items = <<<EOT


/**
 * Define text for viewer class MPV{$class_name}
 */
define ( 'MPV{$upper_class_name}_TITLE', '$viewer_name' );
define ( 'MPV{$upper_class_name}_MAIN_{$text_count}', '$viewer_description' );
EOT;

         /** 
          * Construct additional view links other than Text 
          * such as MathML, TeX and Maxima
          */
         $additional_views = '';
         $mathml_msg = '';
         if ( $view_mathml )
         {
            $additional_views .= <<<EOT

      \$view_link .= \$view == 'mathml' ? '<b>MathML</b> | ' : '<a href="' . mprewriteurl( 'index.php?' . \$qstring . 'sub{$viewer_level}=mathml' ) . '">MathML</a> | ';
EOT;
            $mathml_msg = <<<EOT

<?php
   /**
    * Print MathML message
    */
   if ( \$view == 'mathml' )
   {
      echo MPVIEWER_MATHML_MSG . '<br />';
   }
?>
EOT;
         }
         if ( $view_tex )
         {
            $additional_views .= <<<EOT

      \$view_link .= \$view == 'tex' ? '<b>TeX</b> | ' : '<a href="' . mprewriteurl( 'index.php?' . \$qstring . 'sub{$viewer_level}=tex') . '">TeX</a> | ';
EOT;
         }
         if ( $view_maxima )
         {
            $additional_views .= <<<EOT

      \$view_link .= \$view == 'maxima' ? '<b>Maxima</b> | ' : '<a href="' . mprewriteurl( 'index.php?' . \$qstring . 'sub{$viewer_level}=maxima' ) . '">Maxima</a> | ';
EOT;
         }

         /** 
          * Construct post items, form items, text items and session items
          */
         $post_items = '';
         $form_items = '';
         $form_input_1 = '';
         $form_input_2 = '';
         $form_input_3 = '';
         $form_input_4 = '';
         $session_items = '';
         $additional_security_check = '';
         for ( $i = 0; $i < count( $form_types ); $i++)
         {
            $type = $form_types[$i];
            $name = $form_names[$i];
            if ( $name == '' ) continue;
            $text = $form_texts[$i];
            $escaped_text = escapeshellarg($text);
            $text_count++;
            if ( $type == 'text' )
            {
               if ( $form_input_1 == '' ) $form_input_1 = $name;
               else
               {
                  if ( $form_input_2 == '' ) $form_input_2 = $name;
                  else if ( $form_input_3 == '' ) $form_input_3 = $name;
                  else if ( $form_input_4 == '' ) $form_input_4 = $name;
                  $additional_security_check .= <<<EOT

         \$maximaphp->setInput( \$$name );
         \$$name = \$maximaphp->secure();
EOT;
                  $session_items .= <<<EOT

            \$_SESSION['$name'] = \$$name;
EOT;
               }
               $post_items .= <<<EOT


      if ( isset( \$_POST['$name'] ) )
         \$$name = \$_POST['$name'];
      else
      {
         if ( isset( \$_SESSION['$name'] ) )
            \$$name = \$_SESSION['$name'];
         else
            \$$name = '';
      }
EOT;
               $form_items .= <<<EOT

   <tr><td valign="top"><?php echo MPV{$upper_class_name}_MAIN_{$text_count} ?> </td><td><input type="text" name="$name" size="$this->defTextSize" value="<?php echo \$$name ?>" /><br /><span class="eg"><?php echo MPVIEWER_EG ?> </span></td></tr>
EOT;
            }
            else if ( $type == 'textarea' )
            {
               if ( $form_input_1 == '' ) $form_input_1 = $name;
               else
               {
                  if ( $form_input_2 == '' ) $form_input_2 = $name;
                  else if ( $form_input_3 == '' ) $form_input_3 = $name;
                  else if ( $form_input_4 == '' ) $form_input_4 = $name;
                  $additional_security_check .= <<<EOT

         \$maximaphp->setInput( \$$name );
         \$$name = \$maximaphp->secure();
EOT;
                  $session_items .= <<<EOT

            \$_SESSION['$name'] = \$$name;
EOT;
               }
               $post_items .= <<<EOT


      if ( isset( \$_POST['$name'] ) )
         \$$name = stripslashes(\$_POST['$name']);
      else
      {
         if ( isset( \$_SESSION['$name'] ) )
            \$$name = \$_SESSION['$name'];
         else
            \$$name = '';
      }
EOT;
               $form_items .= <<<EOT

   <tr><td valign="top"><?php echo MPV{$upper_class_name}_MAIN_{$text_count} ?> </td><td><textarea name="$name" rows="$this->defTextareaRows" cols="$this->defTextareaCols"><?php echo htmlentities( \$$name ) ?></textarea><br /><span class="eg"><?php echo MPVIEWER_EG ?> </span></td></tr>
EOT;
            }
            else if ( $type == 'checkbox' )
            {
               $post_items .= <<<EOT


      if ( isset( \$_POST['$name'] ) )
         \$$name = TRUE;
      else
         \$$name = FALSE;
EOT;
               $form_items .= <<<EOT

   <tr><td> </td><td><input type="checkbox" name="$name" <?php echo \$$name ? 'checked="checked"' : '' ?> /> <?php echo MPV{$upper_class_name}_MAIN_{$text_count} ?></td></tr>
EOT;
            }
            else if ( $type == 'radio' )
            {
               if ( $form_input_2 == '' ) $form_input_2 = $name;
               else if ( $form_input_3 == '' ) $form_input_3 = $name;
               else if ( $form_input_4 == '' ) $form_input_4 = $name;
               $session_items .= <<<EOT

            \$_SESSION['$name'] = \$$name;
EOT;
               $post_items .= <<<EOT


      if ( isset( \$_POST['$name'] ) )
         \$$name = \$_POST['$name'];
      else
      {
         if ( isset( \$_SESSION['$name'] ) )
            \$$name = \$_SESSION['$name'];
         else
            \$$name = 'value1';
      }
EOT;
               $form_items .= <<<EOT

   <tr><td><?php echo MPV{$upper_class_name}_MAIN_{$text_count} ?> </td><td>
   <input type="radio" name="$name" value="value1" <?php echo \$$name == 'value1' ? 'checked="checked"' : '' ?> /> Value 1
   <input type="radio" name="$name" value="value2" <?php echo \$$name == 'value2' ? 'checked="checked"' : '' ?> /> Value 2
   </td></tr>
EOT;
            }
            else if ( $type == 'select' )
            {
               if ( $form_input_2 == '' ) $form_input_2 = $name;
               else if ( $form_input_3 == '' ) $form_input_3 = $name;
               else if ( $form_input_4 == '' ) $form_input_4 = $name;
               $session_items .= <<<EOT

            \$_SESSION['$name'] = \$$name;
EOT;
               $post_items .= <<<EOT


      if ( isset( \$_POST['$name'] ) )
         \$$name = \$_POST['$name'];
      else
      {
         if ( isset( \$_SESSION['$name'] ) )
            \$$name = \$_SESSION['$name'];
         else
            \$$name = 'value1';
      }
EOT;
               $form_items .= <<<EOT

   <tr><td><?php echo MPV{$upper_class_name}_MAIN_{$text_count} ?> </td><td>
   <select name="$name">
   <option value="value1" <?php echo \$$name == 'value1' ? 'selected="selected"' : '' ?>>Value 1</option>
   <option value="value2" <?php echo \$$name == 'value2' ? 'selected="selected"' : '' ?>>Value 2</option>
   </select>
   </td></tr>
EOT;
            }
            if ( $escaped_text == '' ) $escaped_text = "''";
            $text_items .= <<<EOT

define ( 'MPV{$upper_class_name}_MAIN_{$text_count}', $escaped_text );
EOT;
         }

         /**
          * Reset simplify and numeric items
          */
         $format_output_comment = '';
         $format_output_args = "\$result";
         $format_output_example = <<<EOT

       * \$result = preg_replace( '/.*(?=\\n\s*:\| ##LOWER_FORM_INPUT_1## \|:)/s', '', \$result );
       *
EOT;
         $ev_simp_variable = '';
         $ev_simp_pattern = '';
         $ev_simp_replacement = '';
         $ev_numer_variable = '';
         $ev_numer_replacement = '';

         /**
          * Construct simplify items
          */
         if ( $simplify )
         {
            $format_output_args .= ", \$simplify";
            $format_output_comment .= <<<EOT

    * @param boolean \$simplify whether to simplify output or not
EOT;
            $format_output_example .= <<<EOT
 if ( \$simplify or \$numeric )
       *    \$result = preg_replace( '/\(%i[0-9]+\).*(?=\\n\s*\:\| simplified \|:)/s', '', \$result );
       * else
EOT;
            if ( $numeric )
            {
               $ev_simp_variable = <<<EOT

            \$ev_simp = '; block(disp(":| simplified |:")##EV_NUMER_1##, fullratsimp(radcan(expand(%)))##EV_NUMER_2##)'; 
EOT;
            }
            else
            {
               $ev_simp_variable = <<<EOT

            \$ev_simp = '; block(disp(":| simplified |:"), fullratsimp(radcan(expand(%))))'; 
EOT;
            }
            $ev_simp_pattern = "##EV_SIMP##";
            $ev_simp_replacement = <<<EOT

            \$tomaxima = str_replace( '##EV_SIMP##', (\$simplify or \$numeric) ? \$ev_simp : '', \$tomaxima );
EOT;
            $post_items .= <<<EOT


      if ( isset( \$_POST['simplify'] ) )
         \$simplify = TRUE;
      else
         \$simplify = FALSE;
EOT;
            $form_items .= <<<EOT

   <tr><td> </td><td><input type="checkbox" name="simplify" <?php echo \$simplify ? 'checked="checked"' : '' ?> /> <?php echo MPVIEWER_SIMPLIFY ?></td></tr>
EOT;
         }

         /**
          * Construct numeric items
          */
         $rational_pattern = '';
         $rational_code_pattern = '';
         if ( $numeric )
         {
            $format_output_args .= ", \$numeric";
            $format_output_comment .= <<<EOT

    * @param boolean \$numeric whether to apply numeric or not
EOT;
            if ( !$simplify )
            {
               $format_output_example .= <<<EOT
 if ( \$simplify or \$numeric )
       *    \$result = preg_replace( '/\(%i[0-9]+\).*(?=\\n\s*\:\| simplified \|:)/s', '', \$result );
       * else
EOT;
               $ev_simp_variable = <<<EOT

            \$ev_simp = '; block(disp(":| simplified |:")##EV_NUMER_1##, fullratsimp(radcan(expand(%)))##EV_NUMER_2##)'; 
EOT;
               $ev_simp_pattern = "##EV_SIMP##";
               $ev_numer_replacement = <<<EOT

            \$tomaxima = str_replace( '##EV_SIMP##', (\$simplify or \$numeric) ? \$ev_simp : '', \$tomaxima );
EOT;
            }
            $ev_numer_variable = <<<EOT

            \$ev_numer_1 = ', numer:true, %enumer:true'; 
            \$ev_numer_2 = ', scanmap(rational,%%)';
EOT;
            $rational_pattern = '##RATIONAL_CODE##';
            $rational_code_pattern = <<<EOT0

            \$rational_code = <<<EOT

rational(z) := block
(
 [n: num(z)],
 [d: denom(z)],
 [c: conjugate(d)],
 expand((n*c)/(expand(d*c)))
);
EOT;
EOT0;
            $ev_numer_replacement .= <<<EOT

            \$tomaxima = str_replace( '##EV_NUMER_1##', \$numeric ? \$ev_numer_1 : '', \$tomaxima );
            \$tomaxima = str_replace( '##EV_NUMER_2##', \$numeric ? \$ev_numer_2 : '', \$tomaxima );
            \$tomaxima = str_replace( '##RATIONAL_CODE##', \$numeric ? \$rational_code : '', \$tomaxima );
EOT;
            $post_items .= <<<EOT


      if ( isset( \$_POST['numeric'] ) )
         \$numeric = TRUE;
      else
         \$numeric = FALSE;
EOT;
            $form_items .= <<<EOT

   <tr><td> </td><td><input type="checkbox" name="numeric" <?php echo \$numeric ? 'checked="checked"' : '' ?> /> <?php echo MPVIEWER_NUMERIC ?></td></tr>
EOT;
         }
         if ( $simplify or $numeric )
         {
            $format_output_example .= <<<EOT

       *    \$result = preg_replace( '/\(%i[0-9]+\).*(?=\\n\s*:\| ##LOWER_CLASS_NAME## \|:)/s', '', \$result );
       * \$result = preg_replace( '/\\n`rat\' replaced.+\\n/U', '', \$result );
EOT;
         }
         else
         {
            $format_output_example .= <<<EOT
 \$result = preg_replace( '/\(%i[0-9]+\).*(?=\\n\s*:\| ##LOWER_CLASS_NAME## \|:)/s', '', \$result );
EOT;
         }
         /**
          * Construct example system
          */
         $example_entries = 'array( ';
         $get_example_entries = '';
         $set_example_text = '';
         for ( $i = 0; $i < $example_number; $i++ )
         {
            $j = $i + 1;
            $example_entries .= "'', ";
            $get_example_entries .= "htmlentities( \$this->examples[\$i][$i] ) . '<br />' . ";
            $set_example_text .= "\n            \$" . ${"form_input_{$j}"} . " = \$this->examples[\$m][$i];";
         }
         if ( $example_number == 1 )
            $example_entries = "''";
         else
            $example_entries = rtrim( $example_entries, ', ' ) . ' )';
         $get_example_entries = rtrim( $get_example_entries, '. ' );

         /**
          * Open class template and make it
          */
         $template = file_get_contents( "mpviewer.tpl" );
         $template = str_replace( '##VIEWER_LEVEL##', $viewer_level, $template );
         $template = str_replace( '##VIEWER_NAME##', $viewer_name, $template );
         $template = str_replace( '##CLASS_NAME##', $class_name, $template );
         $template = str_replace( '##AUTHOR_NAME##', $author_name, $template );
         $template = str_replace( '##AUTHOR_URL_OR_EMAIL##', $author_url_or_email, $template );
         $template = str_replace( '##ADDITIONAL_SECURITY_CHECK##', $additional_security_check, $template );
         $template = str_replace( '##ADDITIONAL_VIEWS##', $additional_views, $template );
         $template = str_replace( '##MATHML_MSG##', $mathml_msg, $template );
         $template = str_replace( '##EXAMPLE_ENTRIES##', $example_entries, $template );
         $template = str_replace( '##GET_EXAMPLE_ENTRIES##', $get_example_entries, $template );
         $template = str_replace( '##SET_EXAMPLE_TEXT##', $set_example_text, $template );
         $template = str_replace( '##POST_ITEMS##', $post_items, $template );
         $template = str_replace( '##FORM_ITEMS##', $form_items, $template );
         $template = str_replace( '##SESSION_ITEMS##', $session_items, $template );
         $template = str_replace( '##FORM_INPUT_1##', "\$$form_input_1", $template );
         $template = str_replace( '##FORMAT_OUTPUT_COMMENT##', "$format_output_comment", $template );
         $template = str_replace( '##FORMAT_OUTPUT_ARGS##', "$format_output_args", $template );
         $template = str_replace( '##FORMAT_OUTPUT_EXAMPLE##', "$format_output_example", $template );
         $template = str_replace( '##EV_SIMP_VARIABLE##', "$ev_simp_variable", $template );
         $template = str_replace( '##EV_SIMP_PATTERN##', "$ev_simp_pattern", $template );
         $template = str_replace( '##EV_SIMP_REPLACEMENT##', "$ev_simp_replacement", $template );
         $template = str_replace( '##EV_NUMER_VARIABLE##', "$ev_numer_variable", $template );
         $template = str_replace( '##EV_NUMER_REPLACEMENT##', "$ev_numer_replacement", $template );
         $template = str_replace( '##RATIONAL_PATTERN##', "$rational_pattern", $template );
         $template = str_replace( '##RATIONAL_CODE_PATTERN##', "$rational_code_pattern", $template );
         $template = str_replace( '##LOWER_CLASS_NAME##', $lower_class_name, $template );
         $template = str_replace( '##UPPER_CLASS_NAME##', $upper_class_name, $template );
         $template = str_replace( '##LOWER_FORM_INPUT_1##', $form_input_1, $template );
         $template = str_replace( '##UPPER_FORM_INPUT_1##', strtoupper($form_input_1), $template );
         $text_count++;
         $template = str_replace( '##TEXT_COUNT_1##', $text_count, $template );
         $text_items .= <<<EOT

define ( 'MPV{$upper_class_name}_MAIN_{$text_count}', 'Want to use your own Maxima..? Copy and paste the following code into your local Maxima and $viewer_name all your expressions away.. :-)' );
EOT;
         $text_count++;
         $template = str_replace( '##TEXT_COUNT_2##', $text_count, $template );
         $text_items .= <<<EOT

define ( 'MPV{$upper_class_name}_MAIN_{$text_count}', "/*\\n* Generated by MaximaPHP $viewer_name\\n* $author_url_or_email\\n*/" );
EOT;

         /**
          * Write class definition to a class file
          */
         $filename = 'mpv' . $lower_class_name . '.php';
         if ( !$handle = fopen( $filename, 'w' ) ) 
         {
               echo "Cannot open file ($filename)<br />";
         }
         else
         {
            if ( fwrite( $handle, $template ) === FALSE ) 
            {
                echo "Cannot write to file ($filename)<br />";
            }
            else
            {
               echo "Success, wrote class MPV{$class_name} to file ($filename)<br />";
            }
            fclose($handle);
         }

         /**
          * Open default text definition and make it
          */
         $filename = "mptext.php";
         $new_text_holder = <<<EOT
/**
 * DON'T ALTER THIS LAST COMMENT
 * It is used by Viewer Generator to insert new text
 *
 * Define text for viewer class MPVNextViewer
 */
EOT;
         $text_items .= "\n\n$new_text_holder";
         $template = file_get_contents( $filename );
         $lang_file = '';
         if ( preg_match( '/mptext-[a-z]+\.php/', $template, $matches ) ) $lang_file = $matches[0];
         $template = str_replace( $new_text_holder, $text_items, $template );

         /**
          * Write text definition to default text file
          */
         if ( !( $handle = fopen( $filename, 'w' ) ) ) 
         {
               echo "Cannot open file ($filename)<br />";
         }
         else
         {
            if ( fwrite( $handle, $template ) === FALSE ) 
            {
                echo "Cannot write to file ($filename)<br />";
            }
            else
            {
               echo "Success, wrote text definition to file ($filename)<br />";
            }
            fclose($handle);
         }

         $lang_write = FALSE;
         if ( $lang_file != '' and file_exists( "lang/$lang_file" ) )
         {
            /**
             * Open language specific text definition and make it
             */
            $filename = "lang/$lang_file";
            $template = file_get_contents( $filename );
            $lang_file = '';
            if ( preg_match( '/mptext-[a-z]+\.php/', $template, $matches ) ) $lang_file = $matches[0];
            $template = str_replace( $new_text_holder, $text_items, $template );

            /**
             * Write text definition to language specific text file
             */
            if ( !( $handle = fopen( $filename, 'w' ) ) ) 
            {
                  echo "Cannot open file ($filename)<br />";
            }
            else
            {
               if ( fwrite( $handle, $template ) === FALSE ) 
               {
                   echo "Cannot write to file ($filename)<br />";
               }
               else
               {
                  $lang_write = TRUE;
                  echo "Success, wrote text definition to file ($filename)<br />";
               }
               fclose($handle);
            }
         }

         if ( !$lang_write )
         {
            /**
             * Write text definition to temporary text file
             */
            $filename = 'mpv' . $lower_class_name . '.txt';
            if ( !( $handle = fopen( $filename, 'w' ) ) ) 
            {
                  echo "Cannot open file ($filename)<br />";
            }
            else
            {
               if ( fwrite( $handle, $text_items ) === FALSE ) 
               {
                   echo "Cannot write to file ($filename)<br />";
               }
               else
               {
                  echo "Success, wrote text definition to temporary file ($filename).<br />\nInsert its content to your default and language specific text definition and delete this file.";
               }
            }
            fclose($handle);
         }
      }
?>

<?php
   }
}

?>
Return current item: MaximaPHP