PHP Parsing and Writing to text file

Discussion in 'Programming' started by dopex, Apr 20, 2008.

  1. #1
    I really don't know if it's possible with PHP but I do know it's possible with Visual Basic, but I can't have it in a program.

    Basically what I want to do is, parse something from a webpage and write it to a text file, one line per parse. What I want to parse is in between

    <iframe id="apppanel_104237_canvas" src="
    PHP:
    and
    "width="800" height="600" scrolling="no" frameborder="0" style="border: none; background:transparent;"></iframe>
    PHP:
    the page its on is
    http://profile.myspace.com/Modules/Applications/Pages/Canvas.aspx?appId=104237
    PHP:
    an example of what it would parse would be

    http://truthbox.cubaby.com/?country=US&lang=en&oauth_consumer_key=http%3A%2F%2Ftruthbox.applatform.com&oauth_nonce=633443134456767411&oauth_signature=pvNvux%2FNgBz3%2BO1uX2JCRcaHcYI%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1208716645&oauth_token=&oauth_version=1.0&opensocial_owner_id=4073476&opensocial_viewer_id=4073476&pto=COMMENTS%2CBLOG%2CBULLETINS%2CPROFILE%2CSEND_MESSAGE
    PHP:
     
    dopex, Apr 20, 2008 IP
  2. CPURules

    CPURules Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here's a functions file[has a wrapper class & a function]. After the functions is the script that does the parsing.

    functions.php: (cURL class thanks to Keith @ DarkZtar, it's posted publicly there)
    
    <?
    function GSB($oString, $oStart, $oEnd, $start = 1) {
    $strings = explode($oStart, $oString);
    $tempstr = $strings[$start];
    
    $strings = explode($oEnd, $tempstr);
    return $strings[0];
    }
    
    class cURL {
            /*
            * @author Keith Kurson (delusions@gmail.com)
            * @date October 31, 2006
            * @version 1.0
            */
            /*
            * Headers
            */
            var $headers;
            /*
            * User Agent
            */
            var $user_agent;
            /*
            * Compression
            */
            var $compression;
            /*
            * Cookie File
            */
            var $cookie_file;
            /*
            * Proxy Server
            * ip:port
            */
            var $proxy;
            /*
            * FTP Support
            * Username, Password & Host
            */
            var $ftp_username;
            var $ftp_password;
            var $ftp_host;
            /*
            * Initiate the class
            */
            function cURL($cookies=TRUE,$cookie='cookies.txt',$compression='gzip',$proxy='') {
                    $this->error_check();
                    $this->headers[] = "Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg";
                    $this->headers[] = "Connection: Keep-Alive";
                    $this->headers[] = "Content-type: application/x-www-form-urlencoded";
                    $this->user_agent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)";
                    $this->compression=$compression;
                    $this->proxy=$proxy;
                    $this->cookies=$cookies;
                    if ($this->cookies == TRUE) $this->cookie($cookie);
                    /* EDIT FOR FTP HERE */
                    $this->ftp_username = '';
                    $this->ftp_password = '';
                    $this->ftp_host = 'ftp.yoursite.com';
                    /* END EDIT */
            }
            /*
            * Error Checks
            */
            function error_check() {
                    if (!function_exists('curl_setopt')) $this->Error('You do not hve the <b>cURL</b> package compiled with PHP. Please contact your host.');
                    if (!function_exists('ftp_connect')) $this->Error('You do not have the <b>FTP</b> package compiled with PHP. Please contact your host.');
            }
            /*
            * Tests the Cookie File
            */
            function cookie($cookie_file) {
                    if (file_exists($cookie_file)) {
                            $this->cookie_file=$cookie_file;
                    } else {
                            @$this->ftp_chmod($_SERVER['DOCUMENT_ROOT']) or $this->error('Error with FTP');
                            @fopen($cookie_file,'w') or $this->error("The cookie file could not be opened. Make sure this directory has the correct permissions");
                            $this->cookie_file=$cookie_file;
                            @fclose($cookie_file);
                    }
            }
            /*
            * Runs a GET through cURL
            */
            function get($url,$refer='') {
                    $process = curl_init($url);
                    curl_setopt($process, CURLOPT_REFERER, $refer);
                    curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);
                    curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
                    if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
                    if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
                    curl_setopt($process,CURLOPT_ENCODING , $this->compression);
                    curl_setopt($process, CURLOPT_TIMEOUT, 30);
                    if ($this->proxy) curl_setopt($cUrl, CURLOPT_PROXY, 'proxy_ip:proxy_port');
                    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
                    $return = curl_exec($process);
                    curl_close($process);
                    return $return;
            }
            /*
            * Runs a POST through cURL
            */
            function post($url,$data,$refer) {
                    $process = curl_init($url);
                    curl_setopt($process, CURLOPT_REFERER, $refer);
                    curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);
                    curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
                    if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
                    if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
                    curl_setopt($process, CURLOPT_ENCODING , $this->compression);
                    curl_setopt($process, CURLOPT_TIMEOUT, 30);
                    if ($this->proxy) curl_setopt($cUrl, CURLOPT_PROXY, 'proxy_ip:proxy_port');
                    curl_setopt($process, CURLOPT_POSTFIELDS, $data);
                    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
                    curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
                    curl_setopt($process, CURLOPT_POST, 1);
                    $return = curl_exec($process);
                    curl_close($process);
                    return $return;
            }
            /*
            * FTP Chmod Script
            * Used if chmod isn't allowed
            */
            function ftp_chmod($path,$mod) {
                    $ftp_location = str_replace($path);
                    // set up basic connection
                    $ftp = ftp_connect($this->ftp_server);
    
                    // login with username and password
                    $login_result = ftp_login($ftp, $this->ftp_username, $this->ftp_password);
    
                    // try to chmod $path directory
                    if (ftp_site($ftp, 'CHMOD '.$mod.' '.$ftp_location) !== false) {
                    $success=TRUE;
                    }
                    else {
                    $success=FALSE;
                    }
    
                    // close the connection
                    ftp_close($ftp);
                    return $success;
            }
            /*
            * Error Output
            */
            function error($error) {
                    echo "<center><div style='width:500px;border: 3px solid #FFEEFF; padding: 3px; background-color: #FFDDFF;font-family: verdana; font-size: 10px'><b>cURL Error</b><br>$error</div></center>";
                    die;
            }
    }
    
    ?> 
    
    Code (markup):
    Parse/Write script:
    
    <?php
    include("functions.php");
    $w = new cURL();
    $website = "http://www.google.com"; //Change this to the website that has the iframe code.
    $strHTML = $w->get($website);
    
    $strRes = GSB($strHTML, "<iframe id=\"apppanel_104237_canvas\" src=\"","\" width=\"800\" height=\"600\" scrolling=\"no\" frameborder=\"0\" style=\"border: none; background:transparent;\"></iframe>");
    
    $file = "result.txt"; //Change this to the file you want it to write to.
    $f = fopen($file, "a");
    fwrite($f, $strRes);
    fclose($f);
    ?>
    
    Code (markup):
    Tell me how that works for ya :)
     
    CPURules, Apr 20, 2008 IP
  3. dopex

    dopex Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    wow, fantastic man.
    i've yet to try it but one question.
    as a php programmer, as i assume you are one.
    do you just write it from scratch and it all comes naturally to you or what.

    /edit
    how could i just make it so that it parses it when I hit a submit button.
    (figured it out)

    /edit2
    i figured out how to make it write on a new line so that was teh pown.
    theres one problem. the script works great. you can check it here.
    www.xdope.info/truthbox2
    and the results will be at www.xdope.info/truthbox2/result.txt

    sadly, i just realized something and i am now uber sad. but oh well, there goes my hope down the drain.
    when it parses it, i just realized that it parses it as if it was the server and not as the client.

    if that makes any sense.

    meaning, if i were to edit the script to parse for the Welcome, <username> @ the top right, if I were to parse it it would say not logged in.

    sorry if im not making any sense.

    is there a way... for example to edit the script you have given me to parse it for who evers username is logged in?

    like if you check the website www.xdope.info/truthbox2 , and you were to hit the button PWN!

    in the result file, it would have your username (if we were to make it for the welcome username scenario)

    and if I were to hit it, it would be mine, dopex.

    i think what i'm saying is probably a long shot, but meh. darn. thanks in advance :D
     
    dopex, Apr 20, 2008 IP
  4. CPURules

    CPURules Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The cURL class isn't mine. The other one is. It just kinda comes naturally to me xD

    If the site requires logging in, I could write a login for you(assuming there were no CAPTCHA's and I could borrow an account[so I know what I need to send through the form])

    But yeah, I could make it say (username): (Url here)
     
    CPURules, Apr 21, 2008 IP
  5. dopex

    dopex Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    do you need to have a login?
    cause when i have people hit the button, they're not supposed to know. =/

    /edit
    like is there a way to just click on pwn without having to login or anything? basically, the way for it to work is the person needs to be logged on to MySpace for it to work. If the person has to login for it to get their username (if we were doing that scenario) then thats not really good.
     
    dopex, Apr 22, 2008 IP
  6. dopex

    dopex Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Bring
    Up
    My
    Post :D
     
    dopex, Apr 24, 2008 IP
  7. dopex

    dopex Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    help anyone?
     
    dopex, Apr 29, 2008 IP
  8. CPURules

    CPURules Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    If they are already logged in, then it tries to grab it?

    That can't be done, sorry.. I can't reference cookies that are stored on the user's computer(I can if they're local domain, and since I can't change myspace's coding for that, the cookies can't be retrieved)
     
    CPURules, May 1, 2008 IP
  9. crazyfalove00

    crazyfalove00 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    this code is for the myspace truth box right? could u tell me where to put the code to find out who is writing to me please?
     
    crazyfalove00, May 28, 2008 IP
  10. crazyfalove00

    crazyfalove00 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    can u help me; is that the code to find out who is writing to me in the truth box in myspace? if it is can you tell me where to put it and all that ?
     
    crazyfalove00, May 28, 2008 IP