public function execute(){

		// If it exists and is good, nothing else needs to be done, (other than flush the session data)
		// This is hit if the user has to manually copy in the configuration.xml data.
		if(file_exists(ROOT_PDIR . '/config/configuration.xml')){
			unset($_SESSION['configs']);
			$this->setAsPassed();
			reload();
		}

		// Load in the configuration example, merge in the SESSION data, and apply them or display the code.
		$xml = new \XMLLoader();
		$xml->setRootName('configuration');
		$xml->loadFromFile(ROOT_PDIR . 'config/configuration.example.xml');

		$elements = $xml->getElements('return|define');
		foreach($elements as $el){
			$name        = $el->getAttribute('name');
			$children    = $el->childNodes;

			foreach($children as $c){
				if($c->nodeName == 'value'){
					// This one requires a random string.
					if($name == 'SECRET_ENCRYPTION_PASSPHRASE' && isset($_SESSION['configs'][$name]) && $_SESSION['configs'][$name] == 'RANDOM'){
						$value = \Core\random_hex(96);
						$c->nodeValue = $value;
					}
					elseif($name == 'SERVER_ID' && isset($_SESSION['configs'][$name]) && $_SESSION['configs'][$name] == 'RANDOM'){
						// The server ID is a 32-digit random string.
						$value = \Core\random_hex(32);
						$c->nodeValue = $value;
					}
					// An override is provided, use that and overwrite the xml.
					elseif(isset($_SESSION['configs'][$name])){
						$value = $_SESSION['configs'][$name];
						$c->nodeValue = $value;
					}
				}
			}
		}

		// Try to save this back down.
		$fdata = $xml->asPrettyXML();

		if(is_writable(ROOT_PDIR . '/config')){
			// Just automatically copy it over, (with the necessary tranformations).
			file_put_contents(ROOT_PDIR . 'config/configuration.xml', $fdata);
			unset($_SESSION['configs']);
			$this->setAsPassed();
			reload();
			// :)
		}
		else{
			// Display the instructions to the user.
			$this->getTemplate()->assign('contents', $fdata);
		}
	}
예제 #2
0
	/**
	 * Handler to save the CSV file locally.
	 *
	 * @param \Form $form
	 *
	 * @return bool
	 */
	public static function FormHandler1(\Form $form) {

		// If it's gotten here, all the form validation has succeeded!
		// Pretty simple eh? :p
		/** @var $el \FormFileInput */
		$el = $form->getElement('file');
		$file = $el->getFile();

		Session::Set('user-import', [
			'file' => $file->getFilename(),
			'key' => \Core\random_hex(10),
		]);
		return true;
	}
예제 #3
0
/**
 * Encode an email address in a way that makes it difficult for standard bots to read.
 *
 * @todo Finish documentation of smarty_function_email
 *
 * @param array  $params  Associative (and/or indexed) array of smarty parameters passed in from the template
 * @param Smarty $smarty  Parent Smarty template object
 *
 * @throws SmartyException
 *
 * @return string
 */
