+/**
+ * Function to start the session and store the cookie with the session_id as
+ * HttpOnly cookie which means that the cookie isn't accessible by javascript
+ * (IE6 only)
+ * Note that as sqsession_is_active() no longer discriminates as to when
+ * it calls this function, session_start() has to have E_NOTICE suppression
+ * (thus the @ sign).
+ */
+function sqsession_start() {
+ global $base_uri;
+
+ sq_call_function_suppress_errors('session_start');
+ // was: @session_start();
+ $session_id = session_id();
+
+ // session_starts sets the sessionid cookie but without the httponly var
+ // setting the cookie again sets the httponly cookie attribute
+ //
+ // need to check if headers have been sent, since sqsession_is_active()
+ // has become just a passthru to this function, so the sqsetcookie()
+ // below is called every time, even after headers have already been sent
+ //
+ if (!headers_sent())
+ sqsetcookie(session_name(),$session_id,false,$base_uri);
+}
+
+
+
+/**
+ * Set a cookie
+ *
+ * @param string $sName The name of the cookie.
+ * @param string $sValue The value of the cookie.
+ * @param int $iExpire The time the cookie expires. This is a Unix
+ * timestamp so is in number of seconds since
+ * the epoch.
+ * @param string $sPath The path on the server in which the cookie
+ * will be available on.
+ * @param string $sDomain The domain that the cookie is available.
+ * @param boolean $bSecure Indicates that the cookie should only be
+ * transmitted over a secure HTTPS connection.
+ * @param boolean $bHttpOnly Disallow JS to access the cookie (IE6 only)
+ * @param boolean $bReplace Replace previous cookies with same name?
+ *
+ * @return void
+ *
+ * @since 1.4.16 and 1.5.1
+ *
+ */
+function sqsetcookie($sName, $sValue='deleted', $iExpire=0, $sPath="", $sDomain="",
+ $bSecure=false, $bHttpOnly=true, $bReplace=false) {
+
+ // if we have a secure connection then limit the cookies to https only.
+ global $is_secure_connection;
+ if ($sName && $is_secure_connection)
+ $bSecure = true;
+
+ // admin config can override the restriction of secure-only cookies
+ global $only_secure_cookies;
+ if (!$only_secure_cookies)
+ $bSecure = false;
+
+ if (false && check_php_version(5,2)) {
+ // php 5 supports the httponly attribute in setcookie, but because setcookie seems a bit
+ // broken we use the header function for php 5.2 as well. We might change that later.
+ //setcookie($sName,$sValue,(int) $iExpire,$sPath,$sDomain,$bSecure,$bHttpOnly);
+ } else {
+ if (!empty($sDomain)) {
+ // Fix the domain to accept domains with and without 'www.'.
+ if (strtolower(substr($sDomain, 0, 4)) == 'www.') $sDomain = substr($sDomain, 4);
+ $sDomain = '.' . $sDomain;
+
+ // Remove port information.
+ $Port = strpos($sDomain, ':');
+ if ($Port !== false) $sDomain = substr($sDomain, 0, $Port);
+ }
+ if (!$sValue) $sValue = 'deleted';
+ header('Set-Cookie: ' . rawurlencode($sName) . '=' . rawurlencode($sValue)
+ . (empty($iExpire) ? '' : '; expires=' . gmdate('D, d-M-Y H:i:s', $iExpire) . ' GMT')
+ . (empty($sPath) ? '' : '; path=' . $sPath)
+ . (empty($sDomain) ? '' : '; domain=' . $sDomain)
+ . (!$bSecure ? '' : '; secure')
+ . (!$bHttpOnly ? '' : '; HttpOnly'), $bReplace);
+ }
+}
+
+
+/**
+ * session_regenerate_id replacement for PHP < 4.3.2
+ *
+ * This code is borrowed from Gallery, session.php version 1.53.2.1
+ */
+if (!function_exists('session_regenerate_id')) {
+
+ function php_combined_lcg() {
+ $tv = gettimeofday();
+ $lcg['s1'] = $tv['sec'] ^ (~$tv['usec']);
+ $lcg['s2'] = mt_rand();
+ $q = (int) ($lcg['s1'] / 53668);
+ $lcg['s1'] = (int) (40014 * ($lcg['s1'] - 53668 * $q) - 12211 * $q);
+ if ($lcg['s1'] < 0) {
+ $lcg['s1'] += 2147483563;
+ }
+ $q = (int) ($lcg['s2'] / 52774);
+ $lcg['s2'] = (int) (40692 * ($lcg['s2'] - 52774 * $q) - 3791 * $q);
+ if ($lcg['s2'] < 0) {
+ $lcg['s2'] += 2147483399;
+ }
+ $z = (int) ($lcg['s1'] - $lcg['s2']);
+ if ($z < 1) {
+ $z += 2147483562;
+ }
+ return $z * 4.656613e-10;
+ }
+
+ function session_regenerate_id() {
+ global $base_uri;
+ $tv = gettimeofday();
+ sqgetGlobalVar('REMOTE_ADDR',$remote_addr,SQ_SERVER);
+ $buf = sprintf("%.15s%ld%ld%0.8f", $remote_addr, $tv['sec'], $tv['usec'], php_combined_lcg() * 10);
+ session_id(md5($buf));
+ if (ini_get('session.use_cookies')) {
+ sqsetcookie(session_name(), session_id(), 0, $base_uri);
+ }
+ return TRUE;
+ }
+}
+
+
+/**
+ * php_self
+ *
+ * Creates an URL for the page calling this function, using either the PHP global
+ * REQUEST_URI, or the PHP global PHP_SELF with QUERY_STRING added. Before 1.5.1
+ * function was stored in function/strings.php.
+ *
+ * @return string the complete url for this page
+ * @since 1.2.3
+ */
+function php_self () {
+ if ( sqgetGlobalVar('REQUEST_URI', $req_uri, SQ_SERVER) && !empty($req_uri) ) {
+ return $req_uri;
+ }
+
+ if ( sqgetGlobalVar('PHP_SELF', $php_self, SQ_SERVER) && !empty($php_self) ) {
+
+ // need to add query string to end of PHP_SELF to match REQUEST_URI
+ //
+ if ( sqgetGlobalVar('QUERY_STRING', $query_string, SQ_SERVER) && !empty($query_string) ) {
+ $php_self .= '?' . $query_string;
+ }
+
+ return $php_self;
+ }
+
+ return '';
+}
+
+
+/**
+ * Print variable
+ *
+ * sm_print_r($some_variable, [$some_other_variable [, ...]]);
+ *
+ * Debugging function - does the same as print_r, but makes sure special
+ * characters are converted to htmlentities first. This will allow
+ * values like <some@email.address> to be displayed.
+ * The output is wrapped in <<pre>> and <</pre>> tags.
+ * Since 1.4.2 accepts unlimited number of arguments.
+ * @since 1.4.1
+ * @return void
+ */
+function sm_print_r() {
+ ob_start(); // Buffer output
+ foreach(func_get_args() as $var) {
+ print_r($var);
+ echo "\n";
+ // php has get_class_methods function that can print class methods
+ if (is_object($var)) {
+ // get class methods if $var is object
+ $aMethods=get_class_methods(get_class($var));
+ // make sure that $aMethods is array and array is not empty
+ if (is_array($aMethods) && $aMethods!=array()) {
+ echo "Object methods:\n";
+ foreach($aMethods as $method) {
+ echo '* ' . $method . "\n";
+ }
+ }
+ echo "\n";
+ }