Exemple #1
0
 /**
  * @method POST
  */
 function form()
 {
     // parse request
     parse_str($this->request->data, $request);
     $siteId = $request['siteId'];
     $pageId = $request['pageId'];
     $site = Site::GetBySiteId($siteId);
     $page = Page::GetByPageId($pageId);
     // create a form (sent to webhooks)
     $wh_form = array('SiteId' => $siteId, 'PageId' => $pageId);
     if ($site != null && $page != null) {
         $subject = BRAND . ': Form Submission [' . $site['Name'] . ': ' . $page['Name'] . ']';
         $content = '<h3>Site Information</h3>' . '<table>' . '<tr>' . '<td style="width: 125px; padding: 5px 25px 5px 0;">Site</td>' . '<td style="padding: 5px 0">' . $site['Name'] . '</td>' . '</tr>' . '<tr>' . '<td style="width: 125px; padding: 5px 25px 5px 0;">Page</td>' . '<td style="padding: 5px 0">' . $page['Name'] . '</td>' . '</tr>' . '</table>' . '<h3>Form Details</h3>' . '<table>';
         foreach ($request as $key => $value) {
             if ($key != 'siteId' && $key != 'pageId') {
                 // clean up title
                 $title = urldecode($key);
                 $title = preg_replace('/(?!^)[A-Z]{2,}(?=[A-Z][a-z])|[A-Z][a-z]|[0-9]{1,}/', ' $0', $key);
                 $title = ucwords($title);
                 // decode value
                 $value = urldecode($value);
                 $content .= '<tr>' . '<td style="width: 125px; padding: 5px 25px 5px 0;">' . $title . '</td>' . '<td style="padding: 5px 0">' . $value . '</td>' . '</tr>';
                 $wh_form[$key] = $value;
             }
         }
         $content .= '</table>';
         // sends the email
         $to = $site['PrimaryEmail'];
         $from = $site['PrimaryEmail'];
         $fromName = $site['Name'];
         // send site email
         Utilities::SendSiteEmail($site, $to, $site['PrimaryEmail'], $site['Name'], $subject, $content);
         // send webhook
         Webhooks::FormSubmit($wh_form);
         // return a successful response (200)
         return new Tonic\Response(Tonic\Response::OK);
     } else {
         // unauthorized access
         return new Tonic\Response(Tonic\Response::UNAUTHORIZED);
     }
 }
