Welcome Guest, Not a member yet? Register   Sign In
Alpha version of self-updating controller
#1

[eluser]Unknown[/eluser]
Hello,
I have developed an auto updater for my own needs, and wanted to share it with you.

What it does ?
If you have a web application and it's stored on many servers, you can put one to be the master and then the slave ones will update from this one.

For now it only works with one area, the controllers.

you have to add in config.php
Code:
$config['update_path']='';//url to the update controller
$config['update_ignore']=array('index.html');//files to ignore when updating
$config['update_zones']=array('controllers');//soon...

the controller, which is very basic
update.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Update extends CI_Controller
{

function index()
{
  $this->load->library('Curl');
  $this->load->helper('directory');
  $URL=$this->config->item('update_path');
  $remote=explode("\n",$this->curl->simple_get($URL.'check_versions'));
  //print_r($remote);
  $local= directory_map('./application/controllers/');
  $needs_update='';
  $sha_files='';
  
  foreach ($remote as $ritem){
   $data=explode('|',$ritem);
   foreach ($local as $item){
    if ($data[0]==$item){
    //a gasit fisierul local
    if (sha1_file('./application/controllers/'.$item)<>$data[1]){
      $needs_update.=$item."|";
      $sha_files.=$data[1]."|";
     }
    }
   }
  }
  
  
  
  foreach ($local as $item){
   foreach ($remote as $ritem){
    $data=explode('|',$ritem);
    if ($data[0]==$item){
     //a gasit hash-ul remote pt fisierul curent
     if (sha1_file('./application/controllers/'.$item)<>$data[1]){
      $needs_update.=$item."|";
      $sha_files.=$data[1]."|";
     }
    }
   }
  // echo $item."|".sha1_file('./application/controllers/'.$item)."\n";
  }
  if (strlen($needs_update)>0){
   //proceed with update
   $update=explode("|",$needs_update);
   $update_check=explode("|",$sha_files);
   for ($i = 0; $i < count($update)-1; $i++) {
    //descarca fiecare fisier
    $remote_file=$this->curl->simple_get($URL.'get_file/'.$update[$i]);
    if ($remote_file<>'-1'){
     $this->load->helper('file');
     write_file('./application/controllers/temp.'.$i, $remote_file);
     if (sha1_file('./application/controllers/temp.'.$i)==$update_check[$i]){
      write_file('./application/controllers/'.$update[$i], $remote_file);
      unlink('./application/controllers/temp.'.$i);
      echo 'Updated '.$update[$i];
     }else{
      echo 'Incomplete '.$update[$i];
     }
    }else{
     //ignored this file
    }
   }
   echo 'All updated';
  }else{
   echo 'All ok!';
  }
}

function check_versions(){
  $this->load->helper('directory');
  $this->output->set_header("Content-Type: text/plain");
  $files = directory_map('./application/controllers/');
  foreach ($files as $item){
   echo $item."|".sha1_file('./application/controllers/'.$item)."\n";
  }
}
function get_file($name){
  $this->output->set_header("Content-Type: text/plain");
  if (in_array($name,$this->config->item('update_ignore'))){
   echo "-1";
  }else{
   $this->load->helper('download');
   $data = file_get_contents('./application/controllers/'.$name);
   echo $data;
  }
}
}


Messages In This Thread
Alpha version of self-updating controller - by El Forum - 09-07-2012, 06:12 AM



Theme © iAndrew 2016 - Forum software by © MyBB