Simple Groupware PHP Cross Reference Groupware Applications

Source: /src/extensions.php - 298 lines - 12673 bytes - Summary - Text - Print

   1  <?php
   2      /**************************************************************************\
   3      * Simple Groupware 0.743                                                   *
   4      * http://www.simple-groupware.de                                           *
   5      * Copyright (C) 2002-2012 by Thomas Bley                                   *
   6      * ------------------------------------------------------------------------ *
   7      *  This program is free software; you can redistribute it and/or           *
   8      *  modify it under the terms of the GNU General Public License Version 2   *
   9      *  as published by the Free Software Foundation; only version 2            *
  10      *  of the License, no later version.                                       *
  11      *                                                                          *
  12      *  This program is distributed in the hope that it will be useful,         *
  13      *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
  14      *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
  15      *  GNU General Public License for more details.                            *
  16      *                                                                          *
  17      *  You should have received a copy of the GNU General Public License       *
  18      *  Version 2 along with this program; if not, write to the Free Software   *
  19      *  Foundation, Inc., 59 Temple Place - Suite 330, Boston,                  *
  20      *  MA  02111-1307, USA.                                                    *
  21      \**************************************************************************/
  22  
  23  define("NOCONTENT",true);
  24  
  25  require ("index.php");
  26  require ("lib/tar/Tar.php");
  27  @set_time_limit(600);
  28  
  29  if (!sys_is_super_admin($_SESSION["username"])) sys_die("{t}Not allowed. Please log in as super administrator.{/t}");
  30  
  31  setup::out('
  32  <html>
  33  <head>
  34  <title>Simple Groupware</title>
  35  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  36  <style>
  37    body, h2, img, div, a {
  38      background-color: #FFFFFF; color: #666666; font-size: 13px; font-family: Arial, Helvetica, Verdana, sans-serif;
  39    }
  40    a, input { color: #0000FF; }
  41    .border { border-bottom: 1px solid black; }
  42    .headline {
  43      letter-spacing: 2px;
  44      font-size: 18px;
  45      font-weight: bold;
  46    }
  47    .description {
  48      background-color:#EFEFEF;
  49      padding:10px;
  50    }
  51    input {
  52      font-size: 11px; background-color: #F5F5F5; border: 1px solid #AAAAAA; height: 18px;
  53      vertical-align: middle; padding-left: 5px; padding-right: 5px; border-radius: 10px;
  54    }
  55    .submit { color: #0000FF; background-color: #FFFFFF; width: 125px; font-weight: bold; }
  56  </style>
  57  <script>
  58  function showhide(obj) {
  59    obj = document.getElementById(obj);
  60    if (obj.style.display=="none") {
  61      obj.style.display="";
  62    } else {
  63      obj.style.display="none";
  64    }
  65    return false;
  66  }
  67  </script>
  68  </head>
  69  <body>
  70  <div class="border headline">Simple Groupware '.CORE_VERSION_STRING.' {t}Extensions{/t}</div>
  71  ');
  72  setup::out("<a href='index.php'>{t}Back{/t}</a><br>");
  73  
  74  foreach (array("../old/", SIMPLE_EXT) as $folder) {
  75    if (!is_writable($folder)) setup::out_exit(sprintf("[1] {t}Please give write access to %s{/t}",$folder));
  76  }
  77  
  78  if ((empty($_REQUEST["extension"]) and empty($_REQUEST["uninstall"]) and empty($_REQUEST["cfile"])) or !sys_validate_token()) {
  79    setup::out("
  80      <div style='color:#ff0000;'>
  81      <b>{t}Warning{/t}</b>:<br>
  82      - Please make a complete backup of your database (e.g. using phpMyAdmin)<br>
  83      - Please make a complete backup of your sgs folder (e.g. /var/www/htdocs/sgs/)<br>
  84      - Make sure both backups are complete!
  85      </div>
  86    ");
  87    setup::out("{t}Downloading extension list{/t} ...<br>");
  88  
  89    $url = "http://sourceforge.net/projects/simplgroup/files/simplegroupware_modules/modules.xml";
  90    if (!($data = sys_cache_get("modules.xml"))) {
  91      $data = @file_get_contents($url);
  92      sys_cache_set("modules.xml", $data, 3600);
  93    }
  94    if (($xml = @simplexml_load_string($data))) {
  95      foreach ($xml as $package) {
  96        $php_version = (string)$package->php_version;
  97        $sgs_version = (string)$package->require_version;
  98  
  99        $target = SIMPLE_EXT.substr(basename($package->filename),0,-3);
 100        if (file_exists($target)) continue;
 101        $id = md5($package->filename);
 102        
 103        if (version_compare(PHP_VERSION, $php_version, "<")) {
 104          setup::out(sprintf("{t}Setup needs php with at least version %s !{/t} ", $php_version), false);
 105        } else if (version_compare(CORE_VERSION_STRING, $sgs_version, "<")) {
 106          setup::out(sprintf("{t}Setup needs Simple Groupware with at least version %s !{/t} ", $sgs_version), false);
 107        } else {
 108          setup::out("<a href='extensions.php?token=".modify::get_form_token()."&extension=".$package->name."&filename=".$package->filename."'>{t}I n s t a l l{/t}</a> ", false);
 109        }
 110        setup::out($package->title." <a href='#' onclick='return showhide(\"".$id."\")'>{t}Info{/t}</a>", false);
 111        setup::out("<br><div class='description' style='display:none;' id='".$id."'>".nl2br(trim($package->description))."</div>");
 112      }
 113    } else {
 114      setup::out(sprintf("{t}Connection error: %s [%s]{/t}", $url, "HTTP")."<br>".strip_tags($data, "<br><p><h1><center>"));
 115    }
 116    
 117    setup::out("{t}Package from local file system (.tar.gz){/t}:<br/>{t}current path{/t}: ".str_replace("\\","/",getcwd())."/<br/>");
 118  
 119    $dir = opendir("./");
 120    while (($file=readdir($dir))) {
 121      if ($file!="." and $file!=".." and preg_match("|^SimpleGroupware\_.*?.tar\.gz\$|i",$file)) {
 122        setup::out("<a href='extensions.php?token=".modify::get_form_token()."&cfile=".$file."'>{t}I n s t a l l{/t}</a>&nbsp; ".$file."<br/>");
 123      }
 124    }
 125    closedir($dir);
 126    setup::out("<form method='POST'><input type='hidden' name='token' value='".modify::get_form_token()."'><input type='text' name='cfile' value='/tmp/SimpleGroupware_SomeExtension_0.x.tar.gz' style='width:300px;'>&nbsp;<input type='submit' class='submit' value='{t}I n s t a l l{/t}'><br>");
 127    
 128    $can_uninstall = false;
 129    foreach (scandir(SIMPLE_EXT) as $file) {
 130      if ($file[0]=="." or !is_dir(SIMPLE_EXT.$file) or !file_exists(SIMPLE_EXT.$file."/package.xml")) continue;
 131  
 132      $package = simplexml_load_file(SIMPLE_EXT.$file."/package.xml");
 133      $id = md5($package->filename);
 134  
 135      setup::out("<a onclick='if (!confirm(\"{t}Really uninstall the module ?{/t}\")) return false;' href='extensions.php?token=".modify::get_form_token()."&uninstall=".$package->filename."'>{t}U n i n s t a l l{/t}</a> ".$package->title, false);
 136      setup::out(" <a href='#' onclick='return showhide(\"".$id."\")'>{t}Info{/t}</a>", false);
 137      setup::out(" ({t}installed{/t} ".sys_date("{t}m/d/Y{/t}", filemtime(SIMPLE_EXT.$file)).")");
 138      setup::out("<div class='description' style='display:none;' id='".$id."'>".nl2br(trim($package->description))."</div>");
 139      $can_uninstall = true;
 140    }
 141    if ($can_uninstall) setup::out("<b>{t}Note{/t}:</b> {t}Uninstall does not delete any data in the database.{/t}<br>");
 142    setup::out_exit('<div style="border-top: 1px solid black;">Powered by Simple Groupware, Copyright (C) 2002-2012 by Thomas Bley.</div></div></body></html>');
 143  }
 144  
 145  if (!empty($_REQUEST["uninstall"])) {
 146    extensions::uninstall(substr($_REQUEST["uninstall"],0,-3));
 147    extensions::update_modules_list();
 148    setup::out("<br><a href='extensions.php'>{t}C O N T I N U E{/t}</a>");
 149    setup::out_exit('<br><div style="border-top: 1px solid black;">Powered by Simple Groupware, Copyright (C) 2002-2012 by Thomas Bley.</div></div></body></html>');
 150  }
 151  
 152  if (!empty($_REQUEST["cfile"])) {
 153    $source = $_REQUEST["cfile"];
 154    $filename = basename($source);
 155    if (!file_exists($source) or filesize($source)==0) sys_die("{t}Error{/t}: file-check [0] ".$source);
 156  
 157    // TODO optimize
 158    $tar_object = new Archive_Tar($source);
 159    $tar_object->setErrorHandling(PEAR_ERROR_PRINT);
 160    $file_list = $tar_object->ListContent();
 161  
 162    if (!is_array($file_list) or empty($file_list[0]["filename"])) {
 163      sys_die("{t}Error{/t}: file-check [0b] ".$source);
 164    }
 165    $extension = dirname($file_list[0]["filename"]);
 166  } else if (!empty($_REQUEST["filename"]) and !empty($_REQUEST["extension"])) {
 167    $filename = $_REQUEST["filename"];
 168    $extension = $_REQUEST["extension"];
 169    $source = "http://sourceforge.net/projects/simplgroup/files/simplegroupware_modules/".$extension."/".$filename."/download";
 170  } else {
 171    sys_die("Missing parameters.");
 172  }
 173  if (file_exists(SIMPLE_EXT.$extension."/package.xml")) {
 174    $xml = simplexml_load_file(SIMPLE_EXT.$extension."/package.xml");
 175    extensions::uninstall(substr($xml->filename,0,-3));
 176    setup::out("<hr>");
 177  }
 178  extensions::install($source, $filename);
 179  
 180  setup::out("<br><a href='extensions.php'>{t}C O N T I N U E{/t}</a>");
 181  setup::out('<br><div style="border-top: 1px solid black;">Powered by Simple Groupware, Copyright (C) 2002-2012 by Thomas Bley.</div></div></body></html>');
 182  
 183  abstract class extensions {
 184    static function uninstall($filename) {
 185      $source = SIMPLE_EXT.basename($filename);
 186      setup::out(sprintf("{t}Processing %s ...{/t}",basename($source)));
 187  
 188      $tar_object = new Archive_Tar($source);
 189      $tar_object->setErrorHandling(PEAR_ERROR_PRINT);
 190  
 191      $file_list = $tar_object->ListContent();
 192      if (!is_array($file_list) or !isset($file_list[0]["filename"])) {
 193        sys_die("{t}Error{/t}: tar [1] ".$source);
 194      }
 195      $base = "../old/".basename($source)."/";
 196      foreach ($file_list as $file) {
 197        if (is_file(SIMPLE_EXT.$file["filename"])) {
 198          sys_mkdir(dirname($base.$file["filename"]));
 199          rename(SIMPLE_EXT.$file["filename"],$base.$file["filename"]);
 200        
 201          if (basename($file["filename"])=="uninstall.php") {
 202            setup::out("");
 203            require($base.$file["filename"]);
 204          }
 205        }
 206        @rmdir(SIMPLE_EXT.dirname($file["filename"]));
 207      }
 208      rename($source,$base.basename($source));
 209    }
 210  
 211    static function install($source, $filename) {
 212      $temp_folder = SIMPLE_CACHE."/extensions/".md5($source)."/";
 213      sys_mkdir($temp_folder);
 214      $target = $temp_folder.substr($filename,0,-3);
 215  
 216      setup::out("{t}Download{/t}: ".$source." ...");
 217      if ($fz = gzopen($source,"r") and $fp = fopen($target,"w")) {
 218        $i = 0;
 219        while (!gzeof($fz)) {
 220          $i++;
 221          setup::out(".",false);
 222          if ($i%160==0) setup::out();
 223          fwrite($fp,gzread($fz, 16384));
 224        }
 225        gzclose($fz);
 226        fclose($fp);
 227      } else sys_die("{t}Error{/t}: gzopen [2] ".$source);
 228  
 229      setup::out();
 230      if (!file_exists($target) or filesize($target)==0 or filesize($target)%10240!=0) {
 231        sys_die("{t}Error{/t}: file-check [3] Filesize: ".filesize($target)." ".$target);
 232      }
 233      setup::out(sprintf("{t}Processing %s ...{/t}",basename($target)));
 234  
 235      $tar_object = new Archive_Tar($target);
 236      $tar_object->setErrorHandling(PEAR_ERROR_PRINT);
 237      $tar_object->extract($temp_folder);
 238  
 239      $file_list = $tar_object->ListContent();
 240      if (!is_array($file_list) or !isset($file_list[0]["filename"]) or !is_dir($temp_folder.$file_list[0]["filename"])) {
 241        sys_die("{t}Error{/t}: tar [4] ".$target);
 242      }
 243      setup::build_trans(SETUP_LANGUAGE, $temp_folder, SIMPLE_EXT);
 244      extensions::update_modules_list();
 245  
 246      $ext_folder = db_select_value("simple_sys_tree","id","anchor=@anchor@",array("anchor"=>"extensions"));
 247  
 248      foreach ($file_list as $file) {
 249        sys_chmod(SIMPLE_EXT.$file["filename"]);
 250        setup::out(sprintf("{t}Processing %s ...{/t}",SIMPLE_EXT.$file["filename"]));
 251        
 252        if (basename($file["filename"])=="install.php") {
 253          setup::out("");
 254          require(SIMPLE_EXT.$file["filename"]);
 255          setup::out("");
 256        }
 257        if (basename($file["filename"])=="readme.txt") {
 258          $data = file_get_contents(SIMPLE_EXT.$file["filename"]);
 259          setup::out(nl2br("\n".modify::htmlquote($data)."\n"));
 260        }
 261        if (!empty($ext_folder) and basename($file["filename"])=="folders.xml") {
 262          setup::out(sprintf("{t}Processing %s ...{/t}","folder structure"));
 263          folders::create_default_folders(SIMPLE_EXT.$file["filename"],$ext_folder,false);
 264        }
 265      }
 266      dirs_delete_all($temp_folder);
 267    }
 268    
 269    function update_modules_list() {
 270      setup::out(sprintf("<br>{t}Processing %s ...{/t}",SIMPLE_EXT."*/modules.txt"));
 271      $data = "";
 272      foreach (scandir(SIMPLE_EXT) as $file) {
 273        if ($file[0]=="." or !is_dir(SIMPLE_EXT.$file)) continue;
 274        if (!file_exists(SIMPLE_EXT.$file."/modules.txt")) continue;
 275        $data .= trim(file_get_contents(SIMPLE_EXT.$file."/modules.txt"))."\n";
 276      }
 277      $target = SIMPLE_EXT."modules/schema/modules_ext.txt";
 278      @unlink($target);
 279      if ($data!="") {
 280        sys_mkdir(dirname($target)); 
 281        file_put_contents($target, $data, LOCK_EX);
 282      }
 283      
 284      setup::out(sprintf("{t}Processing %s ...{/t}",SIMPLE_EXT."*/sys_modules.txt"));
 285      $data = "";
 286      foreach (scandir(SIMPLE_EXT) as $file) {
 287        if ($file[0]=="." or !is_dir(SIMPLE_EXT.$file)) continue;
 288        if (!file_exists(SIMPLE_EXT.$file."/sys_modules.txt")) continue;
 289        $data .= trim(file_get_contents(SIMPLE_EXT.$file."/sys_modules.txt"))."\n";
 290      }
 291      $target = SIMPLE_EXT."modules/schema_sys/modules_ext.txt";
 292      @unlink($target);
 293      if ($data!="") {
 294        sys_mkdir(dirname($target));
 295        file_put_contents($target, $data, LOCK_EX);
 296      }
 297    }
 298  }

title

Description

title

Description

title

Description

title

title

Body