Simple PHP Auto-Update System

回覆文章
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

Simple PHP Auto-Update System

文章 yehlu »

http://maxmorgandesign.com/simple_php_a ... te_system/

代碼: 選擇全部

<h1>DYNAMIC UPDATE SYSTEM</h1>
<?
ini_set('max_execution_time',60);
//Check for an update. We have a simple file that has a new release version on each line. (1.00, 1.02, 1.03, etc.)
$getVersions = file_get_contents('http://your-server.com/CMS-UPDATE-PACKAGES/current-release-versions.php') or die ('ERROR');
if ($getVersions != '')
{
    //If we managed to access that file, then lets break up those release versions into an array.
    echo '<p>CURRENT VERSION: '.get_siteInfo('CMS-Version').'</p>';
    echo '<p>Reading Current Releases List</p>';
    $versionList = explode("\\n", $getVersions);    
    foreach ($versionList as $aV)
    {
        if ( $aV > get_siteInfo('CMS-Version')) {
            echo '<p>New Update Found: v'.$aV.'</p>';
            $found = true;
           
            //Download The File If We Do Not Have It
            if ( !is_file(  $_ENV['site']['files']['includes-dir'].'/UPDATES/MMD-CMS-'.$aV.'.zip' )) {
                echo '<p>Downloading New Update</p>';
                $newUpdate = file_get_contents('http://your-server.com/CMS-UPDATE-PACKAGES/MMD-CMS-'.$aV.'.zip');
                if ( !is_dir( $_ENV['site']['files']['includes-dir'].'/UPDATES/' ) ) mkdir ( $_ENV['site']['files']['includes-dir'].'/UPDATES/' );
                $dlHandler = fopen($_ENV['site']['files']['includes-dir'].'/UPDATES/MMD-CMS-'.$aV.'.zip', 'w');
                if ( !fwrite($dlHandler, $newUpdate) ) { echo '<p>Could not save new update. Operation aborted.</p>'; exit(); }
                fclose($dlHandler);
                echo '<p>Update Downloaded And Saved</p>';
            } else echo '<p>Update already downloaded.</p>';    
           
            if ($_GET['doUpdate'] == true) {
                //Open The File And Do Stuff
                $zipHandle = zip_open($_ENV['site']['files']['includes-dir'].'/UPDATES/MMD-CMS-'.$aV.'.zip');
                echo '<ul>';
                while ($aF = zip_read($zipHandle) )
                {
                    $thisFileName = zip_entry_name($aF);
                    $thisFileDir = dirname($thisFileName);
                   
                    //Continue if its not a file
                    if ( substr($thisFileName,-1,1) == '/') continue;
                   
    
                    //Make the directory if we need to...
                    if ( !is_dir ( $_ENV['site']['files']['server-root'].'/'.$thisFileDir ) )
                    {
                         mkdir ( $_ENV['site']['files']['server-root'].'/'.$thisFileDir );
                         echo '<li>Created Directory '.$thisFileDir.'</li>';
                    }
                   
                    //Overwrite the file
                    if ( !is_dir($_ENV['site']['files']['server-root'].'/'.$thisFileName) ) {
                        echo '<li>'.$thisFileName.'...........';
                        $contents = zip_entry_read($aF, zip_entry_filesize($aF));
                        $contents = str_replace("\\r\\n", "\\n", $contents);
                        $updateThis = '';
                       
                        //If we need to run commands, then do it.
                        if ( $thisFileName == 'upgrade.php' )
                        {
                            $upgradeExec = fopen ('upgrade.php','w');
                            fwrite($upgradeExec, $contents);
                            fclose($upgradeExec);
                            include ('upgrade.php');
                            unlink('upgrade.php');
                            echo' EXECUTED</li>';
                        }
                        else
                        {
                            $updateThis = fopen($_ENV['site']['files']['server-root'].'/'.$thisFileName, 'w');
                            fwrite($updateThis, $contents);
                            fclose($updateThis);
                            unset($contents);
                            echo' UPDATED</li>';
                        }
                    }
                }
                echo '</ul>';
                $updated = TRUE;
            }
            else echo '<p>Update ready. <a href="?doUpdate=true">&raquo; Install Now?</a></p>';
            break;
        }
    }
    
    if ($updated == true)
    {
        set_setting('site','CMS',$aV);
        echo '<p class="success">&raquo; CMS Updated to v'.$aV.'</p>';
    }
    else if ($found != true) echo '<p>&raquo; No update is available.</p>';

    
}
else echo '<p>Could not find latest realeases.</p>';
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

