/libs/lib-setup.php

  1. <?php 
  2.  
  3. if ( ! function_exists('gde_activate') ) { 
  4. // no access if parent plugin is disabled or when accessed directly 
  5. wp_die('<p>'.__('You do not have sufficient permissions to access this page.').'</p>'); 
  6.  
  7. /** 
  8. * Define system defaults (settings/profiles) 
  9. * 
  10. * @since 2.5.5.1 
  11. * @return mixed 
  12. */ 
  13. function gde_defaults( $type ) { 
  14. global $env; 
  15.  
  16. // gather/set environment info 
  17. if ( ! $env ) { 
  18. $pdata = gde_get_plugin_data(); 
  19. $baseurl = gde_base_url(); 
  20. $default_lang = gde_get_locale(); 
  21. $apikey = ''; 
  22. $env = array( 
  23. 'pdata' => $pdata,  
  24. 'baseurl' => $baseurl,  
  25. 'default_lang' => $default_lang 
  26. ); 
  27.  
  28. // define "global" options (multisite only) 
  29. $globalopts = array( 
  30. 'file_maxsize' => '12',  
  31. 'beta_check' => 'no' 
  32. ); 
  33.  
  34. // define default options 
  35. $defopts = array( 
  36. 'ed_disable' => 'no',  
  37. 'ed_extend_upload' => 'yes',  
  38. 'ed_embed_sc' => 'yes',  
  39. 'file_maxsize' => '12',  
  40. 'error_check' => 'yes',  
  41. 'error_display' => 'yes',  
  42. 'error_log' => 'no',  
  43. 'beta_check' => 'no',  
  44. 'ga_enable' => 'no',  
  45. 'ga_category' => $env['pdata']['Name'],  
  46. 'ga_label' => 'url' 
  47. ); 
  48.  
  49. // define default profile(s) 
  50. $defpros = array( 
  51. "default" => array( 
  52. "desc" => __('This is the default profile, used when no profile is specified.', 'google-document-embedder'),  
  53. "viewer" => 'standard',  
  54. "default_width" => '100%',  
  55. "default_height" => '500px',  
  56. "tb_mobile" => 'default',  
  57. "tb_flags" => '',  
  58. "tb_fullscr" => 'default',  
  59. "tb_fullwin" => 'new',  
  60. "tb_fulluser" => 'no',  
  61. "tb_print" => 'no',  
  62. "vw_bgcolor" => '#EBEBEB',  
  63. "vw_pbcolor" => '#DADADA',  
  64. "vw_css" => '',  
  65. "vw_flags" => '',  
  66. "language" => $env['default_lang'],  
  67. "base_url" => $env['baseurl'],  
  68. "link_show" => 'all',  
  69. "link_mask" => 'no',  
  70. "link_block" => 'no',  
  71. "link_text" => __('Download', 'google-document-embedder') . ' (%TYPE, %SIZE)',  
  72. "link_pos" => 'below',  
  73. "link_force" => 'no',  
  74. "cache" => 'on' 
  75. ),  
  76. "max-doc-security" => array( 
  77. "desc" => __('Hide document location and text selection, prevent downloads', 'google-document-embedder'),  
  78. "viewer" => 'enhanced',  
  79. "default_width" => '100%',  
  80. "default_height" => '500px',  
  81. "tb_mobile" => 'default',  
  82. "tb_flags" => '',  
  83. "tb_fullscr" => 'viewer',  
  84. "tb_fullwin" => 'new',  
  85. "tb_fulluser" => 'no',  
  86. "tb_print" => 'no',  
  87. "vw_bgcolor" => '#EBEBEB',  
  88. "vw_pbcolor" => '#DADADA',  
  89. "vw_css" => '',  
  90. "vw_flags" => '',  
  91. "language" => $env['default_lang'],  
  92. "base_url" => $env['baseurl'],  
  93. "link_show" => 'none',  
  94. "link_mask" => 'no',  
  95. "link_block" => 'yes',  
  96. "link_text" => '',  
  97. "link_pos" => 'below',  
  98. "link_force" => 'no',  
  99. "cache" => 'on' 
  100. ),  
  101. "dark" => array( 
  102. "desc" => __('Dark-colored theme, example of custom CSS option', 'google-document-embedder'),  
  103. "viewer" => 'enhanced',  
  104. "default_width" => '100%',  
  105. "default_height" => '500px',  
  106. "tb_mobile" => 'default',  
  107. "tb_flags" => '',  
  108. "tb_fullscr" => 'viewer',  
  109. "tb_fullwin" => 'new',  
  110. "tb_fulluser" => 'no',  
  111. "tb_print" => 'no',  
  112. "vw_bgcolor" => '',  
  113. "vw_pbcolor" => '',  
  114. "vw_css" => GDE_PLUGIN_URL . 'css/gde-dark.css',  
  115. "vw_flags" => '',  
  116. "language" => $env['default_lang'],  
  117. "base_url" => $env['baseurl'],  
  118. "link_show" => 'all',  
  119. "link_mask" => 'no',  
  120. "link_block" => 'no',  
  121. "link_text" => __('Download', 'google-document-embedder') . ' (%TYPE, %SIZE)',  
  122. "link_pos" => 'below',  
  123. "link_force" => 'no',  
  124. "cache" => 'on' 
  125. ); 
  126.  
  127. switch ( $type ) { 
  128. case "globals": 
  129. return $globalopts; 
  130. break; 
  131. case "options": 
  132. return $defopts; 
  133. break; 
  134. case "profiles": 
  135. return $defpros; 
  136. break; 
  137. default: 
  138. gde_dx_log('Defaults requested but type not specified'); 
  139. return false; 
  140. break; 
  141.  
  142. /** 
  143. * Perform activation 
  144. * 
  145. * @since 2.5.0.1 
  146. * @return void 
  147. */ 
  148. function gde_setup() { 
  149. if ( GDE_DX_LOGGING > 0 ) { 
  150. gde_dx_log("Dx log manually enabled in functions.php"); 
  151.  
  152. // clear any beta transient 
  153. gde_dx_log("Clearing beta cache"); 
  154. delete_site_transient( 'gde_beta_version' ); 
  155. delete_transient( 'gde_beta_version' ); 
  156. delete_option( 'external_updates-google-document-embedder' ); 
  157.  
  158. gde_dx_log("Activating..."); 
  159.  
  160. if ( is_multisite() ) { 
  161. if ( ! $gdeglobals = get_site_option( 'gde_globals' ) ) { 
  162. gde_dx_log("Writing multisite global options"); 
  163. $globalopts = gde_defaults('global'); 
  164. update_site_option( 'gde_globals', $globalopts ); 
  165.  
  166. // check for existing or updated options 
  167. $gdeoptions = gde_get_options(); 
  168.  
  169. // check for existence of default profile (re-activation?) 
  170. if ( ! gde_get_profiles( 1 ) ) { 
  171. // new activation - write profile(s) 
  172. $defpros = gde_defaults('profiles'); 
  173. foreach ( $defpros as $key => $prodata ) { 
  174. if ( $key == "default" ) { 
  175. $id = 1; // default profile is always ID 1 
  176. } else { 
  177. $id = null; // assign next id 
  178.  
  179. // prepare profile 
  180. $desc = $prodata['desc']; 
  181. unset( $prodata['desc'] ); 
  182.  
  183. // write profile 
  184. $data = serialize( $prodata ); 
  185. $profile = array( $key, $desc, $data ); 
  186. if ( gde_write_profile( $profile, $id ) < 1 ) { 
  187. gde_dx_log("Failed to write profile '$key'"); 
  188. } else { 
  189. gde_dx_log("Profiles already exist"); 
  190. update_profiles(); 
  191.  
  192. gde_dx_log("Activation complete."); 
  193.  
  194. /** 
  195. * Upgrade profiles if changes have been made 
  196. * 
  197. * @since 2.5.5.1 
  198. * @return void 
  199. */ 
  200. function update_profiles() { 
  201. $prodata = gde_get_profiles(); 
  202. $defpros = gde_defaults('profiles'); 
  203. $default = $defpros['default']; 
  204.  
  205. foreach ( $prodata as $profile ) { 
  206. $updated = false; 
  207.  
  208. $id = $profile['profile_id']; 
  209. if ( $data = @unserialize( $profile['profile_data'] ) ) { 
  210. foreach ( $default as $k => $v ) { 
  211. if ( $k !== "desc" && ! array_key_exists( $k, $data ) ) { 
  212. $data[$k] = $default[$k]; 
  213.  
  214. $updated = true; 
  215.  
  216. if ( $updated ) { 
  217. // write updated profile 
  218. $data = serialize( $data ); 
  219. $newpro = array( $profile['profile_name'], $profile['profile_desc'], $data ); 
  220. if ( gde_write_profile( $newpro, $id, true ) < 1 ) { 
  221. gde_dx_log("Failed to update profile '" . $profile['profile_name'] . "'"); 
  222.  
  223. /** 
  224. * Get the current options, upgrading or resetting them as needed 
  225. * 
  226. * @since 2.5.5.1 
  227. * @return array Current value of gde_options 
  228. */ 
  229. function gde_get_options() { 
  230. $defopts = gde_defaults('options'); 
  231.  
  232. if ( ! $gdeoptions = get_option('gde_options') ) { 
  233. // options don't exist 
  234. gde_dx_log("Writing default options"); 
  235. update_option('gde_options', $defopts); 
  236. } else { 
  237. // check if upgrading from < 2.5 
  238. if ( isset( $gdeoptions['default_width'] ) ) { 
  239. gde_dx_log("Old options found - resetting"); 
  240. $defopts['upgraded'] = "yes"; 
  241. update_option('gde_options', $defopts); 
  242. } else { 
  243. gde_dx_log("Options already exist"); 
  244.  
  245. // check or upgrade options 
  246. $updated = false; 
  247. foreach ( $defopts as $k => $v ) { 
  248. if ( ! array_key_exists( $k, $gdeoptions ) ) { 
  249. $gdeoptions[$k] = $v; 
  250. //gde_dx_log("New option $k added"); 
  251. $updated = true; 
  252.  
  253. if ( $updated ) { 
  254. gde_dx_log('Options were updated'); 
  255. update_option('gde_options', $defopts); 
  256.  
  257. return $gdeoptions; 
  258.  
  259. /** 
  260. * Create/update database table to store profile data 
  261. * 
  262. * @since 2.5.0.1 
  263. * @return bool Whether or not table creation/update was successful 
  264. */ 
  265. function gde_db_tables( $gde_db_ver ) { 
  266. global $wpdb; 
  267.  
  268. // attempt to trap table creation failures 
  269. $fails = 0; 
  270.  
  271. // check for missing required tables (clear db version) 
  272. $table = $wpdb->prefix . 'gde_profiles'; 
  273. if ($wpdb->get_var( "SHOW TABLES LIKE '$table'" ) !== $table) { 
  274. //gde_dx_log("profiles db failed health check"); 
  275. delete_site_option( 'gde_db_version' ); 
  276.  
  277. $table = $wpdb->prefix . 'gde_secure'; 
  278. if ($wpdb->get_var( "SHOW TABLES LIKE '$table'" ) !== $table) { 
  279. //gde_dx_log("securedoc db failed health check"); 
  280. delete_site_option( 'gde_db_version' ); 
  281.  
  282. if ( is_multisite() ) { 
  283. $db_ver_installed = get_site_option( 'gde_db_version', 0 ); 
  284. } else { 
  285. $db_ver_installed = get_option( 'gde_db_version', 0 ); 
  286.  
  287. gde_dx_log("Installed DB ver: $db_ver_installed; This DB ver: " . $gde_db_ver ); 
  288. if ( version_compare( $gde_db_ver, $db_ver_installed, ">" ) ) { 
  289. // install or upgrade profile table 
  290. $table = $wpdb->prefix . 'gde_profiles'; 
  291.  
  292. $sql = "CREATE TABLE " . $table . " ( 
  293. profile_id mediumint(9) UNSIGNED NOT NULL AUTO_INCREMENT,  
  294. profile_name varchar(64) NOT NULL,  
  295. profile_desc varchar(255) NULL,  
  296. profile_data longtext NOT NULL,  
  297. UNIQUE KEY (profile_id) 
  298. ) ENGINE=MyISAM DEFAULT CHARSET=utf8; "; 
  299.  
  300. if ( isset( $sql ) ) { 
  301. // write table or update to database 
  302. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); 
  303. dbDelta($sql); 
  304.  
  305. if ($wpdb->get_var( "SHOW TABLES LIKE '$table'" ) == $table ) { 
  306. gde_dx_log("Profile table create/update successful"); 
  307. } else { 
  308. gde_dx_log("Profile table create/update failed"); 
  309. $fails++; 
  310.  
  311. // install or upgrade securedoc table 
  312. $table = $wpdb->prefix . 'gde_secure'; 
  313. $sql = "CREATE TABLE " . $table . " ( 
  314. code varchar(10) NOT NULL,  
  315. url varchar(255) NOT NULL,  
  316. murl varchar(100) NOT NULL,  
  317. stamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,  
  318. autoexpire enum('Y', 'N') NOT NULL DEFAULT 'N',  
  319. UNIQUE KEY code (code) 
  320. ) ENGINE=MyISAM DEFAULT CHARSET=utf8; "; 
  321.  
  322. if ( isset( $sql ) ) { 
  323. // write table or update to database 
  324. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); 
  325. dbDelta( $sql ); 
  326.  
  327. if ( $wpdb->get_var( "SHOW TABLES LIKE '$table'" ) == $table ) { 
  328. gde_dx_log("Secure doc table create/update successful"); 
  329. } else { 
  330. gde_dx_log("Secure doc table create/update failed"); 
  331. $fails++; 
  332. } else { 
  333. gde_dx_log("Tables OK, nothing to do"); 
  334.  
  335. if ( $fails > 0 ) { 
  336. delete_site_option( 'gde_db_version' ); 
  337. return false; 
  338. } else { 
  339. update_site_option( 'gde_db_version', $gde_db_ver ); 
  340. return true; 
  341.  
  342. ?> 
.