PageRenderTime 71ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/cphplib/cphplib.inc

https://github.com/esokullu/grou.ps
PHP | 4203 lines | 2890 code | 416 blank | 897 comment | 823 complexity | d5ef5cee4ea951782ea3e7aceb14e341 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. <?php
  2. /**
  3. * cphplib is a PHP-functions library. Those could simply be integrated into existing PHP-Scripts and allow an easy use.
  4. * These functions are very flexible to use because they are kept very common.
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * @category Utilities
  9. * @package cphplib
  10. * @author Alexander Meindl <am@meindlsoft.com>
  11. * @author Sven Reul <heffer@quaddamage.de>
  12. * @copyright (c) 2002-2005 meindlSOFT
  13. * @license Released under v2 of the GNU LGPL
  14. * @version Version 0.50 CVS: $Id: cphplib.inc,v 1.20 2005/11/11 09:12:25 alex Exp $
  15. * @link http://www.meindlsoft.com/tools.php
  16. */
  17. /**
  18. * Replacement for useless PHP empty function
  19. * Returns true, if string is empty
  20. *
  21. * @param string $str string to validate
  22. * @param bool $mode true for compatible with echo {default: false}
  23. * @return bool
  24. */
  25. function isvoid($str, $mode=false)
  26. {
  27. $rc = false;
  28. if (is_array($str))
  29. {
  30. if (count($str)==0)
  31. {
  32. $rc = true;
  33. }
  34. }
  35. else
  36. {
  37. $str = trim($str);
  38. if (strlen($str)==0)
  39. {
  40. $rc = true;
  41. }
  42. else if (($mode) && ($str=="0"))
  43. {
  44. $rc = true;
  45. }
  46. }
  47. return $rc;
  48. }
  49. /**
  50. * cphplib class
  51. */
  52. class cphplib
  53. {
  54. /**
  55. * cphplib version
  56. *
  57. * @var float
  58. */
  59. var $version = "0.50";
  60. /**
  61. * Error level
  62. * 0: show no error message
  63. * 1: only print message
  64. * 2: die, if error occurres
  65. * 3: user defined function (callback) => error_user_function required
  66. *
  67. * @var int
  68. */
  69. var $error_level = 2;
  70. /**
  71. * User defined function
  72. *
  73. * Required for error_level=3
  74. *
  75. * @var mixed
  76. */
  77. var $error_user_function;
  78. /**
  79. * Date format
  80. *
  81. * I = International date identifier (MM/DD/YYYY)
  82. * S = Science date identifier (YYYY-MM-DD)
  83. * C = Date without seperators (YYYYMMDD)
  84. * L = German date identifier (DD.MM.YYYY)
  85. *
  86. * @var char
  87. */
  88. var $date_format;
  89. /**
  90. * Time format
  91. *
  92. * Y = 24 hours mode
  93. * N = 12 hours mode
  94. *
  95. * @var char
  96. */
  97. var $time_format;
  98. /**
  99. * locale to use (see "man locale")
  100. *
  101. * @var string
  102. */
  103. var $locale;
  104. /**
  105. * xhtml output
  106. *
  107. * @var bool
  108. */
  109. var $xhtml = true;
  110. /**
  111. * Max. entries on one page of a list
  112. *
  113. * @var int
  114. */
  115. var $page_entries = 20;
  116. /**
  117. * Type of database
  118. *
  119. * @var string
  120. */
  121. var $db_type;
  122. /**
  123. * Database pear object
  124. * (mysql or pgsql)
  125. *
  126. * @var resource
  127. */
  128. var $db;
  129. /**
  130. * like not case sensitive for SQL
  131. *
  132. * @var resource
  133. */
  134. var $sql_like;
  135. /**
  136. * International seperator
  137. *
  138. * @var char
  139. */
  140. var $m_sep_i = "/";
  141. /**
  142. * Science seperator
  143. *
  144. * @var char
  145. */
  146. var $m_sep_s = "-";
  147. /**
  148. * German seperator
  149. *
  150. * @var char
  151. */
  152. var $m_sep_l = ".";
  153. /**
  154. * Image URL to all images
  155. *
  156. * @var string
  157. */
  158. var $image_url = "images";
  159. // db session variables
  160. var $m_dbsession_handler = false; // must be true for handling session_id
  161. var $m_dbsession_id = ""; // dbsession ID
  162. var $m_dbsession_id_name = "SID"; // dbsession ID name
  163. var $m_dbsession_table = "dbsession"; // table name for dbsession use
  164. var $m_dbsession_detail_table = "dbsession_detail"; // table name for dbsession use
  165. var $m_dbsession_timeout = 60; // dbsession timeout
  166. /**
  167. * cookies can be used for dbsession
  168. *
  169. * @var bool
  170. */
  171. var $m_dbsession_cookies = false;
  172. /**
  173. * take special care for mod_rewrite
  174. *
  175. * at the moment only url() is supported
  176. *
  177. * @var bool
  178. */
  179. var $mod_rewrite = false;
  180. /**
  181. * constructor
  182. *
  183. * @param string $locale if no empty, locale and string will be initialised
  184. * @return cphplib
  185. */
  186. function cphplib($locale="de_DE")
  187. {
  188. if (!empty($locale))
  189. {
  190. $this->locale = $locale;
  191. $this->set_locale();
  192. $this->set_strings();
  193. }
  194. }
  195. /**
  196. * Set locale
  197. *
  198. */
  199. function set_locale()
  200. {
  201. if ($this->locale=="de_DE")
  202. {
  203. $this->date_format = "L";
  204. $this->time_format = "Y";
  205. if ($this->check_php_version("4.3.0"))
  206. {
  207. setlocale(LC_TIME, 'de_DE@euro', 'de_DE', 'german', 'deu');
  208. setlocale(LC_CTYPE, 'de_DE@euro', 'de_DE', 'german', 'deu');
  209. }
  210. else if ($this->os_type(true) == "w")
  211. {
  212. setlocale(LC_TIME, 'german');
  213. setlocale(LC_CTYPE, 'german');
  214. }
  215. else
  216. {
  217. setlocale(LC_TIME, 'de_DE');
  218. setlocale(LC_CTYPE, 'de_DE');
  219. }
  220. }
  221. else
  222. {
  223. $this->set_format("I", "N");
  224. setlocale(LC_TIME, $this->locale);
  225. setlocale(LC_CTYPE, $this->locale);
  226. }
  227. }
  228. /**
  229. * Set date and time format to cphplib
  230. *
  231. * @param char $date_format see $this->date_format for valid values
  232. * @param char $time_format see $this->time_format for valid values
  233. */
  234. function set_format($date_format, $time_format)
  235. {
  236. $this->date_format = $date_format;
  237. $this->time_format = $time_format;
  238. }
  239. /**
  240. * Set strings for pager and Date methods
  241. *
  242. */
  243. function set_strings()
  244. {
  245. if (substr($this->locale, 0, 2) =="de")
  246. {
  247. include_once("i18n/german.inc");
  248. }
  249. else
  250. {
  251. include_once("i18n/english.inc");
  252. }
  253. }
  254. /**
  255. * Opens a connection to a database server and select database
  256. * Furthermore it sets $this->db to the PEAR database object,
  257. * which is required for dbsession
  258. *
  259. * @param string $dsn Data Source Name ( for more information see PEAR documentation)
  260. * addon array key:
  261. * persistent = true for persistent {default: false}
  262. * @param bool $set_db if true, db object will be set to $this->db
  263. * (and db_tyle and sql_like, too)
  264. * @return object PEAR database object
  265. */
  266. function db_connect($dsn, $set_db=true)
  267. {
  268. if ($this->file_exists("DB.php"))
  269. {
  270. if ((!isvoid($dsn['phptype'])) && (!(extension_loaded($dsn['phptype']))))
  271. {
  272. $this->show_error("db_connect", "PHP database module &quot;".$dsn['phptype']."&quot; is not supported by your system.");
  273. }
  274. require_once("DB.php");
  275. }
  276. else
  277. {
  278. $this->show_error("db_connect", "Pear \"DB Package\" required.");
  279. }
  280. if ($dsn['persistent']) $db =& DB::connect($dsn, true);
  281. else $db =& DB::connect($dsn ,false);
  282. if (DB::isError($db))
  283. {
  284. $this->show_error("db_connect", $db->getMessage(), $db->getCode());
  285. }
  286. if ($set_db)
  287. {
  288. $this->db =& $db;
  289. if ($dsn['phptype']=="mysqli") $this->db_type = "mysql";
  290. else $this->db_type = $dsn['phptype'];
  291. if ($dsn['phptype']=="pgsql") $this->sql_like = "ILIKE";
  292. else $this->sql_like = "LIKE";
  293. }
  294. return $db;
  295. }
  296. /**
  297. * disconnect current database connection
  298. * (change $this->db for select the right one, if you
  299. * use more the one connection)
  300. *
  301. * @return bool true if no errors occurred, otherwise false
  302. */
  303. function db_close()
  304. {
  305. if (DB::isError($this->db))
  306. {
  307. $this->show_error("db_close", $this->db->getMessage());
  308. }
  309. return @ $this->db->disconnect();
  310. }
  311. /**
  312. * Get the id generated from the previous INSERT operation
  313. *
  314. * @return int
  315. */
  316. function db_insert_id()
  317. {
  318. if (DB::isError($this->db))
  319. {
  320. $this->show_error("db_insert_id", $this->db->getMessage());
  321. }
  322. $rc = $this->db->getOne("SELECT last_insert_id()");
  323. if (DB::isError($rc))
  324. {
  325. $this->show_error("db_insert_id", $rc->getMessage());
  326. }
  327. return $rc;
  328. }
  329. /**
  330. * Get next available number
  331. *
  332. * @param string $table database table to use
  333. * @param string $column database column to use
  334. * @param string $where SQL WHERE restriction, e.g. "thisvalue>0"
  335. * (without "WHERE" in string!)
  336. * @return int highst number in column + 1
  337. */
  338. function db_next_id($table, $column, $where=null)
  339. {
  340. if (DB::isError($this->db))
  341. {
  342. $this->show_error("db_next_id", $this->db->getMessage());
  343. }
  344. $sqlstr = "SELECT MAX($column)+1 FROM $table";
  345. if (isset($where)) $sqlstr .= " WHERE ".$where;
  346. $tmp_id = $this->db->getOne($sqlstr);
  347. if (DB::isError($tmp_id))
  348. {
  349. $this->show_error("db_next_id", $tmp_id->getMessage());
  350. }
  351. if (empty($tmp_id))
  352. {
  353. return 1;
  354. }
  355. else
  356. {
  357. return $tmp_id;
  358. }
  359. }
  360. /**
  361. * Get next available number
  362. *
  363. * @param string $seq_name name of sequence
  364. * @return int
  365. */
  366. function db_seq_id($seq_name)
  367. {
  368. if (DB::isError($this->db))
  369. {
  370. $this->show_error("db_seq_id", $this->db->getMessage());
  371. }
  372. else if (isvoid($seq_name))
  373. {
  374. $this->show_error("db_seq_id", "seq_name is missing");
  375. }
  376. else if (!is_object($this->db))
  377. {
  378. $this->show_error("db_seq_id", "\$this-&gt;db is not an database object");
  379. }
  380. $tmp_id = $this->db->nextId($seq_name);
  381. if (DB::isError($tmp_id))
  382. {
  383. $this->show_error("db_seq_id", $tmp_id->getMessage());
  384. }
  385. if (empty($tmp_id)) return 1;
  386. else return $tmp_id;
  387. }
  388. /**
  389. * Get number of lowest unused number in a column)
  390. *
  391. * @param string $table database table to use
  392. * @param string $column database column to use
  393. * @param string $where_key database column name for limitation {default: void}
  394. * @param string $where_value limitation value {default: void}
  395. * @return int lowest unused number in a column
  396. */
  397. function db_free_id($table, $column, $where_key="", $where_value="")
  398. {
  399. if (DB::isError($this->db))
  400. {
  401. $this->show_error("db_free_id", $this->db->getMessage());
  402. }
  403. $rc=0;
  404. if ((isvoid($where_key)) && (isvoid($where_value)))
  405. $sqlstr = "SELECT $column FROM $table ORDER BY $column";
  406. else if ((!isvoid($where_key)) && (!isvoid($where_value)))
  407. $sqlstr = "SELECT $column FROM $table WHERE $where_key='$where_value' ORDER BY $column";
  408. if (!isvoid($sqlstr))
  409. {
  410. $que = $this->db->query($sqlstr);
  411. if ($que->numRows()>0)
  412. {
  413. $count=1;
  414. while ($row = $que->fetchRow())
  415. {
  416. $tmp_id = $row[0];
  417. if ($count==$tmp_id)
  418. $count++;
  419. else
  420. {
  421. $rc = $count;
  422. break;
  423. }
  424. }
  425. if ($rc==0)
  426. $rc=$count;
  427. }
  428. else
  429. $rc=1;
  430. }
  431. return $rc;
  432. }
  433. /**
  434. * check right database version
  435. *
  436. * @param float $mav major release number {default: 3.23}
  437. * @param float $miv minor release number {default: 6}
  438. * @return string active database version, if false empty
  439. */
  440. function db_version($mav="3.23", $miv="6")
  441. {
  442. if (DB::isError($this->db))
  443. {
  444. $this->show_error("db_version", $this->db->getMessage());
  445. }
  446. $rc = "unknown";
  447. if (($this->db_type)=="mysql")
  448. {
  449. if ($this->db->phptype=="mysqli") $tmp_rc = mysqli_get_server_info($this->db->connection);
  450. else $tmp_rc = mysql_get_server_info();
  451. $t_mav1 = strtok($tmp_rc,'.');
  452. $t_mav2 = strtok('.');
  453. $t_miv = strtok('.');
  454. $t_mav = $t_mav1.".".$t_mav2;
  455. if ($t_mav > $mav) $rc = "MySQL ".$tmp_rc;
  456. else if (($t_mav == $mav) && (intval($t_miv) >= $miv)) $rc = "MySQL ".$tmp_rc;
  457. else
  458. {
  459. $this->show_error("db_version", "MySQL version to old (version $mav.$miv or higher required), <b>program aborted</b>.");
  460. }
  461. }
  462. else
  463. {
  464. $tmp_rc = $this->db->getOne("SELECT version()");
  465. if (!isvoid($tmp_rc))
  466. {
  467. $rc = substr($tmp_rc,0,strpos($tmp_rc," on"));
  468. if (isvoid($rc)) $rc = $tmp_rc; // just to be sure
  469. }
  470. }
  471. return $rc;
  472. }
  473. /**
  474. * starts dbsession. Enables session fallback handling. This functions
  475. * will be required, if you want to handle the session_id with
  476. * url or formstart
  477. * Required : read README file
  478. *
  479. * @param string $session_id session id to use, if empty new unique id
  480. * will be generated
  481. * @param bool $table_error show error message, if dbsession tables doesn't exist
  482. * @return string session_id
  483. */
  484. function dbsession_start($session_id="", $table_error=true)
  485. {
  486. global $HTTP_COOKIE_VARS;
  487. $this->m_dbsession_handler=true;
  488. $IDpassed = false;
  489. if (DB::isError($this->db))
  490. {
  491. $this->show_error("dbsession_start", $this->db->getMessage());
  492. }
  493. else
  494. {
  495. $tdata = $this->db->getListOf("tables");
  496. if ((!in_array($this->m_dbsession_table,$tdata)) || (!in_array($this->m_dbsession_detail_table,$tdata)))
  497. $dbtables_found = false;
  498. else
  499. $dbtables_found = true;
  500. if ((!$dbtables_found) && ($table_error))
  501. {
  502. $this->show_error("dbsession_start", "missing dbsession database table");
  503. }
  504. }
  505. if (empty($session_id)) $SID = $this->get_user_var($this->m_dbsession_id_name,'POST,GET,COOKIE');
  506. else $SID = $session_id;
  507. // generate session_is
  508. $session_is = $this->get_id_string(false);
  509. // validate session_id
  510. if ((!empty($SID)) && (strlen($SID)==32) && ($dbtables_found))
  511. {
  512. $t_sec = date("s");
  513. $t_min = date("i");
  514. $t_hours = date("H");
  515. $t_day = date("d");
  516. $t_month = date("m");
  517. $t_year = date("Y");
  518. $session_start = $t_year."-".$t_month."-".$t_day." ".$t_hours.":".$t_min.":".$t_sec;
  519. $stempel = mktime($t_hours,($t_min+$this->m_dbsession_timeout),$t_sec,$t_month,$t_day,$t_year);
  520. $faellig = getdate($stempel);
  521. $session_end = $faellig['year']."-".$faellig['mon']."-".$faellig['mday']." ".$faellig['hours'].":".$faellig['minutes'].":".$faellig['seconds'];
  522. $sqlstr = "SELECT COUNT(*) FROM ".$this->m_dbsession_table." WHERE session_id='$SID'";
  523. if ($this->db->getOne($sqlstr)>0)
  524. {
  525. $sqlstr = "SELECT session_is,session_end FROM ".$this->m_dbsession_table." WHERE session_id='$SID'";
  526. $sdata = $this->db->getRow($sqlstr,DB_FETCHMODE_ASSOC);
  527. if (!empty($sdata['session_end']))
  528. {
  529. $tmp_stamp_db = $this->convtoTimestamp($sdata['session_end'],'datetime');
  530. $tmp_stamp_now = $this->convtoTimestamp();
  531. if (($sdata['session_is']==$session_is) && ($tmp_stamp_db > $tmp_stamp_now))
  532. {
  533. $sqlstr = "UPDATE ".$this->m_dbsession_table." SET session_end=".$this->sql_value($session_end);
  534. $sqlstr .= " WHERE session_id='$SID'";
  535. $this->db->query($sqlstr);
  536. $IDpassed = true;
  537. }
  538. else
  539. $this->dbsession_end($SID);
  540. }
  541. }
  542. else
  543. {
  544. $sqlstr = "INSERT INTO ".$this->m_dbsession_table." (session_id,session_start,session_end,session_is)";
  545. $sqlstr .= " VALUES ('$SID',";
  546. $sqlstr .= $this->sql_value($session_start).",";
  547. $sqlstr .= $this->sql_value($session_end).",";
  548. $sqlstr .= $this->sql_value($session_is) .")";
  549. $this->db->query($sqlstr);
  550. $IDpassed = true;
  551. }
  552. }
  553. // calculate new SID and session_is
  554. if (!$IDpassed) $SID = $this->get_id_string(true);
  555. $this->m_dbsession_id = $SID;
  556. setcookie($this->m_dbsession_id_name, $this->m_dbsession_id,0,"/");
  557. if ((!$IDpassed) && ($dbtables_found)) // no valid session_id in url found
  558. {
  559. $tmp_script_name = $_SERVER['SCRIPT_NAME'];
  560. $tmp_server_name = $_SERVER['SERVER_NAME'];
  561. $tmp_server_port = $_SERVER['SERVER_PORT'];
  562. $tmp_query_string = $_SERVER['QUERY_STRING'];
  563. $query = $tmp_query_string != ""
  564. ? "?".$tmp_query_string
  565. : "";
  566. $url = $tmp_script_name.$query;
  567. // non-standard port?
  568. $portMatch = array();
  569. $port = !preg_match("/^(80|443)$/", $tmp_server_port, $portMatch)
  570. ? ":".$tmp_server_port
  571. : "";
  572. $new_location = (($portMatch[1] == 443) ? "https://" : "http://");
  573. $new_location .= $tmp_server_name.$port.$this->url($url,"",2);
  574. // redirect
  575. header("Location: $new_location");
  576. exit;
  577. }
  578. $this->m_dbsession_cookies = (isset($_COOKIE[$this->m_dbsession_id_name]) && @strlen($_COOKIE[$this->m_dbsession_id_name]) == 32);
  579. return $SID;
  580. }
  581. /**
  582. * starts sub session within a session. You can use it to group the session
  583. * in different parts
  584. * Required : read README file
  585. *
  586. * @param string $session_id session id to use (main session)
  587. * @param string $session_subid session_subid. If empty new unique id
  588. * will be generated if required
  589. * @return string session_subid
  590. */
  591. function dbsessionsub_start($session_id, $session_subid="")
  592. {
  593. if (DB::isError($this->db))
  594. {
  595. $this->show_error("dbsessionsub_start", $this->db->getMessage());
  596. }
  597. $sqlstr = "SELECT COUNT(*) FROM ".$this->m_dbsession_table." WHERE session_id='$session_id'";
  598. if ($this->db->getOne($sqlstr)==0)
  599. {
  600. $this->show_error("dbsessionsub_start", "invalid session_id");
  601. }
  602. // generate session_is
  603. $session_is = $this->get_id_string(false);
  604. // validate session_subid
  605. if ((empty($session_subid)) || (strlen($session_subid)!=32))
  606. $session_subid = $this->get_id_string(true);
  607. $t_sec = date("s");
  608. $t_min = date("i");
  609. $t_hours = date("H");
  610. $t_day = date("d");
  611. $t_month = date("m");
  612. $t_year = date("Y");
  613. $session_start = $t_year."-".$t_month."-".$t_day." ".$t_hours.":".$t_min.":".$t_sec;
  614. $stempel = mktime($t_hours,($t_min+$this->m_dbsession_timeout),$t_sec,$t_month,$t_day,$t_year);
  615. $faellig = getdate($stempel);
  616. $session_end = $faellig['year']."-".$faellig['mon']."-".$faellig['mday']." ".$faellig['hours'].":".$faellig['minutes'].":".$faellig['seconds'];
  617. $sqlstr = "SELECT COUNT(*) FROM ".$this->m_dbsession_table." WHERE session_subid='$session_subid'";
  618. if ($this->db->getOne($sqlstr)>0) // maybe update is overkill, but it's safer.
  619. {
  620. $sqlstr = "UPDATE ".$this->m_dbsession_table." SET session_end=".$this->sql_value($session_end);
  621. $sqlstr .= " WHERE session_subid='$session_subid'";
  622. $this->db->query($sqlstr);
  623. }
  624. else
  625. {
  626. $sqlstr = "INSERT INTO ".$this->m_dbsession_table." (session_id,session_subid,session_start,session_end,session_is)";
  627. $sqlstr .= " VALUES ('$session_id','$session_subid',";
  628. $sqlstr .= $this->sql_value($session_start).",";
  629. $sqlstr .= $this->sql_value($session_end).",";
  630. $sqlstr .= $this->sql_value($session_is) .")";
  631. $this->db->query($sqlstr);
  632. }
  633. return $session_subid;
  634. }
  635. /**
  636. * ends dbsession or dbsessionsub
  637. * (Read the README file for requirements)
  638. *
  639. * @param string $session_id session id which should end ( and expired dbsession will automatically be removed)
  640. * @return bool true if successfully end dbsession
  641. */
  642. function dbsession_end($session_id="")
  643. {
  644. if (DB::isError($this->db))
  645. {
  646. $this->show_error("dbsession_end", $this->db->getMessage());
  647. }
  648. $sqlstr = "SELECT session_id, session_subid FROM ".$this->m_dbsession_table." WHERE session_end<=NOW()";
  649. if (!isvoid($session_id))
  650. $sqlstr .= " OR session_id=".$this->sql_value($session_id)." OR session_subid=".$this->sql_value($session_id);
  651. @ $que = $this->db->query($sqlstr);
  652. @ $num = $que->numRows();
  653. if ($num>0)
  654. {
  655. while ($row = $que->fetchRow())
  656. {
  657. if (!empty($row[1])) // sub session
  658. {
  659. $sqlstr1 = "DELETE FROM ".$this->m_dbsession_table." WHERE session_subid=".$this->sql_value($row[1]);
  660. $sqlstr2 = "DELETE FROM ".$this->m_dbsession_detail_table." WHERE session_id=".$this->sql_value($row[1]);
  661. }
  662. else // session with all sub sessions
  663. {
  664. $sqlstr1 = "DELETE FROM ".$this->m_dbsession_table." WHERE session_id=".$this->sql_value($row[0]);
  665. $sqlstr2 = "DELETE FROM ".$this->m_dbsession_detail_table." WHERE session_id=".$this->sql_value($row[0]);
  666. // take care of sub sessions /////////////
  667. $sqlstr = "SELECT session_subid FROM ".$this->m_dbsession_table." WHERE session_id=".$this->sql_value($row[0]);
  668. if (!isvoid($session_id))
  669. @ $sub_que = $this->db->query($sqlstr);
  670. @ $num = $sub_que->numRows();
  671. if ($num>0)
  672. {
  673. while ($sub_row = $sub_que->fetchRow())
  674. {
  675. $sqlstr = "DELETE FROM ".$this->m_dbsession_detail_table." WHERE session_id=".$this->sql_value($sub_row[0]);
  676. @ $this->db->query($sqlstr1);
  677. }
  678. }
  679. //////////////////////////////////////////
  680. }
  681. @ $this->db->query($sqlstr1);
  682. @ $this->db->query($sqlstr2);
  683. }
  684. }
  685. return true;
  686. }
  687. /**
  688. * register variable in dbsession
  689. * Required : read README file
  690. *
  691. * @param string $session_id session id to use
  692. * @param string $var_name variable to add
  693. * @param mixed $var_value value of variable
  694. * @return string value of variable
  695. */
  696. function dbsession_write($session_id, $var_name, $var_value)
  697. {
  698. $rc = "";
  699. if (DB::isError($this->db))
  700. {
  701. $this->show_error("dbsession_write", $this->db->getMessage());
  702. }
  703. if ((empty($session_id)) || (empty($var_name)))
  704. {
  705. $error_string = "dbsession_write error: var_name missing";
  706. trigger_error($error_string, E_USER_ERROR);
  707. }
  708. else if (empty($var_value))
  709. {
  710. $this->dbsession_delete($session_id, $var_name);
  711. }
  712. else
  713. {
  714. // if already stored, delete it
  715. $sqlstr = "SELECT COUNT(session_id) FROM ".$this->m_dbsession_detail_table." WHERE session_id=".$this->sql_value($session_id)." AND session_var=".$this->sql_value($var_name);
  716. if ($this->db->getOne($sqlstr)>0) // update
  717. $this->dbsession_delete($session_id,$var_name);
  718. if (is_array($var_value))
  719. {
  720. $sqlstr = "INSERT INTO ".$this->m_dbsession_detail_table." (session_id,session_array,session_var,session_value) VALUES (".$this->sql_value($session_id).",'Y',";
  721. $sqlstr .= "'$var_name',".$this->sql_value(serialize($var_value)).")";
  722. $this->db->query($sqlstr);
  723. }
  724. else
  725. {
  726. $sqlstr = "INSERT INTO ".$this->m_dbsession_detail_table." (session_id,session_array,session_var,session_value) VALUES (".$this->sql_value($session_id).",'N',";
  727. $sqlstr .= $this->sql_value($var_name) .",";
  728. if (!isvoid($var_value)) $sqlstr .= $this->sql_value($var_value) .")";
  729. else $sqlstr .= "NULL)";
  730. $this->db->query($sqlstr);
  731. }
  732. $rc = $var_value;
  733. }
  734. return $rc;
  735. }
  736. /**
  737. * register variable in dbsession
  738. *
  739. * @param string $session_id session id to use
  740. * @param string $var_name name of variable
  741. * @return string value of variable
  742. */
  743. function dbsession_read($session_id, $var_name)
  744. {
  745. $rc = "";
  746. if (DB::isError($this->db))
  747. {
  748. $this->show_error("dbsession_read", $this->db->getMessage());
  749. }
  750. if ((empty($session_id)) || (empty($var_name)))
  751. {
  752. $error_string = "dbsession_read error: var_name missing";
  753. trigger_error($error_string, E_USER_ERROR);
  754. }
  755. else
  756. {
  757. $sqlstr = "SELECT session_array,session_value FROM ".$this->m_dbsession_detail_table;
  758. $sqlstr .= " WHERE session_id=".$this->sql_value($session_id)." AND session_var=".$this->sql_value($var_name);
  759. $row = $this->db->getRow($sqlstr);
  760. if (is_array($row))
  761. {
  762. if ($row[0]=="N") $rc = $row[1];
  763. else $rc = unserialize($row[1]);
  764. }
  765. }
  766. return $rc;
  767. }
  768. /**
  769. * unregister variable in dbsession
  770. * Required : read README file
  771. *
  772. * @param string $session_id session id which will be used for the variabel
  773. * @param string $var_name variable to remove
  774. * @return bool true if successfully removed variable
  775. */
  776. function dbsession_delete($session_id, $var_name)
  777. {
  778. if (DB::isError($this->db))
  779. {
  780. $this->show_error("dbsession_delete", $this->db->getMessage());
  781. }
  782. if ((empty($session_id)) || (empty($var_name)))
  783. {
  784. $error_string = "dbsession_delete error: var_name missing";
  785. trigger_error($error_string, E_USER_ERROR);
  786. return false;
  787. }
  788. else
  789. {
  790. $sqlstr = "DELETE FROM ".$this->m_dbsession_detail_table." WHERE session_id=".$this->sql_value($session_id)." AND session_var=".$this->sql_value($var_name);
  791. $this->db->query($sqlstr);
  792. return true;
  793. }
  794. }
  795. /**
  796. * prints url as html tag
  797. * (this function can only handle the session_id, if dbsession_start has
  798. * been called immediately after creating the class object)
  799. *
  800. * @param string $url url
  801. * @param string $name name of url {default: url}
  802. * @param int $mode 0: no session_id
  803. * 1: with session_id if required {default: 1}
  804. * 2: just url with session_id (without TAG)
  805. * 3: just url with session_id (without TAG), but with delimiter &amp; instead of &
  806. * 4: with session_id (always)
  807. * @param string $title url title (hover text)
  808. * @param string $customize other parameters like target, style or class
  809. * @return string created url
  810. */
  811. function url($url, $name="", $mode=1, $title="", $customize="")
  812. {
  813. if ((($mode>0) && (!$this->m_dbsession_cookies) && (!$this->mod_rewrite))
  814. || ($mode==4))
  815. {
  816. if ($this->m_dbsession_handler) // if cphplib handles session fallback
  817. {
  818. // only add session_id if its on same host as script was executed (server).
  819. if ((($this->url_on_scripthost($url)) || ($mode==4)) &&
  820. (substr_count($url,"mailto:")==0))
  821. {
  822. // Anchor-Fragment extrahieren
  823. $dummyArray = split("#",$url);
  824. $pathInfo = $dummyArray[0];
  825. // evtl. (defekte) Session-ID(s) aus dem Querystring entfernen
  826. $pathInfo = preg_replace("/[?|&]".$this->m_dbsession_id_name."=[^?|&]*/","",$pathInfo);
  827. // evtl. Query-Delimiter korrigieren
  828. if (preg_match("/&/",$pathInfo) && !preg_match("/\?/",$pathInfo))
  829. $pathInfo = preg_replace("/&/","?",$pathInfo,1);
  830. // clear trash
  831. $match = array();
  832. preg_match("/(.*)(?<!&|\?)/",$pathInfo,$match);
  833. $url = $match[0];
  834. // add new session name and session id
  835. if (($mode==1) || ($mode==3) || ($mode==4)) $url .= preg_match("/\?/",$url) ? "&amp;" : "?";
  836. else $url .= preg_match("/\?/",$url) ? "&" : "?";
  837. $url .= $this->m_dbsession_id_name."=".$this->m_dbsession_id;
  838. // add anchor part again
  839. $url .= isset($dummyArray[1]) ? "#".$dummyArray[1] : "";
  840. }
  841. }
  842. }
  843. if (($mode==2) || ($mode==3)) return $url;
  844. else
  845. {
  846. if (isvoid($name)) $name = $url;
  847. else $name = trim($name);
  848. $rc = "<a href=\"".$url."\"";
  849. if (!isvoid($title)) $rc .= " title=\"".$title."\"";
  850. if (!isvoid($customize)) $rc .= " $customize";
  851. $rc .= ">".$name."</a>";
  852. return $rc;
  853. }
  854. }
  855. /**
  856. * removes magic quotes
  857. *
  858. * @param array $array
  859. */
  860. function remove_magic_quotes(&$array)
  861. {
  862. if(!get_magic_quotes_gpc()) return;
  863. foreach($array as $key => $value)
  864. {
  865. if(is_array($value)) $this->remove_magic_quotes($value);
  866. else $array[$key] = stripslashes($value);
  867. }
  868. }
  869. /**
  870. * returns value of special variable type from user input
  871. * (If register_globals is Off (default since 4.2.0), this function
  872. * can be used, to handle user variables (insecure variables).
  873. * You can use more than one type (seperated with ",")
  874. * DBSESSION only works if dbsession_start has been called before.
  875. *
  876. * @param string $var_name name of variable
  877. * @param string $var_type type of variable (POST, GET, COOKIE, SCRIPT, SESSION or DBSESSION)
  878. * (note: SCRIPT means, declared variable above the function)
  879. * @return mixed value of variable or "", if invalid
  880. */
  881. function get_user_var($var_name, $var_type)
  882. {
  883. global $HTTP_POST_VARS, $HTTP_GET_VARS, $HTTP_SESSION_VARS, $HTTP_COOKIE_VARS;
  884. $rc = "";
  885. $types = explode(",", $var_type);
  886. while (list(, $value) = each($types))
  887. {
  888. if (strtoupper($value) == "POST")
  889. {
  890. if (isset($_POST))
  891. {
  892. if ((isset($_POST[$var_name])) && (!isvoid($_POST[$var_name])))
  893. {
  894. if (get_magic_quotes_gpc())
  895. {
  896. if (is_array($_POST[$var_name]))
  897. {
  898. $this->remove_magic_quotes($_POST[$var_name]);
  899. return $_POST[$var_name];
  900. }
  901. else return stripslashes($_POST[$var_name]);
  902. }
  903. else return $_POST[$var_name];
  904. }
  905. }
  906. else
  907. {
  908. if ((isset($HTTP_POST_VARS[$var_name])) && (!isvoid($HTTP_POST_VARS[$var_name])))
  909. {
  910. if (get_magic_quotes_gpc())
  911. {
  912. if (is_array($HTTP_POST_VARS[$var_name]))
  913. {
  914. $this->remove_magic_quotes($HTTP_POST_VARS[$var_name]);
  915. return $HTTP_POST_VARS[$var_name];
  916. }
  917. else return stripslashes($HTTP_POST_VARS[$var_name]);
  918. }
  919. else return $HTTP_POST_VARS[$var_name];
  920. }
  921. }
  922. }
  923. else if (strtoupper($value) == "GET")
  924. {
  925. if (isset($_GET))
  926. {
  927. if ((isset($_GET[$var_name])) && (!isvoid($_GET[$var_name])))
  928. {
  929. if (get_magic_quotes_gpc())
  930. {
  931. if (is_array($_GET[$var_name]))
  932. {
  933. $this->remove_magic_quotes($_GET[$var_name]);
  934. return $_GET[$var_name];
  935. }
  936. else return stripslashes($_GET[$var_name]);
  937. }
  938. else return $_GET[$var_name];
  939. }
  940. }
  941. else
  942. {
  943. if ((isset($HTTP_GET_VARS[$var_name])) && (!isvoid($HTTP_GET_VARS[$var_name])))
  944. {
  945. if (get_magic_quotes_gpc())
  946. {
  947. if (is_array($HTTP_GET_VARS[$var_name]))
  948. {
  949. $this->remove_magic_quotes($HTTP_GET_VARS[$var_name]);
  950. return $HTTP_GET_VARS[$var_name];
  951. }
  952. else return stripslashes($HTTP_GET_VARS[$var_name]);
  953. }
  954. else return $HTTP_GET_VARS[$var_name];
  955. }
  956. }
  957. }
  958. else if (strtoupper($value) == "COOKIE")
  959. {
  960. if (isset($_COOKIE))
  961. {
  962. if ((isset($_COOKIE[$var_name])) && (!isvoid($_COOKIE[$var_name])))
  963. {
  964. if (get_magic_quotes_gpc())
  965. {
  966. if (is_array($_COOKIE[$var_name]))
  967. {
  968. $this->remove_magic_quotes($_COOKIE[$var_name]);
  969. return $_COOKIE[$var_name];
  970. }
  971. else return stripslashes($_COOKIE[$var_name]);
  972. }
  973. else return $_COOKIE[$var_name];
  974. }
  975. }
  976. else
  977. {
  978. if ((isset($HTTP_COOKIE_VARS[$var_name])) && (!isvoid($HTTP_COOKIE_VARS[$var_name])))
  979. {
  980. if (get_magic_quotes_gpc())
  981. {
  982. if (is_array($HTTP_COOKIE_VARS[$var_name]))
  983. {
  984. $this->remove_magic_quotes($HTTP_COOKIE_VARS[$var_name]);
  985. return $HTTP_COOKIE_VARS[$var_name];
  986. }
  987. else return stripslashes($HTTP_COOKIE_VARS[$var_name]);
  988. }
  989. else return $HTTP_COOKIE_VARS[$var_name];
  990. }
  991. }
  992. }
  993. else if (strtoupper($value) == "SESSION")
  994. {
  995. if (isset($_SESSION))
  996. {
  997. if ((isset($_SESSION[$var_name])) && (!isvoid($_SESSION[$var_name])))
  998. return $_SESSION[$var_name];
  999. }
  1000. else
  1001. {
  1002. if ((isset($HTTP_SESSION_VARS)) && (!isvoid($HTTP_SESSION_VARS)))
  1003. return $HTTP_SESSION_VARS[$var_name];
  1004. }
  1005. }
  1006. else if ((strtoupper($value) == "DBSESSION") && ($this->m_dbsession_handler))
  1007. {
  1008. $SID = $this->m_dbsession_id;
  1009. if (empty($SID))
  1010. {
  1011. $this->show_error("get_user_var", "dbsession_id is missing (DBSESSION)");
  1012. }
  1013. $tmp_rc = $this->dbsession_read($SID,$var_name);
  1014. if (isset($tmp_rc)) return $tmp_rc;
  1015. }
  1016. }
  1017. return $rc;
  1018. }
  1019. /**
  1020. * search needle in multi array
  1021. *
  1022. * @param string $needle
  1023. * @param array $haystack
  1024. * @return bool
  1025. */
  1026. function in_multi_array($needle, $haystack)
  1027. {
  1028. $rc = false;
  1029. if(is_array($haystack))
  1030. {
  1031. if(in_array($needle, $haystack))
  1032. {
  1033. $rc = true;
  1034. }
  1035. else
  1036. {
  1037. for($i = 0; $i<sizeof($haystack); $i++)
  1038. {
  1039. if(is_array($haystack[$i]))
  1040. {
  1041. if($this->in_multi_array($needle, $haystack[$i]))
  1042. {
  1043. $rc = true;
  1044. break;
  1045. }
  1046. }
  1047. }
  1048. }
  1049. }
  1050. return $rc;
  1051. }
  1052. /**
  1053. * Removes duplicate values from an array (recursive)
  1054. * (this function is much slower than the internal php function)
  1055. *
  1056. * @param array $thearray array to unique
  1057. * @return array
  1058. */
  1059. function array_unique(&$thearray)
  1060. {
  1061. sort($thearray);
  1062. reset($thearray);
  1063. $newarray = array();
  1064. $i = 0;
  1065. $element = current($thearray);
  1066. for ($n=0;$n<sizeof($thearray);$n++)
  1067. {
  1068. if (next($thearray) != $element)
  1069. {
  1070. $newarray[$i] = $element;
  1071. $element = current($thearray);
  1072. $i++;
  1073. }
  1074. }
  1075. return $newarray;
  1076. }
  1077. /**
  1078. * sort array (subfunction for usort)
  1079. *
  1080. * @param array $a keys
  1081. * @param array $b values
  1082. * @return array
  1083. */
  1084. function array_sort($a, $b)
  1085. {
  1086. $as = strtoupper(trim($a[1]));
  1087. $bs = strtoupper(trim($b[1]));
  1088. $as = strtr($as, "ä", "A");
  1089. $as = strtr($as, "ö", "O");
  1090. $as = strtr($as, "ü", "U");
  1091. $as = strtr($as, "ß", "S");
  1092. $bs = strtr($bs, "Ä", "A");
  1093. $bs = strtr($bs, "Õ", "O");
  1094. $bs = strtr($bs, "Ü", "U");
  1095. if ($as == $bs) return 0;
  1096. else return ($as > $bs)?1:-1;
  1097. }
  1098. /**
  1099. *reverse sort array (subfunction for usort)
  1100. *
  1101. * @param array $a keys
  1102. * @param array $b values
  1103. * @return array
  1104. */
  1105. function array_rsort($a, $b)
  1106. {
  1107. $as = strtoupper(trim($a[1]));
  1108. $bs = strtoupper(trim($b[1]));
  1109. $as = strtr($as, "ä", "A");
  1110. $as = strtr($as, "ö", "O");
  1111. $as = strtr($as, "ü", "U");
  1112. $as = strtr($as, "ß", "S");
  1113. $bs = strtr($bs, "Ä", "A");
  1114. $bs = strtr($bs, "Õ", "O");
  1115. $bs = strtr($bs, "Ü", "U");
  1116. if ($as == $bs) return 0;
  1117. else return ($as < $bs)?1:-1;
  1118. }
  1119. /**
  1120. * the current date
  1121. *
  1122. * @param char $date_format see member variables $this->date_format below
  1123. * @return date
  1124. */
  1125. function currentDate($date_format=null)
  1126. {
  1127. if ((!isset($date_format)) || (empty($date_format))) $date_format = $this->date_format;
  1128. $day = date("d");
  1129. $month = date("m");
  1130. $year = date("Y");
  1131. if ($date_format=="I") return $month.$this->m_sep_i.$day .$this->m_sep_i.$year;
  1132. else if ($date_format=="S") return $year .$this->m_sep_s.$month.$this->m_sep_s.$day;
  1133. else if ($date_format=="C") return $year .$month. $day;
  1134. else return $day .$this->m_sep_l.$month.$this->m_sep_l.$year;
  1135. }
  1136. /**
  1137. * the current time
  1138. *
  1139. * @param char $time_format see member variable $this->time_format below
  1140. * @return time
  1141. */
  1142. function currentTime($time_format=null)
  1143. {
  1144. if ((!isset($time_format)) || (empty($time_format))) $time_format = $this->time_format;
  1145. if ($time_format=="Y")
  1146. return date("H").":".date("i").":".date("s");
  1147. else
  1148. return date("h").":".date("i").":".date("s")." ".date("a");
  1149. }
  1150. /**
  1151. * Get date from db-date format
  1152. * (short form of convDate)
  1153. *
  1154. * @param string $dbdate
  1155. * @param bool $short_mode
  1156. * @return string
  1157. */
  1158. function show_dbdate($dbdate, $short_mode=true)
  1159. {
  1160. if ($short_mode==true)
  1161. {
  1162. return $this->convDate($dbdate, 'S', $this->date_format, array('short_mode'=>true));
  1163. }
  1164. else
  1165. {
  1166. return $this->convDate($dbdate, 'S', $this->date_format);
  1167. }
  1168. }
  1169. /**
  1170. * Get Datetime from db-date format
  1171. * (short form of convDate)
  1172. *
  1173. * @param string $dbdate
  1174. * @param bool $with_seconds
  1175. * @param bool $short_mode
  1176. * @param string $at
  1177. * @return string
  1178. */
  1179. function show_dbdatetime($dbdate, $with_seconds=false, $short_mode=true, $at=null)
  1180. {
  1181. return $this->convDateTime($dbdate, $this->date_format, $this->time_format, $with_seconds, $short_mode, $at);
  1182. }
  1183. /**
  1184. * Removes all whitespaces in a string
  1185. *
  1186. * @param string $str
  1187. * @return string
  1188. */
  1189. function killSpace($str)
  1190. {
  1191. $rc = htmlentities($str);
  1192. $rc = str_replace("&nbsp;","",$rc);
  1193. $rc = str_replace("&#160;","",$rc);
  1194. $rc = str_replace(" ","",$rc);
  1195. return $rc;
  1196. }
  1197. /**
  1198. * validate given date
  1199. *
  1200. * @param mixed $timestamp array of date for validating or timestamp
  1201. * array keys: timestamp['year']
  1202. * timestamp['month']
  1203. * timestamp['day']
  1204. * timestamp['hour']
  1205. * timestamp['minute']
  1206. * timestamp['second']
  1207. * @param bool $mode if true, input date is a timestamp {default: false}
  1208. * @return timestamp timestamp with valid date, if invalid input datas, return timestamp will
  1209. * be return the nearest valid date
  1210. */
  1211. function validate_timestamp($timestamp,$mode=false)
  1212. {
  1213. if (!$mode)
  1214. {
  1215. $timestamp = mktime($timestamp['hour'],
  1216. $timestamp['minute'],
  1217. $timestamp['second'],
  1218. $timestamp['month'],
  1219. $timestamp['day'],
  1220. $timestamp['year']);
  1221. }
  1222. if ($timestamp<mktime(2,0,0,1,1,1970))
  1223. return mktime(2,0,0,1,1,1970);
  1224. else if ($timestamp>mktime(2,0,0,12,31,2037))
  1225. return mktime(2,0,0,12,31,2037);
  1226. else
  1227. return $timestamp;
  1228. }
  1229. /**
  1230. * calculates days to a given date
  1231. *
  1232. * @param date $start_date first date
  1233. * @param date $end_date last date
  1234. * @param char $date_format see member variables $this->date_format below
  1235. * @return int number of days, if date is out of range -1
  1236. */
  1237. function date2days($start_date, $end_date, $date_format="S")
  1238. {
  1239. if ($date_format!="S")
  1240. {
  1241. $start_date = $this->convDate($start_date, $date_format, 'S');
  1242. $end_date = $this->convDate($end_date, $date_format, 'S');
  1243. }
  1244. $start_year = strtok($start_date,"-");
  1245. $start_month = strtok("-");
  1246. $start_day = strtok("-");
  1247. $end_year = strtok($end_date,"-");
  1248. $end_month = strtok("-");
  1249. $end_day = strtok("-");
  1250. if ($this->file_exists("Date.php"))
  1251. require_once("Date.php");
  1252. else
  1253. {
  1254. $this->show_error("date2days", "Pear \"Date Package\" required.");
  1255. }
  1256. return Date_Calc::dateDiff($start_day,$start_month,$start_year,$end_day,$end_month,$end_year);
  1257. }
  1258. /**
  1259. * return file extension
  1260. *
  1261. * @param string $filename
  1262. * @return string
  1263. */
  1264. function fileExtension($filename)
  1265. {
  1266. $rc="";
  1267. $max_length = strlen($filename);
  1268. $counter=0;
  1269. while($max_length>0)
  1270. {
  1271. $ch = $filename[$max_length];
  1272. if ($ch == ".")
  1273. break;
  1274. $rc .= $ch;
  1275. $max_length--;
  1276. $counter++;
  1277. }
  1278. if ($rc!="") $rc = strrev($rc);
  1279. return $rc;
  1280. }
  1281. /**
  1282. * convert string to number while converting old seperator with "."
  1283. *
  1284. * @param string $value string to convert {default: ","}
  1285. * @param string $sep_old old seperator
  1286. * @return float
  1287. */
  1288. function convToNum($value,$sep_old=",")
  1289. {
  1290. $rc = "";
  1291. if (empty($sep_old))
  1292. {
  1293. $this->show_error("convtoNum", "missing parameter sep_old");
  1294. }
  1295. $st = trim($value);
  1296. $sep_new = ".";
  1297. if (!isvoid($st))
  1298. {
  1299. if ($this->checkNumber($st,$sep_old))
  1300. {
  1301. if ($sep_old != $sep_new)
  1302. {
  1303. if ($this->checkNumber($st,$sep_old))
  1304. {
  1305. $num_sep = substr_count($st,$sep_old);
  1306. if ($num_sep==1)
  1307. $rc = str_replace($sep_old,$sep_new,$st);
  1308. else
  1309. $rc = $st;
  1310. }
  1311. }
  1312. else
  1313. $rc = $st;
  1314. }
  1315. }
  1316. return $rc;
  1317. }
  1318. /**
  1319. * convert seperator in number and fill number to specified length
  1320. *
  1321. * @param float $st number to convert
  1322. * @param char $sep_new new seperator {default: "."}
  1323. * @param int $precision numbers behind seperator {default: 0 }
  1324. * (filling with 0; this function doesn't cut or round numbers)
  1325. * @param string $group if set, this group seperator will be used
  1326. * @return string converted string with number
  1327. */
  1328. function convnumSep($st, $sep_new=".", $precision=0, $group=null)
  1329. {
  1330. $rc="";
  1331. if (!isvoid($st))
  1332. {
  1333. if ($this->checkNumber($st))
  1334. {
  1335. $sep_old = ".";
  1336. $num_sep = substr_count($st,$sep_old);
  1337. if (($num_sep==0) && ($precision>0)) // no old seperator
  1338. {
  1339. $tmp_st = $st.$sep_new;
  1340. for ($ix=0;$ix<$precision;$ix++)
  1341. $tmp_st .= "0";
  1342. $rc = $tmp_st;
  1343. }
  1344. else // with old seperator
  1345. {
  1346. $ln = strlen($st);
  1347. $pos = strrpos($st, $sep_old);
  1348. $ln_ext = $ln-$pos-1;
  1349. if ($ln_ext<$precision)
  1350. {
  1351. $ln_miss = $precision - $ln_ext;
  1352. $tmp_st=$st;
  1353. for ($ix=0;$ix<$ln_miss;$ix++)
  1354. $tmp_st .= "0";
  1355. $rc = str_replace($sep_old, $sep_new, $tmp_st);
  1356. }
  1357. else
  1358. {
  1359. $rc = str_replace($sep_old, $sep_new, $st);
  1360. }
  1361. }
  1362. // fill group seperator, if defined
  1363. if (isset($group))
  1364. {
  1365. if ($rc[0]=="-") $g_rc = substr($rc, 1);
  1366. else $g_rc = $rc;
  1367. $ln = strlen($g_rc); // get new length
  1368. $pos = strrpos($g_rc, $sep_new);
  1369. if ($pos>3)
  1370. {
  1371. $suffix_ln = $ln-$pos+1;
  1372. $prefix_ln = $ln-$suffix_ln+1;
  1373. if ($prefix_ln>3)
  1374. {
  1375. $tmp_rc = "";
  1376. $prefix = strrev(substr($g_rc, 0, $prefix_ln));
  1377. for ($ix=0; $ix<$prefix_ln; $ix++)
  1378. {
  1379. if (($ix>0) && ($ix%3==0)) $tmp_rc .= $group;
  1380. $tmp_rc .= $prefix[$ix];
  1381. }
  1382. $g_rc = strrev($tmp_rc).substr($g_rc, $pos);
  1383. }
  1384. }
  1385. if ($rc[0]=="-") $rc = "-".$g_rc;
  1386. else $rc = $g_rc;
  1387. }
  1388. }
  1389. }
  1390. else
  1391. $rc = $st;
  1392. return $rc;
  1393. }
  1394. /**
  1395. * validate time format and convert it into right format
  1396. *
  1397. * @param string $time time (or date) to convert, if empty NOW will be used
  1398. * @param char $format if empty or time, e.g. 19:45:59
  1399. * datetime, e.g. 2002-07-20 21:02:55
  1400. * @return string converted time, if wrong input false
  1401. */
  1402. function convToTimestamp($time="",$format="")
  1403. {
  1404. $timestamp = "";
  1405. if ((isvoid($format)) || ($format=="time")) // only time
  1406. {
  1407. if (isvoid($time)) $timestamp = strtotime("now");
  1408. else
  1409. {
  1410. $t_hours = strtok(trim($time),":");
  1411. $t_min = strtok(":");
  1412. $t_sec = strtok(":");
  1413. $t_day = date("d");
  1414. $t_month = date("m");
  1415. $t_year = date("Y");
  1416. $tmp_stamp = mktime($t_hours,$t_min,$t_sec,$t_month,$t_day,$t_year);
  1417. if ($tmp_stamp>0)
  1418. $timestamp = $tmp_stamp;
  1419. }
  1420. }
  1421. if (($format=="datetime") && (!isvoid($time))) // datetime
  1422. {
  1423. $tdate = trim(strtok(trim($time)," "));
  1424. $ttime = trim(strtok(" "));
  1425. $t_year = strtok($tdate,"-");
  1426. $t_month = strtok("-");
  1427. $t_day = strtok("-");
  1428. $t_hours = strtok($ttime,":");
  1429. $t_min = strtok(":");
  1430. $t_sec = strtok(":");
  1431. $tmp_stamp = mktime($t_hours,$t_min,$t_sec,$t_month,$t_day,$t_year);
  1432. if ($tmp_stamp>0)
  1433. $timestamp = $tmp_stamp;
  1434. }
  1435. return $timestamp;
  1436. }
  1437. /**
  1438. * validate time format and convert it into right format
  1439. *
  1440. * @param string $time time to convert
  1441. * @param char $time_format see member variable $this->time_format above
  1442. * @param bool $with_seconds if false, don't return seconds {default: true}
  1443. * @return string converted time, if wrong input false
  1444. */
  1445. function convTime($time, $time_format=null, $with_seconds=true)
  1446. {
  1447. $rc="";
  1448. if ((!isset($time_format)) || (empty($time_format))) $time_format = $this->time_format;
  1449. $ln = strlen($time);
  1450. if ($ln == 5)
  1451. {
  1452. $part1 = strtok($time,":");
  1453. $part2 = strtok(":");
  1454. if ((strlen($part1)==2) && (strlen($part2)==2))
  1455. {
  1456. if (($part1>=0) && ($part1<25) && ($part2>=0) && ($part2<60))
  1457. {
  1458. if ($time_format=="N") // 12 hours
  1459. {
  1460. if ($part1>=12)
  1461. {
  1462. $part1 = $part1-12;
  1463. $end = "&nbsp;pm";
  1464. }
  1465. else
  1466. {
  1467. $part1 += 0;
  1468. $end = "&nbsp;am";
  1469. }
  1470. if ($part1==0)
  1471. $part1 = 12;
  1472. $rc = $part1 . ":" . $part2.$end;
  1473. }
  1474. else // 24 hours
  1475. {
  1476. $rc = $part1 . ":" . $part2;
  1477. }
  1478. }
  1479. }
  1480. }
  1481. else if ($ln >= 8)
  1482. {
  1483. $part1 = strtok($time,":");
  1484. $part2 = strtok(":");
  1485. $part3 = strtok(":");
  1486. if (strlen($part3)>2) $part3 = substr($part3, 0, 2);
  1487. if ((strlen($part1)==2) && (strlen($part2)==2) && (strlen($part3)==2))
  1488. {
  1489. if (($part1>=0) && ($part1<25) && ($part2>=0) && ($part2<60) && ($part3>=0) && ($part3<60))
  1490. {
  1491. if ($time_format=="N") // 12 hours
  1492. {
  1493. if ($part1>12)
  1494. {
  1495. $part1 = $part1-12;
  1496. $end = "&nbsp;pm";
  1497. }
  1498. else
  1499. {
  1500. $part1 += 0;
  1501. $end = "&nbsp;am";
  1502. }
  1503. $rc = $part1 . ":" .$part2;
  1504. if ($with_seconds) $rc .= ":".$part3;
  1505. $rc .= $end;
  1506. }
  1507. else // 24 hours
  1508. {
  1509. $rc = $part1 . ":" .$part2;
  1510. if ($with_seconds) $rc .= ":".$part3;
  1511. }
  1512. }
  1513. }
  1514. }
  1515. return $rc;
  1516. }
  1517. /**
  1518. * convert datetime format
  1519. *
  1520. * @param string $datetime db datetime field format
  1521. * @param char $date_format see member variable m_date above
  1522. * @param char $time_format see member variable m_time above
  1523. * @param bool $with_seconds show seconds
  1524. * @param bool $short_mode if today, yesterday or tomorrow
  1525. * show string for date
  1526. * @param string $at
  1527. * @return string datetime in converted format
  1528. */
  1529. function convDateTime($datetime, $date_format="", $time_format="", $with_seconds=false, $short_mode=true, $at=null)
  1530. {
  1531. $rc="";
  1532. if (!isvoid($datetime))
  1533. {
  1534. if ((!isset($date_format)) || (empty($date_format))) $date_format = $this->date_format;
  1535. if ((!isset($time_format)) || (empty($time_format))) $time_format = $this->time_format;
  1536. $tmp_date = strtok($datetime, " ");
  1537. $tmp_time = strtok(" ");
  1538. if (($short_mode) && (!isvoid($tmp_time)))
  1539. $rc = $this->convDate($tmp_date, 'S', $date_format, array('short_mode'=>true));
  1540. else
  1541. $rc = $this->convDate($tmp_date, 'S', $date_format);
  1542. if (!isvoid($tmp_time))
  1543. {
  1544. if (isset($at)) $tmp_at = $at;
  1545. else $tmp_at = STR_AT;
  1546. if ($tmp_at!=",") $rc .= "&nbsp;";
  1547. $rc .= $tmp_at."&nbsp;";
  1548. $rc .= $this->convTime($tmp_time, $time_format, $with_seconds);
  1549. }
  1550. }
  1551. return $rc;
  1552. }
  1553. /**
  1554. * Convert date format
  1555. *
  1556. * @param string $date_str date to convert
  1557. * @param string $src_format see member variables $this->date_format above
  1558. * @param string $dest_format see member variables $this->date_format above or 'short' or 'long'
  1559. * @param array $customize year_format string = long : e.g. 2002 {default}
  1560. * short: e.g. 02
  1561. * void : e.g.
  1562. * leading_zeros bool = true : e.g. 01 {default}
  1563. * false: e.g. 1
  1564. * with_weekday bool = true: with weekday
  1565. * false: without weekdays
  1566. * (only available if dest_format is short or long)
  1567. * locale string = see member variables m_locale above
  1568. * first_valid_year = first valid year, if specified date is before, it will be signed
  1569. * as invalid {default: 1850}
  1570. * short_mode = if today, yesterday or tomorrow
  1571. * show string for date {default: false}
  1572. * @return string date or {if wrong input) empty
  1573. */
  1574. function convDate($date_str, $src_format, $dest_format, $customize=null)
  1575. {
  1576. $rc = "";
  1577. $sep_old = "";
  1578. $sep_new = "";
  1579. $date_package = false;
  1580. // all years are possible, but year 0 cannot be selected (does anyone need it?)
  1581. if ((!isset($customize['first_valid_year'])) || (intval($customize['first_valid_year'])==0))
  1582. $customize['first_valid_year'] = 1850;
  1583. if ($this->file_exists("Date.php"))
  1584. {
  1585. require_once("Date.php");
  1586. $date_package = true;
  1587. }
  1588. if ((isset($customize['locale'])) && (!setlocale(LC_TIME,$customize['locale'])))
  1589. {
  1590. $this->show_error("convDate", "locale not readable from system!");
  1591. }
  1592. if (strtoupper($src_format)=="L") // DD.MM.YYYY
  1593. {
  1594. $sep_old = $this->m_sep_l;
  1595. $day = intval(strtok($date_str,$sep_old));
  1596. $month = intval(strtok($sep_old));
  1597. $year = intval(strtok($sep_old));
  1598. if ($year<20) $year += 2000;
  1599. else if ($year<100) $year += 1900;
  1600. }
  1601. else if (strtoupper($src_format)=="I") // MM/DD/YYYY
  1602. {
  1603. $sep_old = $this->m_sep_i;
  1604. $month = intval(strtok($date_str,$sep_old));
  1605. $day = intval(strtok($sep_old));
  1606. $year = intval(strtok($sep_old));
  1607. if ($year<20) $year += 2000;
  1608. else if ($year<100) $year += 1900;
  1609. }
  1610. else if (strtoupper($src_format)=="S") // YYYY-MM-DD
  1611. {
  1612. $sep_old = $this->m_sep_s;
  1613. $year = intval(strtok($date_str,$sep_old));
  1614. $month = intval(strtok($sep_old));
  1615. $day = intval(strtok($sep_old));
  1616. if ($year<20) $year += 2000;
  1617. else if ($year<100) $year += 1900;
  1618. }
  1619. else if (strtoupper($src_format)=="C") // YYYYMMDD
  1620. {
  1621. if (strlen($date_str)==8)
  1622. {
  1623. $year = substr($date_str,0,4);
  1624. $month = substr($date_str,4,2);
  1625. $day = substr($date_str,6,2);
  1626. }
  1627. else
  1628. {
  1629. $year = substr($date_str,0,2);
  1630. $month = substr($date_str,2,2);
  1631. $day = substr($date_str,4,2);
  1632. }
  1633. if ($year<20) $year += 2000;
  1634. else if ($year<100) $year += 1900;
  1635. }
  1636. if (strtoupper($dest_format)=="I") $sep_new = $this->m_sep_i;
  1637. else if (strtoupper($dest_format)=="S") $sep_new = $this->m_sep_s;
  1638. else if (strtoupper($dest_format)=="L") $sep_new = $this->m_sep_l;
  1639. else if (strtoupper($dest_format)=="C") $sep_new = "";
  1640. else $sep_new = $this->m_sep_s;
  1641. if ((checkdate($month,$day,$year)) && ($customize['first_valid_year']<=$year))
  1642. {
  1643. $short_mode = false;
  1644. if ((isset($customize['short_mode'])) && ($customize['short_mode']))
  1645. {
  1646. if (date("Y-n-j")=="$year-$month-$day")
  1647. {
  1648. $rc = STR_TODAY;
  1649. $short_mode=true;
  1650. }
  1651. else if (date("Y-n-j", mktime(date('H'),date('i'), 0,date('m'), date('d')+1,date('Y')))=="$year-$month-$day")
  1652. {
  1653. $rc = STR_TOMORROW;
  1654. $short_mode=true;
  1655. }
  1656. else if (date("Y-n-j", mktime(date('H'),date('i'), 0,date('m'), date('d')-1,date('Y')))=="$year-$month-$day")
  1657. {
  1658. $rc = STR_YESTERDAY;
  1659. $short_mode=true;
  1660. }
  1661. }
  1662. if ($short_mode==false)
  1663. {
  1664. if (isset($customize['year_format']))
  1665. {
  1666. $y_form = $customize['year_format'];
  1667. if ($y_form=="short") $year = substr($year,strlen($year)-2,2);
  1668. else if ($y_form=="void") $year = "";
  1669. }
  1670. $day = sprintf("%02d",$day);
  1671. $month = sprintf("%02d",$month);
  1672. if ((isset($customize['leading_zeros'])) && (!empty($sep_new)))
  1673. {
  1674. $l_zeros = $customize['leading_zeros'];
  1675. if (!$l_zeros)
  1676. {
  1677. $day = intval($day);
  1678. $month = intval($month);
  1679. }
  1680. }
  1681. else if (empty($sep_new))
  1682. {
  1683. if (strlen($day)<2) $day .= "0";
  1684. if (strlen($month)<2) $month .= "0";
  1685. }
  1686. if (($dest_format=="long") || ($dest_format=="short"))
  1687. {
  1688. if ($date_package)
  1689. {
  1690. $stamp = new Date();
  1691. $stamp->setDate("$year-$month-$day",DATE_FORMAT_ISO);
  1692. }
  1693. $wday = "";
  1694. if (isset($customize['with_weekday']))
  1695. {
  1696. $with_wday = $customize['with_weekday'];
  1697. if ($with_wday)
  1698. {
  1699. if ($date_package)
  1700. {
  1701. if ($dest_format=="long") $wday = $stamp->format("%A").", ";
  1702. else $wday = $stamp->format("%a").", ";
  1703. }
  1704. else
  1705. {
  1706. $this->show_error("convDate", "Pear \"Date Package\" required for specificed parameters.");
  1707. }
  1708. }
  1709. }
  1710. if ($date_package)
  1711. {
  1712. if ($dest_format=="long") $month = $stamp->format("%B");
  1713. else $month = $stamp->format("%b");
  1714. }
  1715. else
  1716. {
  1717. $this->show_error("convDate", "Pear \"Date Package\" required for specificed parameters.");
  1718. }
  1719. if ($this->m_date=="I") $rc = $wday.$month."/".$day."/".$year;
  1720. else $rc = $wday.$day.". ".$month." ".$year;
  1721. }
  1722. else
  1723. {
  1724. if ($sep_new==$this->m_sep_i)
  1725. {
  1726. $rc = $month.$this->m_sep_i.$day;
  1727. if (!isvoid($year))
  1728. $rc .= $this->m_sep_i.$year;
  1729. }
  1730. else if ($sep_new==$this->m_sep_s)
  1731. {
  1732. if (!isvoid($year))
  1733. $rc .= $year.$this->m_sep_s;
  1734. $rc .= $month.$this->m_sep_s.$day;
  1735. }
  1736. else if ($sep_new==$this->m_sep_l)
  1737. {
  1738. $rc = $day.$this->m_sep_l.$month.$this->m_sep_l.$year;
  1739. }
  1740. else
  1741. {
  1742. $rc = intval($year.$month.$day);
  1743. }
  1744. }
  1745. } // !short mode end
  1746. }
  1747. return $rc;
  1748. }
  1749. /**
  1750. * Checks whether a file or directory exists (also checks the include_path in php.ini)
  1751. *
  1752. * @param string $filename
  1753. * @return bool
  1754. */
  1755. function file_exists($filename)
  1756. {
  1757. $os_type = $this->os_type(true);
  1758. if ($os_type=="w")
  1759. {
  1760. if (substr($filename,1,3) == ":\\")
  1761. return file_exists($filename);
  1762. $sep = ";";
  1763. $sep_dir = "\\";
  1764. }
  1765. else
  1766. {
  1767. if (substr($filename,0,1) == "/")
  1768. return file_exists($filename);
  1769. $sep = ":";
  1770. $sep_dir = "/";
  1771. }
  1772. $incpath = ini_get("include_path");
  1773. foreach (explode($sep,$incpath) as $path)
  1774. {
  1775. if (file_exists($path.$sep_dir.$filename))
  1776. return true;
  1777. }
  1778. return false;
  1779. }
  1780. /**
  1781. * validate number (positiv) with wildcards
  1782. *
  1783. * @param string $number number to validate
  1784. * @param char $sep valid seperator {default: ","}
  1785. * @return bool
  1786. */
  1787. function checkNumWildcard($number, $sep=",")
  1788. {
  1789. $needle = "/[^0-9\*".$sep."]/";
  1790. if (!preg_match($needle, trim($number)))
  1791. return true;
  1792. else
  1793. return false;
  1794. }
  1795. /**
  1796. * validate phone number
  1797. *
  1798. * @param string $phone phone number to validate
  1799. * @param bool $mode
  1800. * @return bool
  1801. */
  1802. function checkPhone($phone, $mode=false)
  1803. {
  1804. if ($mode) $needle = '/[^\sA-Za-z0-9\(\)\-\+\/]/';
  1805. else $needle = '/[^\s0-9\(\)\-\+\/]/';
  1806. if (!preg_match($needle, $phone))
  1807. return true;
  1808. else
  1809. return false;
  1810. }
  1811. /**
  1812. * validate post zip code
  1813. *
  1814. * @param string $zip zip to validate
  1815. * @return bool
  1816. */
  1817. function checkZip($zip)
  1818. {
  1819. if (!preg_match('/[^\s\w\-]/', $zip))
  1820. return true;
  1821. else
  1822. return false;
  1823. }
  1824. /**
  1825. * validate German Bankleitzahl
  1826. * ( allowed seperators: "-", "." or " ")
  1827. *
  1828. * @param string $bank_id bank id (German BLZ) to validate
  1829. * @return bool
  1830. */
  1831. function checkBankId($bank_id)
  1832. {
  1833. $bank_id = str_replace("-", "", $bank_id);
  1834. $bank_id = str_replace(".", "", $bank_id);
  1835. $bank_id = str_replace(" ", "", $bank_id);
  1836. if (strlen($bank_id)==8) return true;
  1837. else return false;
  1838. }
  1839. /**
  1840. * Check birthday
  1841. *
  1842. * @param date $birthday
  1843. * @param char $date_format
  1844. * @return bool
  1845. */
  1846. function checkBirthday($birthday, $date_format="S")
  1847. {
  1848. $tmp_birthday = $this->convDate($birthday, $date_format, 'S');
  1849. $tmp_b = $this->convDate($tmp_birthday, "S", "C");
  1850. $tmp_now = date("Ymd");
  1851. if (!$tmp_birthday) return false;
  1852. else if ($tmp_b>$tmp_now) return false;
  1853. return true;
  1854. }
  1855. /**
  1856. * validate email address format
  1857. *
  1858. * @param string $email email address
  1859. * @param bool $with_dns true for dns check {default: false}
  1860. * (only works on non-windows platforms)
  1861. * @return bool
  1862. */
  1863. function checkEmail($email, $with_dns=false)
  1864. {
  1865. $rc = false;
  1866. $known_doms = "ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba";
  1867. $known_doms .= "|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca";
  1868. $known_doms .= "|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy";
  1869. $known_doms .= "|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm";
  1870. $known_doms .= "|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw";
  1871. $known_doms .= "|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm";
  1872. $known_doms .= "|jo|jobs|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls";
  1873. $known_doms .= "|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt";
  1874. $known_doms .= "|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np";
  1875. $known_doms .= "|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw";
  1876. $known_doms .= "|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so";
  1877. $known_doms .= "|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|travel|tt|tv";
  1878. $known_doms .= "|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt";
  1879. $known_doms .= "|yu|za|zm|zw";
  1880. $email = trim($email);
  1881. if (preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+($known_doms)$|(([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", $email))
  1882. {
  1883. if (($with_dns) && ($this->os_type(true)!="w"))
  1884. {
  1885. $host = explode('@', $email);
  1886. // Check for MX record
  1887. if( checkdnsrr($host[1], 'MX') ) $rc = true;
  1888. // Check for A record
  1889. if( checkdnsrr($host[1], 'A') ) $rc = true;
  1890. // Check for CNAME record
  1891. if( checkdnsrr($host[1], 'CNAME') ) $rc = true;
  1892. }
  1893. else
  1894. $rc = true;
  1895. }
  1896. return $rc;
  1897. }
  1898. /**
  1899. * validate domain name
  1900. *
  1901. * @param string $domain domain name
  1902. * @param string $tld top level domain
  1903. * @return bool
  1904. */
  1905. function checkDomainName($domain, $tld=null)
  1906. {
  1907. $rc = false;
  1908. $len = strlen($domain);
  1909. $dot_count = substr_count($domain, ".");
  1910. if (($dot_count>0) &&
  1911. ($len>1) &&
  1912. ($len<=(63*$dot_count)) &&
  1913. (!preg_match('/[^a-z0-9\._\-]/i', $domain)))
  1914. {
  1915. if (isset($tld))
  1916. {
  1917. if (substr($domain, ($len - strlen($tld) - 1)==$tld))
  1918. $rc = true;
  1919. }
  1920. else
  1921. $rc = true;
  1922. }
  1923. return $rc;
  1924. }
  1925. /**
  1926. * validate IP address
  1927. *
  1928. * @param string $addr IP address to validate
  1929. * @return bool true if valid
  1930. */
  1931. function checkIP($addr)
  1932. {
  1933. $rc = false;
  1934. if ((strlen($addr)<16) && (!preg_match('/[^0-9\.]/', $addr)))
  1935. {
  1936. $ip = explode(".", $addr);
  1937. if (count($ip)==4)
  1938. {
  1939. $rc = true;
  1940. for ($i=0;$i<4;$i++)
  1941. {
  1942. if ((strlen($ip[$i])==0) ||
  1943. ($ip[$i]<0) ||
  1944. ($ip[$i]>255))
  1945. {
  1946. $rc = false;
  1947. break;
  1948. }
  1949. }
  1950. }
  1951. }
  1952. return $rc;
  1953. }
  1954. /**
  1955. * validate MAC address
  1956. *
  1957. * @param string $addr MAC address to validate
  1958. * @return bool
  1959. */
  1960. function checkMAC($addr)
  1961. {
  1962. $rc = false;
  1963. if ((strlen($addr)==17) && (!preg_match('/[^A-Fa-f0-9\:]/',$addr)))
  1964. {
  1965. $mac = explode(":", $addr);
  1966. if (count($mac)==6)
  1967. {
  1968. $rc = true;
  1969. for ($i=0;$i<=5;$i++)
  1970. {
  1971. if ((strlen($mac[$i])!=2) ||
  1972. (hexdec($mac[$i])<0) ||
  1973. (hexdec($mac[$i]>255)))
  1974. {
  1975. $rc = false;
  1976. break;
  1977. }
  1978. }
  1979. }
  1980. }
  1981. return $rc;
  1982. }
  1983. /**
  1984. * check if a string has HTML tags
  1985. *
  1986. * @param string $str string to validate
  1987. * @return bool true if string has html tags
  1988. */
  1989. function checkHtmlTag($str)
  1990. {
  1991. if (!preg_match('/[<]/',trim($str)))
  1992. return true;
  1993. else
  1994. return false;
  1995. }
  1996. /**
  1997. * check if string is a valid color value for HTML and CSS
  1998. *
  1999. * @param string $str string to validate
  2000. * @return bool
  2001. */
  2002. function checkColor($str)
  2003. {
  2004. $rc = false;
  2005. $colors = array("aqua", "black", "blue", "fuchsia", "gray", "green", "lime",
  2006. "maroon", "navy", "olive", "purple", "red", "silver", "teal", "white", "yellow");
  2007. if (in_array($str, $colors))
  2008. $rc = true;
  2009. else if ((substr($str,0,1)=="#") && (strlen($str)<8))
  2010. {
  2011. if (!preg_match('/[^A-Fa-f0-9]/',substr($str,1)))
  2012. $rc = true;
  2013. }
  2014. return $rc;
  2015. }
  2016. /**
  2017. * is string a valid number?
  2018. *
  2019. * @param string $number number to validate
  2020. * @param char $sep valid seperator {default: "."}
  2021. * @param bool $mode if true, with negative numbers {default: true}
  2022. * @param bool $with_null if true, number "0" is valid {default: true}
  2023. * @return bool
  2024. */
  2025. function checkNumber($number, $sep=".", $mode=true, $with_null=true)
  2026. {
  2027. $rc = false;
  2028. $num = trim($number);
  2029. if (!isvoid($sep))
  2030. {
  2031. $num_sep = substr_count($num, $sep);
  2032. if ($num_sep < 2)
  2033. {
  2034. if ($mode)
  2035. $simple = "/[^0-9\-\\$sep]/";
  2036. else
  2037. $simple = "/[^0-9\\$sep]/";
  2038. if (!preg_match($simple, $num))
  2039. {
  2040. $pos = strpos($num, '-');
  2041. if ($pos===false) $rc = true;
  2042. else if ($pos==0) $rc = true;
  2043. }
  2044. }
  2045. else if (($mode) && (!preg_match('/[^0-9\-]/',$num))) $rc = true;
  2046. else if (!preg_match('/[^0-9]/',$num)) $rc = true;
  2047. }
  2048. else // only integer
  2049. {
  2050. if (($mode) && (!preg_match('/[^0-9\-]/',$num))) $rc = true;
  2051. else if (!preg_match('/[^0-9]/',$num)) $rc = true;
  2052. }
  2053. if ((!$with_null) && ($rc) && ($num=="0"))
  2054. $rc = false;
  2055. return $rc;
  2056. }
  2057. /**
  2058. * validate user name
  2059. * (only alpha-numeric, '_' and '-' (but not as first sign,
  2060. * because some systems has problems with this) are allowed)
  2061. *
  2062. * @param string $user_name user to validate
  2063. * @param int $min_length minimal length of username {default: 3}
  2064. * @param bool $allow_upper_case true : case insensitive {default: true}
  2065. * false : upper letters are not allowed
  2066. * @return bool
  2067. */
  2068. function checkUserName($user_name, $min_length=3, $allow_upper_case=true)
  2069. {
  2070. $rc = false;
  2071. $str = trim($user_name);
  2072. if ((strlen($str) >= $min_length) && ($str[0]!='-'))
  2073. {
  2074. if (!$allow_upper_case) $val='/[^a-z0-9\_\-]/';
  2075. else $val='/[^A-Za-z0-9\_\-]/';
  2076. if (!preg_match($val, $str)) $rc = true;
  2077. }
  2078. return $rc;
  2079. }
  2080. /**
  2081. * checks version of php and if pear is installed
  2082. *
  2083. * @param string $min_ver oldest php version, which is allowed
  2084. * @param bool $with_pear true: if pear must installed {default: false}
  2085. * @param bool $mode true: die with message {default: false}
  2086. * @return bool
  2087. */
  2088. function check_php_version($min_ver, $with_pear=false, $mode=false)
  2089. {
  2090. $rc = false;
  2091. $pear_installed = false;
  2092. $curr_php = phpversion();
  2093. $c_p1 = strtok($curr_php,'.');
  2094. $c_p2 = strtok('.');
  2095. $c_p3 = strtok('.');
  2096. $m_p1 = strtok($min_ver,'.');
  2097. $m_p2 = strtok('.');
  2098. $m_p3 = strtok('.');
  2099. if ($c_p1 > $m_p1) $rc=true;
  2100. else if ($c_p1==$m_p1)
  2101. {
  2102. if ($c_p2 > $m_p2) $rc=true;
  2103. else if ($c_p2==$m_p2)
  2104. {
  2105. // if c_p3 is no number (because it is a dev version, take 0)
  2106. if (!cphplib::checkNumber($c_p3,""))
  2107. $c_p3=0;
  2108. if ($c_p3 >= $m_p3) $rc=true;
  2109. }
  2110. }
  2111. // pear only works with php >= 4
  2112. if (($with_pear) && ($c_p1>=4))
  2113. {
  2114. @ include_once("PEAR.php");
  2115. $pear_installed = class_exists("PEAR");
  2116. }
  2117. if ($mode)
  2118. {
  2119. if (!$rc)
  2120. {
  2121. $this->show_error("check_php_version", "version >= $min_ver required (installed php version is $curr_php).");
  2122. }
  2123. else if (($with_pear) && (!$pear_installed))
  2124. {
  2125. $this->show_error("check_php_version", "php pear is missing (installed php version is $curr_php).");
  2126. }
  2127. }
  2128. else
  2129. {
  2130. if (($with_pear) && (!$pear_installed))
  2131. $rc = false;
  2132. }
  2133. return $rc;
  2134. }
  2135. /**
  2136. * validate password format
  2137. *
  2138. * @param string $password password string
  2139. * @param int $min_length minimal length of password {default: 5}
  2140. * @param int $strength 0: no special rules {default}
  2141. * 1: at least one number and one char
  2142. * 2: same as 1 and at least one lowercase and and uppercase letter
  2143. * 3: same as 2 and at least one special character
  2144. * @return bool true for qualified password
  2145. */
  2146. function checkPassword($password, $min_length=5, $strength=0)
  2147. {
  2148. $rc = false;
  2149. $password = trim($password);
  2150. $special = "\.\,;\:\!\@\#\%\&\*\-\_\=\(\)\+§<>\$";
  2151. $allowed = "/[^[:alnum:]".$special."]/i";
  2152. if ((strlen($password) >= $min_length) && (!preg_match($allowed, $password)))
  2153. {
  2154. switch($strength)
  2155. {
  2156. case 1: // at least one number and one char
  2157. if ((preg_match('/[A-Za-z]/i', $password)) &&
  2158. (preg_match('/[0-9]/', $password)))
  2159. $rc = true;
  2160. break;
  2161. case 2: // same as 1 and at least one lowercase and and uppercase letter
  2162. if ((preg_match('/[A-Z]/', $password)) &&
  2163. (preg_match('/[a-z]/', $password)) &&
  2164. (preg_match('/[0-9]/', $password)))
  2165. $rc = true;
  2166. break;
  2167. case 3: // same as 2 and at least one special character
  2168. if ((preg_match('/[A-Z]/', $password)) &&
  2169. (preg_match('/[a-z]/', $password)) &&
  2170. (preg_match('/[0-9]/', $password)) &&
  2171. (preg_match('/['.$special.']/', $password)))
  2172. $rc = true;
  2173. break;
  2174. default: // no special rules
  2175. $rc = true;
  2176. }
  2177. }
  2178. return $rc;
  2179. }
  2180. /**
  2181. * validate string with letters only
  2182. *
  2183. * @param string $str tring to validate
  2184. * @param bool $mode true for German Umlaute include {default: false}
  2185. * @return bool
  2186. */
  2187. function checkString($str,$mode=false)
  2188. {
  2189. $rc = false;
  2190. $str = trim($str);
  2191. if ($mode==true)
  2192. {
  2193. if (!preg_match('/[^A-Za-zöüäßÄÜÖ]/',$str))
  2194. $rc = true;
  2195. }
  2196. else
  2197. {
  2198. if (!preg_match('/[^A-Za-z]/',$str))
  2199. $rc = true;
  2200. }
  2201. return $rc;
  2202. }
  2203. /**
  2204. * convert German Umlaute to HTML and vice versa
  2205. *
  2206. * @param string $str string to convert
  2207. * @param bool $mode true: convert to HTML
  2208. * false: from HTML
  2209. * @return string
  2210. */
  2211. function convUmlaute($str, $mode=true)
  2212. {
  2213. if ($mode)
  2214. {
  2215. $str = str_replace("ä", "&auml;", $str);
  2216. $str = str_replace("ö", "&ouml;", $str);
  2217. $str = str_replace("ü", "&uuml;", $str);
  2218. $str = str_replace("Ä", "&Auml;", $str);
  2219. $str = str_replace("Ö", "&Ouml;", $str);
  2220. $str = str_replace("Ü", "&Uuml;", $str);
  2221. $str = str_replace("ß", "&szlig;", $str);
  2222. }
  2223. else
  2224. {
  2225. $str = str_replace("&auml;", "ä", $str);
  2226. $str = str_replace("&ouml;", "ö", $str);
  2227. $str = str_replace("&uuml;", "ü", $str);
  2228. $str = str_replace("&Auml;", "Ä", $str);
  2229. $str = str_replace("&Ouml;", "Ö", $str);
  2230. $str = str_replace("&Uuml;", "Ü", $str);
  2231. $str = str_replace("&szlig;", "ß", $str);
  2232. }
  2233. return $str;
  2234. }
  2235. /**
  2236. * Envelops a string with quotes (for db INSERT,UPDATES or SELECT)
  2237. *
  2238. * @param string $str string to convert
  2239. * @param bool $conv_special_char convert special charactar to html convention
  2240. * (great helper to secure SQL strings) {default: false}
  2241. * (conv_special_char is not recomended for an INSERT or UPDATE SQL string)
  2242. * @param int $active_null 0: deactive NULL value
  2243. * 1: active NULL value {default}
  2244. * 2: active NULL value, with 0 is invalid
  2245. * @return string converted string
  2246. */
  2247. function sql_value($str,$conv_special_char=false, $active_null=1)
  2248. {
  2249. if (!isvoid($str)) $str = trim($str);
  2250. if ((($active_null==1) && (!isvoid($str))) ||
  2251. (($active_null==2) && (!empty($str))))
  2252. {
  2253. if ($conv_special_char)
  2254. {
  2255. $str = $this->convHtml($str);
  2256. $str = $this->convUmlaute($str, false);
  2257. }
  2258. return "'" . addslashes($str) . "'";
  2259. }
  2260. else if ($active_null>0) return "NULL";
  2261. else return "''";
  2262. }
  2263. /**
  2264. * convert ascii string to HTML string
  2265. * (because of a bug, it is better to set magic_quotes_gpc=Off in your php.ini)
  2266. *
  2267. * @param string $str string to convert
  2268. * @param bool $mode true : html->ascii
  2269. * false: ascii->html {default}
  2270. * @param bool $with_trim true : cut off whitespaces before and after the string {default}
  2271. * false: leave string with whitespaces
  2272. * @param bool $with_link true : allow HTML links
  2273. * (you have to use [url=http://mydomain.com/]thebest[/url] syntax or
  2274. * [mail=me@mydomain.com]Me[/mail]
  2275. * (at the moment it works only in ascii->html))
  2276. * false: disallow HTML links {default}
  2277. * @return string
  2278. */
  2279. function convHtml($str, $mode=false, $with_trim=true, $with_link=false)
  2280. {
  2281. if ($with_trim) $str = trim($str);
  2282. if (!$mode) // ascii -> html
  2283. {
  2284. if ((!isvoid($str)) && ($str != '*'))
  2285. {
  2286. if (ini_get("magic_quotes_gpc")!=1) // if "magic_quotes_gpc=off"
  2287. {
  2288. $str = str_replace("\\", "\\\\", $str);
  2289. $str = stripslashes($str);
  2290. }
  2291. if (defined(STR_CHARACTER_SET)) $charset = STR_CHARACTER_SET;
  2292. else $charset = "iso-8859-1";
  2293. if (defined(STR_CHARACTER_SET)) $rc = htmlentities($str, ENT_QUOTES, $charset);
  2294. else $rc = htmlentities($str, ENT_QUOTES);
  2295. }
  2296. else
  2297. $rc = $str;
  2298. if ($with_link)
  2299. {
  2300. // Set up the parameters for a URL search string
  2301. $URLSearchString = " a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'";
  2302. // Set up the parameters for a MAIL search string
  2303. $MAILSearchString = $URLSearchString . " a-zA-Z0-9\.@";
  2304. // Perform URL Search
  2305. $rc = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/", '<a href="$1" target="_blank">$1</a>', $rc);
  2306. $rc = preg_replace("(\[url\=([$URLSearchString]*)\](.*?)\[/url\])", '<a href="$1" target="_blank">$2</a>', $rc);
  2307. // Perform same Window URL Search
  2308. $rc = preg_replace("/\[surl\]([$URLSearchString]*)\[\/surl\]/", '<a href="$1">$1</a>', $rc);
  2309. $rc = preg_replace("(\[surl\=([$URLSearchString]*)\](.*?)\[/surl\])", '<a href="$1">$2</a>', $rc);
  2310. // Perform MAIL Search
  2311. $rc = preg_replace("(\[mail\]([$MAILSearchString]*)\[/mail\])", '<a href="mailto:$1">$1</a>', $rc);
  2312. $rc = preg_replace("(\[mail\=([$MAILSearchString]*)\](.*?)\[/mail\])", '<a href="mailto:$1">$2</a>', $rc);
  2313. // Check for bold text
  2314. $rc = preg_replace("(\[b\](.+?)\[\/b])is",'<span style="font-weight: bold;">$1</span>', $rc);
  2315. // Check for italic text
  2316. $rc = preg_replace("(\[i\](.+?)\[\/i\])is",'<span style="font-style: italic;">$1</span>', $rc);
  2317. }
  2318. }
  2319. else // html -> ascii
  2320. {
  2321. $trans = get_html_translation_table(HTML_ENTITIES);
  2322. $trans = array_flip($trans);
  2323. $rc = strtr($str, $trans);
  2324. $rc = str_replace("&euro;","EUR",$rc);
  2325. if (!$with_link) // show url text, if available
  2326. {
  2327. $start_tag_1 = "[url=\"";
  2328. $start_tag_1_count = substr_count($rc, $start_tag_1);
  2329. $start_tag_2 = "\"]";
  2330. $start_tag_2_count = substr_count($rc, $start_tag_2);
  2331. $end_tag = "[/url]";
  2332. $end_tag_count = substr_count($rc, $end_tag);
  2333. if (($start_tag_1_count>0) &&
  2334. ($start_tag_1_count==$end_tag_count) &&
  2335. ($start_tag_2_count==$end_tag_count)) // clear links only, if we have the same sum of start and end tags
  2336. {
  2337. $rc = str_replace($end_tag,"", $rc);
  2338. $pos1 = strpos($rc,$start_tag_1);
  2339. while($pos1)
  2340. {
  2341. $pos2 = strpos($rc,$start_tag_2);
  2342. $part1 = substr($rc,0,$pos1);
  2343. $part2 = substr($rc,($pos2+strlen($start_tag_2)));
  2344. $rc = $part1.$part2;
  2345. $pos1 = strpos($rc,$start_tag_1);
  2346. }
  2347. }
  2348. }
  2349. }
  2350. return $rc;
  2351. }
  2352. /**
  2353. * convert first letter to upper or lower
  2354. *
  2355. * @param string $str string to convert
  2356. * @param bool $mode true for upper, false for lower
  2357. * @return string
  2358. */
  2359. function convFirstLetter($str,$mode=false)
  2360. {
  2361. if (!$mode)
  2362. return substr(strtolower($str),0,1).substr($str,1,(strlen($str)-1));
  2363. else
  2364. return substr(strtouppter($str),0,1).substr($str,1,(strlen($str)-1));
  2365. }
  2366. /**
  2367. * Return end tag for formular objects
  2368. * attending xhtml settings ($this->xhtml)
  2369. *
  2370. * @return string
  2371. */
  2372. function end_tag()
  2373. {
  2374. if ($this->xhtml) return " />";
  2375. else return ">";
  2376. }
  2377. /**
  2378. * prints a meta description and meta keywords line for xhtml
  2379. * Required : every line in filename is a keyword, first line is the description
  2380. * (first line with quotes!!!)
  2381. *
  2382. * @param string $filename filename with keywords
  2383. * @param bool $mode true: print it as meta tags {default}
  2384. * false: return array with text and keywords
  2385. * @return mixed if mode is false, array with text and keywords
  2386. */
  2387. function metatag_write($filename, $mode=true)
  2388. {
  2389. $rc = array();
  2390. if (file_exists($filename))
  2391. {
  2392. $close_sign = $this->end_tag()."\n";
  2393. $fcontents = file($filename);
  2394. $meta = array();
  2395. while (list ($line_num, $line) = each ($fcontents))
  2396. {
  2397. if ($line_num==0)
  2398. {
  2399. $ver_info = strstr ($line, 'INFO');
  2400. if (!empty($ver_info))
  2401. {
  2402. $ver_info = trim(substr($ver_info,6));
  2403. $ver_len = strlen($ver_info)-1;
  2404. $rc['text'] = substr($ver_info,0,$ver_len);
  2405. if (($mode) && (!isvoid($rc['text'])))
  2406. {
  2407. print "\t<meta name=\"description\" content=\"".$rc['text']."\"".$close_sign;
  2408. print "\t<meta name=\"DC.Description\" content=\"".$rc['text']."\"".$close_sign;
  2409. }
  2410. }
  2411. }
  2412. else
  2413. {
  2414. $line = trim($line);
  2415. if (!isvoid($line))
  2416. $meta[] = $line;
  2417. }
  2418. }
  2419. if (count($meta))
  2420. {
  2421. $count=0;
  2422. $rc['keywords'] = "";
  2423. while(list(, $values)=each($meta))
  2424. {
  2425. $count++;
  2426. if ($count==1) $rc['keywords'] .= $values;
  2427. else $rc['keywords'] .= ",".$values;
  2428. }
  2429. if ($mode)
  2430. {
  2431. print "\t<meta name=\"keywords\" content=\"".$rc['keywords']."\"".$close_sign;
  2432. }
  2433. }
  2434. }
  2435. if (!$mode) return $rc;
  2436. else return "";
  2437. }
  2438. /**
  2439. * hit counter for web pages
  2440. * Required : every line in filename is a keyword, first line is the description
  2441. * (first line with quotes!!!)
  2442. *
  2443. * @param string $id id string
  2444. * @param bool $r true: reload allowed
  2445. * false: reload disallowed {default}
  2446. * @param bool $db_open true: database connection aready open
  2447. * false: no database connection open, open one
  2448. * @param array $CONF CONF database array, if empty use counterconfig.php
  2449. * (have to be in same directory as cphplib.inc)
  2450. * @return int
  2451. */
  2452. function hitcounter($id, $r=false, $db_open=false, $CONF=null)
  2453. {
  2454. if (empty($id))
  2455. {
  2456. $this->show_error("hitcounter", "missing id string.");
  2457. }
  2458. $server_domain = $_SERVER['SERVER_NAME'];
  2459. if ($CONF['storage_type']!="files") // db
  2460. {
  2461. if (!$db_open)
  2462. {
  2463. if (!is_array($CONF))
  2464. require_once("hitconfig.php");
  2465. $this->db =& $this->db_connect($CONF['db']);
  2466. }
  2467. if (empty($this->db))
  2468. {
  2469. $this->show_error("hitcounter", "PEAR database object missing");
  2470. }
  2471. }
  2472. else if (!is_array($CONF)) // if storage is files, always open hitconfig.php, if no CONF array is given
  2473. require_once("hitconfig.php");
  2474. if (!is_array($CONF))
  2475. {
  2476. $this->show_error("hitcounter", "no CONF array found.");
  2477. }
  2478. $file_count = $CONF['storage_dir']."/hitcounter_".$id.".count";
  2479. $file_id = $CONF['storage_dir']."/hitcounter_".$id.".id";
  2480. $last_num = 0;
  2481. $rc_num = 0;
  2482. $current_id_string = $this->get_id_string(false);
  2483. ////////////////////////////////////////////////////////////////////////////
  2484. // check if counter is allowed in current script
  2485. if (!$this->net_access($CONF['allowed_domains'], $CONF['allowed_ips'], "server"))
  2486. {
  2487. die(STR_ACCESS_DENIED);
  2488. }
  2489. if ($CONF['storage_type']!="files") // db
  2490. {
  2491. // get old number
  2492. $sqlstr = "SELECT hits,id_string FROM ".$CONF['db']['table']." WHERE domain=".$this->sql_value($server_domain);
  2493. $sqlstr .= " AND counter_name=".$this->sql_value($id);
  2494. $row = $this->db->getRow($sqlstr);
  2495. if (DB::isError($row))
  2496. {
  2497. $this->show_error("hitcounter", $row->getMessage());
  2498. }
  2499. if (is_array($row))
  2500. {
  2501. $last_num = $row[0];
  2502. $last_id_string = $row[1];
  2503. }
  2504. }
  2505. else if (file_exists($file_count)) // files
  2506. {
  2507. $fd = fopen($file_count,"r+" );
  2508. $last_num = intval(fread($fd,filesize($file_count)));
  2509. fclose($fd);
  2510. if (file_exists($file_id)) // no get last_id_string, if file exits
  2511. {
  2512. $fd = fopen($file_id,"r+");
  2513. $last_id_string = fread($fd,filesize($file_id));
  2514. fclose($fd);
  2515. }
  2516. }
  2517. if ($last_num>0)
  2518. {
  2519. ////////////////////////////////////////////////////////////////////////////
  2520. // check if visistor should be ignored
  2521. $ignore_hit = $this->net_access($CONF['ignored_domains'], $CONF['ignored_ips']);
  2522. if ((!$ignore_hit) && (($last_id_string!=$current_id_string) || ($r)) )
  2523. {
  2524. $rc_num = $last_num+1;
  2525. if ($CONF['storage_type']=="files")
  2526. {
  2527. $fd = fopen($file_count,"w+");
  2528. fwrite($fd,$rc_num);
  2529. fclose($fd);
  2530. $fd = fopen($file_id,"w+");
  2531. fwrite($fd,$current_id_string);
  2532. fclose($fd);
  2533. }
  2534. else
  2535. {
  2536. $sqlstr = "UPDATE ".$CONF['db']['table']." SET hits=$rc_num";
  2537. $sqlstr .= ",id_string=".$this->sql_value($current_id_string);
  2538. $sqlstr .= "WHERE domain=".$this->sql_value($server_domain);
  2539. $sqlstr .= " AND counter_name=".$this->sql_value($id);
  2540. $dbi = $this->db->query($sqlstr);
  2541. if (DB::isError($dbi))
  2542. {
  2543. $this->show_error("hitcounter", $dbi->getMessage());
  2544. }
  2545. }
  2546. }
  2547. else
  2548. {
  2549. $rc_num = $last_num;
  2550. }
  2551. }
  2552. else // start with 1
  2553. {
  2554. $rc_num = 1;
  2555. if ($CONF['storage_type']=="files")
  2556. {
  2557. touch($file_count);
  2558. touch($file_id);
  2559. chmod($file_count,0664);
  2560. chmod($file_count,0664);
  2561. $fd = fopen($file_count,"w+");
  2562. fwrite($fd,$rc_num);
  2563. fclose($fd);
  2564. $fd = fopen($file_id,"w+");
  2565. fwrite($fd,$current_id_string);
  2566. fclose($fd);
  2567. }
  2568. else
  2569. {
  2570. $sqlstr = "INSERT INTO ".$CONF['db']['table']."(domain,counter_name,hits,id_string) ";
  2571. $sqlstr .= "VALUES (".$this->sql_value($server_domain).",".$this->sql_value($id).",";
  2572. $sqlstr .= $rc_num.",".$this->sql_value($current_id_string).")";
  2573. $dbi = $this->db->query($sqlstr);
  2574. if (DB::isError($dbi))
  2575. {
  2576. $this->show_error("hitcounter", $dbi->getMessage());
  2577. }
  2578. }
  2579. }
  2580. return $rc_num;
  2581. }
  2582. /**
  2583. * Returns type of browser
  2584. *
  2585. * @param bool $details true = return array with browser type and
  2586. * gecko engine version (only available for Mozilla based browsers)
  2587. * browser_type = browser type
  2588. * gecko_version = gecko version integer {default: 0}
  2589. * false = return letter for browser type {default: false}
  2590. * @return char array if details is true, or char:
  2591. * m = Mozilla (and all clones)
  2592. * i = Internet Explorer
  2593. * n = Netscape
  2594. * o = Opera
  2595. * k = Konqueror
  2596. * "" = unknown
  2597. */
  2598. function browser_type($details=false)
  2599. {
  2600. $rc = "";
  2601. $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
  2602. if (substr_count($agent,"opera")>0) $rc = "o";
  2603. else if (substr_count($agent,"konqueror")>0) $rc = "k";
  2604. else if (substr_count($agent,"gecko")>0) $rc = "m";
  2605. else if (substr_count($agent,"msie")>0) $rc = "i";
  2606. else if (substr_count($agent,"mozilla")>0) $rc = "n";
  2607. if ($details)
  2608. {
  2609. $rc_array = array();
  2610. $rc_array['browser_type'] = $rc;
  2611. $needle = "gecko/";
  2612. if (($rc=="m") && (substr_count($agent,$needle)>0))
  2613. {
  2614. $gecko = trim(substr($agent,(strpos($agent, $needle))+6));
  2615. $ln = strlen($gecko);
  2616. if ($ln>0)
  2617. {
  2618. $tmp_gecko = "";
  2619. for($i=0;$i<$ln;$i++)
  2620. {
  2621. $chr = $gecko[$i];
  2622. if (is_numeric($chr)) $tmp_gecko .= $chr;
  2623. else break;
  2624. }
  2625. $gecko = $tmp_gecko;
  2626. }
  2627. $rc_array['gecko_version'] = intval($gecko);
  2628. }
  2629. else
  2630. {
  2631. $rc_array['gecko_version'] = 0;
  2632. }
  2633. return $rc_array;
  2634. }
  2635. else
  2636. {
  2637. return $rc;
  2638. }
  2639. }
  2640. /**
  2641. * returns type of operating system
  2642. *
  2643. * @param bool $mode true for server os,
  2644. * false for client os {default: false}
  2645. * @return char w = Windows
  2646. * m = Mac
  2647. * l = Linux
  2648. * u = Unix
  2649. * o = OS/2
  2650. * "" = unknown
  2651. */
  2652. function os_type($mode=false)
  2653. {
  2654. $rc="";
  2655. if ($mode)
  2656. {
  2657. $agent = strtolower(PHP_OS);
  2658. if ((substr_count($agent,"win32")>0) ||
  2659. (substr_count($agent,"winnt")>0) ||
  2660. (substr_count($agent,"windows")>0)) $rc = "w";
  2661. else if (substr_count($agent,"macos")>0) $rc = "m";
  2662. else if (substr_count($agent,"linux")>0) $rc = "l";
  2663. else if (substr_count($agent,"unix")>0) $rc = "u";
  2664. else if (substr_count($agent,"os/2")>0) $rc = "o";
  2665. if (isvoid($rc))
  2666. {
  2667. if (substr_count($_SERVER['WINDIR'],":\\")>0)
  2668. {
  2669. $rc = "w";
  2670. }
  2671. }
  2672. }
  2673. else
  2674. {
  2675. $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
  2676. if (substr_count($agent,"win")>0) $rc = "w";
  2677. else if (substr_count($agent,"mac")>0) $rc = "m";
  2678. else if (substr_count($agent,"linux")>0) $rc = "l";
  2679. else if (substr_count($agent,"unix")>0) $rc = "u";
  2680. else if (substr_count($agent,"os/2")>0) $rc = "o";
  2681. }
  2682. return $rc;
  2683. }
  2684. /**
  2685. * calculates CSS font sizes. This is a small solution, that the appearance looks similar
  2686. * with different browser types and operating systems
  2687. *
  2688. * @param char $os_type type of operating system {default: 'user agent os'}
  2689. * @param char $browser_type type of browser {default: 'user agent os'}
  2690. * @return array array with css font sizes
  2691. */
  2692. function get_fontsizes($os_type="",$browser_type="")
  2693. {
  2694. if (isvoid($os_type)) $os_type = $this->os_type();
  2695. if (isvoid($browser_type)) $browser_type = $this->browser_type();
  2696. $fdata = array();
  2697. // font_size_smallest
  2698. if (($os_type == "w") && ($browser_type=="o"))
  2699. $fdata['font_size_smallest'] = "8px";
  2700. else if (($os_type == "m") && ($browser_type!="i") && ($browser_type!="m"))
  2701. $fdata['font_size_smallest'] = "x-small";
  2702. else
  2703. $fdata['font_size_smallest'] = "xx-small";
  2704. // font_size_small
  2705. if (($os_type == "w") && ($browser_type=="i"))
  2706. $fdata['font_size_small'] = "75%";
  2707. else if (($os_type == "w") && ($browser_type=="o"))
  2708. $fdata['font_size_small'] = "xx-small";
  2709. else if (($os_type == "m") && ($browser_type!="i")&& ($browser_type!="m"))
  2710. $fdata['font_size_small'] = "small";
  2711. else if (($os_type != "w") && ($browser_type=="o"))
  2712. $fdata['font_size_small'] = "95%";
  2713. else if ($browser_type=="k")
  2714. $fdata['font_size_small'] = "xx-small";
  2715. else
  2716. $fdata['font_size_small'] = "x-small";
  2717. // font_size
  2718. if (($os_type == "m") && ($browser_type!="i") && ($browser_type!="m"))
  2719. $fdata['font_size'] = "medium";
  2720. else if ((($os_type == "w") && ($browser_type=="i")) || ($browser_type=="o"))
  2721. $fdata['font_size'] = "x-small";
  2722. else if ($browser_type=="k")
  2723. $fdata['font_size'] = "x-small";
  2724. else
  2725. $fdata['font_size'] = "small";
  2726. // font_size_big
  2727. if ((($os_type == "w") && ($browser_type=="i")) || ($browser_type=="o"))
  2728. $fdata['font_size_big'] = "medium";
  2729. else
  2730. $fdata['font_size_big'] = "large";
  2731. // font_size_biggest
  2732. if ((($os_type == "w") && ($browser_type=="i")) || ($browser_type=="o"))
  2733. $fdata['font_size_biggest'] = "large";
  2734. else
  2735. $fdata['font_size_biggest'] = "x-large";
  2736. return $fdata;
  2737. }
  2738. /**
  2739. * checks if specified url is on script server
  2740. * (on the host where your scripts are)
  2741. *
  2742. * @param string $url
  2743. * @return bool true if it is on host
  2744. */
  2745. function url_on_scripthost($url)
  2746. {
  2747. $rc = false;
  2748. $tmp_array = parse_url($url);
  2749. if ((!isset($tmp_array['host'])) || ($tmp_array['host'] == $_SERVER['HTTP_HOST']))
  2750. $rc = true;
  2751. return $rc;
  2752. }
  2753. /**
  2754. * Return cross sum of a number
  2755. *
  2756. * @param int $number
  2757. * @return int
  2758. */
  2759. function cross_sum($number)
  2760. {
  2761. $rc = 0;
  2762. $i_max = strlen($number);
  2763. for ($i=0;$i<$i_max;$i++)
  2764. $rc = $rc+$number[$i];
  2765. return $rc;
  2766. }
  2767. /**
  2768. * checks for protocol, if missing http will be used
  2769. *
  2770. * @param string $url
  2771. * @return string
  2772. */
  2773. function convUrl($url)
  2774. {
  2775. if (substr_count($url,"://")>0)
  2776. $rc = $url;
  2777. else
  2778. $rc = "http://".$url;
  2779. if (substr_count($rc,".")<2)
  2780. {
  2781. $pos = strpos($rc, ":");
  2782. $rc = substr_replace($rc,"www.",$pos+3,0);
  2783. }
  2784. return $rc;
  2785. }
  2786. /**
  2787. * counts days till specified day
  2788. *
  2789. * @param date $enddate
  2790. * @return int days till specified date
  2791. */
  2792. function countdown($enddate)
  2793. {
  2794. $ln = strlen($enddate);
  2795. $ix = 0;
  2796. $jahr=$monat=$tag="";
  2797. while ($ix < $ln)
  2798. {
  2799. $ch = $enddate[$ix];
  2800. $ix++;
  2801. if ($ix<= 4)
  2802. {
  2803. $jahr=$jahr . $ch;
  2804. }
  2805. else if (($ix> 5) && ($ix<8))
  2806. {
  2807. $monat=$monat . $ch;
  2808. }
  2809. else if ($ix> 8)
  2810. {
  2811. $tag=$tag . $ch;
  2812. }
  2813. }
  2814. $date1 = mktime(0, 0, 0, $monat,$tag,$jahr);
  2815. $date2 = strtotime ("now");
  2816. if ($date1 > $date2)
  2817. {
  2818. $rc = ($date1 - $date2) / 86400; // 24 * 60 *60
  2819. }
  2820. else if ($date1 < $date2)
  2821. {
  2822. $rc = ($date2 - $date1) / 86400; // 24 * 60 *60
  2823. }
  2824. else
  2825. {
  2826. $rc = 0;
  2827. }
  2828. return ceil($rc);
  2829. }
  2830. /**
  2831. * computes the exact age (considering leap years!) of the person born on
  2832. * the specified birthday measured from today or a specified date
  2833. *
  2834. * @param string $date Date on which the person was born
  2835. * @param string $thisdate Date from which the age is measured (default: today)
  2836. * @param char $date_format Format of the date and thisdate strings (default: "S"cience format)
  2837. * @param bool $fixed if true, age of current year will be returned {default: false}
  2838. * @return int age of person, returns "" if the person is not born yet or the date is wrong!
  2839. * (returns "born"-string, if the age is exactly 0)
  2840. */
  2841. function age_from_birthday($date, $thisdate="", $date_format="S", $fixed=false)
  2842. {
  2843. $date = $this->convDate($date, $date_format,"S");
  2844. $year = intval(strtok($date,"-"));
  2845. $month = intval(strtok("-"));
  2846. $day = intval(strtok("-"));
  2847. if ($thisdate=="")
  2848. {
  2849. $thisyear = date("Y");
  2850. $thismonth = date("n");
  2851. $thisday = date("j");
  2852. }
  2853. else
  2854. {
  2855. $thisdate = $this->convDate($thisdate, $date_format, "S");
  2856. $thisyear = intval(strtok($thisdate,"-"));
  2857. $thismonth = intval(strtok("-"));
  2858. $thisday = intval(strtok("-"));
  2859. }
  2860. $age = $thisyear-$year;
  2861. if (!$fixed)
  2862. {
  2863. if (($thismonth<$month) ||
  2864. (($thismonth==$month)&&($thisday<$day)))
  2865. $age = $age-1;
  2866. }
  2867. if (($age<0)||(!checkdate($month,$day,$year)))
  2868. $age = "";
  2869. return $age;
  2870. }
  2871. /**
  2872. * generate rand string/number with specified length
  2873. *
  2874. * @param int $length length of chars/numbers for return
  2875. * @param string $values string with all allowed characters/numbers
  2876. * @return string generated rand string, on error ""
  2877. */
  2878. function rand_value($length=1, $values="")
  2879. {
  2880. $rc = "";
  2881. if (isvoid($values))
  2882. {
  2883. $values = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  2884. }
  2885. for ($i=0;$i<intval($length);$i++)
  2886. {
  2887. srand((double) microtime() * 1000000);
  2888. $rc .= $values[mt_rand(0,strlen($values)-1)];
  2889. }
  2890. return $rc;
  2891. }
  2892. /**
  2893. * generate password with specified characteristics
  2894. *
  2895. * @param array $options options array:
  2896. * min_length = min. length of password {default: 8}
  2897. * max_length = max. length of password {default: 8}
  2898. * exclude_signs = string with forbiddenen signs
  2899. * include_signs = string with signs, which have to been used
  2900. * exclude_pwd = this password is forbidden
  2901. * with_numbers = password has to use at least one number {true}
  2902. * with_lowers = password has to use at least one lower letter {true}
  2903. * with_uppers = password has to use at least one upper letter {true}
  2904. * with_signs = password has to use at least one special char {true}
  2905. * @return string generated password string
  2906. * (if an error occurred, false)
  2907. */
  2908. function generate_password($options=null)
  2909. {
  2910. // defaults
  2911. $min_length = 8;
  2912. $max_length = 8;
  2913. $with_numbers = true;
  2914. $with_lowers = true;
  2915. $with_uppers = true;
  2916. $with_signs = true;
  2917. if (is_array($options))
  2918. {
  2919. if (array_key_exists("min_length", $options))
  2920. {
  2921. if (intval($options['min_length'])<3)
  2922. return false;
  2923. else
  2924. $min_length = $options['min_length'];
  2925. }
  2926. if (array_key_exists("max_length", $options))
  2927. {
  2928. if (intval($options['max_length'])<$min_length)
  2929. return false;
  2930. else
  2931. $max_length = $options['max_length'];
  2932. }
  2933. else if ($min_length>$max_length)
  2934. $max_length = $min_length;
  2935. if ((array_key_exists("with_numbers", $options)) && (!$options['with_numbers']))
  2936. $with_numbers = false;
  2937. if ((array_key_exists("with_lowers", $options)) && (!$options['with_lowers']))
  2938. $with_lowers = false;
  2939. if ((array_key_exists("with_uppers", $options)) && (!$options['with_uppers']))
  2940. $with_uppers = false;
  2941. if ((array_key_exists("with_signs", $options)) && (!$options['with_signs']))
  2942. $with_signs = false;
  2943. if ((!$with_numbers) && (!$with_lowers) && (!$with_uppers) && (!$with_signs))
  2944. return false;
  2945. else if (($with_numbers) && ($with_lowers) && ($with_uppers) && ($with_signs) && ($min_length<4))
  2946. return false;
  2947. }
  2948. $numbers = range(0, 9);
  2949. $lowers = range('a', 'z');
  2950. $uppers = range('A', 'Z');
  2951. $signs = str_split("!@#%&*-_=.,;:^()[]{}/\\");
  2952. $haystack = array();
  2953. if ($with_numbers)
  2954. $haystack = $numbers;
  2955. if ($with_lowers)
  2956. $haystack = array_merge($haystack, $lowers);
  2957. if ($with_uppers)
  2958. $haystack = array_merge($haystack, $uppers);
  2959. if ($with_signs)
  2960. $haystack = array_merge($haystack, $signs);
  2961. // exclude forbitten signs
  2962. $ex_sum = 0;
  2963. if (!isvoid($options['exclude_signs']))
  2964. {
  2965. $ex = str_split($options['exclude_signs']);
  2966. $ex = array_unique($ex);
  2967. $ex_sum = count($ex);
  2968. if ($ex_sum>0)
  2969. {
  2970. // looking for a better solution, kind of array_unmerge
  2971. for ($i=0; $i<$ex_sum; $i++)
  2972. {
  2973. reset($haystack);
  2974. while(list($key, $value)=each($haystack))
  2975. {
  2976. $tmp_ex = $ex[$i];
  2977. // need "", type conversion?
  2978. if ("$value"==$tmp_ex)
  2979. {
  2980. unset($haystack[$key]);
  2981. break;
  2982. }
  2983. }
  2984. }
  2985. }
  2986. }
  2987. // build include array
  2988. $in_sum = 0;
  2989. if (!isvoid($options['include_signs']))
  2990. {
  2991. $in = str_split($options['include_signs']);
  2992. $in = array_unique($in);
  2993. $in_sum = count($in);
  2994. if ($in_sum>$min_length)
  2995. return false;
  2996. else if ($ex_sum>0)
  2997. {
  2998. // check exclude list for include signs
  2999. $needle = str_replace("/", "\\/", $options['include_signs']);
  3000. $needle = str_replace("(", "\\(", $needle);
  3001. $needle = str_replace(")", "\\)", $needle);
  3002. $needle = str_replace("-", "\\-", $needle);
  3003. $needle = str_replace("-", "\\-", $needle);
  3004. $needle = str_replace("\\", "\\\\", $needle);
  3005. $needle = str_replace(";", "\\;", $needle);
  3006. $needle = str_replace(".", "\\.", $needle);
  3007. $needle = str_replace(",", "\\,", $needle);
  3008. $needle = str_replace("[", "\\,", $needle);
  3009. $needle = str_replace("]", "\\,", $needle);
  3010. $needle = str_replace("{", "\\{", $needle);
  3011. $needle = str_replace("}", "\\}", $needle);
  3012. if (preg_match('/['.$needle.']/', $options['exclude_signs']))
  3013. return false;
  3014. }
  3015. }
  3016. $max_cnt = 1000; // if 1000 tries find no password, return false!
  3017. $cnt = 0;
  3018. $rc = false;
  3019. while(1)
  3020. {
  3021. $cnt++;
  3022. if ($cnt>$max_cnt) break;
  3023. // length of password
  3024. if ($min_length==$max_length)
  3025. {
  3026. $pwd_length = $min_length;
  3027. }
  3028. else
  3029. {
  3030. $range = range($min_length, $max_length);
  3031. $pwd_length = $range[array_rand($range, 1)];
  3032. }
  3033. $tmp_passwd = "";
  3034. for ($i=0; $i<$pwd_length; $i++)
  3035. {
  3036. $key = array_rand($haystack, 1);
  3037. $tmp_passwd .= $haystack[$key];
  3038. }
  3039. // include signs
  3040. if ($in_sum>0)
  3041. {
  3042. $in_range = range(0, $pwd_length-1);
  3043. for ($i=0; $i<$in_sum; $i++)
  3044. {
  3045. $in_pos = array_rand($in_range, 1);
  3046. $tmp_passwd[$in_pos] = $in[$i];
  3047. // remove from range
  3048. unset($in_range[$in_pos]);
  3049. }
  3050. }
  3051. // count how many lowercase, uppercase, and digits are in the password
  3052. $uc = 0; $lc = 0; $num = 0; $sc = 0;
  3053. for ($i = 0, $j = $pwd_length; $i < $j; $i++)
  3054. {
  3055. $c = substr($tmp_passwd, $i, 1);
  3056. if (preg_match('/^[[:upper:]]$/',$c))
  3057. $uc++;
  3058. else if (preg_match('/^[[:lower:]]$/',$c))
  3059. $lc++;
  3060. else if (preg_match('/^[[:digit:]]$/',$c))
  3061. $num++;
  3062. else
  3063. $sc++;
  3064. }
  3065. // currenty not supported with include chars
  3066. if ($in_sum==0)
  3067. {
  3068. if (($with_lowers) && ($lc==0))
  3069. continue;
  3070. else if (($with_uppers) && ($uc==0))
  3071. continue;
  3072. else if (($with_numbers) && ($uc==0))
  3073. continue;
  3074. else if (($with_signs) && ($sc==0))
  3075. continue;
  3076. }
  3077. if ((!isvoid($options['exclude_pwd'])) && ($options['exclude_pwd']==$tmp_passwd))
  3078. continue;
  3079. $rc = $tmp_passwd;
  3080. break;
  3081. }
  3082. return $rc;
  3083. }
  3084. /**
  3085. * create unique identification string
  3086. *
  3087. * @param bool $with_time true : with timestamp {default}
  3088. * false: without considering time and date
  3089. * @return string unique identification string
  3090. */
  3091. function get_id_string($with_time=true)
  3092. {
  3093. $id_string = $_SERVER['REMOTE_ADDR'].$_SERVER['HTTP_USER_AGENT'];
  3094. if ($with_time)
  3095. {
  3096. mt_srand( (double) microtime() * 1000000);
  3097. $tmp_time = md5(mt_rand());
  3098. return md5($id_string.$tmp_time);
  3099. }
  3100. else
  3101. return md5($id_string);
  3102. }
  3103. /**
  3104. * get the ip address from the client, even if the client is befind a proxy
  3105. * (it doesn't work with transparent proxy!)
  3106. *
  3107. * @return string ip address of the client
  3108. */
  3109. function get_client_ip()
  3110. {
  3111. global $REMOTE_ADDR;
  3112. $client_ip = ( !empty($_SERVER['REMOTE_ADDR']) ) ? $_SERVER['REMOTE_ADDR'] : ( ( !empty($_ENV['REMOTE_ADDR']) ) ? $_ENV['REMOTE_ADDR'] : $REMOTE_ADDR );
  3113. // Check for headers used by proxy servers to send the Client IP.
  3114. // We should look for HTTP_CLIENT_IP before HTTP_X_FORWARDED_FOR.
  3115. if ($_SERVER['HTTP_CLIENT_IP']) $proxy_ip = $_SERVER['HTTP_CLIENT_IP'];
  3116. else if ($_SERVER['HTTP_X_FORWARDED_FOR']) $proxy_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  3117. // Proxy is used, see if the specified Client IP is valid.
  3118. // Sometimes it's 10.x.x.x or 127.x.x.x... Just making sure.
  3119. if ($proxy_ip)
  3120. {
  3121. $ip_list = array();
  3122. if ( preg_match("/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/", $proxy_ip, $ip_list) )
  3123. {
  3124. $private_ip = array('/^0\./', '/^127\.0\.0\.1/', '/^192\.168\..*/', '/^172\.16\..*/', '/^10.\.*/', '/^224.\.*/', '/^240.\.*/');
  3125. $client_ip = preg_replace($private_ip, $client_ip, $ip_list[1]);
  3126. }
  3127. }
  3128. return $client_ip;
  3129. }
  3130. /**
  3131. * Verify EAN
  3132. *
  3133. * @param string $ean
  3134. * @param string $type type of EAN: {default: EAN13}
  3135. * EAN8,EAN13,EAN14,UPC12,ILN,NVE18
  3136. * @return bool
  3137. */
  3138. function checkEAN($ean,$type=null)
  3139. {
  3140. $rc = false;
  3141. if ($this->checkNumber($ean,"",false)) // only positive, integer numbers
  3142. {
  3143. $len = strlen($ean);
  3144. $factor = 3;
  3145. $sum = 0;
  3146. $run_mode = false;
  3147. if (!empty($type)) $type = strtoupper($type);
  3148. if (($type=="EAN8") && ($len==8)) $run_mode = true;
  3149. else if ((($type=="EAN13") || ($type=="ILN")) && ($len==13)) $run_mode = true;
  3150. else if (($type=="EAN14") && ($len==14)) $run_mode = true;
  3151. else if (($type=="UPC12") && ($len==12)) $run_mode = true;
  3152. else if (($type=="NVE18") && ($len==18)) $run_mode = true;
  3153. if ($run_mode)
  3154. {
  3155. $rest = substr($ean,-1);
  3156. $wert = substr($ean,0,$len-1);
  3157. for ($i=($len-1);$i>0;--$i)
  3158. {
  3159. $sum = $sum + substr($wert,$i-1,1) * $factor;
  3160. $factor = 4 - $factor;
  3161. }
  3162. if ($rest==((1000-$sum)%10)) $rc = true;
  3163. }
  3164. }
  3165. return $rc;
  3166. }
  3167. /**
  3168. * Return size of image file
  3169. *
  3170. * @param string $filename
  3171. * @return int
  3172. */
  3173. function get_image_size($filename)
  3174. {
  3175. $rc = "";
  3176. if (file_exists($filename))
  3177. {
  3178. $img_info = getimagesize($filename);
  3179. $rc = " ".$img_info[3];
  3180. }
  3181. return $rc;
  3182. }
  3183. /**
  3184. * shorten text to given length for display
  3185. *
  3186. * @param string $text text string
  3187. * @param int $max_length maximum lenght of text
  3188. * @param string $dots hars for dots {default: ...}
  3189. * @return string
  3190. */
  3191. function text_shorten($text, $max_length, $dots="...")
  3192. {
  3193. $text_l = strlen($text);
  3194. $dots_l = strlen($dots);
  3195. if (($text_l>$dots_l) && ($text_l>$max_length))
  3196. {
  3197. $rc = substr($text, 0, $max_length).$dots;
  3198. }
  3199. else
  3200. {
  3201. $rc = $text;
  3202. }
  3203. return $rc;
  3204. }
  3205. /**
  3206. * convert rgb to decimal color code
  3207. *
  3208. * @param string $rgbstr rgb string
  3209. * @return array decimal color code
  3210. */
  3211. function rgb2dec($rgbstr)
  3212. {
  3213. $c = array();
  3214. ereg("([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})", strtoupper($rgbstr), $c);
  3215. if ($c[0])
  3216. {
  3217. $arr['r']= hexdec($c[1]);
  3218. $arr['g']= hexdec($c[2]);
  3219. $arr['b']= hexdec($c[3]);
  3220. }
  3221. return $arr;
  3222. }
  3223. /**
  3224. * check, if client IP or hostname is in access list
  3225. *
  3226. * @param array $hosts array with hostnames, which have access
  3227. * @param array $ips array with ips, which have access
  3228. * @param bool $mode client, client ips (REMOTE_ADDR) {default}
  3229. * server, client ips (SERVER_ADDR)
  3230. * referrer (HTTP_REFERER)
  3231. * @return bool true : access
  3232. * false : no access
  3233. */
  3234. function net_access($hosts=null, $ips=null, $mode="client")
  3235. {
  3236. $rc = false;
  3237. if ($mode=="server")
  3238. {
  3239. $test_ip = $_SERVER['SERVER_ADDR'];
  3240. $test_host = $_SERVER['SERVER_NAME'];
  3241. }
  3242. else if ($mode=="referer")
  3243. {
  3244. $referer = $_SERVER['HTTP_REFERER'];
  3245. if (empty($referer))
  3246. {
  3247. return false;
  3248. }
  3249. else
  3250. {
  3251. $uinfo = parse_url($referer);
  3252. $test_host = $uinfo['host'];
  3253. $test_ip = gethostbyname($test_host);
  3254. }
  3255. }
  3256. else // client
  3257. {
  3258. $test_ip = $_SERVER['REMOTE_ADDR'];
  3259. $test_host = gethostbyaddr($test_ip);
  3260. }
  3261. if ((is_array($ips)) && (!empty($test_ip)))
  3262. {
  3263. foreach ($ips as $tmp_ip)
  3264. {
  3265. if (!$tmp_ip) continue;
  3266. $regexp = preg_quote($tmp_ip);
  3267. $regexp = "/$regexp/i";
  3268. if ( preg_match($regexp, $test_ip) == true )
  3269. $rc = true;
  3270. }
  3271. }
  3272. // if no valid ip was found, check host names
  3273. if ((!$rc) && (is_array($hosts)) && (!empty($test_host)))
  3274. {
  3275. foreach ($hosts as $tmp_host)
  3276. {
  3277. if (!$tmp_host) continue;
  3278. $regexp = preg_quote($tmp_host);
  3279. $regexp = "/$regexp/i";
  3280. if ( preg_match($regexp, $test_host) == true )
  3281. $rc = true;
  3282. }
  3283. }
  3284. return $rc;
  3285. }
  3286. /**
  3287. * pager (navigation) for list
  3288. *
  3289. * @param string $url link for urls
  3290. * @param int $current_entry current page number for navigation
  3291. * @param int $max_entries highest page number in navigation
  3292. * @param int $page_entries entries per page {default: $this->page_entries}
  3293. * @param int $image_url overwrite global image url
  3294. * @return string html string for paper
  3295. */
  3296. function pager($url, $current_entry=null, $max_entries=null, $page_entries=null, $image_url=null)
  3297. {
  3298. if (!isset($image_url)) $image_url = $this->image_url;
  3299. $rc = "";
  3300. if (empty($page_entries)) $page_entries = $this->page_entries;
  3301. if (empty($page_entries))
  3302. {
  3303. $this->show_error("pager", "page_entries variable not defined!", $page_entries);
  3304. }
  3305. $current_page = intval($current_entry);
  3306. $max_pages = intval($max_entries);
  3307. if ($current_page==0) $current_page = 1; // 0 is not valid!
  3308. if ($max_entries % $page_entries==0) // no rest
  3309. $last_page = (($max_entries/$page_entries)-1)*$page_entries+1;
  3310. else
  3311. $last_page = floor($max_entries/$page_entries)*$page_entries+1;
  3312. if ($current_page==-1) $current_page = $current_entry = $last_page;
  3313. if ((empty($max_entries)) || ($max_entries<$current_entry)) $max_entries = $current_entry;
  3314. $current_page = floor($current_entry/$page_entries)+1;
  3315. $max_pages = ceil($max_entries/$page_entries);
  3316. if ($max_pages>1)
  3317. {
  3318. // Zeige mindestens 5 und wachse bis auf 10
  3319. if ($current_page==1) $show_max = 5;
  3320. else if ($current_page==2) $show_max = 6;
  3321. else if ($current_page==3) $show_max = 7;
  3322. else if ($current_page==4) $show_max = 8;
  3323. else if ($current_page==5) $show_max = 9;
  3324. else $show_max = 10;
  3325. $prev_page = $current_page-1;
  3326. $next_page = $current_page+1;
  3327. $prev_entry = ($prev_page*$page_entries)-$page_entries+1;
  3328. $next_entry = ($next_page*$page_entries)-$page_entries+1;
  3329. if ($max_pages<$show_max) $show_max = $max_pages;
  3330. if ($current_page>1)
  3331. {
  3332. $rc .= $this->url($url."&amp;spage=1","<img src=\"".$image_url."/arrow_small_prev.gif\" alt=\"".STR_PAGE_FIRST."\" style=\"border: none; vertical-align: bottom;\"".$this->end_tag(), 1, "","onmouseover=\"window.status='".STR_PAGE_FIRST."';return true\" onmouseout=\"window.status=''\"")."&nbsp;";
  3333. $rc .= $this->url($url."&amp;spage=$prev_entry","<img src=\"".$image_url."/arrow_small_prev2.gif\" alt=\"".STR_PAGE_PREV."\" style=\"border: none; vertical-align: bottom;\"".$this->end_tag(), 1, "","onmouseover=\"window.status='".STR_PAGE_PREV."';return true\" onmouseout=\"window.status=''\"")."&nbsp;";
  3334. }
  3335. $rc .= "[&nbsp;&nbsp;";
  3336. if ($current_page>6)
  3337. {
  3338. if ($current_page==$max_pages) $seite=$current_page-9;
  3339. else if ($current_page==($max_pages-1)) $seite=$current_page-8;
  3340. else if ($current_page==($max_pages-2)) $seite=$current_page-7;
  3341. else if ($current_page==($max_pages-3)) $seite=$current_page-6;
  3342. else if ($current_page==($max_pages-4)) $seite=$current_page-5;
  3343. else $seite=$current_page-4;
  3344. $show_max_real=$seite+9;
  3345. if ($show_max_real>$max_pages) $show_max_real = $max_pages; // error correction
  3346. if ($seite<1) $seite=1; // error correction
  3347. }
  3348. else
  3349. {
  3350. $seite=1;
  3351. $show_max_real=$show_max;
  3352. }
  3353. for($seite;($seite<=$show_max_real);$seite++)
  3354. {
  3355. $site_no = ($seite*$page_entries)-$page_entries+1;
  3356. $tmp_url = $url."&amp;spage=$site_no";
  3357. if ($seite==$current_page) $rc .= $seite;
  3358. else $rc .= $this->url($tmp_url,$seite,1,"","onmouseover=\"window.status='".STR_PAGE." $seite';return true\" onmouseout=\"window.status=''\" style=\"color: black;\"");
  3359. $rc .= "&nbsp;&nbsp;";
  3360. }
  3361. $rc .= "]";
  3362. if ($current_page!=$max_pages)
  3363. {
  3364. $rc .= "&nbsp;".$this->url($url."&amp;spage=$next_entry","<img src=\"".$image_url."/arrow_small_next2.gif\" alt=\"".STR_PAGE_NEXT."\" style=\"border: none; vertical-align: bottom;\"".$this->end_tag(), 1, "","onmouseover=\"window.status='".STR_PAGE_NEXT."';return true\" onmouseout=\"window.status=''\"");
  3365. $rc .= "&nbsp;".$this->url($url."&amp;spage=$last_page","<img src=\"".$image_url."/arrow_small_next.gif\" alt=\"".STR_PAGE_LAST."\" style=\"border: none; vertical-align: bottom;\"".$this->end_tag(), 1, "","onmouseover=\"window.status='".STR_PAGE_LAST."';return true\" onmouseout=\"window.status=''\"");
  3366. }
  3367. $rc .= "<br".$this->end_tag()."\n";
  3368. }
  3369. return $rc;
  3370. }
  3371. /**
  3372. * Setup HTML header
  3373. *
  3374. * @param array $customize charset set charset
  3375. * no_cache if true, page will not be cachable
  3376. * gzip if true, gzip compression will be used
  3377. */
  3378. function html_header($customize=null)
  3379. {
  3380. // default values
  3381. $charset = STR_CHARACTER_SET;
  3382. $no_cache = true;
  3383. $gzip = true;
  3384. if (isset($customize['charset'])) $charset = $customize['charset'];
  3385. if (isset($customize['no_cache'])) $no_cache = $customize['no_cache'];
  3386. if (isset($customize['gzip'])) $gzip = $customize['gzip'];
  3387. /////////////////////////////////////////////////////////////////////////////
  3388. if ($gzip) ob_start('ob_gzhandler');
  3389. if (!$no_cache)
  3390. {
  3391. $now = gmdate('D, d M Y H:i:s').' GMT';
  3392. header('Expires: '.$now); // rfc2616 - Section 14.21
  3393. header('Last-Modified: '.$now);
  3394. header('Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
  3395. header('Pragma: no-cache'); // HTTP/1.0
  3396. }
  3397. if (!isvoid($charset)) header('Content-Type: text/html; charset="'.$charset.'"');
  3398. }
  3399. /**
  3400. * Write HTML head
  3401. *
  3402. * @param array $customize title
  3403. * charset
  3404. * no_cache
  3405. * lang
  3406. * seo
  3407. * with_header
  3408. * print
  3409. * @return string
  3410. */
  3411. function html_head($customize)
  3412. {
  3413. // default values
  3414. $title = "no title";
  3415. $charset = STR_CHARACTER_SET;
  3416. $no_cache = true;
  3417. $lang = "de";
  3418. $seo = true;
  3419. $with_header = true;
  3420. $print = true;
  3421. if ($this->xhtml) $doc_mode = "xhtml";
  3422. else $doc_mode = "html";
  3423. if ($doc_mode=="frame")
  3424. {
  3425. $end_sign = "";
  3426. $doctype = "<!DOCTYPE html\n\tPUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n\t\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
  3427. }
  3428. else if ($doc_mode=="html")
  3429. {
  3430. $end_sign = "";
  3431. $doctype = "<!DOCTYPE html\n\tPUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n\t\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
  3432. }
  3433. else
  3434. {
  3435. $end_sign = " /";
  3436. $doc_mode = "xhtml";
  3437. $doctype = "<!DOCTYPE html\n\tPUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n\t\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
  3438. }
  3439. if (isset($customize['title'])) $title = $customize['title'];
  3440. if (isset($customize['charset'])) $charset = $customize['charset'];
  3441. if (isset($customize['no_cache'])) $no_cache = $customize['no_cache'];
  3442. if (isset($customize['lang'])) $lang = $customize['lang'];
  3443. if (isset($customize['seo'])) $seo = $customize['seo'];
  3444. if (isset($customize['print'])) $print = $customize['print'];
  3445. if (isset($customize['with_header'])) $with_header = $customize['with_header'];
  3446. /////////////////////////////////////////////////////////////////////////////
  3447. if ($with_header) $this->html_header($customize);
  3448. $rc = "";
  3449. if ($doc_mode=="xhtml")
  3450. $rc .= "<?xml version=\"1.0\" encoding=\"$charset\"?>\n";
  3451. $rc .= $doctype;
  3452. if ($doc_mode=="xhtml")
  3453. $rc .= "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"$lang\" lang=\"$lang\">\n";
  3454. $rc .= "<head>\n";
  3455. $rc .= "\t<title>$title</title>\n";
  3456. $rc .= "\t<meta name=\"content-language\" content=\"".$lang."\"".$end_sign.">\n";
  3457. if ($seo)
  3458. $rc .= "\t<meta name=\"robots\" content=\"index,follow\"".$end_sign.">\n";
  3459. else
  3460. $rc .= "\t<meta name=\"robots\" content=\"noindex,nofollow\"".$end_sign.">\n";
  3461. if ($no_cache)
  3462. $rc .= "\t<meta http-equiv=\"expires\" content=\"0\"".$end_sign.">\n";
  3463. if (isset($customize['addon'])) $rc .= "\n".$customize['addon'];
  3464. //$rc .= "</head>\n";
  3465. if ($print) print $rc;
  3466. return $rc;
  3467. }
  3468. /**
  3469. * Calculate active URL of an array for validation
  3470. * (can be used for multi URL sites (e.g. dyndns)
  3471. *
  3472. * @param mixed $urls an url or an array of urls
  3473. * @return string validated base url
  3474. */
  3475. function get_base_url($urls)
  3476. {
  3477. if (is_array($urls))
  3478. {
  3479. $rc = false;
  3480. if ($_SERVER['SERVER_PORT']=="443") $tmp_prot = "https";
  3481. else $tmp_prot = "http";
  3482. $tmp_url = $tmp_prot."://".$_SERVER['HTTP_HOST'];
  3483. while (list(, $url) = each($urls))
  3484. {
  3485. if (substr_count($url, $tmp_url)>0)
  3486. {
  3487. $rc = $url;
  3488. break;
  3489. }
  3490. }
  3491. if ($rc===false)
  3492. {
  3493. die(STR_ACCESS_DENIED);
  3494. }
  3495. }
  3496. else
  3497. {
  3498. $rc = $urls;
  3499. }
  3500. return $rc;
  3501. }
  3502. /**
  3503. * Convert string to javascript compatible output
  3504. *
  3505. * @param string $string
  3506. * @return string
  3507. */
  3508. function js_conv($string)
  3509. {
  3510. $new_string = str_replace("'","\\'", $string);
  3511. $new_string = str_replace("\"","&quot;", $new_string);
  3512. return $new_string;
  3513. }
  3514. /**
  3515. * Show cphplib error
  3516. *
  3517. * like error_level is defined
  3518. *
  3519. * @param string $function cphplib function name
  3520. * @param string $message error message
  3521. * @param mixed $rc return code
  3522. */
  3523. function show_error($function, $message, $rc=null)
  3524. {
  3525. if ($this->error_level>0)
  3526. {
  3527. $message = $function." error: ".$message;
  3528. if (isset($rc))
  3529. {
  3530. $message .= " (rc=$rc)";
  3531. }
  3532. if ($this->error_level==3)
  3533. {
  3534. call_user_func($this->error_user_function, $message);
  3535. }
  3536. else if ($this->error_level==1)
  3537. {
  3538. echo $message;
  3539. }
  3540. else
  3541. {
  3542. die($message);
  3543. }
  3544. }
  3545. }
  3546. /**
  3547. * Get url for mod_rewrite
  3548. *
  3549. * @param string $prefix prefix of URL
  3550. * @param int $id
  3551. * @param string $name
  3552. * @param bool $invert_prefix if true, use prefix and id as suffix
  3553. * @param string $extension file extention
  3554. * @return string
  3555. */
  3556. function get_mod_rewrite_url($prefix, $id, $name="", $invert_prefix=false, $extension=".html")
  3557. {
  3558. // configuration for name
  3559. $max_length = 50;
  3560. $max_hyphen = 5;
  3561. if ($invert_prefix) $rc = "";
  3562. else $rc = $prefix.$id;
  3563. if (!empty($name))
  3564. {
  3565. if ((!$invert_prefix) && (!empty($rc))) $rc .= "_";
  3566. $name = $this->get_filename_from_text($name);
  3567. $pieces = explode("_", $name);
  3568. // get allowed length and hyphen
  3569. if ((strlen($name)>$max_length) || ($pieces>0))
  3570. {
  3571. $tmp_name = "";
  3572. $rc_name = "";
  3573. $cnt=0;
  3574. while(list(, $word)=each($pieces))
  3575. {
  3576. if ($cnt>0) $tmp_name .= "_";
  3577. $tmp_name .= $word;
  3578. if (strlen($tmp_name)<=$max_length)
  3579. {
  3580. $rc_name = $tmp_name;
  3581. }
  3582. else
  3583. {
  3584. break;
  3585. }
  3586. $cnt++;
  3587. if ($cnt==$max_hyphen) break;
  3588. }
  3589. $name = $rc_name;
  3590. }
  3591. $rc .= $name;
  3592. if ($invert_prefix) $rc .= "_";
  3593. }
  3594. if ($invert_prefix) $rc .= $prefix.$id;
  3595. $rc .= $extension;
  3596. return $rc;
  3597. }
  3598. /**
  3599. * Cleanup text from invalid characters for filename
  3600. *
  3601. * @param string $name
  3602. * @return string
  3603. */
  3604. function get_filename_from_text($name)
  3605. {
  3606. $name = strtolower($name);
  3607. $name = str_replace("- ", " ", $name);
  3608. $name = str_replace(" ", "_", $name);
  3609. $name = str_replace("__", "_", $name);
  3610. // Umlaute
  3611. $trans = get_html_translation_table(HTML_ENTITIES);
  3612. $trans = array_flip($trans);
  3613. $name = strtr($name, $trans);
  3614. $name = str_replace("ä", "ae", $name);
  3615. $name = str_replace("ö", "oe", $name);
  3616. $name = str_replace("ü", "ue", $name);
  3617. $name = str_replace("ß", "ss", $name);
  3618. $name = str_replace("Ä", "Ae", $name);
  3619. $name = str_replace("Ö", "Oe", $name);
  3620. $name = str_replace("Ü", "Ue", $name);
  3621. // valid characters
  3622. $valid = range('A', 'Z');
  3623. $valid = array_merge(range('a', 'z'), $valid);
  3624. $valid = array_merge(range('0', '9'), $valid);
  3625. $valid[] = "-";
  3626. $valid[] = "_";
  3627. $valid[] = " ";
  3628. $valid = array_flip($valid);
  3629. $max = strlen($name);
  3630. $rc_name = "";
  3631. for($i=0; $i<$max; $i++)
  3632. {
  3633. $chr = $name[$i];
  3634. if (array_key_exists($chr, $valid))
  3635. {
  3636. $rc_name .= $chr;
  3637. }
  3638. }
  3639. $rc_name = str_replace("-_", "_", $rc_name);
  3640. // twice!!!
  3641. $rc_name = str_replace("__", "_", $rc_name);
  3642. $rc_name = str_replace("__", "_", $rc_name);
  3643. return $rc_name;
  3644. }
  3645. /**
  3646. * table header for all tables with optional sort support
  3647. *
  3648. * @param array $cols colomns array
  3649. * @param int $default_col default colomn
  3650. * @param string $sort sort order
  3651. * @param string $base_url base url for all links
  3652. * @param array $customize bool $static true: don't use sort header
  3653. * false: sort header {default}
  3654. * bool $use_th true: th for table vill be used {default}
  3655. * false: td for table will be used
  3656. * int $max_entries if > 0: static mode, if max_entries < 2 {default: 2}
  3657. * string $mask if exists, it will be added on the end of the URL, e.g #mask
  3658. * string $tr_class CSS class for TR
  3659. * string $th_class CSS class for TD/TH
  3660. * @return string html output
  3661. */
  3662. function table_header($cols, $default_col, $sort, $base_url, $customize=null)
  3663. {
  3664. $static = false;
  3665. $use_th = true;
  3666. $max_entries = 2;
  3667. $mask = "";
  3668. $tr_class = "";
  3669. $td_class = "";
  3670. if (is_array($customize))
  3671. {
  3672. if ($customize['static']) $static = true;
  3673. if (!$customize['use_th']) $use_th = false;
  3674. if (isset($customize['max_entries'])) $max_entries = $customize['max_entries'];
  3675. if (isset($customize['mask'])) $mask = $customize['mask'];
  3676. if (isset($customize['tr_class'])) $tr_class = $customize['tr_class'];
  3677. if (isset($customize['td_class'])) $td_class = $customize['td_class'];
  3678. }
  3679. if ($use_th) $th = "th";
  3680. else $th = "td";
  3681. $rc = "\n<!-- ############################## table header ############################ -->\n";
  3682. $rc .= "<tr align=\"left\"";
  3683. if (!empty($tr_class)) $rc .= " class=\"".$tr_class."\"";
  3684. $rc .= ">\n";
  3685. $cols_sum = count($cols);
  3686. if ($cols_sum>0)
  3687. {
  3688. while(list($key, $value)=each($cols))
  3689. {
  3690. if ((isset($value['turn'])) && ($value['turn']==1))
  3691. {
  3692. $sort_suffix1 = "_asc";
  3693. $sort_suffix2 = "_desc";
  3694. $sort_img1 = "<img src=\"".$this->image_url."/order_desc.gif\" id=\"order_desc\" alt=\"\" />&nbsp;&nbsp;";
  3695. $sort_img2 = "<img src=\"".$this->image_url."/order_asc.gif\" id=\"order_asc\" alt=\"\" />&nbsp;&nbsp;";
  3696. $url_addon2 = "style=\"background: none;\" onmouseover=\"window.status='".STR_SORT_DESC."';return true\" onmouseout=\"window.status=''\"";
  3697. $url_img_addon1 = "style=\"background: none;\" onmouseover=\"window.status='".STR_SORT_ASC."';document.getElementById('order_desc').src='".$this->image_url."/order_asc.gif';return true\" onmouseout=\"window.status='';document.getElementById('order_desc').src='".$this->image_url."/order_desc.gif';\"";
  3698. $url_img_addon2 = "style=\"background: none;\" onmouseover=\"window.status='".STR_SORT_DESC."';document.getElementById('order_asc').src='".$this->image_url."/order_desc.gif';return true\" onmouseout=\"window.status='';document.getElementById('order_asc').src='".$this->image_url."/order_asc.gif';\"";
  3699. }
  3700. else
  3701. {
  3702. $sort_suffix1 = "_desc";
  3703. $sort_suffix2 = "_asc";
  3704. $sort_img1 = "<img src=\"".$this->image_url."/order_asc.gif\" id=\"order_asc\" alt=\"\" />&nbsp;&nbsp;";
  3705. $sort_img2 = "<img src=\"".$this->image_url."/order_desc.gif\" id=\"order_desc\" alt=\"\" />&nbsp;&nbsp;";
  3706. $url_addon2 = "style=\"background: none;\" onmouseover=\"window.status='".STR_SORT_ASC."';return true\" onmouseout=\"window.status='';\"";
  3707. $url_img_addon1 = "style=\"background: none;\" onmouseover=\"window.status='".STR_SORT_DESC."';document.getElementById('order_asc').src='".$this->image_url."/order_desc.gif';return true\" onmouseout=\"window.status='';document.getElementById('order_asc').src='".$this->image_url."/order_asc.gif';\"";
  3708. $url_img_addon2 = "style=\"background: none;\" onmouseover=\"window.status='".STR_SORT_ASC."';document.getElementById('order_desc').src='".$this->image_url."/order_asc.gif';return true\" onmouseout=\"window.status='';document.getElementById('order_desc').src='".$this->image_url."/order_desc.gif';\"";
  3709. }
  3710. $rc .= "\t<".$th;
  3711. if (isset($td_class)) $rc .= " class=\"".$td_class."\"";
  3712. if ((isset($value['width'])) && (!isvoid($value['width'])))
  3713. {
  3714. $rc .= " style=\"width: ".($value['width']*1.2)."px\"";
  3715. }
  3716. if ((isset($value['colspan'])) && (!isvoid($value['colspan']))) $rc .= " colspan=\"".$value['colspan']."\"";
  3717. if ((isset($value['align'])) && (!isvoid($value['align']))) $rc .= " align=\"".$value['align']."\"";
  3718. else $rc .= " align=\"left\""; // required for konqueror
  3719. if ((isvoid($value['value'])) || ($max_entries<2))
  3720. {
  3721. $rc .= ">".$value['name']."&nbsp;</".$th."\n";
  3722. }
  3723. else
  3724. {
  3725. if (($sort==$value['value'].$sort_suffix2) || ((isvoid($sort)) && ($key==$default_col)))
  3726. {
  3727. if ($static==true)
  3728. $rc .= ">".$value['name']."&nbsp;".$sort_img1."</".$th.">\n";
  3729. else
  3730. $rc .= ">".$this->url($base_url."&amp;sort=".$value['value'].$sort_suffix1.$mask, $value['name'], 1,"",$url_img_addon1)."&nbsp;".$sort_img1."</".$th.">\n";
  3731. }
  3732. else if (($sort==$value['value'].$sort_suffix1) || ((isvoid($sort)) && ($key==$default_col)))
  3733. {
  3734. if ($static==true)
  3735. $rc .= ">".$value['name']."&nbsp;".$sort_img2."</".$th.">\n";
  3736. else
  3737. $rc .= ">".$this->url($base_url."&amp;sort=".$value['value'].$sort_suffix2.$mask, $value['name'], 1,"",$url_img_addon2)."&nbsp;".$sort_img2."</".$th.">\n";
  3738. }
  3739. else
  3740. {
  3741. if ($static==true)
  3742. $rc .= ">".$value['name']."&nbsp;</".$th.">\n";
  3743. else
  3744. $rc .= ">".$this->url($base_url."&amp;sort=".$value['value'].$sort_suffix2.$mask, $value['name'], 1,"",$url_addon2)."&nbsp;</".$th.">\n";
  3745. }
  3746. }
  3747. }
  3748. }
  3749. else
  3750. {
  3751. $rc .= "\t<".$th.">&nbsp;</".$th.">\n";
  3752. }
  3753. $rc .= "</tr>\n";
  3754. $rc .= "<!-- ############################## table header end ############################ -->\n\n";
  3755. return $rc;
  3756. }
  3757. }
  3758. ?>