University of Southern California

soc1c.php

00001 <?php
00002 /***************************************************************************
00003  * soc.php - Basic Objects used in SOC and IUI
00004  *
00005  * @author R. S. Doiel rsdoiel@usc.edu
00006  *
00007  * @copyright copyright (c) 2007 University of Southern California
00008  *
00009  ***************************************************************************/
00010 require_once("WSCORE2.php");
00011 
00012 function fmt($msg, $otag = "",$ctag = "<br />\n") {
00013   if (isset($_SERVER['HTTP_HOST'])) {
00014     return $otag.$msg.$ctag;
00015   } else {
00016     if (strpos($otag,"<li>") !== false) {
00017       return " * ".$msg."\n";
00018     } else {
00019       return $msg."\n";
00020     }
00021   }
00022 }
00023 
00024 
00026 class SOC_Widget extends WSCORE2 {
00027   
00032   function SOC_Widget($conf = NULL) {
00033     if (is_string($conf) && file_exists($conf)) {
00034       SOC_Widget::conf($conf);
00035     } else {
00036       WSCORE2::__construct($conf);
00037     }
00038     $this->version_msg = "SOC1C";
00039   }
00040   
00041     
00047   function remove($key) {
00048     if (isset($this->attributes[$key])) {
00049       unset($this->attributes[$key]);
00050     }
00051     return ! isset($this->attributes[$key]);
00052   }
00053   
00054   
00058   function version () {
00059     return $this->version_msg;
00060   }
00061 
00062 
00069   function error ($msg) {
00070     return $this->message("ERROR: ".$msg);
00071   }
00072   
00073 
00081   function errors ($prefix = "\nWARNING: ") {
00082     $msg = $this->message();
00083     if ($msg !== false) {
00084       return $prefix.$msg;
00085     }
00086     return false;
00087   }
00088 
00089 
00094   function errorReset() {
00095     for ($i = 0; $i < count($this->message_queue); $i++) {
00096       if (strpos($this->message_queue[$i],"ERROR:") !== false) {
00097         $this->message_queue[$i] = "";
00098       }
00099     }
00100     return true;
00101   }
00102   
00103 
00107   function conf ($configuration) {
00108     if (! file_exists($configuration)) {
00109       $this->error("Can't find $configuration file.");
00110       return false;
00111     }
00112     if (! is_readable($configuration)) {
00113       $this->error("Can't read $configuration file.");
00114       return false;
00115     }
00116     $config_text = file($configuration);
00117     foreach ($config_text as $line) {
00118       // Strip off comments and white space
00119       if (strstr($line,'#')) {
00120           list($src,$comment) = explode('#',$line,2);
00121           $line = $src;
00122       }
00123       // Set key value pairs.
00124       if (trim($line)) {
00125         list ($key, $value) = explode(':',$line,2);
00126         $this->set(trim($key),trim($value));
00127       }
00128     }
00129     return true;
00130   }
00131 }
00132 
00133 
00138 class SOC_Cache2 extends WSCORE2 {
00142   public function __construct ($conf = NULL) {
00143     WSCORE2::__construct ($conf);
00144   }
00145   
00150   public function createTables () {
00151     switch ($this->get("db_type")) {
00152     case 'sqlite':
00153       // sqlite2 doesn't support IF EXISTS so just blindly run this 
00154       // and assume it works.
00155       $this->execute("DROP TABLE soc_cache");
00156       $this->execute("CREATE TABLE soc_cache (cache_id INTEGER AUTO_INCREMENT PRIMARY KEY, uri VARCHAR(255), src TEXT, modified TIMESTAMP)");
00157       if ($this->errorCount() === 0) {
00158         return true;
00159       }
00160       break;
00161     case 'mysql':
00162       $this->execute("DROP TABLE IF EXISTS soc_cache");
00163       $this->execute("CREATE TABLE IF NOT EXISTS soc_cache (cache_id INTEGER AUTO_INCREMENT PRIMARY KEY, uri VARCHAR(255), src MEDIUMTEXT, modified TIMESTAMP)");
00164       if ($this->errorCount() === 0) {
00165         return true;
00166       }
00167       break;
00168     default:
00169       $this->message("ERROR: Database type not configured.");
00170       return false;
00171     }
00172     return false;
00173   }
00174 
00182   public function setCache($uri, $src = false, $verbose = false) {
00183                 $this->execute("DELETE FROM soc_cache WHERE uri = '" .trim($uri) . "'", $verbose);
00184     if ($src === false) {
00185       // Clear the item from the cache
00186       return ($this->errorCount() === 0);
00187     }
00188     if ($this->errorCount() > 0) {
00189         $this->message();// Ugly, Dump the errors in the message queue.
00190     }
00191     if ($this->get("db_type") == 'mysql') {
00192         $src = mysql_escape_string(mb_convert_encoding($src,"UTF-8"));
00193     }
00194     $this->execute("INSERT INTO soc_cache (uri, src) VALUES ('".$uri."', '".$src."')", $verbose);
00195                 if ($this->errorCount() > 0) {
00196                         $this->message("ERROR: cache not set for $uri");
00197                         return false;
00198     }
00199     return true;
00200   }
00201   
00208   public function getCache($uri, $verbose = false) {
00209     $data = array('uri' => $uri);
00210     $this->mapExecute("SELECT src FROM soc_cache WHERE uri = '{uri}'", $data, $verbose);
00211     $row = $this->getRow();
00212     $this->release();
00213     if (isset($row['src'])) {
00214       return $row['src'];
00215     }
00216     return false;
00217   }
00218   
00224   public function listCache ($verbose = false) {
00225     $rows = array();
00226     $this->execute("SELECT uri FROM soc_cache ORDER BY modified", $verbose);
00227     while ($row = $this->getRow()) {
00228       if (isset($row['uri'])) {
00229         $rows[] = $row['uri'];
00230       }
00231     }
00232     $this->release();
00233     return $rows;
00234   }
00235 }
00236 
00242 class SOC_Configuration extends SOC_Widget {
00243 
00251   function SOC_Configuration ($app_name="soc", $conf="soc1c.conf") {
00252     SOC_Widget::SOC_Widget();
00253     // Setup pathing to see if this solves the Smarty reference problem.
00254     $php_include_path = get_include_path ();
00255     if (! strstr("$php_include_path", ".")) {
00256         set_include_path(".:" . $php_include_path);
00257     }
00258 
00259     SOC_Widget::set('app_name', $app_name);
00260     SOC_Widget::set('soc_conf', $conf);
00261 
00262     if (isset($_SERVER) && isset($_SERVER['HTTP_HOST'])) {
00263       SOC_Widget::set('hostname', $_SERVER['HTTP_HOST']);
00264     } else {
00265       SOC_Widget::set('hostname', "web-app.usc.edu");
00266     }
00267     SOC_Widget::set('soc_path', "/www/ws/$app_name");
00268     SOC_Widget::set('soc_uri', "http://".
00269                 SOC_Widget::get('hostname').
00270                 "/ws/$app_name");
00271  
00272     SOC_Widget::set("db_type", "sqlite");
00273     SOC_Widget::set("db_name", "soc.sq2");
00274     
00275     if (file_exists ("../_conf/$conf")) {
00276       SOC_Widget::set('soc_conf', "../_conf/$conf");
00277     } else if (file_exists ("_conf/$conf")) {
00278       SOC_Widget::set('soc_conf', "_conf/$conf");
00279     } else if (file_exists("$conf")) {
00280       SOC_Widget::set('soc_conf', "./$conf");
00281     } else {
00282       exit("<strong>WARNING: can't find $conf file.  Application isn't configured.</strong><br />\n");
00283     }
00284 
00285     SOC_Widget::conf(SOC_Widget::get('soc_conf'));
00286   }  
00287 }
00288 ?>