/
members_bulkpics.php
executable file
·139 lines (121 loc) · 4.19 KB
/
members_bulkpics.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
// --------------------------------------------------------------------
// members_bulkpics.php -- Uploads pictures in bulk.
//
// Created: 1/2/15 DLB -- Hacked from Admin and Epic Scouts
// --------------------------------------------------------------------
require_once "libs/all.php";
session_start();
log_page();
CheckLogin();
CheckEditor();
$timer = new timer();
$loc = 'members_bulkpics.php';
$ins_file = "docs/importpics.txt";
$error_msg = "";
$success_msg = "";
$instructions = "";
if(file_exists($ins_file)) { $instructions = file_get_contents($ins_file); }
if( $_SERVER["REQUEST_METHOD"] == "POST")
{
if(!isset($_FILES["CsvFile"])) { DieWithMsg($loc, '$_FILES not set.'); }
$fileinfo = $_FILES["CsvFile"];
$filesize = $fileinfo["size"];
$filetype = $fileinfo["type"];
$fileerr = $fileinfo["error"];
$tempfile = $fileinfo["tmp_name"];
if(empty($tempfile) || $fileerr == 4)
{
$error_msg = "No input file given.";
}
else if($filesize <= 0)
{
$error_msg = "Input file was empty.";
}
else
{
ProcessBulkPics($tempfile, $error_msg);
}
}
include "forms/header.php";
include "forms/navform.php";
include "forms/members_menubar.php";
include "forms/members_bulkpics_form.php";
include "forms/footer.php";
// --------------------------------------------------------------------
function ProcessBulkPics($filename, &$error_msg)
{
global $config;
$loc = "members_bulkpics.php->ProcessBulkPics";
$file = fopen($filename, "r");
if($file === false)
{
$error_msg = "Unable to open file.";
return;
}
$n_okay = 0;
$n_fail = 0;
$ln = 1;
// The first line is the column headers.
$header = fgetcsv($file); $ln++;
if($header === false) return $n;
// Now, do some sanity checks to make sure we have
// an appropriate file.
if(!in_array("UserName", $header) ||
!in_array("Picture", $header))
{
$error_msg = "Input file does not required columns.";
return;
}
$tstart = microtime(true); // Time the entire operation... Don't go over 4 minutes.
$btimeout = false;
while(true)
{
$result = set_time_limit(60);
if($result == false) {log_error($loc, "Unable to set/reset time limit to 20 seconds."); }
$data = fgetcsv($file); $ln++;
if($data === false) break;
// Don't process blank lines.
if(count($data) <= 0) continue;
if(is_null($data[0])) continue;
// Organize the data into an associtive array
$fields = JoinKeyValues($header, $data);
// Skip lines that don't have required data
if(!isset($fields["UserName"]) ||
!isset($fields["Picture"]))
{
continue;
}
$username = $fields["UserName"];
$picfile = $fields["Picture"];
if(empty($username) || empty($picfile)) continue;
$result = AddPictureToUser($username, $picfile);
if($result === true) $n_okay++;
else $n_fail++;
$telp = (microtime(true) - $tstart);
if($telp > 240.0) {$btimeout = true; break; }
}
$error_msg = $n_okay . ' pictures imported. ' . $n_fail . ' failures. ' . $ln . ' lines processed.';
if($btimeout) {$error_msg .= ' ** TimeOut Occured, Process aborted. **'; }
log_msg($loc, $error_msg);
}
function AddPictureToUser($username, $source)
{
$loc = "members_bulkpics.php->AddPIctureToUser";
$userid = GetUserIDFromName($username);
$userinfo = GetUserInfo($userid);
if($userinfo === false) DieWithMsg($loc, 'User with ID=' . $userid . ' not found, but should be there.');
// Copy the file into our website.
$target = GetTempDir() . "temppic.jpg";
$result = @copy($source, $target);
if($result == false)
{
log_msg($loc, array('Picture not added. Unable to copy file.',
'External File=' . $source,
'Internal Target=' . $target));
return false;
}
$id = StoreUserPic($target, $userid);
return true;
}
?>