phpgroupware-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Phpgroupware-cvs] phpgwapi/inc class.shm.inc.php


From: Dave Hall
Subject: [Phpgroupware-cvs] phpgwapi/inc class.shm.inc.php
Date: Tue, 15 Aug 2006 01:22:51 +0000

CVSROOT:        /cvsroot/phpgwapi
Module name:    phpgwapi
Changes by:     Dave Hall <skwashd>     06/08/15 01:22:51

Modified files:
        inc            : class.shm.inc.php 

Log message:
        fix ftok error when using open_basedir, added some basic docs, renamed 
constants to avoid conflicts, a couple of small code cleanups

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/phpgwapi/inc/class.shm.inc.php?cvsroot=phpgwapi&r1=1.4&r2=1.5

Patches:
Index: class.shm.inc.php
===================================================================
RCS file: /cvsroot/phpgwapi/phpgwapi/inc/class.shm.inc.php,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- class.shm.inc.php   19 Jul 2006 22:12:23 -0000      1.4
+++ class.shm.inc.php   15 Aug 2006 01:22:51 -0000      1.5
@@ -63,7 +63,7 @@
        * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
        * @package phpgwapi
        * @subpackage application
-       * @version $Id: class.shm.inc.php,v 1.4 2006/07/19 22:12:23 sigurdne 
Exp $
+       * @version $Id: class.shm.inc.php,v 1.5 2006/08/15 01:22:51 skwashd Exp 
$
        */
 
        class shm
@@ -73,26 +73,43 @@
                 */
                var $hashid;
                
+               /**
+               * @constructor
+               */
                function shm()
                {
 
-                       DEFINE('CACHE_SECONDS', 60 * 60);
-                       DEFINE('LOG', false);
-                       DEFINE('LOG_FILE', '/tmp/debug.log' );
-                       DEFINE('HASH_PRIME', 2147483647); //2^31 -1
-                       DEFINE('LOCK', '/');
-               //      DEFINE('HASHID', 'hash_table_key');
+                       define('PHPGW_SHM_CACHE_SECONDS', 60 * 60);
+                       define('PHPGW_SHM_LOG', false);
+                       define('PHPGW_SHM_LOG_FILE', '/tmp/debug.log' );
+                       define('PHPGW_SHM_HASH_PRIME', 2147483647); //2^31 -1
+                       define('PHPGW_SHM_LOCK', 
$GLOBALS['phpgw_info']['server']['temp_dir']);
+               //      DEFINE('PHPGW_SHM_HASHID', 'hash_table_key');
                }
  
+               /**
+               * Log a message
+               *
+               * @param string $log_string - the message to be logged
+               * @todo switch to the API logging class
+               */
                function log_this($log_string)
                {
-                       if(!LOG) return;
-                       $file = fopen(LOG_FILE, "a");
+                       if(!PHPGW_SHM_LOG)
+                       {
+                               return;
+                       }
+                       $file = fopen(PHPGW_SHM_LOG_FILE, "a");
                        fwrite($file, $log_string);
                        fclose($file);
                        return;
                }
 
+               /**
+               * Delete a block from memory
+               *
+               * @param int $id memory block id
+               */
                function delete_mem($id)
                {
                        if ( (int) $id )
@@ -104,11 +121,24 @@
                        }
                }
 
+               /**
+               * Read a block from memory
+               *
+               * @param int $id memory block id
+               * @return string the data from memory block
+               */
                function &read_mem($id)
                {
                        return shmop_read($id, 0, shmop_size($id));
                }
 
+               /**
+               * Write data to a block of memory
+               *
+               * @param int $id block id to store data at
+               * @param string $data the data to store
+               * @return bool was the data written to memory ?
+               */
                function write_mem($id, $data)
                {
                        if(shmop_size($id)< strlen($data))
@@ -124,6 +154,14 @@
                        return true;
                }
 
+               /**
+               * Create a shared memory segment
+               *
+               * @internal shouldn't the perms really be 0600 ? skwashd 
20060815
+               * @param ?? $key the key for the memory allocation
+               * @param int $size the size of the memory allocation being 
requested (in bytes)
+               * @return int the id of the memory block allocated
+               */
                function create_mem($key, $size)
                {
                        $id = @shmop_open($key, "n", 0644, $size);
@@ -134,6 +172,13 @@
                        return $id;
                }
 
+               /**
+               * Check to see if a memory block is already allocated
+               * 
+               * @internal php.net/shmop_open suggests using shmop_open($key, 
'a', 0, 0); for an existing block - skwashd 200608015
+               * @param ??? $key the key for the memory allocaiton
+               * @return int the id of the memory block - 0 when not found
+               */
                function mem_exist($key)
                {
                        if(!$id = @shmop_open($key, "a", 0644, 100))
@@ -145,11 +190,24 @@
                        return $id;
                }
 
