PHP 4 version (processlist.php)

2006-01-31
2013-04-26
  • michael kimsal
    michael kimsal
    2006-01-31

    I hacked the processlist to work in PHP4 - I know this is truly a temp hack, but figured I'd post it here for anyone that might need it.

    <?

    header("Content-Type: text/xml");

       //include db connection
    include 'dbconfig.inc.php';
    $xmlString = "<?xml version='1.0'?>\n";

    $result = mysql_query("show processlist",$db) or die(mysql_error($db));

       //init filters.
    $filters = array("Host","db");

    while($row = mysql_fetch_assoc($result)) {

              //check the current thread against filter rules.
              $insertFlag=1; //  1=Insert  0=No Insert
              //format Host.
              $hostValue = chopHost($row['Host']);

              foreach($filters as $currentFilter) {
                    if($insertFlag != 0) {
                       if($currentFilter == "Host") {
                          $insertFlag = filter($hostValue,$_GET[$currentFilter]);
                       } else {
                          $insertFlag = filter($row[$currentFilter],$_GET[$currentFilter]);
                       }//end if/else.
                    }//end if.

              }//end for/each.

              if($insertFlag == 1) {
                     //create a new thread element.
                    $string = "<div class='Columns'>\n";
                    $xmlString .= $string;

                     //insert thread into DOM object.
                 foreach($row as $fieldname => $fieldvalue) {

                    //format Host.
                    if($fieldname == "Host") {
                       $fieldvalue = $hostValue;
                    }//end if.

                    //this for/each loop assigns the values
                //to the DOM object for later use as XML.
                    $string = "<div class='Column dataDetail'>\n";
                    $xmlString .= $string;
                    $xmlString .= $fieldvalue;
                    $xmlString.="</div>\n";

                 }//end for/each.
                    $xmlString .= "</div>\n";
              }//end if.

       }//end while.

    echo $xmlString;

       function filter($fieldValue,$filterValue) {

              //check for wildcard *
              //wildcard will allow substring results, like  mp* would return mp3 and mp333, etc.
              /*
              if($array_wild = count(explode("*",$filterValue)) > 0) {
                 //a wild card was used.
                 //return positive if the fieldValue is a substring of $filterValue.
                 //now, check to see if there is a substring match.
                 if(strpos($fieldValue,$filterValue) != FALSE) {
                    //a substring match was found.
                    return 1;
                 } else {
                    return 0;
                 }//end if/else.
              }//end if/else.
              */

          if($filterValue == "ALL") {

             return 1;
          }//end if.

          if($fieldValue == $filterValue) {
             return 1;
          }//end if.

          return 0;  //nothing matched up.

       }//end filter.

       function chopHost($host) {
          //this function will return the appropriate hostname
          //or IP address without the port specifications.

          //strip the port.
          $chopped_fieldvalue = explode(":",$host);
          $chopped_fieldvalueNoPort = $chopped_fieldvalue[0];

              $chopped_array = explode(".",$chopped_fieldvalueNoPort);

              //now determine what to return.
              if(count($chopped_array) == 4) {
                //this is an IP address. (red-man specific hack.)
                return($chopped_fieldvalueNoPort);
              } else {
                    //return the first host name.
                    return($chopped_array[0]); //return first element in explode list.
              }//end if/else.

       }//end chopHost.

    ?>

     
    • groovecoder
      groovecoder
      2006-01-31

      Wow, thanks! We just had someone install this on PHP4 and they experienced the problems with the DOM functions. We'll use this and check to make sure it lines up with our planned enhancements.

      If it works, we'll distribute it with future releases as processlist.php4

      Thanks again!

       
    • michael kimsal
      michael kimsal
      2006-02-01

      In an ideal world the DOM access could abstracted, and a factory method could determine which type of DOM controller to hand back to you (a PHP4 or PHP5 compatible one), and you'd just use $dom->addchild()  calls which are calling to the abstracted object, not the main PHP functions.  However, I didn't have the time to put that together right now.  I will be mentioning ajaxMyTop on a future podcast at webdevradio.com too :)

       
      • groovecoder
        groovecoder
        2006-02-01

        I found this site:

        http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/index.en.html

        which is apparently a facade/adapter method to make PHP4 code compatible with PHP5 code, but I think I'd rather keep the underlying DOM code as the PHP5 method because it's supposed to be faster, etc.

        Thanks again for the code and for the comments.

         
    • Joe Grossberg
      Joe Grossberg
      2006-02-02

      Hmm. You can actually do it with less modification of the original code, because PHP 4 has access to the depricated DOM XML module (or at least some versions do).

      I agree that abstracting out which module you are using would be nicer, but here goes:

      ============================================

      <?

      /* Heavily modified to use DOM XML extension, which is available in PHP4 --jsg */

         header("Content-Type: text/xml");

         //include db connection
         include 'dbconfig.inc.php';

         $xml = domxml_new_doc('1');

         //setup the initial root element of the xml document.
         $rootElement = $xml->create_element('response');
         $rootElement = $xml->append_child($rootElement);

         $result = mysql_query("show processlist",$db) or die(mysql_error($db));

         //init filters.
         $filters = array("Host","db");

         while($row = mysql_fetch_assoc($result)) {

                //check the current thread against filter rules.
                $insertFlag=1; //  1=Insert  0=No Insert

                //format Host.

                $hostValue = chopHost($row[Host]);

                foreach($filters as $currentFilter) {
                      if($insertFlag != 0) {
                         if($currentFilter == "Host") {
                            $insertFlag = filter($hostValue,$_GET[$currentFilter]);
                         } else {
                            $insertFlag = filter($row[$currentFilter],$_GET[$currentFilter]);
                         }//end if/else.
                      }//end if.

                }//end for/each.

                if($insertFlag == 1) {

                       //create a new thread element.
                   $thread = $xml->create_element('div');
                   $thread->set_attribute('class','Columns');
                   $thread = $rootElement->append_child($thread);

                       //insert thread into DOM object.
                   foreach($row as $fieldname => $fieldvalue) {

                      //format Host.
                      if($fieldname == "Host") {
                         $fieldvalue = $hostValue;
                      }//end if.

                      //this for/each loop assigns the values
                  //to the DOM object for later use as XML.
                      $child = $xml->create_element('div');
                      $child->set_attribute('class','Column dataDetail');
                      $child = $thread->append_child($child);

                      $value = $xml->create_text_node($fieldvalue);
                          $value = $child->append_child($value);
                   }//end for/each.
                }//end if.

         }//end while.

         //$xml_string = $xml->saveXML();
         $xml_string = $xml->html_dump_mem();

         echo $xml_string;

         function filter($fieldValue,$filterValue) {

                //check for wildcard *
                //wildcard will allow substring results, like  mp* would return mp3 and mp333, etc.
                /*
                if($array_wild = count(explode("*",$filterValue)) > 0) {
                   //a wild card was used.
                   //return positive if the fieldValue is a substring of $filterValue.
                   //now, check to see if there is a substring match.
                   if(strpos($fieldValue,$filterValue) != FALSE) {
                      //a substring match was found.
                      return 1;
                   } else {
                      return 0;
                   }//end if/else.
                }//end if/else.
                */

            if($filterValue == "ALL") {

               return 1;
            }//end if.

            if($fieldValue == $filterValue) {
               return 1;
            }//end if.

            return 0;  //nothing matched up.

         }//end filter.

         function chopHost($host) {
            //this function will return the appropriate hostname
            //or IP address without the port specifications.

            //strip the port.
            $chopped_fieldvalue = explode(":",$host);
            $chopped_fieldvalueNoPort = $chopped_fieldvalue[0];

                $chopped_array = explode(".",$chopped_fieldvalueNoPort);

                //now determine what to return.
                if(count($chopped_array) == 4) {
                  //this is an IP address. (red-man specific hack.)
                  return($chopped_fieldvalueNoPort);
                } else {
                      //return the first host name.
                      return($chopped_array[0]); //return first element in explode list.
                }//end if/else.

         }//end chopHost.

      ?>

      ===============================================

       
    • I've modified ajaxMyTop-0.7.2 to be PHP4 compatible. It would probably remain upward compatible with PHP5 if http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/index.en.html was used. It took considerably more work than just replacing ProcessList.php. Is anyone interested in this code?

       
      • groovecoder
        groovecoder
        2007-04-17

        Thanks Jesse! It'd be great to include the code here in the project tracker somewhere. I'd suggest attaching it to a new thread in the "Patches" tracker. If it's a non-invasive patch, I'll merge it into the svn and publish a new release.

        Thanks.