Source Code Documentation


s2member-pro\includes\classes\imports-in.inc.php


  1. <?php
  2. /**
  3. * Handles various importations ( inner processing routines ).
  4. *
  5. * Copyright: © 2009-2011
  6. * {@link http://www.websharks-inc.com/ WebSharks, Inc.}
  7. * ( coded in the USA )
  8. *
  9. * This WordPress® plugin ( s2Member Pro ) is comprised of two parts:
  10. *
  11. * o (1) Its PHP code is licensed under the GPL license, as is WordPress®.
  12. * You should have received a copy of the GNU General Public License,
  13. * along with this software. In the main directory, see: /licensing/
  14. * If not, see: {@link http://www.gnu.org/licenses/}.
  15. *
  16. * o (2) All other parts of ( s2Member Pro ); including, but not limited to:
  17. * the CSS code, some JavaScript code, images, and design;
  18. * are licensed according to the license purchased.
  19. * See: {@link http://www.s2member.com/prices/}
  20. *
  21. * Unless you have our prior written consent, you must NOT directly or indirectly license,
  22. * sub-license, sell, resell, or provide for free; part (2) of the s2Member Pro Module;
  23. * or make an offer to do any of these things. All of these things are strictly
  24. * prohibited with part (2) of the s2Member Pro Module.
  25. *
  26. * Your purchase of s2Member Pro includes free lifetime upgrades via s2Member.com
  27. * ( i.e. new features, bug fixes, updates, improvements ); along with full access
  28. * to our video tutorial library: {@link http://www.s2member.com/videos/}
  29. *
  30. * @package s2Member\Imports
  31. * @since 1.5
  32. */
  33. if(realpath(__FILE__) === realpath($_SERVER["SCRIPT_FILENAME"]))
  34. exit("Do not access this file directly.");
  35. /**/
  36. if(!class_exists("c_ws_plugin__s2member_pro_imports_in"))
  37. {
  38. /**
  39. * Handles various importations ( inner processing routines ).
  40. *
  41. * @package s2Member\Imports
  42. * @since 1.5
  43. */
  44. class c_ws_plugin__s2member_pro_imports_in
  45. {
  46. /**
  47. * Handles the importation of Users/Members.
  48. *
  49. * @package s2Member\Imports
  50. * @since 110815
  51. *
  52. * @return null
  53. */
  54. public static function import_users()
  55. {
  56. if(!empty($_POST["ws_plugin__s2member_pro_import_users"]) && ($nonce = $_POST["ws_plugin__s2member_pro_import_users"]) && wp_verify_nonce($nonce, "ws-plugin--s2member-pro-import-users") && current_user_can("create_users"))
  57. {
  58. global /* Global database object reference. */ $wpdb;
  59. global /* Multisite Networking. */ $current_site, $current_blog;
  60. /**/
  61. @set_time_limit /* Make time for processing large import files. */(0);
  62. @ini_set("memory_limit", apply_filters("admin_memory_limit", WP_MAX_MEMORY_LIMIT));
  63. /**/
  64. remove_all_actions("profile_update").remove_all_actions("user_register");
  65. remove_all_actions("added_existing_user").remove_all_actions("add_user_to_blog");
  66. /**/
  67. if(!empty($_FILES["ws_plugin__s2member_pro_import_users_file"]) && empty($_FILES["ws_plugin__s2member_pro_import_users_file"]["error"]))
  68. $file = fopen($_FILES["ws_plugin__s2member_pro_import_users_file"]["tmp_name"], "r");
  69. /**/
  70. else if(!empty($_POST["ws_plugin__s2member_pro_import_users_direct_input"]))
  71. fwrite(($file = tmpfile()), trim(stripslashes($_POST["ws_plugin__s2member_pro_import_users_direct_input"]))).fseek($file, 0);
  72. /**/
  73. if /* Only process if we have a resource. */(isset($file) && is_resource($file) && !($imported = 0))
  74. {
  75. $configured_custom_reg_fields = /* Initialize. */ array();
  76. if($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_fields"])
  77. foreach(json_decode($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_fields"], true) as $field)
  78. if($field_var = preg_replace("/[^a-z0-9]/i", "_", strtolower($field["id"])))
  79. $configured_custom_reg_fields[] = $field_var;
  80. /**/
  81. while(($data = ((version_compare(PHP_VERSION, "5.3", ">=")) ? fgetcsv($file, 0, ",", '"', '"') : fgetcsv($file, 0, ",", '"'))) !== false)
  82. {
  83. $line = /* CSV lines. */ (int)$line + 1;
  84. /**/
  85. $data = c_ws_plugin__s2member_utils_strings::trim_deep($data);
  86. $data = stripslashes_deep($data);
  87. /**/
  88. if /* Skip first line if it contains headers. */($line === 1 && strtoupper($data[0]) === "ID")
  89. {
  90. $line = $line - 1;
  91. continue;
  92. }
  93. if(is_multisite() && c_ws_plugin__s2member_utils_conds::is_multisite_farm() && !is_main_site())
  94. {
  95. $ID = $data[0];
  96. /**/
  97. $user_login = preg_replace("/\s+/", "", sanitize_user($data[1], is_multisite()));
  98. $user_pass = (string)"";
  99. /**/
  100. $first_name = $data[2];
  101. $last_name = $data[3];
  102. $display_name = $data[4];
  103. /**/
  104. $user_email = sanitize_email($data[5]);
  105. $user_url = $data[6];
  106. /**/
  107. $role = $data[7];
  108. $custom_capabilities = $data[8];
  109. /**/
  110. $user_registered = ($data[9]) ? date("Y-m-d H:i:s", strtotime($data[9])) : "";
  111. $paid_registration_times = ($data[10]) ? maybe_unserialize($data[10]) : "";
  112. $last_payment_time = ($data[11]) ? strtotime($data[11]) : "";
  113. $auto_eot_time = ($data[12]) ? strtotime($data[12]) : "";
  114. /**/
  115. $custom = $data[13];
  116. $subscr_id = $data[14];
  117. $subscr_gateway = strtolower($data[15]);
  118. /**/
  119. $custom_fields = array(); /* Initialize Custom Fields. */
  120. if(count($data) > 16) /* Now loop through Custom Fields. */
  121. for($i = 16; $i < count($data); $i = $i + 2) /* Skip over 2 fields at a time here. */
  122. if($field_var = preg_replace("/[^a-z0-9]/i", "_", strtolower(trim($data[$i], "^* \t\n\r\0\x0B"))))
  123. if(in_array($field_var, $configured_custom_reg_fields)) /* Must be pre-configured. */
  124. $custom_fields[$field_var] = maybe_unserialize($data[$i + 1]);
  125. }
  126. else /* Otherwise, we use the standardized format for importation.*/
  127. {
  128. $ID = $data[0];
  129. /**/
  130. $user_login = preg_replace("/\s+/", "", sanitize_user($data[1], is_multisite()));
  131. $user_pass = $data[2];
  132. /**/
  133. $first_name = $data[3];
  134. $last_name = $data[4];
  135. $display_name = $data[5];
  136. /**/
  137. $user_email = sanitize_email($data[6]);
  138. $user_url = $data[7];
  139. /**/
  140. $role = $data[8];
  141. $custom_capabilities = $data[9];
  142. /**/
  143. $user_registered = ($data[10]) ? date("Y-m-d H:i:s", strtotime($data[10])) : "";
  144. $paid_registration_times = ($data[11]) ? maybe_unserialize($data[11]) : "";
  145. $last_payment_time = ($data[12]) ? strtotime($data[12]) : "";
  146. $auto_eot_time = ($data[13]) ? strtotime($data[13]) : "";
  147. /**/
  148. $custom = $data[14];
  149. $subscr_id = $data[15];
  150. $subscr_gateway = strtolower($data[16]);
  151. /**/
  152. $custom_fields = array(); /* Initialize Custom Fields. */
  153. if(count($data) > 17) /* Now loop through Custom Fields. */
  154. for($i = 17; $i < count($data); $i = $i + 2) /* Skip over 2 fields at a time here. */
  155. if($field_var = preg_replace("/[^a-z0-9]/i", "_", strtolower(trim($data[$i], "^* \t\n\r\0\x0B"))))
  156. if(in_array($field_var, $configured_custom_reg_fields)) /* Must be pre-configured. */
  157. $custom_fields[$field_var] = maybe_unserialize($data[$i + 1]);
  158. }
  159. /**/
  160. $role = (is_numeric($role)) ? (($role == 0) ? "subscriber" : "s2member_level".$role) : $role;
  161. /**/
  162. if($paid_registration_times && !is_array($paid_registration_times))
  163. $paid_registration_times = array("level" => strtotime($paid_registration_times));
  164. $paid_registration_times = (!$paid_registration_times || !is_array($paid_registration_times)) ? array(): $paid_registration_times;
  165. /**/
  166. $user_details = compact("ID", "user_login", "user_pass", "first_name", "last_name", "display_name", "user_email", "user_url", "role", "user_registered");
  167. if(empty($user_details["user_pass"])) /* If there was NO Password given. */
  168. unset($user_details["user_pass"]); /* Unset the Password array element. */
  169. /**/
  170. if($ID) /* Are we dealing with an existing User ID? */
  171. {
  172. if(is_object($user = new WP_User($ID)) && $user->ID) /* Is this User in the database? */
  173. {
  174. if(!is_multisite() || is_user_member_of_blog($ID)) /* Must be a Member of this Blog. */
  175. {
  176. if((!is_multisite() || !is_super_admin($ID)) && !$user->has_cap("administrator"))
  177. {
  178. if(strtolower($role) !== "administrator") /* Do NOT update to Administrator. */
  179. {
  180. if($user_email && is_email($user_email)) /* Is the email address valid? */
  181. {
  182. if($user_login) /* Has a Username ( aka: user_login ) been supplied? */
  183. {
  184. if(validate_username($user_login)) /* Is the Username valid? */
  185. {
  186. if(($_same_email = (strtolower($user_email) === strtolower($user->user_email))) || !email_exists($user_email))
  187. {
  188. if(($_same_login = (strtolower($user_login) === strtolower($user->user_login))) || !username_exists($user_login))
  189. {
  190. if(!is_multisite() || ($_same_email && $_same_login) || (($_ = wpmu_validate_user_signup($user_login, $user_email)) && (!is_wp_error($_["errors"]) || !$_["errors"]->get_error_code())))
  191. {
  192. if(is_multisite() && c_ws_plugin__s2member_utils_conds::is_multisite_farm() && !is_main_site())
  193. unset($user_details["user_login"], $user_details["user_pass"]);
  194. /**/
  195. if(($user_id = wp_update_user($user_details)))
  196. {
  197. update_user_option($user_id, "s2member_custom", $custom);
  198. update_user_option($user_id, "s2member_subscr_id", $subscr_id);
  199. update_user_option($user_id, "s2member_subscr_gateway", $subscr_gateway);
  200. update_user_option($user_id, "s2member_auto_eot_time", $auto_eot_time);
  201. update_user_option($user_id, "s2member_paid_registration_times", $paid_registration_times);
  202. update_user_option($user_id, "s2member_last_payment_time", $last_payment_time);
  203. update_user_option($user_id, "s2member_custom_fields", $custom_fields);
  204. /**/
  205. foreach($user->allcaps as $cap => $cap_enabled)
  206. if(preg_match("/^access_s2member_ccap_/", $cap))
  207. $user->remove_cap($ccap = $cap);
  208. /**/
  209. if($custom_capabilities && preg_replace("/^-all[\r\n\t\s;,]*/", "", str_replace("+", "", $custom_capabilities)))
  210. foreach(preg_split("/[\r\n\t\s;,]+/", preg_replace("/^-all[\r\n\t\s;,]*/", "", str_replace("+", "", $custom_capabilities))) as $ccap)
  211. if(strlen($ccap = trim(strtolower(preg_replace("/[^a-z_0-9]/i", "", $ccap)))))
  212. $user->add_cap("access_s2member_ccap_".$ccap);
  213. /**/
  214. $imported = $imported + 1;
  215. }
  216. else
  217. $errors[] = "Line #".$line.". User ID# <code>".$ID."</code> could NOT be updated. Unknown error, please try again.";
  218. }
  219. else
  220. $errors[] = "Line #".$line.". Network. The Username and/or Email ( <code>".esc_html($user_login)."</code> / <code>".esc_html($user_email)."</code> ) are in conflict w/ Network rules.";
  221. }
  222. else
  223. $errors[] = "Line #".$line.". Conflicting. The Username ( <code>".esc_html($user_login)."</code> ), already exists.";
  224. }
  225. else
  226. $errors[] = "Line #".$line.". Conflicting. The Email address ( <code>".esc_html($user_email)."</code> ), already exists.";
  227. }
  228. else
  229. $errors[] = "Line #".$line.". Invalid Username ( <code>".esc_html($user_login)."</code> ). Lowercase alphanumerics are required.";
  230. }
  231. else
  232. $errors[] = "Line #".$line.". Missing Username; please try again."; /* We have two separate errors for Usernames. This provides clarity. */
  233. }
  234. else
  235. $errors[] = "Line #".$line.". Missing or invalid Email address ( <code>".esc_html($user_email)."</code> ); please try again.";
  236. }
  237. else
  238. $errors[] = "Line #".$line.". User ID# <code>".$ID."</code> cannot be updated to an Administrator. Bypassing this line for security.";
  239. }
  240. else
  241. $errors[] = "Line #".$line.". User ID# <code>".$ID."</code> belongs to an Administrator. Bypassing this line for security.";
  242. }
  243. else
  244. $errors[] = "Line #".$line.". User ID# <code>".$ID."</code> does NOT belong to an existing User on this site.";
  245. }
  246. else
  247. $errors[] = "Line #".$line.". User ID# <code>".$ID."</code> does NOT belong to an existing User.";
  248. }
  249. /**/
  250. else if(is_multisite() && ($user_id = c_ws_plugin__s2member_utils_users::ms_user_login_email_exists_but_not_on_blog($user_login, $user_email)) && !is_super_admin($user_id))
  251. {
  252. if(strtolower($role) !== "administrator") /* Do NOT add existing Users as Administrators. */
  253. {
  254. if(add_existing_user_to_blog(array("user_id" => $user_id, "role" => $role)))
  255. {
  256. if(is_object($user = new WP_User($user_id)) && $user->ID)
  257. {
  258. update_user_option($user_id, "s2member_custom", $custom);
  259. update_user_option($user_id, "s2member_subscr_id", $subscr_id);
  260. update_user_option($user_id, "s2member_subscr_gateway", $subscr_gateway);
  261. update_user_option($user_id, "s2member_auto_eot_time", $auto_eot_time);
  262. update_user_option($user_id, "s2member_paid_registration_times", $paid_registration_times);
  263. update_user_option($user_id, "s2member_last_payment_time", $last_payment_time);
  264. update_user_option($user_id, "s2member_custom_fields", $custom_fields);
  265. /**/
  266. foreach($user->allcaps as $cap => $cap_enabled)
  267. if(preg_match("/^access_s2member_ccap_/", $cap))
  268. $user->remove_cap($ccap = $cap);
  269. /**/
  270. if($custom_capabilities && preg_replace("/^-all[\r\n\t\s;,]*/", "", str_replace("+", "", $custom_capabilities)))
  271. foreach(preg_split("/[\r\n\t\s;,]+/", preg_replace("/^-all[\r\n\t\s;,]*/", "", str_replace("+", "", $custom_capabilities))) as $ccap)
  272. if(strlen($ccap = trim(strtolower(preg_replace("/[^a-z_0-9]/i", "", $ccap)))))
  273. $user->add_cap("access_s2member_ccap_".$ccap);
  274. /**/
  275. $imported = $imported + 1;
  276. }
  277. else
  278. $errors[] = "Line #".$line.". Unknown object error, please try again.";
  279. }
  280. else
  281. $errors[] = "Line #".$line.". Unknown User/site addition error, please try again.";
  282. }
  283. else
  284. $errors[] = "Line #".$line.". Role cannot be Administrator. Bypassing this line for security.";
  285. }
  286. /**/
  287. else /* Otherwise, we are adding a brand new User. */
  288. {
  289. if(strtolower($role) !== "administrator") /* Admin? */
  290. {
  291. if($user_email && is_email($user_email) /* Valid? */)
  292. {
  293. if($user_login) /* Was a Username even supplied? */
  294. {
  295. if(validate_username($user_login)) /* Is it valid? */
  296. {
  297. if(!email_exists($user_email) /* Exists already? */)
  298. {
  299. if(!username_exists($user_login) /* Exists? */)
  300. {
  301. if(!is_multisite() || (($_ = wpmu_validate_user_signup($user_login, $user_email)) && (!is_wp_error($_["errors"]) || !$_["errors"]->get_error_code())))
  302. {
  303. if(($user_id = wp_insert_user($user_details)))
  304. {
  305. if(is_object($user = new WP_User($user_id)) && $user->ID)
  306. {
  307. if($user_pass) /* If we are given an "un-encrypted Password". */
  308. wp_update_user(array("ID" => $user_id, "user_pass" => $user_pass));
  309. /**/
  310. if(is_multisite()) /* New Users on a Multisite Network need this too. */
  311. update_user_meta($user_id, "s2member_originating_blog", $current_blog->blog_id);
  312. /**/
  313. update_user_option($user_id, "s2member_custom", $custom);
  314. update_user_option($user_id, "s2member_subscr_id", $subscr_id);
  315. update_user_option($user_id, "s2member_subscr_gateway", $subscr_gateway);
  316. update_user_option($user_id, "s2member_auto_eot_time", $auto_eot_time);
  317. update_user_option($user_id, "s2member_paid_registration_times", $paid_registration_times);
  318. update_user_option($user_id, "s2member_last_payment_time", $last_payment_time);
  319. update_user_option($user_id, "s2member_custom_fields", $custom_fields);
  320. /**/
  321. foreach($user->allcaps as $cap => $cap_enabled)
  322. if(preg_match("/^access_s2member_ccap_/", $cap))
  323. $user->remove_cap($ccap = $cap);
  324. /**/
  325. if($custom_capabilities && preg_replace("/^-all[\r\n\t\s;,]*/", "", str_replace("+", "", $custom_capabilities)))
  326. foreach(preg_split("/[\r\n\t\s;,]+/", preg_replace("/^-all[\r\n\t\s;,]*/", "", str_replace("+", "", $custom_capabilities))) as $ccap)
  327. if(strlen($ccap = trim(strtolower(preg_replace("/[^a-z_0-9]/i", "", $ccap)))))
  328. $user->add_cap("access_s2member_ccap_".$ccap);
  329. /**/
  330. $imported = $imported + 1;
  331. }
  332. else
  333. $errors[] = "Line #".$line.". Unknown object error, please try again.";
  334. }
  335. else
  336. $errors[] = "Line #".$line.". Unknown insertion error, please try again.";
  337. }
  338. else
  339. $errors[] = "Line #".$line.". Network. The Username and/or Email ( <code>".esc_html($user_login)."</code> / <code>".esc_html($user_email)."</code> ) are in conflict w/ Network rules.";
  340. }
  341. else
  342. $errors[] = "Line #".$line.". Conflicting. The Username ( <code>".esc_html($user_login)."</code> ), already exists.";
  343. }
  344. else
  345. $errors[] = "Line #".$line.". Conflicting. The Email address ( <code>".esc_html($user_email)."</code> ), already exists.";
  346. }
  347. else
  348. $errors[] = "Line #".$line.". Invalid Username ( <code>".esc_html($user_login)."</code> ). Lowercase alphanumerics are required.";
  349. }
  350. else
  351. $errors[] = "Line #".$line.". Missing Username; please try again."; /* We have two separate errors for Usernames. This provides clarity. */
  352. }
  353. else
  354. $errors[] = "Line #".$line.". Missing or invalid Email address ( <code>".esc_html($user_email)."</code> ); please try again.";
  355. }
  356. else
  357. $errors[] = "Line #".$line.". Role cannot be Administrator. Bypassing this line for security.";
  358. }
  359. }
  360. /**/
  361. fclose($file);
  362. }
  363. else
  364. $errors[] = "No data was received. Please try again."; /* The upload failed, or it was empty. */
  365. /**/
  366. c_ws_plugin__s2member_admin_notices::display_admin_notice('Operation complete. Users/Members imported: <code>'.(int)$imported.'</code>.');
  367. /**/
  368. if(!empty($errors)) /* Here is where a detailed error log will be returned to the Site Owner; as a way of clarifying what just happened during importation. */
  369. c_ws_plugin__s2member_admin_notices::display_admin_notice('<strong>The following errors were encountered during importation:</strong><ul style="font-size:80%; list-style:disc outside; margin-left:25px;"><li>'.implode("</li><li>", $errors).'</li></ul>', true);
  370. }
  371. /**/
  372. return; /* Return for uniformity. */
  373. }
  374. /**
  375. * Handles the importation of options.
  376. *
  377. * @package s2Member\Imports
  378. * @since 110815
  379. *
  380. * @return null
  381. */
  382. public static function import_ops()
  383. {
  384. if(!empty($_POST["ws_plugin__s2member_pro_import_ops"]) && ($nonce = $_POST["ws_plugin__s2member_pro_import_ops"]) && wp_verify_nonce($nonce, "ws-plugin--s2member-pro-import-ops") && current_user_can("create_users"))
  385. {
  386. @set_time_limit(0); /* Make time for processing large import files. */
  387. @ini_set("memory_limit", apply_filters("admin_memory_limit", WP_MAX_MEMORY_LIMIT));
  388. /**/
  389. if(!empty($_FILES["ws_plugin__s2member_pro_import_ops_file"]) && empty($_FILES["ws_plugin__s2member_pro_import_ops_file"]["error"]))
  390. $file = file_get_contents($_FILES["ws_plugin__s2member_pro_import_ops_file"]["tmp_name"], "r");
  391. /**/
  392. if(!empty($file)) /* Only process if we have an importation file. */
  393. {
  394. if(is_array($import = c_ws_plugin__s2member_pro_utils_ops::op_replace(@unserialize($file), true)) && !empty($import) && ($import["configured"] = "1"))
  395. {
  396. unset($import["options_checksum"], $import["options_version"]);
  397. /**/
  398. foreach($import as $key => $value) /* Add prefixes now. */
  399. {
  400. (is_array($value)) ? array_unshift($value, "update-signal") : null;
  401. $import["ws_plugin__s2member_".$key] = $value;
  402. unset($import[$key]);
  403. }
  404. /**/
  405. c_ws_plugin__s2member_menu_pages::update_all_options($import, true, true, false, false, false);
  406. }
  407. else
  408. $errors[] = "Invalid data received. Please try again."; /* Unserialization failed? */
  409. }
  410. else
  411. $errors[] = "No data was received. Please try again."; /* The upload failed, or it was empty. */
  412. /**/
  413. if(!empty($errors)) /* Here is where a detailed error log will be returned to the Site Owner; as a way of clarifying what just happened during importation. */
  414. c_ws_plugin__s2member_admin_notices::display_admin_notice('<strong>The following errors were encountered during importation:</strong><ul style="font-size:80%; list-style:disc outside; margin-left:25px;"><li>'.implode("</li><li>", $errors).'</li></ul>', true);
  415. else
  416. c_ws_plugin__s2member_admin_notices::display_admin_notice('Operation complete. Options imported.');
  417. }
  418. /**/
  419. return; /* Return for uniformity. */
  420. }
  421. }
  422. }
  423. ?>

s2member-pro\includes\classes\imports-in.inc.php

Source Code Documentation