Source for file cphplib.inc

Documentation is available at cphplib.inc

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

Documentation generated on Fri, 11 Nov 2005 10:39:55 +0100 by phpDocumentor 1.3.0RC3