ILIAS  Release_5_0_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(ilCertificate $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  // #10271
222  if($this->readActive())
223  {
224  $newObject->writeActive(true);
225  }
226  }
227 
233  public function deleteCertificate()
234  {
235  if (@file_exists($this->getAdapter()->getCertificatePath()))
236  {
237  include_once "./Services/Utilities/classes/class.ilUtil.php";
238  ilUtil::delDir($this->getAdapter()->getCertificatePath());
239  $this->getAdapter()->deleteCertificate();
240  }
241  $this->writeActive(false);
242  }
243 
247  public function getFormFieldsFromFO()
248  {
249  if (@file_exists($this->getXSLPath()))
250  {
251  $xslfo = file_get_contents($this->getXSLPath());
252  }
253  // retrieve form information (using a dirty way with regular expressions)
254  $pagewidth = "21cm";
255  if (preg_match("/page-width\=\"([^\"]+)\"/", $xslfo, $matches))
256  {
257  $pagewidth = $matches[1];
258  }
259  $pageheight = "29.7cm";
260  if (preg_match("/page-height\=\"([^\"]+)\"/", $xslfo, $matches))
261  {
262  $pageheight = $matches[1];
263  }
264  $certificatesettings = new ilSetting("certificate");
265  $pagesize = $certificatesettings->get("pageformat");;
266  if (((strcmp($pageheight, "29.7cm") == 0) || (strcmp($pageheight, "297mm") == 0)) && ((strcmp($pagewidth, "21cm") == 0) || (strcmp($pagewidth, "210mm") == 0)))
267  {
268  $pagesize = "a4";
269  }
270  else if (((strcmp($pagewidth, "29.7cm") == 0) || (strcmp($pagewidth, "297mm") == 0)) && ((strcmp($pageheight, "21cm") == 0) || (strcmp($pageheight, "210mm") == 0)))
271  {
272  $pagesize = "a4landscape";
273  }
274  else if (((strcmp($pageheight, "21cm") == 0) || (strcmp($pageheight, "210mm") == 0)) && ((strcmp($pagewidth, "14.8cm") == 0) || (strcmp($pagewidth, "148mm") == 0)))
275  {
276  $pagesize = "a5";
277  }
278  else if (((strcmp($pagewidth, "21cm") == 0) || (strcmp($pagewidth, "210mm") == 0)) && ((strcmp($pageheight, "14.8cm") == 0) || (strcmp($pageheight, "148mm") == 0)))
279  {
280  $pagesize = "a5landscape";
281  }
282  else if (((strcmp($pageheight, "11in") == 0)) && ((strcmp($pagewidth, "8.5in") == 0)))
283  {
284  $pagesize = "letter";
285  }
286  else if (((strcmp($pagewidth, "11in") == 0)) && ((strcmp($pageheight, "8.5in") == 0)))
287  {
288  $pagesize = "letterlandscape";
289  }
290  else
291  {
292  $pagesize = "custom";
293  }
294  if (!strlen($xslfo)) $pagesize = $certificatesettings->get("pageformat");;
295 
296  $paddingtop = "0cm";
297  if (preg_match("/padding-top\=\"([^\"]+)\"/", $xslfo, $matches))
298  {
299  $paddingtop = $matches[1];
300  }
301  $marginbody_top = "0cm";
302  $marginbody_right = "2cm";
303  $marginbody_bottom = "0cm";
304  $marginbody_left = "2cm";
305  if(preg_match("/fo:flow[^>]*margin\=\"([^\"]+)\"/", $xslfo, $matches))
306  {
307  // Backwards compatibility
308  $marginbody = $matches[1];
309  if (preg_match_all("/([^\s]+)/", $marginbody, $matches))
310  {
311  $marginbody_top = $matches[1][0];
312  $marginbody_right = $matches[1][1];
313  $marginbody_bottom = $matches[1][2];
314  $marginbody_left = $matches[1][3];
315  }
316  }
317  else if(preg_match("/fo:region-body[^>]*margin\=\"([^\"]+)\"/", $xslfo, $matches))
318  {
319  $marginbody = $matches[1];
320  if (preg_match_all("/([^\s]+)/", $marginbody, $matches))
321  {
322  $marginbody_top = $matches[1][0];
323  $marginbody_right = $matches[1][1];
324  $marginbody_bottom = $matches[1][2];
325  $marginbody_left = $matches[1][3];
326  }
327  }
328 
329  $xsl = file_get_contents("./Services/Certificate/xml/fo2xhtml.xsl");
330  if ((strlen($xslfo)) && (strlen($xsl)))
331  {
332  $args = array( '/_xml' => $xslfo, '/_xsl' => $xsl );
333  $xh = xslt_create();
334  $output = xslt_process($xh, "arg:/_xml", "arg:/_xsl", NULL, $args, NULL);
335  xslt_error($xh);
336  xslt_free($xh);
337  }
338 
339  $output = preg_replace("/<\?xml[^>]+?>/", "", $output);
340  // dirty hack: the php xslt processing seems not to recognize the following
341  // replacements, so we do it in the code as well
342  $output = str_replace("&#xA0;", "<br />", $output);
343  $output = str_replace("&#160;", "<br />", $output);
344  $form_fields = array(
345  "pageformat" => $pagesize,
346  "pagewidth" => $pagewidth,
347  "pageheight" => $pageheight,
348  "padding_top" => $paddingtop,
349  "margin_body_top" => $marginbody_top,
350  "margin_body_right" => $marginbody_right,
351  "margin_body_bottom" => $marginbody_bottom,
352  "margin_body_left" => $marginbody_left,
353  "certificate_text" => $output
354  );
355  $this->getAdapter()->addFormFieldsFromObject($form_fields);
356  return $form_fields;
357  }
358 
365  public function processXHTML2FO($form_data, $for_export = FALSE)
366  {
367  $content = "<html><body>".$form_data["certificate_text"]."</body></html>";
368  $content = preg_replace("/<p>(&nbsp;){1,}<\\/p>/", "<p></p>", $content);
369  $content = preg_replace("/<p>(\\s)*?<\\/p>/", "<p></p>", $content);
370 // $content = str_replace("<p>&nbsp;</p>", "<p><br /></p>", $content);
371 // $content = str_replace("<p> </p>", "<p><br /></p>", $content);
372  $content = str_replace("<p></p>", "<p class=\"emptyrow\"></p>", $content);
373  $content = str_replace("&nbsp;", "&#160;", $content);
374  $content = preg_replace("//", "", $content);
375 
376  include_once "./Services/Certificate/classes/class.ilXmlChecker.php";
377  $check = new ilXMLChecker();
378  $check->setXMLContent($content);
379  $check->startParsing();
380  if ($check->hasError())
381  {
382  throw new Exception($this->lng->txt("certificate_not_well_formed"));
383  }
384 
385  $xsl = file_get_contents("./Services/Certificate/xml/xhtml2fo.xsl");
386 
387  // additional font support
388  $xsl = str_replace(
389  'font-family="Helvetica, unifont"',
390  'font-family="'.$GLOBALS['ilSetting']->get('rpc_pdf_font','Helvetica, unifont').'"',
391  $xsl
392  );
393 
394  $args = array( '/_xml' => $content, '/_xsl' => $xsl );
395  $xh = xslt_create();
396  if (strcmp($form_data["pageformat"], "custom") == 0)
397  {
398  $pageheight = $form_data["pageheight"];
399  $pagewidth = $form_data["pagewidth"];
400  }
401  else
402  {
403  $pageformats = $this->getPageFormats();
404  $pageheight = $pageformats[$form_data["pageformat"]]["height"];
405  $pagewidth = $pageformats[$form_data["pageformat"]]["width"];
406  }
407  include_once "./Services/Certificate/classes/class.ilObjCertificateSettingsAccess.php";
409  $params = array(
410  "pageheight" => $pageheight,
411  "pagewidth" => $pagewidth,
412  "backgroundimage" => $backgroundimage,
413  "marginbody" => $form_data["margin_body_top"] . " " . $form_data["margin_body_right"] . " " . $form_data["margin_body_bottom"] . " " . $form_data["margin_body_left"],
414  "paddingtop" => $form_data["padding_top"]
415  );
416  $output = xslt_process($xh, "arg:/_xml", "arg:/_xsl", NULL, $args, $params);
417  xslt_error($xh);
418  xslt_free($xh);
419  return $output;
420  }
421 
429  private function exchangeCertificateVariables($certificate_text, $insert_tags = array())
430  {
431  if (count($insert_tags) == 0)
432  {
433  $insert_tags = $this->getAdapter()->getCertificateVariablesForPreview();
434 
435  foreach (self::getCustomCertificateFields() as $k => $f)
436  {
437  $insert_tags[$f["ph"]] = $f["name"];
438  }
439  }
440  foreach ($insert_tags as $var => $value)
441  {
442  $certificate_text = str_replace($var, $value, $certificate_text);
443  }
444  return $certificate_text;
445  }
446 
452  public function outCertificate($params, $deliver = TRUE)
453  {
454  global $ilLog;
456  $insert_tags = $this->getAdapter()->getCertificateVariablesForPresentation($params);
457 
458  include_once("./Services/User/classes/class.ilUserDefinedData.php");
459  $cust_data = new ilUserDefinedData($this->getAdapter()->getUserIdForParams($params));
460  $cust_data = $cust_data->getAll();
461  foreach (self::getCustomCertificateFields() as $k => $f)
462  {
463  $insert_tags[$f["ph"]] = $cust_data["f_".$k];
464  }
465 
466  $xslfo = file_get_contents($this->getXSLPath());
467 
468  include_once './Services/WebServices/RPC/classes/class.ilRpcClientFactory.php';
469  try
470  {
471  $pdf_base64 = ilRpcClientFactory::factory('RPCTransformationHandler')->ilFO2PDF(
472  $this->exchangeCertificateVariables($xslfo, $insert_tags));
473  if ($deliver)
474  {
475  include_once "./Services/Utilities/classes/class.ilUtil.php";
476  ilUtil::deliverData($pdf_base64->scalar, $this->getAdapter()->getCertificateFilename($params), "application/pdf");
477  }
478  else
479  {
480  return $pdf_base64->scalar;
481  }
482  }
483  catch(XML_RPC2_FaultException $e)
484  {
485  $ilLog->write(__METHOD__.': '.$e->getMessage());
486  return false;
487  }
488  catch(Exception $e)
489  {
490  $ilLog->write(__METHOD__.': '.$e->getMessage());
491  return false;
492  }
493 
495 
496  /*
497  include_once "./Services/Transformation/classes/class.ilFO2PDF.php";
498  $fo2pdf = new ilFO2PDF();
499  $fo2pdf->setFOString($this->exchangeCertificateVariables($xslfo, $insert_tags));
500  $result = $fo2pdf->send();
501  if ($deliver)
502  {
503  include_once "./Services/Utilities/classes/class.ilUtil.php";
504  ilUtil::deliverData($result, $this->getAdapter()->getCertificateFilename($params), "application/pdf");
505  }
506  else
507  {
508  return $result;
509  }
510  */
511  }
512 
516  public function createPreview()
517  {
518  global $ilLog;
519 
521 
522  $xslfo = file_get_contents($this->getXSLPath());
523 
524  include_once './Services/WebServices/RPC/classes/class.ilRpcClientFactory.php';
525  try
526  {
527  $pdf_base64 = ilRpcClientFactory::factory('RPCTransformationHandler')->ilFO2PDF(
528  $this->exchangeCertificateVariables($xslfo));
529  ilUtil::deliverData($pdf_base64->scalar, $this->getAdapter()->getCertificateFilename(), "application/pdf");
530 
531  }
532  catch(XML_RPC2_FaultException $e)
533  {
534  $ilLog->write(__METHOD__.': '.$e->getMessage());
535  return false;
536  }
537  catch(Exception $e)
538  {
539  $ilLog->write(__METHOD__.': '.$e->getMessage());
540  return false;
541  }
542 
544 
545  /*
546  include_once "./Services/Transformation/classes/class.ilFO2PDF.php";
547  $fo2pdf = new ilFO2PDF();
548  $fo2pdf->setFOString($this->exchangeCertificateVariables($xslfo));
549  $result = $fo2pdf->send();
550  include_once "./Services/Utilities/classes/class.ilUtil.php";
551  ilUtil::deliverData($result, $this->getAdapter()->getCertificateFilename(), "application/pdf");
552  */
553  }
554 
560  public function saveCertificate($xslfo, $filename = "")
561  {
562  if (!file_exists($this->getAdapter()->getCertificatePath()))
563  {
564  ilUtil::makeDirParents($this->getAdapter()->getCertificatePath());
565  }
566  if (strlen($filename) == 0)
567  {
568  $filename = $this->getXSLPath();
569  }
570  $fh = fopen($filename, "w");
571  fwrite($fh, $xslfo);
572  fclose($fh);
573  }
574 
582  public function uploadBackgroundImage($image_tempfilename)
583  {
584  if (!empty($image_tempfilename))
585  {
586  $image_filename = "background_upload";
587  $convert_filename = $this->getBackgroundImageName();
588  $imagepath = $this->getAdapter()->getCertificatePath();
589  if (!file_exists($imagepath))
590  {
591  ilUtil::makeDirParents($imagepath);
592  }
593  // upload the file
594  if (!ilUtil::moveUploadedFile($image_tempfilename, $image_filename, $this->getBackgroundImageTempfilePath()))
595  {
596  return FALSE;
597  }
598  // convert the uploaded file to JPEG
601  if (!file_exists($this->getBackgroundImagePath()))
602  {
603  // something went wrong converting the file. use the original file and hope, that PDF can work with it
604  if (!ilUtil::moveUploadedFile($this->getBackgroundImageTempfilePath(), $convert_filename, $this->getBackgroundImagePath()))
605  {
606  return FALSE;
607  }
608  }
609  unlink($this->getBackgroundImageTempfilePath());
610  if (file_exists($this->getBackgroundImagePath()) && (filesize($this->getBackgroundImagePath()) > 0))
611  {
612  return TRUE;
613  }
614  }
615  return FALSE;
616  }
617 
623  public function hasBackgroundImage()
624  {
625  if (file_exists($this->getBackgroundImagePath()) && (filesize($this->getBackgroundImagePath()) > 0))
626  {
627  return TRUE;
628  }
629  else
630  {
631  return FALSE;
632  }
633  }
634 
640  public function isComplete()
641  {
642  if(self::isActive())
643  {
644  $obj_id = $this->getAdapter()->getCertificateID();
645  if($obj_id && !self::isObjectActive($obj_id))
646  {
647  return FALSE;
648  }
649  if (file_exists($this->getAdapter()->getCertificatePath()))
650  {
651  if (file_exists($this->getXSLPath()) && (filesize($this->getXSLPath()) > 0))
652  {
653  return TRUE;
654  }
655  }
656  }
657  return FALSE;
658  }
659 
667  {
668  if (is_object($adapter) && method_exists($adapter, "getCertificatePath"))
669  {
670  if(self::isActive())
671  {
672  $obj_id = $adapter->getCertificateID();
673  if($obj_id && !self::isObjectActive($obj_id))
674  {
675  return FALSE;
676  }
677 
678  $certificatepath = $adapter->getCertificatePath();
679  if (file_exists($certificatepath))
680  {
681  $xslpath = $adapter->getCertificatePath() . ilCertificate::_getXSLName();
682  if (file_exists($xslpath) && (filesize($xslpath) > 0))
683  {
684  return TRUE;
685  }
686  }
687  }
688  }
689  return FALSE;
690  }
691 
697  public function getPageFormats()
698  {
699  return array(
700  "a4" => array(
701  "name" => $this->lng->txt("certificate_a4"), // (297 mm x 210 mm)
702  "value" => "a4",
703  "width" => "210mm",
704  "height" => "297mm"
705  ),
706  "a4landscape" => array(
707  "name" => $this->lng->txt("certificate_a4_landscape"), // (210 mm x 297 mm)",
708  "value" => "a4landscape",
709  "width" => "297mm",
710  "height" => "210mm"
711  ),
712  "a5" => array(
713  "name" => $this->lng->txt("certificate_a5"), // (210 mm x 148.5 mm)
714  "value" => "a5",
715  "width" => "148mm",
716  "height" => "210mm"
717  ),
718  "a5landscape" => array(
719  "name" => $this->lng->txt("certificate_a5_landscape"), // (148.5 mm x 210 mm)
720  "value" => "a5landscape",
721  "width" => "210mm",
722  "height" => "148mm"
723  ),
724  "letter" => array(
725  "name" => $this->lng->txt("certificate_letter"), // (11 inch x 8.5 inch)
726  "value" => "letter",
727  "width" => "8.5in",
728  "height" => "11in"
729  ),
730  "letterlandscape" => array(
731  "name" => $this->lng->txt("certificate_letter_landscape"), // (8.5 inch x 11 inch)
732  "value" => "letterlandscape",
733  "width" => "11in",
734  "height" => "8.5in"
735  ),
736  "custom" => array(
737  "name" => $this->lng->txt("certificate_custom"),
738  "value" => "custom",
739  "width" => "",
740  "height" => ""
741  )
742  );
743  }
744 
748  public function deliverExportFileXML()
749  {
750  include_once "./Services/Utilities/classes/class.ilUtil.php";
751  $exportpath = $this->createArchiveDirectory();
752  ilUtil::makeDir($exportpath);
753  $xsl = file_get_contents($this->getXSLPath());
754  $xslexport = str_replace($this->getAdapter()->getCertificatePath(), "", $xsl);
755  // save export xsl file
756  $this->saveCertificate($xslexport, $exportpath . $this->getXSLName());
757  // save background image
758  if ($this->hasBackgroundImage())
759  {
760  copy($this->getBackgroundImagePath(), $exportpath . $this->getBackgroundImageName());
761  }
762  else
763  {
764  include_once "./Services/Certificate/classes/class.ilObjCertificateSettingsAccess.php";
766  {
768  }
769  }
770  $zipfile = time() . "__" . IL_INST_ID . "__" . $this->getAdapter()->getAdapterType() . "__" . $this->getAdapter()->getCertificateId() . "__certificate.zip";
771  ilUtil::zip($exportpath, $this->getAdapter()->getCertificatePath() . $zipfile);
772  ilUtil::delDir($exportpath);
773  ilUtil::deliverFile($this->getAdapter()->getCertificatePath() . $zipfile, $zipfile, "application/zip");
774  }
775 
781  public function importCertificate($zipfile, $filename)
782  {
783  include_once "./Services/Utilities/classes/class.ilUtil.php";
784  $importpath = $this->createArchiveDirectory();
785  if (!ilUtil::moveUploadedFile($zipfile, $filename, $importpath . $filename))
786  {
787  ilUtil::delDir($importpath);
788  return FALSE;
789  }
790  ilUtil::unzip($importpath . $filename, TRUE);
791  $subdir = str_replace(".zip", "", strtolower($filename)) . "/";
792  $copydir = "";
793  if (is_dir($importpath . $subdir))
794  {
795  $dirinfo = ilUtil::getDir($importpath . $subdir);
796  $copydir = $importpath . $subdir;
797  }
798  else
799  {
800  $dirinfo = ilUtil::getDir($importpath);
801  $copydir = $importpath;
802  }
803  $xmlfiles = 0;
804  $otherfiles = 0;
805  foreach ($dirinfo as $file)
806  {
807  if (strcmp($file["type"], "file") == 0)
808  {
809  if (strpos($file["entry"], ".xml") !== FALSE)
810  {
811  $xmlfiles++;
812  }
813  else if (strpos($file["entry"], ".zip") !== FALSE)
814  {
815  }
816  else
817  {
818  $otherfiles++;
819  }
820  }
821  }
822  // if one XML file is in the archive, we try to import it
823  if ($xmlfiles == 1)
824  {
825  foreach ($dirinfo as $file)
826  {
827  if (strcmp($file["type"], "file") == 0)
828  {
829  if (strpos($file["entry"], ".xml") !== FALSE)
830  {
831  $xsl = file_get_contents($copydir . $file["entry"]);
832  // as long as we cannot make RPC calls in a given directory, we have
833  // to add the complete path to every url
834  $xsl = preg_replace("/url\([']{0,1}(.*?)[']{0,1}\)/", "url(" . $this->getAdapter()->getCertificatePath() . "\${1})", $xsl);
835  $this->saveCertificate($xsl);
836  }
837  else if (strpos($file["entry"], ".zip") !== FALSE)
838  {
839  }
840  else
841  {
842  @copy($copydir . $file["entry"], $this->getAdapter()->getCertificatePath() . $file["entry"]);
843  if (strcmp($this->getBackgroundImagePath(), $this->getAdapter()->getCertificatePath() . $file["entry"]) == 0)
844  {
845  // upload of the background image, create a preview
847  }
848  }
849  }
850  }
851  }
852  else
853  {
854  ilUtil::delDir($importpath);
855  return FALSE;
856  }
857  ilUtil::delDir($importpath);
858  return TRUE;
859  }
860 
866  public function getAdapter()
867  {
868  return $this->adapter;
869  }
870 
876  public function setAdapter($adapter)
877  {
878  $this->adapter =& $adapter;
879  }
880 
881  /***************************************
882  /* BULK CERTIFICATE PROCESSING METHODS *
883  /***************************************
884 
890  public function createArchiveDirectory()
891  {
892  $dir = $this->getAdapter()->getCertificatePath() . time() . "__" . IL_INST_ID . "__" . $this->getAdapter()->getAdapterType() . "__" . $this->getAdapter()->getCertificateId() . "__certificate/";
893  include_once "./Services/Utilities/classes/class.ilUtil.php";
894  ilUtil::makeDirParents($dir);
895  return $dir;
896  }
897 
905  public function addPDFtoArchiveDirectory($pdfdata, $dir, $filename)
906  {
907  $fh = fopen($dir . $filename, "wb");
908  fwrite($fh, $pdfdata);
909  fclose($fh);
910  }
911 
919  public function zipCertificatesInArchiveDirectory($dir, $deliver = TRUE)
920  {
921  $zipfile = time() . "__" . IL_INST_ID . "__" . $this->getAdapter()->getAdapterType() . "__" . $this->getAdapter()->getCertificateId() . "__certificates.zip";
922  ilUtil::zip($dir, $this->getAdapter()->getCertificatePath() . $zipfile);
923  ilUtil::delDir($dir);
924  if ($deliver)
925  {
926  ilUtil::deliverFile($this->getAdapter()->getCertificatePath() . $zipfile, $zipfile, "application/zip");
927  }
928  return $this->getAdapter()->getCertificatePath() . $zipfile;
929  }
930 
931  public static function isActive()
932  {
933  if(self::$is_active === null)
934  {
935  // basic admin setting active?
936  $certificate_active = new ilSetting("certificate");
937  $certificate_active = (bool)$certificate_active->get("active");
938 
939  // java/rtpc-server active?
940  if($certificate_active)
941  {
942  include_once './Services/WebServices/RPC/classes/class.ilRPCServerSettings.php';
943  $certificate_active = ilRPCServerSettings::getInstance()->isEnabled();
944  }
945 
946  self::$is_active = (bool)$certificate_active;
947  }
948  return self::$is_active;
949  }
950 
951  public static function isObjectActive($a_obj_id)
952  {
953  $chk = self::areObjectsActive(array($a_obj_id));
954  return $chk[$a_obj_id];
955  }
956  public static function areObjectsActive(array $a_obj_ids)
957  {
958  global $ilDB;
959 
960  $all = array();
961  foreach($a_obj_ids as $id)
962  {
963  $all[$id] = false;
964  }
965 
966  $set = $ilDB->query("SELECT obj_id FROM il_certificate".
967  " WHERE ".$ilDB->in("obj_id", $a_obj_ids, "", "integer"));
968  while($row = $ilDB->fetchAssoc($set))
969  {
970  $all[$row["obj_id"]] = true;
971  }
972  return $all;
973  }
974 
975  public function readActive()
976  {
977  global $ilDB;
978 
979  $obj_id = $this->adapter->getCertificateID();
980  $set = $ilDB->query("SELECT obj_id FROM il_certificate".
981  " WHERE obj_id = ".$ilDB->quote($obj_id, "integer"));
982  return $ilDB->numRows($set);
983  }
984 
985  public function writeActive($a_value)
986  {
987  global $ilDB;
988 
989  $obj_id = $this->adapter->getCertificateID();
990 
991  if((bool)$a_value)
992  {
993  $ilDB->replace("il_certificate",
994  array("obj_id"=>array("integer", $obj_id)),
995  array());
996  }
997  else
998  {
999  $ilDB->manipulate("DELETE FROM il_certificate".
1000  " WHERE obj_id = ".$ilDB->quote($obj_id, "integer"));
1001  }
1002  }
1003 
1009  public static function _goto($ref_id)
1010  {
1011  global $ilCtrl;
1012  include_once "./Services/Object/classes/class.ilObject.php";
1013  $type = ilObject::_lookupType($ref_id, true);
1014  switch ($type)
1015  {
1016  case 'sahs':
1017  $cmd_link = "ilias.php?baseClass=ilSAHSPresentationGUI&ref_id=".$ref_id.
1018  "&cmd=downloadCertificate";
1019  ilUtil::redirect($cmd_link);
1020  break;
1021  case 'tst':
1022  default:
1023  $ilCtrl->redirectByClass("ilrepositorygui", "frameset");
1024  break;
1025  }
1026  }
1027 
1031  static function getCustomCertificateFields()
1032  {
1033  include_once("./Services/User/classes/class.ilUserDefinedFields.php");
1034  $user_field_definitions = ilUserDefinedFields::_getInstance();
1035  $fds = $user_field_definitions->getDefinitions();
1036  $fields = array();
1037  foreach ($fds as $f)
1038  {
1039  if ($f["certificate"])
1040  {
1041  $fields[$f["field_id"]] = array("name" => $f["field_name"],
1042  "ph" => "[#".str_replace(" ", "_", strtoupper($f["field_name"]))."]");
1043  }
1044  }
1045 
1046  return $fields;
1047  }
1048 
1052  public function getExchangeContent()
1053  {
1054  if(!file_exists($this->getXSLPath()))
1055  {
1056  return '';
1057  }
1058 
1059  $output = '';
1060  $xsl_file_content = file_get_contents($this->getXSLPath());
1061  $xsl = file_get_contents("./Services/Certificate/xml/fo2xhtml.xsl");
1062 
1063  if((strlen($xsl_file_content)) && (strlen($xsl)))
1064  {
1065  $args = array('/_xml' => $xsl_file_content, '/_xsl' => $xsl);
1066  $xh = xslt_create();
1067  $output = xslt_process($xh, "arg:/_xml", "arg:/_xsl", NULL, $args, NULL);
1068  xslt_error($xh);
1069  xslt_free($xh);
1070  }
1071 
1072  $output = preg_replace("/<\?xml[^>]+?>/", "", $output);
1073  // dirty hack: the php xslt processing seems not to recognize the following
1074  // replacements, so we do it in the code as well
1075  $output = str_replace("&#xA0;", "<br />", $output);
1076  $output = str_replace("&#160;", "<br />", $output);
1077 
1078  return $output;
1079  }
1080 
1081  public function outCertificateWithGivenContentAndVariables($content, $insert_tags)
1082  {
1083  global $ilLog;
1084 
1085  ilDatePresentation::setUseRelativeDates(false);
1086 
1087  $form_fields = $this->getFormFieldsFromFO();
1088  $form_fields['certificate_text'] = $content;
1089  $xslfo = $this->processXHTML2FO($form_fields);
1090 
1091  $content = $this->exchangeCertificateVariables($xslfo, $insert_tags);
1092  $content = str_replace('[BR]', "<fo:block/>", $content);
1093 
1094  include_once './Services/WebServices/RPC/classes/class.ilRpcClientFactory.php';
1095  try
1096  {
1097  $pdf_base64 = ilRpcClientFactory::factory('RPCTransformationHandler')->ilFO2PDF($content);
1098  include_once "./Services/Utilities/classes/class.ilUtil.php";
1099  ilUtil::deliverData($pdf_base64->scalar, $this->getAdapter()->getCertificateFilename(array()), "application/pdf");
1100  }
1101  catch(XML_RPC2_FaultException $e)
1102  {
1103  $ilLog->write(__METHOD__.': '.$e->getMessage());
1104  return false;
1105  }
1106  catch(Exception $e)
1107  {
1108  $ilLog->write(__METHOD__.': '.$e->getMessage());
1109  return false;
1110  }
1111 
1112  ilDatePresentation::setUseRelativeDates(true);
1113  }
1114 }
1115 
1116 ?>