/** * {@inheritDoc} */ public static function __callStatic($method, $parameters) { $instance = new static(); if (in_array($method, array_keys($instance->paths))) { return $instance->newQuery()->from($instance->getPath($method)); } return call_user_func_array(array($instance, $method), $parameters); }
/** * Find a model by it's domain name * * @param mixed $id * @return \NielsTholenaar\OpenProviderClient\Model\Domain|static|null */ public static function find($domainName) { $instance = new static(); // Explode domain and extension $domainParts = explode('.', $domainName, 2); if (count($domainParts) < 2) { return null; } // Set the path $path = $instance->getPath('show'); $query = $instance->from($path); // Set required parameters $query = $query->postField('domainNamePattern', $domainParts[0]); $query = $query->postField('limit', 1); return $query->first(); }
/** * Build the site. */ public static function build($dest = 'build') { $app = new static(); array_unshift($app->queue, Middleware::saveResponse($app->getPath($dest))); foreach ($app->sources as $source) { if (!$source->useToBuild()) { continue; } foreach ($source->getIterator() as $sourceFile) { foreach ($source->getUris($sourceFile) as $uri) { $response = $app(new ServerRequest([], [], $uri), new Response()); switch ($response->getStatusCode()) { case 500: throw new RuntimeException("Error processing '{$uri}'"); case 200: echo "OK {$uri}\n"; } } } } }
/** * Handles an UploadedFile from form input. Stores, creates a model, and generates a thumbnail. * * @param UploadedFile $upload * @return FileStorage */ public static function storeUpload(UploadedFile $upload) { $fileContent = File::get($upload); $fileMD5 = md5((string) File::get($upload)); $storage = static::getHash($fileMD5); if (!$storage instanceof static) { $storage = new static(); $fileTime = $storage->freshTimestamp(); $storage->hash = $fileMD5; $storage->banned = false; $storage->filesize = $upload->getSize(); $storage->mime = $upload->getClientMimeType(); $storage->first_uploaded_at = $fileTime; $storage->upload_count = 0; if (!isset($upload->case)) { $ext = $upload->guessExtension(); $upload->case = Sleuth::check($upload->getRealPath(), $ext); if (!$upload->case) { $upload->case = Sleuth::check($upload->getRealPath()); } } if (is_object($upload->case)) { $storage->mime = $upload->case->getMimeType(); if ($upload->case->getMetaData()) { $storage->meta = json_encode($upload->case->getMetaData()); } } } else { $fileTime = $storage->freshTimestamp(); } if (!Storage::exists($storage->getPath())) { Storage::put($storage->getPath(), $fileContent); Storage::makeDirectory($storage->getDirectoryThumb()); } $storage->processAttachment(); return $storage; }
/** * Handles an UploadedFile from form input. Stores, creates a model, and generates a thumbnail. * * @static * @param UploadedFile|File $file * @return FileStorage */ public static function storeUpload($file) { $clientUpload = false; if (!$file instanceof SymfonyFile && !$file instanceof UploadedFile) { throw new \InvalidArgumentException("First argument for FileStorage::storeUpload is not a File or UploadedFile."); return false; } else { if ($file instanceof UploadedFile) { $clientUpload = true; } } $fileContent = File::get($file); $fileMD5 = md5((string) File::get($file)); $storage = static::getHash($fileMD5); if (!$storage instanceof static) { $storage = new static(); $fileTime = $storage->freshTimestamp(); $storage->hash = $fileMD5; $storage->banned = false; $storage->filesize = $file->getSize(); $storage->mime = $clientUpload ? $file->getClientMimeType() : $file->getMimeType(); $storage->first_uploaded_at = $fileTime; $storage->upload_count = 0; if (!isset($file->case)) { $ext = $file->guessExtension(); $file->case = Sleuth::check($file->getRealPath(), $ext); if (!$file->case) { $file->case = Sleuth::check($file->getRealPath()); } } if (is_object($file->case)) { $storage->mime = $file->case->getMimeType(); if ($file->case->getMetaData()) { $storage->meta = json_encode($file->case->getMetaData()); } } } else { $fileTime = $storage->freshTimestamp(); } if (!Storage::exists($storage->getPath())) { Storage::put($storage->getPath(), $fileContent); Storage::makeDirectory($storage->getDirectoryThumb()); } $storage->processAttachment(); return $storage; }
/** * Convert the link to a relative link by substracting a base URI * * This is the opposite of resolving a relative link - i.e. creating a * relative reference link from an original URI and a base URI. * * If the two URIs do not intersect (e.g. the original URI is not in any * way related to the base URI) the URI will not be modified. * * @param Uri|string $baseUri * @return Uri */ public function makeRelative($baseUri) { // Copy base URI, we should not modify it $baseUri = new static($baseUri); $this->normalize(); $baseUri->normalize(); $host = $this->getHost(); $baseHost = $baseUri->getHost(); if ($host && $baseHost && $host != $baseHost) { // Not the same hostname return $this; } $port = $this->getPort(); $basePort = $baseUri->getPort(); if ($port && $basePort && $port != $basePort) { // Not the same port return $this; } $scheme = $this->getScheme(); $baseScheme = $baseUri->getScheme(); if ($scheme && $baseScheme && $scheme != $baseScheme) { // Not the same scheme (e.g. HTTP vs. HTTPS) return $this; } // Remove host, port and scheme $this->setHost(null)->setPort(null)->setScheme(null); // Is path the same? if ($this->getPath() == $baseUri->getPath()) { $this->setPath(''); return $this; } $pathParts = preg_split('|(/)|', $this->getPath(), null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); $baseParts = preg_split('|(/)|', $baseUri->getPath(), null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); // Get the intersection of existing path parts and those from the // provided URI $matchingParts = array_intersect_assoc($pathParts, $baseParts); // Loop through the matches foreach ($matchingParts as $index => $segment) { // If we skip an index at any point, we have parent traversal, and // need to prepend the path accordingly if ($index && !isset($matchingParts[$index - 1])) { array_unshift($pathParts, '../'); continue; } // Otherwise, we simply unset the given path segment unset($pathParts[$index]); } // Reset the path by imploding path segments $this->setPath(implode($pathParts)); return $this; }
/** * Handles an UploadedFile from form input. Stores, creates a model, and generates a thumbnail. * * @param UploadedFile $upload * @return FileStorage */ public static function storeUpload(UploadedFile $upload) { $fileContent = File::get($upload); $fileMD5 = md5(File::get($upload)); $storage = static::getHash($fileMD5); if (!$storage instanceof static) { $storage = new static(); $fileTime = $storage->freshTimestamp(); $storage->hash = $fileMD5; $storage->banned = false; $storage->filesize = $upload->getSize(); $storage->mime = $upload->getClientMimeType(); $storage->first_uploaded_at = $fileTime; $storage->upload_count = 0; if (isset($upload->ffmpegData)) { $meta = []; $codecType = null; $codecName = null; foreach ($upload->ffmpegData as $datum) { $datumItems = explode("=", $datum, 2); if (count($datumItems) == 2) { $datumValue = $datumItems[1]; switch ($datumItems[0]) { case "codec_name": $codecName = $datumItems[1]; break; case "codec_type": $codecType = $datumItems[1]; break; default: $datumKeys = explode(":", $datumItems[0], 2); if (count($datumKeys) == 2) { if ($datumKeys[0] === "TAG") { $meta[$datumKeys[1]] = $datumItems[1]; } } break; } } } if (!is_null($codecType) && !is_null($codecName)) { $storage->mime = "{$codecType}/{$codecName}"; } if (count($meta)) { $storage->meta = json_encode($meta); } } } else { $fileTime = $storage->freshTimestamp(); } $storage->last_uploaded_at = $fileTime; $storage->upload_count += 1; $storage->save(); if (!Storage::exists($storage->getPath())) { Storage::put($storage->getPath(), $fileContent); Storage::makeDirectory($storage->getDirectoryThumb()); } $storage->processThumb(); return $storage; }
/** * Returns the edit theme. * By default the edit theme is loaded from the cms.editTheme parameter, * but this behavior can be overridden by the cms.editTheme event listeners. * If the edit theme is not defined in the configuration file, the active theme * is returned. * @return \Cms\Classes\Theme Returns the loaded theme object. * If the theme doesn't exist, returns null. */ public static function getEditTheme() { $editTheme = Config::get('cms.editTheme'); if (!$editTheme) { $editTheme = static::getActiveTheme()->getDirName(); } $apiResult = Event::fire('cms.editTheme', [], true); if ($apiResult !== null) { $editTheme = $apiResult; } if (!strlen($editTheme)) { throw new SystemException(Lang::get('cms::lang.theme.edit.not_set')); } $theme = new static(); $theme->load($editTheme); if (!File::isDirectory($theme->getPath())) { return null; } return $theme; }
/** * Handles an UploadedFile from form input. Stores, creates a model, and generates a thumbnail. * * @param UploadedFile $upload * @return FileStorage */ public static function storeUpload(UploadedFile $upload) { $fileContent = File::get($upload); $fileMD5 = md5(File::get($upload)); $storage = static::getHash($fileMD5); if (!$storage instanceof static) { $storage = new static(); $fileTime = $storage->freshTimestamp(); $storage->hash = $fileMD5; $storage->banned = false; $storage->filesize = $upload->getSize(); $storage->mime = $upload->getClientMimeType(); $storage->first_uploaded_at = $fileTime; $storage->upload_count = 0; } else { $fileTime = $storage->freshTimestamp(); } $storage->last_uploaded_at = $fileTime; $storage->upload_count += 1; $storage->save(); if (!Storage::exists($storage->getPath())) { Storage::put($storage->getPath(), $fileContent); Storage::makeDirectory($storage->getDirectoryThumb()); } $storage->processThumb(); return $storage; }
/** * Moves the directory. * * @param Directory|string $newParentDir Parent directory (string or object). * @param boolean $overwrite Overwrite an existing directory. * * @replaces #move Note that the original class claimed to return `$this` * but called `#rename()` which always returned `null`. This version * retains the incorrectly documented behaviour. */ public function move($newParentDir, $overwrite = false) { if (!$newParentDir instanceof static) { $newParentDir = new static($newParentDir); } if (strpos($newParentDir->getPath(), $this->getPath()) === 0) { throw new ValidationException('It is not possible to move a directory into one of its sub-directories'); } return $this->rename($newParentDir->getPath() . $this->getName(), $overwrite); }
/** * Transform References * * This function implements the "transform references" algorithm from * the RFC3986 specification for URIs. * * @see http://tools.ietf.org/html/rfc3986#page-31 * * @param string $relative * @param bool $strict * * @return static */ public function transformReference($relative, $strict = false) { $base = $this; $relative = new static((string) $relative); $transformed = new static(); if (!$strict && $relative->getScheme() == $this->getScheme()) { $relative->setScheme(null); } if ($relative->getScheme() !== null) { $transformed->setScheme($relative->getScheme()); $transformed->setAuthority($relative->getAuthority()); $transformed->setPath(static::remove_dot_segments($relative->getPath())); $transformed->setQuery($relative->getQuery()); } else { if ($relative->getAuthority() !== null) { $transformed->setAuthority($relative->getAuthority()); $transformed->setPath(static::remove_dot_segments($relative->getPath())); $transformed->setQuery($relative->getQuery()); } else { if ($relative->getPath() == '') { $transformed->setPath($base->getPath()); if ($relative->getQuery() !== null) { $transformed->setQuery($relative->getQuery()); } else { $transformed->setQuery($base->getQuery()); } } else { if ('/' == substr($relative->getPath(), 0, 1)) { $transformed->setPath(static::remove_dot_segments($relative->getPath())); } else { $transformed->setPath(static::merge($base->getPath(), $relative->getPath())); $transformed->setPath(static::remove_dot_segments($transformed->getPath())); } $transformed->setQuery($relative->getQuery()); } $transformed->setAuthority($base->getAuthority()); } $transformed->setScheme($base->getScheme()); } $transformed->setFragment($relative->getFragment()); return $transformed; }
/** * Handles an UploadedFile from form input. Stores, creates a model, and generates a thumbnail. * * @param UploadedFile $upload * @return FileStorage */ public static function storeUpload(UploadedFile $upload) { $fileContent = File::get($upload); $fileMD5 = md5(File::get($upload)); $storage = static::getHash($fileMD5); if (!$storage instanceof static) { $storage = new static(); $fileTime = $storage->freshTimestamp(); $storage->hash = $fileMD5; $storage->banned = false; $storage->filesize = $upload->getSize(); $storage->mime = $upload->getClientMimeType(); $storage->first_uploaded_at = $fileTime; $storage->upload_count = 0; } else { $fileTime = $storage->freshTimestamp(); } $storage->last_uploaded_at = $fileTime; $storage->upload_count += 1; $storage->save(); if (!Storage::exists($storage->getPath())) { Storage::put($storage->getPath(), $fileContent); Storage::makeDirectory($storage->getDirectoryThumb()); $imageManager = new ImageManager(); $imageManager->make($storage->getFullPath())->resize(300, 300, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); })->save($storage->getFullPathThumb()); } return $storage; }
/** * This returns an Url object for an absolute or relative url or an Url object * @author Michael Ritter <*****@*****.**> * @todo This method does what the constructor of a clean Url class should do! * @param mixed $url Url object or absolute or relative url as string * @return \Cx\Core\Routing\self|\Cx\Core\Routing\Url Url object representing $url */ public static function fromMagic($url) { // if an Url object is provided, return if (is_object($url) && $url instanceof self) { return $url; } $matches = array(); preg_match('#(http(s)?|file)://#', $url, $matches); // relative URL if (!count($matches)) { $absoluteUrl = self::fromRequest(); preg_match('#((?:http(?:s)?|file)://)((?:[^/]*))([/$](?:.*)/)?#', $absoluteUrl->toString(true), $matches); // starting with a /? if (substr($url, 0, 1) == '/') { $url = $matches[1] . $matches[2] . $url; } else { $url = $matches[1] . $matches[2] . $matches[3] . $url; } $url = new static($url); // absolute URL } else { $url = new static($url); } // disable virtual language dir if not in Backend if (preg_match('/.*(cadmin).*/', $url->getPath()) < 1 && $url->getProtocol() != 'file') { $url->setMode('frontend'); } else { $url->setMode('backend'); } return $url; }
/** * Determines if a theme with given directory name exists * @param string $dirName The theme directory * @return bool */ public static function exists($dirName) { $theme = new static(); $path = $theme->getPath($dirName); return File::isDirectory($path); }