Google Doc Embedder - Version 1.8

Version Description

  • Added: Ability to set height/width to percentage (thanks eturfboer)
  • Fixed: Compatibility with PHP 5.3+, various function tuning
Download this release

Release Info

Developer k3davis
Plugin Icon wp plugin Google Doc Embedder
Version 1.8
Comparing to
See all releases

Code changes from version 1.7.3 to 1.8

Files changed (6) hide show
  1. functions.php +23 -19
  2. gviewer.php +26 -18
  3. options.php +19 -5
  4. readme.txt +11 -4
  5. screenshot-2.png +0 -0
  6. uninstall.php +2 -7
functions.php CHANGED
@@ -4,30 +4,33 @@ function getDefaults() {
4
  // set global default settings
5
  $defaults = array(
6
  "gde_default_width" => "600",
 
7
  "gde_default_height" => "500",
 
8
  "gde_show_dl" => 1,
9
  "gde_link_text" => "Download (%FT, %FS)",
10
  "gde_link_pos" => "below",
11
  "gde_link_func" => "default"
12
  );
13
-
14
  return $defaults;
15
  }
16
 
 
 
 
 
 
 
 
 
 
 
 
17
  function validLink($link) {
18
 
19
- $urlregex = "^(https?://)"
20
- . "?(([0-9A-Za-z_!~*'().&=+$%-]+: )?[0-9A-Za-z_!~*'().&=+$%-]+@)?" //user@
21
- . "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP- 199.194.52.184
22
- . "|" // allows either IP or domain
23
- . "([0-9A-Za-z_!~*'()-]+\.)*" // tertiary domain(s)- www.
24
- . "([0-9A-Za-z][0-9A-Za-z-]{0,61})?[0-9A-Za-z]\." // second level domain
25
- . "[A-Za-z]{2,6})" // first level domain- .com or .museum
26
- . "(:[0-9]{1,4})?" // port number- :80
27
- . "((/?)|" // a slash isn't required if there is no file name
28
- . "(/[0-9A-Za-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
29
-
30
- if (eregi($urlregex, $link)) {
31
  return true;
32
  } else {
33
  return false;
@@ -36,7 +39,7 @@ function validLink($link) {
36
 
37
  function validType($link, $exts) {
38
 
39
- if(preg_match("/($exts)$/i",$link)){
40
  return true;
41
  } else {
42
  return false;
@@ -47,8 +50,7 @@ function validUrl($url) {
47
 
48
  // checks for existence and returns filesize
49
  $handle = curl_init($url);
50
- if (false === $handle)
51
- {
52
  return false;
53
  }
54
  curl_setopt($handle, CURLOPT_HEADER, true);
@@ -98,8 +100,11 @@ function formatBytes($bytes, $precision = 2) {
98
  }
99
  }
100
 
101
- function sanitizeOpt($value) {
102
- $value = ereg_replace('[^0-9]+', '', $value);
 
 
 
103
  return $value;
104
  }
105
 
@@ -112,7 +117,6 @@ function getPluginUrl() {
112
  return plugins_url(plugin_basename(dirname(__FILE__)));
113
  }
114
 
115
-
116
  function shortUrl($u) {
117
  return file_get_contents('http://tinyurl.com/api-create.php?url='.$u);
118
  }
4
  // set global default settings
5
  $defaults = array(
6
  "gde_default_width" => "600",
7
+ "gde_width_type" => "px",
8
  "gde_default_height" => "500",
9
+ "gde_height_type" => "px",
10
  "gde_show_dl" => 1,
11
  "gde_link_text" => "Download (%FT, %FS)",
12
  "gde_link_pos" => "below",
13
  "gde_link_func" => "default"
14
  );
 
15
  return $defaults;
16
  }
17
 
18
+ function getObsolete() {
19
+ // deprecated options
20
+ $legacy_options = array(
21
+ "gde_xlogo" => 0,
22
+ "gde_xfull" => 0,
23
+ "gde_xpgup" => 0,
24
+ "gde_xzoom" => 0
25
+ );
26
+ return $legacy_options;
27
+ }
28
+
29
  function validLink($link) {
30
 
31
+ $urlregex = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/i';
32
+
33
+ if (preg_match($urlregex, $link)) {
 
 
 
 
 
 
 
 
 
34
  return true;
35
  } else {
36
  return false;
39
 
40
  function validType($link, $exts) {
41
 
42
+ if(preg_match("/($exts)$/i",$link)) {
43
  return true;
44
  } else {
45
  return false;
50
 
51
  // checks for existence and returns filesize
52
  $handle = curl_init($url);
53
+ if (false === $handle) {
 
54
  return false;
55
  }
56
  curl_setopt($handle, CURLOPT_HEADER, true);
100
  }
101
  }
102
 
103
+ function sanitizeOpt($value, $type) {
104
+ $value = preg_replace("/[^0-9]*/", '', $value);
105
+ if (($type == "pc") && ($value > 100)) {
106
+ $value = "100";
107
+ }
108
  return $value;
109
  }
110
 
117
  return plugins_url(plugin_basename(dirname(__FILE__)));
118
  }
119
 
 
120
  function shortUrl($u) {
121
  return file_get_contents('http://tinyurl.com/api-create.php?url='.$u);
122
  }
gviewer.php CHANGED
@@ -5,7 +5,7 @@ Plugin Name: Google Doc Embedder
5
  Plugin URI: http://davismetro.com/gde/
6
  Description: Lets you embed PDF files, PowerPoint presentations, and TIFF images in a web page using the Google Docs Viewer.
7
  Author: Kevin Davis
8
- Version: 1.7.3
9
  */
10
 
11
  /* Copyright 2009 Kevin Davis. E-mail: kev@tnw.org
@@ -32,27 +32,40 @@ function gviewer_func($atts) {
32
 
33
  // current settings
34
  $dl = get_option('gde_show_dl');
35
- $wd = get_option('gde_default_width');
36
- $ht = get_option('gde_default_height');
37
  $txt = get_option('gde_link_text');
38
  extract(shortcode_atts(array(
39
  'file' => '',
40
  'save' => $dl,
41
- 'width' => $wd,
42
- 'height' => $ht
 
43
  ), $atts));
44
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  // supported file types - list acceptable extensions separated by |
46
  $exts = "pdf|ppt|tif|tiff";
47
 
48
  // check link for validity
49
  if (!$file) {
50
  $code = "\n<!-- GDE EMBED ERROR: file attribute not found (check syntax) -->\n";
51
- } elseif (!validLink($file)){
52
  $code = "\n<!-- GDE EMBED ERROR: invalid URL, please use fully qualified URL -->\n";
53
- } elseif (!validType($file,$exts)) {
54
  $code = "\n<!-- GDE EMBED ERROR: unsupported file type -->\n";
55
- } elseif (!$fsize = validUrl($file)) {
56
  $code = "\n<!-- GDE EMBED ERROR: file not found -->\n";
57
  } else {
58
  $pUrl = getPluginUrl();
@@ -62,7 +75,7 @@ function gviewer_func($atts) {
62
  $fsize = formatBytes($fsize);
63
 
64
  $code=<<<HERE
65
- <iframe src="%U%" style="width:%W%px; height:%H%px;" frameborder="0" class="gde-frame"></iframe>\n
66
  HERE;
67
 
68
  $lnk = "http://docs.google.com/viewer?url=".urlencode($file)."&embedded=true";
@@ -124,13 +137,8 @@ function gde_activate() {
124
  add_option($set, $val);
125
  }
126
 
127
- // remove legacy options if present
128
- $legacy_options = array(
129
- "gde_xlogo" => 0,
130
- "gde_xfull" => 0,
131
- "gde_xpgup" => 0,
132
- "gde_xzoom" => 0
133
- );
134
  foreach ($legacy_options as $lopt => $val) {
135
  delete_option($lopt);
136
  }
@@ -152,7 +160,7 @@ function gde_options() {
152
  $plugin = plugin_basename(__FILE__);
153
  function my_plugin_actlinks( $links ) {
154
  $settings_link = '<a href="options-general.php?page=gviewer.php">Settings</a>';
155
- array_unshift( $links, $settings_link );
156
  return $links;
157
  }
158
  add_filter("plugin_action_links_$plugin", 'my_plugin_actlinks' );
5
  Plugin URI: http://davismetro.com/gde/
6
  Description: Lets you embed PDF files, PowerPoint presentations, and TIFF images in a web page using the Google Docs Viewer.
7
  Author: Kevin Davis
8
+ Version: 1.8
9
  */
10
 
11
  /* Copyright 2009 Kevin Davis. E-mail: kev@tnw.org
32
 
33
  // current settings
34
  $dl = get_option('gde_show_dl');
 
 
35
  $txt = get_option('gde_link_text');
36
  extract(shortcode_atts(array(
37
  'file' => '',
38
  'save' => $dl,
39
+ 'width' => '',
40
+ 'height' => '',
41
+ 'force' => ''
42
  ), $atts));
43
+ $width = str_replace("px", "", trim($width));
44
+ if (!$width || !preg_match("/^\d+%?$/", $width)) {
45
+ $width = get_option('gde_default_width');
46
+ if (get_option('gde_width_type') == "pc") {
47
+ $width .= "%";
48
+ }
49
+ }
50
+ $height = str_replace("px", "", trim($height));
51
+ if (!$height || !preg_match("/^\d+%?$/", $height)) {
52
+ $height = get_option('gde_default_height');
53
+ if (get_option('gde_height_type') == "pc") {
54
+ $height .= "%";
55
+ }
56
+ }
57
+
58
  // supported file types - list acceptable extensions separated by |
59
  $exts = "pdf|ppt|tif|tiff";
60
 
61
  // check link for validity
62
  if (!$file) {
63
  $code = "\n<!-- GDE EMBED ERROR: file attribute not found (check syntax) -->\n";
64
+ } elseif ((!validLink($file)) && ($force !== "1")) {
65
  $code = "\n<!-- GDE EMBED ERROR: invalid URL, please use fully qualified URL -->\n";
66
+ } elseif ((!validType($file,$exts)) && ($force !== "1")) {
67
  $code = "\n<!-- GDE EMBED ERROR: unsupported file type -->\n";
68
+ } elseif ((!$fsize = validUrl($file)) && ($force !== "1")) {
69
  $code = "\n<!-- GDE EMBED ERROR: file not found -->\n";
70
  } else {
71
  $pUrl = getPluginUrl();
75
  $fsize = formatBytes($fsize);
76
 
77
  $code=<<<HERE
78
+ <iframe src="%U%" width="%W%" height="%H%" frameborder="0" style="min-width:305px;" class="gde-frame"></iframe>\n
79
  HERE;
80
 
81
  $lnk = "http://docs.google.com/viewer?url=".urlencode($file)."&embedded=true";
137
  add_option($set, $val);
138
  }
139
 
140
+ // remove deprecated options if present
141
+ $legacy_options = getObsolete();
 
 
 
 
 
142
  foreach ($legacy_options as $lopt => $val) {
143
  delete_option($lopt);
144
  }
160
  $plugin = plugin_basename(__FILE__);
161
  function my_plugin_actlinks( $links ) {
162
  $settings_link = '<a href="options-general.php?page=gviewer.php">Settings</a>';
163
+ array_unshift($links, $settings_link);
164
  return $links;
165
  }
166
  add_filter("plugin_action_links_$plugin", 'my_plugin_actlinks' );
options.php CHANGED
@@ -7,7 +7,9 @@ if(isset($_REQUEST['defaults'])) {
7
  // reset to plug-in defaults
8
  $set = getDefaults();
9
  update_option('gde_default_width', $set['gde_default_width']);
 
10
  update_option('gde_default_height', $set['gde_default_height']);
 
11
  update_option('gde_show_dl', $set['gde_show_dl']);
12
  update_option('gde_link_text', $set['gde_link_text']);
13
  update_option('gde_link_pos', $set['gde_link_pos']);
@@ -24,11 +26,17 @@ if(isset($_REQUEST['defaults'])) {
24
  }
25
  if(isset($_POST['gde_default_width'])) {
26
  $neww = $_POST['gde_default_width'];
27
- if (strlen($neww) > 0) update_option('gde_default_width', sanitizeOpt($neww));
 
 
 
28
  }
29
  if(isset($_POST['gde_default_height'])) {
30
  $newh = $_POST['gde_default_height'];
31
- if (strlen($newh) > 0) update_option('gde_default_height', sanitizeOpt($newh));
 
 
 
32
  }
33
  if(isset($_POST['gde_link_text'])) {
34
  $newt = $_POST['gde_link_text'];
@@ -53,15 +61,21 @@ if(isset($_REQUEST['defaults'])) {
53
  <table class="form-table">
54
  <tr valign="top">
55
  <td colspan="2"><strong>Global Viewer Options</strong><br/>
56
- To override size on individual posts, manually set <code>height=</code> and <code>width=</code> (number in px) in the post shortcode.</td>
57
  </tr>
58
  <tr valign="top">
59
  <th scope="row">Default Width</th>
60
- <td><input type="text" size="5" name="gde_default_width" value="<?php echo get_option('gde_default_width'); ?>" /> px</td>
 
 
 
61
  </tr>
62
  <tr valign="top">
63
  <th scope="row">Default Height</th>
64
- <td><input type="text" size="5" name="gde_default_height" value="<?php echo get_option('gde_default_height'); ?>" /> px</td>
 
 
 
65
  </tr>
66
  <tr valign="top">
67
  <td colspan="2"><strong>Download Link Options</strong><br/>
7
  // reset to plug-in defaults
8
  $set = getDefaults();
9
  update_option('gde_default_width', $set['gde_default_width']);
10
+ update_option('gde_width_type', $set['gde_width_type']);
11
  update_option('gde_default_height', $set['gde_default_height']);
12
+ update_option('gde_height_type', $set['gde_height_type']);
13
  update_option('gde_show_dl', $set['gde_show_dl']);
14
  update_option('gde_link_text', $set['gde_link_text']);
15
  update_option('gde_link_pos', $set['gde_link_pos']);
26
  }
27
  if(isset($_POST['gde_default_width'])) {
28
  $neww = $_POST['gde_default_width'];
29
+ if (strlen($neww) > 0) update_option('gde_default_width', sanitizeOpt($neww, $_POST['gde_width_type']));
30
+ }
31
+ if(isset($_POST['gde_width_type'])) {
32
+ update_option('gde_width_type', $_POST['gde_width_type']);
33
  }
34
  if(isset($_POST['gde_default_height'])) {
35
  $newh = $_POST['gde_default_height'];
36
+ if (strlen($newh) > 0) update_option('gde_default_height', sanitizeOpt($newh, $_POST['gde_height_type']));
37
+ }
38
+ if(isset($_POST['gde_height_type'])) {
39
+ update_option('gde_height_type', $_POST['gde_height_type']);
40
  }
41
  if(isset($_POST['gde_link_text'])) {
42
  $newt = $_POST['gde_link_text'];
61
  <table class="form-table">
62
  <tr valign="top">
63
  <td colspan="2"><strong>Global Viewer Options</strong><br/>
64
+ To override size on individual posts, manually set in the post shortcode using (for example) <code>height="400"</code> (px) or <code>width="80%"</code>.</td>
65
  </tr>
66
  <tr valign="top">
67
  <th scope="row">Default Width</th>
68
+ <td><input type="text" size="5" name="gde_default_width" value="<?php echo get_option('gde_default_width'); ?>" /> <select name="gde_width_type">
69
+ <?php showOption('px', 'gde_width_type', t('px')); ?>
70
+ <?php showOption('pc', 'gde_width_type', t('%')); ?>
71
+ </select></td>
72
  </tr>
73
  <tr valign="top">
74
  <th scope="row">Default Height</th>
75
+ <td><input type="text" size="5" name="gde_default_height" value="<?php echo get_option('gde_default_height'); ?>" /> <select name="gde_height_type">
76
+ <?php showOption('px', 'gde_height_type', t('px')); ?>
77
+ <?php showOption('pc', 'gde_height_type', t('%')); ?>
78
+ </select></td>
79
  </tr>
80
  <tr valign="top">
81
  <td colspan="2"><strong>Download Link Options</strong><br/>
readme.txt CHANGED
@@ -29,7 +29,7 @@ Go to "GDE Settings" (under "Settings" in the admin panel) to change defaults, o
29
  == Frequently Asked Questions ==
30
 
31
  = What file types can be embedded? =
32
- This plug-in can embed PDF, PowerPoint (PPT), or TIFF files only. The file to embed must first be available somewhere on the internet. You can upload it to your WordPress site using the standard techniques, or link to a file on another site. **You do not need to save the file in Google Documents first to embed it, and in fact you should NOT do so, unless the file is shared publicly (can be accessed without logging in).**
33
 
34
  = How do I embed a file in my page or post? =
35
  Use the custom shortcode `[gview]` to embed the file, as shown:
@@ -41,14 +41,17 @@ Note: the `file=` attribute (pointing to the full URL of the file) is **required
41
  Optional attributes:
42
 
43
  * `save=` : Set to 0 if you wish to suppress the direct download link to the file under the embedded viewer (1 for on, by default)
44
- * `width=` : To override the default width of the viewer, enter a new width value (number in pixels)
45
- * `height=` : To override the default height of the viewer, enter a new height value (number in pixels)
 
 
 
46
 
47
  = Nothing is showing up! What do I do? =
48
  View the source on the web page where you've embedded the viewer. In order to degrade gracefully in case an error occurs, error messages will be inserted as HTML comments in these pages at the spot the viewer is called.
49
 
50
  = I wish the plug-in had feature XYZ... =
51
- That's not a question ;) but if you have any particular ideas on further development of this plug-in, please <a href="http://wordpress.org/tags/google-document-embedder?forum_id=10#postform">post a message</a> and I'll see what I can do.
52
 
53
  == Screenshots ==
54
 
@@ -57,6 +60,10 @@ That's not a question ;) but if you have any particular ideas on further develop
57
 
58
  == Changelog ==
59
 
 
 
 
 
60
  = 1.7.3 =
61
  * Fixed: File URL containing tilde (~) considered invalid (thanks mjurek)
62
 
29
  == Frequently Asked Questions ==
30
 
31
  = What file types can be embedded? =
32
+ This plug-in can embed PDF, PowerPoint (PPT), or TIFF files only. The file to embed must first be publicly available somewhere on the internet. You can upload it to your WordPress site using the standard techniques, or link to a file on another site.
33
 
34
  = How do I embed a file in my page or post? =
35
  Use the custom shortcode `[gview]` to embed the file, as shown:
41
  Optional attributes:
42
 
43
  * `save=` : Set to 0 if you wish to suppress the direct download link to the file under the embedded viewer (1 for on, by default)
44
+ * `width=` : To override the default width of the viewer, enter a new width value - e.g., "400" (px) or "80%"
45
+ * `height=` : To override the default height of the viewer, enter a new height value - e.g., "400" (px) or "80%"
46
+
47
+ = Will it embed files that are password-protected or stored in protected folders/sites? =
48
+ Most likely, no. If your file requires a login to view - such as being saved in a password-protected directory, or behind a firewall (on your intranet, etc.), the viewer will probably not be able to access the file. For files stored on Google Docs, the viewer will prompt you to log in first, which most users presumably couldn't do. This is what is meant above, that the document should be "publicly available." Please save the file in a publicly accessible location for best results.
49
 
50
  = Nothing is showing up! What do I do? =
51
  View the source on the web page where you've embedded the viewer. In order to degrade gracefully in case an error occurs, error messages will be inserted as HTML comments in these pages at the spot the viewer is called.
52
 
53
  = I wish the plug-in had feature XYZ... =
54
+ That's not a question ;) but if you have any particular ideas on further development of this plug-in, please post <a href="http://wordpress.org/tags/google-document-embedder?forum_id=10#postform">on the forum</a> or privately using the <a href="http://www.davismetro.com/gde/support/">support form</a> and I'll see what I can do.
55
 
56
  == Screenshots ==
57
 
60
 
61
  == Changelog ==
62
 
63
+ = 1.8 =
64
+ * Added: Ability to set height/width to percentage (thanks eturfboer)
65
+ * Fixed: Compatibility with PHP 5.3+, various function tuning
66
+
67
  = 1.7.3 =
68
  * Fixed: File URL containing tilde (~) considered invalid (thanks mjurek)
69
 
screenshot-2.png CHANGED
Binary file
uninstall.php CHANGED
@@ -8,14 +8,9 @@ foreach ($options as $opt => $val) {
8
  delete_option($opt);
9
  }
10
 
11
- // remove legacy options if present
12
 
13
- $legacy_options = array(
14
- "gde_xlogo" => 0,
15
- "gde_xfull" => 0,
16
- "gde_xpgup" => 0,
17
- "gde_xzoom" => 0
18
- );
19
  foreach ($legacy_options as $lopt => $val) {
20
  delete_option($lopt);
21
  }
8
  delete_option($opt);
9
  }
10
 
11
+ // remove deprecated options if present
12
 
13
+ $legacy_options = getObsolete();
 
 
 
 
 
14
  foreach ($legacy_options as $lopt => $val) {
15
  delete_option($lopt);
16
  }