/**
	 * @param mixed $value
	 *
	 * @return bool
	 */
	public function setValue($value) {
		if ($this->get('required') && !$value) {
			$this->_error = $this->get('label') . ' is required.';
			return false;
		}

		// _link_ allows users to paste in a URL for a given file.  This is then copied locally as normal.
		// In order to detect this, I need to look for the presence of a protocol indicator and this element needs
		// to have allowlink set.
		if($this->get('allowlink') && strpos($value, '_link_://') === 0){
			$n = $this->get('name');
			$value = substr($value, 9);

			// Source
			$f = new \Core\Filestore\Backends\FileRemote($value);

			if(!$f->exists()){
				$this->_error = 'Remote file does not seem to exist';
				return false;
			}

			// Destination
			$nf = \Core\Filestore\Factory::File($this->get('basedir') . '/' . $f->getBaseFilename());

			// do NOT copy the contents over until the accept check has been ran!

			// Now that I have a file object, (in the temp filesystem still), I should validate the filetype
			// to see if the developer wanted a strict "accept" type to be requested.
			// If present, I'll have something to run through and see if the file matches.
			// I need the destination now because I need to full filename if an extension is requested in the accept.
			if($this->get('accept')){
				$acceptcheck = \Core\check_file_mimetype($this->get('accept'), $f->getMimetype(), $nf->getExtension());

				// Now that all the mimetypes have run through, I can see if one matched.
				if($acceptcheck != ''){
					$this->_error = $acceptcheck;
					return false;
				}
			}

			// Now all the checks should be completed and I can safely copy the file away from the temporary filesystem.
			$f->copyTo($nf);

			$value = $nf->getFilename(false);
		}
		elseif(($this->get('browsable') || $this->get('browseable')) && strpos($value, '_browse_://public') === 0){
			$n = $this->get('name');
			$value = substr($value, 11);

			// Source
			$f = \Core\Filestore\Factory::File($value);

			if(!$f->exists()){
				$this->_error = 'File does not seem to exist';
				return false;
			}

			// Now that I have a file object, I still need to validate that this file was what the user was supposed to select.
			// If present, I'll have something to run through and see if the file matches.
			if($this->get('accept')){
				$acceptcheck = \Core\check_file_mimetype($this->get('accept'), $f->getMimetype(), $f->getExtension());

				// Now that all the mimetypes have run through, I can see if one matched.
				if($acceptcheck != ''){
					$this->_error = $acceptcheck;
					return false;
				}
			}
		}
		elseif ($value == '_upload_') {
			$n = $this->get('name');

			// Because PHP will have different sources depending if the name has [] in it...
			if (strpos($n, '][') !== false) {
				// This is a 2+ nested array value.

				preg_match_all('#\[([^\]]*)\]#', $n, $matches);
				$p1 = substr($n, 0, strpos($n, '['));
				$src =& $_FILES[$p1];

				$in = array(
					'name'     => $src['name'],
					'type'     => $src['type'],
					'tmp_name' => $src['tmp_name'],
					'error'    => $src['error'],
					'size'     => $src['size'],
				);

				foreach($matches[1] as $next){
					$in['name']     =& $in['name'][$next];
					$in['type']     =& $in['type'][$next];
					$in['tmp_name'] =& $in['tmp_name'][$next];
					$in['error']    =& $in['error'][$next];
					$in['size']     =& $in['size'][$next];
				}
			}
			elseif (strpos($n, '[') !== false) {
				// This is a single array value.

				$p1 = substr($n, 0, strpos($n, '['));
				$p2 = substr($n, strpos($n, '[') + 1, -1);

				if (!isset($_FILES[$p1])) {
					$this->_error = 'No file uploaded for ' . $this->get('label');
					return false;
				}

				$in = array(
					'name'     => $_FILES[$p1]['name'][$p2],
					'type'     => $_FILES[$p1]['type'][$p2],
					'tmp_name' => $_FILES[$p1]['tmp_name'][$p2],
					'error'    => $_FILES[$p1]['error'][$p2],
					'size'     => $_FILES[$p1]['size'][$p2],
				);
			}
			else {
				$in =& $_FILES[$n];
			}


			if (!isset($in)) {
				$this->_error = 'No file uploaded for ' . $this->get('label');
				return false;
			}
			else {
				$error = \Core\translate_upload_error($in['error']);
				if($error != ''){
					$this->_error = $error;
					return false;
				}

				// Source
				$f = \Core\Filestore\Factory::File($in['tmp_name']);

				// Destination
				// Make sure the filename is sanitized.
				// Also, limit the new filename to 40 characters.
				$newbasename = substr(\Core\str_to_url($in['name'], true), 0, 40);
				$nf = \Core\Filestore\Factory::File($this->get('basedir') . '/' . $newbasename);

				// do NOT copy the contents over until the accept check has been ran!

				// Now that I have a file object, (in the temp filesystem still), I should validate the filetype
				// to see if the developer wanted a strict "accept" type to be requested.
				// If present, I'll have something to run through and see if the file matches.
				// I need the destination now because I need to full filename if an extension is requested in the accept.
				if($this->get('accept')){
					$acceptcheck = \Core\check_file_mimetype($this->get('accept'), $f->getMimetype(), $nf->getExtension());

					// Now that all the mimetypes have run through, I can see if one matched.
					if($acceptcheck != ''){
						$this->_error = $acceptcheck;
						return false;
					}
				}

				// Now all the checks should be completed and I can safely copy the file away from the temporary filesystem.
				$f->copyTo($nf);

				$value = $nf->getFilename(false);
			}
		}

		$this->_attributes['value'] = $value;
		return true;
	}
Esempio n. 2
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());
 }
Esempio n. 3
0
	public function testCopyTo() {
		$file = new \Core\Filestore\Backends\FileRemote($this->_testfile);

		// I should be able to copy to a filename.
		// this gets resolved to a local file.
		$copy = $file->copyTo('tmp/tests-fileremotetest-testcopyto.dat');
		$this->assertInstanceOf('\\Core\\Filestore\\File', $copy);
		$this->assertTrue($copy->exists());
		$this->assertTrue($copy->delete());

		// And it should be able to copy to a local file object.
		$copy = new \Core\Filestore\Backends\FileLocal('tmp/tests-fileremotetest-testcopyto.dat');
		$this->assertFalse($copy->exists());
		$file->copyTo($copy);
		$this->assertTrue($copy->exists());
		$this->assertTrue($copy->delete());
	}