ContentController   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 221
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 4
dl 0
loc 221
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A accessRules() 0 12 1
B filters() 0 35 5
B actionIndex() 0 36 5
B actionPassword() 0 57 7
B actionList() 0 34 1
A actionNR() 0 14 3
1
<?php
2
3
class ContentController extends CiiController
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
	/**
6
	 * Base filter, allows logged in and non-logged in users to cache the page
7
	 */
8
	public function filters()
9
	{
10
		$id = Yii::app()->getRequest()->getQuery('id');
11
12
		if ($id != NULL)
13
		{
14
			$vid =  Yii::app()->getRequest()->getQuery('vid');
15
			return array(
16
				'accessControl',
17
				array(
18
					'CHttpCacheFilter + index',
19
					'cacheControl'=>Cii::get(Yii::app()->user->id) == NULL ? 'public' : 'private' .', no-cache, must-revalidate',
20
					'etagSeed' => $id.$vid
21
				),
22
				array(
23
					'COutputCache + index',
24
					'duration' => YII_DEBUG ? 1 : 86400, // 24 hour cache duration
25
					'varyByParam' => array('id', 'vid'),
26
					'varyByLanguage' => true,
27
					'varyByExpression' => 'Yii::app()->user->isGuest'
28
				)
29
			);
30
		}
31
32
		return CMap::mergeArray(parent::filters(), array(array(
33
			'COutputCache + list',
34
			'duration' => YII_DEBUG ? 1 : 86400,
35
			'varyByParam' => array('page'),
36
			'varyByLanguage' => true,
37
			'dependency' => array(
38
				'class'=>'CDbCacheDependency',
39
				'sql'=>'SELECT MAX(updated) FROM content',
40
			)
41
		)));
42
	}
43
44
45
	/**
46
	 * Specifies the access control rules.
47
	 * This method is used by the 'accessControl' filter.
48
	 * @return array access control rules
49
	 */
50
	public function accessRules()
51
	{
52
		return array(
53
			array('allow',  // Allow all users to any section
54
				'actions' => array('index', 'password', 'list'),
55
				'users'=>array('*'),
56
			),
57
			array('deny',  // deny all users
58
				'users'=>array('*'),
59
			),
60
		);
61
	}
62
63
	/**
64
	 * Handles all incoming requests for the entire site that are not previous defined in CUrlManager
65
	 * Requests come in, are verified, and then pulled from the database dynamically
66
	 * @param $id	- The content ID that we want to pull from the database
67
	 **/
68
	public function actionIndex($id=NULL, $vid=NULL)
0 ignored issues
show
Coding Style introduced by
actionIndex uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
69
	{
70
		// Set the ReturnURL to this page so that the user can be redirected back to here after login
71
		Yii::app()->user->setReturnUrl($this->beforeCiiAction($id));
72
73
		// Retrieve the data
74
		$content = Content::model()->findByPk($id);
75
76
		if ($content->status != 1 || !$content->isPublished())
77
			throw new CHttpException(404, Yii::t('ciims.controllers.Content', 'The article you specified does not exist. If you bookmarked this page, please delete it.'));
78
79
		// Check for a password
80
		if (!empty($content->password))
81
		{
82
			// Check SESSION to see if a password is set
83
			$tmpPassword = Cii::get(Cii::get(Cii::get($_SESSION, 'password', array()), $id, array()), 'password', NULL);
84
85
			if ($tmpPassword != $content->password)
86
				$this->redirect(Yii::app()->createUrl('/content/password/' . $id));
87
		}
88
89
		// Parse Metadata
90
		$this->setLayout($content->layout);
91
92
		$this->setPageTitle(Yii::t('ciims.controllers.Content', '{{app_name}} | {{label}}', array(
93
			'{{app_name}}' => Cii::getConfig('name', Yii::app()->name),
94
			'{{label}}'    => $content->title
95
		)));
96
97
		$this->params['meta']['description'] = $content->extract;
98
		$this->render($content->view, array(
99
			'id' 	=> $content->id,
100
			'data' 	=> $content,
101
			'meta' 	=> $content->parseMeta($content->id)
102
		));
103
	}
104
105
	/**
106
	 * Forces a password to be assigned before the user can proceed to the previous page
107
	 * @param $id - ID of the content we want to investigate
108
	 **/
109
	public function actionPassword($id=NULL)
