PageRenderTime 498ms CodeModel.GetById 27ms RepoModel.GetById 3ms app.codeStats 0ms

/trunk/root-cookie.php

https://github.com/linickx/root-cookie
PHP | 355 lines | 245 code | 58 blank | 52 comment | 29 complexity | 80d4ba07741af88f749ea4d004a47b18 MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: root Cookie
  4. Plugin URI: http://www.linickx.com/3495/root-cookie-1-6-two-years-in-the-making
  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.6
  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
  31. */
  32. function wp_set_auth_cookie($user_id, $remember = false, $secure = '') {
  33. if ( $remember ) {
  34. $expiration = $expire = time() + apply_filters('auth_cookie_expiration', 1209600, $user_id, $remember);
  35. } else {
  36. $expiration = time() + apply_filters('auth_cookie_expiration', 172800, $user_id, $remember);
  37. $expire = 0;
  38. }
  39. if ( '' === $secure )
  40. $secure = is_ssl();
  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. setcookie($auth_cookie_name, $auth_cookie, $expire, ROOT_COOKIE, $domain, $secure, true);
  77. /** Duplicate of above - Created by Find & Replace
  78. setcookie($auth_cookie_name, $auth_cookie, $expire, ROOT_COOKIE, $domain, $secure, true);
  79. **/
  80. setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, ROOT_COOKIE, $domain, $secure_logged_in_cookie, true);
  81. if ( COOKIEPATH != SITECOOKIEPATH )
  82. setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true);
  83. }
  84. endif;
  85. if ( !function_exists('wp_clear_auth_cookie') ) :
  86. /**
  87. * Removes all of the cookies associated with authentication.
  88. *
  89. * @since 2.5
  90. */
  91. function wp_clear_auth_cookie() {
  92. do_action('clear_auth_cookie');
  93. $subdomain = get_option('rootcookie_subdomain');
  94. $rootcookie_subdomain_manual = get_option('rootcookie_subdomain_manual');
  95. # As ABOVE!
  96. if($subdomain==1)
  97. {
  98. $info = get_bloginfo('url');
  99. $info = parse_url($info);
  100. $info = $info['host'];
  101. $exp = explode('.',$info);
  102. if(count($exp)==3){$domain = '.'.$exp[1].'.'.$exp[2];}
  103. elseif(count($exp)==2){$domain = '.'.$info;}
  104. elseif(3<count($exp)){$exp = array_reverse($exp); $domain = '.'.$exp[1].'.'.$exp[0];}
  105. else{$domain = COOKIE_DOMAIN;}
  106. }
  107. elseif (!is_null($rootcookie_subdomain_manual))
  108. {
  109. $domain = $rootcookie_subdomain_manual;
  110. }
  111. else
  112. {
  113. $domain = COOKIE_DOMAIN;
  114. }
  115. /** Clear All possible cookies **/
  116. setcookie(AUTH_COOKIE, ' ', time() - 31536000, ADMIN_COOKIE_PATH, COOKIE_DOMAIN);
  117. setcookie(AUTH_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, COOKIE_DOMAIN);
  118. setcookie(AUTH_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, $domain);
  119. setcookie(AUTH_COOKIE, ' ', time() - 31536000, ADMIN_COOKIE_PATH, $domain);
  120. setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, ADMIN_COOKIE_PATH, COOKIE_DOMAIN);
  121. setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, COOKIE_DOMAIN);
  122. setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, $domain);
  123. setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, ADMIN_COOKIE_PATH, $domain);
  124. setcookie(AUTH_COOKIE, ' ', time() - 31536000, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN);
  125. setcookie(AUTH_COOKIE, ' ', time() - 31536000, PLUGINS_COOKIE_PATH, $domain);
  126. setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN);
  127. setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, PLUGINS_COOKIE_PATH, $domain);
  128. setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
  129. setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, COOKIE_DOMAIN);
  130. setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, $domain);
  131. setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, COOKIEPATH, $domain);
  132. setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
  133. setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, $domain);
  134. // Old cookies
  135. setcookie(AUTH_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
  136. setcookie(AUTH_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, COOKIE_DOMAIN);
  137. setcookie(AUTH_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, $domain);
  138. setcookie(AUTH_COOKIE, ' ', time() - 31536000, COOKIEPATH, $domain);
  139. setcookie(AUTH_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
  140. setcookie(AUTH_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, $domain);
  141. setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
  142. setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, COOKIE_DOMAIN);
  143. setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, $domain);
  144. setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, COOKIEPATH, $domain);
  145. setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
  146. setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, $domain);
  147. // Even older cookies
  148. setcookie(USER_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
  149. setcookie(USER_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, COOKIE_DOMAIN);
  150. setcookie(USER_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, $domain);
  151. setcookie(USER_COOKIE, ' ', time() - 31536000, COOKIEPATH, $domain);
  152. setcookie(PASS_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
  153. setcookie(PASS_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, COOKIE_DOMAIN);
  154. setcookie(PASS_COOKIE, ' ', time() - 31536000, ROOT_COOKIE, $domain);
  155. setcookie(PASS_COOKIE, ' ', time() - 31536000, COOKIEPATH, $domain);
  156. setcookie(USER_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
  157. setcookie(USER_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, $domain);
  158. setcookie(PASS_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
  159. setcookie(PASS_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, $domain);
  160. }
  161. endif;
  162. function rootcookie_activate ()
  163. {
  164. $opt_val = get_option('rootcookie_subdomain');
  165. if($opt_val!=1)
  166. {
  167. delete_option('rootcookie_subdomain');
  168. add_option('rootcookie_subdomain',0);
  169. }
  170. }
  171. function rootcookie_menu ()
  172. {
  173. global $rootcookie_admin_hook;
  174. $rootcookie_admin_hook = add_options_page('root Cookie Options', 'root Cookie ', 'manage_options' , 'root-cookie', 'rootcookie_options');
  175. }
  176. function rootcookie_menu_help($contextual_help, $screen_id, $screen)
  177. {
  178. global $rootcookie_admin_hook;
  179. if ($screen_id == $rootcookie_admin_hook) {
  180. $contextual_help = file_get_contents(WP_PLUGIN_DIR . '/root-cookie/admin-options-help.inc.php'); // the help html
  181. }
  182. return $contextual_help;
  183. }
  184. function rootcookie_options ()
  185. {
  186. // Read in existing option value from database
  187. $rootcookie_subdomain_on = get_option('rootcookie_subdomain');
  188. $rootcookie_subdomain_manual = get_option('rootcookie_subdomain_manual');
  189. $rootcookie_donate = get_option('rootcookie_donate');
  190. $checked=false;
  191. if($rootcookie_subdomain_on==1){$checked=true;}
  192. $donate=false;
  193. if($rootcookie_donate==1){$donate=true;}
  194. // See if the user has posted us some information
  195. // If they did, this hidden field will be set to 'Y'
  196. if( $_POST['rootcookie_submit_hidden'] == 'Y' )
  197. {
  198. if(isset($_POST['rootcookie_subdomain']))
  199. {
  200. # This enables the guessing that the domain written by Scott
  201. $rootcookie_subdomain_on = 1;
  202. $checked=true;
  203. } else {
  204. $rootcookie_subdomain_on = 0;
  205. $checked=false;
  206. # Implement a manual domain method for .co.uk or .co.jp etc
  207. if(isset($_POST['rootcookie_subdomain_manual'])) {
  208. $rootcookie_subdomain_manual = $_POST['rootcookie_subdomain_manual'];
  209. update_option('rootcookie_subdomain_manual', $rootcookie_subdomain_manual );
  210. }
  211. }
  212. if(isset($_POST['rootcookie_donate'])) {
  213. $rootcookie_donate = 1;
  214. $donate=true;
  215. update_option('rootcookie_donate', $rootcookie_donate );
  216. }
  217. update_option('rootcookie_subdomain', $rootcookie_subdomain_on );
  218. echo '<div class="updated"><p><strong>'._('Options saved.').'</strong></p></div>';
  219. // Re-Read Val so Form Prints Correctly.
  220. $rootcookie_subdomain_on = get_option('rootcookie_subdomain');
  221. $rootcookie_subdomain_manual = get_option('rootcookie_subdomain_manual');
  222. }
  223. ?>
  224. <div class="wrap">
  225. <?php echo "<h2>" . __( 'root Cookie Options', 'rootcookie_trans_domain' ) . "</h2>"; ?>
  226. <form name="plugin_options" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>">
  227. <input type="hidden" name="rootcookie_submit_hidden" value="Y" />
  228. <p>
  229. <table class="form-table">
  230. <tr valign="top">
  231. <th scope="row"><?php _e("Allow Cookies to go across All Subdomains:", 'rootcookie_trans_domain' ); ?> </th>
  232. <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>
  233. </tr>
  234. <?php
  235. if(!$checked){
  236. ?>
  237. <tr valign="top">
  238. <th scope="row">OR</th>
  239. <td><!-- ... --></td>
  240. </tr>
  241. <tr valign="top">
  242. <th scope="row"><?php _e('Domain Name') ?></th>
  243. <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>
  244. </td>
  245. </tr>
  246. <?php
  247. }
  248. ?>
  249. </table>
  250. </p><hr />
  251. <p class="submit">
  252. <input type="submit" name="Submit" value="<?php _e('Update Options', 'rootcookie_trans_domain' ) ?>" />
  253. </p>
  254. <?php
  255. if ( !$donate ) {
  256. ?>
  257. <div style="float:right; text-align:center" >
  258. <a href="http://www.linickx.com/donate">
  259. <img src="<?php echo plugins_url( 'root-cookie/donate.png' , dirname(__FILE__) )?>" alt="donate" /> <br />
  260. <small>Buy the author a beer to say thanks!</small>
  261. </a> <br />
  262. <small>
  263. <input type="checkbox" name="rootcookie_donate" value="1" ><em>Tick, yep done that!</em>
  264. </small>
  265. </div>
  266. <?php
  267. }
  268. ?>
  269. </form>
  270. <?php
  271. # Let's tell users about RK :)
  272. $lnx_feed = fetch_feed('http://www.linickx.com/tag/root-cookie/feed');
  273. echo "<h3>Root Cookie News</h3>";
  274. echo "<ul>";
  275. if (isset($lnx_feed->errors)) {
  276. echo '<li><b>Error Downloading Feed</b>. Looks like you are going to have to visit <a href="http://www.linickx.com/tag/root-cookie/feed">http://www.linickx.com/tag/root-cookie/feed</a> manually to keep up with the news! </li>';
  277. } else {
  278. foreach ($lnx_feed->get_items() as $item){
  279. printf('<li><a href="%s">%s</a></li>',$item->get_permalink(), $item->get_title());
  280. }
  281. }
  282. echo "</ul>";
  283. ?>
  284. <p><small><a href="http://www.linickx.com/archives/tag/root-cookie/feed">Subcribe to this feed</a></small></p>
  285. </div>
  286. <?php
  287. }
  288. // Run all actions and hooks at the end to keep it tidy
  289. if (is_admin()) { // only run admin stuff if we're an admin.
  290. add_action('admin_menu', 'rootcookie_menu');
  291. add_action('contextual_help', 'rootcookie_menu_help', 10, 3);
  292. }
  293. register_activation_hook( __FILE__,'rootcookie_activate');
  294. ?>