CMS  Version 3.9
phonegap_manager.inc
Go to the documentation of this file.
1 <?php
13 global $phonegap;
14 
20 {
21  static $phonegapFiles = array();
22  static $appScripts = array();
23  static $supportScripts = array();
24 
25  function PhonegapManager()
26  {
27 
28  }
29 
30  static function setDefaults()
31  {
32  Settings::setDefaultValue("phonegap", "enable_phonegap", true, Boolean, "Enables support for Phonegap/Cordova applications");
33  Settings::setDefaultValue("phonegap", "enable_phonegap_debugging", false, Boolean, "Enables PhoneGap debugging mode - append phonegap=1 to any URL in your desktop browser to turn on PhoneGap mode");
34  Settings::setDefaultValue("phonegap", "phonegap_debug_version", "2.4.0", String, "The version of Phonegap that is assumed in debugging mode");
35  Settings::setDefaultValue("phonegap", "phonegap_debug_os", "android", String, "The OS of Phonegap that is assumed in debugging mode");
36  Settings::setDefaultValue("phonegap", "use_local_phonegap_files", false, Boolean, "Check this box to pull the local cordova files in the packaged app, instead of pulling them from the server (EXPERIMENTAL)");
37  }
38 
42  static function detectPhonegap()
43  {
44  global $script;
45 
46  trace("Detecting PhoneGap", 4);
47 
48  if ($_SESSION["phonegap"])
49  {
50  if ($_SESSION["phonegap_os"] != "" && $_SESSION["phonegap_version"] != "")
51  {
52  return;
53  }
54  else
55  {
56  trace("Phonegap flag without OS or version detected in session for {$_SERVER["REMOTE_ADDR"]} - resetting", 2);
57  $_SESSION["phonegap"] = false;
58  }
59  }
60 
61  $matches = array();
62  if (preg_match("/PhoneGap\\/Cordova (\\w+) (\\d+\\.\\d+\\.\\d+)/i", $_SERVER['HTTP_USER_AGENT'], $matches))
63  {
64  $_SESSION["phonegap"] = true;
65  $_SESSION["phonegap_os"] = $matches[1];
66  $_SESSION["phonegap_version"] = $matches[2];
67 
68  trace("PhoneGap {$_SESSION["phonegap_version"]} for {$_SESSION["phonegap_os"]} enabled for {$_SERVER["REMOTE_ADDR"]} via User Agent", 2);
69  }
70  else if ($_POST["phonegap"] && $_POST["phonegap_version"] != "" && $_POST["phonegap_os"] != "")
71  {
72  $_SESSION["phonegap"] = true;
73  $_SESSION["phonegap_version"] = $_POST["phonegap_version"];
74  $_SESSION["phonegap_os"] = $_POST["phonegap_os"];
75 
76  trace("PhoneGap {$_SESSION["phonegap_version"]} for {$_SESSION["phonegap_os"]} enabled for {$_SERVER["REMOTE_ADDR"]} via POST", 2);
77  }
78  else if (Settings::getValue("phonegap", "enable_phonegap_debugging") && $_GET["phonegap"])
79  {
80  $_SESSION["phonegap"] = true;
81  $_SESSION["phonegap_version"] = Settings::getValue("phonegap", "phonegap_debug_version");
82  $_SESSION["phonegap_os"] = Settings::getValue("phonegap", "phonegap_debug_os");
83 
84  trace("PhoneGap {$_SESSION["phonegap_version"]} for {$_SESSION["phonegap_os"]} enabled for {$_SERVER["REMOTE_ADDR"]} in debugging mode", 2);
85  }
86 
87  if ($_SESSION["phonegap_os"] == "droid") $_SESSION["phonegap_os"] = "android";
88  }
89 
90  static function hasPhonegap()
91  {
92  if (!Settings::getValue("phonegap", "enable_phonegap")) return false;
93  return (isset($_SESSION["phonegap"]) && $_SESSION["phonegap"] == true) || stripos($_SERVER['HTTP_USER_AGENT'], "PhoneGap/Cordova") !== false;
94  }
95 
96  static function showVersion()
97  {
98  if ($_SESSION["phonegap"])
99  {
100  echo "<p>Using Phonegap version ".$_SESSION["phonegap_version"]." on ".$_SESSION["phonegap_os"]."</p>";
101  }
102  else
103  {
104  echo "<p>Not using Phonegap</p>";
105  }
106  }
107 
112  static function getOS()
113  {
114  $os = $_SESSION["phonegap_os"];
115  if ($os == "droid") $os = "android";
116  return $os;
117  }
118 
119  static function addScript($script)
120  {
121  global $phonegap;
122  global $phonegap_csp;
123 
125  {
126  ComponentManager::fireEvent("RegisterPhonegapFiles");
127  ComponentManager::fireEvent("RegisterPhonegapAppScripts");
128  ComponentManager::fireEvent("RegisterPhonegapSupportScripts");
129 
130  $os = PhonegapManager::getOS();
131 
132  //AJG: Do not add phonegap script references if we do not definitively know the OS
133  if (!$os) return $script;
134 
135  trace(print_r(PhonegapManager::$appScripts, true), 3);
136 
137  $file = PhonegapManager::getPhonegapFile($os, $_SESSION["phonegap_version"]);
138  $app = PhonegapManager::getPhonegapAppScript($os, $_SESSION["phonegap_version"]);
139 
140  if ($app)
141  {
142  $phonegap = "<script type='text/javascript' src='{$app}'></script>\n".$phonegap;
143  }
144 
145  if (Settings::getValue("phonegap", "use_local_phonegap_files"))
146  {
147  $phonegap_csp = "gap: cdvfile:";
148  $phonegap = "<script type='text/javascript' src='cdvfile://localhost/bundle/www/cordova.js'></script>".$phonegap;
149  }
150  else
151  {
152  $phonegap_csp = "gap:";
153  $phonegap = "<script type='text/javascript' src='{$file}'></script>\n".$phonegap;
154  }
155  foreach(PhonegapManager::$supportScripts as $supportScript => $dummy)
156  {
157  $phonegap .= "<script type='text/javascript' src='{$supportScript}'></script>\n";
158  }
159  }
160  else
161  {
162  $phonegap = "";
163  }
164 
165  return $script;
166  }
167 
168  static function registerPhonegapFile($platform, $version, $file)
169  {
170  trace("Registering Phonegap file for $platform $version: $file", 3);
171 
172  PhonegapManager::$phonegapFiles["{$platform}-{$version}"] = $file;
173  }
174 
175  static function getPhonegapFile($platform, $version)
176  {
177  $key = "{$platform}-{$version}";
178  if (array_key_exists($key, PhonegapManager::$phonegapFiles))
179  {
180  // Send the registered configured file for the platform and version
181  return PhonegapManager::$phonegapFiles[$key];
182  }
183 
184  // Send default file with no plugins
185  return "/components/phonegap/js/{$platform}/cordova-{$version}.js";
186  }
187 
188  static function registerPhonegapAppScript($platform, $version, $file)
189  {
190  trace("Registering Phonegap App Script for $platform $version: $file", 3);
191  PhonegapManager::$appScripts["{$platform}-{$version}"] = $file;
192  }
193 
194  static function getPhonegapAppScript($platform, $version)
195  {
196  $key = "{$platform}-{$version}";
197  if (array_key_exists($key, PhonegapManager::$appScripts))
198  {
199  return PhonegapManager::$appScripts[$key];
200  }
201 
202  $key = "*-{$version}";
203  if (array_key_exists($key, PhonegapManager::$appScripts))
204  {
205  return PhonegapManager::$appScripts[$key];
206  }
207 
208  if (array_key_exists("*-*", PhonegapManager::$appScripts))
209  {
210  return PhonegapManager::$appScripts["*-*"];
211  }
212 
213  return "";
214  }
215 
216  static function registerPhonegapSupportScript($s)
217  {
219  }
220 
221  static function defaultSupportScripts()
222  {
223  PhonegapManager::registerPhonegapSupportScript("/components/phonegap/js/photo_uploader.js");
224  PhonegapManager::registerPhonegapSupportScript("/components/phonegap/js/photo_attachment_uploader.js");
225  }
226 }
227 ?>
$_POST["owner_id"]
Definition: blog_form.inc:54
$dummy
Definition: data_import.inc:7
$file
Definition: delete.inc:47
static fireEvent($event, $parameter=null, $mustBeConsumed=false)
Fire an event to all subscribers as detailed in their manifests.
Provides support for managing Phonegap apps.
static getOS()
Retrieve the current client device's OS (assuming it is running in Phonegap / Cordova)
static detectPhonegap()
Detects whether a phonegap / cordova app is being used to access the current page.
static addScript($script)
static registerPhonegapFile($platform, $version, $file)
static getPhonegapFile($platform, $version)
static defaultSupportScripts()
static registerPhonegapSupportScript($s)
static getPhonegapAppScript($platform, $version)
static registerPhonegapAppScript($platform, $version, $file)
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104
static setDefaultValue($component, $name, $value, $field_type="String", $annotation="", $category="", $options="", $weight=0)
Sets the default value of the given component setting.
Definition: settings.inc:174
global $phonegap
Provides a central management class for event handlers and common functionality for the phonegap comp...
$_SESSION["useMobile"]
Definition: override.inc:7