function compile_error($message, $file, $line) { error::reset(); error::pitch(new FAError($message, $file, $line)); critical_error(); }
function alterTable($table, $alterdefs) { if ($alterdefs != '') { $result = sqlite_query($this->link, "SELECT sql, name, type FROM sqlite_master WHERE tbl_name = '" . $table . "' ORDER BY type DESC"); if (sqlite_num_rows($result) > 0) { $row = sqlite_fetch_array($result); //table sql $tmpname = 't' . time(); $origsql = trim(preg_replace("/[\\s]+/", " ", str_replace(",", ", ", preg_replace("/[\\(]/", "( ", $row['sql'], 1)))); $createtemptableSQL = 'CREATE TEMPORARY ' . substr(trim(preg_replace("'" . $table . "'", $tmpname, $origsql, 1)), 6); $createindexsql = array(); $i = 0; $defs = preg_split("/[,]+/", $alterdefs, -1, PREG_SPLIT_NO_EMPTY); $prevword = $table; /* Doesn't work with decimal() columns.. e.g. decimal(5,2) */ $oldcols = preg_split("/[,]+/", substr(trim($createtemptableSQL), strpos(trim($createtemptableSQL), '(') + 1), -1, PREG_SPLIT_NO_EMPTY); $newcols = array(); for ($i = 0; $i < count($oldcols); $i++) { $colparts = preg_split("/[\\s]+/", $oldcols[$i], -1, PREG_SPLIT_NO_EMPTY); $oldcols[$i] = $colparts[0]; $newcols[$colparts[0]] = $colparts[0]; } $newcolumns = ''; $oldcolumns = ''; reset($newcols); while (list($key, $val) = each($newcols)) { $newcolumns .= iif($newcolumns, ', ', '') . $val; $oldcolumns .= iif($oldcolumns, ', ', '') . $key; } $copytotempsql = 'INSERT INTO ' . $tmpname . '(' . $newcolumns . ') SELECT ' . $oldcolumns . ' FROM ' . $table; $dropoldsql = 'DROP TABLE ' . $table; $createtesttableSQL = $createtemptableSQL; foreach ($defs as $def) { $defparts = preg_split("/[\\s]+/", $def, -1, PREG_SPLIT_NO_EMPTY); $action = strtolower($defparts[0]); switch ($action) { case 'add': if (sizeof($defparts) <= 2) { error::pitch(new FAError('An error occured near "' . $defparts[0] . iif($defparts[1], ' ' . $defparts[1], '') . '": syntax error.', __FILE__, __LINE__)); return FALSE; } $createtesttableSQL = substr($createtesttableSQL, 0, strlen($createtesttableSQL) - 1) . ','; for ($i = 1; $i < sizeof($defparts); $i++) { $createtesttableSQL .= ' ' . $defparts[$i]; } $createtesttableSQL .= ')'; break; case 'change': if (count($defparts) <= 3) { error::pitch(new FAError('An error occured near "' . $defparts[0] . iif($defparts[1], ' ' . $defparts[1], '') . iif($defparts[2], ' ' . $defparts[2], '') . '": syntax error.', __FILE__, __LINE__)); return FALSE; } if ($severpos = strpos($createtesttableSQL, ' ' . $defparts[1] . ' ')) { if ($newcols[$defparts[1]] != $defparts[1]) { error::pitch(new FAError('unknown column "' . $defparts[1] . '" in "' . $table . '"', __FILE__, __LINE__)); return FALSE; } $newcols[$defparts[1]] = $defparts[2]; $nextcommapos = strpos($createtesttableSQL, ',', $severpos); $insertval = ''; for ($i = 2; $i < count($defparts); $i++) { $insertval .= ' ' . $defparts[$i]; } if ($nextcommapos) { $createtesttableSQL = substr($createtesttableSQL, 0, $severpos) . $insertval . substr($createtesttableSQL, $nextcommapos); } else { $createtesttableSQL = substr($createtesttableSQL, 0, $severpos - iif(strpos($createtesttableSQL, ','), 0, 1)) . $insertval . ')'; } } else { error::pitch(new FAError('Unknown column "' . $defparts[1] . '" in "' . $table . '"', __FILE__, __LINE__)); return FALSE; } break; case 'drop': if (count($defparts) < 2) { error::pitch(new FAError('An error occured near "' . $defparts[0] . iif($defparts[1], ' ' . $defparts[1], '') . '": syntax error.', __FILE__, __LINE__)); return FALSE; } if ($severpos = strpos($createtesttableSQL, ' ' . $defparts[1] . ' ')) { $nextcommapos = strpos($createtesttableSQL, ',', $severpos); if ($nextcommapos) { $createtesttableSQL = substr($createtesttableSQL, 0, $severpos) . substr($createtesttableSQL, $nextcommapos + 1); } else { $createtesttableSQL = substr($createtesttableSQL, 0, $severpos - (strpos($createtesttableSQL, ',') ? 0 : 1) - 1) . ')'; } unset($newcols[$defparts[1]]); } else { error::pitch(new FAError('Unknown column "' . $defparts[1] . '" in "' . $table . '".', __FILE__, __LINE__)); return FALSE; } break; default: error::pitch(new FAError('An error occured near "' . $prevword . '": syntax error.', __FILE__, __LINE__)); return FALSE; } $prevword = $defparts[count($defparts) - 1]; } /** * this block of code generates a test table simply to verify that the columns * specifed are valid in an sql statement this ensures that no reserved words * are used as columns, for example. */ if (!$this->query($createtesttableSQL)) { error::pitch(new FAError('The test table could not be created.<br /><br />' . $createtesttable, __FILE__, __LINE__)); return FALSE; } $droptempsql = 'DROP TABLE ' . $tmpname; sqlite_query($this->link, $droptempsql); /* end block */ $createnewtableSQL = 'CREATE ' . substr(trim(preg_replace("'" . $tmpname . "'", $table, $createtesttableSQL, 1)), 17); $newcolumns = ''; $oldcolumns = ''; reset($newcols); while (list($key, $val) = each($newcols)) { $newcolumns .= iif($newcolumns, ', ', '') . $val; $oldcolumns .= iif($oldcolumns, ', ', '') . $key; } $copytonewsql = 'INSERT INTO ' . $table . '(' . $newcolumns . ') SELECT ' . $oldcolumns . ' FROM ' . $tmpname; /** * Use a transaction here so that if one query fails, they all fail */ /* Begin the transaction */ $this->beginTransaction(); /* Create our temporary table */ $this->executeUpdate($createtemptableSQL); /* Copy the data to the temporary table */ $this->executeUpdate($copytotempsql); /* Drop the table that we are modifying */ $this->executeUpdate($dropoldsql); /* Recreate that original table with the column added/changed/droped */ $this->executeUpdate($createnewtableSQL); /* Copy the data from our temporary table to our new table */ $this->executeUpdate($copytonewsql); /* Drop our temporary table */ $this->executeUpdate($droptempsql); /* Finish the transaction */ $this->commitTransaction(); } else { error::pitch(new FAError('Non-existant table: ' . $table, __FILE__, __LINE__)); return FALSE; } return true; } }
function render() { $filename = $this->getFilename(TPL_SOURCE); $compiled = $this->getFilename(TPL_COMPILED); if ($filename == $compiled) { error::pitch(new FAError("The source and compiled templates are the same: {$filename}", __FILE__, __LINE__)); return FALSE; } if (!is_readable($filename) || !file_exists($filename)) { error::pitch(new FAError("Cannot read template file or file does not exist: {$filename}", __FILE__, __LINE__)); return FALSE; } require_once TPL_BASE_DIR . '/runtime.php'; if ($this->context == NULL) { $this->context =& new TPL_Context($this->vars, $this->lists); } $template = $this; $context =& $this->context; if (!$this->cache) { $source =& new TPL_Source($filename); $buffer = $source->compile(); eval(" ?>{$buffer}<?php "); } else { if ($this->getForce() || !file_exists($compiled) || filemtime($filename) > filemtime($compiled)) { $source =& new TPL_Source($filename); $source->compileTo($compiled); } if (!file_exists($compiled)) { error::pitch(new FAError("The compiled template does not exist or could not be created: " . $compiled, __FILE__, __LINE__)); return FALSE; } include $compiled; } }