/**
  * Find the extension of a file from its filename and
  * get it in lower case
  *
  * @param string $file : name of the file
  * @return string : extension of the file in lower case
  */
 public static function fileExtension($file)
 {
     if (Steelcode_String_Helper::isNull($file)) {
         return null;
     } else {
         $ext = end(explode('.', $file));
         return strtolower($ext);
     }
 }
 /**
  * Get the current script url without the script name
  *
  * @return string
  */
 public static function url()
 {
     $hostUrl = self::host();
     if (!empty($scriptName = Steelcode_Server_Vars::getVar('SCRIPT_NAME'))) {
         $arr = explode('/', $scriptName);
         $arrLen = count($arr);
         if ($arrLen <= 2) {
             return $hostUrl;
         } else {
             if (Steelcode_String_Helper::isNull($arr[0])) {
                 unset($arr[0]);
             }
             unset($arr[$arrLen - 1]);
             $newUrl = implode('/', $arr);
             return "{$hostUrl}/{$newUrl}";
         }
     }
     return $hostUrl;
 }
 /**
  * Create project components according to configuration
  *
  * @throws Steelcode_Project_Exception
  */
 public function execute()
 {
     if (!isset($this->_config['component'])) {
         throw new Steelcode_Project_Exception('Component name not found');
     }
     try {
         switch ($this->_config['component']) {
             case 'application':
                 $this->_objComponent = new Steelcode_Project_Application();
                 break;
             case 'domain':
                 $this->_objComponent = new Steelcode_Project_Domain();
                 break;
             case 'controller':
                 if (!isset($this->_config['parent']) || Steelcode_String_Helper::isNull($this->_config['parent'])) {
                     throw new Steelcode_Project_Exception('* Error! Domain is not specified for creating controller');
                 }
                 $this->_objComponent = new Steelcode_Project_Controller();
                 $this->_objComponent->setDomain($this->_config['parent']);
                 break;
             case 'model':
                 $this->_objComponent = new Steelcode_Project_Model();
                 break;
             case 'layout':
                 $this->_config['name'] = "layout-{$this->_config['name']}";
                 $this->_objComponent = new Steelcode_Project_Layout();
                 break;
             case 'config':
                 $this->_objComponent = new Steelcode_Project_Config();
                 break;
             case 'bootstrap':
                 $this->_objComponent = new Steelcode_Project_Bootstrap();
                 break;
             default:
                 throw new Steelcode_Project_Exception("* Error! Invalid component name '{$this->_config['component']}' found");
                 break;
         }
         $this->_objComponent->setLocation($this->_location);
         $this->_objComponent->create($this->_config['name']);
         $this->_messageText = $this->_objComponent->getMessageText();
     } catch (Steelcode_Project_Exception $ex) {
         $this->_errorText = $ex->getMessage();
         throw $ex;
     }
 }
 /**
  * Get file contents from $_FILE server variable and move
  * the uploaded file to a location specified in $location
  *
  * @param string $location (optional) : upload media directory
  * @throws Steelcode_Media_Exception
  */
 public function uploadFile($location = null)
 {
     if (!isset($_FILES) || !is_array($_FILES)) {
         $this->_lastFileError = 'No files are selected to upload';
         throw new Steelcode_Media_Exception($this->_lastFileError);
     }
     if (Steelcode_String_Helper::isNull($location)) {
         $mediaDir = $this->_mediaLocation;
     } else {
         $mediaDir = $location;
     }
     if (Steelcode_String_Helper::isNull($mediaDir)) {
         $this->_lastFileError = 'No destination is set to move uploaded files';
         throw new Steelcode_Media_Exception($this->_lastFileError);
     }
     $fileName = $_FILES[$this->_inputIndex]['name'];
     $fileSize = $_FILES[$this->_inputIndex]['size'];
     $fileType = $_FILES[$this->_inputIndex]['type'];
     $fileExt = Steelcode_Media_Helper::fileExtension($fileName);
     if ($fileSize > $this->_fileMaxSize) {
         $this->_lastFileError = 'File size is larger than the limit';
         throw new Steelcode_Media_Exception($this->_lastFileError);
     }
     if (false === in_array($fileExt, $this->_extensionList)) {
         $this->_lastFileError = 'You can not upload this kind of file';
         throw new Steelcode_Media_Exception($this->_lastFileError);
     }
     do {
         $shaName = sha1($fileName);
         $shaName = str_shuffle($shaName) . ".{$fileExt}";
         $newFilePath = "{$mediaDir}{$shaName}";
     } while (file_exists($newFilePath));
     if ($_FILES[$this->_inputIndex]['error'] > 0) {
         $this->_lastFileError = 'Server set an error code (' . $_FILES[$this->_inputIndex]['error'] . ')';
         $this->_lastFileErrorCode = $_FILES[$this->_inputIndex]['error'];
         throw new Steelcode_Media_Exception($this->_lastFileError, $this->_lastFileErrorCode);
     }
     if (move_uploaded_file($_FILES[$this->_inputIndex]['tmp_name'], $newFilePath)) {
         $this->_lastFileName = $fileName;
         $this->_lastFileType = $fileType;
         $this->_lastFileExt = $fileExt;
         $this->_lastFileSize = $fileSize;
         $this->_lastFileHash = $shaName;
     } else {
         $this->_lastFileError = 'There was an error while uploading the file';
         throw new Steelcode_Media_Exception($this->_lastFileError);
     }
 }
