function combinePath($baseType, $filename) { $file = ServerUtils::buildPath(ServerUtils::getRealPath($baseType), $filename); return ServerUtils::formalPath($file); }
function UploadFile($baseType, $filename, $append = false) { LogUtils::log_str('UploadFile Begin'); LogUtils::log_obj(func_get_args()); @set_time_limit(0); $server =& $GLOBALS['as_server']; $sys =& $GLOBALS['system']; //$db = $sys->database(); $atts = $server->getAttachments(); if (count($atts) > 0) { LogUtils::log_str('received atts'); $file = ServerUtils::combinePath($baseType, $filename); LogUtils::log_str($file); $att = null; foreach ($atts as $attitem) { $att = $attitem; break; } if (!is_dir($dir = dirname($file))) { LogUtils::log_str('create dir:' . $dir); mkdir_p($dir); } LogUtils::log_str('save file length:' . strlen($att['data'])); $done = false; /* $gimg_pattern = "gpic/"; if (DIRECTORY_SEPARATOR != "/") $gimg_pattern = str_replace("/", DIRECTORY_SEPARATOR, $gimg_pattern); if ($baseType == SBT_ImageDir && strpos($file, $gimg_pattern)) { $o = $sys->loadModel('goods/gimage'); if ($o) { $tmpfile = HOME_DIR.'/tmp/'.md5($file.implode(',',microtime())); file_put_contents($tmpfile, $att['data']); $upfile_data = array( "name" => basename($file), "type" => "application/octet-stream", "tmp_name"=> $tmpfile, "error" => 0, "size" => filesize($tmpfile) ); $data = $o->save_upload($upfile_data); $done = true; } } */ if (!$done) { LogUtils::log_str('save file:' . $file); if ($append) { file_put_contents($file, $att['data'], FILE_APPEND); } else { file_put_contents($file, $att['data']); } @chmod($file, 0644); } } else { LogUtils::log_str('no atts found'); } LogUtils::log_str('UploadFile Return'); }
function UploadRecord($table, $fields, $guidfield, $idfield, $syncfield, $delimiter = ',', $enclosure = '"') { LogUtils::log_str('UploadRecord Begin'); LogUtils::log_obj(func_get_args()); $server =& $GLOBALS['as_server']; $sys =& $GLOBALS['system']; $db = $sys->database(); $syncitems = array(); $atts = $server->getAttachments(); LogUtils::log_obj($atts); if (count($atts) > 0) { $att = null; foreach ($atts as $attitem) { $att = $attitem; break; } $csvfile = ServerUtils::formalPath(ServerUtils::buildPath(AS_TMP_DIR, 'tmpcsv' . time() . '.txt')); file_put_contents($csvfile, $att['data']); LogUtils::log_str($csvfile); $list = TextUtils::csv2array($csvfile, $fields, $delimiter, $enclosure); unlink($csvfile); $validators = BaseValidator::loadValidators(AS_VALIDATOR_DIR, $table, $sys); $idcolarr = split(',', $idfield); foreach ($list as $row) { LogUtils::log_obj($row); $sync_item = array(); $sync_item['guid'] = ''; $sync_item['id'] = ''; $sync_item['succ'] = false; $sync_item['errmsg'] = ''; $sync_item['syncstate'] = AS_SYNC_ADDED; if (array_key_exists($guidfield, $row)) { $sync_item['guid'] = $row[$guidfield]; } if (array_key_exists($syncfield, $row)) { $sync_item['syncstate'] = $row[$syncfield]; } $idcnd = array(); $idcndstr = ''; foreach ($idcolarr as $idcol) { if (array_key_exists($idcol, $row)) { $idcnd[$idcol] = $row[$idcol]; if (!empty($idcndstr)) { $idcndstr .= ' and '; } $idcndstr .= $idcol . "=" . $db->quote($row[$idcol]); } } $sync_item['id'] = implode(',', $idcnd); LogUtils::log_obj($idcnd); switch ($sync_item['syncstate']) { case AS_SYNC_DELETED: if (count($idcnd) > 0) { if (BaseValidator::runValidateBefore($validators, 'delete', $row)) { $sql = "delete from sdb_{$table} where {$idcndstr}"; LogUtils::log_str($sql); if ($db->exec($sql)) { $sync_item['succ'] = true; BaseValidator::runValidateAfter($validators, 'delete', $row); } } } break; case AS_SYNC_UNCHANGED: case AS_SYNC_MODIFIED: if (count($idcnd) > 0) { $sql = "select * from sdb_{$table} where {$idcndstr}"; LogUtils::log_str($sql); $count = $db->_count($sql); if ($count > 0) { if (BaseValidator::runValidateBefore($validators, 'update', $row)) { $rs = $db->query($sql); $sql = $db->getUpdateSql($rs, $row, true); LogUtils::log_str($sql); if ($sql && $db->exec($sql)) { $sync_item['succ'] = true; BaseValidator::runValidateAfter($validators, 'update', $row); } } } else { if (BaseValidator::runValidateBefore($validators, 'insert', $row)) { $rs = $db->query($sql); $sql = $db->getInsertSQL($rs, $row); LogUtils::log_str($sql); if ($sql && $db->exec($sql)) { if (count($idcnd) == 1) { $sync_item['id'] = $db->lastInsertId(); } $sync_item['succ'] = true; BaseValidator::runValidateAfter($validators, 'insert', $row); } } } } break; case AS_SYNC_ADDED: $count = 0; if (count($idcnd) > 0) { $sql = "select * from sdb_{$table} where {$idcndstr}"; LogUtils::log_str($sql); $count = $db->_count($sql); } if ($count > 0) { if (BaseValidator::runValidateBefore($validators, 'update', $row)) { $rs = $db->query($sql); $sql = $db->getUpdateSql($rs, $row, true); LogUtils::log_str($sql); if ($sql && $db->exec($sql)) { $sync_item['succ'] = true; BaseValidator::runValidateAfter($validators, 'update', $row); } } } else { if (BaseValidator::runValidateBefore($validators, 'insert', $row)) { $sql = "select * from sdb_{$table} where 0=1"; LogUtils::log_str($sql); $rs = $db->query($sql); $sql = $db->getInsertSQL($rs, $row); LogUtils::log_str($sql); if ($sql && $db->exec($sql)) { if (count($idcnd) == 1) { $sync_item['id'] = $db->lastInsertId(); } $sync_item['succ'] = true; BaseValidator::runValidateAfter($validators, 'insert', $row); } } } break; } LogUtils::log_obj($sync_item); $syncitems[] = $sync_item; } } $pack = array('items' => $syncitems); LogUtils::log_str('UploadRecord Return'); return $pack; }