function s_string_at(&$weibo) { if (s_bad_string($weibo)) { return false; } return preg_replace("/\\@([\\w_\\x{4e00}-\\x{9fa5}]+)/u", "<a usercard=\"name=\$1\" title=\"\$0\" href=\"http://weibo.com/n/\$1\">\$0</a>", $weibo); }
function s_cookie_set($key, $val, $exp, $path) { if (s_bad_string($key)) { return false; } return setrawcookie($key, $val); }
function s_memcache_del($key) { if (s_bad_string($key) || false === ($cache = s_memcache_local())) { return false; } //不做值存在检查,直接写 return $cache->delete(md5(MEM_CACHE_KEY_PREFIX . $key)); }
function s_string_length($string, $trim = false) { if (s_bad_string($string, $string, $trim)) { return false; } $len1 = strlen($string); $len2 = mb_strlen($string); return $len1 === $len2 ? $len1 : $len2; }
function s_action_redirect($url) { if (s_bad_string($url)) { $url = defined('APP_NAME') ? '/' . APP_NAME : ''; } if (!s_bad_ajax()) { return s_action_json(array('error' => 1, 'redirect' => $url)); } //302 header("Location: {$url}"); return ""; }
function s_safe_unhtml($string, $trim = false) { if (s_bad_string($string, $string, $trim)) { return false; } $string = str_replace('&', '&', $string); $string = str_replace(''', "'", $string); $string = str_replace('"', '"', $string); $string = str_replace('>', '>', $string); $string = str_replace('<', '>', $string); return $string; }
function s_string_2dir($path, $mask = 0755) { if (s_bad_string($path)) { return false; } if (isset($_SERVER["SINASRV_CACHE_DIR"])) { $real = $_SERVER["SINASRV_CACHE_DIR"] . $path; } if (!is_dir($real) && !mkdir($real, $mask, true)) { return false; } return array("url" => $_SERVER["SINASRV_CACHE_URL"] . "/" . $path, "dir" => $_SERVER["SINASRV_CACHE_DIR"] . $path); }
function s_cookie_set($key, $val, $exp = 0, $path = "/", $secure = false) { if (s_bad_string($key)) { return false; } if (s_bad_string($val)) { $val = strval($val); } if (s_bad_id($exp)) { $exp = false; } //return setrawcookie($key, $val); return setcookie($key, $val, $exp ? $exp + s_action_time() : false, $path, $secure); }
function s_badge_new($uid, $bid, $username, $password) { if (s_bad_id($uid) || s_bad_string($username) || s_bad_string($password)) { return s_err_arg(); } $key = 'badge_new_by#' . $uid . $bid . $username . $password; if (false === ($data = s_memcache($key))) { $data = array('source' => APP_KEY, 'badge_id' => $bid, 'uids' => $uid, '_username' => $username, '_password' => $password); if ($data = s_badge_http('http://i2.api.weibo.com/2/proxy/badges/issue.json', $data, 'post')) { //缓存一小时 s_memcache($key, $data, 3600); } } return $data; }
function s_badge_new($uid, $bid, $username, $password) { if (s_bad_id($uid) || s_bad_string($username) || s_bad_string($password)) { return s_err_arg(); } $key = "badge_new_by#uid={$uid}&bid={$bid}&user={$username}&password={$password}"; if (false === ($data = s_memcache($key))) { $data = array('badge_id' => $bid, 'uids' => $uid, '_username' => $username, '_password' => $password); if (false === ($data = s_badge_http('http://api.t.sina.com.cn/badges/app/issue.json?source=' . APP_KEY, $data, 'post'))) { return s_err_sdk(); } //缓存一小时 s_memcache($key, $data, 300); } return $data; }
function s_weibo_list_time($list, $format = "m月d日 H:i", $postfix = "") { if (s_bad_array($list) || s_bad_string($format)) { return false; } foreach ($list as &$item) { if (isset($item['time'])) { $item['time'] = date($format . $postfix, $item['time']); } unset($item['fdate']); unset($item['ftime']); unset($item['status']); unset($item); } return $list; }
function s_badge_new($uid, $bid, $username, $password) { if (s_bad_id($uid) || s_bad_string($username) || s_bad_string($password)) { return s_err_arg(); } $key = "badge_new_by#uid={$uid}&bid={$bid}&user={$username}&password={$password}"; if (false === ($data = s_memcache($key))) { $data = array('badge_id' => $bid, 'uids' => $uid, '_username' => $username, '_password' => $password); //if (false === ( $data = s_badge_http('http://api.weibo.com/2/proxy/badges/issue.json', $data, 'post') )) { if (false === ($data = s_badge_http('http://i2.api.weibo.com/2/proxy/badges/issue.json', $data, 'post'))) { return s_err_sdk(); } //缓存五分钟 s_memcache($key, $data, 300); } return $data; }
function s_memcache($key, $value = false, $method = "set") { return false; if (s_bad_string($key)) { return false; } if ($value === false) { //获取memcache值 return s_memcache_get($key); } else { if ($method === "set") { //设置memcache值 return s_memcache_set($key, $value); } } return false; }
function s_string_2dir($path, $mask = 0755) { if (s_bad_string($path)) { return false; } //检查是否为绝对路径 if (substr($path, 0, 1) !== '/') { //非绝对路径自动添加项目前缀 if (isset($_SERVER["SINASRV_CACHE_DIR"])) { $real = $_SERVER["SINASRV_CACHE_DIR"] . $path; } } else { //绝对路径 $real = $path; } if (!is_dir($real) && !mkdir($real, $mask, true)) { return false; } return array("url" => $_SERVER["SINASRV_CACHE_URL"] . "/" . $path, "dir" => $_SERVER["SINASRV_CACHE_DIR"] . $path); }
function &s_watermark(&$photo, &$watermark, $x = 0, $y = 0) { if (s_bad_gd() || s_bad_string($photo) || s_bad_string($watermark)) { return s_err_arg(); } //获取底板图片和水印图片 if (false === ($plate = @imagecreatefromjpeg($photo)) || false === ($water = @imagecreatefrompng($watermark))) { return s_err_log('image error.'); } //计算水平间隔 //检查图片大小 $p_w = imagesx($plate); $p_h = imagesy($plate); $w_w = imagesx($water); $w_h = imagesy($water); if ($p_w < $w_w || $p_h < $w_h) { //消毁对象 imagedestroy($plate); imagedestroy($water); return s_err_log('water height or width more than plate'); } //TODO: 使用临时目录 $time = s_action_time(); $path = '/tmp/'; $path .= defined('APP_NAME') ? APP_NAME : date('Y-m-d', $time) . '_auto'; if (!is_dir($path) && !mkdir($path, 0755, true)) { return false; } $file = $path . '/' . $time . '_' . rand(1, 10000) . '.jpg'; //合并图片 if (false === imagecopy($plate, $water, $p_w - $w_w, $p_h - $w_h, 0, 0, $w_w, $w_h) || false === imagejpeg($plate, $file)) { return s_err_log('unsuccess to {$file}.'); } //消毁对象 imagedestroy($plate); imagedestroy($water); //返回图片地址或者图片数据 return $file; }
function s_weibo_detail_by_mid($mid, $key = false) { if (is_string($mid)) { //查一个 $mid = array($mid); } else { if (is_array($mid)) { //查多个 if (is_string($key)) { //是一个联合数组,那么按$key取值 $list = $mid; $mid = array(); foreach ($list as $item) { if (!s_bad_string($item[$key], $id)) { $mid[] = $id; } } unset($list); } $mid = array_unique($mid); } } if (s_bad_array($mid) || false == ($mid = s_weibo_2id_by_mids($mid))) { return false; } //查询所有的微博详情 $list = array(); foreach ($mid as $key => $wid) { $list[$key] = s_weibo_by_wid($wid); } return $list; }
function s_live_post(&$user, &$mids, $act = 0) { if (s_bad_array($user) || s_bad_id($act) || s_bad_string($mids)) { return s_err_arg(); } $data = array('uid' => $user['uid'], 'lid' => $lid, 'mid' => $mids, 'act' => $act); if (false === ($data = s_live_http('http://i.service.t.sina.com.cn/sapps/live/setmblogstatus.php', $data, 'post'))) { return s_err_sdk(); } return $data; }
function s_bad_referer(&$referer = false, $other = false) { if (!isset($_SERVER['HTTP_HOST'])) { return false; } $hosts = array('weibo.cn', 'weibo.com', 'sina.com', 'sina.com.cn'); if (!s_bad_string($other)) { $hosts[] = $other; } $host = $_SERVER['HTTP_HOST']; foreach ($hosts as &$item) { if (preg_match("/{$item}\$/i", $host)) { if (false !== $referer && isset($_SERVER['HTTP_REFERER'])) { $referer = $_SERVER['HTTP_REFERER']; } return false; } unset($item); } return true; }
function _s_db_delete($table, $v1) { if (s_bad_string($table, $table, true) || !($v1 = is_array($v1) && isset($v1["id"]) ? intval($v1["id"]) : $v1) || s_bad_id($v1)) { return s_err_arg(); } if (defined("APP_DB_PREFIX")) { //替换表名:"%s_user:update" => "201204disney_user:update" $table = sprintf($table, APP_DB_PREFIX, true); } $sql = "update `{$table}` set `status`=-1 where `id`= {$v1}"; return s_db_exec($sql); }
function s_weibo_surl($url) { if (s_bad_string($url)) { return false; } //看cache中是否存在 $mkey = 'weibo_surl#surl=' . $url; if (false === ($data = s_memcache($mkey))) { //缓存中没有,请求服务器 $params = array('url_long' => $url); if (false === ($data = s_weibo_http('https://api.weibo.com/2/short_url/shorten.json', $params))) { return false; } //缓存起来一天 s_memcache($mkey, $data, 86400); } return $data; }
function s_user_ship($uid) { $data = array(); if (!s_bad_id($uid)) { //微博ID $data['target_id'] = $uid; } else { if (!s_bad_string($uid)) { //微博昵称 $data['target_screen_name'] = $uid; } } if (s_bad_array($data)) { return s_err_arg(); } //2.0接口返回程序未被授权 //return s_weibo_http("https://api.weibo.com/2/friendships/create.json", $data, "post"); return s_weibo_http("http://api.t.sina.com.cn/friendships/show.json", $data); }
function s_http_json($url, &$params = false, $method = "get") { if (s_bad_string($url) || false === ($response = s_http_response($url, $params, $method))) { return false; } return json_decode($response, true); }
function s_bad_get($key, &$var = false, $type = "string", $html = true) { if (s_bad_string($key) || !isset($_GET[$key])) { return true; } if ($type === "string") { //字符类型 if ($html !== true) { //不需要转义,直接返回判断结果 return s_bad_string($_GET[$key], $var); } //需要对参数转义处理 if (true === s_bad_string($_GET[$key], $var)) { //不需要转义,因为参数已经验证失败 return true; } if ($var !== false) { $var = s_safe_html($var); } //验证成功,此处返回 return false; } else { if ($type === "int") { //整型 return s_bad_id($_GET[$key], $var); } else { if ($type === "int0") { return s_bad_0id($_GET[$key], $var); } else { if ($type === "email") { //邮箱 return s_bad_email($_GET[$key], $var); } else { if ($type === "phone" || $type === "telphone") { //手机或电话(只需要验证telphone,因为telphone的规则很松已经包含手机了) return s_bad_telphone($_GET[$key], $var); } else { if ($type === "mobile") { //手机 return s_bad_mobile($_GET[$key], $var); } } } } } } return true; }
function s_action_page($assign = false, $tpl = false) { if ($tpl === false) { //需要自动设置$tpl路径 if (s_bad_string($_SERVER['SCRIPT_FILENAME'], $file) || false === ($pos = strrpos($file, '.php')) || false === ($tpl = substr($file, 0, $pos))) { return false; } //截取php文件,得到tpl文件 $tpl .= '.tpl'; } else { if (strpos($tpl, '/') === 0) { //绝对路径 $tpl = $_SERVER['DOCUMENT_ROOT'] . $tpl; } } return s_smarty($tpl, $assign); }
function s_bad_get($key, &$var = false, $type = "string") { if (s_bad_string($key) || !isset($_GET[$key])) { return true; } if ($type === "string") { //字符类型 return s_bad_string($_GET[$key], $var); } else { if ($type === "int") { //整型 return s_bad_id($_GET[$key], $var); } else { if ($type === "int0") { return s_bad_0id($_GET[$key], $var); } else { if ($type === "email") { //邮箱 return s_bad_email($_GET[$key], $var); } else { if ($type === "phone" || $type === "telphone") { //手机或电话(只需要验证telphone,因为telphone的规则很松已经包含手机了) return s_bad_telphone($_GET[$key], $var); } else { if ($type === "mobile") { //手机 return s_bad_mobile($_GET[$key], $var); } } } } } } return true; }
function s_user_message($uid, $message, $mid = false) { if (s_bad_id($uid)) { return false; } if (s_bad_string($message)) { return false; } $data = array(); $data['uid'] = $uid; $data['text'] = $message; if (is_int($mid)) { $data['id'] = $mid; } return s_weibo_http("http://i2.api.weibo.com/2/direct_messages/new.json", $data, 'post'); }
function s_memcache_del($key) { if (s_bad_string($key) || false === ($cache = s_memcache_local())) { return false; } //不做值存在检查,直接写 return $cache->delete($key); }
function s_action_dir($path = false, $mask = 0755) { if ($path === false) { $path = defined('APP_NAME') ? APP_NAME : 'tmp'; } if (s_bad_string($path)) { return false; } $real = false; //检查是否为绝对路径 if (substr($path, 0, 1) !== '/') { //非绝对路径自动添加项目前缀 if (isset($_SERVER["SINASRV_CACHE_DIR"])) { $real = $_SERVER["SINASRV_CACHE_DIR"] . $path; } } else { //绝对路径 $real = $path; } if (!is_dir($real) && !mkdir($real, $mask, true)) { return false; } return array("url" => $_SERVER["SINASRV_CACHE_URL"] . "/" . $path, "dir" => $_SERVER["SINASRV_CACHE_DIR"] . $path); }
function s_sql_desc($sql) { if (s_bad_string($sql, $sql)) { return false; } $select = strpos($sql, "select "); $insert = strpos($sql, "insert "); $update = strpos($sql, "update "); $from = strpos($sql, "from "); $where = strpos($sql, "where "); $group = strpos($sql, "group "); $order = strpos($sql, "order "); $limit = strpos($sql, "limit "); $type = substr($sql, 0, strpos($sql, " ")); //echo " from: {$from} \t where: {$where} \t group: {$group} \t order: {$order} \t limit: {$limit} \t type: {$type} "; return array("sql" => $sql, "type" => $type, "table" => s_sql_table($sql, $select, $update, $insert, $from), "select" => s_sql_select($sql, $from, $select), "where" => s_sql_where($sql, $where, $order, $group, $limit), "group" => s_sql_group($sql, $group, $order, $limit), "limit" => s_sql_limit($sql, $limit)); }
function s_user_reply_comment($weibo) { if (s_bad_array($weibo) || s_bad_id($weibo["id"]) || s_bad_string($weibo["comment"])) { return false; } return s_weibo_http("https://api.weibo.com/2/comments/reply.json", $weibo); }