/gviewer.php

  1. <?php 
  2.  
  3. /** 
  4. Plugin Name: Google Doc Embedder 
  5. Plugin URI: http://www.davistribe.org/gde/ 
  6. Description: Lets you embed MS Office, PDF, TIFF, and many other file types in a web page using the Google Docs Viewer (no Flash or PDF browser plug-ins required). 
  7. Author: Kevin Davis 
  8. Author URI: http://www.davistribe.org/ 
  9. Text Domain: gde 
  10. Domain Path: /languages/ 
  11. Version: 2.5.7 
  12. License: GPLv2 
  13. */ 
  14.  
  15. /** 
  16. * LICENSE 
  17. * This file is part of Google Doc Embedder. 
  18. * 
  19. * Google Doc Embedder is free software; you can redistribute it and/or 
  20. * modify it under the terms of the GNU General Public License 
  21. * as published by the Free Software Foundation; either version 2 
  22. * of the License, or (at your option) any later version. 
  23. * 
  24. * This program is distributed in the hope that it will be useful,  
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  27. * GNU General Public License for more details. 
  28. * 
  29. * You should have received a copy of the GNU General Public License 
  30. * along with this program; if not, write to the Free Software 
  31. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 
  32. * 
  33. * @package google-document-embedder 
  34. * @author Kevin Davis <wpp@tnw.org> 
  35. * @copyright Copyright 2012 Kevin Davis 
  36. * @license http://www.gnu.org/licenses/gpl.txt GPL 2.0 
  37. * @link http://www.davistribe.org/gde/ 
  38. */ 
  39.  
  40. // boring init junk 
  41. $gde_ver = "2.5.7.98"; 
  42. $gde_db_ver = "1.2"; // update also in gde_activate() 
  43.  
  44. require_once( plugin_dir_path( __FILE__ ) . 'functions.php' ); 
  45. global $wp_version; 
  46.  
  47. $pdata = gde_get_plugin_data(); 
  48. $gdeoptions = get_option( 'gde_options' ); 
  49. $gdetypes = gde_supported_types();  
  50.  
  51. // check for db health 
  52. $healthy = gde_debug_tables(); 
  53.  
  54. // add admin functions only if needed 
  55. if ( is_admin() ) { require_once( GDE_PLUGIN_DIR . 'functions-admin.php' ); } 
  56.  
  57. // get global settings - not implemented in this release 
  58. /** 
  59. if ( is_multisite() ) { 
  60. $gdeglobals = get_site_option( 'gde_globals' ); 
  61. } 
  62. */ 
  63.  
  64. // activate plugin, allow clear dx log on deactivate 
  65. register_activation_hook( __FILE__, 'gde_activate' ); 
  66. register_deactivation_hook( __FILE__, 'gde_deactivate' ); 
  67.  
  68. // bring the magic 
  69. add_action( 'plugins_loaded', 'gde_load' ); 
  70. add_shortcode( 'gview', 'gde_do_shortcode' ); 
  71.  
  72. function gde_do_shortcode( $atts ) { 
  73. global $healthy, $gdeoptions; //$gdeglobals 
  74.  
  75. // check profile table health 
  76. if ( ! $healthy ) { 
  77. delete_option('gde_db_version'); 
  78. return gde_show_error( __('Unable to load profile settings', 'gde') ); 
  79.  
  80. // handle global setting overrides - not active in this release 
  81. /** 
  82. if ($gdeglobals['enforce_viewer'] == "std") { 
  83. $gdeoptions['disable_proxy'] = "yes"; 
  84. } 
  85. if ($gdeglobals['enforce_lang']) { 
  86. $gdeoptions['default_lang'] = $gdeglobals['enforce_lang']; 
  87. } 
  88. */ 
  89.  
  90. extract( shortcode_atts( array ( 
  91. 'file' => '',  
  92. 'profile' => 1, // default profile is always ID 1 
  93. 'save' => '',  
  94. 'width' => '',  
  95. 'height' => '',  
  96. 'cache' => '',  
  97. 'title' => '', // not yet implemented 
  98. 'page' => '',  
  99.  
  100. // backwards compatibility < gde 2.5 (still work but now "deprecated" and discouraged in the documentation) 
  101. 'authonly' => '',  
  102. 'lang' => '' 
  103. ), $atts ) ); 
  104.  
  105. // get requested profile data (or default if doesn't exist) 
  106. $term = $profile; 
  107. if ( is_numeric( $term ) ) { 
  108. // id-based lookup 
  109. if ( ! $profile = gde_get_profiles( $term ) ) { 
  110. gde_dx_log("Loading default profile instead"); 
  111. if ( ! $profile = gde_get_profiles( 1 ) ) { 
  112. $code = gde_show_error( __('Unable to load requested profile.', 'gde') ); 
  113. } else { 
  114. $pid = 1; 
  115. } else { 
  116. $pid = $term; 
  117. } else { 
  118. // name-based lookup 
  119. if ( ! $profile = gde_get_profiles( strtolower( $term ) ) ) { 
  120. gde_dx_log("Loading default profile instead"); 
  121. if ( ! $profile = gde_get_profiles( 1 ) ) { 
  122. $code = gde_show_error( __('Unable to load requested profile.', 'gde') ); 
  123. } else { 
  124. $pid = 1; 
  125. } else { 
  126. $pid = $profile['profile_id']; 
  127.  
  128. // use profile defaults if shortcode override not defined 
  129. if ( $save !== "0" ) { 
  130. if ( empty( $save ) ) { 
  131. $save = $profile['link_show']; 
  132. if ( empty( $width ) ) { 
  133. $width = $profile['default_width']; 
  134. if ( empty( $height ) ) { 
  135. $height = $profile['default_height']; 
  136. if ( $cache !== "0" ) { 
  137. if ( empty( $cache ) ) { 
  138. $cache = $profile['cache']; 
  139. if ( empty( $lang ) ) { 
  140. if ( $profile['language'] !== "en_US" ) { 
  141. $lang = $profile['language']; 
  142.  
  143. // tweak the dimensions if necessary 
  144. $width = gde_sanitize_dims( $width ); 
  145. $height = gde_sanitize_dims( $height ); 
  146.  
  147. // add base url if needed 
  148. if ( ! preg_match( "/^http/i", $file ) ) { 
  149. if ( substr( $file, 0, 2 ) == "//" ) { 
  150. // append dynamic protocol 
  151. if ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ) { 
  152. $file = "https:" . $file; 
  153. } else { 
  154. $file = "http:" . $file; 
  155. } elseif ( isset( $profile['base_url'] ) ) { 
  156. // not a full link, add base URL if available 
  157. if ( substr( $file, 0, 1 ) == "/" ) { 
  158. // remove any preceding slash from doc (base URL adds it) 
  159. $file = ltrim( $file, '/' ); 
  160. $file = $profile['base_url'] . $file; 
  161.  
  162. // capture file details 
  163. $fn = basename( $file ); 
  164. $fnp = gde_split_filename( $fn ); 
  165.  
  166. // file validation 
  167. if ( $gdeoptions['error_check'] == "no" ) { 
  168. $force = true; 
  169. } else { 
  170. $force = false; 
  171. $status = gde_validate_file( str_replace( " ", "%20", $file ), $force ); 
  172.  
  173. if ( ! isset( $code ) && ! is_array( $status ) && $status !== -1 ) { 
  174. // validation failed 
  175. $code = gde_show_error( $status ); 
  176. } elseif ( ! isset( $code ) ) { 
  177. // validation passed or was skipped 
  178.  
  179. // check for max filesize 
  180. $viewer = true; 
  181. if ( $gdeoptions['file_maxsize'] > 0 && isset( $status['fsize'] ) ) { 
  182. $maxbytes = (int) $gdeoptions['file_maxsize'] * 1024 * 1024; 
  183. if ( $status['fsize'] > $maxbytes ) { 
  184. $viewer = false; 
  185.  
  186. // generate links (embed, download) 
  187. $links = array( $file, $file ); 
  188. if ( $profile['link_block'] == "yes" && gde_is_blockable( $profile ) ) { 
  189. if ( $secure = gde_get_secure_url( $file ) ) { 
  190. $links[0] = $secure; 
  191. } else { 
  192. $links[0] = ''; 
  193. $links[1] = ''; 
  194. } elseif ( $profile['link_show'] !== "none" ) { 
  195. if ( $profile['link_force'] == "yes" && $profile['link_mask'] == "no" ) { 
  196. $links[1] = GDE_PLUGIN_URL . "load.php?d=" . urlencode( $links[1] ); 
  197. } elseif ( $profile['link_force'] == "no" && $profile['link_mask'] == "yes" ) { 
  198. $short = gde_get_short_url( $links[0] ); 
  199. $links[0] = $short; 
  200. $links[1] = $short; 
  201. } elseif ( $profile['link_force'] == "yes" && $profile['link_mask'] == "yes" ) { 
  202. $short = gde_get_short_url( GDE_PLUGIN_URL . "load.php?d=" . urlencode( $links[0] ) ); 
  203. $links[0] = $short; 
  204. $links[1] = $short; 
  205.  
  206. // obfuscate filename if cache disabled (globally or via shortcode) 
  207. // note that this is ignored if the document is secure to prevent each hit from generating a new db row 
  208. if ( ! empty( $links[1] ) && ( $cache == "off" || $cache == "0" ) ) { 
  209. $links[0] .= "?" . time(); 
  210.  
  211. // check for failed secure doc 
  212. if ( empty( $links[0] ) && empty( $links[1] ) ) { 
  213. $code = gde_show_error( __('Unable to secure document', 'gde') ); 
  214. } else { 
  215.  
  216. // which viewer? 
  217. if ( $profile['viewer'] == "enhanced" ) { 
  218. $lnk = GDE_PLUGIN_URL . "view.php?url=" . urlencode( $links[0] ) . "&hl=" . $lang . "&gpid=" . $pid; 
  219. } else { 
  220. $lnk = "http://docs.google.com/viewer?url=" . urlencode( $links[0] ) . "&hl=" . $lang; 
  221.  
  222. // what mode? 
  223. if ( $profile['tb_mobile'] == "always" ) { 
  224. $lnk .= "&mobile=true"; 
  225. } else { 
  226. $lnk .= "&embedded=true"; 
  227.  
  228. // build viewer 
  229. if ( $viewer == false ) { 
  230. // exceeds max filesize 
  231. $vwr = ''; 
  232. } else { 
  233. $vwr = '<iframe src="%U%" class="gde-frame" style="width:%W%; height:%H%; border: none;"%ATTRS%></iframe>'; 
  234. $vwr = str_replace("%U%", $lnk, $vwr); 
  235. $vwr = str_replace("%W%", $width, $vwr); 
  236. $vwr = str_replace("%H%", $height, $vwr); 
  237.  
  238. // frame attributes 
  239. $vattr[] = ' scrolling="no"'; // iphone scrolling bug 
  240. if ( ! empty( $page ) && is_numeric( $page ) ) { // selected starting page 
  241. $page = (int) $page - 1; 
  242. $vattr[] = ' onload="javascript:this.contentWindow.location.hash=\':0.page.' . $page . '\';"'; 
  243. $vwr = str_replace( "%ATTRS%", implode( '', $vattr ), $vwr ); 
  244.  
  245. // show download link? 
  246. $allow_save = false; 
  247. if ( ! empty( $links[1] ) ) { // link empty = secure document; ignore any other save attribute 
  248. if ( $save == "all" || $save == "1" ) { 
  249. $allow_save = true; 
  250. } elseif ( ( $save == "users" || $authonly == "1" ) && is_user_logged_in() ) { 
  251. $allow_save = true; 
  252.  
  253. if ( $allow_save ) { 
  254. // build download link 
  255. $linkcode = '<p class="gde-text"><a href="%LINK%" class="gde-link"%ATTRS%>%TXT%</a></p>'; 
  256. $linkcode = str_replace( "%LINK%", $links[1], $linkcode ); 
  257.  
  258. // fix type 
  259. $ftype = strtoupper( $fnp[1] ); 
  260. if ( $ftype == "TIF" ) {  
  261. $ftype = "TIFF"; 
  262.  
  263. // link attributes 
  264. if ( $profile['link_mask'] == "yes" ) { 
  265. $attr[] = ' rel="nofollow"'; 
  266. $attr[] = gde_ga_event( $file ); // GA integration 
  267. $linkcode = str_replace("%ATTRS%", implode( '', $attr ), $linkcode); 
  268.  
  269. // link text 
  270. if ( empty( $profile['link_text'] ) ) { 
  271. $profile['link_text'] = __('Download', 'gde'); 
  272. $dltext = str_replace( "%TITLE", $title, $profile['link_text'] ); 
  273. $dltext = str_replace( "%FILE", $fn, $dltext ); 
  274. $dltext = str_replace( "%TYPE", $ftype, $dltext ); 
  275. $dltext = str_replace( "%SIZE", gde_format_bytes( $status['fsize'] ), $dltext ); 
  276.  
  277. $linkcode = str_replace( "%TXT%", $dltext, $linkcode ); 
  278. } else { 
  279. $linkcode = ''; 
  280.  
  281. // link position 
  282. if ( $profile['link_pos'] == "above" ) { 
  283. $code = $linkcode . "\n" . $vwr; 
  284. } else { 
  285. $code = $vwr . "\n" . $linkcode; 
  286.  
  287. return $code; 
  288.  
  289. if ( is_admin() ) { 
  290. // add quick settings link to plugin list 
  291. add_filter( "plugin_action_links_" . plugin_basename( __FILE__ ), 'gde_actlinks' ); 
  292.  
  293. // beta notification (if enabled) 
  294. if ( gde_check_for_beta( __FILE__ ) ) { 
  295. // override plugin update text 
  296. add_action( 'admin_enqueue_scripts', 'gde_admin_beta_js_update' ); 
  297. } else { 
  298. // no update available, but notify if currently using a beta 
  299. add_action( 'after_plugin_row', 'gde_warn_on_plugin_page' ); 
  300.  
  301. // editor integration 
  302. if ( ! isset( $gdeoptions['ed_disable'] ) || $gdeoptions['ed_disable'] == "no" ) { 
  303. // add quicktag 
  304. add_action( 'admin_print_scripts', 'gde_admin_print_scripts' ); 
  305.  
  306. // add tinymce button 
  307. add_action( 'admin_init', 'gde_mce_addbuttons' ); 
  308.  
  309. // extend media upload support to natively unsupported mime types 
  310. if ( $gdeoptions['ed_extend_upload'] == "yes" ) { 
  311. add_filter( 'upload_mimes', 'gde_upload_mimes' ); 
  312.  
  313. if ( version_compare( $wp_version, "3.5", "<" ) ) { 
  314. // embed shortcode instead of link from media library for supported types 
  315. add_filter( 'attachment_fields_to_edit', 'gde_attachment_fields_to_edit', null, 2 ); 
  316. add_filter( 'media_send_to_editor', 'gde_media_insert', 20, 3 ); 
  317. } else { 
  318. //add_filter( 'attachment_fields_to_edit', 'gde_attachment_fields_to_edit_35', null, 2 ); 
  319. add_filter( 'media_send_to_editor', 'gde_media_insert_35', 20, 3 ); 
  320.  
  321. // add local settings page 
  322. add_action( 'admin_menu', 'gde_option_page' ); 
  323.  
  324. //if ( is_multisite() ) { 
  325. // add global settings page 
  326. //add_action( 'network_admin_menu', 'gde_site_option_page' ); // not present in this release 
  327. //} 
  328.  
  329. /** 
  330. * Activate the plugin 
  331. * 
  332. * @since 0.2 
  333. * @return void 
  334. * @note This function must remain in this file 
  335. */ 
  336. function gde_activate( $network_wide ) { 
  337. // check for sufficient php version (minimum supports json_encode) 
  338. if ( ! ( phpversion() >= '5.2.0' ) ) { 
  339. wp_die( 'Your server is running PHP version ' . phpversion() . ' but this plugin requires at least 5.2.0' ); 
  340.  
  341. // set db schema version for this release - global not available here 
  342. $gde_db_ver = "1.2"; 
  343.  
  344. // check for network-wide activation (currently not supported) 
  345. if ( $network_wide ) { 
  346. wp_die("Network activation is not supported at this time. Please activate individually until an update is available."); 
  347.  
  348. require_once( plugin_dir_path( __FILE__ ) . 'libs/lib-setup.php' ); 
  349.  
  350. // create/update profile db, if necessary 
  351. if ( gde_db_tables( $gde_db_ver ) ) { 
  352. gde_setup(); 
  353. } else { 
  354. gde_dx_log("Table creation failed; setup halted"); 
  355. wp_die( __("Setup wasn't able to create the required database tables.", 'gde') ); 
  356.  
  357. /** 
  358. * Remove dx log on deactivation 
  359. * 
  360. * @since 2.5.2.1 
  361. * @return void 
  362. */ 
  363. function gde_deactivate() { 
  364. global $wpdb; 
  365.  
  366. $table = $wpdb->base_prefix . 'gde_dx_log'; 
  367. if ( is_multisite() ) { 
  368. $blogid = get_current_blog_id(); 
  369. $wpdb->query("DELETE FROM $table WHERE blogid = '$blogid'"); 
  370. } else { 
  371. $wpdb->query("DROP TABLE IF EXISTS $table"); 
  372.  
  373. /** 
  374. * Actions to perform when plugins have finished loading (before init) 
  375. * 
  376. * @since 2.5.2.1 
  377. * @return void 
  378. */ 
  379. function gde_load() { 
  380. // localization 
  381. load_plugin_textdomain( 'gde', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); 
  382.  
  383. ?> 
.