96dcc41a5bae415384477ce3353103d4085ca935
[atutor.git] / mods / certify / download_certificate.php
1 <?php
2 define('AT_INCLUDE_PATH', '../../include/');
3
4 require (AT_INCLUDE_PATH.'vitals.inc.php');
5 require 'certify_functions.php';
6
7 // authenticate(AT_PRIV_CERTIFY); // TODO: Find correct privileges
8
9 if (isset($_GET['certify_id']))
10         $certify_id     = $addslashes($_GET['certify_id']);
11
12 $filebase = AT_CONTENT_DIR .'certify/cert_'.$_SESSION['member_id'].'_'.$certify_id.'.';
13 $templatefile = AT_CONTENT_DIR .'certify/template_'.$certify_id.'.pdf';
14 if (file_exists($templatefile)) {
15         $template = $templatefile;
16 } else {
17         $template = dirname(realpath('test.pdf')).'/mods/certify/test.pdf';
18 }
19
20
21 if (!file_exists($filebase.'pdf')) {
22         // Fetch cached scores
23         
24         $sql =  '
25         
26                 SELECT
27                         '.TABLE_PREFIX.'courses.title AS coursetitle,
28                         '.TABLE_PREFIX.'members.first_name,
29                         '.TABLE_PREFIX.'members.second_name,
30                         '.TABLE_PREFIX.'members.last_name,
31                         '.TABLE_PREFIX.'members.email,
32                         '.TABLE_PREFIX.'certify.title AS certifytitle
33         
34                 FROM '.TABLE_PREFIX.'members
35                 INNER JOIN '.TABLE_PREFIX.'certify ON '.TABLE_PREFIX.'certify.certify_id = '.$certify_id.'
36                 INNER JOIN '.TABLE_PREFIX.'courses ON '.TABLE_PREFIX.'certify.course_id = '.TABLE_PREFIX.'courses.course_id
37         
38                 WHERE '.TABLE_PREFIX.'members.member_id = '.$_SESSION['member_id'].' 
39         ';
40
41         //echo $sql;
42         //exit();
43
44         $result = mysql_query($sql, $db) or die(mysql_error() . $sql);
45         
46         if ( !$row = mysql_fetch_assoc($result)) { // Probably a hack attempt, so aborting should be sufficient
47                 echo "Oh no you don't!";
48                 exit();
49         }
50
51         if ( getCertificateProgress($_SESSION['member_id'], $certify_id)<100 ) { // Probably a hack attempt, so aborting should be sufficient
52                 echo "Oh no you don't!";
53                 exit();
54         }
55
56         // Generate FDF
57
58
59         $params = array(
60         //      'course_name'   => iconv("UTF-8", "ISO-8859-1//IGNORE", $row['coursetitle']),
61                 'full_name'             => iconv("UTF-8", "ISO-8859-1//IGNORE", implode(' ',array($row['first_name'],$row['second_name'],$row['last_name']))),
62                 'test_name'             => iconv("UTF-8", "ISO-8859-1//IGNORE", $row['certifytitle']),
63         //      'score'                 => iconv("UTF-8", "ISO-8859-1//IGNORE", 'Bestått'),
64                 'issued_date'   => iconv("UTF-8", "ISO-8859-1//IGNORE", date('F j, Y'))
65         );
66         
67         $fdfparams = '';
68         foreach ($params as $key => $value) {
69                 $fdfparams .= '<</T('.$key.')/V('.$value.')>>';
70         }
71         
72         //$filename = tempnam('', 'atutor_certify');
73         $filename = $filebase.'fdf';
74
75         $handle = fopen($filename,'wb');
76         fwrite($handle,"%FDF-1.2
77 %\xE2\xE3\xCF\xD3
78 1 0 obj
79 << 
80 /FDF << /Fields [ ".$fdfparams."] 
81 /F (http://www.helsekompetanse.no/test.pdf) /ID [ <".md5(time()).">
82 ] >> 
83 >> 
84 endobj
85 trailer
86 <<
87 /Root 1 0 R 
88
89 >>
90 %%EOF");
91         fclose($handle);
92         
93         // Flatten with PDF
94
95         $output = array();
96         $return_var = 0;
97         
98 //      $exec = '/opt/local/bin/pdftk '.$template.' fill_form '.$filename.' output '.$filebase.'pdf flatten';
99         $exec = '/usr/bin/pdftk '.$template.' fill_form '.$filename.' output '.$filebase.'pdf flatten';
100         exec($exec, $output, $return_var);
101
102         //unlink($filename);
103 }
104
105 if (file_exists($filebase.'pdf')) {
106
107   // Send PDF
108   header('Content-Description: File Transfer');
109   header('Content-Type: application/pdf');
110   header('Content-Disposition: attachment; filename="'.basename($filebase.'pdf').'"'); // TODO: Fix better filename
111   header('Content-Transfer-Encoding: binary');
112   header('Expires: 0');
113   header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
114   header('Pragma: public');
115   header('Content-Length: ' . filesize($filebase.'pdf'));
116   //ob_clean();
117   //flush();
118   readfile($filebase.'pdf');
119
120 } else {
121   echo "PDFTK failed - not installed or wrong path?";
122   exit;
123 }
124 ?>