HomeHelpTrac

source: trunk/inc/utils.inc.php @ 16208

Revision 16208, 46.4 KB checked in by Alexander Trofimov, 4 weeks ago (diff)

Email templates design

Line 
1<?php defined('BX_DOL') or die('hack attempt');
2/**
3 * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
4 * CC-BY License - http://creativecommons.org/licenses/by/3.0/
5 *
6 * @defgroup    DolphinCore Dolphin Core
7 * @{
8 */
9
10define('BX_DOL_LOCALE_TIME', 2);
11define('BX_DOL_LOCALE_DATE_SHORT', 4);
12define('BX_DOL_LOCALE_DATE', 5);
13
14define('BX_DOL_LOCALE_PHP', 1);
15define('BX_DOL_LOCALE_DB', 2);
16
17define('BX_DATA_TEXT', 1); ///< regular text data type
18define('BX_DATA_TEXT_MULTILINE', 2); ///< regular multiline text data type
19define('BX_DATA_INT', 3); ///< integer data type
20define('BX_DATA_FLOAT', 4); ///< float data type
21define('BX_DATA_CHECKBOX', 5); ///< checkbox data type, 'on' or empty value
22define('BX_DATA_HTML', 6); ///< HTML data type
23define('BX_DATA_DATE', 7); ///< date data type stored as yyyy-mm-dd
24define('BX_DATA_DATE_TS', 8); ///< date data type stored as unixtimestamp
25define('BX_DATA_DATETIME_TS', 9); ///< date/time data type stored as unixtimestamp
26
27define('BX_ESCAPE_STR_AUTO', 0); ///< turn apostropes and quote signs into html special chars, for use in @see bx_js_string and @see bx_html_attribute
28define('BX_ESCAPE_STR_APOS', 1); ///< escape apostrophes only, for js strings enclosed in apostrophes, for use in @see bx_js_string and @see bx_html_attribute
29define('BX_ESCAPE_STR_QUOTE', 2); ///< escape quotes only, for js strings enclosed in quotes, for use in @see bx_js_string and @see bx_html_attribute
30
31define('BX_EMAIL_SYSTEM', 0); ///< system email without unsubscribe link, like forgot password or email verification
32define('BX_EMAIL_NOTIFY', 1); ///< notification message, with unsubscribe link
33define('BX_EMAIL_MASS', 2); ///< mass email, one mesage send to manu users, with unsubscribe link
34
35/**
36 * The following two functions are needed to convert title to uri and back.
37 * It usefull when titles are used in URLs, like in Categories and Tags.
38 */
39function title2uri($sValue) {
40    return str_replace(
41        array('&', '/', '\\', '"', '+'),
42        array('[and]', '[slash]', '[backslash]', '[quote]', '[plus]'),
43        $sValue
44    );
45}
46function uri2title($sValue) {
47    return str_replace(
48        array('[and]', '[slash]', '[backslash]', '[quote]', '[plus]'),
49        array('&', '/', '\\', '"', '+'),
50        $sValue
51    );
52}
53
54/**
55 * Convert date(timestamp) in accordance with requested format code.
56 *
57 * @param string $sTimestamp - timestamp
58 * @param integer $iCode - format code
59 *                  1(4) - short date format. @see sys_options -> short_date_format_php
60 *                  2 - time format. @see sys_options -> time_format_php
61 *                  3(5) - long date format. @see sys_options -> date_format_php
62 *                  6 - RFC 2822 date format.
63 */
64function getLocaleDate($sTimestamp = '', $iCode = BX_DOL_LOCALE_DATE_SHORT) {
65    $sFormat = (int)$iCode == 6 ? 'r' : getLocaleFormat($iCode);
66
67    return date($sFormat, $sTimestamp);
68}
69/**
70 * Get data format in accordance with requested format code and format type.
71 *
72 * @param integer $iCode - format code
73 *                  1(4) - short date format. @see sys_options -> short_date_format_php
74 *                  2 - time format. @see sys_options -> time_format_php
75 *                  3(5) - long date format. @see sys_options -> date_format_php
76 *                  6 - RFC 2822 date format.
77 * @param integer $iType - format type
78 *                  1 - for PHP code.
79 *                  2 - for database.
80 */
81function getLocaleFormat($iCode = BX_DOL_LOCALE_DATE_SHORT, $iType = BX_DOL_LOCALE_PHP) {
82    $sPostfix = (int)$iType == BX_DOL_LOCALE_PHP ? '_php' : '';
83
84    $sResult = '';
85    switch ($iCode) {
86        case 2:
87            $sResult = getParam('time_format' . $sPostfix);
88            break;
89        case 1:
90        case 4:
91            $sResult = getParam('short_date_format' . $sPostfix);
92            break;
93        case 3:
94        case 5:
95            $sResult = getParam('date_format' . $sPostfix);
96            break;
97    }
98
99    return $sResult;
100}
101
102/**
103 * Function will check on blocked status;
104 *
105 * @param  : $iFirstProfile (integer) - first profile's id;
106 * @param  : $iSecondProfile (integer) - second profile's id;
107 * @return : (boolean) - true if pair will blocked;
108 */
109function isBlocked($iFirstProfile, $iSecondProfile)
110{
111    $iFirstProfile = (int)$iFirstProfile;
112    $iSecondProfile = (int)$iSecondProfile;
113    $sQuery = "SELECT COUNT(*) FROM `sys_block_list` WHERE `ID` = {$iFirstProfile} AND `Profile` = {$iSecondProfile}";
114    return db_value( $sQuery) ? true : false;
115}
116
117/*
118 * function for work with profile
119 */
120function is_friends($id1, $id2) {
121    $id1 = (int)$id1;
122    $id2 = (int)$id2;
123    if ($id1 == 0 || $id2 == 0)
124       return;
125    $cnt = db_arr("SELECT SUM(`Check`) AS 'cnt' FROM `sys_friend_list` WHERE `ID`='{$id1}' AND `Profile`='{$id2}' OR `ID`='{$id2}' AND `Profile`='{$id1}'");
126    return ($cnt['cnt'] > 0 ? true : false);
127}
128
129/*
130 * functions for limiting maximal word length
131 */
132function strmaxwordlen($input, $len = 100) {
133    return $input;
134}
135
136/*
137 * functions for limiting maximal text length
138 */
139function strmaxtextlen($input, $len = 60) {
140    if ( strlen($input) > $len )
141        return mb_substr($input, 0, $len - 4) . "...";
142    else
143        return $input;
144}
145
146function html2txt($content, $tags = "") {
147    while($content != strip_tags($content, $tags)) {
148        $content = strip_tags($content, $tags);
149    }
150
151    return $content;
152}
153
154function html_encode($text) {
155     $searcharray =  array(
156    "'([-_\w\d.]+@[-_\w\d.]+)'",
157    "'((?:(?!://).{3}|^.{0,2}))(www\.[-\d\w\.\/]+)'",
158    "'(http[s]?:\/\/[-_~\w\d\.\/]+)'");
159
160    $replacearray = array(
161    "<a href=\"mailto:\\1\">\\1</a>",
162    "\\1http://\\2",
163    "<a href=\"\\1\" target=_blank>\\1</a>");
164
165   return preg_replace($searcharray, $replacearray, stripslashes($text));
166}
167
168/**
169 * Functions to process user input.
170 * DON'T use to process data before passing to SQL query - use db prepare instead @see BxDolDb::prepare.
171 * It is ok to use bx_process_input and then db prepare.
172 * @param $mixedData data to process
173 * @param $iDataType how to handle data, possible valies:
174 *          BX_DATA_INT - integer value
175 *          BX_DATA_FLOAT - float values
176 *          BX_DATA_CHECKBOX - 'on' or empty string
177 *          BX_DATA_TEXT - text data, single line (default)
178 *          BX_DATA_TEXT_MULTILINE - text data, multiple lines
179 *          BX_DATA_HTML - HTML data
180 *          BX_DATA_DATE - date data type stored as yyyy-mm-dd
181 *          BX_DATA_DATE_TS' -  date data type stored as unixtimestamp
182 *          BX_DATA_DATETIME_TS - date/time data type stored as unixtimestamp
183 * @param $mixedParams optional parameters to pass for validation
184 * @return the filtered data, or FALSE if the filter fails.
185 */
186function bx_process_input ($mixedData, $iDataType = BX_DATA_TEXT, $mixedParams = false, $isCheckMagicQuotes = true) {
187
188    if (is_array($mixedData)) {
189        foreach ($mixedData as $k => $v)
190            $mixedData[$k] = bx_process_input($v, $iDataType, $mixedParams);
191        return $mixedData;
192    }
193
194    if (get_magic_quotes_gpc() && $isCheckMagicQuotes)
195        $mixedData = stripslashes($mixedData);
196
197    switch ($iDataType) {
198    case BX_DATA_INT:
199        return filter_var(trim($mixedData), FILTER_VALIDATE_INT);
200    case BX_DATA_FLOAT:
201        return filter_var(trim($mixedData), FILTER_VALIDATE_FLOAT);
202    case BX_DATA_CHECKBOX:
203        return 'on' == trim($mixedData) ? 'on' : '';
204
205    case BX_DATA_DATE:
206        // maybe consider using strtotime
207        $mixedData = trim($mixedData);
208        if (!preg_match('/^\d{4}-\d{1,2}-\d{1,2}$/', $mixedData))
209            return false;
210        list($iYear, $iMonth, $iDay) = explode('-', $mixedData); // 1985-10-28
211        $iDay   = intval($iDay);
212        $iMonth = intval($iMonth);
213        $iYear  = intval($iYear);
214        return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
215    case BX_DATA_DATE_TS:
216        $mixedData = trim($mixedData);
217        if (!preg_match('/^\d{4}-\d{1,2}-\d{1,2}$/', $mixedData))
218            return false;
219        list($iYear, $iMonth, $iDay) = explode('-', $mixedData);
220        $iDay   = intval($iDay);
221        $iMonth = intval($iMonth);
222        $iYear  = intval($iYear);
223        $iRet = mktime (0, 0, 0, $iMonth, $iDay, $iYear);
224        return $iRet > 0 ? $iRet : false;
225    case BX_DATA_DATETIME_TS:
226        if (!preg_match('#(\d+)\-(\d+)\-(\d+)[\sT]{1}(\d+):(\d+):(\d+)#', $mixedData, $m))
227            return bx_process_input ($mixedData, BX_DATA_DATE_TS, $mixedParams, $isCheckMagicQuotes);
228        $iDay   = $m[3];
229        $iMonth = $m[2];
230        $iYear  = $m[1];
231        $iH = $m[4];
232        $iM = $m[5];
233        $iS = $m[6];
234        $iRet = mktime ($iH, $iM, $iS, $iMonth, $iDay, $iYear);
235        return $iRet > 0 ? $iRet : false;
236
237    case BX_DATA_HTML:
238        return clear_xss($mixedData);
239    case BX_DATA_TEXT_MULTILINE:
240        return nl2br(strip_tags($mixedData));
241    case BX_DATA_TEXT:
242    default:
243        return $mixedData;
244    }
245}
246
247/*
248 * Functions to process user output.
249 * Always use this function before output data which was entered by user before.
250 * @param $mixedData string to process
251 * @param $iDataType how to handle data, possible valies the same as in bx_process_input function, see bx_process_input.
252 * @param $mixedParams optional parameters to pass for validation
253 * @return the filtered data, or FALSE if the filter fails.
254 */
255function bx_process_output ($mixedData, $iDataType = BX_DATA_TEXT, $mixedParams = false) {
256
257    if (is_array($mixedData)) {
258        foreach ($mixedData as $k => $v)
259            $mixedData[$k] = bx_process_output($v, $iDataType, $mixedParams);
260        return $mixedData;
261    }
262
263    switch ($iDataType) {
264    case BX_DATA_INT:
265        return filter_var($mixedData, FILTER_VALIDATE_INT);
266    case BX_DATA_FLOAT:
267        return filter_var($mixedData, FILTER_VALIDATE_FLOAT);
268    case BX_DATA_CHECKBOX:
269        return 'on' == trim($mixedData) ? 'on' : '';
270
271    case BX_DATA_DATE:
272        return $mixedData;
273    case BX_DATA_DATE_TS:
274        return empty($mixedData) ? '' : date("Y-m-d", $mixedData);
275    case BX_DATA_DATETIME_TS:
276        return empty($mixedData) ? '' : date("Y-m-d H:i:s", $mixedData);
277
278    case BX_DATA_HTML:
279        return $mixedData;
280    case BX_DATA_TEXT_MULTILINE:
281        return $mixedData;
282    case BX_DATA_TEXT:
283    default:
284        return htmlspecialchars_adv($mixedData);
285    }
286}
287
288/*
289 * This function apply bx_process_input and then bx_process_output.
290 * Use this function to output data immediately after receiving, without saving to database.
291 * Patams are the same as bx_process_input function - @see bx_process_input
292 */
293function bx_process_pass ($mixedData, $iDataType = BX_DATA_TEXT, $mixedParams = false, $isCheckMagicQuotes = true) {
294    return bx_process_output(bx_process_input ($mixedData, $iDataType, $mixedParams, $isCheckMagicQuotes), $iDataType, $mixedParams);
295}
296
297/**
298 * DEPRECATED
299 * use bx_process_input + bx_process_output instead
300 * --------
301 * function for processing pass data
302 *
303 * This function cleans the GET/POST/COOKIE data if magic_quotes_gpc() is on
304 * for data which should be outputed immediately after submit
305 */
306/*
307function process_pass_data( $text, $strip_tags = 0 ) {
308    if ( $strip_tags )
309        $text = strip_tags($text);
310
311    if ( !get_magic_quotes_gpc() )
312        return $text;
313    else
314        return stripslashes($text);
315}
316*/
317
318/*
319 * function for output data from database into html
320 */
321function htmlspecialchars_adv( $string ) {
322    return htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
323}
324
325function process_text_output( $text, $maxwordlen = 100 ) {
326    return ( htmlspecialchars_adv( strmaxwordlen( $text, $maxwordlen ) ) );
327}
328
329function process_text_withlinks_output( $text, $maxwordlen = 100 ) {
330    return nl2br( html_encode( htmlspecialchars_adv( strmaxwordlen( $text, $maxwordlen ) ) ) );
331}
332
333function process_line_output( $text, $maxwordlen = 100 ) {
334    return htmlspecialchars_adv( strmaxwordlen( $text, $maxwordlen ) );
335}
336
337function process_html_output( $text, $maxwordlen = 100 ) {
338    return strmaxwordlen( $text, $maxwordlen );
339}
340
341/**
342*    Used to construct sturctured arrays in GET or POST data. Supports multidimensional arrays.
343*
344*    @param array    $Values    Specifies values and values names, that should be submitted. Can be multidimensional.
345*
346*    @return string    HTML code, which contains <input type="hidden"...> tags with names and values, specified in $Values array.
347*/
348function ConstructHiddenValues($Values) {
349    /**
350    *    Recursive function, processes multidimensional arrays
351    *
352    *    @param string $Name    Full name of array, including all subarrays' names
353    *
354    *    @param array $Value    Array of values, can be multidimensional
355    *
356    *    @return string    Properly consctructed <input type="hidden"...> tags
357    */
358    function ConstructHiddenSubValues($Name, $Value)     {
359        if (is_array($Value)) {
360            $Result = "";
361            foreach ($Value as $KeyName => $SubValue) {
362                $Result .= ConstructHiddenSubValues("{$Name}[{$KeyName}]", $SubValue);
363            }
364        } else
365            // Exit recurse
366            $Result = "<input type=\"hidden\" name=\"".htmlspecialchars($Name)."\" value=\"".htmlspecialchars($Value)."\" />\n";
367
368        return $Result;
369    }
370    /* End of ConstructHiddenSubValues function */
371
372    $Result = '';
373    if (is_array($Values)) {
374        foreach ($Values as $KeyName => $Value) {
375            $Result .= ConstructHiddenSubValues($KeyName, $Value);
376        }
377    }
378
379    return $Result;
380}
381
382/**
383*    Returns HTML/javascript code, which redirects to another URL with passing specified data (through specified method)
384*
385*    @param string    $ActionURL    destination URL
386*
387*    @param array    $Params    Parameters to be passed (through GET or POST)
388*
389*    @param string    $Method    Submit mode. Only two values are valid: 'get' and 'post'
390*
391*    @return mixed    Correspondent HTML/javascript code or false, if input data is wrong
392*/
393function RedirectCode($ActionURL, $Params = NULL, $Method = "get", $Title = 'Redirect') {
394    if ((strcasecmp(trim($Method), "get") && strcasecmp(trim($Method), "post")) || (trim($ActionURL) == ""))
395        return false;
396
397    ob_start();
398
399?>
400<html>
401    <head>
402        <title><?= $Title ?></title>
403    </head>
404    <body>
405        <form name="RedirectForm" action="<?= htmlspecialchars($ActionURL) ?>" method="<?= $Method ?>">
406
407<?= ConstructHiddenValues($Params) ?>
408
409        </form>
410        <script type="text/javascript">
411            <!--
412            document.forms['RedirectForm'].submit();
413            -->
414        </script>
415    </body>
416</html>
417<?
418
419    $Result = ob_get_contents();
420    ob_end_clean();
421
422    return $Result;
423}
424
425/**
426*    Redirects browser to another URL, passing parameters through POST or GET
427*    Actually just prints code, returned by RedirectCode (see RedirectCode)
428*/
429function Redirect($ActionURL, $Params = NULL, $Method = "get", $Title = 'Redirect') {
430    $RedirectCodeValue = RedirectCode($ActionURL, $Params, $Method, $Title);
431    if ($RedirectCodeValue !== false)
432        echo $RedirectCodeValue;
433}
434
435function isRWAccessible($sFileName) {
436    clearstatcache();
437    $perms = fileperms($sFileName);
438    return ( $perms & 0x0004 && $perms & 0x0002 ) ? true : false;
439}
440
441/**
442 * Send email function
443 *
444 * @param $sRecipientEmail - Email where email should be send
445 * @param $sMailSubject - subject of the message
446 * @param $sMailBody - Body of the message
447 * @param $iRecipientID - ID of recipient profile
448 * @param $aPlus - Array of additional information
449 * @param $iEmailType - email message type: BX_EMAIL_SYSTEM, BX_EMAIL_NOTIFY or BX_EMAIL_MASS
450 * @return true if message was send or false otherwise
451 */
452function sendMail($sRecipientEmail, $sMailSubject, $sMailBody, $iRecipientID = 0, $aPlus = array(), $iEmailType = BX_EMAIL_NOTIFY, $sEmailFlag = 'html', $isDisableAlert = false) {
453
454    // make sure that recipient's email is valid and message isn't empty
455    if (!$sMailBody || !$sRecipientEmail || preg_match('/\(2\)$/', $sRecipientEmail))
456        return false;
457
458    // get recipient account
459    bx_import('BxDolAccount');
460    $oAccount = BxDolAccount::getInstance($sRecipientEmail);
461    $aAccountInfo = $oAccount ? $oAccount->getInfo() : false;
462
463    // don't send bulk emails if user didn't subscribed to site news or email is unconfirmed
464    if ($aAccountInfo && BX_EMAIL_MASS == $iEmailType && (!$aAccountInfo['email_confirmed'] || !$aAccountInfo['receive_news']))
465        return false;
466
467    // don't send email notifications if user didn't subscribed to notifications or email is unconfirmed
468    if ($aAccountInfo && BX_EMAIL_NOTIFY == $iEmailType && (!$aAccountInfo['email_confirmed'] || !$aAccountInfo['receive_updates']))
469        return false;
470
471    // if profile id is provided - get profile's info
472    $aRecipientInfo = false;
473    if ($iRecipientID) {
474        bx_import('BxDolProfile');
475        $oProfile = BxDolProfile::getInstance($iRecipientID);
476        if ($oProfile)
477            $aRecipientInfo = $oProfile->getInfo();
478    }
479
480    // get site vars
481    $sEmailNotify       = getParam('site_email_notify');
482    $sSiteTitle         = getParam('site_title');
483   
484    // add unsubscribe link
485    if (empty($aPlus['unsubscribe'])) {
486        $aPlus['unsubscribe'] = '';
487        if ($oAccount && (BX_EMAIL_MASS == $iEmailType || BX_EMAIL_NOTIFY == $iEmailType))
488            $aPlus['unsubscribe'] = ($sLink = $oAccount->getUnsubscribeLink($iEmailType)) ? '<a href="' . BX_DOL_URL_ROOT . $sLink . '">' . _t('_sys_et_txt_unsubscribe') . '</a>' : '';
489    }
490
491    // parse template
492    if ($aPlus || $iRecipientID) {
493        if(!is_array($aPlus))
494            $aPlus = array();
495        bx_import('BxDolEmailTemplates');
496        $oEmailTemplates = BxDolEmailTemplates::getInstance();
497        $sMailSubject = $oEmailTemplates->parseContent($sMailSubject, $aPlus, $iRecipientID);
498        $sMailBody = $oEmailTemplates->parseContent($sMailBody, $aPlus, $iRecipientID);
499    }
500
501    // email message headers
502    $sMailHeader = "From: =?UTF-8?B?" . base64_encode( $sSiteTitle ) . "?= <{$sEmailNotify}>";
503    $sMailParameters = "-f{$sEmailNotify}";
504    $sMailSubject = '=?UTF-8?B?' . base64_encode( $sMailSubject ) . '?=';
505    $sMailHeader = "MIME-Version: 1.0\r\n" . $sMailHeader;
506
507    // if SMPT mailer is installed and enabled - send mail throught it
508    if (!$isDisableAlert && 'on' == getParam('bx_smtp_on')) { // TODO: remake to use alert: before_send_mail
509        return BxDolService::call('bx_smtp', 'send', array($sRecipientEmail, $sMailSubject, $sMailBody, $sMailHeader, $sMailParameters, 'html' == $sEmailFlag, $aRecipientInfo));
510    }
511
512    // send mail
513    if( 'html' == $sEmailFlag) {
514        $sMailHeader = "Content-type: text/html; charset=UTF-8\r\n" . $sMailHeader;
515        $iSendingResult = mail( $sRecipientEmail, $sMailSubject, $sMailBody, $sMailHeader, $sMailParameters );
516    } else {
517        $sMailHeader = "Content-type: text/plain; charset=UTF-8\r\n" . $sMailHeader;
518        $sMailBody = html2txt($sMailBody);
519        $iSendingResult = mail( $sRecipientEmail, $sMailSubject, html2txt($sMailBody), $sMailHeader, $sMailParameters );
520    }
521
522    // system alert
523    if (!$isDisableAlert) {
524        bx_alert('profile', 'send_mail', (isset($aRecipientInfo['ID']) ? $aRecipientInfo['ID'] : 0), '', array(
525            'email'     => $sRecipientEmail,
526            'subject'   => $sMailSubject,
527            'body'      => $sMailBody,
528            'header'    => $sMailHeader,
529            'params'    => $sMailParameters,
530            'html'      => 'html' == $sEmailFlag ? true : false,
531        ));
532    }
533
534    return $iSendingResult;
535}
536
537/*
538 * Getting an array with Templates' Names
539 */
540function get_templates_array($bEnabledOnly = true) {
541    bx_import('BxDolDb');
542    return BxDolDb::getInstance()->getPairs("SELECT `uri`, `title` FROM `sys_modules` WHERE 1 AND `type`='" . BX_DOL_MODULE_TYPE_TEMPLATE . "'" . ($bEnabledOnly ? " AND `enabled`='1'" : ""), "uri", "title");
543}
544
545/*
546 * The Function Show a Line with Templates Names
547 */
548function templates_select_txt() {
549    $templ_choices = get_templates_array();
550    $current_template = ( strlen( $_GET['skin'] ) ) ? $_GET['skin'] : $_COOKIE['skin'];
551
552    foreach ($templ_choices as $tmpl_key => $tmpl_value) {
553        if ($current_template == $tmpl_key) {
554            $ReturnResult .= $tmpl_value . ' | ';
555        } else {
556            $sGetTransfer = bx_encode_url_params($_GET, array('skin'));
557            $ReturnResult .= '<a href="' . bx_html_attribute($_SERVER['PHP_SELF']) . '?' . $sGetTransfer . 'skin=' . $tmpl_key . '">' . $tmpl_value . '</a> | ';
558        }
559    }
560    return $ReturnResult;
561}
562
563function extFileExists( $sFileSrc ) {
564    return (file_exists( $sFileSrc ) && is_file( $sFileSrc )) ? true : false;
565}
566
567function getVisitorIP() {
568    $ip = "0.0.0.0";
569    if( ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) && ( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) ) {
570        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
571    } elseif( ( isset( $_SERVER['HTTP_CLIENT_IP'])) && (!empty($_SERVER['HTTP_CLIENT_IP'] ) ) ) {
572        $ip = explode(".",$_SERVER['HTTP_CLIENT_IP']);
573        $ip = $ip[3].".".$ip[2].".".$ip[1].".".$ip[0];
574    } elseif((!isset( $_SERVER['HTTP_X_FORWARDED_FOR'])) || (empty($_SERVER['HTTP_X_FORWARDED_FOR']))) {
575        if ((!isset( $_SERVER['HTTP_CLIENT_IP'])) && (empty($_SERVER['HTTP_CLIENT_IP']))) {
576            $ip = $_SERVER['REMOTE_ADDR'];
577        }
578    }
579    return $ip;
580}
581
582function genFlag( $country ) {
583    return '<img src="' . genFlagUrl($country) . '" />';
584}
585
586function genFlagUrl($country) {
587    bx_import('BxDolTemplate');
588    return BxDolTemplate::getInstance()->getIconUrl('sys_fl_' . strtolower($country) . '.gif');
589}
590
591// print debug information ( e.g. arrays )
592function echoDbg( $what, $desc = '' ) {
593    if ( $desc )
594        echo "<b>$desc:</b> ";
595    echo "<pre>";
596        print_r( $what );
597    echo "</pre>\n";
598}
599
600function echoDbgLog($mWhat, $sDesc = '', $sFileName = 'debug.log') {
601    $sCont =
602        '--- ' . date('r') . ' (' . BX_DOL_START_TIME . ") ---\n" .
603        $sDesc . "\n" .
604        print_r($mWhat, true) . "\n\n\n";
605
606    $rFile = fopen(BxDolConfig::getInstance()->get('path_dynamic', 'tmp') . $sFileName, 'a');
607    fwrite($rFile, $sCont);
608    fclose($rFile);
609}
610
611function clear_xss($val) {
612
613    if ($GLOBALS['logged']['admin'])
614        return $val;
615
616    // HTML Purifier plugin
617    global $oHtmlPurifier;
618    require_once( BX_DIRECTORY_PATH_PLUGINS . 'htmlpurifier/HTMLPurifier.standalone.php' );
619    if (!isset($oHtmlPurifier)) {
620
621        HTMLPurifier_Bootstrap::registerAutoload();
622
623        $oConfig = HTMLPurifier_Config::createDefault();
624
625        $oConfig->set('HTML.SafeObject', 'true');
626        $oConfig->set('Output.FlashCompat', 'true');
627        $oConfig->set('HTML.FlashAllowFullScreen', 'true');
628
629        $oConfig->set('Filter.Custom', array (new HTMLPurifier_Filter_LocalMovie()));
630
631        $oConfig->set('HTML.DefinitionID', '1');
632        $oDef = $oConfig->getHTMLDefinition(true);
633        $oDef->addAttribute('a', 'target', 'Enum#_blank,_self,_target,_top');
634
635        $oHtmlPurifier = new HTMLPurifier($oConfig);
636    }
637
638    return $oHtmlPurifier->purify($val);
639}
640
641function _format_when ($iSec) {
642    $s = '';
643
644    if ($iSec>0) {
645        if ($iSec < 3600) {
646            $i = round($iSec/60);
647            $s .= (0 == $i || 1 == $i) ? _t('_x_minute_ago', '1') : _t('_x_minute_ago', $i, 's');
648        } else if ($iSec < 86400) {
649            $i = round($iSec/60/60);
650            $s .= (0 == $i || 1 == $i) ? _t('_x_hour_ago', '1') : _t('_x_hour_ago', $i, 's');
651        } else {
652            $i = round($iSec/60/60/24);
653            $s .= (0 == $i || 1 == $i) ? _t('_x_day_ago', '1') : _t('_x_day_ago', $i, 's');
654        }
655    } else {
656        if ($iSec > -3600) {
657            $i = round($iSec/60);
658            $s .= (0 == $i || 1 == $i) ? _t('_in_x_minute', '1') : _t('_in_x_minute', -$i, 's');
659        } else if ($iSec > -86400) {
660            $i = round($iSec/60/60);
661            $s .= (0 == $i || 1 == $i) ? _t('_in_x_hour', '1') : _t('_in_x_hour', -$i, 's');
662        } elseif ($iSec < -86400) {
663            $i = round($iSec/60/60/24);
664            $s .= (0 == $i || 1 == $i) ? _t('_in_x_day', '1') : _t('_in_x_day', -$i, 's');
665        }
666    }
667    return $s;
668}
669
670function defineTimeInterval($iTime) {
671    $iTime = time() - (int)$iTime;
672    $sCode = _format_when($iTime);
673    return $sCode;
674}
675
676function execSqlFile($sFileName) {
677    if (! $f = fopen($sFileName, "r"))
678        return false;
679
680    db_res( "SET NAMES 'utf8'" );
681
682    $s_sql = "";
683    while ( $s = fgets ( $f, 10240) ) {
684        $s = trim( $s ); //Utf with BOM only
685
686        if( !strlen( $s ) ) continue;
687        if ( mb_substr( $s, 0, 1 ) == '#'  ) continue; //pass comments
688        if ( mb_substr( $s, 0, 2 ) == '--' ) continue;
689
690        $s_sql .= $s;
691
692        if ( mb_substr( $s, -1 ) != ';' ) continue;
693
694        db_res( $s_sql );
695        $s_sql = "";
696    }
697
698    fclose($f);
699    return true;
700}
701
702function replace_full_uris( $text ) {
703    $text = preg_replace_callback( '/([\s\n\r]src\=")([^"]+)(")/', 'replace_full_uri', $text );
704    return $text;
705}
706
707function replace_full_uri( $matches ) {
708    if( substr( $matches[2], 0, 7 ) != 'http://' and substr( $matches[2], 0, 6 ) != 'ftp://' )
709        $matches[2] = BX_DOL_URL_ROOT . $matches[2];
710
711    return $matches[1] . $matches[2] . $matches[3];
712}
713
714//--------------------------------------- friendly permalinks --------------------------------------//
715//------------------------------------------- main functions ---------------------------------------//
716function uriGenerate ($s, $sTable, $sField, $sEmpty = '-') {
717    $s = uriFilter($s, $sEmpty);
718    if(uriCheckUniq($s, $sTable, $sField))
719        return $s;
720
721    // cut off redundant part
722    if(get_mb_len($s) > 240)
723        $s = get_mb_substr($s, 0, 240);
724
725    // try to add date
726    $s .= '-' . date('Y-m-d');
727    if(uriCheckUniq($s, $sTable, $sField))
728        return $s;
729
730    // try to add number
731    for($i = 0 ; $i < 999 ; ++$i)
732        if(uriCheckUniq($s . '-' . $i, $sTable, $sField))
733            return ($s . '-' . $i);
734
735    return rand(0, 999999999);
736}
737
738function uriFilter ($s, $sEmpty = '-') {
739    bx_import('BxTemplConfig');
740    if (BxTemplConfig::getInstance()->bAllowUnicodeInPreg)
741        $s = get_mb_replace ('/[^\pL^\pN]+/u', '-', $s); // unicode characters
742    else
743        $s = get_mb_replace ('/([^\d^\w]+)/u', '-', $s); // latin characters only
744
745    $s = get_mb_replace ('/([-^]+)/', '-', $s);
746    return !$s ? $sEmpty : trim($s, " -");
747}
748
749function uriCheckUniq ($s, $sTable, $sField) {
750    bx_import('BxDolDb');
751    $oDb = BxDolDb::getInstance();
752
753    $sSql = $oDb->prepare("SELECT 1 FROM `$sTable` WHERE `$sField`=? LIMIT 1", $s);
754    return !$oDb->query($sSql);
755}
756
757function get_mb_replace ($sPattern, $sReplace, $s) {
758    return preg_replace ($sPattern, $sReplace, $s);
759}
760
761function get_mb_len ($s) {
762    return mb_strlen($s);
763}
764
765function get_mb_substr ($s, $iStart, $iLen) {
766    return mb_substr ($s, $iStart, $iLen);
767}
768
769function bx_mb_substr_replace($s, $sReplace, $iPosStart, $iLength) {
770    return mb_substr($s, 0, $iPosStart) . $sReplace . mb_substr($s, $iPosStart + $iLength);
771}
772
773function bx_mb_strpos ($s, $sReplacement, $iStart = 0) {
774    return mb_strpos($s, $sReplacement, $iStart);
775}
776
777/**
778 * Block user IP
779 *
780 * @param $sIP mixed
781 * @param $iExpirationInSec integer
782 * @param $sComment string
783 * @return void
784 */
785function bx_block_ip($mixedIP, $iExpirationInSec = 86400, $sComment = '') {
786
787    if (preg_match('/^[0-9]+$/', $mixedIP))
788        $iIP = $mixedIP;
789    else
790        $iIP = sprintf("%u", ip2long($sIP));
791
792    $iExpirationInSec = time() + (int)$iExpirationInSec;
793
794    $oDb = BxDolDb::getInstance();
795    $sQuery = $oDb->prepare("SELECT ID FROM `sys_ip_list` WHERE `From` = ? AND `To` = ? LIMIT 1", $iIP, $iIP);
796    if (!$oDb->getOne($sQuery)) {
797        $sQuery = $oDb->prepare("INSERT INTO `sys_ip_list` SET `From` = ?, `To` = ?, `Type` = 'deny', `LastDT` = ?, `Desc` = ?", $iIP, $iIP, $iExpirationInSec, $sComment);
798        return $oDb->res($sQuery);
799    }
800    return false;
801}
802
803function bx_is_ip_dns_blacklisted($sCurIP = '', $sType = '') {
804
805    if (defined('BX_DOL_CRON_EXECUTE'))
806        return false;
807
808    if (!$sCurIP)
809        $sCurIP = getVisitorIP();
810
811    if (bx_is_ip_whitelisted($sCurIP))
812        return false;
813
814    $o = bx_instance('BxDolDNSBlacklists');
815    if (BX_DOL_DNSBL_POSITIVE == $o->dnsbl_lookup_ip(BX_DOL_DNSBL_CHAIN_SPAMMERS, $sCurIP) && BX_DOL_DNSBL_POSITIVE != $o->dnsbl_lookup_ip(BX_DOL_DNSBL_CHAIN_WHITELIST, $sCurIP))
816    {
817        $o->onPositiveDetection ($sCurIP, $sType);
818        return true;
819    }
820
821    return false;
822}
823
824function bx_is_ip_whitelisted($sCurIP = '') {
825
826    if (defined('BX_DOL_CRON_EXECUTE'))
827        return true;
828
829    $iIPGlobalType = (int)getParam('ipListGlobalType');
830    if ($iIPGlobalType != 1 && $iIPGlobalType != 2) // 0 - disabled
831        return false;
832
833    if (!$sCurIP)
834        $sCurIP = getVisitorIP();
835    $iCurIP = sprintf("%u", ip2long($sCurIP));
836    $iCurrTume = time();
837
838    return db_value("SELECT `ID` FROM `sys_ip_list` WHERE `Type` = 'allow' AND `LastDT` > $iCurrTume AND `From` <= '$iCurIP' AND `To` >= '$iCurIP' LIMIT 1") ? true : false;
839}
840
841function bx_is_ip_blocked($sCurIP = '') {
842
843    if (defined('BX_DOL_CRON_EXECUTE'))
844        return false;
845
846    $iIPGlobalType = (int)getParam('ipListGlobalType');
847    if ($iIPGlobalType != 1 && $iIPGlobalType != 2) // 0 - disabled
848        return false;
849
850    if (!$sCurIP)
851        $sCurIP = getVisitorIP();
852    $iCurIP = sprintf("%u", ip2long($sCurIP));
853    $iCurrTume = time();
854
855    if (bx_is_ip_whitelisted($sCurIP))
856        return false;
857
858    $isBlocked = db_value("SELECT `ID` FROM `sys_ip_list` WHERE `Type` = 'deny' AND `LastDT` > $iCurrTume AND `From` <= '$iCurIP' AND `To` >= '$iCurIP' LIMIT 1");
859    if ($isBlocked)
860        return true;
861
862    // 1 - all allowed except listed
863    // 2 - all blocked except listed
864    return $iIPGlobalType == 2 ? true : false;
865}
866
867/**
868 *  spam checking function
869 *  @param $s content to check for spam
870 *  @param $isStripSlashes slashes parameter:
871 *          BX_SLASHES_AUTO - automatically detect magic_quotes_gpc setting
872 *          BX_SLASHES_NO_ACTION - do not perform any action with slashes
873 *  @return true if spam detected
874 */
875function bx_is_spam ($val, $isStripSlashes = BX_SLASHES_AUTO) {
876
877    if (defined('BX_DOL_CRON_EXECUTE'))
878        return false;
879
880    if (isAdmin())
881        return false;
882
883    if (bx_is_ip_whitelisted($sCurIP))
884        return false;
885
886    if (get_magic_quotes_gpc() && $isStripSlashes == BX_SLASHES_AUTO)
887        $val = stripslashes($val);
888
889    $bRet = false;
890    if ('on' == getParam('sys_uridnsbl_enable')) {
891        $oBxDolDNSURIBlacklists = bx_instance('BxDolDNSURIBlacklists');
892        if ($oBxDolDNSURIBlacklists->isSpam($val)) {
893            $oBxDolDNSURIBlacklists->onPositiveDetection($val);
894            $bRet = true;
895        }
896    }
897
898    if ('on' == getParam('sys_akismet_enable')) {
899        $oBxDolAkismet = bx_instance('BxDolAkismet');
900        if ($oBxDolAkismet->isSpam($val)) {
901            $oBxDolAkismet->onPositiveDetection($val);
902            $bRet = true;
903        }
904    }
905
906    if ($bRet && 'on' == getParam('sys_antispam_report')) {
907
908        $iProfileId = getLoggedId();
909        $aPlus = array(
910            'SpammerUrl' => getProfileLink($iProfileId),
911            'SpammerNickName' => getNickName($iProfileId),
912            'Page' => htmlspecialchars_adv($_SERVER['PHP_SELF']),
913            'Get' => print_r($_GET, true),
914            'SpamContent' => htmlspecialchars_adv($val),
915        );
916
917        bx_import('BxDolEmailTemplates');
918        $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_SpamReportAuto', $aPlus);
919        if (!$aTemplate)
920            trigger_error('Email template or translation missing: t_SpamReportAuto', E_USER_ERROR);
921
922        sendMail(getParam('site_email'), $aTemplate['Subject'], $aTemplate['Body']);
923    }
924
925    if ($bRet && 'on' == getParam('sys_antispam_block'))
926        return true;
927
928    return false;
929}
930
931function getmicrotime() {
932    list($usec, $sec) = explode(" ", microtime());
933    return ((float)$usec + (float)$sec);
934}
935
936/**
937** @description : function will create cache file with all SQL queries ;
938** @return        :
939*/
940function genSiteStatCache() {
941
942    $sqlQuery = "SELECT `Name` as `name`,
943                        `Title` as `capt`,
944                        `UserQuery` as `query`,
945                        `UserLink` as `link`,
946                        `IconName` as `icon`,
947                        `AdminQuery` as `adm_query`,
948                           `AdminLink` as `adm_link`
949                        FROM `sys_stat_site`
950                        ORDER BY `StatOrder` ASC, `ID` ASC";
951
952    $rData = db_res($sqlQuery);
953
954    $sLine = "return array( \n";
955    while ($aVal = mysql_fetch_assoc($rData)) {
956        $sLine .= genSiteStatFile($aVal);
957    }
958    $sLine = rtrim($sLine, ",\n")."\n);";
959
960    $aResult = eval($sLine);
961
962    $oCache = BxDolDb::getInstance()->getDbCacheObject();
963    return $oCache->setData (BxDolDb::getInstance()->genDbCacheKey('sys_stat_site'), $aResult);
964}
965
966/**
967 * Function will cute the parameter from received string;
968 * remove received parameter from 'GET' query ;
969 *
970 * @param        : $aExceptNames (string) - name of unnecessary parameter;
971 * @return       : cleared string;
972 */
973function getClearedParam( $sExceptParam, $sString ) {
974    return preg_replace( "/(&amp;|&){$sExceptParam}=([a-z0-9\_\-]{1,})/i",'', $sString);
975}
976
977/**
978 * import class file, it detect class path by its prefix or module array
979 *
980 * @param $sClassName - full class name or class postfix in a case of module class
981 * @param $aModule - module array or true to get module array from global variable
982 */
983function bx_import($sClassName, $aModule = array()) {
984    if (class_exists($sClassName))
985        return;
986
987    if ($aModule) {
988        $a = (true === $aModule) ? $GLOABLS['aModule'] : $aModule;
989        if (class_exists($a['class_prefix'] . $sClassName))
990            return;
991        require_once (BX_DIRECTORY_PATH_MODULES . $a['path'] . 'classes/' . $a['class_prefix'] . $sClassName . '.php');
992    }
993
994    if (0 == strncmp($sClassName, 'BxDol', 5)) {
995        if (0 == strncmp($sClassName, 'BxDolStudio', 11))
996            require_once(BX_DOL_DIR_STUDIO_CLASSES . $sClassName . '.php');
997        else
998            require_once(BX_DIRECTORY_PATH_CLASSES . $sClassName . '.php');
999        return;
1000    }
1001
1002    if (0 == strncmp($sClassName, 'BxBase', 6)) {
1003        if(0 == strncmp($sClassName, 'BxBaseStudio', 12))
1004            require_once(BX_DOL_DIR_STUDIO_BASE . 'scripts/' . $sClassName . '.php');
1005        else
1006            require_once(BX_DIRECTORY_PATH_BASE . 'scripts/' . $sClassName . '.php');
1007        return;
1008    }
1009
1010    if (0 == strncmp($sClassName, 'BxTempl', 7) && !class_exists($sClassName)) {
1011        if(0 == strncmp($sClassName, 'BxTemplStudio', 13)) {
1012            bx_import('BxDolStudioTemplate');
1013            $sPath = BX_DIRECTORY_PATH_MODULES . BxDolStudioTemplate::getInstance()->getPath() . 'data/template/studio/scripts/' . $sClassName . '.php';
1014        }
1015        else {
1016            bx_import('BxDolTemplate');
1017            $sPath = BX_DIRECTORY_PATH_MODULES . BxDolTemplate::getInstance()->getPath() . 'data/template/system/scripts/' . $sClassName . '.php';
1018        }
1019
1020        if(file_exists($sPath)) {
1021            require_once($sPath);
1022            return;
1023        }
1024
1025        echo "<b>Fatal error:</b> Class (" . $sClassName . ") not found.";
1026        exit;
1027    }
1028}
1029
1030/**
1031 * Gets an instance of class pathing necessary parameters if it's necessary.
1032 *
1033 * @param string $sClassName class name.
1034 * @param array $aParams an array of parameters to be pathed to the constructor of the class.
1035 * @param array $aModule an array with module description. Is used when the requested class is located in some module.
1036 * @return unknown
1037 */
1038function bx_instance($sClassName, $aParams = array(), $aModule = array()) {
1039    if(isset($GLOBALS['bxDolClasses'][$sClassName]))
1040        return $GLOBALS['bxDolClasses'][$sClassName];
1041    else {
1042        bx_import((empty($aModule) ? $sClassName : str_replace($aModule['class_prefix'], '', $sClassName)), $aModule);
1043
1044        if(empty($aParams))
1045            $GLOBALS['bxDolClasses'][$sClassName] = new $sClassName();
1046        else {
1047            $sParams = "";
1048            foreach($aParams as $mixedKey => $mixedValue)
1049                $sParams .= "\$aParams[" . $mixedKey . "], ";
1050            $sParams = substr($sParams, 0, -2);
1051
1052            $GLOBALS['bxDolClasses'][$sClassName] = eval("return new " . $sClassName . "(" . $sParams . ");"); // TODO: remake without eval
1053        }
1054
1055        return $GLOBALS['bxDolClasses'][$sClassName];
1056    }
1057}
1058
1059
1060/**
1061 * Escapes string/array ready to pass to js script with filtered symbols like ', " etc
1062 *
1063 * @param $mixedInput - string/array which should be filtered
1064 * @param $iQuoteType - string escaping method: BX_ESCAPE_STR_AUTO(default), BX_ESCAPE_STR_APOS or BX_ESCAPE_STR_QUOTE
1065 * @return converted string / array
1066 */
1067function bx_js_string ($mixedInput, $iQuoteType = BX_ESCAPE_STR_AUTO) {
1068    $aUnits = array(
1069        "\n" => "\\n",
1070        "\r" => "",
1071    );
1072    if (BX_ESCAPE_STR_APOS == $iQuoteType) {
1073        $aUnits["'"] = "\\'";
1074        $aUnits['<script'] = "<scr' + 'ipt";
1075        $aUnits['</script>'] = "</scr' + 'ipt>";
1076    } elseif (BX_ESCAPE_STR_QUOTE == $iQuoteType) {
1077        $aUnits['"'] = '\\"';
1078        $aUnits['<script'] = '<scr" + "ipt';
1079        $aUnits['</script>'] = '</scr" + "ipt>';
1080    } else {
1081        $aUnits['"'] = '&quote;';
1082        $aUnits["'"] = '&apos;';
1083        $aUnits["<"] = '&lt;';
1084        $aUnits[">"] = '&gt;';
1085    }
1086    return str_replace(array_keys($aUnits), array_values($aUnits), $mixedInput);
1087}
1088
1089/**
1090 * Return input string/array ready to pass to html attribute with filtered symbols like ', " etc
1091 *
1092 * @param mixed $mixedInput - string/array which should be filtered
1093 * @return converted string / array
1094 */
1095function bx_html_attribute ($mixedInput, $iQuoteType = BX_ESCAPE_STR_AUTO) {
1096
1097    $aUnits = array ();
1098    if (BX_ESCAPE_STR_APOS == $iQuoteType)
1099        $aUnits["'"] = "\\'";
1100    elseif (BX_ESCAPE_STR_QUOTE == $iQuoteType)
1101        $aUnits['"'] = '\\"';
1102    else
1103        $aUnits = array("\"" => "&quot;", "'" => "&apos;");
1104
1105    return str_replace(array_keys($aUnits), array_values($aUnits), $mixedInput);
1106}
1107
1108/**
1109 * Escapes string/array ready to pass to php script with filtered symbols like ', " etc
1110 *
1111 * @param mixed $mixedInput - string/array which should be filtered
1112 * @return converted string / array
1113 */
1114function bx_php_string_apos ($mixedInput) {
1115    return str_replace("'", "\\'", $mixedInput);
1116}
1117function bx_php_string_quot ($mixedInput) {
1118    return str_replace('"', '\\"', $mixedInput);
1119}
1120
1121/**
1122 * Gets file contents by URL.
1123 *
1124 * @param string $sFileUrl - file URL to be read.
1125 * @param array $aParams - an array of parameters to be pathed with URL.
1126 * @return string the file's contents.
1127 */
1128function bx_file_get_contents($sFileUrl, $aParams = array(), $bChangeTimeout = false) {
1129
1130    if ($aParams)
1131        $sFileUrl = bx_append_url_params($sFileUrl, $aParams);
1132
1133    $sResult = '';
1134    if(function_exists('curl_init')) {
1135        $rConnect = curl_init();
1136
1137        curl_setopt($rConnect, CURLOPT_URL, $sFileUrl);
1138        curl_setopt($rConnect, CURLOPT_HEADER, 0);
1139        curl_setopt($rConnect, CURLOPT_RETURNTRANSFER, 1);
1140        curl_setopt($rConnect, CURLOPT_FOLLOWLOCATION, 1);
1141
1142        if ($bChangeTimeout) {
1143            curl_setopt($rConnect, CURLOPT_CONNECTTIMEOUT, 3);
1144            curl_setopt($rConnect, CURLOPT_TIMEOUT, 3);
1145        }
1146
1147        $sAllCookies = '';
1148        foreach($_COOKIE as $sKey=>$sValue){
1149            $sAllCookies .= $sKey."=".$sValue.";";
1150        }
1151        curl_setopt($rConnect, CURLOPT_COOKIE, $sAllCookies);
1152
1153        $sResult = curl_exec($rConnect);
1154        curl_close($rConnect);
1155    } else {
1156
1157        $iSaveTimeout = false;
1158        if ($bChangeTimeout) {
1159            $iSaveTimeout = ini_get('default_socket_timeout');
1160            ini_set('default_socket_timeout', 3);
1161        }
1162
1163        $sResult = @file_get_contents($sFileUrl);
1164
1165        if ($bChangeTimeout && false !== $iSaveTimeout) {
1166            ini_set('default_socket_timeout', $iSaveTimeout);
1167        }
1168    }
1169
1170    return $sResult;
1171}
1172
1173function bx_append_url_params ($sUrl, $mixedParams) {
1174    $sParams = false == strpos($sUrl, '?') ? '?' : '&';
1175       
1176    if (is_array($mixedParams)) {
1177        foreach($mixedParams as $sKey => $sValue)
1178            $sParams .= $sKey . '=' . $sValue . '&';
1179        $sParams = substr($sParams, 0, -1);
1180    } else {
1181        $sParams .= $mixedParams;
1182    }   
1183    return $sUrl . $sParams;
1184}
1185
1186/**
1187 * perform write log into 'tmp/log.txt' (for any debug development)
1188 *
1189 * @param $sNewLineText - New line debug text
1190  */
1191function writeLog($sNewLineText = 'test') {
1192    $sFileName = BX_DIRECTORY_PATH_ROOT . 'tmp/log.txt';
1193
1194    if (is_writable($sFileName)) {
1195        if (! $vHandle = fopen($sFileName, 'a')) {
1196             echo "Unable to open ({$sFileName})";
1197        }
1198        if (fwrite($vHandle, $sNewLineText . "\r\n") === FALSE) {
1199            echo "Unable write to ({$sFileName})";
1200        }
1201        fclose($vHandle);
1202
1203    } else {
1204        echo "{$sFileName} is not writeable";
1205    }
1206}
1207
1208function getLinkSet ($sLinkString, $sUrlPrefix, $sDivider = ';,', $bUriConvert = false) {
1209    $aSet = preg_split( '/['.$sDivider.']/', $sLinkString, 0, PREG_SPLIT_NO_EMPTY);
1210    $sFinalSet = '';
1211
1212    foreach ($aSet as $sKey) {
1213        $sLink =  $sUrlPrefix . urlencode($bUriConvert ? title2uri($sKey) : $sKey);
1214        $sFinalSet .= '<a href="' . $sUrlPrefix . urlencode(title2uri($sKey)) . '">' . $sKey . '</a> ';
1215    }
1216
1217    return trim($sFinalSet, ' ');
1218}
1219
1220// TODO: move to files modules - it is used only there
1221function getRelatedWords (&$aInfo) {
1222    $sString = implode(' ', $aInfo);
1223    $aRes = array_unique(explode(' ', $sString));
1224    $sString = implode(' ', $aRes);
1225    return addslashes($sString);
1226}
1227
1228// TODO: move to sites module - it is used only there
1229function getSiteInfo($sSourceUrl)
1230{
1231    $aResult = array();
1232    $sContent = bx_file_get_contents($sSourceUrl);
1233
1234    if (strlen($sContent))
1235    {
1236        preg_match("/<title>(.*)<\/title>/", $sContent, $aMatch);
1237        $aResult['title'] = $aMatch[1];
1238
1239        preg_match("/<meta.*name[='\" ]+description['\"].*content[='\" ]+(.*)['\"].*><\/meta>/", $sContent, $aMatch);
1240        $aResult['description'] = $aMatch[1];
1241    }
1242
1243    return $aResult;
1244}
1245
1246// calculation ini_get('upload_max_filesize') in bytes as example
1247function return_bytes($val) {
1248    $val = trim($val);
1249    $last = strtolower($val{strlen($val)-1});
1250    $val = (int)$val;
1251    switch($last) {
1252        // The 'G' modifier is available since PHP 5.1.0
1253        case 'k':
1254            $val *= 1024;
1255            break;
1256        case 'm':
1257            $val *= 1024 * 1024;
1258            break;
1259        case 'g':
1260            $val *= 1024 * 1024 * 1024;
1261            break;
1262    }
1263    return $val;
1264}
1265
1266// Generate Random Password
1267function genRndPwd($iLength = 8, $bSpecialCharacters = true) {
1268    $sPassword = '';
1269    $sChars = "abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";
1270
1271    if($bSpecialCharacters === true)
1272        $sChars .= "!?=/&+,.";
1273
1274    srand((double)microtime()*1000000);
1275    for($i = 0; $i < $iLength; $i++) {
1276        $x = mt_rand(0, strlen($sChars) -1);
1277        $sPassword .= $sChars{$x};
1278    }
1279
1280    return $sPassword;
1281}
1282
1283// Generate Random Salt for Password encryption
1284function genRndSalt() {
1285    return genRndPwd(8, true);
1286}
1287
1288// Encrypt User Password
1289function encryptUserPwd($sPwd, $sSalt) {
1290    return sha1(md5($sPwd) . $sSalt);
1291}
1292
1293// Advanced stripslashes. Strips strings and arrays
1294function stripslashes_adv($s) {
1295    if (is_string($s))
1296        return stripslashes($s);
1297    elseif (is_array($s)) {
1298        foreach ($s as $k => $v) {
1299            $s[$k] = stripslashes($v);
1300        }
1301        return $s;
1302    } else
1303        return $s;
1304}
1305
1306function bx_get ($sName) {
1307    if (isset($_GET[$sName]))
1308        return $_GET[$sName];
1309    elseif (isset($_POST[$sName]))
1310        return $_POST[$sName];
1311    else
1312        return false;
1313}
1314
1315function bx_encode_url_params ($a, $aExcludeKeys = array (), $aOnlyKeys = false) {
1316    $s = '';
1317    foreach ($a as $sKey => $sVal) {
1318        if (in_array($sKey, $aExcludeKeys))
1319            continue;
1320        if (false !== $aOnlyKeys && !in_array($sKey, $aOnlyKeys))
1321            continue;
1322        if (is_array($sVal)) {
1323            foreach ($sVal as $sSubVal) {
1324                $s .= rawurlencode($sKey) . '[]=' . rawurlencode(is_array($sSubVal) ? 'array' : $sSubVal) . '&';
1325            }
1326        } else {
1327            $s .= rawurlencode($sKey) . '=' . rawurlencode($sVal) . '&';
1328        }
1329    }
1330    return $s;
1331}
1332
1333/**
1334 * Convert array to attributes string
1335 *
1336 * Example:
1337 * @code
1338 * $a = array('name' => 'test', 'value' => 5);
1339 * $s = bx_convert_array2attrs($a);
1340 * echo $s; // outputs: name="test" value="5"
1341 * @endcode
1342 *
1343 * @param $a - array of attributes
1344 * @param $sClasses - classes to merge with 'class' attribute
1345 * @param $sStyles - styles to merge with 'style' attribute
1346 * @return string
1347 */
1348function bx_convert_array2attrs ($a, $sClasses = false, $sStyles = false) {
1349    $sRet = '';
1350
1351    if (!$a || !is_array($a))
1352        $a = array();
1353
1354    if ($sClasses) {
1355        $sClasses = trim($sClasses);
1356        $a['class'] = $sClasses . (!empty($a['class']) ? ' ' . $a['class'] : '');
1357    }
1358
1359    if ($sStyles) {
1360        $sStyles = trim($sStyles);
1361        if (';' != $sStyles[strlen($sStyles)-1])
1362            $sStyles .= ';';
1363        $a['style'] = $sStyles . (!empty($a['style']) ? ' ' . $a['style'] : '');
1364    }
1365
1366    foreach ($a as $sKey => $sValue) {
1367
1368        if (empty($sValue) || is_null($sValue)) // pass NULL values
1369            continue;
1370
1371        $sValueC = bx_html_attribute($sValue, BX_ESCAPE_STR_QUOTE);
1372
1373        $sRet .= " $sKey=\"$sValueC\"";
1374    }
1375
1376
1377    return $sRet;
1378}
1379
1380function bx_unicode_urldecode($s) {
1381
1382    preg_match_all('/%u([[:alnum:]]{4})/', $s, $a);
1383
1384    foreach ($a[1] as $uniord)
1385    {
1386        $dec = hexdec($uniord);
1387        $utf = '';
1388
1389        if ($dec < 128)
1390        {
1391            $utf = chr($dec);
1392        }
1393        else if ($dec < 2048)
1394        {
1395            $utf = chr(192 + (($dec - ($dec % 64)) / 64));
1396            $utf .= chr(128 + ($dec % 64));
1397        }
1398        else
1399        {
1400            $utf = chr(224 + (($dec - ($dec % 4096)) / 4096));
1401            $utf .= chr(128 + ((($dec % 4096) - ($dec % 64)) / 64));
1402            $utf .= chr(128 + ($dec % 64));
1403        }
1404
1405        $s = str_replace('%u'.$uniord, $utf, $s);
1406    }
1407
1408    return urldecode($s);
1409}
1410
1411/**
1412 * Raise an alert
1413 * @param string $sType - system type
1414 * @param string $sAction - system action
1415 * @param int $iObjectId - object id
1416 * @param int $iSenderId - sender (action's author) profile id, if it is false - then currectly logged in profile id is used
1417 */
1418function bx_alert($sUnit, $sAction, $iObjectId, $iSender = false, $aExtras = array()) {
1419    $o = new BxDolAlerts($sUnit, $sAction, $iObjectId, $iSender, $aExtras);
1420    $o->alert();
1421}
1422
1423function getSitesArray ($sLink) {
1424
1425    $aSites = $GLOBALS['MySQL']->fromCache ('sys_shared_sites', 'getAllWithKey', "SELECT `ID` as `id`, `URL` as `url`, `Icon` as `icon`, `Name` FROM `sys_shared_sites`", 'Name');
1426
1427    $sLink = htmlentities(($sLink));
1428
1429    foreach ($aSites as $sKey => $aValue)
1430        $aSites[$sKey]['url'] .= $sLink;
1431
1432    return $aSites;
1433}
1434
1435
1436/** @} */
1437
Note: See TracBrowser for help on using the repository browser.