Advertisement
Guest User

WP 3.1 firestats dirty fix

a guest
Mar 5th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 41.10 KB | None | 0 0
  1. <?php
  2. /*
  3.  Plugin Name: FireStats
  4.  Plugin URI: http://firestats.cc
  5.  Description: Statistics plugin for WordPress.
  6.  Version: 1.6.7-stable
  7.  Author: Omry Yadan
  8.  Author URI: http://blog.firestats.cc
  9.  */
  10.  
  11. // this is an version of the FireStats this file is a part of.
  12. // it's used by firestats core to detect if the correct file is installed.
  13. // (there can in be inconsistencies in case of a satelitte installation).
  14. define('FS_WORDPRESS_PLUGIN_VER','1.6.7-stable');
  15.  
  16. /**
  17.  * Initialize FireStats callbacks in any case
  18.  */
  19. fs_initialize_fs_callbacks();
  20.  
  21.  
  22. // This bit pretends to be the any file inside firestats path
  23. // so the browser will allow the js code to send the ajax request.
  24. if (isset($_GET['fs_javascript']))
  25. {
  26.     add_action('init','fs_resume_js_call');
  27. }
  28.  
  29. function fs_resume_js_call()
  30. {
  31.     $path = fs_get_firestats_path();
  32.     $file = $_GET['fs_javascript'];
  33.     unset($_GET['fs_javascript']);
  34.    
  35.     $allowed_files = array
  36.     (
  37.         'php/ajax-handler.php',
  38.         'php/window-donation.php',
  39.         'css/base.css.php',
  40.         'js/page-settings.js.php',
  41.         'php/page-settings.php',
  42.         'js/page-wordpress-settings.js.php',
  43.         'php/page-wordpress-settings.php',
  44.         'js/page-database.js.php',
  45.         'php/page-database.php',
  46.         'js/page-users.js.php',
  47.         'css/page-users.css.php',
  48.         'php/page-users.php',
  49.         'js/page-sites.js.php',
  50.         'css/page-sites.css.php',
  51.         'php/page-sites.php',
  52.         'php/page-tools.php',
  53.         'php/window-add-excluded-url.php',
  54.         'php/window-add-excluded-ip.php',
  55.         'php/window-delete-site.php',
  56.         'php/window-new-edit-site.php',
  57.         'php/window-edit-user.php',
  58.         'php/window-delete-user.php',
  59.         'php/tools/system_test.php',
  60.         'js/firestats.js.php'
  61.     );
  62.    
  63.    
  64.     $found = false;
  65.    
  66.     foreach ($allowed_files as $a)
  67.     {
  68.         if ($file == $a)
  69.         {
  70.             $found = true;
  71.             break;
  72.         }
  73.     }
  74.    
  75.     if (!$found) die("invalid fs_javascript");
  76.        
  77.     require_once("$path/$file");
  78.     die();
  79. }
  80.  
  81. global $FS_CONTEXT;
  82. $FS_CONTEXT = array();
  83. $FS_CONTEXT['TYPE'] = 'WORDPRESS';
  84. $FS_CONTEXT['WP_PATH'] = fs_get_wp_config_path();
  85. $FS_CONTEXT['JAVASCRIPT_URL'] = fs_get_js_url();
  86.  
  87. /**
  88.  * Only initialize the plugin if not indirectly invoked from ajax plugin
  89.  * to avoid wierd order of initialization problems caused by various wordpress versions.
  90.  */
  91. if (!defined('FS_AJAX_HANDLER'))
  92. {
  93.     $is_wpmu = fs_is_wpmu();
  94.     fs_initialize_wp_plugin($is_wpmu);
  95. }
  96.  
  97. function fs_initialize_fs_callbacks()
  98. {
  99.     $FS_PATH = fs_get_firestats_path();
  100.     if ($FS_PATH)
  101.     {
  102.         // if WPLANG is defined, set FireStats's default language to it.
  103.         if(defined('WPLANG') && !defined('FS_DEFAULT_LANG')) define('FS_DEFAULT_LANG',WPLANG);
  104.        
  105.         require_once($FS_PATH.'/php/init.php');
  106.         fs_add_action("db_upgraded", "fs_plugin_db_update");
  107.         fs_add_action("authenticated", "fs_register_dashboard_widgets");
  108.     }
  109. }
  110.  
  111. function fs_initialize_wp_plugin($is_wpmu)
  112. {
  113.     if (!function_exists("add_action")) return;
  114.  
  115.     add_action('wp_head', 'fs_add_wordpress', 1);
  116.     add_action('admin_footer', 'fs_admin_footer');
  117.     add_action('admin_menu', 'fs_add_page');
  118.     add_action('admin_head', 'fs_admin_head');
  119.     add_action('widgets_init', 'fs_widget_init');
  120.     add_action('publish_post', 'fs_update_post_title');
  121.     add_action('edit_post', 'fs_update_post_title');
  122.     add_action('after_plugin_row','fs_new_version_nag');
  123.     if (!function_exists('wp_add_dashboard_widget')) add_filter('wp_dashboard_widgets', 'fs_add_dashboard_widgets');
  124.  
  125.     // wpmu
  126.     if ($is_wpmu)
  127.     {
  128.         add_action('delete_blog','fs_delete_wpmu_blog');
  129.     }
  130.  
  131.     global $wp_version;
  132.     if (!fs_is_wpmu() && version_compare($wp_version,"2.0") == -1) // wordpress is older than 2.0
  133.     {
  134.         $activated = (basename($_SERVER['SCRIPT_NAME']) == 'plugins.php' && isset($_GET['activate']));
  135.         if ($activated) fs_activate();
  136.     }
  137.     else
  138.     {
  139.         register_activation_hook(__FILE__, 'fs_activate');
  140.     }
  141.  
  142.  
  143.     if (get_option('firestats_add_comment_flag') == 'true')
  144.     {
  145.         add_filter('get_comment_author_link', 'fs_add_comment_flag', 100);
  146.     }
  147.  
  148.     if (get_option('firestats_add_comment_browser_os') == 'true')
  149.     {
  150.         add_filter('get_comment_author_link', 'fs_add_comment_browser_os', 100);
  151.     }
  152.  
  153.     $FS_PATH = fs_get_firestats_path();
  154.     if ($FS_PATH) // needed when installing in satellite mode
  155.     {
  156.         require_once($FS_PATH.'/php/api.php');
  157.     }
  158.  
  159.     // show footer by default
  160.     $show_footer = get_option('firestats_show_footer');
  161.     if ((empty($show_footer) && DEFAULT_SHOW_FIRESTATS_FOOTER) || $show_footer == "true")
  162.     {
  163.         add_action('wp_footer','fs_echo_footer');
  164.     }
  165.  
  166.     $FS_PATH = fs_get_firestats_path();
  167.     if ($FS_PATH)
  168.     {
  169.         // only relevant if we know FS path.
  170.         // otherwise fs_do_action is not in the scope.
  171.         fs_do_action("wordpress_plugin_initialized");
  172.     }
  173. }
  174.  
  175.  
  176. function fs_activate()
  177. {
  178.     $FS_PATH = fs_get_firestats_path();
  179.     if (!$FS_PATH)
  180.     {
  181.         return;
  182.     }
  183.  
  184.     require_once($FS_PATH.'/php/db-setup.php');
  185.     $res = fs_install();
  186.     if ($res !== true)
  187.     {
  188.         $plugin_name = substr(__FILE__, strlen(ABSPATH . PLUGINDIR . '/'));
  189.         deactivate_plugins($plugin_name);
  190.         wp_die(sprintf("Error installing FireStats tables: %s",$res));
  191.         return;
  192.     }
  193.  
  194.     $res = fs_register_wordpress();
  195.     if ($res !== true)
  196.     {
  197.         wp_die($res);
  198.         return;
  199.     }
  200.     fs_update_post_titles();
  201.  
  202. }
  203.  
  204. function fs_update_post_title($id)
  205. {
  206.     $post = &get_post($id);
  207.     if ($post->post_type == 'post' || $post->post_type == 'page')
  208.     {
  209.         $FS_PATH = fs_get_firestats_path();
  210.         $site_id = get_option('firestats_site_id');
  211.         if (!$FS_PATH)
  212.         {
  213.             return;
  214.         }
  215.         require_once($FS_PATH.'/php/db-sql.php');
  216.  
  217.  
  218.         $title = get_the_title($id);
  219.         if (empty($title))
  220.         {
  221.             return;
  222.         }
  223.         $link = get_permalink($id);
  224.         if (empty($link))
  225.         {
  226.             return;
  227.         }
  228.  
  229.         // make sure the url exists in the urls table.;
  230.         $res = fs_insert_url($link, $site_id);
  231.  
  232.         if (!$res)
  233.         {
  234.             echo $res;
  235.             return;
  236.         }
  237.  
  238.         // replace title with current one
  239.         fs_set_url_title($link,$title);
  240.  
  241.         // mark url as a post
  242.         if ($post->post_type == 'post')
  243.         {
  244.             fs_set_url_type($link,FS_URL_TYPE_POST);
  245.         }
  246.     }
  247.  
  248. }
  249.  
  250. /**
  251.  * Registers this instance of wordpress with FireStats.
  252.  * This is requires so that if there is more than one blog/system that works with
  253.  * the same FireStats instance it will be possible to filter the stats per site.
  254.  */
  255. function fs_register_wordpress()
  256. {
  257.     $FS_PATH = fs_get_firestats_path();
  258.     if (!$FS_PATH) return true;
  259.     require_once($FS_PATH.'/php/db-sql.php');
  260.  
  261.  
  262.     $firestats_site_id = get_option('firestats_site_id');
  263.     if ($firestats_site_id == null && fs_is_wpmu())
  264.     {
  265.         // for wpmu sites, use the blog id as the firestats site id.
  266.         global $blog_id;
  267.         $firestats_site_id = $blog_id;
  268.     }
  269.  
  270.     $site_exists = fs_site_exists($firestats_site_id);
  271.     if (is_string($site_exists)) return $site_exists;
  272.     if (!$site_exists)
  273.     {
  274.         $firestats_site_id = fs_register_site($firestats_site_id);
  275.         if (!is_numeric($firestats_site_id))
  276.         {
  277.             $site_info = ($site_id != null ? "(site_id=$firestats_site_id)" : "(new site)");
  278.             return "FireStats: error registering blog ".$site_info . " : $firestats_site_id";
  279.         }
  280.         update_option('firestats_site_id',$firestats_site_id);
  281.     }
  282.  
  283.     $name = get_option('blogname');
  284.     if (empty($name))
  285.     {
  286.         $name = "Unamed blog (#$firestats_site_id)";
  287.     }
  288.    
  289.     $type = FS_SITE_TYPE_WORDPRESS;
  290.  
  291.     $res = fs_update_site_params($firestats_site_id,$firestats_site_id, $name, $type);
  292.     if ($res !== true)
  293.     {
  294.         return $res;
  295.     }
  296.  
  297.     // update the filter to show us this blog by default after the installation
  298.     update_option('firestats_sites_filter',$firestats_site_id);
  299.     return true;
  300. }
  301.  
  302.  
  303.  
  304. function fs_is_wpmu()
  305. {
  306.     return file_exists(ABSPATH."/wpmu-settings.php");
  307. }
  308.  
  309. function fs_full_installation()
  310. {
  311.     return file_exists(dirname(__FILE__).'/php/db-hit.php');
  312. }
  313.  
  314. function fs_override_base_url()
  315. {
  316.     if (fs_full_installation())
  317.     {
  318.         $site_url = get_option("siteurl");
  319.  
  320.         // make sure the url ends with /
  321.         $last = substr($site_url, strlen( $site_url ) - 1 );
  322.         if ($last != "/") $site_url .= "/";
  323.  
  324.         // calculate base url based on current directory.
  325.         $base_len = strlen(ABSPATH);
  326.         $suffix = substr(dirname(__FILE__),$base_len)."/";
  327.         // fix windows path sperator to url path seperator.
  328.         $suffix = str_replace("\\","/",$suffix);
  329.         $base_url = $site_url . $suffix;
  330.         return $base_url;
  331.     }
  332.     else // not full installation == satelite of a standlone firestats.
  333.     {
  334.         if (fs_is_wpmu())
  335.         {
  336.             $url = get_site_option('firestats_url');
  337.         }
  338.         else
  339.         {
  340.             $url = get_option('firestats_url');
  341.         }
  342.         // make sure the url ends with /
  343.         $last = substr($url, strlen( $url ) - 1 );
  344.         if ($last != "/") $url .= "/";
  345.         return $url;
  346.     }
  347. }
  348.  
  349. function fs_get_firestats_path($reset = false)
  350. {
  351.     static $path;
  352.     if (!isset($path) || $reset)
  353.     {
  354.         $path = fs_get_firestats_path_impl();
  355.     }
  356.  
  357.     if (file_exists($path.'/firestats.info')) return $path;
  358.     else return false;
  359. }
  360.  
  361. function fs_get_firestats_path_impl()
  362. {
  363.     if (!fs_full_installation())
  364.     {
  365.         if (fs_is_wpmu())
  366.         {
  367.             $path = get_site_option('firestats_path');
  368.         }
  369.         else
  370.         {
  371.             $path = get_option('firestats_path');
  372.         }
  373.         if ($path == null || $path == '')
  374.         {
  375.             return false;
  376.         }
  377.         else
  378.         {
  379.             return $path;
  380.         }
  381.     }
  382.     else
  383.     {
  384.         return dirname(__FILE__);
  385.     }
  386. }
  387.  
  388. # Small info on DashBoard-page
  389. function fs_admin_footer()
  390. {
  391.     $admin = dirname($_SERVER['SCRIPT_FILENAME']);
  392.     $admin = substr($admin, strrpos($admin, '/')+1);
  393.     $query = $_SERVER["QUERY_STRING"];
  394.     if ($admin == 'wp-admin' && basename($_SERVER['SCRIPT_FILENAME']) == 'index.php' && $query == '')
  395.     {
  396.         $FS_PATH = fs_get_firestats_path();
  397.         if (!$FS_PATH) return;
  398.         require_once($FS_PATH.'/php/auth.php');
  399.         if (!fs_can_use()) return;
  400.  
  401.         require_once($FS_PATH.'/php/db-sql.php');
  402.         $url = fs_get_firestats_url();
  403.         $title = "<h3>".fs_r("FireStats"). $url."</h3><span id ='firestats_span'>".fs_r('Loading...')."</span>";
  404.         print
  405.             '<script language="javascript" type="text/javascript">
  406.                 var e = document.getElementById("zeitgeist");
  407.                 if (e)
  408.                 {
  409.                     var div = document.createElement("DIV");
  410.                     div.id = div.innerHTML = "'.$title.'";
  411.                     e.appendChild(div);
  412.                 }
  413.             </script> ';
  414.         flush();
  415.  
  416.         $count = fs_get_hit_count();
  417.         $unique = fs_get_unique_hit_count();
  418.         $last_24h_count= fs_get_hit_count(1);
  419.         $last_24h_unique = fs_get_unique_hit_count(1);
  420.  
  421.         echo "<!-- admin = $admin, script =  ".basename($_SERVER['SCRIPT_FILENAME'])."  -->";
  422.         $content.= sprintf(fs_r("Total : %s page views and %s visits"),'<strong>'.$count.'</strong>','<strong>'.$unique.'</strong>').'<br/>';
  423.         $content.= sprintf(fs_r("Last 24 hours : %s page views and %s visits"),'<strong>'.$last_24h_count.'</strong>','<strong>'.$last_24h_unique.'</strong>').'<br/>';
  424.         print
  425.         '<script language="javascript" type="text/javascript">
  426.             var e = document.getElementById("firestats_span");
  427.             if (e)
  428.             {
  429.                 e.innerHTML = "'.$content.'";
  430.             }
  431.             </script> ';
  432.     }
  433. }
  434.  
  435. function fs_get_firestats_url($txt = null)
  436. {
  437.     $txt = $txt ? $txt  : "&raquo;";
  438.     if (fs_full_installation())
  439.     {
  440.         $plugin_dir = substr(dirname(__FILE__), strlen(ABSPATH . PLUGINDIR . '/'));
  441.         // hack around stupid wp bug under windows
  442.         if (fs_is_windows())
  443.         {
  444.             $link = "index.php?page=$plugin_dir%5C" . basename(__FILE__);
  445.  
  446.         }
  447.         else
  448.         {
  449.             $link = "index.php?page=$plugin_dir/" . basename(__FILE__);
  450.         }
  451.         $url = "<a href='$link'>$txt</a>";
  452.     }
  453.     else
  454.     {
  455.         $file = __FILE__;
  456.         $url = "<a href='index.php?page=$file'>$txt</a>";
  457.     }
  458.     return $url;
  459. }
  460.  
  461. function fs_authenticate_wp_user()
  462. {
  463.     // use wordpress users only when installed in hosted mode
  464.     if (fs_full_installation())
  465.     {
  466.         global $current_user;
  467.         $path = fs_get_firestats_path();
  468.         require_once($path.'/php/auth.php');
  469.         $user = new stdClass();
  470.         $user->name = $current_user->user_login;
  471.         $user->id = $current_user->id;
  472. //      if (fs_is_wpmu())
  473. //      {
  474.             if (is_super_admin())
  475.             {
  476.                 $user->security_level = SEC_ADMIN;
  477.             }
  478.             else
  479.             {
  480.                 $user->security_level = current_user_can('publish_posts') ? SEC_USER : SEC_NONE;
  481.             }
  482. /*      }
  483.         else
  484.         {
  485.             if (current_user_can('manage_options'))
  486.             {
  487.                 $user->security_level = SEC_ADMIN;
  488.             }
  489.             else
  490.             {
  491.                 if (current_user_can('moderate_comments')) // editor
  492.                 {
  493.                     $user_level = 4;
  494.                 }
  495.                 else
  496.                 if (current_user_can('publish_posts')) // author
  497.                 {
  498.                     $user_level = 3;
  499.                 }
  500.                 else
  501.                 if (current_user_can('edit_posts')) // contributor
  502.                 {
  503.                     $user_level = 2;
  504.                 }
  505.                 else
  506.                 if (current_user_can('read')) // subscriber
  507.                 {
  508.                     $user_level = 1;
  509.                 }
  510.                 else
  511.                 {
  512.                     $user_level = 0; // a bumhug
  513.                 }
  514.  
  515.                 $required = (int)fs_get_local_option('firestats_min_view_security_level',3);
  516.                 $user->security_level = $required <= $user_level ? SEC_USER : SEC_NONE;
  517.             }
  518.         }
  519. */
  520.         // this mark the user as a user that can access all the sites.
  521.         // this is a little hack for WordPress in hosted installation, where users are not real FireStats users
  522.         // and it makes no sense to add an entry in the user_sites table for each user.
  523.         $user->check_user_sites_table = false;
  524.  
  525.         fs_start_user_session($user);
  526.     }
  527.     else
  528.     {
  529.         fs_resume_user_session();
  530.         if (!fs_authenticated())
  531.         {
  532.             fs_start_user_session(null); // dummy session that can only be used to login.
  533.         }
  534.     }
  535. }
  536.  
  537. function fs_page()
  538. {
  539.     $path = fs_get_firestats_path();
  540.     if ($path)
  541.     {
  542.         if (!fs_full_installation()) // satellite installation
  543.         {
  544.             if (defined('DEMO'))
  545.             {
  546.                 $user = new stdClass();
  547.                 $user->name = "Demo";
  548.                 $user->id = 1;
  549.                 $user->security_level = SEC_USER;
  550.                 $res = fs_start_user_session($user);
  551.             }
  552.             else
  553.             {
  554.                 $res = fs_resume_user_session();
  555.             }
  556.  
  557.             if ($res === true)
  558.             {
  559.                 fs_show_embedded_page($path.'/php/tabbed-pane.php');
  560.                 return;
  561.             }
  562.             else
  563.             {
  564.                 fs_show_embedded_page($path.'/login.php', true, true, true);
  565.             }
  566.         }
  567.         else // full installation
  568.         {
  569.             if (fs_can_use() === true)
  570.             {
  571.                 fs_show_embedded_page($path.'/php/tabbed-pane.php');
  572.                 return;
  573.             }
  574.             else
  575.             {
  576.                 $msg = fs_r("You are not authorized to access FireStats");
  577.                 $msg = "<div class='error'>$msg</div>";
  578.  
  579.                 fs_show_embedded_page($msg,false);
  580.             }
  581.         }
  582.     }
  583.     else
  584.     {
  585.         $href = sprintf("<a href='options-general.php?page=FireStats'>%s</a>",__('Options'));
  586.         echo '<div class="error" style="margin-top:40px;margin-bottom:40px">'.sprintf(__('You need to configure FireStats in the %s menu'),$href).'</div>';
  587.     }
  588. }
  589.  
  590. function fs_endsWith( $str, $sub ) {
  591.     return ( substr( $str, strlen( $str ) - strlen( $sub ) ) === $sub );
  592. }
  593.  
  594. function fs_admin_head()
  595. {
  596.     $FS_PATH = fs_get_firestats_path();
  597.     if ($FS_PATH)
  598.     {
  599.         if (FS_WORDPRESS_PLUGIN_VER != FS_VERSION)
  600.         {
  601.             echo "Version mismatch between FireStats plugin (".FS_WORDPRESS_PLUGIN_VER.") and FireStats installation (".FS_VERSION.")";
  602.             return;
  603.         }
  604.  
  605.         fs_authenticate_wp_user();
  606.         if (strstr($_SERVER["QUERY_STRING"],basename(__FILE__)) !== false)
  607.         {
  608.             require_once($FS_PATH.'/php/html-utils.php');
  609.             fs_output_head();
  610.         }
  611.     }
  612. }
  613.  
  614. function fs_add_wordpress()
  615. {
  616.     $FS_PATH = fs_get_firestats_path();
  617.     if (!$FS_PATH) return;
  618.     if (is_404()) return; // don't log 404.
  619.     require_once($FS_PATH.'/php/db-hit.php');
  620.     $firestats_site_id = get_option('firestats_site_id');
  621.     // extract user ID in a wordpress specific method.
  622.     global $user_ID;
  623.     get_currentuserinfo();
  624.  
  625.     $excluded_users = get_option('firestats_excluded_users');
  626.     $excluded_user = $user_ID && $excluded_users && in_array($user_ID,explode(",",$excluded_users));
  627.     if (!$excluded_user)
  628.     {
  629.         // WP likes to add slashes to the useragent, which causes problems.
  630.         $_SERVER['HTTP_USER_AGENT'] = stripslashes($_SERVER['HTTP_USER_AGENT']);
  631.         $_SERVER['REQUEST_URI'] = stripslashes($_SERVER['REQUEST_URI']);
  632.         $_SERVER['HTTP_REFERER'] = stripslashes($_SERVER['HTTP_REFERER']);
  633.        
  634.         fs_add_site_hit($firestats_site_id,false);
  635.     }
  636. }
  637.  
  638. # Add a sub-menu to the "manage"-page.
  639. function fs_add_page()
  640. {
  641.     add_submenu_page('index.php', 'FireStats', 'FireStats', 1, __FILE__, 'fs_page');
  642.     if (!fs_full_installation())
  643.     {
  644.         if (!fs_is_wpmu() || is_super_admin())
  645.         {
  646.             add_submenu_page('options-general.php', 'FireStats', 'FireStats', 8, 'FireStats', 'fs_options_page');
  647.         }
  648.     }
  649. }
  650.  
  651.  
  652. function fs_options_page()
  653. {
  654.     if (isset($_POST['action']) && $_POST['action'] == 'update')
  655.     {
  656.         update_option('firestats_path',$_POST['firestats_path']);
  657.         update_option('firestats_url',$_POST['firestats_url']);
  658.     }
  659.     $path = get_option('firestats_path');
  660.     $url = get_option('firestats_url');
  661.     ?>
  662. <div class="fwrap">
  663. <h2><?php _e('FireStats configuration')?></h2>
  664. <form method="post"><input type="hidden" name="action" value="update" />
  665.     <?php
  666.     $path_good = false;
  667.     $url_good = false;
  668.     $path_version = '';
  669.     $url_version = '';
  670.     if (ini_get('safe_mode') == 1)
  671.     {
  672.         ?>
  673. <div class="error"><?php _e("Your PHP is configured in safe mode, FireStats may be impossible to configure in satellite mode.")?></div>
  674.         <?php
  675.     }
  676.  
  677.     if (!empty($path))
  678.     {
  679.         $len = strlen($path);
  680.         if ($path[$len - 1] != '/' && $path[$len - 1] != '\\')
  681.         {
  682.             $path .= '/';
  683.         }
  684.  
  685.         if (file_exists($path.'firestats.info'))
  686.         {?>
  687. <div class="updated fade"><?php
  688. echo sprintf(__("FireStats detected at %s"),"<b>$path</b>").'<br/>';
  689. $path_good = true;
  690. $info = file($path.'firestats.info');
  691. $path_version = $info[0];
  692. ?></div>
  693. <?php
  694.         }
  695.         else
  696.         {?>
  697. <div class="error"><?php echo sprintf(__("FireStats was not found at %s"),"<b>$path</b>")?></div>
  698.         <?php
  699.         }
  700.     }
  701.     else
  702.     {
  703.         echo '<div class="error">'.__("Enter the directory that contains FireStats").'</div>';
  704.     }
  705.  
  706.     if (!empty($url))
  707.     {
  708.         ob_start();
  709.         $file = file($url.'/firestats.info');
  710.         $output = ob_get_clean();
  711.         if ($file !== false)
  712.         {?>
  713. <div class="updated fade"><?php
  714. echo sprintf(__("FireStats detected at %s"),"<b>$url</b>").'<br/>';
  715. $url_good = true;
  716. $url_version = $file[0];
  717. ?></div>
  718. <?php
  719.         }
  720.         else
  721.         {
  722.             if ($output != '')
  723.             {
  724.                 $msg = sprintf(__("Error : %s"),"<b>$output</b>");
  725.             }
  726.             else
  727.             {
  728.                 $msg = sprintf(__("FireStats was not found at %s"),"<b>$url</b>");
  729.             }
  730.             {?>
  731. <div class="error"><?php echo $msg?></div>
  732.             <?php
  733.             }
  734.         }
  735.     }
  736.     else
  737.     {
  738.         echo '<div class="error">'. __("Enter FireStats url").'</div>';
  739.     }
  740.  
  741.     if ($path_good && $url_good)
  742.     {
  743.         fs_get_firestats_path(true); // reset cached path.
  744.  
  745.         $plugin_ver = "FireStats/".FS_WORDPRESS_PLUGIN_VER;
  746.         if (trim($plugin_ver) != trim($path_version))
  747.         {
  748.             ?>
  749. <div class="error"><?php echo sprintf(__("Version mismatch between %s (%s) and FireStats at path (%s)"),basename(__FILE__),$plugin_ver, $path_version)?></div>
  750.             <?php
  751.         }
  752.         else
  753.         {
  754.             $res = fs_register_wordpress();
  755.             if ($res !== true)
  756.             {
  757.                 echo "<div class='error'>$res</div>";
  758.             }
  759.             else
  760.             {
  761.                 fs_update_post_titles();
  762.                 echo '<div class="updated fade">'.sprintf(__('Everything is okay, click %s to open %s'),'<b>'.fs_get_firestats_url('here').'</b>',"$path_version").'</div>';
  763.             }
  764.  
  765.         }
  766.     }
  767.     ?>
  768. <table>
  769.     <tr>
  770.         <td><?php _e('FireStats path : ')?></td>
  771.         <td><input type="text" class="code" id="firestats_path"
  772.             name="firestats_path" size="60" value="<?php echo $path?>" /> <?php echo __('Example:')."<b>".$_SERVER["DOCUMENT_ROOT"]."firestats/</b>"?></td>
  773.     </tr>
  774.     <tr>
  775.         <td><?php _e('FireStats URL : ')?></td>
  776.         <td><input type="text" class="code" id="firestats_url"
  777.             name="firestats_url" size="60" value="<?php echo $url?>" /> <?php _e('Example: <b>http://your_site.com/firestats</b>')?>
  778.         </td>
  779.     </tr>
  780. </table>
  781.  
  782. <p class="submit"><input type="submit" name="Submit"
  783.     value="<?php _e('Update options')?>&raquo;" /></p>
  784. </form>
  785. </div>
  786.     <?php
  787. }
  788.  
  789. function fs_widget_init()
  790. {
  791.     // Check for the required plugin functions. This will prevent fatal
  792.     // errors occurring when you deactivate the dynamic-sidebar plugin.
  793.     if ( !function_exists('register_sidebar_widget') )
  794.         return;
  795.  
  796.     $FS_PATH = fs_get_firestats_path();
  797.     $configured = true;
  798.     if ($FS_PATH)
  799.     {
  800.         global $wp_version;
  801.         if ($wp_version != "2.5") // 2.5 displayed actual img tag instead of image. fixed in 2.5.1
  802.         {
  803.             require_once($FS_PATH.'/php/utils.php');
  804.             $img = fs_url("img/firestats-icon-small.png");
  805.             $name = "<img alt='FireStats icon' src='$img'/>&nbsp;%s";
  806.         }
  807.         else
  808.         {
  809.             $name = "%s";
  810.         }
  811.     }
  812.     else
  813.     {
  814.         $name = "%s (FireStats is not configured)";
  815.         $configured  = false;
  816.     }
  817.     // not translated due to a bug in wordpress that prevents saving of
  818.     // widgets with non ascii characters in their name.
  819.     $stats_name = sprintf($name,"Statistics");
  820.     $popular_name = sprintf($name,"Popular posts");
  821.     $rss_name = sprintf($name,"RSS subscribers");
  822.  
  823.     // stats widget
  824.     register_sidebar_widget($stats_name, 'fs_widget');
  825.     if ($configured) register_widget_control($stats_name, 'fs_widget_control', 300, 100);
  826.  
  827.     // popular pages widget
  828.     register_sidebar_widget($popular_name, 'fs_popular_pages_widget');
  829.     if ($configured) register_widget_control($popular_name, 'fs_popular_pages_widget_control', 400, 200);
  830.  
  831.     // rss subscribers
  832.     register_sidebar_widget($rss_name, 'fs_rss_subscribers_widget');
  833.     if ($configured) register_widget_control($rss_name, 'fs_rss_subscribers_controll', 300, 100);
  834. }
  835.  
  836.  
  837. function fs_rss_subscribers_widget($args)
  838. {
  839.     // $args is an array of strings that help widgets to conform to
  840.     // the active theme: before_widget, before_title, after_widget,
  841.     // and after_title are the array keys. Default tags: li and h2.
  842.     extract($args);
  843.  
  844.     // Each widget can store its own options. We keep strings here.
  845.     $options = get_option('firestats_rss_subscribers_widget');
  846.     $title = $options['title3'];
  847.     $hide_logo = $options['hide_logo3'];
  848.  
  849.     // These lines generate our output. Widgets can be very complex
  850.     // but as you can see here, they can also be very, very simple.
  851.     echo $before_widget . $before_title . $title . $after_title;
  852.     if (fs_plugin_installed("RSS Subscribers"))
  853.     {
  854.         echo fs_get_rss_subscribers_box($hide_logo);
  855.     }
  856.     else
  857.     {
  858.         $FS_PATH = fs_get_firestats_path();
  859.         if ($FS_PATH)
  860.         {
  861.             require_once($FS_PATH.'/php/html-utils.php');
  862.             echo fs_create_wiki_help_link("FireStatsPRO");
  863.         }
  864.     }
  865.     echo $after_widget;
  866. }
  867.  
  868. function fs_rss_subscribers_controll()
  869. {
  870.     // Get our options and see if we're handling a form submission.
  871.     $options = get_option('firestats_rss_subscribers_widget');
  872.  
  873.     if (empty($options))
  874.     {
  875.         $options = array('title3'=>fs_r('FireStats'),'hide_logo3'=>false);
  876.     }
  877.  
  878.     if ( $_POST['firestats-rss-subscribers-widget-submit'])
  879.     {
  880.         $hide_logo = $_POST['hide_logo3'] == 'on';
  881.         // Remember to sanitize and format use input appropriately.
  882.         $options['title3'] = strip_tags(stripslashes($_POST['firestats-title3']));
  883.         $options['hide_logo3'] = $hide_logo;
  884.         update_option('firestats_rss_subscribers_widget', $options);
  885.     }
  886.    
  887.     // Be sure you format your options to be valid HTML attributes.
  888.     $title = htmlspecialchars($options['title3'], ENT_QUOTES);
  889.     $hide_logo = $options['hide_logo3'];
  890.  
  891.     // Here is our little form segment. Notice that we don't need a
  892.     // complete form. This will be embedded into the existing form.
  893.     ?>
  894. <table>
  895.     <tr>
  896.         <td><label for="firestats-title3"><?php fs_e('Title:')?></label></td>
  897.         <td><input style="width: 200px;" id="firestats-title3"
  898.             name="firestats-title3" type="text" value="<?php echo $title?>" /></td>
  899.     </tr>
  900.     <tr>
  901.         <td colspan="2"><input id="hide_logo3" name="hide_logo3" type="checkbox"
  902.         <?php echo $hide_logo ? "checked='checked'" : ""?> /> <label
  903.             for="hide_logo3"><?php fs_e('Hide FireStats logo')?></label></td>
  904.     </tr>
  905. </table>
  906. <input
  907.     type="hidden" id="firestats-rss-subscribers-widget-submit"
  908.     name="firestats-rss-subscribers-widget-submit" value="1" />
  909.     <?php
  910. }
  911.  
  912.  
  913. function fs_widget($args)
  914. {
  915.     // $args is an array of strings that help widgets to conform to
  916.     // the active theme: before_widget, before_title, after_widget,
  917.     // and after_title are the array keys. Default tags: li and h2.
  918.     extract($args);
  919.  
  920.     // Each widget can store its own options. We keep strings here.
  921.     $options = get_option('widget_firestats');
  922.     $title = $options['title'];
  923.     $hide_logo = $options['hide_logo'];
  924.  
  925.     // These lines generate our output. Widgets can be very complex
  926.     // but as you can see here, they can also be very, very simple.
  927.     echo $before_widget . $before_title . $title . $after_title;
  928.     echo fs_get_stats_box($hide_logo);
  929.     echo $after_widget;
  930. }
  931.  
  932. function fs_widget_control()
  933. {
  934.     // Get our options and see if we're handling a form submission.
  935.     $options = get_option('widget_firestats');
  936.  
  937.     if (empty($options))
  938.     {
  939.         $options = array('title'=>fs_r('FireStats'), 'hide_logo'=>false);
  940.     }
  941.  
  942.     if ( $_POST['firestats-stats-widget-submit'])
  943.     {
  944.         $hide_logo = $_POST['hide_logo'] == 'on';
  945.         // Remember to sanitize and format use input appropriately.
  946.         $options['title'] = strip_tags(stripslashes($_POST['firestats-title']));
  947.         $options['hide_logo'] = $hide_logo;
  948.         update_option('widget_firestats', $options);
  949.     }
  950.  
  951.     // Be sure you format your options to be valid HTML attributes.
  952.     $title = htmlspecialchars($options['title'], ENT_QUOTES);
  953.     $hide_logo = $options['hide_logo'];
  954.  
  955.     // Here is our little form segment. Notice that we don't need a
  956.     // complete form. This will be embedded into the existing form.
  957.     ?>
  958. <table>
  959.     <tr>
  960.         <td><label for="firestats-title2"><?php fs_e('Title:')?></label></td>
  961.         <td><input style="width: 200px;" id="firestats-title"
  962.             name="firestats-title" type="text" value="<?php echo $title?>" /></td>
  963.     </tr>
  964.     <tr>
  965.         <td colspan="2"><input id="hide_logo" name="hide_logo" type="checkbox"
  966.         <?php echo $hide_logo ? "checked='checked'" : ""?> /> <label
  967.             for="hide_logo"><?php fs_e('Hide FireStats logo')?></label></td>
  968.     </tr>
  969. </table>
  970. <input
  971.     type="hidden" id="firestats-stats-widget-submit"
  972.     name="firestats-stats-widget-submit" value="1" />
  973.     <?php
  974. }
  975.  
  976. function fs_popular_pages_widget($args)
  977. {
  978.     $FS_PATH = fs_get_firestats_path();
  979.     require_once($FS_PATH.'/php/html-utils.php');
  980.  
  981.     // $args is an array of strings that help widgets to conform to
  982.     // the active theme: before_widget, before_title, after_widget,
  983.     // and after_title are the array keys. Default tags: li and h2.
  984.     extract($args);
  985.  
  986.     // Each widget can store its own options. We keep strings here.
  987.     $options = get_option('firestats_popular_pages_widget');
  988.     $title = htmlspecialchars($options['title'], ENT_QUOTES);
  989.     $num_to_show = htmlspecialchars($options['num_to_show'], ENT_QUOTES);
  990.     $days_ago = htmlspecialchars($options['days_ago'], ENT_QUOTES);
  991.     $hide_logo = $options['hide_logo2'];
  992.  
  993.     echo $before_widget . $before_title . $title . $after_title;
  994.     echo fs_get_popular_posts_list($num_to_show, $days_ago, $hide_logo);
  995.     echo $after_widget;
  996. }
  997.  
  998. function fs_popular_pages_widget_control()
  999. {
  1000.     // Get our options and see if we're handling a form submission.
  1001.     $options = get_option('firestats_popular_pages_widget');
  1002.     if ( empty($options))
  1003.     {
  1004.         $options = array('title'=>fs_r("Popular posts"),
  1005.                              'num_to_show'=>10,
  1006.                              'days_ago'=>90,
  1007.                              'hide_logo2'=>false);
  1008.     }
  1009.  
  1010.     if ( $_POST['firestats-submit'])
  1011.     {
  1012.         $hide_logo = $_POST['hide_logo2'] == 'on';
  1013.  
  1014.         // Remember to sanitize and format use input appropriately.
  1015.  
  1016.         $options['title'] =       strip_tags(stripslashes(isset($_POST['firestats-title2']) ? $_POST['firestats-title2'] : $options['title']));
  1017.         $options['num_to_show'] = strip_tags(stripslashes(isset($_POST['num_to_show'])      ? $_POST['num_to_show']      : $options['num_to_show']));
  1018.         $options['days_ago'] =    strip_tags(stripslashes(isset($_POST['days_ago2'])        ? $_POST['days_ago2']        : $options['days_ago']));
  1019.         $options['hide_logo2'] = $hide_logo;
  1020.         if (!is_numeric($options['num_to_show'])) $options['num_to_show'] = 10;
  1021.         if (!is_numeric($options['days_ago']) && !empty($options['days_ago'])) $options['days_ago'] = 90;
  1022.         update_option('firestats_popular_pages_widget', $options);
  1023.         delete_option('cached_firestats_popular_pages');
  1024.     }
  1025.  
  1026.     // Be sure you format your options to be valid HTML attributes.
  1027.     $title = htmlspecialchars($options['title'], ENT_QUOTES);
  1028.     $num_to_show = htmlspecialchars($options['num_to_show'], ENT_QUOTES);
  1029.     $days_ago = htmlspecialchars($options['days_ago'], ENT_QUOTES);
  1030.     $hide_logo = $options['hide_logo2'];
  1031.  
  1032.     // Here is our little form segment. Notice that we don't need a
  1033.     // complete form. This will be embedded into the existing form.
  1034.     ?>
  1035. <table>
  1036.     <tr>
  1037.         <td><label for="firestats-title2"><?php fs_e('Title:')?></label></td>
  1038.         <td><input style="width: 200px;" id="firestats-title2"
  1039.             name="firestats-title2" type="text" value="<?php echo $title?>" /></td>
  1040.     </tr>
  1041.     <tr>
  1042.         <td><label for="num_to_show"><?php fs_e('Number to show')?></label></td>
  1043.         <td><input style="width: 200px;" id="num_to_show" name="num_to_show"
  1044.             type="text" value="<?php echo $num_to_show?>" /></td>
  1045.     </tr>
  1046.     <tr>
  1047.         <td><label for="days_ago"><?php fs_e('Days ago (Empty for all time)')?></label></td>
  1048.         <td><input style="width: 200px;" id="days_ago2" name="days_ago2"
  1049.             type="text" value="<?php echo $days_ago?>" /></td>
  1050.     </tr>
  1051.     <tr>
  1052.         <td colspan="2"><input id="hide_logo2" name="hide_logo2"
  1053.             type="checkbox" <?php echo $hide_logo ? "checked='checked'" : ""?> />
  1054.         <label for="hide_logo2"><?php fs_e('Hide FireStats logo')?></label></td>
  1055.     </tr>
  1056. </table>
  1057. <input
  1058.     type="hidden" id="firestats-submit" name="firestats-submit" value="1" />
  1059.     <?php
  1060. }
  1061.  
  1062. /**
  1063.  * returns a cached bit of data from the options table.
  1064.  * if the data is too old, it generates new data with the generator function
  1065.  */
  1066. function fs_get_cached_data($name, $generator, $timeout = 3600)
  1067. {
  1068.     $s = get_option($name);
  1069.     if (is_string($s))
  1070.     $cached = get_option($name);
  1071.     else
  1072.     $cached = $s;
  1073.  
  1074.     $stale = false;
  1075.     if (empty($cached))
  1076.     {
  1077.         $stale = true;
  1078.     }
  1079.     else
  1080.     {
  1081.         $timestamp = $cached['timestamp'];
  1082.         if (time() - $timestamp > $timeout)
  1083.         {
  1084.             $stale = true;
  1085.         }
  1086.         else
  1087.         {
  1088.             $res = $cached['data'];
  1089.         }
  1090.     }
  1091.  
  1092.     if ($stale)
  1093.     {
  1094.         $res = $generator();
  1095.         $cached = array('timestamp'=>time(), 'data'=>$res);
  1096.         update_option($name,serialize($cached));
  1097.     }
  1098.  
  1099.     return $res;
  1100. }
  1101.  
  1102. function fs_get_rss_subscribers_box($hide_logo = false)
  1103. {
  1104.     $FS_PATH = fs_get_firestats_path();
  1105.     if (!$FS_PATH) return "FireStats is not configured yet";
  1106.     require_once($FS_PATH.'/php/init.php');
  1107.  
  1108.     $powered = fs_get_powered_by('fs_powered_by');
  1109.     $cache_timeout = 10*60; // 10 minutes
  1110.     $subscribers = fs_get_cached_data('firestats_rss_subscribers', fs_get_rss_subscribers, $cache_timeout);
  1111.     $text = sprintf(fs_r("%s readers"),$subscribers);
  1112.     $res = "
  1113. <!-- You can customize the sidebox by playing with your theme css-->
  1114. <ul class='firestats_sidebox'>
  1115.     <li>$text</li>
  1116. </ul>";
  1117.     if (!$hide_logo) $res .= $powered;
  1118.  
  1119.     return $res;
  1120. }
  1121.  
  1122.  
  1123. function fs_get_stats_box($hide_logo = false, $with_rss = false)
  1124. {
  1125.     $with_rss = $with_rss && fs_plugin_installed("RSS Subscribers");   
  1126.     $FS_PATH = fs_get_firestats_path();
  1127.     if (!$FS_PATH) return "FireStats is not configured yet";
  1128.     require_once($FS_PATH.'/php/db-sql.php');
  1129.  
  1130.     $powered = fs_get_powered_by('fs_powered_by');
  1131.     $cache_timeout = 10*60; // 10 minutes
  1132.     $count = fs_get_cached_data('cached_firestats_hit_count', fs_get_hit_count, $cache_timeout);
  1133.     $unique = fs_get_cached_data('cached_firestats_unique_hits', fs_get_unique_hit_count, $cache_timeout);
  1134.     $last_24h_count = fs_get_cached_data('cached_firestats_last_24h_count', create_function('','return fs_get_hit_count(1);'),$cache_timeout);
  1135.     $last_24h_unique = fs_get_cached_data('cached_firestats_last_24h_visits', create_function('','return fs_get_unique_hit_count(1);'),$cache_timeout);
  1136.    
  1137.     if ($with_rss)
  1138.     {
  1139.         $subscribers = fs_get_cached_data('firestats_rss_subscribers', fs_get_rss_subscribers, $cache_timeout);
  1140.     }
  1141.  
  1142.  
  1143.     $total_visits  = fs_r("Pages displayed : ")."<b>$count</b>";
  1144.     $total_uniques = fs_r("Unique visitors : ")."<b>$unique</b>";
  1145.     $visits_today  = fs_r("Pages displayed in last 24 hours : ")."<b>$last_24h_count</b>";
  1146.     $uniques_today = fs_r("Unique visitors in last 24 hours : ")."<b>$last_24h_unique</b>";
  1147.     if ($with_rss)
  1148.     {
  1149.         $rss_subscribers = fs_r("RSS subscribers")." : <b>$subscribers</b>";
  1150.     }
  1151.    
  1152.  
  1153.     $res = "
  1154. <!-- You can customize the sidebox by playing with your theme css-->
  1155. <ul class='firestats_sidebox'>
  1156.     <li>$total_visits</li>
  1157.     <li>$total_uniques</li>
  1158.     <li>$visits_today</li>
  1159.     <li>$uniques_today</li>
  1160.     ".($with_rss ? "<li>$rss_subscribers</li>" : "")."
  1161. </ul>";
  1162.     if (!$hide_logo) $res .= $powered;
  1163.  
  1164.     return $res;
  1165. }
  1166.  
  1167. function fs_add_comment_flag($link)
  1168. {
  1169.     $FS_PATH = fs_get_firestats_path();
  1170.     if (!$FS_PATH) return;
  1171.     require_once($FS_PATH.'/php/ip2country.php');
  1172.     require_once($FS_PATH.'/php/utils.php');
  1173.     if (fs_called_by(null, 'wp_widget_recent_comments') || fs_called_by("WP_Widget_Recent_Comments", 'widget')) return $link;
  1174.     $ip = get_comment_author_IP();
  1175.     $code = fs_ip2c($ip);
  1176.     if (!$code) return $link;
  1177.     return $link .' '. fs_get_country_flag_url($code);
  1178. }
  1179.  
  1180. function fs_add_comment_browser_os($link)
  1181. {
  1182.     global $comment;
  1183.     $ua = $comment->comment_agent;
  1184.     if (!$ua) return $link;
  1185.     $FS_PATH = fs_get_firestats_path();
  1186.     if (!$FS_PATH) return;
  1187.     require_once($FS_PATH.'/php/browsniff.php');
  1188.     require_once($FS_PATH.'/php/utils.php');
  1189.     if (fs_called_by(null, 'wp_widget_recent_comments') || fs_called_by("WP_Widget_Recent_Comments", 'widget')) return $link;
  1190.     return $link . ' '.fs_pri_browser_images($ua);
  1191. }
  1192.  
  1193. function fs_echo_footer()
  1194. {
  1195.     $FS_PATH = fs_get_firestats_path();
  1196.     if (!$FS_PATH) return;
  1197.     require_once($FS_PATH.'/php/db-sql.php');
  1198.  
  1199.     $stats = get_option('firestats_show_footer_stats') == 'true';
  1200.     if ($stats)
  1201.     {
  1202.         $cache_timeout = 10*60; // 10 minutes
  1203.         $count = fs_get_cached_data('cached_firestats_hit_count', fs_get_hit_count, $cache_timeout);
  1204.         $unique = fs_get_cached_data('cached_firestats_unique_hits', fs_get_unique_hit_count, $cache_timeout);
  1205.         $last_24h_count = fs_get_cached_data('cached_firestats_last_24h_count', create_function('','return fs_get_hit_count(1);'),$cache_timeout);
  1206.         $last_24h_unique = fs_get_cached_data('cached_firestats_last_24h_visits', create_function('','return fs_get_unique_hit_count(1);'),$cache_timeout);
  1207.         echo $count  .' '.fs_r('pages viewed')  . ", $last_24h_count "  . fs_r('today')."<br/>";
  1208.         echo $unique .' '.fs_r('visits')        . ", $last_24h_unique " . fs_r('today')."<br/>";
  1209.     }
  1210.     echo fs_get_powered_by('fs_powered_by');
  1211. }
  1212.  
  1213. function fs_get_powered_by($css_class)
  1214. {
  1215.     $img = fs_url("img/firestats-icon-small.png");
  1216.     $firestats_url = FS_HOMEPAGE;
  1217.     $powered = "<img alt='FireStats icon' src='$img'/><a href='$firestats_url'>&nbsp;".fs_r("Powered by FireStats").'</a>';
  1218.     return "";
  1219. }
  1220.  
  1221.  
  1222.  
  1223. function fs_get_wp_config_path()
  1224. {
  1225.     $base = dirname(__FILE__);
  1226.     $path = false;
  1227.  
  1228.     if (@file_exists(dirname(dirname($base))."/wp-config.php"))
  1229.     {
  1230.         $path = dirname(dirname($base))."/wp-config.php";
  1231.     }
  1232.     else
  1233.     if (@file_exists(dirname(dirname(dirname($base)))."/wp-config.php"))
  1234.     {
  1235.         $path = dirname(dirname(dirname($base)))."/wp-config.php";
  1236.     }
  1237.     else
  1238.     $path = false;
  1239.  
  1240.     if ($path != false)
  1241.     {
  1242.         $path = str_replace("\\", "/", $path);
  1243.     }
  1244.     return $path;
  1245. }
  1246.  
  1247.  
  1248. /**
  1249.  * Local option storage for wordpress, used by fs_update_local_option to update wordpress value in a generic way.
  1250.  */
  1251. function fs_update_local_option_impl($key, $value)
  1252. {
  1253.     update_option($key,$value);
  1254. }
  1255.  
  1256. /**
  1257.  * Local option storage for wordpress, used by fs_get_local_option to get wordpress value in a generic way.
  1258.  */
  1259. function fs_get_local_option_impl($key)
  1260. {
  1261.     return get_option($key);
  1262. }
  1263.  
  1264. function fs_define_system_settings_impl()
  1265. {
  1266.     global $FS_SYSTEM_SETTINGS;
  1267.     $FS_SYSTEM_SETTINGS['site_selector_mode'] = fs_full_installation()
  1268.     ? FS_SITE_SELECTOR_MODE_ADMIN_ONLY
  1269.     : FS_SITE_SELECTOR_MODE_BY_USERS_SITES_TABLE;
  1270.  
  1271. }
  1272.  
  1273. /**
  1274.  * Returns a list of local (saved in WordPress) options that a non admin user may change.
  1275.  * @return array of strings
  1276.  */
  1277. function fs_local_options_allowed_for_non_admin()
  1278. {
  1279.     if (fs_is_wpmu())
  1280.     {
  1281.         // even non admin user is allowed to save those options in a wpmu blog.
  1282.         return array(
  1283.             'firestats_show_footer',
  1284.             'firestats_show_footer_stats',
  1285.             'firestats_add_comment_flag',
  1286.             'firestats_add_comment_browser_os',
  1287.             'firestats_sites_filter'
  1288.             );
  1289.     }
  1290.     else
  1291.     {
  1292.         return array();
  1293.     }
  1294. }
  1295.  
  1296. function fs_is_windows()
  1297. {
  1298.     if (!isset($_ENV['OS'])) return false; // assume not windows.
  1299.     if (strpos(strtolower($_ENV['OS']), "windows") === false) return false;
  1300.     return true;
  1301. }
  1302.  
  1303.  
  1304. function fs_update_post_titles()
  1305. {
  1306.     $path = fs_get_firestats_path();
  1307.     if (!$path) return "FS_PATH not defined";
  1308.     require_once($path.'/php/db-sql.php');
  1309.  
  1310.     global $wpdb;
  1311.     $posts = $wpdb->get_results("SELECT ID,post_title,post_type FROM $wpdb->posts WHERE post_type = 'post' OR post_type = 'page'");
  1312.     if ($posts === false)
  1313.     {
  1314.         return $wpdb->last_error;
  1315.     }
  1316.  
  1317.     // this is required because of a wp bug: calling get_permalink assumes pluggable was included
  1318.     // and throws an error if it's not.
  1319.     // the if is needed because plugable only exists starting from a particular version of wp.
  1320.     if (file_exists(ABSPATH . "/wp-includes/pluggable.php"))
  1321.     {
  1322.         require_once(ABSPATH . "/wp-includes/pluggable.php");
  1323.     }
  1324.  
  1325.     $site_id = get_option('firestats_site_id');
  1326.     $fsdb = &fs_get_db_conn();
  1327.     $urls = fs_urls_table();
  1328.     $post_type = FS_URL_TYPE_POST;
  1329.     // reset type of all urls in this site id that are marked as posts
  1330.     $fsdb->query("UPDATE `$urls` set type=NULL,title=NULL WHERE type='$post_type' AND site_id = '$site_id'");
  1331.     foreach($posts as $post)
  1332.     {
  1333.         $id = $post->ID;
  1334.         $title = $post->post_title;
  1335.         $link = get_permalink($id);
  1336.         if (empty($link)) continue;
  1337.         // make sure the url exists in the urls table.;
  1338.         $url_id = fs_get_url_id($link);
  1339.         if ($url_id == null)
  1340.         {
  1341.             $res = fs_insert_url($link, $site_id);
  1342.             if ($res !== true)
  1343.             {
  1344.                 return "Error inserting url: $res";
  1345.             }
  1346.             $url_id = fs_get_url_id($link);
  1347.         }
  1348.  
  1349.         // replace title with current one
  1350.         $res = fs_set_url_title($link,$title);
  1351.         if ($res !== true)
  1352.         {
  1353.             return "Error handling post ($post->post_title) : $res";
  1354.         }
  1355.  
  1356.         // mark url as a post
  1357.         if ($post->post_type == 'post')
  1358.         {
  1359.             $res = fs_set_url_type($link,FS_URL_TYPE_POST);
  1360.             if ($res !== true)
  1361.             {
  1362.                 return $res;
  1363.             }
  1364.         }      
  1365.     }
  1366.  
  1367.     // clear popular pages cache
  1368.     delete_option('cached_firestats_popular_pages');
  1369.     return true;
  1370. }
  1371.  
  1372. function fs_plugin_db_update()
  1373. {
  1374.     // database version of firestats data in this particular blog.
  1375.     // if there are several blogs on the same firestats db, each one may need to do some actions in the upgrade prorcess.
  1376.     $db_version = get_option('firestats_db_version');
  1377.     if (empty($db_version)) $db_version = 0;
  1378.     if ($db_version < 11)
  1379.     {
  1380.         if (!fs_is_wpmu())
  1381.         {
  1382.             $res = fs_update_post_titles();
  1383.             if ($res !== true)
  1384.             {
  1385.                 echo $res;
  1386.                 return false;
  1387.             }
  1388.         }
  1389.     }
  1390.  
  1391.     update_option('firestats_db_version',FS_REQUIRED_DB_VERSION);
  1392. }
  1393.  
  1394. function fs_get_js_url()
  1395. {
  1396.     // as if this is not complicated enough, we need to add some windows specific hacks for IE7.
  1397.     $f = "index.php?page=".__FILE__."&fs_javascript=";
  1398.     $f = str_replace("\\","%5C",$f);
  1399.     return $f;
  1400. }
  1401.  
  1402. function fs_new_version_nag($file)
  1403. {
  1404.     $plugin_name = substr(__FILE__, strlen(ABSPATH . PLUGINDIR . '/'));
  1405.     if ($file != $plugin_name) return;
  1406.     $FS_PATH = fs_get_firestats_path();
  1407.     if ($FS_PATH)
  1408.     {
  1409.         require_once($FS_PATH.'/php/version-check.php');
  1410.         $msg = fs_get_latest_firestats_version_message();
  1411.         if ($msg != "")
  1412.         {
  1413.             echo "<tr><td colspan='5' class='plugin-update'>";
  1414.             echo $msg;
  1415.             echo "</td></tr>";
  1416.         }
  1417.     }
  1418. }
  1419.  
  1420. function fs_get_popular_posts_list($num_to_show = 10, $days_ago = 90, $hide_logo = false)
  1421. {
  1422.     $FS_PATH = fs_get_firestats_path();
  1423.     if (!$FS_PATH) return;
  1424.     require_once($FS_PATH.'/php/html-utils.php');
  1425.  
  1426.     if ($days_ago == '') $days_ago = 'null';
  1427.     if ($num_to_show == '') $num_to_show = '10';
  1428.     $generator =  create_function('',"return fs_get_popular_pages_tree($num_to_show, $days_ago, FS_URL_TYPE_POST,false, null);");
  1429.     $res = fs_get_cached_data('cached_firestats_popular_pages', $generator, 3600);
  1430.     if (!$hide_logo)
  1431.     $res .= "<br/>".fs_get_powered_by('fs_powered_by');
  1432.     return $res;
  1433. }
  1434.  
  1435. function fs_delete_wpmu_blog($blog_id)
  1436. {
  1437.     $FS_PATH = fs_get_firestats_path();
  1438.     if (!$FS_PATH) return;
  1439.     require_once($FS_PATH.'/php/db-sql.php');
  1440.     fs_delete_site($blog_id,'delete',null);
  1441.     fs_log("blog $blog_id deleted");
  1442. }
  1443.  
  1444. function fs_register_dashboard_widgets()
  1445. {
  1446.     $fs_path = fs_get_firestats_path();
  1447.     if (!$fs_path) return;
  1448.     require_once("$fs_path/php/db-common.php");
  1449.     if (fs_db_valid() && fs_can_use())
  1450.     {
  1451.         if (function_exists('wp_add_dashboard_widget'))
  1452.         {
  1453.             wp_add_dashboard_widget( 'firestats_stats_widget', 'FireStats statistics', 'fs_dashboard_widget' );
  1454.         }
  1455.         else
  1456.         {
  1457.             wp_register_sidebar_widget('firestats_stats_widget', 'FireStats statistics', 'fs_dashboard_widgets_pre_27');
  1458.         }
  1459.     }
  1460. }
  1461.  
  1462. function fs_add_dashboard_widgets( $widgets )
  1463. {
  1464.     global $wp_registered_widgets;
  1465.     if ( !isset($wp_registered_widgets['firestats_stats_widget']) ) return $widgets;
  1466.     array_splice( $widgets, 0, 0, 'firestats_stats_widget' );
  1467.     return $widgets;
  1468. }
  1469.  
  1470. /**
  1471.  * for wp <= 2.6
  1472.  */
  1473. function fs_dashboard_widgets_pre_27($args)
  1474. {
  1475.     extract($args);
  1476.     $img = fs_url("img/firestats-icon-small.png");
  1477.     $fs =  fs_r("FireStats");
  1478.     $title = fs_get_firestats_url("<img src='$img'> $fs</img>");
  1479.     echo $before_widget . $before_title . $title . $after_title;
  1480.     echo fs_get_stats_box(true,true);
  1481.     echo $after_widget;
  1482. }
  1483.  
  1484. /**
  1485.  * for wp >= 2.7
  1486.  */
  1487. function fs_dashboard_widget()
  1488. {
  1489.     $img = fs_url("img/firestats-icon-small.png");
  1490.     $fs =  fs_r("FireStats");
  1491.     $title = fs_get_firestats_url("<img src='$img'> $fs</img>");
  1492.     echo  $title;
  1493.     echo fs_get_stats_box(true,true);
  1494. }
  1495.  
  1496. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement