home comics writing pictures archive about

display.php

Language: PHP Hypertext Preprocessor
Last Modified: 2024-03-01 1:56:19 AM UTC
File Size: 6664 bytes
http://www.penguinstew.ca/scripts/display.php
<?php
namespace Scripts;
require_once 'Model\Pictures\PicModel.php';
use Model\Pictures\PicModel;
abstract class Display
{
/* Gets the value of attribute from the line
Line: The string to search for the attribute in
Name: The name of the attribute to search for
Returns the value of the attribute in the string
*/
static function GetAttribute($line, $name) {
//Build attribute search string, looking for attribute='<value>'
$attribute = $name. "='";
//Get start point of attribute value
$atrStart = strpos($line, $attribute) + strlen($attribute);
if($atrStart === false){
return "";
}
//Look through line until the ending ' is found
$atrEnd = 0;
for($i = $atrStart ; $i < strlen($line) ; $i++ ) {
if($line[$i] == '\'' && $line[$i - 1] != '\\')
{
//Get the ending index
$atrEnd = $i;
break;
}
}
//Return the substring from start to end of the value
return substr($line, $atrStart, $atrEnd - $atrStart);
}
/* Tests the given string to see if it contains one of the special display tags.
Line: line to test for display tag
If the line contains one of the display tags it is ran through the corresponding function
and returned. If it does not then the line is returned unmodified.
*/
static function DisplayLine($line)
{
if(stripos($line,"[picture:")){
return Display::displayPicture($line);
} else if (stripos($line,"[download:")) {
return Display::displayDownload($line);
} else if (stripos($line,"[viewcode:")) {
return Display::displayCodeFile($line);
} else {
return $line;
}
}
/* Builds a picture frame from the information in the line
Line: The string containing information of the format: [picture:id='<id>'set='<set>'caption='<caption>']
Returns a string representing the picture box
*/
static function DisplayPicture($line)
{
$ressult = "";
// line
//Get attributes
$id = Display::getAttribute($line, "id");
$set = Display::getAttribute($line, "set");
$caption = Display::getAttribute($line, "caption");
$pic = PicModel::GetPic($set, $id);
//Open pic file
if ($pic != null) {
//Get the Name which is the first line
$title = $pic->Name;
//Get the Logo filename which is the third line
$picPath = $pic->BasePath . $pic->Id . "/". $pic->MediumThumbnailPath;
//Build the result
$ressult .= "\n<div class=\"imagePostBox center centered\">";
$ressult .= "\n<a href=\"/pictures/". $set. "/pic/". $id ."\">";
$ressult .= "\n<img class=\"imagePost\"";
$ressult .= "\nsrc=\"/". $picPath. "\"";
$ressult .= "\nalt=\"". $pic->Description ."\"/>";
$ressult .= "\n</a>";
$ressult .= "\n<br><b>". $caption ."</b>";
$ressult .= "\n</div>";
}
return $ressult;
}
/* Builds a file information black and code viewer link from the information in the line
Line: The string containing information of the format: [viewcode:file='<file>'type='<type>'lang='<lang>']
Returns a string representing the file block
*/
static function DisplayCodeFile($line) {
$result = "";
//Get attributes
$file = Display::getAttribute($line, "file");
$type = Display::getAttribute($line, "type");
$lang = Display::getAttribute($line, "lang");
$link = Display::getAttribute($line, "link") == "true";
$typeOverride = Display::GetAttribute($line, "typeOverride");
if($typeOverride != "")
{
$typeOverride = "_". $typeOverride;
}
if(file_exists($file)) {
//Get file information
$fileDir = pathinfo($file, PATHINFO_DIRNAME);
if($fileDir != "") {
//If file directory is not blank (root) add a trailing slash
$fileDir .= "/";
}
$fileName = pathinfo($file, PATHINFO_FILENAME);
$fileExt = pathinfo($file, PATHINFO_EXTENSION);
$fileBase = basename($file);
//Build the result
$result = "<h3>". basename($file) ."</h3>";
$result .= "<div class=\"block\">";
$result .= "<div class=\"blockLeft\">";
$result .= "<img style=\"border:0;width:48px;height:48px\"";
$result .= "src=\"/images/types/". $fileExt .".png\"";
$result .= "alt=\"". $fileExt ." type icon\" >";
$result .= "</div>";
$result .= "<div>";
$result .= "<b>Type: </b>". $type ."<br>";
$result .= "<b>Language: </b>". $lang ."<br>";
$result .= "<a href=\"/viewcode/". $fileDir . $fileName ."_". $fileExt . $typeOverride ."\">". $fileBase ."</a><br>";
if($link) {
//If the file is meant to be linked add a proper link to it
$result .= "<b>Link: </b><a href=\"/". $file ."\">". $fileBase ."</a><br>";
}
$result .= "</div>";
$result .= "</div>";
}
else
{
$result = "<b>File not found: ". $file. "</b>";
}
return $result;
}
/* Builds a download link from the information in the line
Line: The string containing information of the format: [download:file='<file>']
Returns a string representing the download link
*/
static function DisplayDownload($line) {
$ressult = "";
//Get attributes
$file = Display::getAttribute($line, "file");
if(file_exists($file)) {
//Build the result
$filesize = Display::FormatFileSize(filesize($file));
$ressult = "<img style=\"border:0\" class=\"vcenter\" src=\"/images/Buttons/download.png\" alt=\"download\"> ";
$ressult .= "<b>Download:</b> <a href=". substr($file, 2). ">". basename($file). " (". $filesize . ")</a> <br>";
}
return $ressult;
}
/* Removes new line characters from line */
static function CleanLine($line) {
$line = str_replace("\r", "", $line);
$line = str_replace("\n", "", $line);
return $line;
}
static function FormatFileSize($fileSize)
{
if($fileSize < 1024)
{
return $fileSize. " bytes";
}
$fileSize = $fileSize / 1024;
if ($fileSize < 1024)
{
return $fileSize. " KiB";
}
$fileSize = $fileSize / 1024;
return $fileSize. " MiB";
}
}
?>
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