Source Code Documentation


pro\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. $custom_field_vars = /* Initialize this array. */ 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. {
  79. $custom_field_var = preg_replace("/[^a-z0-9]/i", "_", strtolower($field["id"]));
  80. $custom_field_vars[] = $custom_field_var;
  81. }
  82. sort /* Always sort this array. */($custom_field_vars, SORT_STRING);
  83.  
  84. while(($data = ((version_compare(PHP_VERSION, "5.3", ">=")) ? fgetcsv($file, 0, ",", '"', '"') : fgetcsv($file, 0, ",", '"'))) !== false)
  85. {
  86. $line = /* CSV lines. */ (int)$line + 1;
  87.  
  88. $data = c_ws_plugin__s2member_utils_strings::trim_deep($data);
  89. $data = stripslashes_deep($data);
  90.  
  91. if /* Skip first line if it contains headers. */($line === 1 && strtoupper($data[0]) === "ID")
  92. {
  93. $line = $line - 1;
  94. continue;
  95. }
  96. if(is_multisite() && c_ws_plugin__s2member_utils_conds::is_multisite_farm() && !is_main_site())
  97. {
  98. $ID = $data[0];
  99.  
  100. $user_login = (is_multisite()) ? strtolower($data[1]) : $data[1];
  101. $user_login = preg_replace("/\s+/", "", sanitize_user($user_login, is_multisite()));
  102. $user_pass = (string)"";
  103.  
  104. $first_name = $data[2];
  105. $last_name = $data[3];
  106. $display_name = $data[4];
  107.  
  108. $user_email = sanitize_email($data[5]);
  109. $user_url = $data[6];
  110.  
  111. $role = $data[7];
  112. $custom_capabilities = $data[8];
  113.  
  114. $user_registered = ($data[9]) ? date("Y-m-d H:i:s", strtotime($data[9])) : "";
  115. $paid_registration_times = ($data[10]) ? maybe_unserialize($data[10]) : "";
  116. $last_payment_time = ($data[11]) ? strtotime($data[11]) : "";
  117. $auto_eot_time = ($data[12]) ? strtotime($data[12]) : "";
  118.  
  119. $custom = $data[13];
  120. $subscr_id = $data[14];
  121. $subscr_gateway = strtolower($data[15]);
  122.  
  123. $custom_fields = /* Initialize. */ array();
  124. if /* Now loop through Custom Fields. */(count($data) > 16)
  125. for($i = 16, $j = 0; $i < count($data); $i++, $j++)
  126. if /* A field in this position? */ (isset($custom_field_vars[$j]))
  127. $custom_fields[$custom_field_vars[$j]] = maybe_unserialize($data[$i]);
  128. }
  129. else // Otherwise, we use the standardized format for importation.
  130. {
  131. $ID = $data[0];
  132.  
  133. $user_login = (is_multisite()) ? strtolower($data[1]) : $data[1];
  134. $user_login = preg_replace("/\s+/", "", sanitize_user($user_login, is_multisite()));
  135. $user_pass = $data[2];
  136.  
  137. $first_name = $data[3];
  138. $last_name = $data[4];
  139. $display_name = $data[5];
  140.  
  141. $user_email = sanitize_email($data[6]);
  142. $user_url = $data[7];
  143.  
  144. $role = $data[8];
  145. $custom_capabilities = $data[9];
  146.  
  147. $user_registered = ($data[10]) ? date("Y-m-d H:i:s", strtotime($data[10])) : "";
  148. $paid_registration_times = ($data[11]) ? maybe_unserialize($data[11]) : "";
  149. $last_payment_time = ($data[12]) ? strtotime($data[12]) : "";
  150. $auto_eot_time = ($data[13]) ? strtotime($data[13]) : "";
  151.  
  152. $custom = $data[14];
  153. $subscr_id = $data[15];
  154. $subscr_gateway = strtolower($data[16]);
  155.  
  156. $custom_fields = /* Initialize. */ array();
  157. if /* Now loop through Custom Fields. */(count($data) > 17)
  158. for($i = 17, $j = 0; $i < count($data); $i++, $j++)
  159. if /* A field in this position? */ (isset($custom_field_vars[$j]))
  160. $custom_fields[$custom_field_vars[$j]] = maybe_unserialize($data[$i]);
  161. }
  162. $role = (is_numeric($role)) ? (($role == 0) ? "subscriber" : "s2member_level".$role) : $role;
  163.  
  164. if($paid_registration_times && !is_array($paid_registration_times))
  165. $paid_registration_times = array("level" => strtotime($paid_registration_times));
  166. $paid_registration_times = (!$paid_registration_times || !is_array($paid_registration_times)) ? array(): $paid_registration_times;
  167.  
  168. $user_details = compact("ID", "user_login", "user_pass", "first_name", "last_name", "display_name", "user_email", "user_url", "role", "user_registered");
  169. if(empty($user_details["user_pass"])) // If there was NO Password given.
  170. unset($user_details["user_pass"]); // Unset the Password array element.
  171.  
  172. if($ID) // Are we dealing with an existing User ID?
  173. {
  174. if(is_object($user = new WP_User($ID)) && $user->ID) // Is this User in the database?
  175. {
  176. if(!is_multisite() || is_user_member_of_blog($ID)) // Must be a Member of this Blog.
  177. {
  178. if((!is_multisite() || !is_super_admin($ID)) && !$user->has_cap("administrator"))
  179. {
  180. if(strtolower($role) !== "administrator") // Do NOT update to Administrator.
  181. {
  182. if($user_email && is_email($user_email)) // Is the email address valid?
  183. {
  184. if($user_login) // Has a Username (aka: user_login) been supplied?
  185. {
  186. if(validate_username($user_login)) // Is the Username valid?
  187. {
  188. if(($_same_email = (strtolower($user_email) === strtolower($user->user_email))) || !email_exists($user_email))
  189. {
  190. if(($_same_login = (strtolower($user_login) === strtolower($user->user_login))) || !username_exists($user_login))
  191. {
  192. if(!is_multisite() || ($_same_email && $_same_login) || (($_ = wpmu_validate_user_signup($user_login, $user_email)) && (!is_wp_error($_["errors"]) || !$_["errors"]->get_error_code())))
  193. {
  194. if(is_multisite() && c_ws_plugin__s2member_utils_conds::is_multisite_farm() && !is_main_site())
  195. unset($user_details["user_login"], $user_details["user_pass"]);
  196.  
  197. if(($user_id = wp_update_user($user_details)))
  198. {
  199. update_user_option($user_id, "s2member_custom", $custom);
  200. update_user_option($user_id, "s2member_subscr_id", $subscr_id);
  201. update_user_option($user_id, "s2member_subscr_gateway", $subscr_gateway);
  202. update_user_option($user_id, "s2member_auto_eot_time", $auto_eot_time);
  203. update_user_option($user_id, "s2member_paid_registration_times", $paid_registration_times);
  204. update_user_option($user_id, "s2member_last_payment_time", $last_payment_time);
  205. update_user_option($user_id, "s2member_custom_fields", $custom_fields);
  206.  
  207. foreach($user->allcaps as $cap => $cap_enabled)
  208. if(preg_match("/^access_s2member_ccap_/", $cap))
  209. $user->remove_cap($ccap = $cap);
  210.  
  211. if($custom_capabilities && preg_replace("/^-all[\r\n\t\s;,]*/", "", str_replace("+", "", $custom_capabilities)))
  212. foreach(preg_split("/[\r\n\t\s;,]+/", preg_replace("/^-all[\r\n\t\s;,]*/", "", str_replace("+", "", $custom_capabilities))) as $ccap)
  213. if(strlen($ccap = trim(strtolower(preg_replace("/[^a-z_0-9]/i", "", $ccap)))))
  214. $user->add_cap("access_s2member_ccap_".$ccap);
  215.  
  216. $imported = $imported + 1;
  217. }
  218. else
  219. $errors[] = "Line #".$line.". User ID# <code>".$ID."</code> could NOT be updated. Unknown error, please try again.";
  220. }
  221. else
  222. $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.";
  223. }
  224. else
  225. $errors[] = "Line #".$line.". Conflicting. The Username (<code>".esc_html($user_login)."</code>), already exists.";
  226. }
  227. else
  228. $errors[] = "Line #".$line.". Conflicting. The Email address (<code>".esc_html($user_email)."</code>), already exists.";
  229. }
  230. else
  231. $errors[] = "Line #".$line.". Invalid Username (<code>".esc_html($user_login)."</code>). Lowercase alphanumerics are required.";
  232. }
  233. else
  234. $errors[] = "Line #".$line.". Missing Username; please try again."; // We have two separate errors for Usernames. This provides clarity.
  235. }
  236. else
  237. $errors[] = "Line #".$line.". Missing or invalid Email address (<code>".esc_html($user_email)."</code>); please try again.";
  238. }
  239. else
  240. $errors[] = "Line #".$line.". User ID# <code>".$ID."</code> cannot be updated to an Administrator. Bypassing this line for security.";
  241. }
  242. else
  243. $errors[] = "Line #".$line.". User ID# <code>".$ID."</code> belongs to an Administrator. Bypassing this line for security.";
  244. }
  245. else
  246. $errors[] = "Line #".$line.". User ID# <code>".$ID."</code> does NOT belong to an existing User on this site.";
  247. }
  248. else
  249. $errors[] = "Line #".$line.". User ID# <code>".$ID."</code> does NOT belong to an existing User.";
  250. }
  251.  
  252. 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))
  253. {
  254. if(strtolower($role) !== "administrator") // Do NOT add existing Users as Administrators.
  255. {
  256. if(add_existing_user_to_blog(array("user_id" => $user_id, "role" => $role)))
  257. {
  258. if(is_object($user = new WP_User($user_id)) && $user->ID)
  259. {
  260. update_user_option($user_id, "s2member_custom", $custom);
  261. update_user_option($user_id, "s2member_subscr_id", $subscr_id);
  262. update_user_option($user_id, "s2member_subscr_gateway", $subscr_gateway);
  263. update_user_option($user_id, "s2member_auto_eot_time", $auto_eot_time);
  264. update_user_option($user_id, "s2member_paid_registration_times", $paid_registration_times);
  265. update_user_option($user_id, "s2member_last_payment_time", $last_payment_time);
  266. update_user_option($user_id, "s2member_custom_fields", $custom_fields);
  267.  
  268. foreach($user->allcaps as $cap => $cap_enabled)
  269. if(preg_match("/^access_s2member_ccap_/", $cap))
  270. $user->remove_cap($ccap = $cap);
  271.  
  272. if($custom_capabilities && preg_replace("/^-all[\r\n\t\s;,]*/", "", str_replace("+", "", $custom_capabilities)))
  273. foreach(preg_split("/[\r\n\t\s;,]+/", preg_replace("/^-all[\r\n\t\s;,]*/", "", str_replace("+", "", $custom_capabilities))) as $ccap)
  274. if(strlen($ccap = trim(strtolower(preg_replace("/[^a-z_0-9]/i", "", $ccap)))))
  275. $user->add_cap("access_s2member_ccap_".$ccap);
  276.  
  277. $imported = $imported + 1;
  278. }
  279. else
  280. $errors[] = "Line #".$line.". Unknown object error, please try again.";
  281. }
  282. else
  283. $errors[] = "Line #".$line.". Unknown User/site addition error, please try again.";
  284. }
  285. else
  286. $errors[] = "Line #".$line.". Role cannot be Administrator. Bypassing this line for security.";
  287. }
  288.  
  289. else // Otherwise, we are adding a brand new User.
  290. {
  291. if(strtolower($role) !== "administrator") // Admin?
  292. {
  293. if($user_email && is_email($user_email) /* Valid? */)
  294. {
  295. if($user_login) // Was a Username even supplied?
  296. {
  297. if(validate_username($user_login)) // Is it valid?
  298. {
  299. if(!email_exists($user_email) /* Exists already? */)
  300. {
  301. if(!username_exists($user_login) /* Exists? */)
  302. {
  303. if(!is_multisite() || (($_ = wpmu_validate_user_signup($user_login, $user_email)) && (!is_wp_error($_["errors"]) || !$_["errors"]->get_error_code())))
  304. {
  305. if(($user_id = wp_insert_user($user_details)))
  306. {
  307. if(is_object($user = new WP_User($user_id)) && $user->ID)
  308. {
  309. if($user_pass) // If we are given an "un-encrypted Password".
  310. wp_update_user(array("ID" => $user_id, "user_pass" => $user_pass));
  311.  
  312. if(is_multisite()) // New Users on a Multisite Network need this too.
  313. update_user_meta($user_id, "s2member_originating_blog", $current_blog->blog_id);
  314.  
  315. update_user_option($user_id, "s2member_custom", $custom);
  316. update_user_option($user_id, "s2member_subscr_id", $subscr_id);
  317. update_user_option($user_id, "s2member_subscr_gateway", $subscr_gateway);
  318. update_user_option($user_id, "s2member_auto_eot_time", $auto_eot_time);
  319. update_user_option($user_id, "s2member_paid_registration_times", $paid_registration_times);
  320. update_user_option($user_id, "s2member_last_payment_time", $last_payment_time);
  321. update_user_option($user_id, "s2member_custom_fields", $custom_fields);
  322.  
  323. foreach($user->allcaps as $cap => $cap_enabled)
  324. if(preg_match("/^access_s2member_ccap_/", $cap))
  325. $user->remove_cap($ccap = $cap);
  326.  
  327. if($custom_capabilities && preg_replace("/^-all[\r\n\t\s;,]*/", "", str_replace("+", "", $custom_capabilities)))
  328. foreach(preg_split("/[\r\n\t\s;,]+/", preg_replace("/^-all[\r\n\t\s;,]*/", "", str_replace("+", "", $custom_capabilities))) as $ccap)
  329. if(strlen($ccap = trim(strtolower(preg_replace("/[^a-z_0-9]/i", "", $ccap)))))
  330. $user->add_cap("access_s2member_ccap_".$ccap);
  331.  
  332. $imported = $imported + 1;
  333. }
  334. else
  335. $errors[] = "Line #".$line.". Unknown object error, please try again.";
  336. }
  337. else
  338. $errors[] = "Line #".$line.". Unknown insertion error, please try again.";
  339. }
  340. else
  341. $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.";
  342. }
  343. else
  344. $errors[] = "Line #".$line.". Conflicting. The Username (<code>".esc_html($user_login)."</code>), already exists.";
  345. }
  346. else
  347. $errors[] = "Line #".$line.". Conflicting. The Email address (<code>".esc_html($user_email)."</code>), already exists.";
  348. }
  349. else
  350. $errors[] = "Line #".$line.". Invalid Username (<code>".esc_html($user_login)."</code>). Lowercase alphanumerics are required.";
  351. }
  352. else
  353. $errors[] = "Line #".$line.". Missing Username; please try again."; // We have two separate errors for Usernames. This provides clarity.
  354. }
  355. else
  356. $errors[] = "Line #".$line.". Missing or invalid Email address (<code>".esc_html($user_email)."</code>); please try again.";
  357. }
  358. else
  359. $errors[] = "Line #".$line.". Role cannot be Administrator. Bypassing this line for security.";
  360. }
  361. }
  362.  
  363. fclose($file);
  364. }
  365. else
  366. $errors[] = "No data was received. Please try again."; // The upload failed, or it was empty.
  367.  
  368. c_ws_plugin__s2member_admin_notices::display_admin_notice('Operation complete. Users/Members imported: <code>'.(int)$imported.'</code>.');
  369.  
  370. 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.
  371. 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);
  372. }
  373.  
  374. return /* Return for uniformity. */;
  375. }
  376. /**
  377. * Handles the importation of options.
  378. *
  379. * @package s2Member\Imports
  380. * @since 110815
  381. *
  382. * @return null
  383. */
  384. public static function import_ops()
  385. {
  386. 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"))
  387. {
  388. @set_time_limit(0); // Make time for processing large import files.
  389. @ini_set("memory_limit", apply_filters("admin_memory_limit", WP_MAX_MEMORY_LIMIT));
  390.  
  391. if(!empty($_FILES["ws_plugin__s2member_pro_import_ops_file"]) && empty($_FILES["ws_plugin__s2member_pro_import_ops_file"]["error"]))
  392. $file = file_get_contents($_FILES["ws_plugin__s2member_pro_import_ops_file"]["tmp_name"], "r");
  393.  
  394. if(!empty($file)) // Only process if we have an importation file.
  395. {
  396. if(is_array($import = c_ws_plugin__s2member_pro_utils_ops::op_replace(@unserialize($file), true)) && !empty($import) && ($import["configured"] = "1"))
  397. {
  398. unset($import["options_checksum"], $import["options_version"]);
  399.  
  400. foreach($import as $key => $value) // Add prefixes now.
  401. {
  402. (is_array($value)) ? array_unshift($value, "update-signal") : null;
  403. $import["ws_plugin__s2member_".$key] = $value;
  404. unset($import[$key]);
  405. }
  406.  
  407. c_ws_plugin__s2member_menu_pages::update_all_options($import, true, true, false, false, false);
  408. }
  409. else
  410. $errors[] = "Invalid data received. Please try again."; // Unserialization failed?
  411. }
  412. else
  413. $errors[] = "No data was received. Please try again."; // The upload failed, or it was empty.
  414.  
  415. 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.
  416. 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);
  417. else
  418. c_ws_plugin__s2member_admin_notices::display_admin_notice('Operation complete. Options imported.');
  419. }
  420. return /* Return for uniformity. */;
  421. }
  422. }
  423. }
  424. ?>

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

Source Code Documentation