View sourcecode

The following files exists in this folder. Click to view.

functions.php

366 lines UTF-8 Unix (LF)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?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;
}

?>