From 0b3809c379b87a61261ee0a502b322a385133837 Mon Sep 17 00:00:00 2001
From: Naomi Aro <naomiaro@gmail.com>
Date: Thu, 5 Apr 2012 14:18:42 +0200
Subject: [PATCH] CC-3110 : Password reset

added sender email to preferences form, created db upgrade script.
---
 .../controllers/AuthController.php            |  4 +--
 .../controllers/PreferenceController.php      |  1 +
 .../application/forms/GeneralPreferences.php  | 14 ++++++++
 airtime_mvc/application/models/Auth.php       |  9 ++---
 airtime_mvc/application/models/Email.php      | 12 +------
 airtime_mvc/application/models/Preference.php |  8 +++++
 .../scripts/form/preferences_general.phtml    | 16 +++++++++
 .../upgrades/airtime-2.1.0/DbUpgrade.php      |  2 +-
 .../common/Version20120405114454.php          | 33 +++++++++++++++++++
 9 files changed, 79 insertions(+), 20 deletions(-)
 create mode 100644 install_minimal/upgrades/airtime-2.1.0/common/Version20120405114454.php

diff --git a/airtime_mvc/application/controllers/AuthController.php b/airtime_mvc/application/controllers/AuthController.php
index ef6733383..e2b33fe3d 100644
--- a/airtime_mvc/application/controllers/AuthController.php
+++ b/airtime_mvc/application/controllers/AuthController.php
@@ -56,7 +56,7 @@ class AuthController extends Zend_Controller_Action
         //check validity of token
         if (!$auth->checkToken($user_id, $token, 'password.restore')) {
         	echo "token not valid";
-            //$this->_helper->redirector('index', 'login');
+            $this->_helper->redirector('index', 'login');
         }
         
         if ($request->isPost() && $form->isValid($request->getPost())) {
@@ -82,7 +82,7 @@ class AuthController extends Zend_Controller_Action
             $authStorage = $zend_auth->getStorage();
             $authStorage->write($userInfo);
                 
-            $this->_helper->redirector('index', 'nowplaying');     
+            $this->_helper->redirector('index', 'showbuilder');     
         }
 
         $this->view->form = $form;
diff --git a/airtime_mvc/application/controllers/PreferenceController.php b/airtime_mvc/application/controllers/PreferenceController.php
index 99d556807..06b9486e3 100644
--- a/airtime_mvc/application/controllers/PreferenceController.php
+++ b/airtime_mvc/application/controllers/PreferenceController.php
@@ -39,6 +39,7 @@ class PreferenceController extends Zend_Controller_Action
                 Application_Model_Preference::SetAllow3rdPartyApi($values["preferences_general"]["thirdPartyApi"]);
                 Application_Model_Preference::SetTimezone($values["preferences_general"]["timezone"]);
                 Application_Model_Preference::SetWeekStartDay($values["preferences_general"]["weekStartDay"]);
+                Application_Model_Preference::SetSystemEmail($values["preferences_general"]["systemEmail"]);
 
                 Application_Model_Preference::SetAutoUploadRecordedShowToSoundcloud($values["preferences_soundcloud"]["UseSoundCloud"]);
                 Application_Model_Preference::SetUploadToSoundcloudOption($values["preferences_soundcloud"]["UploadToSoundcloudOption"]);
diff --git a/airtime_mvc/application/forms/GeneralPreferences.php b/airtime_mvc/application/forms/GeneralPreferences.php
index 9e71aed56..2b4ac8a61 100644
--- a/airtime_mvc/application/forms/GeneralPreferences.php
+++ b/airtime_mvc/application/forms/GeneralPreferences.php
@@ -64,6 +64,20 @@ class Application_Form_GeneralPreferences extends Zend_Form_SubForm
         $week_start_day->setValue(Application_Model_Preference::GetWeekStartDay());
         $week_start_day->setDecorators(array('ViewHelper'));
         $this->addElement($week_start_day);
