| [ Indice ] |
Riferimento incrociato di Joomla! 1.5.14 - VM 1.1.4Servizio fornito da VMItalia |
[Vedi sommario] [Stampa] [Vedi testo]
1 <?php 2 if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' ); 3 /** 4 * This is no class! This file only provides core virtuemart functions. 5 * 6 * @version $Id: ps_main.php 1888 2009-09-24 21:33:16Z soeren_nb $ 7 * @package VirtueMart 8 * @subpackage classes 9 * @copyright Copyright (C) 2004-2008 soeren - All rights reserved. 10 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php 11 * VirtueMart is free software. This version may have been modified pursuant 12 * to the GNU General Public License, and as distributed it includes or 13 * is derivative of works licensed under the GNU General Public License or 14 * other free or open source software licenses. 15 * See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details. 16 * 17 * http://virtuemart.net 18 */ 19 20 21 /** 22 * This function validates a given date and creates a timestamp 23 * @deprecated 24 * 25 * @param array $d 26 * @param string $field The name of the field 27 * @param string $type 28 * @return boolean 29 */ 30 if(function_exists('date_default_timezone_set')){ 31 date_default_timezone_set('GMT0'); 32 }else{ 33 putenv("TZ=GMT"); 34 } 35 36 function process_date_time(&$d,$field,$type="") { 37 $month = $d["$field" . "_month"]; 38 $day = $d["$field" . "_day"]; 39 $year = $d["$field" . "_year"]; 40 $hour = $d["$field" . "_hour"]; 41 $minute = $d["$field" . "_minute"]; 42 $use = $d["$field" . "_use"]; 43 $valid = true; 44 45 /* If user unchecked "Use date and time" then time = 0 */ 46 if (!$use) { 47 $d[$field] = 0; 48 return true; 49 } 50 if (!checkdate($month,$day,$year)) { 51 $d["error"] .= "ERROR: $type date is invalid."; 52 $valid = false; 53 } 54 if (!$hour and !$minute) { 55 $hour = 0; 56 $minute = 0; 57 } elseif ($hour < 0 or $hour > 23 or $minute < 0 or $minute > 59) { 58 $d["error"] .= "ERROR: $type time is invalid."; 59 $valid = false; 60 } 61 62 if ($valid) { 63 $d[$field] = mktime($hour,$minute,0,$month,$day,$year); 64 } 65 66 return $valid; 67 } 68 69 /** 70 * Validates an email address by using regular expressions 71 * Does not resolve the domain name! 72 * 73 * @param string $email 74 * @return boolean The result of the validation 75 */ 76 function vmValidateEmail( $email ) { 77 $valid = preg_match( '/^[\w\.\-]+@\w+[\w\.\-]*?\.\w{1,4}$/', $email ); 78 79 return $valid; 80 } 81 82 /** 83 * Checks if a given string is a valid (from-)name or subject for an email 84 * 85 * @since 1.0.7 86 * @param string $string String to check for validity 87 * @return boolean 88 */ 89 function vmValidateName( $string ) { 90 /* 91 * The following regular expression blocks all strings containing any low control characters: 92 * 0x00-0x1F, 0x7F 93 * These should be control characters in almost all used charsets. 94 * The high control chars in ISO-8859-n (0x80-0x9F) are unused (e.g. http://en.wikipedia.org/wiki/ISO_8859-1) 95 * Since they are valid UTF-8 bytes (e.g. used as the second byte of a two byte char), 96 * they must not be filtered. 97 */ 98 $invalid = preg_match( '/[\x00-\x1F\x7F]/', $string ); 99 if ($invalid) { 100 return false; 101 } else { 102 return true; 103 } 104 } 105 /** 106 * Validates an EU-vat number 107 * @author Steve Endredy 108 * @param string $euvat EU-vat number to validate 109 * @return boolean The result of the validation 110 */ 111 function vmValidateEUVat( $euvat ){ 112 require_once ( CLASSPATH . 'nusoap/nusoap.php' ); 113 require_once ( CLASSPATH . 'euvatcheck.class.php' ); 114 $GLOBALS['vmLogger']->debug( 'Checking for valid EU VAT ID' ); 115 $vatcheck = new VmEUVatCheck($euvat); 116 return $vatcheck->validvatid; 117 } 118 119 /** 120 * Returns the current time in microseconds 121 * 122 * @return float current time in microseconds 123 */ 124 function utime() 125 { 126 list($usec, $sec) = explode(" ", microtime()); 127 return ((float)$usec + (float)$sec); 128 } 129 130 131 /** 132 * Checks if $item is in $list 133 * 134 * @param array $list 135 * @param string $item 136 * @return mixed An integer representing the postion of $item in $list, false when not in list 137 */ 138 function in_list($list, $item) { 139 for ($i=0;$i<$list["cnt"];$i++) { 140 if (!strcmp($list[$i]["name"],$item)) { 141 return $i; 142 } 143 } 144 return False; 145 } 146 147 /** 148 * reads a file and returns its content as a string 149 * 150 * @param string $file The path to the file that shall be read 151 * @param string $defaultfile The path to the file to is read when $file doesn't exist 152 * @return string The file contents 153 */ 154 function read_file( $file, $defaultfile='' ) { 155 156 // open the HTML file and read it into $html 157 if (file_exists( $file )) { 158 $html_file = fopen( $file, "r" ); 159 } 160 elseif( !empty( $defaultfile ) && file_exists( $defaultfile ) ) { 161 $html_file = fopen( $defaultfile, "r" ); 162 } 163 else { 164 return; 165 } 166 if( $html_file === false ) { 167 $GLOBALS['vmLogger']->err( 'Could not open '.basename( $file ).'.' ); 168 return; 169 } 170 $html = ""; 171 172 while (!feof($html_file)) { 173 $buffer = fgets($html_file, 1024); 174 $html .= $buffer; 175 } 176 fclose ($html_file); 177 178 return( $html ); 179 } 180 181 /** 182 * Includes all needed classes for a core module and create + populate the objects 183 * 184 * @param string $module The name of the virtuemart core module 185 */ 186 function include_class($module) { 187 188 // globalize the vars so that they can be used outside of this function 189 global $VM_LANG, $ps_vendor, $ps_affiliate, $ps_manufacturer, $ps_manufacturer_category, 190 $ps_user, $ps_vendor_category, $ps_checkout, $ps_intershipper, $ps_shipping, $ps_order, $ps_order_status, 191 $ps_product,$ps_product_category , $ps_product_attribute, 192 $ps_product_type, // Changed Product Type 193 $ps_product_type_parameter, // Changed Product Type 194 $ps_product_product_type, // Changed Product Type 195 $ps_product_price, $nh_report, $ps_payment_method, $ps_shopper, $ps_shopper_group, 196 $ps_cart, $ps_zone,$ps_tax, $zw_waiting_list; 197 198 $VM_LANG->load($module); 199 200 switch ( $module ) { 201 202 case "account": 203 break; 204 205 case "admin" : 206 207 // Load class files 208 require_once (CLASSPATH. 'ps_html.php' ); 209 require_once (CLASSPATH. 'ps_function.php' ); 210 require_once (CLASSPATH. 'ps_module.php' ); 211 require_once (CLASSPATH. 'ps_perm.php' ); 212 require_once (CLASSPATH. 'ps_user.php' ); 213 require_once (CLASSPATH. 'ps_user_address.php' ); 214 215 //Instantiate Classes 216 $ps_html = new ps_html; 217 $ps_function = new ps_function; 218 $ps_module= new ps_module; 219 $ps_perm= new ps_perm; 220 $ps_user= new ps_user; 221 $ps_user_address = new ps_user_address; 222 223 break; 224 225 case "checkout" : 226 // Load class file 227 require_once (CLASSPATH. 'ps_checkout.php' ); 228 229 //Instantiate Class 230 //$ps_checkout = new ps_checkout; 231 232 break; 233 234 case "order" : 235 // Load classes 236 require_once (CLASSPATH.'ps_order.php' ); 237 require_once (CLASSPATH.'ps_order_status.php' ); 238 239 // Instantiate Classes 240 $ps_order = new ps_order; 241 $ps_order_status = new ps_order_status; 242 break; 243 244 case "product" : 245 // Load Classes 246 require_once (CLASSPATH.'ps_product.php' ); 247 require_once (CLASSPATH.'ps_product_category.php' ); 248 require_once (CLASSPATH.'ps_product_attribute.php' ); 249 require_once (CLASSPATH.'ps_product_type.php' ); // Changed Product Type 250 require_once (CLASSPATH.'ps_product_type_parameter.php' ); // Changed Product Type 251 require_once (CLASSPATH.'ps_product_product_type.php' ); // Changed Product Type 252 require_once (CLASSPATH.'ps_product_price.php' ); 253 254 // Instantiate Classes 255 $ps_product = new ps_product; 256 $ps_product_category = new ps_product_category; 257 $ps_product_attribute = new ps_product_attribute; 258 $ps_product_type = new ps_product_type; // Changed Product Type 259 $ps_product_type_parameter = new ps_product_type_parameter; // Changed Product Type 260 $ps_product_product_type = new ps_product_product_type; // Changed Product Type 261 $ps_product_price = new ps_product_price; 262 break; 263 264 case "reportbasic" : 265 // Load Classes 266 require_once ( CLASSPATH . 'ps_reportbasic.php'); 267 $nh_report = new nh_report; 268 break; 269 270 case "shipping" : 271 // Load Class 272 require_once ( CLASSPATH . 'ps_shipping.php'); 273 // Instantiate Class 274 $ps_shipping = new ps_shipping; 275 break; 276 277 case "shop" : 278 // Load Classes 279 require_once ( CLASSPATH. 'ps_cart.php' ); 280 require_once ( CLASSPATH. 'zw_waiting_list.php'); 281 282 // Instantiate Classes 283 $ps_cart = new ps_cart; 284 $zw_waiting_list = new zw_waiting_list; 285 break; 286 287 case "shopper" : 288 // Load Classes 289 require_once ( CLASSPATH . 'ps_shopper.php' ); 290 require_once ( CLASSPATH . 'ps_shopper_group.php' ); 291 // Instantiate Classes 292 $ps_shopper = new ps_shopper; 293 $ps_shopper_group = new ps_shopper_group; 294 break; 295 296 case "store" : 297 // Load Classes 298 require_once ( CLASSPATH . 'ps_payment_method.php' ); 299 // Instantiate Classes 300 $ps_payment_method = new ps_payment_method; 301 break; 302 303 case "tax" : 304 // Load Classes 305 require_once ( CLASSPATH . 'ps_tax.php' ); 306 // Instantiate Classes 307 $ps_tax = new ps_tax; 308 break; 309 310 case "vendor" : 311 // Load Classes 312 require_once (CLASSPATH . 'ps_vendor.php' ); 313 require_once (CLASSPATH . 'ps_vendor_category.php' ); 314 // Instantiate Classes 315 $ps_vendor = new ps_vendor; 316 $ps_vendor_category = new ps_vendor_category; 317 break; 318 319 case "zone" : 320 // Load Class 321 require_once (CLASSPATH . 'ps_zone.php'); 322 // Instantiate Class 323 $ps_zone = new ps_zone; 324 break; 325 326 case "manufacturer" : 327 328 require_once (CLASSPATH . 'ps_manufacturer.php'); 329 require_once (CLASSPATH . 'ps_manufacturer_category.php'); 330 $ps_manufacturer = new ps_manufacturer; 331 $ps_manufacturer_category = new ps_manufacturer_category; 332 break; 333 } 334 } 335 336 /** 337 * Login validation function 338 * 339 * Username and encoded password is compared to db entries in the mos_users 340 * table. A successful validation returns true, otherwise false 341 */ 342 function vmCheckPass() { 343 global $database, $perm, $my, $mainframe; 344 345 // only allow access to admins or storeadmins 346 if( $perm->check("admin,storeadmin")) { 347 348 $username = $my->username; 349 $passwd_plain = $passwd = trim( vmGet( $_POST, 'passwd', '' ) ); 350 if( empty( $passwd_plain )) { 351 $GLOBALS['vmLogger']->err( 'Password empty!'); 352 return false; 353 } 354 $passwd = md5( $passwd ); 355 $bypost = 1; 356 if (!$username || !$passwd || $_REQUEST['option'] != "com_virtuemart") { 357 return false; 358 } elseif( vmIsJoomla('1.5') ) { 359 $credentials = array(); 360 $credentials['username'] = $username; 361 $credentials['password'] = $passwd_plain; 362 363 $options = array(); 364 365 jimport( 'joomla.user.authentication'); 366 $authenticate = & JAuthentication::getInstance(); 367 $response = $authenticate->authenticate($credentials, $options); 368 369 if ($response->status === JAUTHENTICATE_STATUS_SUCCESS) { 370 return true; 371 } else { 372 return false; 373 } 374 375 } else { 376 if( vmIsJoomla('1.0.12', '<=', false )) { 377 $database->setQuery( "SELECT id, gid, block, usertype" 378 . "\nFROM #__users" 379 . "\nWHERE username='$username' AND password='$passwd'" 380 ); 381 $row = null; 382 $res = $database->loadObject( $row ); 383 } else { 384 $query = "SELECT id, name, username, password, usertype, block, gid" 385 . "\n FROM #__users" 386 . "\n WHERE username = ". $database->Quote( $username ); 387 $database->setQuery( $query ); 388 $row = null; 389 $database->loadObject( $row ); 390 391 list($hash, $salt) = explode(':', $row->password); 392 $cryptpass = md5($passwd_plain.$salt); 393 $res = $hash == $cryptpass; 394 } 395 if ($res) { 396 return true; 397 } 398 else { 399 $GLOBALS['vmLogger']->err( 'The Password you\'ve entered is not correct for your User Account'); 400 return false; 401 } 402 } 403 } 404 return false; 405 } 406 /** 407 * Formerly used to print a search header for lists 408 * use class listFactory instead 409 * @deprecated 410 * 411 */ 412 function search_header() { 413 echo "### THIS FUNCTION IS DEPRECATED. Use the class listFactory instead. ###"; 414 } 415 /** 416 * Formerly used to print a search header for lists 417 * use class listFactory instead 418 * @deprecated 419 * 420 */ 421 function search_footer() { 422 echo "### THIS FUNCTION IS DEPRECATED. Use the class listFactory instead. ###"; 423 } 424 /** 425 * Used by the frontend adminsitration to save editor field contents 426 * 427 * @param string $editor1 the name of the editor field no. 1 428 * @param string $editor2 the name of the editor field no. 2 429 */ 430 function editorScript($editor1='', $editor2='') { 431 ?> 432 <script type="text/javascript"> 433 function submitbutton(pressbutton) { 434 var form = document.adminForm; 435 if (pressbutton == 'cancel') { 436 submitform( pressbutton ); 437 return; 438 } 439 <?php 440 if ($editor1 != '') { 441 if( vmIsJoomla(1.5) ) { 442 jimport('joomla.html.editor'); 443 $editor = JEditor::getInstance($GLOBALS['mainframe']->getCfg('editor')); 444 echo $editor->getContent('editor1'); 445 } else { 446 getEditorContents( 'editor1', $editor1 ); 447 } 448 } 449 if ($editor2 != '') { 450 if( vmIsJoomla(1.5) ) { 451 jimport('joomla.html.editor'); 452 $editor = JEditor::getInstance($GLOBALS['mainframe']->getCfg('editor')); 453 echo $editor->getContent('editor2'); 454 } else { 455 getEditorContents( 'editor2', $editor2 ); 456 457 } 458 } ?> 459 submitform( pressbutton ); 460 461 } 462 </script><?php 463 } 464 465 /** 466 * Function to create an email object for further use (uses phpMailer) 467 * @param string From e-mail address 468 * @param string From name 469 * @param string E-mail subject 470 * @param string Message body 471 * @return phpMailer Mail object 472 */ 473 function vmCreateMail( $from='', $fromname='', $subject='', $body='' ) { 474 global $mosConfig_absolute_path, $mosConfig_sendmail; 475 global $mosConfig_smtpauth, $mosConfig_smtpuser; 476 global $mosConfig_smtppass, $mosConfig_smtphost; 477 global $mosConfig_mailfrom, $mosConfig_fromname, $mosConfig_mailer; 478 479 $phpmailer_classname='phpmailer'; 480 if( file_exists( $mosConfig_absolute_path . '/libraries/phpmailer/phpmailer.php') ) { 481 $phpmailer_path = $mosConfig_absolute_path . '/libraries/phpmailer/phpmailer.php'; 482 }elseif( file_exists( $mosConfig_absolute_path . '/includes/phpmailer/class.phpmailer.php')) { 483 $phpmailer_path = $mosConfig_absolute_path . '/includes/phpmailer/class.phpmailer.php'; 484 $phpmailer_classname = 'mosphpmailer'; 485 } 486 require_once( $phpmailer_path ); 487 if( class_exists( $phpmailer_classname )) { 488 $mail = new $phpmailer_classname(); 489 } 490 $phpmailer_path = dirname( $phpmailer_path ); 491 $mail->PluginDir = $phpmailer_path .'/'; 492 $mail->SetLanguage( 'en', $phpmailer_path . '/language/' ); 493 $mail->CharSet = vmGetCharset(); 494 $mail->IsMail(); 495 $mail->From = $from ? $from : $mosConfig_mailfrom; 496 $mail->FromName = $fromname ? $fromname : $mosConfig_fromname; 497 $mail->Sender = $from ? $from : $mosConfig_mailfrom; 498 $mail->Mailer = $mosConfig_mailer; 499 500 // Add smtp values if needed 501 if ( $mosConfig_mailer == 'smtp' ) { 502 $mail->SMTPAuth = $mosConfig_smtpauth; 503 $mail->Username = $mosConfig_smtpuser; 504 $mail->Password = $mosConfig_smtppass; 505 $mail->Host = $mosConfig_smtphost; 506 } else 507 508 // Set sendmail path 509 if ( $mosConfig_mailer == 'sendmail' ) { 510 if (isset($mosConfig_sendmail)) 511 $mail->Sendmail = $mosConfig_sendmail; 512 } // if 513 if( $subject ) { 514 $mail->Subject = vmAbstractLanguage::safe_utf8_encode( $subject, $mail->CharSet ); 515 } 516 if( $body) { 517 $mail->Body = $body; 518 } 519 // Patch to get correct Line Endings 520 switch( substr( strtoupper( PHP_OS ), 0, 3 ) ) { 521 case "WIN": 522 $mail->LE = "\r\n"; 523 break; 524 case "MAC": // Mac OS9 525 $mail->LE = "\r"; 526 break; 527 case "DAR": // Mac OSX 528 default: // change nothing 529 break; 530 } 531 return $mail; 532 } 533 534 /** 535 * Mail function (uses phpMailer) 536 * @param string From e-mail address 537 * @param string From name 538 * @param string/array Recipient e-mail address(es) 539 * @param string E-mail subject 540 * @param string Message body 541 * @param boolean false = plain text, true = HTML 542 * @param string/array CC e-mail address(es) 543 * @param string/array BCC e-mail address(es) 544 * @param array Images path,cid,name,filename,encoding,mimetype 545 * @param string/array Attachment file name(s) 546 * @return boolean Mail send success 547 */ 548 function vmMail($from, $fromname, $recipient, $subject, $body, $Altbody='', $mode=false, $cc=NULL, $bcc=NULL, $images=null, $attachment=null, $replyto=null ) { 549 global $mosConfig_debug; 550 551 // Filter from, fromname and subject 552 if (!vmValidateEmail( $from ) || !vmValidateName( $fromname ) || !vmValidateName( $subject )) { 553 return false; 554 } 555 556 $mail = vmCreateMail( $from, $fromname, $subject, $body ); 557 558 if( $Altbody != "" ) { 559 // In this section we take care for utf-8 encoded mails 560 $mail->AltBody = vmAbstractLanguage::safe_utf8_encode( $Altbody, $mail->CharSet ); 561 } 562 563 // activate HTML formatted emails 564 if ( $mode ) { 565 $mail->IsHTML(true); 566 } 567 if( $mail->ContentType == "text/plain" ) { 568 $mail->Body = vmAbstractLanguage::safe_utf8_encode( $mail->Body, $mail->CharSet ); 569 } 570 if( is_array($recipient) ) { 571 foreach ($recipient as $to) { 572 if( vmValidateEmail( $to )) { 573 $mail->AddAddress($to); 574 } 575 } 576 } else { 577 if( vmValidateEmail( $recipient )) { 578 $mail->AddAddress($recipient); 579 } 580 } 581 if (isset($cc)) { 582 if( is_array($cc) ) 583 foreach ($cc as $to) { 584 if( vmValidateEmail( $to )) { 585 $mail->AddCC($to); 586 } 587 } 588 else { 589 if( vmValidateEmail( $cc )) { 590 $mail->AddCC($cc); 591 } 592 } 593 } 594 if (isset($bcc)) { 595 if( is_array($bcc) ) 596 foreach ($bcc as $to) { 597 if( vmValidateEmail( $to )) { 598 $mail->AddBCC($to); 599 } 600 } 601 else { 602 if( vmValidateEmail( $bcc )) { 603 $mail->AddBCC($bcc); 604 } 605 } 606 } 607 if( !empty($replyto) && vmValidateEmail( $replyto )) { 608 $mail->AddReplyTo($replyto); 609 } 610 if( $images ) { 611 foreach( $images as $image) { 612 $mail->AddEmbeddedImage( $image['path'], $image['name'], $image['filename'], $image['encoding'], $image['mimetype']); 613 } 614 } 615 if ($attachment) { 616 if ( is_array($attachment) ) 617 foreach ($attachment as $fname) $mail->AddAttachment($fname); 618 else 619 $mail->AddAttachment($attachment); 620 } 621 $mailssend = $mail->Send(); 622 623 if( $mosConfig_debug ) { 624 //$mosDebug->message( "Mails send: $mailssend"); 625 } 626 if( $mail->error_count > 0 ) { 627 //$mosDebug->message( "The mail message $fromname <$from> about $subject to $recipient <b>failed</b><br /><pre>$body</pre>", false ); 628 //$mosDebug->message( "Mailer Error: " . $mail->ErrorInfo . "" ); 629 } 630 return $mailssend; 631 } 632 633 // $ Id: html_entity_decode.php,v 1.7 2005/01/26 04:55:13 aidan Exp $ 634 if (!defined('ENT_NOQUOTES')) { 635 define('ENT_NOQUOTES', 0); 636 } 637 if (!defined('ENT_COMPAT')) { 638 define('ENT_COMPAT', 2); 639 } 640 if (!defined('ENT_QUOTES')) { 641 define('ENT_QUOTES', 3); 642 } 643 644 /** 645 * Replace html_entity_decode() 646 * 647 * @category PHP 648 * @package PHP_Compat 649 * @link http://php.net/function.html_entity_decode 650 * @author David Irvine <dave@codexweb.co.za> 651 * @author Aidan Lister <aidan@php.net> 652 * @since PHP 4.3.0 653 * @internal Setting the charset will not do anything 654 * @require PHP 4.0.0 (user_error) 655 */ 656 function vmHtmlEntityDecode($string, $quote_style = ENT_COMPAT, $charset = null) { 657 if( function_exists('html_entity_decode')) { 658 return @html_entity_decode( $string, $quote_style, $charset ); 659 } 660 if (!is_int($quote_style) && !is_null($quote_style)) { 661 user_error(__FUNCTION__.'() expects parameter 2 to be long, ' . 662 gettype($quote_style) . ' given', E_USER_WARNING); 663 return; 664 } 665 $trans_tbl = get_html_translation_table(HTML_ENTITIES); 666 $trans_tbl = array_flip($trans_tbl); 667 668 // Add single quote to translation table; 669 $trans_tbl['''] = '\''; 670 671 // Not translating double quotes 672 if ($quote_style & ENT_NOQUOTES) { 673 // Remove double quote from translation table 674 unset($trans_tbl['"']); 675 } 676 677 return strtr($string, $trans_tbl); 678 } 679 /** 680 * Unescapes REQUEST values if magic_quotes_gpc is set 681 * 682 * @param string $string The string to strip slashes from 683 * @return string 684 * @since 1.1.0 685 */ 686 function vmGetUnEscaped( $string ) { 687 if (get_magic_quotes_gpc()==1) { 688 // if (ini_get('magic_quotes_sybase')) return str_replace("''","'",$string); 689 return ( stripslashes( $string )); // this does not handle it correctly if magic_quotes_sybase is ON. 690 } else { 691 return ( $string ); 692 } 693 } 694 695 /** 696 * Reads a file and sends them in chunks to the browser 697 * This should overcome memory problems 698 * http://www.php.net/manual/en/function.readfile.php#54295 699 * 700 * @since 1.0.3 701 * @param string $filename 702 * @param boolean $retbytes 703 * @return mixed 704 */ 705 function vmReadFileChunked($filename,$retbytes=true) { 706 $chunksize = 1*(1024*1024); // how many bytes per chunk 707 $buffer = ''; 708 $cnt =0; 709 // $handle = fopen($filename, 'rb'); 710 $handle = fopen($filename, 'rb'); 711 if ($handle === false) { 712 return false; 713 } 714 // Prevent time outs on big files 715 @set_time_limit(0); 716 // PHP on Windows has a useless "usleep" function until 5.0.0 717 if( substr( strtoupper( PHP_OS ), 0, 3 ) == 'WIN' && version_compare( phpversion(), '5.0' ) < 0 ) { 718 $sleepfunc = 'sleep'; 719 $time = 1; // sec. 720 } else { 721 $sleepfunc = 'usleep'; 722 $time = 100; // msec. 723 } 724 while (!feof($handle)) { 725 $buffer = fread($handle, $chunksize); 726 echo $buffer; 727 $sleepfunc($time); 728 @ob_flush(); 729 flush(); 730 if ($retbytes) { 731 $cnt += strlen($buffer); 732 } 733 } 734 $status = fclose($handle); 735 if ($retbytes && $status) { 736 return $cnt; // return num. bytes delivered like readfile() does. 737 } 738 return $status; 739 } 740 741 /** 742 * Returns the charset string from the global _ISO constant 743 * 744 * @return string UTF-8 by default 745 * @since 1.0.5 746 */ 747 function vmGetCharset() { 748 $iso = explode( '=', @constant('_ISO') ); 749 if( !empty( $iso[1] )) { 750 return $iso[1]; 751 } 752 else { 753 return 'UTF-8'; 754 } 755 } 756 /** 757 * Create a file system - safe file name 758 * 759 * @param string $filename 760 * @since 1.1.0 761 */ 762 function vmSafeFileName( $filename ) { 763 764 $filename = preg_replace('/[^a-zA-Z0-9\.]/', '_', $filename ); 765 return $filename; 766 } 767 function vmIsAdminMode() { 768 global $page; 769 return ( (defined( '_VM_IS_BACKEND' ) 770 || @$_REQUEST['pshop_mode'] == 'admin' 771 || strstr($page,'_list') 772 || strstr($page,'_print') 773 || strstr($page,'_cfg') 774 || strstr($page,'_form')) 775 && ( strncmp('account.',$page, 8) !== 0 776 && strncmp('checkout.',$page, 9) !== 0 777 && strncmp('shop.',$page, 5) !== 0 778 ) 779 ); 780 } 781 782 783 function vmCreateHash( $seed='virtuemart' ) { 784 return md5( ENCODE_KEY . md5( $seed ) ); 785 } 786 787 /** 788 * Generate a random password 789 * 790 * @static 791 * @param int $length Length of the password to generate 792 * @return string Random Password 793 * @since 1.1 794 */ 795 function vmGenRandomPassword($length = 8) 796 { 797 $salt = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 798 $len = strlen($salt); 799 $makepass = ''; 800 mt_srand(10000000 * (double) microtime()); 801 802 for ($i = 0; $i < $length; $i ++) { 803 $makepass .= $salt[mt_rand(0, $len -1)]; 804 } 805 806 return $makepass; 807 } 808 809 810 /** 811 * Equivalent to Joomla's josSpoofCheck function 812 * @author Joomla core team 813 * 814 * @param boolean $header 815 * @param unknown_type $alt 816 */ 817 function vmSpoofCheck( $header=NULL, $alt=NULL ) { 818 global $vm_mainframe; 819 if( !empty( $_GET['vmtoken']) || !empty( $_POST['vmtoken'])) { 820 $validate_hash = vmGet( $_REQUEST, 'vmtoken', null ); 821 $validate = vmSpoofValue($alt) == $validate_hash; 822 } else { 823 $validate = vmGet( $_REQUEST, vmSpoofValue($alt), 0 ); 824 } 825 826 // probably a spoofing attack 827 if (!$validate) { 828 header( 'HTTP/1.0 403 Forbidden' ); 829 $vm_mainframe->errorAlert( 'Sorry, but we could not verify your Security Token.\nGo back and try again please.' ); 830 return false; 831 } 832 833 // First, make sure the form was posted from a browser. 834 // For basic web-forms, we don't care about anything 835 // other than requests from a browser: 836 if (!isset( $_SERVER['HTTP_USER_AGENT'] )) { 837 header( 'HTTP/1.0 403 Forbidden' ); 838 $vm_mainframe->errorAlert( 'Sorry, but we could not identify your web browser.\nBut this is necessary for using this web page.' ); 839 return false; 840 } 841 /* //NOTE: this is not really necessary, because GET request should also be allowed. 842 // Make sure the request was done using "POST" 843 if (!$_SERVER['REQUEST_METHOD'] == 'POST' ) { 844 header( 'HTTP/1.0 403 Forbidden' ); 845 $vm_mainframe->errorAlert( $VM_LANG->_('NOT_AUTH') ); 846 return false; 847 } 848 */ 849 if ($header) { 850 // Attempt to defend against header injections: 851 $badStrings = array( 852 'Content-Type:', 853 'MIME-Version:', 854 'Content-Transfer-Encoding:', 855 'bcc:', 856 'cc:' 857 ); 858 859 // Loop through each POST'ed value and test if it contains 860 // one of the $badStrings: 861 foreach ($_POST as $k => $v){ 862 foreach ($badStrings as $v2) { 863 if (strpos( $v, $v2 ) !== false) { 864 header( "HTTP/1.0 403 Forbidden" ); 865 $vm_mainframe->errorAlert( 'We are sorry, but using E-Mail Headers in Fields is not allowed.' ); 866 return false; 867 } 868 } 869 } 870 871 // Made it past spammer test, free up some memory 872 // and continue rest of script: 873 unset($k, $v, $v2, $badStrings); 874 } 875 return true; 876 } 877 /** 878 * Equivalent to Joomla's josSpoofValue function 879 * 880 * @param boolean $alt 881 * @return string Validation Hash 882 */ 883 function vmSpoofValue($alt=NULL) { 884 global $auth, $mainframe, $_VERSION; 885 886 if ($alt) { 887 if ( $alt == 1 ) { 888 $random = date( 'Ymd' ); 889 } else { 890 $random = $alt . date( 'Ymd' ); 891 } 892 } else { 893 $random = date( 'dmY' ); 894 } 895 $validate = vmCreateHash( $mainframe->getCfg( 'db' ) . $random . $auth['user_id']); 896 897 if( $_VERSION->DEV_LEVEL >= 11 ) { 898 // Joomla 1.0.11 compatibility workaround 899 // the prefix ensures that the hash is non-numeric 900 // otherwise it will be intercepted by globals.php 901 $validate = 'j' . $validate; 902 } 903 904 return $validate; 905 } 906 907 /** 908 * This function creates the superglobal variable $product_currency 909 * This variable is used for currency conversion 910 * 911 */ 912 function vmSetGlobalCurrency(){ 913 global $vendor_accepted_currencies, $vendor_currency, $vmLogger; 914 915 if( !defined('_VM_IS_BACKEND') && empty( $_REQUEST['ajax_request']) && empty($_REQUEST['pshop_mode'])) { 916 if( isset( $_REQUEST['product_currency']) ) { 917 $GLOBALS['product_currency'] = $_SESSION['product_currency'] = vmGet($_REQUEST, 'product_currency' ); 918 } 919 } 920 $GLOBALS['product_currency'] = vmGet($_SESSION, 'product_currency', $vendor_currency); 921 922 // Check if the selected currency is accepted! (the vendor currency is always accepted) 923 if( $GLOBALS['product_currency'] != $vendor_currency ) { 924 if( empty( $vendor_accepted_currencies )) { 925 $vendor_accepted_currencies = $vendor_currency; 926 } 927 $page = vmGet($_REQUEST,'page'); 928 $acceptedCurrencies = explode(',', $vendor_accepted_currencies ); 929 if( !in_array( $GLOBALS['product_currency'], $acceptedCurrencies) 930 && (stristr( $page, 'checkout.') || stristr( $page, 'account.') || stristr( $page, 'shop.cart')) ) { 931 // Fallback to global vendor currency (as set in the store form) 932 $vmLogger->warning( 'The Currency you had selected ('.$GLOBALS['product_currency'].') is not accepted for Checkout.'); 933 $GLOBALS['product_currency'] = $vendor_currency; 934 } 935 } 936 } 937 938 function vmIsJoomla( $version='', $operator='=', $compare_minor_versions=true) { 939 global $_VERSION; 940 $this_version = ''; 941 if( !empty($_VERSION) && is_object($_VERSION)) { 942 $jversion =& $_VERSION; 943 $this_version = $jversion->RELEASE; 944 } 945 elseif ( defined('JVERSION')) { 946 $jversion = new JVersion(); 947 $this_version = $jversion->RELEASE; 948 } else { 949 include_once( $GLOBALS['mosConfig_absolute_path'].'/includes/version.php' ); 950 $jversion =& $_VERSION; 951 $this_version = $jversion->RELEASE; 952 } 953 if( !$compare_minor_versions ) $this_version .= '.'. $jversion->DEV_LEVEL; 954 if( empty( $version ) ) { 955 return !empty($this_version) && strtolower($jversion->PRODUCT) == 'joomla!'; 956 } 957 $allowed_operators = array( '<', 'lt', '<=', 'le', '>', 'gt', '>=', 'ge', '==', '=', 'eq', '!=', '<>', 'ne' ); 958 959 if( $compare_minor_versions ) { 960 if( $jversion->RELEASE != $version ) { 961 return false; 962 } 963 } 964 if( in_array($operator, $allowed_operators )) { 965 return version_compare( $this_version, $version, $operator ); 966 } 967 return false; 968 } 969 function vmIsHttpsMode() { 970 return ($_SERVER['SERVER_PORT'] == 443 || @$_SERVER['HTTPS'] == 'on'); 971 } 972 /** 973 * Checks if the Request is a XML HTTP Request (via Ajax) 974 * @since 1.1.1 975 * @return boolean 976 */ 977 function vmIsXHR() { 978 return strtolower(vmGet($_SERVER,'HTTP_X_REQUESTED_WITH')) == 'xmlhttprequest' 979 || vmGet($_REQUEST,'ajax_request') == '1'; 980 } 981 /** 982 * Utility function redirect the browser location to another url 983 * 984 * Can optionally provide a message. 985 * @param string The URL to redirect to 986 * @param string A Message to display to the user 987 */ 988 function vmRedirect( $url, $msg='' ) { 989 if( function_exists('mosRedirect')) { 990 mosRedirect($url, $msg ); 991 } elseif( vmIsJoomla( '1.5', '>=' ) ) { 992 global $mainframe; 993 $mainframe->redirect( $url, $msg ); 994 } else { 995 global $mainframe; 996 997 // specific filters 998 $iFilter = vmInputFilter::getInstance(); 999 $url = $iFilter->process( $url ); 1000 if (!empty($msg)) { 1001 $msg = $iFilter->process( $msg ); 1002 } 1003 1004 // Strip out any line breaks and throw away the rest 1005 $url = preg_split("/[\r\n]/", $url); 1006 $url = $url[0]; 1007 1008 if ($iFilter->badAttributeValue( array( 'href', $url ))) { 1009 $url = $GLOBALS['mosConfig_live_site']; 1010 } 1011 1012 if (trim( $msg )) { 1013 if (strpos( $url, '?' )) { 1014 $url .= '&mosmsg=' . urlencode( $msg ); 1015 } else { 1016 $url .= '?mosmsg=' . urlencode( $msg ); 1017 } 1018 } 1019 1020 if (headers_sent()) { 1021 echo '<script type="text/javascript">document.location.href=\''.$url.'\';</script>'; 1022 } else { 1023 @ob_end_clean(); // clear output buffer 1024 header( 'HTTP/1.1 301 Moved Permanently' ); 1025 header( "Location: ". $url ); 1026 } 1027 $GLOBALS['vm_mainframe']->close(true); 1028 } 1029 } 1030 /** 1031 * Raise the memory limit when it is lower than the needed value 1032 * 1033 * @param string $setLimit Example: 16M 1034 */ 1035 function vmRaiseMemoryLimit( $setLimit ) { 1036 1037 $memLimit = @ini_get('memory_limit'); 1038 1039 if( stristr( $memLimit, 'k') ) { 1040 $memLimit = str_replace( 'k', '', str_replace( 'K', '', $memLimit )) * 1024; 1041 } 1042 elseif( stristr( $memLimit, 'm') ) { 1043 $memLimit = str_replace( 'm', '', str_replace( 'M', '', $memLimit )) * 1024 * 1024; 1044 } 1045 1046 if( stristr( $setLimit, 'k') ) { 1047 $setLimitB = str_replace( 'k', '', str_replace( 'K', '', $setLimit )) * 1024; 1048 } 1049 elseif( stristr( $setLimit, 'm') ) { 1050 $setLimitB = str_replace( 'm', '', str_replace( 'M', '', $setLimit )) * 1024 * 1024; 1051 } 1052 1053 if( $memLimit < $setLimitB ) { 1054 @ini_set('memory_limit', $setLimit ); 1055 } 1056 } 1057 /** 1058 * Returns a formatted date 1059 * 1060 * @param int $time TimeStamp format 1061 * @param String $dateformat strftime Format String 1062 * @return String 1063 */ 1064 function vmFormatDate( $time=0, $dateformat='' ) { 1065 global $vendor_date_format; 1066 if( empty($time)) $time = time(); 1067 1068 if( vmIsJoomla('1.5') ) { 1069 if( empty( $dateformat )) { 1070 return JHTML::_('date', $time, $vendor_date_format); 1071 } else { 1072 return JHTML::_('date', $time, $dateformat); 1073 } 1074 } else { 1075 if( empty( $dateformat )) { 1076 return strftime( $vendor_date_format, $time ); 1077 } else { 1078 return strftime( $dateformat, $time ); 1079 } 1080 } 1081 } 1082 /** 1083 * Function to strip additional / or \ in a path name 1084 * @param string The path 1085 * @param boolean Add trailing slash 1086 */ 1087 function vmPathName($p_path,$p_addtrailingslash = true) { 1088 $retval = ""; 1089 1090 $isWin = (substr(PHP_OS, 0, 3) == 'WIN'); 1091 1092 if ($isWin) { 1093 $retval = str_replace( '/', '\\', $p_path ); 1094 if ($p_addtrailingslash) { 1095 if (substr( $retval, -1 ) != '\\') { 1096 $retval .= '\\'; 1097 } 1098 } 1099 1100 // Check if UNC path 1101 $unc = substr($retval,0,2) == '\\\\' ? 1 : 0; 1102 1103 // Remove double \\ 1104 $retval = str_replace( '\\\\', '\\', $retval ); 1105 1106 // If UNC path, we have to add one \ in front or everything breaks! 1107 if ( $unc == 1 ) { 1108 $retval = '\\'.$retval; 1109 } 1110 } else { 1111 $retval = str_replace( '\\', '/', $p_path ); 1112 if ($p_addtrailingslash) { 1113 if (substr( $retval, -1 ) != '/') { 1114 $retval .= '/'; 1115 } 1116 } 1117 1118 // Check if UNC path 1119 $unc = substr($retval,0,2) == '//' ? 1 : 0; 1120 1121 // Remove double // 1122 $retval = str_replace('//','/',$retval); 1123 1124 // If UNC path, we have to add one / in front or everything breaks! 1125 if ( $unc == 1 ) { 1126 $retval = '/'.$retval; 1127 } 1128 } 1129 1130 return $retval; 1131 } 1132 /** 1133 * Utility function to read the files in a directory 1134 * @param string The file system path 1135 * @param string A filter for the names 1136 * @param boolean Recurse search into sub-directories 1137 * @param boolean True if to prepend the full path to the file name 1138 */ 1139 function vmReadDirectory( $path, $filter='.', $recurse=false, $fullpath=false ) { 1140 $arr = array(); 1141 if (!@is_dir( $path )) { 1142 return $arr; 1143 } 1144 $handle = opendir( $path ); 1145 1146 while ($file = readdir($handle)) { 1147 $dir = vmPathName( $path.'/'.$file, false ); 1148 $isDir = is_dir( $dir ); 1149 if (($file != ".") && ($file != "..")) { 1150 if (preg_match( "/$filter/", $file )) { 1151 if ($fullpath) { 1152 $arr[] = trim( vmPathName( $path.'/'.$file, false ) ); 1153 } else { 1154 $arr[] = trim( $file ); 1155 } 1156 } 1157 if ($recurse && $isDir) { 1158 $arr2 = vmReadDirectory( $dir, $filter, $recurse, $fullpath ); 1159 $arr = array_merge( $arr, $arr2 ); 1160 } 1161 } 1162 } 1163 closedir($handle); 1164 asort($arr); 1165 return $arr; 1166 } 1167 /** 1168 * Helper Function to completely remove a subdirectory 1169 * 1170 * @param string $dirname 1171 * @return boolean 1172 */ 1173 function vmRemoveDirectoryR( $dirname ) { 1174 if ($dirHandle = opendir($dirname)){ 1175 $old_cwd = getcwd(); 1176 chdir($dirname); 1177 while ($file = readdir($dirHandle)){ 1178 if ($file == '.' || $file == '..') continue; 1179 if (is_dir($file)){ 1180 if (!vmRemoveDirectoryR($file)) return false; 1181 }else{ 1182 if (!@unlink($file)) return false; 1183 } 1184 } 1185 closedir($dirHandle); 1186 chdir($old_cwd); 1187 if (!@rmdir($dirname)) return false; 1188 return true; 1189 }else{ 1190 return false; 1191 } 1192 } 1193 /** 1194 * Utility function to return a value from a named array or a specified default 1195 * 1196 * @static 1197 * @param array $array A named array 1198 * @param string $name The key to search for 1199 * @param mixed $default The default value to give if no key found 1200 * @param string $type Return type for the variable (INT, FLOAT, STRING, WORD, BOOLEAN, ARRAY) 1201 * @return mixed The value from the source array 1202 * @since 1.1 1203 */ 1204 function vmGetArrayValue(&$array, $name, $default=null, $type='') { 1205 // Initialize variables 1206 $result = null; 1207 1208 if (isset ($array[$name])) { 1209 $result = $array[$name]; 1210 } 1211 1212 // Handle the default case 1213 if ((is_null($result))) { 1214 $result = $default; 1215 } 1216 1217 // Handle the type constraint 1218 switch (strtoupper($type)) { 1219 case 'INT' : 1220 case 'INTEGER' : 1221 // Only use the first integer value 1222 @ preg_match('/-?[0-9]+/', $result, $matches); 1223 $result = @ (int) $matches[0]; 1224 break; 1225 1226 case 'FLOAT' : 1227 case 'DOUBLE' : 1228 // Only use the first floating point value 1229 @ preg_match('/-?[0-9]+(\.[0-9]+)?/', $result, $matches); 1230 $result = @ (float) $matches[0]; 1231 break; 1232 1233 case 'BOOL' : 1234 case 'BOOLEAN' : 1235 $result = (bool) $result; 1236 break; 1237 1238 case 'ARRAY' : 1239 if (!is_array($result)) { 1240 $result = array ($result); 1241 } 1242 break; 1243 1244 case 'STRING' : 1245 $result = (string) $result; 1246 break; 1247 1248 case 'WORD' : 1249 $result = (string) preg_replace( '#\W#', '', $result ); 1250 break; 1251 1252 case 'NONE' : 1253 default : 1254 // No casting necessary 1255 break; 1256 } 1257 return $result; 1258 } 1259 1260 function vmGetCleanArrayFromKeyword( $keyword ) { 1261 global $database; 1262 $keywordArr = array(); 1263 1264 if( empty( $keyword )) return $keywordArr; 1265 1266 $keywords = explode( " ", $keyword, 10 ); 1267 1268 foreach( $keywords as $searchstring ) { 1269 $searchstring = trim( stripslashes($searchstring) ); 1270 $strlen = strlen($searchstring); 1271 if( $strlen > 2 ) { 1272 /*if( $searchstring[0] == "\"" || $searchstring[0]=="'" ) { 1273 $searchstring[0] = " "; 1274 } 1275 if( $searchstring[strlen($searchstring)-1] == "\"" || $searchstring[strlen($searchstring)-1]=="'" ) { 1276 $searchstring[strlen($searchstring)-1] = " "; 1277 }*/ 1278 $searchstring = $database->getEscaped( $searchstring ); 1279 $searchstring = str_replace('\"', '"', $searchstring ); 1280 1281 $keywordArr[] = $searchstring; 1282 } 1283 } 1284 return $keywordArr; 1285 } 1286 /** 1287 * Replaces & with & for xhtml compliance 1288 * 1289 * Needed to handle unicode conflicts due to unicode conflicts 1290 */ 1291 function vmAmpReplace( $text ) { 1292 $text = str_replace( '&&', '*--*', $text ); 1293 $text = str_replace( '&#', '*-*', $text ); 1294 $text = str_replace( '&', '&', $text ); 1295 $text = preg_replace( '|&(?![\w]+;)|', '&', $text ); 1296 $text = str_replace( '*-*', '&#', $text ); 1297 $text = str_replace( '*--*', '&&', $text ); 1298 1299 return $text; 1300 } 1301 1302 /** 1303 * Converts array to integer values 1304 * 1305 * @param array 1306 * @param int A default value to assign if $array is not an array 1307 * @return array 1308 */ 1309 function vmArrayToInts( &$array, $default=null ) { 1310 if (is_array( $array )) { 1311 foreach( $array as $key => $value ) { 1312 $array[$key] = (int) $value; 1313 } 1314 } else { 1315 if (is_null( $default )) { 1316 $array = array(); 1317 return array(); // Kept for backwards compatibility 1318 } else { 1319 $array = array( (int) $default ); 1320 return array( $default ); // Kept for backwards compatibility 1321 } 1322 } 1323 } 1324 function vmRoute( $nonSefUrl) { 1325 if (class_exists('JApplication')) { // J 1.5 1326 $nonSefUrl = str_replace( '&', '&', $nonSefUrl); 1327 $nonSefUrl = str_replace( JURI::base(), '', $nonSefUrl); // you are adding & and mosConfig_live_site to urls, but it is actually the role of the sef function to do this. So we have to remove them, otherwise Joomla router will not accept to sef-y the url 1328 $url = JRoute::_( $nonSefUrl); 1329 } else { // J 1.0 1330 $url = sefRelToAbs( $nonSefUrl); 1331 } 1332 return $url; 1333 } 1334 ?>
titolo
Descrizione
Corpo
titolo
Descrizione
Corpo
titolo
Descrizione
Corpo
titolo
Corpo
| Generato il: Mon Oct 19 20:29:27 2009 | Generato con PHPXref 0.7 |