-
Notifications
You must be signed in to change notification settings - Fork 2
/
SlypCore.php
345 lines (299 loc) · 11.5 KB
/
SlypCore.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
340
341
342
343
344
345
<?php
/*
SlypCore
S L y P (Simple, Limpio y Potente / Simple, Lean y Potent)
MIT License
===========
Copyright (c) 2012 Agustin Amenabar <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
class SlypCore{
protected $_errors=array();
protected $acctions=array();
protected $mailWebmaster = '[email protected]';
protected $nombreMarca = 'Code Blog Médula';
protected $mailAministrador = '[email protected]';
protected $_templateDir = '';
protected $_articlesFolder = "./articulos";
protected $_pagesFolder = "./paginas";
protected $_cachedHtml = './articulos/cached/';
protected $_possibleExtensions = array('textile','txt','md');
protected $articleList;
const noventa = 7776000;
const treinta = 2592000;
function __construct()
{
require 'inc/libs/Netcarver/Textile/Parser.php';
require 'inc/libs/Netcarver/Textile/DataBag.php';
require 'inc/libs/Netcarver/Textile/Tag.php';
require "inc/libs/markdown.php";
require "inc/libs/simple_html_dom.php";
}
public function getArticleList(){
if( !count( $this->articleList ) ){
$this->loadArticleList();
}
return $this->articleList;
}
public function getArticle($filename){
//$this->processContents();
//print_r($this->articleList);
if( substr( $filename, -5 ) == '.html' )$filename = substr( $filename, 0, ( strlen( $filename ) - 5 ) );
if( substr( $filename, -4 ) == '.txt' )$filename = substr( $filename, 0, ( strlen( $filename ) - 4 ) );
$ao = new stdClass();
$arl = $this->getArticleList();
$articleFilename='';
foreach ($arl as $onea) {
if( $onea['cleanname'] == $filename ){
$articleFilename = $onea['filename'];
foreach ($onea as $key => $value) {
$ao->{$key} = $value;
}
break;
}
}
if (!isset($ao->modDate)) {
$this->_errors['5']=true;
return false;
}
$ao->path = $this->_cachedHtml.($articleFilename).$ao->modDate.'.html';
if(!is_file($ao->path))echo 'NO ENCUENTRO: '.$ao->path;
//var_dump($ao);
return $ao;
}
public function getTemplateFile($bname){
if( is_file( $this->_templateDir . $bname) )return $this->_templateDir . $bname;
if( is_file( $this->_templateDir . $bname . '.php') )return $this->_templateDir . $bname . '.php';
if( is_file( $this->_templateDir . $bname . '.html') )return $this->_templateDir . $bname . '.html';
return '';
}
public function processFile(){
}
public function processContents($forceRewrite=false){
$fileList = $this->getFileList();
$this->articleList = array();
foreach ($fileList as $oneArticle) {
$leyendoEncabezados = true;
$articleRawFile = $this->_articlesFolder . "/" . $oneArticle['filename'];
$articleRawFilesize = filesize( $articleRawFile );
$fhandl = fopen($articleRawFile, 'r+b');
$lastpos = 0;
// ==================================================================
//
// lee y procesa meta del encabezado
//
// ------------------------------------------------------------------
while (($buffer = fgets($fhandl)) !== false) {
//echo "\nbuff=".trim($buffer).';';
if(!trim($buffer)){
if( !isset($oneArticle['pubDate']) ){//si el artículo no tiene fecha de publicación guardada, guardamos la última modificación como fecha de publicación
$oneArticle['pubDate'] = date('c',$oneArticle['modDate']);;//date('Y-m-dTH:i:sZ',$oneArticle['modDate']);
$agregado = 'pubDate:'.$oneArticle['pubDate'].PHP_EOL.PHP_EOL;
$theRest = fread($fhandl, $articleRawFilesize);
fseek($fhandl, $lastpos);
fwrite($fhandl, $agregado.$theRest);
$articleRawFilesize += strlen($agregado);
fseek($fhandl, $lastpos);
//$leyendoEncabezados=false;
continue;
}
if (isset($oneArticle['draft'])) {
//$leyendoEncabezados=false;
$oneArticle=null;
break;
}
$leyendoEncabezados=false;
break;
}
$lastpos = ftell($fhandl);
$posDivisor = strpos($buffer, ':');
if($posDivisor===false)$posDivisor = strlen( $buffer );
$key = substr($buffer, 0, $posDivisor);
$value = substr($buffer, $posDivisor+1);
//echo "\n".trim($key).'=>'.trim($value)." ".$oneArticle.' '.$oneArticle['filename'];
if( strtolower( trim( $key ) ) == 'not_article' )continue;
$oneArticle[trim($key)] = trim($value);
}
// ==================================================================
//
// cuerpo del documento
//
// ------------------------------------------------------------------
if(!$leyendoEncabezados){//ya leímos los encabezados ahora, procesamos el cuerpo de texto.
$articleCachedUrl = $this->_cachedHtml.$oneArticle['filename'].$oneArticle['modDate'].'.html';
//if( true ){
if( !is_file( $articleCachedUrl ) || $forceRewrite ){
$articleRawText = fread( $fhandl, $articleRawFilesize );
if(!$articleRawText)$articleRawText = 'ERROR: no file could be read by fread.';
if(!isset($oneArticle['markdown'])){
$textileParser = new \Netcarver\Textile\Parser();
$articleHtml = $textileParser
->setDocumentType('html5')
->parse($articleRawText);
}else{
$articleHtml = Markdown($articleRawText);
}
$articleDom = str_get_html($articleHtml);
$introParagraph = $articleDom->find('.intro',0);
if($introParagraph === null)$introParagraph = $articleDom->find('p',0);
if($introParagraph !== null){
$oneArticle['intro'] = trim( $introParagraph->innertext );
}
}
if( @file_put_contents( $articleCachedUrl, $articleHtml ) === false ){
$this->_errors['2']=true;
}
}
if($oneArticle!==null)$this->articleList[] = $oneArticle;
fclose($fhandl);
}
//benchmarkUnserialization($this->articleList);
//print_r($this->articleList);
usort($this->articleList, function($a, $b) {
return implode('', explode('-', $b['pubDate'])) - implode('', explode('-', $a['pubDate']));
});
if( @file_put_contents( $this->_cachedHtml.'contents.enc', serialize($this->articleList) ) === false)$this->_errors['3'] = true;
return $this->articleList;
}
private function getFileList(){
$directory = new DirectoryIterator( $this->_articlesFolder );
$fileList = Array();
foreach ($directory as $fileinfo) {
if ($fileinfo->isFile()) {
$extension = pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION);
//echo "<br>". $fileinfo->getFilename() . ' -> ' . $extension;
$sufix = '';
if($extension)$sufix='.'.$extension;
if(strtolower($extension) === 'txt' || strtolower($extension) === 'textile' || strtolower($extension) === 'md' ){
$oneArticle = array(
'filename' => $fileinfo->getFilename(),
'cleanname'=> $fileinfo->getBasename($sufix),
'modDate' => $fileinfo->getMTime()
);
$fileList[]=$oneArticle;
}
}
}
return $fileList;
}
protected function loadArticleList(){
$encodedFileList = $this->_cachedHtml.'contents.enc';
if( is_file( $encodedFileList ) ){
$unserializedT = @unserialize( file_get_contents( $encodedFileList ) );
if( $unserializedT === false)$this->_errors['4'] = true;
$this->articleList = $unserializedT;
}else{
$this->processContents();
}
}
protected function avisaWebmaster($message=''){
$message.="\r\n\r\nen la fecha:".date('Y-m-d H:i:s');
$message.="\r\n\r\nen el archivo:".$_SERVER['PHP_SELF']."\r\nllamdo en:".$_SERVER['REQUEST_URI'];
$this->$mailWebmaster;
$this->$nombreMarca;
$this->$mailAministrador;
$to = $mailWebmaster;
$subject = 'Error en sitio'.$nombreMarca;
$additionalHeaders = "From: Soporte $nombreMarca <".$mailAministrador.">\n";
$additionalHeaders .= 'MIME-Version: 1.0' . "\n";
$additionalHeaders .= 'Content-type: text/plain; charset=utf-8' . "\n";
@mail($to, $subject,$message,$additionalHeaders);
}
protected function valida_email($email) {
if (preg_match("/[\\000-\\037]/",$email)) {
return false;
}
$pattern = "/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD";
if(!preg_match($pattern, $email)){
return false;
}
return true;
}
public function benchmarkUnserialization($toSer){
$enJson = json_encode($toSer);
$enPhp = serialize($toSer);
//***************** JSON
$beforeJson = microtime(true);
for ($i=0; $i < 100000 ; $i++) {
json_decode($enJson);
}
$afterJson = microtime(true);
echo ($afterJson-$beforeJson) . " sec/json_decode\n";
//***************** UNSERIALIZE
$beforePhp = microtime(true);
for ($i=0; $i < 100000 ; $i++) {
unserialize($enPhp);
}
$afterPhp = microtime(true);
echo ($afterPhp-$beforePhp) . " sec/unserialize\n";
}
public function getError($nombre=false){
if($nombre===false){
return (bool) count($this->_errors);
}else{
if(!isset($this->_errors[$nombre]))return false;
return (bool) $this->_errors[$nombre];
}
return false;
}
public function getErrorList(){
return $this->_errors;
}
public function newArticle( $filename ){
$type = 'article';
if( isset( $_GET['type'] ) && $_GET['type'] == 'page' )$type='page';
if(isset($_SERVER['HTTP_HOST']) && strpos($_SERVER['HTTP_HOST'],'localhost')===false){
$emsg = "This operation can only be done on a development evironment (localserver)";
$this->_errors['permiso'] = $emsg;
return $emsg;
}
$src_dir = $this->_articlesFolder;
if( $type == 'page' )$src_dir = $this->_pagesFolder;
if ( is_file( $src_dir . "/" . $filename . ".textile") || is_file( $src_dir . "/" . $filename . ".txt") || is_file( $src_dir . "/" . $filename . ".md") ) {
return 'ERROR: The document: “' . $filename . '” already exist.';
}
$theTitle = implode(' ', explode('-', $filename));
$toSave='title:' . $theTitle . "\n";
$toSave.='subtitle:' . "\n";
$toSave.='classes: language-markup' . "\n";
$toSave.='draft: true' . "\n";
$toSave.="\n\n";
$toSave.=' <header><hgroup>
h1. '.$theTitle.'
h2. Subtitle
</hgroup></header>' . "\n" .'
p(intro). This is the intro that will be lloked up for the lead text of an article.
h2(#ac2). __[fr]Mise en place__, first subtitle
Here goes some text with a "link to this header":#ac2 followed by an image
!img/content/file-structure-postcss-guide.png(This will be the alt text)!
After the image, some code:
bc(language-javascript). var someObject = {
part:true,
follow: "false"
};
And another paragraph
';
$rs = file_put_contents($src_dir . "/" . $filename . ".textile", $toSave);
if ( $rs === false ) {
$emsg = "The document: “ $filename ” could not be created on the documents folder.";
$this->_errors['permiso'] = $emsg;
return $emsg;
}
return "The document: “ $filename ” has been created.";
}
}
?>