source: extensions/FacebookPlug/Plugin/include/header.inc.php @ 8435

Last change on this file since 8435 was 8435, checked in by rub, 13 years ago

Fix language set/detect on Facebook connect

  • Property svn:eol-style set to LF
File size: 6.9 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | FacebookPlug - a Piwigo Plugin                                        |
4// | Copyright (C) 2010-2011 Ruben ARNAUD - rub@piwigo.org                 |
5// +-----------------------------------------------------------------------+
6// | This program is free software; you can redistribute it and/or modify  |
7// | it under the terms of the GNU General Public License as published by  |
8// | the Free Software Foundation                                          |
9// |                                                                       |
10// | This program is distributed in the hope that it will be useful, but   |
11// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
12// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
13// | General Public License for more details.                              |
14// |                                                                       |
15// | You should have received a copy of the GNU General Public License     |
16// | along with this program; if not, write to the Free Software           |
17// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
18// | USA.                                                                  |
19// +-----------------------------------------------------------------------+
20
21if (!defined('PHPWG_ROOT_PATH'))
22{
23  die('Hacking attempt!');
24}
25
26function fbp_get_fb_language()
27{
28  global $user;
29
30  // Array compute by plugins/FacebookPlug/tools/check_language.php script
31  $valid_fb_languages = array ( 0 => 'ca_ES', 1 => 'cs_CZ', 2 => 'da_DK', 3 => 'de_DE', 4 => 'es_ES', 5 => 'fa_IR', 6 => 'fr_CA', 7 => 'fr_FR', 8 => 'he_IL', 9 => 'hr_HR', 10 => 'hu_HU', 11 => 'it_IT', 12 => 'ja_JP', 13 => 'ka_GE', 14 => 'lv_LV', 15 => 'mk_MK', 16 => 'nl_NL', 17 => 'pl_PL', 18 => 'pt_BR', 19 => 'pt_PT', 20 => 'ro_RO', 21 => 'ru_RU', 22 => 'sk_SK', 23 => 'sr_RS', 24 => 'sv_SE', 25 => 'tr_TR', 26 => 'vi_VN', 27 => 'zh_CN', );
32
33  // User language
34  $language = $user['language'];
35  if (! in_array($language, $valid_fb_languages))
36  {
37    $code_lang = substr($language, 0, 2);
38    // Compose a language
39    $language = $code_lang.'_'.strtoupper($code_lang);
40    if (! in_array($language, $valid_fb_languages))
41    {
42      // Search a language
43      foreach ($valid_fb_languages as $valid_fb_language)
44      {
45        if ($code_lang == substr($valid_fb_language, 0, 2))
46        {
47          return $valid_fb_language;
48        }
49      }
50      // default language
51      $language = 'en_US';
52    }
53  }
54
55  return $language;
56}
57
58function fbp_header($content, &$smarty)
59{
60  // replace tag html
61  $search = '<html ';
62  $replacement = '<html xmlns:fb="http://www.facebook.com/2008/fbml" ';
63
64  //~ $content = preg_replace('#'.$search.'#', $replacement, $content);
65  return preg_replace('#'.$search.'#', $replacement, $content);
66  //~ $fbp_content = file_get_contents(FBP_DIR.'/tpl/init.fb.tpl');
67
68  //~ $search = '<div id="the_page">';
69  //~ return preg_replace('#'.$search.'#', $fbp_content.$search, $content);
70}
71
72function fbp_init()
73{
74  global $template, $conf, $user, $page;
75
76  // Get init facebook
77  $page['fbp']['do_facebook_init'] = trigger_event('fbp_do_facebook_init', $conf['fbp']['force_facebook_init']);
78  if ($page['fbp']['do_facebook_init'])
79  {
80    // define ID
81    $template->assign('FACEBOOK_APP_ID', (is_numeric($conf['fbp']['facebook_app_id']) ? $conf['fbp']['facebook_app_id'] : FACEBOOK_APP_ID));
82    // define language
83    $template->assign('FACEBOOK_LANGUAGE', fbp_get_fb_language());
84    $template->assign('fbp', $conf['fbp']);
85    //~ $template->smarty->register_modifier('boolean_to_string', 'boolean_to_string');
86  }
87}
88
89function fbp_loc_end_page_header()
90{
91  global $template, $conf, $user, $page;
92
93  if ($page['fbp']['do_facebook_init'])
94  {
95    // set prefilter
96    $template->set_prefilter('header', 'fbp_header');
97
98    if (empty($page['fbp']['url']))
99    {
100      $page['fbp']['url'] = get_absolute_root_url().script_basename();
101      if ($conf['php_extension_in_urls'])
102      {
103        $page['fbp']['url'] .= '.php';
104      }
105    }
106
107    $template->assign('fbp_page', $page['fbp']);
108
109    $template->set_filename('fbp_init.fb', FBP_DIR.'/tpl/init.fb.tpl');
110    $template->append('head_elements', $template->parse('fbp_init.fb', true));
111
112    if (
113        (script_basename() == 'index')
114        and
115        ($conf['fbp']['social_plugin_activity_feed']['enabled'] or $conf['fbp']['social_plugin_like_box']['enabled'])
116        )
117    {
118      $template->block_html_head('', '<link rel="stylesheet" type="text/css" href="'.FBP_PATH.'/css/fbp.css">', $smarty, $repeat);
119    }
120  }
121}
122
123function fbp_do_facebook_init($do_it)
124{
125  global $conf;
126
127  return 
128    $do_it
129  or
130    (
131      (
132        (script_basename() == 'picture')
133        and
134        ($conf['fbp']['social_plugin_like_button']['enabled'] or $conf['fbp']['social_plugin_facepile']['enabled'] or $conf['fbp']['social_plugin_comments']['enabled'])
135      )
136      or
137      (
138        (script_basename() == 'index')
139        and
140        ($conf['fbp']['social_plugin_activity_feed']['enabled'] or $conf['fbp']['social_plugin_like_box']['enabled'])
141      )
142      or
143      (
144        (script_basename() == 'admin')
145        and
146        (isset($_GET['page']) and $_GET['page'] == 'plugin')
147        and
148        (isset($_GET['section']) and strtok($_GET['section'], '/') == 'FacebookPlug')
149      )
150    )
151  ;
152}
153
154function fbp_is_facebook_ip()
155{
156  foreach (array(
157    FBP_IP_FB, 
158    //~ '127.0',
159      ) as $ip_fb)
160  {
161    if (preg_match('/'.$ip_fb. '/', $_SERVER["REMOTE_ADDR"]))
162    {
163      return true;
164    }
165  }
166  return false;
167}
168
169function ftp_loc_end_section_init()
170{
171  global $conf, $page, $user;
172
173  // No restristion for facebook user
174  if (
175      $page['fbp']['do_facebook_init']
176      and 
177      is_a_guest()
178      and
179      $conf['fbp']['allow_fb_access_private_page']
180      and
181      fbp_is_facebook_ip()
182    )
183  {
184    global $user, $header_notes;
185
186    // Notes
187    $header_notes[] = 'Facebook robot detected, guest can access private page';
188    // Allow guest access
189    $conf['guest_access'] = true;
190    // No forbidden categorie
191    $user['forbidden_categories'] = '';
192    $user['level'] = max($conf['available_permission_levels']);
193    switch (script_basename())
194    {
195      case 'picture':
196        if (isset($page['image_id']))
197        {
198          $page['rank_of'][$page['image_id']] = 0;
199          $page['items'] = array_flip($page['rank_of']);
200        }
201        break;
202      case 'index':
203        if (isset($page['category']['representative_picture_id']))
204        {
205          fbp_loc_begin_index_category_thumbnails(array($page['category']));
206        }
207      break;
208    }
209    //~ else if $page['category']['id']
210  }
211}
212
213add_event_handler('init', 'fbp_init');
214add_event_handler('loc_end_page_header', 'fbp_loc_end_page_header');
215add_event_handler('fbp_do_facebook_init', 'fbp_do_facebook_init');
216add_event_handler('loc_end_section_init', 'ftp_loc_end_section_init');
217
218?>
Note: See TracBrowser for help on using the repository browser.