-
Notifications
You must be signed in to change notification settings - Fork 35
/
csv.php
executable file
·149 lines (115 loc) · 4.21 KB
/
csv.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
140
141
142
143
144
145
146
147
148
149
<?php session_start();
require_once('library/odf.php');
$id = uniqid(); //To be used with filenames to differentiate simultaneous files being processed
$csv = $id.$_FILES["file"]["name"];
/******************************** csv File input validation********************************************/
//Link to other file in case any of folllowing conditions fail
$url = "<meta http-equiv='Refresh' content='3; URL=option.php?var=csv'>";
if($_FILES["file"]["name"] == NULL) //checks if no file is selected
{
echo "<center><h2>NO (.csv) File Selected</h2></center>";
echo $url;
exit;
}
else
{
$ext = strrchr($csv,".");
if($ext != ".csv") //checks if csv format file is not selected
{
echo "<center><h2>Invalid File Format for .csv file...</h2></center>";
echo $url;
exit;
}
move_uploaded_file($_FILES["file"]["tmp_name"],"uploads/csv/data/".$csv);
}
$base = $_SESSION["base"]; //Getting file name with filled Institute Details
$odf = new odf("odt/base/$base.odt"); //Initializing the object with above file name
$file = $_FILES["photo"]["name"];
if($file!=NULL)
{
chdir('uploads/csv/');
exec("mkdir $id"); //MAking folder to store the compressed file and then ectract it.
move_uploaded_file($_FILES["photo"]["tmp_name"],"$id/$file"); //storing the compressed folder for images in
$extension = strrchr($file,"."); //using strrchr() to fetch the extension of the file
chdir("$id"); //Changing the directory to extract the files at that location
if ($extension == ".gz")
{
exec("tar -zxvf $file"); //extracting the tar.gz file;
}
elseif($extension == ".zip")
{
exec("unzip $file"); //extracting the zip file;
}
else
{
echo "<center><strong>Invalid File Format for compressed images Folder.</strong></center>";
echo $url;
exit;
}
chdir('../../..'); //changing directory back to previous
$dest = strtok($_FILES["photo"]["name"],"."); //using strtok for storing the folder Name after extraction
unlink("uploads/csv/$id/$file"); //After Extracting Deleting the compressed file
}
$csvfile = fopen("uploads/csv/data/$csv","r"); //Opening csv file in read mode
//Variable initialised to trace line Number being processed
$lineNumber = 1;
//Variable initialised to trace if any error in csv file occurs
$errorExist = 0;
$article = $odf->setSegment('articles'); //Defining Segment articles( used in .odt file)
//Fetching data in each row of csv file to array $result
while(($result = fgetcsv($csvfile,1000, ","))!==FALSE)
{
//image
if($_FILES["photo"]["name"]!=NULL)
{
$pic = "uploads/csv/$id/$dest/".$result[7];
if(!file_exists($pic))
{
$pic = "uploads/manual/image.gif";
}
$article->setImage('pic',$pic,4);
}
else $article->pic(" ");
//name
if($result[2] == NULL)
$article->nameArticle(" ".$result[0]." ".$result[1]." ".$result[3]);
else
$article->nameArticle(" ".$result[0]." ".$result[1]." ".$result[2]." ".$result[3]);
//Institute/Department Name
if($result[5] == NULL)
$article->deptArticle($result[4].", ".$result[6]);
else
$article->deptArticle($result[4].", ".$result[5]);
$article->merge(); //Ending the current segment
}
$odf->mergeSegment($article); //Ending the segment Object
// Original ODT file.
$source_file = "odt/cert/$id.odt";
// We save the file
$odf -> saveToDisk($source_file);
$output_file = "pdf/$id.pdf";
$command = '/usr/bin/unoconv -o '.$output_file.' -f pdf '.$source_file;
$result = shell_exec($command);
echo $result;
echo '<html>
<head>
<link href="style/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="style/style.css" rel="stylesheet" media="screen">
</head>
<body>
<center>
<h1>Your Certificate has been Generated!</h1>
<form action="odt/cert/'.$id.'.odt">
<input class="btn btn-primary" type="submit" value="Download ODT">
</form>
<form action="pdf/'.$id.'.pdf">
<input class="btn btn-primary" type="submit" value="View/Download PDF">
</form>
<form action="index.html">
<input class="btn btn-primary" type="submit" value="Goto First Page">
</form>
</center>
</body>
</html>';
exit;
?>