PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/joomla/libraries/joomla/utilities/utility.php

https://github.com/bhar1red/anahita
PHP | 182 lines | 74 code | 18 blank | 90 comment | 5 complexity | 765004492cec455d5c645c8abb5b92ea MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id: utility.php 14401 2010-01-26 14:10:00Z louis $
  4. * @package Joomla.Framework
  5. * @subpackage Utilities
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant to the
  9. * GNU General Public License, and as distributed it includes or is derivative
  10. * of works licensed under the GNU General Public License or other free or open
  11. * source software licenses. See COPYRIGHT.php for copyright notices and
  12. * details.
  13. */
  14. // Check to ensure this file is within the rest of the framework
  15. defined('JPATH_BASE') or die();
  16. /**
  17. * JUtility is a utility functions class
  18. *
  19. * @static
  20. * @package Joomla.Framework
  21. * @subpackage Utilities
  22. * @since 1.5
  23. */
  24. class JUtility
  25. {
  26. /**
  27. * Mail function (uses phpMailer)
  28. *
  29. * @param string $from From e-mail address
  30. * @param string $fromname From name
  31. * @param mixed $recipient Recipient e-mail address(es)
  32. * @param string $subject E-mail subject
  33. * @param string $body Message body
  34. * @param boolean $mode false = plain text, true = HTML
  35. * @param mixed $cc CC e-mail address(es)
  36. * @param mixed $bcc BCC e-mail address(es)
  37. * @param mixed $attachment Attachment file name(s)
  38. * @param mixed $replyto Reply to email address(es)
  39. * @param mixed $replytoname Reply to name(s)
  40. * @return boolean True on success
  41. */
  42. static public function sendMail($from, $fromname, $recipient, $subject, $body, $mode=0, $cc=null, $bcc=null, $attachment=null, $replyto=null, $replytoname=null )
  43. {
  44. // Get a JMail instance
  45. $mail =& JFactory::getMailer();
  46. $mail->setSender(array($from, $fromname));
  47. $mail->setSubject($subject);
  48. $mail->setBody($body);
  49. // Are we sending the email as HTML?
  50. if ( $mode ) {
  51. $mail->IsHTML(true);
  52. }
  53. $mail->addRecipient($recipient);
  54. $mail->addCC($cc);
  55. $mail->addBCC($bcc);
  56. $mail->addAttachment($attachment);
  57. // Take care of reply email addresses
  58. if( is_array( $replyto ) ) {
  59. $numReplyTo = count($replyto);
  60. for ( $i=0; $i < $numReplyTo; $i++){
  61. $mail->addReplyTo( array($replyto[$i], $replytoname[$i]) );
  62. }
  63. } elseif( isset( $replyto ) ) {
  64. $mail->addReplyTo( array( $replyto, $replytoname ) );
  65. }
  66. return $mail->Send();
  67. }
  68. /**
  69. * Sends mail to administrator for approval of a user submission
  70. *
  71. * @param string $adminName Name of administrator
  72. * @param string $adminEmail Email address of administrator
  73. * @param string $email [NOT USED TODO: Deprecate?]
  74. * @param string $type Type of item to approve
  75. * @param string $title Title of item to approve
  76. * @param string $author Author of item to approve
  77. * @return boolean True on success
  78. */
  79. static public function sendAdminMail( $adminName, $adminEmail, $email, $type, $title, $author, $url = null )
  80. {
  81. $subject = JText::_( 'User Submitted' ) ." '". $type ."'";
  82. $message = sprintf ( JText::_( 'MAIL_MSG_ADMIN' ), $adminName, $type, $title, $author, $url, $url, 'administrator', $type);
  83. $message .= JText::_( 'MAIL_MSG') ."\n";
  84. // Get a JMail instance
  85. $mail =& JFactory::getMailer();
  86. $mail->addRecipient($adminEmail);
  87. $mail->setSubject($subject);
  88. $mail->setBody($message);
  89. return $mail->Send();
  90. }
  91. /**
  92. * Provides a secure hash based on a seed
  93. *
  94. * @param string Seed string
  95. * @return string
  96. */
  97. static public function getHash( $seed )
  98. {
  99. $conf =& JFactory::getConfig();
  100. return md5( $conf->getValue('config.secret') . $seed );
  101. }
  102. /**
  103. * Method to determine a hash for anti-spoofing variable names
  104. *
  105. * @return string Hashed var name
  106. * @since 1.5
  107. * @static
  108. */
  109. static public function getToken($forceNew = false)
  110. {
  111. $user = &JFactory::getUser();
  112. $session = &JFactory::getSession();
  113. $hash = JUtility::getHash( $user->get( 'id', 0 ).$session->getToken( $forceNew ) );
  114. return $hash;
  115. }
  116. /**
  117. * Method to extract key/value pairs out of a string with xml style attributes
  118. *
  119. * @param string $string String containing xml style attributes
  120. * @return array Key/Value pairs for the attributes
  121. * @since 1.5
  122. */
  123. static public function parseAttributes( $string )
  124. {
  125. //Initialize variables
  126. $attr = array();
  127. $retarray = array();
  128. // Lets grab all the key/value pairs using a regular expression
  129. preg_match_all( '/([\w:-]+)[\s]?=[\s]?"([^"]*)"/i', $string, $attr );
  130. if (is_array($attr))
  131. {
  132. $numPairs = count($attr[1]);
  133. for($i = 0; $i < $numPairs; $i++ )
  134. {
  135. $retarray[$attr[1][$i]] = $attr[2][$i];
  136. }
  137. }
  138. return $retarray;
  139. }
  140. /**
  141. * Method to determine if the host OS is Windows
  142. *
  143. * @return true if Windows OS
  144. * @since 1.5
  145. * @static
  146. */
  147. static public function isWinOS() {
  148. return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
  149. }
  150. /**
  151. * Method to dump the structure of a variable for debugging purposes
  152. *
  153. * @param mixed A variable
  154. * @param boolean True to ensure all characters are htmlsafe
  155. * @return string
  156. * @since 1.5
  157. * @static
  158. */
  159. static public function dump( &$var, $htmlSafe = true )
  160. {
  161. $result = var_export( $var, true );
  162. return '<pre>'.( $htmlSafe ? htmlspecialchars( $result ) : $result).'</pre>';
  163. }
  164. }