fogproject  1.5.9
About: FOG is a Linux-based network computer cloning solution for Windows, Mac OSX and various Linux distributions that ties together a few open-source tools with a PHP-based web interface. FOG doesn’t use any boot disks, or CDs; everything is done via TFTP and PXE.
  Fossies Dox: fogproject-1.5.9.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

schema.class.php
Go to the documentation of this file.
1 <?php
22 class Schema extends FOGController
23 {
29  protected $operators = [
30  '=', '<', '>', '<=', '>=', '<>', '!=',
31  'like', 'like binary', 'not like', 'between', 'ilike',
32  '&', '|', '^', '<<', '>>',
33  'rlike', 'regexp', 'not regexp',
34  '~', '~*', '!~', '!~*', 'similar to',
35  'not similar to', 'not ilike', '~~*', '!~~*'
36  ];
42  protected $databaseTable = 'schemaVersion';
48  protected $databaseFields = array(
49  'id' => 'vID',
50  'version' => 'vValue',
51  );
57  public static function getDBName()
58  {
59  return DATABASE_NAME;
60  }
66  public static function createDatabaseQuery()
67  {
68  return self::createDatabase(self::getDBName(), false);
69  }
75  public static function useDatabaseQuery()
76  {
77  return sprintf(
78  'USE `%s`',
79  self::getDBName()
80  );
81  }
92  public function dropDuplicateData(
93  $dbname,
94  $table = array(),
95  $indexNeeded = false
96  ) {
97  if (empty($dbname)) {
98  return;
99  }
100  if (count($table) < 1) {
101  return;
102  }
103  $queries = array();
104  $tablename = $table[0];
105  $indexes = (array)$table[1];
106  $dropIndex = $table[2];
107  if ($indexNeeded) {
108  if (count($indexes) < 1) {
109  return;
110  } elseif (count($indexes) === 1) {
111  $ending = sprintf(
112  'INDEX (`%s`)',
113  array_shift($indexes)
114  );
115  } else {
116  $ending = sprintf(
117  'INDEX `%s` (`%s`)',
118  array_shift($indexes),
119  implode('`,`', $indexes)
120  );
121  }
122  } else {
123  $ending = sprintf(
124  '(`%s`)',
125  implode('`,`', $indexes)
126  );
127  }
128  $queries[] = sprintf(
129  'DROP TABLE IF EXISTS `%s`.`_%s`',
130  $dbname,
131  $tablename
132  );
133  $queries[] = sprintf(
134  'CREATE TABLE `%s`.`_%s` LIKE `%s`.`%s`',
135  $dbname,
136  $tablename,
137  $dbname,
138  $tablename
139  );
140  $queries[] = sprintf(
141  'ALTER TABLE `%s`.`_%s` ADD UNIQUE %s',
142  $dbname,
143  $tablename,
144  $ending
145  );
146  $queries[] = sprintf(
147  'INSERT IGNORE INTO `%s`.`_%s` %s',
148  $dbname,
149  $tablename,
150  sprintf(
151  'SELECT * FROM `%s`.`%s`',
152  $dbname,
153  $tablename
154  )
155  );
156  $queries[] = sprintf(
157  'DROP TABLE `%s`.`%s`',
158  $dbname,
159  $tablename
160  );
161  $queries[] = sprintf(
162  'RENAME TABLE `%s`.`_%s` TO `%s`.`%s`',
163  $dbname,
164  $tablename,
165  $dbname,
166  $tablename
167  );
168  if ($dropIndex) {
169  $queries[] = sprintf(
170  'ALTER TABLE `%s`.`%s` DROP INDEX `%s`',
171  $dbname,
172  $tablename,
173  $dropIndex
174  );
175  }
176  return $queries;
177  }
186  public function exportdb(
187  $backup_name = '',
188  $remove_file = true
189  ) {
190  $orig_exec_time = ini_get('max_execution_time');
191  set_time_limit(0);
192  $file = '/tmp/fog_backup_tmp.sql';
193  if (!$backup_name) {
194  $backup_name = sprintf(
195  'fog_backup_%s.sql',
196  self::formatTime('', 'Ymd_His')
197  );
198  }
199  $dump = self::getClass('Mysqldump');
200  $dump->start($file);
201  if (!file_exists($file) || !is_readable($file)) {
202  throw new Exception(_('Could not read tmp file.'));
203  }
204  if ($remove_file) {
205  while (ob_get_level()) {
206  ob_end_clean();
207  }
208  $fh = fopen($file, 'rb');
209  header('Content-Type: text/plain');
210  header("Content-Disposition: attachment; filename=$backup_name");
211  header('Cache-Control: private');
212  while (feof($fh) === false) {
213  echo fread($fh, 4096);
214  }
215  fclose($fh);
216  ini_set('max_execution_time', $orig_exec_time);
217  ini_set('request_terminate_timeout', $orig_term_time);
218  unlink($file);
219  return;
220  }
221  set_time_limit($orig_exec_time);
222  return $file;
223  }
231  public function importdb($file)
232  {
233  $orig_exec_time = ini_get('max_execution_time');
234  set_time_limit(0);
235  if (false === ($fh = fopen($file, 'rb'))) {
236  throw new Exception(_('Error Opening DB File'));
237  }
238  while (($line = fgets($fh)) !== false) {
239  if (substr($line, 0, 2) == '--' || $line == '') {
240  continue;
241  }
242  $tmpline .= $line;
243  if (substr(trim($line), -1, 1) == ';') {
244  if (false === self::$DB->query($tmpline)) {
245  $error .= sprintf(
246  "%s '<strong>%s': %s</strong><br/><br/>",
247  _('Error performing query'),
248  $line,
249  self::$DB->sqlerror()
250  );
251  }
252  $tmpline = '';
253  }
254  }
255  fclose($fh);
256  set_time_limit($orig_exec_time);
257  if ($error) {
258  return $error;
259  }
260  return true;
261  }
270  public static function createDatabase(
271  $name,
272  $exists
273  ) {
274  if (!is_bool($exists)) {
275  throw new Exception(_('Exists item must be boolean'));
276  }
277  $string = sprintf(
278  'CREATE DATABASE %s`%s`',
279  (
280  false == $exists ?
281  ' IF NOT EXISTS' :
282  ''
283  ),
284  $name
285  );
286  return $string;
287  }
306  public static function createTable(
307  $name,
308  $exists,
309  $fields,
310  $types,
311  $nulls,
312  $default,
313  $unique,
314  $engine = 'MyISAM',
315  $charset = 'utf8',
316  $prime = '',
317  $autoin = ''
318  ) {
319  if (empty($name)) {
320  throw new Exception(_('Must have a name to create the table'));
321  }
322  $fieldCount = count($fields);
323  $typeCount = count($types);
324  if ($fieldCount !== $typeCount) {
325  throw new Exception(_('Fields and types must have equal count'));
326  }
327  if (empty($engine)) {
328  $engine = 'MyISAM';
329  }
330  if (empty($charset)) {
331  $charset = 'utf8';
332  }
333  $sql = sprintf(
334  'CREATE TABLE%s `%s` (',
335  (
336  $exists ?
337  ' IF NOT EXISTS' :
338  ''
339  ),
340  $name
341  );
342  foreach ((array)$fields as $i => &$field) {
343  $sql .= sprintf(
344  '`%s` %s%s%s%s,',
345  $field,
346  $types[$i],
347  (
348  $nulls[$i] === false ?
349  ' NOT NULL' :
350  ''
351  ),
352  (
353  $default[$i] ?
354  sprintf(
355  ' DEFAULT %s',
356  $default[$i]
357  ) :
358  ''
359  ),
360  (
361  $field === $autoin ?
362  ' AUTO_INCREMENT' :
363  ''
364  )
365  );
366  unset($field);
367  }
368  if ($prime) {
369  $sql .= sprintf(
370  'PRIMARY KEY (`%s`)',
371  $prime
372  );
373  }
374  foreach ((array)$unique as $i => &$uniq) {
375  if (!$uniq) {
376  continue;
377  }
378  if (is_array($uniq)) {
379  $uniq = implode('`,`', $uniq);
380  }
381  $sql .= sprintf(
382  ',UNIQUE INDEX `index%d` (`%s`)',
383  $i,
384  $uniq
385  );
386  unset($uniq);
387  }
388  $sql .= ') ';
389  $sql .= sprintf(
390  'ENGINE=%s',
391  $engine
392  );
393  if ($autoin) {
394  $sql .= ' AUTO_INCREMENT=1';
395  }
396  $sql .= ' DEFAULT ';
397  $sql .= sprintf(
398  'CHARSET=%s',
399  $charset
400  );
401  $sql .= ' ROW_FORMAT=DYNAMIC';
402  return $sql;
403  }
411  public static function dropTable($name)
412  {
413  if (empty($name)) {
414  throw new Exception(_('Need the table name to drop'));
415  }
416  return sprintf(
417  'DROP TABLE IF EXISTS `%s`',
418  $name
419  );
420  }
421 }
FOGBase\getClass
static getClass($class, $data='', $props=false)
Definition: fogbase.class.php:439
Schema
Definition: schema.class.php:23
$file
if(isset($_REQUEST['legclient'])) if(isset($_REQUEST['newclient'])) if(isset($_REQUEST['fogprep'])) if(isset($_REQUEST['fogcrypt'])) if(isset($_REQUEST['smartinstaller'])) if(!file_exists($filename)) $file
Definition: download.php:61
Schema\useDatabaseQuery
static useDatabaseQuery()
Definition: schema.class.php:75
Schema\$databaseTable
$databaseTable
Definition: schema.class.php:42
$exists
$exists
Definition: check_node_exists.php:26
Schema\getDBName
static getDBName()
Definition: schema.class.php:57
Schema\createTable
static createTable( $name, $exists, $fields, $types, $nulls, $default, $unique, $engine='MyISAM', $charset='utf8', $prime='', $autoin='')
Definition: schema.class.php:306
Schema\createDatabase
static createDatabase( $name, $exists)
Definition: schema.class.php:270
Schema\createDatabaseQuery
static createDatabaseQuery()
Definition: schema.class.php:66
Schema\dropDuplicateData
dropDuplicateData( $dbname, $table=array(), $indexNeeded=false)
Definition: schema.class.php:92
Schema\dropTable
static dropTable($name)
Definition: schema.class.php:411
FOGController
Definition: fogcontroller.class.php:29
Schema\$databaseFields
$databaseFields
Definition: schema.class.php:48
Schema\importdb
importdb($file)
Definition: schema.class.php:231
Schema\exportdb
exportdb( $backup_name='', $remove_file=true)
Definition: schema.class.php:186
Schema\$operators
$operators
Definition: schema.class.php:29