Esempio n. 1
0
function saveremotefile($url, $thumbarr = array(100, 100), $mkthumb = 1, $maxsize = 0)
{
    global $_SCONFIG, $_SGLOBAL;
    $patharr = $blank = array('file' => '', 'thumb' => '', 'name' => '', 'type' => '', 'size' => 0);
    $ext = fileext($url);
    $patharr['type'] = $ext;
    if (in_array($ext, array('jpg', 'jpeg', 'gif', 'png'))) {
        $isimage = 1;
    } else {
        $isimage = 0;
        $ext = 'attach';
    }
    //debug 文件名
    if (empty($_SGLOBAL['_num'])) {
        $_SGLOBAL['_num'] = 0;
    }
    $_SGLOBAL['_num'] = intval($_SGLOBAL['_num']);
    $_SGLOBAL['_num']++;
    $filemain = $_SGLOBAL['supe_uid'] . '_' . sgmdate($_SGLOBAL['timestamp'], 'YmdHis') . $_SGLOBAL['_num'] . random(4);
    $patharr['name'] = $filemain . '.' . $ext;
    //debug 得到存储目录
    $dirpath = getattachdir();
    if (!empty($dirpath)) {
        $dirpath .= '/';
    }
    $patharr['file'] = $dirpath . $filemain . '.' . $ext;
    //debug 上传
    $content = sreadfile($url, 'rb', 1, $maxsize);
    if (empty($content)) {
        return $blank;
    }
    writefile(A_DIR . '/' . $patharr['file'], $content, 'text', 'wb', 0);
    if (!file_exists(A_DIR . '/' . $patharr['file'])) {
        return $blank;
    }
    $imageinfo = @getimagesize(A_DIR . '/' . $patharr['file']);
    list($width, $height, $type) = !empty($imageinfo) ? $imageinfo : array('', '', '');
    if (!in_array($type, array(1, 2, 3, 6, 13))) {
        @unlink(A_DIR . '/' . $patharr['file']);
        return $blank;
    }
    $patharr['size'] = filesize(A_DIR . '/' . $patharr['file']);
    //debug 缩略图水印
    if ($isimage) {
        if ($mkthumb && $ext != 'gif') {
            //debug 缩略图
            $patharr['thumb'] = makethumb($patharr['file'], $thumbarr);
            //debug 加水印
            if (!empty($patharr['thumb'])) {
                makewatermark($patharr['file']);
            }
        }
        if (empty($patharr['thumb'])) {
            $patharr['thumb'] = $patharr['file'];
        }
    }
    return $patharr;
}
Esempio n. 2
0
function stream_save($strdata, $albumid = 0, $fileext = 'jpg', $name='', $title='', $delsize=0, $from = false) {
	global $_SGLOBAL, $space, $_SCONFIG, $_SC;

	if($albumid<0) $albumid = 0;
	
	$setarr = array();
	$filepath = getfilepath($fileext, true);
	$newfilename = $_SC['attachdir'].'./'.$filepath;

	if($handle = fopen($newfilename, 'wb')) {
		if(fwrite($handle, $strdata) !== FALSE) {
			fclose($handle);
			$size = filesize($newfilename);
			//检查空间大小

			if(empty($space)) {
				$space = getspace($_SGLOBAL['supe_uid']);
				$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('space')." WHERE uid='$_SGLOBAL[supe_uid]'");
				$space = $_SGLOBAL['db']->fetch_array($query);
				$_SGLOBAL['supe_username'] = addslashes($space['username']);
			}
			$_SGLOBAL['member'] = $space;

			$maxattachsize = checkperm('maxattachsize');//单位MB
			if($maxattachsize) {//0为不限制
				if($space['attachsize'] + $size - $delsize > $maxattachsize + $space['addsize']) {
					@unlink($newfilename);
					return -1;
				}
			}
			
			//检查是否图片
			if(function_exists('getimagesize')) {	
				$tmp_imagesize = @getimagesize($newfilename);
				list($tmp_width, $tmp_height, $tmp_type) = (array)$tmp_imagesize;
				$tmp_size = $tmp_width * $tmp_height;
				if($tmp_size > 16777216 || $tmp_size < 4 || empty($tmp_type) || strpos($tmp_imagesize['mime'], 'flash') > 0) {
					@unlink($newfilename);
					return -2;
				}
			}

			//缩略图
			include_once(S_ROOT.'./source/function_image.php');
			$thumbpath = makethumb($newfilename);
			$thumb = empty($thumbpath)?0:1;

			//大头帖不添加水印
			if($_SCONFIG['allowwatermark']) {
				makewatermark($newfilename);
			}

			//入库
			$filename = addslashes(($name ? $name : substr(strrchr($filepath, '/'), 1)));
			$title = getstr($title, 200, 1, 1, 1);
			
			if($albumid) {
				preg_match("/^new\:(.+)$/i", $albumid, $matchs);
				if(!empty($matchs[1])) {
					$albumname = shtmlspecialchars(trim($matchs[1]));
					if(empty($albumname)) $albumname = sgmdate('Ymd');
					$albumid = album_creat(array('albumname' => $albumname));
				} else {
					$albumid = intval($albumid);
					if($albumid) {
						$query = $_SGLOBAL['db']->query("SELECT albumname,friend FROM ".tname('album')." WHERE albumid='$albumid' AND uid='$_SGLOBAL[supe_uid]'");
						if($value = $_SGLOBAL['db']->fetch_array($query)) {
							$albumname = addslashes($value['albumname']);
							$albumfriend = $value['friend'];
						} else {
							$albumname = sgmdate('Ymd');
							$albumid = album_creat(array('albumname' => $albumname));
						}
					}
				}
			} else {
				$albumid = 0;
			}

			$setarr = array(
				'albumid' => $albumid,
				'uid' => $_SGLOBAL['supe_uid'],
				'username' => $_SGLOBAL['supe_username'],
				'dateline' => $_SGLOBAL['timestamp'],
				'filename' => $filename,
				'postip' => getonlineip(),
				'title' => $title,
				'type' => $fileext,
				'size' => $size,
				'filepath' => $filepath,
				'thumb' => $thumb
			);
			$setarr['picid'] = inserttable('pic', $setarr, 1);

			//更新附件大小
			//积分
			$setsql = '';
			if($from) {
				$reward = getreward($from, 0);
				if($reward['credit']) {
					$setsql = ",credit=credit+$reward[credit]";
				}
				if($reward['experience']) {
					$setsql .= ",experience=experience+$reward[experience]";
				}
			}
			$_SGLOBAL['db']->query("UPDATE ".tname('space')." SET attachsize=attachsize+'$size', updatetime='$_SGLOBAL[timestamp]' $setsql WHERE uid='$_SGLOBAL[supe_uid]'");

			//相册更新
			if($albumid) {
				$file = $filepath.($thumb?'.thumb.jpg':'');
				$_SGLOBAL['db']->query("UPDATE ".tname('album')."
					SET picnum=picnum+1, updatetime='$_SGLOBAL[timestamp]', pic='$file', picflag='1'
					WHERE albumid='$albumid'");
			}

			//最后进行ftp上传,防止垃圾产生
			if($_SCONFIG['allowftp']) {
				include_once(S_ROOT.'./source/function_ftp.php');
				if(ftpupload($newfilename, $filepath)) {
					$setarr['remote'] = 1;
					updatetable('pic', array('remote'=>$setarr['remote']), array('picid'=>$setarr['picid']));
					if($albumid) updatetable('album', array('picflag'=>2), array('albumid'=>$albumid));
				} else {
					return -4;
				}
			}
			
			//统计
			updatestat('pic');

			return $setarr;
    	} else {
    		fclose($handle);
    	}
	}
	return -3;
}
Esempio n. 3
0
function stream_save($strdata, $albumid = 0, $fileext = 'jpg', $name = '', $title = '', $delsize = 0)
{
    global $_SGLOBAL, $space, $_SCONFIG, $_SC;
    $setarr = array();
    $filepath = getfilepath($fileext, true);
    $newfilename = $_SC['attachdir'] . './' . $filepath;
    if ($handle = fopen($newfilename, 'wb')) {
        if (fwrite($handle, $strdata) !== FALSE) {
            fclose($handle);
            $size = filesize($newfilename);
            //检查空间大小
            if (empty($space)) {
                $query = $_SGLOBAL['db']->query("SELECT username, credit, groupid, attachsize, addsize FROM " . tname('space') . " WHERE uid='{$_SGLOBAL['supe_uid']}'");
                $space = $_SGLOBAL['db']->fetch_array($query);
                $_SGLOBAL['supe_username'] = addslashes($space['username']);
            }
            $_SGLOBAL['member'] = $space;
            $maxattachsize = intval(checkperm('maxattachsize'));
            //单位MB
            if ($maxattachsize) {
                //0为不限制
                if ($space['attachsize'] + $size - $delsize > $maxattachsize + $space['addsize']) {
                    @unlink($newfilename);
                    return -1;
                }
            }
            //检查是否图片
            if (function_exists('getimagesize') && !@getimagesize($newfilename)) {
                @unlink($newfilename);
                return -2;
            }
            //缩略图
            include_once S_ROOT . './source/function_image.php';
            $thumbpath = makethumb($newfilename);
            $thumb = empty($thumbpath) ? 0 : 1;
            //大头帖不添加水印
            if ($_SCONFIG['allowwatermark']) {
                makewatermark($newfilename);
            }
            //入库
            $filename = addslashes($name ? $name : substr(strrchr($filepath, '/'), 1));
            $title = $title;
            if ($albumid) {
                preg_match("/^new\\:(.+)\$/i", $albumid, $matchs);
                if (!empty($matchs[1])) {
                    $albumname = shtmlspecialchars(trim($matchs[1]));
                    if (empty($albumname)) {
                        $albumname = sgmdate('Ymd');
                    }
                    $albumid = album_creat(array('albumname' => $albumname));
                } else {
                    $albumid = intval($albumid);
                    if ($albumid) {
                        $query = $_SGLOBAL['db']->query("SELECT albumname,friend FROM " . tname('album') . " WHERE albumid='{$albumid}' AND uid='{$_SGLOBAL['supe_uid']}'");
                        if ($value = $_SGLOBAL['db']->fetch_array($query)) {
                            $albumname = addslashes($value['albumname']);
                            $albumfriend = $value['friend'];
                        } else {
                            $albumname = sgmdate('Ymd');
                            $albumid = album_creat(array('albumname' => $albumname));
                        }
                    }
                }
            } else {
                $albumname = sgmdate('Ymd');
                $albumid = album_creat(array('albumname' => $albumname));
            }
            $setarr = array('albumid' => $albumid, 'uid' => $_SGLOBAL['supe_uid'], 'dateline' => $_SGLOBAL['timestamp'], 'filename' => $filename, 'postip' => getonlineip(), 'title' => $title, 'type' => $fileext, 'size' => $size, 'filepath' => $filepath, 'thumb' => $thumb);
            $setarr['picid'] = inserttable('pic', $setarr, 1);
            //更新附件大小
            //积分
            $setsql = '';
            if ($pic_credit = creditrule('get', 'pic')) {
                $setsql = ",credit=credit+{$pic_credit}";
            }
            $_SGLOBAL['db']->query("UPDATE " . tname('space') . " SET attachsize=attachsize+'{$size}', updatetime='{$_SGLOBAL['timestamp']}' {$setsql} WHERE uid='{$_SGLOBAL['supe_uid']}'");
            //相册更新
            if ($albumid) {
                $file = $filepath . ($thumb ? '.thumb.jpg' : '');
                $_SGLOBAL['db']->query("UPDATE " . tname('album') . "\r\n\t\t\t\t\tSET picnum=picnum+1, updatetime='{$_SGLOBAL['timestamp']}', pic='{$file}', picflag='1'\r\n\t\t\t\t\tWHERE albumid='{$albumid}'");
            }
            //最后进行ftp上传,防止垃圾产生
            if ($_SCONFIG['allowftp']) {
                include_once S_ROOT . './source/function_ftp.php';
                if (ftpupload($newfilename, $filepath)) {
                    $setarr['remote'] = 1;
                    updatetable('pic', array('remote' => $setarr['remote']), array('picid' => $setarr['picid']));
                    if ($albumid) {
                        updatetable('album', array('picflag' => 2), array('albumid' => $albumid));
                    }
                }
            }
            $siteurl = '';
            if (empty($setarr['remote'])) {
                $uri = $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI'] : ($_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']);
                $siteurl = 'http://' . $_SERVER['HTTP_HOST'] . substr($uri, 0, strexists($uri, '/api') ? strrpos($uri, '/') - 3 : strrpos($uri, '/') + 1);
            }
            $setarr['filepathall'] = $siteurl . mkpicurl($setarr, 0);
            return $setarr;
        } else {
            fclose($handle);
        }
    }
    return -3;
}