ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilUtil.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
17 class ilUtil
18 {
29  public static function getImageTagByType($a_type, $a_path, $a_big = false)
30  {
31  global $lng;
32 
33  $size = ($a_big)
34  ? "big"
35  : "small";
36 
37  include_once("./Services/Object/classes/class.ilObject.php");
38  $filename = ilObject::_getIcon("", $size, $a_type);
39 
40  return "<img src=\"".$filename."\" alt=\"".$lng->txt("obj_".$a_type)."\" title=\"".$lng->txt("obj_".$a_type)."\" border=\"0\" vspace=\"0\"/>";
41  }
42 
55  public static function getTypeIconPath($a_type,$a_obj_id,$a_size = 'small')
56  {
57  include_once("./Services/Object/classes/class.ilObject.php");
58  return ilObject::_getIcon($a_obj_id, $a_size, $a_type);
59  }
60 
71  public static function getImagePath($img, $module_path = "", $mode = "output", $offline = false)
72  {
73  global $ilias, $styleDefinition, $ilCtrl, $ilUser;
74 
75  if (is_int(strpos($_SERVER["PHP_SELF"], "setup.php")))
76  {
77  $module_path = "..";
78  }
79  if ($module_path != "")
80  {
81  $module_path = "/".$module_path;
82  }
83 
84  // default image
85  $default_img = ".".$module_path."/templates/default/images/".$img;
86 
87  // use ilStyleDefinition instead of account to get the current skin and style
88  require_once("./Services/Style/classes/class.ilStyleDefinition.php");
89  $current_skin = ilStyleDefinition::getCurrentSkin();
90  $current_style = ilStyleDefinition::getCurrentStyle();
91 
92  if (is_object($styleDefinition))
93  {
94  $image_dir = $styleDefinition->getImageDirectory(
96  $current_style);
97  }
98  if ($current_skin == "default")
99  {
100  $user_img = ".".$module_path."/templates/default/".$image_dir."/".$img;
101  $skin_img = ".".$module_path."/templates/default/images/".$img;
102  }
103  else if (is_object($styleDefinition) && $current_skin != "default")
104  {
105  $user_img = "./Customizing/global/skin/".
106  $current_skin.$module_path."/".$image_dir."/".$img;
107  $skin_img = "./Customizing/global/skin/".
108  $current_skin.$module_path."/images/".$img;
109  }
110 
111  // temp svg patch
112  /*
113  $pi = pathinfo($img);
114  if ($pi["dirname"] != "") {
115  $pi["dirname"] = $pi["dirname"]."/";
116  }
117  $svg_img = ".".$module_path."/templates/default/images/".$pi["dirname"].$pi["filename"].".svg";
118  if (file_exists($svg_img))
119  {
120  return $svg_img;
121  }*/
122 
123 
124  if ($offline)
125  {
126  return "./images/".$img;
127  }
128  else if (@file_exists($user_img) && $image_dir != "")
129  {
130  return $user_img; // found image for skin and style
131  }
132  else if (file_exists($skin_img))
133  {
134  return $skin_img; // found image in skin/images
135  }
136 
137  return $default_img; // take image in default
138  }
139 
150  public static function getHtmlPath($relative_path)
151  {
152  if (substr($relative_path, 0, 2) == './')
153  {
154  $relative_path = (substr($relative_path, 1));
155  }
156  if (substr($relative_path, 0, 1) != '/')
157  {
158  $relative_path = '/' . $relative_path;
159  }
160  $htmlpath = ILIAS_HTTP_PATH . $relative_path;
161  return $htmlpath;
162  }
163 
176  public static function getStyleSheetLocation($mode = "output", $a_css_name = "", $a_css_location = "")
177  {
178  global $ilias;
179 
180  // add version as parameter to force reload for new releases
181  // use ilStyleDefinition instead of account to get the current style
182  require_once("./Services/Style/classes/class.ilStyleDefinition.php");
183  $stylesheet_name = (strlen($a_css_name))
184  ? $a_css_name
186  if (strlen($a_css_location) && (strcmp(substr($a_css_location, -1), "/") != 0))
187  {
188  $a_css_location = $a_css_location . "/";
189  }
190 
191  $filename = "";
192  // use ilStyleDefinition instead of account to get the current skin
193  require_once("./Services/Style/classes/class.ilStyleDefinition.php");
194  if (ilStyleDefinition::getCurrentSkin() != "default")
195  {
196  $filename = "./Customizing/global/skin/".ilStyleDefinition::getCurrentSkin()."/".$a_css_location.$stylesheet_name;
197  }
198  if (strlen($filename) == 0 || !file_exists($filename))
199  {
200  $filename = "./" . $a_css_location . "templates/default/".$stylesheet_name;
201  }
202  $vers = "";
203  if ($mode != "filesystem")
204  {
205  $vers = str_replace(" ", "-", $ilias->getSetting("ilias_version"));
206  $vers = "?vers=".str_replace(".", "-", $vers);
207  }
208  return $filename . $vers;
209  }
210 
221  public static function getJSLocation($a_js_name, $a_js_location = "", $add_version = FALSE)
222  {
223  global $ilias;
224 
225  // add version as parameter to force reload for new releases
226  $js_name = $a_js_name;
227  if (strlen($a_js_location) && (strcmp(substr($a_js_location, -1), "/") != 0)) $a_js_location = $a_js_location . "/";
228 
229  $filename = "";
230  // use ilStyleDefinition instead of account to get the current skin
231  require_once("./Services/Style/classes/class.ilStyleDefinition.php");
232  if (ilStyleDefinition::getCurrentSkin() != "default")
233  {
234  $filename = "./Customizing/global/skin/".ilStyleDefinition::getCurrentSkin()."/".$a_js_location.$js_name;
235  }
236  if (strlen($filename) == 0 || !file_exists($filename))
237  {
238  $filename = "./" . $a_js_location . "templates/default/".$js_name;
239  }
240  $vers = "";
241  if ($add_version)
242  {
243  $vers = str_replace(" ", "-", $ilias->getSetting("ilias_version"));
244  $vers = "?vers=".str_replace(".", "-", $vers);
245  }
246  return $filename . $vers;
247  }
248 
256  public static function getP3PLocation()
257  {
258  global $ilias;
259 
260  if (defined("ILIAS_MODULE"))
261  {
262  $base = '';
263  for($i = 0;$i < count(explode('/',ILIAS_MODULE));$i++)
264  {
265  $base .= "../Services/Privacy/";
266  }
267  }
268  else
269  {
270  $base = "./Services/Privacy/";
271  }
272 
273  if (is_file($base."w3c/p3p.xml"))
274  {
275  return ILIAS_HTTP_PATH."w3c/p3p.xml";
276  }
277  else
278  {
279  return ILIAS_HTTP_PATH."/w3c/p3p_template.xml";
280  }
281  }
282 
290  public static function getNewContentStyleSheetLocation($mode = "output")
291  {
292  global $ilias;
293 
294  // add version as parameter to force reload for new releases
295  if ($mode != "filesystem")
296  {
297  $vers = str_replace(" ", "-", $ilias->getSetting("ilias_version"));
298  $vers = "?vers=".str_replace(".", "-", $vers);
299  }
300 
301  // use ilStyleDefinition instead of account to get the current skin and style
302  require_once("./Services/Style/classes/class.ilStyleDefinition.php");
303  if (ilStyleDefinition::getCurrentSkin() == "default")
304  {
305  $in_style = "./templates/".ilStyleDefinition::getCurrentSkin()."/"
306  .ilStyleDefinition::getCurrentStyle()."_cont.css";
307  }
308  else
309  {
310  $in_style = "./Customizing/global/skin/".ilStyleDefinition::getCurrentSkin()."/"
311  .ilStyleDefinition::getCurrentStyle()."_cont.css";
312  }
313 
314  if (is_file("./".$in_style))
315  {
316  return $in_style.$vers;
317  }
318  else
319  {
320  return "templates/default/delos_cont.css".$vers;
321  }
322  }
323 
342  public static function formSelect($selected,$varname,$options,$multiple = false,$direct_text = false, $size = "0",
343  $style_class = "", $attribs = "",$disabled = false)
344  {
345  global $lng;
346 
347  if ($multiple == true)
348  {
349  $multiple = " multiple=\"multiple\"";
350  }
351  else
352  {
353  $multiple = "";
354  $size = 0;
355  }
356 
357  $class = " class=\" form-control ".$style_class."\"";
358 
359  // use form-inline!
360  // this is workaround the whole function should be set deprecated
361  // $attributes = " style='display:inline-block;' ";
362 
363  if (is_array($attribs))
364  {
365  foreach ($attribs as $key => $val)
366  {
367  $attributes .= " ".$key."=\"".$val."\"";
368  }
369  }
370  if($disabled)
371  {
372  $disabled = ' disabled=\"disabled\"';
373  }
374 
375  $str = "<select name=\"".$varname ."\"".$multiple." $class size=\"".$size."\" $attributes $disabled>\n";
376 
377  foreach ((array) $options as $key => $val)
378  {
379  $style = "";
380  if (is_array($val))
381  {
382  $style = $val["style"];
383  $val = $val["text"]; // mus be last line, since we overwrite
384  }
385 
386  $sty = ($style != "")
387  ? ' style="'.$style.'" '
388  : "";
389 
390  if ($direct_text)
391  {
392  $str .= " <option $sty value=\"".$key."\"";
393  }
394  else
395  {
396  $str .= " <option $sty value=\"".$val."\"";
397  }
398  if (is_array($selected) )
399  {
400  if (in_array($key,$selected))
401  {
402  $str .= " selected=\"selected\"";
403  }
404  }
405  else if ($selected == $key)
406  {
407  $str .= " selected=\"selected\"";
408  }
409 
410  if ($direct_text)
411  {
412  $str .= ">".$val."</option>\n";
413  }
414  else
415  {
416  $str .= ">".$lng->txt($val)."</option>\n";
417  }
418  }
419 
420  $str .= "</select>\n";
421 
422  return $str;
423  }
424 
434  public static function getSelectName ($selected,$values)
435  {
436  return($values[$selected]);
437  }
438 
450  public static function formCheckbox ($checked,$varname,$value,$disabled = false)
451  {
452  $str = "<input type=\"checkbox\" name=\"".$varname."\"";
453 
454  if ($checked == 1)
455  {
456  $str .= " checked=\"checked\"";
457  }
458 
459  if ($disabled)
460  {
461  $str .= " disabled=\"disabled\"";
462  }
463 
464  $array_var = false;
465 
466  if (substr($varname,-2) == "[]")
467  {
468  $array_var = true;
469  }
470 
471  // if varname ends with [], use varname[-2] + _ + value as id tag (e.g. "user_id[]" => "user_id_15")
472  if ($array_var)
473  {
474  $varname_id = substr($varname,0,-2)."_".$value;
475  }
476  else
477  {
478  $varname_id = $varname;
479  }
480 
481  // dirty removal of other "[]" in string
482  $varname_id = ereg_replace("\[","_",$varname_id);
483  $varname_id = ereg_replace("\]","",$varname_id);
484 
485  $str .= " value=\"".$value."\" id=\"".$varname_id."\" />\n";
486 
487  return $str;
488  }
489 
501  public static function formDisabledRadioButton($checked,$varname,$value,$disabled)
502  {
503  if ($disabled) {
504  $str = "<input disabled type=\"radio\" name=\"".$varname."\"";
505  }
506  else {
507  $str = "<input type=\"radio\" name=\"".$varname."\"";
508  }
509  if ($checked == 1)
510  {
511  $str .= " checked=\"checked\"";
512  }
513 
514  $str .= " value=\"".$value."\"";
515  $str .= " id=\"".$value."\" />\n";
516 
517  return $str;
518 
519  }
520 
521 
532  public static function formRadioButton($checked,$varname,$value,$onclick=null, $disabled = false)
533  {
534  $str = '<input ';
535 
536  if($onclick)
537  {
538  $str .= ('onclick="'.$onclick.'"');
539  }
540 
541  $str .= (" type=\"radio\" name=\"".$varname."\"");
542  if ($checked == 1)
543  {
544  $str .= " checked=\"checked\"";
545  }
546 
547  if ($disabled)
548  {
549  $str .= " disabled=\"disabled\"";
550  }
551 
552  $str .= " value=\"".$value."\"";
553 
554  $str .= " id=\"".$value."\" />\n";
555 
556  return $str;
557  }
558 
559 
569  public static function formInput($varname,$value,$disabled = false)
570  {
571 
572  $str = "<input type=\"input\" name=\"".$varname."\"";
573  if ($disabled)
574  {
575  $str .= " disabled";
576  }
577 
578  $str .= " value=\"".$value."\"";
579 
580  $str .= " id=\"".$value."\" />\n";
581 
582  return $str;
583  }
584 
585 
592  public static function checkInput ($vars)
593  {
594  // TO DO:
595  // Diese Funktion soll Formfeldeingaben berprfen (empty und required)
596  }
597 
604  public static function setPathStr ($a_path)
605  {
606  if ("" != $a_path && "/" != substr($a_path, -1))
607  {
608  $a_path .= "/";
609  //$a_path = substr($a_path,1);
610  }
611 
612  //return getcwd().$a_path;
613  return $a_path;
614  }
615 
628  public static function switchColor ($a_num,$a_css1,$a_css2)
629  {
630  if (!($a_num % 2))
631  {
632  return $a_css1;
633  }
634  else
635  {
636  return $a_css2;
637  }
638  }
639 
648  public static function checkFormEmpty ($emptyFields)
649  {
650 
651  $feedback = "";
652 
653  foreach ($emptyFields as $key => $val)
654  {
655  if ($val == "") {
656  if ($feedback != "") $feedback .= ", ";
657  $feedback .= $key;
658  }
659  }
660 
661  return $feedback;
662  }
663 
688  public static function Linkbar ($AScript,$AHits,$ALimit,$AOffset,$AParams = array(),$ALayout = array(), $prefix = '')
689  {
690  $LinkBar = "";
691 
692  $layout_link = "";
693  $layout_prev = "&lt;&lt;";
694  $layout_next = "&gt;&gt;";
695 
696  // layout options
697  if (count($ALayout > 0))
698  {
699  if ($ALayout["link"])
700  {
701  $layout_link = " class=\"".$ALayout["link"]."\"";
702  }
703 
704  if ($ALayout["prev"])
705  {
706  $layout_prev = $ALayout["prev"];
707  }
708 
709  if ($ALayout["next"])
710  {
711  $layout_next = $ALayout["next"];
712  }
713  }
714 
715  // show links, if hits greater limit
716  // or offset > 0 (can be > 0 due to former setting)
717  if ($AHits > $ALimit || $AOffset > 0)
718  {
719  if (!empty($AParams))
720  {
721  foreach ($AParams as $key => $value)
722  {
723  $params.= $key."=".$value."&";
724  }
725  }
726  // if ($params) $params = substr($params,0,-1);
727  if(strpos($AScript,'&'))
728  {
729  $link = $AScript."&".$params.$prefix."offset=";
730  }
731  else
732  {
733  $link = $AScript."?".$params.$prefix."offset=";
734  }
735 
736  // ?bergehe "zurck"-link, wenn offset 0 ist.
737  if ($AOffset >= 1)
738  {
739  $prevoffset = $AOffset - $ALimit;
740  if ($prevoffset < 0) $prevoffset = 0;
741  $LinkBar .= "<a".$layout_link." href=\"".$link.$prevoffset."\">".$layout_prev."&nbsp;</a>";
742  }
743 
744  // Ben?tigte Seitenzahl kalkulieren
745  $pages=intval($AHits/$ALimit);
746 
747  // Wenn ein Rest bleibt, addiere eine Seite
748  if (($AHits % $ALimit))
749  $pages++;
750 
751  // Bei Offset = 0 keine Seitenzahlen anzeigen : DEAKTIVIERT
752  // if ($AOffset != 0) {
753 
754  // ansonsten zeige Links zu den anderen Seiten an
755  for ($i = 1 ;$i <= $pages ; $i++)
756  {
757  $newoffset=$ALimit*($i-1);
758 
759  if ($newoffset == $AOffset)
760  {
761  $LinkBar .= "[".$i."] ";
762  }
763  else
764  {
765  $LinkBar .= '<a '.$layout_link.' href="'.
766  $link.$newoffset.'">['.$i.']</a> ';
767  }
768  }
769  // }
770 
771  // Checken, ob letze Seite erreicht ist
772  // Wenn nicht, gebe einen "Weiter"-Link aus
773  if (! ( ($AOffset/$ALimit)==($pages-1) ) && ($pages!=1) )
774  {
775  $newoffset=$AOffset+$ALimit;
776  $LinkBar .= "<a".$layout_link." href=\"".$link.$newoffset."\">&nbsp;".$layout_next."</a>";
777  }
778 
779  return $LinkBar;
780  }
781  else
782  {
783  return false;
784  }
785  }
786 
798  public static function makeClickable($a_text, $detectGotoLinks = false)
799  {
800  // New code, uses MediaWiki Sanitizer
801  $ret = $a_text;
802 
803  // www-URL ohne ://-Angabe
804  $ret = eregi_replace("(^|[[:space:]]+)(www\.)([[:alnum:]#?/&=\.-]+)",
805  "\\1http://\\2\\3", $ret);
806 
807  // ftp-URL ohne ://-Angabe
808  $ret = eregi_replace("(^|[[:space:]]+)(ftp\.)([[:alnum:]#?/&=\.-]+)",
809  "\\1ftp://\\2\\3", $ret);
810 
811  // E-Mail (this does not work as expected, users must add mailto: manually)
812  //$ret = eregi_replace("(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))",
813  // "mailto:\\1", $ret);
814 
815  // mask existing image tags
816  $ret = str_replace('src="http://', '"***masked_im_start***', $ret);
817 
818  include_once("./Services/Utilities/classes/class.ilMWParserAdapter.php");
819  $parser = new ilMWParserAdapter();
820  $ret = $parser->replaceFreeExternalLinks($ret);
821 
822  // unmask existing image tags
823  $ret = str_replace('"***masked_im_start***', 'src="http://', $ret);
824 
825  // Should be Safe
826 
827  if ($detectGotoLinks)
828  // replace target blank with self and text with object title.
829  {
830  $regExp = "<a[^>]*href=\"(".str_replace("/","\/",ILIAS_HTTP_PATH)."\/goto.php\?target=\w+_(\d+)[^\"]*)\"[^>]*>[^<]*<\/a>";
831 // echo htmlentities($regExp);
832  $ret = preg_replace_callback(
833  "/".$regExp."/i",
834  array("ilUtil", "replaceLinkProperties"),
835  $ret);
836 
837  // Static links
838  $regExp = "<a[^>]*href=\"(".str_replace("/","\/",ILIAS_HTTP_PATH)."\/goto_.*[a-z0-9]+_([0-9]+)\.html)\"[^>]*>[^<]*<\/a>";
839 // echo htmlentities($regExp);
840  $ret = preg_replace_callback(
841  "/".$regExp."/i",
842  array("ilUtil", "replaceLinkProperties"),
843  $ret);
844  }
845 
846  return($ret);
847  }
848 
862  public static function replaceLinkProperties ($matches)
863  {
864  $link = $matches[0];
865  $ref_id = $matches[2];
866 
867  if ($ref_id > 0)
868  {
869  $obj_id = ilObject::_lookupObjId($ref_id);
870  if ($obj_id > 0)
871  {
872  $title = ilObject::_lookupTitle($obj_id);
873  $link = "<a href=".$matches[1]." target=\"_self\">".$title."</a>";
874  }
875  }
876  return $link;
877  }
878 
897  public static function makeDateSelect($prefix, $year = "", $month = "", $day = "", $startyear = "",$a_long_month = true,$a_further_options = array(), $emptyoption = false)
898  {
899  global $lng;
900 
901  $disabled = '';
902  if(isset($a_further_options['disabled']) and $a_further_options['disabled'])
903  {
904  $disabled = 'disabled="disabled" ';
905  }
906 
907  $now = getdate();
908  if (!$emptyoption)
909  {
910  if (!strlen($year)) $year = $now["year"];
911  if (!strlen($month)) $month = $now["mon"];
912  if (!strlen($day)) $day = $now["mday"];
913  }
914 
915  $year = (int) $year;
916  $month = (int) $month;
917  $day = (int) $day;
918 
919  // build day select
920 
921  $sel_day .= '<select class="form-control" ';
922  if(isset($a_further_options['select_attributes']))
923  {
924  foreach($a_further_options['select_attributes'] as $name => $value)
925  {
926  $sel_day .= ($name.'="'.$value.'" ');
927  }
928  }
929 
930  $sel_day .= $disabled."name=\"".$prefix."[d]\" id=\"".$prefix."_d\">\n";
931 
932  if ($emptyoption) $sel_day .= "<option value=\"0\">--</option>\n";
933  for ($i = 1; $i <= 31; $i++)
934  {
935  $sel_day .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
936  }
937  $sel_day .= "</select>\n";
938  $sel_day = preg_replace("/(value\=\"$day\")/", "$1 selected=\"selected\"", $sel_day);
939 
940  // build month select
941  $sel_month = '<select class="form-control" ';
942  if(isset($a_further_options['select_attributes']))
943  {
944  foreach($a_further_options['select_attributes'] as $name => $value)
945  {
946  $sel_month .= ($name.'="'.$value.'" ');
947  }
948  }
949  $sel_month .= $disabled."name=\"".$prefix."[m]\" id=\"".$prefix."_m\">\n";
950 
951  if ($emptyoption) $sel_month .= "<option value=\"0\">--</option>\n";
952  for ($i = 1; $i <= 12; $i++)
953  {
954  if($a_long_month)
955  {
956  $sel_month .= "<option value=\"$i\">" . $lng->txt("month_" . sprintf("%02d", $i) . "_long") . "</option>\n";
957  }
958  else
959  {
960  $sel_month .= "<option value=\"$i\">" . $i . "</option>\n";
961  }
962  }
963  $sel_month .= "</select>\n";
964  $sel_month = preg_replace("/(value\=\"$month\")/", "$1 selected=\"selected\"", $sel_month);
965 
966  // build year select
967  $sel_year = '<select class="form-control" ';
968  if(isset($a_further_options['select_attributes']))
969  {
970  foreach($a_further_options['select_attributes'] as $name => $value)
971  {
972  $sel_year .= ($name.'="'.$value.'" ');
973  }
974  }
975  $sel_year .= $disabled."name=\"".$prefix."[y]\" id=\"".$prefix."_y\">\n";
976  if ((strlen($startyear) == 0) || ($startyear > $year))
977  {
978  if (!$emptyoption || $year != 0) $startyear = $year - 5;
979  }
980 
981  if(($year + 5) < (date('Y',time()) + 5))
982  {
983  $end_year = date('Y',time()) + 5;
984  }
985  else
986  {
987  $end_year = $year + 5;
988  }
989 
990  if ($emptyoption) $sel_year .= "<option value=\"0\">----</option>\n";
991  for ($i = $startyear; $i <= $end_year; $i++)
992  {
993  $sel_year .= "<option value=\"$i\">" . sprintf("%04d", $i) . "</option>\n";
994  }
995  $sel_year .= "</select>\n";
996  $sel_year = preg_replace("/(value\=\"$year\")/", "$1 selected=\"selected\"", $sel_year);
997 
998  //$dateformat = $lng->text["lang_dateformat"];
999  $dateformat = "d-m-Y";
1000  $dateformat = strtolower(preg_replace("/\W/", "", $dateformat));
1001  $dateformat = strtolower(preg_replace("/(\w)/", "%%$1", $dateformat));
1002  $dateformat = preg_replace("/%%d/", $sel_day, $dateformat);
1003  $dateformat = preg_replace("/%%m/", $sel_month, $dateformat);
1004  $dateformat = preg_replace("/%%y/", $sel_year, $dateformat);
1005  return $dateformat;
1006  }
1007 
1026  public static function makeTimeSelect($prefix, $short = true, $hour = "", $minute = "", $second = "",$a_use_default = true,$a_further_options = array())
1027  {
1028  global $lng, $ilUser;
1029 
1030  $minute_steps = 1;
1031  $disabled = '';
1032  if(count($a_further_options))
1033  {
1034  if(isset($a_further_options['minute_steps']))
1035  {
1036  $minute_steps = $a_further_options['minute_steps'];
1037  }
1038  if(isset($a_further_options['disabled']) and $a_further_options['disabled'])
1039  {
1040  $disabled = 'disabled="disabled" ';
1041  }
1042  }
1043 
1044  if ($a_use_default and !strlen("$hour$minute$second")) {
1045  $now = localtime();
1046  $hour = $now[2];
1047  $minute = $now[1];
1048  $second = $now[0];
1049  } else {
1050  $hour = (int)$hour;
1051  $minute = (int)$minute;
1052  $second = (int)$second;
1053  }
1054  // build hour select
1055  $sel_hour = '<select ';
1056  if(isset($a_further_options['select_attributes']))
1057  {
1058  foreach($a_further_options['select_attributes'] as $name => $value)
1059  {
1060  $sel_hour .= $name.'='.$value.' ';
1061  }
1062  }
1063  $sel_hour .= " ".$disabled."name=\"".$prefix."[h]\" id=\"".$prefix."_h\" class=\"form-control\">\n";
1064 
1065  $format = $ilUser->getTimeFormat();
1066  for ($i = 0; $i <= 23; $i++)
1067  {
1068  if($format == ilCalendarSettings::TIME_FORMAT_24)
1069  {
1070  $sel_hour .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
1071  }
1072  else
1073  {
1074  $sel_hour .= "<option value=\"$i\">" . date("ga", mktime($i, 0, 0)) . "</option>\n";
1075  }
1076  }
1077  $sel_hour .= "</select>\n";
1078  $sel_hour = preg_replace("/(value\=\"$hour\")/", "$1 selected=\"selected\"", $sel_hour);
1079 
1080  // build minutes select
1081  $sel_minute .= "<select ".$disabled."name=\"".$prefix."[m]\" id=\"".$prefix."_m\" class=\"form-control\">\n";
1082 
1083  for ($i = 0; $i <= 59; $i = $i + $minute_steps)
1084  {
1085  $sel_minute .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
1086  }
1087  $sel_minute .= "</select>\n";
1088  $sel_minute = preg_replace("/(value\=\"$minute\")/", "$1 selected=\"selected\"", $sel_minute);
1089 
1090  if (!$short) {
1091  // build seconds select
1092  $sel_second .= "<select ".$disabled."name=\"".$prefix."[s]\" id=\"".$prefix."_s\" class=\"form-control\">\n";
1093 
1094  for ($i = 0; $i <= 59; $i++)
1095  {
1096  $sel_second .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
1097  }
1098  $sel_second .= "</select>\n";
1099  $sel_second = preg_replace("/(value\=\"$second\")/", "$1 selected=\"selected\"", $sel_second);
1100  }
1101  $timeformat = $lng->text["lang_timeformat"];
1102  if (strlen($timeformat) == 0) $timeformat = "H:i:s";
1103  $timeformat = strtolower(preg_replace("/\W/", "", $timeformat));
1104  $timeformat = preg_replace("/(\w)/", "%%$1", $timeformat);
1105  $timeformat = preg_replace("/%%h/", $sel_hour, $timeformat);
1106  $timeformat = preg_replace("/%%i/", $sel_minute, $timeformat);
1107  if ($short) {
1108  $timeformat = preg_replace("/%%s/", "", $timeformat);
1109  } else {
1110  $timeformat = preg_replace("/%%s/", $sel_second, $timeformat);
1111  }
1112  return $timeformat;
1113  }
1114 
1128  public static function is_email($a_email)
1129  {
1130  // BEGIN Mail: If possible, use PearMail to validate e-mail address
1131  global $ilErr;
1132 
1133  // additional check for ilias object is needed,
1134  // otherwise setup will fail with this if branch
1135  if(is_object($ilErr)) // seems to work in Setup now
1136  {
1137  require_once './Services/PEAR/lib/Mail/RFC822.php';
1138  $parser = new Mail_RFC822();
1140  $addresses = $parser->parseAddressList($a_email, 'ilias', false, true);
1141  if(!is_a($addresses, 'PEAR_Error') &&
1142  count($addresses) == 1 && $addresses[0]->host != 'ilias'
1143  )
1144  {
1145  PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($ilErr, "errorHandler"));
1146  return true;
1147  }
1148  else
1149  {
1150  PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($ilErr, "errorHandler"));
1151  return false;
1152  }
1153  }
1154  else
1155  {
1156  $tlds = strtolower(
1157  "AC|AD|AE|AERO|AF|AG|AI|AL|AM|AN|AO|AQ|AR|ARPA|AS|ASIA|AT|AU|AW|AX|AZ|BA|BB|BD|BE|BF|BG|BH|BI|BIZ|BJ|BM|BN|BO|BR|BS|BT|BV|BW|BY|".
1158  "BZ|CA|CAT|CC|CD|CF|CG|CH|CI|CK|CL|CM|CN|CO|COM|COOP|CR|CU|CV|CX|CY|CZ|DE|DJ|DK|DM|DO|DZ|EC|EDU|EE|EG|".
1159  "ER|ES|ET|EU|FI|FJ|FK|FM|FO|FR|GA|GB|GD|GE|GF|GG|GH|GI|GL|GM|GN|GOV|GP|GQ|GR|GS|GT|GU|GW|GY|HK|HM|HN|HR|HT|".
1160  "HU|ID|IE|IL|IM|IN|INFO|INT|IO|IQ|IR|IS|IT|JE|JM|JO|JOBS|JP|KE|KG|KH|KI|KM|KN|KP|KR|KW|KY|KZ|LA|LB|LC|".
1161  "LI|LK|LR|LS|LT|LU|LV|LY|MA|MC|MD|ME|MG|MH|MIL|MK|ML|MM|MN|MO|MOBI|MP|MQ|MR|MS|MT|MU|MUSEUM|MV|MW|MX|".
1162  "MY|MZ|NA|NAME|NC|NE|NET|NF|NG|NI|NL|NO|NP|NR|NU|NZ|OM|ORG|PA|PE|PF|PG|PH|PK|PL|PM|PN|PR|PRO|PS|".
1163  "PT|PW|PY|QA|RE|RO|RS|RU|RW|SA|SB|SC|SD|SE|SG|SH|SI|SJ|SK|SL|SM|SN|SO|SR|ST|SU|SV|SY|SZ|TC|TD|TEL|".
1164  "TF|TG|TH|TJ|TK|TL|TM|TN|TO|TP|TR|TRAVEL|TT|TV|TW|TZ|UA|UG|UK|US|UY|UZ|VA|VC|VE|VG|VI|VN|VU|".
1165  "WF|WS|XN|YE|YT|YU|ZA|ZM|ZW");
1166 
1167  return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(".$tlds.")|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$a_email));
1168  }
1169  // END Mail: If possible, use PearMail to validate e-mail address
1170  }
1171 
1180  public static function isPassword($a_passwd, &$customError = null)
1181  {
1182  global $lng;
1183 
1184  include_once('./Services/PrivacySecurity/classes/class.ilSecuritySettings.php');
1185  $security = ilSecuritySettings::_getInstance();
1186 
1187  // check if password is empty
1188  if( empty($a_passwd) )
1189  {
1190  $customError = $lng->txt('password_empty');
1191  return false;
1192  }
1193 
1194  $isPassword = true;
1195  $errors = array();
1196 
1197  // check if password to short
1198  if( $security->getPasswordMinLength() > 0 && strlen($a_passwd) < $security->getPasswordMinLength() )
1199  {
1200  $errors[] = sprintf( $lng->txt('password_to_short'), $security->getPasswordMinLength() );
1201  $isPassword = false;
1202  }
1203 
1204  // check if password not to long
1205  // Hmmmmm, maybe we should discuss this limitation. In my opinion it is stupid to limit the password length ;-). There should only be a technical limitation (field size in database).
1206  if( $security->getPasswordMaxLength() > 0 && strlen($a_passwd) > $security->getPasswordMaxLength() )
1207  {
1208  $errors[] = sprintf( $lng->txt('password_to_long'), $security->getPasswordMaxLength() );
1209  $isPassword = false;
1210  }
1211 
1212  // if password must contains Chars and Numbers
1213  if( $security->isPasswordCharsAndNumbersEnabled() )
1214  {
1215  $hasCharsAndNumbers = true;
1216 
1217  // check password for existing chars
1218  if( !preg_match('/[A-Za-z]+/',$a_passwd) )
1219  {
1220  $hasCharsAndNumbers = false;
1221  }
1222 
1223  // check password for existing numbers
1224  if( !preg_match('/[0-9]+/',$a_passwd) )
1225  {
1226  $hasCharsAndNumbers = false;
1227  }
1228 
1229  if( !$hasCharsAndNumbers )
1230  {
1231  $errors[] = $lng->txt('password_must_chars_and_numbers');
1232  $isPassword = false;
1233  }
1234  }
1235 
1236  require_once 'Services/Utilities/classes/class.ilStr.php';
1237  if($security->getPasswordNumberOfUppercaseChars() > 0)
1238  {
1239  if(ilStr::strLen($a_passwd) - ilStr::strLen(preg_replace('/[A-Z]/', '', $a_passwd)) < $security->getPasswordNumberOfUppercaseChars())
1240  {
1241  $errors[] = sprintf($lng->txt('password_must_contain_ucase_chars'), $security->getPasswordNumberOfUppercaseChars());
1242  $isPassword = false;
1243  }
1244  }
1245 
1246  if($security->getPasswordNumberOfLowercaseChars() > 0)
1247  {
1248  if(ilStr::strLen($a_passwd) - ilStr::strLen(preg_replace('/[a-z]/', '', $a_passwd)) < $security->getPasswordNumberOfLowercaseChars())
1249  {
1250  $errors[] = sprintf($lng->txt('password_must_contain_lcase_chars'), $security->getPasswordNumberOfLowercaseChars());
1251  $isPassword = false;
1252  }
1253  }
1254 
1255  // if password must contains Special-Chars
1256  if( $security->isPasswordSpecialCharsEnabled() )
1257  {
1258  // check password for existing special-chars
1259  if( !preg_match( self::getPasswordValidChars(true, true) , $a_passwd) )
1260  {
1261  $errors[] = $lng->txt('password_must_special_chars');
1262  $isPassword = false;
1263  }
1264  }
1265 
1266  // ensure password matches the positive list of chars/special-chars
1267  if( !preg_match( self::getPasswordValidChars() , $a_passwd) )
1268  {
1269  $errors[] = $lng->txt('password_contains_invalid_chars');
1270  $isPassword = false;
1271  }
1272 
1273  // build custom error message
1274  if( count($errors) == 1 )
1275  {
1276  $customError = $errors[0];
1277  }
1278  elseif( count($errors) > 1 )
1279  {
1280  $customError = $lng->txt('password_multiple_errors');
1281  $customError .= '<br />'.implode('<br />', $errors);
1282  }
1283 
1284  return $isPassword;
1285  }
1286 
1293  public static function isPasswordValidForUserContext($clear_text_password, $user, &$error_language_variable = null)
1294  {
1295  include_once 'Services/PrivacySecurity/classes/class.ilSecuritySettings.php';
1296  $security = ilSecuritySettings::_getInstance();
1297 
1298  $login = null;
1299 
1300  if(is_string($user))
1301  {
1302  $login = $user;
1303  }
1304  else if(is_array($user))
1305  {
1306  // Try to get loginname and user_id from array
1307  $login = $user['login'];
1308  $userId = $user['id'];
1309  }
1310  else if($user instanceof ilObjUser)
1311  {
1312  $login = $user->getLogin();
1313  $userId = $user->getId();
1314  }
1315 
1316  // The user context (user instance or id) can be used for further validation (e.g. compare a password with the users' password history, etc.) in future releases.
1317 
1318  if($login && (int)$security->getPasswordMustNotContainLoginnameStatus() &&
1319  strpos(strtolower($clear_text_password), strtolower($login)) !== false
1320  )
1321  {
1322  $error_language_variable = 'password_contains_parts_of_login_err';
1323  return false;
1324  }
1325 
1326  return true;
1327  }
1328 
1336  public static function getPasswordValidChars($a_as_regex = true, $a_only_special_chars = false)
1337  {
1338  if( $a_as_regex )
1339  {
1340  if( $a_only_special_chars )
1341  {
1342  return '/[_\.\+\?\#\-\*\@!\$\%\~\/\:\;]+/';
1343  }
1344  else
1345  {
1346  return '/^[A-Za-z0-9_\.\+\?\#\-\*\@!\$\%\~\/\:\;]+$/';
1347  }
1348  }
1349  else
1350  {
1351  return 'A-Z a-z 0-9 _.+?#-*@!$%~/:;';
1352  }
1353  }
1354 
1362  public static function getPasswordRequirementsInfo()
1363  {
1364  global $lng;
1365 
1366  include_once('./Services/PrivacySecurity/classes/class.ilSecuritySettings.php');
1367  $security = ilSecuritySettings::_getInstance();
1368 
1369  $infos = array(sprintf($lng->txt('password_allow_chars'), self::getPasswordValidChars(false)));
1370 
1371  // check if password to short
1372  if( $security->getPasswordMinLength() > 0 )
1373  {
1374  $infos[] = sprintf( $lng->txt('password_to_short'), $security->getPasswordMinLength() );
1375  }
1376 
1377  // check if password not to long
1378  if( $security->getPasswordMaxLength() > 0 )
1379  {
1380  $infos[] = sprintf( $lng->txt('password_to_long'), $security->getPasswordMaxLength() );
1381  }
1382 
1383  // if password must contains Chars and Numbers
1384  if( $security->isPasswordCharsAndNumbersEnabled() )
1385  {
1386  $infos[] = $lng->txt('password_must_chars_and_numbers');
1387  }
1388 
1389  // if password must contains Special-Chars
1390  if( $security->isPasswordSpecialCharsEnabled() )
1391  {
1392  $infos[] = $lng->txt('password_must_special_chars');
1393  }
1394 
1395  if($security->getPasswordNumberOfUppercaseChars() > 0)
1396  {
1397  $infos[] = sprintf($lng->txt('password_must_contain_ucase_chars'), $security->getPasswordNumberOfUppercaseChars());
1398  }
1399 
1400  if($security->getPasswordNumberOfLowercaseChars() > 0)
1401  {
1402  $infos[] = sprintf($lng->txt('password_must_contain_lcase_chars'), $security->getPasswordNumberOfLowercaseChars());
1403  }
1404 
1405  return implode('<br />', $infos);
1406  }
1407 
1408  /*
1409  * validates a login
1410  * @access public
1411  * @param string login
1412  * @return boolean true if valid
1413  */
1414  function isLogin($a_login)
1415  {
1416  if (empty($a_login))
1417  {
1418  return false;
1419  }
1420 
1421  if (strlen($a_login) < 3)
1422  {
1423  return false;
1424  }
1425 
1426  // FIXME - If ILIAS is configured to use RFC 822
1427  // compliant mail addresses we should not
1428  // allow the @ character.
1429  if (!ereg("^[A-Za-z0-9_\.\+\*\@!\$\%\~\-]+$", $a_login))
1430  {
1431  return false;
1432  }
1433 
1434  return true;
1435  }
1436 
1450  public static function shortenText ($a_str, $a_len, $a_dots = false, $a_next_blank = false,
1451  $a_keep_extension = false)
1452  {
1453  include_once("./Services/Utilities/classes/class.ilStr.php");
1454  if (ilStr::strLen($a_str) > $a_len)
1455  {
1456  if ($a_next_blank)
1457  {
1458  $len = ilStr::strPos($a_str, " ", $a_len);
1459  }
1460  else
1461  {
1462  $len = $a_len;
1463  }
1464  // BEGIN WebDAV
1465  // - Shorten names in the middle, before the filename extension
1466  // Workaround for Windows WebDAV Client:
1467  // Use the unicode ellipsis symbol for shortening instead of
1468  // three full stop characters.
1469  if ($a_keep_extension)
1470  {
1471  $p = strrpos($a_str, '.'); // this messes up normal shortening, see bug #6190
1472  }
1473  if ($p === false || $p == 0 || strlen($a_str) - $p > $a_len)
1474  {
1475  $a_str = ilStr::subStr($a_str,0,$len);
1476  if ($a_dots)
1477  {
1478  $a_str .= "\xe2\x80\xa6"; // UTF-8 encoding for Unicode ellipsis character.
1479  }
1480  }
1481  else
1482  {
1483  if ($a_dots)
1484  {
1485  $a_str = ilStr::subStr($a_str,0,$len - (strlen($a_str) - $p + 1))."\xe2\x80\xa6".substr($a_str, $p);
1486  }
1487  else
1488  {
1489  $a_str = ilStr::subStr($a_str,0,$len - (strlen($a_str) - $p + 1)).substr($a_str, $p);
1490  }
1491  }
1492  }
1493 
1494  return $a_str;
1495  }
1496 
1507  public static function shortenWords($a_str, $a_len = 30, $a_dots = true)
1508  {
1509  include_once("./Services/Utilities/classes/class.ilStr.php");
1510  $str_arr = explode(" ", $a_str);
1511 
1512  for ($i = 0; $i < count($str_arr); $i++)
1513  {
1514  if (ilStr::strLen($str_arr[$i]) > $a_len)
1515  {
1516  $str_arr[$i] = ilStr::subStr($str_arr[$i], 0, $a_len);
1517  if ($a_dots)
1518  {
1519  $str_arr[$i].= "...";
1520  }
1521  }
1522  }
1523 
1524  return implode($str_arr, " ");
1525  }
1526 
1536  public static function attribsToArray($a_str)
1537  {
1538  $attribs = array();
1539  while (is_int(strpos($a_str, "=")))
1540  {
1541  $eq_pos = strpos($a_str, "=");
1542  $qu1_pos = strpos($a_str, "\"");
1543  $qu2_pos = strpos(substr($a_str, $qu1_pos + 1), "\"") + $qu1_pos + 1;
1544  if (is_int($eq_pos) && is_int($qu1_pos) && is_int($qu2_pos))
1545  {
1546  $var = trim(substr($a_str, 0, $eq_pos));
1547  $val = trim(substr($a_str, $qu1_pos + 1, ($qu2_pos - $qu1_pos) - 1));
1548  $attribs[$var] = $val;
1549  $a_str = substr($a_str, $qu2_pos + 1);
1550  }
1551  else
1552  {
1553  $a_str = "";
1554  }
1555  }
1556  return $attribs;
1557  }
1558 
1570  public static function rCopy ($a_sdir, $a_tdir, $preserveTimeAttributes = false)
1571  {
1572  // check if arguments are directories
1573  if (!@is_dir($a_sdir) or
1574  !@is_dir($a_tdir))
1575  {
1576  return FALSE;
1577  }
1578 
1579  // read a_sdir, copy files and copy directories recursively
1580  $dir = opendir($a_sdir);
1581 
1582  while($file = readdir($dir))
1583  {
1584  if ($file != "." and
1585  $file != "..")
1586  {
1587  // directories
1588  if (@is_dir($a_sdir."/".$file))
1589  {
1590  if (!@is_dir($a_tdir."/".$file))
1591  {
1592  if (!ilUtil::makeDir($a_tdir."/".$file))
1593  return FALSE;
1594 
1595  //chmod($a_tdir."/".$file, 0775);
1596  }
1597 
1598  if (!ilUtil::rCopy($a_sdir."/".$file,$a_tdir."/".$file))
1599  {
1600  return FALSE;
1601  }
1602  }
1603 
1604  // files
1605  if (@is_file($a_sdir."/".$file))
1606  {
1607  if (!copy($a_sdir."/".$file,$a_tdir."/".$file))
1608  {
1609  return FALSE;
1610  }
1611  if ($preserveTimeAttributes)
1612  touch($a_tdir."/".$file, filectime($a_sdir."/".$file));
1613  }
1614  }
1615  }
1616  return TRUE;
1617  }
1618 
1627  public static function getWebspaceDir($mode = "filesystem")
1628  {
1629  global $ilias;
1630 
1631  if ($mode == "filesystem")
1632  {
1633  return "./".ILIAS_WEB_DIR."/".$ilias->client_id;
1634  }
1635  else
1636  {
1637  if (defined("ILIAS_MODULE"))
1638  {
1639  return "../".ILIAS_WEB_DIR."/".$ilias->client_id;
1640  }
1641  else
1642  {
1643  return "./".ILIAS_WEB_DIR."/".$ilias->client_id;
1644  }
1645  }
1646 
1647  //return $ilias->ini->readVariable("server","webspace_dir");
1648  }
1649 
1656  public static function getDataDir()
1657  {
1658  return CLIENT_DATA_DIR;
1659  //global $ilias;
1660 
1661  //return $ilias->ini->readVariable("server", "data_dir");
1662  }
1663 
1673  public static function getUsersOnline($a_user_id = 0)
1674  {
1675  include_once("./Services/User/classes/class.ilObjUser.php");
1676  return ilObjUser::_getUsersOnline($a_user_id);
1677  }
1678 
1689  public static function getAssociatedUsersOnline($a_user_id)
1690  {
1691  include_once("./Services/User/classes/class.ilObjUser.php");
1692  return ilObjUser::_getAssociatedUsersOnline($a_user_id);
1693  }
1694 
1702  public static function ilTempnam()
1703  {
1704  $temp_path = ilUtil::getDataDir() . "/temp";
1705  if (!is_dir($temp_path))
1706  {
1707  ilUtil::createDirectory($temp_path);
1708  }
1709  $temp_name = tempnam($temp_path, "tmp");
1710  // --->
1711  // added the following line because tempnam creates a backslash on some
1712  // Windows systems which leads to problems, because the "...\tmp..." can be
1713  // interpreted as "...{TAB-CHARACTER}...". The normal slash works fine
1714  // even under windows (Helmut Schottmüller, 2005-08-31)
1715  $temp_name = str_replace("\\", "/", $temp_name);
1716  // --->
1717  unlink($temp_name);
1718  return $temp_name;
1719  }
1720 
1729  public static function createDirectory($a_dir, $a_mod = 0755)
1730  {
1731  ilUtil::makeDir($a_dir);
1732  //@mkdir($a_dir);
1733  //@chmod($a_dir, $a_mod);
1734  }
1735 
1736 
1745  public static function unzip($a_file, $overwrite = false, $a_flat = false)
1746  {
1747  if (!is_file($a_file))
1748  {
1749  return;
1750  }
1751 
1752  // if flat, move file to temp directory first
1753  if ($a_flat)
1754  {
1755  $tmpdir = ilUtil::ilTempnam();
1756  ilUtil::makeDir($tmpdir);
1757  copy($a_file, $tmpdir.DIRECTORY_SEPARATOR.basename($a_file));
1758  $orig_file = $a_file;
1759  $a_file = $tmpdir.DIRECTORY_SEPARATOR.basename($a_file);
1760  $origpathinfo = pathinfo($orig_file);
1761  }
1762 
1763  $pathinfo = pathinfo($a_file);
1764  $dir = $pathinfo["dirname"];
1765  $file = $pathinfo["basename"];
1766 
1767  // unzip
1768  $cdir = getcwd();
1769  chdir($dir);
1770  $unzip = PATH_TO_UNZIP;
1771 
1772  // the following workaround has been removed due to bug
1773  // http://www.ilias.de/mantis/view.php?id=7578
1774  // since the workaround is quite old, it may not be necessary
1775  // anymore, alex 9 Oct 2012
1776 /*
1777  // workaround for unzip problem (unzip of subdirectories fails, so
1778  // we create the subdirectories ourselves first)
1779  // get list
1780  $unzipcmd = "-Z -1 ".ilUtil::escapeShellArg($file);
1781  $arr = ilUtil::execQuoted($unzip, $unzipcmd);
1782  $zdirs = array();
1783 
1784  foreach($arr as $line)
1785  {
1786  if(is_int(strpos($line, "/")))
1787  {
1788  $zdir = substr($line, 0, strrpos($line, "/"));
1789  $nr = substr_count($zdir, "/");
1790  //echo $zdir." ".$nr."<br>";
1791  while ($zdir != "")
1792  {
1793  $nr = substr_count($zdir, "/");
1794  $zdirs[$zdir] = $nr; // collect directories
1795  //echo $dir." ".$nr."<br>";
1796  $zdir = substr($zdir, 0, strrpos($zdir, "/"));
1797  }
1798  }
1799  }
1800 
1801  asort($zdirs);
1802 
1803  foreach($zdirs as $zdir => $nr) // create directories
1804  {
1805  ilUtil::createDirectory($zdir);
1806  }
1807 */
1808 
1809  // real unzip
1810  if (!$overwrite)
1811  {
1812  $unzipcmd = ilUtil::escapeShellArg($file);
1813  }
1814  else
1815  {
1816  $unzipcmd = "-o ".ilUtil::escapeShellArg($file);
1817  }
1818  ilUtil::execQuoted($unzip, $unzipcmd);
1819 
1820  chdir($cdir);
1821 
1822  // if flat, get all files and move them to original directory
1823  if ($a_flat)
1824  {
1825  include_once("./Services/Utilities/classes/class.ilFileUtils.php");
1826  $filearray = array();
1827  ilFileUtils::recursive_dirscan($tmpdir, $filearray);
1828  if (is_array($filearray["file"]))
1829  {
1830  foreach ($filearray["file"] as $k => $f)
1831  {
1832  if (substr($f, 0, 1) != "." && $f != basename($orig_file))
1833  {
1834  copy($filearray["path"][$k].$f, $origpathinfo["dirname"].DIRECTORY_SEPARATOR.$f);
1835  }
1836  }
1837  }
1838  ilUtil::delDir($tmpdir);
1839  }
1840  }
1841 
1848  public static function zip($a_dir, $a_file, $compress_content = false)
1849  {
1850  $cdir = getcwd();
1851 
1852  if($compress_content)
1853  {
1854  $a_dir .="/*";
1855  $pathinfo = pathinfo($a_dir);
1856  chdir($pathinfo["dirname"]);
1857  }
1858 
1859  $pathinfo = pathinfo($a_file);
1860  $dir = $pathinfo["dirname"];
1861  $file = $pathinfo["basename"];
1862 
1863  if(!$compress_content)
1864  {
1865  chdir($dir);
1866  }
1867 
1868  $zip = PATH_TO_ZIP;
1869 
1870  if(!$zip)
1871  {
1872  chdir($cdir);
1873  return false;
1874  }
1875 
1876  if (is_array($a_dir))
1877  {
1878  $source = "";
1879  foreach($a_dir as $dir)
1880  {
1881  $name = basename($dir);
1882  $source.= " ".ilUtil::escapeShellArg($name);
1883  }
1884  }
1885  else
1886  {
1887  $name = basename($a_dir);
1888  if (trim($name) != "*")
1889  {
1890  $source = ilUtil::escapeShellArg($name);
1891  }
1892  else
1893  {
1894  $source = $name;
1895  }
1896  }
1897 
1898  $zipcmd = "-r ".ilUtil::escapeShellArg($a_file)." ".$source;
1899  ilUtil::execQuoted($zip, $zipcmd);
1900  chdir($cdir);
1901  return true;
1902  }
1903 
1904  public static function CreateIsoFromFolder($a_dir, $a_file)
1905  {
1906  $cdir = getcwd();
1907 
1908  $pathinfo = pathinfo($a_dir);
1909  chdir($pathinfo["dirname"]);
1910 
1911  $pathinfo = pathinfo($a_file);
1912  $dir = $pathinfo["dirname"];
1913  $file = $pathinfo["basename"]; $zipcmd = "-r ".ilUtil::escapeShellArg($a_file)." ".$source;
1914 
1915  $mkisofs = PATH_TO_MKISOFS;
1916  if(!$mkisofs)
1917  {
1918  chdir($cdir);
1919  return false;
1920  }
1921 
1922  $name = basename($a_dir);
1923  $source = ilUtil::escapeShellArg($name);
1924 
1925  $zipcmd = "-r -J -o ".$a_file." ".$source;
1926  ilUtil::execQuoted($mkisofs, $zipcmd);
1927  chdir($cdir);
1928  return true;
1929  }
1930 
1939  public static function getConvertCmd()
1940  {
1941  return PATH_TO_CONVERT;
1942  }
1943 
1951  public static function execConvert($args)
1952  {
1953  ilUtil::execQuoted(PATH_TO_CONVERT, $args);
1954  }
1955 
1962  public static function isConvertVersionAtLeast($a_version)
1963  {
1964  $current_version = ilUtil::execQuoted(PATH_TO_CONVERT, "--version");
1965  $current_version = self::processConvertVersion($current_version[0]);
1966  $version = self::processConvertVersion($a_version);
1967  if($current_version >= $version)
1968  {
1969  return true;
1970  }
1971  return false;
1972  }
1973 
1980  protected static function processConvertVersion($a_version)
1981  {
1982  if(preg_match("/([0-9]+)\.([0-9]+)\.([0-9]+)([\.|\-]([0-9]+))?/", $a_version, $match))
1983  {
1984  $version = str_pad($match[1], 2, 0, STR_PAD_LEFT).
1985  str_pad($match[2], 2, 0, STR_PAD_LEFT).
1986  str_pad($match[3], 2, 0, STR_PAD_LEFT).
1987  str_pad($match[5], 2, 0, STR_PAD_LEFT);
1988  return (int)$version;
1989  }
1990  }
1991 
2001  public static function convertImage($a_from, $a_to, $a_target_format = "", $a_geometry = "",
2002  $a_background_color = "")
2003  {
2004  $format_str = ($a_target_format != "")
2005  ? strtoupper($a_target_format).":"
2006  : "";
2007  $geometry = "";
2008  if ($a_geometry != "")
2009  {
2010  if (is_int(strpos($a_geometry, "x")))
2011  {
2012  $geometry = " -geometry ".$a_geometry." ";
2013  }
2014  else
2015  {
2016  $geometry = " -geometry ".$a_geometry."x".$a_geometry." ";
2017  }
2018  }
2019 
2020  $bg_color = ($a_background_color != "")
2021  ? " -background color ".$a_background_color." "
2022  : "";
2023  $convert_cmd = ilUtil::escapeShellArg($a_from)." ".$bg_color.$geometry.ilUtil::escapeShellArg($format_str.$a_to);
2024 
2025  ilUtil::execConvert($convert_cmd);
2026  }
2027 
2038  public static function resizeImage($a_from, $a_to, $a_width, $a_height, $a_constrain_prop = false)
2039  {
2040  if ($a_constrain_prop)
2041  {
2042  $size = " -geometry ".$a_width."x".$a_height." ";
2043  }
2044  else
2045  {
2046  $size = " -resize ".$a_width."x".$a_height."! ";
2047  }
2048  $convert_cmd = ilUtil::escapeShellArg($a_from)." ".$size.ilUtil::escapeShellArg($a_to);
2049 
2050  ilUtil::execConvert($convert_cmd);
2051  }
2052 
2059  public static function img($a_src, $a_alt = "", $a_width = "", $a_height = "", $a_border = 0, $a_id = "", $a_class = "")
2060  {
2061  $img = '<img src="'.$a_src.'"';
2062  if ($a_alt != "")
2063  {
2064  $img.= ' alt="'.$a_alt.'" title="'.$a_alt.'"';
2065  }
2066  if ($a_width != "")
2067  {
2068  $img.= ' width="'.$a_width.'"';
2069  }
2070  if ($a_height != "")
2071  {
2072  $img.= ' height="'.$a_height.'"';
2073  }
2074  if ($a_class != "")
2075  {
2076  $img.= ' class="'.$a_class.'"';
2077  }
2078  if ($a_id != "")
2079  {
2080  $img.= ' id="'.$a_id.'"';
2081  }
2082  $img.= ' border="'.(int) $a_border.'"/>';
2083 
2084  return $img;
2085  }
2086 
2094  public static function html2pdf($html, $pdf_file)
2095  {
2096  $html_file = str_replace(".pdf",".html",$pdf_file);
2097 
2098  $fp = fopen( $html_file ,"wb");
2099  fwrite($fp, $html);
2100  fclose($fp);
2101 
2102  ilUtil::htmlfile2pdf($html_file,$pdf_file);
2103  }
2104 
2111  public static function htmlfile2pdf($html_file, $pdf_file)
2112  {
2113  $htmldoc_path = PATH_TO_HTMLDOC;
2114 
2115  $htmldoc = "--no-toc ";
2116  $htmldoc .= "--no-jpeg ";
2117  $htmldoc .= "--webpage ";
2118  $htmldoc .= "--outfile " . ilUtil::escapeShellArg($pdf_file) . " ";
2119  $htmldoc .= "--bodyfont Arial ";
2120  $htmldoc .= "--charset iso-8859-15 ";
2121  $htmldoc .= "--color ";
2122  $htmldoc .= "--size A4 "; // --landscape
2123  $htmldoc .= "--format pdf ";
2124  $htmldoc .= "--footer ... ";
2125  $htmldoc .= "--header ... ";
2126  $htmldoc .= "--left 60 ";
2127  // $htmldoc .= "--right 200 ";
2128  $htmldoc .= $html_file;
2129  ilUtil::execQuoted($htmldoc_path, $htmldoc);
2130 
2131  }
2132 
2139  public static function deliverData($a_data, $a_filename, $mime = "application/octet-stream", $charset = "")
2140  {
2141  $disposition = "attachment"; // "inline" to view file in browser or "attachment" to download to hard disk
2142  // $mime = "application/octet-stream"; // or whatever the mime type is
2143 
2144  include_once './Services/Http/classes/class.ilHTTPS.php';
2145 
2146  //if($_SERVER['HTTPS'])
2147  if( ilHTTPS::getInstance()->isDetected() )
2148  {
2149 
2150  // Added different handling for IE and HTTPS => send pragma after content informations
2154  #header("Pragma: ");
2155  #header("Cache-Control: ");
2156  #header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
2157  #header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
2158  #header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
2159  #header("Cache-Control: post-check=0, pre-check=0", false);
2160  }
2161  else if ($disposition == "attachment")
2162  {
2163  header("Cache-control: private");
2164  }
2165  else
2166  {
2167  header("Cache-Control: no-cache, must-revalidate");
2168  header("Pragma: no-cache");
2169  }
2170 
2171  $ascii_filename = ilUtil::getASCIIFilename($a_filename);
2172 
2173  if (strlen($charset))
2174  {
2175  $charset = "; charset=$charset";
2176  }
2177  header("Content-Type: $mime$charset");
2178  header("Content-Disposition:$disposition; filename=\"".$ascii_filename."\"");
2179  header("Content-Description: ".$ascii_filename);
2180  header("Content-Length: ".(string)(strlen($a_data)));
2181 
2182  //if($_SERVER['HTTPS'])
2183  if( ilHTTPS::getInstance()->isDetected() )
2184  {
2185  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
2186  header('Pragma: public');
2187  }
2188 
2189  header("Connection: close");
2190  echo $a_data;
2191  exit;
2192  }
2193 
2194  // BEGIN WebDAV: Show file in browser or provide it as attachment
2202  public static function deliverFile($a_file, $a_filename,$a_mime = '', $isInline = false, $removeAfterDelivery = false,
2203  $a_exit_after = true)
2204  {
2205  // should we fail silently?
2206  if(!file_exists($a_file))
2207  {
2208  return false;
2209  }
2210 
2211  if ($isInline) {
2212  $disposition = "inline"; // "inline" to view file in browser
2213  } else {
2214  $disposition = "attachment"; // "attachment" to download to hard disk
2215  //$a_mime = "application/octet-stream"; // override mime type to ensure that no browser tries to show the file anyway.
2216  }
2217  // END WebDAV: Show file in browser or provide it as attachment
2218 
2219  if(strlen($a_mime))
2220  {
2221  $mime = $a_mime;
2222  }
2223  else
2224  {
2225  $mime = "application/octet-stream"; // or whatever the mime type is
2226  }
2227  // BEGIN WebDAV: Removed broken HTTPS code.
2228  // END WebDAV: Removed broken HTTPS code.
2229  if ($disposition == "attachment")
2230  {
2231  header("Cache-control: private");
2232  }
2233  else
2234  {
2235  header("Cache-Control: no-cache, must-revalidate");
2236  header("Pragma: no-cache");
2237  }
2238 
2239  $ascii_filename = ilUtil::getASCIIFilename($a_filename);
2240 
2241  header("Content-Type: $mime");
2242  header("Content-Disposition:$disposition; filename=\"".$ascii_filename."\"");
2243  header("Content-Description: ".$ascii_filename);
2244 
2245  // #7271: if notice gets thrown download will fail in IE
2246  $filesize = @filesize($a_file);
2247  if ($filesize)
2248  {
2249  header("Content-Length: ".(string)$filesize);
2250  }
2251 
2252  include_once './Services/Http/classes/class.ilHTTPS.php';
2253  #if($_SERVER['HTTPS'])
2254  if(ilHTTPS::getInstance()->isDetected())
2255  {
2256  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
2257  header('Pragma: public');
2258  }
2259 
2260  header("Connection: close");
2261  ilUtil::readFile( $a_file );
2262  if ($removeAfterDelivery)
2263  {
2264  unlink ($a_file);
2265  }
2266  if ($a_exit_after)
2267  {
2268  exit;
2269  }
2270  }
2271 
2272 
2282  public static function readFile($a_file)
2283  {
2284  $chunksize = 1*(1024*1024); // how many bytes per chunk
2285  $buffer = '';
2286  $handle = fopen($a_file, 'rb');
2287  if ($handle === false)
2288  {
2289  return false;
2290  }
2291  while (!feof($handle))
2292  {
2293  $buffer = fread($handle, $chunksize);
2294  print $buffer;
2295  }
2296  return fclose($handle);
2297  }
2298 
2306  public static function getASCIIFilename($a_filename)
2307  {
2308  // The filename must be converted to ASCII, as of RFC 2183,
2309  // section 2.3.
2310 
2322 
2325 
2326  $ascii_filename = htmlentities($a_filename, ENT_NOQUOTES, 'UTF-8');
2327  $ascii_filename = preg_replace('/\&(.)[^;]*;/', '\\1', $ascii_filename);
2328  $ascii_filename = preg_replace('/[\x7f-\xff]/', '_', $ascii_filename);
2329 
2330  // OS do not allow the following characters in filenames: \/:*?"<>|
2331  $ascii_filename = preg_replace('/[:\x5c\/\*\?\"<>\|]/', '_', $ascii_filename);
2332  return $ascii_filename;
2333  }
2334 
2341  public static function htmlentitiesOutsideHTMLTags($htmlText)
2342  {
2343  $matches = Array();
2344  $sep = '###HTMLTAG###';
2345 
2346  preg_match_all("@<[^>]*>@", $htmlText, $matches);
2347  $tmp = preg_replace("@(<[^>]*>)@", $sep, $htmlText);
2348  $tmp = explode($sep, $tmp);
2349 
2350  for ($i=0; $i<count($tmp); $i++)
2351  $tmp[$i] = htmlentities($tmp[$i], ENT_COMPAT, "UTF-8");
2352 
2353  $tmp = join($sep, $tmp);
2354 
2355  for ($i=0; $i<count($matches[0]); $i++)
2356  $tmp = preg_replace("@$sep@", $matches[0][$i], $tmp, 1);
2357 
2358  return $tmp;
2359  }
2360 
2367  public static function getJavaPath()
2368  {
2369  return PATH_TO_JAVA;
2370  //global $ilias;
2371 
2372  //return $ilias->getSetting("java_path");
2373  }
2374 
2382  public static function appendUrlParameterString($a_url, $a_par, $xml_style = false)
2383  {
2384  $amp = $xml_style
2385  ? "&amp;"
2386  : "&";
2387 
2388  $url = (is_int(strpos($a_url, "?")))
2389  ? $a_url.$amp.$a_par
2390  : $a_url."?".$a_par;
2391 
2392  return $url;
2393  }
2394 
2410  public static function makeDir($a_dir)
2411  {
2412  $a_dir = trim($a_dir);
2413 
2414  // remove trailing slash (bugfix for php 4.2.x)
2415  if (substr($a_dir,-1) == "/")
2416  {
2417  $a_dir = substr($a_dir,0,-1);
2418  }
2419 
2420  // check if a_dir comes with a path
2421  if (!($path = substr($a_dir,0, strrpos($a_dir,"/") - strlen($a_dir))))
2422  {
2423  $path = ".";
2424  }
2425 
2426  // create directory with file permissions of parent directory
2427  umask(0000);
2428  return @mkdir($a_dir,fileperms($path));
2429  }
2430 
2431 
2446  public static function makeDirParents($a_dir)
2447  {
2448  $dirs = array($a_dir);
2449  $a_dir = dirname($a_dir);
2450  $last_dirname = '';
2451 
2452  while($last_dirname != $a_dir)
2453  {
2454  array_unshift($dirs, $a_dir);
2455  $last_dirname = $a_dir;
2456  $a_dir = dirname($a_dir);
2457  }
2458 
2459  // find the first existing dir
2460  $reverse_paths = array_reverse($dirs, TRUE);
2461  $found_index = -1;
2462  foreach ($reverse_paths as $key => $value)
2463  {
2464  if ($found_index == -1)
2465  {
2466  if (is_dir($value))
2467  {
2468  $found_index = $key;
2469  }
2470  }
2471  }
2472 
2473  umask(0000);
2474  foreach ($dirs as $dirindex => $dir)
2475  {
2476  // starting with the longest existing path
2477  if ($dirindex >= $found_index)
2478  {
2479  if (! file_exists($dir))
2480  {
2481  if (strcmp(substr($dir,strlen($dir)-1,1),"/") == 0)
2482  {
2483  // on some systems there is an error when there is a slash
2484  // at the end of a directory in mkdir, see Mantis #2554
2485  $dir = substr($dir,0,strlen($dir)-1);
2486  }
2487  if (! mkdir($dir, $umask))
2488  {
2489  error_log("Can't make directory: $dir");
2490  return false;
2491  }
2492  }
2493  elseif (! is_dir($dir))
2494  {
2495  error_log("$dir is not a directory");
2496  return false;
2497  }
2498  else
2499  {
2500  // get umask of the last existing parent directory
2501  $umask = fileperms($dir);
2502  }
2503  }
2504  }
2505  return true;
2506  }
2507 
2517  public static function delDir($a_dir, $a_clean_only = false)
2518  {
2519  if (!is_dir($a_dir) || is_int(strpos($a_dir, "..")))
2520  {
2521  return;
2522  }
2523 
2524  $current_dir = opendir($a_dir);
2525 
2526  $files = array();
2527 
2528  // this extra loop has been necessary because of a strange bug
2529  // at least on MacOS X. A looped readdir() didn't work
2530  // correctly with larger directories
2531  // when an unlink happened inside the loop. Getting all files
2532  // into the memory first solved the problem.
2533  while($entryname = readdir($current_dir))
2534  {
2535  $files[] = $entryname;
2536  }
2537 
2538  foreach($files as $file)
2539  {
2540  if(is_dir($a_dir."/".$file) and ($file != "." and $file!=".."))
2541  {
2542  ilUtil::delDir(${a_dir}."/".${file});
2543  }
2544  elseif ($file != "." and $file != "..")
2545  {
2546  unlink(${a_dir}."/".${file});
2547  }
2548  }
2549 
2550  closedir($current_dir);
2551  if (!$a_clean_only)
2552  {
2553  @rmdir(${a_dir});
2554  }
2555  }
2556 
2557 
2564  public static function getDir($a_dir, $a_rec = false, $a_sub_dir = "")
2565  {
2566  $current_dir = opendir($a_dir.$a_sub_dir);
2567 
2568  $dirs = array();
2569  $files = array();
2570  $subitems = array();
2571  while($entry = readdir($current_dir))
2572  {
2573  if(is_dir($a_dir."/".$entry))
2574  {
2575  $dirs[$entry] = array("type" => "dir", "entry" => $entry,
2576  "subdir" => $a_sub_dir);
2577  if ($a_rec && $entry != "." && $entry != "..")
2578  {
2579  $si = ilUtil::getDir($a_dir, true, $a_sub_dir."/".$entry);
2580  $subitems = array_merge($subitems, $si);
2581  }
2582  }
2583  else
2584  {
2585  if ($entry != "." && $entry != "..")
2586  {
2587  $size = filesize($a_dir.$a_sub_dir."/".$entry);
2588  $files[$entry] = array("type" => "file", "entry" => $entry,
2589  "size" => $size, "subdir" => $a_sub_dir);
2590  }
2591  }
2592  }
2593  ksort($dirs);
2594  ksort($files);
2595 
2596  return array_merge($dirs, $files, $subitems);
2597  }
2598 
2605  public static function stripSlashesArray($a_arr, $a_strip_html = true, $a_allow = "")
2606  {
2607  if (is_array($a_arr))
2608  {
2609  foreach ($a_arr as $k => $v)
2610  {
2611  $a_arr[$k] = ilUtil::stripSlashes($v, $a_strip_html, $a_allow);
2612  }
2613  }
2614 
2615  return $a_arr;
2616  }
2617 
2624  public static function stripSlashesRecursive($a_data, $a_strip_html = true, $a_allow = "")
2625  {
2626  if (is_array($a_data))
2627  {
2628  foreach ($a_data as $k => $v)
2629  {
2630  if (is_array($v))
2631  {
2632  $a_data[$k] = ilUtil::stripSlashesRecursive($v, $a_strip_html, $a_allow);
2633  }
2634  else
2635  {
2636  $a_data[$k] = ilUtil::stripSlashes($v, $a_strip_html, $a_allow);
2637  }
2638  }
2639  }
2640  else
2641  {
2642  $a_data = ilUtil::stripSlashes($a_data, $a_strip_html, $a_allow);
2643  }
2644 
2645  return $a_data;
2646  }
2647 
2655  public static function stripSlashes($a_str, $a_strip_html = true, $a_allow = "")
2656  {
2657  if (ini_get("magic_quotes_gpc"))
2658  {
2659  $a_str = stripslashes($a_str);
2660  }
2661 //echo "<br><br>-".$a_strip_html."-".htmlentities($a_str);
2662 //echo "<br>-".htmlentities(ilUtil::secureString($a_str, $a_strip_html, $a_allow));
2663  return ilUtil::secureString($a_str, $a_strip_html, $a_allow);
2664  }
2665 
2673  public static function stripOnlySlashes($a_str)
2674  {
2675  if (ini_get("magic_quotes_gpc"))
2676  {
2677  $a_str = stripslashes($a_str);
2678  }
2679 
2680  return $a_str;
2681  }
2682 
2689  public static function secureString($a_str, $a_strip_html = true, $a_allow = "")
2690  {
2691  // check whether all allowed tags can be made secure
2692  $only_secure = true;
2693  $allow_tags = explode(">", $a_allow);
2694  $sec_tags = ilUtil::getSecureTags();
2695  $allow_array = array();
2696  foreach($allow_tags as $allow)
2697  {
2698  if ($allow != "")
2699  {
2700  $allow = str_replace("<", "", $allow);
2701 
2702  if (!in_array($allow, $sec_tags))
2703  {
2704  $only_secure = false;
2705  }
2706  $allow_array[] = $allow;
2707  }
2708  }
2709 
2710  // default behaviour: allow only secure tags 1:1
2711  if (($only_secure || $a_allow == "") && $a_strip_html)
2712  {
2713  if ($a_allow == "")
2714  {
2715  $allow_array = array ("b", "i", "strong", "em", "code", "cite",
2716  "gap", "sub", "sup", "pre", "strike");
2717  }
2718 
2719  // this currently removes parts of strings like "a <= b"
2720  // because "a <= b" is treated like "<spam onclick='hurt()'>ss</spam>"
2721  $a_str = ilUtil::maskSecureTags($a_str, $allow_array);
2722  $a_str = strip_tags($a_str); // strip all other tags
2723  $a_str = ilUtil::unmaskSecureTags($a_str, $allow_array);
2724 
2725  // a possible solution could be something like:
2726  // $a_str = str_replace("<", "&lt;", $a_str);
2727  // $a_str = str_replace(">", "&gt;", $a_str);
2728  // $a_str = ilUtil::unmaskSecureTags($a_str, $allow_array);
2729  //
2730  // output would be ok then, but input fields would show
2731  // "a &lt;= b" for input "a <= b" if data is brought back to a form
2732  }
2733  else
2734  {
2735  // only for scripts, that need to allow more/other tags and parameters
2736  if ($a_strip_html)
2737  {
2738  $a_str = ilUtil::stripScriptHTML($a_str, $a_allow);
2739  }
2740  }
2741 
2742  return $a_str;
2743  }
2744 
2745  public static function getSecureTags()
2746  {
2747  return array("strong", "em", "u", "strike", "ol", "li", "ul", "p", "div",
2748  "i", "b", "code", "sup", "sub", "pre", "gap", "a", "img");
2749  }
2750 
2751  public static function maskSecureTags($a_str, $allow_array)
2752  {
2753  foreach ($allow_array as $t)
2754  {
2755  switch($t)
2756  {
2757  case "a":
2758  $a_str = ilUtil::maskAttributeTag($a_str, "a", "href");
2759  break;
2760 
2761  case "img":
2762  $a_str = ilUtil::maskAttributeTag($a_str, "img", "src");
2763  break;
2764 
2765  case "p":
2766  case "div":
2767  $a_str = ilUtil::maskTag($a_str, $t, array(
2768  array("param" => "align", "value" => "left"),
2769  array("param" => "align", "value" => "center"),
2770  array("param" => "align", "value" => "justify"),
2771  array("param" => "align", "value" => "right")
2772  ));
2773  break;
2774 
2775  default:
2776  $a_str = ilUtil::maskTag($a_str, $t);
2777  break;
2778  }
2779  }
2780 
2781  return $a_str;
2782  }
2783 
2784  public static function unmaskSecureTags($a_str, $allow_array)
2785  {
2786  foreach ($allow_array as $t)
2787  {
2788  switch($t)
2789  {
2790  case "a":
2791  $a_str = ilUtil::unmaskAttributeTag($a_str, "a", "href");
2792  break;
2793 
2794  case "img":
2795  $a_str = ilUtil::unmaskAttributeTag($a_str, "img", "src");
2796  break;
2797 
2798  case "p":
2799  case "div":
2800  $a_str = ilUtil::unmaskTag($a_str, $t, array(
2801  array("param" => "align", "value" => "left"),
2802  array("param" => "align", "value" => "center"),
2803  array("param" => "align", "value" => "justify"),
2804  array("param" => "align", "value" => "right")
2805  ));
2806  break;
2807 
2808  default:
2809  $a_str = ilUtil::unmaskTag($a_str, $t);
2810  break;
2811  }
2812  }
2813 
2814  return $a_str;
2815  }
2816 
2824  public static function securePlainString($a_str)
2825  {
2826  if (ini_get("magic_quotes_gpc"))
2827  {
2828  return stripslashes($a_str);
2829  }
2830  else
2831  {
2832  return $a_str;
2833  }
2834  }
2851  public static function htmlencodePlainString($a_str, $a_make_links_clickable, $a_detect_goto_links = false)
2852  {
2853  $encoded = "";
2854 
2855  if ($a_make_links_clickable)
2856  {
2857  // Find text sequences in the plain text string which match
2858  // the URI syntax rules, and pass them to ilUtil::makeClickable.
2859  // Encode all other text sequences in the plain text string using
2860  // htmlspecialchars and nl2br.
2861  // The following expressions matches URI's as specified in RFC 2396.
2862  //
2863  // The expression matches URI's, which start with some well known
2864  // schemes, like "http:", or with "www.". This must be followed
2865  // by at least one of the following RFC 2396 expressions:
2866  // - alphanum: [a-zA-Z0-9]
2867  // - reserved: [;\/?:|&=+$,]
2868  // - mark: [\\-_.!~*\'()]
2869  // - escaped: %[0-9a-fA-F]{2}
2870  // - fragment delimiter: #
2871  // - uric_no_slash: [;?:@&=+$,]
2872  $matches = array();
2873  $numberOfMatches = preg_match_all('/(?:(?:http|https|ftp|ftps|mailto):|www\.)(?:[a-zA-Z0-9]|[;\/?:|&=+$,]|[\\-_.!~*\'()]|%[0-9a-fA-F]{2}|#|[;?:@&=+$,])+/',$a_str, $matches, PREG_OFFSET_CAPTURE);
2874  $pos1 = 0;
2875  $encoded = "";
2876  foreach ($matches as $match)
2877  {
2878  }
2879  foreach ($matches[0] as $match)
2880  {
2881  $matched_text = $match[0];
2882  $pos2 = $match[1];
2883  if ($matched_offset != previous_offset)
2884  {
2885  // encode plain text
2886  $encoded .= nl2br(htmlspecialchars(substr($a_str, $pos1, $pos2 - $pos1)));
2887  }
2888  // encode URI
2889  $encoded .= ilUtil::makeClickable($matched_text, $a_detect_goto_links);
2890 
2891 
2892  $pos1 = $pos2 + strlen($matched_text);
2893  }
2894  if ($pos1 < strlen($a_str))
2895  {
2896  $encoded .= nl2br(htmlspecialchars(substr($a_str, $pos1)));
2897  }
2898  }
2899  else
2900  {
2901  $encoded = nl2br(htmlspecialchars($a_str));
2902  }
2903  return $encoded;
2904  }
2905 
2906 
2907  public static function maskAttributeTag($a_str, $tag, $tag_att)
2908  {
2909  global $ilLog;
2910 
2911  $ws = "[ \t\r\f\v\n]*";
2912  $att = $ws."[^>]*".$ws;
2913 
2914  while (eregi("<($tag$att($tag_att$ws=$ws\"(([\$@!*()~;,_0-9A-z/:=%\\.&#?+\\-])*)\")$att)>",
2915  $a_str, $found))
2916  {
2917  $un = array(".", "-", "+", "?", '$', "*", "(", ")");
2918  $esc = array();
2919  foreach($un as $v)
2920  {
2921  $esc[] = "\\".$v;
2922  }
2923  $ff = str_replace($un, $esc, $found[1]);
2924 
2925  $old_str = $a_str;
2926  $a_str = eregi_replace("<".$ff.">",
2927  "&lt;$tag $tag_att$tag_att=\"".$found[3]."\"&gt;", $a_str);
2928  if ($old_str == $a_str)
2929  {
2930  $ilLog->write("ilUtil::maskA-".htmlentities($old_str)." == ".
2931  htmlentities($a_str));
2932  return $a_str;
2933  }
2934  }
2935  $a_str = str_ireplace("</$tag>",
2936  "&lt;/$tag&gt;", $a_str);
2937  return $a_str;
2938  }
2939 
2940  public static function unmaskAttributeTag($a_str, $tag, $tag_att)
2941  {
2942  global $ilLog;
2943 
2944  while (eregi("&lt;($tag $tag_att$tag_att=\"(([\$@!*()~;,_0-9A-z/:=%\\.&#?+\\-])*)\")&gt;",
2945  $a_str, $found))
2946  {
2947  $un = array(".", "-", "+", "?", '$', "*", "(", ")");
2948  $esc = array();
2949  foreach($un as $v)
2950  {
2951  $esc[] = "\\".$v;
2952  }
2953  $ff = str_replace($un, $esc, $found[1]);
2954 
2955  $old_str = $a_str;
2956  $a_str = eregi_replace("&lt;".$ff."&gt;",
2957  "<$tag $tag_att=\"".ilUtil::secureLink($found[2])."\">", $a_str);
2958  if ($old_str == $a_str)
2959  {
2960  $ilLog->write("ilUtil::unmaskA-".htmlentities($old_str)." == ".
2961  htmlentities($a_str));
2962  return $a_str;
2963  }
2964  }
2965  $a_str = str_replace("&lt;/$tag&gt;", "</$tag>", $a_str);
2966  return $a_str;
2967  }
2968 
2969  public static function maskTag($a_str, $t, $fix_param = "")
2970  {
2971  $a_str = str_replace(array("<$t>", "<".strtoupper($t).">"),
2972  "&lt;".$t."&gt;", $a_str);
2973  $a_str = str_replace(array("</$t>", "</".strtoupper($t).">"),
2974  "&lt;/".$t."&gt;", $a_str);
2975 
2976  if (is_array($fix_param))
2977  {
2978  foreach ($fix_param as $p)
2979  {
2980  $k = $p["param"];
2981  $v = $p["value"];
2982  $a_str = str_replace("<$t $k=\"$v\">",
2983  "&lt;"."$t $k=\"$v\""."&gt;", $a_str);
2984  }
2985  }
2986 
2987  return $a_str;
2988  }
2989 
2990  public static function unmaskTag($a_str, $t, $fix_param = "")
2991  {
2992  $a_str = str_replace("&lt;".$t."&gt;", "<".$t.">", $a_str);
2993  $a_str = str_replace("&lt;/".$t."&gt;", "</".$t.">", $a_str);
2994 
2995  if (is_array($fix_param))
2996  {
2997  foreach ($fix_param as $p)
2998  {
2999  $k = $p["param"];
3000  $v = $p["value"];
3001  $a_str = str_replace("&lt;$t $k=\"$v\"&gt;",
3002  "<"."$t $k=\"$v\"".">", $a_str);
3003  }
3004  }
3005  return $a_str;
3006  }
3007 
3008  public static function secureLink($a_str)
3009  {
3010  $a_str = str_ireplace("javascript", "jvscrpt", $a_str);
3011  $a_str = str_ireplace(array("%00", "%0a", "%0d", "%1a", "&#00;", "&#x00;",
3012  "&#0;", "&#x0;", "&#x0a;", "&#x0d;", "&#10;", "&#13;"), "-", $a_str);
3013  return $a_str;
3014  }
3015 
3029  public static function stripScriptHTML($a_str, $a_allow = "", $a_rm_js = true)
3030  {
3031  //$a_str = strip_tags($a_str, $a_allow);
3032 
3033  $negativestr = "a,abbr,acronym,address,applet,area,b,base,basefont,".
3034  "bdo,big,blockquote,body,br,button,caption,center,cite,code,col,".
3035  "colgroup,dd,del,dfn,dir,div,dl,dt,em,fieldset,font,form,frame,".
3036  "frameset,h1,h2,h3,h4,h5,h6,head,hr,html,i,iframe,img,input,ins,isindex,kbd,".
3037  "label,legend,li,link,map,menu,meta,noframes,noscript,object,ol,".
3038  "optgroup,option,p,param,q,s,samp,script,select,small,span,".
3039  "strike,strong,style,sub,sup,table,tbody,td,textarea,tfoot,th,thead,".
3040  "title,tr,tt,u,ul,var";
3041  $a_allow = strtolower ($a_allow);
3042  $negatives = explode(",",$negativestr);
3043  $outer_old_str = "";
3044  while($outer_old_str != $a_str)
3045  {
3046  $outer_old_str = $a_str;
3047  foreach ($negatives as $item)
3048  {
3049  $pos = strpos($a_allow, "<$item>");
3050 
3051  // remove complete tag, if not allowed
3052  if ($pos === false)
3053  {
3054  $old_str = "";
3055  while($old_str != $a_str)
3056  {
3057  $old_str = $a_str;
3058  $a_str = preg_replace("/<\/?\s*$item(\/?)\s*>/i", "", $a_str);
3059  $a_str = preg_replace("/<\/?\s*$item(\/?)\s+([^>]*)>/i", "", $a_str);
3060  }
3061  }
3062  }
3063  }
3064 
3065  if ($a_rm_js)
3066  {
3067  // remove all attributes if an "on..." attribute is given
3068  $a_str = preg_replace("/<\s*\w*(\/?)(\s+[^>]*)?(\s+on[^>]*)>/i", "", $a_str);
3069 
3070  // remove all attributes if a "javascript" is within tag
3071  $a_str = preg_replace("/<\s*\w*(\/?)\s+[^>]*javascript[^>]*>/i", "", $a_str);
3072 
3073  // remove all attributes if an "expression" is within tag
3074  // (IE allows something like <b style='width:expression(alert(1))'>test</b>)
3075  $a_str = preg_replace("/<\s*\w*(\/?)\s+[^>]*expression[^>]*>/i", "", $a_str);
3076  }
3077 
3078  return $a_str;
3079  }
3080 
3092  public static function prepareFormOutput($a_str, $a_strip = false)
3093  {
3094  if($a_strip)
3095  {
3096  $a_str = ilUtil::stripSlashes($a_str);
3097  }
3098  $a_str = htmlspecialchars($a_str);
3099  // Added replacement of curly brackets to prevent
3100  // problems with PEAR templates, because {xyz} will
3101  // be removed as unused template variable
3102  $a_str = str_replace("{", "&#123;", $a_str);
3103  $a_str = str_replace("}", "&#125;", $a_str);
3104  // needed for LaTeX conversion \\ in LaTeX is a line break
3105  // but without this replacement, php changes \\ to \
3106  $a_str = str_replace("\\", "&#92;", $a_str);
3107  return $a_str;
3108  }
3109 
3110 
3120  public static function prepareDBString($a_str)
3121  {
3122  return addslashes($a_str);
3123  }
3124 
3125 
3134  public static function removeItemFromDesktops($a_id)
3135  {
3136  return ilObjUser::_removeItemFromDesktops($a_id);
3137  }
3138 
3139 
3149  public static function extractParameterString($a_parstr)
3150  {
3151  // parse parameters in array
3152  $par = array();
3153  $ok=true;
3154  while(($spos=strpos($a_parstr,"=")) && $ok)
3155  {
3156  // extract parameter
3157  $cpar = substr($a_parstr,0,$spos);
3158  $a_parstr = substr($a_parstr,$spos,strlen($a_parstr)-$spos);
3159  while(substr($cpar,0,1)=="," ||substr($cpar,0,1)==" " || substr($cpar,0,1)==chr(13) || substr($cpar,0,1)==chr(10))
3160  $cpar = substr($cpar,1,strlen($cpar)-1);
3161  while(substr($cpar,strlen($cpar)-1,1)==" " || substr($cpar,strlen($cpar)-1,1)==chr(13) || substr($cpar,strlen($cpar)-1,1)==chr(10))
3162  $cpar = substr($cpar,0,strlen($cpar)-1);
3163 
3164  // parameter name should only
3165  $cpar_old = "";
3166  while($cpar != $cpar_old)
3167  {
3168  $cpar_old = $cpar;
3169  $cpar = eregi_replace("[^a-zA-Z0-9_]", "", $cpar);
3170  }
3171 
3172  // extract value
3173  if ($cpar != "")
3174  {
3175  if($spos=strpos($a_parstr,"\""))
3176  {
3177  $a_parstr = substr($a_parstr,$spos+1,strlen($a_parstr)-$spos);
3178  $spos=strpos($a_parstr,"\"");
3179  if(is_int($spos))
3180  {
3181  $cval = substr($a_parstr,0,$spos);
3182  $par[$cpar]=$cval;
3183  $a_parstr = substr($a_parstr,$spos+1,strlen($a_parstr)-$spos-1);
3184  }
3185  else
3186  $ok=false;
3187  }
3188  else
3189  $ok=false;
3190  }
3191  }
3192 
3193  if($ok) return $par; else return false;
3194  }
3195 
3196  public static function assembleParameterString($a_par_arr)
3197  {
3198  if (is_array($a_par_arr))
3199  {
3200  $target_arr = array();
3201  foreach ($a_par_arr as $par => $val)
3202  {
3203  $target_arr[] = "$par=\"$val\"";
3204  }
3205  $target_str = implode(", ", $target_arr);
3206  }
3207 
3208  return $target_str;
3209  }
3210 
3217  public static function dumpString($a_str)
3218  {
3219  $ret = $a_str.": ";
3220  for($i=0; $i<strlen($a_str); $i++)
3221  {
3222  $ret.= ord(substr($a_str,$i,1))." ";
3223  }
3224  return $ret;
3225  }
3226 
3227 
3234  public static function yn2tf($a_yn)
3235  {
3236  if(strtolower($a_yn) == "y")
3237  {
3238  return true;
3239  }
3240  else
3241  {
3242  return false;
3243  }
3244  }
3245 
3252  public static function tf2yn($a_tf)
3253  {
3254  if($a_tf)
3255  {
3256  return "y";
3257  }
3258  else
3259  {
3260  return "n";
3261  }
3262  }
3263 
3274  public static function sort_func ($a, $b)
3275  {
3276  global $array_sortby,$array_sortorder;
3277 
3278  if(!isset($array_sortby))
3279  {
3280  // occured in: setup -> new client -> install languages -> sorting of languages
3281  $array_sortby = 0;
3282  }
3283 
3284  // this comparison should give optimal results if
3285  // locale is provided and mb string functions are supported
3286  if ($array_sortorder == "asc")
3287  {
3288  return ilStr::strCmp($a[$array_sortby], $b[$array_sortby]);
3289  }
3290 
3291  if ($array_sortorder == "desc")
3292  {
3293  return !ilStr::strCmp($a[$array_sortby], $b[$array_sortby]);
3294  return strcoll(ilStr::strToUpper($b[$array_sortby]), ilStr::strToUpper($a[$array_sortby]));
3295  }
3296  }
3297 
3308  public static function sort_func_numeric ($a, $b)
3309  {
3310  global $array_sortby,$array_sortorder;
3311 
3312  if ($array_sortorder == "asc")
3313  {
3314  return $a["$array_sortby"] > $b["$array_sortby"];
3315  }
3316 
3317  if ($array_sortorder == "desc")
3318  {
3319  return $a["$array_sortby"] < $b["$array_sortby"];
3320  }
3321  }
3334  public static function sortArray($array,$a_array_sortby,$a_array_sortorder = 0,$a_numeric = false,
3335  $a_keep_keys = false)
3336  {
3337  include_once("./Services/Utilities/classes/class.ilStr.php");
3338 
3339  // BEGIN WebDAV: Provide a 'stable' sort algorithm
3340  if (! $a_keep_keys) {
3341  return self::stableSortArray($array,$a_array_sortby,$a_array_sortorder,$a_numeric,$a_keep_keys);
3342  }
3343  // END WebDAV Provide a 'stable' sort algorithm
3344 
3345  global $array_sortby,$array_sortorder;
3346 
3347  $array_sortby = $a_array_sortby;
3348 
3349  if ($a_array_sortorder == "desc")
3350  {
3351  $array_sortorder = "desc";
3352  }
3353  else
3354  {
3355  $array_sortorder = "asc";
3356  }
3357  if($a_numeric)
3358  {
3359  if ($a_keep_keys)
3360  {
3361  uasort($array, array("ilUtil", "sort_func_numeric"));
3362  }
3363  else
3364  {
3365  usort($array, array("ilUtil", "sort_func_numeric"));
3366  }
3367  }
3368  else
3369  {
3370  if ($a_keep_keys)
3371  {
3372  uasort($array, array("ilUtil", "sort_func"));
3373  }
3374  else
3375  {
3376  usort($array, array("ilUtil", "sort_func"));
3377  }
3378  }
3379  //usort($array,"ilUtil::sort_func");
3380 
3381  return $array;
3382  }
3383  // BEGIN WebDAV: Provide a 'stable' sort algorithm
3398  public static function stableSortArray($array,$a_array_sortby,$a_array_sortorder = 0,$a_numeric = false)
3399  {
3400  global $array_sortby,$array_sortorder;
3401 
3402  $array_sortby = $a_array_sortby;
3403 
3404  if ($a_array_sortorder == "desc")
3405  {
3406  $array_sortorder = "desc";
3407  }
3408  else
3409  {
3410  $array_sortorder = "asc";
3411  }
3412 
3413  // Create a copy of the array values for sorting
3414  $sort_array = array_values($array);
3415 
3416  if($a_numeric)
3417  {
3418  ilUtil::mergesort($sort_array, array("ilUtil", "sort_func_numeric"));
3419  }
3420  else
3421  {
3422  ilUtil::mergesort($sort_array, array("ilUtil", "sort_func"));
3423  }
3424 
3425  return $sort_array;
3426  }
3427  public static function mergesort(&$array, $cmp_function = 'strcmp') {
3428  // Arrays of size < 2 require no action.
3429  if (count($array) < 2) return;
3430 
3431  // Split the array in half
3432  $halfway = count($array) / 2;
3433  $array1 = array_slice($array, 0, $halfway);
3434  $array2 = array_slice($array, $halfway);
3435 
3436  // Recurse to sort the two halves
3437  ilUtil::mergesort($array1, $cmp_function);
3438  ilUtil::mergesort($array2, $cmp_function);
3439 
3440  // If all of $array1 is <= all of $array2, just append them.
3441  if (call_user_func($cmp_function, end($array1), $array2[0]) < 1) {
3442  $array = array_merge($array1, $array2);
3443  return;
3444  }
3445 
3446  // Merge the two sorted arrays into a single sorted array
3447  $array = array();
3448  $ptr1 = $ptr2 = 0;
3449  while ($ptr1 < count($array1) && $ptr2 < count($array2)) {
3450  if (call_user_func($cmp_function, $array1[$ptr1], $array2[$ptr2]) < 1) {
3451  $array[] = $array1[$ptr1++];
3452  }
3453  else {
3454  $array[] = $array2[$ptr2++];
3455  }
3456  }
3457 
3458  // Merge the remainder
3459  while ($ptr1 < count($array1)) $array[] = $array1[$ptr1++];
3460  while ($ptr2 < count($array2)) $array[] = $array2[$ptr2++];
3461 
3462  return;
3463  }
3464  // END WebDAV: Provide a 'stable' sort algorithm
3465 
3477  public static function unique_multi_array($array, $sub_key)
3478  {
3479  $target = array();
3480  $existing_sub_key_values = array();
3481 
3482  foreach ($array as $key=>$sub_array)
3483  {
3484  if (!in_array($sub_array[$sub_key], $existing_sub_key_values))
3485  {
3486  $existing_sub_key_values[] = $sub_array[$sub_key];
3487  $target[$key] = $sub_array;
3488  }
3489  }
3490 
3491  return $target;
3492  }
3493 
3494 
3504  public static function getGDSupportedImageType($a_desired_type)
3505  {
3506  $a_desired_type = strtolower($a_desired_type);
3507  // get supported Image Types
3508  $im_types = ImageTypes();
3509 
3510  switch($a_desired_type)
3511  {
3512  case "jpg":
3513  case "jpeg":
3514  if ($im_types & IMG_JPG) return "jpg";
3515  if ($im_types & IMG_GIF) return "gif";
3516  if ($im_types & IMG_PNG) return "png";
3517  break;
3518 
3519  case "gif":
3520  if ($im_types & IMG_GIF) return "gif";
3521  if ($im_types & IMG_JPG) return "jpg";
3522  if ($im_types & IMG_PNG) return "png";
3523  break;
3524 
3525  case "png":
3526  if ($im_types & IMG_PNG) return "png";
3527  if ($im_types & IMG_JPG) return "jpg";
3528  if ($im_types & IMG_GIF) return "gif";
3529  break;
3530  }
3531 
3532  return "";
3533  }
3534 
3544  public static function deducibleSize($a_mime)
3545  {
3546  if (($a_mime == "image/gif") || ($a_mime == "image/jpeg") ||
3547  ($a_mime == "image/png") || ($a_mime == "application/x-shockwave-flash") ||
3548  ($a_mime == "image/tiff") || ($a_mime == "image/x-ms-bmp") ||
3549  ($a_mime == "image/psd") || ($a_mime == "image/iff"))
3550  {
3551  return true;
3552  }
3553  else
3554  {
3555  return false;
3556  }
3557  }
3558 
3559 
3567  public static function redirect($a_script)
3568  {
3569  global $log, $PHP_SELF;
3570 
3571 //echo "<br>".$a_script;
3572  if (!is_int(strpos($a_script, "://")))
3573  {
3574  if (substr($a_script, 0, 1) != "/" && defined("ILIAS_HTTP_PATH"))
3575  {
3576  if (is_int(strpos($_SERVER["PHP_SELF"], "/setup/")))
3577  {
3578  $a_script = "setup/".$a_script;
3579  }
3580  $a_script = ILIAS_HTTP_PATH."/".$a_script;
3581  }
3582  }
3583 //echo "<br>".$a_script; exit;
3584 
3585  // include the user interface hook
3586  global $ilPluginAdmin;
3587  if (is_object($ilPluginAdmin))
3588  {
3589  $pl_names = $ilPluginAdmin->getActivePluginsForSlot(IL_COMP_SERVICE, "UIComponent", "uihk");
3590  foreach ($pl_names as $pl)
3591  {
3592  $ui_plugin = ilPluginAdmin::getPluginObject(IL_COMP_SERVICE, "UIComponent", "uihk", $pl);
3593  $gui_class = $ui_plugin->getUIClassInstance();
3594  $resp = $gui_class->getHTML("Services/Utilities", "redirect", array("html" => $a_script));
3595  if ($resp["mode"] != ilUIHookPluginGUI::KEEP)
3596  {
3597  $a_script = $gui_class->modifyHTML($a_script, $resp);
3598  }
3599  }
3600  }
3601 
3602  header("Location: ".$a_script);
3603  exit();
3604  }
3605 
3614  public static function insertInstIntoID($a_value)
3615  {
3616  if (substr($a_value, 0, 4) == "il__")
3617  {
3618  $a_value = "il_".IL_INST_ID."_".substr($a_value, 4, strlen($a_value) - 4);
3619  }
3620 
3621  return $a_value;
3622  }
3623 
3634  public static function groupNameExists($a_group_name,$a_id = 0)
3635  {
3636  global $ilDB,$ilErr;
3637 
3638  if (empty($a_group_name))
3639  {
3640  $message = __METHOD__.": No groupname given!";
3641  $ilErr->raiseError($message,$ilErr->WARNING);
3642  }
3643 
3644  $clause = ($a_id) ? " AND obj_id != ".$ilDB->quote($a_id)." " : "";
3645 
3646  $q = "SELECT obj_id FROM object_data ".
3647  "WHERE title = ".$ilDB->quote($a_group_name, "text")." ".
3648  "AND type = ".$ilDB->quote("grp", "text").
3649  $clause;
3650 
3651  $r = $ilDB->query($q);
3652 
3653  if ($r->numRows())
3654  {
3655  return true;
3656  }
3657  else
3658  {
3659  return false;
3660  }
3661  }
3662 
3669  public static function getMemString()
3670  {
3671  $my_pid = getmypid();
3672  return ("MEMORY USAGE (% KB PID ): ".`ps -eo%mem,rss,pid | grep $my_pid`);
3673  }
3674 
3681  public static function isWindows()
3682  {
3683  if (strtolower(substr(php_uname(), 0, 3)) == "win")
3684  {
3685  return true;
3686  }
3687  return false;
3688  }
3689 
3690 
3691  public static function escapeShellArg($a_arg)
3692  {
3693  setlocale(LC_CTYPE, "UTF8", "en_US.UTF-8"); // fix for PHP escapeshellcmd bug. See: http://bugs.php.net/bug.php?id=45132
3694  // see also ilias bug 5630
3695  return escapeshellarg($a_arg);
3696  }
3697 
3707  public static function escapeShellCmd($a_arg)
3708  {
3709  if(ini_get('safe_mode') == 1)
3710  {
3711  return $a_arg;
3712  }
3713  setlocale(LC_CTYPE, "UTF8", "en_US.UTF-8"); // fix for PHP escapeshellcmd bug. See: http://bugs.php.net/bug.php?id=45132
3714  return escapeshellcmd($a_arg);
3715  }
3716 
3726  public static function execQuoted($cmd, $args = NULL)
3727  {
3728  global $ilLog;
3729 
3730  if(ilUtil::isWindows() && strpos($cmd, " ") !== false && substr($cmd, 0, 1) !== '"')
3731  {
3732  // cmd won't work without quotes
3733  $cmd = '"'.$cmd.'"';
3734  if($args)
3735  {
3736  // args are also quoted, workaround is to quote the whole command AGAIN
3737  // was fixed in php 5.2 (see php bug #25361)
3738  if (version_compare(phpversion(), "5.2", "<") && strpos($args, '"') !== false)
3739  {
3740  $cmd = '"'.$cmd." ".$args.'"';
3741  }
3742  // args are not quoted or php is fixed, just append
3743  else
3744  {
3745  $cmd .= " ".$args;
3746  }
3747  }
3748  }
3749  // nothing todo, just append args
3750  else if($args)
3751  {
3752  $cmd .= " ".$args;
3753  }
3754 //echo "<br>".$cmd; exit;
3755  exec($cmd, $arr);
3756 // $ilLog->write("ilUtil::execQuoted: ".$cmd.".");
3757  return $arr;
3758  }
3759 
3782  public static function excelTime($year = "", $month = "", $day = "", $hour = "", $minute = "", $second = "")
3783  {
3784  $starting_time = mktime(0, 0, 0, 1, 2, 1970);
3785  if (strcmp("$year$month$day$hour$minute$second", "") == 0)
3786  {
3787  $target_time = time();
3788  }
3789  else
3790  {
3791  if ($year < 1970)
3792  {
3793  return 0;
3794  }
3795  }
3796  $target_time = mktime($hour, $minute, $second, $month, $day, $year);
3797  $difference = $target_time - $starting_time;
3798  $days = (($difference - ($difference % 86400)) / 86400);
3799  $difference = $difference - ($days * 86400) + 3600;
3800 
3801  // #15343 - using a global locale leads to , instead of . for (implicit) floats
3802  return str_replace(",", ".", ($days + 25570 + ($difference / 86400)));
3803  }
3804 
3811  public static function renameExecutables($a_dir)
3812  {
3813  $def_arr = explode(",", SUFFIX_REPL_DEFAULT);
3814  foreach ($def_arr as $def)
3815  {
3816  ilUtil::rRenameSuffix($a_dir, trim($def), "sec");
3817  }
3818 
3819  $def_arr = explode(",", SUFFIX_REPL_ADDITIONAL);
3820  foreach ($def_arr as $def)
3821  {
3822  ilUtil::rRenameSuffix($a_dir, trim($def), "sec");
3823  }
3824  }
3825 
3838  public static function rRenameSuffix ($a_dir, $a_old_suffix, $a_new_suffix)
3839  {
3840  if ($a_dir == "/" || $a_dir == "" || is_int(strpos($a_dir, ".."))
3841  || trim($a_old_suffix) == "")
3842  {
3843  return false;
3844  }
3845 
3846  // check if argument is directory
3847  if (!@is_dir($a_dir))
3848  {
3849  return false;
3850  }
3851 
3852  // read a_dir
3853  $dir = opendir($a_dir);
3854 
3855  while($file = readdir($dir))
3856  {
3857  if ($file != "." and
3858  $file != "..")
3859  {
3860  // directories
3861  if (@is_dir($a_dir."/".$file))
3862  {
3863  ilUtil::rRenameSuffix($a_dir."/".$file, $a_old_suffix, $a_new_suffix);
3864  }
3865 
3866  // files
3867  if (@is_file($a_dir."/".$file))
3868  {
3869  // first check for files with trailing dot
3870  if(strrpos($file,'.') == (strlen($file) - 1))
3871  {
3872  rename($a_dir.'/'.$file,substr($a_dir.'/'.$file,0,-1));
3873  $file = substr($file,0,-1);
3874  }
3875 
3876  $path_info = pathinfo($a_dir."/".$file);
3877 
3878  if (strtolower($path_info["extension"]) ==
3879  strtolower($a_old_suffix))
3880  {
3881  $pos = strrpos($a_dir."/".$file, ".");
3882  $new_name = substr($a_dir."/".$file, 0, $pos).".".$a_new_suffix;
3883  rename($a_dir."/".$file, $new_name);
3884  }
3885  }
3886  }
3887  }
3888  return true;
3889  }
3890 
3891  public static function isAPICall () {
3892  return strpos($_SERVER["SCRIPT_FILENAME"],"api") !== false ||
3893  strpos($_SERVER["SCRIPT_FILENAME"],"dummy") !== false;
3894  }
3895 
3896  public static function KT_replaceParam($qstring, $paramName, $paramValue) {
3897  if (preg_match("/&" . $paramName . "=/", $qstring)) {
3898  return preg_replace("/&" . $paramName . "=[^&]+/", "&" . $paramName . "=" . urlencode($paramValue), $qstring);
3899  } else {
3900  return $qstring . "&" . $paramName . "=" . urlencode($paramValue);
3901  }
3902  }
3903 
3904  public static function replaceUrlParameterString ($url, $parametersArray) {
3905 
3906  foreach ($parametersArray as $paramName => $paramValue ) {
3907  $url = ilUtil::KT_replaceParam($url, $paramName, $paramValue);
3908  }
3909  return $url;
3910  }
3911 
3918  public static function generatePasswords ($a_number)
3919  {
3920  $ret = array();
3921  srand((double) microtime()*1000000);
3922 
3923  include_once('./Services/PrivacySecurity/classes/class.ilSecuritySettings.php');
3924  $security = ilSecuritySettings::_getInstance();
3925 
3926  for ($i=1; $i<=$a_number; $i++)
3927  {
3928  $min = ($security->getPasswordMinLength() > 0)
3929  ? $security->getPasswordMinLength()
3930  : 6;
3931  $max = ($security->getPasswordMaxLength() > 0)
3932  ? $security->getPasswordMaxLength()
3933  : 10;
3934  if ($min > $max)
3935  {
3936  $max = $max + 1;
3937  }
3938  $length = rand($min,$max);
3939  $next = rand(1,2);
3940  $vowels = "aeiou";
3941  $vowels_uc = strtoupper($vowels);
3942  $consonants = "bcdfghjklmnpqrstvwxyz";
3943  $consonants_uc = strtoupper($consonants);
3944  $numbers = "1234567890";
3945  $special = "_.+?#-*@!$%~";
3946  $pw = "";
3947 
3948  if($security->getPasswordNumberOfUppercaseChars() > 0)
3949  {
3950  for($j = 0; $j < $security->getPasswordNumberOfUppercaseChars(); $j++)
3951  {
3952  switch ($next)
3953  {
3954  case 1:
3955  $pw.= $consonants_uc[rand(0, strlen($consonants_uc) - 1)];
3956  $next = 2;
3957  break;
3958 
3959  case 2:
3960  $pw.= $vowels_uc[rand(0, strlen($vowels_uc) - 1)];
3961  $next = 1;
3962  break;
3963  }
3964  }
3965  }
3966 
3967  if($security->isPasswordCharsAndNumbersEnabled())
3968  {
3969  $pw.= $numbers[rand(0, strlen($numbers) - 1)];
3970  }
3971 
3972  if($security->isPasswordSpecialCharsEnabled())
3973  {
3974  $pw.= $special[rand(0, strlen($special) - 1)];
3975  }
3976 
3977  $num_lcase_chars = max($security->getPasswordNumberOfLowercaseChars(), $length - strlen($pw));
3978  for($j = 0; $j < $num_lcase_chars; $j++)
3979  {
3980  switch ($next)
3981  {
3982  case 1:
3983  $pw.= $consonants[rand(0, strlen($consonants) - 1)];
3984  $next = 2;
3985  break;
3986 
3987  case 2:
3988  $pw.= $vowels[rand(0, strlen($vowels) - 1)];
3989  $next = 1;
3990  break;
3991  }
3992  }
3993 
3994  $pw = str_shuffle($pw);
3995 
3996  $ret[] = $pw;
3997  }
3998  return $ret;
3999  }
4000 
4001  public static function removeTrailingPathSeparators($path)
4002  {
4003  $path = preg_replace("/[\/\\\]+$/", "", $path);
4004  return $path;
4005  }
4006 
4017  public static function array_php2js($data)
4018  {
4019  if (empty($data))
4020  {
4021  $data = array();
4022  }
4023 
4024  foreach($data as $k=>$datum)
4025  {
4026  if(is_null($datum)) $data[$k] = 'null';
4027  if(is_string($datum)) $data[$k] = "'" . $datum . "'";
4028  if(is_array($datum)) $data[$k] = array_php2js($datum);
4029  }
4030 
4031  return "[" . implode(', ', $data) . "]";
4032  }
4033 
4040  public static function virusHandling($a_file, $a_orig_name = "", $a_clean = true)
4041  {
4042  global $lng;
4043 
4044  if (IL_VIRUS_SCANNER != "None")
4045  {
4046  require_once("./Services/VirusScanner/classes/class.ilVirusScannerFactory.php");
4048  if (($vs_txt = $vs->scanFile($a_file, $a_orig_name)) != "")
4049  {
4050  if ($a_clean && (IL_VIRUS_CLEAN_COMMAND != ""))
4051  {
4052  $clean_txt = $vs->cleanFile($a_file, $a_orig_name);
4053  if ($vs->fileCleaned())
4054  {
4055  $vs_txt.= "<br />".$lng->txt("cleaned_file").
4056  "<br />".$clean_txt;
4057  $vs_txt.= "<br />".$lng->txt("repeat_scan");
4058  if (($vs2_txt = $vs->scanFile($a_file, $a_orig_name)) != "")
4059  {
4060  return array(false, nl2br($vs_txt)."<br />".$lng->txt("repeat_scan_failed").
4061  "<br />".nl2br($vs2_txt));
4062  }
4063  else
4064  {
4065  return array(true, nl2br($vs_txt)."<br />".$lng->txt("repeat_scan_succeded"));
4066  }
4067  }
4068  else
4069  {
4070  return array(false, nl2br($vs_txt)."<br />".$lng->txt("cleaning_failed"));
4071  }
4072  }
4073  else
4074  {
4075  return array(false, nl2br($vs_txt));
4076  }
4077  }
4078  }
4079 
4080  return array(true,"");
4081  }
4082 
4083 
4090  public static function moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors = true,
4091  $a_mode = "move_uploaded")
4092  {
4093  global $lng, $ilias;
4094 //echo "<br>ilUtli::moveuploadedFile($a_name)";
4095 
4096  if (!is_file($a_file))
4097  {
4098  if ($a_raise_errors)
4099  {
4100  $ilias->raiseError($lng->txt("upload_error_file_not_found"), $ilias->error_obj->MESSAGE);
4101  }
4102  else
4103  {
4104  ilUtil::sendFailure($lng->txt("upload_error_file_not_found"), true);
4105  }
4106  return false;
4107  }
4108 
4109  // virus handling
4110  $vir = ilUtil::virusHandling($a_file, $a_name);
4111  if (!$vir[0])
4112  {
4113  unlink($a_file);
4114  if ($a_raise_errors)
4115  {
4116  $ilias->raiseError($lng->txt("file_is_infected")."<br />".
4117  $vir[1],
4118  $ilias->error_obj->MESSAGE);
4119  }
4120  else
4121  {
4122  ilUtil::sendFailure($lng->txt("file_is_infected")."<br />".
4123  $vir[1], true);
4124  }
4125  return false;
4126  }
4127  else
4128  {
4129  if ($vir[1] != "")
4130  {
4131  ilUtil::sendInfo($vir[1], true);
4132  }
4133  switch ($a_mode)
4134  {
4135  case "rename":
4136  return rename($a_file, $a_target);
4137  break;
4138 
4139  case "copy":
4140  return copy($a_file, $a_target);
4141  break;
4142 
4143  default:
4144  return move_uploaded_file($a_file, $a_target);
4145  break;
4146  }
4147  }
4148  }
4149 
4150 
4157  public static function date_mysql2time($mysql_date_time) {
4158  list($datum, $uhrzeit) = explode (" ",$mysql_date_time);
4159  list($jahr, $monat, $tag) = explode("-", $datum);
4160  list($std, $min, $sec) = explode(":", $uhrzeit);
4161  return mktime ((int) $std, (int) $min, (int) $sec, (int) $monat, (int) $tag, (int) $jahr);
4162  }
4163 
4170  public static function now()
4171  {
4172  return date("Y-m-d H:i:s");
4173  }
4174 
4190  public static function &processCSVRow(&$row, $quoteAll = FALSE, $separator = ";", $outUTF8 = FALSE, $compatibleWithMSExcel = TRUE)
4191  {
4192  $resultarray = array();
4193  foreach ($row as $rowindex => $entry)
4194  {
4195  $surround = FALSE;
4196  if ($quoteAll)
4197  {
4198  $surround = TRUE;
4199  }
4200  if (strpos($entry, "\"") !== FALSE)
4201  {
4202  $entry = str_replace("\"", "\"\"", $entry);
4203  $surround = TRUE;
4204  }
4205  if (strpos($entry, $separator) !== FALSE)
4206  {
4207  $surround = TRUE;
4208  }
4209  if ($compatibleWithMSExcel)
4210  {
4211  // replace all CR LF with LF (for Excel for Windows compatibility
4212  $entry = str_replace(chr(13).chr(10), chr(10), $entry);
4213  }
4214  if ($surround)
4215  {
4216  if ($outUTF8)
4217  {
4218  $resultarray[$rowindex] = "\"" . $entry . "\"";
4219  }
4220  else
4221  {
4222  $resultarray[$rowindex] = utf8_decode("\"" . $entry . "\"");
4223  }
4224  }
4225  else
4226  {
4227  if ($outUTF8)
4228  {
4229  $resultarray[$rowindex] = $entry;
4230  }
4231  else
4232  {
4233  $resultarray[$rowindex] = utf8_decode($entry);
4234  }
4235  }
4236  }
4237  return $resultarray;
4238  }
4239 
4240  // validates a domain name (example: www.ilias.de)
4241  public static function isDN($a_str)
4242  {
4243  return(preg_match("/^[a-z]+([a-z0-9-]*[a-z0-9]+)?(\.([a-z]+([a-z0-9-]*[a-z0-9]+)?)+)*$/",$a_str));
4244  }
4245 
4246  // validates an IP address (example: 192.168.1.1)
4247  public static function isIPv4($a_str)
4248  {
4249  return(preg_match("/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.".
4250  "(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/",$a_str));
4251  }
4252 
4253 
4282  public static function _getObjectsByOperations($a_obj_type,$a_operation,$a_usr_id = 0,$limit = 0)
4283  {
4284  global $ilDB,$rbacreview,$ilAccess,$ilUser,$ilias,$tree;
4285 
4286  if(!is_array($a_obj_type))
4287  {
4288  $where = "WHERE type = ".$ilDB->quote($a_obj_type, "text")." ";
4289  }
4290  else
4291  {
4292  $where = "WHERE ".$ilDB->in("type", $a_obj_type, false, "text")." ";
4293  }
4294 
4295  // limit number of results default is search result limit
4296  if(!$limit)
4297  {
4298  $limit = $ilias->getSetting('search_max_hits',100);
4299  }
4300  if($limit == -1)
4301  {
4302  $limit = 10000;
4303  }
4304 
4305  // default to logged in usr
4306  $a_usr_id = $a_usr_id ? $a_usr_id : $ilUser->getId();
4307  $a_roles = $rbacreview->assignedRoles($a_usr_id);
4308 
4309  // Since no rbac_pa entries are available for the system role. This function returns !all! ref_ids in the case the user
4310  // is assigned to the system role
4311  if($rbacreview->isAssigned($a_usr_id,SYSTEM_ROLE_ID))
4312  {
4313  $query = "SELECT ref_id FROM object_reference obr LEFT JOIN object_data obd ON obr.obj_id = obd.obj_id ".
4314  "LEFT JOIN tree ON obr.ref_id = tree.child ".
4315  $where.
4316  "AND tree = 1";
4317 
4318  $res = $ilDB->query($query);
4319  $counter = 0;
4320  while($row = $ilDB->fetchObject($res))
4321  {
4322  // Filter recovery folder
4323  if($tree->isGrandChild(RECOVERY_FOLDER_ID, $row->ref_id))
4324  {
4325  continue;
4326  }
4327 
4328  if($counter++ >= $limit)
4329  {
4330  break;
4331  }
4332 
4333  $ref_ids[] = $row->ref_id;
4334  }
4335  return $ref_ids ? $ref_ids : array();
4336  } // End Administrators
4337 
4338  // Check ownership if it is not asked for edit_permission or a create permission
4339  if($a_operation == 'edit_permissions' or strpos($a_operation,'create') !== false)
4340  {
4341  $check_owner = ") ";
4342  }
4343  else
4344  {
4345  $check_owner = "OR owner = ".$ilDB->quote($a_usr_id, "integer").") ";
4346  }
4347 
4348  $ops_ids = ilRbacReview::_getOperationIdsByName(array($a_operation));
4349  $ops_id = $ops_ids[0];
4350 
4351  $and = "AND ((".$ilDB->in("rol_id", $a_roles, false, "integer")." ";
4352 
4353  $query = "SELECT DISTINCT(obr.ref_id),obr.obj_id,type FROM object_reference obr ".
4354  "JOIN object_data obd ON obd.obj_id = obr.obj_id ".
4355  "LEFT JOIN rbac_pa ON obr.ref_id = rbac_pa.ref_id ".
4356  $where.
4357  $and.
4358  "AND (".$ilDB->like("ops_id", "text","%i:".$ops_id."%"). " ".
4359  "OR ".$ilDB->like("ops_id", "text", "%:\"".$ops_id."\";%").")) ".
4360  $check_owner;
4361 
4362  $res = $ilDB->query($query);
4363  $counter = 0;
4364  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
4365  {
4366  if($counter >= $limit)
4367  {
4368  break;
4369  }
4370 
4371  // Filter objects in recovery folder
4372  if($tree->isGrandChild(RECOVERY_FOLDER_ID, $row->ref_id))
4373  {
4374  continue;
4375  }
4376 
4377  // Check deleted, hierarchical access ...
4378  if($ilAccess->checkAccessOfUser($a_usr_id,$a_operation,'',$row->ref_id,$row->type,$row->obj_id))
4379  {
4380  $counter++;
4381  $ref_ids[] = $row->ref_id;
4382  }
4383  }
4384  return $ref_ids ? $ref_ids : array();
4385  }
4386 
4387 
4394  function includeMathjax($a_tpl = null)
4395  {
4396  global $tpl;
4397 
4398  if ($a_tpl == null)
4399  {
4400  $a_tpl = $tpl;
4401  }
4402 
4403  // - take care of html exports (-> see buildLatexImages)
4404  include_once "./Services/Administration/classes/class.ilSetting.php";
4405  $mathJaxSetting = new ilSetting("MathJax");
4406  $use_mathjax = $mathJaxSetting->get("enable");
4407  if ($use_mathjax)
4408  {
4409  $a_tpl->addJavaScript($mathJaxSetting->get("path_to_mathjax"));
4410  }
4411  }
4412 
4413 
4423  public static function insertLatexImages($a_text, $a_start = "\[tex\]", $a_end = "\[\/tex\]")
4424  {
4425  global $tpl, $lng, $ilUser;
4426 
4427  $cgi = URL_TO_LATEX;
4428 
4429  // - take care of html exports (-> see buildLatexImages)
4430  include_once "./Services/Administration/classes/class.ilSetting.php";
4431  $mathJaxSetting = new ilSetting("MathJax");
4432  $use_mathjax = $mathJaxSetting->get("enable");
4433  if ($use_mathjax)
4434  {
4435  $a_text = preg_replace("/\\\\([RZN])([^a-zA-Z]|<\/span>)/", "\\mathbb{"."$1"."}"."$2", $a_text);
4436  $tpl->addJavaScript($mathJaxSetting->get("path_to_mathjax"));
4437  }
4438 
4439  // this is a fix for bug5362
4440  $cpos = 0;
4441  $o_start = $a_start;
4442  $o_end = $a_end;
4443  $a_start = str_replace("\\", "", $a_start);
4444  $a_end = str_replace("\\", "", $a_end);
4445 
4446  while (is_int($spos = stripos($a_text, $a_start, $cpos))) // find next start
4447  {
4448  if (is_int ($epos = stripos($a_text, $a_end, $spos + 1)))
4449  {
4450  $tex = substr($a_text, $spos + strlen($a_start), $epos - $spos - strlen($a_start));
4451 
4452  // replace, if tags do not go across div borders
4453  if (!is_int(strpos($tex, "</div>")))
4454  {
4455  if (!$use_mathjax)
4456  {
4457  $a_text = substr($a_text, 0, $spos).
4458  "<img alt=\"".htmlentities($tex)."\" src=\"".$cgi."?".
4459  rawurlencode(str_replace('&amp;', '&', str_replace('&gt;', '>', str_replace('&lt;', '<', $tex))))."\" ".
4460  " />".
4461  substr($a_text, $epos + strlen($a_end));
4462  }
4463  else
4464  {
4465  $tex = $a_start.$tex.$a_end;
4466 
4467  switch ((int) $mathJaxSetting->get("limiter"))
4468  {
4469  case 1:
4470  $mj_start = "[tex]";
4471  $mj_end = "[/tex]";
4472  break;
4473 
4474  case 2:
4475  $mj_start = '<span class="math">';
4476  $mj_end = '</span>';
4477  break;
4478 
4479  default:
4480  $mj_start = "\(";
4481  $mj_end = "\)";
4482  break;
4483  }
4484 
4485  $replacement =
4486  preg_replace('/' . $o_start . '(.*?)' . $o_end . '/ie',
4487  "'".$mj_start."' . preg_replace('/[\\\\\\\\\\]{2}/', '\\cr', str_replace('<', '&lt;', str_replace('<br/>', '', str_replace('<br />', '', str_replace('<br>', '', '$1'))))) . '".$mj_end."'", $tex);
4488  // added special handling for \\ -> \cr, < -> $lt; and removal of <br/> tags in jsMath expressions, H. Schottmüller, 2007-09-09
4489  $a_text = substr($a_text, 0, $spos).
4490  $replacement.
4491  substr($a_text, $epos + strlen($a_end));
4492  }
4493  }
4494  }
4495  $cpos = $spos + 1;
4496  }
4497 
4498  $result_text = $a_text;
4499 
4500  return $result_text;
4501  }
4502 
4512  public static function buildLatexImages($a_text, $a_dir)
4513  {
4514  $result_text = $a_text;
4515 
4516  $start = "\[tex\]";
4517  $end = "\[\/tex\]";
4518 
4519  $cgi = URL_TO_LATEX;
4520 
4521  if ($cgi != "")
4522  {
4523  while (preg_match('/' . $start . '(.*?)' . $end . '/ie', $result_text, $found))
4524  {
4525  $cnt = (int) $GLOBALS["teximgcnt"]++;
4526  // get image from cgi and write it to file
4527  $fpr = @fopen($cgi."?".rawurlencode($found[1]), "r");
4528  $lcnt = 0;
4529  if ($fpr)
4530  {
4531  while(!feof($fpr))
4532  {
4533  $buf = fread($fpr, 1024);
4534  if ($lcnt == 0)
4535  {
4536  if (is_int(strpos(strtoupper(substr($buf, 0, 5)), "GIF")))
4537  {
4538  $suffix = "gif";
4539  }
4540  else
4541  {
4542  $suffix = "png";
4543  }
4544  $fpw = fopen($a_dir."/teximg/img".$cnt.".".$suffix, "w");
4545  }
4546  $lcnt++;
4547  fwrite($fpw, $buf);
4548  }
4549  fclose($fpw);
4550  fclose($fpr);
4551  }
4552 
4553  // replace tex-tag
4554  $img_str = "./teximg/img".$cnt.".".$suffix;
4555  $result_text = str_replace($found[0],
4556  '<img alt="'.$found[1].'" src="'.$img_str.'" />', $result_text);
4557  }
4558  }
4559 
4560  return $result_text;
4561  }
4562 
4571  public static function prepareTextareaOutput($txt_output, $prepare_for_latex_output = FALSE, $omitNl2BrWhenTextArea = false)
4572  {
4573  $result = $txt_output;
4574  $is_html = self::isHTML($result);
4575 
4576  if ($prepare_for_latex_output)
4577  {
4578  $result = ilUtil::insertLatexImages($result, "<span class\=\"latex\">", "<\/span>");
4579  $result = ilUtil::insertLatexImages($result, "\[tex\]", "\[\/tex\]");
4580  }
4581 
4582  // removed: did not work with magic_quotes_gpc = On
4583  if (!$is_html )
4584  {
4585  if(!$omitNl2BrWhenTextArea)
4586  {
4587  // if the string does not contain HTML code, replace the newlines with HTML line breaks
4588  $result = preg_replace("/[\n]/", "<br />", $result);
4589  }
4590  }
4591  else
4592  {
4593  // patch for problems with the <pre> tags in tinyMCE
4594  if (preg_match_all("/(<pre>.*?<\/pre>)/ims", $result, $matches))
4595  {
4596  foreach ($matches[0] as $found)
4597  {
4598  $replacement = "";
4599  if (strpos("\n", $found) === FALSE)
4600  {
4601  $replacement = "\n";
4602  }
4603  $removed = preg_replace("/<br\s*?\/>/ims", $replacement, $found);
4604  $result = str_replace($found, $removed, $result);
4605  }
4606  }
4607  }
4608  if ($prepare_for_latex_output)
4609  {
4610  // replace special characters to prevent problems with the ILIAS template system
4611  // eg. if someone uses {1} as an answer, nothing will be shown without the replacement
4612  $result = str_replace("{", "&#123;", $result);
4613  $result = str_replace("}", "&#125;", $result);
4614  $result = str_replace("\\", "&#92;", $result);
4615  }
4616 
4617  return $result;
4618  }
4619 
4628  public static function isHTML($a_text)
4629  {
4630  if( preg_match("/<[^>]*?>/", $a_text) )
4631  {
4632  return true;
4633  }
4634 
4635  return false;
4636  }
4637 
4646  public static function int2array ($seconds, $periods = null)
4647  {
4648  // Define time periods
4649  if (!is_array($periods))
4650  {
4651  $periods = array (
4652  'years' => 31536000,
4653  'months' => 2592000,
4654  'days' => 86400,
4655  'hours' => 3600,
4656  'minutes' => 60,
4657  'seconds' => 1
4658  );
4659  }
4660 
4661  // Loop
4662  $seconds = (float) $seconds;
4663  foreach ($periods as $period => $value)
4664  {
4665  $count = floor($seconds / $value);
4666 
4667  if ($count == 0)
4668  {
4669  continue;
4670  }
4671 
4672  $values[$period] = $count;
4673  $seconds = $seconds % $value;
4674  }
4675  // Return
4676  if (empty($values))
4677  {
4678  $values = null;
4679  }
4680 
4681  return $values;
4682  }
4683 
4692  public static function timearray2string ($duration)
4693  {
4694  global $lng;
4695 
4696  if (!is_array($duration))
4697  {
4698  return false;
4699  }
4700 
4701  foreach ($duration as $key => $value) {
4702 
4703  // Plural
4704  if ($value > 1)
4705  {
4706  $segment_name = $key;
4707  $segment_name = $lng->txt($segment_name);
4708  $segment = $value . ' ' . $segment_name;
4709  }
4710  else
4711  {
4712  $segment_name = substr($key, 0, -1);
4713  $segment_name = $lng->txt($segment_name);
4714  $segment = $value . ' ' . $segment_name;
4715  }
4716 
4717  $array[] = $segment;
4718  }
4719  $len = count($array);
4720 
4721  if ($len>3)
4722  {
4723  $array=array_slice($array,0,(3-$len));
4724  }
4725 
4726  $str = implode(', ', $array);
4727 
4728  return $str;
4729  }
4730 
4731  public static function getFileSizeInfo()
4732  {
4733  global $lng;
4734 
4735  // get the value for the maximal uploadable filesize from the php.ini (if available)
4736  $umf=get_cfg_var("upload_max_filesize");
4737  // get the value for the maximal post data from the php.ini (if available)
4738  $pms=get_cfg_var("post_max_size");
4739 
4740  // use the smaller one as limit
4741  $max_filesize=min($umf, $pms);
4742  if (!$max_filesize) $max_filesize=max($umf, $pms);
4743 
4744  return $lng->txt("file_notice")." $max_filesize.";
4745  }
4746 
4755  public static function __extractRefId($role_title)
4756  {
4757 
4758  $test_str = explode('_',$role_title);
4759 
4760  if ($test_str[0] == 'il')
4761  {
4762  $test2 = (int) $test_str[3];
4763  return is_numeric ($test2) ? (int) $test2 : false;
4764  }
4765  return false;
4766  }
4767 
4778  public static function __extractId($ilias_id, $inst_id)
4779  {
4780 
4781  $test_str = explode('_',$ilias_id);
4782 
4783  if ($test_str[0] == 'il' && $test_str[1] == $inst_id && count($test_str) == 4)
4784  {
4785  $test2 = (int) $test_str[3];
4786  return is_numeric ($test2) ? (int) $test2 : false;
4787  }
4788  return false;
4789  }
4790 
4805  public static function _sortIds($a_ids,$a_table,$a_field,$a_id_name)
4806  {
4807  global $ilDB;
4808 
4809  if(!$a_ids)
4810  {
4811  return array();
4812  }
4813 
4814  // use database to sort user array
4815  $where = "WHERE ".$a_id_name." IN (";
4816  $where .= implode(",", ilUtil::quoteArray($a_ids));
4817  $where .= ") ";
4818 
4819  $query = "SELECT ".$a_id_name." FROM ".$a_table." ".
4820  $where.
4821  "ORDER BY ".$a_field;
4822 
4823  $res = $ilDB->query($query);
4824  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
4825  {
4826  $ids[] = $row->$a_id_name;
4827  }
4828  return $ids ? $ids : array();
4829  }
4830 
4840  public static function getMySQLTimestamp($a_ts)
4841  {
4842  global $ilDB;
4843 
4844  return $a_ts;
4845  }
4846 
4853  public static function quoteArray($a_array)
4854  {
4855  global $ilDB;
4856 
4857 
4858  if(!is_array($a_array) or !count($a_array))
4859  {
4860  return array("''");
4861  }
4862 
4863  foreach($a_array as $k => $item)
4864  {
4865  $a_array[$k] = $ilDB->quote($item);
4866  }
4867 
4868  return $a_array;
4869  }
4870 
4879  public static function sendInfo($a_info = "",$a_keep = false)
4880  {
4881  global $tpl;
4882  $tpl->setMessage("info", $a_info, $a_keep);
4883  }
4884 
4893  public static function sendFailure($a_info = "",$a_keep = false)
4894  {
4895  global $tpl;
4896  $tpl->setMessage("failure", $a_info, $a_keep);
4897  }
4898 
4905  public static function sendQuestion($a_info = "",$a_keep = false)
4906  {
4907  global $tpl;
4908  $tpl->setMessage("question", $a_info, $a_keep);
4909  }
4910 
4919  public static function sendSuccess($a_info = "",$a_keep = false)
4920  {
4921  global $tpl;
4922  $tpl->setMessage("success", $a_info, $a_keep);
4923  }
4924 
4925  public static function infoPanel($a_keep = true)
4926  {
4927  global $tpl,$ilias,$lng;
4928 
4929  if (!empty($_SESSION["infopanel"]) and is_array($_SESSION["infopanel"]))
4930  {
4931  $tpl->addBlockFile("INFOPANEL", "infopanel", "tpl.infopanel.html",
4932  "Services/Utilities");
4933  $tpl->setCurrentBlock("infopanel");
4934 
4935  if (!empty($_SESSION["infopanel"]["text"]))
4936  {
4937  $link = "<a href=\"".$dir.$_SESSION["infopanel"]["link"]."\" target=\"".
4938  ilFrameTargetInfo::_getFrame("MainContent").
4939  "\">";
4940  $link .= $lng->txt($_SESSION["infopanel"]["text"]);
4941  $link .= "</a>";
4942  }
4943 
4944  // deactivated
4945  if (!empty($_SESSION["infopanel"]["img"]))
4946  {
4947  $link .= "<td><a href=\"".$_SESSION["infopanel"]["link"]."\" target=\"".
4948  ilFrameTargetInfo::_getFrame("MainContent").
4949  "\">";
4950  $link .= "<img src=\"".$ilias->tplPath.$ilias->account->prefs["skin"]."/images/".
4951  $_SESSION["infopanel"]["img"]."\" border=\"0\" vspace=\"0\"/>";
4952  $link .= "</a></td>";
4953  }
4954 
4955  $tpl->setVariable("INFO_ICONS",$link);
4956  $tpl->parseCurrentBlock();
4957  }
4958 
4959  //if (!$a_keep)
4960  //{
4961  ilSession::clear("infopanel");
4962  //}
4963  }
4964 
4965 
4974  public static function dirsize($directory)
4975  {
4976  $size = 0;
4977  if (!is_dir($directory))
4978  {
4979  // BEGIN DiskQuota Suppress PHP warning when attempting to determine
4980  // dirsize of non-existing directory
4981  $size = @filesize($directory);
4982  // END DiskQuota Suppress PHP warning.
4983  return ($size === false) ? -1 : $size;
4984  }
4985  if ($DIR = opendir($directory))
4986  {
4987  while (($dirfile = readdir($DIR)) !== false)
4988  {
4989  if (is_link($directory . DIRECTORY_SEPARATOR . $dirfile) || $dirfile == '.' || $dirfile == '..')
4990  continue;
4991  if (is_file($directory . DIRECTORY_SEPARATOR . $dirfile))
4992  $size += filesize($directory . DIRECTORY_SEPARATOR . $dirfile);
4993  else if (is_dir($directory . DIRECTORY_SEPARATOR . $dirfile))
4994  {
4995  // BEGIN DiskQuota: dirsize is not a global function anymore
4996  $dirSize = ilUtil::dirsize($directory . DIRECTORY_SEPARATOR . $dirfile);
4997  // END DiskQuota: dirsize is not a global function anymore
4998  if ($dirSize >= 0)
4999  $size += $dirSize;
5000  else return -1;
5001  }
5002  }
5003  closedir($DIR);
5004  }
5005  return $size;
5006  }
5007 
5008  public static function randomhash()
5009  {
5010  return md5(rand(1,9999999) + str_replace(" ", "", (string) microtime()));
5011  }
5012 
5013  public static function setCookie($a_cookie_name,$a_cookie_value = '', $a_also_set_super_global = true, $a_set_cookie_invalid = false)
5014  {
5015  /*
5016  if(!(bool)$a_set_cookie_invalid) $expire = IL_COOKIE_EXPIRE;
5017  else $expire = time() - (365*24*60*60);
5018  */
5019  // Temporary fix for feed.php
5020  if(!(bool)$a_set_cookie_invalid) $expire = 0;
5021  else $expire = time() - (365*24*60*60);
5022 
5023  // setcookie() supports 5th parameter
5024  // only for php version 5.2.0 and above
5025  if( version_compare(PHP_VERSION, '5.2.0', '>=') )
5026  {
5027  // PHP version >= 5.2.0
5028  setcookie( $a_cookie_name, $a_cookie_value, $expire,
5029  IL_COOKIE_PATH, IL_COOKIE_DOMAIN, IL_COOKIE_SECURE, IL_COOKIE_HTTPONLY
5030  );
5031  }
5032  else
5033  {
5034  // PHP version < 5.2.0
5035  setcookie( $a_cookie_name, $a_cookie_value, $expire,
5036  IL_COOKIE_PATH, IL_COOKIE_DOMAIN, IL_COOKIE_SECURE
5037  );
5038  }
5039 
5040  if((bool)$a_also_set_super_global) $_COOKIE[$a_cookie_name] = $a_cookie_value;
5041  }
5042 
5043  public static function _sanitizeFilemame($a_filename)
5044  {
5045  return strip_tags(self::stripSlashes($a_filename));
5046  }
5047 
5048  public static function _getHttpPath()
5049  {
5050  global $ilIliasIniFile;
5051 
5052  if($_SERVER['SHELL'] || php_sapi_name() == 'cli' ||
5053  // fallback for windows systems, useful in crons
5054  (class_exists("ilContext") && !ilContext::usesHTTP()))
5055  {
5056  return $ilIliasIniFile->readVariable('server', 'http_path');
5057  }
5058  else
5059  {
5060  return ILIAS_HTTP_PATH;
5061  }
5062  }
5063 
5069  public static function printBacktrace($a_limit = 0)
5070  {
5071  $bt = debug_backtrace();
5072  $cnt = 0;
5073  foreach ($bt as $t)
5074  {
5075  if ($cnt != 0 && ($a_limit == 0 || $cnt <= $a_limit))
5076  {
5077  echo "<br>".$t["file"].", ".$t["function"]." [".$t["line"]."]";
5078  }
5079  $cnt++;
5080  }
5081  echo "<br>";
5082  }
5083 
5098  public static function parseImportId($a_import_id)
5099  {
5100  $exploded = explode('_',$a_import_id);
5101 
5102  $parsed['orig'] = $a_import_id;
5103  if($exploded[0] == 'il')
5104  {
5105  $parsed['prefix'] = $exploded[0];
5106  }
5107  if(is_numeric($exploded[1]))
5108  {
5109  $parsed['inst_id'] = (int) $exploded[1];
5110  }
5111  $parsed['type'] = $exploded[2];
5112 
5113  if(is_numeric($exploded[3]))
5114  {
5115  $parsed['id'] = (int) $exploded[3];
5116  }
5117  return $parsed;
5118  }
5119 
5126  public static function unserializeSession($data)
5127  {
5128  $vars = preg_split(
5129  '/([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff^|]*)\|/',
5130  $data,
5131  -1,
5132  PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
5133  );
5134 
5135  $result = array();
5136 
5137  for($i = 0; $vars[$i]; $i++)
5138  {
5139  $result[$vars[$i++]] = unserialize($vars[$i]);
5140  }
5141 
5142  return $result;
5143  }
5144 
5145 
5156  function rangeDownload($file) {
5157 
5158  $fp = @fopen($file, 'rb');
5159 
5160  $size = filesize($file); // File size
5161  $length = $size; // Content length
5162  $start = 0; // Start byte
5163  $end = $size - 1; // End byte
5164  // Now that we've gotten so far without errors we send the accept range header
5165  /* At the moment we only support single ranges.
5166  * Multiple ranges requires some more work to ensure it works correctly
5167  * and comply with the spesifications: http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.2
5168  *
5169  * Multirange support annouces itself with:
5170  * header('Accept-Ranges: bytes');
5171  *
5172  * Multirange content must be sent with multipart/byteranges mediatype,
5173  * (mediatype = mimetype)
5174  * as well as a boundry header to indicate the various chunks of data.
5175  */
5176  header("Accept-Ranges: 0-$length");
5177  // header('Accept-Ranges: bytes');
5178  // multipart/byteranges
5179  // http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.2
5180  if (isset($_SERVER['HTTP_RANGE'])) {
5181 
5182  $c_start = $start;
5183  $c_end = $end;
5184  // Extract the range string
5185  list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
5186  // Make sure the client hasn't sent us a multibyte range
5187  if (strpos($range, ',') !== false) {
5188 
5189  // (?) Shoud this be issued here, or should the first
5190  // range be used? Or should the header be ignored and
5191  // we output the whole content?
5192  header('HTTP/1.1 416 Requested Range Not Satisfiable');
5193  header("Content-Range: bytes $start-$end/$size");
5194  // (?) Echo some info to the client?
5195  exit;
5196  }
5197  // If the range starts with an '-' we start from the beginning
5198  // If not, we forward the file pointer
5199  // And make sure to get the end byte if spesified
5200  if ($range == '-') {
5201 
5202  // The n-number of the last bytes is requested
5203  $c_start = $size - substr($range, 1);
5204  }
5205  else {
5206 
5207  $range = explode('-', $range);
5208  $c_start = $range[0];
5209  $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
5210  }
5211  /* Check the range and make sure it's treated according to the specs.
5212  * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
5213  */
5214  // End bytes can not be larger than $end.
5215  $c_end = ($c_end > $end) ? $end : $c_end;
5216  // Validate the requested range and return an error if it's not correct.
5217  if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
5218 
5219  header('HTTP/1.1 416 Requested Range Not Satisfiable');
5220  header("Content-Range: bytes $start-$end/$size");
5221  // (?) Echo some info to the client?
5222  exit;
5223  }
5224  $start = $c_start;
5225  $end = $c_end;
5226  $length = $end - $start + 1; // Calculate new content length
5227  fseek($fp, $start);
5228  header('HTTP/1.1 206 Partial Content');
5229  }
5230  // Notify the client the byte range we'll be outputting
5231  header("Content-Range: bytes $start-$end/$size");
5232  header("Content-Length: $length");
5233 
5234  // Start buffered download
5235  $buffer = 1024 * 8;
5236  while(!feof($fp) && ($p = ftell($fp)) <= $end) {
5237 
5238  if ($p + $buffer > $end) {
5239 
5240  // In case we're only outputtin a chunk, make sure we don't
5241  // read past the length
5242  $buffer = $end - $p + 1;
5243  }
5244  set_time_limit(0); // Reset time limit for big files
5245  echo fread($fp, $buffer);
5246  flush(); // Free up memory. Otherwise large files will trigger PHP's memory limit.
5247  }
5248 
5249  fclose($fp);
5250 
5251  }
5252 
5253 
5254 } // END class.ilUtil
5255 
5256 
5257 ?>