function smarty_function_email($params, $smarty){

	if(isset($params['address']) && $params['address']){
		$email = $params['address'];
	}
	elseif(isset($params['email']) && $params['email']){
		$email = $params['email'];
	}
	elseif(isset($params[0]) && $params[0]){
		$email = $params[0];
	}
	else{
		return '{email} Unable to encode email addresses if no email is provided!';
	}

	\Core\view()->addScript('assets/js/core.strings.js');
	\Core\view()->addScript('assets/js/core.email.js');

	$id = 'e' . \Core\random_hex(5);
	$user = str_rot13(substr($email, 0, strpos($email, '@')));
	$tld = substr($email, strrpos($email, '.')+1);

	$atts = [
		'href' => '#',
		'id' => $id,
		'data-user' => $user,
		'data-tld' => $tld,
		'data-domain' => substr($email, strlen($user) + 1, -1-strlen($tld)),
	];

	$html = '';
	foreach($atts as $k => $v){
		$html .= ' ' . $k . '="' . $v . '"';
	}
	$html = '<a' . $html . '>#</a>';

	$html .= '<script type="text/javascript">Core.Email.Assemble("' . $id . '");</script>';
	return $html;
}
 public static function _GenerateLicenses(Form $form)
 {
     $qty = $form->getElementValue('qty');
     if (!is_numeric($qty)) {
         Core::SetMessage('Please set a valid quantity', 'error');
         return false;
     }
     if ($qty < 1) {
         Core::SetMessage('Please set a quantity greater than 0', 'error');
         return false;
     }
     if ($qty > 999) {
         Core::SetMessage('Quantity limited to 999', 'warning');
         $qty = 999;
     }
     $expires = new \Core\Date\DateTime();
     $expires->modify($form->getElementValue('duration'));
     $expires = $expires->format('Y-m-d');
     for ($i = 0; $i < $qty; $i++) {
         $license = new PackageRepositoryLicenseModel();
         $license->set('password', \Core\random_hex(rand(35, 49)));
         $license->set('expires', $expires);
         $license->save();
     }
     Core::SetMessage('Generated ' . $qty . ' license(s)!', 'success');
     return '/packagerepositorylicense/admin';
 }
예제 #5
0
	/**
	 * Generate a random hex-deciman value of a given length.
	 *
	 * @param int     $length
	 * @param boolean $casesensitive [false] Set to true to return a case-sensitive string.
	 *                              Otherwise the resulting string will simply be all uppercase.
	 *
	 * @return string
	 */
	static public function RandomHex($length = 1, $casesensitive = false) {
		return \Core\random_hex($length, $casesensitive);
	}
예제 #6
0
	public function serverid(){
		// Admin-only page.
		if(!\Core\user()->checkAccess('g:admin')){
			return View::ERROR_ACCESSDENIED;
		}

		$view    = $this->getView();
		$request = $this->getPageRequest();
		
		$serverid = defined('SERVER_ID') ? SERVER_ID : null;
		
		if($serverid === null || $serverid == ''){
			\Core\set_message('t:MESSAGE_ERROR_SERVER_ID_NOT_SET_ADD_TO_CONFIGURATION');
			$newkey = \Core\random_hex(32);
		}
		elseif(strlen($serverid) < 32){
			\Core\set_message('t:MESSAGE_WARNING_SERVER_ID_LEGACY_UPDATE_NOW');
			$newkey = \Core\random_hex(32);
		}
		else{
			// Format the server ID to be human-readable (ish).
			$serverid = wordwrap($serverid, 4, '-', true);
			$newkey = null;
		}
		
		
		$view->title = 't:STRING_SERVER_ID';
		$view->assign('server_id', $serverid);
		$view->assign('new_key', $newkey);
	}
