Xataface 2.0
Xataface Application Framework
Dataface/Serializer.php
Go to the documentation of this file.
00001 <?php
00002 /*-------------------------------------------------------------------------------
00003  * Xataface Web Application Framework
00004  * Copyright (C) 2005-2008 Web Lite Solutions Corp (shannah@sfu.ca)
00005  * 
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  * 
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019  *-------------------------------------------------------------------------------
00020  */
00021 import( 'Dataface/Table.php');
00022 import( 'Dataface/Error.php');
00023 class Dataface_Serializer {
00024 
00025 
00026         var $_table;
00027         
00028         function Dataface_Serializer($tablename){
00029                 $this->_table =& Dataface_Table::loadTable($tablename);
00030         }
00031         
00032         static function number2db($value)
00033         {
00034             $larr = localeconv();
00035             $search = array(
00036                 $larr['decimal_point'], 
00037                 $larr['mon_decimal_point'], 
00038                 $larr['thousands_sep'], 
00039                 $larr['mon_thousands_sep'], 
00040                 $larr['currency_symbol'], 
00041                 $larr['int_curr_symbol']
00042             );
00043             $replace = array('.', '.', '', '', '', '');
00044         
00045             return str_replace($search, $replace, $value);
00046         }
00047         
00048         
00057         function serialize($fieldname, $value, $handleRepeat=true){
00058                 
00059                 // check to see if the input value is a placeholder.  If it is, we should pass it 
00060                 // through untouched.
00061                 if ( is_string($value) and preg_match('/^__(.*)__$/', $value)){
00062                         // This fixes an issue with addRelatedRecord();
00063                         return $value;
00064                 }
00065                 
00066                 if ( $value === null ){
00067                         return null;
00068                 }
00069                 
00070                 if ( strpos($fieldname, '.') !== false ){
00071                         // This is a related field.
00072                         $table =& $this->_table->getTableTableForField($fieldname);
00073                         list( $relname, $fieldname) = explode('.', $fieldname);
00074                         $serializer = new Dataface_Serializer($table->tablename);
00075                         $out = $serializer->serialize($fieldname, $value, $handleRepeat);
00076                         
00077                         return $out;
00078                         
00079                 }
00080                 
00081                 $table =& $this->_table;
00082                 $field =& $table->getField($fieldname);
00083                 if ( PEAR::isError($field) ){
00084                         throw new Exception($field->getMessage());
00085                 }
00086                 
00087                 $delegate =& $table->getDelegate();
00088                 if ( $delegate !== null and method_exists($delegate, $fieldname."__serialize") ){
00089                         $val = call_user_func(array(&$delegate, $fieldname."__serialize"), $value);
00090                         
00091                         return $val;
00092                 }
00093                 $widget = $field['widget'];
00094                 $type = $widget['type'];
00095                 
00096                 
00097                 if ( $handleRepeat and $field['repeat'] and is_array($value) ){
00098                         foreach ($value as $key=>$val){
00099                                 $value[$key] = $this->serialize($fieldname, $val, false);
00100                         }
00101                         
00102                         $value = implode($field['separator'], $value);
00103                         
00104                 }
00105                 
00106                 $evt = new stdClass;
00107                 $evt->table = $this->_table;
00108                 $evt->field =& $field;
00109                 $evt->value = $value;
00110                 $evt->done = false;
00111                 $this->_table->app->fireEvent('serialize_field_value', $evt);
00112                 if ( $evt->done ){
00113                         return $evt->value;
00114                 }
00115                 
00116                 if ($table->isDate( $fieldname ) ){
00117                         
00118                         if ( !isset($value) || !$value ) return null;
00119                         $params = $value; //$field['value'];
00120                         if ( is_string($params)  and strtotime($params) ){
00121                                 $timestamp = strtotime($params);
00122                                 switch ($table->getType($fieldname)){
00123                                         case 'date':
00124                                                 return date('Y-m-d', $timestamp);
00125                                         case 'datetime':
00126                                         case 'timestamp':
00127                                                 return date('Y-m-d h:i:s', $timestamp);
00128                                         case 'time':
00129                                                 return date('h:i:s', $timestamp);
00130                                         case 'year':
00131                                                 return date('Y', $timestamp);
00132                                 }
00133                                         
00134                         }
00135                         if ( !is_array($params) ) return null;
00136                         
00137                         
00138                         $datestr = str_pad($params['year'],4,"0",STR_PAD_LEFT).'-'.str_pad($params['month'],2,"0",STR_PAD_LEFT).'-'.str_pad($params['day'],2,"0",STR_PAD_LEFT);
00139                         $timestr = str_pad($params['hours'],2,"0",STR_PAD_LEFT).':'.str_pad($params['minutes'],2,"0",STR_PAD_LEFT).':'.str_pad($params['seconds'], 2,"0",STR_PAD_LEFT);
00140                         
00141                         switch ( $table->getType($fieldname) ){ 
00142                                 case 'date':
00143                                         return $datestr;
00144                                         //return "FROM_UNIXTIME('$datestr')";
00145                                 case 'datetime':
00146                                         return $datestr.' '.$timestr;
00147                                         //return "FROM_UNIXTIME('$datestr $timestr')";
00148                                 case 'timestamp':
00149                                         return str_pad($params['year'],4,"0",STR_PAD_LEFT).str_pad($params['month'],2,"0",STR_PAD_LEFT).str_pad($params['day'],2,"0",STR_PAD_LEFT).str_pad($params['hours'],2,"0",STR_PAD_LEFT).str_pad($params['minutes'],2,"0",STR_PAD_LEFT).str_pad($params['seconds'],2,"0",STR_PAD_LEFT);
00150                                 case 'time':
00151                                         return $timestr;
00152                                 case 'year':
00153                                         return str_pad($params['year'],4,"0",STR_PAD_LEFT);
00154                         }
00155 
00156                 }
00157                 
00158                 //if ( $table->isInt( $fieldname ) ){
00159                 //      if ( !$value ) return 0;
00160                 //      return $value;
00161                 //}
00162                 
00163                 //if ( $table->isFloat( $fieldname) ){
00164                 //      return self::number2db(doubleval($value));
00165                 //}
00166                 
00167                 
00168                 
00169                 
00170                 if ( is_array( $value ) ){
00171                         if ( $widget['type'] == 'table' or $widget['type'] == 'group'){
00172                                 import( 'XML/Serializer.php');
00173                                 $serializer = new XML_Serializer(array('typeHints'=>true));
00174                                 $ser_res =& $serializer->serialize($value);
00175                                 if (!PEAR::isError($ser_res) ){
00176                                         return $serializer->getSerializedData();
00177                                 }
00178                         }
00179 
00180                         throw new Exception("Trying to serialize value for field '$fieldname' that we don't know what to do with.  The value is an array and we don't know how to parse it.", E_USER_ERROR);
00181                         
00182                 } else {
00183                         
00184                         
00185                         return $value;
00186                 }
00187         
00188         
00189         
00190         }
00191         
00192 
00193         
00198         function unserialize($fieldname, $value){
00199                 throw new Exception("Not implemented yet.", E_USER_ERROR);
00200         
00201         }
00202         
00203         
00208         function encrypt($fieldname, $value=null){
00209                 if ( !isset($value) ) $value = '';
00210                 if ( strpos($fieldname, '.') !== false ){
00211                         // This is a related field.
00212                         $table =& $this->_table->getTableTableForField($fieldname);
00213                         list( $relname, $fieldname) = explode('.', $fieldname);
00214                         $serializer = new Dataface_Serializer($table->tablename);
00215                         $out = $serializer->encrypt($fieldname, $value);
00216                         
00217                         return $out;
00218                         
00219                 }
00220                 $field = $this->_table->getField($fieldname);
00221                 if ( PEAR::isError($field) ){
00222                         error_log($field->getMessage()."\n".implode("\n", $field->getBacktrace()));
00223                         throw new Exception("Failed to encrypt field $fieldname.  See error log for details.", E_USER_ERROR);
00224                         
00225                 }
00226                 if ( isset($field['encryption']) ){
00227                         switch(strtolower($field['encryption'])){
00228                                 case 'md5':
00229                                         return 'MD5('.$value.')';
00230                                 case 'password':
00231                                         return 'PASSWORD('.$value.')';
00232                                 case 'sha1':
00233                                         return 'SHA1('.$value.')';
00234                                 case 'encrypt':
00235                                         return 'ENCRYPT('.$value.')';
00236                                 case 'aes_encrypt':
00237                                         return 'AES_ENCRYPT('.$value.',\''.addslashes($field['aes_key']).'\')';
00238                         }
00239                 }
00240                 return $value;
00241         }
00242         
00243         
00244         
00245         
00246 
00247 
00248 
00249 }
 All Data Structures Namespaces Files Functions Variables Enumerations