Ejemplo n.º 1
0
 public function code($params = null)
 {
     if (!is_array($params)) {
         // This is a GET for a dir or file. -- cwells
         if (is_null($params)) {
             $params = '';
             $path = '.';
         } else {
             $path = $params;
         }
         require_once \CWA\LIB_PATH . 'cwa/io/FileManager.php';
         $fileManager = new \CWA\IO\FileManager('.');
         if (!$fileManager->exists($params)) {
             throw new InvalidArgumentException('The specified file does not exist within this application.', 400);
         } else {
             if ($fileManager->isFile($params)) {
                 $this->loadView('code-file');
                 $this->view->setData(array('FileContents' => file_get_contents($path), 'FilePath' => $path, 'ReadOnly' => !is_writable($path)));
             } else {
                 if ($fileManager->isDirectory($params)) {
                     $this->loadView('code-dir');
                     $this->view->setData(array('DirectoryPath' => $path, 'Directory' => $fileManager->getDirectoryListing($params), 'PathPrefix' => "{$this->pathInURL}/code/" . (empty($params) ? '' : "{$params}/")));
                 } else {
                     throw new InvalidArgumentException('You must specify a valid file or directory path.', 400);
                 }
             }
         }
     } else {
         // This is a POST with the contents of a file. -- cwells
         if (!isset($params['file-path']) || empty($params['file-path'])) {
             throw new InvalidArgumentException('You must specify a file path.', 400);
         } else {
             if (strpos(realpath($params['file-path']), realpath('.')) !== 0) {
                 throw new InvalidArgumentException('The specified file does not exist within this application.', 400);
             } else {
                 if (!is_file($params['file-path'])) {
                     throw new InvalidArgumentException('You must specify a valid file path.', 400);
                 } else {
                     if (file_put_contents($params['file-path'], utf8_encode($params['file-contents'])) === false) {
                         $this->loadView('code-file');
                         $this->view->setStatus('Failed to update the specified file.', 500);
                         $this->view->setData(array('FileContents' => $params['file-contents'], 'FilePath' => $params['file-path']));
                     } else {
                         $this->loadView('code-file-save');
                         $this->view->setData('FilePath', $params['file-path']);
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 2
0
 public function save(array &$properties)
 {
     if (empty($properties) || !is_array($properties)) {
         throw new InvalidArgumentException('You must provide the values to update.', 400);
     }
     // Use IsPublic to determine whether to set or clear the Published datetime. -- cwells
     if (empty($properties['IsPublic'])) {
         // Not published.
         $properties['Published'] = null;
     } else {
         if ($properties['IsPublic'] === '1' && empty($properties['Published'])) {
             // New publish.
             $properties['Published'] = date(\CWA\DB\DATETIME_PHP_TO_DB);
         }
     }
     unset($properties['IsPublic']);
     // Since it's not a real property on the object.
     // The Summary should be plain text with no double quotes. -- cwells
     $properties['Summary'] = str_replace('"', "'", strip_tags($properties['Summary']));
     // If this is an existing blog post and the slug changed, move associated images. -- cwells
     if (!empty($properties['ID']) && $properties['OldSlug'] !== $properties['Slug']) {
         require_once \CWA\LIB_PATH . 'cwa/io/FileManager.php';
         $fileManager = new \CWA\IO\FileManager("../public/images{$this->pathInURL}");
         $oldPath = $properties['OldSlug'][0] . '/' . $properties['OldSlug'];
         $newPath = $properties['Slug'][0] . '/' . $properties['Slug'];
         if ($fileManager->isDirectory($oldPath)) {
             if (!$fileManager->isDirectory(dirname($newPath))) {
                 $fileManager->mkdir(dirname($newPath));
                 // Error check happens below. -- cwells
             }
             if (!$fileManager->rename($oldPath, $newPath)) {
                 throw new InvalidArgumentException('Failed to rename the image directory.', 500);
             }
             // Update any URLs that refer to the old path in the content. -- cwells
             $properties['Body'] = str_replace("/images{$this->pathInURL}/{$oldPath}/", "/images{$this->pathInURL}/{$newPath}/", $properties['Body']);
         }
     }
     unset($properties['OldSlug']);
     parent::save($properties);
 }