+               /**
+               * Close a memory allocation - this does not delete it the 
allocation - call shm::delete_mem first
+               *
+               * @param int $id the memory allocation id
+               */
                function close_mem($id)
                {
-                       if($id!=0) shmop_close($id);
+                       if( $id != 0)
+                       {
+                               shmop_close($id);
+                       }
                }
 
+               /**
+               * Get a value from memory
+               *
+               * @todo document me properly
+               */
                function &get_value($key)
                {
                        $hash_id =& $this->hash($key);
@@ -173,9 +231,14 @@
                        return $value['value'];
                }
 
+               /**
+               * Store a value in memory
+               *
+               * @todo document me properly
+               */
                function store_value($key,$value)
                {
-                       $SHM_KEY = ftok(LOCK, 'R');
+                       $SHM_KEY = ftok(PHPGW_SHM_LOCK, 'R');
                        $shmid = @sem_get($SHM_KEY, 1024, 0644 | IPC_CREAT);
                        sem_acquire($shmid);
                        $hash_id = $this->hash($key);
@@ -212,9 +275,14 @@
                        return $hash_id;
                }
 
+               /**
+               * Update keys
+               *
+               * @todo document me properly
+               */
                function update_keys($key, $id)
                {
-                       $SHM_KEY = ftok(LOCK, 'R');
+                       $SHM_KEY = ftok(PHPGW_SHM_LOCK, 'R');
                        $shmid = @sem_get($SHM_KEY, 1024, 0644 | IPC_CREAT);
                        sem_acquire($shmid);
                        $temp =& $this->get_value($this->hashid);
@@ -223,25 +291,42 @@
                        sem_release($shmid);
                }
 
+               /**
+               * Create a one way hash of a value
+               *
+               * @param string $hash_string the string to encrypt
+               * @return string the encrypted hash
+               */
                function &hash($hash_string)
                {
-                       $hash =& fmod(hexdec(md5($hash_string)), HASH_PRIME);
+                       $hash =& fmod(hexdec(md5($hash_string)), 
PHPGW_SHM_HASH_PRIME);
                        //$this->log_this("Hashing " . $hash_string . " to " . 
$hash . "\n"); 
                        return $hash;
                }
 
+               /**
+               * Cache the contents at the end of a request ?
+               *
+               * @param string $content the contents to cache
+               * @return string the contents which was cached
+               */
                function cache_end($contents)
                {
                        if(trim($contents))
                        {
                                $datasize = strlen($contents);
-                               $hash_string = 
"http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"].serialize($_POST);
+                               $hash_string = 
"http://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}" . serialize($_POST);
                                $shmid = 
$this->store_value($hash_string,$contents);
                                $this->update_keys($hash_string,$shmid);
                        }
                        return $contents; //display
                }
 
+               /**
+               * Delete an entry from the cache
+               *
+               * @
+               */
                function delete_key($key)
                {
                        if(!function_exists('ftok'))
@@ -249,7 +334,7 @@
                                return;
                        }
                        
-                       $SHM_KEY = ftok(LOCK, 'R');
+                       $SHM_KEY = ftok(PHPGW_SHM_LOCK, 'R');
                        $shmid = @sem_get($SHM_KEY, 1024, 0644 | IPC_CREAT);
 
                        sem_acquire($shmid);
@@ -267,9 +352,14 @@
                        sem_release($shmid);
                }
 
+               /**
+               * Clear all values from the cache?
+               *
+               * @todo document me properly
+               */
                function clear_cache()
                {
-                       $SHM_KEY = ftok(LOCK, 'R');
+                       $SHM_KEY = ftok(PHPGW_SHM_LOCK, 'R');
                        $shmid = @sem_get($SHM_KEY, 1024, 0644 | IPC_CREAT);
                        sem_acquire($shmid);
                        $data = $this->get_value($this->hashid);
@@ -286,15 +376,18 @@
                }
 
 
+               /**
+               * Delete stale entries from the cache
+               */
                function garbage_collection()
                {
-                       $SHM_KEY = ftok(LOCK, 'R');
+                       $SHM_KEY = ftok(PHPGW_SHM_LOCK, 'R');
                        $shmid = @sem_get($SHM_KEY, 1024, 0644 | IPC_CREAT);
                        sem_acquire($shmid);
                        $data = $this->get_value($this->hashid);
                        foreach ($data as $k => $v)
                        {
-                               if(time() - $v['time'] > CACHE_SECONDS)
+                               if(time() - $v['time'] > 
PHPGW_SHM_CACHE_SECONDS)
                                {
                                        //$this->log_this("garbage collection 
found expired key $k, value $v[shmid] in hash table... deleting\n");
                                        $id = $this->mem_exist($v['shmid']);
@@ -307,9 +400,12 @@
                        sem_release($shmid);
                }
 
+               /**
+               * Get cached values for current url
+               */
                function do_cache()
                {
-                       $key = 
"http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"].serialize($_POST);
+                       $key = 
"http://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}" . serialize($_POST);
                        $contents =& $this->get_value($key);
                        if($contents)
                        {




reply via email to

[Prev in Thread] Current Thread [Next in Thread]