/** * 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; }
/** * 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); }
/** * 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)); } }
/** * Initializes the TextMessage object. Pre-sets all required variables * * // Parameters for this functions can be specified by: * - app_phone_twilio_account_sid * - app_phone_twilio_account_token * - app_phone_default_from_number * * * @param string $twilio_account_sid * // * * @param string $twilio_account_token * // * * @return null */ protected function initialize($twilio_account_sid = null, $twilio_account_token = null) { $twilio_settings = \sfConfig::get('app_phone_twilio', array()); if (array_key_exists('account_sid', $twilio_settings)) { $twilio_account_sid = $twilio_settings['account_sid']; } if (array_key_exists('account_token', $twilio_settings)) { $twilio_account_token = $twilio_settings['account_token']; } if (($default_from_number = \sfConfig::get('app_phone_default_from_number', null)) !== null) { $this->setFrom($default_from_number); } \Altumo\Validation\Strings::assertNonEmptyString($twilio_account_sid, '$twilio_account_sid is required'); \Altumo\Validation\Strings::assertNonEmptyString($twilio_account_token, '$twilio_account_token is required'); // create Twilio Client $twilio_client = new \Services_Twilio($twilio_account_sid, $twilio_account_token); $this->setTwilioClient($twilio_client); $this->setEnabled(\sfConfig::get('app_phone_enabled', false)); return null; }
/** * 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; }
/** * 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; }
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)); }
/** * 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); }
/** * 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; }
/** * Adds a "bcc" recipient to this message * * * @param string $email_address //email address of cc recipient * @param string $name //name of recipient * * @return \sfAltumoPlugin\Email\Message */ public function setBcc($email_address, $name = null) { $email_address = \Altumo\Validation\Emails::assertEmailAddress($email_address, '"' . $email_address . '" is not a valid email address for setBcc()'); if (!is_null($name)) { \Altumo\Validation\Strings::assertNonEmptyString($name); } $this->bcc_recipients[$email_address] = $name; return $this; }
/** * 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; }