public static function addJob($id, $function, $data) { $client = new \GearmanClient(); $client->addServer(gearman_server, gearman_port); $job = new Job(); $job->setId($id)->setExpireTime(time() + 172800)->setReference(array('CLIController', $function, $data)); $job_handle = $client->doBackground(app_name . 'handle', serialize($job)); Log::write(__METHOD__ . ' invoked gearman job (' . $function . ') for id ' . $id . ' ' . app_name . 'handle' . ' ' . $client->returnCode()); }
/** * 404 Error Handler * * @uses MyException::show_error() * * @param string $page Page URI * @param bool $log_error Whether to log the error * @return void */ public function show_404($page = '', $log_error = TRUE) { $heading = '404 Page Not Found'; $message = 'The page you requested was not found.'; if ($log_error) { \Core\Log::write('error', $heading . ': ' . $page, '404'); } $this->show_error($heading, $message, 'error_404', 404); }
public function delete() { if ($this->status) { $list = array_flip(array_flip(func_get_args())); foreach ($list as $v) { $path = $this->root . $v; if (is_file($path) && is_writable($path)) { if (!unlink($path)) { Log::write(_("Image file delete fail:") . $path); } } } } else { } }
/** * 用户注册 * @param $email string 邮箱 * @param $password string hash密码 * @param $name string 用户名 * @param $captcha string 验证码 * @return int 错误代码或者用户成功注册ID */ public function Register($email, $password, $name, $captcha) { $email = strtolower(trim($email)); $password = strtolower(trim($password)); $name = strtolower(trim($name)); $captcha = trim($captcha); if (($code = hook()->apply("UserRegister_Register_before", 0, $email, $password, $name, $captcha)) < 0) { return $code; } if (!$this->Captcha($captcha)) { return -1; } if (!UserCheck::CheckPassword($password)) { return -2; } if (UserCheck::CheckName($name) !== true) { return -4; } if (UserCheck::CheckEmail($email) !== true) { return -5; } $ip = new Ip(); $register_array = ['user_name' => $name, 'user_email' => $email, 'user_aliases' => $name, 'user_password' => '', 'user_salt' => salt(64), 'user_registered_time' => date("Y-m-d H:i:s"), 'user_registered_ip' => $ip->ip2bin($ip->realip()), 'user_cookie_salt' => salt(64), 'user_avatar' => UserCheck::DefaultAvatar(), 'user_status' => 0]; $register_array['user_password'] = UserCheck::CreatePassword($password, $register_array['user_salt']); $reg_code = db()->insert("users", $register_array); if ($reg_code <= 0) { Log::write(_("User register insert sql error."), Log::SQL); return -3; } try { //关于注册成功的提醒 hook()->apply("UserRegister_Register_success", $reg_code, $register_array); if (hook()->apply("UserRegister_Register_success_send_mail", true)) { //判断是否注册过程中需要发送注册邮件 $u = new User($reg_code); $this->SendActivationMail($u); } } catch (\Exception $ex) { Log::write(_("User register success exception notice"), Log::NOTICE); } return $reg_code; }
/** * 检测用户,根据字符串数组 * @param string[] $list * @param int $u_id * @return int[] */ private function checkUser($list, $u_id) { $list = array_flip(array_flip($list)); if (count($list) > 10) { $this->throwMsg(-5); } $ss = $this->db->select("users", ['id'], ['user_name' => $list]); $list = []; foreach ($ss as $v) { $list[] = intval($v['id']); } if (count($list) < 1) { return []; } if ($u_id !== 0) { $ss = implode(",", $list); $sql = <<<EOM SELECT `users_id`, `follow_users_id` FROM `users_follow_users` WHERE (`users_id` = {$u_id} AND `follow_users_id` in ({$ss})) OR (`users_id` in ({$ss}) AND `follow_users_id` = {$u_id}); EOM; $stmt = $this->db->getReader()->query($sql); if ($stmt === false) { Log::write(_("Get sql message users error."), Log::SQL); $this->throwMsg(-8); } $ss = $stmt->fetchAll(\PDO::FETCH_ASSOC); unset($stmt); $list = []; foreach ($ss as $v) { if (!isset($list[$v['users_id']])) { $list[$v['users_id']] = intval($v['users_id']); } if (!isset($list[$v['follow_users_id']])) { $list[$v['follow_users_id']] = intval($v['follow_users_id']); } } if (isset($list[$u_id])) { unset($list[$u_id]); } } return $list; }
public function delete($id, $uid) { $id = +$id; $uid = +$uid; if (!$this->db->has("feed", ['AND' => ['id' => $id, 'users_id' => $uid]])) { $this->throwMsg(-1); } $status = $this->db->delete("feed", ['AND' => ['id' => $id, 'users_id' => $uid]]); if ($status === false) { Log::write(_("Delete feed error."), Log::SQL); $this->throwMsg(-2); } if ($status != 1) { $this->throwMsg(-3); } }
private function initUser(&$data) { $ids = []; foreach ($data as &$v) { if (!isset($ids[$v['users_id']]) && User::UserStack($v['users_id']) === false) { $ids[$v['users_id']] = $v['users_id']; } } if (reset($ids)) { $list = $this->db->select("users", ['id' => 'id', 'user_name' => 'name', 'user_aliases' => 'aliases', 'user_email' => 'email', 'user_url' => 'url', 'user_status' => 'status', 'user_registered_time' => 'registered_time', 'user_last_login_time' => 'last_login_time', 'user_avatar' => 'avatar'], ['id' => $ids]); if ($list === false) { Log::write(_("Select other users error."), Log::SQL); } else { foreach ($list as &$v) { //保存数据到堆栈 new User($v, true); } } } }
/** * 构造方法,使用评论的信息和对应的用户类 * @param array $set_info * @param int $user_id */ function __construct($set_info, $user_id) { $this->ip_c = Ip::getInstance(); $this->user = User::getUser($user_id); if (is_array($set_info) && isset($set_info['comment_id'])) { static $names = []; if (count($names) === 0) { $ref = new \ReflectionClass($this); foreach ($ref->getProperties() as $ro) { $names[] = $ro->getName(); } $names = array_flip($names); unset($names['ip_c'], $names['user'], $names['sub_node']); } if (isset($set_info['comment_ip'])) { $set_info['comment_ip'] = $this->ip_c->bin2ip($set_info['comment_ip']); } foreach ($set_info as $k => $v) { if (isset($names[$k])) { $this->{$k} = $v; } } self::$comment_stack[$this->getCommentId()] = $this; } else { Log::write(_("Comment class construct error"), Log::ALERT); } }
public function __destruct() { if ($this->log === true) { if (!empty($this->_sql)) { \Core\Log::write($this->_sql, 'SQL'); } } }
/** * * @param array $recipients * @param string $subject * @param string $mailBody * @param boolean $isHtml * @param boolean $from */ private static function sendMail($recipients, $subject, $mailBody, $isHtml, $from = false) { if (!(bool) strlen($mailBody)) { $msg = 'Empty mail body found. Sending defered'; return false; } else { try { $mail = new Phpmailer(); // $mail->IsSendmail(); $fromEmail = app_mail_from_email; $fromName = app_mail_from_name; if ($from && is_array($from) && !empty($from)) { if (isset($from['fromEmail'])) { $fromEmail = $from['fromEmail']; } if (isset($from['fromName'])) { $fromName = $from['fromName']; } } $mail->Subject = $subject; $mail->Encoding = 'base64'; //'quoted-printable'; $mail->Hostname = app_mail_host; $mail->Sender = $fromEmail; $mail->set('Return-Path', $fromName); $mail->AddReplyTo($fromEmail, $fromName); $mail->SetFrom($fromEmail, $fromName); if ((bool) $isHtml) { $mail->MsgHTML((string) $mailBody); require_once pmg_root . 'includes' . DIRECTORY_SEPARATOR . 'html2textnew.inc.php'; $mailtotext = new \Html2Text\Html2Text((string) $mailBody); //$mail->AltBody = convert_html_to_text((string)$mailBody); $mail->AltBody = $mailtotext->get_text(); } else { $mail->Body = (string) $mailBody; } //$mail->SetWordWrap(); if (SANDBOX_MODE) { $mail->AddAddress('*****@*****.**', $toName); } else { foreach ($recipients['to'] as $recipient) { $toName = isset($recipient['toName']) ? $recipient['toName'] : ''; if (!isset($recipient['toMail'])) { if (filter_var($recipient, FILTER_VALIDATE_EMAIL)) { $recipient['toMail'] = $recipient; } else { throw new Exception("Error Processing Request"); } } $mail->AddAddress($recipient['toMail'], $toName); } foreach ($recipients['cc'] as $recipient) { $mail->AddCC($recipient['ccEmail'], $recipient['ccName']); } foreach ($recipients['bcc'] as $recipient) { $mail->AddBCC($recipient['bccEmail'], $recipient['bccName']); } } if ($mail->Send()) { Log::write(__METHOD__ . ' Mail sent to ' . json_encode($recipients['to'])); $mail->SmtpClose(); return true; } else { $mail->SmtpClose(); return $mail->ErrorInfo; } } catch (Exception $e) { Log::write($e->getMessage()); return false; } } }
private function clearOtherAvatar($avatar_name) { foreach (self::$list as $v) { $path = _BasePath_ . "/avatar/{$v}/" . $avatar_name; if (is_file($path)) { if (!@unlink($path)) { Log::write("Delete avatar error." . $path, Log::WARN); } } } }
/** * 获取上一页和下一页的信息 */ private function getPreviousNext() { if ($this->gallery_id > 0) { $read = $this->db->getReader(); $stmt = $read->query("call getGalleryPreviousNextID(" . $read->quote($this->gallery_id) . ");"); if ($stmt !== false) { $list = $stmt->fetchAll(\PDO::FETCH_ASSOC); $this->info['previous_and_next'] = ['previous' => NULL, 'next' => NULL]; //释放连接操作 unset($stmt); if (isset($list[0]) && array_sum($list[0]) > 0) { $list = $list[0]; $info = $read->select("gallery", ['id', 'gallery_title'], ['id' => $list, 'ORDER' => 'id']); if ($info === false) { Log::write(_("Get gallery previous and next info error.") . join(",", $read->error()), Log::SQL); $this->throwMsg(-15); } else { for ($i = 0, $c = count($info); $i < $c; ++$i) { if ($info[$i]['id'] == $list['previous']) { $this->info['previous_and_next']['previous'] =& $info[$i]; } else { if ($info[$i]['id'] == $list['next']) { $this->info['previous_and_next']['next'] =& $info[$i]; } } } } } else { //为找到上一页和下一页数据 //可能仅仅一页,数组留空 } } else { Log::write(_("Gallery get previous and next page id error.") . join(", ", $read->error()), Log::SQL); $this->throwMsg(-15); } } }
/** * 对本地图片做缩略图处理 * @param string $path * @param array $server_meta * @return array */ private function makeThumbnail($path, $server_meta) { $rt = []; if (isset($server_meta['server_root_path'])) { $root = $server_meta['server_root_path']; $img_path = $root . "/" . $path; if (is_file($img_path) && is_readable($img_path)) { try { c_lib()->load('image'); $img = new Image(Image::IMAGE_GD, $img_path); $img_w = $img->width(); $img_h = $img->height(); $x = $img_w / $img_h; //创建高清图 if ($img_w > image_hd_width()) { $img->thumb(image_hd_width(), image_hd_width() / $x, Image::IMAGE_THUMB_SCALE); $t_path = "hd/" . dirname($path); $this->createPath($root . "/" . $t_path); $img->save($root . "/hd/" . $path); $rt['pic_hd_path'] = "hd/" . $path; $rt['pic_hd_width'] = $img->width(); $rt['pic_hd_height'] = $img->height(); } else { $rt['pic_hd_path'] = $path; } //创建显示图 if ($img_w > image_display_width()) { $img->thumb(image_display_width(), image_display_width() / $x, Image::IMAGE_THUMB_SCALE); $t_path = "display/" . dirname($path); $this->createPath($root . "/" . $t_path); $img->save($root . "/display/" . $path); $rt['pic_display_path'] = "display/" . $path; $rt['pic_display_width'] = $img->width(); $rt['pic_display_height'] = $img->height(); } else { $rt['pic_display_path'] = $path; } //创建缩略图 $img_w = $img->width(); $c_w = image_thumbnail_width(); $c_h = image_thumbnail_height(); $img_h = $img->height(); if ($img_w < $c_w) { //目标图片宽度小于缩略图 $img_w = $c_w; $img_h = $img_h * $c_w / $img_w; } if ($img_h < $c_h) { //目标图片高度小于缩略图 $img_h = $c_h; $img_w = $img_w * $c_h / $img_h; } //if($img_w / $img_h < $c_w / $c_h){ if ($img_w * $c_h < $c_w * $img_h) { //化简表达 $x_w = $img_w; $x_h = ceil($x_w * $c_h / $c_w); } else { $x_h = $img_h; $x_w = ceil($x_h * $c_w / $c_h); } Log::write(print_r(get_defined_vars(), true)); $img->thumb($x_w, $x_h, Image::IMAGE_THUMB_CENTER); Log::write(print_r(get_defined_vars(), true)); //$img->thumb($c_w, $c_h, Image::IMAGE_THUMB_SCALE); $t_path = "thumbnail/" . dirname($path); $this->createPath($root . "/" . $t_path); $img->save($root . "/thumbnail/" . $path); $rt['pic_thumbnails_path'] = "thumbnail/" . $path; $rt['pic_thumbnails_width'] = $c_w; $rt['pic_thumbnails_height'] = $c_h; } catch (\Exception $ex) { return []; } } } return $rt; }
/** * 记录错误信息 * @param $err */ private function record_error($err) { Log::write($err, Log::ALERT); }
/** * 获取异常 * @param int $code * @return string */ public function getMsg($code) { switch (intval($code)) { case -1: Log::write(_("Delete comment error.") . implode(",", $this->db->error()['write']), Log::SQL); return _("Delete comment error."); case -2: return _("No comment had delete."); case -3: return _("Comment is not exists."); case -4: return _("Comment type id not defined."); case -5: return _("Comment top add error."); case -6: Log::write(_("Cancel comment like error.") . implode(",", $this->db->error()['write']), Log::SQL); return _("Cancel comment like error."); case -7: Log::write(_("Like comment error.") . implode(",", $this->db->error()['write']), Log::SQL); return _("Like comment error."); default: return _("Unknown error."); } }
/** * 使用COOKIE登录系统 * @return bool|User */ public static function CookieLogin() { $cookie = trim(req()->cookie('UserLogin')); if (!empty($cookie)) { $cookie = explode("\t", $cookie); } if (count($cookie) == 2) { $cookie[0] = intval($cookie[0]); $cookie[1] = trim($cookie[1]); if ($cookie[0] > 0) { try { $user = new User($cookie[0]); if ($user->getCookieLogin() == $cookie[1]) { if (in_array($user->getStatus(), [0, 1, 2])) { if (trim(req()->cookie('LoginFlag')) != date("Y-m-d")) { //如果COOKIE中的日期和当前日期不相符就设置 try { self::setLastLoginInfo($user); } catch (\Exception $ex) { Log::write(_("User last login info set error.ID: ") . $user->getId() . _(".Exception:") . $ex->getMessage(), Log::SQL); } } return $user; } } } catch (\Exception $ex) { return false; } } } return false; }
/** * 添加一个访问计数器 * 返回当前访问次数或者FALSE * @param string $type * @param int $id * @return int|false */ public function addCount($type, $id) { if (!in_array($type, $this->type_list)) { return false; } if (!$this->db->has($type, ['id' => $id])) { return false; } $count = $this->db->select($type . "_views", ['views_count'], ['AND' => [$type . "_id" => $id, 'views_date' => date("Y-m-d")], 'LIMIT' => 1]); if ($count === false) { Log::write(_("Count info get error."), Log::SQL); return false; } if (isset($count[0]['views_count'])) { $status = $this->db->update($type . "_views", ['views_count[+]' => 1], ['AND' => [$type . "_id" => $id, 'views_date' => date("Y-m-d")]]); if ($status === false) { Log::write(_("Count info update error."), Log::SQL); return false; } if ($status > 0) { return $count[0]['views_count'] + 1; } } else { $status = $this->db->insert($type . "_views", [$type . "_id" => $id, 'views_date' => date("Y-m-d"), 'views_count' => 1]); if ($status !== -1) { return 1; } else { Log::write(_("Count info insert error."), Log::SQL); } } return false; }
/** * 发送消息给关注用户 * @param null $rt * @param int $f_id 被关注ID * @param int $u_id 关注者ID * @return null */ public function message_follow_me($rt, $f_id, $u_id) { try { if ($this->notice($f_id, 'message', 'follow_me')) { $user = User::getUser($u_id); $follow_user = User::getUser($f_id); $mt = new MailTemplate("message_notice/follow_me.md"); $mt->setUserInfo($follow_user->getInfo()); $mt->setValues(['follow_user_aliases' => $user->getAliases(), 'follow_user_url' => user_link($user->getName()), 'follow_list_link' => get_url('Follow', 'ta'), 'follow_user_name' => $user->getName()]); $this->message->addNoticeMsg($mt->getTitle(), $mt->getContent(), $f_id); } } catch (\Exception $ex) { Log::write(_("Message to message_follow_me create a Exception.") . "EX:[" . $ex->getCode() . "]:" . $ex->getMessage(), Log::NOTICE); } return $rt; }
/** * 回复微信服务器, 此函数支持链式操作 * Example: $this->text('msg tips')->reply(); * @param array $msg 要发送的信息, 默认取$this->msg * @param bool $return 是否返回信息而不抛出到浏览器 默认:否 * @return string */ public function reply($msg = array(), $return = false) { if (empty($msg)) { $msg = $this->msg; } $xml = $this->xml_encode($msg); Log::write('reply', $xml); if ($return) { return $xml; } else { echo $xml; } }
/** * @param array $list */ private function getListDataParse(&$list) { if ($list === false) { Log::write(_("Get comment list of user error."), Log::SQL); $this->throwMsg(-2); } $ol = []; for ($i = 0, $l = count($list); $i < $l; ++$i) { $ol[$list[$i][$this->type . "_id"]] = $list[$i][$this->type . "_id"]; } $info = $this->cm->getCommentTypeInfo($this->type, $ol); for ($i = 0, $l = count($list); $i < $l; ++$i) { if (isset($info[$list[$i][$this->type . "_id"]])) { $list[$i]['info'] =& $info[$list[$i][$this->type . "_id"]]; } else { //此处数据出错,或者数据不完整 Log::write(_("Data loss on CommentList class."), Log::NOTICE); unset($list[$i]); } } }
/** * 记录Cookie */ private function writeCookie() { if (!file_exists(COOKIE_PATH)) { @mkdir(COOKIE_PATH); } $filename = COOKIE_PATH . '/' . md5($this->username); Log::write($this->cookie, 0, $filename); }
/** * 获取异常信息 * @param int $code * @return string */ public function getMsg($code) { switch ($code) { case -1: return _("User is error."); case -2: Log::write(implode(", ", db()->error()['read']) . "\n" . db()->last_query()['read'], Log::SQL); return _("Data select error on sql."); default: return _("Unknown error"); } }
/** * @param int|'+'|'-' $operate * @param null|User|int $user * @return bool|User */ public static function UserStack($operate, $user = NULL) { if (is_int($operate) && isset(self::$user_stack[$operate])) { return self::$user_stack[$operate]; } else { switch ($operate) { case '+': if (is_object($user) && get_class($user) == "ULib\\User" && $user->getId() > 0) { self::$user_stack[$user->getId()] = $user; self::$user_stack_name[$user->getName()] = $user->getId(); return true; } else { Log::write(_("User add stack error.") . print_r($user, true), Log::ALERT); } break; case '-': if (is_int($user) && isset(self::$user_stack[$user])) { unset(self::$user_stack[$user]); $tmp = array_flip(self::$user_stack_name); unset($tmp[$user]); self::$user_stack_name = array_flip($tmp); return true; } break; } } return false; }
public function update($title, $name, $content, $category, $keyword, $description, $status, $allow_comment, $user_id) { if (!$this->existsCheck($this->post_id, $user_id)) { $this->throwMsg(-5); } $title = trim($title); $name = trim($name); $content = trim($content); $this->checkTitle($title); $this->checkName($name); if ($this->existsName($name, $this->post_id)) { $this->throwMsg(-9); } if (empty($content)) { $this->throwMsg(-8); } if (!in_array($category, $this->getCategory())) { $this->throwMsg(-10); } if ($status < 0 || $status > 1) { $this->throwMsg(-11); } $data = ['post_name' => $name, 'post_title' => $title, 'post_content' => $content, 'post_category' => $category, 'post_keyword' => $keyword, 'post_description' => $description, 'post_status' => $status, 'post_update_time' => date("Y-m-d H:i:s"), 'post_allow_comment' => $allow_comment > 0 ? 1 : 0]; if ($this->db->update('posts', $data, ['id' => $this->post_id]) < 0) { Log::write(_("Update post error.") . Log::SQL); $this->throwMsg(-12); } }
/** * 邮件发送 * @param string $name * @param string $email * @param string|null $title * @param string|null $textContent * @param bool $queue * @throws \Exception */ public function mailSend($name, $email, $title = NULL, $textContent = NULL, $queue = true) { if (!hook()->apply("MailTemplate_mailSend", true)) { //是否取消所有邮件发送记录 Log::write(_("Mail send is cancel.") . print_r(func_get_args(), true), Log::NOTICE); return; } if (!$this->is_set_values) { $this->setValues([]); } try { c_lib()->load('mail'); $mail = new Mail(); if ($title === NULL) { $title = $this->title; } $mail->Subject = $title; $mail->addAddress($email, $name); $mail->msgHTML($this->content); if ($textContent === NULL) { $mail->AltBody = $this->text; } else { $mail->AltBody = $textContent; } if (hook()->apply('MailTemplate_mailSend_noQueue', !$queue)) { $mail->send(); } else { lib()->load('Queue'); $queue = new Queue(); $mail_call = new MailCallback($mail); $queue->add($mail_call, NULL, ['lib' => ['MailTemplate'], 'c_lib' => ['mail']]); } } catch (\Exception $ex) { throw new \Exception(_("Mail Send Error.") . debug(" :" . $ex->getMessage())); } }
private function insertData(FeedInterface $object) { $content = @serialize($object); $sid = $object->getSid(); $action = $object->getAction(); if (is_string($action)) { $status = $this->db->insert('feed', ['action' => $action, 'content' => $content, 'sid' => $sid, 'users_id' => $object->getUserId(), 'time' => date("Y-m-d H:i:s")]); if ($status < 1) { Log::write(_("Insert feed error.") . "On " . $action, Log::SQL); return false; } return $status; } return false; }
/** * 插入评论内容 * @param string $content * @param int $parent * @param int $parent_top * @param int $user_id * @return int */ private function insert_comment($content, $parent, $parent_top, $user_id) { c_lib()->load('input'); $input = new Input(); $ip = $input->getIp()->binIp(); $ua = $input->getUA(); $write = $this->db->getWriter(); $write->pdo->beginTransaction(); $info = ['users_id' => $user_id, 'comment_content' => $content, 'comment_time' => $input->time(), 'comment_parent' => $parent ? $parent : NULL, 'comment_parent_top' => $parent_top ? $parent_top : NULL, 'comment_ip' => $ip, 'comment_agent' => $ua]; $info['comment_status'] = $this->get_comment_status($info); $comment_id = $write->insert("comments", $info); if ($comment_id < 1) { $write->pdo->rollBack(); Log::write(implode(",", $write->error()), Log::SQL); $this->throwMsg(-5); } $has_flag = $write->insert($this->relation_table, ['comments_id' => $comment_id, $this->relation_field => $this->id, 'users_id' => $user_id, 'object_users_id' => $this->get_object_users_id()]); if ($has_flag < 0) { $write->pdo->rollBack(); Log::write(implode(",", $write->error()), Log::SQL); $this->throwMsg(-6); } $write->pdo->commit(); $rt = $this->comment_hook($content, $parent, $parent_top, $user_id); if ($parent != 0) { hook()->apply('Comment_reply', $rt, $this->comment_type, $this->id, $user_id, $parent, $parent_top, $content); } return $comment_id; }