1<?php 2 3/* 4 * Nibbleblog - 5 * http://www.nibbleblog.com 6 * Author Diego Ignacio Gabriel Najar Carrascal 7 8 * All NibbleBlog code is released under the GNU General Public License. 9 * See COPYRIGHT.txt and LICENSE.txt. 10*/ 11 12class NBFUNCTIONS 13{ 14 15/* 16====================================================================================== 17 ENCRYPT 18====================================================================================== 19*/ 20 // return string 21 public function encrypt($string, $key) 22 { 23 $string = base64_encode( mcrypt_encrypt( MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5($key) ) ); 24 return( $string ); 25 } 26 27 // return string 28 public function desencrypt($string, $key) 29 { 30 $string = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, md5($key) ), "\0" ); 31 return( $string ); 32 } 33 34/* 35====================================================================================== 36 NETWORK 37====================================================================================== 38*/ 39 // return string 40 public function get_user_ip() 41 { 42 if (getenv('HTTP_X_FORWARDED_FOR')) 43 $realip = getenv('HTTP_X_FORWARDED_FOR'); 44 elseif (getenv('HTTP_CLIENT_IP')) 45 $realip = getenv('HTTP_CLIENT_IP'); 46 else 47 $realip = getenv('REMOTE_ADDR'); 48 49 return($realip); 50 } 51 52 public function get_user_agent() 53 { 54 return( getenv('HTTP_USER_AGENT') ); 55 } 56 57/* 58====================================================================================== 59 RANDOMS 60====================================================================================== 61*/ 62 63function random_text($length) 64{ 65 $characteres = "1234567890abcdefghijklmnopqrstuvwxyz"; 66 for($i=0; $i<$length; $i++) 67 { 68 $text .= $characteres{rand(0,35)}; 69 } 70 return $text; 71} 72 73function random_number($min, $max) 74{ 75 return( rand($min, $max) ); 76} 77 78/* 79====================================================================================== 80 VALIDATION 81====================================================================================== 82*/ 83 // Clean text for URL 84 public function clean_url($text) 85 { 86 $text = str_replace(array("!", "*", "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]"),"",$text); 87 $text = str_replace(" ","_",$text); 88 89 return($text); 90 } 91 92 // Convert html to plain text 93 public function html2text($text) 94 { 95 return(htmlspecialchars($text, ENT_QUOTES, 'utf-8')); 96 } 97 98 // Convert plain text to html 99 public function text2html($html) 100 { 101 return(html_entity_decode($html, ENT_QUOTES, 'utf-8')); 102 } 103 104 // Clean magic quotes 105 public function clean_magic_quotes($ARGS) 106 { 107 $tmp_array = array(); 108 foreach($ARGS as $key => $arg) 109 { 110 $tmp_array[$key] = stripslashes($arg); 111 } 112 113 return($tmp_array); 114 } 115 116 // RETURN 117 // TRUE - si contiene el substring 118 // FALSE - caso contrario 119 public function is_substring($string, $substring) 120 { 121 return( strpos($string, $substring) !== false ); 122 } 123 124 // Strip spaces 125 public function strip_spaces($string) 126 { 127 return( str_replace(' ','',$string) ); 128 } 129 130 // Strip quotes ' and " 131 public function strip_quotes($text) 132 { 133 $text = str_replace('\'', '', $text); 134 $text = str_replace('"', '', $text); 135 return( $text ); 136 } 137 138 // RETURN 139 // TRUE - is not empty 140 // FALSE - is empty 141 public function not_empty($string) 142 { 143 $string = $this->strip_spaces($string); 144 return( !empty($string) ); 145 } 146 147 public function valid_mail($mail) 148 { 149 return ( filter_var($mail, FILTER_VALIDATE_EMAIL) ); 150 } 151 152 public function valid_int($int) 153 { 154 if($int === 0) 155 return( true ); 156 elseif (filter_var($int, FILTER_VALIDATE_INT) === false ) 157 return( false ); 158 else 159 return( true ); 160 } 161 162/* 163====================================================================================== 164 FILES 165====================================================================================== 166*/ 167 168// Devuelve un arreglo con el listado de archivos 169// $path con una barra al final, ej: /home/ 170// $ext : xml 171// $file_expression : *.0.*.*.*.*.*.*.*.* 172// $flag_dir : si quiero listar directorios 173// $sort_asc_numeric : ordeno ascedente numerico 174// $sort_desc_numeric : ordeno descendente numerico 175function ls($path, $file_expression = NULL, $ext, $flag_dir = false, $sort_asc_numeric = false, $sort_desc_numeric = false) 176{ 177 if($flag_dir) 178 { 179 $files = glob($path . $file_expression, GLOB_ONLYDIR); 180 } 181 else 182 { 183 $files = glob($path . $file_expression . '.' . $ext); 184 } 185 186 foreach($files as $key=>$file) 187 { 188 $files[$key] = basename($file); 189 } 190 191 // Sort 192 if($sort_asc_numeric) 193 { 194 sort($files, SORT_NUMERIC); 195 } 196 elseif($sort_desc_numeric) 197 { 198 rsort($files, SORT_NUMERIC); 199 } 200 201 return $files; 202} 203 204/* 205====================================================================================== 206 PLUGINS 207====================================================================================== 208*/ 209 210function plugin_get_info($plugin, $lang) 211{ 212 // Language for plugin 213 if( !@include( PATH_PLUGINS . $plugin . '/lang/' . $lang . '.php' ) ) 214 require( PATH_PLUGINS . $plugin . '/lang/english.php' ); 215 216 include(PATH_PLUGINS.$plugin.'/'.$plugin.'.php'); 217 218 $tmp_array = array(); 219 $tmp_array['name'] = constant( 'PLUGIN_'.strtoupper($plugin).'_NAME' ); 220 $tmp_array['description'] = constant( 'PLUGIN_'.strtoupper($plugin).'_DESCRIPTION' ); 221 $tmp_array['version'] = constant( 'PLUGIN_'.strtoupper($plugin).'_VERSION' ); 222 $tmp_array['release_date'] = constant( 'PLUGIN_'.strtoupper($plugin).'_RELEASE_DATE' ); 223 $tmp_array['author'] = constant( 'PLUGIN_'.strtoupper($plugin).'_AUTHOR' ); 224 $tmp_array['author_mail'] = constant( 'PLUGIN_'.strtoupper($plugin).'_AUTHOR_MAIL' ); 225 $tmp_array['author_web'] = constant( 'PLUGIN_'.strtoupper($plugin).'_AUTHOR_WEB' ); 226 $tmp_array['license'] = constant( 'PLUGIN_'.strtoupper($plugin).'_LICENSE' ); 227 return $tmp_array; 228} 229 230/* 231====================================================================================== 232 RSS 233====================================================================================== 234*/ 235 236function generate_rss($dbxml_config, $dbxml_post, $dbxml_rss) 237{ 238 $pubdate = $this->unixstamp_to_date($dbxml_config->get_timezone(), $this->unixstamp()); 239 $dbxml_rss->create_body($dbxml_config->get_name(), $dbxml_config->get_url(), $dbxml_config->get_description(), $pubdate['r']); 240 241 $posts = $dbxml_post->get_list_by_page(0, $dbxml_config->get_items_rss()); 242 foreach($posts as $post) 243 { 244 $pubdate = $this->unixstamp_to_date($dbxml_config->get_timezone(), $post['pub_date']); 245 $dbxml_rss->add_item($post['title'], $dbxml_config->get_url() . '/post.php?idpost=' . $post['idpost'], $post['content_part1'], $pubdate['r']); 246 } 247 248 $dbxml_rss->savetofile(); 249} 250 251/* 252====================================================================================== 253 AJAX 254====================================================================================== 255*/ 256 257public function ajax_header($tmp) 258{ 259 $xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'; 260 $xml .= '<ajax>'; 261 $xml .= $tmp; 262 $xml .= '</ajax>'; 263 return( $xml ); 264} 265 266/* 267====================================================================================== 268 TEXT 269====================================================================================== 270*/ 271 272public function cut_text($text, $maxlength) 273{ 274 return( substr($text,0,strrpos(substr($text,0,$maxlength)," ")) ); 275} 276 277/* 278====================================================================================== 279 DATE 280====================================================================================== 281*/ 282 283 // Convierte unixstamp a date 284 // Devuelve un arreglo y en cada posicion devuelve un dato de la fecha 285 // Para ver los datos que devuelve mirar la tabla de la funcion date de php 286 // http://php.net/manual/en/function.date.php 287 // 288 // Parametros 289 // $gmt_user -> Zona horaria en la que se encuentra el usuario 290 // $time_unix -> Fecha en formato Unix 291 public function unixstamp_to_date($gmt_user, $time_unix) 292 { 293 $zone = 3600 * $gmt_user; 294 295 $date = array(); 296 297 $date['s'] = date('s', $time_unix + $zone); 298 $date['i'] = date('i', $time_unix + $zone); 299 $date['h'] = date('h', $time_unix + $zone); 300 $date['H'] = date('H', $time_unix + $zone); 301 $date['d'] = date('d', $time_unix + $zone); 302 $date['N'] = date('N', $time_unix + $zone); 303 $date['n'] = date('n', $time_unix + $zone); 304 $date['m'] = date('m', $time_unix + $zone); 305 $date['Y'] = date('Y', $time_unix + $zone); 306 $date['y'] = date('y', $time_unix + $zone); 307 $date['a'] = date('a', $time_unix + $zone); 308 $date['r'] = date('r', $time_unix + $zone); 309 310 return( $date ); 311 } 312 313 // Devuelve la fecha en formato Unixstamp y esta corrida en GMT = 0 314 public function unixstamp() 315 { 316 return( time() - date('Z') ); 317 } 318 319 public function time_ago($tm, $rcs = 0) 320 { 321 $cur_tm = time() - date('Z'); 322 $dif = $cur_tm-$tm; 323 $pds = array('SECOND/S','MINUTE/S','HOUR/S','DAY/S','WEEK/S','MONTH/S','YEAR/S','DECADE/S'); 324 $lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600); 325 for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]); 326 $no = floor($no); 327 $x['number'] = $no; 328 $x['i18n']= $pds[$v]; 329 return($x); 330 } 331} 332 333 334class LOGIN extends NBFUNCTIONS 335{ 336 function LOGIN() 337 { 338 session_start(); 339 } 340 341 public function set_token() 342 { 343 // expire 1hour 344 setcookie('token', $this->random_text(10), time()+3600, '/'); 345 } 346 347 public function get_token() 348 { 349 return( $_COOKIE['token'] ); 350 } 351 352 public function valid_token($token) 353 { 354 return( (strcmp($token, $this->get_token()) == 0) && isset($_COOKIE['token']) ); 355 } 356 357 private function get_key() 358 { 359 return( md5( $this->get_user_agent() . $this->get_user_ip() ) ); 360 } 361 362 public function verify_login($username, $password) 363 { 364 require( FILE_SHADOW ); 365 366 if( $username === $_USER[0]['user'] ) 367 { 368 if( md5( $password ) === $_USER[0]['password'] ) 369 { 370 $_SESSION['nb_iduser'] = 0; 371 $_SESSION['nb_date'] = $this->unixstamp(); 372 $_SESSION['nb_user'] = $username; 373 $_SESSION['nb_key'] = $this->get_key(); 374 375 return( true ); 376 } 377 } 378 379 return( false ); 380 } 381 382 public function get_iduser() 383 { 384 return( $_SESSION['nb_iduser'] ); 385 } 386 387 public function is_login() 388 { 389 if( isset( $_SESSION['nb_key'] ) && isset( $_SESSION['nb_user'] ) && isset( $_SESSION['nb_date'] ) ) 390 { 391 if( !(strcmp($_SESSION['nb_key'], $this->get_key() ) != 0) ) 392 return( true ); 393 } 394 395 return( false ); 396 } 397 398 public function logout() 399 { 400 $_SESSION = array(); 401 unset($_SESSION['nb_date']); 402 unset($_SESSION['nb_user']); 403 unset($_SESSION['nb_key']); 404 session_destroy(); 405 } 406} 407 408$login = new LOGIN(); 409 410?> 411