public
Description: Plug-in / filter for Frog CMS: WYMeditor
Homepage:
Clone URL: git://github.com/them/frog_wymeditor.git
them (author)
Fri Jul 17 11:35:03 -0700 2009
commit  77d596704d311f737f6030e5b6f89c1c522459bc
tree    e27ab2dd66dab45b6ba34ebbcbea40b7bdeef693
parent  5bcaa56618e830f09d4ac366d150f1914c068349
frog_wymeditor / WymeditorController.php
100644 140 lines (120 sloc) 4.063 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
class WymeditorController extends PluginController {
  // Plugin information
  const PLUGIN_ID = "wymeditor";
 
  // Location of the view folder
  const VIEW_FOLDER = "wymeditor/views/";
  const PLUGIN_REL_VIEW_FOLDER = "../../plugins/wymeditor/views/";
 
  // Error types
  const ERROR_EMPTY = 'empty';
  const ERROR_NOT_EXISTING = 'not_existing';
  const ERROR_NOT_VALID = 'not_valid';
 
  /**
* Create a new controller instance and apply the sidebar to the backend.
*/
  public function __construct() {
    AuthUser::load();
    if (!(AuthUser::isLoggedIn())) {
        redirect(get_url('login'));
    }
    
    $this->setLayout('backend');
    $this->assignToLayout('sidebar', $this->create_view('sidebar'));
  }
 
  /**
* Settings function to delete the metadata table.
*/
  public function settings() {
    // TODO: There should be a better way to control access rights than copying every time
    if (!AuthUser::hasPermission('administrator') && !AuthUser::hasPermission('developer')) {
      redirect(get_url());
    }
 
    // Show the error in the view
    $error = null;
    // Previous settings
    $stylesheet = Plugin::getSetting('stylesheet', self::PLUGIN_ID);
    
    // Update request
    if (get_request_method() == 'POST' &&
     isset($_POST['stylesheet']) &&
     !empty($_POST['stylesheet'])) {
      // Give the user a second chance and preserve the value in the form
      $stylesheet = $_POST['stylesheet'];
      // Little security by only allowing '.css' files
      if (strrpos($_POST['stylesheet'], '.css') == (strlen($_POST['stylesheet']) - 4)) {
       Plugin::setSetting('stylesheet', $stylesheet, self::PLUGIN_ID);
      }
      else {
        $error = self::ERROR_NOT_VALID;
      }
    }
    
    if (!$error) {
      if (empty($stylesheet)) {
       $error = self::ERROR_EMPTY;
      }
      elseif (!is_readable(FROG_ROOT.$stylesheet)) {
        $error = self::ERROR_NOT_EXISTING;
      }
    }
    
    $this->display('settings', array(
      'error' => $error,
      'stylesheet' => $stylesheet
    ));
  }
 
  public function stylesheet() {
    $stylesheet = Plugin::getSetting('stylesheet', self::PLUGIN_ID);
    
    if (!empty($stylesheet) && is_readable(FROG_ROOT.$stylesheet)) {
      header('content-type: text/css');
      print file_get_contents(FROG_ROOT.$stylesheet);
    }
    else {
      header("HTTP/1.0 404 Not Found");
      exit();
    }
  }
  
  public function environment() {
    header("content-type: text/javascript");
    $this->setLayout(null);
    
    $this->display('environment', array(
      'language' => I18n::getLocale(),
      'stylesheet' => get_url('plugin/'.self::PLUGIN_ID.'/stylesheet'),
    ));
  }
 
  /**
* Private function that provide the default values for the view.
*
* @return default values
*/
  private function get_default_view_vars() {
    $vars = array();
    
    $vars['plugin_id'] = self::PLUGIN_ID;
    $vars['plugin_url'] = get_url('plugin/'.self::PLUGIN_ID.'/');
    
    return $vars;
  }
 
  /**
* Overwrite the render function to enforce that some variables are
* available for the whole view artifacts.
* Simplify the view file handling by prefixing the file with the
* plugin directory.
*
* @param view the view file
* @param vars parameter for the views
* @return the view
*/
  /*@overwrite('render')*/
  public function render($view, $vars=array()) {
    $vars = array_merge($this->get_default_view_vars(), $vars);
 
    /* We only render views for this plugin. So add the prefix of the view folder to every view file. */
    return parent::render(self::VIEW_FOLDER.$view, $vars);
  }
 
  /**
* View factory for the controller and the view.
*
* @param view the filename without the postfix
* @param vars the template vars
* @return a view object
*/
  public function create_view($view, $vars=array()) {
    $vars = array_merge($this->get_default_view_vars(), $vars);
      
    return new View(self::PLUGIN_REL_VIEW_FOLDER.$view, $vars);
  }
}
?>