Exemple #2
0
 /**
  * @method POST
  */
 function post()
 {
     parse_str($this->request->data, $request);
     // parse request
     $friendlyId = trim($request['friendlyId']);
     $name = trim($request['name']);
     $s_passcode = $request['passcode'];
     $timeZone = $request['timeZone'];
     $email = '';
     $password = '';
     $language = DEFAULT_LANGUAGE;
     // language for the app
     $direction = DEFAULT_DIRECTION;
     $userId = -1;
     // get first name and lastname
     $firstName = $request['firstName'];
     $lastName = $request['lastName'];
     // validate name and friendlyId
     if ($friendlyId == '' || $name == '') {
         return new Tonic\Response(Tonic\Response::BADREQUEST);
     }
     $theme = DEFAULT_THEME;
     // set theme
     if (isset($request['theme'])) {
         $theme = $request['theme'];
     }
     // set language if set
     if (isset($request['language'])) {
         $language = $request['language'];
     }
     // set direction if set
     if (isset($request['direction'])) {
         $direction = $request['direction'];
     }
     // check for email and password
     if (isset($request['email'])) {
         $userLanguage = 'en-us';
         if (isset($request['userLanguage'])) {
             $userLanguage = $request['userLanguage'];
         }
         $email = $request['email'];
         $password = $request['password'];
         // valide email and password
         if ($email == '' || $password == '') {
             return new Tonic\Response(Tonic\Response::BADREQUEST);
         }
     } else {
         // get an authuser
         $authUser = new AuthUser();
         if ($authUser->UserId && $authUser->IsSuperAdmin == true) {
             // check if authorized
             $userId = $authUser->UserId;
         } else {
             return new Tonic\Response(Tonic\Response::UNAUTHORIZED);
         }
     }
     // defaults
     $domain = SITE_URL;
     $domain = str_replace('{{friendlyId}}', $friendlyId, $domain);
     $logoUrl = 'sample-logo.png';
     $altLogoUrl = '';
     if ($s_passcode == PASSCODE) {
         $isFriendlyIdUnique = Site::IsFriendlyIdUnique($friendlyId);
         // check for reserved names
         if ($friendlyId == 'app' || $friendlyId == 'sites' || $friendlyId == 'api' || $friendlyId == 'triangulate' || $friendlyId == 'developer') {
             $isFriendlyIdUnique = false;
         }
         if ($isFriendlyIdUnique == false) {
             return new Tonic\Response(Tonic\Response::CONFLICT);
         }
         // default is blank
         $welcomeEmail = '';
         $receiptEmail = '';
         // files for emails
         $welcome_file = APP_LOCATION . '/site/emails/welcome.html';
         $receipt_file = APP_LOCATION . '/site/emails/receipt.html';
         // make sure the welcome email exists
         if (file_exists($welcome_file)) {
             // get default email file
             $welcomeEmail = file_get_contents($welcome_file);
         }
         // make sure the receipt email exists
         if (file_exists($receipt_file)) {
             // get default email file
             $receiptEmail = file_get_contents($receipt_file);
         }
         // add the site
         $site = Site::Add($domain, $name, $friendlyId, $logoUrl, $altLogoUrl, $theme, $email, $timeZone, $language, $direction, $welcomeEmail, $receiptEmail);
         // add the admin
         if ($email != '') {
             $isActive = 1;
             // admins by default are active
             $user = User::Add($email, $password, $firstName, $lastName, 'Admin', $userLanguage, $isActive, $site['SiteId']);
             $userId = $user['UserId'];
         }
         // set $siteId
         $siteId = $site['SiteId'];
         // publishes a theme for a site
         Publish::PublishTheme($site, $theme);
         // publish default content for the theme
         Publish::PublishDefaultContent($site, $theme, $user['UserId']);
         // publish the site
         Publish::PublishSite($site['SiteId']);
         // create a locale directory
         $locales_dir = SITES_LOCATION . '/' . $site['FriendlyId'] . '/locales';
         // create locales directory if it does not exist
         if (!file_exists($locales_dir)) {
             mkdir($locales_dir, 0755, true);
         }
         // set directory for locale
         $locale_dir = $locales_dir . '/' . $site['Language'] . '/';
         // make the locale dir if it does not exist
         if (!file_exists($locale_dir)) {
             mkdir($locale_dir, 0755, true);
         }
         // set filename
         $filename = 'translation.json';
         if (!file_exists($locale_dir . $filename)) {
             // create a blank translation file
             Utilities::SaveContent($locale_dir, $filename, '{}');
         }
         // send welcome email
         if (SEND_WELCOME_EMAIL == true && $email != '') {
             $to = $email;
             $from = EMAILS_FROM;
             $fromName = EMAILS_FROM_NAME;
             $subject = WELCOME_EMAIL_SUBJECT;
             $file = WELCOME_EMAIL_FILE;
             // create strings to replace
             $loginUrl = APP_URL . '/login/' . $site['FriendlyId'];
             $newSiteUrl = $domain;
             $replace = array('{{brand-logo}}' => '<img src="' . BRAND_LOGO . '" style="max-height:50px">', '{{brand}}' => BRAND, '{{reply-to}}' => EMAILS_FROM, '{{new-site-url}}' => $newSiteUrl, '{{login-url}}' => $loginUrl);
             // send email from file
             Utilities::SendEmailFromFile($to, $from, $fromName, $subject, $replace, $file);
         }
         // send new site hook
         Webhooks::NewSite($site);
         // send new user hook
         Webhooks::NewUser($user);
         return new Tonic\Response(Tonic\Response::OK);
     } else {
         return new Tonic\Response(Tonic\Response::UNAUTHORIZED);
     }
 }