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

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 
00068         function getImagePath($img, $in_module = false, $mode = "output", $offline = false)
00069         {
00070                 global $ilias, $styleDefinition, $ilCtrl;
00071 
00072                 if(defined("ILIAS_MODULE") and $mode != "filesystem")
00073                 {
00074                         // added to find path for MODULES like Services/Search
00075                         $base = '';
00076                         for($i = 1;$i < count(explode('/',ILIAS_MODULE));$i++)
00077                         {
00078                                 $base .= "../";
00079                         }
00080 
00081                         $dir .= ($base.".");
00082                         // hschottm: added because module images are not working
00083                         // using ilias.php
00084 //                      if (strlen($base) == 0) $dir = "";
00085                 }
00086                 else
00087                 {
00088                         $dir = "";
00089                 }
00090                 $base = "./";
00091                 if ($in_module)
00092                 {
00093                         // if baseClass functionality is used (ilias.php):
00094                         // get template directory from ilCtrl
00095                         if (!empty($_GET["baseClass"]))
00096                         {
00097                                 $base.= $ilCtrl->getModuleDir()."/";
00098                         }
00099                         else
00100                         {
00101                                 if(defined("ILIAS_MODULE"))
00102                                 {
00103                                         $base.= ILIAS_MODULE."/";
00104                                 }
00105                         }
00106                 }
00107                 $base .= "templates/";
00108                 if (is_object($styleDefinition))
00109                 {
00110 
00111                         $st_image_dir = $styleDefinition->getImageDirectory($ilias->account->prefs["style"]);
00112                         #var_dump("<pre>",$st_image_dir,$ilias->account->prefs["style"],"<pre>");
00113 
00114                         $user_skin_and_style = $base.$ilias->account->skin."/".
00115                         $st_image_dir.
00116                         "/images/".$img;
00117                 }
00118                 $user_skin = $base.$ilias->account->skin."/images/".$img;
00119                 $default = $base."default/images/".$img;
00120                 if ($offline)
00121                 {
00122                         return "./images/".$img;
00123                 }
00124                 else if (@file_exists($user_skin_and_style) && $st_image_dir != "")
00125                 {
00126                         return $dir.$user_skin_and_style;
00127                 }
00128                 else if (file_exists($user_skin))
00129                 {
00130                         return $dir.$user_skin;
00131                 }
00132 
00133                 return $dir.$default;
00134         }
00135 
00144     function getHtmlPath($relative_path)
00145     {
00146         if (substr($relative_path, 0, 2) == './')
00147         {
00148             $relative_path = (substr($relative_path, 1));
00149         }
00150         if (substr($relative_path, 0, 1) != '/')
00151         {
00152             $relative_path = '/' . $relative_path;
00153         }
00154         $htmlpath = ILIAS_HTTP_PATH . $relative_path;
00155         return $htmlpath;
00156     }
00157 
00167         function getJSPath($a_js)
00168         {
00169                 global $ilias;
00170 
00171                 if(defined("ILIAS_MODULE"))
00172                 {
00173                         $dir = ".";
00174                 }
00175                 else
00176                 {
00177                         $dir = "";
00178                 }
00179                 $in_style = "./templates/".$ilias->account->skin."/".$ilias->account->prefs["style"]."/".$a_js;
00180                 $in_skin = "./templates/".$ilias->account->skin."/".$a_js;
00181                 $default = "./templates/default/".$a_js;
00182                 if(@is_file($in_style))
00183                 {
00184                         return $dir.$in_style;
00185                 }
00186                 else
00187                 {
00188                         if (@is_file($in_skin))
00189                         {
00190                                 return $dir.$in_skin;
00191                         }
00192                         else
00193                         {
00194                                 return $dir.$default;
00195                         }
00196                 }
00197         }
00198 
00204         function getStyleSheetLocation($mode = "output")
00205         {
00206                 global $ilias;
00207 
00208                 if(defined("ILIAS_MODULE") && $mode != "filesystem")
00209                 {
00210                         // added to find Stylesheet for MODULES like Services/Search
00211                         $base = '';
00212                         for($i = 0;$i < count(explode('/',ILIAS_MODULE));$i++)
00213                         {
00214                                 $base .= "../";
00215                         }
00216                 }
00217                 else
00218                 {
00219                         $base = "./";
00220                 }
00221 
00222                 // add version as parameter to force reload for new releases
00223                 if ($mode != "filesystem")
00224                 {
00225                         $vers = str_replace(" ", "-", $ilias->getSetting("ilias_version"));
00226                         $vers = "?vers=".str_replace(".", "-", $vers);
00227                 }
00228 
00229                 return $base."templates/".$ilias->account->skin."/".$ilias->account->prefs["style"].".css".$vers;
00230         }
00231 
00237         function getP3PLocation()
00238         {
00239                 global $ilias;
00240 
00241                 if (defined("ILIAS_MODULE"))
00242                 {
00243                         // added to find Stylesheet for MODULES like Services/Search
00244                         $base = '';
00245                         for($i = 0;$i < count(explode('/',ILIAS_MODULE));$i++)
00246                         {
00247                                 $base .= "../Services/Privacy/";
00248                         }
00249                 }
00250                 else
00251                 {
00252                         $base = "./Services/Privacy/";
00253                 }
00254 
00255                 if (is_file($base."w3c/p3p.xml"))
00256                 {
00257                         return ILIAS_HTTP_PATH."w3c/p3p.xml";
00258                 }
00259                 else
00260                 {
00261                         return ILIAS_HTTP_PATH."/w3c/p3p_template.xml";
00262                 }
00263         }
00264 
00270         function getNewContentStyleSheetLocation($mode = "output")
00271         {
00272                 global $ilias;
00273 
00274                 if(defined("ILIAS_MODULE") && $mode != "filesystem")
00275                 {
00276                         // added to find Stylesheet for MODULES like Services/Search
00277                         $base = '';
00278                         for($i = 0;$i < count(explode('/',ILIAS_MODULE));$i++)
00279                         {
00280                                 $base .= "../";
00281                         }
00282                 }
00283                 else
00284                 {
00285                         $base = "./";
00286                 }
00287 
00288                 // add version as parameter to force reload for new releases
00289                 if ($mode != "filesystem")
00290                 {
00291                         $vers = str_replace(" ", "-", $ilias->getSetting("ilias_version"));
00292                         $vers = "?vers=".str_replace(".", "-", $vers);
00293                 }
00294 
00295                 $in_style = "templates/".$ilias->account->skin."/".$ilias->account->prefs["style"]."_cont.css";
00296                 if (is_file("./".$in_style))
00297                 {
00298                         return $base.$in_style.$vers;
00299                 }
00300                 else
00301                 {
00302                         return $base."templates/default/delos_cont.css".$vers;
00303                 }
00304         }
00305 
00319         function formSelect($selected,$varname,$options,$multiple = false,$direct_text = false, $size = "0",
00320                 $style_class = "")
00321         {
00322                 global $lng;
00323 
00324                 if ($multiple == true)
00325                 {
00326                         $multiple = " multiple=\"multiple\"";
00327                 }
00328                 else
00329                 {
00330                         $multiple = "";
00331                         $size = 0;
00332                 }
00333                 
00334                 if ($style_class != "")
00335                 {
00336                         $class = " class=\"".$style_class."\"";
00337                 }
00338                 else
00339                 {
00340                         $class = "";
00341                 }
00342 
00343                 $str = "<select name=\"".$varname ."\"".$multiple." $class size=\"".$size."\">\n";
00344 
00345                 foreach ($options as $key => $val)
00346                 {
00347                         if ($direct_text)
00348                         {
00349                                 $str .= " <option value=\"".$key."\"";
00350                         }
00351                         else
00352                         {
00353                                 $str .= " <option value=\"".$val."\"";
00354                         }
00355                         if (is_array($selected) )
00356                         {
00357                                 if (in_array($key,$selected))
00358                                 {
00359                                         $str .= " selected=\"selected\"";
00360                                 }
00361                         }
00362                         else if ($selected == $key)
00363                         {
00364                                 $str .= " selected=\"selected\"";
00365                         }
00366 
00367                         if ($direct_text)
00368                         {
00369                                 $str .= ">".$val."</option>\n";
00370                         }
00371                         else
00372                         {
00373                                 $str .= ">".$lng->txt($val)."</option>\n";
00374                         }
00375                 }
00376 
00377                 $str .= "</select>\n";
00378 
00379                 return $str;
00380         }
00381 
00389         function getSelectName ($selected,$values)
00390         {
00391                 return($values[$selected]);
00392         }
00393 
00403         function formCheckbox ($checked,$varname,$value,$disabled = false)
00404         {
00405                 $str = "<input type=\"checkbox\" name=\"".$varname."\"";
00406 
00407                 if ($checked == 1)
00408                 {
00409                         $str .= " checked=\"checked\"";
00410                 }
00411 
00412                 if ($disabled)
00413                 {
00414                         $str .= " disabled=\"disabled\"";
00415                 }
00416 
00417                 $array_var = false;
00418 
00419                 if (substr($varname,-2) == "[]")
00420                 {
00421                         $array_var = true;
00422                 }
00423 
00424                 // if varname ends with [], use varname[-2] + _ + value as id tag (e.g. "user_id[]" => "user_id_15")
00425                 if ($array_var)
00426                 {
00427                         $varname_id = substr($varname,0,-2)."_".$value;
00428                 }
00429                 else
00430                 {
00431                         $varname_id = $varname;
00432                 }
00433 
00434                 // dirty removal of other "[]" in string
00435                 $varname_id = ereg_replace("\[","_",$varname_id);
00436                 $varname_id = ereg_replace("\]","",$varname_id);
00437 
00438                 $str .= " value=\"".$value."\" id=\"".$varname_id."\" />\n";
00439 
00440                 return $str;
00441         }
00442 
00452         function formDisabledRadioButton($checked,$varname,$value,$disabled)
00453           {
00454             if ($disabled) {
00455               $str = "<input disabled type=\"radio\" name=\"".$varname."\"";
00456             }
00457             else {
00458               $str = "<input type=\"radio\" name=\"".$varname."\"";
00459             }
00460             if ($checked == 1)
00461               {
00462                 $str .= " checked=\"checked\"";
00463               }
00464 
00465             $str .= " value=\"".$value."\"";
00466             $str .= " id=\"".$value."\" />\n";
00467 
00468             return $str;
00469 
00470           }
00471 
00472 
00481         function formRadioButton($checked,$varname,$value)
00482         {
00483                 $str = "<input type=\"radio\" name=\"".$varname."\"";
00484                 if ($checked == 1)
00485                 {
00486                         $str .= " checked=\"checked\"";
00487                 }
00488 
00489                 $str .= " value=\"".$value."\"";
00490 
00491                 $str .= " id=\"".$value."\" />\n";
00492 
00493                 return $str;
00494         }
00495 
00500         function checkInput ($vars)
00501         {
00502                 // TO DO:
00503                 // Diese Funktion soll Formfeldeingaben berprfen (empty und required)
00504         }
00505 
00511         function setPathStr ($a_path)
00512         {
00513                 if ("" != $a_path && "/" != substr($a_path, -1))
00514                 {
00515                         $a_path .= "/";
00516                         //$a_path = substr($a_path,1);
00517                 }
00518 
00519                 //return getcwd().$a_path;
00520                 return $a_path;
00521         }
00522 
00533         function switchColor ($a_num,$a_css1,$a_css2)
00534         {
00535                 if (!($a_num % 2))
00536                 {
00537                         return $a_css1;
00538                 }
00539                 else
00540                 {
00541                         return $a_css2;
00542                 }
00543         }
00544 
00554         function showTabs($a_hl, $a_o)
00555         {
00556                 global $lng;
00557 
00558                 $tpltab = new ilTemplate("tpl.tabs.html", true, true);
00559 
00560                 for ($i=1; $i<=4; $i++)
00561                 {
00562                         $tpltab->setCurrentBlock("tab");
00563                         if ($a_hl == $i)
00564                         {
00565                                 $tabtype = "tabactive";
00566                                 $tab = $tabtype;
00567                         }
00568                         else
00569                         {
00570                                 $tabtype = "tabinactive";
00571                                 $tab = "tab";
00572                         }
00573 
00574                         switch ($i)
00575                         {
00576                                 case 1:
00577                                 $txt = $lng->txt("view_content");
00578                                 break;
00579                                 case 2:
00580                                 $txt = $lng->txt("edit_properties");
00581                                 break;
00582                                 case 3:
00583                                 $txt = $lng->txt("perm_settings");
00584                                 break;
00585                                 case 4:
00586                                 $txt = $lng->txt("show_owner");
00587                                 break;
00588                         } // switch
00589                         $tpltab->setVariable("CONTENT", $txt);
00590                         $tpltab->setVariable("TABTYPE", $tabtype);
00591                         $tpltab->setVariable("TAB", $tab);
00592                         $tpltab->setVariable("LINK", $a_o["LINK".$i]);
00593                         $tpltab->parseCurrentBlock();
00594                 }
00595 
00596                 return $tpltab->get();
00597         }
00598 
00599 
00600 
00607         function checkFormEmpty ($emptyFields)
00608         {
00609 
00610                 $feedback = "";
00611 
00612                 foreach ($emptyFields as $key => $val)
00613                 {
00614                         if ($val == "") {
00615                                 if ($feedback != "") $feedback .= ", ";
00616                                 $feedback .= $key;
00617                         }
00618                 }
00619 
00620                 return $feedback;
00621         }
00622 
00645         function Linkbar ($AScript,$AHits,$ALimit,$AOffset,$AParams = array(),$ALayout = array())
00646         {
00647                 $LinkBar = "";
00648 
00649                 $layout_link = "";
00650                 $layout_prev = "&lt;&lt;";
00651                 $layout_next = "&gt;&gt;";
00652 
00653                 // layout options
00654                 if (count($ALayout > 0))
00655                 {
00656                         if ($ALayout["link"])
00657                         {
00658                                 $layout_link = " class=\"".$ALayout["link"]."\"";
00659                         }
00660 
00661                         if ($ALayout["prev"])
00662                         {
00663                                 $layout_prev = $ALayout["prev"];
00664                         }
00665 
00666                         if ($ALayout["next"])
00667                         {
00668                                 $layout_next = $ALayout["next"];
00669                         }
00670                 }
00671 
00672                 // Wenn Hits gr?sser Limit, zeige Links an
00673                 if ($AHits > $ALimit)
00674                 {
00675                         if (!empty($AParams))
00676                         {
00677                                 foreach ($AParams as $key => $value)
00678                                 {
00679                                         $params.= $key."=".$value."&";
00680                                 }
00681                         }
00682                         // if ($params) $params = substr($params,0,-1);
00683                         if(strpos($AScript,'&'))
00684                         {
00685                                 $link = $AScript."&".$params."offset=";
00686                         }
00687                         else
00688                         {
00689                                 $link = $AScript."?".$params."offset=";
00690                         }
00691 
00692                         // ?bergehe "zurck"-link, wenn offset 0 ist.
00693                         if ($AOffset >= 1)
00694                         {
00695                                 $prevoffset = $AOffset - $ALimit;
00696                                 $LinkBar .= "<a".$layout_link." href=\"".$link.$prevoffset."\">".$layout_prev."&nbsp;</a>";
00697                         }
00698 
00699                         // Ben?tigte Seitenzahl kalkulieren
00700                         $pages=intval($AHits/$ALimit);
00701 
00702                         // Wenn ein Rest bleibt, addiere eine Seite
00703                         if (($AHits % $ALimit))
00704                         $pages++;
00705 
00706                         // Bei Offset = 0 keine Seitenzahlen anzeigen : DEAKTIVIERT
00707                         //                      if ($AOffset != 0) {
00708 
00709                         // ansonsten zeige Links zu den anderen Seiten an
00710                         for ($i = 1 ;$i <= $pages ; $i++)
00711                         {
00712                                 $newoffset=$ALimit*($i-1);
00713 
00714                                 if ($newoffset == $AOffset)
00715                                 {
00716                                         $LinkBar .= "[".$i."] ";
00717                                 }
00718                                 else
00719                                 {
00720                                         $LinkBar .= '<a '.$layout_link.' href="'.
00721                                                 $link.$newoffset.'">['.$i.']</a> ';
00722                                 }
00723                         }
00724                         //                      }
00725 
00726                         // Checken, ob letze Seite erreicht ist
00727                         // Wenn nicht, gebe einen "Weiter"-Link aus
00728                         if (! ( ($AOffset/$ALimit)==($pages-1) ) && ($pages!=1) )
00729                         {
00730                                 $newoffset=$AOffset+$ALimit;
00731                                 $LinkBar .= "<a".$layout_link." href=\"".$link.$newoffset."\">&nbsp;".$layout_next."</a>";
00732                         }
00733 
00734                         return $LinkBar;
00735                 }
00736                 else
00737                 {
00738                         return false;
00739                 }
00740         }
00741 
00750         function makeClickable($a_text)
00751         {
00752                 // URL mit ://-Angabe
00753                 $ret = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=-])",
00754                 "<a href=\"\\1://\\2\\3\" target=\"_blank\">\\1://\\2\\3</a>", $a_text);
00755 
00756                 // www-URL ohne ://-Angabe
00757                 $ret = eregi_replace("([[:space:]]+)(www\.)([[:alnum:]#?/&=\.-]+)",
00758                 "\\1<a href=\"http://\\2\\3\" target=\"_blank\">\\2\\3</a>", $ret);
00759 
00760                 // ftp-URL ohne ://-Angabe
00761                 $ret = eregi_replace("([[:space:]]+)(ftp\.)([[:alnum:]#?/&=\.-]+)",
00762                 "\\1<a href=\"ftp://\\2\\3\" target=\"_blank\">\\2\\3</a>", $ret);
00763 
00764                 // E-Mail
00765                 $ret = eregi_replace("(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))",
00766                 "<a  href=\"mailto:\\1\">\\1</a>", $ret);
00767 
00768                 return($ret);
00769         }
00770 
00787         function StopWatch($begin = -1)
00788         {
00789                 $m = explode(" ",microtime());
00790                 $m = $m[0] + $m[1];
00791 
00792                 if ($begin != -1)
00793                 {
00794                         $m = $m - $begin;
00795                 }
00796 
00797                 return($m);
00798         }
00799 
00816         function makeDateSelect($prefix, $year = "", $month = "", $day = "", $startyear = "",$a_long_month = true)
00817         {
00818                 global $lng;
00819 
00820                 $now = getdate();
00821                 if (!strlen($year)) $year = $now["year"];
00822                 if (!strlen($month)) $month = $now["mon"];
00823                 if (!strlen($day)) $day = $now["mday"];
00824 
00825                 $year = (int) $year;
00826                 $month = (int) $month;
00827                 $day = (int) $day;
00828 
00829                 // build day select
00830                 $sel_day .= "<select name=\"".$prefix."[d]\" id=\"".$prefix."_d\">\n";
00831 
00832                 for ($i = 1; $i <= 31; $i++)
00833                 {
00834                         $sel_day .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
00835                 }
00836                 $sel_day .= "</select>\n";
00837                 $sel_day = preg_replace("/(value\=\"$day\")/", "$1 selected=\"selected\"", $sel_day);
00838 
00839                 // build month select
00840                 $sel_month .= "<select name=\"".$prefix."[m]\" id=\"".$prefix."_m\">\n";
00841 
00842                 for ($i = 1; $i <= 12; $i++)
00843                 {
00844                         if($a_long_month)
00845                         {
00846                                 $sel_month .= "<option value=\"$i\">" . $lng->txt("month_" . sprintf("%02d", $i) . "_long") . "</option>\n";
00847                         }
00848                         else
00849                         {
00850                                 $sel_month .= "<option value=\"$i\">" . $i  . "</option>\n";
00851                         }
00852                 }
00853                 $sel_month .= "</select>\n";
00854                 $sel_month = preg_replace("/(value\=\"$month\")/", "$1 selected=\"selected\"", $sel_month);
00855 
00856                 // build year select
00857                 $sel_year .= "<select name=\"".$prefix."[y]\" id=\"".$prefix."_y\">\n";
00858                 if ((strlen($startyear) == 0) || ($startyear > $year))
00859                 {
00860                         $startyear = $year;
00861                 }
00862                 for ($i = $startyear; $i <= $year + 3; $i++)
00863                 {
00864                         $sel_year .= "<option value=\"$i\">" . sprintf("%04d", $i) . "</option>\n";
00865                 }
00866                 $sel_year .= "</select>\n";
00867                 $sel_year = preg_replace("/(value\=\"$year\")/", "$1 selected=\"selected\"", $sel_year);
00868 
00869                 $dateformat = $lng->text["lang_dateformat"];
00870                 $dateformat = strtolower(preg_replace("/\W/", "", $dateformat));
00871                 $dateformat = strtolower(preg_replace("/(\w)/", "%%$1", $dateformat));
00872                 $dateformat = preg_replace("/%%d/", $sel_day, $dateformat);
00873                 $dateformat = preg_replace("/%%m/", $sel_month, $dateformat);
00874                 $dateformat = preg_replace("/%%y/", $sel_year, $dateformat);
00875                 return $dateformat;
00876         }
00877 
00894         function makeTimeSelect($prefix, $short = true, $hour = "", $minute = "", $second = "",$a_use_default = true)
00895         {
00896                 global $lng;
00897                 if ($a_use_default and !strlen("$hour$minute$second")) {
00898                         $now = localtime();
00899                         $hour = $now[2];
00900                         $minute = $now[1];
00901                         $second = $now[0];
00902                 } else {
00903                         $hour = (int)$hour;
00904                         $minute = (int)$minute;
00905                         $second = (int)$second;
00906                 }
00907                 // build hour select
00908                 $sel_hour .= "<select name=\"".$prefix."[h]\" id=\"".$prefix."_h\">\n";
00909 
00910                 for ($i = 0; $i <= 23; $i++)
00911                 {
00912                         $sel_hour .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
00913                 }
00914                 $sel_hour .= "</select>\n";
00915                 $sel_hour = preg_replace("/(value\=\"$hour\")/", "$1 selected=\"selected\"", $sel_hour);
00916 
00917                 // build minutes select
00918                 $sel_minute .= "<select name=\"".$prefix."[m]\" id=\"".$prefix."_m\">\n";
00919 
00920                 for ($i = 0; $i <= 59; $i++)
00921                 {
00922                         $sel_minute .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
00923                 }
00924                 $sel_minute .= "</select>\n";
00925                 $sel_minute = preg_replace("/(value\=\"$minute\")/", "$1 selected=\"selected\"", $sel_minute);
00926 
00927                 if (!$short) {
00928                         // build seconds select
00929                         $sel_second .= "<select name=\"".$prefix."[s]\" id=\"".$prefix."_s\">\n";
00930 
00931                         for ($i = 0; $i <= 59; $i++)
00932                         {
00933                                 $sel_second .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
00934                         }
00935                         $sel_second .= "</select>\n";
00936                         $sel_second = preg_replace("/(value\=\"$second\")/", "$1 selected=\"selected\"", $sel_second);
00937                 }
00938                 $timeformat = $lng->text["lang_timeformat"];
00939                 if (strlen($timeformat) == 0) $timeformat = "H:i:s";
00940                 $timeformat = strtolower(preg_replace("/\W/", "", $timeformat));
00941                 $timeformat = preg_replace("/(\w)/", "%%$1", $timeformat);
00942                 $timeformat = preg_replace("/%%h/", $sel_hour, $timeformat);
00943                 $timeformat = preg_replace("/%%i/", $sel_minute, $timeformat);
00944                 if ($short) {
00945                         $timeformat = preg_replace("/%%s/", "", $timeformat);
00946                 } else {
00947                         $timeformat = preg_replace("/%%s/", $sel_second, $timeformat);
00948                 }
00949                 return $timeformat;
00950         }
00951 
00952         /*
00953         * This preg-based function checks whether an e-mail address is formally valid.
00954         * It works with all top level domains including the new ones (.biz, .info, .museum etc.)
00955         * and the special ones (.arpa, .int etc.)
00956         * as well as with e-mail addresses based on IPs (e.g. webmaster@123.45.123.45)
00957         * @author       Unknown <mail@philipp-louis.de> (source: http://www.php.net/preg_match)
00958         * @access       public
00959         * @param        string  email address
00960         * @return       boolean true if valid
00961         */
00962         function is_email($a_email)
00963         {
00964                 return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cat|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([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));
00965         }
00966 
00967         /*
00968         * validates a password
00969         * @access       public
00970         * @param        string  password
00971         * @return       boolean true if valid
00972         */
00973         function isPassword($a_passwd)
00974         {
00975                 if (empty($a_passwd))
00976                 {
00977                         return false;
00978                 }
00979 
00980                 if (strlen($a_passwd) < 6)
00981                 {
00982                         return false;
00983                 }
00984                 // due to bug in php does not work
00985                 //if (!ereg("^[A-Za-z0-9_\.\+\-\*\@!\$\%\~]+$", $a_passwd))
00986 
00987                 if (!preg_match("/^[A-Za-z0-9_\.\+\?\#\-\*\@!\$\%\~]+$/", $a_passwd))
00988                 {
00989                         return false;
00990                 }
00991 
00992                 return true;
00993         }
00994 
00995         /*
00996         * validates a login
00997         * @access       public
00998         * @param        string  login
00999         * @return       boolean true if valid
01000         */
01001         function isLogin($a_login)
01002         {
01003                 if (empty($a_login))
01004                 {
01005                         return false;
01006                 }
01007 
01008                 if (strlen($a_login) < 3)
01009                 {
01010                         return false;
01011                 }
01012 
01013                 if (!ereg("^[A-Za-z0-9_\.\+\*\@!\$\%\~\-]+$", $a_login))
01014                 {
01015                         return false;
01016                 }
01017 
01018                 return true;
01019         }
01020 
01032         function shortenText ($a_str, $a_len, $a_dots = false, $a_next_blank = false)
01033         {
01034                 include_once("./classes/class.ilStr.php");
01035                 if (ilStr::strLen($a_str) > $a_len)
01036                 {
01037                         if ($a_next_blank)
01038                         {
01039                                 $len = ilStr::strPos($a_str, " ", $a_len);
01040                         }
01041                         else
01042                         {
01043                                 $len = $a_len;
01044                         }
01045                         $a_str = ilStr::subStr($a_str,0,$len);
01046 
01047                         if ($a_dots)
01048                         {
01049                                 $a_str .= "...";
01050                         }
01051                 }
01052 
01053                 return $a_str;
01054         }
01055 
01063         function attribsToArray($a_str)
01064         {
01065                 $attribs = array();
01066                 while (is_int(strpos($a_str, "=")))
01067                 {
01068                         $eq_pos = strpos($a_str, "=");
01069                         $qu1_pos = strpos($a_str, "\"");
01070                         $qu2_pos = strpos(substr($a_str, $qu1_pos + 1), "\"") + $qu1_pos + 1;
01071                         if (is_int($eq_pos) && is_int($qu1_pos) && is_int($qu2_pos))
01072                         {
01073                                 $var = trim(substr($a_str, 0, $eq_pos));
01074                                 $val = trim(substr($a_str, $qu1_pos + 1, ($qu2_pos - $qu1_pos) - 1));
01075                                 $attribs[$var] = $val;
01076                                 $a_str = substr($a_str, $qu2_pos + 1);
01077                         }
01078                         else
01079                         {
01080                                 $a_str = "";
01081                         }
01082                 }
01083                 return $attribs;
01084         }
01085 
01094         function rCopy ($a_sdir, $a_tdir)
01095         {
01096                 // check if arguments are directories
01097                 if (!@is_dir($a_sdir) or
01098                 !@is_dir($a_tdir))
01099                 {
01100                         return FALSE;
01101                 }
01102 
01103                 // read a_sdir, copy files and copy directories recursively
01104                 $dir = opendir($a_sdir);
01105 
01106                 while($file = readdir($dir))
01107                 {
01108                         if ($file != "." and
01109                         $file != "..")
01110                         {
01111                                 // directories
01112                                 if (@is_dir($a_sdir."/".$file))
01113                                 {
01114                                         if (!@is_dir($a_tdir."/".$file))
01115                                         {
01116                                                 if (!ilUtil::makeDir($a_tdir."/".$file))
01117                                                 return FALSE;
01118 
01119                                                 //chmod($a_tdir."/".$file, 0775);
01120                                         }
01121 
01122                                         if (!ilUtil::rCopy($a_sdir."/".$file,$a_tdir."/".$file))
01123                                         {
01124                                                 return FALSE;
01125                                         }
01126                                 }
01127 
01128                                 // files
01129                                 if (@is_file($a_sdir."/".$file))
01130                                 {
01131                                         if (!copy($a_sdir."/".$file,$a_tdir."/".$file))
01132                                         {
01133                                                 return FALSE;
01134                                         }
01135                                 }
01136                         }
01137                 }
01138                 return TRUE;
01139         }
01140 
01148         function getWebspaceDir($mode = "filesystem")
01149         {
01150                 global $ilias;
01151 
01152                 if ($mode == "filesystem")
01153                 {
01154                         return "./".ILIAS_WEB_DIR."/".$ilias->client_id;
01155                 }
01156                 else
01157                 {
01158                         if (defined("ILIAS_MODULE"))
01159                         {
01160                                 return "../".ILIAS_WEB_DIR."/".$ilias->client_id;
01161                         }
01162                         else
01163                         {
01164                                 return "./".ILIAS_WEB_DIR."/".$ilias->client_id;
01165                         }
01166                 }
01167 
01168                 //return $ilias->ini->readVariable("server","webspace_dir");
01169         }
01170 
01174         function getDataDir()
01175         {
01176                 return CLIENT_DATA_DIR;
01177                 //global $ilias;
01178 
01179                 //return $ilias->ini->readVariable("server", "data_dir");
01180         }
01181 
01189         function getUsersOnline($a_user_id = 0)
01190         {
01191                 global $ilias;
01192 
01193                 if ($a_user_id == 0)
01194                 {
01195                         $where = "WHERE user_id != 0";
01196                 }
01197                 else
01198                 {
01199                         $where = "WHERE user_id = '".$a_user_id."'";
01200                 }
01201 
01202                 $q = "SELECT count(user_id) as num,user_id,data,firstname,lastname,title,login,last_login FROM usr_session ".
01203                 "LEFT JOIN usr_data ON user_id=usr_id ".$where.
01204                 " AND expires>UNIX_TIMESTAMP() ".
01205                 "GROUP BY user_id ".
01206                 "ORDER BY lastname, firstname";
01207                 $r = $ilias->db->query($q);
01208 
01209                 while ($user = $r->fetchRow(DB_FETCHMODE_ASSOC))
01210                 {
01211                         $users[$user["user_id"]] = $user;
01212                 }
01213 
01214                 return $users ? $users : array();
01215         }
01216 
01225         function getAssociatedUsersOnline($a_user_id)
01226         {
01227                 global $ilias;
01228 
01229                 // The difference between active time and session time
01230                 $time_diff = 0;
01231 
01232                 // Get a list of object id's of all courses and groups for which
01233                 // the current user has local roles.
01234                 // Note: we have to use DISTINCT here, because a user may assume
01235                 // multiple roles in a group or a course.
01236                 $q = "SELECT DISTINCT dat.obj_id as obj_id ".
01237                 "FROM rbac_ua AS ua ".
01238                 "JOIN rbac_fa AS fa ON fa.rol_id = ua.rol_id ".
01239                 "JOIN object_reference AS r1 ON r1.ref_id = fa.parent ".
01240                 "JOIN tree ON tree.child = r1.ref_id ".
01241                 "JOIN object_reference AS r2 ON r2.ref_id = tree.parent ".
01242                 "JOIN object_data AS dat ON dat.obj_id = r2.obj_id ".
01243                 "WHERE ua.usr_id = ".$a_user_id." ".
01244                 "AND fa.assign = 'y' ".
01245                 "AND dat.type IN ('grp','crs')";
01246                 $r = $ilias->db->query($q);
01247                 while ($row = $r->fetchRow(DB_FETCHMODE_ASSOC))
01248                 {
01249                         $groups_and_courses_of_user[] = $row["obj_id"];
01250                 }
01251 
01252                 // If the user is not in a course or a group, he has no associated users.
01253                 if (count($groups_and_courses_of_user) == 0)
01254                 {
01255                         $q = "SELECT count(user_id) as num,user_id,data,firstname,lastname,title,login,last_login ".
01256                         "FROM usr_session ".
01257                         "JOIN usr_data ON user_id=usr_id ".
01258                         "WHERE user_id = ".$a_user_id." ".
01259                         "AND expires > UNIX_TIMESTAMP() - ".$time_diff." ".
01260                         "GROUP BY user_id";
01261                 }
01262                 else
01263                 {
01264                         $q = "SELECT count(user_id) as num,s.user_id,s.data,ud.firstname,ud.lastname,ud.title,ud.login,ud.last_login ".
01265                         "FROM usr_session AS s ".
01266                         "JOIN usr_data AS ud ON ud.usr_id = s.user_id ".
01267                         "JOIN rbac_ua AS ua ON ua.usr_id = s.user_id ".
01268                         "JOIN rbac_fa AS fa ON fa.rol_id = ua.rol_id ".
01269                         "JOIN tree ON tree.child = fa.parent ".
01270                         "JOIN object_reference AS or1 ON or1.ref_id = tree.parent ".
01271                         "JOIN object_data AS od ON od.obj_id = or1.obj_id ".
01272                         "WHERE s.user_id != 0 ".
01273                         "AND s.expires > UNIX_TIMESTAMP() - ".$time_diff." ".
01274                         "AND fa.assign = 'y' ".
01275                         "AND od.obj_id IN (".implode(",",$groups_and_courses_of_user).") ".
01276                         "GROUP BY s.user_id ".
01277                         "ORDER BY ud.lastname, ud.firstname";
01278                 }
01279                 $r = $ilias->db->query($q);
01280 
01281                 while ($user = $r->fetchRow(DB_FETCHMODE_ASSOC))
01282                 {
01283                         $users[$user["user_id"]] = $user;
01284                 }
01285 
01286                 return $users ? $users : array();
01287         }
01288 
01294         function ilTempnam()
01295         {
01296                 $temp_path = ilUtil::getDataDir() . "/temp";
01297                 if (!is_dir($temp_path))
01298                 {
01299                         ilUtil::createDirectory($temp_path);
01300                 }
01301                 $temp_name = tempnam($temp_path, "tmp");
01302                 // --->
01303                 // added the following line because tempnam creates a backslash on some
01304                 // Windows systems which leads to problems, because the "...\tmp..." can be
01305                 // interpreted as "...{TAB-CHARACTER}...". The normal slash works fine
01306                 // even under windows (Helmut Schottmüller, 2005-08-31)
01307                 $temp_name = str_replace("\\", "/", $temp_name);
01308                 // --->
01309                 unlink($temp_name);
01310                 return $temp_name;
01311         }
01312 
01318         function createDirectory($a_dir, $a_mod = 0755)
01319         {
01320                 ilUtil::makeDir($a_dir);
01321                 //@mkdir($a_dir);
01322                 //@chmod($a_dir, $a_mod);
01323         }
01324 
01325 
01332         function unzip($a_file, $overwrite = false)
01333         {
01334                 //global $ilias;
01335 
01336                 $pathinfo = pathinfo($a_file);
01337                 $dir = $pathinfo["dirname"];
01338                 $file = $pathinfo["basename"];
01339 
01340                 // unzip
01341                 $cdir = getcwd();
01342                 chdir($dir);
01343                 $unzip = PATH_TO_UNZIP;
01344                 //$unzip = $ilias->getSetting("unzip_path");
01345 
01346                 // workaround for unzip problem (unzip of subdirectories fails, so
01347                 // we create the subdirectories ourselves first)
01348                 // get list
01349                 $unzipcmd = $unzip." -Z -1 ".ilUtil::escapeShellArg($file);
01350                 exec($unzipcmd, $arr);
01351                 $zdirs = array();
01352 
01353                 foreach($arr as $line)
01354                 {
01355                         if(is_int(strpos($line, "/")))
01356                         {
01357                                 $zdir = substr($line, 0, strrpos($line, "/"));
01358                                 $nr = substr_count($zdir, "/");
01359                                 //echo $zdir." ".$nr."<br>";
01360                                 while ($zdir != "")
01361                                 {
01362                                         $nr = substr_count($zdir, "/");
01363                                         $zdirs[$zdir] = $nr;                            // collect directories
01364                                         //echo $dir." ".$nr."<br>";
01365                                         $zdir = substr($zdir, 0, strrpos($zdir, "/"));
01366                                 }
01367                         }
01368                 }
01369 
01370                 asort($zdirs);
01371 
01372                 foreach($zdirs as $zdir => $nr)                         // create directories
01373                 {
01374                         ilUtil::createDirectory($zdir);
01375                 }
01376 
01377                 // real unzip
01378                 if ($overvwrite)
01379                 {
01380                         $unzipcmd = $unzip." ".ilUtil::escapeShellArg($file);
01381                 }
01382                 else
01383                 {
01384                         $unzipcmd = $unzip." -o ".ilUtil::escapeShellArg($file);
01385                 }
01386                 exec($unzipcmd);
01387 
01388                 chdir($cdir);
01389         }
01390 
01394         function zip($a_dir, $a_file)
01395         {
01396                 //global $ilias;
01397 
01398                 $cdir = getcwd();
01399 
01400                 $pathinfo = pathinfo($a_file);
01401                 $dir = $pathinfo["dirname"];
01402                 $file = $pathinfo["basename"];
01403 
01404                 // unzip
01405                 $cdir = getcwd();
01406                 chdir($dir);
01407 
01408                 $zip = PATH_TO_ZIP;
01409                 //$zip = $ilias->getSetting("zip_path");
01410 
01411                 if (is_array($a_dir))
01412                 {
01413                         $source = "";
01414                         foreach($a_dir as $dir)
01415                         {
01416                                 $name = basename($dir);
01417                                 $source.= " ".ilUtil::escapeShellArg($name);
01418                         }
01419                 }
01420                 else
01421                 {
01422                         $name = basename($a_dir);
01423                         $source = ilUtil::escapeShellArg($name);
01424                 }
01425 
01426                 $zipcmd = $zip." -r ".ilUtil::escapeShellArg($a_file)." ".$source;
01427                 exec($zipcmd);
01428 //echo htmlentities($zipcmd);
01429                 chdir($cdir);
01430         }
01431 
01435         function getConvertCmd()
01436         {
01437                 return PATH_TO_CONVERT;
01438                 //global $ilias;
01439 
01440                 //return $ilias->getSetting("convert_path");
01441         }
01442 
01450         function convertImage($a_from, $a_to, $a_target_format = "", $a_geometry = "")
01451         {
01452                 $format_str = ($a_target_format != "")
01453                 ? strtoupper($a_target_format).":"
01454                 : "";
01455                 $geometry = ($a_geometry != "")
01456                 ? " -geometry ".$a_geometry."x".$a_geometry." "
01457                 : "";
01458                 $convert_cmd = ilUtil::getConvertCmd()." ".
01459                 ilUtil::escapeShellArg($a_from)." ".$geometry.ilUtil::escapeShellArg($format_str.$a_to);
01460                 system($convert_cmd);
01461         }
01462 
01471         function resizeImage($a_from, $a_to, $a_width, $a_height)
01472         {
01473                 $size = " -resize ".$a_width."x".$a_height."! ";
01474                 $convert_cmd = ilUtil::getConvertCmd()." ".
01475                         ilUtil::escapeShellArg($a_from)." ".$size.ilUtil::escapeShellArg($a_to);
01476                 system($convert_cmd);
01477         }
01478 
01484         function html2pdf($html, $pdf_file)
01485         {
01486                 //global $ilias;
01487 
01488                 $html_file = str_replace(".pdf",".html",$pdf_file);
01489 
01490                 $fp = fopen( $html_file ,"wb");
01491                 fwrite($fp, $html);
01492                 fclose($fp);
01493 
01494                 $htmldoc_path = PATH_TO_HTMLDOC;
01495                 //$htmldoc_path = $ilias->getSetting("htmldoc_path");
01496 
01497                 $htmldoc = $htmldoc_path." ";
01498                 $htmldoc .= "--no-toc ";
01499                 $htmldoc .= "--no-jpeg ";
01500                 $htmldoc .= "--webpage ";
01501                 $htmldoc .= "--outfile " . ilUtil::escapeShellArg($pdf_file) . " ";
01502                 $htmldoc .= "--bodyfont Arial ";
01503                 $htmldoc .= "--charset iso-8859-15 ";
01504                 $htmldoc .= "--color ";
01505                 $htmldoc .= "--size A4  ";      // --landscape
01506                 $htmldoc .= "--format pdf ";
01507                 $htmldoc .= "--footer ... ";
01508                 $htmldoc .= "--header ... ";
01509                 $htmldoc .= "--left 60 ";
01510                 // $htmldoc .= "--right 200 ";
01511                 $htmldoc .= $html_file;
01512                 exec($htmldoc);
01513 
01514         }
01515 
01519         function deliverData($a_data, $a_filename, $mime = "application/octet-stream", $charset = "")
01520         {
01521                 $disposition = "attachment"; // "inline" to view file in browser or "attachment" to download to hard disk
01522                 //              $mime = "application/octet-stream"; // or whatever the mime type is
01523 
01524                 if (isset($_SERVER["HTTPS"])) {
01525 
01526                         // Added different handling for IE and HTTPS => send pragma after content informations
01530                         #header("Pragma: ");
01531                         #header("Cache-Control: ");
01532                         #header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
01533                         #header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
01534                         #header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
01535                         #header("Cache-Control: post-check=0, pre-check=0", false);
01536                 }
01537                 else if ($disposition == "attachment")
01538                 {
01539                         header("Cache-control: private");
01540                 }
01541                 else
01542                 {
01543                         header("Cache-Control: no-cache, must-revalidate");
01544                         header("Pragma: no-cache");
01545                 }
01546 
01547                 $ascii_filename = ilUtil::getASCIIFilename($a_filename);
01548 
01549                 if (strlen($charset))
01550                 {
01551                         $charset = "; charset=$charset";
01552                 }
01553                 header("Content-Type: $mime$charset");
01554                 header("Content-Disposition:$disposition; filename=\"".$ascii_filename."\"");
01555                 header("Content-Description: ".$ascii_filename);
01556                 header("Content-Length: ".(string)(strlen($a_data)));
01557 
01558                 if($_SERVER['HTTPS'])
01559                 {
01560             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
01561             header('Pragma: public');
01562                 }
01563 
01564                 header("Connection: close");
01565                 echo $a_data;
01566                 exit;
01567         }
01568 
01572         function deliverFile($a_file, $a_filename,$a_mime = '')
01573         {
01574                 $disposition = "attachment"; // "inline" to view file in browser or "attachment" to download to hard disk
01575 
01576                 if(strlen($a_mime))
01577                 {
01578                         $mime = $a_mime;
01579                 }
01580                 else
01581                 {
01582                         $mime = "application/octet-stream"; // or whatever the mime type is
01583                 }
01584                 if (isset($_SERVER["HTTPS"]))
01585                 {
01586                         // Added different handling for IE and HTTPS => send pragma after content informations
01587 
01591                         #header("Pragma: ");
01592                         #header("Pragma: no-cache");
01593                         #header("Cache-Control: ");
01594                         #header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
01595                         #header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
01596                         #header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
01597                         ##header("Cache-Control: post-check=0, pre-check=0", false);
01598                 }
01599                 else if ($disposition == "attachment")
01600                 {
01601                         header("Cache-control: private");
01602                 }
01603                 else
01604                 {
01605                         header("Cache-Control: no-cache, must-revalidate");
01606                         header("Pragma: no-cache");
01607                 }
01608 
01609                 $ascii_filename = ilUtil::getASCIIFilename($a_filename);
01610 
01611                 header("Content-Type: $mime");
01612                 header("Content-Disposition:$disposition; filename=\"".$ascii_filename."\"");
01613                 header("Content-Description: ".$ascii_filename);
01614                 header("Content-Length: ".(string)(filesize($a_file)));
01615 
01616                 if($_SERVER['HTTPS'])
01617                 {
01618             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
01619             header('Pragma: public');
01620                 }
01621 
01622                 header("Connection: close");
01623                 ilUtil::readFile( $a_file );
01624                 exit;
01625         }
01626 
01627 
01634         function readFile($a_file)
01635         {
01636                 $chunksize = 1*(1024*1024); // how many bytes per chunk
01637                 $buffer = '';
01638                 $handle = fopen($a_file, 'rb');
01639                 if ($handle === false)
01640                 {
01641                         return false;
01642                 }
01643                 while (!feof($handle))
01644                 {
01645                         $buffer = fread($handle, $chunksize);
01646                         print $buffer;
01647                 }
01648                 return fclose($handle);
01649         }
01650 
01656         function getASCIIFilename($a_filename)
01657         {
01658                 // The filename must be converted to ASCII, as of RFC 2183,
01659                 // section 2.3.
01660                 // Despite the RFC, Internet Explorer on Windows supports
01661                 // ISO 8895-1 encoding for the file name. We use this fact, to
01662                 // produce a better result, if the user uses IE.
01663 
01675 
01676                 $user_agent = strtolower($_SERVER["HTTP_USER_AGENT"]);
01677                 if ((is_integer(strpos($user_agent, "msie"))) && is_integer(strpos($user_agent, "win")))
01678                 {
01681 
01682                         $ascii_filename = utf8_decode($a_filename);
01683                 }
01684                 else
01685                 {
01688 
01689                         $ascii_filename = htmlentities($a_filename,ENT_NOQUOTES,'UTF-8');
01690                         $ascii_filename = preg_replace('/\&(.)[^;]*;/','\\1', $ascii_filename);
01691                         $ascii_filename = preg_replace('/[\x7f-\xff]/','_', $ascii_filename);
01692                 }
01693 
01694                 // Windows does not allow the following characters in filenames:
01695                 // \/:*?"<>|
01696                 if (is_integer(strpos($user_agent, "win")))
01697                 {
01698                         $ascii_filename = preg_replace('/[:\x5c\/\*\?\"<>\|]/','_', $ascii_filename);
01699                 }
01700 
01701                 return $ascii_filename;
01702         }
01703 
01707         function getJavaPath()
01708         {
01709                 return PATH_TO_JAVA;
01710                 //global $ilias;
01711 
01712                 //return $ilias->getSetting("java_path");
01713         }
01714 
01719         function appendUrlParameterString($a_url, $a_par)
01720         {
01721                 $url = (is_int(strpos($a_url, "?")))
01722                 ? $a_url."&".$a_par
01723                 : $a_url."?".$a_par;
01724 
01725                 return $url;
01726         }
01727 
01742         function makeDir($a_dir)
01743         {
01744                 $a_dir = trim($a_dir);
01745 
01746                 // remove trailing slash (bugfix for php 4.2.x)
01747                 if (substr($a_dir,-1) == "/")
01748                 {
01749                         $a_dir = substr($a_dir,0,-1);
01750                 }
01751 
01752                 // check if a_dir comes with a path
01753                 if (!($path = substr($a_dir,0, strrpos($a_dir,"/") - strlen($a_dir))))
01754                 {
01755                         $path = ".";
01756                 }
01757 
01758                 // create directory with file permissions of parent directory
01759                 umask(0000);
01760                 return @mkdir($a_dir,fileperms($path));
01761         }
01762 
01763 
01776         function makeDirParents($a_dir)
01777         {
01778                 $dirs = array($a_dir);
01779                 $a_dir = dirname($a_dir);
01780                 $last_dirname = '';
01781                 while($last_dirname != $a_dir)
01782                 {
01783                         array_unshift($dirs, $a_dir);
01784                         $last_dirname = $a_dir;
01785                         $a_dir = dirname($a_dir);
01786                 }
01787 
01788                 // find the first existing dir
01789                 $reverse_paths = array_reverse($dirs, TRUE);
01790                 $found_index = -1;
01791                 foreach ($reverse_paths as $key => $value)
01792                 {
01793                         if ($found_index == -1)
01794                         {
01795                                 if (is_dir($value))
01796                                 {
01797                                         $found_index = $key;
01798                                 }
01799                         }
01800                 }
01801 
01802                 umask(0000);
01803                 foreach ($dirs as $dirindex => $dir)
01804                 {
01805                         // starting with the longest existing path
01806                         if ($dirindex >= $found_index)
01807                         {
01808                                 if (! file_exists($dir))
01809                                 {
01810                                         if (strcmp(substr($dir,strlen($dir)-1,1),"/") == 0)
01811                                         {
01812                                                 // on some systems there is an error when there is a slash
01813                                                 // at the end of a directory in mkdir, see Mantis #2554
01814                                                 $dir = substr($dir,0,strlen($dir)-1);
01815                                         }
01816                                         if (! mkdir($dir, $umask))
01817                                         {
01818                                                 error_log("Can't make directory: $dir");
01819                                                 return false;
01820                                         }
01821                                 }
01822                                 elseif (! is_dir($dir))
01823                                 {
01824                                         error_log("$dir is not a directory");
01825                                         return false;
01826                                 }
01827                                 else
01828                                 {
01829                                         // get umask of the last existing parent directory
01830                                         $umask = fileperms($dir);
01831                                 }
01832                         }
01833                 }
01834                 return true;
01835         }
01836 
01844         function delDir($a_dir)
01845         {
01846                 if (!is_dir($a_dir) || is_int(strpos($a_dir, "..")))
01847                 {
01848                         return;
01849                 }
01850 
01851                 $current_dir = opendir($a_dir);
01852 
01853                 $files = array();
01854 
01855                 // this extra loop has been necessary because of a strange bug
01856                 // at least on MacOS X. A looped readdir() didn't work
01857                 // correctly with larger directories
01858                 // when an unlink happened inside the loop. Getting all files
01859                 // into the memory first solved the problem.
01860                 while($entryname = readdir($current_dir))
01861                 {
01862                         $files[] = $entryname;
01863                 }
01864 
01865                 foreach($files as $file)
01866                 {
01867                         if(is_dir($a_dir."/".$file) and ($file != "." and $file!=".."))
01868                         {
01869                                 ilUtil::delDir(${a_dir}."/".${file});
01870                         }
01871                         elseif ($file != "." and $file != "..")
01872                         {
01873                                 unlink(${a_dir}."/".${file});
01874                         }
01875                 }
01876 
01877                 closedir($current_dir);
01878                 rmdir(${a_dir});
01879         }
01880 
01881 
01885         function getDir($a_dir)
01886         {
01887                 $current_dir = opendir($a_dir);
01888 
01889                 $dirs = array();
01890                 $files = array();
01891                 while($entry = readdir($current_dir))
01892                 {
01893                         if(is_dir($a_dir."/".$entry))
01894                         {
01895                                 $dirs[$entry] = array("type" => "dir", "entry" => $entry);
01896                         }
01897                         else
01898                         {
01899                                 $size = filesize($a_dir."/".$entry);
01900                                 $files[$entry] = array("type" => "file", "entry" => $entry,
01901                                 "size" => $size);
01902                         }
01903                 }
01904                 ksort($dirs);
01905                 ksort($files);
01906 
01907                 return array_merge($dirs, $files);
01908         }
01909 
01910 
01918         function getGroupId($a_parent_ref)
01919         {
01920                 return false;
01921 
01922                 global $ilias;
01923 
01924                 $q = "SELECT DISTINCT tree FROM grp_tree WHERE child='".$a_parent_ref."'";
01925                 $r = $ilias->db->query($q);
01926                 $row = $r->fetchRow();
01927 
01928                 return $row[0] ? $row[0] : false;
01929         }
01930 
01936         function stripSlashes($a_str, $a_strip_html = true, $a_allow = "")
01937         {
01938                 if (ini_get("magic_quotes_gpc"))
01939                 {
01940                         $a_str = stripslashes($a_str);
01941                 }
01942 
01943                 // default behaviour: allow only fundamental tags 1:1
01944                 if ($a_allow == "" && $a_strip_html)
01945                 {
01946                         //$a_allow = "<b><i><strong><em><code><cite><gap><sub><sup><pre><strike>";
01947                         
01948                         $tags = array("b", "i", "strong", "em", "code", "cite", "gap", "sub", "sup", "pre", "strike");
01949                         
01950                         foreach ($tags as $t)           // mask allowed tags
01951                         {
01952                                 $a_str = str_replace(array("<$t>", "<".strtoupper($t).">"),
01953                                         "&lt;".$t."&gt;", $a_str);
01954                                 $a_str = str_replace(array("</$t>", "</".strtoupper($t).">"),
01955                                         "&lt;/".$t."&gt;", $a_str);
01956                         }
01957                         
01958                         $a_str = strip_tags($a_str);            // strip all other tags
01959                         
01960                         foreach ($tags as $t)           // mask allowed tags
01961                         {
01962                                 $a_str = str_replace("&lt;".$t."&gt;", "<".$t.">", $a_str);
01963                                 $a_str = str_replace("&lt;/".$t."&gt;", "</".$t.">", $a_str);
01964                         }
01965                         
01966                 }
01967                 else
01968                 {
01969                         // only for scripts, that need to allow more/other tags and parameters
01970                         if ($a_strip_html)
01971                         {
01972                                 $a_str = ilUtil::stripScriptHTML($a_str, $a_allow);
01973                         }
01974                 }
01975                 
01976                 return $a_str;
01977         }
01978 
01979 
01991         function stripScriptHTML($a_str, $a_allow = "", $a_rm_js = true)
01992         {
01993                 //$a_str = strip_tags($a_str, $a_allow);
01994 
01995                 $negativestr = "a,abbr,acronym,address,applet,area,b,base,basefont,".
01996                         "bdo,big,blockquote,body,br,button,caption,center,cite,code,col,".
01997                         "colgroup,dd,del,dfn,dir,div,dl,dt,em,fieldset,font,form,frame,".
01998                         "frameset,h1,h2,h3,h4,h5,h6,head,hr,html,i,iframe,img,input,ins,isindex,kbd,".
01999                         "label,legend,li,link,map,menu,meta,noframes,noscript,object,ol,".
02000                         "optgroup,option,p,param,q,s,samp,script,select,small,span,".
02001                         "strike,strong,style,sub,sup,table,tbody,td,textarea,tfoot,th,thead,".
02002                         "title,tr,tt,u,ul,var";
02003                 $a_allow = strtolower ($a_allow);
02004                 $negatives = split (",",$negativestr);
02005                 $outer_old_str = "";
02006                 while($outer_old_str != $a_str)
02007                 {
02008                         $outer_old_str = $a_str;
02009                         foreach ($negatives as $item)
02010                         {
02011                                 $pos = strpos($a_allow, "<$item>");
02012         
02013                                 // remove complete tag, if not allowed
02014                                 if ($pos === false)
02015                                 {
02016                                         $old_str = "";
02017                                         while($old_str != $a_str)
02018                                         {
02019                                                 $old_str = $a_str;
02020                                                 $a_str = preg_replace("/<\/?\s*$item(\/?)\s*>/i", "", $a_str);
02021                                                 $a_str = preg_replace("/<\/?\s*$item(\/?)\s+([^>]*)>/i", "", $a_str);
02022                                         }
02023                                 }
02024                         }
02025                 }
02026 
02027                 if ($a_rm_js)
02028                 {
02029                         // remove all attributes if an "on..." attribute is given
02030                         $a_str = preg_replace("/<\s*\w*(\/?)(\s+[^>]*)?(\s+on[^>]*)>/i", "", $a_str);
02031 
02032                         // remove all attributes if a "javascript" is within tag
02033                         $a_str = preg_replace("/<\s*\w*(\/?)\s+[^>]*javascript[^>]*>/i", "", $a_str);
02034                         
02035                         // remove all attributes if an "expression" is within tag
02036                         // (IE allows something like <b style='width:expression(alert(1))'>test</b>)
02037                         $a_str = preg_replace("/<\s*\w*(\/?)\s+[^>]*expression[^>]*>/i", "", $a_str);
02038                 }
02039 
02040                 return $a_str;
02041         }
02042 
02043 
02049         function addSlashes($a_str)
02050         {
02051                 if (ini_get("magic_quotes_gpc"))
02052                 {
02053                         return $a_str;
02054                 }
02055                 else
02056                 {
02057                         return addslashes($a_str);
02058                 }
02059         }
02060 
02070         function prepareFormOutput($a_str, $a_strip = false)
02071         {
02072                 if($a_strip)
02073                 {
02074                         $a_str = ilUtil::stripSlashes($a_str);
02075                 }
02076                 $a_str = htmlspecialchars($a_str);
02077                 // Added replacement of curly brackets to prevent
02078                 // problems with PEAR templates, because {xyz} will
02079                 // be removed as unused template variable
02080                 $a_str = str_replace("{", "&#123;", $a_str);
02081                 $a_str = str_replace("}", "&#125;", $a_str);
02082                 // needed for LaTeX conversion \\ in LaTeX is a line break
02083                 // but without this replacement, php changes \\ to \
02084                 $a_str = str_replace("\\", "&#92;", $a_str);
02085                 return $a_str;
02086         }
02087 
02088 
02096         function prepareDBString($a_str)
02097         {
02098                 return addslashes($a_str);
02099         }
02100 
02101 
02108         function removeItemFromDesktops($a_id)
02109         {
02110                 global $ilDB;
02111 
02112                 $q = "SELECT user_id FROM desktop_item WHERE item_id = '".$a_id."'";
02113                 $r = $ilDB->query($q);
02114 
02115                 $users = array();
02116 
02117                 while ($row = $r->fetchRow(DB_FETCHMODE_OBJECT))
02118                 {
02119                         $users[] = $row->user_id;
02120                 } // while
02121 
02122                 if (count($users) > 0)
02123                 {
02124                         $q = "DELETE FROM desktop_item WHERE item_id = '".$a_id."'";
02125                         $ilDB->query($q);
02126                 }
02127 
02128                 return $users;
02129         }
02130 
02131 
02139         function extractParameterString($a_parstr)
02140         {
02141                 // parse parameters in array
02142                 $par = array();
02143                 $ok=true;
02144                 while(($spos=strpos($a_parstr,"=")) && $ok)
02145                 {
02146                         // extract parameter
02147                         $cpar = substr($a_parstr,0,$spos);
02148                         $a_parstr = substr($a_parstr,$spos,strlen($a_parstr)-$spos);
02149                         while(substr($cpar,0,1)=="," ||substr($cpar,0,1)==" " || substr($cpar,0,1)==chr(13) || substr($cpar,0,1)==chr(10))
02150                         $cpar = substr($cpar,1,strlen($cpar)-1);
02151                         while(substr($cpar,strlen($cpar)-1,1)==" " || substr($cpar,strlen($cpar)-1,1)==chr(13) || substr($cpar,strlen($cpar)-1,1)==chr(10))
02152                         $cpar = substr($cpar,0,strlen($cpar)-1);
02153 
02154                         // parameter name should only
02155                         $cpar_old = "";
02156                         while($cpar != $cpar_old)
02157                         {
02158                                 $cpar_old = $cpar;
02159                                 $cpar = eregi_replace("[^a-zA-Z0-9_]", "", $cpar);
02160                         }
02161                         
02162                         // extract value
02163                         if ($cpar != "")
02164                         {
02165                                 if($spos=strpos($a_parstr,"\""))
02166                                 {
02167                                         $a_parstr = substr($a_parstr,$spos+1,strlen($a_parstr)-$spos);
02168                                         $spos=strpos($a_parstr,"\"");
02169                                         if(is_int($spos))
02170                                         {
02171                                                 $cval = substr($a_parstr,0,$spos);
02172                                                 $par[$cpar]=$cval;
02173                                                 $a_parstr = substr($a_parstr,$spos+1,strlen($a_parstr)-$spos-1);
02174                                         }
02175                                         else
02176                                         $ok=false;
02177                                 }
02178                                 else
02179                                 $ok=false;
02180                         }
02181                 }
02182 
02183                 if($ok) return $par; else return false;
02184         }
02185 
02186         function assembleParameterString($a_par_arr)
02187         {
02188                 if (is_array($a_par_arr))
02189                 {
02190                         $target_arr = array();
02191                         foreach ($a_par_arr as $par => $val)
02192                         {
02193                                 $target_arr[] = "$par=\"$val\"";
02194                         }
02195                         $target_str = implode(", ", $target_arr);
02196                 }
02197 
02198                 return $target_str;
02199         }
02200 
02204         function dumpString($a_str)
02205         {
02206                 $ret = $a_str.": ";
02207                 for($i=0; $i<strlen($a_str); $i++)
02208                 {
02209                         $ret.= ord(substr($a_str,$i,1))." ";
02210                 }
02211                 return $ret;
02212         }
02213 
02214 
02218         function yn2tf($a_yn)
02219         {
02220                 if(strtolower($a_yn) == "y")
02221                 {
02222                         return true;
02223                 }
02224                 else
02225                 {
02226                         return false;
02227                 }
02228         }
02229 
02233         function tf2yn($a_tf)
02234         {
02235                 if($a_tf)
02236                 {
02237                         return "y";
02238                 }
02239                 else
02240                 {
02241                         return "n";
02242                 }
02243         }
02244 
02253         function sort_func ($a, $b)
02254         {
02255                 global $array_sortby,$array_sortorder;
02256 
02257                 if ($array_sortorder == "asc")
02258                 {
02259                         return strcasecmp($a[$array_sortby], $b[$array_sortby]);
02260                 }
02261 
02262                 if ($array_sortorder == "desc")
02263                 {
02264                         return strcasecmp($b[$array_sortby], $a[$array_sortby]);
02265                 }
02266         }
02267 
02276         function sort_func_numeric ($a, $b)
02277         {
02278                 global $array_sortby,$array_sortorder;
02279 
02280                 if ($array_sortorder == "asc")
02281                 {
02282                         return $a["$array_sortby"] > $b["$array_sortby"];
02283                 }
02284 
02285                 if ($array_sortorder == "desc")
02286                 {
02287                         return $a["$array_sortby"] < $b["$array_sortby"];
02288                 }
02289         }
02300         function sortArray($array,$a_array_sortby,$a_array_sortorder = 0,$a_numeric = false)
02301         {
02302                 global $array_sortby,$array_sortorder;
02303 
02304                 $array_sortby = $a_array_sortby;
02305 
02306                 if ($a_array_sortorder == "desc")
02307                 {
02308                         $array_sortorder = "desc";
02309                 }
02310                 else
02311                 {
02312                         $array_sortorder = "asc";
02313                 }
02314                 if($a_numeric)
02315                 {
02316                         usort($array, array("ilUtil", "sort_func_numeric"));
02317                 }
02318                 else
02319                 {
02320                         usort($array, array("ilUtil", "sort_func"));
02321                 }
02322                 //usort($array,"ilUtil::sort_func");
02323 
02324                 return $array;
02325         }
02326 
02336         function unique_multi_array($array, $sub_key)
02337         {
02338                 $target = array();
02339                 $existing_sub_key_values = array();
02340 
02341                 foreach ($array as $key=>$sub_array)
02342                 {
02343                         if (!in_array($sub_array[$sub_key], $existing_sub_key_values))
02344                         {
02345                                 $existing_sub_key_values[] = $sub_array[$sub_key];
02346                                 $target[$key] = $sub_array;
02347                         }
02348                 }
02349 
02350                 return $target;
02351         }
02352 
02353 
02361         function getGDSupportedImageType($a_desired_type)
02362         {
02363                 $a_desired_type = strtolower($a_desired_type);
02364                 // get supported Image Types
02365                 $im_types = ImageTypes();
02366 
02367                 switch($a_desired_type)
02368                 {
02369                         case "jpg":
02370                         if ($im_types & IMG_JPG) return "jpg";
02371                         if ($im_types & IMG_GIF) return "gif";
02372                         if ($im_types & IMG_PNG) return "png";
02373                         break;
02374 
02375                         case "gif":
02376                         if ($im_types & IMG_GIF) return "gif";
02377                         if ($im_types & IMG_JPG) return "jpg";
02378                         if ($im_types & IMG_PNG) return "png";
02379                         break;
02380 
02381                         case "png":
02382                         if ($im_types & IMG_PNG) return "png";
02383                         if ($im_types & IMG_JPG) return "jpg";
02384                         if ($im_types & IMG_GIF) return "gif";
02385                         break;
02386                 }
02387 
02388                 return "";
02389         }
02390 
02398         function deducibleSize($a_mime)
02399         {
02400                 if (($a_mime == "image/gif") || ($a_mime == "image/jpeg") ||
02401                 ($a_mime == "image/png") || ($a_mime == "application/x-shockwave-flash") ||
02402                 ($a_mime == "image/tiff") || ($a_mime == "image/x-ms-bmp") ||
02403                 ($a_mime == "image/psd") || ($a_mime == "image/iff"))
02404                 {
02405                         return true;
02406                 }
02407                 else
02408                 {
02409                         return false;
02410                 }
02411         }
02412 
02413 
02419         function redirect($a_script)
02420         {
02421                 global $log, $PHP_SELF;
02422 //$log->write("redirect FROM:".$PHP_SELF." to ".$a_script);
02423                         //vd($a_script);
02424                 header("Location: ".$a_script);
02425                 exit();
02426         }
02427 
02433         function insertInstIntoID($a_value)
02434         {
02435                 if (substr($a_value, 0, 4) == "il__")
02436                 {
02437                         $a_value = "il_".IL_INST_ID."_".substr($a_value, 4, strlen($a_value) - 4);
02438                 }
02439 
02440                 return $a_value;
02441         }
02442 
02451         function groupNameExists($a_group_name,$a_id = 0)
02452         {
02453                 global $ilDB,$ilErr;
02454 
02455                 if (empty($a_group_name))
02456                 {
02457                         $message = get_class($this)."::_NameExists(): No groupname given!";
02458                         $ilErr->raiseError($message,$ilErr->WARNING);
02459                 }
02460 
02461                 $clause = ($a_id) ? " AND obj_id != '".$a_id."'" : "";
02462 
02463                 $q = "SELECT obj_id FROM object_data ".
02464                 "WHERE title = '".addslashes($a_group_name)."' ".
02465                 "AND type = 'grp'".
02466                 $clause;
02467 
02468                 $r = $ilDB->query($q);
02469 
02470                 if ($r->numRows())
02471                 {
02472                         return true;
02473                 }
02474                 else
02475                 {
02476                         return false;
02477                 }
02478         }
02479 
02480         /*
02481         * get the user_ids which correspond a search string
02482         * static function
02483         * @param        string search string
02484         * @access       public
02485         */
02486         function searchGroups($a_search_str)
02487         {
02488                 global $ilDB;
02489 
02490                 $q = "SELECT * ".
02491                         "FROM object_data ,object_reference ".
02492                         "WHERE (object_data.title LIKE '%".$a_search_str."%' ".
02493                         "OR object_data.description LIKE '%".$a_search_str."%') ".
02494                         "AND object_data.type = 'grp' ".
02495                         "AND object_data.obj_id = object_reference.obj_id ".
02496                         "ORDER BY title ";
02497 
02498                 $res = $ilDB->query($q);
02499 
02500                 while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
02501                 {
02502                         // STORE DATA IN ARRAY WITH KEY obj_id
02503                         // SO DUPLICATE ENTRIES ( LINKED OBJECTS ) ARE UNIQUE
02504                         $ids[$row->obj_id] = array(
02505                         "ref_id"        => $row->ref_id,
02506                         "title"         => $row->title,
02507                         "description"   => $row->description);
02508                 }
02509 
02510                 return $ids ? $ids : array();
02511         }
02512 
02516         function getMemString()
02517         {
02518                 $my_pid = getmypid();
02519                 return ("MEMORY USAGE (% KB PID ): ".`ps -eo%mem,rss,pid | grep $my_pid`);
02520         }
02521 
02525         function isWindows()
02526         {
02527                 if (strtolower(substr(php_uname(), 0, 3)) == "win")
02528                 {
02529                         return true;
02530                 }
02531                 return false;
02532         }
02533 
02534         function escapeShellArg($a_arg)
02535         {
02536                 global $PHP_OS;
02537 
02538                 if (ini_get("safe_mode") == 1 || ilUtil::isWindows())
02539                 {
02540                         return $a_arg;
02541                 }
02542                 else
02543                 {
02544                         return escapeshellarg($a_arg);
02545                 }
02546         }
02547 
02548         /*
02549         * Calculates a Microsoft Excel date/time value
02550         *
02551         * Calculates a Microsoft Excel date/time value (nr of days after 1900/1/1 0:00) for
02552         * a given date and time. The function only accepts dates after 1970/1/1, because the
02553         * unix timestamp functions used in the function are starting with that date.
02554         * If you don't enter parameters the date/time value for the actual date/time
02555         * will be calculated.
02556         *
02557         * static function
02558         *
02559         * @param        integer $year Year
02560         * @param        integer $month Month
02561         * @param        integer $day Day
02562         * @param        integer $hour Hour
02563         * @param        integer $minute Minute
02564         * @param        integer $second Second
02565         * @return float The Microsoft Excel date/time value
02566         * @access       public
02567         */
02568         function excelTime($year = "", $month = "", $day = "", $hour = "", $minute = "", $second = "")
02569         {
02570                 $starting_time = mktime(0, 0, 0, 1, 2, 1970);
02571                 if (strcmp("$year$month$day$hour$minute$second", "") == 0)
02572                 {
02573                         $target_time = time();
02574                 }
02575                 else
02576                 {
02577                         if ($year < 1970)
02578                         {
02579                                 return 0;
02580                         }
02581                 }
02582                 $target_time = mktime($hour, $minute, $second, $month, $day, $year);
02583                 $difference = $target_time - $starting_time;
02584                 $days = (($difference - ($difference % 86400)) / 86400);
02585                 $difference = $difference - ($days * 86400) + 3600;
02586                 return ($days + 25570 + ($difference / 86400));
02587         }
02588 
02592         function renameExecutables($a_dir)
02593         {
02594                 $def_arr = explode(",", SUFFIX_REPL_DEFAULT);
02595                 foreach ($def_arr as $def)
02596                 {
02597                         ilUtil::rRenameSuffix($a_dir, trim($def), "sec");
02598                 }
02599 
02600                 $def_arr = explode(",", SUFFIX_REPL_ADDITIONAL);
02601                 foreach ($def_arr as $def)
02602                 {
02603                         ilUtil::rRenameSuffix($a_dir, trim($def), "sec");
02604                 }
02605         }
02606 
02617         function rRenameSuffix ($a_dir, $a_old_suffix, $a_new_suffix)
02618         {
02619                 if ($a_dir == "/" || $a_dir == "" || is_int(strpos($a_dir, ".."))
02620                         || trim($a_old_suffix) == "")
02621                 {
02622                         return false;
02623                 }
02624 
02625                 // check if argument is directory
02626                 if (!@is_dir($a_dir))
02627                 {
02628                         return false;
02629                 }
02630 
02631                 // read a_dir
02632                 $dir = opendir($a_dir);
02633 
02634                 while($file = readdir($dir))
02635                 {
02636                         if ($file != "." and
02637                         $file != "..")
02638                         {
02639                                 // directories
02640                                 if (@is_dir($a_dir."/".$file))
02641                                 {
02642                                         ilUtil::rRenameSuffix($a_dir."/".$file, $a_old_suffix, $a_new_suffix);
02643                                 }
02644 
02645                                 // files
02646                                 if (@is_file($a_dir."/".$file))
02647                                 {
02648                                         // first check for files with trailing dot
02649                                         if(strrpos($file,'.') == (strlen($file) - 1))
02650                                         {
02651                                                 rename($a_dir.'/'.$file,substr($a_dir.'/'.$file,0,-1));
02652                                                 $file = substr($file,0,-1);
02653                                         } 
02654 
02655                                         $path_info = pathinfo($a_dir."/".$file);
02656                                         
02657                                         if (strtolower($path_info["extension"]) ==
02658                                         strtolower($a_old_suffix))
02659                                         {
02660                                                 $pos = strrpos($a_dir."/".$file, ".");
02661                                                 $new_name = substr($a_dir."/".$file, 0, $pos).".".$a_new_suffix;
02662                                                 rename($a_dir."/".$file, $new_name);
02663                                         }
02664                                 }
02665                         }
02666                 }
02667                 return true;
02668         }
02669 
02670         function isAPICall () {
02671                 return  strpos($_SERVER["SCRIPT_FILENAME"],"api") !== false ||
02672                 strpos($_SERVER["SCRIPT_FILENAME"],"dummy") !== false;
02673         }
02674 
02675         function KT_replaceParam($qstring, $paramName, $paramValue) {
02676                 if (preg_match("/&" . $paramName . "=/", $qstring)) {
02677                         return preg_replace("/&" . $paramName . "=[^&]+/", "&" . $paramName . "=" . urlencode($paramValue), $qstring);
02678                 } else {
02679                         return $qstring . "&" . $paramName . "=" . urlencode($paramValue);
02680                 }
02681         }
02682 
02683         function replaceUrlParameterString ($url, $parametersArray) {
02684 
02685                 foreach ($parametersArray as $paramName => $paramValue ) {
02686                         $url = ilUtil::KT_replaceParam($url, $paramName, $paramValue);
02687                 }
02688                 return $url;
02689         }
02690 
02691     function generatePasswords ($a_number)
02692     {
02693         $ret = array();
02694         srand((double) microtime()*1000000);
02695 
02696         for ($i=1; $i<=$a_number; $i++)
02697         {
02698             $length  = rand(6,10);
02699             $next  = rand(1,2);
02700             //$chars = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
02701             $vowels = "aeiou";
02702             $consonants = "bcdfghjklmnpqrstvwxyz";
02703             $pw = "";
02704 
02705             for ($j=0; $j < $length; $j++)
02706             {
02707                 switch ($next)
02708                 {
02709                     case 1:
02710                                         $pw.= $consonants[rand(0,strlen($consonants)-1)];
02711                                         $next = 2;
02712                     break;
02713 
02714                     case 2:
02715                                         $pw.= $vowels[rand(0,strlen($vowels)-1)];
02716                                         $next = 1;
02717                                 break;
02718                 }
02719              }
02720 
02721              $ret[] = $pw;
02722          }
02723 
02724          return $ret;
02725     }
02726 
02727         function removeTrailingPathSeparators($path)
02728         {
02729                 $path = preg_replace("/[\/\\\]+$/", "", $path);
02730                 return $path;
02731         }
02732 
02741         function array_php2js($data)
02742         {
02743                 if (empty($data))
02744                 {
02745                         $data = array();
02746                 }
02747 
02748                 foreach($data as $k=>$datum)
02749                 {
02750                         if(is_null($datum)) $data[$k] = 'null';
02751                         if(is_string($datum)) $data[$k] = "'" . $datum . "'";
02752                         if(is_array($datum)) $data[$k] = array_php2js($datum);
02753                 }
02754 
02755                 return "[" . implode(', ', $data) . "]";
02756         }
02757 
02762         function virusHandling($a_file, $a_orig_name = "", $a_clean = true)
02763         {
02764                 global $lng;
02765 
02766                 if (IL_VIRUS_SCANNER != "None")
02767                 {
02768                         require_once("classes/class.ilVirusScannerFactory.php");
02769                         $vs = ilVirusScannerFactory::_getInstance();
02770                         if (($vs_txt = $vs->scanFile($a_file, $a_orig_name)) != "")
02771                         {
02772                                 if ($a_clean && (IL_VIRUS_CLEAN_COMMAND != ""))
02773                                 {
02774                                         $clean_txt = $vs->cleanFile($a_file, $a_orig_name);
02775                                         if ($vs->fileCleaned())
02776                                         {
02777                                                 $vs_txt.= "<br />".$lng->txt("cleaned_file").
02778                                                         "<br />".$clean_txt;
02779                                                 $vs_txt.= "<br />".$lng->txt("repeat_scan");
02780                                                 if (($vs2_txt = $vs->scanFile($a_file, $a_orig_name)) != "")
02781                                                 {
02782                                                         return array(false, nl2br($vs_txt)."<br />".$lng->txt("repeat_scan_failed").
02783                                                                 "<br />".nl2br($vs2_txt));
02784                                                 }
02785                                                 else
02786                                                 {
02787                                                         return array(true, nl2br($vs_txt)."<br />".$lng->txt("repeat_scan_succeded"));
02788                                                 }
02789                                         }
02790                                         else
02791                                         {
02792                                                 return array(false, nl2br($vs_txt)."<br />".$lng->txt("cleaning_failed"));
02793                                         }
02794                                 }
02795                                 else
02796                                 {
02797                                         return array(false, nl2br($vs_txt));
02798                                 }
02799                         }
02800                 }
02801                 return array(true,"");
02802         }
02803 
02804 
02808         function moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors = true)
02809         {
02810                 global $lng, $ilias;
02811 //echo "<br>ilUtli::moveuploadedFile($a_name)";
02812                 if (!is_file($a_file))
02813                 {
02814                         $lng->txt("upload_error_file_not_found");
02815                 }
02816 
02817                 // virus handling
02818                 $vir = ilUtil::virusHandling($a_file, $a_name);
02819                 if (!$vir[0])
02820                 {
02821                         unlink($a_file);
02822                         if ($a_raise_errors)
02823                         {
02824                                 $ilias->raiseError($lng->txt("file_is_infected")."<br />".
02825                                         $vir[1],
02826                                         $ilias->error_obj->MESSAGE);
02827                         }
02828                         else
02829                         {
02830                                 sendInfo($lng->txt("file_is_infected")."<br />".
02831                                         $vir[1], true);
02832                         }
02833                         return false;
02834                 }
02835                 else
02836                 {
02837                         if ($vir[1] != "")
02838                         {
02839                                 sendInfo($vir[1], true);
02840                         }
02841                         return move_uploaded_file($a_file, $a_target);
02842                 }
02843         }
02844 
02845 
02849          function date_mysql2time($mysql_date_time) {
02850                 list($datum, $uhrzeit) = explode (" ",$mysql_date_time);
02851                 list($jahr, $monat, $tag) = explode("-", $datum);
02852                 list($std, $min, $sec) = explode(":", $uhrzeit);
02853                 return mktime ((int) $std, (int) $min, (int) $sec, (int) $monat, (int) $tag, (int) $jahr);
02854          }
02855 
02869         function &processCSVRow(&$row, $quoteAll = FALSE, $separator = ";", $outUTF8 = FALSE, $compatibleWithMSExcel = TRUE)
02870         {
02871                 $resultarray = array();
02872                 foreach ($row as $rowindex => $entry)
02873                 {
02874                         $surround = FALSE;
02875                         if ($quoteAll)
02876                         {
02877                                 $surround = TRUE;
02878                         }
02879                         if (strpos($entry, "\"") !== FALSE)
02880                         {
02881                                 $entry = str_replace("\"", "\"\"", $entry);
02882                                 $surround = TRUE;
02883                         }
02884                         if (strpos($entry, $separator) !== FALSE)
02885                         {
02886                                 $surround = TRUE;
02887                         }
02888                         if ($compatibleWithMSExcel)
02889                         {
02890                                 // replace all CR LF with LF (for Excel for Windows compatibility
02891                                 $entry = str_replace(chr(13).chr(10), chr(10), $entry);
02892                         }
02893                         if ($surround)
02894                         {
02895                                 if ($outUTF8)
02896                                 {
02897                                         $resultarray[$rowindex] = "\"" . $entry . "\"";
02898                                 }
02899                                 else
02900                                 {
02901                                         $resultarray[$rowindex] = utf8_decode("\"" . $entry . "\"");
02902                                 }
02903                         }
02904                         else
02905                         {
02906                                 if ($outUTF8)
02907                                 {
02908                                         $resultarray[$rowindex] = $entry;
02909                                 }
02910                                 else
02911                                 {
02912                                         $resultarray[$rowindex] = utf8_decode($entry);
02913                                 }
02914                         }
02915                 }
02916                 return $resultarray;
02917         }
02918 
02919         // validates a domain name (example: www.ilias.de)
02920         function isDN($a_str)
02921         {
02922                 return(preg_match("/^[a-z]+([a-z0-9-]*[a-z0-9]+)?(\.([a-z]+([a-z0-9-]*[a-z0-9]+)?)+)*$/",$a_str));
02923         }
02924 
02925         // validates an IP address (example: 192.168.1.1)
02926         function isIPv4($a_str)
02927         {
02928                 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])\.".
02929                                                   "(\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));
02930         }
02931 
02932 
02959         function _getObjectsByOperations($a_obj_type,$a_operation,$a_usr_id = 0,$limit = 0)
02960         {
02961                 global $ilDB,$rbacreview,$ilAccess,$ilUser,$ilias;
02962 
02963                 if(!is_array($a_obj_type))
02964                 {
02965                         $where = "WHERE type = '".$a_obj_type."' ";
02966                 }
02967                 else
02968                 {
02969                         $where = "WHERE type IN('";
02970                         $where .= implode("','",$a_obj_type);
02971                         $where .= "') ";
02972                 }
02973 
02974 
02975                 // limit number of results default is search result limit
02976                 if(!$limit)
02977                 {
02978                         $limit = $ilias->getSetting('search_max_hits',100);
02979                 }
02980                 if($limit = -1)
02981                 {
02982                         $limit = 10000;
02983                 }
02984 
02985                 // default to logged in usr
02986                 $a_usr_id = $a_usr_id ? $a_usr_id : $ilUser->getId();
02987                 $a_roles = $rbacreview->assignedRoles($a_usr_id);
02988 
02989                 // Since no rbac_pa entries are available for the system role. This function returns !all! ref_ids in the case the user
02990                 // is assigned to the system role
02991                 if($rbacreview->isAssigned($a_usr_id,SYSTEM_ROLE_ID))
02992                 {
02993                         $query = "SELECT ref_id FROM object_reference AS obr LEFT JOIN object_data AS obd USING(obj_id) ".
02994                                 "LEFT JOIN tree ON obr.ref_id = tree.child ".
02995                                 $where.
02996                                 "AND tree = 1";
02997 
02998                         $res = $ilDB->query($query);
02999                         $counter = 0;
03000                         while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
03001                         {
03002                                 if(++$counter == $limit)
03003                                 {
03004                                         break;
03005                                 }
03006                                 $ref_ids[] = $row->ref_id;
03007                         }
03008                         return $ref_ids ? $ref_ids : array();
03009                 }
03010 
03011                 $ops_ids = ilRbacReview::_getOperationIdsByName(array($a_operation));
03012                 $ops_id = $ops_ids[0];
03013 
03014                 $query = "SELECT DISTINCT(obr.ref_id),obr.obj_id,type FROM rbac_pa ".
03015                         "LEFT JOIN object_reference AS obr  ON obr.ref_id = rbac_pa.ref_id ".
03016                         "LEFT JOIN object_data AS obd ON obd.obj_id = obr.obj_id ".
03017                         $where.
03018                         "AND ops_id LIKE '%i:".$ops_id."%'";
03019                 $res = $ilDB->query($query);
03020                 $counter = 0;
03021                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
03022                 {
03023                         if($counter >= $limit)
03024                         {
03025                                 break;
03026                         }
03027                         // Check deleted, hierarchical access ...
03028                         if($ilAccess->checkAccessOfUser($a_usr_id,$a_operation,'',$row->ref_id,$row->type,$row->obj_id))
03029                         {
03030                                 $counter++;
03031                                 $ref_ids[] = $row->ref_id;
03032                         }
03033                 }
03034                 return $ref_ids ? $ref_ids : array();
03035         }
03036 
03043         function insertLatexImages($a_text, $a_start = "\[tex\]", $a_end = "\[\/tex\]", $a_cgi = URL_TO_LATEX)
03044         {
03045                 // - take care of html exports (-> see buildLatexImages)
03046                 $result_text = preg_replace('/' . $a_start . '(.*?)' . $a_end . '/ie',
03047                         "'<img alt=\"'.htmlentities('$1').'\" src=\"$a_cgi?'.rawurlencode(str_replace('&amp;', '&', str_replace('&gt;', '>', str_replace('&lt;', '<', '$1')))).'\" ".
03048                         " />'", $a_text);
03049                 return $result_text;
03050         }
03051 
03058         function buildLatexImages($a_text, $a_dir ,$a_start = "\[tex\]", $a_end = "\[\/tex\]", $a_cgi = URL_TO_LATEX)
03059         {
03060                 $result_text = $a_text;
03061         
03062                 if ($a_cgi != "")
03063                 {
03064                         while (preg_match('/' . $a_start . '(.*?)' . $a_end . '/ie', $result_text, $found))
03065                         {
03066                                 $cnt = (int) $GLOBALS["teximgcnt"]++;
03067                                 // get image from cgi and write it to file
03068                                 $fpr = fopen($a_cgi."?".rawurlencode($found[1]), "r");
03069                                 $lcnt = 0;
03070                                 while(!feof($fpr))
03071                                 {
03072                                         $buf = fread($fpr, 1024);
03073                                         if ($lcnt == 0)
03074                                         {
03075                                                 if (is_int(strpos(strtoupper(substr($buf, 0, 5)), "GIF")))
03076                                                 {
03077                                                         $suffix = "gif";
03078                                                 }
03079                                                 else
03080                                                 {
03081                                                         $suffix = "png";
03082                                                 }
03083                                                 $fpw = fopen($a_dir."/teximg/img".$cnt.".".$suffix, "w");
03084                                         }
03085                                         $lcnt++;
03086                                         fwrite($fpw, $buf);
03087                                 }
03088                                 fclose($fpw);
03089                                 fclose($fpr);
03090         
03091                                 // replace tex-tag
03092                                 $img_str = "./teximg/img".$cnt.".".$suffix;
03093                                 $result_text = str_replace($found[0],
03094                                         '<img alt="'.$found[1].'" src="'.$img_str.'" />', $result_text);
03095         
03096                         }
03097                 }
03098 
03099                 return $result_text;
03100         }
03101 
03108         function int2array ($seconds, $periods = null)
03109         {
03110                 // Define time periods
03111                 if (!is_array($periods))
03112                 {
03113                         $periods = array (
03114                         'years' => 31536000,
03115                         'months' => 2592000,
03116                         'days'  => 86400,
03117                         'hours' => 3600,
03118                         'minutes' => 60,
03119                         'seconds' => 1
03120                         );
03121                 }
03122 
03123                 // Loop
03124                 $seconds = (float) $seconds;
03125                 foreach ($periods as $period => $value)
03126                 {
03127                         $count = floor($seconds / $value);
03128 
03129                         if ($count == 0)
03130                         {
03131                                 continue;
03132                         }
03133 
03134                         $values[$period] = $count;
03135                         $seconds = $seconds % $value;
03136                 }
03137                 // Return
03138                 if (empty($values))
03139                 {
03140                         $values = null;
03141                 }
03142 
03143                 return $values;
03144         }
03145 
03153         function timearray2string ($duration)
03154         {
03155                 global $lng;
03156 
03157                 if (!is_array($duration))
03158                 {
03159                         return false;
03160                 }
03161 
03162                 foreach ($duration as $key => $value) {
03163 
03164                         // Plural
03165                         if ($value > 1)
03166                         {
03167                                 $segment_name = $key;
03168                                 $segment_name = $lng->txt($segment_name);
03169                                 $segment = $value . ' ' . $segment_name;
03170                         }
03171                         else
03172                         {
03173                                 $segment_name = substr($key, 0, -1);
03174                                 $segment_name = $lng->txt($segment_name);
03175                                 $segment = $value . ' ' . $segment_name;
03176                         }
03177 
03178                         $array[] = $segment;
03179                 }
03180                 $len = count($array);
03181 
03182                 if ($len>3)
03183                 {
03184                         $array=array_slice($array,0,(3-$len));
03185         }
03186 
03187                 $str = implode(', ', $array);
03188 
03189                 return $str;
03190         }
03191 
03192         function getFileSizeInfo()
03193         {
03194                 global $lng;
03195 
03196                 // get the value for the maximal uploadable filesize from the php.ini (if available)
03197                 $umf=get_cfg_var("upload_max_filesize");
03198                 // get the value for the maximal post data from the php.ini (if available)
03199                 $pms=get_cfg_var("post_max_size");
03200 
03201                 // use the smaller one as limit
03202                 $max_filesize=min($umf, $pms);
03203                 if (!$max_filesize) $max_filesize=max($umf, $pms);
03204 
03205                 return $lng->txt("file_notice")." $max_filesize.";
03206          }
03207 
03214         function __extractRefId($role_title)
03215         {
03216 
03217                 $test_str = explode('_',$role_title);
03218 
03219                 if ($test_str[0] == 'il')
03220                 {
03221                         $test2 = (int) $test_str[3];
03222                         return is_numeric ($test2) ? (int) $test2 : false;
03223                 }
03224                 return false;
03225         }
03226 
03236         function __extractId($ilias_id, $inst_id)
03237         {
03238 
03239                 $test_str = explode('_',$ilias_id);
03240 
03241                 if ($test_str[0] == 'il' && $test_str[1] == $inst_id && count($test_str) == 4)
03242                 {
03243                         $test2 = (int) $test_str[3];
03244                         return is_numeric ($test2) ? (int) $test2 : false;
03245                 }
03246                 return false;
03247         }
03248 
03261         function _sortIds($a_ids,$a_table,$a_field,$a_id_name)
03262         {
03263                 global $ilDB;
03264 
03265                 if(!$a_ids)
03266                 {
03267                         return array();
03268                 }
03269 
03270                 // use database to sort user array
03271                 $where = "WHERE ".$a_id_name." IN ('";
03272                 $where .= implode("','",$a_ids);
03273                 $where .= "') ";
03274 
03275                 $query = "SELECT ".$a_id_name." FROM ".$a_table." ".
03276                         $where.
03277                         "ORDER BY ".$a_field;
03278 
03279                 $res = $ilDB->query($query);
03280                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
03281                 {
03282                         $ids[] = $row->$a_id_name;
03283                 }
03284                 return $ids ? $ids : array();
03285         }
03286         
03294         function getMySQLTimestamp($a_ts)
03295         {
03296                 global $ilDB;
03297 
03298                 if ($ilDB->isMysql4_1OrHigher())
03299                 {
03300                         return $a_ts;
03301                 }
03302                 else
03303                 {
03304                         $t = $a_ts;
03305                         $ts = substr($t, 0, 4)."-".substr($t, 4, 2)."-".substr($t, 6, 2).
03306                                 " ".substr($t, 8, 2).":".substr($t, 10, 2).":".substr($t, 12, 2);
03307                         return $ts;
03308                 }
03309         }
03310 
03311 } // END class.ilUtil
03312 
03313 
03314 ?>

Generated on Fri Dec 13 2013 13:52:08 for ILIAS Release_3_7_x_branch .rev 46817 by  doxygen 1.7.1