PageRenderTime 37ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/google-document-embedder/gviewer.php

https://gitlab.com/endomorphosis/jeffersonsmithmayor
PHP | 399 lines | 273 code | 50 blank | 76 comment | 87 complexity | 469c002821273a7abb51b23df4226da6 MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: Google Doc Embedder
  4. Plugin URI: http://www.davistribe.org/gde/
  5. 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).
  6. Author: Kevin Davis
  7. Author URI: http://www.davistribe.org/
  8. Text Domain: gde
  9. Domain Path: /languages/
  10. Version: 2.4.6
  11. License: GPLv2
  12. */
  13. $gde_ver = "2.4.6.98";
  14. /**
  15. * LICENSE
  16. * This file is part of Google Doc Embedder.
  17. *
  18. * Google Doc Embedder is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License
  20. * as published by the Free Software Foundation; either version 2
  21. * of the License, or (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  31. *
  32. * @package google-document-embedder
  33. * @author Kevin Davis <wpp@tnw.org>
  34. * @copyright Copyright 2012 Kevin Davis
  35. * @license http://www.gnu.org/licenses/gpl.txt GPL 2.0
  36. * @link http://www.davistribe.org/gde/
  37. */
  38. if ( ! defined( 'GDE_PLUGIN_DIR' ) )
  39. define( 'GDE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
  40. if ( ! defined( 'GDE_PLUGIN_URL' ) )
  41. define( 'GDE_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
  42. include_once('gde-functions.php');
  43. $gdeoptions = get_option('gde_options');
  44. $pUrl = plugins_url(plugin_basename(dirname(__FILE__)));
  45. $support_link = '<a href="options-general.php?page=gviewer.php&debug=1">'.__('Support', 'gde').'</a>';
  46. // note: updates here should also be reflected in js/dialog.js
  47. $supported_exts = array(
  48. // ext => mime_type
  49. "ai" => "application/postscript",
  50. "doc" => "application/msword",
  51. "docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml",
  52. "dxf" => "application/dxf",
  53. "eps" => "application/postscript",
  54. "otf" => "font/opentype",
  55. "pages" => "application/x-iwork-pages-sffpages",
  56. "pdf" => "application/pdf",
  57. "pps" => "application/vnd.ms-powerpoint",
  58. "ppt" => "application/vnd.ms-powerpoint",
  59. "pptx" => "application/vnd.openxmlformats-officedocument.presentationml",
  60. "ps" => "application/postscript",
  61. "psd" => "image/photoshop",
  62. "rar" => "application/rar",
  63. "svg" => "image/svg+xml",
  64. "tif" => "image/tiff",
  65. "tiff" => "image/tiff",
  66. "ttf" => "application/x-font-ttf",
  67. "xls" => "application/vnd.ms-excel",
  68. "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml",
  69. "xps" => "application/vnd.ms-xpsdocument",
  70. "zip" => "application/zip"
  71. );
  72. $allowed_exts = implode("|",array_keys($supported_exts));
  73. // basic usage: [gview file="http://path.to/file.pdf"]
  74. function gde_gviewer_func($atts) {
  75. // current settings
  76. global $gdeoptions, $pUrl, $user_ID;
  77. extract(shortcode_atts(array(
  78. 'file' => '',
  79. //'display' => $gdeoptions['default_display'],
  80. 'save' => $gdeoptions['show_dl'],
  81. 'width' => '',
  82. 'height' => '',
  83. 'lang' => $gdeoptions['default_lang'],
  84. 'force' => $gdeoptions['bypass_check'],
  85. 'cache' => $gdeoptions['disable_caching'],
  86. 'authonly' => $gdeoptions['restrict_dl'],
  87. 'theme' => ''
  88. ), $atts));
  89. // add base url if needed
  90. if (!preg_match("/^http/i",$file) && $gdeoptions['base_url']) {
  91. // deal with potential slash issues
  92. if (!preg_match("/\/$/", $gdeoptions['base_url'])) {
  93. $gdeoptions['base_url'] = $gdeoptions['base_url']."/";
  94. }
  95. if (preg_match("/^\//", $file)) {
  96. $file = $file = substr($file, 1);
  97. }
  98. $file = $gdeoptions['base_url'].$file;
  99. }
  100. // set or clean up dimension values
  101. $width = str_replace("px", "", trim($width));
  102. if (!$width || !preg_match("/^\d+%?$/", $width)) {
  103. $width = $gdeoptions['default_width'];
  104. if ($gdeoptions['width_type'] == "pc") {
  105. $width .= "%";
  106. }
  107. }
  108. if (!strpos($width, "%")) {
  109. $width .= "px";
  110. }
  111. $height = str_replace("px", "", trim($height));
  112. if (!$height || !preg_match("/^\d+%?$/", $height)) {
  113. $height = $gdeoptions['default_height'];
  114. if ($gdeoptions['height_type'] == "pc") {
  115. $height .= "%";
  116. }
  117. }
  118. if (!strpos($height, "%")) {
  119. $height .= "px";
  120. }
  121. // translate filenames with spaces (prevent break in file size check, URL check, and mask url option)
  122. $fn = basename($file);
  123. $fnp = gde_splitFilename($fn);
  124. $file = str_replace(" ", "%20", $file); // urlencode and rawurlencode don't reliably work here
  125. // check link for validity
  126. $status = gde_validTests($file, $force);
  127. if ($status && !is_array($status)) {
  128. if (($gdeoptions['disable_hideerrors'] == "no") || !$gdeoptions['disable_hideerrors']) {
  129. $code = "\n<!-- GDE EMBED ERROR: $status -->\n";
  130. } else {
  131. $code = "\n".'<div class="gde-error">Google Doc Embedder '.__('Error', 'gde').": ".$status."</div>\n";
  132. }
  133. } else {
  134. $fsize = gde_formatBytes($status['fsize']);
  135. $code =<<<HERE
  136. %A%
  137. <iframe src="%U%" class="gde-frame" style="width:%W%; height:%H%; border: none;" scrolling="no"></iframe>\n
  138. %B%
  139. HERE;
  140. // obfuscate filename if cache disabled (globally or via shortcode)
  141. if ($gdeoptions['disable_caching'] == "yes" || $cache == "0") {
  142. $uefile = urlencode($file."?".time());
  143. } else {
  144. $uefile = urlencode($file);
  145. }
  146. // check for proxy
  147. if ($gdeoptions['disable_proxy'] == "no") {
  148. $lnk = $pUrl."/proxy.php?url=".$uefile."&hl=".$lang;
  149. } else {
  150. $lnk = "http://docs.google.com/viewer?url=".$uefile."&hl=".$lang;
  151. }
  152. // check for mobile
  153. if (strstr($gdeoptions['restrict_tb'], 'm') !== false) {
  154. $lnk .= "&mobile=true";
  155. } else {
  156. $lnk .= "&embedded=true";
  157. }
  158. // check for theme
  159. if ($theme) {
  160. $lnk .= "&t=$theme";
  161. }
  162. // check for page
  163. /*
  164. if (is_numeric($page)) {
  165. // jump to selected page - experimental (works on refresh but not initial page load)
  166. $page = (int) $page-1;
  167. $lnk = $lnk."#:0.page.".$page;
  168. }
  169. */
  170. // hide download link for anonymous users
  171. get_currentuserinfo();
  172. $dlRestrict = $gdeoptions['restrict_dl'];
  173. if ($user_ID == '') {
  174. if ($dlRestrict == "yes" || $authonly == "yes" || $authonly == "1") {
  175. // no user logged in and restrict set; override link setting
  176. $save = "no";
  177. }
  178. }
  179. $linkcode = "";
  180. if ($save == "yes" || $save == "1") {
  181. $dlMethod = $gdeoptions['link_func'];
  182. if ($fnp[1] == "PDF" || $fnp[1] == "pdf") {
  183. if ($dlMethod == "force" or $dlMethod == "force-mask") {
  184. // fix file name with spaces (again)
  185. $fn = str_replace(" ", "%20", $fn);
  186. $dlFile = $pUrl . "/libs/pdf.php?file=".urlencode($file)."&fn=".$fn;
  187. if ($dlMethod == "force-mask") {
  188. $dlFile = gde_shortUrl($dlFile);
  189. $target = "_blank";
  190. $nofollow = ' rel="nofollow"';
  191. } else {
  192. $target = "_self";
  193. $nofollow = "";
  194. }
  195. $gaTag = 'onclick="var that=this;_gaq.push([\'_trackEvent,\'Download\',\'PDF\',this.href]);setTimeout(function(){location.href=that.href;},200);return false;"';
  196. } elseif ($dlMethod == "default") {
  197. $dlFile = $file;
  198. $target = "_blank";
  199. $gaTag = 'onclick="_gaq.push([\'_trackEvent\',\'Download\',\'PDF\',this.href]);"';
  200. }
  201. } elseif ($dlMethod == "force-mask") {
  202. $dlFile = gde_shortUrl($file);
  203. $nofollow = ' rel="nofollow"';
  204. $target = "_self";
  205. $gaTag = 'onclick="var that=this;_gaq.push([\'_trackEvent,\'Download\',\''.$fnp[1].'\',this.href]);setTimeout(function(){location.href=that.href;},200);return false;"';
  206. } else {
  207. $dlFile = $file;
  208. $nofolow = "";
  209. $target = "_self";
  210. $gaTag = 'onclick="var that=this;_gaq.push([\'_trackEvent,\'Download\',\''.$fnp[1].'\',this.href]);setTimeout(function(){location.href=that.href;},200);return false;"';
  211. }
  212. $txt = $gdeoptions['link_text'];
  213. if ($gdeoptions['enable_ga'] == "yes") {
  214. $gaLink = " $gaTag";
  215. } else {
  216. $gaLink = "";
  217. }
  218. $linkcode .= "<p class=\"gde-text\"><a href=\"$dlFile\" target=\"$target\" class=\"gde-link\"".$gaLink.$nofollow.">$txt</a></p>";
  219. }
  220. if ($gdeoptions['link_pos'] == "above") {
  221. $code = str_replace("%A%", $linkcode, $code);
  222. $code = str_replace("%B%", '', $code);
  223. } else {
  224. $code = str_replace("%A%", '', $code);
  225. $code = str_replace("%B%", $linkcode, $code);
  226. }
  227. $code = str_replace("%U%", $lnk, $code);
  228. $code = str_replace("%W%", $width, $code);
  229. $code = str_replace("%H%", $height, $code);
  230. $code = str_replace("%FN", $fn, $code);
  231. // file type replacement
  232. $ftype = strtoupper($fnp[1]);
  233. if ($ftype == "TIF") {
  234. $ftype = "TIFF";
  235. }
  236. $code = str_replace("%FT", $ftype, $code);
  237. $code = str_replace("%FS", $fsize, $code);
  238. }
  239. return $code;
  240. }
  241. // activate plugin
  242. register_activation_hook( __FILE__, 'gde_activate');
  243. // allow localisation
  244. load_plugin_textdomain('gde', false, basename( dirname( __FILE__ ) ) . '/languages' );
  245. function gde_activate() {
  246. global $wpdb;
  247. // initial options
  248. $init = gde_init();
  249. }
  250. // add an option page
  251. add_action('admin_menu', 'gde_option_page');
  252. function gde_option_page() {
  253. global $gde_settings_page;
  254. $gde_settings_page = add_options_page('GDE '.__('Settings', 'gde'), 'GDE '.__('Settings', 'gde'), 'manage_options', basename(__FILE__), 'gde_options');
  255. // enable settings jQuery
  256. add_action( 'admin_enqueue_scripts', 'gde_admin_custom_js' );
  257. }
  258. function gde_options() {
  259. if ( function_exists('current_user_can') && !current_user_can('manage_options') ) wp_die('You don\'t have access to this page.');
  260. if (! user_can_access_admin_page()) wp_die( __('You do not have sufficient permissions to access this page', 'gde') );
  261. require(plugin_dir_path(__FILE__).'/options.php');
  262. add_action('in_admin_footer', 'gde_admin_footer');
  263. }
  264. // add additional links, for convenience
  265. $plugin = plugin_basename(__FILE__);
  266. function gde_actlinks($links) {
  267. $settings_link = '<a href="options-general.php?page=gviewer.php">'.__('Settings', 'gde').'</a>';
  268. array_unshift($links, $settings_link);
  269. return $links;
  270. }
  271. function gde_metalinks($links, $file) {
  272. global $debug, $support_link;
  273. $plugin = plugin_basename(__FILE__);
  274. if ($file == $plugin) {
  275. $links[] = $support_link;
  276. }
  277. return $links;
  278. }
  279. add_filter("plugin_action_links_$plugin", 'gde_actlinks');
  280. add_filter("plugin_row_meta", 'gde_metalinks', 10, 2);
  281. // check for beta, if enabled
  282. function gde_checkforBeta($plugin) {
  283. global $gde_ver, $pUrl, $gdeoptions;
  284. // beta messages
  285. $beta_msg['avail'] = array(__('Beta version available', 'gde'), __('Please deactivate the plug-in and install the current version if you wish to participate. Otherwise, you can turn off beta version checking in GDE Settings. Testers appreciated!', 'gde'));
  286. $beta_msg['newer'] = array(__('Updated beta version available', 'gde'), __('A newer beta has been released. Please deactivate the plug-in and install the current version. Thanks for your help!', 'gde'));
  287. $beta_msg['current'] = array(__('You\'re running a beta version. Please give feedback.', 'gde'), __('Thank you for running a test version of Google Doc Embedder. You are running the most current beta version. Please give feedback on this version using the &quot;Support&quot; link above. Thanks for your help!', 'gde'));
  288. $beta_msg['link'] = __('more info', 'gde');
  289. $pdata = get_plugin_data(__FILE__);
  290. if (preg_match('/-dev$/i', $pdata['Version'])) { $isbeta = 1; } else { $isbeta = ""; }
  291. if (strpos($pUrl.'/gviewer.php', $plugin) !== false) {
  292. if ($gdeoptions['suppress_beta'] !== "yes") {
  293. $vcheck = wp_remote_fopen(GDE_BETA_CHKFILE);
  294. }
  295. if ($vcheck) {
  296. $lver = $gde_ver;
  297. $status = explode('@', $vcheck);
  298. $rver = $status[1];
  299. $message = $status[3];
  300. if ($isbeta) {
  301. $titleStr = $beta_msg['newer'][0];
  302. $msgStr = $beta_msg['newer'][1];
  303. } else {
  304. $titleStr = $beta_msg['avail'][0];
  305. $msgStr = $beta_msg['avail'][1];
  306. }
  307. $message = str_replace("%msg", $msgStr, $message);
  308. if ((version_compare(strval($rver), strval($lver), '>') == 1)) {
  309. $msg = "$titleStr: <strong>v".$rver."</strong> - ".$message;
  310. echo '<td colspan="5" class="plugin-update" style="line-height:1.2em; font-size:11px; padding:1px;"><div style="background:#A2F099;border:1px solid #4FE23F; padding:2px; font-weight:bold;">'.$titleStr.'. <a href="javascript:void(0);" onclick="jQuery(\'#gde-beta-msg\').toggle();">('.$beta_msg['link'].')</a></div><div id="gde-beta-msg" style="display:none; padding:10px; text-align:center;">'.$msg.'</div></td>';
  311. } elseif ($isbeta) {
  312. $msg = $beta_msg['current'][0];
  313. echo '<td colspan="5" class="plugin-update" style="line-height:1.2em; font-size:11px; padding:1px;"><div style="border:1px solid; padding:2px; font-weight:bold;">'.$beta_msg['current'][1].' <a href="javascript:void(0);" onclick="jQuery(\'#gde-beta-msg\').toggle();">('.$beta_msg['link'].')</a></div><div id="gde-beta-msg" style="display:none; padding:10px; text-align:center;" >'.$msg.'</div></td>';
  314. } else {
  315. return;
  316. }
  317. } elseif ($isbeta) {
  318. $msg = $beta_msg['current'][1];
  319. echo '<td colspan="5" class="plugin-update" style="line-height:1.2em; font-size:11px; padding:1px;"><div style="border:1px solid; padding:2px; font-weight:bold;">'.$beta_msg['current'][0].' <a href="javascript:void(0);" onclick="jQuery(\'#gde-beta-msg\').toggle();">('.$beta_msg['link'].')</a></div><div id="gde-beta-msg" style="display:none; padding:10px; text-align:center;" >'.$msg.'</div></td>';
  320. }
  321. }
  322. }
  323. add_action('after_plugin_row', 'gde_checkforBeta');
  324. // activate shortcode
  325. add_shortcode('gview', 'gde_gviewer_func');
  326. // editor integration
  327. if ($gdeoptions['disable_editor'] !== "yes") {
  328. // add quicktag
  329. add_action( 'admin_print_scripts', 'gde_admin_print_scripts' );
  330. // add tinymce button
  331. add_action( 'admin_init','gde_mce_addbuttons' );
  332. // extend media upload support to natively unsupported mime types
  333. if ($gdeoptions['ed_extend_upload'] == "yes") {
  334. add_filter('upload_mimes', 'gde_upload_mimes');
  335. }
  336. // embed shortcode instead of link from media library for supported types
  337. if ($gdeoptions['ed_embed_sc'] == "yes") {
  338. add_filter('media_send_to_editor', 'gde_media_insert', 20, 3);
  339. }
  340. }
  341. // footer credit
  342. function gde_admin_footer() {
  343. global $support_link;
  344. $pdata = get_plugin_data(__FILE__);
  345. $plugin_str = __('plugin', 'gde');
  346. $version_str = __('Version', 'gde');
  347. printf('%1$s %2$s | %3$s %4$s | %5$s<br />', $pdata['Title'], $plugin_str, $version_str, $pdata['Version'], $support_link);
  348. }
  349. ?>