Re: Simple PHP Auto-Update System

文章 yehlu »

代碼: 選擇全部

https://gist.github.com/ilya-dev/9955344

代碼: 選擇全部

<?php namespace Acme;

use Illuminate\Filesystem\Filesystem;

class Updater {

    /**
     * The Filesystem instance
     *
     * @var Illuminate\Filesystem\Filesystem
     */
    protected $file;


    /**
     * Full path to the application folder
     *
     * @var string
     */
    protected $app;

    /**
     * Full path to the backup folder
     *
     * @var string
     */
    protected $backup;

    /**
     * The constructor
     *
     * @param  Illuminate\Filesystem\Filesystem $file
     * @param  string $app
     * @return void
     */
    public function __construct(Filesystem $file, $app)
    {
        $this->file = $file;

        $this->app  = $app;
    }

    /**
     * Update the application
     *
     * @param  string  $source
     * @param  string  $backup
     * @return boolean
     */
    public function update($source, $backup)
    {
        $this->backup = $backup;

        // if the process failed, we cannot proceed
        if ( ! $this->backup()) return false;

        // get all files from the source directory (recursively)
        $files = $this->file->allFiles($source);

        foreach ($files as $file)
        {
            $from = $this->path($source, $file); // source path
            $to   = $this->path($this->app, $file); // and destination path

            // attempt to [over]write a file
            // if something goes wrong, we rollback all the changes
            if ( ! $this->write($from, $to))
            {
                $this->rollback();

                return false;
            }
        }

        return true;
    }

    /**
     * Get the backup directory path
     *
     * @return string
     */
    protected function getBackupPath()
    {
        return $this->backup;
    }

    /**
     * Backup the whole application
     *
     * @return boolean
     */
    protected function backup()
    {
        return $this->file->copyDirectory($this->app, $this->getBackupPath());
    }

    /**
     * Rollback all made changes
     *
     * @return boolean
     */
    protected function rollback()
    {
        return $this->file->copyDirectory($this->getBackupPath(), $this->app);
    }

    /**
     * Write to a file
     *
     * @param  string  $from
     * @param  string  $to
     * @return boolean
     */
    protected function write($from, $to)
    {
        $directory = dirname($from);

        $file = $this->file;

        return $file->isWritable($directory) and $file->put($from, $to);
    }

    /**
     * Build a path string
     *
     * @param  dynamic
     * @return string
     */
    protected function path()
    {
        $path = implode('/', func_get_args());

        return str_replace('//', '/', $path);
    }

}
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

Re: Simple PHP Auto-Update System

文章 yehlu »

http://www.hotscripts.com/forums/php/56 ... -help.html

代碼: 選擇全部


<?php

$hostfile = fopen("URL TO UPDATE FILE", 'r');

$fh = fopen("latest.zip", 'w');



while (!feof($hostfile)) {

    $output = fread($hostfile, 8192);

    fwrite($fh, $output);

}

   

fclose($hostfile);

fclose($fh);



  require_once('pclzip.lib.php');

  $archive = new PclZip('latest.zip');

  if (($v_result_list = $archive->extract()) == 0) {

    die("Error : ".$archive->errorInfo(true));

  }

  echo "<pre>";

  echo "</pre>";





unlink('latest.zip');



echo "<meta http-equiv=\"refresh\" content=\"3;url=EXTRA SCRIPT OPTIONS.PHP\" />";

?>

回覆文章

回到「PHP」