• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

Services/Utilities/classes/class.ilUtil.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003 +-----------------------------------------------------------------------------+
00004 | ILIAS open source                                                           |
00005 +-----------------------------------------------------------------------------+
00006 | Copyright (c) 1998-2006 ILIAS open source, University of Cologne            |
00007 |                                                                             |
00008 | This program is free software; you can redistribute it and/or               |
00009 | modify it under the terms of the GNU General Public License                 |
00010 | as published by the Free Software Foundation; either version 2              |
00011 | of the License, or (at your option) any later version.                      |
00012 |                                                                             |
00013 | This program is distributed in the hope that it will be useful,             |
00014 | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016 | GNU General Public License for more details.                                |
00017 |                                                                             |
00018 | You should have received a copy of the GNU General Public License           |
00019 | along with this program; if not, write to the Free Software                 |
00020 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021 +-----------------------------------------------------------------------------+
00022 */
00023 
00037 class ilUtil
00038 {
00046         function getImageTagByType($a_type, $a_path, $a_big = false)
00047         {
00048                 global $lng;
00049 
00050                 if ($a_big)
00051                 {
00052                         $big = "_b";
00053                 }
00054                 $filename = "icon_".$a_type."$big.gif";
00055 
00056                 return "<img src=\"".ilUtil::getImagePath($filename)."\" alt=\"".$lng->txt("obj_".$a_type)."\" title=\"".$lng->txt("obj_".$a_type)."\" border=\"0\" vspace=\"0\"/>";
00057                 //return "<img src=\"".$a_path."/images/"."icon_".$a_type."$big.gif\" alt=\"".$lng->txt("obj_".$a_type)."\" title=\"".$lng->txt("obj_".$a_type)."\" border=\"0\" vspace=\"0\"/>";
00058         }
00059         
00071         public function getTypeIconPath($a_type,$a_obj_id,$a_size = 'small')
00072         {
00073                 global $ilSetting;
00074                 
00075                 if($ilSetting->get("custom_icons"))
00076                 {
00077                         switch($a_type)
00078                         {
00079                                 case 'cat':
00080                                 case 'crs':
00081                                 case 'grp':
00082                                         if(strlen($path = ilContainer::_lookupIconPath($a_obj_id,$a_size)))
00083                                         {
00084                                                 return $path;
00085                                         }
00086                         }
00087                 }
00088                 
00089                 switch($a_size)
00090                 {
00091                         case 'tiny':
00092                                 $postfix = '_s.gif';
00093                                 break;
00094                         case 'big':
00095                                 $postfix = '_b.gif';
00096                                 break;
00097                         default:
00098                                 $postfix = '.gif';
00099                                 break;
00100                 }
00101                 return ilUtil::getImagePath('icon_'.$a_type.$postfix);
00102         }
00103 
00112         function getImagePath($img, $module_path = "", $mode = "output", $offline = false)
00113         {
00114                 global $ilias, $styleDefinition, $ilCtrl;
00115 
00116                 if ($module_path != "")
00117                 {
00118                         $module_path = "/".$module_path;
00119                 }
00120 
00121                 // default image
00122                 $default_img = ".".$module_path."/templates/default/images/".$img;
00123 
00124                 if (is_object($styleDefinition))
00125                 {
00126                         $image_dir = $styleDefinition->getImageDirectory($ilias->account->prefs["style"]);
00127                 }
00128                 if ($ilUser->skin == "default")
00129                 {
00130                         $user_img = ".".$module_path."/templates/default/".$image_dir."/".$img;
00131                         $skin_img = ".".$module_path."/templates/default/images/".$img;
00132                 }
00133                 else if (is_object($styleDefinition) && $ilUser->skin != "default")
00134                 {
00135                         $user_img = "./Customizing/global/skin/".
00136                                 $ilias->account->skin.$module_path."/".$image_dir."/".$img;
00137                         $skin_img = "./Customizing/global/skin/".
00138                                 $ilias->account->skin.$module_path."/images/".$img;
00139                 }
00140 
00141                 if ($offline)
00142                 {
00143                         return "./images/".$img;
00144                 }
00145                 else if (@file_exists($user_img) && $image_dir != "")
00146                 {
00147                         return $user_img;               // found image for skin and style
00148                 }
00149                 else if (file_exists($skin_img))
00150                 {
00151                         return $skin_img;               // found image in skin/images
00152                 }
00153 
00154                 return $default_img;                    // take image in default
00155         }
00156 
00165     function getHtmlPath($relative_path)
00166     {
00167         if (substr($relative_path, 0, 2) == './')
00168         {
00169             $relative_path = (substr($relative_path, 1));
00170         }
00171         if (substr($relative_path, 0, 1) != '/')
00172         {
00173             $relative_path = '/' . $relative_path;
00174         }
00175         $htmlpath = ILIAS_HTTP_PATH . $relative_path;
00176         return $htmlpath;
00177     }
00178 
00189         function getStyleSheetLocation($mode = "output", $a_css_name = "", $a_css_location = "")
00190         {
00191                 global $ilias;
00192 
00193                 // add version as parameter to force reload for new releases
00194                 $stylesheet_name = (strlen($a_css_name)) ? $a_css_name : $ilias->account->prefs["style"].".css";
00195                 if (strlen($a_css_location) && (strcmp(substr($a_css_location, -1), "/") != 0)) $a_css_location = $a_css_location . "/";
00196 
00197                 $filename = "";
00198                 if ($ilias->account->skin != "default")
00199                 {
00200                         $filename = "./Customizing/global/skin/".$ilias->account->skin."/".$a_css_location.$stylesheet_name;
00201                 }
00202                 if (strlen($filename) == 0 || !file_exists($filename))
00203                 {
00204                         $filename = "./" . $a_css_location . "templates/default/".$stylesheet_name;
00205                 }
00206                 $vers = "";
00207                 if ($mode != "filesystem")
00208                 {
00209                         $vers = str_replace(" ", "-", $ilias->getSetting("ilias_version"));
00210                         $vers = "?vers=".str_replace(".", "-", $vers);
00211                 }
00212                 return $filename . $vers;
00213         }
00214 
00220         function getP3PLocation()
00221         {
00222                 global $ilias;
00223 
00224                 if (defined("ILIAS_MODULE"))
00225                 {
00226                         $base = '';
00227                         for($i = 0;$i < count(explode('/',ILIAS_MODULE));$i++)
00228                         {
00229                                 $base .= "../Services/Privacy/";
00230                         }
00231                 }
00232                 else
00233                 {
00234                         $base = "./Services/Privacy/";
00235                 }
00236 
00237                 if (is_file($base."w3c/p3p.xml"))
00238                 {
00239                         return ILIAS_HTTP_PATH."w3c/p3p.xml";
00240                 }
00241                 else
00242                 {
00243                         return ILIAS_HTTP_PATH."/w3c/p3p_template.xml";
00244                 }
00245         }
00246 
00252         function getNewContentStyleSheetLocation($mode = "output")
00253         {
00254                 global $ilias;
00255 
00256                 // add version as parameter to force reload for new releases
00257                 if ($mode != "filesystem")
00258                 {
00259                         $vers = str_replace(" ", "-", $ilias->getSetting("ilias_version"));
00260                         $vers = "?vers=".str_replace(".", "-", $vers);
00261                 }
00262 
00263                 if ($ilias->account->skin == "default")
00264                 {
00265                         $in_style = "./templates/".$ilias->account->skin."/".$ilias->account->prefs["style"]."_cont.css";
00266                 }
00267                 else
00268                 {
00269                         $in_style = "./Customizing/global/skin/".$ilias->account->skin."/".$ilias->account->prefs["style"]."_cont.css";
00270                 }
00271 
00272                 if (is_file("./".$in_style))
00273                 {
00274                         return $base.$in_style.$vers;
00275                 }
00276                 else
00277                 {
00278                         return $base."templates/default/delos_cont.css".$vers;
00279                 }
00280         }
00281 
00298         function formSelect($selected,$varname,$options,$multiple = false,$direct_text = false, $size = "0",
00299                 $style_class = "", $attribs = "",$disabled = false)
00300         {
00301                 global $lng;
00302 
00303                 if ($multiple == true)
00304                 {
00305                         $multiple = " multiple=\"multiple\"";
00306                 }
00307                 else
00308                 {
00309                         $multiple = "";
00310                         $size = 0;
00311                 }
00312 
00313                 if ($style_class != "")
00314                 {
00315                         $class = " class=\"".$style_class."\"";
00316                 }
00317                 else
00318                 {
00319                         $class = "";
00320                 }
00321 
00322                 if (is_array($attribs))
00323                 {
00324                         foreach ($attribs as $key => $val)
00325                         {
00326                                 $attributes .= " ".$key."=\"".$val."\"";
00327                         }
00328                 }
00329                 if($disabled)
00330                 {
00331                         $disabled = ' disabled=\"disabled\"';
00332                 }
00333 
00334                 $str = "<select name=\"".$varname ."\"".$multiple." $class size=\"".$size."\" $attributes $disabled>\n";
00335 
00336                 foreach ($options as $key => $val)
00337                 {
00338                         if ($direct_text)
00339                         {
00340                                 $str .= " <option value=\"".$key."\"";
00341                         }
00342                         else
00343                         {
00344                                 $str .= " <option value=\"".$val."\"";
00345                         }
00346                         if (is_array($selected) )
00347                         {
00348                                 if (in_array($key,$selected))
00349                                 {
00350                                         $str .= " selected=\"selected\"";
00351                                 }
00352                         }
00353                         else if ($selected == $key)
00354                         {
00355                                 $str .= " selected=\"selected\"";
00356                         }
00357 
00358                         if ($direct_text)
00359                         {
00360                                 $str .= ">".$val."</option>\n";
00361                         }
00362                         else
00363                         {
00364                                 $str .= ">".$lng->txt($val)."</option>\n";
00365                         }
00366                 }
00367 
00368                 $str .= "</select>\n";
00369 
00370                 return $str;
00371         }
00372 
00380         function getSelectName ($selected,$values)
00381         {
00382                 return($values[$selected]);
00383         }
00384 
00394         function formCheckbox ($checked,$varname,$value,$disabled = false)
00395         {
00396                 $str = "<input type=\"checkbox\" name=\"".$varname."\"";
00397 
00398                 if ($checked == 1)
00399                 {
00400                         $str .= " checked=\"checked\"";
00401                 }
00402 
00403                 if ($disabled)
00404                 {
00405                         $str .= " disabled=\"disabled\"";
00406                 }
00407 
00408                 $array_var = false;
00409 
00410                 if (substr($varname,-2) == "[]")
00411                 {
00412                         $array_var = true;
00413                 }
00414 
00415                 // if varname ends with [], use varname[-2] + _ + value as id tag (e.g. "user_id[]" => "user_id_15")
00416                 if ($array_var)
00417                 {
00418                         $varname_id = substr($varname,0,-2)."_".$value;
00419                 }
00420                 else
00421                 {
00422                         $varname_id = $varname;
00423                 }
00424 
00425                 // dirty removal of other "[]" in string
00426                 $varname_id = ereg_replace("\[","_",$varname_id);
00427                 $varname_id = ereg_replace("\]","",$varname_id);
00428 
00429                 $str .= " value=\"".$value."\" id=\"".$varname_id."\" />\n";
00430 
00431                 return $str;
00432         }
00433 
00443         function formDisabledRadioButton($checked,$varname,$value,$disabled)
00444           {
00445             if ($disabled) {
00446               $str = "<input disabled type=\"radio\" name=\"".$varname."\"";
00447             }
00448             else {
00449               $str = "<input type=\"radio\" name=\"".$varname."\"";
00450             }
00451             if ($checked == 1)
00452               {
00453                 $str .= " checked=\"checked\"";
00454               }
00455 
00456             $str .= " value=\"".$value."\"";
00457             $str .= " id=\"".$value."\" />\n";
00458 
00459             return $str;
00460 
00461           }
00462 
00463 
00472         function formRadioButton($checked,$varname,$value)
00473         {
00474                 $str = "<input type=\"radio\" name=\"".$varname."\"";
00475                 if ($checked == 1)
00476                 {
00477                         $str .= " checked=\"checked\"";
00478                 }
00479 
00480                 $str .= " value=\"".$value."\"";
00481 
00482                 $str .= " id=\"".$value."\" />\n";
00483 
00484                 return $str;
00485         }
00486 
00487 
00496         function formInput($varname,$value,$disabled = false)
00497         {
00498 
00499             $str = "<input type=\"input\" name=\"".$varname."\"";
00500                 if ($disabled)
00501                 {
00502                         $str .= " disabled";
00503                 }
00504 
00505                 $str .= " value=\"".$value."\"";
00506 
00507                 $str .= " id=\"".$value."\" />\n";
00508 
00509                 return $str;
00510         }
00511 
00512 
00517         function checkInput ($vars)
00518         {
00519                 // TO DO:
00520                 // Diese Funktion soll Formfeldeingaben berprfen (empty und required)
00521         }
00522 
00528         function setPathStr ($a_path)
00529         {
00530                 if ("" != $a_path && "/" != substr($a_path, -1))
00531                 {
00532                         $a_path .= "/";
00533                         //$a_path = substr($a_path,1);
00534                 }
00535 
00536                 //return getcwd().$a_path;
00537                 return $a_path;
00538         }
00539 
00550         function switchColor ($a_num,$a_css1,$a_css2)
00551         {
00552                 if (!($a_num % 2))
00553                 {
00554                         return $a_css1;
00555                 }
00556                 else
00557                 {
00558                         return $a_css2;
00559                 }
00560         }
00561 
00571         function showTabs($a_hl, $a_o)
00572         {
00573                 global $lng;
00574 
00575                 $tpltab = new ilTemplate("tpl.tabs.html", true, true);
00576 
00577                 for ($i=1; $i<=4; $i++)
00578                 {
00579                         $tpltab->setCurrentBlock("tab");
00580                         if ($a_hl == $i)
00581                         {
00582                                 $tabtype = "tabactive";
00583                                 $tab = $tabtype;
00584                         }
00585                         else
00586                         {
00587                                 $tabtype = "tabinactive";
00588                                 $tab = "tab";
00589                         }
00590 
00591                         switch ($i)
00592                         {
00593                                 case 1:
00594                                 $txt = $lng->txt("view_content");
00595                                 break;
00596                                 case 2:
00597                                 $txt = $lng->txt("edit_properties");
00598                                 break;
00599                                 case 3:
00600                                 $txt = $lng->txt("perm_settings");
00601                                 break;
00602                                 case 4:
00603                                 $txt = $lng->txt("show_owner");
00604                                 break;
00605                         } // switch
00606                         $tpltab->setVariable("CONTENT", $txt);
00607                         $tpltab->setVariable("TABTYPE", $tabtype);
00608                         $tpltab->setVariable("TAB", $tab);
00609                         $tpltab->setVariable("LINK", $a_o["LINK".$i]);
00610                         $tpltab->parseCurrentBlock();
00611                 }
00612 
00613                 return $tpltab->get();
00614         }
00615 
00616 
00617 
00624         function checkFormEmpty ($emptyFields)
00625         {
00626 
00627                 $feedback = "";
00628 
00629                 foreach ($emptyFields as $key => $val)
00630                 {
00631                         if ($val == "") {
00632                                 if ($feedback != "") $feedback .= ", ";
00633                                 $feedback .= $key;
00634                         }
00635                 }
00636 
00637                 return $feedback;
00638         }
00639 
00662         function Linkbar ($AScript,$AHits,$ALimit,$AOffset,$AParams = array(),$ALayout = array())
00663         {
00664                 $LinkBar = "";
00665 
00666                 $layout_link = "";
00667                 $layout_prev = "&lt;&lt;";
00668                 $layout_next = "&gt;&gt;";
00669 
00670                 // layout options
00671                 if (count($ALayout > 0))
00672                 {
00673                         if ($ALayout["link"])
00674                         {
00675                                 $layout_link = " class=\"".$ALayout["link"]."\"";
00676                         }
00677 
00678                         if ($ALayout["prev"])
00679                         {
00680                                 $layout_prev = $ALayout["prev"];
00681                         }
00682 
00683                         if ($ALayout["next"])
00684                         {
00685                                 $layout_next = $ALayout["next"];
00686                         }
00687                 }
00688 
00689                 // show links, if hits greater limit
00690                 // or offset > 0 (can be > 0 due to former setting)
00691                 if ($AHits > $ALimit || $AOffset > 0)
00692                 {
00693                         if (!empty($AParams))
00694                         {
00695                                 foreach ($AParams as $key => $value)
00696                                 {
00697                                         $params.= $key."=".$value."&";
00698                                 }
00699                         }
00700                         // if ($params) $params = substr($params,0,-1);
00701                         if(strpos($AScript,'&'))
00702                         {
00703                                 $link = $AScript."&".$params."offset=";
00704                         }
00705                         else
00706                         {
00707                                 $link = $AScript."?".$params."offset=";
00708                         }
00709 
00710                         // ?bergehe "zurck"-link, wenn offset 0 ist.
00711                         if ($AOffset >= 1)
00712                         {
00713                                 $prevoffset = $AOffset - $ALimit;
00714                                 if ($prevoffset < 0) $prevoffset = 0;
00715                                 $LinkBar .= "<a".$layout_link." href=\"".$link.$prevoffset."\">".$layout_prev."&nbsp;</a>";
00716                         }
00717 
00718                         // Ben?tigte Seitenzahl kalkulieren
00719                         $pages=intval($AHits/$ALimit);
00720 
00721                         // Wenn ein Rest bleibt, addiere eine Seite
00722                         if (($AHits % $ALimit))
00723                         $pages++;
00724 
00725                         // Bei Offset = 0 keine Seitenzahlen anzeigen : DEAKTIVIERT
00726                         //                      if ($AOffset != 0) {
00727 
00728                         // ansonsten zeige Links zu den anderen Seiten an
00729                         for ($i = 1 ;$i <= $pages ; $i++)
00730                         {
00731                                 $newoffset=$ALimit*($i-1);
00732 
00733                                 if ($newoffset == $AOffset)
00734                                 {
00735                                         $LinkBar .= "[".$i."] ";
00736                                 }
00737                                 else
00738                                 {
00739                                         $LinkBar .= '<a '.$layout_link.' href="'.
00740                                                 $link.$newoffset.'">['.$i.']</a> ';
00741                                 }
00742                         }
00743                         //                      }
00744 
00745                         // Checken, ob letze Seite erreicht ist
00746                         // Wenn nicht, gebe einen "Weiter"-Link aus
00747                         if (! ( ($AOffset/$ALimit)==($pages-1) ) && ($pages!=1) )
00748                         {
00749                                 $newoffset=$AOffset+$ALimit;
00750                                 $LinkBar .= "<a".$layout_link." href=\"".$link.$newoffset."\">&nbsp;".$layout_next."</a>";
00751                         }
00752 
00753                         return $LinkBar;
00754                 }
00755                 else
00756                 {
00757                         return false;
00758                 }
00759         }
00760 
00770         function makeClickable($a_text, $detectGotoLinks = false)
00771         {
00772                 // New code, uses MediaWiki Sanitizer
00773                 $ret = $a_text;
00774                 
00775                 // www-URL ohne ://-Angabe
00776                 $ret = eregi_replace("([[:space:]]+)(www\.)([[:alnum:]#?/&=\.-]+)",
00777                         "\\1http://\\2\\3", $ret);
00778 
00779                 // ftp-URL ohne ://-Angabe
00780                 $ret = eregi_replace("([[:space:]]+)(ftp\.)([[:alnum:]#?/&=\.-]+)",
00781                         "\\1ftp://\\2\\3", $ret);
00782                 
00783                 // E-Mail (this does not work as expected, users must add mailto: manually)
00784                 //$ret = eregi_replace("(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))",
00785                 //      "mailto:\\1", $ret);
00786 
00787                 include_once("./Services/Utilities/classes/class.ilMWParserAdapter.php");
00788                 $parser = new ilMWParserAdapter();
00789                 $ret = $parser->replaceFreeExternalLinks($ret);
00790                         
00791                 //
00792                 // Old Behaviour is unsafe, Thanks to L4teral
00793                 //
00794                 /*
00795                         // URL mit ://-Angabe
00796                         $ret = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=-])",
00797                         "<a href=\"\\1://\\2\\3\" target=\"_blank\">\\1://\\2\\3</a>", $a_text);
00798         
00799                         // www-URL ohne ://-Angabe
00800                         $ret = eregi_replace("([[:space:]]+)(www\.)([[:alnum:]#?/&=\.-]+)",
00801                         "\\1<a href=\"http://\\2\\3\" target=\"_blank\">\\2\\3</a>", $ret);
00802         
00803                         // ftp-URL ohne ://-Angabe
00804                         $ret = eregi_replace("([[:space:]]+)(ftp\.)([[:alnum:]#?/&=\.-]+)",
00805                         "\\1<a href=\"ftp://\\2\\3\" target=\"_blank\">\\2\\3</a>", $ret);
00806         
00807                         // E-Mail
00808                         $ret = eregi_replace("(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))",
00809                         "<a  href=\"mailto:\\1\">\\1</a>", $ret);
00810                 }
00811                 */
00812 
00813                 // Should be Safe
00814                 
00815                 if ($detectGotoLinks)
00816                 // replace target blank with self and text with object title.
00817                 {
00818                         $regExp = "<a[^>]*href=\"(".str_replace("/","\/",ILIAS_HTTP_PATH)."\/goto.php\?target=\w+_(\d+)[^\"]*)\"[^>]*>[^<]*<\/a>";
00819 //                      echo htmlentities($regExp);
00820                         $ret = preg_replace_callback(
00821                                 "/".$regExp."/i",
00822                                 array("ilUtil", "replaceLinkProperties"),
00823                                 $ret);
00824 
00825                         // Static links
00826                         $regExp = "<a[^>]*href=\"(".str_replace("/","\/",ILIAS_HTTP_PATH)."\/goto_.*[a-z0-9]+_([0-9]+)\.html)\"[^>]*>[^<]*<\/a>";
00827 //                      echo htmlentities($regExp);
00828                         $ret = preg_replace_callback(
00829                                 "/".$regExp."/i",
00830                                 array("ilUtil", "replaceLinkProperties"),
00831                                 $ret);
00832                 }
00833 
00834                 return($ret);
00835         }
00836 
00848         function replaceLinkProperties ($matches)
00849         {
00850                 $link = $matches[0];
00851                 $ref_id = $matches[2];
00852 
00853                 if ($ref_id > 0)
00854                 {
00855                         $obj_id = ilObject::_lookupObjId($ref_id);
00856                         if ($obj_id > 0)
00857                         {
00858                                 $title = ilObject::_lookupTitle($obj_id);
00859                                 $link = "<a href=".$matches[1]." target=\"_self\">".$title."</a>";
00860                         }
00861                 }
00862                 return $link;
00863         }
00864 
00881         function StopWatch($begin = -1)
00882         {
00883                 $m = explode(" ",microtime());
00884                 $m = $m[0] + $m[1];
00885 
00886                 if ($begin != -1)
00887                 {
00888                         $m = $m - $begin;
00889                 }
00890 
00891                 return($m);
00892         }
00893 
00910         function makeDateSelect($prefix, $year = "", $month = "", $day = "", $startyear = "",$a_long_month = true,$a_further_options = array())
00911         {
00912                 global $lng;
00913 
00914                 $disabled = '';
00915                 if(isset($a_further_options['disabled']) and $a_further_options['disabled'])
00916                 {
00917                         $disabled = 'disabled="disabled" ';
00918                 }
00919 
00920                 $now = getdate();
00921                 if (!strlen($year)) $year = $now["year"];
00922                 if (!strlen($month)) $month = $now["mon"];
00923                 if (!strlen($day)) $day = $now["mday"];
00924 
00925                 $year = (int) $year;
00926                 $month = (int) $month;
00927                 $day = (int) $day;
00928 
00929                 // build day select
00930                 $sel_day .= "<select ".$disabled."name=\"".$prefix."[d]\" id=\"".$prefix."_d\">\n";
00931 
00932                 for ($i = 1; $i <= 31; $i++)
00933                 {
00934                         $sel_day .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
00935                 }
00936                 $sel_day .= "</select>\n";
00937                 $sel_day = preg_replace("/(value\=\"$day\")/", "$1 selected=\"selected\"", $sel_day);
00938 
00939                 // build month select
00940                 $sel_month .= "<select ".$disabled."name=\"".$prefix."[m]\" id=\"".$prefix."_m\">\n";
00941 
00942                 for ($i = 1; $i <= 12; $i++)
00943                 {
00944                         if($a_long_month)
00945                         {
00946                                 $sel_month .= "<option value=\"$i\">" . $lng->txt("month_" . sprintf("%02d", $i) . "_long") . "</option>\n";
00947                         }
00948                         else
00949                         {
00950                                 $sel_month .= "<option value=\"$i\">" . $i  . "</option>\n";
00951                         }
00952                 }
00953                 $sel_month .= "</select>\n";
00954                 $sel_month = preg_replace("/(value\=\"$month\")/", "$1 selected=\"selected\"", $sel_month);
00955 
00956                 // build year select
00957                 $sel_year .= "<select ".$disabled."name=\"".$prefix."[y]\" id=\"".$prefix."_y\">\n";
00958                 if ((strlen($startyear) == 0) || ($startyear > $year))
00959                 {
00960                         $startyear = $year - 5;
00961                 }
00962                 for ($i = $startyear; $i <= $year + 5; $i++)
00963                 {
00964                         $sel_year .= "<option value=\"$i\">" . sprintf("%04d", $i) . "</option>\n";
00965                 }
00966                 $sel_year .= "</select>\n";
00967                 $sel_year = preg_replace("/(value\=\"$year\")/", "$1 selected=\"selected\"", $sel_year);
00968 
00969                 $dateformat = $lng->text["lang_dateformat"];
00970                 $dateformat = strtolower(preg_replace("/\W/", "", $dateformat));
00971                 $dateformat = strtolower(preg_replace("/(\w)/", "%%$1", $dateformat));
00972                 $dateformat = preg_replace("/%%d/", $sel_day, $dateformat);
00973                 $dateformat = preg_replace("/%%m/", $sel_month, $dateformat);
00974                 $dateformat = preg_replace("/%%y/", $sel_year, $dateformat);
00975                 return $dateformat;
00976         }
00977 
00994         function makeTimeSelect($prefix, $short = true, $hour = "", $minute = "", $second = "",$a_use_default = true,$a_further_options = array())
00995         {
00996                 global $lng;
00997                 
00998                 $minute_steps = 1;
00999                 $disabled = '';
01000                 if(count($a_further_options))
01001                 {
01002                         if(isset($a_further_options['minute_steps']))
01003                         {
01004                                 $minute_steps = $a_further_options['minute_steps'];
01005                         }
01006                         if(isset($a_further_options['disabled']) and $a_further_options['disabled'])
01007                         {
01008                                 $disabled = 'disabled="disabled" ';
01009                         }
01010                 }
01011                 
01012                 if ($a_use_default and !strlen("$hour$minute$second")) {
01013                         $now = localtime();
01014                         $hour = $now[2];
01015                         $minute = $now[1];
01016                         $second = $now[0];
01017                 } else {
01018                         $hour = (int)$hour;
01019                         $minute = (int)$minute;
01020                         $second = (int)$second;
01021                 }
01022                 // build hour select
01023                 $sel_hour .= "<select ".$disabled."name=\"".$prefix."[h]\" id=\"".$prefix."_h\">\n";
01024 
01025                 for ($i = 0; $i <= 23; $i++)
01026                 {
01027                         $sel_hour .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
01028                 }
01029                 $sel_hour .= "</select>\n";
01030                 $sel_hour = preg_replace("/(value\=\"$hour\")/", "$1 selected=\"selected\"", $sel_hour);
01031 
01032                 // build minutes select
01033                 $sel_minute .= "<select ".$disabled."name=\"".$prefix."[m]\" id=\"".$prefix."_m\">\n";
01034 
01035                 for ($i = 0; $i <= 59; $i = $i + $minute_steps)
01036                 {
01037                         $sel_minute .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
01038                 }
01039                 $sel_minute .= "</select>\n";
01040                 $sel_minute = preg_replace("/(value\=\"$minute\")/", "$1 selected=\"selected\"", $sel_minute);
01041 
01042                 if (!$short) {
01043                         // build seconds select
01044                         $sel_second .= "<select ".$disabled."name=\"".$prefix."[s]\" id=\"".$prefix."_s\">\n";
01045 
01046                         for ($i = 0; $i <= 59; $i++)
01047                         {
01048                                 $sel_second .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
01049                         }
01050                         $sel_second .= "</select>\n";
01051                         $sel_second = preg_replace("/(value\=\"$second\")/", "$1 selected=\"selected\"", $sel_second);
01052                 }
01053                 $timeformat = $lng->text["lang_timeformat"];
01054                 if (strlen($timeformat) == 0) $timeformat = "H:i:s";
01055                 $timeformat = strtolower(preg_replace("/\W/", "", $timeformat));
01056                 $timeformat = preg_replace("/(\w)/", "%%$1", $timeformat);
01057                 $timeformat = preg_replace("/%%h/", $sel_hour, $timeformat);
01058                 $timeformat = preg_replace("/%%i/", $sel_minute, $timeformat);
01059                 if ($short) {
01060                         $timeformat = preg_replace("/%%s/", "", $timeformat);
01061                 } else {
01062                         $timeformat = preg_replace("/%%s/", $sel_second, $timeformat);
01063                 }
01064                 return $timeformat;
01065         }
01066 
01067         /*
01068         * This preg-based function checks whether an e-mail address is formally valid.
01069         * It works with all top level domains including the new ones (.biz, .info, .museum etc.)
01070         * and the special ones (.arpa, .int etc.)
01071         * as well as with e-mail addresses based on IPs (e.g. webmaster@123.45.123.45)
01072         * Valid top level domains: http://data.iana.org/TLD/tlds-alpha-by-domain.txt
01073         * @author       Unknown <mail@philipp-louis.de> (source: http://www.php.net/preg_match)
01074         * @access       public
01075         * @param        string  email address
01076         * @return       boolean true if valid
01077         */
01078         function is_email($a_email)
01079         {
01080                 $tlds = strtolower(
01081                         "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|".
01082                         "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|".
01083                         "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|".
01084                         "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|".
01085                         "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|".
01086                         "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|".
01087                         "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|".
01088                         "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|".
01089                         "WF|WS|XN|YE|YT|YU|ZA|ZM|ZW");
01090                 
01091                 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));
01092         }
01093 
01094         /*
01095         * validates a password
01096         * @access       public
01097         * @param        string  password
01098         * @return       boolean true if valid
01099         */
01100         function isPassword($a_passwd)
01101         {
01102                 if (empty($a_passwd))
01103                 {
01104                         return false;
01105                 }
01106 
01107                 if (strlen($a_passwd) < 6)
01108                 {
01109                         return false;
01110                 }
01111                 // due to bug in php does not work
01112                 //if (!ereg("^[A-Za-z0-9_\.\+\-\*\@!\$\%\~]+$", $a_passwd))
01113 
01114                 if (!preg_match("/^[A-Za-z0-9_\.\+\?\#\-\*\@!\$\%\~]+$/", $a_passwd))
01115                 {
01116                         return false;
01117                 }
01118 
01119                 return true;
01120         }
01121 
01122         /*
01123         * validates a login
01124         * @access       public
01125         * @param        string  login
01126         * @return       boolean true if valid
01127         */
01128         function isLogin($a_login)
01129         {
01130                 if (empty($a_login))
01131                 {
01132                         return false;
01133                 }
01134 
01135                 if (strlen($a_login) < 3)
01136                 {
01137                         return false;
01138                 }
01139 
01140                 // FIXME - If ILIAS is configured to use RFC 822
01141                 //         compliant mail addresses we should not
01142                 //         allow the @ character.
01143                 if (!ereg("^[A-Za-z0-9_\.\+\*\@!\$\%\~\-]+$", $a_login))
01144                 {
01145                         return false;
01146                 }
01147 
01148                 return true;
01149         }
01150 
01162         function shortenText ($a_str, $a_len, $a_dots = false, $a_next_blank = false)
01163         {
01164                 include_once("./Services/Utilities/classes/class.ilStr.php");
01165                 if (ilStr::strLen($a_str) > $a_len)
01166                 {
01167                         if ($a_next_blank)
01168                         {
01169                                 $len = ilStr::strPos($a_str, " ", $a_len);
01170                         }
01171                         else
01172                         {
01173                                 $len = $a_len;
01174                         }
01175                         $a_str = ilStr::subStr($a_str,0,$len);
01176 
01177                         if ($a_dots)
01178                         {
01179                                 $a_str .= "...";
01180                         }
01181                 }
01182 
01183                 return $a_str;
01184         }
01185 
01194         function shortenWords($a_str, $a_len = 30, $a_dots = true)
01195         {
01196                 include_once("./Services/Utilities/classes/class.ilStr.php");
01197                 $str_arr = explode(" ", $a_str);
01198                 
01199                 for ($i = 0; $i < count($str_arr); $i++)
01200                 {
01201                         if (ilStr::strLen($str_arr[$i]) > $a_len)
01202                         {
01203                                 $str_arr[$i] = ilStr::subStr($str_arr[$i], 0, $a_len);
01204                                 if ($a_dots)
01205                                 {
01206                                         $str_arr[$i].= "...";
01207                                 }
01208                         }
01209                 }
01210                 
01211                 return implode($str_arr, " ");
01212         }
01213 
01221         function attribsToArray($a_str)
01222         {
01223                 $attribs = array();
01224                 while (is_int(strpos($a_str, "=")))
01225                 {
01226                         $eq_pos = strpos($a_str, "=");
01227                         $qu1_pos = strpos($a_str, "\"");
01228                         $qu2_pos = strpos(substr($a_str, $qu1_pos + 1), "\"") + $qu1_pos + 1;
01229                         if (is_int($eq_pos) && is_int($qu1_pos) && is_int($qu2_pos))
01230                         {
01231                                 $var = trim(substr($a_str, 0, $eq_pos));
01232                                 $val = trim(substr($a_str, $qu1_pos + 1, ($qu2_pos - $qu1_pos) - 1));
01233                                 $attribs[$var] = $val;
01234                                 $a_str = substr($a_str, $qu2_pos + 1);
01235                         }
01236                         else
01237                         {
01238                                 $a_str = "";
01239                         }
01240                 }
01241                 return $attribs;
01242         }
01243 
01253         function rCopy ($a_sdir, $a_tdir, $preserveTimeAttributes = false)
01254         {
01255                 // check if arguments are directories
01256                 if (!@is_dir($a_sdir) or
01257                 !@is_dir($a_tdir))
01258                 {
01259                         return FALSE;
01260                 }
01261 
01262                 // read a_sdir, copy files and copy directories recursively
01263                 $dir = opendir($a_sdir);
01264 
01265                 while($file = readdir($dir))
01266                 {
01267                         if ($file != "." and
01268                         $file != "..")
01269                         {
01270                                 // directories
01271                                 if (@is_dir($a_sdir."/".$file))
01272                                 {
01273                                         if (!@is_dir($a_tdir."/".$file))
01274                                         {
01275                                                 if (!ilUtil::makeDir($a_tdir."/".$file))
01276                                                 return FALSE;
01277 
01278                                                 //chmod($a_tdir."/".$file, 0775);
01279                                         }
01280 
01281                                         if (!ilUtil::rCopy($a_sdir."/".$file,$a_tdir."/".$file))
01282                                         {
01283                                                 return FALSE;
01284                                         }
01285                                 }
01286 
01287                                 // files
01288                                 if (@is_file($a_sdir."/".$file))
01289                                 {
01290                                         if (!copy($a_sdir."/".$file,$a_tdir."/".$file))
01291                                         {
01292                                                 return FALSE;
01293                                         }
01294                                         if ($preserveTimeAttributes)
01295                                                 touch($a_tdir."/".$file, filectime($a_sdir."/".$file));
01296                                 }
01297                         }
01298                 }
01299                 return TRUE;
01300         }
01301 
01309         function getWebspaceDir($mode = "filesystem")
01310         {
01311                 global $ilias;
01312 
01313                 if ($mode == "filesystem")
01314                 {
01315                         return "./".ILIAS_WEB_DIR."/".$ilias->client_id;
01316                 }
01317                 else
01318                 {
01319                         if (defined("ILIAS_MODULE"))
01320                         {
01321                                 return "../".ILIAS_WEB_DIR."/".$ilias->client_id;
01322                         }
01323                         else
01324                         {
01325                                 return "./".ILIAS_WEB_DIR."/".$ilias->client_id;
01326                         }
01327                 }
01328 
01329                 //return $ilias->ini->readVariable("server","webspace_dir");
01330         }
01331 
01335         function getDataDir()
01336         {
01337                 return CLIENT_DATA_DIR;
01338                 //global $ilias;
01339 
01340                 //return $ilias->ini->readVariable("server", "data_dir");
01341         }
01342 
01350         function getUsersOnline($a_user_id = 0)
01351         {
01352                 global $ilias, $ilDB;
01353 
01354                 $pd_set = new ilSetting("pd");
01355                 $atime = $pd_set->get("user_activity_time") * 60;
01356                 $ctime = time();
01357                 
01358                 if ($a_user_id == 0)
01359                 {
01360                         $where = "WHERE user_id != 0 AND agree_date != '0000-00-00 00:00:00'";
01361                 }
01362                 else
01363                 {
01364                         $where = "WHERE user_id = ".$ilDB->quote($a_user_id)." ";
01365                 }
01366 
01367                 $q = "SELECT count(user_id) as num,user_id,firstname,lastname,title,login,last_login,ctime FROM usr_session ".
01368                 "LEFT JOIN usr_data ON user_id=usr_id ".$where.
01369                 "AND expires>UNIX_TIMESTAMP() ".
01370                 "GROUP BY user_id ".
01371                 "ORDER BY lastname, firstname";
01372                 $r = $ilias->db->query($q);
01373 
01374                 while ($user = $r->fetchRow(DB_FETCHMODE_ASSOC))
01375                 {
01376                         if ($atime <= 0
01377                                 || $user["ctime"] + $atime > $ctime)
01378                         {
01379                                 $users[$user["user_id"]] = $user;
01380                         }
01381                 }
01382 
01383                 return $users ? $users : array();
01384         }
01385 
01394         function getAssociatedUsersOnline($a_user_id)
01395         {
01396                 global $ilias, $ilDB;
01397 
01398                 $pd_set = new ilSetting("pd");
01399                 $atime = $pd_set->get("user_activity_time") * 60;
01400                 $ctime = time();
01401 
01402                 // The difference between active time and session time
01403                 $time_diff = 0;
01404 
01405                 // Get a list of object id's of all courses and groups for which
01406                 // the current user has local roles.
01407                 // Note: we have to use DISTINCT here, because a user may assume
01408                 // multiple roles in a group or a course.
01409                 $q = "SELECT DISTINCT dat.obj_id as obj_id ".
01410                 "FROM rbac_ua AS ua ".
01411                 "JOIN rbac_fa AS fa ON fa.rol_id = ua.rol_id ".
01412                 "JOIN object_reference AS r1 ON r1.ref_id = fa.parent ".
01413                 "JOIN tree ON tree.child = r1.ref_id ".
01414                 "JOIN object_reference AS r2 ON r2.ref_id = tree.parent ".
01415                 "JOIN object_data AS dat ON dat.obj_id = r2.obj_id ".
01416                 "WHERE ua.usr_id = ".$ilDB->quote($a_user_id)." ".
01417                 "AND fa.assign = 'y' ".
01418                 "AND dat.type IN ('grp','crs')";
01419                 $r = $ilias->db->query($q);
01420                 while ($row = $r->fetchRow(DB_FETCHMODE_ASSOC))
01421                 {
01422                         $groups_and_courses_of_user[] = $row["obj_id"];
01423                 }
01424 
01425                 // If the user is not in a course or a group, he has no associated users.
01426                 if (count($groups_and_courses_of_user) == 0)
01427                 {
01428                         $q = "SELECT count(user_id) as num,ctime,user_id,firstname,lastname,title,login,last_login ".
01429                         "FROM usr_session ".
01430                         "JOIN usr_data ON user_id=usr_id ".
01431                         "WHERE user_id = ".$ilDB->quote($a_user_id)." ".
01432                         " AND agree_date != '0000-00-00 00:00:00' ".
01433                         "AND expires > UNIX_TIMESTAMP() - ".$time_diff." ".
01434                         "GROUP BY user_id";
01435                 }
01436                 else
01437                 {
01438                         $q = "SELECT count(user_id) as num,s.ctime,s.user_id,ud.firstname,ud.lastname,ud.title,ud.login,ud.last_login ".
01439                         "FROM usr_session AS s ".
01440                         "JOIN usr_data AS ud ON ud.usr_id = s.user_id ".
01441                         "JOIN rbac_ua AS ua ON ua.usr_id = s.user_id ".
01442                         "JOIN rbac_fa AS fa ON fa.rol_id = ua.rol_id ".
01443                         "JOIN tree ON tree.child = fa.parent ".
01444                         "JOIN object_reference AS or1 ON or1.ref_id = tree.parent ".
01445                         "JOIN object_data AS od ON od.obj_id = or1.obj_id ".
01446                         "WHERE s.user_id != 0 ".
01447                         "AND s.expires > UNIX_TIMESTAMP() - ".$time_diff." ".
01448                         "AND fa.assign = 'y' ".
01449                         " AND ud.agree_date != '0000-00-00 00:00:00' ".
01450                         "AND od.obj_id IN (".implode(",",ilUtil::quoteArray($groups_and_courses_of_user)).") ".
01451                         "GROUP BY s.user_id ".
01452                         "ORDER BY ud.lastname, ud.firstname";
01453                 }
01454                 $r = $ilias->db->query($q);
01455 
01456                 while ($user = $r->fetchRow(DB_FETCHMODE_ASSOC))
01457                 {
01458                         if ($atime <= 0
01459                                 || $user["ctime"] + $atime > $ctime)
01460                         {
01461                                 $users[$user["user_id"]] = $user;
01462                         }
01463                 }
01464 
01465                 return $users ? $users : array();
01466         }
01467 
01473         function ilTempnam()
01474         {
01475                 $temp_path = ilUtil::getDataDir() . "/temp";
01476                 if (!is_dir($temp_path))
01477                 {
01478                         ilUtil::createDirectory($temp_path);
01479                 }
01480                 $temp_name = tempnam($temp_path, "tmp");
01481                 // --->
01482                 // added the following line because tempnam creates a backslash on some
01483                 // Windows systems which leads to problems, because the "...\tmp..." can be
01484                 // interpreted as "...{TAB-CHARACTER}...". The normal slash works fine
01485                 // even under windows (Helmut Schottmüller, 2005-08-31)
01486                 $temp_name = str_replace("\\", "/", $temp_name);
01487                 // --->
01488                 unlink($temp_name);
01489                 return $temp_name;
01490         }
01491 
01497         function createDirectory($a_dir, $a_mod = 0755)
01498         {
01499                 ilUtil::makeDir($a_dir);
01500                 //@mkdir($a_dir);
01501                 //@chmod($a_dir, $a_mod);
01502         }
01503 
01504 
01511         function unzip($a_file, $overwrite = false)
01512         {
01513                 //global $ilias;
01514 
01515                 $pathinfo = pathinfo($a_file);
01516                 $dir = $pathinfo["dirname"];
01517                 $file = $pathinfo["basename"];
01518 
01519                 // unzip
01520                 $cdir = getcwd();
01521                 chdir($dir);
01522                 $unzip = PATH_TO_UNZIP;
01523                 //$unzip = $ilias->getSetting("unzip_path");
01524 
01525                 // workaround for unzip problem (unzip of subdirectories fails, so
01526                 // we create the subdirectories ourselves first)
01527                 // get list
01528                 $unzipcmd = $unzip." -Z -1 ".ilUtil::escapeShellArg($file);
01529                 exec($unzipcmd, $arr);
01530                 $zdirs = array();
01531 
01532                 foreach($arr as $line)
01533                 {
01534                         if(is_int(strpos($line, "/")))
01535                         {
01536                                 $zdir = substr($line, 0, strrpos($line, "/"));
01537                                 $nr = substr_count($zdir, "/");
01538                                 //echo $zdir." ".$nr."<br>";
01539                                 while ($zdir != "")
01540                                 {
01541                                         $nr = substr_count($zdir, "/");
01542                                         $zdirs[$zdir] = $nr;                            // collect directories
01543                                         //echo $dir." ".$nr."<br>";
01544                                         $zdir = substr($zdir, 0, strrpos($zdir, "/"));
01545                                 }
01546                         }
01547                 }
01548 
01549                 asort($zdirs);
01550 
01551                 foreach($zdirs as $zdir => $nr)                         // create directories
01552                 {
01553                         ilUtil::createDirectory($zdir);
01554                 }
01555 
01556                 // real unzip
01557                 if ($overvwrite)
01558                 {
01559                         $unzipcmd = $unzip." ".ilUtil::escapeShellArg($file);
01560                 }
01561                 else
01562                 {
01563                         $unzipcmd = $unzip." -o ".ilUtil::escapeShellArg($file);
01564                 }
01565                 exec($unzipcmd);
01566 
01567                 chdir($cdir);
01568         }
01569 
01573         function zip($a_dir, $a_file)
01574         {
01575                 //global $ilias;
01576 
01577                 $cdir = getcwd();
01578 
01579                 $pathinfo = pathinfo($a_file);
01580                 $dir = $pathinfo["dirname"];
01581                 $file = $pathinfo["basename"];
01582 
01583                 // unzip
01584                 $cdir = getcwd();
01585                 chdir($dir);
01586 
01587                 $zip = PATH_TO_ZIP;
01588                 //$zip = $ilias->getSetting("zip_path");
01589                 
01590                 if(!$zip)
01591                 {
01592                         chdir($cdir);
01593                         return false;
01594                 }
01595 
01596                 if (is_array($a_dir))
01597                 {
01598                         $source = "";
01599                         foreach($a_dir as $dir)
01600                         {
01601                                 $name = basename($dir);
01602                                 $source.= " ".ilUtil::escapeShellArg($name);
01603                         }
01604                 }
01605                 else
01606                 {
01607                         $name = basename($a_dir);
01608                         $source = ilUtil::escapeShellArg($name);
01609                 }
01610 
01611                 $zipcmd = $zip." -r ".ilUtil::escapeShellArg($a_file)." ".$source;
01612                 exec($zipcmd);
01613 //echo htmlentities($zipcmd);
01614                 chdir($cdir);
01615                 return true;
01616         }
01617 
01621         function getConvertCmd()
01622         {
01623                 return PATH_TO_CONVERT;
01624                 //global $ilias;
01625 
01626                 //return $ilias->getSetting("convert_path");
01627         }
01628 
01636         function convertImage($a_from, $a_to, $a_target_format = "", $a_geometry = "")
01637         {
01638                 $format_str = ($a_target_format != "")
01639                 ? strtoupper($a_target_format).":"
01640                 : "";
01641                 $geometry = ($a_geometry != "")
01642                 ? " -geometry ".$a_geometry."x".$a_geometry." "
01643                 : "";
01644                 $convert_cmd = ilUtil::getConvertCmd()." ".
01645                 ilUtil::escapeShellArg($a_from)." ".$geometry.ilUtil::escapeShellArg($format_str.$a_to);
01646                 system($convert_cmd);
01647         }
01648 
01657         function resizeImage($a_from, $a_to, $a_width, $a_height)
01658         {
01659                 $size = " -resize ".$a_width."x".$a_height."! ";
01660                 $convert_cmd = ilUtil::getConvertCmd()." ".
01661                         ilUtil::escapeShellArg($a_from)." ".$size.ilUtil::escapeShellArg($a_to);
01662                 system($convert_cmd);
01663         }
01664 
01670         function html2pdf($html, $pdf_file)
01671         {
01672                 //global $ilias;
01673 
01674                 $html_file = str_replace(".pdf",".html",$pdf_file);
01675 
01676                 $fp = fopen( $html_file ,"wb");
01677                 fwrite($fp, $html);
01678                 fclose($fp);
01679 
01680                 $htmldoc_path = PATH_TO_HTMLDOC;
01681                 //$htmldoc_path = $ilias->getSetting("htmldoc_path");
01682 
01683                 $htmldoc = $htmldoc_path." ";
01684                 $htmldoc .= "--no-toc ";
01685                 $htmldoc .= "--no-jpeg ";
01686                 $htmldoc .= "--webpage ";
01687                 $htmldoc .= "--outfile " . ilUtil::escapeShellArg($pdf_file) . " ";
01688                 $htmldoc .= "--bodyfont Arial ";
01689                 $htmldoc .= "--charset iso-8859-15 ";
01690                 $htmldoc .= "--color ";
01691                 $htmldoc .= "--size A4  ";      // --landscape
01692                 $htmldoc .= "--format pdf ";
01693                 $htmldoc .= "--footer ... ";
01694                 $htmldoc .= "--header ... ";
01695                 $htmldoc .= "--left 60 ";
01696                 // $htmldoc .= "--right 200 ";
01697                 $htmldoc .= $html_file;
01698                 exec($htmldoc);
01699 
01700         }
01701 
01705         function deliverData($a_data, $a_filename, $mime = "application/octet-stream", $charset = "")
01706         {
01707                 $disposition = "attachment"; // "inline" to view file in browser or "attachment" to download to hard disk
01708                 //              $mime = "application/octet-stream"; // or whatever the mime type is
01709 
01710                 if (isset($_SERVER["HTTPS"])) {
01711 
01712                         // Added different handling for IE and HTTPS => send pragma after content informations
01716                         #header("Pragma: ");
01717                         #header("Cache-Control: ");
01718                         #header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
01719                         #header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
01720                         #header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
01721                         #header("Cache-Control: post-check=0, pre-check=0", false);
01722                 }
01723                 else if ($disposition == "attachment")
01724                 {
01725                         header("Cache-control: private");
01726                 }
01727                 else
01728                 {
01729                         header("Cache-Control: no-cache, must-revalidate");
01730                         header("Pragma: no-cache");
01731                 }
01732 
01733                 $ascii_filename = ilUtil::getASCIIFilename($a_filename);
01734 
01735                 if (strlen($charset))
01736                 {
01737                         $charset = "; charset=$charset";
01738                 }
01739                 header("Content-Type: $mime$charset");
01740                 header("Content-Disposition:$disposition; filename=\"".$ascii_filename."\"");
01741                 header("Content-Description: ".$ascii_filename);
01742                 header("Content-Length: ".(string)(strlen($a_data)));
01743 
01744                 if($_SERVER['HTTPS'])
01745                 {
01746             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
01747             header('Pragma: public');
01748                 }
01749 
01750                 header("Connection: close");
01751                 echo $a_data;
01752                 exit;
01753         }
01754 
01758         function deliverFile($a_file, $a_filename,$a_mime = '')
01759         {
01760                 $disposition = "attachment"; // "inline" to view file in browser or "attachment" to download to hard disk
01761 
01762                 if(strlen($a_mime))
01763                 {
01764                         $mime = $a_mime;
01765                 }
01766                 else
01767                 {
01768                         $mime = "application/octet-stream"; // or whatever the mime type is
01769                 }
01770                 if (isset($_SERVER["HTTPS"]))
01771                 {
01772                         // Added different handling for IE and HTTPS => send pragma after content informations
01773 
01777                         #header("Pragma: ");
01778                         #header("Pragma: no-cache");
01779                         #header("Cache-Control: ");
01780                         #header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
01781                         #header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
01782                         #header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
01783                         ##header("Cache-Control: post-check=0, pre-check=0", false);
01784                 }
01785                 else if ($disposition == "attachment")
01786                 {
01787                         header("Cache-control: private");
01788                 }
01789                 else
01790                 {
01791                         header("Cache-Control: no-cache, must-revalidate");
01792                         header("Pragma: no-cache");
01793                 }
01794 
01795                 $ascii_filename = ilUtil::getASCIIFilename($a_filename);
01796 
01797                 header("Content-Type: $mime");
01798                 header("Content-Disposition:$disposition; filename=\"".$ascii_filename."\"");
01799                 header("Content-Description: ".$ascii_filename);
01800                 header("Content-Length: ".(string)(filesize($a_file)));
01801 
01802                 if($_SERVER['HTTPS'])
01803                 {
01804             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
01805             header('Pragma: public');
01806                 }
01807 
01808                 header("Connection: close");
01809                 ilUtil::readFile( $a_file );
01810                 exit;
01811         }
01812 
01813 
01820         function readFile($a_file)
01821         {
01822                 $chunksize = 1*(1024*1024); // how many bytes per chunk
01823                 $buffer = '';
01824                 $handle = fopen($a_file, 'rb');
01825                 if ($handle === false)
01826                 {
01827                         return false;
01828                 }
01829                 while (!feof($handle))
01830                 {
01831                         $buffer = fread($handle, $chunksize);
01832                         print $buffer;
01833                 }
01834                 return fclose($handle);
01835         }
01836 
01842         function getASCIIFilename($a_filename)
01843         {
01844                 // The filename must be converted to ASCII, as of RFC 2183,
01845                 // section 2.3.
01846                 // Despite the RFC, Internet Explorer on Windows supports
01847                 // ISO 8895-1 encoding for the file name. We use this fact, to
01848                 // produce a better result, if the user uses IE.
01849 
01861 
01862                 $user_agent = strtolower($_SERVER["HTTP_USER_AGENT"]);
01863                 if ((is_integer(strpos($user_agent, "msie"))) && is_integer(strpos($user_agent, "win")))
01864                 {
01867 
01868                         $ascii_filename = utf8_decode($a_filename);
01869                 }
01870                 else
01871                 {
01874 
01875                         $ascii_filename = htmlentities($a_filename,ENT_NOQUOTES,'UTF-8');
01876                         $ascii_filename = preg_replace('/\&(.)[^;]*;/','\\1', $ascii_filename);
01877                         $ascii_filename = preg_replace('/[\x7f-\xff]/','_', $ascii_filename);
01878                 }
01879 
01880                 // Windows does not allow the following characters in filenames:
01881                 // \/:*?"<>|
01882                 if (is_integer(strpos($user_agent, "win")))
01883                 {
01884                         $ascii_filename = preg_replace('/[:\x5c\/\*\?\"<>\|]/','_', $ascii_filename);
01885                 }
01886 
01887                 return $ascii_filename;
01888         }
01889 
01893         function getJavaPath()
01894         {
01895                 return PATH_TO_JAVA;
01896                 //global $ilias;
01897 
01898                 //return $ilias->getSetting("java_path");
01899         }
01900 
01905         function appendUrlParameterString($a_url, $a_par)
01906         {
01907                 $url = (is_int(strpos($a_url, "?")))
01908                 ? $a_url."&".$a_par
01909                 : $a_url."?".$a_par;
01910 
01911                 return $url;
01912         }
01913 
01928         function makeDir($a_dir)
01929         {
01930                 $a_dir = trim($a_dir);
01931 
01932                 // remove trailing slash (bugfix for php 4.2.x)
01933                 if (substr($a_dir,-1) == "/")
01934                 {
01935                         $a_dir = substr($a_dir,0,-1);
01936                 }
01937 
01938                 // check if a_dir comes with a path
01939                 if (!($path = substr($a_dir,0, strrpos($a_dir,"/") - strlen($a_dir))))
01940                 {
01941                         $path = ".";
01942                 }
01943 
01944                 // create directory with file permissions of parent directory
01945                 umask(0000);
01946                 return @mkdir($a_dir,fileperms($path));
01947         }
01948 
01949 
01962         function makeDirParents($a_dir)
01963         {
01964                 $dirs = array($a_dir);
01965                 $a_dir = dirname($a_dir);
01966                 $last_dirname = '';
01967                 while($last_dirname != $a_dir)
01968                 {
01969                         array_unshift($dirs, $a_dir);
01970                         $last_dirname = $a_dir;
01971                         $a_dir = dirname($a_dir);
01972                 }
01973 
01974                 // find the first existing dir
01975                 $reverse_paths = array_reverse($dirs, TRUE);
01976                 $found_index = -1;
01977                 foreach ($reverse_paths as $key => $value)
01978                 {
01979                         if ($found_index == -1)
01980                         {
01981                                 if (is_dir($value))
01982                                 {
01983                                         $found_index = $key;
01984                                 }
01985                         }
01986                 }
01987 
01988                 umask(0000);
01989                 foreach ($dirs as $dirindex => $dir)
01990                 {
01991                         // starting with the longest existing path
01992                         if ($dirindex >= $found_index)
01993                         {
01994                                 if (! file_exists($dir))
01995                                 {
01996                                         if (strcmp(substr($dir,strlen($dir)-1,1),"/") == 0)
01997                                         {
01998                                                 // on some systems there is an error when there is a slash
01999                                                 // at the end of a directory in mkdir, see Mantis #2554
02000                                                 $dir = substr($dir,0,strlen($dir)-1);
02001                                         }
02002                                         if (! mkdir($dir, $umask))
02003                                         {
02004                                                 error_log("Can't make directory: $dir");
02005                                                 return false;
02006                                         }
02007                                 }
02008                                 elseif (! is_dir($dir))
02009                                 {
02010                                         error_log("$dir is not a directory");
02011                                         return false;
02012                                 }
02013                                 else
02014                                 {
02015                                         // get umask of the last existing parent directory
02016                                         $umask = fileperms($dir);
02017                                 }
02018                         }
02019                 }
02020                 return true;
02021         }
02022 
02030         function delDir($a_dir)
02031         {
02032                 if (!is_dir($a_dir) || is_int(strpos($a_dir, "..")))
02033                 {
02034                         return;
02035                 }
02036 
02037                 $current_dir = opendir($a_dir);
02038 
02039                 $files = array();
02040 
02041                 // this extra loop has been necessary because of a strange bug
02042                 // at least on MacOS X. A looped readdir() didn't work
02043                 // correctly with larger directories
02044                 // when an unlink happened inside the loop. Getting all files
02045                 // into the memory first solved the problem.
02046                 while($entryname = readdir($current_dir))
02047                 {
02048                         $files[] = $entryname;
02049                 }
02050 
02051                 foreach($files as $file)
02052                 {
02053                         if(is_dir($a_dir."/".$file) and ($file != "." and $file!=".."))
02054                         {
02055                                 ilUtil::delDir(${a_dir}."/".${file});
02056                         }
02057                         elseif ($file != "." and $file != "..")
02058                         {
02059                                 unlink(${a_dir}."/".${file});
02060                         }
02061                 }
02062 
02063                 closedir($current_dir);
02064                 @rmdir(${a_dir});
02065         }
02066 
02067 
02071         function getDir($a_dir)
02072         {
02073                 $current_dir = opendir($a_dir);
02074 
02075                 $dirs = array();
02076                 $files = array();
02077                 while($entry = readdir($current_dir))
02078                 {
02079                         if(is_dir($a_dir."/".$entry))
02080                         {
02081                                 $dirs[$entry] = array("type" => "dir", "entry" => $entry);
02082                         }
02083                         else
02084                         {
02085                                 $size = filesize($a_dir."/".$entry);
02086                                 $files[$entry] = array("type" => "file", "entry" => $entry,
02087                                 "size" => $size);
02088                         }
02089                 }
02090                 ksort($dirs);
02091                 ksort($files);
02092 
02093                 return array_merge($dirs, $files);
02094         }
02095 
02096 
02104         function getGroupId($a_parent_ref)
02105         {
02106                 return false;
02107 
02108                 global $ilias, $ilDB;
02109 
02110                 $q = "SELECT DISTINCT tree FROM grp_tree WHERE child=".
02111                         $ilDB->quote($a_parent_ref);
02112                 $r = $ilias->db->query($q);
02113                 $row = $r->fetchRow();
02114 
02115                 return $row[0] ? $row[0] : false;
02116         }
02117 
02123         function stripSlashes($a_str, $a_strip_html = true, $a_allow = "")
02124         {
02125                 if (ini_get("magic_quotes_gpc"))
02126                 {
02127                         $a_str = stripslashes($a_str);
02128                 }
02129                 
02130                 return ilUtil::secureString($a_str, $a_strip_html, $a_allow);
02131         }
02132                 
02136         function secureString($a_str, $a_strip_html = true, $a_allow = "")
02137         {
02138                 // check whether all allowed tags can be made secure
02139                 $only_secure = true;
02140                 $allow_tags = explode(">", $a_allow);
02141                 $sec_tags = ilUtil::getSecureTags();
02142                 $allow_array = array();
02143                 foreach($allow_tags as $allow)
02144                 {
02145                         if ($allow != "")
02146                         {
02147                                 $allow = str_replace("<", "", $allow);
02148 
02149                                 if (!in_array($allow, $sec_tags))
02150                                 {
02151                                         $only_secure = false;
02152                                 }
02153                                 $allow_array[] = $allow;
02154                         }
02155                 }
02156 
02157                 // default behaviour: allow only secure tags 1:1
02158                 if (($only_secure || $a_allow == "") && $a_strip_html)
02159                 {
02160                         if ($a_allow == "")
02161                         {
02162                                 $allow_array = array ("b", "i", "strong", "em", "code", "cite",
02163                                         "gap", "sub", "sup", "pre", "strike");
02164                         }
02165 
02166                         $a_str = ilUtil::maskSecureTags($a_str, $allow_array);
02167                         $a_str = strip_tags($a_str);            // strip all other tags
02168                         $a_str = ilUtil::unmaskSecureTags($a_str, $allow_array);
02169                 }
02170                 else
02171                 {
02172                         // only for scripts, that need to allow more/other tags and parameters
02173                         if ($a_strip_html)
02174                         {
02175                                 $a_str = ilUtil::stripScriptHTML($a_str, $a_allow);
02176                         }
02177                 }
02178 
02179                 return $a_str;
02180         }
02181 
02182         function getSecureTags()
02183         {
02184                 return array("strong", "em", "u", "strike", "ol", "li", "ul", "p", "div",
02185                         "i", "b", "code", "sup", "sub", "pre", "gap", "a", "img");
02186         }
02187 
02188         function maskSecureTags($a_str, $allow_array)
02189         {
02190                 foreach ($allow_array as $t)
02191                 {
02192                         switch($t)
02193                         {
02194                                 case "a":
02195                                         $a_str = ilUtil::maskAttributeTag($a_str, "a", "href");
02196                                         break;
02197                                         
02198                                 case "img":
02199                                         $a_str = ilUtil::maskAttributeTag($a_str, "img", "src");
02200                                         break;
02201 
02202                                 case "p":
02203                                 case "div":
02204                                         $a_str = ilUtil::maskTag($a_str, $t, array(
02205                                                 array("param" => "align", "value" => "left"),
02206                                                 array("param" => "align", "value" => "center"),
02207                                                 array("param" => "align", "value" => "justify"),
02208                                                 array("param" => "align", "value" => "right")
02209                                                 ));
02210                                         break;
02211 
02212                                 default:
02213                                         $a_str = ilUtil::maskTag($a_str, $t);
02214                                         break;
02215                         }
02216                 }
02217 
02218                 return $a_str;
02219         }
02220 
02221         function unmaskSecureTags($a_str, $allow_array)
02222         {
02223                 foreach ($allow_array as $t)
02224                 {
02225                         switch($t)
02226                         {
02227                                 case "a":
02228                                         $a_str = ilUtil::unmaskAttributeTag($a_str, "a", "href");
02229                                         break;
02230 
02231                                 case "img":
02232                                         $a_str = ilUtil::unmaskAttributeTag($a_str, "img", "src");
02233                                         break;
02234 
02235                                 case "p":
02236                                 case "div":
02237                                         $a_str = ilUtil::unmaskTag($a_str, $t, array(
02238                                                 array("param" => "align", "value" => "left"),
02239                                                 array("param" => "align", "value" => "center"),
02240                                                 array("param" => "align", "value" => "justify"),
02241                                                 array("param" => "align", "value" => "right")
02242                                                 ));
02243                                         break;
02244 
02245                                 default:
02246                                         $a_str = ilUtil::unmaskTag($a_str, $t);
02247                                         break;
02248                         }
02249                 }
02250 
02251                 return $a_str;
02252         }
02253 
02254         function maskAttributeTag($a_str, $tag, $tag_att)
02255         {
02256                 global $ilLog;
02257                 
02258                 $ws = "[ \t\r\f\v\n]*";
02259                 $att = $ws."[^>]*".$ws;
02260                 
02261                 while (eregi("<($tag$att($tag_att$ws=$ws\"(([\$@!*()~;,_0-9A-z/:=%\\.&#?+\\-])*)\")$att)>",
02262                         $a_str, $found))
02263                 {
02264                         $un = array(".", "-", "+", "?", '$', "*", "(", ")");
02265                         $esc = array();
02266                         foreach($un as $v)
02267                         {
02268                                 $esc[] = "\\".$v;
02269                         }
02270                         $ff = str_replace($un, $esc, $found[1]);
02271                         
02272                         $old_str = $a_str;
02273                         $a_str = eregi_replace("<".$ff.">",
02274                                 "&lt;$tag $tag_att$tag_att=\"".$found[3]."\"&gt;", $a_str);
02275                         if ($old_str == $a_str)
02276                         {
02277                                 $ilLog->write("ilUtil::maskA-".htmlentities($old_str)." == ".
02278                                         htmlentities($a_str));
02279                                 return $a_str;
02280                         }
02281                 }
02282                 $a_str = str_ireplace("</$tag>",
02283                         "&lt;/$tag&gt;", $a_str);
02284                 return $a_str;
02285         }
02286 
02287         function unmaskAttributeTag($a_str, $tag, $tag_att)
02288         {
02289                 global $ilLog;
02290                 
02291                 while (eregi("&lt;($tag $tag_att$tag_att=\"(([\$@!*()~;,_0-9A-z/:=%\\.&#?+\\-])*)\")&gt;",
02292                         $a_str, $found))
02293                 {
02294                         $un = array(".", "-", "+", "?", '$', "*", "(", ")");
02295                         $esc = array();
02296                         foreach($un as $v)
02297                         {
02298                                 $esc[] = "\\".$v;
02299                         }
02300                         $ff = str_replace($un, $esc, $found[1]);
02301                         
02302                         $old_str = $a_str;
02303                         $a_str = eregi_replace("&lt;".$ff."&gt;",
02304                                 "<$tag $tag_att=\"".ilUtil::secureLink($found[2])."\">", $a_str);
02305                         if ($old_str == $a_str)
02306                         {
02307                                 $ilLog->write("ilUtil::unmaskA-".htmlentities($old_str)." == ".
02308                                         htmlentities($a_str));
02309                                 return $a_str;
02310                         }
02311                 }
02312                 $a_str = str_replace("&lt;/$tag&gt;", "</$tag>", $a_str);
02313                 return $a_str;
02314         }
02315         
02316         function maskTag($a_str, $t, $fix_param = "")
02317         {
02318                 $a_str = str_replace(array("<$t>", "<".strtoupper($t).">"),
02319                         "&lt;".$t."&gt;", $a_str);
02320                 $a_str = str_replace(array("</$t>", "</".strtoupper($t).">"),
02321                         "&lt;/".$t."&gt;", $a_str);
02322 
02323                 if (is_array($fix_param))
02324                 {
02325                         foreach ($fix_param      as $p)
02326                         {
02327                                 $k = $p["param"];
02328                                 $v = $p["value"];
02329                                 $a_str = str_replace("<$t $k=\"$v\">",
02330                                         "&lt;"."$t $k=\"$v\""."&gt;", $a_str);
02331                         }
02332                 }
02333 
02334                 return $a_str;
02335         }
02336 
02337         function unmaskTag($a_str, $t, $fix_param = "")
02338         {
02339                 $a_str = str_replace("&lt;".$t."&gt;", "<".$t.">", $a_str);
02340                 $a_str = str_replace("&lt;/".$t."&gt;", "</".$t.">", $a_str);
02341 
02342                 if (is_array($fix_param))
02343                 {
02344                         foreach ($fix_param      as $p)
02345                         {
02346                                 $k = $p["param"];
02347                                 $v = $p["value"];
02348                                 $a_str = str_replace("&lt;$t $k=\"$v\"&gt;",
02349                                         "<"."$t $k=\"$v\"".">", $a_str);
02350                         }
02351                 }
02352                 return $a_str;
02353         }
02354         
02355         function secureLink($a_str)
02356         {
02357                 $a_str = str_ireplace("javascript", "jvscrpt", $a_str);
02358                 $a_str = str_ireplace(array("%00", "%0a", "%0d", "%1a", "&#00;", "&#x00;",
02359                         "&#0;", "&#x0;", "&#x0a;", "&#x0d;", "&#10;", "&#13;"), "-", $a_str);
02360                 return $a_str;
02361         }
02362 
02374         function stripScriptHTML($a_str, $a_allow = "", $a_rm_js = true)
02375         {
02376                 //$a_str = strip_tags($a_str, $a_allow);
02377 
02378                 $negativestr = "a,abbr,acronym,address,applet,area,b,base,basefont,".
02379                         "bdo,big,blockquote,body,br,button,caption,center,cite,code,col,".
02380                         "colgroup,dd,del,dfn,dir,div,dl,dt,em,fieldset,font,form,frame,".
02381                         "frameset,h1,h2,h3,h4,h5,h6,head,hr,html,i,iframe,img,input,ins,isindex,kbd,".
02382                         "label,legend,li,link,map,menu,meta,noframes,noscript,object,ol,".
02383                         "optgroup,option,p,param,q,s,samp,script,select,small,span,".
02384                         "strike,strong,style,sub,sup,table,tbody,td,textarea,tfoot,th,thead,".
02385                         "title,tr,tt,u,ul,var";
02386                 $a_allow = strtolower ($a_allow);
02387                 $negatives = split (",",$negativestr);
02388                 $outer_old_str = "";
02389                 while($outer_old_str != $a_str)
02390                 {
02391                         $outer_old_str = $a_str;
02392                         foreach ($negatives as $item)
02393                         {
02394                                 $pos = strpos($a_allow, "<$item>");
02395 
02396                                 // remove complete tag, if not allowed
02397                                 if ($pos === false)
02398                                 {
02399                                         $old_str = "";
02400                                         while($old_str != $a_str)
02401                                         {
02402                                                 $old_str = $a_str;
02403                                                 $a_str = preg_replace("/<\/?\s*$item(\/?)\s*>/i", "", $a_str);
02404                                                 $a_str = preg_replace("/<\/?\s*$item(\/?)\s+([^>]*)>/i", "", $a_str);
02405                                         }
02406                                 }
02407                         }
02408                 }
02409 
02410                 if ($a_rm_js)
02411                 {
02412                         // remove all attributes if an "on..." attribute is given
02413                         $a_str = preg_replace("/<\s*\w*(\/?)(\s+[^>]*)?(\s+on[^>]*)>/i", "", $a_str);
02414 
02415                         // remove all attributes if a "javascript" is within tag
02416                         $a_str = preg_replace("/<\s*\w*(\/?)\s+[^>]*javascript[^>]*>/i", "", $a_str);
02417 
02418                         // remove all attributes if an "expression" is within tag
02419                         // (IE allows something like <b style='width:expression(alert(1))'>test</b>)
02420                         $a_str = preg_replace("/<\s*\w*(\/?)\s+[^>]*expression[^>]*>/i", "", $a_str);
02421                 }
02422 
02423                 return $a_str;
02424         }
02425 
02426 
02432         function addSlashes($a_str)
02433         {
02434                 if (ini_get("magic_quotes_gpc"))
02435                 {
02436                         return $a_str;
02437                 }
02438                 else
02439                 {
02440                         return addslashes($a_str);
02441                 }
02442         }
02443 
02453         function prepareFormOutput($a_str, $a_strip = false)
02454         {
02455                 if($a_strip)
02456                 {
02457                         $a_str = ilUtil::stripSlashes($a_str);
02458                 }
02459                 $a_str = htmlspecialchars($a_str);
02460                 // Added replacement of curly brackets to prevent
02461                 // problems with PEAR templates, because {xyz} will
02462                 // be removed as unused template variable
02463                 $a_str = str_replace("{", "&#123;", $a_str);
02464                 $a_str = str_replace("}", "&#125;", $a_str);
02465                 // needed for LaTeX conversion \\ in LaTeX is a line break
02466                 // but without this replacement, php changes \\ to \
02467                 $a_str = str_replace("\\", "&#92;", $a_str);
02468                 return $a_str;
02469         }
02470 
02471 
02479         function prepareDBString($a_str)
02480         {
02481                 return addslashes($a_str);
02482         }
02483 
02484 
02491         function removeItemFromDesktops($a_id)
02492         {
02493                 global $ilDB;
02494 
02495                 $q = "SELECT user_id FROM desktop_item WHERE item_id = ".$ilDB->quote($a_id);
02496                 $r = $ilDB->query($q);
02497 
02498                 $users = array();
02499 
02500                 while ($row = $r->fetchRow(DB_FETCHMODE_OBJECT))
02501                 {
02502                         $users[] = $row->user_id;
02503                 } // while
02504 
02505                 if (count($users) > 0)
02506                 {
02507                         $q = "DELETE FROM desktop_item WHERE item_id = ".
02508                                 $ilDB->quote($a_id);
02509                         $ilDB->query($q);
02510                 }
02511 
02512                 return $users;
02513         }
02514 
02515 
02523         function extractParameterString($a_parstr)
02524         {
02525                 // parse parameters in array
02526                 $par = array();
02527                 $ok=true;
02528                 while(($spos=strpos($a_parstr,"=")) && $ok)
02529                 {
02530                         // extract parameter
02531                         $cpar = substr($a_parstr,0,$spos);
02532                         $a_parstr = substr($a_parstr,$spos,strlen($a_parstr)-$spos);
02533                         while(substr($cpar,0,1)=="," ||substr($cpar,0,1)==" " || substr($cpar,0,1)==chr(13) || substr($cpar,0,1)==chr(10))
02534                         $cpar = substr($cpar,1,strlen($cpar)-1);
02535                         while(substr($cpar,strlen($cpar)-1,1)==" " || substr($cpar,strlen($cpar)-1,1)==chr(13) || substr($cpar,strlen($cpar)-1,1)==chr(10))
02536                         $cpar = substr($cpar,0,strlen($cpar)-1);
02537                         
02538                         // parameter name should only
02539                         $cpar_old = "";
02540                         while($cpar != $cpar_old)
02541                         {
02542                                 $cpar_old = $cpar;
02543                                 $cpar = eregi_replace("[^a-zA-Z0-9_]", "", $cpar);
02544                         }
02545 
02546                         // extract value
02547                         if ($cpar != "")
02548                         {
02549                                 if($spos=strpos($a_parstr,"\""))
02550                                 {
02551                                         $a_parstr = substr($a_parstr,$spos+1,strlen($a_parstr)-$spos);
02552                                         $spos=strpos($a_parstr,"\"");
02553                                         if(is_int($spos))
02554                                         {
02555                                                 $cval = substr($a_parstr,0,$spos);
02556                                                 $par[$cpar]=$cval;
02557                                                 $a_parstr = substr($a_parstr,$spos+1,strlen($a_parstr)-$spos-1);
02558                                         }
02559                                         else
02560                                         $ok=false;
02561                                 }
02562                                 else
02563                                 $ok=false;
02564                         }
02565                 }
02566 
02567                 if($ok) return $par; else return false;
02568         }
02569 
02570         function assembleParameterString($a_par_arr)
02571         {
02572                 if (is_array($a_par_arr))
02573                 {
02574                         $target_arr = array();
02575                         foreach ($a_par_arr as $par => $val)
02576                         {
02577                                 $target_arr[] = "$par=\"$val\"";
02578                         }
02579                         $target_str = implode(", ", $target_arr);
02580                 }
02581 
02582                 return $target_str;
02583         }
02584 
02588         function dumpString($a_str)
02589         {
02590                 $ret = $a_str.": ";
02591                 for($i=0; $i<strlen($a_str); $i++)
02592                 {
02593                         $ret.= ord(substr($a_str,$i,1))." ";
02594                 }
02595                 return $ret;
02596         }
02597 
02598 
02602         function yn2tf($a_yn)
02603         {
02604                 if(strtolower($a_yn) == "y")
02605                 {
02606                         return true;
02607                 }
02608                 else
02609                 {
02610                         return false;
02611                 }
02612         }
02613 
02617         function tf2yn($a_tf)
02618         {
02619                 if($a_tf)
02620                 {
02621                         return "y";
02622                 }
02623                 else
02624                 {
02625                         return "n";
02626                 }
02627         }
02628 
02637         function sort_func ($a, $b)
02638         {
02639                 global $array_sortby,$array_sortorder;
02640 
02641                 if ($array_sortorder == "asc")
02642                 {
02643                         return strnatcasecmp($a[$array_sortby], $b[$array_sortby]);
02644                 }
02645 
02646                 if ($array_sortorder == "desc")
02647                 {
02648                         return strnatcasecmp($b[$array_sortby], $a[$array_sortby]);
02649                 }
02650         }
02651 
02660         function sort_func_numeric ($a, $b)
02661         {
02662                 global $array_sortby,$array_sortorder;
02663 
02664                 if ($array_sortorder == "asc")
02665                 {
02666                         return $a["$array_sortby"] > $b["$array_sortby"];
02667                 }
02668 
02669                 if ($array_sortorder == "desc")
02670                 {
02671                         return $a["$array_sortby"] < $b["$array_sortby"];
02672                 }
02673         }
02684         function sortArray($array,$a_array_sortby,$a_array_sortorder = 0,$a_numeric = false,
02685                 $a_keep_keys = false)
02686         {
02687                 global $array_sortby,$array_sortorder;
02688 
02689                 $array_sortby = $a_array_sortby;
02690 
02691                 if ($a_array_sortorder == "desc")
02692                 {
02693                         $array_sortorder = "desc";
02694                 }
02695                 else
02696                 {
02697                         $array_sortorder = "asc";
02698                 }
02699                 if($a_numeric)
02700                 {
02701                         if ($a_keep_keys)
02702                         {
02703                                 uasort($array, array("ilUtil", "sort_func_numeric"));
02704                         }
02705                         else
02706                         {
02707                                 usort($array, array("ilUtil", "sort_func_numeric"));
02708                         }
02709                 }
02710                 else
02711                 {
02712                         if ($a_keep_keys)
02713                         {
02714                                 uasort($array, array("ilUtil", "sort_func"));
02715                         }
02716                         else
02717                         {
02718                                 usort($array, array("ilUtil", "sort_func"));
02719                         }
02720                 }
02721                 //usort($array,"ilUtil::sort_func");
02722 
02723                 return $array;
02724         }
02725 
02735         function unique_multi_array($array, $sub_key)
02736         {
02737                 $target = array();
02738                 $existing_sub_key_values = array();
02739 
02740                 foreach ($array as $key=>$sub_array)
02741                 {
02742                         if (!in_array($sub_array[$sub_key], $existing_sub_key_values))
02743                         {
02744                                 $existing_sub_key_values[] = $sub_array[$sub_key];
02745                                 $target[$key] = $sub_array;
02746                         }
02747                 }
02748 
02749                 return $target;
02750         }
02751 
02752 
02760         function getGDSupportedImageType($a_desired_type)
02761         {
02762                 $a_desired_type = strtolower($a_desired_type);
02763                 // get supported Image Types
02764                 $im_types = ImageTypes();
02765 
02766                 switch($a_desired_type)
02767                 {
02768                         case "jpg":
02769                         if ($im_types & IMG_JPG) return "jpg";
02770                         if ($im_types & IMG_GIF) return "gif";
02771                         if ($im_types & IMG_PNG) return "png";
02772                         break;
02773 
02774                         case "gif":
02775                         if ($im_types & IMG_GIF) return "gif";
02776                         if ($im_types & IMG_JPG) return "jpg";
02777                         if ($im_types & IMG_PNG) return "png";
02778                         break;
02779 
02780                         case "png":
02781                         if ($im_types & IMG_PNG) return "png";
02782                         if ($im_types & IMG_JPG) return "jpg";
02783                         if ($im_types & IMG_GIF) return "gif";
02784                         break;
02785                 }
02786 
02787                 return "";
02788         }
02789 
02797         function deducibleSize($a_mime)
02798         {
02799                 if (($a_mime == "image/gif") || ($a_mime == "image/jpeg") ||
02800                 ($a_mime == "image/png") || ($a_mime == "application/x-shockwave-flash") ||
02801                 ($a_mime == "image/tiff") || ($a_mime == "image/x-ms-bmp") ||
02802                 ($a_mime == "image/psd") || ($a_mime == "image/iff"))
02803                 {
02804                         return true;
02805                 }
02806                 else
02807                 {
02808                         return false;
02809                 }
02810         }
02811 
02812 
02818         function redirect($a_script)
02819         {
02820                 global $log, $PHP_SELF;
02821 //$log->write("redirect FROM:".$PHP_SELF." to ".$a_script);
02822                         //vd($a_script);
02823                 header("Location: ".$a_script);
02824                 exit();
02825         }
02826 
02832         function insertInstIntoID($a_value)
02833         {
02834                 if (substr($a_value, 0, 4) == "il__")
02835                 {
02836                         $a_value = "il_".IL_INST_ID."_".substr($a_value, 4, strlen($a_value) - 4);
02837                 }
02838 
02839                 return $a_value;
02840         }
02841 
02850         function groupNameExists($a_group_name,$a_id = 0)
02851         {
02852                 global $ilDB,$ilErr;
02853 
02854                 if (empty($a_group_name))
02855                 {
02856                         $message = get_class($this)."::_NameExists(): No groupname given!";
02857                         $ilErr->raiseError($message,$ilErr->WARNING);
02858                 }
02859 
02860                 $clause = ($a_id) ? " AND obj_id != ".$ilDB->quote($a_id)." " : "";
02861 
02862                 $q = "SELECT obj_id FROM object_data ".
02863                 "WHERE title = ".$ilDB->quote($a_group_name)." ".
02864                 "AND type = 'grp'".
02865                 $clause;
02866 
02867                 $r = $ilDB->query($q);
02868 
02869                 if ($r->numRows())
02870                 {
02871                         return true;
02872                 }
02873                 else
02874                 {
02875                         return false;
02876                 }
02877         }
02878 
02879         /*
02880         * get the user_ids which correspond a search string
02881         * static function
02882         * @param        string search string
02883         * @access       public
02884         */
02885         function searchGroups($a_search_str)
02886         {
02887                 global $ilDB;
02888 
02889                 $q = "SELECT * ".
02890                         "FROM object_data ,object_reference ".
02891                         "WHERE (object_data.title LIKE ".$ilDB->quote("%".$a_search_str."%")." ".
02892                         "OR object_data.description LIKE ".$ilDB->quote("%".$a_search_str."%").") ".
02893                         "AND object_data.type = 'grp' ".
02894                         "AND object_data.obj_id = object_reference.obj_id ".
02895                         "ORDER BY title ";
02896 
02897                 $res = $ilDB->query($q);
02898 
02899                 while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
02900                 {
02901                         // STORE DATA IN ARRAY WITH KEY obj_id
02902                         // SO DUPLICATE ENTRIES ( LINKED OBJECTS ) ARE UNIQUE
02903                         $ids[$row->obj_id] = array(
02904                         "ref_id"        => $row->ref_id,
02905                         "title"         => $row->title,
02906                         "description"   => $row->description);
02907                 }
02908 
02909                 return $ids ? $ids : array();
02910         }
02911 
02915         function getMemString()
02916         {
02917                 $my_pid = getmypid();
02918                 return ("MEMORY USAGE (% KB PID ): ".`ps -eo%mem,rss,pid | grep $my_pid`);
02919         }
02920 
02924         function isWindows()
02925         {
02926                 if (strtolower(substr(php_uname(), 0, 3)) == "win")
02927                 {
02928                         return true;
02929                 }
02930                 return false;
02931         }
02932 
02933         function escapeShellArg($a_arg)
02934         {
02935                 global $PHP_OS;
02936 
02937                 if (ini_get("safe_mode") == 1 || ilUtil::isWindows())
02938                 {
02939                         return $a_arg;
02940                 }
02941                 else
02942                 {
02943                         return escapeshellarg($a_arg);
02944                 }
02945         }
02946         
02954         public function escapeShellCmd($a_arg)
02955         {
02956                 if(ini_get('safe_mode') == 1)
02957                 {
02958                         return $a_arg;
02959                 }
02960                 setlocale(LC_CTYPE, "UTF8", "en_US.UTF-8"); // fix for PHP escapeshellcmd bug. See: http://bugs.php.net/bug.php?id=45132
02961                 return escapeshellcmd($a_arg);
02962         }
02963         
02964 
02965         /*
02966         * Calculates a Microsoft Excel date/time value
02967         *
02968         * Calculates a Microsoft Excel date/time value (nr of days after 1900/1/1 0:00) for
02969         * a given date and time. The function only accepts dates after 1970/1/1, because the
02970         * unix timestamp functions used in the function are starting with that date.
02971         * If you don't enter parameters the date/time value for the actual date/time
02972         * will be calculated.
02973         *
02974         * static function
02975         *
02976         * @param        integer $year Year
02977         * @param        integer $month Month
02978         * @param        integer $day Day
02979         * @param        integer $hour Hour
02980         * @param        integer $minute Minute
02981         * @param        integer $second Second
02982         * @return float The Microsoft Excel date/time value
02983         * @access       public
02984         */
02985         function excelTime($year = "", $month = "", $day = "", $hour = "", $minute = "", $second = "")
02986         {
02987                 $starting_time = mktime(0, 0, 0, 1, 2, 1970);
02988                 if (strcmp("$year$month$day$hour$minute$second", "") == 0)
02989                 {
02990                         $target_time = time();
02991                 }
02992                 else
02993                 {
02994                         if ($year < 1970)
02995                         {
02996                                 return 0;
02997                         }
02998                 }
02999                 $target_time = mktime($hour, $minute, $second, $month, $day, $year);
03000                 $difference = $target_time - $starting_time;
03001                 $days = (($difference - ($difference % 86400)) / 86400);
03002                 $difference = $difference - ($days * 86400) + 3600;
03003                 return ($days + 25570 + ($difference / 86400));
03004         }
03005 
03009         function renameExecutables($a_dir)
03010         {
03011                 $def_arr = explode(",", SUFFIX_REPL_DEFAULT);
03012                 foreach ($def_arr as $def)
03013                 {
03014                         ilUtil::rRenameSuffix($a_dir, trim($def), "sec");
03015                 }
03016 
03017                 $def_arr = explode(",", SUFFIX_REPL_ADDITIONAL);
03018                 foreach ($def_arr as $def)
03019                 {
03020                         ilUtil::rRenameSuffix($a_dir, trim($def), "sec");
03021                 }
03022         }
03023 
03034         function rRenameSuffix ($a_dir, $a_old_suffix, $a_new_suffix)
03035         {
03036                 if ($a_dir == "/" || $a_dir == "" || is_int(strpos($a_dir, ".."))
03037                         || trim($a_old_suffix) == "")
03038                 {
03039                         return false;
03040                 }
03041 
03042                 // check if argument is directory
03043                 if (!@is_dir($a_dir))
03044                 {
03045                         return false;
03046                 }
03047 
03048                 // read a_dir
03049                 $dir = opendir($a_dir);
03050 
03051                 while($file = readdir($dir))
03052                 {
03053                         if ($file != "." and
03054                         $file != "..")
03055                         {
03056                                 // directories
03057                                 if (@is_dir($a_dir."/".$file))
03058                                 {
03059                                         ilUtil::rRenameSuffix($a_dir."/".$file, $a_old_suffix, $a_new_suffix);
03060                                 }
03061 
03062                                 // files
03063                                 if (@is_file($a_dir."/".$file))
03064                                 {
03065                                         // first check for files with trailing dot
03066                                         if(strrpos($file,'.') == (strlen($file) - 1))
03067                                         {
03068                                                 rename($a_dir.'/'.$file,substr($a_dir.'/'.$file,0,-1));
03069                                                 $file = substr($file,0,-1);
03070                                         } 
03071 
03072                                         $path_info = pathinfo($a_dir."/".$file);
03073                                         
03074                                         if (strtolower($path_info["extension"]) ==
03075                                         strtolower($a_old_suffix))
03076                                         {
03077                                                 $pos = strrpos($a_dir."/".$file, ".");
03078                                                 $new_name = substr($a_dir."/".$file, 0, $pos).".".$a_new_suffix;
03079                                                 rename($a_dir."/".$file, $new_name);
03080                                         }
03081                                 }
03082                         }
03083                 }
03084                 return true;
03085         }
03086 
03087         function isAPICall () {
03088                 return  strpos($_SERVER["SCRIPT_FILENAME"],"api") !== false ||
03089                 strpos($_SERVER["SCRIPT_FILENAME"],"dummy") !== false;
03090         }
03091 
03092         function KT_replaceParam($qstring, $paramName, $paramValue) {
03093                 if (preg_match("/&" . $paramName . "=/", $qstring)) {
03094                         return preg_replace("/&" . $paramName . "=[^&]+/", "&" . $paramName . "=" . urlencode($paramValue), $qstring);
03095                 } else {
03096                         return $qstring . "&" . $paramName . "=" . urlencode($paramValue);
03097                 }
03098         }
03099 
03100         function replaceUrlParameterString ($url, $parametersArray) {
03101 
03102                 foreach ($parametersArray as $paramName => $paramValue ) {
03103                         $url = ilUtil::KT_replaceParam($url, $paramName, $paramValue);
03104                 }
03105                 return $url;
03106         }
03107 
03108     function generatePasswords ($a_number)
03109     {
03110         $ret = array();
03111         srand((double) microtime()*1000000);
03112 
03113         for ($i=1; $i<=$a_number; $i++)
03114         {
03115             $length  = rand(6,10);
03116             $next  = rand(1,2);
03117             //$chars = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
03118             $vowels = "aeiou";
03119             $consonants = "bcdfghjklmnpqrstvwxyz";
03120             $pw = "";
03121 
03122             for ($j=0; $j < $length; $j++)
03123             {
03124                 switch ($next)
03125                 {
03126                     case 1:
03127                                         $pw.= $consonants[rand(0,strlen($consonants)-1)];
03128                                         $next = 2;
03129                     break;
03130 
03131                     case 2:
03132                                         $pw.= $vowels[rand(0,strlen($vowels)-1)];
03133                                         $next = 1;
03134                                 break;
03135                 }
03136              }
03137 
03138              $ret[] = $pw;
03139          }
03140 
03141          return $ret;
03142     }
03143 
03144         function removeTrailingPathSeparators($path)
03145         {
03146                 $path = preg_replace("/[\/\\\]+$/", "", $path);
03147                 return $path;
03148         }
03149 
03158         function array_php2js($data)
03159         {
03160                 if (empty($data))
03161                 {
03162                         $data = array();
03163                 }
03164 
03165                 foreach($data as $k=>$datum)
03166                 {
03167                         if(is_null($datum)) $data[$k] = 'null';
03168                         if(is_string($datum)) $data[$k] = "'" . $datum . "'";
03169                         if(is_array($datum)) $data[$k] = array_php2js($datum);
03170                 }
03171 
03172                 return "[" . implode(', ', $data) . "]";
03173         }
03174 
03179         function virusHandling($a_file, $a_orig_name = "", $a_clean = true)
03180         {
03181                 global $lng;
03182 
03183                 if (IL_VIRUS_SCANNER != "None")
03184                 {
03185                         require_once("classes/class.ilVirusScannerFactory.php");
03186                         $vs = ilVirusScannerFactory::_getInstance();
03187                         if (($vs_txt = $vs->scanFile($a_file, $a_orig_name)) != "")
03188                         {
03189                                 if ($a_clean && (IL_VIRUS_CLEAN_COMMAND != ""))
03190                                 {
03191                                         $clean_txt = $vs->cleanFile($a_file, $a_orig_name);
03192                                         if ($vs->fileCleaned())
03193                                         {
03194                                                 $vs_txt.= "<br />".$lng->txt("cleaned_file").
03195                                                         "<br />".$clean_txt;
03196                                                 $vs_txt.= "<br />".$lng->txt("repeat_scan");
03197                                                 if (($vs2_txt = $vs->scanFile($a_file, $a_orig_name)) != "")
03198                                                 {
03199                                                         return array(false, nl2br($vs_txt)."<br />".$lng->txt("repeat_scan_failed").
03200                                                                 "<br />".nl2br($vs2_txt));
03201                                                 }
03202                                                 else
03203                                                 {
03204                                                         return array(true, nl2br($vs_txt)."<br />".$lng->txt("repeat_scan_succeded"));
03205                                                 }
03206                                         }
03207                                         else
03208                                         {
03209                                                 return array(false, nl2br($vs_txt)."<br />".$lng->txt("cleaning_failed"));
03210                                         }
03211                                 }
03212                                 else
03213                                 {
03214                                         return array(false, nl2br($vs_txt));
03215                                 }
03216                         }
03217                 }
03218 
03219                 return array(true,"");
03220         }
03221 
03222 
03226         function moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors = true)
03227         {
03228                 global $lng, $ilias;
03229 //echo "<br>ilUtli::moveuploadedFile($a_name)";
03230                 if (!is_file($a_file))
03231                 {
03232                         $lng->txt("upload_error_file_not_found");
03233                 }
03234 
03235                 // virus handling
03236                 $vir = ilUtil::virusHandling($a_file, $a_name);
03237                 if (!$vir[0])
03238                 {
03239                         unlink($a_file);
03240                         if ($a_raise_errors)
03241                         {
03242                                 $ilias->raiseError($lng->txt("file_is_infected")."<br />".
03243                                         $vir[1],
03244                                         $ilias->error_obj->MESSAGE);
03245                         }
03246                         else
03247                         {
03248                                 ilUtil::sendInfo($lng->txt("file_is_infected")."<br />".
03249                                         $vir[1], true);
03250                         }
03251                         return false;
03252                 }
03253                 else
03254                 {
03255                         if ($vir[1] != "")
03256                         {
03257                                 ilUtil::sendInfo($vir[1], true);
03258                         }
03259                         return move_uploaded_file($a_file, $a_target);
03260                 }
03261         }
03262 
03263 
03267          function date_mysql2time($mysql_date_time) {
03268                 list($datum, $uhrzeit) = explode (" ",$mysql_date_time);
03269                 list($jahr, $monat, $tag) = explode("-", $datum);
03270                 list($std, $min, $sec) = explode(":", $uhrzeit);
03271                 return mktime ((int) $std, (int) $min, (int) $sec, (int) $monat, (int) $tag, (int) $jahr);
03272          }
03273 
03287         function &processCSVRow(&$row, $quoteAll = FALSE, $separator = ";", $outUTF8 = FALSE, $compatibleWithMSExcel = TRUE)
03288         {
03289                 $resultarray = array();
03290                 foreach ($row as $rowindex => $entry)
03291                 {
03292                         $surround = FALSE;
03293                         if ($quoteAll)
03294                         {
03295                                 $surround = TRUE;
03296                         }
03297                         if (strpos($entry, "\"") !== FALSE)
03298                         {
03299                                 $entry = str_replace("\"", "\"\"", $entry);
03300                                 $surround = TRUE;
03301                         }
03302                         if (strpos($entry, $separator) !== FALSE)
03303                         {
03304                                 $surround = TRUE;
03305                         }
03306                         if ($compatibleWithMSExcel)
03307                         {
03308                                 // replace all CR LF with LF (for Excel for Windows compatibility
03309                                 $entry = str_replace(chr(13).chr(10), chr(10), $entry);
03310                         }
03311                         if ($surround)
03312                         {
03313                                 if ($outUTF8)
03314                                 {
03315                                         $resultarray[$rowindex] = "\"" . $entry . "\"";
03316                                 }
03317                                 else
03318                                 {
03319                                         $resultarray[$rowindex] = utf8_decode("\"" . $entry . "\"");
03320                                 }
03321                         }
03322                         else
03323                         {
03324                                 if ($outUTF8)
03325                                 {
03326                                         $resultarray[$rowindex] = $entry;
03327                                 }
03328                                 else
03329                                 {
03330                                         $resultarray[$rowindex] = utf8_decode($entry);
03331                                 }
03332                         }
03333                 }
03334                 return $resultarray;
03335         }
03336 
03337         // validates a domain name (example: www.ilias.de)
03338         function isDN($a_str)
03339         {
03340                 return(preg_match("/^[a-z]+([a-z0-9-]*[a-z0-9]+)?(\.([a-z]+([a-z0-9-]*[a-z0-9]+)?)+)*$/",$a_str));
03341         }
03342 
03343         // validates an IP address (example: 192.168.1.1)
03344         function isIPv4($a_str)
03345         {
03346                 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])\.".
03347                                                   "(\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));
03348         }
03349 
03350 
03377         function _getObjectsByOperations($a_obj_type,$a_operation,$a_usr_id = 0,$limit = 0)
03378         {
03379                 global $ilDB,$rbacreview,$ilAccess,$ilUser,$ilias;
03380 
03381                 if(!is_array($a_obj_type))
03382                 {
03383                         $where = "WHERE type = ".$ilDB->quote($a_obj_type)." ";
03384                 }
03385                 else
03386                 {
03387                         $where = "WHERE type IN(";
03388                         $where .= implode(",",ilUtil::quoteArray($a_obj_type));
03389                         $where .= ") ";
03390                 }
03391 
03392                 // limit number of results default is search result limit
03393                 if(!$limit)
03394                 {
03395                         $limit = $ilias->getSetting('search_max_hits',100);
03396                 }
03397                 if($limit == -1)
03398                 {
03399                         $limit = 10000;
03400                 }
03401 
03402                 // default to logged in usr
03403                 $a_usr_id = $a_usr_id ? $a_usr_id : $ilUser->getId();
03404                 $a_roles = $rbacreview->assignedRoles($a_usr_id);
03405 
03406                 // Since no rbac_pa entries are available for the system role. This function returns !all! ref_ids in the case the user
03407                 // is assigned to the system role
03408                 if($rbacreview->isAssigned($a_usr_id,SYSTEM_ROLE_ID))
03409                 {
03410                         $query = "SELECT ref_id FROM object_reference AS obr LEFT JOIN object_data AS obd USING(obj_id) ".
03411                                 "LEFT JOIN tree ON obr.ref_id = tree.child ".
03412                                 $where.
03413                                 "AND tree = 1";
03414 
03415                         $res = $ilDB->query($query);
03416                         $counter = 0;
03417                         while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
03418                         {
03419                                 
03420                                 if($counter++ >= $limit)
03421                                 {
03422                                         break;
03423                                 }
03424                                 $ref_ids[] = $row->ref_id;
03425                         }
03426                         return $ref_ids ? $ref_ids : array();
03427                 } // End Administrators
03428                 
03429                 // Check ownership if it is not asked for edit_permission or a create permission
03430                 if($a_operation == 'edit_permissions' or strpos($a_operation,'create') !== false)
03431                 {
03432                         $check_owner = ") ";
03433                 }
03434                 else
03435                 {
03436                         $check_owner = "OR owner = ".$ilDB->quote($a_usr_id).") ";
03437                 }
03438 
03439                 $ops_ids = ilRbacReview::_getOperationIdsByName(array($a_operation));
03440                 $ops_id = $ops_ids[0];
03441 
03442                 $and = "AND ((rol_id IN(".implode(",",ilUtil::quoteArray($a_roles)).") ";
03443                 
03444 
03445                 $query = "SELECT DISTINCT(obr.ref_id),obr.obj_id,type FROM rbac_pa ".
03446                         "LEFT JOIN object_reference AS obr  ON obr.ref_id = rbac_pa.ref_id ".
03447                         "LEFT JOIN object_data AS obd ON obd.obj_id = obr.obj_id ".
03448                         $where.
03449                         $and.
03450                         "AND (ops_id LIKE ".$ilDB->quote("%i:".$ops_id."%"). " ".
03451                         "OR ops_id LIKE".$ilDB->quote("%:\"".$ops_id."\";%").")) ".
03452                         $check_owner;
03453 
03454                 $res = $ilDB->query($query);
03455                 $counter = 0;
03456                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
03457                 {
03458                         if($counter >= $limit)
03459                         {
03460                                 break;
03461                         }
03462                         // Check deleted, hierarchical access ...
03463                         if($ilAccess->checkAccessOfUser($a_usr_id,$a_operation,'',$row->ref_id,$row->type,$row->obj_id))
03464                         {
03465                                 $counter++;
03466                                 $ref_ids[] = $row->ref_id;
03467                         }
03468                 }
03469                 return $ref_ids ? $ref_ids : array();
03470         }
03471 
03478         function insertLatexImages($a_text, $a_start = "\[tex\]", $a_end = "\[\/tex\]", $a_cgi = URL_TO_LATEX)
03479         {
03480                 global $tpl, $lng, $ilUser;
03481                 
03482                 // - take care of html exports (-> see buildLatexImages)
03483                 include_once "./Services/Administration/classes/class.ilSetting.php";
03484                 $jsMathSetting = new ilSetting("jsMath");
03485                 if ($jsMathSetting->get("enable") && ($ilUser->getPref("js_math") || ($ilUser->getPref("js_math") === FALSE && ($jsMathSetting->get("makedefault")))))
03486                 {
03487                         $info = "";
03488                         if (!$tpl->out_jsmath_info)
03489                         {
03490                                 include_once "./classes/class.ilTemplate.php";
03491                                 $template = new ilTemplate("tpl.jsmath_warning.html", TRUE, TRUE);
03492                                 $lng->loadLanguageModule("jsmath");
03493                                 $template->setVariable("TEXT_JSMATH_NO_JAVASCRIPT", $lng->txt("jsmath_no_javascript"));
03494                                 $info = $template->get();
03495                                 $tpl->out_jsmath_info = TRUE;
03496                         }
03497                         $a_text = preg_replace("/\\\\([RZN])([^a-zA-Z]|<\/span>)/", "\\mathbb{"."$1"."}"."$2", $a_text);
03498                         $result_text = preg_replace('/' . $a_start . '(.*?)' . $a_end . '/ie',
03499                                 "'<span class=\"math\">' . preg_replace('/[\\\\\\\\\\]{2}/', '\\cr', str_replace('<', '&lt;', str_replace('<br />', '', str_replace('<br>', '', '$1')))) . '</span>[[info]]'", $a_text);
03500                                         // added special handling for \\ -> \cr, < -> $lt; and removal of <br/> tags in jsMath expressions, H. Schottmüller, 2007-09-09
03501                         $result_text = str_replace("[[info]]", $info, $result_text);
03502                         $tpl->addJavaScript($jsMathSetting->get("path_to_jsmath") . "/easy/load.js");
03503                 }
03504                 else
03505                 {
03506                         $result_text = preg_replace('/' . $a_start . '(.*?)' . $a_end . '/ie',
03507                                 "'<img alt=\"'.htmlentities('$1').'\" src=\"$a_cgi?'.rawurlencode(str_replace('&amp;', '&', str_replace('&gt;', '>', str_replace('&lt;', '<', '$1')))).'\" ".
03508                                 " />'", $a_text);
03509                 }
03510                 return $result_text;
03511         }
03512 
03519         function buildLatexImages($a_text, $a_dir ,$a_start = "\[tex\]", $a_end = "\[\/tex\]", $a_cgi = URL_TO_LATEX)
03520         {
03521                 $result_text = $a_text;
03522 
03523                 if ($a_cgi != "")
03524                 {
03525                         while (preg_match('/' . $a_start . '(.*?)' . $a_end . '/ie', $result_text, $found))
03526                         {
03527                                 $cnt = (int) $GLOBALS["teximgcnt"]++;
03528                                 // get image from cgi and write it to file
03529                                 $fpr = @fopen($a_cgi."?".rawurlencode($found[1]), "r");
03530                                 $lcnt = 0;
03531                                 if ($fpr)
03532                                 {
03533                                         while(!feof($fpr))
03534                                         {
03535                                                 $buf = fread($fpr, 1024);
03536                                                 if ($lcnt == 0)
03537                                                 {
03538                                                         if (is_int(strpos(strtoupper(substr($buf, 0, 5)), "GIF")))
03539                                                         {
03540                                                                 $suffix = "gif";
03541                                                         }
03542                                                         else
03543                                                         {
03544                                                                 $suffix = "png";
03545                                                         }
03546                                                         $fpw = fopen($a_dir."/teximg/img".$cnt.".".$suffix, "w");
03547                                                 }
03548                                                 $lcnt++;
03549                                                 fwrite($fpw, $buf);
03550                                         }
03551                                         fclose($fpw);
03552                                         fclose($fpr);
03553                                 }
03554 
03555                                 // replace tex-tag
03556                                 $img_str = "./teximg/img".$cnt.".".$suffix;
03557                                 $result_text = str_replace($found[0],
03558                                         '<img alt="'.$found[1].'" src="'.$img_str.'" />', $result_text);
03559                         }
03560                 }
03561 
03562                 return $result_text;
03563         }
03564 
03572         function prepareTextareaOutput($txt_output, $prepare_for_latex_output = FALSE)
03573         {
03574                 $result = $txt_output;
03575                 $is_html = $this->isHTML($result);
03576                 if ($prepare_for_latex_output)
03577                 {
03578                         $result = ilUtil::insertLatexImages($result, "<span class\=\"latex\">", "<\/span>", URL_TO_LATEX);
03579                         $result = ilUtil::insertLatexImages($result, "\[tex\]", "\[\/tex\]", URL_TO_LATEX);
03580                 }
03581                 
03582                 // removed: did not work with magic_quotes_gpc = On
03583                 if (!$is_html)
03584                 {
03585                         // if the string does not contain HTML code, replace the newlines with HTML line breaks
03586                         $result = preg_replace("/[\n]/", "<br />", $result);
03587                 }
03588                 else
03589                 {
03590                         // patch for problems with the <pre> tags in tinyMCE
03591                         if (preg_match_all("/(<pre>.*?<\/pre>)/ims", $result, $matches))
03592                         {
03593                                 foreach ($matches[0] as $found)
03594                                 {
03595                                         $replacement = "";
03596                                         if (strpos("\n", $found) === FALSE)
03597                                         {
03598                                                 $replacement = "\n";
03599                                         }
03600                                         $removed = preg_replace("/<br\s*?\/>/ims", $replacement, $found);
03601                                         $result = str_replace($found, $removed, $result);
03602                                 }
03603                         }
03604                 }
03605                 if ($prepare_for_latex_output)
03606                 {
03607                         // replace special characters to prevent problems with the ILIAS template system
03608                         // eg. if someone uses {1} as an answer, nothing will be shown without the replacement
03609                         $result = str_replace("{", "&#123;", $result);
03610                         $result = str_replace("}", "&#125;", $result);
03611                         $result = str_replace("\\", "&#92;", $result);
03612                 }
03613                 return $result;
03614         }
03615 
03616 
03623         function int2array ($seconds, $periods = null)
03624         {
03625                 // Define time periods
03626                 if (!is_array($periods))
03627                 {
03628                         $periods = array (
03629                         'years' => 31536000,
03630                         'months' => 2592000,
03631                         'days'  => 86400,
03632                         'hours' => 3600,
03633                         'minutes' => 60,
03634                         'seconds' => 1
03635                         );
03636                 }
03637 
03638                 // Loop
03639                 $seconds = (float) $seconds;
03640                 foreach ($periods as $period => $value)
03641                 {
03642                         $count = floor($seconds / $value);
03643 
03644                         if ($count == 0)
03645                         {
03646                                 continue;
03647                         }
03648 
03649                         $values[$period] = $count;
03650                         $seconds = $seconds % $value;
03651                 }
03652                 // Return
03653                 if (empty($values))
03654                 {
03655                         $values = null;
03656                 }
03657 
03658                 return $values;
03659         }
03660 
03667         function timearray2string ($duration)
03668         {
03669                 global $lng;
03670 
03671                 if (!is_array($duration))
03672                 {
03673                         return false;
03674                 }
03675 
03676                 foreach ($duration as $key => $value) {
03677 
03678                         // Plural
03679                         if ($value > 1)
03680                         {
03681                                 $segment_name = $key;
03682                                 $segment_name = $lng->txt($segment_name);
03683                                 $segment = $value . ' ' . $segment_name;
03684                         }
03685                         else
03686                         {
03687                                 $segment_name = substr($key, 0, -1);
03688                                 $segment_name = $lng->txt($segment_name);
03689                                 $segment = $value . ' ' . $segment_name;
03690                         }
03691 
03692                         $array[] = $segment;
03693                 }
03694                 $len = count($array);
03695 
03696                 if ($len>3)
03697                 {
03698                         $array=array_slice($array,0,(3-$len));
03699         }
03700 
03701                 $str = implode(', ', $array);
03702 
03703                 return $str;
03704         }
03705 
03706         function getFileSizeInfo()
03707         {
03708                 global $lng;
03709 
03710                 // get the value for the maximal uploadable filesize from the php.ini (if available)
03711                 $umf=get_cfg_var("upload_max_filesize");
03712                 // get the value for the maximal post data from the php.ini (if available)
03713                 $pms=get_cfg_var("post_max_size");
03714 
03715                 // use the smaller one as limit
03716                 $max_filesize=min($umf, $pms);
03717                 if (!$max_filesize) $max_filesize=max($umf, $pms);
03718 
03719                 return $lng->txt("file_notice")." $max_filesize.";
03720          }
03721 
03728         function __extractRefId($role_title)
03729         {
03730 
03731                 $test_str = explode('_',$role_title);
03732 
03733                 if ($test_str[0] == 'il')
03734                 {
03735                         $test2 = (int) $test_str[3];
03736                         return is_numeric ($test2) ? (int) $test2 : false;
03737                 }
03738                 return false;
03739         }
03740 
03750         function __extractId($ilias_id, $inst_id)
03751         {
03752 
03753                 $test_str = explode('_',$ilias_id);
03754 
03755                 if ($test_str[0] == 'il' && $test_str[1] == $inst_id && count($test_str) == 4)
03756                 {
03757                         $test2 = (int) $test_str[3];
03758                         return is_numeric ($test2) ? (int) $test2 : false;
03759                 }
03760                 return false;
03761         }
03762 
03775         function _sortIds($a_ids,$a_table,$a_field,$a_id_name)
03776         {
03777                 global $ilDB;
03778 
03779                 if(!$a_ids)
03780                 {
03781                         return array();
03782                 }
03783 
03784                 // use database to sort user array
03785                 $where = "WHERE ".$a_id_name." IN (";
03786                 $where .= implode(",", ilUtil::quoteArray($a_ids));
03787                 $where .= ") ";
03788 
03789                 $query = "SELECT ".$a_id_name." FROM ".$a_table." ".
03790                         $where.
03791                         "ORDER BY ".$a_field;
03792 
03793                 $res = $ilDB->query($query);
03794                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
03795                 {
03796                         $ids[] = $row->$a_id_name;
03797                 }
03798                 return $ids ? $ids : array();
03799         }
03800 
03808         function getMySQLTimestamp($a_ts)
03809         {
03810                 global $ilDB;
03811 
03812                 if ($ilDB->isMysql4_1OrHigher())
03813                 {
03814                         return $a_ts;
03815                 }
03816                 else
03817                 {
03818                         $t = $a_ts;
03819                         $ts = substr($t, 0, 4)."-".substr($t, 4, 2)."-".substr($t, 6, 2).
03820                                 " ".substr($t, 8, 2).":".substr($t, 10, 2).":".substr($t, 12, 2);
03821                         return $ts;
03822                 }
03823         }
03824 
03828         function quoteArray($a_array)
03829         {
03830                 global $ilDB;
03831 
03832 
03833                 if(!is_array($a_array) or !count($a_array))
03834                 {
03835                         return array("''");
03836                 }
03837 
03838                 foreach($a_array as $k => $item)
03839                 {
03840                         $a_array[$k] = $ilDB->quote($item);
03841                 }
03842 
03843                 return $a_array;
03844         }
03845 
03854         function sendInfo($a_info = "",$a_keep = false)
03855         {
03856                 global $tpl;
03857 
03858                 if (!empty($a_info))
03859                 {
03860                         $_SESSION["info"] = $a_info;
03861                 }
03862                 if (!empty($_SESSION["info"]))
03863                 {
03864                         $tpl->addBlockFile("MESSAGE", "message", "tpl.message.html");
03865         #               $tpl->setCurrentBlock("message");
03866                         $tpl->setVariable("INFO",$_SESSION["info"]);
03867         #               $tpl->parseCurrentBlock();
03868                 }
03869 
03870                 if (!$a_keep)
03871                 {
03872                         session_unregister("info");
03873                 }
03874         }
03875 
03876         function infoPanel($a_keep = true)
03877         {
03878                 global $tpl,$ilias,$lng;
03879 
03880                 if (!empty($_SESSION["infopanel"]) and is_array($_SESSION["infopanel"]))
03881                 {
03882                         $tpl->addBlockFile("INFOPANEL", "infopanel", "tpl.infopanel.html");
03883                         $tpl->setCurrentBlock("infopanel");
03884 
03885                         if (!empty($_SESSION["infopanel"]["text"]))
03886                         {
03887                                 $link = "<a href=\"".$dir.$_SESSION["infopanel"]["link"]."\" target=\"".
03888                                         ilFrameTargetInfo::_getFrame("MainContent").
03889                                         "\">";
03890                                 $link .= $lng->txt($_SESSION["infopanel"]["text"]);
03891                                 $link .= "</a>";
03892                         }
03893 
03894                         // deactivated
03895                         if (!empty($_SESSION["infopanel"]["img"]))
03896                         {
03897                                 $link .= "<td><a href=\"".$_SESSION["infopanel"]["link"]."\" target=\"".
03898                                         ilFrameTargetInfo::_getFrame("MainContent").
03899                                         "\">";
03900                                 $link .= "<img src=\"".$ilias->tplPath.$ilias->account->prefs["skin"]."/images/".
03901                                         $_SESSION["infopanel"]["img"]."\" border=\"0\" vspace=\"0\"/>";
03902                                 $link .= "</a></td>";
03903                         }
03904 
03905                         $tpl->setVariable("INFO_ICONS",$link);
03906                         $tpl->parseCurrentBlock();
03907                 }
03908 
03909                 //if (!$a_keep)
03910                 //{
03911                                 session_unregister("infopanel");
03912                 //}
03913         }
03914 
03915 
03922         function dirsize($directory)
03923     {
03924                 if (!is_dir($directory))
03925                         return -1;
03926                 $size = 0;
03927                 if ($DIR = opendir($directory))
03928                 {
03929                         while (($dirfile = readdir($DIR)) !== false)
03930                         {
03931                                 if (is_link($directory . DIRECTORY_SEPARATOR  . $dirfile) || $dirfile == '.' || $dirfile == '..')
03932                                         continue;
03933                                 if (is_file($directory .  DIRECTORY_SEPARATOR   . $dirfile))
03934                                         $size += filesize($directory . DIRECTORY_SEPARATOR   . $dirfile);
03935                                 else if (is_dir($directory . DIRECTORY_SEPARATOR   . $dirfile))
03936                                 {
03937                                         $dirSize = dirsize($directory .  DIRECTORY_SEPARATOR   . $dirfile);
03938                                         if ($dirSize >= 0)
03939                                                 $size += $dirSize;
03940                                         else return -1;
03941                                 }
03942                         }
03943                         closedir($DIR);
03944                 }
03945                 return $size;
03946         }
03947         
03948         public static function _sanitizeFilemame($a_filename)
03949         {
03950                 return strip_tags($a_filename);
03951         }       
03952 } // END class.ilUtil
03953 
03954 
03955 ?>

Generated on Fri Dec 13 2013 17:57:02 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1