PageRenderTime 33ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_hwdmediashare/libraries/utilities.php

https://gitlab.com/ppapadatis/Videolearn
PHP | 307 lines | 173 code | 36 blank | 98 comment | 29 complexity | e5e0a87b2906ef470ffd04b9c7528f27 MD5 | raw file
  1. <?php
  2. /**
  3. * @version SVN $Id: utilities.php 709 2012-10-26 09:07:01Z dhorsfall $
  4. * @package hwdMediaShare
  5. * @copyright Copyright (C) 2012 Highwood Design Limited. All rights reserved.
  6. * @license GNU General Public License http://www.gnu.org/copyleft/gpl.html
  7. * @author Dave Horsfall
  8. * @since 20-Mar-2012 10:29:20
  9. */
  10. // No direct access to this file
  11. defined('_JEXEC') or die('Restricted access');
  12. /**
  13. * hwdMediaShare framework upload class
  14. *
  15. * @package hwdMediaShare
  16. * @since 0.1
  17. */
  18. class hwdMediaShareUtilities extends JObject
  19. {
  20. /**
  21. * Callback for escaping.
  22. *
  23. * @var string
  24. */
  25. protected $escape = 'htmlspecialchars';
  26. /**
  27. * Callback for escaping.
  28. *
  29. * @var string
  30. * @deprecated use $escape declare as private
  31. */
  32. protected $_escape = 'htmlspecialchars';
  33. /**
  34. * Charset to use in escaping mechanisms; defaults to urf8 (UTF-8)
  35. *
  36. * @var string
  37. */
  38. protected $charset = 'UTF-8';
  39. /**
  40. * Charset to use in escaping mechanisms; defaults to urf8 (UTF-8)
  41. *
  42. * @var string
  43. * @deprecated use $charset declare as private
  44. */
  45. protected $_charset = 'UTF-8';
  46. /**
  47. * Class constructor.
  48. *
  49. * @param array $config A configuration array including optional elements.
  50. *
  51. * @since 0.1
  52. */
  53. public function __construct($config = array())
  54. {
  55. }
  56. /**
  57. * Returns the hwdMediaShareRemote object, only creating it if it
  58. * doesn't already exist.
  59. *
  60. * @return hwdMediaShareMedia A hwdMediaShareRemote object.
  61. * @since 0.1
  62. */
  63. public static function getInstance()
  64. {
  65. static $instance;
  66. if (!isset ($instance))
  67. {
  68. $c = 'hwdMediaShareUtilities';
  69. $instance = new $c;
  70. }
  71. return $instance;
  72. }
  73. /**
  74. * Escapes a value for output in a view script.
  75. *
  76. * If escaping mechanism is either htmlspecialchars or htmlentities, uses
  77. * {@link $_encoding} setting.
  78. *
  79. * @param mixed $var The output to escape.
  80. *
  81. * @return mixed The escaped value.
  82. *
  83. * @since 11.1
  84. */
  85. public function escape($var)
  86. {
  87. if (in_array($this->_escape, array('htmlspecialchars', 'htmlentities')))
  88. {
  89. return call_user_func($this->_escape, $var, ENT_COMPAT, $this->_charset);
  90. }
  91. return call_user_func($this->_escape, $var);
  92. }
  93. /**
  94. * Method to validate an email
  95. *
  96. * @since 0.1
  97. */
  98. public function validateEmail($data, $strict = false)
  99. {
  100. $regex = $strict ? '/^([.0-9a-z_-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i' : '/^([*+!.&#$¦\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i';
  101. if(preg_match($regex, JString::trim($data), $matches))
  102. {
  103. return array($matches[1], $matches[2]);
  104. }
  105. else
  106. {
  107. return false;
  108. }
  109. }
  110. /**
  111. * Method to validate an url
  112. *
  113. * @since 0.1
  114. */
  115. public function validateUrl($url)
  116. {
  117. return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
  118. }
  119. /**
  120. * Method to get the URL of an avatar
  121. *
  122. * @since 0.1
  123. */
  124. public function getAvatar($user)
  125. {
  126. $hwdms = hwdMediaShareFactory::getInstance();
  127. $config = $hwdms->getConfig();
  128. if ($config->get('community_avatar') == 'cb')
  129. {
  130. $db =& JFactory::getDBO();
  131. $query = $db->getQuery(true);
  132. $query->select('avatar');
  133. $query->from('#__comprofiler');
  134. $query->where('user_id = '.$user->id);
  135. $db->setQuery($query);
  136. $db->query();
  137. $cbAvatar = $db->loadResult();
  138. if (!empty($cbAvatar) && file_exists(JPATH_ROOT.'/images/comprofiler/tn'.$cbAvatar))
  139. {
  140. return JURI::root( true ).'/images/comprofiler/tn'.$cbAvatar;
  141. }
  142. else if (!empty($cbAvatar) && file_exists(JPATH_ROOT.'/images/comprofiler/'.$cbAvatar))
  143. {
  144. return JURI::root( true ).'/images/comprofiler/'.$cbAvatar;
  145. }
  146. }
  147. else if ($config->get('community_avatar') == 'jomsocial')
  148. {
  149. include_once(JPATH_ROOT.'/components/com_community/libraries/core.php');
  150. $JSUser = CFactory::getUser($user->id);
  151. return $JSUser->getThumbAvatar();
  152. }
  153. else if ($config->get('community_avatar') == 'gravatar' && isset($user->email))
  154. {
  155. return "http://www.gravatar.com/avatar/".md5( strtolower( trim( $user->email ) ) );
  156. }
  157. else if ($config->get('community_avatar') == 'jomwall')
  158. {
  159. include_once (JPATH_ROOT.DS.'components'.DS .'com_awdwall' .DS . 'helpers' . DS . 'user.php');
  160. return AwdwallHelperUser::getBigAvatar51($user->id);
  161. }
  162. if (!isset($user->key))
  163. {
  164. // Load user
  165. JTable::addIncludePath(JPATH_ADMINISTRATOR.'/components/com_hwdmediashare/tables');
  166. $table =& JTable::getInstance('UserChannel', 'hwdMediaShareTable');
  167. $table->load( $user->id );
  168. $properties = $table->getProperties(1);
  169. $user = JArrayHelper::toObject($properties, 'JObject');
  170. }
  171. if (isset($user->key))
  172. {
  173. hwdMediaShareFactory::load('files');
  174. hwdMediaShareFiles::getLocalStoragePath();
  175. $folders = hwdMediaShareFiles::getFolders($user->key);
  176. $filename = hwdMediaShareFiles::getFilename($user->key, 10);
  177. $ext = hwdMediaShareFiles::getExtension($user, 10);
  178. $path = hwdMediaShareFiles::getPath($folders, $filename, $ext);
  179. if (file_exists($path))
  180. {
  181. return hwdMediaShareFiles::getUrl($folders, $filename, $ext);
  182. }
  183. }
  184. return JURI::root( true ).'/media/com_hwdmediashare/assets/images/default-avatar.png';
  185. }
  186. /**
  187. * Method to get the URL of an avatar
  188. *
  189. * @since 0.1
  190. */
  191. public function sendSystemEmail($emailSubject, $emailBody)
  192. {
  193. $hwdms = hwdMediaShareFactory::getInstance();
  194. $config = $hwdms->getConfig();
  195. $db =& JFactory::getDBO();
  196. $app =& JFactory::getApplication();
  197. // get all admin users
  198. $query = 'SELECT name, email, sendEmail, id' .
  199. ' FROM #__users' .
  200. ' WHERE sendEmail=1';
  201. $db->setQuery( $query );
  202. $rows = $db->loadObjectList();
  203. // Send mail to all users with users creating permissions and receiving system emails
  204. foreach( $rows as $row )
  205. {
  206. $usercreator = JFactory::getUser($id = $row->id);
  207. if ($usercreator->authorise('core.create', 'com_users'))
  208. {
  209. $return = JFactory::getMailer()->sendMail($app->getCfg('mailfrom'), $app->getCfg('fromname'), $row->email, $emailSubject, $emailBody);
  210. // Check for an error.
  211. if ($return !== true)
  212. {
  213. $this->setError(JText::_('COM_HWDMS_SEND_MAIL_FAILED'));
  214. return false;
  215. }
  216. }
  217. }
  218. return true;
  219. }
  220. /**
  221. * Method to get the size of a modal window
  222. *
  223. * @since 0.1
  224. */
  225. public function modalSize($size="small")
  226. {
  227. JLoader::register('hwdMediaShareHelperMobile', JPATH_ROOT.'/components/com_hwdmediashare/helpers/mobile.php');
  228. $mobile = hwdMediaShareHelperMobile::getInstance();
  229. if ($mobile->_isMobile)
  230. {
  231. $retval = "x: 220, y: 220";
  232. }
  233. else
  234. {
  235. if ($size == "small")
  236. {
  237. $retval = "x: 400, y: 350";
  238. }
  239. else
  240. {
  241. $retval = "x: 800, y: 500";
  242. }
  243. }
  244. return $retval;
  245. }
  246. /**
  247. * Method to get the width of a media item
  248. *
  249. * @since 1.0.2
  250. */
  251. public function getMediaWidth()
  252. {
  253. // Load hwdMediaShare config
  254. $hwdms = hwdMediaShareFactory::getInstance();
  255. $config = $hwdms->getConfig();
  256. JLoader::register('hwdMediaShareHelperMobile', JPATH_ROOT.'/components/com_hwdmediashare/helpers/mobile.php');
  257. $mobile = hwdMediaShareHelperMobile::getInstance();
  258. if ($mobile->_isIpad)
  259. {
  260. return "620";
  261. }
  262. elseif ($mobile->_isMobile)
  263. {
  264. return "300";
  265. }
  266. else
  267. {
  268. return JRequest::getInt('mediaitem_size', $config->get('mediaitem_size', '500'));
  269. }
  270. }
  271. }