• Main Page
  • Related Pages
  • 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-2001 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 
00024 
00034 class ilUtil
00035 {
00043         function getImageTagByType($a_type, $a_path, $a_big = false)
00044         {
00045                 global $lng;
00046 
00047                 if ($a_big)
00048                 {
00049                         $big = "_b";
00050                 }
00051                 $filename = "icon_".$a_type."$big.gif";
00052 
00053                 return "<img src=\"".ilUtil::getImagePath($filename)."\" alt=\"".$lng->txt("obj_".$a_type)."\" title=\"".$lng->txt("obj_".$a_type)."\" border=\"0\" vspace=\"0\"/>";
00054                 //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\"/>";
00055         }
00056 
00065         function getImagePath($img, $in_module = false, $mode = "output", $offline = false)
00066         {
00067                 global $ilias, $styleDefinition;
00068 
00069                 if(defined("ILIAS_MODULE") and !defined("KEEP_IMAGE_PATH") and $mode != "filesystem")
00070                 {
00071                         $dir = ".";
00072                 }
00073                 else
00074                 {
00075                         $dir = "";
00076                 }
00077                 $base = "./";
00078                 if ($in_module)
00079                 {
00080                         $base.= ILIAS_MODULE."/";
00081                 }
00082                 $base .= "templates/";
00083 
00084                 if (is_object($styleDefinition))
00085                 {
00086                         $st_image_dir = $styleDefinition->getImageDirectory($ilias->account->prefs["style"]);
00087                         $user_skin_and_style = $base.$ilias->account->skin."/".
00088                         $st_image_dir.
00089                         "/images/".$img;
00090                 }
00091                 $user_skin = $base.$ilias->account->skin."/images/".$img;
00092                 $default = $base."default/images/".$img;
00093                 if ($offline)
00094                 {
00095                         return "./images/".$img;
00096                 }
00097                 else if (@file_exists($user_skin_and_style) && $st_image_dir != "")
00098                 {
00099                         return $dir.$user_skin_and_style;
00100                 }
00101                 else if (file_exists($user_skin))
00102                 {
00103                         return $dir.$user_skin;
00104                 }
00105 
00106                 return $dir.$default;
00107         }
00108 
00117     function getHtmlPath($relative_path)
00118     {
00119         if (substr($relative_path, 0, 2) == './')
00120         {
00121             $relative_path = (substr($relative_path, 1));
00122         }
00123         if (substr($relative_path, 0, 1) != '/')
00124         {
00125             $relative_path = '/' . $relative_path;
00126         }
00127         $htmlpath = ILIAS_HTTP_PATH . $relative_path;
00128         return $htmlpath;
00129     }
00130 
00131         function getJSPath($a_js)
00132         {
00133                 global $ilias;
00134 
00135                 if(defined("ILIAS_MODULE"))
00136                 {
00137                         $dir = ".";
00138                 }
00139                 else
00140                 {
00141                         $dir = "";
00142                 }
00143                 $in_style = "./templates/".$ilias->account->skin."/".$ilias->account->prefs["style"]."/".$a_js;
00144                 $default = "./templates/".$ilias->account->skin."/".$a_js;
00145                 if(@is_file($in_style))
00146                 {
00147                         return $dir.$in_style;
00148                 }
00149                 else
00150                 {
00151                         return $dir.$default;
00152                 }
00153         }
00154 
00160         function getStyleSheetLocation($mode = "output")
00161         {
00162                 global $ilias;
00163 
00164                 if(defined("ILIAS_MODULE") && $mode != "filesystem")
00165                 {
00166                         $base = "../";
00167                 }
00168                 else
00169                 {
00170                         $base = "./";
00171                 }
00172                 return $base."templates/".$ilias->account->skin."/".$ilias->account->prefs["style"].".css";
00173         }
00174 
00187         function formSelect ($selected,$varname,$options,$multiple = false,$direct_text = false, $size = "0")
00188         {
00189                 global $lng;
00190 
00191                 if ($multiple == true)
00192                 {
00193                         $multiple = "multiple";
00194                 }
00195                 else
00196                 {
00197                         $multiple = "";
00198                         $size = 0;
00199                 }
00200 
00201                 $str = "<select name=\"".$varname ."\"".$multiple." size=\"".$size."\">\n";
00202 
00203                 foreach ($options as $key => $val)
00204                 {
00205                         if ($direct_text)
00206                         {
00207                                 $str .= " <option value=\"".$key."\"";
00208                         }
00209                         else
00210                         {
00211                                 $str .= " <option value=\"".$val."\"";
00212                         }
00213                         if (is_array($selected) )
00214                         {
00215                                 if (in_array($key,$selected))
00216                                 {
00217                                         $str .= " selected=\"selected\"";
00218                                 }
00219                         }
00220                         else if ($selected == $key)
00221                         {
00222                                 $str .= " selected=\"selected\"";
00223                         }
00224 
00225                         if ($direct_text)
00226                         {
00227                                 $str .= ">".$val."</option>\n";
00228                         }
00229                         else
00230                         {
00231                                 $str .= ">".$lng->txt($val)."</option>\n";
00232                         }
00233                 }
00234 
00235                 $str .= "</select>\n";
00236 
00237                 return $str;
00238         }
00239 
00247         function getSelectName ($selected,$values)
00248         {
00249                 return($values[$selected]);
00250         }
00251 
00261         function formCheckbox ($checked,$varname,$value,$disabled = false)
00262         {
00263                 $str = "<input type=\"checkbox\" name=\"".$varname."\"";
00264 
00265                 if ($checked == 1)
00266                 {
00267                         $str .= " checked=\"checked\"";
00268                 }
00269 
00270                 if ($disabled)
00271                 {
00272                         $str .= " disabled=\"disabled\"";
00273                 }
00274                 
00275                 $array_var = false;
00276                 
00277                 if (substr($varname,-2) == "[]")
00278                 {
00279                         $array_var = true;
00280                 }
00281 
00282                 // if varname ends with [], use varname[-2] + _ + value as id tag (e.g. "user_id[]" => "user_id_15")
00283                 if ($array_var)
00284                 {
00285                         $varname_id = substr($varname,0,-2)."_".$value;
00286                 }
00287                 else
00288                 {
00289                         $varname_id = $varname;
00290                 }
00291                 
00292                 // dirty removal of other "[]" in string
00293                 $varname_id = ereg_replace("\[","_",$varname_id);
00294                 $varname_id = ereg_replace("\]","",$varname_id);
00295 
00296                 $str .= " value=\"".$value."\" id=\"".$varname_id."\" />\n";
00297 
00298                 return $str;
00299         }
00300 
00309         function formRadioButton($checked,$varname,$value)
00310         {
00311                 $str = "<input type=\"radio\" name=\"".$varname."\"";
00312                 if ($checked == 1)
00313                 {
00314                         $str .= " checked=\"checked\"";
00315                 }
00316 
00317                 $str .= " value=\"".$value."\"";
00318                 
00319                 $str .= " id=\"".$value."\" />\n";
00320 
00321                 return $str;
00322         }
00323 
00328         function checkInput ($vars)
00329         {
00330                 // TO DO:
00331                 // Diese Funktion soll Formfeldeingaben berprfen (empty und required)
00332         }
00333 
00339         function setPathStr ($a_path)
00340         {
00341                 if ("" != $a_path && "/" != substr($a_path, -1))
00342                 {
00343                         $a_path .= "/";
00344                         //$a_path = substr($a_path,1);
00345                 }
00346 
00347                 //return getcwd().$a_path;
00348                 return $a_path;
00349         }
00350 
00361         function switchColor ($a_num,$a_css1,$a_css2)
00362         {
00363                 if (!($a_num % 2))
00364                 {
00365                         return $a_css1;
00366                 }
00367                 else
00368                 {
00369                         return $a_css2;
00370                 }
00371         }
00372 
00380         function showTabs($a_hl, $a_o)
00381         {
00382                 global $lng;
00383 
00384                 $tpltab = new ilTemplate("tpl.tabs.html", true, true);
00385 
00386                 for ($i=1; $i<=4; $i++)
00387                 {
00388                         $tpltab->setCurrentBlock("tab");
00389                         if ($a_hl == $i)
00390                         {
00391                                 $tabtype = "tabactive";
00392                                 $tab = $tabtype;
00393                         }
00394                         else
00395                         {
00396                                 $tabtype = "tabinactive";
00397                                 $tab = "tab";
00398                         }
00399 
00400                         switch ($i)
00401                         {
00402                                 case 1:
00403                                 $txt = $lng->txt("view_content");
00404                                 break;
00405                                 case 2:
00406                                 $txt = $lng->txt("edit_properties");
00407                                 break;
00408                                 case 3:
00409                                 $txt = $lng->txt("perm_settings");
00410                                 break;
00411                                 case 4:
00412                                 $txt = $lng->txt("show_owner");
00413                                 break;
00414                         } // switch
00415                         $tpltab->setVariable("CONTENT", $txt);
00416                         $tpltab->setVariable("TABTYPE", $tabtype);
00417                         $tpltab->setVariable("TAB", $tab);
00418                         $tpltab->setVariable("LINK", $a_o["LINK".$i]);
00419                         $tpltab->parseCurrentBlock();
00420                 }
00421 
00422                 return $tpltab->get();
00423         }
00424 
00449         function getObjectsByOperations($a_type,$a_operation,$a_checkpath = true)
00450         {
00451                 global $ilDB, $rbacsystem, $tree;
00452                 
00453                 $objects = array();
00454                 
00455                 // return if root node ist not accessible
00456                 if ($a_checkpath === true and !$rbacsystem->checkAccess('read', ROOT_FOLDER_ID, $a_type))
00457                 {
00458                         return $objects;
00459                 }
00460 
00461                 // get all nodes of a_type that which are not deleted
00462                 $q = "SELECT * FROM tree ".
00463                      "LEFT JOIN object_reference ON tree.child=object_reference.ref_id ".
00464                          "LEFT JOIN object_data ON object_reference.obj_id=object_data.obj_id ".
00465                          "WHERE object_data.type = '".$a_type."' ".
00466                          "AND tree.tree = 1";
00467 
00468                 $r = $ilDB->query($q);
00469                 
00470                 // check the desired operation of all found nodes
00471                 while ($row = $r->fetchRow(DB_FETCHMODE_ASSOC))
00472                 {
00473                         if ($rbacsystem->checkAccess($a_operation, $row["ref_id"], $a_type))
00474                         {
00475                                 $objects[] = $row;
00476                         }
00477                 }
00478                 
00479                 // check access of nodes up in the tree for all accessible nodes
00480                 if ($a_checkpath === true)
00481                 {
00482                         $nodes_checked = array(ROOT_FOLDER_ID);
00483                         $nodes_passed = array(ROOT_FOLDER_ID);
00484 
00485                         foreach ($objects as $key => $node)
00486                         {
00487                                 $path = $tree->getPathId($node["ref_id"],ROOT_FOLDER_ID);
00488                                 
00489                                 array_push($nodes_checked,$node["ref_id"]);
00490                                 array_push($nodes_passed,$node["ref_id"]);
00491 
00492                                 if (($num = count($path)) > 2)
00493                                 {
00494                                     for ($i = 1; $i <= ($num - 2); $i++)
00495                                     {
00496 //echo "<br/>i:".$i;
00497                                                 if (in_array($path[$i],$nodes_passed))
00498                                                 {
00499 //echo "<br/>".$i." already passed";
00500                                                         continue;
00501                                                 }
00502                                                 
00503                                                 if (!in_array($path[$i],$nodes_checked))
00504                                                 {
00505                                                     array_push($nodes_checked,$path[$i]);
00506 //echo "<br/>".$i." add to checked";
00507                                                         if ($rbacsystem->checkAccess($a_operation, $path[$i], $a_type))
00508                                                         {
00509                                                             array_push($nodes_passed,$path[$i]);
00510 //echo "<br/>".$i." add to passed";
00511                                                         }
00512                                                         else
00513                                                         {
00514                                                                 unset($objects[$key]);
00515                                                                 // break for
00516                                                                 break;
00517                                                         }
00518                                                 }
00519                                         } // end for
00520                                 } // end if $num
00521 //var_dump("<pre>",$node["ref_id"],$path,$num,"</pre>");
00522                         } // end foreach
00523 //var_dump("<pre>",$nodes_checked,$nodes_passed,"</pre>");
00524                 }
00525                 
00526                 unset($nodes_checked);
00527                 unset($nodes_passed);
00528 
00529                 return $objects;
00530         }
00531 
00532 
00539         function checkFormEmpty ($emptyFields)
00540         {
00541 
00542                 $feedback = "";
00543 
00544                 foreach ($emptyFields as $key => $val)
00545                 {
00546                         if ($val == "") {
00547                                 if ($feedback != "") $feedback .= ", ";
00548                                 $feedback .= $key;
00549                         }
00550                 }
00551 
00552                 return $feedback;
00553         }
00554 
00577         function Linkbar ($AScript,$AHits,$ALimit,$AOffset,$AParams = array(),$ALayout = array())
00578         {
00579                 $LinkBar = "";
00580 
00581                 $layout_link = "";
00582                 $layout_prev = "&lt;&lt;";
00583                 $layout_next = "&gt;&gt;";
00584 
00585                 // layout options
00586                 if (count($ALayout > 0))
00587                 {
00588                         if ($ALayout["link"])
00589                         {
00590                                 $layout_link = " class=\"".$ALayout["link"]."\"";
00591                         }
00592 
00593                         if ($ALayout["prev"])
00594                         {
00595                                 $layout_prev = $ALayout["prev"];
00596                         }
00597 
00598                         if ($ALayout["next"])
00599                         {
00600                                 $layout_next = $ALayout["next"];
00601                         }
00602                 }
00603 
00604                 // Wenn Hits gr?sser Limit, zeige Links an
00605                 if ($AHits > $ALimit)
00606                 {
00607                         if (!empty($AParams))
00608                         {
00609                                 foreach ($AParams as $key => $value)
00610                                 {
00611                                         $params.= $key."=".$value."&";
00612                                 }
00613                         }
00614                         // if ($params) $params = substr($params,0,-1);
00615                         $link = $AScript."?".$params."offset=";
00616 
00617                         // ?bergehe "zurck"-link, wenn offset 0 ist.
00618                         if ($AOffset >= 1)
00619                         {
00620                                 $prevoffset = $AOffset - $ALimit;
00621                                 $LinkBar .= "<a".$layout_link." href=\"".$link.$prevoffset."\">".$layout_prev."&nbsp;</a>";
00622                         }
00623 
00624                         // Ben?tigte Seitenzahl kalkulieren
00625                         $pages=intval($AHits/$ALimit);
00626 
00627                         // Wenn ein Rest bleibt, addiere eine Seite
00628                         if (($AHits % $ALimit))
00629                         $pages++;
00630 
00631                         // Bei Offset = 0 keine Seitenzahlen anzeigen : DEAKTIVIERT
00632                         //                      if ($AOffset != 0) {
00633 
00634                         // ansonsten zeige Links zu den anderen Seiten an
00635                         for ($i = 1 ;$i <= $pages ; $i++)
00636                         {
00637                                 $newoffset=$ALimit*($i-1);
00638 
00639                                 if ($newoffset == $AOffset)
00640                                 {
00641                                         $LinkBar .= "<font color='Gray'>[<b>".$i."</b>]</font> ";
00642                                 }
00643                                 else
00644                                 {
00645                                         $LinkBar .= "[<a".$layout_link." href=\"".$link.$newoffset."\">$i</a>] ";
00646                                 }
00647                         }
00648                         //                      }
00649 
00650                         // Checken, ob letze Seite erreicht ist
00651                         // Wenn nicht, gebe einen "Weiter"-Link aus
00652                         if (! ( ($AOffset/$ALimit)==($pages-1) ) && ($pages!=1) )
00653                         {
00654                                 $newoffset=$AOffset+$ALimit;
00655                                 $LinkBar .= "<a".$layout_link." href=\"".$link.$newoffset."\">&nbsp;".$layout_next."</a>";
00656                         }
00657 
00658                         return $LinkBar;
00659                 }
00660                 else
00661                 {
00662                         return false;
00663                 }
00664         }
00665 
00674         function makeClickable($a_text)
00675         {
00676                 // URL mit ://-Angabe
00677                 $ret = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=-])",
00678                 "<a href=\"\\1://\\2\\3\" target=\"_blank\">\\1://\\2\\3</a>", $a_text);
00679 
00680                 // www-URL ohne ://-Angabe
00681                 $ret = eregi_replace("([[:space:]]+)(www\.)([[:alnum:]#?/&=\.-]+)",
00682                 "\\1<a href=\"http://\\2\\3\" target=\"_blank\">\\2\\3</a>", $ret);
00683 
00684                 // ftp-URL ohne ://-Angabe
00685                 $ret = eregi_replace("([[:space:]]+)(ftp\.)([[:alnum:]#?/&=\.-]+)",
00686                 "\\1<a href=\"ftp://\\2\\3\" target=\"_blank\">\\2\\3</a>", $ret);
00687 
00688                 // E-Mail
00689                 $ret = eregi_replace("(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))",
00690                 "<a  href=\"mailto:\\1\">\\1</a>", $ret);
00691 
00692                 return($ret);
00693         }
00694 
00711         function StopWatch($begin = -1)
00712         {
00713                 $m = explode(" ",microtime());
00714                 $m = $m[0] + $m[1];
00715 
00716                 if ($begin != -1)
00717                 {
00718                         $m = $m - $begin;
00719                 }
00720 
00721                 return($m);
00722         }
00723 
00740         function makeDateSelect($prefix, $year = "", $month = "", $day = "", $startyear = "")
00741         {
00742                 global $lng;
00743 
00744                 if (!strlen("$year$month$day")) {
00745                         $now = getdate();
00746                         $year = $now["year"];
00747                         $month = $now["mon"];
00748                         $day = $now["mday"];
00749                 } else {
00750                         // delete leading zeros
00751                         $year = (int)$year;
00752                         $month = (int)$month;
00753                         $day = (int)$day;
00754                 }
00755 
00756                 // build day select
00757                 $sel_day .= "<select name=\"".$prefix."[d]\" id=\"".$prefix."_d\">\n";
00758 
00759                 for ($i = 1; $i <= 31; $i++)
00760                 {
00761                         $sel_day .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
00762                 }
00763                 $sel_day .= "</select>\n";
00764                 $sel_day = preg_replace("/(value\=\"$day\")/", "$1 selected=\"selected\"", $sel_day);
00765 
00766                 // build month select
00767                 $sel_month .= "<select name=\"".$prefix."[m]\" id=\"".$prefix."_m\">\n";
00768 
00769                 for ($i = 1; $i <= 12; $i++)
00770                 {
00771                         $sel_month .= "<option value=\"$i\">" . $lng->txt("month_" . sprintf("%02d", $i) . "_long") . "</option>\n";
00772                 }
00773                 $sel_month .= "</select>\n";
00774                 $sel_month = preg_replace("/(value\=\"$month\")/", "$1 selected=\"selected\"", $sel_month);
00775 
00776                 // build year select
00777                 $sel_year .= "<select name=\"".$prefix."[y]\" id=\"".$prefix."_y\">\n";
00778                 if ((strlen($startyear) == 0) || ($startyear > $year))
00779                 {
00780                         $startyear = $year;
00781                 }
00782                 for ($i = $startyear; $i <= $year + 3; $i++)
00783                 {
00784                         $sel_year .= "<option value=\"$i\">" . sprintf("%04d", $i) . "</option>\n";
00785                 }
00786                 $sel_year .= "</select>\n";
00787                 $sel_year = preg_replace("/(value\=\"$year\")/", "$1 selected=\"selected\"", $sel_year);
00788 
00789                 $dateformat = $lng->text["lang_dateformat"];
00790                 $dateformat = strtolower(preg_replace("/\W/", "", $dateformat));
00791                 $dateformat = strtolower(preg_replace("/(\w)/", "%%$1", $dateformat));
00792                 $dateformat = preg_replace("/%%d/", $sel_day, $dateformat);
00793                 $dateformat = preg_replace("/%%m/", $sel_month, $dateformat);
00794                 $dateformat = preg_replace("/%%y/", $sel_year, $dateformat);
00795                 return $dateformat;
00796         }
00797 
00814         function makeTimeSelect($prefix, $short = true, $hour = "", $minute = "", $second = "")
00815         {
00816                 global $lng;
00817 
00818                 if (!strlen("$hour$minute$second")) {
00819                         $now = localtime();
00820                         $hour = $now[2];
00821                         $minute = $now[1];
00822                         $second = $now[0];
00823                 } else {
00824                         $hour = (int)$hour;
00825                         $minute = (int)$minute;
00826                         $second = (int)$second;
00827                 }
00828                 // build hour select
00829                 $sel_hour .= "<select name=\"".$prefix."[h]\" id=\"".$prefix."_h\">\n";
00830 
00831                 for ($i = 0; $i <= 23; $i++)
00832                 {
00833                         $sel_hour .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
00834                 }
00835                 $sel_hour .= "</select>\n";
00836                 $sel_hour = preg_replace("/(value\=\"$hour\")/", "$1 selected=\"selected\"", $sel_hour);
00837 
00838                 // build minutes select
00839                 $sel_minute .= "<select name=\"".$prefix."[m]\" id=\"".$prefix."_m\">\n";
00840 
00841                 for ($i = 0; $i <= 59; $i++)
00842                 {
00843                         $sel_minute .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
00844                 }
00845                 $sel_minute .= "</select>\n";
00846                 $sel_minute = preg_replace("/(value\=\"$minute\")/", "$1 selected=\"selected\"", $sel_minute);
00847 
00848                 if (!$short) {
00849                         // build seconds select
00850                         $sel_second .= "<select name=\"".$prefix."[s]\" id=\"".$prefix."_s\">\n";
00851 
00852                         for ($i = 0; $i <= 59; $i++)
00853                         {
00854                                 $sel_second .= "<option value=\"$i\">" . sprintf("%02d", $i) . "</option>\n";
00855                         }
00856                         $sel_second .= "</select>\n";
00857                         $sel_second = preg_replace("/(value\=\"$second\")/", "$1 selected=\"selected\"", $sel_second);
00858                 }
00859                 $timeformat = $lng->text["lang_timeformat"];
00860                 $timeformat = strtolower(preg_replace("/\W/", "", $timeformat));
00861                 $timeformat = preg_replace("/(\w)/", "%%$1", $timeformat);
00862                 $timeformat = preg_replace("/%%h/", $sel_hour, $timeformat);
00863                 $timeformat = preg_replace("/%%i/", $sel_minute, $timeformat);
00864                 if ($short) {
00865                         $timeformat = preg_replace("/%%s/", "", $timeformat);
00866                 } else {
00867                         $timeformat = preg_replace("/%%s/", $sel_second, $timeformat);
00868                 }
00869                 return $timeformat;
00870         }
00871 
00872         /*
00873         * This preg-based function checks whether an e-mail address is formally valid.
00874         * It works with all top level domains including the new ones (.biz, .info, .museum etc.)
00875         * and the special ones (.arpa, .int etc.)
00876         * as well as with e-mail addresses based on IPs (e.g. webmaster@123.45.123.45)
00877         * @author       Unknown <mail@philipp-louis.de> (source: http://www.php.net/preg_match)
00878         * @access       public
00879         * @param        string  email address
00880         * @return       boolean true if valid
00881         */
00882         function is_email($a_email)
00883         {
00884                 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|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));
00885         }
00886 
00887         /*
00888         * validates a password
00889         * @access       public
00890         * @param        string  password
00891         * @return       boolean true if valid
00892         */
00893         function isPassword($a_passwd)
00894         {
00895                 if (empty($a_passwd))
00896                 {
00897                         return false;
00898                 }
00899 
00900                 if (strlen($a_passwd) < 6)
00901                 {
00902                         return false;
00903                 }
00904                 // due to bug in php does not work
00905                 //if (!ereg("^[A-Za-z0-9_\.\+\-\*\@!\$\%\~]+$", $a_passwd)) 
00906 
00907                 if (!preg_match("/^[A-Za-z0-9_\.\+\-\*\@!\$\%\~]+$/", $a_passwd))
00908                 {
00909                         return false;
00910                 }
00911 
00912                 return true;
00913         }
00914 
00915         /*
00916         * validates a login
00917         * @access       public
00918         * @param        string  login
00919         * @return       boolean true if valid
00920         */
00921         function isLogin($a_login)
00922         {
00923                 if (empty($a_login))
00924                 {
00925                         return false;
00926                 }
00927 
00928                 if (strlen($a_login) < 4)
00929                 {
00930                         return false;
00931                 }
00932 
00933                 if (!ereg("^[A-Za-z0-9_\.\+-\*\@!\$\%\~]+$", $a_login))
00934                 {
00935                         return false;
00936                 }
00937 
00938                 return true;
00939         }
00940 
00951         function shortenText ($a_str, $a_len, $a_dots = "false")
00952         {
00953                 if (strlen($a_str) > $a_len)
00954                 {
00955 
00956                         $a_str = substr($a_str,0,$a_len);
00957 
00958                         if ($a_dots)
00959                         {
00960                                 $a_str .= "...";
00961                         }
00962                 }
00963 
00964                 return $a_str;
00965         }
00966 
00974         function attribsToArray($a_str)
00975         {
00976                 $attribs = array();
00977                 while (is_int(strpos($a_str, "=")))
00978                 {
00979                         $eq_pos = strpos($a_str, "=");
00980                         $qu1_pos = strpos($a_str, "\"");
00981                         $qu2_pos = strpos(substr($a_str, $qu1_pos + 1), "\"") + $qu1_pos + 1;
00982                         if (is_int($eq_pos) && is_int($qu1_pos) && is_int($qu2_pos))
00983                         {
00984                                 $var = trim(substr($a_str, 0, $eq_pos));
00985                                 $val = trim(substr($a_str, $qu1_pos + 1, ($qu2_pos - $qu1_pos) - 1));
00986                                 $attribs[$var] = $val;
00987                                 $a_str = substr($a_str, $qu2_pos + 1);
00988                         }
00989                         else
00990                         {
00991                                 $a_str = "";
00992                         }
00993                 }
00994                 return $attribs;
00995         }
00996 
01005         function rCopy ($a_sdir, $a_tdir)
01006         {
01007                 // check if arguments are directories
01008                 if (!@is_dir($a_sdir) or
01009                 !@is_dir($a_tdir))
01010                 {
01011                         return FALSE;
01012                 }
01013 
01014                 // read a_sdir, copy files and copy directories recursively
01015                 $dir = opendir($a_sdir);
01016 
01017                 while($file = readdir($dir))
01018                 {
01019                         if ($file != "." and
01020                         $file != "..")
01021                         {
01022                                 // directories
01023                                 if (@is_dir($a_sdir."/".$file))
01024                                 {
01025                                         if (!@is_dir($a_tdir."/".$file))
01026                                         {
01027                                                 if (!ilUtil::makeDir($a_tdir."/".$file))
01028                                                 return FALSE;
01029 
01030                                                 //chmod($a_tdir."/".$file, 0775);
01031                                         }
01032 
01033                                         if (!ilUtil::rCopy($a_sdir."/".$file,$a_tdir."/".$file))
01034                                         {
01035                                                 return FALSE;
01036                                         }
01037                                 }
01038 
01039                                 // files
01040                                 if (@is_file($a_sdir."/".$file))
01041                                 {
01042                                         if (!copy($a_sdir."/".$file,$a_tdir."/".$file))
01043                                         {
01044                                                 return FALSE;
01045                                         }
01046                                 }
01047                         }
01048                 }
01049                 return TRUE;
01050         }
01051 
01059         function getWebspaceDir($mode = "filesystem")
01060         {
01061                 global $ilias;
01062 
01063                 if ($mode == "filesystem")
01064                 {
01065                         return "./".ILIAS_WEB_DIR."/".$ilias->client_id;
01066                 }
01067                 else
01068                 {
01069                         if (defined("ILIAS_MODULE"))
01070                         {
01071                                 return "../".ILIAS_WEB_DIR."/".$ilias->client_id;
01072                         }
01073                         else
01074                         {
01075                                 return "./".ILIAS_WEB_DIR."/".$ilias->client_id;
01076                         }
01077                 }
01078 
01079                 //return $ilias->ini->readVariable("server","webspace_dir");
01080         }
01081 
01085         function getDataDir()
01086         {
01087                 return CLIENT_DATA_DIR;
01088                 //global $ilias;
01089 
01090                 //return $ilias->ini->readVariable("server", "data_dir");
01091         }
01092 
01100         function getUsersOnline($a_user_id = 0)
01101         {
01102                 global $ilias;
01103 
01104                 if ($a_user_id == 0)
01105                 {
01106                         $where = "WHERE user_id != 0";
01107                 }
01108                 else
01109                 {
01110                         $where = "WHERE user_id = '".$a_user_id."'";
01111                 }
01112 
01113                 $q = "SELECT count(user_id) as num,user_id,data,firstname,lastname,title,login,last_login FROM usr_session ".
01114                 "LEFT JOIN usr_data ON user_id=usr_id ".$where.
01115                 " AND expires>UNIX_TIMESTAMP() GROUP BY user_id";
01116                 $r = $ilias->db->query($q);
01117 
01118                 while ($user = $r->fetchRow(DB_FETCHMODE_ASSOC))
01119                 {
01120                         $users[$user["user_id"]] = $user;
01121                 }
01122 
01123                 return $users ? $users : array();
01124         }
01125 
01131         function ilTempnam()
01132         {
01133                 $temp_path = ilUtil::getDataDir() . "/temp";
01134                 if (!is_dir($temp_path))
01135                 {
01136                         ilUtil::createDirectory($temp_path);
01137                 }
01138                 $temp_name = tempnam($temp_path, "tmp");
01139                 unlink($temp_name);
01140                 return $temp_name;
01141         }
01142 
01148         function createDirectory($a_dir, $a_mod = 0755)
01149         {
01150                 ilUtil::makeDir($a_dir);
01151                 //@mkdir($a_dir);
01152                 //@chmod($a_dir, $a_mod);
01153         }
01154 
01155 
01161         function unzip($a_file)
01162         {
01163                 //global $ilias;
01164 
01165                 $pathinfo = pathinfo($a_file);
01166                 $dir = $pathinfo["dirname"];
01167                 $file = $pathinfo["basename"];
01168 
01169                 // unzip
01170                 $cdir = getcwd();
01171                 chdir($dir);
01172                 $unzip = PATH_TO_UNZIP;
01173                 //$unzip = $ilias->getSetting("unzip_path");
01174 
01175                 // workaround for unzip problem (unzip of subdirectories fails, so
01176                 // we create the subdirectories ourselves first)
01177                 // get list
01178                 $unzipcmd = $unzip." -Z -1 ".ilUtil::escapeShellArg($file);
01179                 exec($unzipcmd, $arr);
01180                 $zdirs = array();
01181 
01182                 foreach($arr as $line)
01183                 {
01184                         if(is_int(strpos($line, "/")))
01185                         {
01186                                 $zdir = substr($line, 0, strrpos($line, "/"));
01187                                 $nr = substr_count($zdir, "/");
01188                                 //echo $zdir." ".$nr."<br>";
01189                                 while ($zdir != "")
01190                                 {
01191                                         $nr = substr_count($zdir, "/");
01192                                         $zdirs[$zdir] = $nr;                            // collect directories
01193                                         //echo $dir." ".$nr."<br>";
01194                                         $zdir = substr($zdir, 0, strrpos($zdir, "/"));
01195                                 }
01196                         }
01197                 }
01198 
01199                 asort($zdirs);
01200 
01201                 foreach($zdirs as $zdir => $nr)                         // create directories
01202                 {
01203                         ilUtil::createDirectory($zdir);
01204                 }
01205 
01206                 // real unzip
01207                 $unzipcmd = $unzip." -o ".ilUtil::escapeShellArg($file);
01208                 exec($unzipcmd);
01209 
01210                 chdir($cdir);
01211         }
01212 
01216         function zip($a_dir, $a_file)
01217         {
01218                 //global $ilias;
01219 
01220                 $cdir = getcwd();
01221 
01222                 $pathinfo = pathinfo($a_file);
01223                 $dir = $pathinfo["dirname"];
01224                 $file = $pathinfo["basename"];
01225 
01226                 // unzip
01227                 $cdir = getcwd();
01228                 chdir($dir);
01229 
01230                 $zip = PATH_TO_ZIP;
01231                 //$zip = $ilias->getSetting("zip_path");
01232 
01233                 if (is_array($a_dir))
01234                 {
01235                         $source = "";
01236                         foreach($a_dir as $dir)
01237                         {
01238                                 $name = basename($dir);
01239                                 $source.= " ".ilUtil::escapeShellArg($name);
01240                         }
01241                 }
01242                 else
01243                 {
01244                         $name = basename($a_dir);
01245                         $source = ilUtil::escapeShellArg($name);
01246                 }
01247 
01248                 $zipcmd = $zip." -r ".ilUtil::escapeShellArg($a_file)." ".$source;
01249                 exec($zipcmd);
01250 //echo htmlentities($zipcmd);
01251                 chdir($cdir);
01252         }
01253 
01257         function getConvertCmd()
01258         {
01259                 return PATH_TO_CONVERT;
01260                 //global $ilias;
01261 
01262                 //return $ilias->getSetting("convert_path");
01263         }
01264 
01272         function convertImage($a_from, $a_to, $a_target_format = "", $a_geometry = "")
01273         {
01274                 $format_str = ($a_target_format != "")
01275                 ? strtoupper($a_target_format).":"
01276                 : "";
01277                 $geometry = ($a_geometry != "")
01278                 ? " -geometry ".$a_geometry."x".$a_geometry." "
01279                 : "";
01280                 $convert_cmd = ilUtil::getConvertCmd()." ".
01281                 ilUtil::escapeShellArg($a_from)." ".$geometry.ilUtil::escapeShellArg($format_str.$a_to);
01282                 system($convert_cmd);
01283         }
01284 
01293         function resizeImage($a_from, $a_to, $a_width, $a_height)
01294         {
01295                 $size = " -resize ".$a_width."x".$a_height."! ";
01296                 $convert_cmd = ilUtil::getConvertCmd()." ".
01297                         ilUtil::escapeShellArg($a_from)." ".$size.ilUtil::escapeShellArg($a_to);
01298                 system($convert_cmd);
01299         }
01300 
01306         function html2pdf($html, $pdf_file)
01307         {
01308                 //global $ilias;
01309 
01310                 $html_file = str_replace(".pdf",".html",$pdf_file);
01311 
01312                 $fp = fopen( $html_file ,"wb");
01313                 fwrite($fp, $html);
01314                 fclose($fp);
01315 
01316                 $htmldoc_path = PATH_TO_HTMLDOC;
01317                 //$htmldoc_path = $ilias->getSetting("htmldoc_path");
01318 
01319                 $htmldoc = $htmldoc_path." ";
01320                 $htmldoc .= "--no-toc ";
01321                 $htmldoc .= "--no-jpeg ";
01322                 $htmldoc .= "--webpage ";
01323                 $htmldoc .= "--outfile " . ilUtil::escapeShellArg($pdf_file) . " ";
01324                 $htmldoc .= "--bodyfont Arial ";
01325                 $htmldoc .= "--charset iso-8859-15 ";
01326                 $htmldoc .= "--color ";
01327                 $htmldoc .= "--size A4  ";      // --landscape
01328                 $htmldoc .= "--format pdf ";
01329                 $htmldoc .= "--footer ... ";
01330                 $htmldoc .= "--header ... ";
01331                 $htmldoc .= "--left 60 ";
01332                 // $htmldoc .= "--right 200 ";
01333                 $htmldoc .= $html_file;
01334                 exec($htmldoc);
01335 
01336         }
01337 
01341         function deliverData($a_data, $a_filename, $mime = "application/octet-stream")
01342         {
01343                 $disposition = "attachment"; // "inline" to view file in browser or "attachment" to download to hard disk
01344                 //              $mime = "application/octet-stream"; // or whatever the mime type is
01345 
01346                 if (isset($_SERVER["HTTPS"])) {
01350                         header("Pragma: ");
01351                         header("Cache-Control: ");
01352                         header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
01353                         header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
01354                         header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
01355                         header("Cache-Control: post-check=0, pre-check=0", false);
01356                 }
01357                 else if ($disposition == "attachment")
01358                 {
01359                         header("Cache-control: private");
01360                 }
01361                 else
01362                 {
01363                         header("Cache-Control: no-cache, must-revalidate");
01364                         header("Pragma: no-cache");
01365                 }
01366 
01367                 $ascii_filename = ilUtil::getASCIIFilename($a_filename);
01368 
01369                 header("Content-Type: $mime");
01370                 header("Content-Disposition:$disposition; filename=\"".$ascii_filename."\"");
01371                 header("Content-Description: ".$ascii_filename);
01372                 header("Content-Length: ".(string)(strlen($a_data)));
01373                 header("Connection: close");
01374 
01375                 echo $a_data;
01376                 exit;
01377         }
01378 
01382         function deliverFile($a_file, $a_filename)
01383         {
01384                 $disposition = "attachment"; // "inline" to view file in browser or "attachment" to download to hard disk
01385                 $mime = "application/octet-stream"; // or whatever the mime type is
01386                 if (isset($_SERVER["HTTPS"]))
01387                 {
01391                         header("Pragma: ");
01392                         header("Cache-Control: ");
01393                         header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
01394                         header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
01395                         header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
01396                         header("Cache-Control: post-check=0, pre-check=0", false);
01397                 }
01398                 else if ($disposition == "attachment")
01399                 {
01400                         header("Cache-control: private");
01401                 }
01402                 else
01403                 {
01404                         header("Cache-Control: no-cache, must-revalidate");
01405                         header("Pragma: no-cache");
01406                 }
01407 
01408                 $ascii_filename = ilUtil::getASCIIFilename($a_filename);
01409 
01410                 header("Content-Type: $mime");
01411                 header("Content-Disposition:$disposition; filename=\"".$ascii_filename."\"");
01412                 header("Content-Description: ".$ascii_filename);
01413                 header("Content-Length: ".(string)(filesize($a_file)));
01414                 header("Connection: close");
01415                 ilUtil::readFile( $a_file );
01416                 exit;
01417         }
01418         
01419         
01426         function readFile($a_file)
01427         {
01428                 $chunksize = 1*(1024*1024); // how many bytes per chunk
01429                 $buffer = '';
01430                 $handle = fopen($a_file, 'rb');
01431                 if ($handle === false)
01432                 {
01433                         return false;
01434                 }
01435                 while (!feof($handle))
01436                 {
01437                         $buffer = fread($handle, $chunksize);
01438                         print $buffer;
01439                 }
01440                 return fclose($handle); 
01441         }
01442 
01448         function getASCIIFilename($a_filename)
01449         {
01450                 // The filename must be converted to ASCII, as of RFC 2183,
01451                 // section 2.3.
01452                 // Despite the RFC, Internet Explorer on Windows supports
01453                 // ISO 8895-1 encoding for the file name. We use this fact, to
01454                 // produce a better result, if the user uses IE.
01455 
01467 
01468                 $user_agent = strtolower($_SERVER["HTTP_USER_AGENT"]);
01469                 if ((is_integer(strpos($user_agent, "msie"))) && is_integer(strpos($user_agent, "win")))
01470                 {
01473 
01474                         $ascii_filename = utf8_decode($a_filename);
01475                 }
01476                 else
01477                 {
01480 
01481                         $ascii_filename = htmlentities($a_filename,ENT_NOQUOTES,'UTF-8');
01482                         $ascii_filename = preg_replace('/\&(.)[^;]*;/','\\1', $ascii_filename);
01483                         $ascii_filename = preg_replace('/[\x7f-\xff]/','_', $ascii_filename);
01484                 }
01485 
01486                 // Windows does not allow the following characters in filenames:
01487                 // \/:*?"<>|
01488                 if (is_integer(strpos($user_agent, "win")))
01489                 {
01490                         $ascii_filename = preg_replace('/[:\x5c\/\*\?\"<>\|]/','_', $ascii_filename);
01491                 }
01492 
01493                 return $ascii_filename;
01494         }
01495 
01499         function getJavaPath()
01500         {
01501                 return PATH_TO_JAVA;
01502                 //global $ilias;
01503 
01504                 //return $ilias->getSetting("java_path");
01505         }
01506 
01511         function appendUrlParameterString($a_url, $a_par)
01512         {
01513                 $url = (is_int(strpos($a_url, "?")))
01514                 ? $a_url."&".$a_par
01515                 : $a_url."?".$a_par;
01516 
01517                 return $url;
01518         }
01519 
01534         function makeDir($a_dir)
01535         {
01536                 $a_dir = trim($a_dir);
01537 
01538                 // remove trailing slash (bugfix for php 4.2.x)
01539                 if (substr($a_dir,-1) == "/")
01540                 {
01541                         $a_dir = substr($a_dir,0,-1);
01542                 }
01543 
01544                 // check if a_dir comes with a path
01545                 if (!($path = substr($a_dir,0, strrpos($a_dir,"/") - strlen($a_dir))))
01546                 {
01547                         $path = ".";
01548                 }
01549 
01550                 // create directory with file permissions of parent directory
01551                 umask(0000);
01552                 return @mkdir($a_dir,fileperms($path));
01553         }
01554 
01555 
01568         function makeDirParents($a_dir)
01569         {
01570                 $dirs = array($a_dir);
01571                 $a_dir = dirname($a_dir);
01572                 $last_dirname = '';
01573                 while($last_dirname != $a_dir)
01574                 {
01575                         array_unshift($dirs, $a_dir);
01576                         $last_dirname = $a_dir;
01577                         $a_dir = dirname($a_dir);
01578                 }
01579 
01580                 // find the first existing dir
01581                 $reverse_paths = array_reverse($dirs, TRUE);
01582                 $found_index = -1;
01583                 foreach ($reverse_paths as $key => $value)
01584                 {
01585                         if ($found_index == -1)
01586                         {
01587                                 if (is_dir($value))
01588                                 {
01589                                         $found_index = $key;
01590                                 }
01591                         }
01592                 }
01593                 
01594                 umask(0000);
01595                 foreach ($dirs as $dirindex => $dir)
01596                 {
01597                         // starting with the longest existing path
01598                         if ($dirindex >= $found_index) 
01599                         {
01600                                 if (! file_exists($dir))
01601                                 {
01602                                         if (! mkdir($dir, $umask))
01603                                         {
01604                                                 error_log("Can't make directory: $dir");
01605                                                 return false;
01606                                         }
01607                                 }
01608                                 elseif (! is_dir($dir))
01609                                 {
01610                                         error_log("$dir is not a directory");
01611                                         return false;
01612                                 }
01613                                 else
01614                                 {
01615                                         // get umask of the last existing parent directory
01616                                         $umask = fileperms($dir);
01617                                 }
01618                         }
01619                 }
01620                 return true;
01621         }
01622 
01630         function delDir($a_dir)
01631         {
01632                 if (!is_dir($a_dir) || is_int(strpos($a_dir, "..")))
01633                 {
01634                         return;
01635                 }
01636 
01637                 $current_dir = opendir($a_dir);
01638 
01639                 $files = array();
01640                 
01641                 // this extra loop has been necessary because of a strange bug
01642                 // at least on MacOS X. A looped readdir() didn't work
01643                 // correctly with larger directories 
01644                 // when an unlink happened inside the loop. Getting all files
01645                 // into the memory first solved the problem.
01646                 while($entryname = readdir($current_dir))
01647                 {
01648                         $files[] = $entryname;
01649                 }
01650                 
01651                 foreach($files as $file)
01652                 {
01653                         if(is_dir($a_dir."/".$file) and ($file != "." and $file!=".."))
01654                         {
01655                                 ilUtil::delDir(${a_dir}."/".${file});
01656                         }
01657                         elseif ($file != "." and $file != "..")
01658                         {
01659                                 unlink(${a_dir}."/".${file});
01660                         }
01661                 }
01662 
01663                 closedir($current_dir);
01664                 rmdir(${a_dir});
01665         }
01666 
01667 
01671         function getDir($a_dir)
01672         {
01673                 $current_dir = opendir($a_dir);
01674 
01675                 $dirs = array();
01676                 $files = array();
01677                 while($entry = readdir($current_dir))
01678                 {
01679                         if(is_dir($a_dir."/".$entry))
01680                         {
01681                                 $dirs[$entry] = array("type" => "dir", "entry" => $entry);
01682                         }
01683                         else
01684                         {
01685                                 $size = filesize($a_dir."/".$entry);
01686                                 $files[$entry] = array("type" => "file", "entry" => $entry,
01687                                 "size" => $size);
01688                         }
01689                 }
01690                 ksort($dirs);
01691                 ksort($files);
01692 
01693                 return array_merge($dirs, $files);
01694         }
01695 
01696 
01704         function getGroupId($a_parent_ref)
01705         {
01706                 return false;
01707 
01708                 global $ilias;
01709 
01710                 $q = "SELECT DISTINCT tree FROM grp_tree WHERE child='".$a_parent_ref."'";
01711                 $r = $ilias->db->query($q);
01712                 $row = $r->fetchRow();
01713 
01714                 return $row[0] ? $row[0] : false;
01715         }
01716 
01722         function stripSlashes($a_str, $a_strip_html = true, $a_allow = "")
01723         {
01724                 if (ini_get("magic_quotes_gpc"))
01725                 {
01726                         $a_str = stripslashes($a_str);
01727                 }
01728 
01729                 // set default allowed tags
01730                 if ($a_allow == "")
01731                 {
01732                         $a_allow = "<b><i><strong><em><code><cite><gap><sub><sup><pre><strike>";
01733                 }
01734 
01735                 if ($a_strip_html)
01736                 {
01737                         $a_str = ilUtil::stripScriptHTML($a_str, $a_allow);
01738                 }
01739 
01740                 return $a_str;
01741         }
01742 
01743 
01755         function stripScriptHTML($a_str, $a_allow = "", $a_rm_js = true)
01756         {
01757                 //$a_str = strip_tags($a_str, $a_allow);
01758 
01759                 $negativestr = "a,abbr,acronym,address,applet,area,b,base,basefont,".
01760                         "bdo,big,blockquote,body,br,button,caption,center,cite,code,col,".
01761                         "colgroup,dd,del,dfn,dir,div,dl,dt,em,fieldset,font,form,frame,".
01762                         "frameset,h1,h2,h3,h4,h5,h6,head,hr,html,i,iframe,img,input,ins,isindex,kbd,".
01763                         "label,legend,li,link,map,menu,meta,noframes,noscript,object,ol,".
01764                         "optgroup,option,p,param,pre,q,s,samp,script,select,small,span,".
01765                         "strike,strong,style,sub,sup,table,tbody,td,textarea,tfoot,th,thead,".
01766                         "title,tr,tt,u,ul,var";
01767                 $a_allow = strtolower ($a_allow);
01768                 $negatives = split (",",$negativestr);
01769                 foreach ($negatives as $item)
01770                 {
01771                         $pos = strpos($a_allow, "<$item>");
01772 
01773                         // remove complete tag, if not allowed
01774                         if ($pos === false)
01775                         {
01776                                 $old_str = "";
01777                                 while($old_str != $a_str)
01778                                 {
01779                                         $old_str = $a_str;
01780                                         $a_str = preg_replace("/<\/?\s*$item\s*>/i", "", $a_str);
01781                                         $a_str = preg_replace("/<\/?\s*$item\s+([^>]*)>/i", "", $a_str);
01782                                 }
01783                         }
01784 
01785                         if ($a_rm_js)
01786                         {
01787                                 // remove all attributes if an "on..." attribute is given
01788                                 $a_str = preg_replace("/<\s*".$item."(\s+[^>]*)?(\s+on[^>]*)>/i", "<$item>", $a_str);
01789 
01790                                 // remove all attributes if a "javascript" is within tag
01791                                 $a_str = preg_replace("/<\s*".$item."\s+[^>]*javascript[^>]*>/i", "<$item>", $a_str);
01792                         }
01793                 }
01794 
01795                 return $a_str;
01796         }
01797 
01798 
01804         function addSlashes($a_str)
01805         {
01806                 if (ini_get("magic_quotes_gpc"))
01807                 {
01808                         return $a_str;
01809                 }
01810                 else
01811                 {
01812                         return addslashes($a_str);
01813                 }
01814         }
01815 
01825         function prepareFormOutput($a_str, $a_strip = false)
01826         {
01827                 if($a_strip)
01828                 {
01829                         $a_str = ilUtil::stripSlashes($a_str);
01830                 }
01831                 return htmlspecialchars($a_str);
01832         }
01833 
01834 
01842         function prepareDBString($a_str)
01843         {
01844                 return addslashes($a_str);
01845         }
01846 
01847 
01854         function removeItemFromDesktops($a_id)
01855         {
01856                 global $ilias;
01857 
01858                 $q = "SELECT user_id FROM desktop_item WHERE item_id = '".$a_id."'";
01859                 $r = $ilias->db->query($q);
01860 
01861                 $users = array();
01862 
01863                 while ($row = $r->fetchRow(DB_FETCHMODE_OBJECT))
01864                 {
01865                         $users[] = $row->user_id;
01866                 } // while
01867 
01868                 if (count($users) > 0)
01869                 {
01870                         $q = "DELETE FROM desktop_item WHERE item_id = '".$a_id."'";
01871                         $ilias->db->query($q);
01872                 }
01873 
01874                 return $users;
01875         }
01876 
01877 
01885         function extractParameterString($a_parstr)
01886         {
01887                 // parse parameters in array
01888                 $par = array();
01889                 $ok=true;
01890                 while(($spos=strpos($a_parstr,"=")) && $ok)
01891                 {
01892                         // extract parameter
01893                         $cpar = substr($a_parstr,0,$spos);
01894                         $a_parstr = substr($a_parstr,$spos,strlen($a_parstr)-$spos);
01895                         while(substr($cpar,0,1)=="," ||substr($cpar,0,1)==" " || substr($cpar,0,1)==chr(13) || substr($cpar,0,1)==chr(10))
01896                         $cpar = substr($cpar,1,strlen($cpar)-1);
01897                         while(substr($cpar,strlen($cpar)-1,1)==" " || substr($cpar,strlen($cpar)-1,1)==chr(13) || substr($cpar,strlen($cpar)-1,1)==chr(10))
01898                         $cpar = substr($cpar,0,strlen($cpar)-1);
01899 
01900                         // extract value
01901                         if($spos=strpos($a_parstr,"\""))
01902                         {
01903                                 $a_parstr = substr($a_parstr,$spos+1,strlen($a_parstr)-$spos);
01904                                 $spos=strpos($a_parstr,"\"");
01905                                 if(is_int($spos))
01906                                 {
01907                                         $cval = substr($a_parstr,0,$spos);
01908                                         $par[$cpar]=$cval;
01909                                         $a_parstr = substr($a_parstr,$spos+1,strlen($a_parstr)-$spos-1);
01910                                 }
01911                                 else
01912                                 $ok=false;
01913                         }
01914                         else
01915                         $ok=false;
01916                 }
01917 
01918                 if($ok) return $par; else return false;
01919         }
01920 
01921         function assembleParameterString($a_par_arr)
01922         {
01923                 if (is_array($a_par_arr))
01924                 {
01925                         $target_arr = array();
01926                         foreach ($a_par_arr as $par => $val)
01927                         {
01928                                 $target_arr[] = "$par=\"$val\"";
01929                         }
01930                         $target_str = implode(", ", $target_arr);
01931                 }
01932 
01933                 return $target_str;
01934         }
01935 
01939         function dumpString($a_str)
01940         {
01941                 $ret = $a_str.": ";
01942                 for($i=0; $i<strlen($a_str); $i++)
01943                 {
01944                         $ret.= ord(substr($a_str,$i,1))." ";
01945                 }
01946                 return $ret;
01947         }
01948 
01949 
01953         function yn2tf($a_yn)
01954         {
01955                 if(strtolower($a_yn) == "y")
01956                 {
01957                         return true;
01958                 }
01959                 else
01960                 {
01961                         return false;
01962                 }
01963         }
01964 
01968         function tf2yn($a_tf)
01969         {
01970                 if($a_tf)
01971                 {
01972                         return "y";
01973                 }
01974                 else
01975                 {
01976                         return "f";
01977                 }
01978         }
01979 
01988         function sort_func ($a, $b)
01989         {
01990                 global $array_sortby,$array_sortorder;
01991 
01992                 if ($array_sortorder == "asc")
01993                 {
01994                         return strcasecmp($a[$array_sortby], $b[$array_sortby]);
01995                 }
01996 
01997                 if ($array_sortorder == "desc")
01998                 {
01999                         return strcasecmp($b[$array_sortby], $a[$array_sortby]);
02000                 }
02001         }
02002 
02011         function sort_func_numeric ($a, $b)
02012         {
02013                 global $array_sortby,$array_sortorder;
02014 
02015                 if ($array_sortorder == "asc")
02016                 {
02017                         return $a["$array_sortby"] > $b["$array_sortby"];
02018                 }
02019 
02020                 if ($array_sortorder == "desc")
02021                 {
02022                         return $a["$array_sortby"] < $b["$array_sortby"];
02023                 }
02024         }
02035         function sortArray($array,$a_array_sortby,$a_array_sortorder = 0,$a_numeric = false)
02036         {
02037                 global $array_sortby,$array_sortorder;
02038 
02039                 $array_sortby = $a_array_sortby;
02040 
02041                 if ($a_array_sortorder == "desc")
02042                 {
02043                         $array_sortorder = "desc";
02044                 }
02045                 else
02046                 {
02047                         $array_sortorder = "asc";
02048                 }
02049                 if($a_numeric)
02050                 {
02051                         usort($array, array("ilUtil", "sort_func_numeric"));
02052                 }
02053                 else
02054                 {
02055                         usort($array, array("ilUtil", "sort_func"));
02056                 }
02057                 //usort($array,"ilUtil::sort_func");
02058 
02059                 return $array;
02060         }
02061 
02071         function unique_multi_array($array, $sub_key)
02072         {
02073                 $target = array();
02074                 $existing_sub_key_values = array();
02075 
02076                 foreach ($array as $key=>$sub_array)
02077                 {
02078                         if (!in_array($sub_array[$sub_key], $existing_sub_key_values))
02079                         {
02080                                 $existing_sub_key_values[] = $sub_array[$sub_key];
02081                                 $target[$key] = $sub_array;
02082                         }
02083                 }
02084 
02085                 return $target;
02086         }
02087 
02088 
02096         function getGDSupportedImageType($a_desired_type)
02097         {
02098                 $a_desired_type = strtolower($a_desired_type);
02099                 // get supported Image Types
02100                 $im_types = ImageTypes();
02101 
02102                 switch($a_desired_type)
02103                 {
02104                         case "jpg":
02105                         if ($im_types & IMG_JPG) return "jpg";
02106                         if ($im_types & IMG_GIF) return "gif";
02107                         if ($im_types & IMG_PNG) return "png";
02108                         break;
02109 
02110                         case "gif":
02111                         if ($im_types & IMG_GIF) return "gif";
02112                         if ($im_types & IMG_JPG) return "jpg";
02113                         if ($im_types & IMG_PNG) return "png";
02114                         break;
02115 
02116                         case "png":
02117                         if ($im_types & IMG_PNG) return "png";
02118                         if ($im_types & IMG_JPG) return "jpg";
02119                         if ($im_types & IMG_GIF) return "gif";
02120                         break;
02121                 }
02122 
02123                 return "";
02124         }
02125 
02133         function deducibleSize($a_mime)
02134         {
02135                 if (($a_mime == "image/gif") || ($a_mime == "image/jpeg") ||
02136                 ($a_mime == "image/png") || ($a_mime == "application/x-shockwave-flash") ||
02137                 ($a_mime == "image/tiff") || ($a_mime == "image/x-ms-bmp") ||
02138                 ($a_mime == "image/psd") || ($a_mime == "image/iff"))
02139                 {
02140                         return true;
02141                 }
02142                 else
02143                 {
02144                         return false;
02145                 }
02146         }
02147 
02148 
02154         function redirect($a_script)
02155         {
02156                 global $log, $PHP_SELF;
02157 //$log->write("redirect FROM:".$PHP_SELF." to ".$a_script);
02158                 header("Location: ".$a_script);
02159                 exit();
02160         }
02161 
02167         function insertInstIntoID($a_value)
02168         {
02169                 if (substr($a_value, 0, 4) == "il__")
02170                 {
02171                         $a_value = "il_".IL_INST_ID."_".substr($a_value, 4, strlen($a_value) - 4);
02172                 }
02173 
02174                 return $a_value;
02175         }
02176 
02185         function groupNameExists($a_group_name,$a_id = 0)
02186         {
02187                 global $ilDB,$ilErr;
02188 
02189                 if (empty($a_group_name))
02190                 {
02191                         $message = get_class($this)."::_NameExists(): No groupname given!";
02192                         $ilErr->raiseError($message,$ilErr->WARNING);
02193                 }
02194 
02195                 $clause = ($a_id) ? " AND obj_id != '".$a_id."'" : "";
02196 
02197                 $q = "SELECT obj_id FROM object_data ".
02198                 "WHERE title = '".addslashes($a_group_name)."' ".
02199                 "AND type = 'grp'".
02200                 $clause;
02201                 $r = $ilDB->query($q);
02202 
02203                 if ($r->numRows() == 1)
02204                 {
02205                         return true;
02206                 }
02207                 else
02208                 {
02209                         return false;
02210                 }
02211         }
02212 
02213         /*
02214         * get the user_ids which correspond a search string
02215         * static function
02216         * @param        string search string
02217         * @access       public
02218         */
02219         function searchGroups($a_search_str)
02220         {
02221                 global $ilDB;
02222 
02223                 $q = "SELECT * ".
02224                 "FROM object_data ,object_reference ".
02225                 "WHERE (object_data.title LIKE '%".$a_search_str."%' ".
02226                 "OR object_data.description LIKE '%".$a_search_str."%') ".
02227                 "AND object_data.type = 'grp' ".
02228                 "AND object_data.obj_id = object_reference.obj_id";
02229 
02230                 $res = $ilDB->query($q);
02231 
02232                 while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
02233                 {
02234                         // STORE DATA IN ARRAY WITH KEY obj_id
02235                         // SO DUPLICATE ENTRIES ( LINKED OBJECTS ) ARE UNIQUE
02236                         $ids[$row->obj_id] = array(
02237                         "ref_id"        => $row->ref_id,
02238                         "title"         => $row->title,
02239                         "description"   => $row->description);
02240                 }
02241 
02242                 return $ids ? $ids : array();
02243         }
02244 
02248         function getMemString()
02249         {
02250                 $my_pid = getmypid();
02251                 return ("MEMORY USAGE (% KB PID ): ".`ps -eo%mem,rss,pid | grep $my_pid`);
02252         }
02253 
02257         function isWindows()
02258         {
02259                 if (strtolower(substr(php_uname(), 0, 3)) == "win")
02260                 {
02261                         return true;
02262                 }
02263                 return false;
02264         }
02265 
02266         function escapeShellArg($a_arg)
02267         {
02268                 global $PHP_OS;
02269 
02270                 if (ini_get("safe_mode") == 1 || ilUtil::isWindows())
02271                 {
02272                         return $a_arg;
02273                 }
02274                 else
02275                 {
02276                         return escapeshellarg($a_arg);
02277                 }
02278         }
02279 
02280         /*
02281         * Calculates a Microsoft Excel date/time value
02282         *
02283         * Calculates a Microsoft Excel date/time value (nr of days after 1900/1/1 0:00) for
02284         * a given date and time. The function only accepts dates after 1970/1/1, because the
02285         * unix timestamp functions used in the function are starting with that date.
02286         * If you don't enter parameters the date/time value for the actual date/time
02287         * will be calculated.
02288         *
02289         * static function
02290         *
02291         * @param        integer $year Year
02292         * @param        integer $month Month
02293         * @param        integer $day Day
02294         * @param        integer $hour Hour
02295         * @param        integer $minute Minute
02296         * @param        integer $second Second
02297         * @return float The Microsoft Excel date/time value
02298         * @access       public
02299         */
02300         function excelTime($year = "", $month = "", $day = "", $hour = "", $minute = "", $second = "")
02301         {
02302                 $starting_time = mktime(0, 0, 0, 1, 2, 1970);
02303                 if (strcmp("$year$month$day$hour$minute$second", "") == 0)
02304                 {
02305                         $target_time = time();
02306                 }
02307                 else
02308                 {
02309                         if ($year < 1970)
02310                         {
02311                                 return 0;
02312                         }
02313                 }
02314                 $target_time = mktime($hour, $minute, $second, $month, $day, $year);
02315                 $difference = $target_time - $starting_time;
02316                 $days = (($difference - ($difference % 86400)) / 86400);
02317                 $difference = $difference - ($days * 86400) + 3600;
02318                 return ($days + 25570 + ($difference / 86400));
02319         }
02320 
02324         function renameExecutables($a_dir)
02325         {
02326                 ilUtil::rRenameSuffix($a_dir, "php", "sec");
02327                 ilUtil::rRenameSuffix($a_dir, "php3", "sec");
02328                 ilUtil::rRenameSuffix($a_dir, "php4", "sec");
02329                 ilUtil::rRenameSuffix($a_dir, "inc", "sec");
02330                 ilUtil::rRenameSuffix($a_dir, "lang", "sec");
02331                 ilUtil::rRenameSuffix($a_dir, "phtml", "sec");
02332                 ilUtil::rRenameSuffix($a_dir, "htaccess", "sec");
02333         }
02334 
02343         function rRenameSuffix ($a_dir, $a_old_suffix, $a_new_suffix)
02344         {
02345                 if ($a_dir == "/" || $a_dir == "" || is_int(strpos($a_dir, "..")))
02346                 {
02347                         return false;
02348                 }
02349 
02350                 // check if argument is directory
02351                 if (!@is_dir($a_dir))
02352                 {
02353                         return false;
02354                 }
02355 
02356                 // read a_dir
02357                 $dir = opendir($a_dir);
02358 
02359                 while($file = readdir($dir))
02360                 {
02361                         if ($file != "." and
02362                         $file != "..")
02363                         {
02364                                 // directories
02365                                 if (@is_dir($a_dir."/".$file))
02366                                 {
02367                                         ilUtil::rRenameSuffix($a_dir."/".$file, $a_old_suffix, $a_new_suffix);
02368                                 }
02369 
02370                                 // files
02371                                 if (@is_file($a_dir."/".$file))
02372                                 {
02373                                         $path_info = pathinfo($a_dir."/".$file);
02374                                         if (strtolower($path_info["extension"]) ==
02375                                         strtolower($a_old_suffix))
02376                                         {
02377                                                 $pos = strrpos($a_dir."/".$file, ".");
02378                                                 $new_name = substr($a_dir."/".$file, 0, $pos).".".$a_new_suffix;
02379                                                 rename($a_dir."/".$file, $new_name);
02380                                         }
02381                                 }
02382                         }
02383                 }
02384                 return true;
02385         }
02386 
02387         function isAPICall () {
02388                 return  strpos($_SERVER["SCRIPT_FILENAME"],"api") !== false ||
02389                 strpos($_SERVER["SCRIPT_FILENAME"],"dummy") !== false;
02390         }
02391 
02392         function KT_replaceParam($qstring, $paramName, $paramValue) {
02393                 if (preg_match("/&" . $paramName . "=/", $qstring)) {
02394                         return preg_replace("/&" . $paramName . "=[^&]+/", "&" . $paramName . "=" . urlencode($paramValue), $qstring);
02395                 } else {
02396                         return $qstring . "&" . $paramName . "=" . urlencode($paramValue);
02397                 }
02398         }
02399 
02400         function replaceUrlParameterString ($url, $parametersArray) {
02401                 
02402                 foreach ($parametersArray as $paramName => $paramValue ) {
02403                         $url = ilUtil::KT_replaceParam($url, $paramName, $paramValue);
02404                 }
02405                 return $url;
02406         }
02407 
02408     function generatePasswords ($a_number)
02409     {
02410         $ret = array();
02411         srand((double) microtime()*1000000);
02412 
02413         for ($i=1; $i<=$a_number; $i++)
02414         {
02415             $length  = rand(6,10);
02416             $next  = rand(1,2);
02417             //$chars = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
02418             $vowels = "aeiou";
02419             $consonants = "bcdfghjklmnpqrstvwxyz";
02420             $pw = "";
02421 
02422             for ($j=0; $j < $length; $j++)
02423             {
02424                 switch ($next)
02425                 {
02426                     case 1:
02427                                         $pw.= $consonants[rand(0,strlen($consonants)-1)];
02428                                         $next = 2;
02429                     break;
02430                     
02431                     case 2:
02432                                         $pw.= $vowels[rand(0,strlen($vowels)-1)];
02433                                         $next = 1;
02434                                 break;
02435                 }
02436              }
02437 
02438              $ret[] = $pw;
02439          }
02440 
02441          return $ret;
02442     }
02443         
02444         function removeTrailingPathSeparators($path)
02445         {
02446                 $path = preg_replace("/[\/\\\]+$/", "", $path);
02447                 return $path;
02448         }
02449         
02458         function array_php2js($data)
02459         {
02460                 if (empty($data))
02461                 {
02462                         $data = array();
02463                 }
02464 
02465                 foreach($data as $k=>$datum)
02466                 {
02467                         if(is_null($datum)) $data[$k] = 'null';
02468                         if(is_string($datum)) $data[$k] = "'" . $datum . "'";
02469                         if(is_array($datum)) $data[$k] = array_php2js($datum);
02470                 }
02471                 
02472                 return "[" . implode(', ', $data) . "]";
02473         }
02474 
02479         function virusHandling($a_file, $a_orig_name = "", $a_clean = true)
02480         {
02481                 global $lng;
02482 
02483                 if (IL_VIRUS_SCANNER != "None")
02484                 {
02485                         require_once("classes/class.ilVirusScannerFactory.php");
02486                         $vs = ilVirusScannerFactory::_getInstance();
02487                         if (($vs_txt = $vs->scanFile($a_file, $a_orig_name)) != "")
02488                         {
02489                                 if ($a_clean && (IL_VIRUS_CLEAN_COMMAND != ""))
02490                                 {
02491                                         $clean_txt = $vs->cleanFile($a_file, $a_orig_name);
02492                                         if ($vs->fileCleaned())
02493                                         {
02494                                                 $vs_txt.= "<br />".$lng->txt("cleaned_file").
02495                                                         "<br />".$clean_txt;
02496                                                 $vs_txt.= "<br />".$lng->txt("repeat_scan");
02497                                                 if (($vs2_txt = $vs->scanFile($a_file, $a_orig_name)) != "")
02498                                                 {
02499                                                         return array(false, nl2br($vs_txt)."<br />".$lng->txt("repeat_scan_failed").
02500                                                                 "<br />".nl2br($vs2_txt));
02501                                                 }
02502                                                 else
02503                                                 {
02504                                                         return array(true, nl2br($vs_txt)."<br />".$lng->txt("repeat_scan_succeded"));
02505                                                 }
02506                                         }
02507                                         else
02508                                         {
02509                                                 return array(false, nl2br($vs_txt)."<br />".$lng->txt("cleaning_failed"));
02510                                         }
02511                                 }
02512                                 else
02513                                 {
02514                                         return array(false, nl2br($vs_txt));
02515                                 }
02516                         }
02517                 }
02518                 return array(true,"");
02519         }
02520 
02521 
02525         function moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors = true)
02526         {
02527                 global $lng, $ilias;
02528 //echo "<br>ilUtli::moveuploadedFile($a_name)";
02529                 if (!is_file($a_file))
02530                 {
02531                         $lng->txt("upload_error_file_not_found");
02532                 }
02533 
02534                 // virus handling
02535                 $vir = ilUtil::virusHandling($a_file, $a_name);
02536                 if (!$vir[0])
02537                 {
02538                         unlink($a_file);
02539                         if ($a_raise_errors)
02540                         {
02541                                 $ilias->raiseError($lng->txt("file_is_infected")."<br />".
02542                                         $vir[1],
02543                                         $ilias->error_obj->MESSAGE);
02544                         }
02545                         else
02546                         {
02547                                 sendInfo($lng->txt("file_is_infected")."<br />".
02548                                         $vir[1], true);
02549                         }
02550                         return false;
02551                 }
02552                 else
02553                 {
02554                         if ($vir[1] != "")
02555                         {
02556                                 sendInfo($vir[1], true);
02557                         }
02558                         return move_uploaded_file($a_file, $a_target);
02559                 }
02560         }
02561         
02562 } // END class.ilUtil
02563 ?>

Generated on Fri Dec 13 2013 09:06:35 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1