예제 #7
0
 /**
  * Sync the user back to the linked Facebook account.
  *
  * <h3>Usage:</h3>
  * <pre class="code">
  * $auth->syncUser($_POST['access-token']);
  * </pre>
  *
  * @param string $access_token A valid access token for the user to sync up.
  *
  * @return bool True or false on success.
  */
 public function syncUser($access_token)
 {
     try {
         $facebook = new \Facebook(['appId' => FACEBOOK_APP_ID, 'secret' => FACEBOOK_APP_SECRET]);
         $facebook->setAccessToken($access_token);
         /** @var array $user_profile The array of user data from Facebook */
         $user_profile = $facebook->api('/me');
     } catch (\Exception $e) {
         return false;
     }
     $user = $this->_usermodel;
     if (!$user->exists()) {
         // Some config options for new accounts only.
         $profiles = $user->get('external_profiles');
         if (!is_array($profiles)) {
             $profiles = [];
         }
         $profiles[] = [['type' => 'facebook', 'url' => $user_profile['link'], 'title' => 'Facebook Profile']];
         $user->set('external_profiles', $profiles);
         // Another component from the user-social component.
         // This needs to be unique, so do a little fudging if necessary.
         try {
             $user->set('username', $user_profile['username']);
         } catch (\ModelValidationException $e) {
             $user->set('username', $user_profile['username'] . '-' . \Core\random_hex(3));
         }
         // Sync the user avatar.
         $f = new \Core\Filestore\Backends\FileRemote('http://graph.facebook.com/' . $user_profile['id'] . '/picture?type=large');
         $dest = \Core\Filestore\Factory::File('public/user/avatar/' . $f->getBaseFilename());
         $f->copyTo($dest);
         $user->set('avatar', 'public/user/avatar/' . $dest->getBaseFilename());
     }
     // Get all user configs and load in anything possible.
     $user->set('first_name', $user_profile['first_name']);
     $user->set('last_name', $user_profile['last_name']);
     $user->set('gender', ucwords($user_profile['gender']));
     $user->set('facebook_id', $user_profile['id']);
     $user->set('facebook_link', $user_profile['link']);
     $user->set('facebook_access_token', $facebook->getAccessToken());
 }
	public function execute(){

		// If there's already a configuration file present... just skip to the next.
		if(file_exists(ROOT_PDIR . '/config/configuration.xml')){
			$this->setAsPassed();
			reload();
		}

		// This will contain the temporary configuration values for the installer.
		if(!isset($_SESSION['configs'])) $_SESSION['configs'] = [];

		$xml = new \XMLLoader();
		$xml->setRootName('configuration');
		$xml->loadFromFile(ROOT_PDIR . 'config/configuration.example.xml');
		$formelements = [];

		// Since we're pulling from the ant version, set some nice defaults for the user.
		$valuedefaults = [
			'@{db.server}@' => 'localhost',
			'@{db.port}@' => '3306',
			'@{db.type}@' => 'mysqli',
			'@{db.name}@' => '',
			'@{db.user}@' => '',
			'@{db.pass}@' => '',
			'@{devmode}@' => 'false',
			'/tmp/coreplus-web/' => '/tmp/' . $_SERVER['HTTP_HOST'] . '-web/',
			'/tmp/coreplus-cli/' => '/tmp/' . $_SERVER['HTTP_HOST'] . '-cli/',
			'RANDOM' => \Core\random_hex(96),
		];

		$elements = $xml->getElements('return|define');
		foreach($elements as $el){
			$node        = $el->nodeName;
			$name        = $el->getAttribute('name');
			$type        = $el->getAttribute('type');
			$formtype    = $el->getAttribute('formtype');
			$advanced    = $el->getAttribute('advanced');
			$children    = $el->childNodes;
			$value       = null;
			$valuenode   = null;
			$description = null;
			$options     = [];

			// Defaults
			if($advanced === null || $advanced === '') $advanced = "1";

			foreach($children as $c){
				switch($c->nodeName){
					case 'value':
						$value = trim($c->nodeValue);
						$valuenode = $c;
						break;
					case 'description':
						$description = trim($c->nodeValue);
						break;
					case 'option':
						$options[] = trim($c->nodeValue);
						break;
					case '#text':
						break;
					case '#comment':
						break;
					default:
						trigger_error('Unknown sub-node for ' . $node . ' ' . $name . ': ' . $c->nodeName);
				}
			}

			// Since we're pulling from the ant version, set some nice defaults for the user.
			if(isset($valuedefaults[$value])){
				$value = $valuedefaults[$value];
			}

			// Save the value?
			if($_SERVER['REQUEST_METHOD'] == 'POST'){
				if($type == 'boolean' && $formtype == 'checkbox'){
					$value = isset($_POST[$name]) ? 'true' : 'false';
				}
				else{
					$value = isset($_POST[$name]) ? $_POST[$name] : '';
				}

				$_SESSION['configs'][$name] = $value;
			}
			elseif(isset($_SESSION['configs'][$name])){
				$value = $_SESSION['configs'][$name];
			}

			//$value = $el->getElement('value')->nodeValue;

			// Throw this element onto the array for the template to render out.
			$formelements[] = [
				'name'        => $name,
				// Make the title more appealing than machine names...
				'title'       => ucwords(strtolower(str_replace('_', ' ', $name))),
				// Remap "formtype" to "type", since this will be used in a form afterall!
				'type'        => $formtype,
				'value'       => $value,
				'description' => $description,
				'options'     => $options,
				'advanced'    => $advanced,
			];
		}


		// If it's a POST... try the settings and if valid, proceed.
		$message = null;
		$instructions = null;

		if($_SERVER['REQUEST_METHOD'] == 'POST'){
			if($message === null){
				$connectionresults = $this->testDatabaseConnection();
				if($connectionresults['status'] != 'passed'){
					//var_dump($connectionresults); die();
					$message = $connectionresults['message'];
					$instructions = $connectionresults['instructions'];
				}
			}

			if($message === null){
				// Test the assets too!
				$results = $this->testDirectoryWritable('assets/');
				if($results['status'] != 'passed'){
					//var_dump($connectionresults); die();
					$message = $results['message'];
					$instructions = $results['instructions'];
				}
			}

			if($message === null){
				// Test the assets too!
				$results = $this->testDirectoryWritable('public/');
				if($results['status'] != 'passed'){
					//var_dump($connectionresults); die();
					$message = $results['message'];
					$instructions = $results['instructions'];
				}
			}


			if($message === null){
				// Still null after all the tests have ran?
				// w00t!
				$this->setAsPassed();
				reload();
			}
		}

		$this->getTemplate()->assign('message', $message);
		$this->getTemplate()->assign('instructions', $instructions);
		$this->getTemplate()->assign('formelements', $formelements);
		//var_dump($formelements);// die();
	}
	/**
	 * Standard render function for this form element
	 *
	 * @return string
	 */
	public function render() {
		static $renderedcount = 0;

		$renderedcount++;
		$this->_targetname = '_formaccessstring' . $renderedcount . \Core\random_hex(8);

		$v               = trim($this->get('value'));
		$checked         = 'advanced';
		$advanced_groups = array();
		$type            = 'whitelist';

		if ($v == '*') {
			$checked = 'basic_anyone';
		} elseif ($v == '!*') {
			$checked = 'basic_admin';
		}
		elseif (!$v) {
			// Blank value
			$checked = 'advanced';
		}
		elseif ($v == 'g:anonymous') {
			$checked = 'basic_anonymous';
		}
		elseif ($v == 'g:authenticated') {
			$checked = 'basic_authenticated';
		}
		elseif ($v == 'none') {
			// Allow for a blank value.
			$checked = null;
			$type    = null;
		}
		else {
			// Determine the sub groups checked.
			$checked = 'advanced';
			$parts   = array_map('trim', explode(';', $v));
			foreach ($parts as $p) {
				if ($p == '*') {
					// If a wildcard is present, mark the groups as ones to blacklist.
					$type = 'blacklist';
					continue;
				}
				list($t, $tv) = explode(':', $p);
				// Trim off the '!' in front of it, it'll be picked up by the presence of the '*' at the end.
				if ($tv{0} == '!') $tv = substr($tv, 1);
				$advanced_groups[] = $tv;
			}
		}

		$groups = array();

		// Tack on the system groups.
		$anongroup = new UserGroupModel();
		$anongroup->setFromArray(
			array(
				'id'   => 'anonymous',
				'name' => 'Anonymous Users'
			)
		);

		$authgroup = new UserGroupModel();
		$authgroup->setFromArray(
			array(
				'id'   => 'authenticated',
				'name' => 'Authenticated Users'
			)
		);
		$groups[] = $anongroup;
		$groups[] = $authgroup;

		// Find all the groups currently on the site.
		$groups = array_merge($groups, UserGroupModel::Find(null, null, 'name'));
		foreach ($groups as $k => $v) {
			if (in_array($v->get('id'), $advanced_groups)) $v['checked'] = true;
		}


		$tpl = \Core\Templates\Template::Factory($this->getTemplateName());
		$tpl->assign('element', $this);
		$tpl->assign('groups', $groups);
		$tpl->assign('dynname', $this->_targetname);
		$tpl->assign('main_checked', $checked);
		$tpl->assign('advanced_type', $type);

		return $tpl->fetch();
	}