0 ignored issues
show
Coding Style introduced by
actionPassword uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
actionPassword uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
110
	{
111
		$this->setPageTitle(Yii::t('ciims.controllers.Content', '{{app_name}} | {{label}}', array(
112
			'{{app_name}}' => Cii::getConfig('name', Yii::app()->name),
113
			'{{label}}'    => Yii::t('ciims.controllers.Content', 'Password Required')
114
		)));
115
116
		if ($id == NULL)
117
			$this->redirect(Yii::app()->user->returnUrl);
118
119
		// Set some default data
120
		if (Cii::get(Cii::get($_SESSION, 'password', array()), $id, NULL) == NULL)
121
			$_SESSION['password'][$id] = array('tries'=>0, 'expires' => time() + 300);
122
123
		// If the number of attempts is >= 3
124
		if (Cii::get(Cii::get(Cii::get($_SESSION, 'password', array()), $id, array()), 'tries', 0) >= 3)
125
		{
126
			// If the expires time has already passed, unlock the account
127
			if (Cii::get(Cii::get(Cii::get($_SESSION, 'password', array()), $id, array()), 'expires', 0) <= time())
128
			{
129
				$_SESSION['password'][$id] = array('tries'=>0, 'expires' => time() + 300);
130
			}
131
			else
132
			{
133
				// Otherwise prevent access to it
134
				Yii::app()->user->setFlash('error', Yii::t('ciims.controllers.Content', 'Too many password attempts. Please try again in 5 minutes'));
135
				unset($_POST['password']);
136
				$_SESSION['password'][$id]['expires'] 	= time() + 300;
137
			}
138
		}
139
140
		if (Cii::get($_POST, 'password', NULL) !== NULL)
141
		{
142
			$content = Content::model()->findByPk($id);
143
144
			$encrypted = Cii::encrypt(Cii::get($_POST, 'password'));
145
146
			if ($encrypted == $content->attributes['password'])
147
			{
148
				$_SESSION['password'][$id]['password'] = $encrypted;
149
				$_SESSION['password'][$id]['tries'] = 0;
150
				$this->redirect(Yii::app()->createUrl($content->attributes['slug']));
151
			}
152
			else
153
			{
154
				Yii::app()->user->setFlash('error', Yii::t('ciims.controllers.Content', 'Incorrect password'));
155
				$_SESSION['password'][$id]['tries'] 	= $_SESSION['password'][$id]['tries'] + 1;
156
				$_SESSION['password'][$id]['expires'] 	= time() + 300;
157
			}
158
159
		}
160
161
		$this->layout = 'password';
162
		$this->render('password', array(
163
			'id' => $id
164
		));
165
	}
166
167
	/*
168
	 * Displays a listing of all blog posts for all time in all categories
169
	 * Is used as a generic catch all behavior
170
	 */
171
	public function actionList()
172
	{
173
		$this->setPageTitle(Yii::t('ciims.controllers.Content', '{{app_name}} | {{label}}', array(
174
			'{{app_name}}' => Cii::getConfig('name', Yii::app()->name),
175
			'{{label}}'    => Yii::t('ciims.controllers.Content', 'All Content')
176
		)));
177
178
		$this->setLayout('default');
179
180
		$pageSize = Cii::getConfig('contentPaginationSize', 10);
181
182
		$criteria = Content::model()
183
					->getBaseCriteria()
184
					->addCondition('type_id >= 2')
185
					->addCondition('password = ""');
186
187
		$criteria->order = 'published DESC';
188
189
		$criteria->limit = $pageSize;
190
191
		$itemCount = Content::model()->count($criteria);
192
		$pages = new CPagination($itemCount);
193
		$pages->pageSize=$pageSize;
194
195
		$criteria->offset = $criteria->limit*($pages->getCurrentPage());
196
		$data = Content::model()->findAll($criteria);
197
		$pages->applyLimit($criteria);
198
199
		$this->render('all', array(
200
			'data'		=> $data,
201
			'itemCount' => $itemCount,
202
			'pages' 	=> $pages
203
		));
204
	}
205
206
	/**
207
	 * No routing action
208
	 */
209
	public function actionNR()
210
	{
211
        $themeName = Cii::getConfig('theme', 'default');
212
        if (file_exists(Yii::getPathOfAlias('webroot.themes.') . DS . $themeName .  DS . 'Theme.php'))
213
        {
214
            Yii::import('webroot.themes.' . $themeName . '.Theme');
215
            $theme = new Theme;
216
        }
217
218
		if ($theme->noRouting !== false)
219
			$this->render('index');
220
		else
221
			throw new CHttpException(404);
222
	}
223
}
224