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

/tags/1.5/root-cookie.php

https://github.com/linickx/root-cookie
PHP | 279 lines | 188 code | 36 blank | 55 comment | 26 complexity | 451aefe7625ed6d97b77763f7af56805 MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: root Cookie
  4. Plugin URI: http://www.linickx.com/archives/1856/introducing-root-cookie-1-5-now-with-subdomain-support
  5. Description: Changes the cookie default path to / (i.e. the whole domain.com not just domain.com/blog) with an option to go across subdomains
  6. Author: Nick [LINICKX] Bettison and Vizion Interactive, Inc
  7. Version: 1.5
  8. Author URI: http://www.linickx.com
  9. License: Free to use non-commercially.
  10. Warranties: None.
  11. == The Changelog has been moved to readme.txt ==
  12. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  13. */
  14. # OK, so we rock up and setup a constant....
  15. define('ROOT_COOKIE', '/' );
  16. # Then we paste the WP functions from /wp-includes/pluggable.php
  17. # ...
  18. # and to finish we replace COOKIEPATH, PLUGINS_COOKIE_PATH and ADMIN_COOKIE_PATH with ROOT_COOKIE, job done!
  19. if ( !function_exists('wp_set_auth_cookie') ) :
  20. /**
  21. * Sets the authentication cookies based User ID.
  22. *
  23. * The $remember parameter increases the time that the cookie will be kept. The
  24. * default the cookie is kept without remembering is two days. When $remember is
  25. * set, the cookies will be kept for 14 days or two weeks.
  26. *
  27. * @since 2.5
  28. *
  29. * @param int $user_id User ID
  30. * @param bool $remember Whether to remember the user or not
  31. */
  32. function wp_set_auth_cookie($user_id, $remember = false, $secure = '') {
  33. if ( $remember ) {
  34. $expiration = $expire = time() + 1209600;
  35. } else {
  36. $expiration = time() + 172800;
  37. $expire = 0;
  38. }
  39. if ( '' === $secure )
  40. $secure = is_ssl() ? true : false;
  41. if ( $secure ) {
  42. $auth_cookie_name = SECURE_AUTH_COOKIE;
  43. $scheme = 'secure_auth';
  44. } else {
  45. $auth_cookie_name = AUTH_COOKIE;
  46. $scheme = 'auth';
  47. }
  48. $auth_cookie = wp_generate_auth_cookie($user_id, $expiration, $scheme);
  49. $logged_in_cookie = wp_generate_auth_cookie($user_id, $expiration, 'logged_in');
  50. do_action('set_auth_cookie', $auth_cookie, $expire, $expiration, $user_id, $scheme);
  51. do_action('set_logged_in_cookie', $logged_in_cookie, $expire, $expiration, $user_id, 'logged_in');
  52. $subdomain = get_option('rootcookie_subdomain');
  53. $rootcookie_subdomain_manual = get_option('rootcookie_subdomain_manual');
  54. if($subdomain==1)
  55. {
  56. # Use Scotts implementation
  57. $info = get_bloginfo('url');
  58. $info = parse_url($info);
  59. $info = $info['host'];
  60. $exp = explode('.',$info);
  61. if(count($exp)==3){$domain = '.'.$exp[1].'.'.$exp[2];}
  62. elseif(count($exp)==2){$domain = '.'.$info;}
  63. elseif(3<count($exp)){$exp = array_reverse($exp); $domain = '.'.$exp[1].'.'.$exp[0];}
  64. else{$domain = COOKIE_DOMAIN;}
  65. }
  66. elseif (!is_null($rootcookie_subdomain_manual))
  67. {
  68. # Use manual domain name setting
  69. $domain = $rootcookie_subdomain_manual;
  70. }
  71. else
  72. {
  73. # Default
  74. $domain = COOKIE_DOMAIN;
  75. }
  76. // Set httponly if the php version is >= 5.2.0
  77. if ( version_compare(phpversion(), '5.2.0', 'ge') ) {
  78. setcookie($auth_cookie_name, $auth_cookie, $expire, ROOT_COOKIE, $domain, $secure);
  79. /** Duplicate of above - Created by Find & Replace
  80. setcookie($auth_cookie_name, $auth_cookie, $expire, ROOT_COOKIE, $domain, $secure);
  81. */
  82. setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, ROOT_COOKIE, $domain);
  83. if ( COOKIEPATH != SITECOOKIEPATH )
  84. setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, ROOT_COOKIE, $domain, false, true);
  85. } else {
  86. $cookie_domain = $domain;
  87. if ( !empty($cookie_domain) )
  88. $cookie_domain .= '; HttpOnly';
  89. setcookie($auth_cookie_name, $auth_cookie, $expire, ROOT_COOKIE, $cookie_domain, $secure);
  90. /** Duplicate of above - Created by Find & Replace
  91. setcookie($auth_cookie_name, $auth_cookie, $expire, ROOT_COOKIE, $cookie_domain, $secure);
  92. */
  93. setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, ROOT_COOKIE, $cookie_domain);
  94. if ( ROOT_COOKIE != SITECOOKIEPATH )
  95. setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, ROOT_COOKIE, $domain);
  96. }
  97. }
  98. endif;
  99. if ( !function_exists('wp_clear_auth_cookie') ) :
  100. /**
  101. * Removes all of the cookies associated with authentication.
  102. *
  103. * @since 2.5
  104. */
  105. function wp_clear_auth_cookie() {
  106. do_action('clear_auth_cookie');
  107. $subdomain = get_option('rootcookie_subdomain');
  108. $rootcookie_subdomain_manual = get_option('rootcookie_subdomain_manual');
  109. # As ABOVE!
  110. if($subdomain==1)
  111. {
  112. $info = get_bloginfo('url');
  113. $info = parse_url($info);
  114. $info = $info['host'];
  115. $exp = explode('.',$info);
  116. if(count($exp)==3){$domain = '.'.$exp[1].'.'.$exp[2];}
  117. elseif(count($exp)==2){$domain = '.'.$info;}
  118. elseif(3<count($exp)){$exp = array_reverse($exp); $domain = '.'.$exp[1].'.'.$exp[0];}
  119. else{$domain = COOKIE_DOMAIN;}
  120. }
  121. elseif (!is_null($rootcookie_subdomain_manual))
  122. {
  123. $domain = $rootcookie_subdomain_manual;
  124. }
  125. else
  126. {
  127. $domain = COOKIE_DOMAIN;
  128. }
  129. setcookie(AUTH_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, $domain);
  130. setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, $domain);
  131. setcookie(AUTH_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, $domain);
  132. setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, $domain);
  133. setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, $domain);
  134. setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, $domain);
  135. // Old cookies
  136. setcookie(AUTH_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, $domain);
  137. setcookie(AUTH_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, $domain);
  138. setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, $domain);
  139. setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, $domain);
  140. // Even older cookies
  141. setcookie(USER_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, $domain);
  142. setcookie(PASS_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, $domain);
  143. setcookie(USER_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, $domain);
  144. setcookie(PASS_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, $domain);
  145. }
  146. endif;
  147. function rootcookie_activate ()
  148. {
  149. $opt_val = get_option('rootcookie_subdomain');
  150. if($opt_val!=1)
  151. {
  152. delete_option('rootcookie_subdomain');
  153. add_option('rootcookie_subdomain',0);
  154. }
  155. }
  156. function rootcookie_menu ()
  157. {
  158. add_options_page('root Cookie Options', 'root Cookie ', 8, __FILE__, 'rootcookie_options');
  159. }
  160. function rootcookie_options ()
  161. {
  162. // Read in existing option value from database
  163. $rootcookie_subdomain_on = get_option('rootcookie_subdomain');
  164. $rootcookie_subdomain_manual = get_option('rootcookie_subdomain_manual');
  165. $checked=false;
  166. if($rootcookie_subdomain_on==1){$checked=true;}
  167. // See if the user has posted us some information
  168. // If they did, this hidden field will be set to 'Y'
  169. if( $_POST['rootcookie_submit_hidden'] == 'Y' )
  170. {
  171. if(isset($_POST['rootcookie_subdomain']))
  172. {
  173. # This enables the guessing that the domain written by Scott
  174. $rootcookie_subdomain_on = 1;
  175. $checked=true;
  176. } else {
  177. $rootcookie_subdomain_on = 0;
  178. $checked=false;
  179. # Implement a manual domain method for .co.uk or .co.jp etc
  180. if(isset($_POST['rootcookie_subdomain_manual'])) {
  181. $rootcookie_subdomain_manual = $_POST['rootcookie_subdomain_manual'];
  182. update_option('rootcookie_subdomain_manual', $rootcookie_subdomain_manual );
  183. }
  184. }
  185. update_option('rootcookie_subdomain', $rootcookie_subdomain_on );
  186. echo '<div class="updated"><p><strong>'._('Options saved.').'</strong></p></div>';
  187. // Re-Read Val so Form Prints Correctly.
  188. $rootcookie_subdomain_on = get_option('rootcookie_subdomain');
  189. $rootcookie_subdomain_manual = get_option('rootcookie_subdomain_manual');
  190. }
  191. ?>
  192. <div class="wrap">
  193. <?php echo "<h2>" . __( 'root Cookie Options', 'rootcookie_trans_domain' ) . "</h2>"; ?>
  194. <form name="plugin_options" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>">
  195. <input type="hidden" name="rootcookie_submit_hidden" value="Y" />
  196. <p>
  197. <table class="form-table">
  198. <tr valign="top">
  199. <th scope="row"><?php _e("Allow Cookies to go across All Subdomains:", 'rootcookie_trans_domain' ); ?> </th>
  200. <td><input type="checkbox" name="rootcookie_subdomain" value="<?php echo $rootcookie_subdomain_on; ?>"<?php if($checked){echo " CHECKED";} ?> /><span class="description">Tick this box and we'll try to <b>guess</b> your domain and enable the cookie.</span></td>
  201. </tr>
  202. <?php
  203. if(!$checked){
  204. ?>
  205. <tr valign="top">
  206. <th scope="row">OR</th>
  207. <td><!-- ... --></td>
  208. </tr>
  209. <tr valign="top">
  210. <th scope="row"><?php _e('Domain Name') ?></th>
  211. <td><input name="rootcookie_subdomain_manual" id="rootcookie_subdomain_manual" class="regular-text" value="<?php echo $rootcookie_subdomain_manual; ?>" /><span class="description"><b>Optional:</b> Put you domain name in here example <code>linickx.co.uk</code> and we'll set the cookie to that.</span>
  212. </td>
  213. </tr>
  214. <?php
  215. }
  216. ?>
  217. </table>
  218. </p><hr />
  219. <p class="submit">
  220. <input type="submit" name="Submit" value="<?php _e('Update Options', 'rootcookie_trans_domain' ) ?>" />
  221. </p>
  222. </form>
  223. <?php
  224. # Let's tell users about RK :)
  225. $lnx_feed = fetch_feed('http://www.linickx.com/archives/tag/root-cookie/feed');
  226. echo "<h3>Root Cookie News &amp; Tutorials</h3>";
  227. echo "<ul>";
  228. foreach ($lnx_feed->get_items() as $item){
  229. printf('<li><a href="%s">%s</a></li>',$item->get_permalink(), $item->get_title());
  230. }
  231. echo "</ul>";
  232. ?>
  233. <p><small><a href="http://www.linickx.com/archives/tag/root-cookie/feed">Subcribe to this feed</a></small></p>
  234. </div>
  235. <?php
  236. }
  237. // Run all actions and hooks at the end to keep it tidy
  238. add_action('admin_menu', 'rootcookie_menu');
  239. register_activation_hook( __FILE__,'rootcookie_activate');
  240. ?>