public function test_value_array_function() { $array = ['bestaat' => 'Wel']; $array['nested'] =& $array; $this->assertSame('Wel', \Sledgehammer\array_value($array, 'bestaat'), '\\Sledgehammer\\array_value($var, "key") geeft de waarde van $var["key"] terug'); $this->assertSame(null, \Sledgehammer\array_value($array, 'bestaatNiet'), '\\Sledgehammer\\array_value() op een niet bestaande index geeft null terug'); $this->assertSame(null, \Sledgehammer\array_value($array, 'bestaat', 'niet'), '\\Sledgehammer\\array_value() met een index geeft null terug'); $this->assertSame('Wel', \Sledgehammer\array_value($array, 'nested', 'nested', 'bestaat'), '\\Sledgehammer\\array_value($var, "key1", "key1") heeft de waarde van $var["key1"]["key2] terug'); $this->assertSame(null, \Sledgehammer\array_value($array, 'nested', 'nested', 'bestaatNiet')); }
/** * Constructor. * * @param mixed $variable The variable to display on render() */ public function __construct($variable, $backtrace = null) { $this->variable = $variable; if ($backtrace === null) { $backtrace = debug_backtrace(); } if (isset($backtrace[0]['file']) && basename($backtrace[0]['file']) == 'functions.php' && isset($backtrace[1]['function']) && ($backtrace[1]['function'] === 'Sledgehammer\\dump' || $backtrace[1]['function'] === 'Sledgehammer\\debugr')) { if (isset($backtrace[1]['file']) && basename($backtrace[1]['file']) == 'helpers.php' && isset($backtrace[2]['function']) && ($backtrace[2]['function'] === 'dump' || $backtrace[2]['function'] === 'debugr')) { // Call via the dump or debugr function $this->trace = ['invocation' => $backtrace[2]['function'], 'file' => $backtrace[2]['file'], 'line' => $backtrace[2]['line']]; } else { // Call via the Sledgehammer\dump or Sledgehammer\debugr function $this->trace = ['invocation' => $backtrace[1]['function'], 'file' => $backtrace[1]['file'], 'line' => $backtrace[1]['line']]; } } else { // Via constructing this Dump object. $this->trace = ['invocation' => 'new ' . __CLASS__, 'file' => $backtrace[0]['file'], 'line' => $backtrace[0]['line']]; if (\Sledgehammer\array_value($backtrace[0], 'class') === 'Sledgehammer\\DebugR') { $this->trace['invocation'] = 'DebugR::dump'; } } }
/** * Gets the current url based on the information in the $_SERVER array. * * @return Url */ public static function getCurrentURL() { if (self::$current === null) { if (\Sledgehammer\array_value($_SERVER, 'HTTPS') == 'on') { $scheme = 'https'; $port = $_SERVER['SERVER_PORT'] == '443' ? '' : ':' . $_SERVER['SERVER_PORT']; } else { $scheme = 'http'; $port = $_SERVER['SERVER_PORT'] == '80' ? '' : ':' . $_SERVER['SERVER_PORT']; } $domain = $_SERVER['SERVER_NAME']; if (filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { // An IP6 address? $domain = '[' . $domain . ']'; // Enclose IP in brackets } self::$current = new self($scheme . '://' . $domain . $port . $_SERVER['REQUEST_URI']); } return clone self::$current; }
/** * Download and install a PEAR package. * * @throws Exceptions on failure * * @param string $package * @param string $version * @param array $options array( * 'version' = Install a specific version * 'target' => alternative target directory * 'channel' => specifiy the channel * ) */ public function install($package, $options = []) { $version = \Sledgehammer\array_value($options, 'version') ?: 'stable'; if (isset($options['channel'])) { $channel = $options['channel']; $this->addChannel($channel); if (empty($this->channels[$channel]['packages'][$package])) { if (isset($this->channels[$channel]['packages'])) { foreach ($this->channels[$channel]['packages'] as $name => $info) { if (strcasecmp($name, $package) === 0) { return $this->install($name, $options); } } } throw new InfoException('Package "' . $package . '" not found in channel: ' . $channel, \Sledgehammer\quoted_human_implode(' and ', array_keys($this->channels[$channel]['packages']))); } $packageLocation =& $this->channels[$channel]['packages'][$package]; } else { if (count($this->channels) === 0) { $this->addChannel('pear.php.net'); } if (empty($this->packages[$package])) { foreach ($this->packages as $name => $channel) { if (strcasecmp($name, $package) === 0) { return $this->install($name, $options); } } throw new InfoException('Package "' . $package . '" not found in channels: ' . \Sledgehammer\quoted_human_implode(' and ', array_keys($this->channels)), 'Available packages: ' . \Sledgehammer\quoted_human_implode(' and ', array_keys($this->packages))); } $packageLocation =& $this->channels[$this->packages[$package]]['packages'][$package]; } $release = $this->findRelease($packageLocation, $version); if (\Sledgehammer\array_value($packageLocation, 'installed') === $version) { return; } $this->trigger('installing', $this, $package, $version); $tmpFolder = \Sledgehammer\TMP_DIR . 'PearInstaller/'; $folderName = $package . '-' . $version; $tarFile = $tmpFolder . $folderName . '/package.tar'; \Sledgehammer\mkdirs(dirname($tarFile)); if (file_exists($tarFile) === false) { // Is this package already in the tmp folder Curl::download($release->g . '.tar', $tarFile); } chdir(dirname($tarFile)); system('tar xf ' . escapeshellarg($tarFile), $exit); if ($exit !== 0) { throw new Exception('Unable to untar "' . $tarFile . '"'); } if (file_exists(dirname($tarFile) . '/package2.xml')) { $info = simplexml_load_file(dirname($tarFile) . '/package2.xml'); } else { $info = simplexml_load_file(dirname($tarFile) . '/package.xml'); } // Install dependencies first foreach ($info->dependencies->required->package as $dependancy) { if ($dependancy->conflicts) { // \Sledgehammer\notice('Dependancy "'.$dependancy->name.'" for "'.$package.'" <conflicts />'); continue; } $this->install((string) $dependancy->name, array('channel' => (string) $dependancy->channel)); } $renames = []; foreach ($info->phprelease as $release) { if ($release->count() > 0) { foreach ($release->filelist->install as $move) { $renames[(string) $move['name']] = (string) $move['as']; } } } $files = $this->extractFiles($info->contents->dir, '', '/', $renames); foreach ($files as $file) { if (isset($this->targets[$file['role']])) { $dir = $this->targets[$file['role']]; if (in_array($file['role'], array('doc', 'www'))) { if (\Sledgehammer\text($file['to'])->startsWith($package) == false) { $dir = $this->makePath($dir, $package); } } $target = $this->makePath($dir, $file['to']); if (\Sledgehammer\mkdirs(dirname($target)) == false || is_writable(dirname($target)) == false) { throw new Exception('Target "' . $target . '" is not writable'); } $source = $this->makePath($tmpFolder . $folderName . '/' . $folderName, $file['from']); if (isset($file['tasks'])) { $contents = file_get_contents($source); foreach ($file['tasks'] as $task) { $value = null; if ($task['type'] === 'package-info') { if ($task['to'] == 'version') { $value = $version; } elseif ($task['to'] == 'state') { $value = (string) $info->stability->release; } } elseif ($task['type'] == 'pear-config') { if (substr($task['to'], -4) === '_dir') { $role = substr($task['to'], 0, -4); if (isset($this->targets[$role])) { $value = $this->targets[$role]; // @todo calculate relative paths \Sledgehammer\notice('Harcoding path "' . $value . '" into "' . $file['to'] . '"', $file); } } elseif ($task['to'] == 'php_bin') { $value = trim(`which php`); \Sledgehammer\notice('Harcoding path "' . $value . '" into "' . $file['to'] . '"', $file); } } if ($task['task'] === 'replace') { if ($value != '') { $contents = str_replace($task['from'], $value, $contents); } else { \Sledgehammer\notice($task['type'] . ' "' . $task['to'] . '" not yet supported'); } } else { \Sledgehammer\notice('task "' . $task['task'] . '" not implemented'); } } file_put_contents($target, $contents); } else { copy($source, $target); } } } \Sledgehammer\rmdir_recursive($tmpFolder . $folderName . '/' . $folderName); $packageLocation['installed'] = $version; $this->trigger('installed', $this, $package, $version); }
/** * De error van html-opmaak voorzien. * * @param int|Exception $type * @param string $message * @param mixed $information */ private function render($type, $message = null, $information = null) { echo "<!-- \"'> -->\n"; // break out of the tag/attribute if (self::isThrowable($type)) { $exception = $type; $type = $message === '__UNCAUGHT_EXCEPTION__' ? E_ERROR : E_WARNING; if ($exception instanceof InfoException) { $information = $exception->getInformation(); } $message = $exception->getMessage(); } else { $exception = false; } switch ($type) { case E_NOTICE: case E_USER_NOTICE: $offset = ' -52px'; $label_color = '#3a77cd'; // blue $message_color = '#3a77cd'; break; case E_WARNING: case E_USER_WARNING: $offset = ' -26px'; $label_color = '#f89406'; // orange $message_color = '#d46d11'; break; case E_DEPRECATED: case E_USER_DEPRECATED: $offset = ' -78px'; $label_color = '#999'; // gray $message_color = '#333'; break; case E_STRICT: $offset = ' -104px'; $label_color = '#777bb4'; // purple $message_color = '#50537f'; break; default: $offset = ''; $label_color = '#c94a48'; // red $message_color = '#c00'; } // Determine if a full report should be rendered $showDetails = true; if ($this->detail_limit !== 'NO_LIMIT') { if (isset($this->limits['backtrace'][$type]) === false) { $this->limits['backtrace'][$type] = $this->detail_limit; } if ($this->limits['backtrace'][$type] === 0) { $showDetails = false; } else { --$this->limits['backtrace'][$type]; } } $style = array('padding: 10px 15px 15px 15px', 'background-color: #fcf8e3', 'color: #333', 'font: 12px/1.25 \'Helvetica Neue\', Helvetica, sans-serif', 'text-shadow: none', '-webkit-font-smoothing: antialiased', 'font-smoothing: antialiased', 'text-align: left', 'overflow-x: auto', 'white-space: normal'); if ($showDetails === false) { $style[] = 'padding-top: 14px'; } if (!$this->email) { $style[] = 'border: 1px solid #eeb; border-radius: 4px; margin: 15px 5px 18px 5px'; } echo '<div style="', implode(';', $style), '">'; if ($showDetails) { $scheme = \Sledgehammer\array_value($_SERVER, 'HTTPS') == 'on' ? 'https' : 'http'; echo '<span style="display:inline-block; width: 26px; height: 26px; vertical-align: middle; margin: 0 6px 2px 0; background: url(\'' . $scheme . '://bfanger.nl/core/img/ErrorHandler.png\')' . $offset . '"></span>'; } echo '<span style="font-size:13px; text-shadow: 0 1px 0 #fff;color:', $message_color, '">'; if (is_array($message)) { $message = 'Array'; } if ($information === null && strpos($message, 'Missing argument ') === 0) { $information = $this->resolveFunction($message); // informatie tonen over welke parameters er verwacht worden. } $message_plain = $message; if (strpos($message, '<span style="color:') === false) { // Alleen html van de syntax_highlight functie toestaan, alle overige htmlkarakers escapen $message_plain = htmlspecialchars($message_plain); // Vertaal alle html karakters } if ($showDetails) { $label_style = ''; } else { $label_style = array('padding: 1px 3px 2px', 'font-size: 10px', 'line-height: 15px', 'text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25)', 'color: #fff', 'vertical-align: middle', 'white-space: nowrap', 'background-color: ' . $label_color, 'border-radius: 3px'); $label_style = ' style="' . implode(';', $label_style) . '"'; } echo '<b' . $label_style . '>'; if ($exception) { if ($type == E_ERROR) { echo 'Uncaught '; } echo get_class($exception); } else { echo $this->error_types[$type]; } echo "</b> \n\n\t", $message_plain, "\n\n</span>"; if ($showDetails || $this->email) { echo '<hr style="height: 1px; background: #eeb; border: 0;margin: 7px 0px 12px -1px;" />'; if ($information !== null && !empty($information)) { echo "<b>Extra information</b><br />\n<span style='color:#007700'>"; if (is_array($information)) { $this->renderArray($information); } elseif (is_object($information)) { echo \Sledgehammer\syntax_highlight($information), ":<br />\n"; $this->renderArray($information); } else { echo $information, "<br />\n"; } echo '</span>'; } if ($this->email) { // email specifieke informatie blokken genereren? $this->renderBrowserInfo(); $this->renderServerInfo(); } $this->renderBacktrace(); switch ($type) { case E_WARNING: case E_USER_ERROR: case E_USER_WARNING: if (class_exists('Sledgehammer\\Logger', false) && !empty(Logger::$instances)) { echo '<br />'; foreach (Logger::$instances as $name => $logger) { if ($logger->count === 0 && count($logger->entries) === 0) { continue; } if ($this->email) { echo '<b>', $name, '</b><br />'; $logger->render(); } else { $logger->statusbar($name . ': '); echo "<br />\n"; } } } break; } } else { $location = $this->resolveLocation(); if ($location !== false) { echo ' in <b>', $location['file'], '</b> on line <b>', $location['line'], '</b>'; } } echo '</div>'; }
$targetFolders[] = $folder; } } } if (count($targetFolders) == 0) { echo " FAILED: No folders detected.\n"; echo 'Usage: php ' . basename(__FILE__) . " folder1 [folder2]\n"; echo " \n"; return false; } $modules = Framework::getModules(); $folders = []; foreach ($modules as $folder => $info) { $modulePath = $info['path']; if (is_dir($modulePath . 'public')) { if (\Sledgehammer\array_value($info, 'app')) { $folders[$modulePath . 'public'] = ''; } else { $folders[$modulePath . 'public'] = '/' . $folder; } } } foreach ($targetFolders as $targetFolder) { $fileCount = 0; echo "\nPopulating /" . $targetFolder . " ...\n"; foreach ($folders as $folder => $targetSuffix) { $targetPath = \Sledgehammer\PATH . $targetFolder . $targetSuffix; \Sledgehammer\mkdirs($targetPath); $fileCount += copydir($folder, $targetPath, array('.svn')); } echo ' ' . $fileCount . " files copied\n";
/** * Get the value of a specific response header. * Requires the CURLOPT_HEADER option. * * @param string $header Name of the header (case-insensitive) * * @return string|null */ public function getHeader($header) { $headers = array_change_key_case($this->getHeaders()); return \Sledgehammer\array_value($headers, strtolower($header)); }
/** * @param string $label * @param string $message * @param string $overwrite */ public static function send($label, $message, $overwrite = false) { if (self::isEnabled() === false) { return; } if (preg_match('/^(?<label>[a-z0-9\\-]+)(?<suffix>\\.[0-9]+)?$/i', $label, $match) == false) { \Sledgehammer\notice('Label: "' . $label . '" in invalid', 'A label may contain number, letters and "-"'); return; } $number = \Sledgehammer\array_value(self::$increments, $match['label']); if (isset($match['suffix'])) { // Has a suffix? $labelSuffix = $match[0]; if ($overwrite === false) { \Sledgehammer\notice('Overwrite flag required for label: "' . $label . '"'); return; } if ($number <= substr($match['suffix'], 1)) { self::$increments[$match['label']] = substr($match['suffix'], 1) + 1; } } elseif ($overwrite === false) { if ($number) { $label .= '.' . $number; } self::$increments[$match['label']] = $number + 1; } if (headers_sent($file, $line)) { if ($file == '' && $line == 0) { $location = ''; } else { $location = ', output started in ' . $file . ' on line ' . $line; } \Sledgehammer\notice('Couldn\'t sent header(s)' . $location); return; } $value = base64_encode($message); $length = strlen($value); // Prevent 325 net::ERR_RESPONSE_HEADERS_TOO_BIG in Google Chrome. if (self::$bytesSent + $length >= 240000) { // >= 235KiB? if (self::$bytesSent < 239950) { call_user_func(self::$headerAdd, 'DebugR-' . $label . ': ' . base64_encode('DebugR: TOO_MUCH_DATA')); } return; } if ($length <= 4000) { // Under 4KB? (96B for the label) $header = 'DebugR-' . $label . ': '; call_user_func(self::$headerAdd, $header . $value); self::$bytesSent += strlen($header) + $length; } else { // Send in 4KB chunks. call_user_func(self::$headerRemove, 'DebugR-' . $label); $chunks = str_split($value, 4000); foreach ($chunks as $index => $chunk) { $header = 'DebugR-' . $label . '.chunk' . $index . ': '; call_user_func(self::$headerAdd, $header . $chunk); self::$bytesSent += strlen($header); } self::$bytesSent += $length; } }