Simple Groupware PHP Cross Reference Groupware Applications

Source: /src/console.php - 258 lines - 10300 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  if (!sys_is_super_admin($_SESSION["username"])) sys_die("{t}Not allowed. Please log in as super administrator.{/t}");
  27  
  28  if (empty($_REQUEST["console"])) $_REQUEST["console"] = "sys";
  29  
  30  if (!empty($_GET['func'])) {
  31    if (!function_exists("json_encode")) require ("lib/json/JSON.php");
  32    $result = call_user_func_array(array('funcs', $_GET['func']), explode(',', $_GET['params']));
  33    exit(json_encode($result));
  34  }
  35  
  36  $smarty = new Smarty;
  37  $smarty->compile_dir = SIMPLE_CACHE."/smarty";
  38  $smarty->template_dir = "templates";
  39  $smarty->assign("console", $_REQUEST["console"]);
  40  
  41  $code = "";
  42  $tlimit = 0;
  43  $mlimit = 0;
  44  if (!empty($_REQUEST["code"])) {
  45    if (!sys_validate_token()) sys_die("{t}Invalid security token{/t}");
  46    $code = $_REQUEST["code"];
  47  }
  48  if (!empty($_REQUEST["name"])) {
  49    if (!sys_validate_token()) sys_die("{t}Invalid security token{/t}");
  50    $code = db_select_value("simple_sys_console", "command", "name=@name@", array("name"=>$_REQUEST["name"]));
  51  }
  52  if (!empty($_REQUEST["tlimit"])) {
  53    $tlimit = (int)$_REQUEST["tlimit"];
  54  }
  55  if (!empty($_REQUEST["mlimit"])) {
  56    $mlimit = (int)$_REQUEST["mlimit"];
  57  }
  58  
  59  $smarty->assign("code", $code);
  60  $smarty->assign("tlimit", $tlimit ? $tlimit : "");
  61  $smarty->assign("mlimit", $mlimit ? $mlimit : "");
  62  
  63  if ($tlimit > 0) {
  64    set_time_limit($tlimit);
  65  }
  66  if ($mlimit > 0) {
  67    ini_set("memory_limit", $mlimit."M");
  68  }
  69  
  70  $start = microtime(true);
  71  if ($_REQUEST["console"]=="php") {
  72    $content = "";
  73    if ($code!="") {
  74      ob_start();
  75      eval($code);  
  76      $content = ob_get_contents();
  77      ob_end_clean();
  78    }
  79    $title = "PHP Console (PHP ".phpversion().")";
  80    if ($content!="") $content = "<pre>".modify::htmlquote($content)."</pre>";
  81  } else if ($_REQUEST["console"]=="sys") {
  82    $content = "";
  83    if ($code!="") $content = sys_exec(str_replace("\n","&",trim($code)));
  84    $title = "SYS Console:&nbsp; ".getcwd()." @ ".$_SERVER["SERVER_NAME"]."&nbsp; [".$_SERVER["SERVER_SOFTWARE"]."]";
  85    if ($content!="") $content = "<pre>".modify::htmlquote($content)."</pre>";
  86  } else {
  87    $content = "";
  88    $title = "SQL Console:&nbsp; ".SETUP_DB_USER." @ ".SETUP_DB_NAME."&nbsp; [".SETUP_DB_TYPE." ".sgsml_parser::sql_version()."] ";
  89    $title .= sys_date("{t}m/d/y g:i:s a{/t}");
  90    if ($code!="") {
  91      if (($data = sql_fetch($code,false)) === false) {
  92        $content .= sql_error();
  93      } else if (is_array($data) and count($data)>0) {
  94        $content .= show_table($data, isset($_REQUEST["full_texts"]), isset($_REQUEST["vertical"]));
  95      } else if (SETUP_DB_TYPE=="mysql" and $num = mysql_affected_rows()) {
  96        $content .= sprintf("{t}%s rows affected{/t}",$num);
  97      } else {
  98        $content .= "{t}Empty{/t}";
  99      }
 100    }
 101    $smarty->assign("database", SETUP_DB_NAME);
 102    $smarty->assign("auto_complete", true);
 103  }
 104  if ($code!="") {
 105    $content .= "<br/>&nbsp;".sprintf("{t}%s secs{/t}", number_format(microtime(true)-$start, 4));
 106    $content .= ", ".sprintf("{t}%s M memory usage{/t}", number_format(memory_get_peak_usage(true)/1048576, 2));
 107  }
 108  if (DEBUG) $content = sys_remove_trans($content);
 109  
 110  $smarty->assign("title", $title);
 111  $smarty->assign("content", $content);
 112  if (isset($_REQUEST["no_gui"])) {
 113    echo $content;
 114  } else {
 115    $smarty->display("console.tpl");
 116  }
 117  
 118  function show_table($data, $full_texts=false, $vertical=true) {
 119    if (count($data)==0) return "";
 120    $limit = Max(round(220/count($data[0])),20);
 121    if ($vertical) $limit = 150;
 122  
 123    $content = "<table border='0' cellpadding='0' cellspacing='0'>";
 124    if (!$vertical) {
 125      $content .= "<tr>";
 126      foreach (array_keys($data[0]) as $value) {
 127        if ($full_texts and strlen($value)>$limit) $value = substr($value,0,$limit)."...";
 128        $content .= "<td nowrap><b>".modify::htmlquote($value)."</b></td>";
 129      }
 130      $content .= "</tr>";
 131    }
 132    foreach ($data as $dataset) {
 133      if ($vertical) {
 134        foreach ($dataset as $key=>$value) {
 135          if ($value=="") continue;
 136          $content .= "<tr onmouseover='this.style.backgroundColor=\"#EFEFEF\";' onmouseout='this.style.backgroundColor=\"\";'>";
 137          $content .= "<td style='width:25%;' valign='top'><b>".modify::htmlquote($key)."</b></td>";
 138          $hint = $value;
 139          if (is_numeric($value) and strlen($value)==10) $value .= " / ".sys_date("Y-m-d H:i:s a", $value);
 140          if (!$full_texts and strlen($value)>$limit) $value = substr($value,0,$limit)."...";
 141          $content .= "<td title='".modify::htmlquote($hint)."'>".nl2br(modify::htmlquote($value))."</td></tr>";
 142        }
 143        $content .= "<tr><td colspan='2'><hr></td></tr>";
 144      } else {
 145        $content .= "<tr onmouseover='this.style.backgroundColor=\"#EFEFEF\";' onmouseout='this.style.backgroundColor=\"\";'>";
 146        foreach ($dataset as $key=>$value) {
 147          if ($vertical) {
 148            if ($full_texts and strlen($key)>$limit) $key = substr($key,0,$limit)."...";
 149            $content .= "<td nowrap><b>".modify::htmlquote($key)."</b></td>";
 150          }
 151          $hint = $value;
 152          if (is_numeric($value) and strlen($value)==10) $hint = sys_date("Y-m-d H:i:s a", $value);
 153          if (!$full_texts and strlen($value)>$limit) $value = substr($value,0,$limit)."...";
 154          $content .= "<td nowrap title='".modify::htmlquote($hint)."'>".modify::htmlquote($value)."</td>";
 155        }
 156        $content .= "</tr>";
 157      }
 158    }
 159    return $content."</table>";
 160  }
 161  
 162  class funcs {
 163    static function get_databases($prefix) {
 164      $result = array();
 165      $prefix = sql_quote($prefix);
 166      if (SETUP_DB_TYPE=="mysql")    {
 167        $query = "SHOW databases like '{$prefix}%'";
 168      } else if (SETUP_DB_TYPE=="sqlite") {
 169        return array();
 170      } else if (SETUP_DB_TYPE=="pgsql") {
 171        $query = "SELECT datname FROM pg_database WHERE datname like '{$prefix}%'";
 172      }
 173      $data = sql_fetch($query);
 174      foreach ($data as $row) $result[] = array(implode($row).'.',implode($row));
 175      return $result;
 176    }
 177    static function get_tables($prefix,$add_db = true) {
 178      $result = array();
 179      if (!strpos($prefix,'.')) $prefix .= ".";
 180      $prefix = explode('.', $prefix);
 181      if ($prefix[0]=='') return array();
 182      $database = sql_quote($prefix[0]);
 183      $prefix = sql_quote($prefix[1]);
 184  
 185      if (SETUP_DB_TYPE=="mysql")    {
 186        $query = "SELECT table_name,table_rows,table_comment
 187                  FROM information_schema.tables
 188                  WHERE table_schema = '{$database}'
 189                  AND table_name like '{$prefix}%'";
 190      } else if (SETUP_DB_TYPE=="sqlite") {
 191        $query = "select name as table_name,0 as table_rows, '' as table_comment from sqlite_master where type='table' and name like '{$prefix}%'";
 192      } else if (SETUP_DB_TYPE=="pgsql") {
 193        $query = "SELECT table_name,
 194                    (select reltuples from pg_class where relname = table_name) as table_rows,
 195                    '' as table_comment
 196                  FROM information_schema.tables    
 197                  WHERE table_schema = 'public'
 198                  AND table_catalog = '{$database}'
 199                  AND table_name not like 'show_%'
 200                  AND table_name like '{$prefix}%'";
 201      }
 202      $data = sql_fetch($query);
 203      $letters = range('a', 'k');
 204      $result[] = array("","---- {$database} ----");
 205      foreach ($data as $row) {
 206        $letter = '';
 207        $count = count($result);
 208        for ($i=0; $i<strlen($count); $i++) $letter .= $letters[substr($count,$i,1)];
 209        if ($add_db) $alias = $database.'.'; else $alias = ''; 
 210        $result[] = array("{$alias}{$row["table_name"]} {$letter}","{$row["table_name"]} ({$row["table_rows"]} rows {$row["table_comment"]})");
 211      }
 212      if (count($result)==1) return array();
 213      return $result;
 214    }
 215  
 216    static function get_columns($prefix,$alias) {
 217      $result = array();
 218      $prefix .= '..';
 219      $prefix = explode('.', $prefix);
 220      if ($prefix[0]=='' or $prefix[1]=='') return array();
 221      $database = sql_quote($prefix[0]);
 222      $table = sql_quote($prefix[1]);
 223      $prefix = sql_quote($prefix[2]);
 224  
 225      if (SETUP_DB_TYPE=="mysql")    {
 226        $query = "SELECT column_name, column_type, column_comment
 227                  FROM information_schema.columns
 228                  WHERE table_schema = '{$database}'
 229                    AND table_name = '{$table}'
 230                    AND column_name like '{$prefix}%'";
 231        $data = sql_fetch($query);
 232      } else if (SETUP_DB_TYPE=="sqlite") {
 233        $data = sql_fetch("PRAGMA table_info({$table})");
 234        if (is_array($data) and count($data)>0) {
 235          foreach ($data as $key=>$row) {
 236            $data[$key]["column_name"] = $row["name"];
 237            $data[$key]["column_type"] = $row["type"];
 238            $data[$key]["column_comment"] = "";
 239          }
 240        }
 241      } else if (SETUP_DB_TYPE=="pgsql") {
 242        $query = "SELECT column_name, data_type||' '||coalesce(character_maximum_length,0) as column_type, '' as column_comment
 243                  FROM information_schema.columns
 244                  WHERE table_schema = 'public'
 245                    AND table_catalog = '{$database}'
 246                    AND table_name = '{$table}'
 247                    AND column_name like '{$prefix}%'";
 248        $data = sql_fetch($query);
 249      }
 250      $result[] = array("","---- {$table} ({$database}) ----");
 251      if (is_array($data) and count($data)>0) {
 252        foreach ($data as $row) {
 253          $result[] = array("{$alias}{$row["column_name"]}","{$row["column_name"]} ({$row["column_type"]} {$row["column_comment"]})");
 254        }
 255      }
 256      return $result;
 257    }
 258  }

title

Description

title

Description

title

Description

title

title

Body