PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_breezingforms/libraries/crosstec/functions/helpers.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip-alpes
PHP | 496 lines | 311 code | 74 blank | 111 comment | 100 complexity | 4b0f805f702773b44e6b6288b1610241 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT, LGPL-3.0, LGPL-2.0, JSON
  1. <?php
  2. /**
  3. * BreezingForms - A Joomla Forms Application
  4. * @version 1.8
  5. * @package BreezingForms
  6. * @copyright (C) 2008-2012 by Markus Bopp
  7. * @license Released under the terms of the GNU General Public License
  8. **/
  9. defined('_JEXEC') or die('Direct Access to this location is not allowed.');
  10. function bf_startsWith($haystack, $needle)
  11. {
  12. $length = strlen($needle);
  13. return (substr($haystack, 0, $length) === $needle);
  14. }
  15. function bf_endsWith($haystack, $needle)
  16. {
  17. $length = strlen($needle);
  18. if ($length == 0) {
  19. return true;
  20. }
  21. return (substr($haystack, -$length) === $needle);
  22. }
  23. function bf_is_mobile() {
  24. $is_mobile = false;
  25. // Check user agent string
  26. $agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
  27. if (empty($agent)) {
  28. return false;
  29. }
  30. $mobile_devices = array(
  31. 'is_iphone' => 'iphone',
  32. 'is_ipad' => 'ipad',
  33. 'is_ipod' => 'ipod',
  34. 'is_kindle' => 'kindle'
  35. );
  36. $mobile_oss = array(
  37. 'is_ios' => 'ip(hone|ad|od)',
  38. 'is_android' => 'android',
  39. 'is_webos' => '(web|hpw)os',
  40. 'is_palmos' => 'palm(\s?os|source)',
  41. 'is_windows' => 'windows (phone|ce)',
  42. 'is_symbian' => 'symbian(\s?os|)|symbos',
  43. 'is_bbos' => 'blackberry(.*?version\/\d+|\d+\/\d+)',
  44. 'is_bada' => 'bada'
  45. );
  46. $mobile_browsers = array(
  47. 'is_opera_mobile' => 'opera (mobi|mini)', // Opera Mobile or Mini
  48. 'is_webkit_mobile' => '(android|nokia|webos|hpwos|blackberry).*?webkit|webkit.*?(mobile|kindle|bolt|skyfire|dolfin|iris)', // Webkit mobile
  49. 'is_firefox_mobile' => 'fennec', // Firefox mobile
  50. 'is_ie_mobile' => 'iemobile|windows ce', // IE mobile
  51. 'is_netfront' => 'netfront|kindle|psp|blazer|jasmine', // Netfront
  52. 'is_uc_browser' => 'ucweb' // UC browser
  53. );
  54. $groups = array($mobile_devices, $mobile_oss, $mobile_browsers);
  55. foreach ($groups as $group) {
  56. foreach ($group as $name => $regex) {
  57. if (preg_match('/' . $regex . '/i', $agent)) {
  58. $is_mobile = true;
  59. break;
  60. }
  61. }
  62. }
  63. // Fallbacks
  64. if ($is_mobile === false) {
  65. $regex = 'nokia|motorola|sony|ericsson|lge?(-|;|\/|\s)|htc|samsung|asus|mobile|phone|tablet|pocket|wap|wireless|up\.browser|up\.link|j2me|midp|cldc|kddi|mmp|obigo|novarra|teleca|openwave|uzardweb|pre\/|hiptop|avantgo|plucker|xiino|elaine|vodafone|sprint|o2';
  66. $accept = isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : '';
  67. if (false !== strpos($accept, 'text/vnd.wap.wml')
  68. || false !== strpos($accept, 'application/vnd.wap.xhtml+xml')
  69. || isset($_SERVER['HTTP_X_WAP_PROFILE'])
  70. || isset($_SERVER['HTTP_PROFILE'])
  71. || preg_match('/' . $regex . '/i', $agent)
  72. ) {
  73. $is_mobile = true;
  74. }
  75. }
  76. return $is_mobile;
  77. }
  78. /**
  79. * Mail creator as expected by former FacileForms code
  80. * This is a not really Legacy, so it stays like that
  81. *
  82. * @param string $from
  83. * @param string $fromname
  84. * @param string $subject
  85. * @param string $body
  86. * @return JMail
  87. */
  88. function bf_getFieldSelectorList($form_id, $element_target_id){
  89. $db = JFactory::getDBO();
  90. $db->setQuery("Select `name` From #__facileforms_elements Where form = " . intval($form_id) . " And `name` Not In ('bfFakeName','bfFakeName2','bfFakeName3','bfFakeName4','bfFakeName5','bfFakeName6') Order by `ordering`");
  91. jimport('joomla.version');
  92. $version = new JVersion();
  93. if(version_compare($version->getShortVersion(), '3.0', '>=')){
  94. $rows = $db->loadColumn();
  95. }else{
  96. $rows = $db->loadResultArray();
  97. }
  98. $out = '<script type="text/javascript">
  99. function insertAtCursor_'.$element_target_id.'(myValue) {
  100. var myField = document.getElementById("'.$element_target_id.'");
  101. //IE support
  102. if (document.selection) {
  103. myField.focus();
  104. sel = document.selection.createRange();
  105. sel.text = myValue;
  106. }
  107. //MOZILLA/NETSCAPE support
  108. else if (myField.selectionStart || myField.selectionStart == \'0\') {
  109. var startPos = myField.selectionStart;
  110. var endPos = myField.selectionEnd;
  111. myField.value = myField.value.substring(0, startPos)
  112. + myValue
  113. + myField.value.substring(endPos, myField.value.length);
  114. } else {
  115. myField.value += myValue;
  116. }
  117. }
  118. </script>';
  119. if($rows){
  120. foreach($rows As $row){
  121. $out .= '<a href="javascript: insertAtCursor_'.$element_target_id.'(\'{'.$row.':label}\');void(0);">{'.$row.':label}</a><br/>';
  122. $out .= '<a href="javascript: insertAtCursor_'.$element_target_id.'(\'{'.$row.':value}\');void(0);">{'.$row.':value}</a><br/><br/>';
  123. }
  124. }
  125. return $out;
  126. }
  127. function bf_ToolTip( $tooltip, $title='', $width='', $image='tooltip.png', $text='', $href='', $link=1 )
  128. {
  129. // Initialize the toolips if required
  130. static $init;
  131. if ( ! $init )
  132. {
  133. JHTML::_('behavior.tooltip');
  134. $init = true;
  135. }
  136. return JHTML::_('tooltip', $tooltip, $title, $image, $text, $href, $link);
  137. }
  138. // used if copy is disabled
  139. function bf_copy($file1,$file2){
  140. $contentx =@file_get_contents($file1);
  141. $openedfile = @fopen($file2, "w");
  142. @fwrite($openedfile, $contentx);
  143. @fclose($openedfile);
  144. if ($contentx === FALSE) {
  145. $status=false;
  146. }else $status=true;
  147. return $status;
  148. }
  149. function bf_createMail( $from='', $fromname='', $subject, $body ) {
  150. $mail =& JFactory::getMailer();
  151. $mail->From = $from ? $from : $mail->From;
  152. $mail->Sender = $from ? $from : $mail->From;
  153. $mail->FromName = $fromname ? $fromname : $mail->FromName;
  154. $mail->Subject = $subject;
  155. $mail->Body = $body;
  156. //$mail->AddrAppend('Reply-To', $mail->From);
  157. //$mail->AddReplyTo($mail->From, $mail->FromName);
  158. return $mail;
  159. }
  160. function bf_sendNotificationBySession($session){
  161. $contents = JFactory::getSession()->get($session, array());
  162. if(count($contents) != 0){
  163. $from = $contents['from'];
  164. $fromname = $contents['fromname'];
  165. $recipient = $contents['recipients'];
  166. $subject = $contents['subject'];
  167. $body = $contents['body'];
  168. $attachment = $contents['attachment'];
  169. $html = $contents['isHtml'];
  170. if((is_array($recipient) && count($recipient) != 0) || ( !is_array($recipient) && $recipient != '' )){
  171. $mail = bf_createMail($from, $fromname, $subject, $body);
  172. if (is_array($recipient))
  173. foreach ($recipient as $to) $mail->AddAddress($to);
  174. else
  175. $mail->AddAddress($recipient);
  176. if ($attachment) {
  177. if ( is_array($attachment) )
  178. foreach ($attachment as $fname) $mail->AddAttachment($fname);
  179. else
  180. $mail->AddAttachment($attachment);
  181. } // if
  182. if (isset($html)) $mail->IsHTML($html);
  183. $mail->Send();
  184. }
  185. }
  186. JFactory::getSession()->set($session, array());
  187. }
  188. function bf_sendNotificationByPaymentCache($formId, $recordId, $type = 'admin'){
  189. $contents = array();
  190. $sourcePath = JPATH_SITE . '/media/breezingforms/payment_cache/';
  191. if (@file_exists($sourcePath) && @is_readable($sourcePath) && @is_dir($sourcePath) && $handle = @opendir($sourcePath)) {
  192. while (false !== ($file = @readdir($handle))) {
  193. if($file!="." && $file!="..") {
  194. $parts = explode('_', $file);
  195. if(count($parts)==4) {
  196. if($parts[0] == intval($formId) && $parts[1] == intval($recordId) && $parts[2] == $type) {
  197. $contents = unserialize(JFile::read($sourcePath.$file));
  198. JFile::delete($sourcePath.$file);
  199. break;
  200. }
  201. }
  202. }
  203. }
  204. @closedir($handle);
  205. }
  206. if(count($contents) != 0){
  207. $from = $contents['from'];
  208. $fromname = $contents['fromname'];
  209. $recipient = $contents['recipients'];
  210. $subject = $contents['subject'];
  211. $body = $contents['body'];
  212. $attachment = $contents['attachment'];
  213. $html = $contents['isHtml'];
  214. if((is_array($recipient) && count($recipient) != 0) || ( !is_array($recipient) && $recipient != '' )){
  215. $mail = bf_createMail($from, $fromname, $subject, $body);
  216. if (is_array($recipient))
  217. foreach ($recipient as $to) $mail->AddAddress($to);
  218. else
  219. $mail->AddAddress($recipient);
  220. if ($attachment) {
  221. if ( is_array($attachment) )
  222. foreach ($attachment as $fname) $mail->AddAttachment($fname);
  223. else
  224. $mail->AddAttachment($attachment);
  225. } // if
  226. if (isset($html)) $mail->IsHTML($html);
  227. $mail->Send();
  228. }
  229. }
  230. }
  231. /**
  232. * The name says it all
  233. *
  234. * @param string $string
  235. * @return boolean
  236. */
  237. function bf_isUTF8($string) {
  238. if (is_array($string))
  239. {
  240. $enc = implode('', $string);
  241. return @!((ord($enc[0]) != 239) && (ord($enc[1]) != 187) && (ord($enc[2]) != 191));
  242. }
  243. else
  244. {
  245. return (utf8_encode(utf8_decode($string)) == $string);
  246. }
  247. }
  248. /**
  249. * The classic recursive slash remover
  250. *
  251. * @param string $value raw
  252. * @return string cleaned
  253. */
  254. function bf_stripslashes_deep($value)
  255. {
  256. if(get_magic_quotes_gpc()) {
  257. $value = is_array($value) ?
  258. array_map('bf_stripslashes_deep', $value) :
  259. stripslashes($value);
  260. }
  261. return $value;
  262. }
  263. function bf_is_email ($email, $checkDNS = false) {
  264. // Check that $email is a valid address
  265. // (http://tools.ietf.org/html/rfc3696)
  266. // (http://tools.ietf.org/html/rfc2822)
  267. // (http://tools.ietf.org/html/rfc5322#section-3.4.1)
  268. // (http://tools.ietf.org/html/rfc5321#section-4.1.3)
  269. // (http://tools.ietf.org/html/rfc4291#section-2.2)
  270. // (http://tools.ietf.org/html/rfc1123#section-2.1)
  271. // the upper limit on address lengths should normally be considered to be 256
  272. // (http://www.rfc-editor.org/errata_search.php?rfc=3696)
  273. if (strlen($email) > 256) return false; // Too long
  274. // Contemporary email addresses consist of a "local part" separated from
  275. // a "domain part" (a fully-qualified domain name) by an at-sign ("@").
  276. // (http://tools.ietf.org/html/rfc3696#section-3)
  277. $index = strrpos($email,'@');
  278. if ($index === false) return false; // No at-sign
  279. if ($index === 0) return false; // No local part
  280. if ($index > 64) return false; // Local part too long
  281. $localPart = substr($email, 0, $index);
  282. $domain = substr($email, $index + 1);
  283. $domainLength = strlen($domain);
  284. if ($domainLength === 0) return false; // No domain part
  285. if ($domainLength > 255) return false; // Domain part too long
  286. // Let's check the local part for RFC compliance...
  287. //
  288. // local-part = dot-atom / quoted-string / obs-local-part
  289. // obs-local-part = word *("." word)
  290. // (http://tools.ietf.org/html/rfc2822#section-3.4.1)
  291. if (preg_match('/^"(?:.)*"$/', $localPart) > 0) {
  292. $dotArray[] = $localPart;
  293. } else {
  294. $dotArray = explode('.', $localPart);
  295. }
  296. foreach ($dotArray as $localElement) {
  297. // Period (".") may...appear, but may not be used to start or end the
  298. // local part, nor may two or more consecutive periods appear.
  299. // (http://tools.ietf.org/html/rfc3696#section-3)
  300. //
  301. // A zero-length element implies a period at the beginning or end of the
  302. // local part, or two periods together. Either way it's not allowed.
  303. if ($localElement === '') return false; // Dots in wrong place
  304. // Each dot-delimited component can be an atom or a quoted string
  305. // (because of the obs-local-part provision)
  306. if (preg_match('/^"(?:.)*"$/', $localElement) > 0) {
  307. // Quoted-string tests:
  308. //
  309. // Note that since quoted-pair
  310. // is allowed in a quoted-string, the quote and backslash characters may
  311. // appear in a quoted-string so long as they appear as a quoted-pair.
  312. // (http://tools.ietf.org/html/rfc2822#section-3.2.5)
  313. $groupCount = preg_match_all('/(?:^"|"$|\\\\\\\\|\\\\")|(\\\\|")/', $localElement, $matches);
  314. array_multisort($matches[1], SORT_DESC);
  315. if ($matches[1][0] !== '') return false; // Unescaped quote or backslash character inside quoted string
  316. if (preg_match('/^"\\\\*"$/', $localElement) > 0) return false; // "" and "\" are slipping through - note: must tidy this up
  317. } else {
  318. // Unquoted string tests:
  319. //
  320. // Any ASCII graphic (printing) character other than the
  321. // at-sign ("@"), backslash, double quote, comma, or square brackets may
  322. // appear without quoting. If any of that list of excluded characters
  323. // are to appear, they must be quoted
  324. // (http://tools.ietf.org/html/rfc3696#section-3)
  325. //
  326. $stripped = '';
  327. // Any excluded characters? i.e. <space>, @, [, ], \, ", <comma>
  328. if (preg_match('/[ @\\[\\]\\\\",]/', $localElement) > 0)
  329. // Check all excluded characters are escaped
  330. $stripped = preg_replace('/\\\\[ @\\[\\]\\\\",]/', '', $localElement);
  331. if (preg_match('/[ @\\[\\]\\\\",]/', $stripped) > 0) return false; // Unquoted excluded characters
  332. }
  333. }
  334. // Now let's check the domain part...
  335. // The domain name can also be replaced by an IP address in square brackets
  336. // (http://tools.ietf.org/html/rfc3696#section-3)
  337. // (http://tools.ietf.org/html/rfc5321#section-4.1.3)
  338. // (http://tools.ietf.org/html/rfc4291#section-2.2)
  339. if (preg_match('/^\\[(.)+]$/', $domain) === 1) {
  340. // It's an address-literal
  341. $addressLiteral = substr($domain, 1, $domainLength - 2);
  342. $matchesIP = array();
  343. // Extract IPv4 part from the end of the address-literal (if there is one)
  344. if (preg_match('/\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/', $addressLiteral, $matchesIP) > 0) {
  345. $index = strrpos($addressLiteral, $matchesIP[0]);
  346. if ($index === 0) {
  347. // Nothing there except a valid IPv4 address, so...
  348. return true;
  349. } else {
  350. // Assume it's an attempt at a mixed address (IPv6 + IPv4)
  351. if ($addressLiteral[$index - 1] !== ':') return false; // Character preceding IPv4 address must be ':'
  352. if (substr($addressLiteral, 0, 5) !== 'IPv6:') return false; // RFC5321 section 4.1.3
  353. $IPv6 = substr($addressLiteral, 5, ($index ===7) ? 2 : $index - 6);
  354. $groupMax = 6;
  355. }
  356. } else {
  357. // It must be an attempt at pure IPv6
  358. if (substr($addressLiteral, 0, 5) !== 'IPv6:') return false; // RFC5321 section 4.1.3
  359. $IPv6 = substr($addressLiteral, 5);
  360. $groupMax = 8;
  361. }
  362. $groupCount = preg_match_all('/^[0-9a-fA-F]{0,4}|\\:[0-9a-fA-F]{0,4}|(.)/', $IPv6, $matchesIP);
  363. $index = strpos($IPv6,'::');
  364. if ($index === false) {
  365. // We need exactly the right number of groups
  366. if ($groupCount !== $groupMax) return false; // RFC5321 section 4.1.3
  367. } else {
  368. if ($index !== strrpos($IPv6,'::')) return false; // More than one '::'
  369. $groupMax = ($index === 0 || $index === (strlen($IPv6) - 2)) ? $groupMax : $groupMax - 1;
  370. if ($groupCount > $groupMax) return false; // Too many IPv6 groups in address
  371. }
  372. // Check for unmatched characters
  373. array_multisort($matchesIP
  374. [1], SORT_DESC);
  375. if ($matchesIP[1][0] !== '') return false; // Illegal characters in address
  376. // It's a valid IPv6 address, so...
  377. return true;
  378. } else {
  379. // It's a domain name...
  380. // The syntax of a legal Internet host name was specified in RFC-952
  381. // One aspect of host name syntax is hereby changed: the
  382. // restriction on the first character is relaxed to allow either a
  383. // letter or a digit.
  384. // (http://tools.ietf.org/html/rfc1123#section-2.1)
  385. //
  386. // NB RFC 1123 updates RFC 1035, but this is not currently apparent from reading RFC 1035.
  387. //
  388. // Most common applications, including email and the Web, will generally not permit...escaped strings
  389. // (http://tools.ietf.org/html/rfc3696#section-2)
  390. //
  391. // Characters outside the set of alphabetic characters, digits, and hyphen MUST NOT appear in domain name
  392. // labels for SMTP clients or servers
  393. // (http://tools.ietf.org/html/rfc5321#section-4.1.2)
  394. //
  395. // RFC5321 precludes the use of a trailing dot in a domain name for SMTP purposes
  396. // (http://tools.ietf.org/html/rfc5321#section-4.1.2)
  397. $matches = array();
  398. $groupCount = preg_match_all('/(?:[0-9a-zA-Z][0-9a-zA-Z-]{0,61}[0-9a-zA-Z]|[a-zA-Z])(?:\\.|$)|(.)/', $domain, $matches);
  399. $level = count($matches[0]);
  400. if ($level == 1) return false; // Mail host can't be a TLD
  401. $TLD = $matches[0][$level - 1];
  402. if (substr($TLD, strlen($TLD) - 1, 1) === '.') return false; // TLD can't end in a dot
  403. if (preg_match('/^[0-9]+$/', $TLD) > 0) return false; // TLD can't be all-numeric
  404. // Check for unmatched characters
  405. array_multisort($matches[1], SORT_DESC);
  406. if ($matches[1][0] !== '') return false; // Illegal characters in domain, or label longer than 63 characters
  407. // Check DNS?
  408. if ($checkDNS && function_exists('checkdnsrr')) {
  409. if (!(checkdnsrr($domain, 'A') || checkdnsrr($domain, 'MX'))) {
  410. return false; // Domain doesn't actually exist
  411. }
  412. }
  413. // Eliminate all other factors, and the one which remains must be the truth.
  414. // (Sherlock Holmes, The Sign of Four)
  415. return true;
  416. }
  417. }
  418. function BFRedirect($link, $msg = null) {
  419. $mainframe = JFactory::getApplication();
  420. $mainframe->redirect($link, $msg);
  421. }