The following files exists in this folder. Click to view.
functions.php366 lines UTF-8 Unix (LF) 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
<?php
/**
$fileName - filnamnet, inkl katalog
$specialCharacter - innebär om htmlspecialchars() skall köras, true eller false.
*/
/*
Dra in hela formatteringen hit, även med <pre>-tagg, typ och all kod.
Fixa detta till createCodeFromFile och createCodeFromArray.
Dokumentera sedan skiten!!!!
Flytta också hela highlight.js-deklarationen till några variabler som läggs i common.
*/
/* OLD! */ /*
function createCodeFromFile($fileName, $specialCharacter){
$returnString = "";
$file = fopen($fileName, "r");
while(!feof($file)){
$returnString .= $specialCharacter ? "<code>".htmlspecialchars(fgets($file))."</code>" : "<code>".fgets($file)."</code>";
}
fclose($file);
return $returnString;
}
*/
function readFromFile($fileName, $specialCharacter){
$returnString = "";
$file = fopen($fileName, "r");
while(!feof($file)){
$returnString .= $specialCharacter ? htmlspecialchars(fgets($file)) : fgets($file);
}
fclose($file);
return $returnString;
}
/**
Läs av type automatiskt beroende på vilken fil som hämtas?
*/
function createCodeFromFile($fileName, $specialCharacter = false, $type = "css"){
$returnString = "<pre><code class=\"{$type}\">";
$file = fopen($fileName, "r");
while(!feof($file)){
$returnString .= $specialCharacter ? htmlspecialchars(fgets($file)) : fgets($file);
}
fclose($file);
$returnString .= "</code></pre>";
return $returnString;
}
/**
createCodeSHFromArray
in: $fileName - name of file
in: $specialCharacter - true or false, default is false
in: $type - what kind of file, plain is default
in: $option - formatting of output in syntax highlight window
out: the formatted string
*/
function createCodeSHFromFile($fileName, $specialCharacter = false, $type = "plain", $option = "tab-size: 2; toolbar: false;"){
$option = $option == "" ? "tab-size: 2; toolbar: false;" : $option;
# Fixa ordning på brushen med en switch
switch($type){
case 'plain':
$type = "text";
break;
case 'html':
$type = "php";
break;
case 'cs':
$type = "csharp";
break;
case 'c++':
$type = "cpp";
break;
case 'java':
$type = "java";
break;
default:
$type = $type;
break;
}
$type = "'brush: {$type};{$option}'";
# tab-size: 2;
# toolbar: false;
# auto-links: false
# first-line: 10
# highlight: 2
# highlight: [1, 3]
# html-script: true
$returnString = "<pre class={$type}>";
$file = fopen($fileName, "r");
while(!feof($file)){
$returnString .= $specialCharacter ? htmlspecialchars(fgets($file)) : fgets($file);
}
fclose($file);
$returnString .= "</pre>";
return $returnString;
}
/**
createCodeSHFromArray
in: $arr - the array
in: $specialCharacter - true or false, default is false
in: $type - what kind of file, plain is default
in: $option - formatting of output in syntax highlight window
out: the formatted string
*/
function createCodeSHFromArray($arr, $specialCharacter = false, $type = "plain", $option = "tab-size: 2; toolbar: false;"){
$option = $option == "" ? "tab-size: 2; toolbar: false;" : $option;
# Fixa ordning på brushen med en switch
switch($type){
case 'plain':
$type = "text";
break;
case 'html':
$type = "php";
break;
case 'cs':
$type = "csharp";
break;
case 'c++':
$type = "cpp";
break;
case 'java':
$type = "java";
break;
default:
$type = $type;
break;
}
$type = "'brush: {$type};{$option}'";
$returnString = "<pre class={$type}>";
for($i = 0; $i < count($arr); $i++) {
# $returnString .= "<code>".$arr[$i]."</code>\n";
# $returnString .= $specialCharacter ? "<code>".htmlspecialchars($arr[$i])."</code>\n" : "<code>".$arr[$i]."</code>\n";
$returnString .= $specialCharacter ? htmlspecialchars($arr[$i])."\n" : $arr[$i]."\n";
}
$returnString .= "</pre>";
return $returnString;
}
/**
createCodeSHFromString
in: $str - the string
in: $specialCharacter - true or false, default is false
in: $type - what kind of file, plain is default
in: $option - formatting of output in syntax highlight window
out: the formatted string
*/
function createCodeSHFromString($str, $specialCharacter = false, $type = "plain", $option = "tab-size: 2; toolbar: false;"){
$option = $option == "" ? "tab-size: 2; toolbar: false;" : $option;
# Fixa ordning på brushen med en switch
switch($type){
case 'plain':
$type = "text";
break;
case 'html':
$type = "php";
break;
case 'cs':
$type = "csharp";
break;
case 'c++':
$type = "cpp";
break;
case 'java':
$type = "java";
break;
default:
$type = $type;
break;
}
$type = "'brush: {$type};{$option}'";
$returnString = "<pre class={$type}>";
$returnString .= $specialCharacter ? htmlspecialchars($str)."\n" : $str."\n";
$returnString .= "</pre>";
return $returnString;
}
/**
createCodeFromArray
in: $arr - the array
in: $specialCharacter - true or false, default is false
in: $type - what kind of file, css is default
out: the formatted string
*/
function createCodeFromArray($arr, $specialCharacter = false, $type = "css"){
$returnString = "<pre><code class=\"{$type}\">";
for($i = 0; $i < count($arr); $i++) {
# $returnString .= "<code>".$arr[$i]."</code>\n";
# $returnString .= $specialCharacter ? "<code>".htmlspecialchars($arr[$i])."</code>\n" : "<code>".$arr[$i]."</code>\n";
$returnString .= $specialCharacter ? htmlspecialchars($arr[$i])."\n" : $arr[$i]."\n";
}
$returnString .= "</code></pre>";
return $returnString;
}
/**
createCodeFromString
in: $str - the string
in: $specialCharacter - true or false, default is false
in: $type - what kind of file, css is default
out: the formatted string
*/
function createCodeFromString($str, $specialCharacter = false, $type = "css"){
$returnString = "<pre><code class=\"{$type}\">";
$returnString .= $specialCharacter ? htmlspecialchars($str) : $str;
$returnString .= "\n</code></pre>";
return $returnString;
}
/*
Old
function createCodeFromArray($arr, $specialCharacter = false){
$returnString = "";
for($i = 0; $i < count($arr); $i++) {
# $returnString .= "<code>".$arr[$i]."</code>\n";
$returnString .= $specialCharacter ? "<code>".htmlspecialchars($arr[$i])."</code>\n" : "<code>".$arr[$i]."</code>\n";
}
return $returnString;
}
*/
function createTableFromSql($arr, $sql){
require('dbConn.php');
$dbInfo = "mysql:host=".$host.";dbname=".$db.";charset=utf8";
$db = new PDO($dbInfo, $user, $pwd);
$returnString = "<table class=\"utskrift\">
<tr>";
# Skapar tabellhuvudet
for($i = 0; $i < count($arr); $i++) {
$returnString .= "<th>".$arr[$i]."</th>\n";
}
$returnString .= "</tr>";
# Hämtar resultset och placerar ut det i tabellen
$stmt = $db->query($sql);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $row) {
$returnString .= "<tr>";
foreach ($arr as $i){
$returnString .= $row[$i]!="" ? "<td>{$row[$i]}</td>\n" : "<td><i>null</i></td>";
}
$returnString .= "</tr>";
}
$returnString .= "</table>";
return $returnString;
}
// -------------------------------------------------------------------------------------------
//
// executeSQLFile
// description: Run all sql-scripts from a file to the database
// inparam: $fileName, name of sql-file to run
// inparam: $urlRedirect, the page to redirect
// return: nothing
//
function executeSQLFile($fileName, $urlRedirect){
// Läser in databaskopplingen, require eftersom det måste göras igen!
require('dbConn.php');
$mess = "";
try
{
$sql = file_get_contents($fileName);
$qr = $pdo->exec($sql); # 2 är bra lokalt, 0 är bra på Binero
$mess = "Databasen återställd!";
# Kolla över detta, http://php.net/manual/en/pdo.exec.php
# $qr skall ge värdet för antal rader, kan man jobba med $db->errorInfo() för
// Errorinfo
// ok: Array ( [0] => 00000 [1] => [2] => )
# print_r($pdo->errorInfo());
#$mess = $pdo->errorInfo();
/*
if($qr == 2){
$mess = "Databasen återställd!";
} else {
$mess = "Något gick fel, databasen är ej återställd";
}*/
}
catch (PDOException $e)
{
$mess = "Connection failed: $e->getMessage()" ;
}
$urlRedirect .= "?mess=".$mess;
header("location: $urlRedirect");
}
// -------------------------------------------------------------------------------------------
//
// createTableFromSqlEdit
// description: Makes a table of result from a sql script
// inparam: $arr, array of attribute to print head of table
// inparam: $sql, the sql script
// inparam: $table, name of table to link edit and delete
// return: String of table content
//
function createTableFromSqlEdit($arr, $sql, $table){
// Läser in databaskopplingen, require eftersom det måste göras igen!
require('dbConn.php');
$returnString = "<table class=\"utskrift\">
<tr>";
# Skapar tabellhuvudet
for($i = 0; $i < count($arr); $i++) {
$returnString .= "<th>".$arr[$i]."</th>\n";
}
$returnString .= "<th>delete</th>";
$returnString .= "</tr>";
# Hämtar resultset och placerar ut det i tabellen
$stmt = $pdo->query($sql);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
# Special, hämta id och skriv ut det i första kolumnen och sista som länkar.
foreach ($result as $row) {
$id = -1;
$returnString .= "<tr>";
foreach ($arr as $i){
if($id == -1){
$id = $row[$i];
$returnString .= "<td><a href='edit.php?table={$table}&id={$id}'>{$row[$i]}</a></td>";
} else {
$returnString .= $row[$i]!="" ? "<td>{$row[$i]}</td>\n" : "<td><i>null</i></td>";
}
}
$returnString .= "<td><a href='edit.php?table={$table}&action=delete&id={$id}'>X</a></td>";
$returnString .= "</tr>";
}
$returnString .= "</table>";
return $returnString;
}
?>