ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCertificate.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2011 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 
16 {
22  protected $ctrl;
23 
29  protected $tree;
30 
36  protected $ilias;
37 
43  protected $tpl;
44 
50  protected $lng;
51 
57  protected $adapter;
58 
59  protected static $is_active;
60 
67  {
68  global $lng, $tpl, $ilCtrl, $ilias, $tree;
69 
70  $this->lng =& $lng;
71  $this->tpl =& $tpl;
72  $this->ctrl =& $ilCtrl;
73  $this->ilias =& $ilias;
74  $this->tree =& $tree;
75  $this->adapter =& $adapter;
76  }
77 
83  public function getBackgroundImagePath()
84  {
85  return $this->getAdapter()->getCertificatePath() . $this->getBackgroundImageName();
86  }
87 
93  public function getBackgroundImageName()
94  {
95  return "background.jpg";
96  }
97 
103  public function getBackgroundImageThumbPath()
104  {
105  return $this->getAdapter()->getCertificatePath() . $this->getBackgroundImageName() . ".thumb.jpg";
106  }
107 
114  {
115  return $this->getAdapter()->getCertificatePath() . "background_upload";
116  }
117 
123  public function getXSLPath()
124  {
125  return $this->getAdapter()->getCertificatePath() . $this->getXSLName();
126  }
127 
133  function getXSLName()
134  {
135  return "certificate.xml";
136  }
137 
143  public static function _getXSLName()
144  {
145  return "certificate.xml";
146  }
147 
153  public function getBackgroundImagePathWeb()
154  {
155  // TODO: this is generic now -> provide better solution
156  include_once "./Services/Utilities/classes/class.ilUtil.php";
157  $webdir = $this->getAdapter()->getCertificatePath() . $this->getBackgroundImageName();
159  }
160 
167  {
168  // TODO: this is generic now -> provide better solution
169  include_once "./Services/Utilities/classes/class.ilUtil.php";
171  }
172 
178  public function deleteBackgroundImage()
179  {
180  $result = TRUE;
181  if (file_exists($this->getBackgroundImageThumbPath()))
182  {
183  $result = $result & unlink($this->getBackgroundImageThumbPath());
184  }
185  if (file_exists($this->getBackgroundImagePath()))
186  {
187  $result = $result & unlink($this->getBackgroundImagePath());
188  }
189  if (file_exists($this->getBackgroundImageTempfilePath()))
190  {
191  $result = $result & unlink($this->getBackgroundImageTempfilePath());
192  }
193  return $result;
194  }
195 
201  public function cloneCertificate($newObject)
202  {
203  $xsl = $this->getXSLPath();
204  $bgimage = $this->getBackgroundImagePath();
205  $bgimagethumb = $this->getBackgroundImageThumbPath();
206  $certificatepath = $this->getAdapter()->getCertificatePath();
207 
208  $new_xsl = $newObject->getXSLPath();
209  $new_bgimage = $newObject->getBackgroundImagePath();
210  $new_bgimagethumb = $newObject->getBackgroundImageThumbPath();
211  $new_certificatepath = $newObject->getAdapter()->getCertificatePath();
212 
213  if (@file_exists($xsl))
214  {
215  ilUtil::makeDirParents($new_certificatepath);
216  @copy($xsl, $new_xsl);
217  if (@file_exists($bgimage)) @copy($bgimage, $new_bgimage);
218  if (@file_exists($bgimagethumb)) @copy($bgimagethumb, $new_bgimagethumb);
219  }
220  }
221 
227  public function deleteCertificate()
228  {
229  if (@file_exists($this->getAdapter()->getCertificatePath()))
230  {
231  include_once "./Services/Utilities/classes/class.ilUtil.php";
232  ilUtil::delDir($this->getAdapter()->getCertificatePath());
233  $this->getAdapter()->deleteCertificate();
234  }
235  $this->writeActive(false);
236  }
237 
241  public function getFormFieldsFromFO()
242  {
243  if (@file_exists($this->getXSLPath()))
244  {
245  $xslfo = file_get_contents($this->getXSLPath());
246  }
247  // retrieve form information (using a dirty way with regular expressions)
248  $pagewidth = "21cm";
249  if (preg_match("/page-width\=\"([^\"]+)\"/", $xslfo, $matches))
250  {
251  $pagewidth = $matches[1];
252  }
253  $pageheight = "29.7cm";
254  if (preg_match("/page-height\=\"([^\"]+)\"/", $xslfo, $matches))
255  {
256  $pageheight = $matches[1];
257  }
258  $certificatesettings = new ilSetting("certificate");
259  $pagesize = $certificatesettings->get("pageformat");;
260  if (((strcmp($pageheight, "29.7cm") == 0) || (strcmp($pageheight, "297mm") == 0)) && ((strcmp($pagewidth, "21cm") == 0) || (strcmp($pagewidth, "210mm") == 0)))
261  {
262  $pagesize = "a4";
263  }
264  else if (((strcmp($pagewidth, "29.7cm") == 0) || (strcmp($pagewidth, "297mm") == 0)) && ((strcmp($pageheight, "21cm") == 0) || (strcmp($pageheight, "210mm") == 0)))
265  {
266  $pagesize = "a4landscape";
267  }
268  else if (((strcmp($pageheight, "21cm") == 0) || (strcmp($pageheight, "210mm") == 0)) && ((strcmp($pagewidth, "14.8cm") == 0) || (strcmp($pagewidth, "148mm") == 0)))
269  {
270  $pagesize = "a5";
271  }
272  else if (((strcmp($pagewidth, "21cm") == 0) || (strcmp($pagewidth, "210mm") == 0)) && ((strcmp($pageheight, "14.8cm") == 0) || (strcmp($pageheight, "148mm") == 0)))
273  {
274  $pagesize = "a5landscape";
275  }
276  else if (((strcmp($pageheight, "11in") == 0)) && ((strcmp($pagewidth, "8.5in") == 0)))
277  {
278  $pagesize = "letter";
279  }
280  else if (((strcmp($pagewidth, "11in") == 0)) && ((strcmp($pageheight, "8.5in") == 0)))
281  {
282  $pagesize = "letterlandscape";
283  }
284  else
285  {
286  $pagesize = "custom";
287  }
288  if (!strlen($xslfo)) $pagesize = $certificatesettings->get("pageformat");;
289 
290  $paddingtop = "0cm";
291  if (preg_match("/padding-top\=\"([^\"]+)\"/", $xslfo, $matches))
292  {
293  $paddingtop = $matches[1];
294  }
295  $marginbody_top = "0cm";
296  $marginbody_right = "2cm";
297  $marginbody_bottom = "0cm";
298  $marginbody_left = "2cm";
299  if(preg_match("/fo:flow[^>]*margin\=\"([^\"]+)\"/", $xslfo, $matches))
300  {
301  // Backwards compatibility
302  $marginbody = $matches[1];
303  if (preg_match_all("/([^\s]+)/", $marginbody, $matches))
304  {
305  $marginbody_top = $matches[1][0];
306  $marginbody_right = $matches[1][1];
307  $marginbody_bottom = $matches[1][2];
308  $marginbody_left = $matches[1][3];
309  }
310  }
311  else if(preg_match("/fo:region-body[^>]*margin\=\"([^\"]+)\"/", $xslfo, $matches))
312  {
313  $marginbody = $matches[1];
314  if (preg_match_all("/([^\s]+)/", $marginbody, $matches))
315  {
316  $marginbody_top = $matches[1][0];
317  $marginbody_right = $matches[1][1];
318  $marginbody_bottom = $matches[1][2];
319  $marginbody_left = $matches[1][3];
320  }
321  }
322 
323  $xsl = file_get_contents("./Services/Certificate/xml/fo2xhtml.xsl");
324  if ((strlen($xslfo)) && (strlen($xsl)))
325  {
326  $args = array( '/_xml' => $xslfo, '/_xsl' => $xsl );
327  $xh = xslt_create();
328  $output = xslt_process($xh, "arg:/_xml", "arg:/_xsl", NULL, $args, NULL);
329  xslt_error($xh);
330  xslt_free($xh);
331  }
332 
333  $output = preg_replace("/<\?xml[^>]+?>/", "", $output);
334  // dirty hack: the php xslt processing seems not to recognize the following
335  // replacements, so we do it in the code as well
336  $output = str_replace("&#xA0;", "<br />", $output);
337  $output = str_replace("&#160;", "<br />", $output);
338  $form_fields = array(
339  "pageformat" => $pagesize,
340  "pagewidth" => $pagewidth,
341  "pageheight" => $pageheight,
342  "padding_top" => $paddingtop,
343  "margin_body_top" => $marginbody_top,
344  "margin_body_right" => $marginbody_right,
345  "margin_body_bottom" => $marginbody_bottom,
346  "margin_body_left" => $marginbody_left,
347  "certificate_text" => $output
348  );
349  $this->getAdapter()->addFormFieldsFromObject($form_fields);
350  return $form_fields;
351  }
352 
359  public function processXHTML2FO($form_data, $for_export = FALSE)
360  {
361  $content = "<html><body>".$form_data["certificate_text"]."</body></html>";
362  $content = preg_replace("/<p>(&nbsp;){1,}<\\/p>/", "<p></p>", $content);
363  $content = preg_replace("/<p>(\\s)*?<\\/p>/", "<p></p>", $content);
364 // $content = str_replace("<p>&nbsp;</p>", "<p><br /></p>", $content);
365 // $content = str_replace("<p> </p>", "<p><br /></p>", $content);
366  $content = str_replace("<p></p>", "<p class=\"emptyrow\"></p>", $content);
367  $content = str_replace("&nbsp;", "&#160;", $content);
368  $content = preg_replace("//", "", $content);
369 
370  include_once "./Services/Certificate/classes/class.ilXmlChecker.php";
371  $check = new ilXMLChecker();
372  $check->setXMLContent($content);
373  $check->startParsing();
374  if ($check->hasError())
375  {
376  throw new Exception($this->lng->txt("certificate_not_well_formed"));
377  }
378 
379  $xsl = file_get_contents("./Services/Certificate/xml/xhtml2fo.xsl");
380 
381  // additional font support
382  $xsl = str_replace(
383  'font-family="Helvetica, unifont"',
384  'font-family="'.$GLOBALS['ilSetting']->get('rpc_pdf_font','Helvetica, unifont').'"',
385  $xsl
386  );
387 
388  $args = array( '/_xml' => $content, '/_xsl' => $xsl );
389  $xh = xslt_create();
390  if (strcmp($form_data["pageformat"], "custom") == 0)
391  {
392  $pageheight = $form_data["pageheight"];
393  $pagewidth = $form_data["pagewidth"];
394  }
395  else
396  {
397  $pageformats = $this->getPageFormats();
398  $pageheight = $pageformats[$form_data["pageformat"]]["height"];
399  $pagewidth = $pageformats[$form_data["pageformat"]]["width"];
400  }
401  include_once "./Services/Certificate/classes/class.ilObjCertificateSettingsAccess.php";
403  $params = array(
404  "pageheight" => $pageheight,
405  "pagewidth" => $pagewidth,
406  "backgroundimage" => $backgroundimage,
407  "marginbody" => $form_data["margin_body_top"] . " " . $form_data["margin_body_right"] . " " . $form_data["margin_body_bottom"] . " " . $form_data["margin_body_left"],
408  "paddingtop" => $form_data["padding_top"]
409  );
410  $output = xslt_process($xh, "arg:/_xml", "arg:/_xsl", NULL, $args, $params);
411  xslt_error($xh);
412  xslt_free($xh);
413  return $output;
414  }
415 
423  private function exchangeCertificateVariables($certificate_text, $insert_tags = array())
424  {
425  if (count($insert_tags) == 0)
426  {
427  $insert_tags = $this->getAdapter()->getCertificateVariablesForPreview();
428 
429  foreach (self::getCustomCertificateFields() as $k => $f)
430  {
431  $insert_tags[$f["ph"]] = $f["name"];
432  }
433  }
434  foreach ($insert_tags as $var => $value)
435  {
436  $certificate_text = str_replace($var, $value, $certificate_text);
437  }
438  return $certificate_text;
439  }
440 
446  public function outCertificate($params, $deliver = TRUE)
447  {
448  global $ilLog;
450  $insert_tags = $this->getAdapter()->getCertificateVariablesForPresentation($params);
451 
452  include_once("./Services/User/classes/class.ilUserDefinedData.php");
453  $cust_data = new ilUserDefinedData($this->getAdapter()->getUserIdForParams($params));
454  $cust_data = $cust_data->getAll();
455  foreach (self::getCustomCertificateFields() as $k => $f)
456  {
457  $insert_tags[$f["ph"]] = $cust_data["f_".$k];
458  }
459 
460  $xslfo = file_get_contents($this->getXSLPath());
461 
462  include_once './Services/WebServices/RPC/classes/class.ilRpcClientFactory.php';
463  try
464  {
465  $pdf_base64 = ilRpcClientFactory::factory('RPCTransformationHandler')->ilFO2PDF(
466  $this->exchangeCertificateVariables($xslfo, $insert_tags));
467  if ($deliver)
468  {
469  include_once "./Services/Utilities/classes/class.ilUtil.php";
470  ilUtil::deliverData($pdf_base64->scalar, $this->getAdapter()->getCertificateFilename($params), "application/pdf");
471  }
472  else
473  {
474  return $pdf_base64->scalar;
475  }
476  }
477  catch(XML_RPC2_FaultException $e)
478  {
479  $ilLog->write(__METHOD__.': '.$e->getMessage());
480  return false;
481  }
482  catch(Exception $e)
483  {
484  $ilLog->write(__METHOD__.': '.$e->getMessage());
485  return false;
486  }
487 
489 
490  /*
491  include_once "./Services/Transformation/classes/class.ilFO2PDF.php";
492  $fo2pdf = new ilFO2PDF();
493  $fo2pdf->setFOString($this->exchangeCertificateVariables($xslfo, $insert_tags));
494  $result = $fo2pdf->send();
495  if ($deliver)
496  {
497  include_once "./Services/Utilities/classes/class.ilUtil.php";
498  ilUtil::deliverData($result, $this->getAdapter()->getCertificateFilename($params), "application/pdf");
499  }
500  else
501  {
502  return $result;
503  }
504  */
505  }
506 
510  public function createPreview()
511  {
512  global $ilLog;
513 
515 
516  $xslfo = file_get_contents($this->getXSLPath());
517 
518  include_once './Services/WebServices/RPC/classes/class.ilRpcClientFactory.php';
519  try
520  {
521  $pdf_base64 = ilRpcClientFactory::factory('RPCTransformationHandler')->ilFO2PDF(
522  $this->exchangeCertificateVariables($xslfo));
523  ilUtil::deliverData($pdf_base64->scalar, $this->getAdapter()->getCertificateFilename(), "application/pdf");
524 
525  }
526  catch(XML_RPC2_FaultException $e)
527  {
528  $ilLog->write(__METHOD__.': '.$e->getMessage());
529  return false;
530  }
531  catch(Exception $e)
532  {
533  $ilLog->write(__METHOD__.': '.$e->getMessage());
534  return false;
535  }
536 
538 
539  /*
540  include_once "./Services/Transformation/classes/class.ilFO2PDF.php";
541  $fo2pdf = new ilFO2PDF();
542  $fo2pdf->setFOString($this->exchangeCertificateVariables($xslfo));
543  $result = $fo2pdf->send();
544  include_once "./Services/Utilities/classes/class.ilUtil.php";
545  ilUtil::deliverData($result, $this->getAdapter()->getCertificateFilename(), "application/pdf");
546  */
547  }
548 
554  public function saveCertificate($xslfo, $filename = "")
555  {
556  if (!file_exists($this->getAdapter()->getCertificatePath()))
557  {
558  ilUtil::makeDirParents($this->getAdapter()->getCertificatePath());
559  }
560  if (strlen($filename) == 0)
561  {
562  $filename = $this->getXSLPath();
563  }
564  $fh = fopen($filename, "w");
565  fwrite($fh, $xslfo);
566  fclose($fh);
567  }
568 
576  public function uploadBackgroundImage($image_tempfilename)
577  {
578  if (!empty($image_tempfilename))
579  {
580  $image_filename = "background_upload";
581  $convert_filename = $this->getBackgroundImageName();
582  $imagepath = $this->getAdapter()->getCertificatePath();
583  if (!file_exists($imagepath))
584  {
585  ilUtil::makeDirParents($imagepath);
586  }
587  // upload the file
588  if (!ilUtil::moveUploadedFile($image_tempfilename, $image_filename, $this->getBackgroundImageTempfilePath()))
589  {
590  return FALSE;
591  }
592  // convert the uploaded file to JPEG
595  if (!file_exists($this->getBackgroundImagePath()))
596  {
597  // something went wrong converting the file. use the original file and hope, that PDF can work with it
598  if (!ilUtil::moveUploadedFile($this->getBackgroundImageTempfilePath(), $convert_filename, $this->getBackgroundImagePath()))
599  {
600  return FALSE;
601  }
602  }
603  unlink($this->getBackgroundImageTempfilePath());
604  if (file_exists($this->getBackgroundImagePath()) && (filesize($this->getBackgroundImagePath()) > 0))
605  {
606  return TRUE;
607  }
608  }
609  return FALSE;
610  }
611 
617  public function hasBackgroundImage()
618  {
619  if (file_exists($this->getBackgroundImagePath()) && (filesize($this->getBackgroundImagePath()) > 0))
620  {
621  return TRUE;
622  }
623  else
624  {
625  return FALSE;
626  }
627  }
628 
634  public function isComplete()
635  {
636  if(self::isActive())
637  {
638  $obj_id = $this->getAdapter()->getCertificateID();
639  if($obj_id && !self::isObjectActive($obj_id))
640  {
641  return FALSE;
642  }
643  if (file_exists($this->getAdapter()->getCertificatePath()))
644  {
645  if (file_exists($this->getXSLPath()) && (filesize($this->getXSLPath()) > 0))
646  {
647  return TRUE;
648  }
649  }
650  }
651  return FALSE;
652  }
653 
661  {
662  if (is_object($adapter) && method_exists($adapter, "getCertificatePath"))
663  {
664  if(self::isActive())
665  {
666  $obj_id = $adapter->getCertificateID();
667  if($obj_id && !self::isObjectActive($obj_id))
668  {
669  return FALSE;
670  }
671 
672  $certificatepath = $adapter->getCertificatePath();
673  if (file_exists($certificatepath))
674  {
675  $xslpath = $adapter->getCertificatePath() . ilCertificate::_getXSLName();
676  if (file_exists($xslpath) && (filesize($xslpath) > 0))
677  {
678  return TRUE;
679  }
680  }
681  }
682  }
683  return FALSE;
684  }
685 
691  public function getPageFormats()
692  {
693  return array(
694  "a4" => array(
695  "name" => $this->lng->txt("certificate_a4"), // (297 mm x 210 mm)
696  "value" => "a4",
697  "width" => "210mm",
698  "height" => "297mm"
699  ),
700  "a4landscape" => array(
701  "name" => $this->lng->txt("certificate_a4_landscape"), // (210 mm x 297 mm)",
702  "value" => "a4landscape",
703  "width" => "297mm",
704  "height" => "210mm"
705  ),
706  "a5" => array(
707  "name" => $this->lng->txt("certificate_a5"), // (210 mm x 148.5 mm)
708  "value" => "a5",
709  "width" => "148mm",
710  "height" => "210mm"
711  ),
712  "a5landscape" => array(
713  "name" => $this->lng->txt("certificate_a5_landscape"), // (148.5 mm x 210 mm)
714  "value" => "a5landscape",
715  "width" => "210mm",
716  "height" => "148mm"
717  ),
718  "letter" => array(
719  "name" => $this->lng->txt("certificate_letter"), // (11 inch x 8.5 inch)
720  "value" => "letter",
721  "width" => "8.5in",
722  "height" => "11in"
723  ),
724  "letterlandscape" => array(
725  "name" => $this->lng->txt("certificate_letter_landscape"), // (8.5 inch x 11 inch)
726  "value" => "letterlandscape",
727  "width" => "11in",
728  "height" => "8.5in"
729  ),
730  "custom" => array(
731  "name" => $this->lng->txt("certificate_custom"),
732  "value" => "custom",
733  "width" => "",
734  "height" => ""
735  )
736  );
737  }
738 
742  public function deliverExportFileXML()
743  {
744  include_once "./Services/Utilities/classes/class.ilUtil.php";
745  $exportpath = $this->createArchiveDirectory();
746  ilUtil::makeDir($exportpath);
747  $xsl = file_get_contents($this->getXSLPath());
748  $xslexport = str_replace($this->getAdapter()->getCertificatePath(), "", $xsl);
749  // save export xsl file
750  $this->saveCertificate($xslexport, $exportpath . $this->getXSLName());
751  // save background image
752  if ($this->hasBackgroundImage())
753  {
754  copy($this->getBackgroundImagePath(), $exportpath . $this->getBackgroundImageName());
755  }
756  else
757  {
758  include_once "./Services/Certificate/classes/class.ilObjCertificateSettingsAccess.php";
760  {
762  }
763  }
764  $zipfile = time() . "__" . IL_INST_ID . "__" . $this->getAdapter()->getAdapterType() . "__" . $this->getAdapter()->getCertificateId() . "__certificate.zip";
765  ilUtil::zip($exportpath, $this->getAdapter()->getCertificatePath() . $zipfile);
766  ilUtil::delDir($exportpath);
767  ilUtil::deliverFile($this->getAdapter()->getCertificatePath() . $zipfile, $zipfile, "application/zip");
768  }
769 
775  public function importCertificate($zipfile, $filename)
776  {
777  include_once "./Services/Utilities/classes/class.ilUtil.php";
778  $importpath = $this->createArchiveDirectory();
779  if (!ilUtil::moveUploadedFile($zipfile, $filename, $importpath . $filename))
780  {
781  ilUtil::delDir($importpath);
782  return FALSE;
783  }
784  ilUtil::unzip($importpath . $filename, TRUE);
785  $subdir = str_replace(".zip", "", strtolower($filename)) . "/";
786  $copydir = "";
787  if (is_dir($importpath . $subdir))
788  {
789  $dirinfo = ilUtil::getDir($importpath . $subdir);
790  $copydir = $importpath . $subdir;
791  }
792  else
793  {
794  $dirinfo = ilUtil::getDir($importpath);
795  $copydir = $importpath;
796  }
797  $xmlfiles = 0;
798  $otherfiles = 0;
799  foreach ($dirinfo as $file)
800  {
801  if (strcmp($file["type"], "file") == 0)
802  {
803  if (strpos($file["entry"], ".xml") !== FALSE)
804  {
805  $xmlfiles++;
806  }
807  else if (strpos($file["entry"], ".zip") !== FALSE)
808  {
809  }
810  else
811  {
812  $otherfiles++;
813  }
814  }
815  }
816  // if one XML file is in the archive, we try to import it
817  if ($xmlfiles == 1)
818  {
819  foreach ($dirinfo as $file)
820  {
821  if (strcmp($file["type"], "file") == 0)
822  {
823  if (strpos($file["entry"], ".xml") !== FALSE)
824  {
825  $xsl = file_get_contents($copydir . $file["entry"]);
826  // as long as we cannot make RPC calls in a given directory, we have
827  // to add the complete path to every url
828  $xsl = preg_replace("/url\([']{0,1}(.*?)[']{0,1}\)/", "url(" . $this->getAdapter()->getCertificatePath() . "\${1})", $xsl);
829  $this->saveCertificate($xsl);
830  }
831  else if (strpos($file["entry"], ".zip") !== FALSE)
832  {
833  }
834  else
835  {
836  @copy($copydir . $file["entry"], $this->getAdapter()->getCertificatePath() . $file["entry"]);
837  if (strcmp($this->getBackgroundImagePath(), $this->getAdapter()->getCertificatePath() . $file["entry"]) == 0)
838  {
839  // upload of the background image, create a preview
841  }
842  }
843  }
844  }
845  }
846  else
847  {
848  ilUtil::delDir($importpath);
849  return FALSE;
850  }
851  ilUtil::delDir($importpath);
852  return TRUE;
853  }
854 
860  public function getAdapter()
861  {
862  return $this->adapter;
863  }
864 
870  public function setAdapter($adapter)
871  {
872  $this->adapter =& $adapter;
873  }
874 
875  /***************************************
876  /* BULK CERTIFICATE PROCESSING METHODS *
877  /***************************************
878 
884  public function createArchiveDirectory()
885  {
886  $dir = $this->getAdapter()->getCertificatePath() . time() . "__" . IL_INST_ID . "__" . $this->getAdapter()->getAdapterType() . "__" . $this->getAdapter()->getCertificateId() . "__certificate/";
887  include_once "./Services/Utilities/classes/class.ilUtil.php";
888  ilUtil::makeDirParents($dir);
889  return $dir;
890  }
891 
899  public function addPDFtoArchiveDirectory($pdfdata, $dir, $filename)
900  {
901  $fh = fopen($dir . $filename, "wb");
902  fwrite($fh, $pdfdata);
903  fclose($fh);
904  }
905 
913  public function zipCertificatesInArchiveDirectory($dir, $deliver = TRUE)
914  {
915  $zipfile = time() . "__" . IL_INST_ID . "__" . $this->getAdapter()->getAdapterType() . "__" . $this->getAdapter()->getCertificateId() . "__certificates.zip";
916  ilUtil::zip($dir, $this->getAdapter()->getCertificatePath() . $zipfile);
917  ilUtil::delDir($dir);
918  if ($deliver)
919  {
920  ilUtil::deliverFile($this->getAdapter()->getCertificatePath() . $zipfile, $zipfile, "application/zip");
921  }
922  return $this->getAdapter()->getCertificatePath() . $zipfile;
923  }
924 
925  public static function isActive()
926  {
927  if(self::$is_active === null)
928  {
929  // basic admin setting active?
930  $certificate_active = new ilSetting("certificate");
931  $certificate_active = (bool)$certificate_active->get("active");
932 
933  // java/rtpc-server active?
934  if($certificate_active)
935  {
936  include_once './Services/WebServices/RPC/classes/class.ilRPCServerSettings.php';
937  $certificate_active = ilRPCServerSettings::getInstance()->isEnabled();
938  }
939 
940  self::$is_active = (bool)$certificate_active;
941  }
942  return self::$is_active;
943  }
944 
945  public static function isObjectActive($a_obj_id)
946  {
947  $chk = self::areObjectsActive(array($a_obj_id));
948  return $chk[$a_obj_id];
949  }
950  public static function areObjectsActive(array $a_obj_ids)
951  {
952  global $ilDB;
953 
954  $all = array();
955  foreach($a_obj_ids as $id)
956  {
957  $all[$id] = false;
958  }
959 
960  $set = $ilDB->query("SELECT obj_id FROM il_certificate".
961  " WHERE ".$ilDB->in("obj_id", $a_obj_ids, "", "integer"));
962  while($row = $ilDB->fetchAssoc($set))
963  {
964  $all[$row["obj_id"]] = true;
965  }
966  return $all;
967  }
968 
969  public function readActive()
970  {
971  global $ilDB;
972 
973  $obj_id = $this->adapter->getCertificateID();
974  $set = $ilDB->query("SELECT obj_id FROM il_certificate".
975  " WHERE obj_id = ".$ilDB->quote($obj_id, "integer"));
976  return $ilDB->numRows($set);
977  }
978 
979  public function writeActive($a_value)
980  {
981  global $ilDB;
982 
983  $obj_id = $this->adapter->getCertificateID();
984 
985  if((bool)$a_value)
986  {
987  $ilDB->replace("il_certificate",
988  array("obj_id"=>array("integer", $obj_id)),
989  array());
990  }
991  else
992  {
993  $ilDB->manipulate("DELETE FROM il_certificate".
994  " WHERE obj_id = ".$ilDB->quote($obj_id, "integer"));
995  }
996  }
997 
1003  public static function _goto($ref_id)
1004  {
1005  global $ilCtrl;
1006  include_once "./Services/Object/classes/class.ilObject.php";
1007  $type = ilObject::_lookupType($ref_id, true);
1008  switch ($type)
1009  {
1010  case 'sahs':
1011  $cmd_link = "ilias.php?baseClass=ilSAHSPresentationGUI&ref_id=".$ref_id.
1012  "&cmd=downloadCertificate";
1013  ilUtil::redirect($cmd_link);
1014  break;
1015  case 'tst':
1016  default:
1017  $ilCtrl->redirectByClass("ilrepositorygui", "frameset");
1018  break;
1019  }
1020  }
1021 
1025  static function getCustomCertificateFields()
1026  {
1027  include_once("./Services/User/classes/class.ilUserDefinedFields.php");
1028  $user_field_definitions = ilUserDefinedFields::_getInstance();
1029  $fds = $user_field_definitions->getDefinitions();
1030  $fields = array();
1031  foreach ($fds as $f)
1032  {
1033  if ($f["certificate"])
1034  {
1035  $fields[$f["field_id"]] = array("name" => $f["field_name"],
1036  "ph" => "[#".str_replace(" ", "_", strtoupper($f["field_name"]))."]");
1037  }
1038  }
1039 
1040  return $fields;
1041  }
1042 
1046  public function getExchangeContent()
1047  {
1048  if(!file_exists($this->getXSLPath()))
1049  {
1050  return '';
1051  }
1052 
1053  $output = '';
1054  $xsl_file_content = file_get_contents($this->getXSLPath());
1055  $xsl = file_get_contents("./Services/Certificate/xml/fo2xhtml.xsl");
1056 
1057  if((strlen($xsl_file_content)) && (strlen($xsl)))
1058  {
1059  $args = array('/_xml' => $xsl_file_content, '/_xsl' => $xsl);
1060  $xh = xslt_create();
1061  $output = xslt_process($xh, "arg:/_xml", "arg:/_xsl", NULL, $args, NULL);
1062  xslt_error($xh);
1063  xslt_free($xh);
1064  }
1065 
1066  $output = preg_replace("/<\?xml[^>]+?>/", "", $output);
1067  // dirty hack: the php xslt processing seems not to recognize the following
1068  // replacements, so we do it in the code as well
1069  $output = str_replace("&#xA0;", "<br />", $output);
1070  $output = str_replace("&#160;", "<br />", $output);
1071 
1072  return $output;
1073  }
1074 
1075  public function outCertificateWithGivenContentAndVariables($content, $insert_tags)
1076  {
1077  global $ilLog;
1078 
1079  ilDatePresentation::setUseRelativeDates(false);
1080 
1081  $form_fields = $this->getFormFieldsFromFO();
1082  $form_fields['certificate_text'] = $content;
1083  $xslfo = $this->processXHTML2FO($form_fields);
1084 
1085  $content = $this->exchangeCertificateVariables($xslfo, $insert_tags);
1086  $content = str_replace('[BR]', "<fo:block/>", $content);
1087 
1088  include_once './Services/WebServices/RPC/classes/class.ilRpcClientFactory.php';
1089  try
1090  {
1091  $pdf_base64 = ilRpcClientFactory::factory('RPCTransformationHandler')->ilFO2PDF($content);
1092  include_once "./Services/Utilities/classes/class.ilUtil.php";
1093  ilUtil::deliverData($pdf_base64->scalar, $this->getAdapter()->getCertificateFilename(array()), "application/pdf");
1094  }
1095  catch(XML_RPC2_FaultException $e)
1096  {
1097  $ilLog->write(__METHOD__.': '.$e->getMessage());
1098  return false;
1099  }
1100  catch(Exception $e)
1101  {
1102  $ilLog->write(__METHOD__.': '.$e->getMessage());
1103  return false;
1104  }
1105 
1106  ilDatePresentation::setUseRelativeDates(true);
1107  }
1108 }