1: <?php
  2: /**
  3:  * Copyright 2010-2014 Horde LLC (http://www.horde.org/)
  4:  *
  5:  * See the enclosed file COPYING for license information (GPL). If you
  6:  * did not receive this file, see http://www.horde.org/licenses/gpl.
  7:  *
  8:  * @category  Horde
  9:  * @copyright 2010-2014 Horde LLC
 10:  * @license   http://www.horde.org/licenses/gpl GPL
 11:  * @package   IMP
 12:  */
 13: 
 14: /**
 15:  * This class provides the data structure for a user-defined message flag.
 16:  *
 17:  * @author    Michael Slusarz <slusarz@horde.org>
 18:  * @category  Horde
 19:  * @copyright 2010-2014 Horde LLC
 20:  * @license   http://www.horde.org/licenses/gpl GPL
 21:  * @package   IMP
 22:  */
 23: class IMP_Flag_User extends IMP_Flag_Imap
 24: {
 25:     /**
 26:      */
 27:     protected $_canset = true;
 28: 
 29:     /**
 30:      */
 31:     protected $_css = 'flagUser';
 32: 
 33:     /**
 34:      * The flag label.
 35:      *
 36:      * @var string
 37:      */
 38:     protected $_label;
 39: 
 40:     /**
 41:      * Constructor.
 42:      *
 43:      * @param string $label    The label.
 44:      * @param string $flag     The IMAP flag.
 45:      * @param string $bgcolor  The background color.
 46:      */
 47:     public function __construct($label, $flag = null, $bgcolor = null)
 48:     {
 49:         $this->label = $label;
 50:         $this->imapflag = is_null($flag)
 51:             ? $label
 52:             : $flag;
 53:         if (isset($bgcolor)) {
 54:             $this->bgcolor = $bgcolor;
 55:         }
 56:     }
 57: 
 58:     /**
 59:      * @throws IMP_Exception
 60:      */
 61:     public function __set($name, $value)
 62:     {
 63:         switch ($name) {
 64:         case 'imapflag':
 65:             /* IMAP keywords must conform to RFC 3501 [9] (flag-keyword). */
 66:             $atom = new Horde_Imap_Client_Data_Format_Atom(
 67:                 /* 2: Convert whitespace to underscore. */
 68:                 strtr(
 69:                     /* 1: Do UTF-8 -> ASCII transliteration. */
 70:                     Horde_String_Transliterate::toAscii($value),
 71:                     ' ',
 72:                     '_'
 73:                 )
 74:             );
 75: 
 76:             /* 3: Remove all non-atom characters. */
 77:             $imapflag = $atom->stripNonAtomCharacters();
 78: 
 79:             /* 4: If string is empty (i.e. it contained all non-ASCII
 80:              * characters that could not be converted), save the hashed value
 81:              * of original string as flag. */
 82:             if (!strlen($imapflag)) {
 83:                 $imapflag = hash(
 84:                     (PHP_MINOR_VERSION >= 4) ? 'fnv132' : 'sha1',
 85:                     $value
 86:                 );
 87:             }
 88: 
 89:             $this->_imapflag = $imapflag;
 90:             break;
 91: 
 92:         case 'label':
 93:             $this->_label = $value;
 94:             break;
 95: 
 96:         default:
 97:             parent::__set($name, $value);
 98:             break;
 99:         }
100:     }
101: 
102:     /**
103:      */
104:     protected function _getLabel()
105:     {
106:         return $this->_label;
107:     }
108: 
109:     /* Serializable methods. */
110: 
111:     /**
112:      */
113:     public function serialize()
114:     {
115:         return json_encode(array(
116:             parent::serialize(),
117:             $this->_label,
118:             $this->_imapflag
119:         ));
120:     }
121: 
122:     /**
123:      */
124:     public function unserialize($data)
125:     {
126:         $data = json_decode($data, true);
127: 
128:         parent::unserialize($data[0]);
129:         $this->_label = $data[1];
130:         $this->_imapflag = $data[2];
131:     }
132: 
133: }
134: