示例#1
0
  @Override
  public void commit() throws Exception {
    Set<UUID> userIDs = new HashSet<UUID>();

    // Users
    Integer userCount = getParameterInteger("users");
    for (int i = 0; i < userCount; i++) {
      Pair<String, String> kvp = getParameterTypeAhead("user_" + i);
      if (kvp != null && !Util.isEmpty(kvp.getKey())) {
        User u = UserStore.getInstance().loadByLoginName(kvp.getKey());
        if (u != null) {
          userIDs.add(u.getID());
        }
      }
    }

    // Groups
    Integer groupCount = getParameterInteger("groups");
    for (int i = 0; i < groupCount; i++) {
      Pair<String, String> kvp = getParameterTypeAhead("group_" + i);
      if (kvp != null && !Util.isEmpty(kvp.getKey())) {
        UserGroup lg = UserGroupStore.getInstance().loadByName(kvp.getKey());
        if (lg != null) {
          userIDs.addAll(UserUserGroupLinkStore.getInstance().getUsersForGroup(lg.getID()));
        }
      }
    }

    // Content
    String subject = getParameterString("subject");
    String body = getParameterString("body");
    Map<String, String> notifParams =
        new ParameterMap(AdHocNotif.PARAM_SUBJECT, subject).plus(AdHocNotif.PARAM_BODY, body);

    // Send
    Server fed = ServerStore.getInstance().loadFederation();
    Date date = getParameterDate("date");

    this.messageCount = new HashMap<String, Integer>();
    for (String channel : Channel.getPush()) {
      if (isParameter(channel) == true && fed.isChannelEnabled(channel) == true) {
        for (UUID userID : userIDs) {
          Notifier.send(channel, date, userID, null, AdHocNotif.COMMAND, notifParams);

          // !$! Consider delayed schedule

          Integer count = this.messageCount.get(channel);
          if (count == null) {
            this.messageCount.put(channel, 1);
          } else {
            this.messageCount.put(channel, (1 + count));
          }
        }
      }
    }
  }
示例#2
0
  @Override
  public void renderVoiceXML() throws Exception {
    User user = UserStore.getInstance().open(getContext().getUserID());

    String code = user.getPhoneVerificationCode();
    int p = code.indexOf(":");
    if (p >= 0) {
      code = code.substring(0, p);
    }

    String msg =
        Util.htmlEncode(
            getString("profile:Phone.VerifyMessage", Setup.getAppTitle(getLocale()), "$digits$"));
    StringBuilder digits = new StringBuilder();
    for (int i = 0; i < code.length(); i++) {
      digits.append("<break time=\"200ms\"/>");
      digits.append(code.charAt(i));
    }
    msg = Util.strReplace(msg, "$digits$", digits.toString());

    write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    write("<vxml version=\"2.1\" xml:lang=\"");
    writeEncode(getLocale().getLanguage());
    if (!Util.isEmpty(getLocale().getCountry())) {
      write("-");
      writeEncode(getLocale().getCountry());
    }
    write("\">");
    write("<form>");

    write("<block>");
    for (int i = 0; i < 10; i++) {
      write("<prompt bargein=\"false\">");
      write(msg);
      write("</prompt>");
      write("<break time=\"2s\"/>");
    }
    write("</block>");

    write("</form>");
    write("</vxml>");
  }
示例#3
0
 @Override
 public String getTitle() throws Exception {
   if (Util.isEmpty(this.article.getTitle()) || getContext().getUserAgent().isSmartPhone()) {
     if (this.article.getSection().equalsIgnoreCase(BabyConsts.SECTION_RESOURCE)) {
       return getString("information:Article.Resource");
     } else {
       return getString("information:Article.Article");
     }
   } else {
     return this.article.getTitle();
   }
 }
示例#4
0
  private void renderSendCode() throws Exception {
    writeFormOpen();

    writeEncode(getString("profile:Phone.VerifyHelp", CODE_LEN));
    write("<br><br>");
    writeButton(
        "send",
        getString(
            "profile:Phone.SendCode",
            Util.stripCountryCodeFromPhoneNumber(getParameterPhone("number"))));

    // Post back the number
    writeHiddenInput("fullnumber", getParameterPhone("number"));

    writeFormClose();
  }
示例#5
0
  private void renderEnterPhone() throws Exception {
    RequestContext ctx = getContext();
    User user = UserStore.getInstance().load(ctx.getUserID());
    Server fed = ServerStore.getInstance().loadFederation();

    writeFormOpen();

    TwoColFormControl twoCol = new TwoColFormControl(this);

    twoCol.writeTextRow(getString("profile:Phone.EnterHelp"));
    twoCol.writeSpaceRow();

    twoCol.writeRow(getString("profile:Phone.Number"));
    new PhoneInputControl(twoCol, "number")
        .limitCountries(fed.getVoiceCountries())
        .setInitialValue(user.getPhone())
        .render();

    twoCol.render();

    write("<br>");
    writeButton("enter", getString("controls:Button.Next"));
    write(" ");
    if (ctx.getCommand(1).equals(UrlGenerator.COMMAND_SETUP)) {
      new ButtonInputControl(this, "clear")
          .setSubdued(true)
          .setValue(getString("profile:Phone.Skip"))
          .render();
    } else if (!Util.isEmpty(user.getPhone())) {
      new ButtonInputControl(this, "clear")
          .setStrong(true)
          .setValue(getString("profile:Phone.Clear"))
          .render();
    }

    writeFormClose();
  }
示例#6
0
  private void renderEnterCode() throws Exception {
    writeFormOpen();

    TwoColFormControl twoCol = new TwoColFormControl(this);

    twoCol.writeTextRow(getString("profile:Phone.CodeHelp", CODE_LEN));
    twoCol.writeSpaceRow();

    twoCol.writeRow(getString("profile:Phone.Number"));
    twoCol.writeEncode(Util.stripCountryCodeFromPhoneNumber(getParameterString("fullnumber")));

    twoCol.writeRow(getString("profile:Phone.Code", CODE_LEN));
    twoCol.writeTextInput("code", null, CODE_LEN, CODE_LEN);

    twoCol.render();

    write("<br>");
    writeButton("verify", getString("profile:Phone.Verify"));

    // Post back the number
    writeHiddenInput("fullnumber", null);

    writeFormClose();
  }
示例#7
0
  @Override
  public void validate() throws Exception {
    int countAddressees = 0;

    // Users
    Integer userCount = getParameterInteger("users");
    for (int i = 0; i < userCount; i++) {
      Pair<String, String> kvp = getParameterTypeAhead("user_" + i);
      if (kvp != null && !Util.isEmpty(kvp.getKey())) {
        User u = UserStore.getInstance().loadByLoginName(kvp.getKey());
        if (u == null) {
          throw new WebFormException(
              "user_" + i, getString("admin:AdHocMessage.InvalidLoginName", kvp.getValue()));
        }
        countAddressees++;
      }
    }

    // Groups
    Integer groupCount = getParameterInteger("groups");
    for (int i = 0; i < groupCount; i++) {
      Pair<String, String> kvp = getParameterTypeAhead("group_" + i);
      if (kvp != null && !Util.isEmpty(kvp.getKey())) {
        UserGroup lg = UserGroupStore.getInstance().loadByName(kvp.getKey());
        if (lg == null) {
          throw new WebFormException(
              "group_" + i, getString("admin:AdHocMessage.InvalidGroupName", kvp.getValue()));
        }
        countAddressees++;
      }
    }

    // Check number of recipients
    if (countAddressees == 0) {
      throw new WebFormException(
          new String[] {"groups", "users"}, getString("admin:AdHocMessage.NoRecipients"));
    }

    // Channels
    int countChannels = 0;
    for (String channel : Channel.getAll()) {
      if (isParameter(channel)) {
        countChannels++;
      }
    }
    if (countChannels == 0) {
      throw new WebFormException(Channel.getAll(), getString("common:Errors.MissingField"));
    }

    // Subject and body
    boolean mandateSubject = isParameter(Channel.EMAIL);
    validateParameterString("subject", mandateSubject ? 1 : 0, 128);

    String html = getParameterRichEdit("body");
    if (Util.isEmptyHTML(html)) {
      throw new WebFormException("body", getString("common:Errors.MissingField"));
    }

    // Date
    validateParameterDate("date");
  }
示例#8
0
  /**
   * Executes the <code>WebPage</code> corresponding to the <code>RequestContext</code>.
   *
   * @param ctx
   */
  public static void execute(WebPage page, RequestContext ctx) throws Exception {
    // Attach the request context to this thread
    RequestContext prevCtx = RequestContext.setCurrent(ctx);

    try {
      // Check authorization
      if (page.isAuthorized() == false) {
        throw new UnauthorizedException();
      }

      // Redirect from HTTP to HTTPS and vice versa, as needed
      // But do not redirect POST requests from HTTPS to HTTP since they cause infinite redirection
      // loop
      boolean ssl = page.isSecureSocket() && Setup.isSSL();
      if (ssl != ctx.isSecureSocket()
          && Channel.isSupportsSecureSocket(ctx.getChannel())
          && (ctx.getMethod().equalsIgnoreCase("GET") || ssl == true)) {
        throw new SecureSocketException();
      }

      // Update last activity date of user once every 1/4 session
      Date now = new Date();
      User user = UserStore.getInstance().load(ctx.getUserID());
      if (user != null
          && (ctx.getMethod().equalsIgnoreCase("POST") || Channel.isPush(ctx.getChannel()) == false)
          && (user.getLastActive() == null
              || user.getLastActive().getTime() + Setup.getSessionLength() / 4L < now.getTime())) {
        user = (User) user.clone();
        user.setLastActive(now);
        UserStore.getInstance().save(user);
      }

      page.init();

      if (ctx.getMethod().equalsIgnoreCase("POST")) {
        // Counter XSS attacks by checking that form data includes the session ID
        String sessionParam = ctx.getParameter(RequestContext.PARAM_SESSION);
        boolean sessionParamMatch =
            sessionParam != null && sessionParam.equals(ctx.getSessionID().toString());
        if (page.isProtectXSS() && ctx.getSessionID() != null && !sessionParamMatch) {
          throw new BadRequestException();
        }

        // Validate and commit the form
        if (page.isActionable()) {
          try {
            page.validate();

            // Actions
            if (!Util.isEmpty(ctx.getParameter(RequestContext.PARAM_ACTION))) {
              // Log the event
              LogEntryStore.log(new ActionLogEntry());
            }

            page.setCommitted(true);
            page.commit(); // May throw RedirectException, PageNotFoundException, etc.
          } catch (WebFormException webFormExc) {
            page.setFormException(webFormExc);
          }
        } else {
          // Page does not support POST
          throw new PageNotFoundException();
        }
      }
      page.render();
    } finally {
      // Restore the request context for this thread
      RequestContext.setCurrent(prevCtx);
    }
  }
示例#9
0
  @Override
  public void renderHTML() throws Exception {
    UserAgent ua = getContext().getUserAgent();

    //		writeEncode(this.article.getSourceURL());
    //		write("<br><br>");

    //		boolean healthyBeginnings = this.article.getSection().equals(BabyConsts.SECTION_INFO);
    //		writeHorizontalNav(healthyBeginnings? ViewArticleListPage.COMMAND :
    // ViewResourceListPage.COMMAND);

    write("<div class=Article>");

    if (!Util.isEmpty(this.article.getSubSection())) {
      write("<div class=Subsection>");
      writeEncode(this.article.getSubSection());
      write("</div>");
    }

    if (ua.isSmartPhone() && !Util.isEmpty(this.article.getTitle())) {
      write("<h2>");
      writeEncode(this.article.getTitle());
      write("</h2>");
    }

    if (!Util.isEmpty(this.article.getYouTubeVideoID())) {
      write("<div align=center>");
      int width = 600;
      if (width > ua.getScreenWidth()) {
        width = ua.getScreenWidth() - 10;
      }
      int height = width * 2 / 3;
      writeYouTubeVideo(this.article.getYouTubeVideoID(), width, height);
      write("</div><br>");
    }

    if (this.article.getPhoto() != null) {
      new ImageControl(this)
          .img(
              this.article.getPhoto(),
              getContext().getUserAgent().isSmartPhone()
                  ? BabyConsts.IMAGESIZE_BOX_150X150
                  : BabyConsts.IMAGESIZE_BOX_400X400)
          .setAttribute("align", "right")
          .render();
    }

    String html = this.article.getHTML();
    String lcHtml = html.toLowerCase(Locale.US);
    int p = 0;
    while (p < html.length()) {
      int q = lcHtml.indexOf("<a ", p);
      if (q < 0) {
        write(html.substring(p));
        break;
      } else {
        int qq = lcHtml.indexOf(">", q);
        if (qq < 0) {
          write(html.substring(p));
          break;
        }
        int h = lcHtml.indexOf("href=", q);
        if (h < 0) {
          write(html.substring(p, qq + 1));
        }
        int ws = lcHtml.indexOf(" ", h);
        if (ws < 0 || ws > qq) {
          ws = qq;
        }
        String href = html.substring(h + 5, ws);
        if (href.startsWith("\"") || href.startsWith("'")) {
          href = href.substring(1);
        }
        if (href.endsWith("\"") || href.endsWith("'")) {
          href = href.substring(0, href.length() - 1);
        }
        href = BabyUtil.resolveLink(href);
        write(html.substring(p, h));
        if (href != null) {
          write("href=\"");
          writeEncode(href);
          write("\"");
          if (href.startsWith("http:") || href.startsWith("https:")) {
            write(" target=_blank");
          }
        }
        write(html.substring(ws, qq + 1));

        p = qq + 1;
      }
    }

    write("</div>");
  }