define('INCLUDE_PATH', 'Steelcode/');
require_once INCLUDE_PATH . 'steelcode.php';
$opt = new Steelcode_Console_Getopt();
/* Array of available short options */
$shortOpts = array('l' => $opt::REQUIRED, 'a' => $opt::REQUIRED, 'c' => $opt::REQUIRED, 'n' => $opt::REQUIRED, 'p' => $opt::OPTIONAL);
/* Array of available long options */
$longOptions = array('location' => $opt::REQUIRED, 'action' => $opt::REQUIRED, 'component' => $opt::REQUIRED, 'name' => $opt::REQUIRED, 'parent' => $opt::OPTIONAL);
$opt->setShortOption($shortOpts);
$opt->setLongOption($longOptions);
$options = $opt->getOptions();
try {
    if (isset($options['l']) || isset($options['location'])) {
        $location = isset($options['l']) ? $options['l'] : $options['location'];
    } else {
        exit("No target location specified!\n");
    }
    $config = array('action' => isset($options['a']) ? $options['a'] : $options['action'], 'component' => isset($options['c']) ? $options['c'] : $options['component'], 'name' => isset($options['n']) ? $options['n'] : $options['name']);
    if (isset($options['p']) && $options['p'] != false) {
        $config['parent'] = $options['p'];
    } elseif (isset($options['parent']) && $options['parent'] != false) {
        $config['parent'] = $options['parent'];
    }
    if (isset($config['parent']) && Steelcode_String_Helper::isNull($config['parent'])) {
        unset($config['parent']);
    }
    $project = new Steelcode_Project_Handler($location, $config);
    $project->execute();
    echo "{$project->getMessageText()}\n";
} catch (Steelcode_Project_Exception $ex) {
    echo "{$ex->getMessage()}\n";
}
 /**
  * Set a cookie
  *
  * @param string $name
  * @param string $value
  * @param bool $httpOnly
  * @param string $expires
  * @param string $path
  * @param string $domain
  */
 public function setCookie($name, $value, $httpOnly = true, $expires = '', $path = '/', $domain = '')
 {
     $expires = $expires === '' ? Steelcode_Date_Helper::UTCDate('+7 day', 'D, j M Y H:i:s e') : Steelcode_Date_Helper::UTCDate($expires, 'D, j M Y H:i:s e');
     $cookie = "{$name}={$value};Path={$path};Expires={$expires}";
     $cookie .= Steelcode_String_Helper::isNull($domain) ? '' : ";Domain={$domain}";
     $cookie .= Steelcode_Ssl_Helper::isSSL() ? ';Secure' : '';
     $cookie .= $httpOnly ? ';HttpOnly' : '';
     exit($cookie);
     //$this->setFields( 'Set-Cookie', $cookie );
 }