PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/shrimp/pi.shrimp.php

https://github.com/erikreagan/hivelogic-ee-shrimp
PHP | 326 lines | 172 code | 54 blank | 100 comment | 14 complexity | 35b5089c695e8734f1a163012e06e237 MD5 | raw file
  1. <?php
  2. /*
  3. =====================================================
  4. Shrimp - by Dan Benjamin, Hivelogic Corporation
  5. -----------------------------------------------------
  6. http://hivelogic.com/projects/shrimp
  7. -----------------------------------------------------
  8. Copyright (c) 2009 Hivelogic Corporation
  9. =====================================================
  10. This software is released under the
  11. GNU General Public License (v3.0)
  12. http://www.gnu.org/licenses/gpl-3.0.txt
  13. =====================================================
  14. File: pi.shrimp.php
  15. -----------------------------------------------------
  16. Purpose: Shorten website URLs
  17. =====================================================
  18. */
  19. /*
  20. * Forked by @danott and later by @erikreagan
  21. *
  22. * Dan wanted to have one template as the redirector for multiple weblogs.
  23. * His solution was to use the EE preference for weblog search results
  24. * path to build the longurl for the entry.
  25. *
  26. * Also changed some defaults along the way, and added some new ones
  27. * for increased functionality for his needs.
  28. *
  29. * Erik ported the plugin to include compatibility for both EE 1.x and EE 2.x
  30. */
  31. $plugin_info = array(
  32. 'pi_name' => 'Shrimp',
  33. 'pi_version' => '1.5',
  34. 'pi_author' => 'Dan Benjamin',
  35. 'pi_author_url' => 'http://hivelogic.com/projects/shrimp/',
  36. 'pi_description' => 'Shortens website URLs.',
  37. 'pi_usage' => Shrimp::usage()
  38. );
  39. /**
  40. * Super Class
  41. *
  42. * @package Shrimp
  43. * @category Plugin
  44. * @author Dan Benjamin
  45. * @link http://hivelogic.com/projects/shrimp
  46. */
  47. class Shrimp {
  48. var $default_redirect_template = "u";
  49. // @danott fork - a new default
  50. // In some scenarious, I'd rather just go to the homepage rather than show an error.
  51. var $do_forward_instead_of_error = TRUE;
  52. /*
  53. * from @erikreagan
  54. * Initialize objects for EE1 and EE2 use in a single plugin
  55. * They're later referenced in the constructor based on app version number
  56. */
  57. var $DB;
  58. var $FNS;
  59. var $OUT;
  60. var $TMPL;
  61. // initialize the variables
  62. var $entry_id = '';
  63. var $template = '';
  64. var $title = '';
  65. var $path = '';
  66. var $url = '';
  67. /*
  68. * from @erikreagan
  69. * This variable will be for our DB table nomenclature.
  70. * It's defined in our constructor
  71. */
  72. var $nomenclature = '';
  73. // retrieve the data and filter or reformat it
  74. function Shrimp()
  75. {
  76. /*
  77. * from @erikreagan
  78. * EE version check to properly reference our EE objects
  79. */
  80. if (version_compare(APP_VER, '2', '<'))
  81. {
  82. // EE 1.x is in play
  83. global $DB, $FNS, $OUT, $TMPL;
  84. $this->DB =& $DB;
  85. $this->FNS =& $FNS;
  86. $this->OUT =& $OUT;
  87. $this->TMPL =& $TMPL;
  88. $this->nomenclature = 'weblog';
  89. } else {
  90. // EE 2.x is in play
  91. $this->EE =& get_instance();
  92. $this->DB =& $this->EE->db;
  93. $this->FNS =& $this->EE->functions;
  94. $this->OUT =& $this->EE->output;
  95. $this->TMPL =& $this->EE->TMPL;
  96. $this->nomenclature = 'channel';
  97. }
  98. // pre-sanitize the entry_id as it may be used in a SQL query
  99. $this->entry_id = (int)$this->DB->escape_str($this->TMPL->fetch_param('entry_id'));
  100. /* @danott fork
  101. * This was on multiple lines. Moved it down to one for simplicity sake.
  102. * Either grabs the template from the parameters cleans it up, or uses the default.
  103. *
  104. * fetch_param returns escaped characters, so we need to convert the slashes
  105. * if no template group is specified, use the default.
  106. */
  107. $this->template = ( ! $this->TMPL->fetch_param('template')) ? $this->default_redirect_template : str_replace(SLASH,'/',$this->TMPL->fetch_param('template'));
  108. // sanitize the title so it will be usable in an <a href> tag
  109. $this->title = htmlentities($this->TMPL->fetch_param('title'));
  110. // create the shortened URL path without the protocol and domain
  111. $this->path = ('/'.$this->template.'/'.$this->entry_id);
  112. // create the shortened URL
  113. $this->url = $this->FNS->create_url($this->path,false);
  114. }
  115. // generate a valid HTML link
  116. // uses the shortened URL in place of a title if no title is specified
  117. function link()
  118. {
  119. $title = ($this->title === '') ? $this->url : $this->title;
  120. return '<a href="'.$this->url.'" title="'.$title.'">'.$title.'</a>';
  121. }
  122. // generate a meta tag suitable for placement within the <head> block of a webpage
  123. // will retrn NULL if no entry_id is assigned, allowing placement in a sitewide header
  124. function meta_tag()
  125. {
  126. return ($this->entry_id === '') ? NULL : '<link rel="shorturl" href="'.$this->url.'" />';
  127. }
  128. // return the shortened URL
  129. function permalink()
  130. {
  131. return $this->url;
  132. }
  133. // provides redirection from the shortened URL to the full-length URL using the
  134. // specified entry_id and template group (for use in the redirection template)
  135. function redirect() {
  136. /* @danott
  137. * To me, it seemed like a more elegant solution to not have to pass a template parameter
  138. * but rarther to use some preferences we already have stored. Using the entry's, weblog's
  139. * search result path, we can build the same url that a search page would take this entry to.
  140. * It may not work for all people, having not set that preference, but it works for me.
  141. * This way you can have one template 's' to provide the shortcut url for multiple weblogs.
  142. */
  143. /*
  144. * from @erikreagan
  145. * Query has been written to use "weblog" or "channel" based on EE version
  146. */
  147. $query = $this->DB->query("SELECT exp_".$this->nomenclature."_titles.url_title, exp_".$this->nomenclature."s.search_results_url
  148. FROM exp_".$this->nomenclature."_titles
  149. JOIN exp_".$this->nomenclature."s ON exp_".$this->nomenclature."_titles.".$this->nomenclature."_id = exp_".$this->nomenclature."s.".$this->nomenclature."_id
  150. WHERE exp_".$this->nomenclature."_titles.entry_id = ".$this->entry_id);
  151. // display an error if the specified entry_id doesn't exist
  152. if ($query->num_rows == 0)
  153. {
  154. // @danott fork - do Dan Benjamin's original error message
  155. if ( ! $this->do_forward_instead_of_error)
  156. {
  157. $this->OUT->show_user_error('general', 'Invalid or nonexistent entry.');
  158. exit();
  159. }
  160. // @danott fork - otherwise, just create a link to the homepage.
  161. $long_url = $this->FNS->create_url('/',false);
  162. } else {
  163. /*
  164. * from @erikreagan
  165. * If we're in EE 2.x we need to reference the result_object
  166. * We do this so we can use the same object with our result regardless of the EE version in play
  167. */
  168. $query->row = (version_compare(APP_VER, '2', '<')) ? $query->row : get_object_vars($query->result_object[0]) ;
  169. // create the full-length URL using the specified template group and the retrieved url_title
  170. // If the weblog has a search_results_url we use that, otherwise we use the template paramter passed to the plugin
  171. // Using the longhand syntax for readability
  172. if ($query->row['search_results_url'] != '') {
  173. $long_url = $query->row['search_results_url'].$query->row['url_title'];
  174. } else {
  175. $long_url = $this->FNS->create_url($this->template.'/'.$query->row['url_title'],false);
  176. }
  177. }
  178. // issue a redirect to the full-length URL with a 301 status and exit
  179. header("HTTP/1.1 301 Moved Permanently");
  180. header("Location: ".$long_url);
  181. exit();
  182. }
  183. // return the path without the protocol and domain
  184. function relative_url()
  185. {
  186. return $this->path;
  187. }
  188. // plugin usage
  189. function usage()
  190. {
  191. ob_start();
  192. ?>
  193. Shrimp is an ExpressionEngine plugin that provides URL shortening functionality. Shrimp is different from similar plugins in that it provides features like customizable link generation, access to the shortened URL for your own custom links, smart meta tags which hide themselves on non-entry pages, access to the relative URL (without the protocol or domain), and more, without any unnecessary database access.
  194. Shrimp transforms long URLs like this:
  195. http://site.com/weblog/entries/some-article-title
  196. into shortened URLs like this:
  197. http://site.com/u/123
  198. Shrimp then provides redirection from the shortened URL to the long URL through the use of a simple, one-line template.
  199. Shrimp is different from similar plugins in that it provides features like customizable link generation, access to the shortened URL for your own custom links, smart meta tags which hide themselves on non-entry pages, access to the relative URL (without the protocol or domain), and more, without any unnecessary database access.
  200. It is recommended that you remove the "index.php" from your URLs as described here (although Shrimp will work either way):
  201. http://expressionengine.com/wiki/Remove_index.php_From_URLs
  202. == USAGE ==
  203. Integrating Shrimp into your ExpressionEngine website is a straight forward process.
  204. Create a new template group for the redirection. Give the group a short name, such as "u", in order to keep the URL as small as possible (Shrimp will actually assume the default template group name "u" if you don't specify one).
  205. Paste the following line into the index template in the new template group. You must replace "weblog/entries" in the example below with the name of your individual entry view template:
  206. {exp:shrimp:redirect template="weblog/entries" entry_id="{segment_2}"}
  207. Add the following line to the <head>...</head> block of your entry view template, replacing the template value with the name of the template group you created in the first step (or omit it and Shrimp will use "u"):
  208. {exp:shrimp:meta_tag template="u" entry_id="{entry_id}"}
  209. Display the short link on your site, within an {exp:weblog:entries} tag block. Shrimp can generate a full <a href> tag with a customizable title like this:
  210. {exp:shrimp:link title="Short URL" template="u" entry_id="{entry_id}"}
  211. This will generate an <a href> tag like this:
  212. <a href="http://site.com/u/123" title="Short URL">Short URL</a>
  213. You can specify anything you want for the link title, such as the entry's title:
  214. {exp:shrimp:link title="{title}" template="u" entry_id="{entry_id}"}
  215. This will generate an <a href> tag like this:
  216. <a href="http://site.com/u/123" title="My Entry Title">My Entry Title</a>
  217. If you'd prefer to create your own custom links, you can access the shortened URL directly for use in your own links or for display using the "permalink" method like this:
  218. {exp:shrimp:permalink template="u" entry_id="{entry_id}"}
  219. This will output just the shortened permalink without the <a href> tag, like this:
  220. http://site.com/u/123
  221. If you just want to access the shortened path without the protocol (http://) and site name, you can call the "relative_url" method, like this:
  222. {exp:shrimp:relative_url template="u" entry_id="{entry_id}"}
  223. This will return just the path of the short URL, like this:
  224. /u/123
  225. == Change Log ==
  226. 1.0
  227. * Initial release.
  228. 1.0.1
  229. * Fixed a superfluous slash in the <a href> tag.
  230. * Adding a check for existing entry_id. Will now display an error if not found.
  231. 1.0.2.
  232. * Casting entry_id as an integer to prevent PHP errors
  233. 1.5
  234. * Updated by Erik Reagan (@erikreagan) to support both ExpressionEngine 1.x and 2.x
  235. <?php
  236. $buffer = ob_get_contents();
  237. ob_end_clean();
  238. return $buffer;
  239. }
  240. // end of usage function
  241. }
  242. // end of Shrimp class
  243. /* end of file pi.shrimp.php */
  244. /* location: ./system/plugins/pi.shrimp.php */