+        
+        // Add end date element
+        $systemEmail = new Zend_Form_Element_Text('systemEmail');
+        $systemEmail->class = 'input_text';
+        $systemEmail->setRequired(false)
+            ->setValue(Application_Model_Preference::GetSystemEmail())
+            ->setLabel('Airtime System Email')
+            ->setFilters(array('StringTrim'))
+            ->setValidators(array(
+                        'NotEmpty',
+                        'EmailAddress'
+                    ))
+            ->setDecorators(array('ViewHelper'));
+        $this->addElement($systemEmail);
     }
 
     private function getTimezones(){
diff --git a/airtime_mvc/application/models/Auth.php b/airtime_mvc/application/models/Auth.php
index 11d0c0519..6e5cdb62c 100644
--- a/airtime_mvc/application/models/Auth.php
+++ b/airtime_mvc/application/models/Auth.php
@@ -6,7 +6,7 @@ class Application_Model_Auth {
 	
 	private function generateToken($action, $user_id) 
 	{
-	   $salt = "pro";
+	   $salt = md5("pro");
 	   $token = self::generateRandomString();
 	   
 	   $info = new CcSubjsToken();
@@ -25,10 +25,7 @@ class Application_Model_Auth {
                
         $e_link_protocol = empty($_SERVER['HTTPS']) ? "http" : "https";
         $e_link_base = $_SERVER['SERVER_NAME'];
-        $e_link_path = $view->url(array('user_id' => $user->getDbId(), 
-                                    'token' => $token
-                                    ), 
-                                    'password-change');
+        $e_link_path = $view->url(array('user_id' => $user->getDbId(), 'token' => $token), 'password-change');
        
         $message = "Click this link: {$e_link_protocol}://{$e_link_base}{$e_link_path}";
        
@@ -45,7 +42,7 @@ class Application_Model_Auth {
 	
     public function checkToken($user_id, $token, $action)
     {
-    	$salt = "pro";
+    	$salt = md5("pro");
     	
         $token_info = CcSubjsTokenQuery::create()
            ->filterByDbAction($action)
diff --git a/airtime_mvc/application/models/Email.php b/airtime_mvc/application/models/Email.php
index 73fd3c048..7a111a572 100644
--- a/airtime_mvc/application/models/Email.php
+++ b/airtime_mvc/application/models/Email.php
@@ -12,20 +12,10 @@ class Application_Model_Email {
      */
     public static function send($subject, $message, $tos, $from = null)
     {
-    	/*
-        $configMail = array( 'auth' => 'login',
-                             'username' => 'user@gmail.com',
-                             'password' => 'password',
-                             'ssl' => 'ssl',
-                             'port' => 465
-        );
-        $mailTransport = new Zend_Mail_Transport_Smtp('smtp.gmail.com',$configMail);
-        */
-    	
         $mail = new Zend_Mail('utf-8');
         $mail->setSubject($subject);
         $mail->setBodyText($message);
-        $mail->setFrom(isset($from) ? $from : 'naomi.aro@sourcefabric.org');
+        $mail->setFrom(isset($from) ? $from : Application_Model_Preference::GetSystemEmail());
 
         foreach ((array) $tos as $to) {
             $mail->addTo($to);
diff --git a/airtime_mvc/application/models/Preference.php b/airtime_mvc/application/models/Preference.php
index d8c0de5d8..f93cf09f9 100644
--- a/airtime_mvc/application/models/Preference.php
+++ b/airtime_mvc/application/models/Preference.php
@@ -772,6 +772,14 @@ class Application_Model_Preference
     public static function GetLiveDJSourceConnectionURL(){
         return self::GetValue("live_dj_source_connection_url");
     }
+    
+    public static function SetSystemEmail($value) {
+        self::SetValue("system_email", $value, false);
+    }
+    
+    public static function GetSystemEmail() {
+        return self::GetValue("system_email");
+    }
     /* User specific preferences end */
     
     public static function ShouldShowPopUp(){
diff --git a/airtime_mvc/application/views/scripts/form/preferences_general.phtml b/airtime_mvc/application/views/scripts/form/preferences_general.phtml
index 12ec92df2..d331ed9ae 100644
--- a/airtime_mvc/application/views/scripts/form/preferences_general.phtml
+++ b/airtime_mvc/application/views/scripts/form/preferences_general.phtml
@@ -92,5 +92,21 @@
                 </ul>
             <?php endif; ?>
         </dd>
+        
+        <!-- System Email option -->
+        <dt id="systemEmail-label" class="block-display">
+            <label class="required" for="timezone"><?php echo $this->element->getElement('systemEmail')->getLabel() ?>:
+            </label>
+        </dt>
+        <dd id="systemEmail-element" class="block-display">
+        	<?php echo $this->element->getElement('systemEmail') ?>
+            <?php if($this->element->getElement('systemEmail')->hasErrors()) : ?>
+                <ul class='errors'>
+                    <?php foreach($this->element->getElement('systemEmail')->getMessages() as $error): ?>
+                        <li><?php echo $error; ?></li>
+                    <?php endforeach; ?>
+                </ul>
+            <?php endif; ?>
+        </dd>
     </dl>
 </fieldset>
diff --git a/install_minimal/upgrades/airtime-2.1.0/DbUpgrade.php b/install_minimal/upgrades/airtime-2.1.0/DbUpgrade.php
index c26115787..ac708cf34 100644
--- a/install_minimal/upgrades/airtime-2.1.0/DbUpgrade.php
+++ b/install_minimal/upgrades/airtime-2.1.0/DbUpgrade.php
@@ -11,7 +11,7 @@ class AirtimeDatabaseUpgrade{
     }
 
     private static function task0(){
-        UpgradeCommon::MigrateTablesToVersion(__DIR__, '20120403143635');
+        UpgradeCommon::MigrateTablesToVersion(__DIR__, '20120405114454');
         $sql = "INSERT INTO cc_pref(\"keystr\", \"valstr\") VALUES('scheduled_play_switch', 'on')";
         UpgradeCommon::nonSelectQueryDb($sql);
     }
diff --git a/install_minimal/upgrades/airtime-2.1.0/common/Version20120405114454.php b/install_minimal/upgrades/airtime-2.1.0/common/Version20120405114454.php
new file mode 100644
index 000000000..3ac11f006
--- /dev/null
+++ b/install_minimal/upgrades/airtime-2.1.0/common/Version20120405114454.php
@@ -0,0 +1,33 @@
+<?php
+
+namespace DoctrineMigrations;
+
+use Doctrine\DBAL\Migrations\AbstractMigration,
+    Doctrine\DBAL\Schema\Schema;
+
+class Version20120405114454 extends AbstractMigration
+{
+    public function up(Schema $schema)
+    {
+        //create cc_subjs_token table
+        $cc_subjs_token = $schema->createTable('cc_subjs_token');
+        
+        $cc_subjs_token->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true));
+        $cc_subjs_token->addColumn('show_id', 'integer',  array('notnull' => 1));
+        $cc_subjs_token->addColumn('action', 'string', array('length' => 255, 'notnull' => 1));
+        $cc_subjs_token->addColumn('token', 'string', array('length' => 40, 'notnull' => 1));
+        $cc_subjs_token->addColumn('created', 'datetime',  array('notnull' => 1));
+        
+        $cc_subjs_token->setPrimaryKey(array('id'));
+        
+        $cc_subjs = $schema->getTable('cc_subjs');
+        $cc_subjs_token->addNamedForeignKeyConstraint('cc_subjs_token_userid_fkey', $cc_subjs, array('user_id'), array('id'));
+        $cc_subjs_token->addUniqueIndex(array('token'), 'uniq_token');
+        //end create cc_subjs_token table
+    }
+
+    public function down(Schema $schema)
+    {
+
+    }
+}
\ No newline at end of file