PageRenderTime 76ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/cphplib/cphplib.inc

https://github.com/esokullu/grou.ps
PHP | 4203 lines | 2890 code | 416 blank | 897 comment | 823 complexity | d5ef5cee4ea951782ea3e7aceb14e341 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * cphplib is a PHP-functions library. Those could simply be integrated into existing PHP-Scripts and allow an easy use.
  4. * These functions are very flexible to use because they are kept very common.
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * @category Utilities
  9. * @package cphplib
  10. * @author Alexander Meindl <am@meindlsoft.com>
  11. * @author Sven Reul <heffer@quaddamage.de>
  12. * @copyright (c) 2002-2005 meindlSOFT
  13. * @license Released under v2 of the GNU LGPL
  14. * @version Version 0.50 CVS: $Id: cphplib.inc,v 1.20 2005/11/11 09:12:25 alex Exp $
  15. * @link http://www.meindlsoft.com/tools.php
  16. */
  17. /**
  18. * Replacement for useless PHP empty function
  19. * Returns true, if string is empty
  20. *
  21. * @param string $str string to validate
  22. * @param bool $mode true for compatible with echo {default: false}
  23. * @return bool
  24. */
  25. function isvoid($str, $mode=false)
  26. {
  27. $rc = false;
  28. if (is_array($str))
  29. {
  30. if (count($str)==0)
  31. {
  32. $rc = true;
  33. }
  34. }
  35. else
  36. {
  37. $str = trim($str);
  38. if (strlen($str)==0)
  39. {
  40. $rc = true;
  41. }
  42. else if (($mode) && ($str=="0"))
  43. {
  44. $rc = true;
  45. }
  46. }
  47. return $rc;
  48. }
  49. /**
  50. * cphplib class
  51. */
  52. class cphplib
  53. {
  54. /**
  55. * cphplib version
  56. *
  57. * @var float
  58. */
  59. var $version = "0.50";
  60. /**
  61. * Error level
  62. * 0: show no error message
  63. * 1: only print message
  64. * 2: die, if error occurres
  65. * 3: user defined function (callback) => error_user_function required
  66. *
  67. * @var int
  68. */
  69. var $error_level = 2;
  70. /**
  71. * User defined function
  72. *
  73. * Required for error_level=3
  74. *
  75. * @var mixed
  76. */
  77. var $error_user_function;
  78. /**
  79. * Date format
  80. *
  81. * I = International date identifier (MM/DD/YYYY)
  82. * S = Science date identifier (YYYY-MM-DD)
  83. * C = Date without seperators (YYYYMMDD)
  84. * L = German date identifier (DD.MM.YYYY)
  85. *
  86. * @var char
  87. */
  88. var $date_format;
  89. /**
  90. * Time format
  91. *
  92. * Y = 24 hours mode
  93. * N = 12 hours mode
  94. *
  95. * @var char
  96. */
  97. var $time_format;
  98. /**
  99. * locale to use (see "man locale")
  100. *
  101. * @var string
  102. */
  103. var $locale;
  104. /**
  105. * xhtml output
  106. *
  107. * @var bool
  108. */
  109. var $xhtml = true;
  110. /**
  111. * Max. entries on one page of a list
  112. *
  113. * @var int
  114. */
  115. var $page_entries = 20;
  116. /**
  117. * Type of database
  118. *
  119. * @var string
  120. */
  121. var $db_type;
  122. /**
  123. * Database pear object
  124. * (mysql or pgsql)
  125. *
  126. * @var resource
  127. */
  128. var $db;
  129. /**
  130. * like not case sensitive for SQL
  131. *
  132. * @var resource
  133. */
  134. var $sql_like;
  135. /**
  136. * International seperator
  137. *
  138. * @var char
  139. */
  140. var $m_sep_i = "/";
  141. /**
  142. * Science seperator
  143. *
  144. * @var char
  145. */
  146. var $m_sep_s = "-";
  147. /**
  148. * German seperator
  149. *
  150. * @var char
  151. */
  152. var $m_sep_l = ".";
  153. /**
  154. * Image URL to all images
  155. *
  156. * @var string
  157. */
  158. var $image_url = "images";
  159. // db session variables
  160. var $m_dbsession_handler = false; // must be true for handling session_id
  161. var $m_dbsession_id = ""; // dbsession ID
  162. var $m_dbsession_id_name = "SID"; // dbsession ID name
  163. var $m_dbsession_table = "dbsession"; // table name for dbsession use
  164. var $m_dbsession_detail_table = "dbsession_detail"; // table name for dbsession use
  165. var $m_dbsession_timeout = 60; // dbsession timeout
  166. /**
  167. * cookies can be used for dbsession
  168. *
  169. * @var bool
  170. */
  171. var $m_dbsession_cookies = false;
  172. /**
  173. * take special care for mod_rewrite
  174. *
  175. * at the moment only url() is supported
  176. *
  177. * @var bool
  178. */
  179. var $mod_rewrite = false;
  180. /**
  181. * constructor
  182. *
  183. * @param string $locale if no empty, locale and string will be initialised
  184. * @return cphplib
  185. */
  186. function cphplib($locale="de_DE")
  187. {
  188. if (!empty($locale))
  189. {
  190. $this->locale = $locale;
  191. $this->set_locale();
  192. $this->set_strings();
  193. }
  194. }
  195. /**
  196. * Set locale
  197. *
  198. */
  199. function set_locale()
  200. {
  201. if ($this->locale=="de_DE")
  202. {
  203. $this->date_format = "L";
  204. $this->time_format = "Y";
  205. if ($this->check_php_version("4.3.0"))
  206. {
  207. setlocale(LC_TIME, 'de_DE@euro', 'de_DE', 'german', 'deu');
  208. setlocale(LC_CTYPE, 'de_DE@euro', 'de_DE', 'german', 'deu');
  209. }
  210. else if ($this->os_type(true) == "w")
  211. {
  212. setlocale(LC_TIME, 'german');
  213. setlocale(LC_CTYPE, 'german');
  214. }
  215. else
  216. {
  217. setlocale(LC_TIME, 'de_DE');
  218. setlocale(LC_CTYPE, 'de_DE');
  219. }
  220. }
  221. else
  222. {
  223. $this->set_format("I", "N");
  224. setlocale(LC_TIME, $this->locale);
  225. setlocale(LC_CTYPE, $this->locale);
  226. }
  227. }
  228. /**
  229. * Set date and time format to cphplib
  230. *
  231. * @param char $date_format see $this->date_format for valid values
  232. * @param char $time_format see $this->time_format for valid values
  233. */
  234. function set_format($date_format, $time_format)
  235. {
  236. $this->date_format = $date_format;
  237. $this->time_format = $time_format;
  238. }
  239. /**
  240. * Set strings for pager and Date methods
  241. *
  242. */
  243. function set_strings()
  244. {
  245. if (substr($this->locale, 0, 2) =="de")
  246. {
  247. include_once("i18n/german.inc");
  248. }
  249. else
  250. {
  251. include_once("i18n/english.inc");
  252. }
  253. }
  254. /**
  255. * Opens a connection to a database server and select database
  256. * Furthermore it sets $this->db to the PEAR database object,
  257. * which is required for dbsession
  258. *
  259. * @param string $dsn Data Source Name ( for more information see PEAR documentation)
  260. * addon array key:
  261. * persistent = true for persistent {default: false}
  262. * @param bool $set_db if true, db object will be set to $this->db
  263. * (and db_tyle and sql_like, too)
  264. * @return object PEAR database object
  265. */
  266. function db_connect($dsn, $set_db=true)
  267. {
  268. if ($this->file_exists("DB.php"))
  269. {
  270. if ((!isvoid($dsn['phptype'])) && (!(extension_loaded($dsn['phptype']))))
  271. {
  272. $this->show_error("db_connect", "PHP database module &quot;".$dsn['phptype']."&quot; is not supported by your system.");
  273. }
  274. require_once("DB.php");
  275. }
  276. else
  277. {
  278. $this->show_error("db_connect", "Pear \"DB Package\" required.");
  279. }
  280. if ($dsn['persistent']) $db =& DB::connect($dsn, true);
  281. else $db =& DB::connect($dsn ,false);
  282. if (DB::isError($db))
  283. {
  284. $this->show_error("db_connect", $db->getMessage(), $db->getCode());
  285. }
  286. if ($set_db)
  287. {
  288. $this->db =& $db;
  289. if ($dsn['phptype']=="mysqli") $this->db_type = "mysql";
  290. else $this->db_type = $dsn['phptype'];
  291. if ($dsn['phptype']=="pgsql") $this->sql_like = "ILIKE";
  292. else $this->sql_like = "LIKE";
  293. }
  294. return $db;
  295. }
  296. /**
  297. * disconnect current database connection
  298. * (change $this->db for select the right one, if you
  299. * use more the one connection)
  300. *
  301. * @return bool true if no errors occurred, otherwise false
  302. */
  303. function db_close()
  304. {
  305. if (DB::isError($this->db))
  306. {
  307. $this->show_error("db_close", $this->db->getMessage());
  308. }
  309. return @ $this->db->disconnect();
  310. }
  311. /**
  312. * Get the id generated from the previous INSERT operation
  313. *
  314. * @return int
  315. */
  316. function db_insert_id()
  317. {
  318. if (DB::isError($this->db))
  319. {
  320. $this->show_error("db_insert_id", $this->db->getMessage());
  321. }
  322. $rc = $this->db->getOne("SELECT last_insert_id()");
  323. if (DB::isError($rc))
  324. {
  325. $this->show_error("db_insert_id", $rc->getMessage());
  326. }
  327. return $rc;
  328. }
  329. /**
  330. * Get next available number
  331. *
  332. * @param string $table database table to use
  333. * @param string $column database column to use
  334. * @param string $where SQL WHERE restriction, e.g. "thisvalue>0"
  335. * (without "WHERE" in string!)
  336. * @return int highst number in column + 1
  337. */
  338. function db_next_id($table, $column, $where=null)
  339. {
  340. if (DB::isError($this->db))
  341. {
  342. $this->show_error("db_next_id", $this->db->getMessage());
  343. }
  344. $sqlstr = "SELECT MAX($column)+1 FROM $table";
  345. if (isset($where)) $sqlstr .= " WHERE ".$where;
  346. $tmp_id = $this->db->getOne($sqlstr);
  347. if (DB::isError($tmp_id))
  348. {
  349. $this->show_error("db_next_id", $tmp_id->getMessage());
  350. }
  351. if (empty($tmp_id))
  352. {
  353. return 1;
  354. }
  355. else
  356. {
  357. return $tmp_id;
  358. }
  359. }
  360. /**
  361. * Get next available number
  362. *
  363. * @param string $seq_name name of sequence
  364. * @return int
  365. */
  366. function db_seq_id($seq_name)
  367. {
  368. if (DB::isError($this->db))
  369. {
  370. $this->show_error("db_seq_id", $this->db->getMessage());
  371. }
  372. else if (isvoid($seq_name))
  373. {
  374. $this->show_error("db_seq_id", "seq_name is missing");
  375. }
  376. else if (!is_object($this->db))
  377. {
  378. $this->show_error("db_seq_id", "\$this-&gt;db is not an database object");
  379. }
  380. $tmp_id = $this->db->nextId($seq_name);
  381. if (DB::isError($tmp_id))
  382. {
  383. $this->show_error("db_seq_id", $tmp_id->getMessage());
  384. }
  385. if (empty($tmp_id)) return 1;
  386. else return $tmp_id;
  387. }
  388. /**
  389. * Get number of lowest unused number in a column)
  390. *
  391. * @param string $table database table to use
  392. * @param string $column database column to use
  393. * @param string $where_key database column name for limitation {default: void}
  394. * @param string $where_value limitation value {default: void}
  395. * @return int lowest unused number in a column
  396. */
  397. function db_free_id($table, $column, $where_key="", $where_value="")
  398. {
  399. if (DB::isError($this->db))
  400. {
  401. $this->show_error("db_free_id", $this->db->getMessage());
  402. }
  403. $rc=0;
  404. if ((isvoid($where_key)) && (isvoid($where_value)))
  405. $sqlstr = "SELECT $column FROM $table ORDER BY $column";
  406. else if ((!isvoid($where_key)) && (!isvoid($where_value)))
  407. $sqlstr = "SELECT $column FROM $table WHERE $where_key='$where_value' ORDER BY $column";
  408. if (!isvoid($sqlstr))
  409. {
  410. $que = $this->db->query($sqlstr);
  411. if ($que->numRows()>0)
  412. {
  413. $count=1;
  414. while ($row = $que->fetchRow())
  415. {
  416. $tmp_id = $row[0];
  417. if ($count==$tmp_id)
  418. $count++;
  419. else
  420. {
  421. $rc = $count;
  422. break;
  423. }
  424. }
  425. if ($rc==0)
  426. $rc=$count;
  427. }
  428. else
  429. $rc=1;
  430. }
  431. return $rc;
  432. }
  433. /**
  434. * check right database version
  435. *
  436. * @param float $mav major release number {default: 3.23}
  437. * @param float $miv minor release number {default: 6}
  438. * @return string active database version, if false empty
  439. */
  440. function db_version($mav="3.23", $miv="6")
  441. {
  442. if (DB::isError($this->db))
  443. {
  444. $this->show_error("db_version", $this->db->getMessage());
  445. }
  446. $rc = "unknown";
  447. if (($this->db_type)=="mysql")
  448. {
  449. if ($this->db->phptype=="mysqli") $tmp_rc = mysqli_get_server_info($this->db->connection);
  450. else $tmp_rc = mysql_get_server_info();
  451. $t_mav1 = strtok($tmp_rc,'.');
  452. $t_mav2 = strtok('.');
  453. $t_miv = strtok('.');
  454. $t_mav = $t_mav1.".".$t_mav2;
  455. if ($t_mav > $mav) $rc = "MySQL ".$tmp_rc;
  456. else if (($t_mav == $mav) && (intval($t_miv) >= $miv)) $rc = "MySQL ".$tmp_rc;
  457. else
  458. {
  459. $this->show_error("db_version", "MySQL version to old (version $mav.$miv or higher required), <b>program aborted</b>.");
  460. }
  461. }
  462. else
  463. {
  464. $tmp_rc = $this->db->getOne("SELECT version()");
  465. if (!isvoid($tmp_rc))
  466. {
  467. $rc = substr($tmp_rc,0,strpos($tmp_rc," on"));
  468. if (isvoid($rc)) $rc = $tmp_rc; // just to be sure
  469. }
  470. }
  471. return $rc;
  472. }
  473. /**
  474. * starts dbsession. Enables session fallback handling. This functions
  475. * will be required, if you want to handle the session_id with
  476. * url or formstart
  477. * Required : read README file
  478. *
  479. * @param string $session_id session id to use, if empty new unique id
  480. * will be generated
  481. * @param bool $table_error show error message, if dbsession tables doesn't exist
  482. * @return string session_id
  483. */
  484. function dbsession_start($session_id="", $table_error=true)
  485. {
  486. global $HTTP_COOKIE_VARS;
  487. $this->m_dbsession_handler=true;
  488. $IDpassed = false;
  489. if (DB::isError($this->db))
  490. {
  491. $this->show_error("dbsession_start", $this->db->getMessage());
  492. }
  493. else
  494. {
  495. $tdata = $this->db->getListOf("tables");
  496. if ((!in_array($this->m_dbsession_table,$tdata)) || (!in_array($this->m_dbsession_detail_table,$tdata)))
  497. $dbtables_found = false;
  498. else
  499. $dbtables_found = true;
  500. if ((!$dbtables_found) && ($table_error))
  501. {
  502. $this->show_error("dbsession_start", "missing dbsession database table");
  503. }
  504. }
  505. if (empty($session_id)) $SID = $this->get_user_var($this->m_dbsession_id_name,'POST,GET,COOKIE');
  506. else $SID = $session_id;
  507. // generate session_is
  508. $session_is = $this->get_id_string(false);
  509. // validate session_id
  510. if ((!empty($SID)) && (strlen($SID)==32) && ($dbtables_found))
  511. {
  512. $t_sec = date("s");
  513. $t_min = date("i");
  514. $t_hours = date("H");
  515. $t_day = date("d");
  516. $t_month = date("m");
  517. $t_year = date("Y");
  518. $session_start = $t_year."-".$t_month."-".$t_day." ".$t_hours.":".$t_min.":".$t_sec;
  519. $stempel = mktime($t_hours,($t_min+$this->m_dbsession_timeout),$t_sec,$t_month,$t_day,$t_year);
  520. $faellig = getdate($stempel);
  521. $session_end = $faellig['year']."-".$faellig['mon']."-".$faellig['mday']." ".$faellig['hours'].":".$faellig['minutes'].":".$faellig['seconds'];
  522. $sqlstr = "SELECT COUNT(*) FROM ".$this->m_dbsession_table." WHERE session_id='$SID'";
  523. if ($this->db->getOne($sqlstr)>0)
  524. {
  525. $sqlstr = "SELECT session_is,session_end FROM ".$this->m_dbsession_table." WHERE session_id='$SID'";
  526. $sdata = $this->db->getRow($sqlstr,DB_FETCHMODE_ASSOC);
  527. if (!empty($sdata['session_end']))
  528. {
  529. $tmp_stamp_db = $this->convtoTimestamp($sdata['session_end'],'datetime');
  530. $tmp_stamp_now = $this->convtoTimestamp();
  531. if (($sdata['session_is']==$session_is) && ($tmp_stamp_db > $tmp_stamp_now))
  532. {
  533. $sqlstr = "UPDATE ".$this->m_dbsession_table." SET session_end=".$this->sql_value($session_end);
  534. $sqlstr .= " WHERE session_id='$SID'";
  535. $this->db->query($sqlstr);
  536. $IDpassed = true;
  537. }
  538. else
  539. $this->dbsession_end($SID);
  540. }
  541. }
  542. else
  543. {
  544. $sqlstr = "INSERT INTO ".$this->m_dbsession_table." (session_id,session_start,session_end,session_is)";
  545. $sqlstr .= " VALUES ('$SID',";
  546. $sqlstr .= $this->sql_value($session_start).",";
  547. $sqlstr .= $this->sql_value($session_end).",";
  548. $sqlstr .= $this->sql_value($session_is) .")";
  549. $this->db->query($sqlstr);
  550. $IDpassed = true;
  551. }
  552. }
  553. // calculate new SID and session_is
  554. if (!$IDpassed) $SID = $this->get_id_string(true);
  555. $this->m_dbsession_id = $SID;
  556. setcookie($this->m_dbsession_id_name, $this->m_dbsession_id,0,"/");
  557. if ((!$IDpassed) && ($dbtables_found)) // no valid session_id in url found
  558. {
  559. $tmp_script_name = $_SERVER['SCRIPT_NAME'];
  560. $tmp_server_name = $_SERVER['SERVER_NAME'];
  561. $tmp_server_port = $_SERVER['SERVER_PORT'];
  562. $tmp_query_string = $_SERVER['QUERY_STRING'];
  563. $query = $tmp_query_string != ""
  564. ? "?".$tmp_query_string
  565. : "";
  566. $url = $tmp_script_name.$query;
  567. // non-standard port?
  568. $portMatch = array();
  569. $port = !preg_match("/^(80|443)$/", $tmp_server_port, $portMatch)
  570. ? ":".$tmp_server_port
  571. : "";
  572. $new_location = (($portMatch[1] == 443) ? "https://" : "http://");
  573. $new_location .= $tmp_server_name.$port.$this->url($url,"",2);
  574. // redirect
  575. header("Location: $new_location");
  576. exit;
  577. }
  578. $this->m_dbsession_cookies = (isset($_COOKIE[$this->m_dbsession_id_name]) && @strlen($_COOKIE[$this->m_dbsession_id_name]) == 32);
  579. return $SID;
  580. }
  581. /**
  582. * starts sub session within a session. You can use it to group the session
  583. * in different parts
  584. * Required : read README file
  585. *
  586. * @param string $session_id session id to use (main session)
  587. * @param string $session_subid session_subid. If empty new unique id
  588. * will be generated if required
  589. * @return string session_subid
  590. */
  591. function dbsessionsub_start($session_id, $session_subid="")
  592. {
  593. if (DB::isError($this->db))
  594. {
  595. $this->show_error("dbsessionsub_start", $this->db->getMessage());
  596. }
  597. $sqlstr = "SELECT COUNT(*) FROM ".$this->m_dbsession_table." WHERE session_id='$session_id'";
  598. if ($this->db->getOne($sqlstr)==0)
  599. {
  600. $this->show_error("dbsessionsub_start", "invalid session_id");
  601. }
  602. // generate session_is
  603. $session_is = $this->get_id_string(false);
  604. // validate session_subid
  605. if ((empty($session_subid)) || (strlen($session_subid)!=32))
  606. $session_subid = $this->get_id_string(true);
  607. $t_sec = date("s");
  608. $t_min = date("i");
  609. $t_hours = date("H");
  610. $t_day = date("d");
  611. $t_month = date("m");
  612. $t_year = date("Y");
  613. $session_start = $t_year."-".$t_month."-".$t_day." ".$t_hours.":".$t_min.":".$t_sec;
  614. $stempel = mktime($t_hours,($t_min+$this->m_dbsession_timeout),$t_sec,$t_month,$t_day,$t_year);
  615. $faellig = getdate($stempel);
  616. $session_end = $faellig['year']."-".$faellig['mon']."-".$faellig['mday']." ".$faellig['hours'].":".$faellig['minutes'].":".$faellig['seconds'];
  617. $sqlstr = "SELECT COUNT(*) FROM ".$this->m_dbsession_table." WHERE session_subid='$session_subid'";
  618. if ($this->db->getOne($sqlstr)>0) // maybe update is overkill, but it's safer.
  619. {
  620. $sqlstr = "UPDATE ".$this->m_dbsession_table." SET session_end=".$this->sql_value($session_end);
  621. $sqlstr .= " WHERE session_subid='$session_subid'";
  622. $this->db->query($sqlstr);
  623. }
  624. else
  625. {
  626. $sqlstr = "INSERT INTO ".$this->m_dbsession_table." (session_id,session_subid,session_start,session_end,session_is)";
  627. $sqlstr .= " VALUES ('$session_id','$session_subid',";
  628. $sqlstr .= $this->sql_value($session_start).",";
  629. $sqlstr .= $this->sql_value($session_end).",";
  630. $sqlstr .= $this->sql_value($session_is) .")";
  631. $this->db->query($sqlstr);
  632. }
  633. return $session_subid;
  634. }
  635. /**
  636. * ends dbsession or dbsessionsub
  637. * (Read the README file for requirements)
  638. *
  639. * @param string $session_id session id which should end ( and expired dbsession will automatically be removed)
  640. * @return bool true if successfully end dbsession
  641. */
  642. function dbsession_end($session_id="")
  643. {
  644. if (DB::isError($this->db))
  645. {
  646. $this->show_error("dbsession_end", $this->db->getMessage());
  647. }
  648. $sqlstr = "SELECT session_id, session_subid FROM ".$this->m_dbsession_table." WHERE session_end<=NOW()";
  649. if (!isvoid($session_id))
  650. $sqlstr .= " OR session_id=".$this->sql_value($session_id)." OR session_subid=".$this->sql_value($session_id);
  651. @ $que = $this->db->query($sqlstr);
  652. @ $num = $que->numRows();
  653. if ($num>0)
  654. {
  655. while ($row = $que->fetchRow())
  656. {
  657. if (!empty($row[1])) // sub session
  658. {
  659. $sqlstr1 = "DELETE FROM ".$this->m_dbsession_table." WHERE session_subid=".$this->sql_value($row[1]);
  660. $sqlstr2 = "DELETE FROM ".$this->m_dbsession_detail_table." WHERE session_id=".$this->sql_value($row[1]);
  661. }
  662. else // session with all sub sessions
  663. {
  664. $sqlstr1 = "DELETE FROM ".$this->m_dbsession_table." WHERE session_id=".$this->sql_value($row[0]);
  665. $sqlstr2 = "DELETE FROM ".$this->m_dbsession_detail_table." WHERE session_id=".$this->sql_value($row[0]);
  666. // take care of sub sessions /////////////
  667. $sqlstr = "SELECT session_subid FROM ".$this->m_dbsession_table." WHERE session_id=".$this->sql_value($row[0]);
  668. if (!isvoid($session_id))
  669. @ $sub_que = $this->db->query($sqlstr);
  670. @ $num = $sub_que->numRows();
  671. if ($num>0)
  672. {
  673. while ($sub_row = $sub_que->fetchRow())
  674. {
  675. $sqlstr = "DELETE FROM ".$this->m_dbsession_detail_table." WHERE session_id=".$this->sql_value($sub_row[0]);
  676. @ $this->db->query($sqlstr1);
  677. }
  678. }
  679. //////////////////////////////////////////
  680. }
  681. @ $this->db->query($sqlstr1);
  682. @ $this->db->query($sqlstr2);
  683. }
  684. }
  685. return true;
  686. }
  687. /**
  688. * register variable in dbsession
  689. * Required : read README file
  690. *
  691. * @param string $session_id session id to use
  692. * @param string $var_name variable to add
  693. * @param mixed $var_value value of variable
  694. * @return string value of variable
  695. */
  696. function dbsession_write($session_id, $var_name, $var_value)
  697. {
  698. $rc = "";
  699. if (DB::isError($this->db))
  700. {
  701. $this->show_error("dbsession_write", $this->db->getMessage());
  702. }
  703. if ((empty($session_id)) || (empty($var_name)))
  704. {
  705. $error_string = "dbsession_write error: var_name missing";
  706. trigger_error($error_string, E_USER_ERROR);
  707. }
  708. else if (empty($var_value))
  709. {
  710. $this->dbsession_delete($session_id, $var_name);
  711. }
  712. else
  713. {
  714. // if already stored, delete it
  715. $sqlstr = "SELECT COUNT(session_id) FROM ".$this->m_dbsession_detail_table." WHERE session_id=".$this->sql_value($session_id)." AND session_var=".$this->sql_value($var_name);
  716. if ($this->db->getOne($sqlstr)>0) // update
  717. $this->dbsession_delete($session_id,$var_name);
  718. if (is_array($var_value))
  719. {
  720. $sqlstr = "INSERT INTO ".$this->m_dbsession_detail_table." (session_id,session_array,session_var,session_value) VALUES (".$this->sql_value($session_id).",'Y',";
  721. $sqlstr .= "'$var_name',".$this->sql_value(serialize($var_value)).")";
  722. $this->db->query($sqlstr);
  723. }
  724. else
  725. {
  726. $sqlstr = "INSERT INTO ".$this->m_dbsession_detail_table." (session_id,session_array,session_var,session_value) VALUES (".$this->sql_value($session_id).",'N',";
  727. $sqlstr .= $this->sql_value($var_name) .",";
  728. if (!isvoid($var_value)) $sqlstr .= $this->sql_value($var_value) .")";
  729. else $sqlstr .= "NULL)";
  730. $this->db->query($sqlstr);
  731. }
  732. $rc = $var_value;
  733. }
  734. return $rc;
  735. }
  736. /**
  737. * register variable in dbsession
  738. *
  739. * @param string $session_id session id to use
  740. * @param string $var_name name of variable
  741. * @return string value of variable
  742. */
  743. function dbsession_read($session_id, $var_name)
  744. {
  745. $rc = "";
  746. if (DB::isError($this->db))
  747. {
  748. $this->show_error("dbsession_read", $this->db->getMessage());
  749. }
  750. if ((empty($session_id)) || (empty($var_name)))
  751. {
  752. $error_string = "dbsession_read error: var_name missing";
  753. trigger_error($error_string, E_USER_ERROR);
  754. }
  755. else
  756. {
  757. $sqlstr = "SELECT session_array,session_value FROM ".$this->m_dbsession_detail_table;
  758. $sqlstr .= " WHERE session_id=".$this->sql_value($session_id)." AND session_var=".$this->sql_value($var_name);
  759. $row = $this->db->getRow($sqlstr);
  760. if (is_array($row))
  761. {
  762. if ($row[0]=="N") $rc = $row[1];
  763. else $rc = unserialize($row[1]);
  764. }
  765. }
  766. return $rc;
  767. }
  768. /**
  769. * unregister variable in dbsession
  770. * Required : read README file
  771. *
  772. * @param string $session_id session id which will be used for the variabel
  773. * @param string $var_name variable to remove
  774. * @return bool true if successfully removed variable
  775. */
  776. function dbsession_delete($session_id, $var_name)
  777. {
  778. if (DB::isError($this->db))
  779. {
  780. $this->show_error("dbsession_delete", $this->db->getMessage());
  781. }
  782. if ((empty($session_id)) || (empty($var_name)))
  783. {
  784. $error_string = "dbsession_delete error: var_name missing";
  785. trigger_error($error_string, E_USER_ERROR);
  786. return false;
  787. }
  788. else
  789. {
  790. $sqlstr = "DELETE FROM ".$this->m_dbsession_detail_table." WHERE session_id=".$this->sql_value($session_id)." AND session_var=".$this->sql_value($var_name);
  791. $this->db->query($sqlstr);
  792. return true;
  793. }
  794. }
  795. /**
  796. * prints url as html tag
  797. * (this function can only handle the session_id, if dbsession_start has
  798. * been called immediately after creating the class object)
  799. *
  800. * @param string $url url
  801. * @param string $name name of url {default: url}
  802. * @param int $mode 0: no session_id
  803. * 1: with session_id if required {default: 1}
  804. * 2: just url with session_id (without TAG)
  805. * 3: just url with session_id (without TAG), but with delimiter &amp; instead of &
  806. * 4: with session_id (always)
  807. * @param string $title url title (hover text)
  808. * @param string $customize other parameters like target, style or class
  809. * @return string created url
  810. */
  811. function url($url, $name="", $mode=1, $title="", $customize="")
  812. {
  813. if ((($mode>0) && (!$this->m_dbsession_cookies) && (!$this->mod_rewrite))
  814. || ($mode==4))
  815. {
  816. if ($this->m_dbsession_handler) // if cphplib handles session fallback
  817. {
  818. // only add session_id if its on same host as script was executed (server).
  819. if ((($this->url_on_scripthost($url)) || ($mode==4)) &&
  820. (substr_count($url,"mailto:")==0))
  821. {
  822. // Anchor-Fragment extrahieren
  823. $dummyArray = split("#",$url);
  824. $pathInfo = $dummyArray[0];
  825. // evtl. (defekte) Session-ID(s) aus dem Querystring entfernen
  826. $pathInfo = preg_replace("/[?|&]".$this->m_dbsession_id_name."=[^?|&]*/","",$pathInfo);
  827. // evtl. Query-Delimiter korrigieren
  828. if (preg_match("/&/",$pathInfo) && !preg_match("/\?/",$pathInfo))
  829. $pathInfo = preg_replace("/&/","?",$pathInfo,1);
  830. // clear trash
  831. $match = array();
  832. preg_match("/(.*)(?<!&|\?)/",$pathInfo,$match);
  833. $url = $match[0];
  834. // add new session name and session id
  835. if (($mode==1) || ($mode==3) || ($mode==4)) $url .= preg_match("/\?/",$url) ? "&amp;" : "?";
  836. else $url .= preg_match("/\?/",$url) ? "&" : "?";
  837. $url .= $this->m_dbsession_id_name."=".$this->m_dbsession_id;
  838. // add anchor part again
  839. $url .= isset($dummyArray[1]) ? "#".$dummyArray[1] : "";
  840. }
  841. }
  842. }
  843. if (($mode==2) || ($mode==3)) return $url;
  844. else
  845. {
  846. if (isvoid($name)) $name = $url;
  847. else $name = trim($name);
  848. $rc = "<a href=\"".$url."\"";
  849. if (!isvoid($title)) $rc .= " title=\"".$title."\"";
  850. if (!isvoid($customize)) $rc .= " $customize";
  851. $rc .= ">".$name."</a>";
  852. return $rc;
  853. }
  854. }
  855. /**
  856. * removes magic quotes
  857. *
  858. * @param array $array
  859. */
  860. function remove_magic_quotes(&$array)
  861. {
  862. if(!get_magic_quotes_gpc()) return;
  863. foreach($array as $key => $value)
  864. {
  865. if(is_array($value)) $this->remove_magic_quotes($value);
  866. else $array[$key] = stripslashes($value);
  867. }
  868. }
  869. /**
  870. * returns value of special variable type from user input
  871. * (If register_globals is Off (default since 4.2.0), this function
  872. * can be used, to handle user variables (insecure variables).
  873. * You can use more than one type (seperated with ",")
  874. * DBSESSION only works if dbsession_start has been called before.
  875. *
  876. * @param string $var_name name of variable
  877. * @param string $var_type type of variable (POST, GET, COOKIE, SCRIPT, SESSION or DBSESSION)
  878. * (note: SCRIPT means, declared variable above the function)
  879. * @return mixed value of variable or "", if invalid
  880. */
  881. function get_user_var($var_name, $var_type)
  882. {
  883. global $HTTP_POST_VARS, $HTTP_GET_VARS, $HTTP_SESSION_VARS, $HTTP_COOKIE_VARS;
  884. $rc = "";
  885. $types = explode(",", $var_type);
  886. while (list(, $value) = each($types))
  887. {
  888. if (strtoupper($value) == "POST")
  889. {
  890. if (isset($_POST))
  891. {
  892. if ((isset($_POST[$var_name])) && (!isvoid($_POST[$var_name])))
  893. {
  894. if (get_magic_quotes_gpc())
  895. {
  896. if (is_array($_POST[$var_name]))
  897. {
  898. $this->remove_magic_quotes($_POST[$var_name]);
  899. return $_POST[$var_name];
  900. }
  901. else return stripslashes($_POST[$var_name]);
  902. }
  903. else return $_POST[$var_name];
  904. }
  905. }
  906. else
  907. {
  908. if ((isset($HTTP_POST_VARS[$var_name])) && (!isvoid($HTTP_POST_VARS[$var_name])))
  909. {
  910. if (get_magic_quotes_gpc())
  911. {
  912. if (is_array($HTTP_POST_VARS[$var_name]))
  913. {
  914. $this->remove_magic_quotes($HTTP_POST_VARS[$var_name]);
  915. return $HTTP_POST_VARS[$var_name];
  916. }
  917. else return stripslashes($HTTP_POST_VARS[$var_name]);
  918. }
  919. else return $HTTP_POST_VARS[$var_name];
  920. }
  921. }
  922. }
  923. else if (strtoupper($value) == "GET")
  924. {
  925. if (isset($_GET))
  926. {
  927. if ((isset($_GET[$var_name])) && (!isvoid($_GET[$var_name])))
  928. {
  929. if (get_magic_quotes_gpc())
  930. {
  931. if (is_array($_GET[$var_name]))
  932. {
  933. $this->remove_magic_quotes($_GET[$var_name]);
  934. return $_GET[$var_name];
  935. }
  936. else return stripslashes($_GET[$var_name]);
  937. }
  938. else return $_GET[$var_name];
  939. }
  940. }
  941. else
  942. {
  943. if ((isset($HTTP_GET_VARS[$var_name])) && (!isvoid($HTTP_GET_VARS[$var_name])))
  944. {
  945. if (get_magic_quotes_gpc())
  946. {
  947. if (is_array($HTTP_GET_VARS[$var_name]))
  948. {
  949. $this->remove_magic_quotes($HTTP_GET_VARS[$var_name]);
  950. return $HTTP_GET_VARS[$var_name];
  951. }
  952. else return stripslashes($HTTP_GET_VARS[$var_name]);
  953. }
  954. else return $HTTP_GET_VARS[$var_name];
  955. }
  956. }
  957. }
  958. else if (strtoupper($value) == "COOKIE")
  959. {
  960. if (isset($_COOKIE))
  961. {
  962. if ((isset($_COOKIE[$var_name])) && (!isvoid($_COOKIE[$var_name])))
  963. {
  964. if (get_magic_quotes_gpc())
  965. {
  966. if (is_array($_COOKIE[$var_name]))
  967. {
  968. $this->remove_magic_quotes($_COOKIE[$var_name]);
  969. return $_COOKIE[$var_name];
  970. }
  971. else return stripslashes($_COOKIE[$var_name]);
  972. }
  973. else return $_COOKIE[$var_name];
  974. }
  975. }
  976. else
  977. {
  978. if ((isset($HTTP_COOKIE_VARS[$var_name])) && (!isvoid($HTTP_COOKIE_VARS[$var_name])))
  979. {
  980. if (get_magic_quotes_gpc())
  981. {
  982. if (is_array($HTTP_COOKIE_VARS[$var_name]))
  983. {
  984. $this->remove_magic_quotes($HTTP_COOKIE_VARS[$var_name]);
  985. return $HTTP_COOKIE_VARS[$var_name];
  986. }
  987. else return stripslashes($HTTP_COOKIE_VARS[$var_name]);
  988. }
  989. else return $HTTP_COOKIE_VARS[$var_name];
  990. }
  991. }
  992. }
  993. else if (strtoupper($value) == "SESSION")
  994. {
  995. if (isset($_SESSION))
  996. {
  997. if ((isset($_SESSION[$var_name])) && (!isvoid($_SESSION[$var_name])))
  998. return $_SESSION[$var_name];
  999. }
  1000. else
  1001. {
  1002. if ((isset($HTTP_SESSION_VARS)) && (!isvoid($HTTP_SESSION_VARS)))
  1003. return $HTTP_SESSION_VARS[$var_name];
  1004. }
  1005. }
  1006. else if ((strtoupper($value) == "DBSESSION") && ($this->m_dbsession_handler))
  1007. {
  1008. $SID = $this->m_dbsession_id;
  1009. if (empty($SID))
  1010. {
  1011. $this->show_error("get_user_var", "dbsession_id is missing (DBSESSION)");
  1012. }
  1013. $tmp_rc = $this->dbsession_read($SID,$var_name);
  1014. if (isset($tmp_rc)) return $tmp_rc;
  1015. }
  1016. }
  1017. return $rc;
  1018. }
  1019. /**
  1020. * search needle in multi array
  1021. *
  1022. * @param string $needle
  1023. * @param array $haystack
  1024. * @return bool
  1025. */
  1026. function in_multi_array($needle, $haystack)
  1027. {
  1028. $rc = false;
  1029. if(is_array($haystack))
  1030. {
  1031. if(in_array($needle, $haystack))
  1032. {
  1033. $rc = true;
  1034. }
  1035. else
  1036. {
  1037. for($i = 0; $i<sizeof($haystack); $i++)
  1038. {
  1039. if(is_array($haystack[$i]))
  1040. {
  1041. if($this->in_multi_array($needle, $haystack[$i]))
  1042. {
  1043. $rc = true;
  1044. break;
  1045. }
  1046. }
  1047. }
  1048. }
  1049. }
  1050. return $rc;
  1051. }
  1052. /**
  1053. * Removes duplicate values from an array (recursive)
  1054. * (this function is much slower than the internal php function)
  1055. *
  1056. * @param array $thearray array to unique
  1057. * @return array
  1058. */
  1059. function array_unique(&$thearray)
  1060. {
  1061. sort($thearray);
  1062. reset($thearray);
  1063. $newarray = array();
  1064. $i = 0;
  1065. $element = current($thearray);
  1066. for ($n=0;$n<sizeof($thearray);$n++)
  1067. {
  1068. if (next($thearray) != $element)
  1069. {
  1070. $newarray[$i] = $element;
  1071. $element = current($thearray);
  1072. $i++;
  1073. }
  1074. }
  1075. return $newarray;
  1076. }
  1077. /**
  1078. * sort array (subfunction for usort)
  1079. *
  1080. * @param array $a keys
  1081. * @param array $b values
  1082. * @return array
  1083. */
  1084. function array_sort($a, $b)
  1085. {
  1086. $as = strtoupper(trim($a[1]));
  1087. $bs = strtoupper(trim($b[1]));
  1088. $as = strtr($as, "ä", "A");
  1089. $as = strtr($as, "ö", "O");
  1090. $as = strtr($as, "ü", "U");
  1091. $as = strtr($as, "ß", "S");
  1092. $bs = strtr($bs, "Ä", "A");
  1093. $bs = strtr($bs, "Õ", "O");
  1094. $bs = strtr($bs, "Ü", "U");
  1095. if ($as == $bs) return 0;
  1096. else return ($as > $bs)?1:-1;
  1097. }
  1098. /**
  1099. *reverse sort array (subfunction for usort)
  1100. *
  1101. * @param array $a keys
  1102. * @param array $b values
  1103. * @return array
  1104. */
  1105. function array_rsort($a, $b)
  1106. {
  1107. $as = strtoupper(trim($a[1]));
  1108. $bs = strtoupper(trim($b[1]));
  1109. $as = strtr($as, "ä", "A");
  1110. $as = strtr($as, "ö", "O");
  1111. $as = strtr($as, "ü", "U");
  1112. $as = strtr($as, "ß", "S");
  1113. $bs = strtr($bs, "Ä", "A");
  1114. $bs = strtr($bs, "Õ", "O");
  1115. $bs = strtr($bs, "Ü", "U");
  1116. if ($as == $bs) return 0;
  1117. else return ($as < $bs)?1:-1;
  1118. }
  1119. /**
  1120. * the current date
  1121. *
  1122. * @param char $date_format see member variables $this->date_format below
  1123. * @return date
  1124. */
  1125. function currentDate($date_format=null)
  1126. {
  1127. if ((!isset($date_format)) || (empty($date_format))) $date_format = $this->date_format;
  1128. $day = date("d");
  1129. $month = date("m");
  1130. $year = date("Y");
  1131. if ($date_format=="I") return $month.$this->m_sep_i.$day .$this->m_sep_i.$year;
  1132. else if ($date_format=="S") return $year .$this->m_sep_s.$month.$this->m_sep_s.$day;
  1133. else if ($date_format=="C") return $year .$month. $day;
  1134. else return $day .$this->m_sep_l.$month.$this->m_sep_l.$year;
  1135. }
  1136. /**
  1137. * the current time
  1138. *
  1139. * @param char $time_format see member variable $this->time_format below
  1140. * @return time
  1141. */
  1142. function currentTime($time_format=null)
  1143. {
  1144. if ((!isset($time_format)) || (empty($time_format))) $time_format = $this->time_format;
  1145. if ($time_format=="Y")
  1146. return date("H").":".date("i").":".date("s");
  1147. else
  1148. return date("h").":".date("i").":".date("s")." ".date("a");
  1149. }
  1150. /**
  1151. * Get date from db-date format
  1152. * (short form of convDate)
  1153. *
  1154. * @param string $dbdate
  1155. * @param bool $short_mode
  1156. * @return string
  1157. */
  1158. function show_dbdate($dbdate, $short_mode=true)
  1159. {
  1160. if ($short_mode==true)
  1161. {
  1162. return $this->convDate($dbdate, 'S', $this->date_format, array('short_mode'=>true));
  1163. }
  1164. else
  1165. {
  1166. return $this->convDate($dbdate, 'S', $this->date_format);
  1167. }
  1168. }
  1169. /**
  1170. * Get Datetime from db-date format
  1171. * (short form of convDate)
  1172. *
  1173. * @param string $dbdate
  1174. * @param bool $with_seconds
  1175. * @param bool $short_mode
  1176. * @param string $at
  1177. * @return string
  1178. */
  1179. function show_dbdatetime($dbdate, $with_seconds=false, $short_mode=true, $at=null)
  1180. {
  1181. return $this->convDateTime($dbdate, $this->date_format, $this->time_format, $with_seconds, $short_mode, $at);
  1182. }
  1183. /**
  1184. * Removes all whitespaces in a string
  1185. *
  1186. * @param string $str
  1187. * @return string
  1188. */
  1189. function killSpace($str)
  1190. {
  1191. $rc = htmlentities($str);
  1192. $rc = str_replace("&nbsp;","",$rc);
  1193. $rc = str_replace("&#160;","",$rc);
  1194. $rc = str_replace(" ","",$rc);
  1195. return $rc;
  1196. }
  1197. /**
  1198. * validate given date
  1199. *
  1200. * @param mixed $timestamp array of date for validating or timestamp
  1201. * array keys: timestamp['year']
  1202. * timestamp['month']
  1203. * timestamp['day']
  1204. * timestamp['hour']
  1205. * timestamp['minute']
  1206. * timestamp['second']
  1207. * @param bool $mode if true, input date is a timestamp {default: false}
  1208. * @return timestamp timestamp with valid date, if invalid input datas, return timestamp will
  1209. * be return the nearest valid date
  1210. */
  1211. function validate_timestamp($timestamp,$mode=false)
  1212. {
  1213. if (!$mode)
  1214. {
  1215. $timestamp = mktime($timestamp['hour'],
  1216. $timestamp['minute'],
  1217. $timestamp['second'],
  1218. $timestamp['month'],
  1219. $timestamp['day'],
  1220. $timestamp['year']);
  1221. }
  1222. if ($timestamp<mktime(2,0,0,1,1,1970))
  1223. return mktime(2,0,0,1,1,1970);
  1224. else if ($timestamp>mktime(2,0,0,12,31,2037))
  1225. return mktime(2,0,0,12,31,2037);
  1226. else
  1227. return $timestamp;
  1228. }
  1229. /**
  1230. * calculates days to a given date
  1231. *
  1232. * @param date $start_date first date
  1233. * @param date $end_date last date
  1234. * @param char $date_format see member variables $this->date_format below
  1235. * @return int number of days, if date is out of range -1
  1236. */
  1237. function date2days($start_date, $end_date, $date_format="S")
  1238. {
  1239. if ($date_format!="S")
  1240. {
  1241. $start_date = $this->convDate($start_date, $date_format, 'S');
  1242. $end_date = $this->convDate($end_date, $date_format, 'S');
  1243. }
  1244. $start_year = strtok($start_date,"-");
  1245. $start_month = strtok("-");
  1246. $start_day = strtok("-");
  1247. $end_year = strtok($end_date,"-");
  1248. $end_month = strtok("-");
  1249. $end_day = strtok("-");
  1250. if ($this->file_exists("Date.php"))
  1251. require_once("Date.php");
  1252. else
  1253. {
  1254. $this->show_error("date2days", "Pear \"Date Package\" required.");
  1255. }
  1256. return Date_Calc::dateDiff($start_day,$start_month,$start_year,$end_day,$end_month,$end_year);
  1257. }
  1258. /**
  1259. * return file extension
  1260. *
  1261. * @param string $filename
  1262. * @return string
  1263. */
  1264. function fileExtension($filename)
  1265. {
  1266. $rc="";
  1267. $max_length = strlen($filename);
  1268. $counter=0;
  1269. while($max_length>0)
  1270. {
  1271. $ch = $filename[$max_length];
  1272. if ($ch == ".")
  1273. break;
  1274. $rc .= $ch;
  1275. $max_length--;
  1276. $counter++;
  1277. }
  1278. if ($rc!="") $rc = strrev($rc);
  1279. return $rc;
  1280. }
  1281. /**
  1282. * convert string to number while converting old seperator with "."
  1283. *
  1284. * @param string $value string to convert {default: ","}
  1285. * @param string $sep_old old seperator
  1286. * @return float
  1287. */
  1288. function convToNum($value,$sep_old=",")
  1289. {
  1290. $rc = "";
  1291. if (empty($sep_old))
  1292. {
  1293. $this->show_error("convtoNum", "missing parameter sep_old");
  1294. }
  1295. $st = trim($value);
  1296. $sep_new = ".";
  1297. if (!isvoid($st))
  1298. {
  1299. if ($this->checkNumber($st,$sep_old))
  1300. {
  1301. if ($sep_old != $sep_new)
  1302. {
  1303. if ($this->checkNumber($st,$sep_old))
  1304. {
  1305. $num_sep = substr_count($st,$sep_old);
  1306. if ($num_sep==1)
  1307. $rc = str_replace($sep_old,$sep_new,$st);
  1308. else
  1309. $rc = $st;
  1310. }
  1311. }
  1312. else
  1313. $rc = $st;
  1314. }
  1315. }
  1316. return $rc;
  1317. }
  1318. /**
  1319. * convert seperator in number and fill number to specified length
  1320. *
  1321. * @param float $st number to convert
  1322. * @param char $sep_new new seperator {default: "."}
  1323. * @param int $precision numbers behind seperator {default: 0 }
  1324. * (filling with 0; this function doesn't cut or round numbers)
  1325. * @param string $group if set, this group seperator will be used
  1326. * @return string converted string with number
  1327. */
  1328. function convnumSep($st, $sep_new=".", $precision=0, $group=null)
  1329. {
  1330. $rc="";
  1331. if (!isvoid($st))
  1332. {
  1333. if ($this->checkNumber($st))
  1334. {
  1335. $sep_old = ".";
  1336. $num_sep = substr_count($st,$sep_old);
  1337. if (($num_sep==0) && ($precision>0)) // no old seperator
  1338. {
  1339. $tmp_st = $st.$sep_new;
  1340. for ($ix=0;$ix<$precision;$ix++)
  1341. $tmp_st .= "0";
  1342. $rc = $tmp_st;
  1343. }
  1344. else // with old seperator
  1345. {
  1346. $ln = strlen($st);
  1347. $pos = strrpos($st, $sep_old);
  1348. $ln_ext = $ln-$pos-1;
  1349. if ($ln_ext<$precision)
  1350. {
  1351. $ln_miss = $precision - $ln_ext;
  1352. $tmp_st=$st;
  1353. for ($ix=0;$ix<$ln_miss;$ix++)
  1354. $tmp_st .= "0";
  1355. $rc = str_replace($sep_old, $sep_new, $tmp_st);
  1356. }
  1357. else
  1358. {
  1359. $rc = str_replace($sep_old, $sep_new, $st);
  1360. }
  1361. }
  1362. // fill group seperator, if defined
  1363. if (isset($group))
  1364. {
  1365. if ($rc[0]=="-") $g_rc = substr($rc, 1);
  1366. else $g_rc = $rc;
  1367. $ln = strlen($g_rc); // get new length
  1368. $pos = strrpos($g_rc, $sep_new);
  1369. if ($pos>3)
  1370. {
  1371. $suffix_ln = $ln-$pos+1;
  1372. $prefix_ln = $ln-$suffix_ln+1;
  1373. if ($prefix_ln>3)
  1374. {
  1375. $tmp_rc = "";
  1376. $prefix = strrev(substr($g_rc, 0, $prefix_ln));
  1377. for ($ix=0; $ix<$prefix_ln; $ix++)
  1378. {
  1379. if (($ix>0) && ($ix%3==0)) $tmp_rc .= $group;
  1380. $tmp_rc .= $prefix[$ix];
  1381. }
  1382. $g_rc = strrev($tmp_rc).substr($g_rc, $pos);
  1383. }
  1384. }
  1385. if ($rc[0]=="-") $rc = "-".$g_rc;
  1386. else $rc = $g_rc;
  1387. }
  1388. }
  1389. }
  1390. else
  1391. $rc = $st;
  1392. return $rc;
  1393. }
  1394. /**
  1395. * validate time format and convert it into right format
  1396. *
  1397. * @param string $time time (or date) to convert, if empty NOW will be used
  1398. * @param char $format if empty or time, e.g. 19:45:59
  1399. * datetime, e.g. 2002-07-20 21:02:55
  1400. * @return string converted time, if wrong input false
  1401. */
  1402. function convToTimestamp($time="",$format="")
  1403. {
  1404. $timestamp = "";
  1405. if ((isvoid($format)) || ($format=="time")) // only time
  1406. {
  1407. if (isvoid($time)) $timestamp = strtotime("now");
  1408. else
  1409. {
  1410. $t_hours = strtok(trim($time),":");
  1411. $t_min = strtok(":");
  1412. $t_sec = strtok(":");
  1413. $t_day = date("d");
  1414. $t_month = date("m");
  1415. $t_year = date("Y");
  1416. $tmp_stamp = mktime($t_hours,$t_min,$t_sec,$t_month,$t_day,$t_year);
  1417. if ($tmp_stamp>0)
  1418. $timestamp = $tmp_stamp;
  1419. }
  1420. }
  1421. if (($format=="datetime") && (!isvoid($time))) // datetime
  1422. {
  1423. $tdate = trim(strtok(trim($time)," "));
  1424. $ttime = trim(strtok(" "));
  1425. $t_year = strtok($tdate,"-");
  1426. $t_month = strtok("-");
  1427. $t_day = strtok("-");
  1428. $t_hours = strtok($ttime,":");
  1429. $t_min = strtok(":");
  1430. $t_sec = strtok(":");
  1431. $tmp_stamp = mktime($t_hours,$t_min,$t_sec,$t_month,$t_day,$t_year);
  1432. if ($tmp_stamp>0)
  1433. $timestamp = $tmp_stamp;
  1434. }
  1435. return $timestamp;
  1436. }
  1437. /**
  1438. * validate time format and convert it into right format
  1439. *
  1440. * @param string $time time to convert
  1441. * @param char $time_format see member variable $this->time_format above
  1442. * @param bool $with_seconds if false, don't return seconds {default: true}
  1443. * @return string converted time, if wrong input false
  1444. */
  1445. function convTime($time, $time_format=null, $with_seconds=true)
  1446. {
  1447. $rc="";
  1448. if ((!isset($time_format)) || (empty($time_format))) $time_format = $this->time_format;
  1449. $ln = strlen($time);
  1450. if ($ln == 5)
  1451. {
  1452. $part1 = strtok($time,":");
  1453. $part2 = strtok(":");
  1454. if ((strlen($part1)==2) && (strlen($part2)==2))
  1455. {
  1456. if (($part1>=0) && ($part1<25) && ($part2>=0) && ($part2<60))
  1457. {
  1458. if ($time_format=="N") // 12 hours
  1459. {
  1460. if ($part1>=12)
  1461. {
  1462. $part1 = $part1-12;
  1463. $end = "&nbsp;pm";
  1464. }
  1465. else
  1466. {
  1467. $part1 += 0;
  1468. $end = "&nbsp;am";
  1469. }
  1470. if ($part1==0)
  1471. $part1 = 12;
  1472. $rc = $part1 . ":" . $part2.$end;
  1473. }
  1474. else // 24 hours
  1475. {
  1476. $rc = $part1 . ":" . $part2;
  1477. }
  1478. }
  1479. }
  1480. }
  1481. else if ($ln >= 8)
  1482. {
  1483. $part1 = strtok($time,":");
  1484. $part2 = strtok(":");
  1485. $part3 = strtok(":");
  1486. if (strlen($part3)>2) $part3 = substr($part3, 0, 2);
  1487. if ((strlen($part1)==2) && (strlen($part2)==2) && (strlen($part3)==2))
  1488. {
  1489. if (($part1>=0) && ($part1<25) && ($part2>=0) && ($part2<60) && ($part3>=0) && ($part3<60))
  1490. {
  1491. if ($time_format=="N") // 12 hours
  1492. {
  1493. if ($part1>12)
  1494. {
  1495. $part1 = $part1-12;
  1496. $end = "&nbsp;pm";
  1497. }
  1498. else
  1499. {
  1500. $part1 += 0;
  1501. $end = "&nbsp;am";
  1502. }
  1503. $rc = $part1 . ":" .$part2;
  1504. if ($with_seconds) $rc .= ":".$part3;
  1505. $rc .= $end;
  1506. }
  1507. else // 24 hours
  1508. {
  1509. $rc = $part1 . ":" .$part2;
  1510. if ($with_seconds) $rc .= ":".$part3;
  1511. }
  1512. }
  1513. }
  1514. }
  1515. return $rc;
  1516. }
  1517. /**
  1518. * convert datetime format
  1519. *
  1520. * @param string $datetime db datetime field format
  1521. * @param char $date_format see member variable m_date above
  1522. * @param char $time_format see member variable m_time above
  1523. * @param bool $with_seconds show seconds
  1524. * @param bool $short_mode if today, yesterday or tomorrow
  1525. * show string for date
  1526. * @param string $at
  1527. * @return string datetime in converted format
  1528. */
  1529. function convDateTime($datetime, $date_format="", $time_format="", $with_seconds=false, $short_mode=true, $at=null)
  1530. {
  1531. $rc="";
  1532. if (!isvoid($datetime))
  1533. {
  1534. if ((!isset($date_format)) || (empty($date_format))) $date_format = $this->date_format;
  1535. if ((!isset($time_format)) || (empty($time_format))) $time_format = $this->time_format;
  1536. $tmp_date = strtok($datetime, " ");
  1537. $tmp_time = strtok(" ");
  1538. if (($short_mode) && (!isvoid($tmp_time)))
  1539. $rc = $this->convDate($tmp_date, 'S', $date_format, array('short_mode'=>true));
  1540. else
  1541. $rc = $this->convDate($tmp_date, 'S', $date_format);
  1542. if (!isvoid($tmp_time))
  1543. {
  1544. if (isset($at)) $tmp_at = $at;
  1545. else $tmp_at = STR_AT;
  1546. if ($tmp_at!=",") $rc .= "&nbsp;";
  1547. $rc .= $tmp_at."&nbsp;";
  1548. $rc .= $this->convTime($tmp_time, $time_format, $with_seconds);
  1549. }
  1550. }
  1551. return $rc;
  1552. }
  1553. /**
  1554. * Convert date format
  1555. *
  1556. * @param string $date_str date to convert
  1557. * @param string $src_format see member variables $this->date_format above
  1558. * @param string $dest_format see member variables $this->date_format above or 'short' or 'long'
  1559. * @param array $customize year_format string = long : e.g. 2002 {default}
  1560. * short: e.g. 02
  1561. * void : e.g.
  1562. * leading_zeros bool = true : e.g. 01 {default}
  1563. * false: e.g. 1
  1564. * with_weekday bool = true: with weekday
  1565. * false: without weekdays
  1566. * (only available if dest_format is short or long)
  1567. * locale string = see member variables m_locale above
  1568. * first_valid_year = first valid year, if specified date is before, it will be signed
  1569. * as invalid {default: 1850}
  1570. * short_mode = if today, yesterday or tomorrow
  1571. * show string for date {default: false}
  1572. * @return string date or {if wrong input) empty
  1573. */
  1574. function convDate($date_str, $src_format, $dest_format, $customize=null)
  1575. {
  1576. $rc = "";
  1577. $sep_old = "";
  1578. $sep_new = "";
  1579. $date_package = false;
  1580. // all years are possible, but year 0 cannot be selected (does anyone need it?)
  1581. if ((!isset($customize['first_valid_year'])) || (intval($customize['first_valid_year'])==0))
  1582. $customize['first_valid_year'] = 1850;
  1583. if ($this->file_exists("Date.php"))
  1584. {
  1585. require_once("Date.php");
  1586. $date_package = true;
  1587. }
  1588. if ((isset($customize['locale'])) && (!setlocale(LC_TIME,$customize['locale'])))
  1589. {
  1590. $this->show_error("convDate", "locale not readable from system!");
  1591. }
  1592. if (strtoupper($src_format)=="L") // DD.MM.YYYY
  1593. {
  1594. $sep_old = $this->m_sep_l;
  1595. $day = intval(strtok($date_str,$sep_old));
  1596. $month = intval(strtok($sep_old));
  1597. $year = intval(strtok($sep_old));
  1598. if ($year<20) $year += 2000;
  1599. else if ($year<100) $year += 1900;
  1600. }
  1601. else if (strtoupper($src_format)=="I") // MM/DD/YYYY
  1602. {
  1603. $sep_old = $this->m_sep_i;
  1604. $month = intval(strtok($date_str,$sep_old));
  1605. $day = intval(strtok($sep_old));
  1606. $year = intval(strtok($sep_old));
  1607. if ($year<20) $year += 2000;
  1608. else if ($year<100) $year += 1900;
  1609. }
  1610. else if (strtoupper($src_format)=="S") // YYYY-MM-DD
  1611. {
  1612. $sep_old = $this->m_sep_s;
  1613. $year = intval(strtok($date_str,$sep_old));
  1614. $month = intval(strtok($sep_old));
  1615. $day = intval(strtok($sep_old));
  1616. if ($year<20) $year += 2000;
  1617. else if ($year<100) $year += 1900;
  1618. }
  1619. else if (strtoupper($src_format)=="C") // YYYYMMDD
  1620. {
  1621. if (strlen($date_str)==8)
  1622. {
  1623. $year = substr($date_str,0,4);
  1624. $month = substr($date_str,4,2);
  1625. $day = substr($date_str,6,2);
  1626. }
  1627. else
  1628. {
  1629. $year = substr($date_str,0,2);
  1630. $month = substr($date_str,2,2);
  1631. $day = substr($date_str,4,2);
  1632. }
  1633. if ($year<20) $year += 2000;
  1634. else if ($year<100) $year += 1900;
  1635. }
  1636. if (strtoupper($dest_format)=="I") $sep_new = $this->m_sep_i;
  1637. else if (strtoupper($dest_format)=="S") $sep_new = $this->m_sep_s;
  1638. else if (strtoupper($dest_format)=="L") $sep_new = $this->m_sep_l;
  1639. else if (strtoupper($dest_format)=="C") $sep_new = "";
  1640. else $sep_new = $this->m_sep_s;
  1641. if ((checkdate($month,$day,$year)) && ($customize['first_valid_year']<=$year))
  1642. {
  1643. $short_mode = false;
  1644. if ((isset($customize['short_mode'])) && ($customize['short_mode']))
  1645. {
  1646. if (date("Y-n-j")=="$year-$month-$day")
  1647. {
  1648. $rc = STR_TODAY;
  1649. $short_mode=true;
  1650. }
  1651. else if (date("Y-n-j", mktime(date('H'),date('i'), 0,date('m'), date('d')+1,date('Y')))=="$year-$month-$day")
  1652. {
  1653. $rc = STR_TOMORROW;
  1654. $short_mode=true;
  1655. }
  1656. else if (date("Y-n-j", mktime(date('H'),date('i'), 0,date('m'), date('d')-1,date('Y')))=="$year-$month-$day")
  1657. {
  1658. $rc = STR_YESTERDAY;
  1659. $short_mode=true;
  1660. }
  1661. }
  1662. if ($short_mode==false)
  1663. {
  1664. if (isset($customize['year_format']))
  1665. {
  1666. $y_form = $customize['year_format'];
  1667. if ($y_form=="short") $year = substr($year,strlen($year)-2,2);
  1668. else if ($y_form=="void") $year = "";
  1669. }
  1670. $day = sprintf("%02d",$day);
  1671. $month = sprintf("%02d",$month);
  1672. if ((isset($customize['leading_zeros'])) && (!empty($sep_new)))
  1673. {
  1674. $l_zeros = $customize['leading_zeros'];
  1675. if (!$l_zeros)
  1676. {
  1677. $day = intval($day);
  1678. $month = intval($month);
  1679. }
  1680. }
  1681. else if (empty($sep_new))
  1682. {
  1683. if (strlen($day)<2) $day .= "0";
  1684. if (strlen($month)<2) $month .= "0";
  1685. }
  1686. if (($dest_format=="long") || ($dest_format=="short"))
  1687. {
  1688. if ($date_package)
  1689. {
  1690. $stamp = new Date();
  1691. $stamp->setDate("$year-$month-$day",DATE_FORMAT_ISO);
  1692. }
  1693. $wday = "";
  1694. if (isset($customize['with_weekday']))
  1695. {
  1696. $with_wday = $customize['with_weekday'];
  1697. if ($with_wday)
  1698. {
  1699. if ($date_package)
  1700. {
  1701. if ($dest_format=="long") $wday = $stamp->format("%A").", ";
  1702. else $wday = $stamp->format("%a").", ";
  1703. }
  1704. else
  1705. {
  1706. $this->show_error("convDate", "Pear \"Date Package\" required for specificed parameters.");
  1707. }
  1708. }
  1709. }
  1710. if ($date_package)
  1711. {
  1712. if ($dest_format=="long") $month = $stamp->format("%B");
  1713. else $month = $stamp->format("%b");
  1714. }
  1715. else
  1716. {
  1717. $this->show_error("convDate", "Pear \"Date Package\" required for specificed parameters.");
  1718. }
  1719. if ($this->m_date=="I") $rc = $wday.$month."/".$day."/".$year;
  1720. else $rc = $wday.$day.". ".$month." ".$year;
  1721. }
  1722. else
  1723. {
  1724. if ($sep_new==$this->m_sep_i)
  1725. {
  1726. $rc = $month.$this->m_sep_i.$day;
  1727. if (!isvoid($year))
  1728. $rc .= $this->m_sep_i.$year;
  1729. }
  1730. else if ($sep_new==$this->m_sep_s)
  1731. {
  1732. if (!isvoid($year))
  1733. $rc .= $year.$this->m_sep_s;
  1734. $rc .= $month.$this->m_sep_s.$day;
  1735. }
  1736. else if ($sep_new==$this->m_sep_l)

Large files files are truncated, but you can click here to view the full file