diff --git a/src/mibew/libs/classes/Mibew/Controller/ButtonCodeController.php b/src/mibew/libs/classes/Mibew/Controller/ButtonCodeController.php index dfd157f6..cdf9cb6a 100644 --- a/src/mibew/libs/classes/Mibew/Controller/ButtonCodeController.php +++ b/src/mibew/libs/classes/Mibew/Controller/ButtonCodeController.php @@ -46,7 +46,7 @@ class ButtonCodeController extends AbstractController 'errors' => array(), ); - $image_locales_map = get_image_locales_map(MIBEW_FS_ROOT . '/locales'); + $image_locales_map = $this->getImageLocalesMap(MIBEW_FS_ROOT . '/locales'); $image = $request->query->get('i', 'mibew'); if (!isset($image_locales_map[$image])) { $page['errors'][] = 'Unknown image: ' . $image; @@ -134,7 +134,8 @@ class ButtonCodeController extends AbstractController $message = getlocal('Click to chat'); } - $page['buttonCode'] = generate_button( + $page['buttonCode'] = $this->generateButton( + $request, '', $lang, $style, @@ -151,7 +152,7 @@ class ButtonCodeController extends AbstractController $page['availableLocales'] = $generate_button ? $image_locales : $locales_list; $page['availableChatStyles'] = $style_list; $page['availableInvitationStyles'] = $invitation_style_list; - $page['groups'] = get_groups_list(); + $page['groups'] = $this->getGroupsList(); $page['availableCodeTypes'] = array( 'button' => getlocal('button'), @@ -180,4 +181,189 @@ class ButtonCodeController extends AbstractController return $this->render('button_code', $page); } + + /** + * Generates button code. + * + * @param string $request Request incoming request. + * @param string $title Page title + * @param string $locale RFC 5646 code for language + * @param string $style name of available style from styles/dialogs folder + * @param string $invitation_style_name name of avalabel style from + * styles/invitations folder + * @param integer $group chat group id + * @param integer $inner chat link message or html code like image code + * @param bool $show_host generated link contains protocol and domain or not + * @param bool $force_secure force protocol to secure (https) or not + * @param bool $mod_security add rule to remove protocol from document location + * in generated javascript code + * @param bool $operator_code add operator code to generated button code or not + * @param bool $disable_invitation forcibly disable invitation regadless of + * tracking settings + * + * @return string Generate chat button code + */ + protected function generateButton( + $request, + $title, + $locale, + $style, + $invitation_style_name, + $group, + $inner, + $show_host, + $force_secure, + $mod_security, + $operator_code, + $disable_invitation + ) { + $host = ($force_secure ? 'https://' : 'http://') . $request->getHost(); + $base_url = ($show_host ? $host : '') + . $request->getBasePath(); + + $url_type = $show_host + ? UrlGeneratorInterface::ABSOLUTE_URL + : UrlGeneratorInterface::ABSOLUTE_PATH; + + // Build the main link + $link_params = array(); + if ($locale) { + $link_params['locale'] = $locale; + } + if ($style) { + $link_params['style'] = $style; + } + if ($group) { + $link_params['group'] = $group; + } + $link = ($show_host && $force_secure) + ? $this->generateSecureUrl('chat_user_start', $link_params) + : $this->generateUrl('chat_user_start', $link_params, $url_type); + + $modsecfix = $mod_security ? ".replace('http://','').replace('https://','')" : ""; + $js_link = "'" . $link + . (empty($link_params) ? '?' : '&') + . "url='+escape(document.location.href$modsecfix)+'&referrer='+escape(document.referrer$modsecfix)"; + + // Get popup window configurations + if ($style) { + $chat_style = new ChatStyle($style); + $chat_configurations = $chat_style->getConfigurations(); + $popup_options = $chat_configurations['chat']['window_params']; + } else { + $popup_options = "toolbar=0,scrollbars=0,location=0,status=1,menubar=0,width=640,height=480,resizable=1"; + } + + // Generate operator code field + if ($operator_code) { + $form_on_submit = "if(navigator.userAgent.toLowerCase().indexOf('opera') != -1 " + . "&& window.event.preventDefault) window.event.preventDefault();" + . "this.newWindow = window.open({$js_link} + '&operator_code=' " + . "+ document.getElementById('mibewOperatorCodeField').value, 'mibew', '{$popup_options}');" + . "this.newWindow.focus();this.newWindow.opener=window;return false;"; + $temp = '
'; + return "" . $temp . ""; + } + + // Generate button + $temp = get_popup($link, "$js_link", $inner, $title, "mibew", $popup_options); + if (!$disable_invitation && Settings::get('enabletracking')) { + $widget_data = array(); + + // Get actual invitation style instance + if (!$invitation_style_name) { + $invitation_style_name = InvitationStyle::getCurrentStyle(); + } + $invitation_style = new InvitationStyle($invitation_style_name); + + // URL of file with additional CSS rules for invitation popup + $widget_data['inviteStyle'] = $base_url . '/' . + $invitation_style->getFilesPath() . + '/invite.css'; + + // Time between requests to the server in milliseconds + $widget_data['requestTimeout'] = Settings::get('updatefrequency_tracking') * 1000; + + // URL for requests + $widget_data['requestURL'] = ($show_host && $force_secure) + ? $this->generateSecureUrl('widget_gateway', array()) + : $this->generateUrl('widget_gateway', array(), $url_type); + + // Locale for invitation + $widget_data['locale'] = $locale; + + // Name of the cookie to track user. Use if third-party cookie blocked + $widget_data['visitorCookieName'] = VISITOR_COOKIE_NAME; + + // Build additional button code + $temp = preg_replace('/^(' + . '' + . ''; + } + + return "" . $temp . ""; + } + + /** + * Prepares list of group options. + * + * @return array Each element of the resultion array is an array with group + * info. See {@link get_all_groups()} description for more details. + */ + protected function getGroupsList() + { + $result = array(); + $all_groups = get_all_groups(); + + $result[] = array( + 'groupid' => '', + 'vclocalname' => getlocal("-all operators-"), + 'level' => 0, + ); + foreach ($all_groups as $g) { + $result[] = $g; + } + + return $result; + } + + /** + * Maps locales onto existing images. + * + * @param string $locales_dir Base directory of locales. + * + * @return array The keys of the resulting array are images names and the + * values are arrays of locales which contains the image. + */ + function getImageLocalesMap($locales_dir) + { + $image_locales = array(); + $all_locales = get_available_locales(); + foreach ($all_locales as $curr) { + $images_dir = "$locales_dir/$curr/button"; + if ($handle = @opendir($images_dir)) { + while (false !== ($file = readdir($handle))) { + $both_files_exist = preg_match("/^(\w+)_on\.(gif|png)$/", $file, $matches) + && is_file("$images_dir/" . $matches[1] . "_off." . $matches[2]); + if ($both_files_exist) { + $image = $matches[1]; + if (!isset($image_locales[$image])) { + $image_locales[$image] = array(); + } + $image_locales[$image][] = $curr; + } + } + closedir($handle); + } + } + + return $image_locales; + } } diff --git a/src/mibew/libs/getcode.php b/src/mibew/libs/getcode.php deleted file mode 100644 index 6c6713a0..00000000 --- a/src/mibew/libs/getcode.php +++ /dev/null @@ -1,192 +0,0 @@ -getConfigurations(); - $popup_options = $chat_configurations['chat']['window_params']; - } else { - $popup_options = "toolbar=0,scrollbars=0,location=0,status=1,menubar=0,width=640,height=480,resizable=1"; - } - - // Generate operator code field - if ($operator_code) { - $form_on_submit = "if(navigator.userAgent.toLowerCase().indexOf('opera') != -1 " - . "&& window.event.preventDefault) window.event.preventDefault();" - . "this.newWindow = window.open({$js_link} + '&operator_code=' " - . "+ document.getElementById('mibewOperatorCodeField').value, 'mibew', '{$popup_options}');" - . "this.newWindow.focus();this.newWindow.opener=window;return false;"; - $temp = ''; - return "" . $temp . ""; - } - - // Generate button - $temp = get_popup($link, "$js_link", $inner, $title, "mibew", $popup_options); - if (!$disable_invitation && Settings::get('enabletracking')) { - $widget_data = array(); - - // Get actual invitation style instance - if (!$invitation_style_name) { - $invitation_style_name = InvitationStyle::getCurrentStyle(); - } - $invitation_style = new InvitationStyle($invitation_style_name); - - // URL of file with additional CSS rules for invitation popup - $widget_data['inviteStyle'] = $app_location . '/' . - $invitation_style->getFilesPath() . - '/invite.css'; - - // Time between requests to the server in milliseconds - $widget_data['requestTimeout'] = Settings::get('updatefrequency_tracking') * 1000; - - // URL for requests - $widget_data['requestURL'] = $app_location . '/widget'; - - // Locale for invitation - $widget_data['locale'] = $locale; - - // Name of the cookie to track user. Use if third-party cookie blocked - $widget_data['visitorCookieName'] = VISITOR_COOKIE_NAME; - - // Build additional button code - $temp = preg_replace('/^(' - . '' - . ''; - } - - return "" . $temp . ""; -} - -/** - * Return list of all chat groups. - * - * @return array It is chat groups structure. contains (groupid integer, - * parent integer, vclocalname string, vclocaldescription string) - */ -function get_groups_list() -{ - $result = array(); - $all_groups = get_all_groups(); - $result[] = array( - 'groupid' => '', - 'vclocalname' => getlocal("-all operators-"), - 'level' => 0, - ); - foreach ($all_groups as $g) { - $result[] = $g; - } - - return $result; -} - -/** - * Return map of chat button images. - * - * @param string $locales_dir Base directory of locales - * - * @return array locales map images. - */ -function get_image_locales_map($locales_dir) -{ - $image_locales = array(); - $all_locales = get_available_locales(); - foreach ($all_locales as $curr) { - $images_dir = "$locales_dir/$curr/button"; - if ($handle = @opendir($images_dir)) { - while (false !== ($file = readdir($handle))) { - $both_files_exist = preg_match("/^(\w+)_on\.(gif|png)$/", $file, $matches) - && is_file("$images_dir/" . $matches[1] . "_off." . $matches[2]); - if ($both_files_exist) { - $image = $matches[1]; - if (!isset($image_locales[$image])) { - $image_locales[$image] = array(); - } - $image_locales[$image][] = $curr; - } - } - closedir($handle); - } - } - - return $image_locales; -} diff --git a/src/mibew/libs/init.php b/src/mibew/libs/init.php index 3507e829..692a7f9b 100644 --- a/src/mibew/libs/init.php +++ b/src/mibew/libs/init.php @@ -99,7 +99,6 @@ if (!installation_in_progress()) { require_once(MIBEW_FS_ROOT . '/libs/canned.php'); require_once(MIBEW_FS_ROOT . '/libs/captcha.php'); require_once(MIBEW_FS_ROOT . '/libs/chat.php'); -require_once(MIBEW_FS_ROOT . '/libs/getcode.php'); require_once(MIBEW_FS_ROOT . '/libs/groups.php'); require_once(MIBEW_FS_ROOT . '/libs/invitation.php'); require_once(MIBEW_FS_ROOT . '/libs/notify.php');