PageRenderTime 75ms CodeModel.GetById 42ms RepoModel.GetById 1ms app.codeStats 0ms

/lists_old/admin/import1.php

https://github.com/condesan/infoandina
PHP | 366 lines | 291 code | 45 blank | 30 comment | 96 complexity | 64e44ef6a2b8774ee3b279dbfdefb330 MD5 | raw file
  1. <?php
  2. require_once dirname(__FILE__).'/accesscheck.php';
  3. $subselect = '';
  4. if (!ALLOW_IMPORT) {
  5. print '<p>'.$GLOBALS['I18N']->get('import is not available').'</p>';
  6. return;
  7. }
  8. print '<script language="Javascript" src="js/progressbar.js" type="text/javascript"></script>';
  9. ignore_user_abort();
  10. set_time_limit(500);
  11. ob_end_flush();
  12. ?>
  13. <p>
  14. <?php
  15. if (!isset($GLOBALS["tmpdir"])) {
  16. $GLOBALS["tmpdir"] = ini_get("upload_tmp_dir");
  17. }
  18. if (!is_dir($GLOBALS["tmpdir"]) || !is_writable($GLOBALS["tmpdir"])) {
  19. $GLOBALS["tmpdir"] = ini_get("upload_tmp_dir");
  20. }
  21. #if (ini_get("open_basedir")) {
  22. if (!is_dir($GLOBALS["tmpdir"]) || !is_writable($GLOBALS["tmpdir"])) {
  23. Warn($GLOBALS['I18N']->get('temp_dir_not_writeable')." (".$GLOBALS["tmpdir"].")");
  24. }
  25. if(isset($_REQUEST['import'])) {
  26. $test_import = (isset($_POST["import_test"]) && $_POST["import_test"] == "yes");
  27. /*
  28. if (!is_array($_POST["lists"]) && !$test_import) {
  29. Fatal_Error($GLOBALS['I18N']->get('select_list'));
  30. return;
  31. }
  32. */
  33. if(empty($_FILES["import_file"])) {
  34. Fatal_Error($GLOBALS['I18N']->get('none_specified'));
  35. return;
  36. }
  37. if(!$_FILES["import_file"]) {
  38. Fatal_Error($GLOBALS['I18N']->get('too_large_inexistant'));
  39. return;
  40. }
  41. if (filesize($_FILES["import_file"]['tmp_name']) > 1000000) {
  42. Fatal_Error($GLOBALS['I18N']->get('too_big'));
  43. return;
  44. }
  45. if( !preg_match("/^[0-9A-Za-z_\.\-\/\s \(\)]+$/", $_FILES["import_file"]["name"]) ) {
  46. Fatal_Error($GLOBALS['I18N']->get('wrong_characters').$_FILES["import_file"]["name"]);
  47. return;
  48. }
  49. if (!$_POST["notify"] && !$test_import) {
  50. Fatal_Error($GLOBALS['I18N']->get('signup_or_notify'));
  51. return;
  52. }
  53. $notify = $_POST["notify"];
  54. if ($_FILES["import_file"] && filesize($_FILES["import_file"]['tmp_name']) > 10) {
  55. $newfile = $GLOBALS['tmpdir'].'/'. $_FILES['import_file']['name'].time();
  56. move_uploaded_file($_FILES['import_file']['tmp_name'], $newfile);
  57. if( !($fp = fopen ($newfile, "r"))) {
  58. Fatal_Error($GLOBALS['I18N']->get('unreadable')." (".$newfile.")");
  59. return;
  60. }
  61. $email_list = fread($fp, filesize ($newfile));
  62. fclose($fp);
  63. } elseif ($_FILES["import_file"]) {
  64. Fatal_Error($GLOBALS['I18N']->get('empty_file'));
  65. return;
  66. }
  67. // Clean up email file
  68. $email_list = trim($email_list);
  69. $email_list = str_replace("\r","\n",$email_list);
  70. $email_list = str_replace("\n\r","\n",$email_list);
  71. $email_list = str_replace("\n\n","\n",$email_list);
  72. if (isset($_REQUEST['import_record_delimiter'])) {
  73. $import_record_delimiter = $_REQUEST['import_record_delimiter'];
  74. } else {
  75. $import_record_delimiter = "\n";
  76. }
  77. // Change delimiter for new line.
  78. if(isset($import_record_delimiter) && $import_record_delimiter != "" && $import_record_delimiter != "\n") {
  79. $email_list = str_replace($import_record_delimiter,"\n",$email_list);
  80. };
  81. if (!isset($import_field_delimiter) || $import_field_delimiter == "" || $import_field_delimiter == "TAB")
  82. $import_field_delimiter = "\t";
  83. // Check file for illegal characters
  84. $illegal_cha = array(",", ";", ":", "#","\t");
  85. for($i=0; $i<count($illegal_cha); $i++) {
  86. if( ($illegal_cha[$i] != $import_field_delimiter) && ($illegal_cha[$i] != $import_record_delimiter) && (strpos($email_list, $illegal_cha[$i]) != false) ) {
  87. Fatal_Error($GLOBALS['I18N']->get('invalid_delimiter')." $import_field_delimiter, $import_record_delimiter");return;
  88. }
  89. };
  90. // Split file/emails into array
  91. $email_list = explode("\n",$email_list);
  92. // Parse the lines into records
  93. $hasinfo = 0;
  94. foreach ($email_list as $line) {
  95. $uservalues = explode($import_field_delimiter,$line);
  96. $email = trim(array_shift($uservalues));
  97. $info = join(" ",$uservalues);
  98. $hasinfo = $hasinfo || $info != "";
  99. $user_list[$email] = array (
  100. "info" => $info
  101. );
  102. }
  103. if (sizeof($email_list) > 300 && !$test_import) {
  104. # this is a possibly a time consuming process, so lets show a progress bar
  105. print '<script language="Javascript" type="text/javascript"> document.write(progressmeter); start();</script>';
  106. flush();
  107. # increase the memory to make sure we are not running out
  108. ini_set("memory_limit","16M");
  109. }
  110. // View test output of emails
  111. if($test_import) {
  112. print $GLOBALS['I18N']->get('test_output').':<br>'.$GLOBALS['I18N']->get('one_email_per_line').'<br>'.$GLOBALS['I18N']->get('output_ok').' <a href="javascript:history.go(-1)">'.$GLOBALS['I18N']->get('back').'</a>'.$GLOBALS['I18N']->get('resubmit').'<br><br>';
  113. $i = 1;
  114. while (list($email,$data) = each ($user_list)) {
  115. $email = trim($email);
  116. if(strlen($email) > 4) {
  117. print "<b>$email</b><br>";
  118. $html = "";
  119. foreach (array("info") as $item)
  120. if ($user_list[$email][$item])
  121. $html .= "$item -> ".$user_list[$email][$item]."<br>";
  122. if ($html) print "<blockquote>$html</blockquote>";
  123. };
  124. if($i == 50) {break;};
  125. $i++;
  126. };
  127. // Do import
  128. } else {
  129. $count_email_add = 0;
  130. $count_email_exist = 0;
  131. $count_list_add = 0;
  132. if (isset($_REQUEST['lists']) && is_array($_REQUEST['lists'])) {
  133. $lists = $_REQUEST['lists'];
  134. } else {
  135. $lists = array();
  136. }
  137. $num_lists = sizeof($lists);
  138. $todo = sizeof($user_list);
  139. $done = 0;
  140. if ($hasinfo) {
  141. # we need to add an info attribute if it does not exist
  142. $req = Sql_Query("select id from ".$tables["attribute"]." where name = \"info\"");
  143. if (!Sql_Affected_Rows()) {
  144. # it did not exist
  145. Sql_Query(sprintf('insert into %s (name,type,listorder,default_value,required,tablename)
  146. values("info","textline",0,"",0,"info")', $tables["attribute"]));
  147. }
  148. }
  149. # which attributes were chosen, apply to all users
  150. $res = Sql_Query("select * from ".$tables["attribute"]);
  151. $attributes = array();
  152. while ($row = Sql_Fetch_Array($res)) {
  153. $fieldname = "attribute" .$row["id"];
  154. $attributes[$row["id"]] = $_POST[$fieldname];
  155. }
  156. while (list($email,$data) = each ($user_list)) {
  157. $done++;
  158. if ($done % 50 ==0) {
  159. print "$done/$todo<br/>";
  160. flush();
  161. }
  162. if(strlen($email) > 4) {
  163. $email = addslashes($email);
  164. // Annoying hack => Much too time consuming. Solution => Set email in users to UNIQUE()
  165. $result = Sql_query("SELECT id,uniqid FROM ".$tables["user"]." WHERE email = '$email'");
  166. if (Sql_affected_rows()) {
  167. // Email exist, remember some values to add them to the lists
  168. $user = Sql_fetch_array($result);
  169. $userid = $user["id"];
  170. $uniqid = $user["uniqid"];
  171. $history_entry = $GLOBALS['I18N']->get('import_user');
  172. $old_data = Sql_Fetch_Array_Query(sprintf('select * from %s where id = %d',$tables["user"],$userid));
  173. $old_data = array_merge($old_data,getUserAttributeValues('',$userid));
  174. # and membership of lists
  175. $req = Sql_Query("select * from {$tables["listuser"]} where userid = $userid");
  176. while ($row = Sql_Fetch_Array($req)) {
  177. $old_listmembership[$row["listid"]] = listName($row["listid"]);
  178. }
  179. $count_email_exist++;
  180. } else {
  181. // Email does not exist
  182. // Create unique number
  183. mt_srand((double)microtime()*1000000);
  184. $randval = mt_rand();
  185. include_once dirname(__FILE__)."/commonlib/lib/userlib.php";
  186. $uniqid = getUniqid();
  187. $query = sprintf('INSERT INTO %s (email,entered,confirmed,uniqid,htmlemail) values("%s",now(),%d,"%s","%s")',
  188. $tables["user"],$email,$notify != "yes",$uniqid,$htmlemail);
  189. $result = Sql_query($query);
  190. $userid = Sql_insert_id();
  191. $count_email_add++;
  192. $some = 1;
  193. $history_entry = $GLOBALS['I18N']->get('import_new_user');
  194. # add the attributes for this user
  195. reset($attributes);
  196. while (list($attr,$value) = each($attributes))
  197. Sql_query(sprintf('replace into %s (attributeid,userid,value) values("%s","%s","%s")',
  198. $tables["user_attribute"],$attr,$userid,addslashes($value)));
  199. }
  200. #add this user to the lists identified
  201. reset($lists);
  202. $addition = 0;
  203. $listoflists = "";
  204. while (list($key,$listid) = each($lists)) {
  205. $query = "replace INTO ".$tables["listuser"]." (userid,listid,entered) values($userid,$listid,now())";
  206. $result = Sql_query($query);
  207. # if the affected rows is 2, the user was already subscribed
  208. $addition = $addition || Sql_Affected_Rows() == 1;
  209. if (!empty($_POST['listname'][$key])) {
  210. $listoflists .= " * ".$_POST['listname'][$key]."\n";
  211. }
  212. }
  213. if ($addition) {
  214. $additional_emails++;
  215. }
  216. $subscribemessage = ereg_replace('\[LISTS\]', $listoflists, getUserConfig("subscribemessage",$userid));
  217. if (!TEST && $notify == "yes" && $addition)
  218. sendMail($email, getConfig("subscribesubject"), $subscribemessage,system_messageheaders(),$envelope);
  219. # history stuff
  220. $current_data = Sql_Fetch_Array_Query(sprintf('select * from %s where id = %d',$tables["user"],$userid));
  221. $current_data = array_merge($current_data,getUserAttributeValues('',$userid));
  222. foreach ($current_data as $key => $val) {
  223. if (!is_numeric($key))
  224. if ($old_data[$key] != $val && $key != "modified") {
  225. $history_entry .= "$key = $val\nchanged from $old_data[$key]\n";
  226. }
  227. }
  228. if (!$history_entry) {
  229. $history_entry = "\n".$GLOBALS['I18N']->get('no_data_changed');
  230. }
  231. # check lists
  232. $req = Sql_Query("select * from {$tables["listuser"]} where userid = $userid");
  233. while ($row = Sql_Fetch_Array($req)) {
  234. $listmembership[$row["listid"]] = listName($row["listid"]);
  235. }
  236. $history_entry .= "\n".$GLOBALS['I18N']->get('lists_subscriptions')."\n";
  237. foreach ($old_listmembership as $key => $val) {
  238. $history_entry .= $GLOBALS['I18N']->get('was_subscribed')." $val\n";
  239. }
  240. foreach ($listmembership as $key => $val) {
  241. $history_entry .= $GLOBALS['I18N']->get('is_subscribed')." $val\n";
  242. }
  243. if (!sizeof($listmembership)) {
  244. $history_entry .= $GLOBALS['I18N']->get('not_subscribed')."\n";
  245. }
  246. addUserHistory($email,$GLOBALS['I18N']->get('import_by').adminName(),$history_entry);
  247. }; // end if
  248. }; // end while
  249. print '<script language="Javascript" type="text/javascript"> finish(); </script>';
  250. # lets be gramatically correct :-)
  251. $displists = ($num_lists == 1) ? $GLOBALS['I18N']->get('list'): $GLOBALS['I18N']->get('lists');
  252. $dispemail = ($count_email_add == 1) ? $GLOBALS['I18N']->get('new_email_was'): $GLOBALS['I18N']->get('new_emails_were');
  253. $dispemail2 = ($additional_emails == 1) ? $GLOBALS['I18N']->get('email_was'): $GLOBALS['I18N']->get('emails_were');
  254. if ($count_email_exist) {
  255. print "<br/>$count_email_exist ".$GLOBALS['I18N']->get('some_emails_exist');
  256. }
  257. if(!$some && !$additional_emails) {
  258. print "<br>".$GLOBALS['I18N']->get('all_emails_exist');
  259. } else {
  260. print "$count_email_add $dispemail ".$GLOBALS['I18N']->get('import_successful')." $num_lists $displists.<br>$additional_emails $dispemail2 ".$GLOBALS['I18N']->get('subscribed')." $displists";
  261. }
  262. }; // end else
  263. print '<p>'.PageLink2("import",$GLOBALS['I18N']->get('import_more_emails')).'</p>';
  264. } else {
  265. ?>
  266. <ul>
  267. <?php echo FormStart(' enctype="multipart/form-data" name="import"')?>
  268. <?php
  269. if ($GLOBALS["require_login"] && !isSuperUser()) {
  270. $access = accessLevel("import1");
  271. switch ($access) {
  272. case "owner":
  273. $subselect = " where owner = ".$_SESSION["logindetails"]["id"];break;
  274. case "all":
  275. $subselect = "";break;
  276. case "none":
  277. default:
  278. $subselect = " where id = 0";break;
  279. }
  280. }
  281. $result = Sql_query("SELECT id,name FROM ".$tables["list"]."$subselect ORDER BY listorder");
  282. $c=0;
  283. if (Sql_Affected_Rows() == 1) {
  284. $row = Sql_fetch_array($result);
  285. printf('<input type=hidden name="listname[%d]" value="%s"><input type=hidden name="lists[%d]" value="%d">'.$GLOBALS['I18N']->get('adding_users').' <b>%s</b>',$c,stripslashes($row["name"]),$c,$row["id"],stripslashes($row["name"]));
  286. } else {
  287. print '<p>'.$GLOBALS['I18N']->get('select_lists').'</p>';
  288. while ($row = Sql_fetch_array($result)) {
  289. printf('<li><input type=hidden name="listname[%d]" value="%s"><input type=checkbox name="lists[%d]" value="%d">%s',$c,stripslashes($row["name"]),$c,$row["id"],stripslashes($row["name"]));
  290. $some = 1;$c++;
  291. }
  292. if (!$some)
  293. echo $GLOBALS['I18N']->get('no_lists').PageLink2("editlist",$GLOBALS['I18N']->get('add_list'));
  294. }
  295. ?>
  296. </ul>
  297. <script language="Javascript" type="text/javascript">
  298. var fieldstocheck = new Array();
  299. var fieldnames = new Array();
  300. function addFieldToCheck(value,name) {
  301. fieldstocheck[fieldstocheck.length] = value;
  302. fieldnames[fieldnames.length] = name;
  303. }
  304. </script>
  305. <table border="1">
  306. <tr><td colspan=2><?php echo $GLOBALS['I18N']->get('info_emails_file'); ?></td></tr>
  307. <tr><td><?php echo $GLOBALS['I18N']->get('emails_file'); ?></td><td><input type="file" name="import_file"></td></tr>
  308. <tr><td><?php echo $GLOBALS['I18N']->get('field_delimiter'); ?></td><td><input type="text" name="import_field_delimiter" size=5> <?php echo $GLOBALS['I18N']->get('tab_default'); ?></td></tr>
  309. <tr><td><?php echo $GLOBALS['I18N']->get('record_delimiter'); ?></td><td><input type="text" name="import_record_delimiter" size=5> <?php echo $GLOBALS['I18N']->get('line_break_default'); ?></td></tr>
  310. <tr><td colspan=2><?php echo $GLOBALS['I18N']->get('info_test_output'); ?></td></tr>
  311. <tr><td><?php echo $GLOBALS['I18N']->get('test_output'); ?></td><td><input type="checkbox" name="import_test" value="yes"></td></tr>
  312. <tr><td colspan=2><?php echo $GLOBALS['I18N']->get('info_notification_email'); ?></td></tr>
  313. <tr><td><?php echo $GLOBALS['I18N']->get('notification_email'); ?><input type="radio" name="notify" value="yes"></td><td><?php echo $GLOBALS['I18N']->get('confirmed_immediately'); ?><input type="radio" name="notify" value="no"></td></tr>
  314. <?php
  315. include_once dirname(__FILE__)."/subscribelib2.php";
  316. print ListAllAttributes();
  317. ?>
  318. <tr><td><input type="submit" name="import" value="<?php echo $GLOBALS['I18N']->get('import'); ?>"></td><td>&nbsp;</td></tr>
  319. </table>
  320. <?php } ?>
  321. </p>