File Manipulation Code
  Home arrow File Manipulation Code arrow Wedit
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
FILE MANIPULATION CODE

Wedit
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2005-03-31

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    //
    // View Source -> Edit Page -> Preview -> Save Page ... All online!!!!

    // Readme file
    //Wedit is a Web based HTML page editor. Html files over a http server or on the machine where Wedit is installed can be edited.
    //
    //Http page : html page on a http server can be accessed by entering the http://servername/path/filename.html
    //
    //Local page: The documentroot is assumed as /var/www/html. So any files (tested for PHP/html file) inside /var/www/html on the server where Wedit is installed can be opened, Edited, Previewd and Saved.
    //
    //Save page : For Saving page thats on a HTTP server, user's Login and password has to be provided. Wedit saves files on HTTP servers using ftp (file transfer protocol)
    //
    //Preview page : HTML/PHP page can be viewed by just the click of a button. To preview a PHP page after making changes, first save the file and then Preview.


    By : aksha

    <?php
    //
    // View Source -> Edit Page -> Preview -> Save Page ... All online!!!!
    // file that opens a HTML/PHP file for editing and view preview of Web pages
    // right now only supports editing of HTML files over http
    // To edit and preview PHP files, Wedit has to be in the server where PHP files are persent
    //

    //1. System requirements
    //2. Installation
    //
    //1. System Requirements
    // Any system which has PHP and a Web server installed.
    // ftp server on the system from where the page is opened and has to be uploaded.
    // Test Broweres - IE, Mozilla
    //
    //2. Installation
    // i) Untar Wedit.tar.gz
    // ii) Copy Wedit directory into /var/www/html (DocumentRoot directory of the Web Server).
    // iii) File thats opened should have 777 permission
    // iv) set "magic_quotes_gpc = off;" in php.ini and restart Web server
    //
    //Open the page http://Wedit-server/Wedit/Wedit.php and start working


    // Readme file
    //Wedit is a Web based HTML page editor. Html files over a http server or on the machine where Wedit is installed can be edited.
    //
    //Http page : html page on a http server can be accessed by entering the http://servername/path/filename.html
    //
    //Local page: The documentroot is assumed as /var/www/html. So any files (tested for PHP/html file) inside /var/www/html on the server where Wedit is installed can be opened, Edited, Previewd and Saved.
    //
    //Save page : For Saving page thats on a HTTP server, user's Login and password has to be provided. Wedit saves files on HTTP servers using ftp (file transfer protocol)
    //
    //Preview page : HTML/PHP page can be viewed by just the click of a button. To preview a PHP page after making changes, first save the file and then Preview.



    // CONDITIONS
    // file thats opened should have 777 permission
    // set "magic_quotes_gpc = off;" in php.ini and restart Web server
    // only files over HTTP can be edited
    // To edit files over HTTPS, Wedit must be installed on the server only
    // PHP files can be previewed only after saving
    // All local files are opened using relative path
    // Assumes DocumentRoot as /var/www/html
    // Does not support HTTPS

    $documentroot = "/var/www/html";

    printf("<html><title>Wedit -- Edit html and PHP pages in Web Browser</title><body>\n\n");
    printf(" <script language=\"javascript\">");
    printf("var showdiv = 0;");
    printf("");
    printf("function previewpage(source,file)");
    printf("{");
    printf(" var previewdiv = document.getElementById(\"previewdiv\");");
    printf(" if ( !showdiv )");
    printf(" {");
    printf(" document.pageform.editbox.style.visibility = \"hidden\"; ");
    printf(" document.pageform.previewbtn.value = \"Show source\"; ");
    printf(" previewdiv.style.visibility = \"visible\"; ");
    printf(" previewdiv.innerHTML = source;");
    printf(" showdiv = 1;");
    printf(" }");
    printf(" else");
    printf(" {");
    printf(" showdiv = 0;");
    printf(" document.pageform.editbox.style.visibility = \"visible\"; ");
    printf(" document.pageform.previewbtn.value = \"Preview\"; ");
    printf(" previewdiv.style.visibility = \"hidden\"; ");
    printf(" }");
    printf("}</script>\n");

    // file to open in browser
    $file = $_POST['path'] ? $_POST['path'] : "http://server/index.html"; // should be selected by user
    $msg = "";


    // Parse the Page path ($file)
    // if the first part is HTTP then set $type=http else local folder
    $bits = explode("/", $file);
    if ($bits[0] == "http:") {
    $type = "http";
    }
    else {
    $type = "local";
    # $openfile= "../" . $file;
    $openfile= $file;
    }

    if ( $type == "http" )
    {
    $ftp_server = $bits[2];

    $filepath = "";
    for($i=3; $i<count($bits); $i++)
    {
    $filepath = $filepath . "/" . $bits[$i];
    $filepath = "temp.html";
    }
    $file = $bits[3] ? $file :($file."/index.html");
    $filepath = $bits[3] ? $filepath : "/index.html";

    $openfile= $file;
    }

    printf(" <table width=\"100%%\"> <tr><td style=\"font-size:13px;font-family:Verdana, Arial; color:#4444ef;\"><b>We (Wedit) </td><td style=\"font-size:13px;font-family:Verdana, Arial; color:#4444ef;text-align:right;\"><b>Edit Web pages online</b>\n");
    printf(" </td></tr><tr><td colspan=2><hr></td></tr></table>\n");

    // page select form
    printf(" <table> <tr><td>\n");

    // page select form
    printf(" <table> <tr><td>\n");
    printf(" <form name=\"pathform\" method=\"post\" action=\"Wedit.php\"> \n");
    printf(" File: <input type=\"text\" name=\"path\" value=\"%s\" size=\"40px\" class=\"textbox\"> \n",$file);
    printf(" <input type=\"button\" name=\"pathsubmit\" value=\"View Source\" class=\"button\" onclick=\"document.pathform.submit();\"> \n");
    printf(" </form> \n");
    printf("</td></tr>\n");

    // Message Block
    printf("</tr><td>\n");
    printf(" <span id=\"msgblock\" name=\"msgblock\" style=\"border:0px solid #cfcfcf;color:#ff0000;font-size:10px;width:300px;\"></span>\n");
    printf("</td></tr></table>\n");

    // Get a file into an array
    $lines = @file($openfile) or send2msgblock("Error: in opening Page");

    if( $lines )
    {
    // Loop through our array, show HTML source as HTML source
    foreach ($lines as $line_num => $line)
    {
    $pagesource = $pagesource . htmlspecialchars($line);
    }
    }


    if ( $_GET['action'] == "save" )
    {
    // save file
    $pagesource = $_POST['editbox'];
    $pagesource = str_replace("\r","",$_POST['editbox']); // remove '\r' that's appended
    // $pagesource = str_replace("\\","",$pagesource); // remove '\r' that's appended

    // Logic
    // 1. Save the file in /tmp on the machine from where Wedit is running
    // 2. Connect to the remote system using ftp, User and password
    // 3. put the file in $DOCUMENTROOT/$PATH

    // Step 2

    $path = $documentroot . "/" . $filepath;

    $ftp_user = $_POST['username'];
    $ftp_pass = $_POST['password'];

    if ($type == "http")
    {
    if ( !$ftp_user || !$ftp_pass )
    {
    send2msgblock("Error: Login and Password have to be entered to Save page on the server");
    }
    else
    {
    // Step 1
    $tempfile = "/tmp/Wedit_pagesource";
    $f = fopen($tempfile,"w") or send2msgblock("Error: Cannot Open file");
    if ($f)
    {
    fwrite($f,$pagesource) or send2msgblock("Error: Cannot save file"); //place $pagesource back in file
    fclose($f);
    }

    // set up a connection or die
    @$conn_id = ftp_connect($ftp_server) or send2msgblock("Error: Cannot connect to $ftp_server");
    // try to login
    if ($conn_id && @ftp_login($conn_id, $ftp_user, $ftp_pass))
    {
    send2msgblock("Connected as $ftp_user@$ftp_server");
    }
    else
    {
    send2msgblock("Error: Cannot connect as $ftp_user");
    }

    // Step 3
    if (@ftp_put($conn_id, $path, $tempfile, FTP_ASCII)) {
    send2msgblock("successfully uploaded $file");
    } else {
    send2msgblock("Error: There was a problem while uploading to $file");
    }

    ftp_close($conn_id);
    }
    } // save local file
    else if ($type == "local")
    {
    // Step 1
    $f = @fopen($openfile,"w") or send2msgblock("Error: Couldn't Open file");
    if ($f)
    {
    @fwrite($f,$pagesource,strlen($pagesource)) or send2msgblock("Error: Couldn't save file"); //place $pagesource back in file
    fclose($f);
    }
    send2msgblock("File is saved on the server");
    }
    }

    // Show the page source in a Text box
    printf("\n <form name=\"pageform\" method=\"post\" action=\"Wedit.php?action=save\"> \n");

    if ($type == "http")
    {
    $sourcevar = "document.pageform.editbox.value";
    }
    else if ($type == "local")
    {
    $localfile = "http://localhost/" . $file ;
    // Get a file into an array.
    $lines = @file($localfile) or send2msgblock("Error in opening Page");

    // Loop through our array, show HTML source as HTML source; and line numbers too.
    if ($lines)
    {
    foreach ($lines as $line_num => $line)
    {
    $localpagesource = $localpagesource . htmlentities($line);
    }
    }

    $sourcevar = "document.pageform.source.value";
    }
    // Hidden fields
    printf(" <input type=\"hidden\" name=\"path\" value=\"%s\" size=\"50px\"> \n",$file);
    printf(" <input type=\"hidden\" name=\"type\" value=\"%s\"> \n",$type);
    printf(" <input type=\"hidden\" name=\"source\" value=\"%s\"> \n",$localpagesource);

    // FTP login and password
    printf(" <table>\n");
    printf(" <tr><td>\n");
    printf(" <b>*</b>Login: <input type=\"text\" name=\"username\" value=\"%s\" size=\"20\" class=\"textbox\"> \n",$ftp_user);
    printf(" <td>\n");
    printf(" <b>*</b>Password: <input type=\"password\" name=\"password\" value=\"%s\" size=\"20\" class=\"textbox\"> \n",$ftp_pass);
    printf(" </tr>\n");
    #printf(" <tr><td><b>*</b>Login and Password of the http server</td></tr>\n");
    printf(" <tr><td>&nbsp;</td></tr>\n");

    // Save and Preview buttons
    printf(" <tr><td>\n");
    printf(" <input type=\"button\" name=\"pagesubmit\" value=\"Save\" class=\"button\" onclick=\"pageform.submit();\"> \n");
    printf("\n <input type=\"button\" name=\"previewbtn\" value=\"Preview\" class=\"button\" onclick=\"previewpage($sourcevar,document.pathform.path.value);\">\n");
    printf(" </tr>\n");

    // Source code
    printf(" <tr><td>\n");
    printf(" <textarea name=\"editbox\" style=\"position:absolute;height:300px;width:500px;\">");
    printf("%s",$pagesource);
    printf("</textarea> \n");

    // Preview Page
    printf(" <div id=\"previewdiv\" style=\"position:absolute;visibility:hidden;height:500px;width:800px;overflow:auto;\">ttt</div> ");
    printf(" </tr>");
    printf(" </form>\n");
    printf("</table>\n");

    printf("<table>\n");
    printf("</table>\n");

    echo "\n</body></html>\n";
    function send2msgblock($showmsg)
    {
    $msg = $msg . $showmsg . "<br>";
    printf("\n<script language=\"javascript\">");
    printf("\nvar msgblock = document.getElementById('msgblock');");
    printf("\n msgblock.innerHTML = msgblock.innerHTML + '$msg'; ");
    printf("\n</script>\n");
    }

    ?>




    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More File Manipulation Code Articles
    More By Codewalkers

     

    IBM® developerWorks developerWorks - FREE Tools!


    Check out the new Jazz space on developerWorks

    <a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts.
    FREE! Go There Now!


    IBM – Taking Web 2.0 to Work

    You'll get answers to many questions and more from David Barnes, Lead Evangelist for IBM Emerging Internet Technologies. David will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve.
    FREE! Go There Now!


    NEW! Driving Business Success with Rational Process Library

    Join this webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance.
    FREE! Go There Now!


    NEW! Download IBM WebSphere Portal V6.1 beta code

    Download the IBM WebSphere Portal V6.1 beta code and learn more about the rich features and enhancements in IBM WebSphere Portal V6.1. WebSphere Portal provides a composite application or business mashup framework and the advanced tooling needed to build flexible, SOA-based solutions, and scalability to meet the needs of any size organization.
    FREE! Go There Now!


    NEW! Trial download: IBM Lotus Forms V3.0

    Get a free trial download of IBM Lotus Forms V3.0 (formerly Workplace Forms), which provides a zero-footprint eForms solution to help you automate and move forms-based business processes off the desktop and onto the Web. With Lotus Forms, you can extend applications beyond the firewall by creating a single electronic form document ready for use in both thick and Web 2.0 thin client format.
    FREE! Go There Now!


    NEW! Try the IBM SOA Sandbox for People

    Visit IBM developerWorks to try the IBM SOA Sandbox for people. The SOA Sandbox for people provides a trial environment with the necessary tooling and components required to enable consistent human and process interaction and collaboration, showing how you can improve user experience and business productivity.
    FREE! Go There Now!


    NEW! Webcast: Calling All Testers! Find Application Vulnerabilities Early in the Development Process Where they are Easier to Fix and Less Risky to your Business

    In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information.
    FREE! Go There Now!


    NEW! Webcast: IBM Rational Build Forge - Beyond the Build

    The discipline of assembling and delivering software is maturing beyond standard developer-centric compile/test software builds. The end-to-end software development lifecycle is emerging as the new focus moves “Beyond the Build.” Join this on demand webcast to learn about methods for streamlining software delivery and key capabilities of the IBM Rational Build Forge framework for automating build and release management in environments of any size.
    FREE! Go There Now!


    NEW! Webcast: Introducing the new Information Server and Solutions community: LeverageInformation

    User communities play an important role in communication and collaboration around products, solutions and other areas of special interest to members. Successful communities are able to provide the right mix of content and services to deliver a value proposition that resonates with each audience. Join Tom Inman, VP of Marketing for Information and Platform Solutions as he introduces the new LeverageINFORMATION community. During this webcast, learn about the value provided by the community and how customers and partners derive value from the community in addressing their own technical and business challenges.
    FREE! Go There Now!


    NEW! Webcast: Quickly provide customized, integrated user interfaces with Lotus Notes 8

    IBM Lotus Notes 8 provides a wide range of developers the ability to provide customized, integrated user interfaces via composite applications and via custom sidebar and toolbar plug-ins. This webcast provides you with tips and techniques to use with out-of-the-box capabilities of Lotus Notes 8, and survey how you can share useful components within your own company and within a larger community.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    FILE MANIPULATION CODE ARTICLES

    - Bandwidth Control with pure PHP
    - Eazy Gallery
    - file_get_contents for PHP < 4.3.0
    - PHP Class: Image Snapshot 1.3
    - Universal downloader
    - Image Gallery v2.0
    - Free/Used Disk Space
    - Directory Lister
    - Directory image view, with selective hidden
    - Move or Copy a Directory (and files and sub ...
    - Ensure_Sub_Directory_Exists
    - Wedit
    - Form Examples Text Boxes to Drop Downs
    - myFiles
    - List files in a directory, no subdirectories





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek