/** * Retrieves a list of processes that are currently running in the system * and that are visible to the system user that is executing this script. * * Optionally, results can be filtered by user_id (uid) or by command using a * regular expression. * * @return array * // array of processes that are running. Each entry looks like this: * array( * process_id => 12345, * user_id => theuser, * command => /user/bin/hello --there * ) */ public static function getRunningProcesses($filter_by_user_id = null, $filter_by_command_regex = null) { // Validate parameters if (!is_null($filter_by_user_id)) { $filter_by_user_id = \Altumo\Validation\Strings::assertNonEmptyString($filter_by_user_id, '$filter_by_user_id expects non-empty string'); } if (!is_null($filter_by_command_regex)) { $filter_by_command_regex = \Altumo\Validation\Strings::assertNonEmptyString($filter_by_command_regex, '$filter_by_command_regex expects non-empty string'); } self::assertPsExists(); // get a list of all system processes $all_processes = `ps -eo pid,user,cmd`; // parse the list into fields preg_match_all('/^\\s*(?P<pid>\\d+)\\s+(?P<uid>.*?)\\s+(?P<cmd>.*?)$/m', $all_processes, $matches); // parse into an array $output = array(); foreach ($matches["pid"] as $process_index => $process_id) { $user_id = $matches["uid"][$process_index]; $command = $matches["cmd"][$process_index]; // filter by user id if (!is_null($filter_by_user_id)) { if (strcmp($filter_by_user_id, $user_id) != 0) { continue; } } // filter by command if (!is_null($filter_by_command_regex)) { if (!preg_match($filter_by_command_regex, $command)) { continue; } } $output[] = array('process_id' => $process_id, 'user_id' => $user_id, 'command' => $command); } return $output; }
/** * Adds an item * * @param double $value * @param string $label * * @throws \Exception if $value doesn't validate * @throws \Exception if $label doesn't validate * * @return \sfAltumoPlugin\Geckoboard\Widget\FunnelWidget */ public function addItem($value, $label) { $value = \Altumo\Validation\Numerics::assertDouble($value); $label = \Altumo\Validation\Strings::assertString($label); $item = new \stdClass(); $item->value = $value; $item->label = $label; $this->items[] = $item; return $this; }
/** * Sets this contact's state by state ISO code. * * @param string $iso_code * @throws \Exception //if ISO code does not exist * @throws \Exception //if ISO code is not a non-empty * string */ public function setStateIsoCode($iso_code) { $iso_code = \Altumo\Validation\Strings::assertNonEmptyString($iso_code); $iso_code = strtoupper($iso_code); $state = \StatePeer::retrieveByCode($iso_code); if (!$state) { throw new \Exception('Unknown ISO code: ' . $iso_code); } $this->setState($state); }
/** * * @param string $text * @param int $type * * @throws \Exception if maximum number of items reached * * @return \sfAltumoPlugin\Geckoboard\Widget\TextWidget */ public function addItem($text, $type = self::TYPE_NONE) { $item = new \stdClass(); // pre-validate and set text $item->text = \Altumo\Validation\Strings::assertString($text); // pre-validate and set type $item->type = \Altumo\Validation\Numerics::assertInteger($type); if (count($this->getItems()) > self::MAXIMUM_ITEM_COUNT - 1) { throw new \Exception("Cannot add new item, maximum count reached"); } // add to items $this->items[] = $item; return $this; }
/** * Throws an exception if $object is not an instance of $class_name. Inheritance * will be accounted for, * * @param mixed $object * * @param string $class_name * //name of the class to match. e.g. "User" * * @param \Exception $exception_message * //custom Exception message to throw. * If null, default message will be used. * * * @throws \Exception // - if $object is not an instance of $class_name and is not null. * // - if $class_name is not a string * * @return mixed */ public static function assertObjectInstanceOfClass(&$object, $class_name, $exception_message = null) { \Altumo\Validation\Strings::assertNonEmptyString($class_name); //remove global namespace if included in $class_name if ($class_name[0] == '\\') { $class_name = substr($class_name, 1); } if (is_null($exception_message)) { $exception_message = 'object of type "' . $class_name . '" expected, but "' . get_class($object) . '" given.'; } if (!is_object($object) || !is_a($object, $class_name)) { throw new \Exception($exception_message); } return $object; }
/** * Main controller for all git hooks. Invokes the proper handler based * on the hook name. * * @param string $hook_name * * @throws \sfCommandException //if hook wasn't known or couldn't * be handled * * @throws \sfCommandException //if hook wasn't a non-empty string */ public function handle($hook_name) { try { \Altumo\Validation\Strings::assertNonEmptyString($hook_name); } catch (\Exception $e) { throw new \sfCommandException('Hook name was not a non-empty string.'); } switch ($hook_name) { case 'post-commit': $this->onPostCommit(); break; case 'post-merge': //git pull $this->onPostMerge(); break; default: throw new \sfCommandException(sprintf('Hook "%s" does not have a handler.', $hook_name)); } }
/** * If the $key specified exists in $array, then * $callback will be invoked with $array[$key] as a parameter. * * @param array $array * @param string $key // Non-empty name of a key to find in $array * @param array $callback // A php "callable" to use as a callback * * @throws \Exception //if $array is not an array, $key is not a string or $callback is not callable * @return mixed // whatever the response of $callback was (if invoked) or false otherwise. */ public static function callIfKeyExists($array, $key, $callback) { //validate if (!is_array($array)) { throw new \Exception('The first parameter must be an array.'); } try { \Altumo\Validation\Strings::assertNonEmptyString($key); } catch (\Exception $e) { throw new \Exception('The second parameter must be a non-empty string.'); } if (!is_callable($callback)) { throw new \Exception('The third parameter must be a callable'); } // If key is set, call apply function if (array_key_exists($key, $array)) { return call_user_func($callback, $array[$key]); } return false; }
/** * (non-PHPdoc) * @see BaseSystemEventSubscription::save() */ public function save(PropelPDO $con = null) { if ($this->isEmailSubscription()) { // if subscription is for email, // validate remote url as email $this->assertEmailValid($this->getRemoteUrl()); // make sure template is set \Altumo\Validation\Strings::assertNonEmptyString($this->getTemplate(), "A template must be set for email subscriptions."); // make sure subject is set \Altumo\Validation\Strings::assertNonEmptyString($this->getSubject(), "A subject must be set for email subscriptions."); } elseif ($this->isRequestSubscription()) { // if subscription is for a request, // validate remote url as url $this->assertRemoteUrlValid($this->getRemoteUrl()); } parent::save($con); }
/** * Removes all non-alphabetic characters from the given string. * * @throws \Exception //if string is not a string * @param string $string */ public static function stripNonAlphaCharacters($string) { \Altumo\Validation\Strings::assertString($string); return preg_replace('/[^a-zA-Z]/', '', $string); }
/** * Stores the SSL certificate in a temporary file and returns its path. * * When making a request with an SSL certificate, curl requires that * certificate to be in a file. This function stores the certificate in a * temporary file and returns its path. * * * @see deleteSslCertificateTempFile * // This method will be called after the certificate has been used in order * // to clean up the file that was created. * * @throws \Exception * // If the temp certificate file cannot be created or written * * @param string $temp_path */ protected function createSslCertificateTempFile() { // If a certificate temp file has already been created, delete it $this->deleteSslCertificateTempFile(); // Gets certificate data $ssl_cert_data = $this->getSslCertificateData(); \Altumo\Validation\Strings::assertNonEmptyString($ssl_cert_data); // Use system temp directory path $temp_path = sys_get_temp_dir(); // Make a new filename for the certificate using its own hash and a random string $temp_filename = $temp_path . '/altumo_pem_' . sha1($ssl_cert_data) . '_' . \Altumo\String\String::generateRandomString(4); // Write the contents of the certificate to the temporary file if (file_put_contents($temp_filename, $this->getSslCertificateData()) === false) { throw new \Exception('Unable to write to temporary certificate file.'); } $this->setSslCertificateTempFile($temp_filename); return $temp_filename; }
/** * Starts a CMS fragment by using output buffering. It will also create a new model and save it if it doesn't exist. * * @param $tag * // tag is a unique identifier for a given fragment * * @param string $chrome_attributes * // attributes of the element that wraps the fragment. This will just be added to the element's definition. * * @throws \Exception * * @return void */ public function startCmsFragment($tag, $chrome_attributes = '', $fragment_type = 'text') { $chrome_attributes = \Altumo\Validation\Strings::assertString($chrome_attributes, '$chrome_attributes expects a string'); // prevent nested fragments if ($this->hasCurrentCmsFragment()) { throw new \Exception('Already inside "' . $this->getCurrentCmsFragment()->getTag() . '". Cannot start another fragment.'); } // retrieve fragment $fragment = \CmsFragmentPeer::retrieveByTag($tag); // if this is a new segment, create a new one. if (is_null($fragment)) { $fragment = \CmsFragmentPeer::getStubCmsFragment($tag, $fragment_type); } // set chrome attributes $fragment->setChromeAttributes($chrome_attributes); // set current fragment $this->setCurrentCmsFragment($fragment); // start output buffering ob_start(); $this->cms_buffer_count++; }
/** * Setter for the collection_namespace field on this PivotalTrackerPackage. * * @param string $collection_namespace * @throws \Exception //if $collection_namespace is not a string */ public function setCollectionNamespace($collection_namespace) { $collection_namespace = \Altumo\Validation\Strings::assertString($collection_namespace); $this->collection_namespace = $collection_namespace; }
/** * @param string $v * * @throws \Exception if $v doesn't validate * * @return \sfAltumoPlugin\Geckoboard\Widget\NumberAndSecondaryStatWidget */ public function setMaxText($v) { $this->max_text = is_null($v) ? $v : \Altumo\Validation\Strings::assertString($v, 'Max text expects a string'); return $this; }
/** * @param string $descriptor * * @return \sfAltumoPlugin\Geckoboard\Widget\LineChartWidget * * @throws \Exception if descriptor fails to validate */ public function addAxisYDescriptor($descriptor) { $this->axis_y_descriptors[] = \Altumo\Validation\Strings::assertString($descriptor); return $this; }
/** * @param string $v * * @return \sfAltumoPlugin\Geckoboard\Widget\NumberAndSecondaryStatWidget * * @throws \Exception if value doesn't validate */ public function setSecondaryText($v) { $this->secondary_text = \Altumo\Validation\Strings::assertString($v); return $this; }
protected function composeFrequentCronCommand($command, $frequency) { $command = \Altumo\Validation\Strings::assertNonEmptyString($command, '$command expects non-empty string'); $frequency = \Altumo\Validation\Numerics::assertPositiveInteger($frequency, '$frequency expects positive integer'); return sprintf(self::getFrequentCronPath() . " --command=%s --frequency=%s", escapeshellarg($command), escapeshellarg($frequency)); }
/** * Sets the subject of this TextMessage * * * @param string $subject * * @return \sfAltumoPlugin\Phone\TextMessage */ public function setText($text) { $this->text = \Altumo\Validation\Strings::assertString($text); return $this; }
/** * Set the content of this Message from a string, or add a content part * in a different format. * * E.g. if also using setContent(), this could be used to add a third part * in 'text/json' if you wanted to do that. Otherwise, it can be used without * setContent() in order to send emails without using partials. * * * @param string $content * @param array $encoding //content encoding. e.g. text/html, text/plain, etc. * * @return \sfAltumoPlugin\Email\Message */ public function setContentString($content, $encoding = 'text/plain') { $content = \Altumo\Validation\Strings::assertString($content, 'Invalid email content. String expected.'); $encoding = \Altumo\Validation\Strings::assertString($encoding, 'Invalid email content encoding. String expected.'); $this->content_parts[] = array($content, $encoding); return $this; }
/** * Sets the application's base namespace as well as any subnamespace that will * be used by the frontend. * * Example for "myApp" * * - myApp * - myApp.model * - myApp.view * * @param string $namespace * // e.g. myApp * * @throws \Exception * // if $namespace is not a non-empty string. * * @return App * // this App. */ protected function setNamespace($namespace) { // clean up namespace before use $this->resetNamespaceHeap(); // validate app namespace $this->namespace = \Altumo\Validation\Strings::assertNonEmptyString($namespace, '$namespace must be a non-empty string'); // add default app sub-namespaces $sub_namespaces = array('model', 'view', 'app', 'config'); // add main app namespace to the heap $this->declareNamespace($this->namespace); // add sub-namespaces to the heap foreach ($sub_namespaces as $sub_namespace) { $this->declareNamespace($this->namespace . '.' . $sub_namespace); } // add altumo namespaces to the heap $this->declareNamespace('altumo.app.App'); return $this; }
/** * Setter for the token field on this PivotalTrackerApiClient. * * @param string $token * @throws \Exception //if $token is not a non-empty string */ public function setToken($token) { $token = \Altumo\Validation\Strings::assertNonEmptyString($token); $this->token = $token; }
/** * Runs a select query with optional bindings and returns data * * @param string $query * // what does this query look like? * * @param array $bindings * // what does this array look like? * * @param integer $return_type * // what are the options? * * @return array * // what's the array keyed like? what are the values? */ protected function runSelectQuery($query, $bindings = array(), $return_type = PDO::FETCH_ASSOC) { // validate query $query = \Altumo\Validation\Strings::assertNonEmptyString($query); // ensure $bindings is an array if (!is_array($bindings)) { throw new \Exception('$bindings is expected to be an Array'); } // validate $return_type if possible // .... $connection = $this->getConnection(); $statement = $connection->prepare($query); $statement->execute($bindings); $rows = $statement->fetchAll($return_type); return $rows; }