Results 1 to 6 of 6

Thread: Offline Pages

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default Offline Pages

    Does anyone know of a pre-existing code to pull a pages code, all resources of that page (images, javascript, css, etc.), and copy them to another directory and rewrite the references so the are pulled from the new directory or at least so the absolute ones become relative? If not I'm planning on making this with filegetcontents, mkdir(create the dir to hold the file/files), preg_replace(find all original resources, make relative), fileputcontents(put source of original page), and copy(copy resources to the new dir). Is there anything I'm missing here? This is all on the same server so security wont be an issue (or can be corrected if it is). Thanks.
    Corrections to my coding/thoughts welcome.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Nope, but that seems like a useful tool. Let me know if you need help conceptually.

    As a general note, remember that HTML tags don't have a required order, so you could have <img src=....> or <img ....src=...> etc.

    It seems fairly straightforward to do this (not easy, but systematic), with <img> <script> <link>, etc. But there's a problem: some resources are referenced within external .js or .css files. And that would be more difficult. For example, many scripts on DD would be missing media if you attempted this.

    And that's not even thinking about iframes.

    Note that there are some other embedded items like flash (or other plugins) that you might not immediately think of but could be important.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    bluewalrus (04-10-2011)

  4. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Thanks, I hadn't thought of calls inside of the resources will have to work that out as well. Do you know how I can send a variable defined elsewhere with the preg_replace_callback?

    PHP Code:
    $css preg_replace_callback("/<link.*?href=\"(.*?)\".*/i""filtersource($domain)"$source);

    function 
    filtersource($location$domain) {

    This fails

    Warning: preg_replace_callback() [function.preg-replace-callback]: Requires argument 2, 'filtersource(http://www.jove.com/)', to be a valid callback in /public_html/a.php on line 14
    Corrections to my coding/thoughts welcome.

  5. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I don't think you can do that. Maybe there is some way to use a class to make that work? Or a global variable...

    Maybe this:
    PHP Code:
    $class = new ClassObj;
    $class->myvar 'myval';

    $css preg_replace_callback("/<link.*?href=\"(.*?)\".*/i""filtersource()"$source);

    function 
    filtersource($location) {

        
    ///this:
        
    $myvar $GLOBALS['class']->myvar;


    There should be a better way, but I'm not sure what it is...


    By the way, if you're looking for more information on the topic, you're basically doing the same thing as a proxy, attempting to take an HTML file and retrieve all necessary secondary files and transmit (in this case, save) them.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. The Following User Says Thank You to djr33 For This Useful Post:

    bluewalrus (04-14-2011)

  7. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    There's a getRelativePath function submitted by a user to php.net:

    http://www.php.net/manual/en/functio...path.php#97885

    on the realpath page.

    I've used it and it worked for what I wanted to do with it. It probably could be used/adapted to your purposes,
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. The Following User Says Thank You to jscheuer1 For This Useful Post:

    bluewalrus (04-14-2011)

  9. #6
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Quote Originally Posted by bluewalrus View Post
    Do you know how I can send a variable defined elsewhere with the preg_replace_callback?

    PHP Code:
    $css preg_replace_callback("/<link.*?href=\"(.*?)\".*/i""filtersource($domain)"$source);

    function 
    filtersource($location$domain) {

    This fails
    Found it on php.net, "To access a local variable within a callback, use currying (delayed argument binding). For example"

    PHP Code:
    <?php
    function curry($func$arity) {
        return 
    create_function(''"
            \$args = func_get_args();
            if(count(\$args) >= 
    $arity)
                return call_user_func_array('
    $func', \$args);
            \$args = var_export(\$args, 1);
            return create_function('','
                \$a = func_get_args();
                \$z = ' . \$args . ';
                \$a = array_merge(\$z,\$a);
                return call_user_func_array(\'
    $func\', \$a);
            ');
        "
    );
    }

    function 
    on_match($transformation$matches)
    {
        return 
    $transformation[strtolower($matches[1])];
    }

    $transform = array('a' => 'Well,''d'=>'whatever''b'=>' ');

    $callback curry(on_match2);
    echo 
    preg_replace_callback('/([a-z])/i'$callback($transform), 'Abcd');

    echo 
    "\n";
    ?>
    Didn't find this when I was at this point though so you can also use the global as djr33 suggests.
    Corrections to my coding/thoughts welcome.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •