-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsptt-getdata.php
339 lines (300 loc) · 9.94 KB
/
sptt-getdata.php
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
<?
// include config vars
include "sptt-config.php";
function sptt_get_site_metadata($whatdata) {
// $whatdata parameter values: lang, home, title, description, short_description, logo_url
global $site_lang;
global $site_tit;
global $site_desc;
global $site_short_desc;
global $site_logo;
global $img_path;
global $site_path;
global $site_home;
if ( $whatdata == 'lang' ) { $sitedata = $site_lang; }
elseif ( $whatdata == 'title' ) { $sitedata = $site_tit; }
elseif ( $whatdata == 'description' ) { $sitedata = $site_desc; }
elseif ( $whatdata == 'short_description' ) { $sitedata = $site_short_desc; }
elseif ( $whatdata == 'logo_url' ) { $sitedata = $img_path.$site_logo; }
elseif ( $whatdata == 'home' ) { $sitedata = $site_home; }
return $sitedata;
}
// end get_site_metadata funcion
/**
* Function: sanitize
* Returns a sanitized string, typically for URLs.
* Author: http://chyrp.net/
*
* Parameters:
* $string - The string to sanitize.
* $force_lowercase - Force the string to lowercase?
* $anal - If set to *true*, will remove all non-alphanumeric characters.
*/
function sptt_sanitize($string, $force_lowercase = true, $anal = false) {
$strip = array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", "[", "{", "]",
"}", "\\", "|", ";", ":", "\"", "'", "‘", "’", "“", "”", "–", "—",
"—", "–", ",", "<", ".", ">", "/", "?");
$clean = trim(str_replace($strip, "", strip_tags($string)));
$clean = preg_replace('/\s+/', "-", $clean);
$clean = ($anal) ? preg_replace("/[^a-zA-Z0-9]/", "", $clean) : $clean ;
return ($force_lowercase) ?
(function_exists('mb_strtolower')) ?
mb_strtolower($clean, 'UTF-8') :
strtolower($clean) :
$clean;
}
// end sptt_sanitize
// check if URL already exists
// if true, constructs a new one
function sptt_check_url($filename) {
global $site_path;
$ext = ".html";
$filepath = $site_path.$filename;
if ( is_file($filepath) ) { // if file exists
$count = 0;
while ( is_file($filepath) ) {
$count++;
$filename = basename($filepath, $ext);
$filename = $filename . '-' . $count . $ext;
$filepath = $site_path . $filename;
}
} // end if file exist
return $filename;
}
// end check if URL already exists
if ( $key != '' ) {
// if google key is set in sptt-config.php, so the content is in a google drive spreadsheet
function sptt_get_data($whatdata) {
// $whatdata parameter values: allposts, postsbycateg, categs
global $working_path; // base directory
global $site_path; // where the HTML files will be stored
global $img_path; // images folder
global $key;
$url = "https://spreadsheets.google.com/feeds/list/" .$key. "/od6/public/values?alt=json";
$file= file_get_contents($url);
$json = json_decode($file);
$rows = $json->{'feed'}->{'entry'};
// data array generation
$data = array();
foreach ( $rows as $row ) {
$status = $row->{'gsx$status'}->{'$t'};
if ( $status != 'publish' ) {}
else {
$cat = $row->{'gsx$category'}->{'$t'};
$tit = $row->{'gsx$title'}->{'$t'};
$desc = $row->{'gsx$description'}->{'$t'};
$content = $row->{'gsx$fullcontent'}->{'$t'};
if ( $desc == '' ) { $desc = $content; }
$img = $row->{'gsx$image'}->{'$t'};
$img_alt = $tit;
$link = $row->{'gsx$externallink'}->{'$t'};
$perma = sptt_sanitize($tit);
$perma = $perma. ".html";
$perma = sptt_check_url($perma);
$athome = $row->{'gsx$showathome'}->{'$t'};
if ( $whatdata == 'postsbycateg' ) {
$data[$cat][] = array(
'categ' => $cat,
'tit' => $tit,
'desc' => $desc,
'content' => $content,
'img' => $img,
'img-alt' => $tit,
'link' => $link,
'perma' => $perma,
'athome' => $athome,
);
$posts = $data;
} elseif ( $whatdata == 'allposts' ) {
$data[] = array(
'categ' => $cat,
'tit' => $tit,
'desc' => $desc,
'content' => $content,
'img' => $img,
'img-alt' => $tit,
'link' => $link,
'perma' => $perma,
);
$posts = $data;
} elseif ( $whatdata == 'categs' ) {
$data[$cat][] = array(
);
$posts = array_keys($data);
} // end if order values
} // end if col1 = 'publish'
} // end loop across gdrive file
return $posts;
}
// end get data from gdrive function
} else {
// if google key is not set in sptt-config.php, so content is in localhost
function sptt_get_data($whatdata) {
// $whatdata parameter values: allposts, postsbycateg, categs
global $working_path; // base directory
global $site_path; // where the HTML files will be stored
global $img_path; // images folder
global $csv_filename; // name (no extension)
global $line_length; // max line lengh (increase in case you have longer lines than 1024 characters)
global $delimiter; // field delimiter character
global $enclosure; // field enclosure character
// open the data file
$fp = fopen($csv_filename.".csv",'r');
// get data and store it in array
if ( $fp !== FALSE ) { // if the file exists and is readable
// data array generation
$data = array();
$line = 0;
while ( ($fp_csv = fgetcsv($fp,$line_length,$delimiter,$enclosure)) !== FALSE ) { // begin main loop
if ( $line == 0 ) {}
elseif ( $fp_csv[0] != 'publish') {}
else {
$cat = $fp_csv[1];
$tit = $fp_csv[2];
$desc = $fp_csv[3];
$content = $fp_csv[4];
if ( $desc == '' ) { $desc == $content; }
$img = $fp_csv[5];
$img_alt = $tit;
$link = $fp_csv[6];
$perma = sptt_sanitize($tit);
$perma = $perma. ".html";
$perma = sptt_check_url($perma);
$athome = $fp_csv[7];
if ( $whatdata == 'postsbycateg' ) {
$data[$cat][] = array(
'categ' => $cat,
'tit' => $tit,
'desc' => $desc,
'content' => $content,
'img' => $img,
'img-alt' => $tit,
'link' => $link,
'perma' => $perma,
'athome' => $athome,
);
$posts = $data;
} elseif ( $whatdata == 'allposts' ) {
$data[] = array(
'categ' => $cat,
'tit' => $tit,
'desc' => $desc,
'content' => $content,
'img' => $img,
'img-alt' => $tit,
'link' => $link,
'perma' => $perma,
);
$posts = $data;
} elseif ( $whatdata == 'categs' ) {
$data[$cat][] = array(
);
$posts = array_keys($data);
} // end if order values
} // end if first line
$line++;
} // end while main loop
fclose($fp); // close file pointer
} else {
echo $cvs_filename. "doesn't exist or is not readable. Check it.";
} // end if file exist and readable
return $posts;
} // end sptt_all_posts function
} // end if google key is set in sptt-config.php
// get category function
function sptt_get_cat_link($cat_name) {
global $building;
$cat_link = sptt_sanitize($cat_name);
$cat_link = $cat_link . ".html";
if ( $building != 'single' ) { $cat_link = sptt_check_url($cat_link); }
//$cat_link = preg_replace("/ /","-",$cat_link);
//$cat_link = preg_replace("/\?/","",$cat_link);
//$cat_link = strtolower($cat_link);
return $cat_link;
} // end get category link function
// if post has image function
function sptt_post_has_image() {
global $img;
global $img_path;
if ( $img != $img_path ) { return true; }
else { return false; }
} // end if post has image
// if post has description
function sptt_post_has_description() {
global $content;
if ( $content != '' ) { return true; }
else { return false; }
} // end if post has description
// if post has more info link
function sptt_post_has_link() {
global $link;
if ( $link != '' ) { return true; }
else { return false; }
} // end if post has link
// if site has a logo
function sptt_site_has_logo() {
global $site_logo;
if ( $site_logo != '' ) { return true; }
else { return false; }
} // end if site has a logo
// if is home function
function sptt_is_home() {
global $building;
if ( $building == 'index' ) { return true; }
} // end if is home function
// if is category function
function sptt_is_category() {
global $building;
if ( $building == 'categs' ) { return true; }
} // end if is category function
// if is single function
function sptt_is_single() {
global $building;
if ( $building == 'single' ) { return true; }
} // end if is single function
// get data from a post
function sptt_get_post_data($field) {
global $cat;
global $tit;
global $desc;
global $content;
global $img;
global $img_alt;
global $link;
global $perma;
$perma = sptt_check_url($perma);
if ( $field == 'category' ) { $post_data = $cat; }
elseif ( $field == 'title' ) { $post_data = $tit; }
elseif ( $field == 'description' ) { $post_data = $desc; }
elseif ( $field == 'content' ) { $post_data = $content; }
elseif ( $field == 'image_url' ) { $post_data = $img; }
elseif ( $field == 'image_alt' ) { $post_data = $img_alt; }
elseif ( $field == 'more_link' ) { $post_data = $link; }
elseif ( $field == 'permalink' ) { $post_data = $perma; }
else { $post_data = "<span style='background-color: yellow; color: black;'>Wrong parameter for sptt_get_post_data(). Try one of the following: category, title, description, content, image_url, image_alt, more_link, permalink.</span>"; }
return $post_data;
} // end get data from a post
// include masonry library function
function sptt_active_masonry($masonry_path,$item_css_class) {
if ( sptt_is_home() ) {
$categ_names = sptt_get_data('categs');
$categ_num = count($categ_names);
} else { $categ_num = 1; }
$masonry_count = 1;
$masonry_out = "
<script type='text/javascript' src='" .$masonry_path. "'></script>
<script>
";
while ( $categ_num >= $masonry_count ) {
$masonry_out .= "
var section" .$masonry_count. " = document.querySelector('#section-" .$masonry_count. "');
var msnry = new Masonry( section" .$masonry_count. ", {
itemSelector: '." .$item_css_class. "'
});
";
$masonry_count++;
}
$masonry_out .= "</script>";
return $masonry_out;
} // end masonry library function
?>