-
Notifications
You must be signed in to change notification settings - Fork 0
/
weever.php
477 lines (375 loc) · 16.8 KB
/
weever.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
/*
Plugin Name: Weever Apps - Mobile Web Apps
Plugin URI: http://weeverapps.com/
Description: Weever Apps: Turn your site into a true HTML5 'web app' for iPhone, Android and Blackberry
Version: 1.5.3
Author: Brian Hogg
Author URI: http://brianhogg.com/
License: GPL3
*/
/* Copyright 2011 Weever Apps Inc. (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
define( 'WEEVER_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'WEEVER_ADMIN_TEMPLATE_DIR', plugins_url( 'templates/admin/', __FILE__ ) );
// Weever debug flag
if ( ! defined( 'WEEVER_DEV' ) )
define( 'WEEVER_DEV', false );
if ( ! function_exists( 'add_action' ) ) {
echo "Plugin file cannot be called directly.";
exit;
}
// TODO: Check for earliest WP version tested and functional
// Weever constants
require_once dirname( __FILE__ ) . '/classes/class-weever-const.php';
// R3S classes for app content output
require_once dirname( __FILE__ ) . '/classes/class-r3s.php';
// SimpleDOM HTML parser
require_once dirname( __FILE__ ) . '/classes/class-simpledom.php';
// Mobile detection class
require_once dirname( __FILE__ ) . '/classes/class-weever-mdetect.php';
// Weever plugin helper functions
require_once dirname( __FILE__ ) . '/classes/class-weever-helper.php';
// Weever App state object classes
require_once dirname( __FILE__ ) . '/classes/class-weever-app.php';
require_once dirname( __FILE__ ) . '/classes/class-weever-app-tab.php';
require_once dirname( __FILE__ ) . '/classes/class-weever-app-subtab.php';
require_once dirname( __FILE__ ) . '/classes/class-weever-app-theme-styles.php';
require_once dirname( __FILE__ ) . '/classes/class-weever-app-theme-launch.php';
if ( is_admin() ) {
require_once dirname( __FILE__ ) . '/admin.php';
require_once dirname( __FILE__ ) . '/classes/class-weever-controller.php';
// Register the ajax calls
add_action( 'wp_ajax_ajaxSubtabDelete', array( 'WeeverController', 'ajaxSubtabDelete' ) );
add_action( 'wp_ajax_ajaxSaveTabName', array( 'WeeverController', 'ajaxSaveTabName' ) );
add_action( 'wp_ajax_ajaxSaveTabIcon', array( 'WeeverController', 'ajaxSaveTabIcon' ) );
add_action( 'wp_ajax_ajaxTabPublish', array( 'WeeverController', 'ajaxTabPublish' ) );
add_action( 'wp_ajax_ajaxPublishSelected', array( 'WeeverController', 'ajaxPublishSelected' ) );
add_action( 'wp_ajax_ajaxUnpublishSelected', array( 'WeeverController', 'ajaxUnpublishSelected' ) );
add_action( 'wp_ajax_ajaxDeleteSelected', array( 'WeeverController', 'ajaxDeleteSelected' ) );
add_action( 'wp_ajax_ajaxSaveSubtabOrder', array( 'WeeverController', 'ajaxSaveSubtabOrder' ) );
add_action( 'wp_ajax_ajaxSaveTabOrder', array( 'WeeverController', 'ajaxSaveTabOrder' ) );
add_action( 'wp_ajax_ajaxSaveNewTab', array( 'WeeverController', 'ajaxSaveNewTab' ) );
add_action( 'wp_ajax_ajaxToggleAppStatus', array( 'WeeverController', 'ajaxToggleAppStatus' ) );
add_action( 'wp_ajax_ajaxUpdateTabSettings', array( 'WeeverController', 'ajaxUpdateTabSettings' ) );
}
function weever_update() {
$db_version = get_option( 'weever_db_version', '' );
if ( WeeverConst::VERSION != $db_version ) {
// Call the upgrade function to make sure this key is up to date
$weeverapp = new WeeverApp( false );
if ( $weeverapp->site_key )
$weeverapp->upgrade_site_key();
update_option( 'weever_db_version', WeeverConst::VERSION );
}
}
add_action('init', 'weever_update', 1);
/**
* Function to load and show bar along the bottom if we're viewing in a mobile browser
*/
/*
function weever_desktop_init() {
wp_register_script( 'weever-desktop', plugins_url( 'static/js/weever-desktop.js' ), array( 'jquery' ), WeeverConst::VERSION, true );
}
add_action('init', 'weever_desktop_init');
*/
function weever_get_redirect_url( $weeverapp = false ) {
if ( $weeverapp === false )
$weeverapp = new WeeverApp( false );
// Pass through the app url
$request_uri = $_SERVER['REQUEST_URI'];
$request_uri = str_replace( "?full=0", "", $request_uri );
$request_uri = str_replace( "&full=0", "", $request_uri );
if ( $request_uri && $request_uri != 'index.php' && $request_uri != '/' )
$exturl = '?exturl=' . $request_uri;
else
$exturl = "";
// Redirect either to the app page or their own domain
// TODO: Check the tier is 1 also?
if ( $weeverapp->domain ) {
$url = 'http://' . $weeverapp->domain . $exturl;
} else {
$url = 'http://weeverapp.com/app/' . $weeverapp->primary_domain . $exturl;
}
return $url;
}
// http://www.webcheatsheet.com/PHP/get_current_page_url.php
function weever_get_current_url() {
$page_url = 'http';
if ( isset( $_SERVER['HTTPS'] ) and 'on' == $_SERVER['HTTPS'] )
$page_url .= "s";
$page_url .= "://";
if ( isset( $_SERVER['SERVER_PORT'] ) and '80' != $_SERVER['SERVER_PORT'] ) {
$page_url .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
} else {
$page_url .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
}
return $page_url;
}
function weever_desktop_print_scripts() {
$weeverapp = new WeeverApp( false );
wp_register_script( 'weever-desktop', WEEVER_PLUGIN_URL . 'static/js/weever-desktop.js', array( 'jquery' ), WeeverConst::VERSION, true );
wp_enqueue_script('weever-desktop');
$url = weever_get_current_url();
// Replace full param
$params = array();
$query = parse_url( $url, PHP_URL_QUERY );
parse_str( $query, $params );
if ( isset( $params['full'] ) )
unset( $params['full'] );
$params['full'] = 0;
$url = preg_replace( '/\?.*/', '', $url ) . '?' . http_build_query( $params );
wp_localize_script('weever-desktop', 'WDesktop',
array(
'url' => $url,
)
);
}
function weever_init() {
// Initialize the session
if ( ! session_id() && !is_admin() )
session_start();
// Load i18n
load_plugin_textdomain( 'weever', false, basename(dirname(__FILE__)) . '/languages/' );
// Check if a feed, R3S encoded template, or the admin site is being accessed
$template = get_query_var( 'template' );
$callback = get_query_var( 'callback' );
if ( is_feed() || is_admin() || ! empty( $template ) || ! empty( $callback ) ) {
return;
}
// Verify the app is enabled
$weeverapp = new WeeverApp( false );
if ( $weeverapp->site_key && $weeverapp->app_enabled && $weeverapp->primary_domain )
{
// Handle the full param and skipping mobile detection
$full = get_query_var( 'full' );
if ( $full != '' ) {
if ( $full == '0' and isset( $_SESSION['ignore_mobile'] ) )
unset( $_SESSION['ignore_mobile'] );
if ( $full == '1' )
$_SESSION['ignore_mobile'] = '1';
}
// Run the mobile checks
$uagent_obj = new WeeverMdetect();
if ( ! $uagent_obj->DetectWebkit() ) {
$_SESSION['ignore_mobile'] = '1';
return;
}
$weever_app_redirect = false;
if ( $weeverapp->granular ) {
$devices = $weeverapp->get_granular_device_option_names();
} else {
$devices = $weeverapp->get_standard_device_option_names();
}
foreach ( $devices as $device ) {
if ( $weeverapp->$device ) {
if ( $uagent_obj->$device() ) {
$weever_app_redirect = true;
break;
}
}
}
// Show bar along the bottom if mobile device but we're not redirecting
if ( $weever_app_redirect === true and isset( $_SESSION['ignore_mobile'] ) and $_SESSION['ignore_mobile'] == '1' ) {
add_action( 'wp_print_scripts', 'weever_desktop_print_scripts' );
}
if ( isset( $_SESSION['ignore_mobile'] ) and $_SESSION['ignore_mobile'] == '1' )
return;
if ( $weever_app_redirect === false ) {
$_SESSION['ignore_mobile'] = '1';
return;
}
// Finally, redirect
$url = weever_get_redirect_url( $weeverapp );
if ( ! headers_sent( $filename, $linenum ) ) {
header( 'Location: ' . $url );
} else {
echo "<!-- Headers sent by $filename (line $linenum) --> ";
die('<a href="'.$url.'">View our mobile web app - click here</a>');
}
die();
}
}
add_action( 'template_redirect', 'weever_init', 0 );
/**
* Add a link to the settings page from the plugins listing page
*
* @param array $links
*/
function weever_settings_link( $links ) {
if ( function_exists( "admin_url" ) ) {
$settings_link = '<a href="' . admin_url( 'admin.php?page=weever-account' ) . '">' . __( 'Settings' ) . '</a>';
array_push( $links, $settings_link );
}
return $links;
}
add_filter( 'plugin_action_links_weever/weever.php', 'weever_settings_link' );
/*
* Custom R3S feed for content distribution
*/
function weever_create_r3sfeed() {
status_header(200);
if ( file_exists( get_stylesheet_directory() . '/feed-r3s.php' ) ) {
load_template( get_stylesheet_directory() . '/feed-r3s.php' );
} elseif ( file_exists( get_template_directory() . '/feed-r3s.php' ) ) {
load_template( get_template_directory() . '/feed-r3s.php' );
} else {
load_template( dirname( __FILE__ ) . '/templates/feed-r3s.php' );
}
}
add_action( 'do_feed_r3s', 'weever_create_r3sfeed', 10, 1 );
function weever_no_limits_for_feed( $val ) {
global $wp_query;
if ( isset( $wp_query->query_vars['feed'] ) and ( $wp_query->query_vars['feed'] == 'r3s' ) )
{
// Default values
$limit = ( is_numeric( get_query_var( 'limit' ) ) and get_query_var( 'limit' ) > 0 ) ? get_query_var( 'limit' ) : 15;
$page = ( is_numeric( get_query_var( 'page' ) ) and get_query_var( 'page' ) > 0 ) ? get_query_var( 'page' ) : 1;
$offset = ( is_numeric( get_query_var( 'start' ) ) and get_query_var( 'start' ) > 0 ) ? get_query_var( 'start' ) : ( ( $page - 1 ) * $limit );
$val = 'LIMIT ' . $offset . ', ' . $limit;
return $val;
}
else
return $val;
}
add_filter( 'post_limits', 'weever_no_limits_for_feed' );
/**
* Disable the feed cache if we're in development mode
*/
function weever_disable_feed_cache(&$feed) {
$feed->enable_cache(false);
}
if ( WEEVER_DEV ) {
add_action( 'wp_feed_options', 'weever_disable_feed_cache' );
}
/**
* Handling the sending of individual pieces of content to the Weever app
*/
function weever_app_request() {
global $wp_query;
if ( array_key_exists( 'template', $wp_query->query_vars ) )
{
switch ( $wp_query->query_vars['template'] ) {
case 'weever_cartographer':
// Capture the HTML from the template file
ob_start();
the_post();
header('Content-type: application/json');
header('Cache-Control: no-cache, must-revalidate');
$callback = get_query_var('callback');
// specs @ https://github.com/WeeverApps/r3s-spec
$jsonHtml = new R3SHtmlContentDetailsMap;
$jsonHtml->language = get_locale();
// TODO: Get the sitename from the current site state
$jsonHtml->publisher = get_option('blogname'); // $conf->getValue('config.sitename');
$jsonHtml->name = get_the_title();
$jsonHtml->author = get_the_author_meta('display_name');
$jsonHtml->datetime["published"] = get_lastpostdate('GMT'); //mysql2date('Y-m-d H:i:s', get_lastpostdate('GMT'), false); //$v->created;
$jsonHtml->datetime["modified"] = get_lastpostmodified('GMT'); //mysql2date('Y-m-d H:i:s', get_lastpostmodified('GMT'), false); //$v->modified;
// Look for post type before more generic stylesheet
$post = get_post( get_the_ID() );
$template_suffixes = array( '-' . $post->post_type, '' );
foreach ( $template_suffixes as $suffix ) {
if ( file_exists( get_stylesheet_directory() . '/weever-content-single' . $suffix . '.php' ) ) {
include( get_stylesheet_directory() . '/weever-content-single' . $suffix . '.php' );
break;
} elseif ( file_exists( get_template_directory() . '/weever-content-single' . $suffix . '.php' ) ) {
include( get_template_directory() . '/weever-content-single' . $suffix . '.php' );
break;
} elseif ( file_exists( dirname( __FILE__ ) . '/templates/weever-content-single' . $suffix . '.php' ) ) {
include( dirname( __FILE__ ) . '/templates/weever-content-single' . $suffix . '.php' );
break;
}
}
$jsonHtml->html = ob_get_clean();
$jsonHtml->image = null;
$html = WeeverSimpleHTMLDomHelper::str_get_html( $jsonHtml->html );
foreach ( @$html->find('img') as $vv )
{
if ( $vv->src )
{
$jsonHtml->image = WeeverHelper::make_absolute($vv->src, site_url());
break;
}
}
if ( ! $jsonHtml->image )
$jsonHtml->image = "";
// Mask external links so we leave only internal ones to play with.
$jsonHtml->html = str_replace( "href=\"http://", "hrefmask=\"weever://", $jsonHtml->html );
$jsonHtml->html = str_replace( "href='http://", "hrefmask='weever://", $jsonHtml->html );
// For HTML5 compliance, we take out spare target="_blank" links just so we don't duplicate
$jsonHtml->html = str_replace( "target=\"_blank\"", "", $jsonHtml->html );
$jsonHtml->html = str_replace( "target='_blank'", "", $jsonHtml->html );
//$jsonHtml->html = str_replace( "href=\"", "target=\"_blank\" href=\"", $jsonHtml->html );
//$jsonHtml->html = str_replace("src=\"/", "src=\"".get_site_url()."/", $jsonHtml->html);
//$jsonHtml->html = str_replace("src=\"images", "src=\"".get_site_url()."/images", $jsonHtml->html);
// Change all links to absolute vs. relative
// http://wintermute.com.au/bits/2005-09/php-relative-absolute-links/
$jsonHtml->html = preg_replace( '#(href|src)="([^:"]*)("|(?:(?:%20|\s|\+)[^"]*"))#', '$1="' . get_site_url() . '$2$3', $jsonHtml->html );
$jsonHtml->html = preg_replace( '#(href|src)=\'([^:\']*)(\'|(?:(?:%20|\s|\+)[^\']*\'))#', '$1=\'' . get_site_url() . '$2$3', $jsonHtml->html );
// Restore external links, ensure target="_blank" applies
$jsonHtml->html = str_replace( "hrefmask=\"weever://", "target=\"_blank\" href=\"http://", $jsonHtml->html);
$jsonHtml->html = str_replace( "hrefmask='weever://", "target=\"_blank\" href='http://", $jsonHtml->html);
$jsonHtml->html = str_replace( "<iframe title=\"YouTube video player\" width=\"480\" height=\"390\"",
"<iframe title=\"YouTube video player\" width=\"160\" height=\"130\"", $jsonHtml->html );
// Add full=1 to the end of all links
// With query param
$jsonHtml->html = preg_replace( '`(href)="http([^?"]*)\?([^?"#]*)(#)?([^?"#]*)"`i', '$1="http$2?$3&full=1$4$5"', $jsonHtml->html );
$jsonHtml->html = preg_replace( '`(href)=\'http([^?\']*)\?([^?\']*)(#)?([^?\'#]*)\'`i', '$1=\'http$2?$3&full=1$4$5\'', $jsonHtml->html );
// Without query param
$jsonHtml->html = preg_replace( '`(href)="http([^?"#]*)(#)?([^?"#]*)"`i', '$1="http$2?full=1$3$4"', $jsonHtml->html );
$jsonHtml->html = preg_replace( '`(href)=\'http([^?\']*)(#)?([^?\'#]*)\'`i', '$1=\'http$2?full=1$3$4\'', $jsonHtml->html );
//$jsonHtml->html = preg_replace( '#(href)=("|\')http(.*)(?)([^("|\')]*)\2#', '$1=$2http$3$4$5&full=1$2', $jsonHtml->html );
//$jsonHtml->html = preg_replace( '#(href)=("|\')http([^\2]+)(\2)#', '$1=$2http$3?full=1$2', $jsonHtml->html );
$jsonOutput = new jsonOutput;
$jsonOutput->results[] = $jsonHtml;
$output = json_encode( $jsonOutput );
if ( $callback )
$json = $callback . '(' . $output . ')';
else
$json = $output;
status_header( 200 );
print_r( $json );
exit;
case 'weever_css':
header('Content-type: text/css');
header('Cache-Control: no-cache, must-revalidate');
if ( file_exists( get_stylesheet_directory() . '/weever.css' ) )
include( get_stylesheet_directory() . '/weever.css' );
elseif ( file_exists( get_template_directory() . '/weever.css' ) )
include( get_template_directory() . '/weever.css' );
exit;
case 'weever_version':
die( var_dump( WeeverConst::VERSION ) );
}
}
}
add_action('template_redirect', 'weever_app_request');
/**
* Additional query variables needed by Weever Apps
*/
function weever_query_vars($vars) {
$vars[] = 'template';
// For pagination in the r3s feed
$vars[] = 'limit';
$vars[] = 'start';
$vars[] = 'page';
// For including a callback function for R3S feed/document
$vars[] = 'callback';
$vars[] = 'full';
// Distance
$vars[] = 'latitude';
$vars[] = 'longitude';
return $vars;
}
add_filter('query_vars', 'weever_query_vars');