The Worst PHP Scraper Ever



  • I recently got to "re-examine" a php program that I wrote for my boss about two years ago. Having gotten out of high school a mere three months earlier, and having never dealt with "enterprise" level coding, I was exasperated by the requirements.

    We use Cacti and Nagios to monitor many of our systems, but evitentally all the extra clicks of logging in were just too much. So he tasked me with a "special assignment": he wanted a single page that he could go to and see all the Nagios AND Cacti data for the selected segment of the network. (Working at a school district, they were pretty much segmented by school.)

    I was given a month. Normally, this wouldn't be so much of a problem, except that I had never even heard of Cacti or Nagios before hand, which meant "integration" was out of the question. I was then left with one option: scrape it all.

    The only problem was that the login authentication which was the cause of my bosses pain was multiplied by ten fold when I was given a web-server that had PHP and no curl. So I did what no programmer should EVER do but which every programmer in an "enterprise" has done: I made something that got it done irregardless of the consequences. I think the actual code would be enlightening at this point:

    $content = shell_exec("/usr/local/bin/wget --no-check-certificate -O - --save-cookies cookies.txt --keep-session-cookies --post-data='login_username=XXXX&login_password=XXXX&action=login' https://XXXXXX/cacti/index.php >/dev/null 2>&1;/usr/local/bin/wget --no-check-certificate -O - --load-cookies cookies.txt '".$url."'");

    $list = "";

    $list .= preg_replace("#.?<img src='(.??.?)'.#", "<grab>\1", $content);
    //$list = preg_replace("/\n$/", "", $list);
    $massive = explode("\n", trim($list));



    // CLOSE CURL


    //echo $content;
    $len = count($massive);
    $len1 = $len-1;

    $n = 0;
    for ($i = 0;$i < $len1;$i++) {
    $var = preg_replace("#<grab>.*#", "<grab>", $massive[$i]);
    if ($var == "<grab>") {
    //echo "Yay";
    //echo $len;
    $daimgs[$n] = str_replace("<grab>", "", $massive[$i]);
    $n++;
    } else {
    //echo ":(";
    //echo $var;
    }
    }
    //echo $list;
    //}

    Evidentally I didn't know how strpos() worked when I wrote this and ended up implementing it with processor intensive arrays and preg_replace()'s, (I didn't even use preg_match()!) This is by far the most WTF code I have ever made, and as I stumbled on it recently, it made me weep inside...

    The worst part is that a month later when I showed it to my boss, he looked at the page quizzically and exclaimed "What the hell is this?!"

    And so it was I lost my programmer's innocence... by the time I finished the program, he had forgetten about the whole assignment.



  • PHB:  How soon can you do this?

    Dilbert:  How soon will you forget you assigned it to me?

    PHB:  Two weeks.

    Dilbert:  Fifteen days.

     



  • Irregardless of what you think, irregardless is not a real word. The correct word in this case is simply "regardless."


  • Discourse touched me in a no-no place

    @un.sined said:

    Irregardless of what you think, irregardless is not a real word.
    Perhaps you should point this fact out to Merriam Webster: http://m-w.com/dictionary/irregardless
    @M-W said:
    The most frequently repeated remark about it is that "there is no such word." There is such a word, however.
    They then go on to suggest you use regardless, however.



  • @PJH said:

    @un.sined said:

    Irregardless of what you think, irregardless is not a real word.
    Perhaps you should point this fact out to Merriam Webster: http://m-w.com/dictionary/irregardless
    @M-W said:
    The most frequently repeated remark about it is that "there is no such word." There is such a word, however.
    They then go on to suggest you use regardless, however.

    Well, webster's, like oxford, takes a very inclusionist policy to language ("if people say it then it's a word"), which doesn't win it many fans of strict language.



  • If they didn't add all of the worthless abortions of language that people are spouting these days why would you need to buy a new dictionary ever again? The simple facts are that irregardless isn't a word, but it's seen enough use that M-W decided they had better mention it to avoid the impression that they're irrelevant. 

     

     

    On a particularly sad note: irregardless is in Firefox's spell checker. A fairy just lost it's wings.



  • @foxyshadis said:

    Well, webster's, like oxford, takes a very inclusionist policy to language ("if people say it then it's a word"), which doesn't win it many fans of strict language.

    Well yes, but dictionaries are designed to help people understand words that are used in real life, not just to explain a particular subset of words that are almost arbitrarily accepted as correct by a select group. People who can't accept that language changes always remind me of this guy, http://en.wikipedia.org/wiki/King_Canute (only without the bit at the end with Jesus)

    [Dives behind fireproofed sofa]
     



  • I think this is a nice response on irregardless


Log in to reply