/libs/lib-setup.php

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