| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
class myActions extends sfActions |
|---|
| 7 |
{ |
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
* Proxy action method to translate a string |
|---|
| 11 |
* |
|---|
| 12 |
* @param string $string |
|---|
| 13 |
* @param array $args |
|---|
| 14 |
* @param $catalogue $catalogue |
|---|
| 15 |
* @return string |
|---|
| 16 |
*/ |
|---|
| 17 |
protected function __($string, $args = array(), $catalogue = 'messages') |
|---|
| 18 |
{ |
|---|
| 19 |
return $this->getI18N()->__($string, $args, $catalogue); |
|---|
| 20 |
} |
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
* Build a route for pagination purpose |
|---|
| 24 |
* |
|---|
| 25 |
* @param string $route_name |
|---|
| 26 |
* @param array $params |
|---|
| 27 |
* @return string |
|---|
| 28 |
*/ |
|---|
| 29 |
protected function buildPagerRoute($route_name, $params = array()) |
|---|
| 30 |
{ |
|---|
| 31 |
if ($query_string = http_build_query($params, '', '&')) |
|---|
| 32 |
{ |
|---|
| 33 |
$route = sprintf('%s?%s&page=', $route_name, $query_string); |
|---|
| 34 |
} |
|---|
| 35 |
else |
|---|
| 36 |
{ |
|---|
| 37 |
$route = sprintf('%s?page=', $route_name); |
|---|
| 38 |
} |
|---|
| 39 |
|
|---|
| 40 |
return $route; |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
* Embed javascript Google API in current response |
|---|
| 45 |
* |
|---|
| 46 |
*/ |
|---|
| 47 |
public function embedGoogleJavascriptApi() |
|---|
| 48 |
{ |
|---|
| 49 |
$this->getResponse()->addJavascript(sprintf('http://www.google.com/jsapi?key=%s', |
|---|
| 50 |
sfConfig::get('app_googlemaps_api_key'))); |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
* Return sfI18N object instance |
|---|
| 55 |
* |
|---|
| 56 |
* @return sfI18N |
|---|
| 57 |
*/ |
|---|
| 58 |
protected function getI18N() |
|---|
| 59 |
{ |
|---|
| 60 |
return $this->getContext()->getI18N(); |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
* Returns the rendered value of a partial |
|---|
| 65 |
* |
|---|
| 66 |
* @param string $module Module name |
|---|
| 67 |
* @param string $partial Partial name (including the underscore prefix) |
|---|
| 68 |
* @param array $vars Var to pass to the partial (optional) |
|---|
| 69 |
* @return string |
|---|
| 70 |
*/ |
|---|
| 71 |
protected function getRenderedPartial($module, $partial, $vars = array()) |
|---|
| 72 |
{ |
|---|
| 73 |
$view = new sfPartialView(sfContext::getInstance(), $module, $partial, null); |
|---|
| 74 |
$view->setPartialVars($vars); |
|---|
| 75 |
return $view->render(); |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
* Get the current view cache manager, if any (it does not exist in dev env) |
|---|
| 80 |
* |
|---|
| 81 |
* @return sfViewCacheManager|null |
|---|
| 82 |
*/ |
|---|
| 83 |
protected function getViewCacheManager() |
|---|
| 84 |
{ |
|---|
| 85 |
return $this->getContext()->getViewCacheManager(); |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
* Purges the cache related to a given object |
|---|
| 90 |
* |
|---|
| 91 |
* @param object $object |
|---|
| 92 |
* @return mixed |
|---|
| 93 |
*/ |
|---|
| 94 |
protected function purgeObjectRelatedCache($object) |
|---|
| 95 |
{ |
|---|
| 96 |
$purge_method = sprintf('purge%sRelatedCache', ucfirst(get_class($object))); |
|---|
| 97 |
|
|---|
| 98 |
if (method_exists($this, $purge_method)) |
|---|
| 99 |
{ |
|---|
| 100 |
return $this->$purge_method($object); |
|---|
| 101 |
} |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
* Purges the homepage cache related assets |
|---|
| 106 |
* |
|---|
| 107 |
*/ |
|---|
| 108 |
protected function purgeHomepage() |
|---|
| 109 |
{ |
|---|
| 110 |
if ($cacheManager = $this->getViewCacheManager()) |
|---|
| 111 |
{ |
|---|
| 112 |
$cacheManager->remove('home/index'); |
|---|
| 113 |
} |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
* Purges the sections homepages cache related assets |
|---|
| 118 |
* |
|---|
| 119 |
*/ |
|---|
| 120 |
protected function purgeHomepages() |
|---|
| 121 |
{ |
|---|
| 122 |
if ($cacheManager = $this->getViewCacheManager()) |
|---|
| 123 |
{ |
|---|
| 124 |
$cacheManager->remove('applications/home'); |
|---|
| 125 |
$cacheManager->remove('blog/home'); |
|---|
| 126 |
$cacheManager->remove('companies/home'); |
|---|
| 127 |
$cacheManager->remove('events/home'); |
|---|
| 128 |
$cacheManager->remove('home/index'); |
|---|
| 129 |
$cacheManager->remove('jobs/home'); |
|---|
| 130 |
$cacheManager->remove('people/home'); |
|---|
| 131 |
} |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
* Purges an application cache related assets |
|---|
| 136 |
* |
|---|
| 137 |
* @param Application $application |
|---|
| 138 |
*/ |
|---|
| 139 |
protected function purgeApplicationRelatedCache(Application $application) |
|---|
| 140 |
{ |
|---|
| 141 |
if ($cacheManager = $this->getViewCacheManager()) |
|---|
| 142 |
{ |
|---|
| 143 |
$cacheManager->remove('applications/home'); |
|---|
| 144 |
$cacheManager->remove('applications/details?slug='.$application->getSlug()); |
|---|
| 145 |
if (!is_null($application->getsfGuardUser())) |
|---|
| 146 |
{ |
|---|
| 147 |
$cacheManager->remove('people/details?slug='.$application->getsfGuardUser()->getUsername()); |
|---|
| 148 |
} |
|---|
| 149 |
$cacheManager->remove('events/_recentEvents'); |
|---|
| 150 |
$cacheManager->remove('events/home'); |
|---|
| 151 |
} |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
* Purges a company cache related assets |
|---|
| 156 |
* |
|---|
| 157 |
* @param Company $company |
|---|
| 158 |
*/ |
|---|
| 159 |
protected function purgeCompanyRelatedCache(Company $company) |
|---|
| 160 |
{ |
|---|
| 161 |
if ($cacheManager = $this->getViewCacheManager()) |
|---|
| 162 |
{ |
|---|
| 163 |
$cacheManager->remove('companies/home'); |
|---|
| 164 |
$cacheManager->remove('company/details?slug='.$company->getSlug()); |
|---|
| 165 |
if (!is_null($company->getsfGuardUser())) |
|---|
| 166 |
{ |
|---|
| 167 |
$cacheManager->remove('people/details?slug='.$company->getsfGuardUser()->getUsername()); |
|---|
| 168 |
} |
|---|
| 169 |
$cacheManager->remove('events/_recentEvents'); |
|---|
| 170 |
$cacheManager->remove('events/home'); |
|---|
| 171 |
} |
|---|
| 172 |
} |
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
* Purges a job cache related assets |
|---|
| 176 |
* |
|---|
| 177 |
* @param Job $job |
|---|
| 178 |
*/ |
|---|
| 179 |
protected function purgeJobRelatedCache(Job $job) |
|---|
| 180 |
{ |
|---|
| 181 |
if ($cacheManager = $this->getViewCacheManager()) |
|---|
| 182 |
{ |
|---|
| 183 |
$cacheManager->remove('jobs/home'); |
|---|
| 184 |
$cacheManager->remove('jobs/details?slug='.$job->getSlug()); |
|---|
| 185 |
if (!is_null($job->getCompany())) |
|---|
| 186 |
{ |
|---|
| 187 |
$cacheManager->remove('company/details?slug='.$job->getCompany()->getSlug()); |
|---|
| 188 |
} |
|---|
| 189 |
$cacheManager->remove('events/_recentEvents'); |
|---|
| 190 |
$cacheManager->remove('events/home'); |
|---|
| 191 |
} |
|---|
| 192 |
} |
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
* Purges a user cache related assets |
|---|
| 196 |
* |
|---|
| 197 |
* @param sfGuardUser $person |
|---|
| 198 |
*/ |
|---|
| 199 |
protected function purgePersonRelatedCache(sfGuardUser $person) |
|---|
| 200 |
{ |
|---|
| 201 |
if ($cacheManager = $this->getViewCacheManager()) |
|---|
| 202 |
{ |
|---|
| 203 |
$cacheManager->remove('people/home'); |
|---|
| 204 |
$cacheManager->remove('people/details?username='.$person->getUsername()); |
|---|
| 205 |
$cacheManager->remove('events/_recentEvents'); |
|---|
| 206 |
$cacheManager->remove('events/home'); |
|---|
| 207 |
} |
|---|
| 208 |
} |
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
* Purges a recommendation cache related assets |
|---|
| 212 |
* |
|---|
| 213 |
* @param Recommendation $recommendation |
|---|
| 214 |
*/ |
|---|
| 215 |
protected function purgeRecommendationRelatedCache(Recommendation $recommendation) |
|---|
| 216 |
{ |
|---|
| 217 |
if ($cacheManager = $this->getViewCacheManager()) |
|---|
| 218 |
{ |
|---|
| 219 |
$cacheManager->remove('people/details?username='.$recommendation->getsfGuardUserRelatedByRecommendedId()->getUsername()); |
|---|
| 220 |
$cacheManager->remove('people/details?username='.$recommendation->getsfGuardUserRelatedByRecommenderId()->getUsername()); |
|---|
| 221 |
$cacheManager->remove('events/_recentEvents'); |
|---|
| 222 |
$cacheManager->remove('events/home'); |
|---|
| 223 |
} |
|---|
| 224 |
} |
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
* Purges an application company relation cache |
|---|
| 228 |
* |
|---|
| 229 |
* @param ApplicationCompany $app_company |
|---|
| 230 |
*/ |
|---|
| 231 |
protected function purgeApplicationCompanyRelatedCache(ApplicationCompany $app_company) |
|---|
| 232 |
{ |
|---|
| 233 |
if ($cacheManager = $this->getViewCacheManager()) |
|---|
| 234 |
{ |
|---|
| 235 |
$cacheManager->remove('application/details?slug='.$app_company->getApplication()->getSlug()); |
|---|
| 236 |
$cacheManager->remove('company/details?slug='.$app_company->getCompany()->getSlug()); |
|---|
| 237 |
$cacheManager->remove('events/_recentEvents'); |
|---|
| 238 |
$cacheManager->remove('events/home'); |
|---|
| 239 |
} |
|---|
| 240 |
} |
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
* Purges an application developer related cache |
|---|
| 244 |
* |
|---|
| 245 |
* @param ApplicationDeveloper $app_person |
|---|
| 246 |
*/ |
|---|
| 247 |
protected function purgeApplicationDeveloperRelatedCache(ApplicationDeveloper $app_person) |
|---|
| 248 |
{ |
|---|
| 249 |
if ($cacheManager = $this->getViewCacheManager()) |
|---|
| 250 |
{ |
|---|
| 251 |
$cacheManager->remove('application/details?slug='.$app_person->getApplication()->getSlug()); |
|---|
| 252 |
$cacheManager->remove('people/details?username='.$app_person->getsfGuardUser()->getUsername()); |
|---|
| 253 |
$cacheManager->remove('events/_recentEvents'); |
|---|
| 254 |
$cacheManager->remove('events/home'); |
|---|
| 255 |
} |
|---|
| 256 |
} |
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
* Purges a company person related cache |
|---|
| 260 |
* |
|---|
| 261 |
* @param CompanyPerson $company_person |
|---|
| 262 |
*/ |
|---|
| 263 |
protected function purgeCompanyPersonRelatedCache(CompanyPerson $company_person) |
|---|
| 264 |
{ |
|---|
| 265 |
if ($cacheManager = $this->getViewCacheManager()) |
|---|
| 266 |
{ |
|---|
| 267 |
$cacheManager->remove('company/details?slug='.$company_person->getCompany()->getSlug()); |
|---|
| 268 |
$cacheManager->remove('people/details?username='.$company_person->getsfGuardUser()->getUsername()); |
|---|
| 269 |
$cacheManager->remove('events/_recentEvents'); |
|---|
| 270 |
$cacheManager->remove('events/home'); |
|---|
| 271 |
} |
|---|
| 272 |
} |
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
* Sends an plain text email for given module/action corresponding view |
|---|
| 276 |
* using SMTP and SwiftMailer lib |
|---|
| 277 |
* |
|---|
| 278 |
* @param string $module Module |
|---|
| 279 |
* @param string $action Action |
|---|
| 280 |
* @param string $to Recipient email address |
|---|
| 281 |
* @param string $subject Subject |
|---|
| 282 |
* @param array $options Options |
|---|
| 283 |
* @return boolean |
|---|
| 284 |
*/ |
|---|
| 285 |
protected function sendSwiftSmtpPlainMail($module, $action, $to, $subject, $options = array()) |
|---|
| 286 |
{ |
|---|
| 287 |
try |
|---|
| 288 |
{ |
|---|
| 289 |
$this->checkMailFlood(); |
|---|
| 290 |
} |
|---|
| 291 |
catch (sfException $e) |
|---|
| 292 |
{ |
|---|
| 293 |
$this->getUser()->setFlash('warning', $e->getMessage()); |
|---|
| 294 |
$this->forward404(sprintf('User "%s" is flooding by mail', $this->getUser()->getGuardUser()->getUsername())); |
|---|
| 295 |
} |
|---|
| 296 |
|
|---|
| 297 |
$conn = new Swift_Connection_SMTP(sfConfig::get('app_mail_hostname')); |
|---|
| 298 |
switch (strtolower(sfConfig::get('app_mail_protocol'))) |
|---|
| 299 |
{ |
|---|
| 300 |
case 'tls': |
|---|
| 301 |
$protocol = Swift_Connection_SMTP::ENC_TLS; |
|---|
| 302 |
break; |
|---|
| 303 |
case 'ssl': |
|---|
| 304 |
$protocol = Swift_Connection_SMTP::ENC_SSL; |
|---|
| 305 |
break; |
|---|
| 306 |
default: |
|---|
| 307 |
$protocol = Swift_Connection_SMTP::ENC_OFF; |
|---|
| 308 |
break; |
|---|
| 309 |
} |
|---|
| 310 |
$conn->setEncryption($protocol); |
|---|
| 311 |
$conn->setPort(sfConfig::get('app_mail_port', 25)); |
|---|
| 312 |
$conn->setUsername(sfConfig::get('app_mail_username')); |
|---|
| 313 |
$conn->setPassword(sfConfig::get('app_mail_password')); |
|---|
| 314 |
|
|---|
| 315 |
$mailer = new Swift($conn); |
|---|
| 316 |
try |
|---|
| 317 |
{ |
|---|
| 318 |
$view = $this->getController()->getPresentationFor($module, $action, 'sfSwiftMail'); |
|---|
| 319 |
} |
|---|
| 320 |
catch (Exception $e) |
|---|
| 321 |
{ |
|---|
| 322 |
$this->logMessage('Mail sending error at view content retrieval time: '.$e->getMessage()); |
|---|
| 323 |
return false; |
|---|
| 324 |
} |
|---|
| 325 |
$subject = trim(sfConfig::get('app_mail_subject_prefix').' '.$subject); |
|---|
| 326 |
$message = new Swift_Message($subject, $view, 'text/plain'); |
|---|
| 327 |
$message->setCharset(sfConfig::get('app_mail_charset', 'utf-8')); |
|---|
| 328 |
|
|---|
| 329 |
try |
|---|
| 330 |
{ |
|---|
| 331 |
if (isset($options['reply-to'])) |
|---|
| 332 |
{ |
|---|
| 333 |
$message->setReplyTo($this->generateSwiftAddress($options['reply-to'])); |
|---|
| 334 |
} |
|---|
| 335 |
|
|---|
| 336 |
if (isset($options['from'])) |
|---|
| 337 |
{ |
|---|
| 338 |
$from = $this->generateSwiftAddress($options['from']); |
|---|
| 339 |
} |
|---|
| 340 |
elseif (isset($options['from-name'])) |
|---|
| 341 |
{ |
|---|
| 342 |
$from = $this->generateSwiftAddress(array('name' => $options['from-name'])); |
|---|
| 343 |
} |
|---|
| 344 |
else |
|---|
| 345 |
{ |
|---|
| 346 |
$from = new Swift_Address(sfConfig::get('app_mail_sender_address'), |
|---|
| 347 |
sfConfig::get('app_mail_sender_name')); |
|---|
| 348 |
} |
|---|
| 349 |
|
|---|
| 350 |
$sent = $mailer->send($message, $to, $from); |
|---|
| 351 |
$this->logMessage(sprintf('Mail sent from "%s" to "%s" with subject "%s"', |
|---|
| 352 |
$from->getAddress(), $to, $subject), 'info'); |
|---|
| 353 |
} |
|---|
| 354 |
catch (Exception $e) |
|---|
| 355 |
{ |
|---|
| 356 |
$this->logMessage('Mail sending error: '.$e->getMessage()); |
|---|
| 357 |
return false; |
|---|
| 358 |
} |
|---|
| 359 |
$mailer->disconnect(); |
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
$this->getUser()->setAttribute('lastMailAt', time()); |
|---|
| 363 |
|
|---|
| 364 |
return ($sent > 0); |
|---|
| 365 |
} |
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| 368 |
* Generates and returns Swift_Address object instance from given parameters |
|---|
| 369 |
* |
|---|
| 370 |
* @param mixed $params |
|---|
| 371 |
* @return Swift_Address |
|---|
| 372 |
* @throws sfException |
|---|
| 373 |
*/ |
|---|
| 374 |
protected function generateSwiftAddress($params) |
|---|
| 375 |
{ |
|---|
| 376 |
if (is_array($params)) |
|---|
| 377 |
{ |
|---|
| 378 |
if (array_key_exists('address', $params)) |
|---|
| 379 |
{ |
|---|
| 380 |
$address = new Swift_Address($params['address'], |
|---|
| 381 |
isset($params['name']) ? $params['name'] : null); |
|---|
| 382 |
} |
|---|
| 383 |
elseif (array_key_exists('name', $params)) |
|---|
| 384 |
{ |
|---|
| 385 |
$address = new Swift_Address(sfConfig::get('app_mail_sender_address'), |
|---|
| 386 |
isset($params['name']) ? $params['name'] : null); |
|---|
| 387 |
} |
|---|
| 388 |
else |
|---|
| 389 |
{ |
|---|
| 390 |
throw new sfException('Invalid adress parameters: '.var_export($params, true)); |
|---|
| 391 |
} |
|---|
| 392 |
} |
|---|
| 393 |
elseif (is_string($params)) |
|---|
| 394 |
{ |
|---|
| 395 |
$address = new Swift_Address($params); |
|---|
| 396 |
} |
|---|
| 397 |
else |
|---|
| 398 |
{ |
|---|
| 399 |
$address = new Swift_Address(sfConfig::get('app_mail_sender_address'), |
|---|
| 400 |
sfConfig::get('app_mail_sender_name')); |
|---|
| 401 |
} |
|---|
| 402 |
return $address; |
|---|
| 403 |
} |
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
* Checks for mail flooding (die, dirty spammers) |
|---|
| 407 |
* |
|---|
| 408 |
* @return boolean |
|---|
| 409 |
* @throws sfException if mail flooding is detected |
|---|
| 410 |
*/ |
|---|
| 411 |
protected function checkMailFlood() |
|---|
| 412 |
{ |
|---|
| 413 |
$user = $this->getUser(); |
|---|
| 414 |
|
|---|
| 415 |
if (!$user) |
|---|
| 416 |
{ |
|---|
| 417 |
throw new sfException($this->__('Mail sending use the session, please allow cookies')); |
|---|
| 418 |
} |
|---|
| 419 |
|
|---|
| 420 |
$lastMailAt = $user->getAttribute('lastMailAt', null); |
|---|
| 421 |
|
|---|
| 422 |
if (is_null($lastMailAt)) |
|---|
| 423 |
{ |
|---|
| 424 |
return; |
|---|
| 425 |
} |
|---|
| 426 |
|
|---|
| 427 |
$ttl = sfConfig::get('app_mail_mail_flood_time', 300); |
|---|
| 428 |
|
|---|
| 429 |
if (time() - $lastMailAt < $ttl) |
|---|
| 430 |
{ |
|---|
| 431 |
throw new sfException(sprintf($this->__('You must wait at least %d seconds between two mail send, please try again later'), $ttl)); |
|---|
| 432 |
} |
|---|
| 433 |
} |
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
* Post execution: if a form has been set, we put it in sfContext to be able |
|---|
| 437 |
* to reference it in the layout or partials |
|---|
| 438 |
* |
|---|
| 439 |
*/ |
|---|
| 440 |
public function postExecute() |
|---|
| 441 |
{ |
|---|
| 442 |
if (isset($this->form) && $this->form instanceof sfForm) |
|---|
| 443 |
{ |
|---|
| 444 |
$this->getContext()->set('form', $this->form); |
|---|
| 445 |
} |
|---|
| 446 |
} |
|---|
| 447 |
|
|---|
| 448 |
} |
|---|
| 449 |
|
|---|