1
+ <?php
2
+
3
+ /*
4
+ SlypCore
5
+
6
+ S L y P (Simple, Limpio y Potente / Simple, Lean y Potent)
7
+
8
+ MIT License
9
+ ===========
10
+
11
+ Copyright (c) 2012 Agustin Amenabar <[email protected] >
12
+
13
+ Permission is hereby granted, free of charge, to any person obtaining a
14
+ copy of this software and associated documentation files (the "Software"),
15
+ to deal in the Software without restriction, including without limitation
16
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
17
+ and/or sell copies of the Software, and to permit persons to whom the
18
+ Software is furnished to do so, subject to the following conditions:
19
+
20
+ The above copyright notice and this permission notice shall be included in
21
+ all copies or substantial portions of the Software.
22
+
23
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29
+ DEALINGS IN THE SOFTWARE.
30
+ */
31
+ class SlypCore{
32
+
33
+ protected $ _errors =array ();
34
+ protected $ acctions =array ();
35
+ protected $ mailWebmaster =
'[email protected] ' ;
36
+ protected $ nombreMarca = 'Code Blog Médula ' ;
37
+ protected $ mailAministrador =
'[email protected] ' ;
38
+ protected $ _templateDir = '' ;
39
+ protected $ _cachedHtml = './articulos/cached/ ' ;
40
+ protected $ articleList ;
41
+ const noventa = 7776000 ;
42
+ const treinta = 2592000 ;
43
+
44
+ function __construct ()
45
+ {
46
+ require "inc/libs/classTextile.php " ;
47
+ require "inc/libs/markdown.php " ;
48
+ require "inc/libs/simple_html_dom.php " ;
49
+ }
50
+
51
+ public function getArticleList (){
52
+ if ( !count ( $ this ->articleList ) ){
53
+ $ this ->loadArticleList ();
54
+ }
55
+ return $ this ->articleList ;
56
+ }
57
+
58
+ function getArticle ($ filename ){
59
+ //$this->processContents();
60
+ //print_r($this->articleList);
61
+ $ ao = new stdClass ();
62
+ $ arl = $ this ->getArticleList ();
63
+ foreach ($ arl as $ onea ) {
64
+ if ( $ onea ['filename ' ] == $ filename ){
65
+ foreach ($ onea as $ key => $ value ) {
66
+ $ ao ->{$ key } = $ value ;
67
+ }
68
+ break ;
69
+ }
70
+ }
71
+ if (!isset ($ ao ->modDate )) {
72
+ $ this ->_errors ['5 ' ]=true ;
73
+ return false ;
74
+ }
75
+ $ ao ->path = $ this ->_cachedHtml .($ filename ).$ ao ->modDate .'.html ' ;
76
+ if (!is_file ($ ao ->path ))echo 'NO ENCUENTRO: ' .$ ao ->path ;
77
+ //var_dump($ao);
78
+ return $ ao ;
79
+ }
80
+
81
+ public function getTemplateFile ($ bname ){
82
+ if ( is_file ( $ this ->_templateDir . $ bname ) )return $ this ->_templateDir . $ bname ;
83
+ if ( is_file ( $ this ->_templateDir . $ bname . '.php ' ) )return $ this ->_templateDir . $ bname . '.php ' ;
84
+ if ( is_file ( $ this ->_templateDir . $ bname . '.html ' ) )return $ this ->_templateDir . $ bname . '.html ' ;
85
+ return '' ;
86
+ }
87
+
88
+ public function processContents ($ forceRewrite =false ){
89
+ $ fileList = $ this ->getFileList ();
90
+ $ this ->articleList = array ();
91
+ foreach ($ fileList as $ oneArticle ) {
92
+ $ leyendoEncabezados = true ;
93
+ $ articleRawFile = "./articulos/ " .$ oneArticle ['filename ' ];
94
+ $ articleRawFilesize = filesize ( $ articleRawFile );
95
+ $ fhandl = fopen ($ articleRawFile , 'r+b ' );
96
+ $ lastpos = 0 ;
97
+ while (($ buffer = fgets ($ fhandl )) !== false ) {
98
+ //echo "\nbuff=".trim($buffer).';';
99
+ if (!trim ($ buffer )){
100
+ 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
101
+ $ oneArticle ['pubDate ' ] = date ('Y-m-d H:i:s ' ,$ oneArticle ['modDate ' ]);
102
+ $ agregado = 'pubDate: ' .$ oneArticle ['pubDate ' ].PHP_EOL .PHP_EOL ;
103
+ $ theRest = fread ($ fhandl , $ articleRawFilesize );
104
+ fseek ($ fhandl , $ lastpos );
105
+ fwrite ($ fhandl , $ agregado .$ theRest );
106
+ $ articleRawFilesize += strlen ($ agregado );
107
+ fseek ($ fhandl , $ lastpos );
108
+ //$leyendoEncabezados=false;
109
+ continue ;
110
+ }
111
+ $ leyendoEncabezados =false ;
112
+ break ;
113
+ }
114
+ $ lastpos = ftell ($ fhandl );
115
+ $ posDivisor = strpos ($ buffer , ': ' );
116
+ if ($ posDivisor ===false )$ posDivisor = strlen ( $ buffer );
117
+ $ key = substr ($ buffer , 0 , $ posDivisor );
118
+ $ value = substr ($ buffer , $ posDivisor +1 );
119
+ //echo "\n".trim($key).'=>'.trim($value)." ".$oneArticle.' '.$oneArticle['filename'];
120
+ if ( strtolower ( trim ( $ key ) ) == 'not_article ' )continue ;
121
+ $ oneArticle [trim ($ key )] = trim ($ value );
122
+ }
123
+ if (!$ leyendoEncabezados ){//ya leímos los encabezados ahora, procesamos el cuerpo de texto.
124
+ $ articleCachedUrl = $ this ->_cachedHtml .$ oneArticle ['filename ' ].$ oneArticle ['modDate ' ].'.html ' ;
125
+ //if( true ){
126
+ if ( !is_file ( $ articleCachedUrl ) || $ forceRewrite ){
127
+ $ articleRawText = fread ( $ fhandl , $ articleRawFilesize );
128
+ if (!$ articleRawText )$ articleRawText = 'ERROR: no file could be read by fread. ' ;
129
+ if (!isset ($ oneArticle ['markdown ' ])){
130
+ $ textile = new Textile ('html5 ' );
131
+ $ articleHtml = $ textile ->TextileThis ($ articleRawText );
132
+ }else {
133
+ $ articleHtml = Markdown ($ articleRawText );
134
+ }
135
+ $ articleDom = str_get_html ($ articleHtml );
136
+ $ introParagraph = $ articleDom ->find ('.intro ' ,0 );
137
+ if ($ introParagraph === null )$ introParagraph = $ articleDom ->find ('p ' ,0 );
138
+ if ($ introParagraph !== null ){
139
+ $ oneArticle ['intro ' ] = trim ( $ introParagraph ->innertext );
140
+ }
141
+ }
142
+ if ( @file_put_contents ( $ articleCachedUrl , $ articleHtml ) ){
143
+ $ this ->_errors ['2 ' ]=true ;
144
+ }
145
+
146
+ }
147
+ $ this ->articleList [] = $ oneArticle ;
148
+ fclose ($ fhandl );
149
+ }
150
+ //benchmarkUnserialization($this->articleList);
151
+ //print_r($this->articleList);
152
+ usort ($ this ->articleList , function ($ a , $ b ) {
153
+ return $ b ['pubDate ' ] - $ a ['pubDate ' ];
154
+ });
155
+ if ( @file_put_contents ( $ this ->_cachedHtml .'contents.enc ' , serialize ($ this ->articleList ) ) === false )$ this ->_errors ['3 ' ] = true ;
156
+ return $ this ->articleList ;
157
+ }
158
+
159
+ private function getFileList (){
160
+ $ directory = new DirectoryIterator ("./articulos " );
161
+ $ fileList = Array ();
162
+ foreach ($ directory as $ fileinfo ) {
163
+ if ($ fileinfo ->isFile ()) {
164
+ $ extension = pathinfo ($ fileinfo ->getFilename (), PATHINFO_EXTENSION );
165
+ //echo "<br>". $fileinfo->getFilename() . ' -> ' . $extension;
166
+ if (strtolower ($ extension ) === 'txt ' ){
167
+ $ oneArticle = array (
168
+ 'filename ' => $ fileinfo ->getFilename (),
169
+ 'modDate ' => $ fileinfo ->getMTime ()
170
+ );
171
+ $ fileList []=$ oneArticle ;
172
+ }
173
+ }
174
+ }
175
+ return $ fileList ;
176
+ }
177
+
178
+ protected function loadArticleList (){
179
+ $ encodedFileList = $ this ->_cachedHtml .'contents.enc ' ;
180
+ if ( is_file ( $ encodedFileList ) ){
181
+ $ unserializedT = @unserialize ( file_get_contents ( $ encodedFileList ) );
182
+ if ( $ unserializedT === false )$ this ->_errors ['4 ' ] = true ;
183
+ $ this ->articleList = $ unserializedT ;
184
+ }else {
185
+ $ this ->processContents ();
186
+ }
187
+ }
188
+
189
+ protected function activaAcciones (){
190
+ $ algunaActivada =false ;
191
+ if (isset ($ _GET ['a ' ])&& $ _GET ['a ' ] && isset ($ this ->acciones [$ _GET ['a ' ]])){
192
+ $ this ->{$ this ->acciones [$ _GET ['a ' ]]}();
193
+ $ algunaActivada =true ;
194
+ }
195
+ return $ algunaActivada ;
196
+ }
197
+
198
+ protected function avisaWebmaster ($ message ='' ){
199
+ $ message .="\r\n\r\nen la fecha: " .date ('Y-m-d H:i:s ' );
200
+ $ message .="\r\n\r\nen el archivo: " .$ _SERVER ['PHP_SELF ' ]."\r\nllamdo en: " .$ _SERVER ['REQUEST_URI ' ];
201
+ $ this ->$ mailWebmaster ;
202
+ $ this ->$ nombreMarca ;
203
+ $ this ->$ mailAministrador ;
204
+ $ to = $ mailWebmaster ;
205
+ $ subject = 'Error en sitio ' .$ nombreMarca ;
206
+ $ additionalHeaders = "From: Soporte $ nombreMarca < " .$ mailAministrador ."> \n" ;
207
+ $ additionalHeaders .= 'MIME-Version: 1.0 ' . "\n" ;
208
+ $ additionalHeaders .= 'Content-type: text/plain; charset=utf-8 ' . "\n" ;
209
+ @mail ($ to , $ subject ,$ message ,$ additionalHeaders );
210
+ }
211
+
212
+ protected function valida_email ($ email ) {
213
+ if (preg_match ("/[ \\000- \\037]/ " ,$ email )) {
214
+ return false ;
215
+ }
216
+ $ pattern = "/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD " ;
217
+ if (!preg_match ($ pattern , $ email )){
218
+ return false ;
219
+ }
220
+ return true ;
221
+ }
222
+
223
+ public function benchmarkUnserialization ($ toSer ){
224
+ $ enJson = json_encode ($ toSer );
225
+ $ enPhp = serialize ($ toSer );
226
+
227
+ //***************** JSON
228
+ $ beforeJson = microtime (true );
229
+
230
+ for ($ i =0 ; $ i < 100000 ; $ i ++) {
231
+ json_decode ($ enJson );
232
+ }
233
+
234
+ $ afterJson = microtime (true );
235
+ echo ($ afterJson -$ beforeJson ) . " sec/json_decode \n" ;
236
+
237
+ //***************** UNSERIALIZE
238
+ $ beforePhp = microtime (true );
239
+
240
+ for ($ i =0 ; $ i < 100000 ; $ i ++) {
241
+ unserialize ($ enPhp );
242
+ }
243
+
244
+ $ afterPhp = microtime (true );
245
+ echo ($ afterPhp -$ beforePhp ) . " sec/unserialize \n" ;
246
+ }
247
+
248
+ public function getError ($ nombre =false ){
249
+ if ($ nombre ===false ){
250
+ return (bool ) count ($ this ->_errors );
251
+ }else {
252
+ if (!isset ($ this ->_errors [$ nombre ]))return false ;
253
+ return (bool ) $ this ->_errors [$ nombre ];
254
+ }
255
+ return false ;
256
+ }
257
+ }
258
+
259
+
260
+
261
+
262
+ ?>
0 commit comments