BMLT Drupal Module
/home/travis/build/bmlt-enabled/bmlt-drupal/bmlt-drupal-satellite-plugin.php
Go to the documentation of this file.
1 <?php
2 /****************************************************************************************//**
3 * \file bmlt-drupal-satellite-plugin.php *
4 * *
5 * \brief This is a Drupal plugin of a BMLT satellite client. *
6 * \version 3.10.0 *
7 * *
8  This file is part of the Basic Meeting List Toolbox (BMLT).
9 
10  Find out more at: http://magshare.org/bmlt
11 
12  BMLT is free software: you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation, either version 3 of the License, or
15  (at your option) any later version.
16 
17  BMLT is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this code. If not, see <http://www.gnu.org/licenses/>.
24 ********************************************************************************************/
25 
26 // define ( '_DEBUG_MODE_', 1 ); //Uncomment for easier JavaScript debugging.
27 
28 // Include the satellite driver class.
29 define('ROOTPATH', __DIR__);
30 require_once(ROOTPATH .'/vendor/bmlt/bmlt-satellite-base-class/bmlt-cms-satellite-plugin.php');
31 
32 global $bmlt_localization; ///< Use this to control the localization.
33 
34 if (isset($_COOKIE) && isset($_COOKIE['bmlt_lang_selector']) && $_COOKIE['bmlt_lang_selector']) {
35  $bmlt_localization = $_COOKIE['bmlt_lang_selector'];
36 } else {
37  if (!isset($bmlt_localization) || !$bmlt_localization) {
38  $language = 'en';
39 
40  if (function_exists('i18n_get_lang')) {
41  $language = i18n_get_lang();
42  }
43 
44  if ($language) {
45  $bmlt_localization = substr($language, 0, 2);
46  }
47  }
48 }
49 
50 if (!isset($bmlt_localization) || !$bmlt_localization) {
51  $bmlt_localization = 'en'; ///< Last-ditch default value.
52 }
53 
54 /****************************************************************************************//**
55 * \class BMLTDrupalPlugin *
56 * *
57 * \brief This is the class that implements and encapsulates the plugin functionality. *
58 * A single instance of this is created, and manages the plugin. *
59 * *
60 * This plugin registers errors by echoing HTML comments, so look at the source code of *
61 * the page if things aren't working right. *
62 ********************************************************************************************/
63 // phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
64 class BMLTDrupalPlugin extends BMLTPlugin
65 // phpcs:enable PSR1.Classes.ClassDeclaration.MissingNamespace
66 {
67  /************************************************************************//**
68  * LOCALIZABLE STRINGS *
69  ****************************************************************************/
70  public $local_strings = array ( 'en' => array (
71  'list_text' => 'Substitute the BMLT shortcodes or HTML comments with instances of the BMLT',
72  'add_instance' => 'Add a BMLT instance inline in text.',
73  'bmlt' =>'Basic Meeting List Toolbox',
74  'bmlt_settings' => 'BMLT Settings',
75  'describe_admin' => 'Configure the BMLT Settings',
76  'access_admin' => 'access administration pages'
77  )
78  );
79 
80  /************************************************************************************//**
81  * \brief Constructor. *
82  ****************************************************************************************/
83  public function __construct()
84  {
85  parent::__construct();
86  }
87 
88  /************************************************************************************//**
89  * \brief Return an HTTP path to the AJAX callback target. *
90  * *
91  * \returns a string, containing the path. *
92  ****************************************************************************************/
93  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
94  protected function get_admin_ajax_base_uri()
95  {
96  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
97  return $this->get_ajax_base_uri().'?q=admin/settings/bmlt';
98  }
99 
100  /************************************************************************************//**
101  * \brief Return an HTTP path to the basic admin form submit (action) URI *
102  * *
103  * \returns a string, containing the path. *
104  ****************************************************************************************/
105  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
106  protected function get_admin_form_uri()
107  {
108  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
109  return $this->get_admin_ajax_base_uri();
110  }
111 
112  /************************************************************************************//**
113  * \brief Return an HTTP path to the AJAX callback target. *
114  * *
115  * \returns a string, containing the path. *
116  ****************************************************************************************/
117  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
118  protected function get_ajax_base_uri()
119  {
120  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
121  // We try to account for SSL and unusual TCP ports.
122  $port = null;
123  $https = false;
124  $from_proxy = array_key_exists("HTTP_X_FORWARDED_PROTO", $_SERVER);
125  if ($from_proxy) {
126  // If the port is specified in the header, use it. If not, default to 80
127  // for http and 443 for https. We can't trust what's in $_SERVER['SERVER_PORT']
128  // because something in front of the server is fielding the request.
129  $https = $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https';
130  if (array_key_exists("HTTP_X_FORWARDED_PORT", $_SERVER)) {
131  $port = intval($_SERVER['HTTP_X_FORWARDED_PORT']);
132  } elseif ($https) {
133  $port = 443;
134  } else {
135  $port = 80;
136  }
137  } else {
138  $port = $_SERVER['SERVER_PORT'];
139  // IIS puts "off" in the HTTPS field, so we need to test for that.
140  $https = (!empty($_SERVER['HTTPS']) && (($_SERVER['HTTPS'] !== 'off') || ($port == 443)));
141  }
142 
143  $server_path = $_SERVER['SERVER_NAME'];
144  $my_path = $_SERVER['PHP_SELF'];
145  $server_path .= trim((($https && ($port != 443)) || (!$https && ($port != 80))) ? ':'.$port : '', '/');
146  $server_path = 'http'.($https ? 's' : '').'://'.$server_path.$my_path;
147  return $server_path;
148  }
149 
150  /************************************************************************************//**
151  * \brief Return an HTTP path to the plugin directory. *
152  * *
153  * \returns a string, containing the path. *
154  ****************************************************************************************/
155  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
156  protected function get_plugin_path()
157  {
158  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
159  if (function_exists('drupal_get_path')) {
160  global $base_url;
161 
162  $ret = $base_url.'/'.drupal_get_path('module', 'bmlt').'/vendor/bmlt/bmlt-satellite-base-class/';
163  } else {
164  $ret = isset($this->my_http_vars['base_url']) ? $this->my_http_vars['base_url'] : null;
165  }
166 
167  return $ret;
168  }
169 
170  /************************************************************************************//**
171  * \brief This uses the Drupal text processor (t) to process the given string. *
172  * *
173  * This allows easier translation of displayed strings. All strings displayed by the *
174  * plugin should go through this function. *
175  * *
176  * \returns a string, processed by WP. *
177  ****************************************************************************************/
178  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
179  public function process_text( $in_string ///< The string to be processed.
180  )
181  {
182  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
183  if (function_exists('t')) {
184  $in_string = htmlspecialchars(t($in_string));
185  } else {
186  echo "<!-- BMLTPlugin Warning (process_text): t() does not exist! -->";
187  }
188 
189  return $in_string;
190  }
191 
192  /************************************************************************************//**
193  * \brief This gets the admin options from the database (allows CMS abstraction). *
194  * *
195  * \returns an associative array, with the option settings. *
196  ****************************************************************************************/
197  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
198  protected function cms_get_option( $in_option_key ///< The name of the option
199  )
200  {
201  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
202  $ret = null;
203 
204  $row = unserialize(variable_get('bmlt_settings', serialize(array ( 0 => $this->geDefaultBMLTOptions() ))));
205 
206  if ($in_option_key != self::$admin2OptionsName) {
207  $index = max(1, intval(str_replace(self::$adminOptionsName.'_', '', $in_option_key)));
208 
209  $ret = isset($row[$index - 1]) ? $row[$index - 1] : $defaults[$index - 1];
210  } else {
211  $ret = array ( 'num_servers' => count($row) );
212  }
213 
214  return $ret;
215  }
216 
217  /************************************************************************************//**
218  * \brief This gets the admin options from the database (allows CMS abstraction). *
219  ****************************************************************************************/
220  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
221  protected function cms_set_option(
222  $in_option_key, ///< The name of the option
223  $in_option_value ///< the values to be set (associative array)
224  ) {
225  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
226  $ret = false;
227 
228  $index = 0;
229 
230  if ($in_option_key != self::$admin2OptionsName) {
231  $index = max(1, intval(str_replace(self::$adminOptionsName.'_', '', $in_option_key)));
232 
233  $row_data = unserialize(variable_get('bmlt_settings', serialize(array ( 0 => $this->geDefaultBMLTOptions() ))));
234 
235  if (isset($row_data) && is_array($row_data) && count($row_data)) {
236  $row_data[$index - 1] = $in_option_value;
237 
238  variable_set('bmlt_settings', serialize($row_data));
239 
240  $ret = true;
241  }
242  } else {
243  $ret = true; // Fake it, till you make it.
244  }
245 
246  return $ret;
247  }
248 
249  /************************************************************************************//**
250  * \brief Deletes a stored option (allows CMS abstraction). *
251  ****************************************************************************************/
252  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
253  protected function cms_delete_option( $in_option_key ///< The name of the option
254  )
255  {
256  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
257  $ret = false;
258 
259  $row = unserialize(variable_get('bmlt_settings', serialize(array ( 0 => $this->geDefaultBMLTOptions() ))));
260  if ($in_option_key != self::$admin2OptionsName) {
261  $index = max(1, intval(str_replace(self::$adminOptionsName.'_', '', $in_option_key)));
262 
263  unset($row[$index - 1]);
264 
265  if (variable_set('bmlt_settings', serialize($row))) {
266  $ret = true;
267  }
268  }
269 
270  return $ret;
271  }
272 
273  /************************************************************************************//**
274  * \brief This gets the page meta for the given page. (allows CMS abstraction). *
275  * *
276  * \returns a mixed type, with the meta data *
277  ****************************************************************************************/
278  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
279  protected function cms_get_post_meta(
280  $in_page_id, ///< The ID of the page/post
281  $in_settings_id ///< The ID of the meta tag to fetch
282  ) {
283  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
284  $ret = null;
285 
286  return $ret;
287  }
288 
289  /************************************************************************************//**
290  * \brief This function fetches the settings ID for a page (if there is one). *
291  * *
292  * If $in_check_mobile is set to true, then ONLY a check for mobile support will be *
293  * made, and no other shortcodes will be checked. *
294  * *
295  * \returns a mixed type, with the settings ID. *
296  ****************************************************************************************/
297  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
298  protected function cms_get_page_settings_id(
299  $in_text, ///< Required (for the base version) content to check.
300  $in_check_mobile = false ///< True if this includes a check for mobile. Default is false.
301  ) {
302  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
303  $my_option_id = parent::cms_get_page_settings_id($in_text, $in_check_mobile);
304 
305  if (!$in_check_mobile && !$my_option_id) { // If nothing else gives, we go for the default (first) settings.
306  $options = $this->getBMLTOptions(1);
307  $my_option_id = $options['id'];
308  }
309 
310  return $my_option_id;
311  }
312 
313  /************************************************************************************//**
314  * THE DRUPAL CALLBACKS *
315  ****************************************************************************************/
316 
317  /************************************************************************************//**
318  * \brief Presents the admin page. *
319  ****************************************************************************************/
320  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
321  public function admin_page()
322  {
323  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
324  echo $this->return_admin_page();
325  }
326 
327  /************************************************************************************//**
328  * \brief Presents the admin menu options. *
329  * *
330  * NOTE: This function requires WP. Most of the rest can probably be more easily *
331  * converted for other CMSes. *
332  ****************************************************************************************/
333  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
334  public function option_menu()
335  {
336  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
337  if (function_exists('add_options_page') && (self::get_plugin_object() instanceof BMLTPlugin)) {
338  add_options_page(self::$local_options_title, self::$local_menu_string, 9, basename(__FILE__), array ( self::get_plugin_object(), 'admin_page' ));
339  } elseif (!function_exists('add_options_page')) {
340  echo "<!-- BMLTPlugin ERROR (option_menu)! No add_options_page()! -->";
341  } else {
342  echo "<!-- BMLTPlugin ERROR (option_menu)! No BMLTPlugin Object! -->";
343  }
344  }
345 
346  /************************************************************************************//**
347  * \brief Echoes any necessary head content. *
348  ****************************************************************************************/
349  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
350  public function standard_head( $in_text = null ///< This is the page content text.
351  )
352  {
353  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
354  $load_head = true;
355 
356  $additional_stuff = "<!-- Added by the BMLT plugin 3.X. -->\n<meta http-equiv=\"X-UA-Compatible\" content=\"IE=EmulateIE7\" />\n<meta http-equiv=\"Content-Style-Type\" content=\"text/css\" />\n<meta http-equiv=\"Content-Script-Type\" content=\"text/javascript\" />\n";
357 
358  $support_mobile = $this->cms_get_page_settings_id($in_text, true);
359 
360  if ($support_mobile) {
361  $mobile_options = $this->getBMLTOptions_by_id($support_mobile);
362  } else {
363  $support_mobile = null;
364  }
365 
366  $options = $this->getBMLTOptions_by_id($this->cms_get_page_settings_id($in_text));
367  if ($support_mobile && is_array($mobile_options) && count($mobile_options)) {
368  $mobile_url = $_SERVER['PHP_SELF'].'?BMLTPlugin_mobile&bmlt_settings_id='.$support_mobile;
369  if (isset($this->my_http_vars['WML'])) {
370  $mobile_url .= '&WML='.intval($this->my_http_vars['WML']);
371  }
372  if (isset($this->my_http_vars['simulate_smartphone'])) {
373  $mobile_url .= '&simulate_smartphone';
374  }
375 
376  if (isset($this->my_http_vars['base_url'])) {
377  $mobile_url .= '&base_url='.urlencode($this->my_http_vars['base_url']);
378  } else {
379  $mobile_url .= '&base_url='.urlencode($this->get_plugin_path());
380  }
381 
382  ob_end_clean();
383  header("location: $mobile_url");
384  die();
385  }
386 
387  $this->load_params();
388 
389  $root_server_root = $options['root_server'];
390 
391  $additional_stuff .= '<meta name="BMLT-Root-URI" content="'.htmlspecialchars($root_server_root).'" />';
392 
393  if ($root_server_root) {
394  $root_server = $root_server_root."/client_interface/xhtml/index.php";
395 
396  $additional_css = '.bmlt_container * {margin:0;padding:0 }';
397 
398  $temp = self::stripFile("nouveau_map_styles.css");
399  if ($temp) {
400  $additional_css .= "\t$temp\n";
401  }
402 
403  $temp = self::stripFile("table_styles.css");
404  if ($temp) {
405  $additional_css .= "\t$temp\n";
406  }
407 
408  $temp = self::stripFile("styles.css", $options['theme']);
409  if ($temp) {
410  $image_dir_path = $this->get_plugin_path() . '/themes/' . $options['theme'] . '/images/';
411  $temp = str_replace('##-IMAGEDIR-##', $image_dir_path, $temp);
412  $additional_css .= "\t$temp\n";
413  }
414 
415  $temp = self::stripFile("nouveau_map_styles.css", $options['theme']);
416  if ($temp) {
417  $image_dir_path = $this->get_plugin_path() . '/themes/' . $options['theme'] . '/images/';
418  $temp = str_replace('##-IMAGEDIR-##', $image_dir_path, $temp);
419  $additional_css .= "\t$temp\n";
420  }
421 
422  $temp = self::stripFile("quicksearch.css");
423  if ($temp) {
424  $additional_css .= "\t$temp\n";
425  }
426 
427  $dirname = dirname(__FILE__) . '/vendor/bmlt/bmlt-satellite-base-class/themes';
428  $dir = new DirectoryIterator($dirname);
429 
430  foreach ($dir as $fileinfo) {
431  if (!$fileinfo->isDot()) {
432  $fName = $fileinfo->getFilename();
433 
434  $temp = self::stripFile("table_styles.css", $fName);
435  if ($temp) {
436  $image_dir_path = $this->get_plugin_path() . '/themes/' . $fName . '/images/';
437  $temp = str_replace('##-IMAGEDIR-##', $image_dir_path, $temp);
438  $additional_css .= "\t$temp\n";
439  }
440 
441  $temp = self::stripFile("quicksearch.css", $fName);
442  if ($temp) {
443  $additional_css .= "\t$temp\n";
444  }
445  }
446  }
447 
448  $temp = self::stripFile("responsiveness.css");
449  if ($temp) {
450  $additional_css .= "\t$temp\n";
451  }
452 
453  if ($options['additional_css']) {
454  $additional_css .= $options['additional_css'];
455  }
456 
457  if ($additional_css) {
458  $additional_stuff .= '<style type="text/css">'.preg_replace("|\s+|", " ", $additional_css).'</style>';
459  }
460 
461  $additional_stuff .= '<script type="text/javascript">';
462 
463  $additional_stuff .= self::stripFile('javascript.js');
464 
465  if ($this->get_shortcode($in_text, 'bmlt_quicksearch')) {
466  $additional_stuff .= self::stripFile('quicksearch.js') . (defined('_DEBUG_MODE_') ? "\n" : '');
467  }
468 
469  if ($this->get_shortcode($in_text, 'bmlt_map')) {
470  $additional_stuff .= self::stripFile('map_search.js');
471  }
472 
473  if ($this->get_shortcode($in_text, 'bmlt_mobile')) {
474  $additional_stuff .= self::stripFile('fast_mobile_lookup.js');
475  }
476 
477  $additional_stuff .= '</script>';
478 
479  if ($additional_stuff) {
480  if (function_exists('drupal_set_html_head')) {
481  drupal_set_html_head($additional_stuff);
482  } elseif (function_exists('drupal_add_html_head')) {
483  $element = array(
484  '#type' => 'markup',
485  '#markup' => $additional_stuff
486  );
487  drupal_add_html_head($element, 'bmlt');
488  }
489  }
490  }
491  }
492 
493  /************************************************************************************//**
494  * \brief Echoes any necessary head content for the admin. *
495  ****************************************************************************************/
496  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
497  public function admin_head()
498  {
499  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
500  $head_content = "<!-- Added by the BMLT plugin 3.0. -->\n<meta http-equiv=\"X-UA-Compatible\" content=\"IE=EmulateIE7\" />\n<meta http-equiv=\"Content-Style-Type\" content=\"text/css\" />\n<meta http-equiv=\"Content-Script-Type\" content=\"text/javascript\" />\n";
501  $head_content .= '<script type="text/javascript">';
502 
503  $head_content .= self::stripFile('javascript.js');
504 
505  $head_content .= '</script>';
506 
507  $options = $this->getBMLTOptions(1); // All options contain the admin key.
508  $key = $options['google_api_key'];
509 
510  $head_content .= '<script type="text/javascript" src="https://maps.google.com/maps/api/js?key='.$key.'"></script>'; // Load the Google Maps stuff for our map.
511 
512  $head_content .= '<link rel="stylesheet" type="text/css" href="';
513 
514  $url = $this->get_plugin_path();
515 
516  $head_content .= htmlspecialchars($url);
517 
518  if (!defined('_DEBUG_MODE_')) {
519  $head_content .= 'style_stripper.php?filename=';
520  }
521 
522  $head_content .= 'admin_styles.css" />';
523 
524  $head_content .= '<script type="text/javascript" src="';
525 
526  $head_content .= htmlspecialchars($url);
527 
528  if (!defined('_DEBUG_MODE_')) {
529  $head_content .= 'js_stripper.php?filename=';
530  }
531 
532  $head_content .= 'admin_javascript.js"></script>';
533 
534  if (function_exists('drupal_set_html_head')) {
535  drupal_set_html_head($head_content);
536  } elseif (function_exists('drupal_add_html_head')) {
537  $element = array(
538  '#type' => 'markup',
539  '#markup' => $head_content
540  );
541  drupal_add_html_head($element, 'bmlt');
542  }
543  }
544 }
545 
546 /****************************************************************************************//**
547 * MAIN CODE CONTEXT *
548 ********************************************************************************************/
549 global $BMLTPluginOp;
550 
551 if (!isset($BMLTPluginOp) && class_exists("BMLTDrupalPlugin")) {
552  $BMLTPluginOp = new BMLTDrupalPlugin();
553 }
global $bmlt_localization
Use this to control the localization.