@@ -180,6 +180,9 @@ |
||
| 180 | 180 | return Sanitize::escape($string, $this->Entry->useDbConfig); |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | + /** |
|
| 184 | + * @param string[] $params |
|
| 185 | + */ |
|
| 183 | 186 | protected function _filterQuery($params) { |
| 184 | 187 | $this->request->query = array_intersect_key($this->request->query, |
| 185 | 188 | array_fill_keys($params, 1)); |
@@ -1,188 +1,188 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - use Saito\SimpleSearchString; |
|
| 4 | - |
|
| 5 | - App::uses('AppController', 'Controller'); |
|
| 6 | - |
|
| 7 | - class SearchesController extends AppController { |
|
| 8 | - |
|
| 9 | - public $components = [ |
|
| 10 | - 'Paginator', |
|
| 11 | - 'Search.Prg' => [ |
|
| 12 | - 'commonProcess' => [ |
|
| 13 | - 'allowedParams' => ['nstrict'], |
|
| 14 | - 'keepPassed' => true, |
|
| 15 | - 'filterEmpty' => true, |
|
| 16 | - 'paramType' => 'querystring' |
|
| 17 | - ] |
|
| 18 | - ] |
|
| 19 | - ]; |
|
| 20 | - |
|
| 21 | - public $helpers = ['Form', 'Html', 'EntryH']; |
|
| 22 | - |
|
| 23 | - public $uses = [ |
|
| 24 | - 'Entry' |
|
| 25 | - ]; |
|
| 26 | - |
|
| 27 | - protected $_paginateConfig = [ |
|
| 28 | - 'limit' => 25 |
|
| 29 | - ]; |
|
| 30 | - |
|
| 31 | - public function beforeFilter() { |
|
| 32 | - parent::beforeFilter(); |
|
| 33 | - $this->Auth->allow('simple'); |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - public function simple() { |
|
| 37 | - $defaults = [ |
|
| 38 | - 'order' => 'time' |
|
| 39 | - ]; |
|
| 40 | - $this->set('order', $defaults['order']); |
|
| 41 | - |
|
| 42 | - // @todo pgsql |
|
| 43 | - $db = $this->Entry->getDataSource(); |
|
| 44 | - // @codingStandardsIgnoreStart |
|
| 45 | - // on 5.5 phpcs assumes this is the deprecated PHP MySQL extension |
|
| 46 | - if (!($db instanceof Mysql)) { |
|
| 47 | - // @codingStandardsIgnoreEnd |
|
| 48 | - $this->redirect(['action' => 'advanced']); |
|
| 49 | - return; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - $minWordLength = $this->Entry->query("SHOW VARIABLES LIKE 'ft_min_word_len'")[0]; |
|
| 53 | - $minWordLength = array_shift($minWordLength)['Value']; |
|
| 54 | - $this->set(compact('minWordLength')); |
|
| 55 | - |
|
| 56 | - if (!isset($this->request->query['q'])) { |
|
| 57 | - // request for empty search form |
|
| 58 | - return; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - $this->_filterQuery(['q', 'page', 'order']); |
|
| 62 | - $qRaw = $this->request->query['q']; |
|
| 63 | - $query = $this->request->query += $defaults; |
|
| 64 | - $this->set(['q' => $qRaw, 'order' => $query['order']]); |
|
| 65 | - |
|
| 66 | - // test query is valid |
|
| 67 | - $SearchString = new SimpleSearchString($qRaw, $minWordLength); |
|
| 68 | - $this->set('minChars', $minWordLength); |
|
| 69 | - |
|
| 70 | - $query['q'] = $SearchString->replaceOperators(); |
|
| 71 | - $omitted = $SearchString->getOmittedWords(); |
|
| 72 | - $this->set('omittedWords', $omitted); |
|
| 73 | - |
|
| 74 | - // sanitize search-term for manual SQL-query |
|
| 75 | - $query['q'] = $this->_sanitize($query['q']); |
|
| 76 | - |
|
| 77 | - // build query |
|
| 78 | - $q = $query['q']; |
|
| 79 | - $order = '`Entry`.`time` DESC'; |
|
| 80 | - $fields = '*'; |
|
| 81 | - if ($query['order'] === 'rank') { |
|
| 82 | - $order = 'rating DESC, ' . $order; |
|
| 83 | - $fields = $fields . ", (MATCH (Entry.subject) AGAINST ('$q' IN BOOLEAN MODE)*2) + (MATCH (Entry.text) AGAINST ('$q' IN BOOLEAN MODE)) + (MATCH (Entry.name) AGAINST ('$q' IN BOOLEAN MODE)*4) AS rating"; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - // query |
|
| 87 | - $this->Paginator->settings = [ |
|
| 88 | - 'fields' => $fields, |
|
| 89 | - 'conditions' => [ |
|
| 90 | - "MATCH (Entry.subject, Entry.text, Entry.name) AGAINST ('$q' IN BOOLEAN MODE)", |
|
| 91 | - 'Entry.category_id' => $this->CurrentUser->Categories->getAllowed() |
|
| 92 | - ], |
|
| 93 | - 'order' => $order, |
|
| 94 | - 'paramType' => 'querystring' |
|
| 95 | - ]; |
|
| 96 | - $this->Paginator->settings += $this->_paginateConfig; |
|
| 97 | - $results = $this->Paginator->paginate('Entry'); |
|
| 98 | - $this->set('results', $results); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @throws NotFoundException |
|
| 103 | - * @throws BadRequestException |
|
| 104 | - */ |
|
| 105 | - public function advanced() { |
|
| 106 | - // year for date drop-down |
|
| 107 | - $first = $this->Entry->find('first', |
|
| 108 | - ['contain' => false, 'order' => 'Entry.id ASC']); |
|
| 109 | - if ($first !== false) { |
|
| 110 | - $startDate = strtotime($first['Entry']['time']); |
|
| 111 | - } else { |
|
| 112 | - $startDate = time(); |
|
| 113 | - } |
|
| 114 | - $this->set('start_year', date('Y', $startDate)); |
|
| 115 | - |
|
| 116 | - // category drop-down |
|
| 117 | - $categories = $this->CurrentUser->Categories->getAllowed('list'); |
|
| 118 | - $this->set('categories', $categories); |
|
| 119 | - |
|
| 120 | - // calculate current month and year |
|
| 121 | - if (isset($this->request->query['month'])) { |
|
| 122 | - $month = $this->request->query['month']; |
|
| 123 | - $year = $this->request->query['year']; |
|
| 124 | - } else { |
|
| 125 | - $month = date('n', $startDate); |
|
| 126 | - $year = date('Y', $startDate); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - $this->Prg->commonProcess(); |
|
| 130 | - $query = $this->Prg->parsedParams(); |
|
| 131 | - |
|
| 132 | - if (!empty($query['subject']) || !empty($query['text']) || |
|
| 133 | - !empty($query['name']) |
|
| 134 | - ) { |
|
| 135 | - // strict username search: set before parseCriteria |
|
| 136 | - if (!empty($this->request->query['nstrict'])) { |
|
| 137 | - // presetVars controller var isn't working in Search v2.3 |
|
| 138 | - $this->Entry->filterArgs['name']['type'] = 'value'; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - $settings = [ |
|
| 142 | - 'conditions' => $this->Entry->parseCriteria($query), |
|
| 143 | - 'order' => ['Entry.time' => 'DESC'], |
|
| 144 | - 'paramType' => 'querystring' |
|
| 145 | - ] + $this->_paginateConfig; |
|
| 146 | - |
|
| 147 | - $time = mktime(0, 0, 0, $month, 1, $year); |
|
| 148 | - if (!$time) { |
|
| 149 | - throw new BadRequestException; |
|
| 150 | - } |
|
| 151 | - $settings['conditions']['time >'] = date( |
|
| 152 | - 'Y-m-d H:i:s', |
|
| 153 | - mktime(0, 0, 0, $month, 1, $year)); |
|
| 154 | - |
|
| 155 | - if (isset($query['category_id']) && (int)$query['category_id'] !== 0) { |
|
| 156 | - if (!isset($categories[(int)$query['category_id']])) { |
|
| 157 | - throw new NotFoundException; |
|
| 158 | - } |
|
| 159 | - } else { |
|
| 160 | - $settings['conditions']['Entry.category_id'] = $this->CurrentUser |
|
| 161 | - ->Categories->getAllowed(); |
|
| 162 | - } |
|
| 163 | - $this->Paginator->settings = $settings; |
|
| 164 | - unset( |
|
| 165 | - $this->request->query['direction'], |
|
| 166 | - $this->request->query['sort'] |
|
| 167 | - ); |
|
| 168 | - $this->set('results', |
|
| 169 | - $this->Paginator->paginate(null, null, ['Entry.time'])); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - if (!isset($query['category_id'])) { |
|
| 173 | - $this->request->data['Entry']['category_id'] = 0; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - $this->set(compact('month', 'year')); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - protected function _sanitize($string) { |
|
| 180 | - return Sanitize::escape($string, $this->Entry->useDbConfig); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - protected function _filterQuery($params) { |
|
| 184 | - $this->request->query = array_intersect_key($this->request->query, |
|
| 185 | - array_fill_keys($params, 1)); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - } |
|
| 3 | + use Saito\SimpleSearchString; |
|
| 4 | + |
|
| 5 | + App::uses('AppController', 'Controller'); |
|
| 6 | + |
|
| 7 | + class SearchesController extends AppController { |
|
| 8 | + |
|
| 9 | + public $components = [ |
|
| 10 | + 'Paginator', |
|
| 11 | + 'Search.Prg' => [ |
|
| 12 | + 'commonProcess' => [ |
|
| 13 | + 'allowedParams' => ['nstrict'], |
|
| 14 | + 'keepPassed' => true, |
|
| 15 | + 'filterEmpty' => true, |
|
| 16 | + 'paramType' => 'querystring' |
|
| 17 | + ] |
|
| 18 | + ] |
|
| 19 | + ]; |
|
| 20 | + |
|
| 21 | + public $helpers = ['Form', 'Html', 'EntryH']; |
|
| 22 | + |
|
| 23 | + public $uses = [ |
|
| 24 | + 'Entry' |
|
| 25 | + ]; |
|
| 26 | + |
|
| 27 | + protected $_paginateConfig = [ |
|
| 28 | + 'limit' => 25 |
|
| 29 | + ]; |
|
| 30 | + |
|
| 31 | + public function beforeFilter() { |
|
| 32 | + parent::beforeFilter(); |
|
| 33 | + $this->Auth->allow('simple'); |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + public function simple() { |
|
| 37 | + $defaults = [ |
|
| 38 | + 'order' => 'time' |
|
| 39 | + ]; |
|
| 40 | + $this->set('order', $defaults['order']); |
|
| 41 | + |
|
| 42 | + // @todo pgsql |
|
| 43 | + $db = $this->Entry->getDataSource(); |
|
| 44 | + // @codingStandardsIgnoreStart |
|
| 45 | + // on 5.5 phpcs assumes this is the deprecated PHP MySQL extension |
|
| 46 | + if (!($db instanceof Mysql)) { |
|
| 47 | + // @codingStandardsIgnoreEnd |
|
| 48 | + $this->redirect(['action' => 'advanced']); |
|
| 49 | + return; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + $minWordLength = $this->Entry->query("SHOW VARIABLES LIKE 'ft_min_word_len'")[0]; |
|
| 53 | + $minWordLength = array_shift($minWordLength)['Value']; |
|
| 54 | + $this->set(compact('minWordLength')); |
|
| 55 | + |
|
| 56 | + if (!isset($this->request->query['q'])) { |
|
| 57 | + // request for empty search form |
|
| 58 | + return; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + $this->_filterQuery(['q', 'page', 'order']); |
|
| 62 | + $qRaw = $this->request->query['q']; |
|
| 63 | + $query = $this->request->query += $defaults; |
|
| 64 | + $this->set(['q' => $qRaw, 'order' => $query['order']]); |
|
| 65 | + |
|
| 66 | + // test query is valid |
|
| 67 | + $SearchString = new SimpleSearchString($qRaw, $minWordLength); |
|
| 68 | + $this->set('minChars', $minWordLength); |
|
| 69 | + |
|
| 70 | + $query['q'] = $SearchString->replaceOperators(); |
|
| 71 | + $omitted = $SearchString->getOmittedWords(); |
|
| 72 | + $this->set('omittedWords', $omitted); |
|
| 73 | + |
|
| 74 | + // sanitize search-term for manual SQL-query |
|
| 75 | + $query['q'] = $this->_sanitize($query['q']); |
|
| 76 | + |
|
| 77 | + // build query |
|
| 78 | + $q = $query['q']; |
|
| 79 | + $order = '`Entry`.`time` DESC'; |
|
| 80 | + $fields = '*'; |
|
| 81 | + if ($query['order'] === 'rank') { |
|
| 82 | + $order = 'rating DESC, ' . $order; |
|
| 83 | + $fields = $fields . ", (MATCH (Entry.subject) AGAINST ('$q' IN BOOLEAN MODE)*2) + (MATCH (Entry.text) AGAINST ('$q' IN BOOLEAN MODE)) + (MATCH (Entry.name) AGAINST ('$q' IN BOOLEAN MODE)*4) AS rating"; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + // query |
|
| 87 | + $this->Paginator->settings = [ |
|
| 88 | + 'fields' => $fields, |
|
| 89 | + 'conditions' => [ |
|
| 90 | + "MATCH (Entry.subject, Entry.text, Entry.name) AGAINST ('$q' IN BOOLEAN MODE)", |
|
| 91 | + 'Entry.category_id' => $this->CurrentUser->Categories->getAllowed() |
|
| 92 | + ], |
|
| 93 | + 'order' => $order, |
|
| 94 | + 'paramType' => 'querystring' |
|
| 95 | + ]; |
|
| 96 | + $this->Paginator->settings += $this->_paginateConfig; |
|
| 97 | + $results = $this->Paginator->paginate('Entry'); |
|
| 98 | + $this->set('results', $results); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @throws NotFoundException |
|
| 103 | + * @throws BadRequestException |
|
| 104 | + */ |
|
| 105 | + public function advanced() { |
|
| 106 | + // year for date drop-down |
|
| 107 | + $first = $this->Entry->find('first', |
|
| 108 | + ['contain' => false, 'order' => 'Entry.id ASC']); |
|
| 109 | + if ($first !== false) { |
|
| 110 | + $startDate = strtotime($first['Entry']['time']); |
|
| 111 | + } else { |
|
| 112 | + $startDate = time(); |
|
| 113 | + } |
|
| 114 | + $this->set('start_year', date('Y', $startDate)); |
|
| 115 | + |
|
| 116 | + // category drop-down |
|
| 117 | + $categories = $this->CurrentUser->Categories->getAllowed('list'); |
|
| 118 | + $this->set('categories', $categories); |
|
| 119 | + |
|
| 120 | + // calculate current month and year |
|
| 121 | + if (isset($this->request->query['month'])) { |
|
| 122 | + $month = $this->request->query['month']; |
|
| 123 | + $year = $this->request->query['year']; |
|
| 124 | + } else { |
|
| 125 | + $month = date('n', $startDate); |
|
| 126 | + $year = date('Y', $startDate); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + $this->Prg->commonProcess(); |
|
| 130 | + $query = $this->Prg->parsedParams(); |
|
| 131 | + |
|
| 132 | + if (!empty($query['subject']) || !empty($query['text']) || |
|
| 133 | + !empty($query['name']) |
|
| 134 | + ) { |
|
| 135 | + // strict username search: set before parseCriteria |
|
| 136 | + if (!empty($this->request->query['nstrict'])) { |
|
| 137 | + // presetVars controller var isn't working in Search v2.3 |
|
| 138 | + $this->Entry->filterArgs['name']['type'] = 'value'; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + $settings = [ |
|
| 142 | + 'conditions' => $this->Entry->parseCriteria($query), |
|
| 143 | + 'order' => ['Entry.time' => 'DESC'], |
|
| 144 | + 'paramType' => 'querystring' |
|
| 145 | + ] + $this->_paginateConfig; |
|
| 146 | + |
|
| 147 | + $time = mktime(0, 0, 0, $month, 1, $year); |
|
| 148 | + if (!$time) { |
|
| 149 | + throw new BadRequestException; |
|
| 150 | + } |
|
| 151 | + $settings['conditions']['time >'] = date( |
|
| 152 | + 'Y-m-d H:i:s', |
|
| 153 | + mktime(0, 0, 0, $month, 1, $year)); |
|
| 154 | + |
|
| 155 | + if (isset($query['category_id']) && (int)$query['category_id'] !== 0) { |
|
| 156 | + if (!isset($categories[(int)$query['category_id']])) { |
|
| 157 | + throw new NotFoundException; |
|
| 158 | + } |
|
| 159 | + } else { |
|
| 160 | + $settings['conditions']['Entry.category_id'] = $this->CurrentUser |
|
| 161 | + ->Categories->getAllowed(); |
|
| 162 | + } |
|
| 163 | + $this->Paginator->settings = $settings; |
|
| 164 | + unset( |
|
| 165 | + $this->request->query['direction'], |
|
| 166 | + $this->request->query['sort'] |
|
| 167 | + ); |
|
| 168 | + $this->set('results', |
|
| 169 | + $this->Paginator->paginate(null, null, ['Entry.time'])); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + if (!isset($query['category_id'])) { |
|
| 173 | + $this->request->data['Entry']['category_id'] = 0; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + $this->set(compact('month', 'year')); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + protected function _sanitize($string) { |
|
| 180 | + return Sanitize::escape($string, $this->Entry->useDbConfig); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + protected function _filterQuery($params) { |
|
| 184 | + $this->request->query = array_intersect_key($this->request->query, |
|
| 185 | + array_fill_keys($params, 1)); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + } |
|
@@ -18,6 +18,9 @@ |
||
| 18 | 18 | return $this->assertEquals($count, $length, "Failed XPath. Expected '$path' to be found $count times instead of $length."); |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | + /** |
|
| 22 | + * @param string $path |
|
| 23 | + */ |
|
| 21 | 24 | public function assertNotXPath($html, $path) { |
| 22 | 25 | return !$this->assertXPath($html, $path, 0); |
| 23 | 26 | } |
@@ -1,34 +1,34 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - namespace Saito\Test; |
|
| 3 | + namespace Saito\Test; |
|
| 4 | 4 | |
| 5 | - trait AssertTrait { |
|
| 5 | + trait AssertTrait { |
|
| 6 | 6 | |
| 7 | - /** |
|
| 8 | - * tests if XPath exists in HTML Source |
|
| 9 | - * |
|
| 10 | - * @param $html HTML |
|
| 11 | - * @param $path XPath |
|
| 12 | - * @param int $count how many times should XPath exist in HTML |
|
| 13 | - * @return mixed |
|
| 14 | - */ |
|
| 15 | - public function assertXPath($html, $path, $count = 1) { |
|
| 16 | - $xpath = $this->_getDOMXPath($html); |
|
| 17 | - $length = $xpath->query($path)->length; |
|
| 18 | - return $this->assertEquals($count, $length, "Failed XPath. Expected '$path' to be found $count times instead of $length."); |
|
| 19 | - } |
|
| 7 | + /** |
|
| 8 | + * tests if XPath exists in HTML Source |
|
| 9 | + * |
|
| 10 | + * @param $html HTML |
|
| 11 | + * @param $path XPath |
|
| 12 | + * @param int $count how many times should XPath exist in HTML |
|
| 13 | + * @return mixed |
|
| 14 | + */ |
|
| 15 | + public function assertXPath($html, $path, $count = 1) { |
|
| 16 | + $xpath = $this->_getDOMXPath($html); |
|
| 17 | + $length = $xpath->query($path)->length; |
|
| 18 | + return $this->assertEquals($count, $length, "Failed XPath. Expected '$path' to be found $count times instead of $length."); |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | - public function assertNotXPath($html, $path) { |
|
| 22 | - return !$this->assertXPath($html, $path, 0); |
|
| 23 | - } |
|
| 21 | + public function assertNotXPath($html, $path) { |
|
| 22 | + return !$this->assertXPath($html, $path, 0); |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - protected function _getDOMXPath($html) { |
|
| 26 | - $document = new \DOMDocument; |
|
| 27 | - libxml_use_internal_errors(true); |
|
| 28 | - $document->loadHTML('<!DOCTYPE html>' . $html); |
|
| 29 | - $xpath = new \DOMXPath($document); |
|
| 30 | - libxml_clear_errors(); |
|
| 31 | - return $xpath; |
|
| 32 | - } |
|
| 25 | + protected function _getDOMXPath($html) { |
|
| 26 | + $document = new \DOMDocument; |
|
| 27 | + libxml_use_internal_errors(true); |
|
| 28 | + $document->loadHTML('<!DOCTYPE html>' . $html); |
|
| 29 | + $xpath = new \DOMXPath($document); |
|
| 30 | + libxml_clear_errors(); |
|
| 31 | + return $xpath; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - } |
|
| 35 | 34 | \ No newline at end of file |
| 35 | + } |
|
| 36 | 36 | \ No newline at end of file |
@@ -48,6 +48,9 @@ discard block |
||
| 48 | 48 | unset($_ENV['HTTP_X_REQUESTED_WITH']); |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | + /** |
|
| 52 | + * @param string $agent |
|
| 53 | + */ |
|
| 51 | 54 | protected function _setUserAgent($agent) { |
| 52 | 55 | if (isset($this->_env['HTTP_USER_AGENT'])) { |
| 53 | 56 | $this->_env['HTTP_USER_AGENT'] = $_ENV['HTTP_USER_AGENT']; |
@@ -63,6 +66,9 @@ discard block |
||
| 63 | 66 | } |
| 64 | 67 | } |
| 65 | 68 | |
| 69 | + /** |
|
| 70 | + * @param integer $id |
|
| 71 | + */ |
|
| 66 | 72 | protected function _loginUser($id) { |
| 67 | 73 | // see: http://stackoverflow.com/a/10411128/1372085 |
| 68 | 74 | $this->_logoutUser(); |
@@ -100,6 +106,9 @@ discard block |
||
| 100 | 106 | endif; |
| 101 | 107 | } |
| 102 | 108 | |
| 109 | + /** |
|
| 110 | + * @param string $name |
|
| 111 | + */ |
|
| 103 | 112 | protected function _notImplementedOnDatasource($name) { |
| 104 | 113 | $mc = $this->controller->modelClass; |
| 105 | 114 | $Ds = $this->controller->{$mc}->getDataSource(); |
@@ -1,8 +1,8 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - namespace Saito\Test; |
|
| 3 | + namespace Saito\Test; |
|
| 4 | 4 | |
| 5 | - /* |
|
| 5 | + /* |
|
| 6 | 6 | fixes early output in PHPUnit 3.7: |
| 7 | 7 | |
| 8 | 8 | $this->printer->write( |
@@ -11,128 +11,128 @@ discard block |
||
| 11 | 11 | |
| 12 | 12 | that prevents session setting in HTML test-run |
| 13 | 13 | */ |
| 14 | - ob_start(); |
|
| 15 | - |
|
| 16 | - // load fixture |
|
| 17 | - \App::uses('UserFixture', 'Fixture'); |
|
| 18 | - |
|
| 19 | - // sets the FULL_BASE_URL for CLI tests |
|
| 20 | - if (!defined('FULL_BASE_URL')) { |
|
| 21 | - define('FULL_BASE_URL', 'http://cakephp.org/'); |
|
| 22 | - } |
|
| 23 | - \Configure::write('App.fullBaseURL', FULL_BASE_URL); |
|
| 24 | - |
|
| 25 | - |
|
| 26 | - class ControllerTestCase extends \ControllerTestCase { |
|
| 27 | - |
|
| 28 | - use AssertTrait; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var array cache environment variables |
|
| 32 | - */ |
|
| 33 | - protected $_env = []; |
|
| 34 | - |
|
| 35 | - protected function _setJson() { |
|
| 36 | - $_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript'; |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - protected function _unsetJson() { |
|
| 40 | - $_SERVER['HTTP_ACCEPT'] = "text/html,application/xhtml+xml,application/xml"; |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - protected function _setAjax() { |
|
| 44 | - $_ENV['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - protected function _unsetAjax() { |
|
| 48 | - unset($_ENV['HTTP_X_REQUESTED_WITH']); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - protected function _setUserAgent($agent) { |
|
| 52 | - if (isset($this->_env['HTTP_USER_AGENT'])) { |
|
| 53 | - $this->_env['HTTP_USER_AGENT'] = $_ENV['HTTP_USER_AGENT']; |
|
| 54 | - } |
|
| 55 | - $_ENV['HTTP_USER_AGENT'] = $agent; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - protected function _unsetUserAgent() { |
|
| 59 | - if (isset($this->_env['HTTP_USER_AGENT'])) { |
|
| 60 | - $_ENV['HTTP_USER_AGENT'] = $this->_env('HTTP_USER_AGENT'); |
|
| 61 | - } else { |
|
| 62 | - unset($_ENV['HTTP_USER_AGENT']); |
|
| 63 | - } |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - protected function _loginUser($id) { |
|
| 67 | - // see: http://stackoverflow.com/a/10411128/1372085 |
|
| 68 | - $this->_logoutUser(); |
|
| 69 | - $userFixture = new \UserFixture(); |
|
| 70 | - $users = $userFixture->records; |
|
| 71 | - |
|
| 72 | - $this->controller->Session->write('Auth.User', $users[$id - 1]); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - protected function _debugEmail() { |
|
| 76 | - \Configure::write('Saito.Debug.email', true); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - protected function _resetEmail() { |
|
| 80 | - \Configure::write('Saito.Debug.email', false); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - public function assertRedirectedTo($url = '') { |
|
| 84 | - $this->assertEquals( |
|
| 85 | - $this->headers['Location'], |
|
| 86 | - \Router::fullBaseUrl() . $this->controller->request->webroot . $url |
|
| 87 | - ); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - protected function _logoutUser() { |
|
| 91 | - // if user is logged-in it should interfere with test runs |
|
| 92 | - if (isset($_COOKIE['SaitoPersistent'])) : |
|
| 93 | - unset($_COOKIE['SaitoPersistent']); |
|
| 94 | - endif; |
|
| 95 | - if (isset($_COOKIE['Saito'])) : |
|
| 96 | - unset($_COOKIE['Saito']); |
|
| 97 | - endif; |
|
| 98 | - if (isset($this->controller->Session) && !empty($this->controller->Session)) : |
|
| 99 | - $this->controller->Session->delete('Auth.User'); |
|
| 100 | - endif; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - protected function _notImplementedOnDatasource($name) { |
|
| 104 | - $mc = $this->controller->modelClass; |
|
| 105 | - $Ds = $this->controller->{$mc}->getDataSource(); |
|
| 106 | - $this->_DsName = get_class($Ds); |
|
| 107 | - if ($this->_DsName === $name) { |
|
| 108 | - $this->markTestIncomplete("Datasource is $name."); |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - public function endTest($method) { |
|
| 113 | - parent::endTest($method); |
|
| 114 | - $this->_logoutUser(); |
|
| 115 | - $this->_unsetAjax(); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - public function setUp() { |
|
| 119 | - parent::setUp(); |
|
| 120 | - $this->_logoutUser(); |
|
| 121 | - $this->_unsetAjax(); |
|
| 122 | - $this->_unsetJson(); |
|
| 123 | - $this->_debugEmail(); |
|
| 124 | - \Configure::write('Cache.disable', true); |
|
| 125 | - \Configure::write('Config.language', 'eng'); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - public function tearDown() { |
|
| 129 | - \Configure::write('Cache.disable', false); |
|
| 130 | - $this->_unsetUserAgent(); |
|
| 131 | - $this->_resetEmail(); |
|
| 132 | - $this->_logoutUser(); |
|
| 133 | - parent::tearDown(); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - } |
|
| 14 | + ob_start(); |
|
| 15 | + |
|
| 16 | + // load fixture |
|
| 17 | + \App::uses('UserFixture', 'Fixture'); |
|
| 18 | + |
|
| 19 | + // sets the FULL_BASE_URL for CLI tests |
|
| 20 | + if (!defined('FULL_BASE_URL')) { |
|
| 21 | + define('FULL_BASE_URL', 'http://cakephp.org/'); |
|
| 22 | + } |
|
| 23 | + \Configure::write('App.fullBaseURL', FULL_BASE_URL); |
|
| 24 | + |
|
| 25 | + |
|
| 26 | + class ControllerTestCase extends \ControllerTestCase { |
|
| 27 | + |
|
| 28 | + use AssertTrait; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var array cache environment variables |
|
| 32 | + */ |
|
| 33 | + protected $_env = []; |
|
| 34 | + |
|
| 35 | + protected function _setJson() { |
|
| 36 | + $_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript'; |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + protected function _unsetJson() { |
|
| 40 | + $_SERVER['HTTP_ACCEPT'] = "text/html,application/xhtml+xml,application/xml"; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + protected function _setAjax() { |
|
| 44 | + $_ENV['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + protected function _unsetAjax() { |
|
| 48 | + unset($_ENV['HTTP_X_REQUESTED_WITH']); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + protected function _setUserAgent($agent) { |
|
| 52 | + if (isset($this->_env['HTTP_USER_AGENT'])) { |
|
| 53 | + $this->_env['HTTP_USER_AGENT'] = $_ENV['HTTP_USER_AGENT']; |
|
| 54 | + } |
|
| 55 | + $_ENV['HTTP_USER_AGENT'] = $agent; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + protected function _unsetUserAgent() { |
|
| 59 | + if (isset($this->_env['HTTP_USER_AGENT'])) { |
|
| 60 | + $_ENV['HTTP_USER_AGENT'] = $this->_env('HTTP_USER_AGENT'); |
|
| 61 | + } else { |
|
| 62 | + unset($_ENV['HTTP_USER_AGENT']); |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + protected function _loginUser($id) { |
|
| 67 | + // see: http://stackoverflow.com/a/10411128/1372085 |
|
| 68 | + $this->_logoutUser(); |
|
| 69 | + $userFixture = new \UserFixture(); |
|
| 70 | + $users = $userFixture->records; |
|
| 71 | + |
|
| 72 | + $this->controller->Session->write('Auth.User', $users[$id - 1]); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + protected function _debugEmail() { |
|
| 76 | + \Configure::write('Saito.Debug.email', true); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + protected function _resetEmail() { |
|
| 80 | + \Configure::write('Saito.Debug.email', false); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + public function assertRedirectedTo($url = '') { |
|
| 84 | + $this->assertEquals( |
|
| 85 | + $this->headers['Location'], |
|
| 86 | + \Router::fullBaseUrl() . $this->controller->request->webroot . $url |
|
| 87 | + ); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + protected function _logoutUser() { |
|
| 91 | + // if user is logged-in it should interfere with test runs |
|
| 92 | + if (isset($_COOKIE['SaitoPersistent'])) : |
|
| 93 | + unset($_COOKIE['SaitoPersistent']); |
|
| 94 | + endif; |
|
| 95 | + if (isset($_COOKIE['Saito'])) : |
|
| 96 | + unset($_COOKIE['Saito']); |
|
| 97 | + endif; |
|
| 98 | + if (isset($this->controller->Session) && !empty($this->controller->Session)) : |
|
| 99 | + $this->controller->Session->delete('Auth.User'); |
|
| 100 | + endif; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + protected function _notImplementedOnDatasource($name) { |
|
| 104 | + $mc = $this->controller->modelClass; |
|
| 105 | + $Ds = $this->controller->{$mc}->getDataSource(); |
|
| 106 | + $this->_DsName = get_class($Ds); |
|
| 107 | + if ($this->_DsName === $name) { |
|
| 108 | + $this->markTestIncomplete("Datasource is $name."); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + public function endTest($method) { |
|
| 113 | + parent::endTest($method); |
|
| 114 | + $this->_logoutUser(); |
|
| 115 | + $this->_unsetAjax(); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + public function setUp() { |
|
| 119 | + parent::setUp(); |
|
| 120 | + $this->_logoutUser(); |
|
| 121 | + $this->_unsetAjax(); |
|
| 122 | + $this->_unsetJson(); |
|
| 123 | + $this->_debugEmail(); |
|
| 124 | + \Configure::write('Cache.disable', true); |
|
| 125 | + \Configure::write('Config.language', 'eng'); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + public function tearDown() { |
|
| 129 | + \Configure::write('Cache.disable', false); |
|
| 130 | + $this->_unsetUserAgent(); |
|
| 131 | + $this->_resetEmail(); |
|
| 132 | + $this->_logoutUser(); |
|
| 133 | + parent::tearDown(); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | 138 | |
@@ -4,6 +4,9 @@ |
||
| 4 | 4 | |
| 5 | 5 | trait SecurityMockTrait { |
| 6 | 6 | |
| 7 | + /** |
|
| 8 | + * @param string $controller |
|
| 9 | + */ |
|
| 7 | 10 | public function generate($controller, $mocks = []) { |
| 8 | 11 | $byPassSecurity = false; |
| 9 | 12 | if (!isset($mocks['components']['Security'])) { |
@@ -1,34 +1,34 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - namespace Saito\Test; |
|
| 3 | + namespace Saito\Test; |
|
| 4 | 4 | |
| 5 | - trait SecurityMockTrait { |
|
| 5 | + trait SecurityMockTrait { |
|
| 6 | 6 | |
| 7 | - public function generate($controller, $mocks = []) { |
|
| 8 | - $byPassSecurity = false; |
|
| 9 | - if (!isset($mocks['components']['Security'])) { |
|
| 10 | - $byPassSecurity = true; |
|
| 11 | - $mocks['components']['Security'] = ['_validateCsrf', '_validatePost']; |
|
| 12 | - } |
|
| 13 | - $Mock = parent::generate($controller, $mocks); |
|
| 14 | - if ($byPassSecurity) { |
|
| 15 | - $this->assertSecurityByPass($Mock); |
|
| 16 | - } |
|
| 17 | - return $Mock; |
|
| 18 | - } |
|
| 7 | + public function generate($controller, $mocks = []) { |
|
| 8 | + $byPassSecurity = false; |
|
| 9 | + if (!isset($mocks['components']['Security'])) { |
|
| 10 | + $byPassSecurity = true; |
|
| 11 | + $mocks['components']['Security'] = ['_validateCsrf', '_validatePost']; |
|
| 12 | + } |
|
| 13 | + $Mock = parent::generate($controller, $mocks); |
|
| 14 | + if ($byPassSecurity) { |
|
| 15 | + $this->assertSecurityByPass($Mock); |
|
| 16 | + } |
|
| 17 | + return $Mock; |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Assume that SecurityComponent was called |
|
| 22 | - * |
|
| 23 | - * @param $Controller |
|
| 24 | - */ |
|
| 25 | - public function assertSecurityBypass($Controller) { |
|
| 26 | - $Controller->Security->expects($this->any()) |
|
| 27 | - ->method('_validatePost') |
|
| 28 | - ->will($this->returnValue(true)); |
|
| 29 | - $Controller->Security->expects($this->any()) |
|
| 30 | - ->method('_validateCsrf') |
|
| 31 | - ->will($this->returnValue(true)); |
|
| 32 | - } |
|
| 20 | + /** |
|
| 21 | + * Assume that SecurityComponent was called |
|
| 22 | + * |
|
| 23 | + * @param $Controller |
|
| 24 | + */ |
|
| 25 | + public function assertSecurityBypass($Controller) { |
|
| 26 | + $Controller->Security->expects($this->any()) |
|
| 27 | + ->method('_validatePost') |
|
| 28 | + ->will($this->returnValue(true)); |
|
| 29 | + $Controller->Security->expects($this->any()) |
|
| 30 | + ->method('_validateCsrf') |
|
| 31 | + ->will($this->returnValue(true)); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - } |
|
| 34 | + } |
|
@@ -29,6 +29,10 @@ discard block |
||
| 29 | 29 | */ |
| 30 | 30 | public $duration = 8035200; |
| 31 | 31 | |
| 32 | + /** |
|
| 33 | + * @param integer $userId |
|
| 34 | + * @param integer $blockedUserId |
|
| 35 | + */ |
|
| 32 | 36 | public function ignore($userId, $blockedUserId) { |
| 33 | 37 | $exists = $this->_get($userId, $blockedUserId); |
| 34 | 38 | if ($exists) { |
@@ -62,6 +66,9 @@ discard block |
||
| 62 | 66 | ]); |
| 63 | 67 | } |
| 64 | 68 | |
| 69 | + /** |
|
| 70 | + * @param integer $id |
|
| 71 | + */ |
|
| 65 | 72 | public function ignoredBy($id) { |
| 66 | 73 | return $this->find( |
| 67 | 74 | 'all', |
@@ -72,6 +79,9 @@ discard block |
||
| 72 | 79 | ); |
| 73 | 80 | } |
| 74 | 81 | |
| 82 | + /** |
|
| 83 | + * @param integer $userId |
|
| 84 | + */ |
|
| 75 | 85 | public function deleteUser($userId) { |
| 76 | 86 | $success = $this->deleteAll(['user_id' => $userId], false); |
| 77 | 87 | $success = $success && $this->deleteAll(['blocked_user_id' => $userId], false); |
@@ -81,8 +91,8 @@ discard block |
||
| 81 | 91 | /** |
| 82 | 92 | * counts how many users ignore the user with ID $id |
| 83 | 93 | * |
| 84 | - * @param $id |
|
| 85 | - * @return array |
|
| 94 | + * @param integer $id |
|
| 95 | + * @return integer |
|
| 86 | 96 | */ |
| 87 | 97 | public function countIgnored($id) { |
| 88 | 98 | return count($this->getIgnored($id)); |
@@ -1,108 +1,108 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - App::uses('AppModel', 'Model'); |
|
| 3 | + App::uses('AppModel', 'Model'); |
|
| 4 | 4 | |
| 5 | - class UserIgnore extends AppModel { |
|
| 5 | + class UserIgnore extends AppModel { |
|
| 6 | 6 | |
| 7 | - public $actsAs = [ |
|
| 8 | - 'Containable', |
|
| 9 | - 'Cron.Cron' => [ |
|
| 10 | - 'removeOld' => [ |
|
| 11 | - 'id' => 'UserIgnore.removeOld', |
|
| 12 | - 'due' => 'daily', |
|
| 13 | - ] |
|
| 14 | - ] |
|
| 15 | - ]; |
|
| 7 | + public $actsAs = [ |
|
| 8 | + 'Containable', |
|
| 9 | + 'Cron.Cron' => [ |
|
| 10 | + 'removeOld' => [ |
|
| 11 | + 'id' => 'UserIgnore.removeOld', |
|
| 12 | + 'due' => 'daily', |
|
| 13 | + ] |
|
| 14 | + ] |
|
| 15 | + ]; |
|
| 16 | 16 | |
| 17 | - public $belongsTo = [ |
|
| 18 | - 'User' => [ |
|
| 19 | - 'className' => 'User', |
|
| 20 | - 'counterCache' => true, |
|
| 21 | - 'fields' => ['id', 'username'], |
|
| 22 | - 'foreignKey' => 'blocked_user_id', |
|
| 23 | - 'order' => ['User.username' => 'asc'] |
|
| 24 | - ] |
|
| 25 | - ]; |
|
| 17 | + public $belongsTo = [ |
|
| 18 | + 'User' => [ |
|
| 19 | + 'className' => 'User', |
|
| 20 | + 'counterCache' => true, |
|
| 21 | + 'fields' => ['id', 'username'], |
|
| 22 | + 'foreignKey' => 'blocked_user_id', |
|
| 23 | + 'order' => ['User.username' => 'asc'] |
|
| 24 | + ] |
|
| 25 | + ]; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @var int 3 months |
|
| 29 | - */ |
|
| 30 | - public $duration = 8035200; |
|
| 27 | + /** |
|
| 28 | + * @var int 3 months |
|
| 29 | + */ |
|
| 30 | + public $duration = 8035200; |
|
| 31 | 31 | |
| 32 | - public function ignore($userId, $blockedUserId) { |
|
| 33 | - $exists = $this->_get($userId, $blockedUserId); |
|
| 34 | - if ($exists) { |
|
| 35 | - return; |
|
| 36 | - } |
|
| 37 | - $this->create(); |
|
| 38 | - $this->save([ |
|
| 39 | - 'user_id' => $userId, |
|
| 40 | - 'blocked_user_id' => $blockedUserId, |
|
| 41 | - 'timestamp' => bDate() |
|
| 42 | - ]); |
|
| 32 | + public function ignore($userId, $blockedUserId) { |
|
| 33 | + $exists = $this->_get($userId, $blockedUserId); |
|
| 34 | + if ($exists) { |
|
| 35 | + return; |
|
| 36 | + } |
|
| 37 | + $this->create(); |
|
| 38 | + $this->save([ |
|
| 39 | + 'user_id' => $userId, |
|
| 40 | + 'blocked_user_id' => $blockedUserId, |
|
| 41 | + 'timestamp' => bDate() |
|
| 42 | + ]); |
|
| 43 | 43 | |
| 44 | - $this->_dispatchEvent('Event.Saito.User.afterIgnore', [ |
|
| 45 | - 'blockedUserId' => $blockedUserId, |
|
| 46 | - 'userId' => $userId |
|
| 47 | - ]); |
|
| 48 | - } |
|
| 44 | + $this->_dispatchEvent('Event.Saito.User.afterIgnore', [ |
|
| 45 | + 'blockedUserId' => $blockedUserId, |
|
| 46 | + 'userId' => $userId |
|
| 47 | + ]); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - public function unignore($userId, $blockedId) { |
|
| 51 | - $entry = $this->_get($userId, $blockedId); |
|
| 52 | - if (empty($entry)) { |
|
| 53 | - return; |
|
| 54 | - } |
|
| 55 | - $this->delete($entry['Ignore']['id']); |
|
| 56 | - } |
|
| 50 | + public function unignore($userId, $blockedId) { |
|
| 51 | + $entry = $this->_get($userId, $blockedId); |
|
| 52 | + if (empty($entry)) { |
|
| 53 | + return; |
|
| 54 | + } |
|
| 55 | + $this->delete($entry['Ignore']['id']); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - protected function _get($userId, $blockedId) { |
|
| 59 | - return $this->find('first', [ |
|
| 60 | - 'contain' => false, |
|
| 61 | - 'conditions' => ['user_id' => $userId, 'blocked_user_id' => $blockedId] |
|
| 62 | - ]); |
|
| 63 | - } |
|
| 58 | + protected function _get($userId, $blockedId) { |
|
| 59 | + return $this->find('first', [ |
|
| 60 | + 'contain' => false, |
|
| 61 | + 'conditions' => ['user_id' => $userId, 'blocked_user_id' => $blockedId] |
|
| 62 | + ]); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - public function ignoredBy($id) { |
|
| 66 | - return $this->find( |
|
| 67 | - 'all', |
|
| 68 | - [ |
|
| 69 | - 'contain' => ['User'], |
|
| 70 | - 'conditions' => ['user_id' => $id] |
|
| 71 | - ] |
|
| 72 | - ); |
|
| 73 | - } |
|
| 65 | + public function ignoredBy($id) { |
|
| 66 | + return $this->find( |
|
| 67 | + 'all', |
|
| 68 | + [ |
|
| 69 | + 'contain' => ['User'], |
|
| 70 | + 'conditions' => ['user_id' => $id] |
|
| 71 | + ] |
|
| 72 | + ); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - public function deleteUser($userId) { |
|
| 76 | - $success = $this->deleteAll(['user_id' => $userId], false); |
|
| 77 | - $success = $success && $this->deleteAll(['blocked_user_id' => $userId], false); |
|
| 78 | - return $success; |
|
| 79 | - } |
|
| 75 | + public function deleteUser($userId) { |
|
| 76 | + $success = $this->deleteAll(['user_id' => $userId], false); |
|
| 77 | + $success = $success && $this->deleteAll(['blocked_user_id' => $userId], false); |
|
| 78 | + return $success; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * counts how many users ignore the user with ID $id |
|
| 83 | - * |
|
| 84 | - * @param $id |
|
| 85 | - * @return array |
|
| 86 | - */ |
|
| 87 | - public function countIgnored($id) { |
|
| 88 | - return count($this->getIgnored($id)); |
|
| 89 | - } |
|
| 81 | + /** |
|
| 82 | + * counts how many users ignore the user with ID $id |
|
| 83 | + * |
|
| 84 | + * @param $id |
|
| 85 | + * @return array |
|
| 86 | + */ |
|
| 87 | + public function countIgnored($id) { |
|
| 88 | + return count($this->getIgnored($id)); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - public function getIgnored($id) { |
|
| 92 | - return $this->find( |
|
| 93 | - 'all', |
|
| 94 | - [ |
|
| 95 | - 'contain' => false, |
|
| 96 | - 'conditions' => ['blocked_user_id' => $id] |
|
| 97 | - ] |
|
| 98 | - ); |
|
| 99 | - } |
|
| 91 | + public function getIgnored($id) { |
|
| 92 | + return $this->find( |
|
| 93 | + 'all', |
|
| 94 | + [ |
|
| 95 | + 'contain' => false, |
|
| 96 | + 'conditions' => ['blocked_user_id' => $id] |
|
| 97 | + ] |
|
| 98 | + ); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - public function removeOld() { |
|
| 102 | - $this->deleteAll([ |
|
| 103 | - 'timestamp <' => bDate(time() - $this->duration) |
|
| 104 | - ], |
|
| 105 | - false); |
|
| 106 | - } |
|
| 101 | + public function removeOld() { |
|
| 102 | + $this->deleteAll([ |
|
| 103 | + 'timestamp <' => bDate(time() - $this->duration) |
|
| 104 | + ], |
|
| 105 | + false); |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - } |
|
| 108 | + } |
|
@@ -169,7 +169,7 @@ discard block |
||
| 169 | 169 | /** |
| 170 | 170 | * |
| 171 | 171 | * @param string $version |
| 172 | - * @param array $action |
|
| 172 | + * @param string $action |
|
| 173 | 173 | * @param array $params |
| 174 | 174 | * @return object |
| 175 | 175 | */ |
@@ -291,7 +291,7 @@ discard block |
||
| 291 | 291 | /** |
| 292 | 292 | * |
| 293 | 293 | * @param resource $ch |
| 294 | - * @param array $headers |
|
| 294 | + * @param string[] $headers |
|
| 295 | 295 | * @return void |
| 296 | 296 | */ |
| 297 | 297 | protected function setCurlOptions(&$ch, $headers = array()) |
@@ -333,7 +333,7 @@ discard block |
||
| 333 | 333 | |
| 334 | 334 | /** |
| 335 | 335 | * |
| 336 | - * @param stdClass $o |
|
| 336 | + * @param \stdClass $o |
|
| 337 | 337 | * @return string |
| 338 | 338 | */ |
| 339 | 339 | public static function reg_imploder(\stdClass $o) |
@@ -274,10 +274,10 @@ |
||
| 274 | 274 | * @return string |
| 275 | 275 | */ |
| 276 | 276 | public function services_regex() { |
| 277 | - $services = $this->services(); |
|
| 278 | - $regexes = array_map(array(__CLASS__, 'reg_imploder'), $services); |
|
| 279 | - return '#'.implode('|', $regexes).'#i'; |
|
| 280 | - } |
|
| 277 | + $services = $this->services(); |
|
| 278 | + $regexes = array_map(array(__CLASS__, 'reg_imploder'), $services); |
|
| 279 | + return '#'.implode('|', $regexes).'#i'; |
|
| 280 | + } |
|
| 281 | 281 | |
| 282 | 282 | /** |
| 283 | 283 | * |
@@ -202,7 +202,7 @@ discard block |
||
| 202 | 202 | if (!$match) { |
| 203 | 203 | //print("rejecting $url"); |
| 204 | 204 | unset($params['urls'][$i]); |
| 205 | - $rejects[$i] = (object)array( |
|
| 205 | + $rejects[$i] = (object) array( |
|
| 206 | 206 | 'error_code' => '401', |
| 207 | 207 | 'error_message' => 'This service requires an Embedly key', |
| 208 | 208 | 'type' => 'error' |
@@ -243,7 +243,7 @@ discard block |
||
| 243 | 243 | array_push($merged_result, $obj); |
| 244 | 244 | } |
| 245 | 245 | |
| 246 | - if($justone) { |
|
| 246 | + if ($justone) { |
|
| 247 | 247 | return array_shift($merged_result); |
| 248 | 248 | } |
| 249 | 249 | |
@@ -365,7 +365,7 @@ discard block |
||
| 365 | 365 | */ |
| 366 | 366 | public static function paramify($input) |
| 367 | 367 | { |
| 368 | - if(is_string($input)) { |
|
| 368 | + if (is_string($input)) { |
|
| 369 | 369 | return array('urls' => $input); |
| 370 | 370 | } |
| 371 | 371 | |
@@ -94,6 +94,7 @@ discard block |
||
| 94 | 94 | * Preform requested target patch checks depending on the unique setting |
| 95 | 95 | * |
| 96 | 96 | * @var string chosen filename target_path |
| 97 | + * @param string $target_path |
|
| 97 | 98 | * @return string of resulting target_path |
| 98 | 99 | * @access private |
| 99 | 100 | */ |
@@ -217,7 +218,7 @@ discard block |
||
| 217 | 218 | /** |
| 218 | 219 | * Checks if there is a file uploaded |
| 219 | 220 | * |
| 220 | - * @return void |
|
| 221 | + * @return boolean |
|
| 221 | 222 | * @access public |
| 222 | 223 | * @param file array of uploaded file (optional) |
| 223 | 224 | */ |
@@ -293,7 +294,7 @@ discard block |
||
| 293 | 294 | } |
| 294 | 295 | |
| 295 | 296 | /** |
| 296 | - * @return boolean true if errors were detected. |
|
| 297 | + * @return integer true if errors were detected. |
|
| 297 | 298 | */ |
| 298 | 299 | function hasErrors(){ |
| 299 | 300 | return count($this->errors); |
@@ -1,40 +1,40 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Uploader class handles a single file to be uploaded to the file system |
|
| 4 | - * |
|
| 5 | - * @author: Nick Baker |
|
| 6 | - * @version: since 6.0.0 |
|
| 7 | - * @link: http://www.webtechnick.com |
|
| 8 | - */ |
|
| 3 | + * Uploader class handles a single file to be uploaded to the file system |
|
| 4 | + * |
|
| 5 | + * @author: Nick Baker |
|
| 6 | + * @version: since 6.0.0 |
|
| 7 | + * @link: http://www.webtechnick.com |
|
| 8 | + */ |
|
| 9 | 9 | class Uploader { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * File to upload. |
|
| 13 | - */ |
|
| 14 | - var $file = array(); |
|
| 11 | + /** |
|
| 12 | + * File to upload. |
|
| 13 | + */ |
|
| 14 | + var $file = array(); |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Global options |
|
| 18 | - * fileTypes to allow to upload |
|
| 19 | - */ |
|
| 20 | - var $options = array(); |
|
| 16 | + /** |
|
| 17 | + * Global options |
|
| 18 | + * fileTypes to allow to upload |
|
| 19 | + */ |
|
| 20 | + var $options = array(); |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * errors holds any errors that occur as string values. |
|
| 24 | - * this can be access to debug the FileUploadComponent |
|
| 25 | - * |
|
| 26 | - * @var array |
|
| 27 | - * @access public |
|
| 28 | - */ |
|
| 29 | - var $errors = array(); |
|
| 22 | + /** |
|
| 23 | + * errors holds any errors that occur as string values. |
|
| 24 | + * this can be access to debug the FileUploadComponent |
|
| 25 | + * |
|
| 26 | + * @var array |
|
| 27 | + * @access public |
|
| 28 | + */ |
|
| 29 | + var $errors = array(); |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Definitions of errors that could occur during upload |
|
| 33 | - * |
|
| 34 | - * @author Jon Langevin |
|
| 35 | - * @var array |
|
| 36 | - */ |
|
| 37 | - var $uploadErrors = array( |
|
| 31 | + /** |
|
| 32 | + * Definitions of errors that could occur during upload |
|
| 33 | + * |
|
| 34 | + * @author Jon Langevin |
|
| 35 | + * @var array |
|
| 36 | + */ |
|
| 37 | + var $uploadErrors = array( |
|
| 38 | 38 | UPLOAD_ERR_OK => 'There is no error, the file uploaded with success.', |
| 39 | 39 | UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.', |
| 40 | 40 | UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.', |
@@ -43,87 +43,87 @@ discard block |
||
| 43 | 43 | UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.', //Introduced in PHP 4.3.10 and PHP 5.0.3. |
| 44 | 44 | UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.', //Introduced in PHP 5.1.0. |
| 45 | 45 | UPLOAD_ERR_EXTENSION => 'File upload stopped by extension.' //Introduced in PHP 5.2.0. |
| 46 | - ); |
|
| 46 | + ); |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Final file is set on move_uploaded_file success. |
|
| 50 | - * This is the file name of the final file that was uploaded |
|
| 51 | - * to the uploadDir directory |
|
| 52 | - * |
|
| 53 | - * @var string of final file name uploaded |
|
| 54 | - * @access public |
|
| 55 | - */ |
|
| 56 | - var $finalFile = null; |
|
| 48 | + /** |
|
| 49 | + * Final file is set on move_uploaded_file success. |
|
| 50 | + * This is the file name of the final file that was uploaded |
|
| 51 | + * to the uploadDir directory |
|
| 52 | + * |
|
| 53 | + * @var string of final file name uploaded |
|
| 54 | + * @access public |
|
| 55 | + */ |
|
| 56 | + var $finalFile = null; |
|
| 57 | 57 | |
| 58 | - function __construct($options = array()){ |
|
| 58 | + function __construct($options = array()){ |
|
| 59 | 59 | $this->options = array_merge($this->options, $options); |
| 60 | - } |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Preform requested callbacks on the filename. |
|
| 64 | - * |
|
| 65 | - * @var string chosen filename |
|
| 66 | - * @return string of resulting filename |
|
| 67 | - * @access private |
|
| 68 | - */ |
|
| 69 | - function __handleFileNameCallback($fileName){ |
|
| 62 | + /** |
|
| 63 | + * Preform requested callbacks on the filename. |
|
| 64 | + * |
|
| 65 | + * @var string chosen filename |
|
| 66 | + * @return string of resulting filename |
|
| 67 | + * @access private |
|
| 68 | + */ |
|
| 69 | + function __handleFileNameCallback($fileName){ |
|
| 70 | 70 | if($this->options['fileNameFunction']){ |
| 71 | - if($this->options['fileModel']){ |
|
| 71 | + if($this->options['fileModel']){ |
|
| 72 | 72 | $Model = ClassRegistry::init($this->options['fileModel']); |
| 73 | 73 | if(method_exists($Model, $this->options['fileNameFunction'])){ |
| 74 | - $fileName = $Model->{$this->options['fileNameFunction']}($fileName); |
|
| 74 | + $fileName = $Model->{$this->options['fileNameFunction']}($fileName); |
|
| 75 | 75 | } |
| 76 | 76 | elseif(function_exists($this->options['fileNameFunction'])){ |
| 77 | - $fileName = call_user_func($this->options['fileNameFunction'], $fileName); |
|
| 77 | + $fileName = call_user_func($this->options['fileNameFunction'], $fileName); |
|
| 78 | + } |
|
| 78 | 79 | } |
| 79 | - } |
|
| 80 | - else { |
|
| 80 | + else { |
|
| 81 | 81 | if(function_exists($this->options['fileNameFunction'])){ |
| 82 | - $fileName = call_user_func($this->options['fileNameFunction'], $fileName); |
|
| 82 | + $fileName = call_user_func($this->options['fileNameFunction'], $fileName); |
|
| 83 | + } |
|
| 83 | 84 | } |
| 84 | - } |
|
| 85 | 85 | |
| 86 | - if(!$fileName){ |
|
| 86 | + if(!$fileName){ |
|
| 87 | 87 | $this->_error('No filename resulting after parsing. Function: ' . $this->options['fileNameFunction']); |
| 88 | - } |
|
| 88 | + } |
|
| 89 | 89 | } |
| 90 | 90 | return $fileName; |
| 91 | - } |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Preform requested target patch checks depending on the unique setting |
|
| 95 | - * |
|
| 96 | - * @var string chosen filename target_path |
|
| 97 | - * @return string of resulting target_path |
|
| 98 | - * @access private |
|
| 99 | - */ |
|
| 100 | - function __handleUnique($target_path){ |
|
| 93 | + /** |
|
| 94 | + * Preform requested target patch checks depending on the unique setting |
|
| 95 | + * |
|
| 96 | + * @var string chosen filename target_path |
|
| 97 | + * @return string of resulting target_path |
|
| 98 | + * @access private |
|
| 99 | + */ |
|
| 100 | + function __handleUnique($target_path){ |
|
| 101 | 101 | if($this->options['unique']){ |
| 102 | - $temp_path = substr($target_path, 0, strlen($target_path) - strlen($this->_ext())); //temp path without the ext |
|
| 103 | - $i=1; |
|
| 104 | - while(file_exists($target_path)){ |
|
| 102 | + $temp_path = substr($target_path, 0, strlen($target_path) - strlen($this->_ext())); //temp path without the ext |
|
| 103 | + $i=1; |
|
| 104 | + while(file_exists($target_path)){ |
|
| 105 | 105 | $target_path = $temp_path . "-" . $i . $this->_ext(); |
| 106 | 106 | $i++; |
| 107 | - } |
|
| 108 | - } |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | 109 | return $target_path; |
| 110 | - } |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * processFile will take a file, or use the current file given to it |
|
| 114 | - * and attempt to save the file to the file system. |
|
| 115 | - * processFile will check to make sure the file is there, and its type is allowed to be saved. |
|
| 116 | - * |
|
| 117 | - * @param file array of uploaded file (optional) |
|
| 118 | - * @return String | false String of finalFile name saved to the file system or false if unable to save to file system. |
|
| 119 | - * @access public |
|
| 120 | - */ |
|
| 121 | - function processFile($file = null){ |
|
| 112 | + /** |
|
| 113 | + * processFile will take a file, or use the current file given to it |
|
| 114 | + * and attempt to save the file to the file system. |
|
| 115 | + * processFile will check to make sure the file is there, and its type is allowed to be saved. |
|
| 116 | + * |
|
| 117 | + * @param file array of uploaded file (optional) |
|
| 118 | + * @return String | false String of finalFile name saved to the file system or false if unable to save to file system. |
|
| 119 | + * @access public |
|
| 120 | + */ |
|
| 121 | + function processFile($file = null){ |
|
| 122 | 122 | $this->setFile($file); |
| 123 | 123 | |
| 124 | 124 | //check if we have a file and if we allow the type, return false otherwise. |
| 125 | 125 | if(!$this->checkFile() || !$this->checkType() || !$this->checkSize()){ |
| 126 | - return false; |
|
| 126 | + return false; |
|
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | //make sure the file doesn't already exist, if it does, add an itteration to it |
@@ -131,143 +131,143 @@ discard block |
||
| 131 | 131 | $fileName = $this->__handleFileNameCallback($this->file['name']); |
| 132 | 132 | //if callback returns false hault the upload |
| 133 | 133 | if(!$fileName){ |
| 134 | - return false; |
|
| 134 | + return false; |
|
| 135 | 135 | } |
| 136 | 136 | $target_path = $up_dir . DS . $fileName; |
| 137 | 137 | $target_path = $this->__handleUnique($target_path); |
| 138 | 138 | |
| 139 | 139 | //now move the file. |
| 140 | 140 | if(move_uploaded_file($this->file['tmp_name'], $target_path)){ |
| 141 | - $this->finalFile = basename($target_path); |
|
| 142 | - return $this->finalFile; |
|
| 141 | + $this->finalFile = basename($target_path); |
|
| 142 | + return $this->finalFile; |
|
| 143 | 143 | } |
| 144 | 144 | else{ |
| 145 | - $this->_error('Unable to save temp file to file system.'); |
|
| 146 | - return false; |
|
| 145 | + $this->_error('Unable to save temp file to file system.'); |
|
| 146 | + return false; |
|
| 147 | + } |
|
| 147 | 148 | } |
| 148 | - } |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * setFile will set a this->file if given one. |
|
| 152 | - * |
|
| 153 | - * @param file array of uploaded file. (optional) |
|
| 154 | - * @return void |
|
| 155 | - */ |
|
| 156 | - function setFile($file = null){ |
|
| 150 | + /** |
|
| 151 | + * setFile will set a this->file if given one. |
|
| 152 | + * |
|
| 153 | + * @param file array of uploaded file. (optional) |
|
| 154 | + * @return void |
|
| 155 | + */ |
|
| 156 | + function setFile($file = null){ |
|
| 157 | 157 | if($file) $this->file = $file; |
| 158 | - } |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - /** |
|
| 161 | - * Returns the extension of the uploaded filename. |
|
| 162 | - * |
|
| 163 | - * @return string $extension A filename extension |
|
| 164 | - * @param file array of uploaded file (optional) |
|
| 165 | - * @access protected |
|
| 166 | - */ |
|
| 167 | - function _ext($file = null){ |
|
| 160 | + /** |
|
| 161 | + * Returns the extension of the uploaded filename. |
|
| 162 | + * |
|
| 163 | + * @return string $extension A filename extension |
|
| 164 | + * @param file array of uploaded file (optional) |
|
| 165 | + * @access protected |
|
| 166 | + */ |
|
| 167 | + function _ext($file = null){ |
|
| 168 | 168 | $this->setFile($file); |
| 169 | 169 | return strrchr($this->file['name'],"."); |
| 170 | - } |
|
| 170 | + } |
|
| 171 | 171 | |
| 172 | - /** |
|
| 173 | - * Adds error messages to the component |
|
| 174 | - * |
|
| 175 | - * @param string $text String of error message to save |
|
| 176 | - * @return void |
|
| 177 | - * @access protected |
|
| 178 | - */ |
|
| 179 | - function _error($text){ |
|
| 172 | + /** |
|
| 173 | + * Adds error messages to the component |
|
| 174 | + * |
|
| 175 | + * @param string $text String of error message to save |
|
| 176 | + * @return void |
|
| 177 | + * @access protected |
|
| 178 | + */ |
|
| 179 | + function _error($text){ |
|
| 180 | 180 | $this->errors[] = __($text); |
| 181 | - } |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | - /** |
|
| 184 | - * Checks if the uploaded type is allowed defined in the allowedTypes |
|
| 185 | - * |
|
| 186 | - * @return boolean if type is accepted |
|
| 187 | - * @param file array of uploaded file (optional) |
|
| 188 | - * @access public |
|
| 189 | - */ |
|
| 190 | - function checkType($file = null){ |
|
| 183 | + /** |
|
| 184 | + * Checks if the uploaded type is allowed defined in the allowedTypes |
|
| 185 | + * |
|
| 186 | + * @return boolean if type is accepted |
|
| 187 | + * @param file array of uploaded file (optional) |
|
| 188 | + * @access public |
|
| 189 | + */ |
|
| 190 | + function checkType($file = null){ |
|
| 191 | 191 | $this->setFile($file); |
| 192 | 192 | foreach($this->options['allowedTypes'] as $ext => $types){ |
| 193 | - if(!is_string($ext)){ |
|
| 193 | + if(!is_string($ext)){ |
|
| 194 | 194 | $ext = $types; |
| 195 | - } |
|
| 196 | - if($ext == '*'){ |
|
| 195 | + } |
|
| 196 | + if($ext == '*'){ |
|
| 197 | 197 | return true; |
| 198 | - } |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | - $ext = strtolower('.' . str_replace('.','', $ext)); |
|
| 201 | - $file_ext = strtolower($this->_ext()); |
|
| 202 | - if($file_ext == $ext){ |
|
| 200 | + $ext = strtolower('.' . str_replace('.','', $ext)); |
|
| 201 | + $file_ext = strtolower($this->_ext()); |
|
| 202 | + if($file_ext == $ext){ |
|
| 203 | 203 | if(is_array($types) && !in_array($this->file['type'], $types)){ |
| 204 | - $this->_error("{$this->file['type']} is not an allowed type."); |
|
| 205 | - return false; |
|
| 204 | + $this->_error("{$this->file['type']} is not an allowed type."); |
|
| 205 | + return false; |
|
| 206 | 206 | } |
| 207 | 207 | else { |
| 208 | - return true; |
|
| 208 | + return true; |
|
| 209 | + } |
|
| 209 | 210 | } |
| 210 | - } |
|
| 211 | 211 | } |
| 212 | 212 | |
| 213 | 213 | $this->_error("Extension is not allowed."); |
| 214 | 214 | return false; |
| 215 | - } |
|
| 215 | + } |
|
| 216 | 216 | |
| 217 | - /** |
|
| 218 | - * Checks if there is a file uploaded |
|
| 219 | - * |
|
| 220 | - * @return void |
|
| 221 | - * @access public |
|
| 222 | - * @param file array of uploaded file (optional) |
|
| 223 | - */ |
|
| 224 | - function checkFile($file = null){ |
|
| 217 | + /** |
|
| 218 | + * Checks if there is a file uploaded |
|
| 219 | + * |
|
| 220 | + * @return void |
|
| 221 | + * @access public |
|
| 222 | + * @param file array of uploaded file (optional) |
|
| 223 | + */ |
|
| 224 | + function checkFile($file = null){ |
|
| 225 | 225 | $this->setFile($file); |
| 226 | 226 | if($this->hasUpload() && $this->file){ |
| 227 | - if(isset($this->file['error']) && $this->file['error'] == UPLOAD_ERR_OK ) { |
|
| 227 | + if(isset($this->file['error']) && $this->file['error'] == UPLOAD_ERR_OK ) { |
|
| 228 | 228 | return true; |
| 229 | - } |
|
| 230 | - else { |
|
| 229 | + } |
|
| 230 | + else { |
|
| 231 | 231 | $this->_error($this->uploadErrors[$this->file['error']]); |
| 232 | - } |
|
| 232 | + } |
|
| 233 | 233 | } |
| 234 | 234 | return false; |
| 235 | - } |
|
| 235 | + } |
|
| 236 | 236 | |
| 237 | - /** |
|
| 238 | - * Checks if the file uploaded exceeds the maxFileSize setting (if there is onw) |
|
| 239 | - * |
|
| 240 | - * @return boolean |
|
| 241 | - * @access public |
|
| 242 | - * @param file array of uploaded file (optional) |
|
| 243 | - */ |
|
| 244 | - function checkSize($file = null){ |
|
| 237 | + /** |
|
| 238 | + * Checks if the file uploaded exceeds the maxFileSize setting (if there is onw) |
|
| 239 | + * |
|
| 240 | + * @return boolean |
|
| 241 | + * @access public |
|
| 242 | + * @param file array of uploaded file (optional) |
|
| 243 | + */ |
|
| 244 | + function checkSize($file = null){ |
|
| 245 | 245 | $this->setFile($file); |
| 246 | 246 | if($this->hasUpload() && $this->file){ |
| 247 | - if(!$this->options['maxFileSize']){ //We don't want to test maxFileSize |
|
| 247 | + if(!$this->options['maxFileSize']){ //We don't want to test maxFileSize |
|
| 248 | 248 | return true; |
| 249 | - } |
|
| 250 | - elseif($this->options['maxFileSize'] && $this->file['size'] < $this->options['maxFileSize']){ |
|
| 249 | + } |
|
| 250 | + elseif($this->options['maxFileSize'] && $this->file['size'] < $this->options['maxFileSize']){ |
|
| 251 | 251 | return true; |
| 252 | - } |
|
| 253 | - else { |
|
| 252 | + } |
|
| 253 | + else { |
|
| 254 | 254 | // $this->_error("File exceeds {$this->options['maxFileSize']} byte limit."); |
| 255 | - $this->_error("File exceeds size limit."); |
|
| 256 | - } |
|
| 255 | + $this->_error("File exceeds size limit."); |
|
| 256 | + } |
|
| 257 | 257 | } |
| 258 | 258 | return false; |
| 259 | - } |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - /** |
|
| 262 | - * removeFile removes a specific file from the uploaded directory |
|
| 263 | - * |
|
| 264 | - * @param string $name A reference to the filename to delete from the uploadDirectory |
|
| 265 | - * @return boolean |
|
| 266 | - * @access public |
|
| 267 | - */ |
|
| 268 | - function removeFile($name = null){ |
|
| 261 | + /** |
|
| 262 | + * removeFile removes a specific file from the uploaded directory |
|
| 263 | + * |
|
| 264 | + * @param string $name A reference to the filename to delete from the uploadDirectory |
|
| 265 | + * @return boolean |
|
| 266 | + * @access public |
|
| 267 | + */ |
|
| 268 | + function removeFile($name = null){ |
|
| 269 | 269 | if(!$name || strpos($name, '://')){ |
| 270 | - return false; |
|
| 270 | + return false; |
|
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | $up_dir = $this->options['uploadDir']; |
@@ -275,70 +275,70 @@ discard block |
||
| 275 | 275 | |
| 276 | 276 | //delete main image -- $name |
| 277 | 277 | if(@unlink($target_path)){ |
| 278 | - return true; |
|
| 278 | + return true; |
|
| 279 | 279 | } else { |
| 280 | - return false; |
|
| 280 | + return false; |
|
| 281 | + } |
|
| 281 | 282 | } |
| 282 | - } |
|
| 283 | 283 | |
| 284 | - /** |
|
| 285 | - * hasUpload |
|
| 286 | - * |
|
| 287 | - * @return boolean true | false depending if a file was actually uploaded. |
|
| 288 | - * @param file array of uploaded file (optional) |
|
| 289 | - */ |
|
| 290 | - function hasUpload($file = null){ |
|
| 284 | + /** |
|
| 285 | + * hasUpload |
|
| 286 | + * |
|
| 287 | + * @return boolean true | false depending if a file was actually uploaded. |
|
| 288 | + * @param file array of uploaded file (optional) |
|
| 289 | + */ |
|
| 290 | + function hasUpload($file = null){ |
|
| 291 | 291 | $this->setFile($file); |
| 292 | 292 | return ($this->_multiArrayKeyExists("tmp_name", $this->file)); |
| 293 | - } |
|
| 293 | + } |
|
| 294 | 294 | |
| 295 | - /** |
|
| 296 | - * @return boolean true if errors were detected. |
|
| 297 | - */ |
|
| 298 | - function hasErrors(){ |
|
| 295 | + /** |
|
| 296 | + * @return boolean true if errors were detected. |
|
| 297 | + */ |
|
| 298 | + function hasErrors(){ |
|
| 299 | 299 | return count($this->errors); |
| 300 | - } |
|
| 300 | + } |
|
| 301 | 301 | |
| 302 | - /** |
|
| 303 | - * showErrors itterates through the errors array |
|
| 304 | - * and returns a concatinated string of errors sepearated by |
|
| 305 | - * the $sep |
|
| 306 | - * |
|
| 307 | - * @param string $sep A seperated defaults to <br /> |
|
| 308 | - * @return string |
|
| 309 | - * @access public |
|
| 310 | - */ |
|
| 311 | - function showErrors($sep = " "){ |
|
| 302 | + /** |
|
| 303 | + * showErrors itterates through the errors array |
|
| 304 | + * and returns a concatinated string of errors sepearated by |
|
| 305 | + * the $sep |
|
| 306 | + * |
|
| 307 | + * @param string $sep A seperated defaults to <br /> |
|
| 308 | + * @return string |
|
| 309 | + * @access public |
|
| 310 | + */ |
|
| 311 | + function showErrors($sep = " "){ |
|
| 312 | 312 | $retval = ""; |
| 313 | 313 | foreach($this->errors as $error){ |
| 314 | - $retval .= "$error $sep"; |
|
| 314 | + $retval .= "$error $sep"; |
|
| 315 | 315 | } |
| 316 | 316 | return $retval; |
| 317 | - } |
|
| 317 | + } |
|
| 318 | 318 | |
| 319 | - /** |
|
| 320 | - * Searches through the $haystack for a $key. |
|
| 321 | - * |
|
| 322 | - * @param string $needle String of key to search for in $haystack |
|
| 323 | - * @param array $haystack Array of which to search for $needle |
|
| 324 | - * @return boolean true if given key is in an array |
|
| 325 | - * @access protected |
|
| 326 | - */ |
|
| 327 | - function _multiArrayKeyExists($needle, $haystack) { |
|
| 319 | + /** |
|
| 320 | + * Searches through the $haystack for a $key. |
|
| 321 | + * |
|
| 322 | + * @param string $needle String of key to search for in $haystack |
|
| 323 | + * @param array $haystack Array of which to search for $needle |
|
| 324 | + * @return boolean true if given key is in an array |
|
| 325 | + * @access protected |
|
| 326 | + */ |
|
| 327 | + function _multiArrayKeyExists($needle, $haystack) { |
|
| 328 | 328 | if(is_array($haystack)){ |
| 329 | - foreach ($haystack as $key=>$value) { |
|
| 329 | + foreach ($haystack as $key=>$value) { |
|
| 330 | 330 | if ($needle===$key && $value) { |
| 331 | - return true; |
|
| 331 | + return true; |
|
| 332 | 332 | } |
| 333 | 333 | if (is_array($value)) { |
| 334 | - if($this->_multiArrayKeyExists($needle, $value)){ |
|
| 334 | + if($this->_multiArrayKeyExists($needle, $value)){ |
|
| 335 | 335 | return true; |
| 336 | - } |
|
| 336 | + } |
|
| 337 | + } |
|
| 337 | 338 | } |
| 338 | - } |
|
| 339 | 339 | } |
| 340 | 340 | return false; |
| 341 | - } |
|
| 341 | + } |
|
| 342 | 342 | |
| 343 | 343 | } |
| 344 | 344 | ?> |
| 345 | 345 | \ No newline at end of file |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | */ |
| 56 | 56 | var $finalFile = null; |
| 57 | 57 | |
| 58 | - function __construct($options = array()){ |
|
| 58 | + function __construct($options = array()) { |
|
| 59 | 59 | $this->options = array_merge($this->options, $options); |
| 60 | 60 | } |
| 61 | 61 | |
@@ -66,25 +66,25 @@ discard block |
||
| 66 | 66 | * @return string of resulting filename |
| 67 | 67 | * @access private |
| 68 | 68 | */ |
| 69 | - function __handleFileNameCallback($fileName){ |
|
| 70 | - if($this->options['fileNameFunction']){ |
|
| 71 | - if($this->options['fileModel']){ |
|
| 69 | + function __handleFileNameCallback($fileName) { |
|
| 70 | + if ($this->options['fileNameFunction']) { |
|
| 71 | + if ($this->options['fileModel']) { |
|
| 72 | 72 | $Model = ClassRegistry::init($this->options['fileModel']); |
| 73 | - if(method_exists($Model, $this->options['fileNameFunction'])){ |
|
| 73 | + if (method_exists($Model, $this->options['fileNameFunction'])) { |
|
| 74 | 74 | $fileName = $Model->{$this->options['fileNameFunction']}($fileName); |
| 75 | 75 | } |
| 76 | - elseif(function_exists($this->options['fileNameFunction'])){ |
|
| 76 | + elseif (function_exists($this->options['fileNameFunction'])) { |
|
| 77 | 77 | $fileName = call_user_func($this->options['fileNameFunction'], $fileName); |
| 78 | 78 | } |
| 79 | 79 | } |
| 80 | 80 | else { |
| 81 | - if(function_exists($this->options['fileNameFunction'])){ |
|
| 81 | + if (function_exists($this->options['fileNameFunction'])) { |
|
| 82 | 82 | $fileName = call_user_func($this->options['fileNameFunction'], $fileName); |
| 83 | 83 | } |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | - if(!$fileName){ |
|
| 87 | - $this->_error('No filename resulting after parsing. Function: ' . $this->options['fileNameFunction']); |
|
| 86 | + if (!$fileName) { |
|
| 87 | + $this->_error('No filename resulting after parsing. Function: '.$this->options['fileNameFunction']); |
|
| 88 | 88 | } |
| 89 | 89 | } |
| 90 | 90 | return $fileName; |
@@ -97,12 +97,12 @@ discard block |
||
| 97 | 97 | * @return string of resulting target_path |
| 98 | 98 | * @access private |
| 99 | 99 | */ |
| 100 | - function __handleUnique($target_path){ |
|
| 101 | - if($this->options['unique']){ |
|
| 100 | + function __handleUnique($target_path) { |
|
| 101 | + if ($this->options['unique']) { |
|
| 102 | 102 | $temp_path = substr($target_path, 0, strlen($target_path) - strlen($this->_ext())); //temp path without the ext |
| 103 | - $i=1; |
|
| 104 | - while(file_exists($target_path)){ |
|
| 105 | - $target_path = $temp_path . "-" . $i . $this->_ext(); |
|
| 103 | + $i = 1; |
|
| 104 | + while (file_exists($target_path)) { |
|
| 105 | + $target_path = $temp_path."-".$i.$this->_ext(); |
|
| 106 | 106 | $i++; |
| 107 | 107 | } |
| 108 | 108 | } |
@@ -118,11 +118,11 @@ discard block |
||
| 118 | 118 | * @return String | false String of finalFile name saved to the file system or false if unable to save to file system. |
| 119 | 119 | * @access public |
| 120 | 120 | */ |
| 121 | - function processFile($file = null){ |
|
| 121 | + function processFile($file = null) { |
|
| 122 | 122 | $this->setFile($file); |
| 123 | 123 | |
| 124 | 124 | //check if we have a file and if we allow the type, return false otherwise. |
| 125 | - if(!$this->checkFile() || !$this->checkType() || !$this->checkSize()){ |
|
| 125 | + if (!$this->checkFile() || !$this->checkType() || !$this->checkSize()) { |
|
| 126 | 126 | return false; |
| 127 | 127 | } |
| 128 | 128 | |
@@ -130,18 +130,18 @@ discard block |
||
| 130 | 130 | $up_dir = $this->options['uploadDir']; |
| 131 | 131 | $fileName = $this->__handleFileNameCallback($this->file['name']); |
| 132 | 132 | //if callback returns false hault the upload |
| 133 | - if(!$fileName){ |
|
| 133 | + if (!$fileName) { |
|
| 134 | 134 | return false; |
| 135 | 135 | } |
| 136 | - $target_path = $up_dir . DS . $fileName; |
|
| 136 | + $target_path = $up_dir.DS.$fileName; |
|
| 137 | 137 | $target_path = $this->__handleUnique($target_path); |
| 138 | 138 | |
| 139 | 139 | //now move the file. |
| 140 | - if(move_uploaded_file($this->file['tmp_name'], $target_path)){ |
|
| 140 | + if (move_uploaded_file($this->file['tmp_name'], $target_path)) { |
|
| 141 | 141 | $this->finalFile = basename($target_path); |
| 142 | 142 | return $this->finalFile; |
| 143 | 143 | } |
| 144 | - else{ |
|
| 144 | + else { |
|
| 145 | 145 | $this->_error('Unable to save temp file to file system.'); |
| 146 | 146 | return false; |
| 147 | 147 | } |
@@ -153,8 +153,8 @@ discard block |
||
| 153 | 153 | * @param file array of uploaded file. (optional) |
| 154 | 154 | * @return void |
| 155 | 155 | */ |
| 156 | - function setFile($file = null){ |
|
| 157 | - if($file) $this->file = $file; |
|
| 156 | + function setFile($file = null) { |
|
| 157 | + if ($file) $this->file = $file; |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | /** |
@@ -164,9 +164,9 @@ discard block |
||
| 164 | 164 | * @param file array of uploaded file (optional) |
| 165 | 165 | * @access protected |
| 166 | 166 | */ |
| 167 | - function _ext($file = null){ |
|
| 167 | + function _ext($file = null) { |
|
| 168 | 168 | $this->setFile($file); |
| 169 | - return strrchr($this->file['name'],"."); |
|
| 169 | + return strrchr($this->file['name'], "."); |
|
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | /** |
@@ -176,7 +176,7 @@ discard block |
||
| 176 | 176 | * @return void |
| 177 | 177 | * @access protected |
| 178 | 178 | */ |
| 179 | - function _error($text){ |
|
| 179 | + function _error($text) { |
|
| 180 | 180 | $this->errors[] = __($text); |
| 181 | 181 | } |
| 182 | 182 | |
@@ -187,20 +187,20 @@ discard block |
||
| 187 | 187 | * @param file array of uploaded file (optional) |
| 188 | 188 | * @access public |
| 189 | 189 | */ |
| 190 | - function checkType($file = null){ |
|
| 190 | + function checkType($file = null) { |
|
| 191 | 191 | $this->setFile($file); |
| 192 | - foreach($this->options['allowedTypes'] as $ext => $types){ |
|
| 193 | - if(!is_string($ext)){ |
|
| 192 | + foreach ($this->options['allowedTypes'] as $ext => $types) { |
|
| 193 | + if (!is_string($ext)) { |
|
| 194 | 194 | $ext = $types; |
| 195 | 195 | } |
| 196 | - if($ext == '*'){ |
|
| 196 | + if ($ext == '*') { |
|
| 197 | 197 | return true; |
| 198 | 198 | } |
| 199 | 199 | |
| 200 | - $ext = strtolower('.' . str_replace('.','', $ext)); |
|
| 200 | + $ext = strtolower('.'.str_replace('.', '', $ext)); |
|
| 201 | 201 | $file_ext = strtolower($this->_ext()); |
| 202 | - if($file_ext == $ext){ |
|
| 203 | - if(is_array($types) && !in_array($this->file['type'], $types)){ |
|
| 202 | + if ($file_ext == $ext) { |
|
| 203 | + if (is_array($types) && !in_array($this->file['type'], $types)) { |
|
| 204 | 204 | $this->_error("{$this->file['type']} is not an allowed type."); |
| 205 | 205 | return false; |
| 206 | 206 | } |
@@ -221,10 +221,10 @@ discard block |
||
| 221 | 221 | * @access public |
| 222 | 222 | * @param file array of uploaded file (optional) |
| 223 | 223 | */ |
| 224 | - function checkFile($file = null){ |
|
| 224 | + function checkFile($file = null) { |
|
| 225 | 225 | $this->setFile($file); |
| 226 | - if($this->hasUpload() && $this->file){ |
|
| 227 | - if(isset($this->file['error']) && $this->file['error'] == UPLOAD_ERR_OK ) { |
|
| 226 | + if ($this->hasUpload() && $this->file) { |
|
| 227 | + if (isset($this->file['error']) && $this->file['error'] == UPLOAD_ERR_OK) { |
|
| 228 | 228 | return true; |
| 229 | 229 | } |
| 230 | 230 | else { |
@@ -241,13 +241,13 @@ discard block |
||
| 241 | 241 | * @access public |
| 242 | 242 | * @param file array of uploaded file (optional) |
| 243 | 243 | */ |
| 244 | - function checkSize($file = null){ |
|
| 244 | + function checkSize($file = null) { |
|
| 245 | 245 | $this->setFile($file); |
| 246 | - if($this->hasUpload() && $this->file){ |
|
| 247 | - if(!$this->options['maxFileSize']){ //We don't want to test maxFileSize |
|
| 246 | + if ($this->hasUpload() && $this->file) { |
|
| 247 | + if (!$this->options['maxFileSize']) { //We don't want to test maxFileSize |
|
| 248 | 248 | return true; |
| 249 | 249 | } |
| 250 | - elseif($this->options['maxFileSize'] && $this->file['size'] < $this->options['maxFileSize']){ |
|
| 250 | + elseif ($this->options['maxFileSize'] && $this->file['size'] < $this->options['maxFileSize']) { |
|
| 251 | 251 | return true; |
| 252 | 252 | } |
| 253 | 253 | else { |
@@ -265,16 +265,16 @@ discard block |
||
| 265 | 265 | * @return boolean |
| 266 | 266 | * @access public |
| 267 | 267 | */ |
| 268 | - function removeFile($name = null){ |
|
| 269 | - if(!$name || strpos($name, '://')){ |
|
| 268 | + function removeFile($name = null) { |
|
| 269 | + if (!$name || strpos($name, '://')) { |
|
| 270 | 270 | return false; |
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | $up_dir = $this->options['uploadDir']; |
| 274 | - $target_path = $up_dir . DS . $name; |
|
| 274 | + $target_path = $up_dir.DS.$name; |
|
| 275 | 275 | |
| 276 | 276 | //delete main image -- $name |
| 277 | - if(@unlink($target_path)){ |
|
| 277 | + if (@unlink($target_path)) { |
|
| 278 | 278 | return true; |
| 279 | 279 | } else { |
| 280 | 280 | return false; |
@@ -287,7 +287,7 @@ discard block |
||
| 287 | 287 | * @return boolean true | false depending if a file was actually uploaded. |
| 288 | 288 | * @param file array of uploaded file (optional) |
| 289 | 289 | */ |
| 290 | - function hasUpload($file = null){ |
|
| 290 | + function hasUpload($file = null) { |
|
| 291 | 291 | $this->setFile($file); |
| 292 | 292 | return ($this->_multiArrayKeyExists("tmp_name", $this->file)); |
| 293 | 293 | } |
@@ -295,7 +295,7 @@ discard block |
||
| 295 | 295 | /** |
| 296 | 296 | * @return boolean true if errors were detected. |
| 297 | 297 | */ |
| 298 | - function hasErrors(){ |
|
| 298 | + function hasErrors() { |
|
| 299 | 299 | return count($this->errors); |
| 300 | 300 | } |
| 301 | 301 | |
@@ -308,9 +308,9 @@ discard block |
||
| 308 | 308 | * @return string |
| 309 | 309 | * @access public |
| 310 | 310 | */ |
| 311 | - function showErrors($sep = " "){ |
|
| 311 | + function showErrors($sep = " ") { |
|
| 312 | 312 | $retval = ""; |
| 313 | - foreach($this->errors as $error){ |
|
| 313 | + foreach ($this->errors as $error) { |
|
| 314 | 314 | $retval .= "$error $sep"; |
| 315 | 315 | } |
| 316 | 316 | return $retval; |
@@ -325,13 +325,13 @@ discard block |
||
| 325 | 325 | * @access protected |
| 326 | 326 | */ |
| 327 | 327 | function _multiArrayKeyExists($needle, $haystack) { |
| 328 | - if(is_array($haystack)){ |
|
| 328 | + if (is_array($haystack)) { |
|
| 329 | 329 | foreach ($haystack as $key=>$value) { |
| 330 | - if ($needle===$key && $value) { |
|
| 330 | + if ($needle === $key && $value) { |
|
| 331 | 331 | return true; |
| 332 | 332 | } |
| 333 | 333 | if (is_array($value)) { |
| 334 | - if($this->_multiArrayKeyExists($needle, $value)){ |
|
| 334 | + if ($this->_multiArrayKeyExists($needle, $value)) { |
|
| 335 | 335 | return true; |
| 336 | 336 | } |
| 337 | 337 | } |
@@ -72,12 +72,10 @@ discard block |
||
| 72 | 72 | $Model = ClassRegistry::init($this->options['fileModel']); |
| 73 | 73 | if(method_exists($Model, $this->options['fileNameFunction'])){ |
| 74 | 74 | $fileName = $Model->{$this->options['fileNameFunction']}($fileName); |
| 75 | - } |
|
| 76 | - elseif(function_exists($this->options['fileNameFunction'])){ |
|
| 75 | + } elseif(function_exists($this->options['fileNameFunction'])){ |
|
| 77 | 76 | $fileName = call_user_func($this->options['fileNameFunction'], $fileName); |
| 78 | 77 | } |
| 79 | - } |
|
| 80 | - else { |
|
| 78 | + } else { |
|
| 81 | 79 | if(function_exists($this->options['fileNameFunction'])){ |
| 82 | 80 | $fileName = call_user_func($this->options['fileNameFunction'], $fileName); |
| 83 | 81 | } |
@@ -140,8 +138,7 @@ discard block |
||
| 140 | 138 | if(move_uploaded_file($this->file['tmp_name'], $target_path)){ |
| 141 | 139 | $this->finalFile = basename($target_path); |
| 142 | 140 | return $this->finalFile; |
| 143 | - } |
|
| 144 | - else{ |
|
| 141 | + } else{ |
|
| 145 | 142 | $this->_error('Unable to save temp file to file system.'); |
| 146 | 143 | return false; |
| 147 | 144 | } |
@@ -154,7 +151,9 @@ discard block |
||
| 154 | 151 | * @return void |
| 155 | 152 | */ |
| 156 | 153 | function setFile($file = null){ |
| 157 | - if($file) $this->file = $file; |
|
| 154 | + if($file) { |
|
| 155 | + $this->file = $file; |
|
| 156 | + } |
|
| 158 | 157 | } |
| 159 | 158 | |
| 160 | 159 | /** |
@@ -203,8 +202,7 @@ discard block |
||
| 203 | 202 | if(is_array($types) && !in_array($this->file['type'], $types)){ |
| 204 | 203 | $this->_error("{$this->file['type']} is not an allowed type."); |
| 205 | 204 | return false; |
| 206 | - } |
|
| 207 | - else { |
|
| 205 | + } else { |
|
| 208 | 206 | return true; |
| 209 | 207 | } |
| 210 | 208 | } |
@@ -226,8 +224,7 @@ discard block |
||
| 226 | 224 | if($this->hasUpload() && $this->file){ |
| 227 | 225 | if(isset($this->file['error']) && $this->file['error'] == UPLOAD_ERR_OK ) { |
| 228 | 226 | return true; |
| 229 | - } |
|
| 230 | - else { |
|
| 227 | + } else { |
|
| 231 | 228 | $this->_error($this->uploadErrors[$this->file['error']]); |
| 232 | 229 | } |
| 233 | 230 | } |
@@ -246,11 +243,9 @@ discard block |
||
| 246 | 243 | if($this->hasUpload() && $this->file){ |
| 247 | 244 | if(!$this->options['maxFileSize']){ //We don't want to test maxFileSize |
| 248 | 245 | return true; |
| 249 | - } |
|
| 250 | - elseif($this->options['maxFileSize'] && $this->file['size'] < $this->options['maxFileSize']){ |
|
| 246 | + } elseif($this->options['maxFileSize'] && $this->file['size'] < $this->options['maxFileSize']){ |
|
| 251 | 247 | return true; |
| 252 | - } |
|
| 253 | - else { |
|
| 248 | + } else { |
|
| 254 | 249 | // $this->_error("File exceeds {$this->options['maxFileSize']} byte limit."); |
| 255 | 250 | $this->_error("File exceeds size limit."); |
| 256 | 251 | } |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | * @param Array|Int $options takes an array of options passed to the image helper, or an integer representing the width of the image to display |
| 100 | 100 | * options: width = 100 (default), if width is set along with autoResize the uploaded image will be resized. |
| 101 | 101 | * @access public |
| 102 | - * @return mixed html tag, url string, or false if unable to find image. |
|
| 102 | + * @return string|false html tag, url string, or false if unable to find image. |
|
| 103 | 103 | */ |
| 104 | 104 | function image($name, $options = array()){ |
| 105 | 105 | $this->fileName = $name; |
@@ -329,7 +329,7 @@ discard block |
||
| 329 | 329 | * Constructor |
| 330 | 330 | * |
| 331 | 331 | * @param [String $imgFile] Image File Name |
| 332 | - * @return RESIZEIMAGE (Class Object) |
|
| 332 | + * @return false|null (Class Object) |
|
| 333 | 333 | */ |
| 334 | 334 | function __construct($imgFile=""){ |
| 335 | 335 | if (!function_exists("imagecreate")){ |
@@ -356,7 +356,7 @@ discard block |
||
| 356 | 356 | * Set image file name |
| 357 | 357 | * |
| 358 | 358 | * @param String $imgFile |
| 359 | - * @return void |
|
| 359 | + * @return boolean |
|
| 360 | 360 | */ |
| 361 | 361 | function setImage($imgFile){ |
| 362 | 362 | $this->imgFile=$imgFile; |
@@ -374,7 +374,7 @@ discard block |
||
| 374 | 374 | * Resize a image to given width and height and keep it's current width and height ratio |
| 375 | 375 | * |
| 376 | 376 | * @param Number $imgwidth |
| 377 | - * @param Numnber $imgheight |
|
| 377 | + * @param integer $imgheight |
|
| 378 | 378 | * @param String $newfile |
| 379 | 379 | */ |
| 380 | 380 | function resize_limitwh($imgwidth,$imgheight,$newfile=NULL){ |
@@ -394,9 +394,9 @@ discard block |
||
| 394 | 394 | /** |
| 395 | 395 | * Resize an image to given percentage. |
| 396 | 396 | * |
| 397 | - * @param Number $percent |
|
| 397 | + * @param integer $percent |
|
| 398 | 398 | * @param String $newfile |
| 399 | - * @return Boolean |
|
| 399 | + * @return false|null |
|
| 400 | 400 | */ |
| 401 | 401 | function resize_percentage($percent=100,$newfile=NULL) { |
| 402 | 402 | $newWidth=($this->imgWidth*$percent)/100; |
@@ -407,10 +407,10 @@ discard block |
||
| 407 | 407 | /** |
| 408 | 408 | * Resize an image to given X and Y percentage. |
| 409 | 409 | * |
| 410 | - * @param Number $xpercent |
|
| 411 | - * @param Number $ypercent |
|
| 410 | + * @param integer $xpercent |
|
| 411 | + * @param integer $ypercent |
|
| 412 | 412 | * @param String $newfile |
| 413 | - * @return Boolean |
|
| 413 | + * @return false|null |
|
| 414 | 414 | */ |
| 415 | 415 | function resize_xypercentage($xpercent=100,$ypercent=100,$newfile=NULL) { |
| 416 | 416 | $newWidth=($this->imgWidth*$xpercent)/100; |
@@ -421,10 +421,10 @@ discard block |
||
| 421 | 421 | /** |
| 422 | 422 | * Resize an image to given width and height |
| 423 | 423 | * |
| 424 | - * @param Number $width |
|
| 425 | - * @param Number $height |
|
| 424 | + * @param integer $width |
|
| 425 | + * @param integer $height |
|
| 426 | 426 | * @param String $newfile |
| 427 | - * @return Boolean |
|
| 427 | + * @return false|null |
|
| 428 | 428 | */ |
| 429 | 429 | function resize($width,$height,$newfile=NULL){ |
| 430 | 430 | if(empty($this->imgFile)){ |
@@ -483,7 +483,7 @@ discard block |
||
| 483 | 483 | * @param Number $width |
| 484 | 484 | * @param Number $height |
| 485 | 485 | * @param String $newfile |
| 486 | - * @return Boolean |
|
| 486 | + * @return false|null |
|
| 487 | 487 | */ |
| 488 | 488 | function _resize($width,$height,$newfile=NULL){ |
| 489 | 489 | if (!function_exists("imagecreate")){ |
@@ -1,160 +1,160 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * FileUPloadHelper is the helper to the component FileUploadComponent. |
|
| 4 | - * This helper REQUIRES the FileUploadComponent. |
|
| 5 | - * |
|
| 6 | - * @author: Nick Baker |
|
| 7 | - * @version: 6.1.1 |
|
| 8 | - * @email: [email protected] |
|
| 9 | - * @link: http://www.webtechnick.com/blogs/view/221/CakePHP_File_Upload_Plugin |
|
| 10 | - * |
|
| 11 | - * @example |
|
| 12 | - * Show an already uploaded image |
|
| 13 | - * $fileUpload->image('filename.jpg', array('width' => 250)); //resizes a thumbnail of 'filename.jpg' to 250 |
|
| 14 | - * $fileUpload->image('filename.jpg', array('width' => 250, 'uploadDir' => 'custom/dir')); //resizes a thumbnail of 'webroot/custom/dir/filename.jpg' to 250 |
|
| 15 | - * |
|
| 16 | - * Show the upload field form |
|
| 17 | - * $fileUpload->input(); //builds the input form based on your FileUploadComponent defaults |
|
| 18 | - * -or- |
|
| 19 | - * $fileUpload->input(array('var' => 'fileVar', 'model' => 'Picture')); //customized input form. |
|
| 20 | - * |
|
| 21 | - */ |
|
| 3 | + * FileUPloadHelper is the helper to the component FileUploadComponent. |
|
| 4 | + * This helper REQUIRES the FileUploadComponent. |
|
| 5 | + * |
|
| 6 | + * @author: Nick Baker |
|
| 7 | + * @version: 6.1.1 |
|
| 8 | + * @email: [email protected] |
|
| 9 | + * @link: http://www.webtechnick.com/blogs/view/221/CakePHP_File_Upload_Plugin |
|
| 10 | + * |
|
| 11 | + * @example |
|
| 12 | + * Show an already uploaded image |
|
| 13 | + * $fileUpload->image('filename.jpg', array('width' => 250)); //resizes a thumbnail of 'filename.jpg' to 250 |
|
| 14 | + * $fileUpload->image('filename.jpg', array('width' => 250, 'uploadDir' => 'custom/dir')); //resizes a thumbnail of 'webroot/custom/dir/filename.jpg' to 250 |
|
| 15 | + * |
|
| 16 | + * Show the upload field form |
|
| 17 | + * $fileUpload->input(); //builds the input form based on your FileUploadComponent defaults |
|
| 18 | + * -or- |
|
| 19 | + * $fileUpload->input(array('var' => 'fileVar', 'model' => 'Picture')); //customized input form. |
|
| 20 | + * |
|
| 21 | + */ |
|
| 22 | 22 | App::import('Config', 'FileUpload.file_upload_settings'); |
| 23 | 23 | class FileUploadHelper extends AppHelper{ |
| 24 | - var $helpers = array('Html', 'Form'); |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * the name of the file passed in. |
|
| 28 | - */ |
|
| 29 | - var $fileName = NULL; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Holds the FileUpload component |
|
| 33 | - */ |
|
| 34 | - var $FileUpload = NULL; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Counts the number of inputs, for multiple fileUpload inputing. |
|
| 38 | - */ |
|
| 39 | - var $inputCount = 0; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Default options for showImage |
|
| 43 | - * |
|
| 44 | - * - width: the width of the image to display (0 means no resizing (default)) |
|
| 45 | - * - resizedDir: is the directory in which to save the resized files into (resized by default) |
|
| 46 | - * - imagePathOnly: will only return the requested image_path (false by default) |
|
| 47 | - * - autoResize: will resize the file automatically if given a valid width. (true by default) |
|
| 48 | - * - resizeThumbOnly: will only resize the image down -- not up past the original's size (true by default) |
|
| 49 | - */ |
|
| 50 | - var $options = array( |
|
| 24 | + var $helpers = array('Html', 'Form'); |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * the name of the file passed in. |
|
| 28 | + */ |
|
| 29 | + var $fileName = NULL; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Holds the FileUpload component |
|
| 33 | + */ |
|
| 34 | + var $FileUpload = NULL; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Counts the number of inputs, for multiple fileUpload inputing. |
|
| 38 | + */ |
|
| 39 | + var $inputCount = 0; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Default options for showImage |
|
| 43 | + * |
|
| 44 | + * - width: the width of the image to display (0 means no resizing (default)) |
|
| 45 | + * - resizedDir: is the directory in which to save the resized files into (resized by default) |
|
| 46 | + * - imagePathOnly: will only return the requested image_path (false by default) |
|
| 47 | + * - autoResize: will resize the file automatically if given a valid width. (true by default) |
|
| 48 | + * - resizeThumbOnly: will only resize the image down -- not up past the original's size (true by default) |
|
| 49 | + */ |
|
| 50 | + var $options = array( |
|
| 51 | 51 | 'width' => 0, //0 means no resizing |
| 52 | 52 | 'resizedDir' => 'resized', // make sure webroot/files/resized is chmod 777 |
| 53 | 53 | 'imagePathOnly' => false, //if true, will only return the requested image_path |
| 54 | 54 | 'autoResize' => true, //if true, will resize the file automatically if given a valid width. |
| 55 | 55 | 'resizeThumbOnly' => true //if true, will only resize the image down -- not up past the original's size |
| 56 | - ); |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * FileUpload Settings set in config/file_upload_settings.php |
|
| 60 | - */ |
|
| 61 | - var $settings = array(); |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Constructor, initiallizes the FileUpload Component |
|
| 65 | - * and sets the default options. |
|
| 66 | - */ |
|
| 67 | - function __construct(View $View, $settings = array()){ |
|
| 68 | - parent::__construct($View, $settings); |
|
| 56 | + ); |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * FileUpload Settings set in config/file_upload_settings.php |
|
| 60 | + */ |
|
| 61 | + var $settings = array(); |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Constructor, initiallizes the FileUpload Component |
|
| 65 | + * and sets the default options. |
|
| 66 | + */ |
|
| 67 | + function __construct(View $View, $settings = array()){ |
|
| 68 | + parent::__construct($View, $settings); |
|
| 69 | 69 | $this->FileUploadSettings = new FileUploadSettings; |
| 70 | 70 | |
| 71 | 71 | //setup settings |
| 72 | 72 | $this->settings = array_merge($this->FileUploadSettings->defaults, $this->options); |
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Reset the helper to its initial state |
|
| 77 | - * @access public |
|
| 78 | - * @return void |
|
| 79 | - */ |
|
| 80 | - function reset(){ |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Reset the helper to its initial state |
|
| 77 | + * @access public |
|
| 78 | + * @return void |
|
| 79 | + */ |
|
| 80 | + function reset(){ |
|
| 81 | 81 | $this->fileName = null; |
| 82 | 82 | $this->options = array( |
| 83 | - 'width' => 0, |
|
| 84 | - 'resizedDir' => 'resized', |
|
| 85 | - 'imagePathOnly' => false, |
|
| 86 | - 'autoResize' => true, |
|
| 87 | - 'resizeThumbOnly' => true |
|
| 83 | + 'width' => 0, |
|
| 84 | + 'resizedDir' => 'resized', |
|
| 85 | + 'imagePathOnly' => false, |
|
| 86 | + 'autoResize' => true, |
|
| 87 | + 'resizeThumbOnly' => true |
|
| 88 | 88 | ); |
| 89 | 89 | |
| 90 | 90 | //setup settings |
| 91 | 91 | $this->settings = array_merge($this->FileUploadSettings->defaults, $this->options); |
| 92 | 92 | unset($this->newImage); |
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * image takes a file_name or Upload.id and returns the HTML image |
|
| 97 | - * |
|
| 98 | - * @param String|Int $name takes a file_name or ID of uploaded file. |
|
| 99 | - * @param Array|Int $options takes an array of options passed to the image helper, or an integer representing the width of the image to display |
|
| 100 | - * options: width = 100 (default), if width is set along with autoResize the uploaded image will be resized. |
|
| 101 | - * @access public |
|
| 102 | - * @return mixed html tag, url string, or false if unable to find image. |
|
| 103 | - */ |
|
| 104 | - function image($name, $options = array()){ |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * image takes a file_name or Upload.id and returns the HTML image |
|
| 97 | + * |
|
| 98 | + * @param String|Int $name takes a file_name or ID of uploaded file. |
|
| 99 | + * @param Array|Int $options takes an array of options passed to the image helper, or an integer representing the width of the image to display |
|
| 100 | + * options: width = 100 (default), if width is set along with autoResize the uploaded image will be resized. |
|
| 101 | + * @access public |
|
| 102 | + * @return mixed html tag, url string, or false if unable to find image. |
|
| 103 | + */ |
|
| 104 | + function image($name, $options = array()){ |
|
| 105 | 105 | $this->fileName = $name; |
| 106 | 106 | //options takes in a width as well |
| 107 | 107 | if(is_int($options)){ |
| 108 | - $width = $options; |
|
| 109 | - $options = array(); |
|
| 110 | - $options['width'] = $width; |
|
| 108 | + $width = $options; |
|
| 109 | + $options = array(); |
|
| 110 | + $options['width'] = $width; |
|
| 111 | 111 | } |
| 112 | 112 | $this->options = array_merge($this->options, $options); |
| 113 | 113 | $this->settings = array_merge($this->settings, $options); |
| 114 | 114 | |
| 115 | 115 | $img = false; |
| 116 | 116 | if(is_string($name)){ |
| 117 | - $img = $this->_getImageByName(); |
|
| 117 | + $img = $this->_getImageByName(); |
|
| 118 | 118 | } |
| 119 | 119 | elseif(is_int($name)){ |
| 120 | - $img = $this->_getImageById(); |
|
| 120 | + $img = $this->_getImageById(); |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | if($img){ |
| 124 | - return $img; |
|
| 124 | + return $img; |
|
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | // $this->log("Unable to find $img"); |
| 128 | 128 | return false; |
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * input takes an array of options and display the file browser html input |
|
| 133 | - * options. |
|
| 134 | - * @param Array $options of model and file options. Defaults to default FileUpload component configuration |
|
| 135 | - * @return String HTML form input element configured for the FileUploadComponent |
|
| 136 | - * @access public |
|
| 137 | - */ |
|
| 138 | - function input($options = array()){ |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * input takes an array of options and display the file browser html input |
|
| 133 | + * options. |
|
| 134 | + * @param Array $options of model and file options. Defaults to default FileUpload component configuration |
|
| 135 | + * @return String HTML form input element configured for the FileUploadComponent |
|
| 136 | + * @access public |
|
| 137 | + */ |
|
| 138 | + function input($options = array()){ |
|
| 139 | 139 | $options = array_merge( |
| 140 | - array('var' => $this->settings['fileVar'],'model' => $this->settings['fileModel']), |
|
| 141 | - $options |
|
| 140 | + array('var' => $this->settings['fileVar'],'model' => $this->settings['fileModel']), |
|
| 141 | + $options |
|
| 142 | 142 | ); |
| 143 | 143 | $configs = $options; |
| 144 | 144 | if($configs['model']){ |
| 145 | - unset($options['model'], $options['var']); |
|
| 145 | + unset($options['model'], $options['var']); |
|
| 146 | 146 | |
| 147 | - return $this->Form->input("{$configs['model']}.".$this->inputCount++.".{$configs['var']}", array_merge(array('type'=>'file'), $options)); |
|
| 147 | + return $this->Form->input("{$configs['model']}.".$this->inputCount++.".{$configs['var']}", array_merge(array('type'=>'file'), $options)); |
|
| 148 | 148 | } |
| 149 | 149 | else { |
| 150 | - return "<input type='file' name='data[{$configs['var']}][".$this->inputCount++."]' />"; |
|
| 150 | + return "<input type='file' name='data[{$configs['var']}][".$this->inputCount++."]' />"; |
|
| 151 | + } |
|
| 151 | 152 | } |
| 152 | - } |
|
| 153 | 153 | |
| 154 | - /** |
|
| 155 | - * @access protected |
|
| 156 | - */ |
|
| 157 | - function _getImageById(){ |
|
| 154 | + /** |
|
| 155 | + * @access protected |
|
| 156 | + */ |
|
| 157 | + function _getImageById(){ |
|
| 158 | 158 | App::import('Component', 'FileUpload.FileUpload'); |
| 159 | 159 | $this->FileUpload = new FileUploadComponent; |
| 160 | 160 | |
@@ -164,121 +164,121 @@ discard block |
||
| 164 | 164 | $Model->recursive = -1; |
| 165 | 165 | $upload = $Model->findById($id); |
| 166 | 166 | if(!empty($upload)){ |
| 167 | - $this->fileName = $upload[$this->settings['fileModel']][$this->settings['fields']['name']]; |
|
| 168 | - return $this->_getImageByName(); |
|
| 167 | + $this->fileName = $upload[$this->settings['fileModel']][$this->settings['fields']['name']]; |
|
| 168 | + return $this->_getImageByName(); |
|
| 169 | 169 | } |
| 170 | 170 | else{ |
| 171 | - return false; |
|
| 171 | + return false; |
|
| 172 | + } |
|
| 172 | 173 | } |
| 173 | - } |
|
| 174 | 174 | |
| 175 | - /** |
|
| 176 | - * _getFullPath returns the full path of the file name |
|
| 177 | - * @access protected |
|
| 178 | - * @return String full path of the file name |
|
| 179 | - */ |
|
| 180 | - function _getFullPath(){ |
|
| 175 | + /** |
|
| 176 | + * _getFullPath returns the full path of the file name |
|
| 177 | + * @access protected |
|
| 178 | + * @return String full path of the file name |
|
| 179 | + */ |
|
| 180 | + function _getFullPath(){ |
|
| 181 | 181 | if($this->_isOutsideSource()){ |
| 182 | - return $this->fileName; |
|
| 182 | + return $this->fileName; |
|
| 183 | 183 | } |
| 184 | 184 | else { |
| 185 | - return WWW_ROOT . $this->_getUploadPath(); |
|
| 185 | + return WWW_ROOT . $this->_getUploadPath(); |
|
| 186 | + } |
|
| 186 | 187 | } |
| 187 | - } |
|
| 188 | 188 | |
| 189 | - /** |
|
| 190 | - * _getImagePath returns the image path of the file name |
|
| 191 | - * @access protected |
|
| 192 | - * @return String full path of the file name |
|
| 193 | - */ |
|
| 194 | - function _getImagePath(){ |
|
| 189 | + /** |
|
| 190 | + * _getImagePath returns the image path of the file name |
|
| 191 | + * @access protected |
|
| 192 | + * @return String full path of the file name |
|
| 193 | + */ |
|
| 194 | + function _getImagePath(){ |
|
| 195 | 195 | if($this->_isOutsideSource()){ |
| 196 | - return $this->fileName; |
|
| 196 | + return $this->fileName; |
|
| 197 | 197 | } |
| 198 | 198 | else { |
| 199 | - return '/' . $this->_getUploadPath(); |
|
| 199 | + return '/' . $this->_getUploadPath(); |
|
| 200 | + } |
|
| 200 | 201 | } |
| 201 | - } |
|
| 202 | 202 | |
| 203 | - /** |
|
| 204 | - * _getUploadPath returns the upload path of all files |
|
| 205 | - * @access protected |
|
| 206 | - * @return String upload path of all files |
|
| 207 | - */ |
|
| 208 | - function _getUploadPath(){ |
|
| 203 | + /** |
|
| 204 | + * _getUploadPath returns the upload path of all files |
|
| 205 | + * @access protected |
|
| 206 | + * @return String upload path of all files |
|
| 207 | + */ |
|
| 208 | + function _getUploadPath(){ |
|
| 209 | 209 | return $this->settings['uploadDir'] . '/' . $this->fileName; |
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * _getExt returns the extension of the filename. |
|
| 214 | - * @access protected |
|
| 215 | - * @return String extension of filename |
|
| 216 | - */ |
|
| 217 | - function _getExt(){ |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * _getExt returns the extension of the filename. |
|
| 214 | + * @access protected |
|
| 215 | + * @return String extension of filename |
|
| 216 | + */ |
|
| 217 | + function _getExt(){ |
|
| 218 | 218 | return strrchr($this->fileName,"."); |
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * Get the image by name and width. |
|
| 223 | - * if width is not specified return full image |
|
| 224 | - * if width is specified, see if width of image exists |
|
| 225 | - * if not, make it, save it, and return it. |
|
| 226 | - * @return String HTML of resized or full image. |
|
| 227 | - */ |
|
| 228 | - function _getImageByName(){ |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * Get the image by name and width. |
|
| 223 | + * if width is not specified return full image |
|
| 224 | + * if width is specified, see if width of image exists |
|
| 225 | + * if not, make it, save it, and return it. |
|
| 226 | + * @return String HTML of resized or full image. |
|
| 227 | + */ |
|
| 228 | + function _getImageByName(){ |
|
| 229 | 229 | //only proceed if we actually have the file in question |
| 230 | 230 | if(!$this->_isOutsideSource() && !file_exists($this->_getFullPath())) return false; |
| 231 | 231 | |
| 232 | 232 | //resize if we have resize on, a width, and if it doesn't already exist. |
| 233 | 233 | if($this->options['autoResize'] && $this->options['width'] > 0 && !file_exists($this->_getResizeNameOrPath($this->_getFullPath()))){ |
| 234 | - $this->_resizeImage(); |
|
| 234 | + $this->_resizeImage(); |
|
| 235 | 235 | } |
| 236 | 236 | return $this->_htmlImage(); |
| 237 | - } |
|
| 237 | + } |
|
| 238 | 238 | |
| 239 | - /** |
|
| 240 | - * @return String of the resizedpath of a filename or path. |
|
| 241 | - * @access protected |
|
| 242 | - */ |
|
| 243 | - function _getResizeNameOrPath($file_name_or_path){ |
|
| 239 | + /** |
|
| 240 | + * @return String of the resizedpath of a filename or path. |
|
| 241 | + * @access protected |
|
| 242 | + */ |
|
| 243 | + function _getResizeNameOrPath($file_name_or_path){ |
|
| 244 | 244 | $file_name = basename($file_name_or_path); |
| 245 | 245 | $path = substr($file_name_or_path, 0, strlen($file_name_or_path) - strlen($file_name)); |
| 246 | 246 | $temp_path = substr($file_name,0,strlen($file_name) - strlen($this->_getExt())) . "x" . $this->options['width'] . $this->_getExt(); |
| 247 | 247 | $full_path = (strlen($this->options['resizedDir']) > 0) ? $path . $this->options['resizedDir'] . '/' . $temp_path : $path . $temp_path; |
| 248 | 248 | return $full_path; |
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * _resizeImage actually resizes the passed in image. |
|
| 253 | - * @access protected |
|
| 254 | - * @return null |
|
| 255 | - */ |
|
| 256 | - function _resizeImage(){ |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * _resizeImage actually resizes the passed in image. |
|
| 253 | + * @access protected |
|
| 254 | + * @return null |
|
| 255 | + */ |
|
| 256 | + function _resizeImage(){ |
|
| 257 | 257 | $this->newImage = new RResizeImage($this->_getFullPath()); |
| 258 | 258 | if($this->newImage->imgWidth > $this->options['width']){ |
| 259 | - $this->newImage->resize_limitwh($this->options['width'], 0, $this->_getResizeNameOrPath($this->_getFullPath())); |
|
| 259 | + $this->newImage->resize_limitwh($this->options['width'], 0, $this->_getResizeNameOrPath($this->_getFullPath())); |
|
| 260 | 260 | } |
| 261 | 261 | else { |
| 262 | - //$this->autoResize = false; |
|
| 262 | + //$this->autoResize = false; |
|
| 263 | + } |
|
| 263 | 264 | } |
| 264 | - } |
|
| 265 | 265 | |
| 266 | - /** |
|
| 267 | - * _htmlImage returns the atual HTML of the resized/full image asked for |
|
| 268 | - * @access protected |
|
| 269 | - * @return String HTML image asked for |
|
| 270 | - */ |
|
| 271 | - function _htmlImage(){ |
|
| 266 | + /** |
|
| 267 | + * _htmlImage returns the atual HTML of the resized/full image asked for |
|
| 268 | + * @access protected |
|
| 269 | + * @return String HTML image asked for |
|
| 270 | + */ |
|
| 271 | + function _htmlImage(){ |
|
| 272 | 272 | if(!$this->_isOutsideSource() && $this->options['autoResize'] && $this->options['width'] > 0){ |
| 273 | - if(isset($this->newImage) && $this->newImage->imgWidth && $this->newImage->imgWidth <= $this->options['width']){ |
|
| 273 | + if(isset($this->newImage) && $this->newImage->imgWidth && $this->newImage->imgWidth <= $this->options['width']){ |
|
| 274 | 274 | $image = $this->_getImagePath(); |
| 275 | - } |
|
| 276 | - else { |
|
| 275 | + } |
|
| 276 | + else { |
|
| 277 | 277 | $image = $this->_getResizeNameOrPath($this->_getImagePath()); |
| 278 | - } |
|
| 278 | + } |
|
| 279 | 279 | } |
| 280 | 280 | else { |
| 281 | - $image = $this->_getImagePath(); |
|
| 281 | + $image = $this->_getImagePath(); |
|
| 282 | 282 | } |
| 283 | 283 | |
| 284 | 284 | $options = $this->options; //copy |
@@ -289,265 +289,265 @@ discard block |
||
| 289 | 289 | |
| 290 | 290 | //return the impage path or image html |
| 291 | 291 | if($this->options['imagePathOnly']){ |
| 292 | - return $image; |
|
| 292 | + return $image; |
|
| 293 | 293 | } |
| 294 | 294 | else { |
| 295 | - return $this->Html->image($image, $options); |
|
| 295 | + return $this->Html->image($image, $options); |
|
| 296 | + } |
|
| 296 | 297 | } |
| 297 | - } |
|
| 298 | 298 | |
| 299 | - /** |
|
| 300 | - * _isOutsideSource searches the fileName string for :// to determine if the image source is inside or outside our server |
|
| 301 | - */ |
|
| 302 | - function _isOutsideSource(){ |
|
| 299 | + /** |
|
| 300 | + * _isOutsideSource searches the fileName string for :// to determine if the image source is inside or outside our server |
|
| 301 | + */ |
|
| 302 | + function _isOutsideSource(){ |
|
| 303 | 303 | return !!strpos($this->fileName, '://'); |
| 304 | - } |
|
| 304 | + } |
|
| 305 | 305 | } |
| 306 | 306 | |
| 307 | 307 | |
| 308 | 308 | |
| 309 | 309 | |
| 310 | - /** |
|
| 311 | - * Image Resizer. |
|
| 312 | - * @author : Harish Chauhan |
|
| 313 | - * @copyright : Freeware |
|
| 314 | - * About :This PHP script will resize the given image and can show on the fly or save as image file. |
|
| 315 | - * |
|
| 316 | - */ |
|
| 317 | - //define("HAR_AUTO_NAME",1); |
|
| 318 | - Class RResizeImage { |
|
| 319 | - var $imgFile=""; |
|
| 320 | - var $imgWidth=0; |
|
| 321 | - var $imgHeight=0; |
|
| 322 | - var $imgType=""; |
|
| 323 | - var $imgAttr=""; |
|
| 324 | - var $type=NULL; |
|
| 325 | - var $_img=NULL; |
|
| 326 | - var $_error=""; |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Constructor |
|
| 330 | - * |
|
| 331 | - * @param [String $imgFile] Image File Name |
|
| 332 | - * @return RESIZEIMAGE (Class Object) |
|
| 333 | - */ |
|
| 334 | - function __construct($imgFile=""){ |
|
| 335 | - if (!function_exists("imagecreate")){ |
|
| 336 | - $this->_error="Error: GD Library is not available."; |
|
| 337 | - return false; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - $this->type=Array(1 => 'GIF', 2 => 'JPG', 3 => 'PNG', 4 => 'SWF', 5 => 'PSD', 6 => 'BMP', 7 => 'TIFF', 8 => 'TIFF', 9 => 'JPC', 10 => 'JP2', 11 => 'JPX', 12 => 'JB2', 13 => 'SWC', 14 => 'IFF', 15 => 'WBMP', 16 => 'XBM'); |
|
| 341 | - if(!empty($imgFile)){ |
|
| 342 | - $this->setImage($imgFile); |
|
| 343 | - } |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Error occured while resizing the image. |
|
| 348 | - * |
|
| 349 | - * @return String |
|
| 350 | - */ |
|
| 351 | - function error(){ |
|
| 352 | - return $this->_error; |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * Set image file name |
|
| 357 | - * |
|
| 358 | - * @param String $imgFile |
|
| 359 | - * @return void |
|
| 360 | - */ |
|
| 361 | - function setImage($imgFile){ |
|
| 362 | - $this->imgFile=$imgFile; |
|
| 363 | - return $this->_createImage(); |
|
| 364 | - } |
|
| 310 | + /** |
|
| 311 | + * Image Resizer. |
|
| 312 | + * @author : Harish Chauhan |
|
| 313 | + * @copyright : Freeware |
|
| 314 | + * About :This PHP script will resize the given image and can show on the fly or save as image file. |
|
| 315 | + * |
|
| 316 | + */ |
|
| 317 | + //define("HAR_AUTO_NAME",1); |
|
| 318 | + Class RResizeImage { |
|
| 319 | + var $imgFile=""; |
|
| 320 | + var $imgWidth=0; |
|
| 321 | + var $imgHeight=0; |
|
| 322 | + var $imgType=""; |
|
| 323 | + var $imgAttr=""; |
|
| 324 | + var $type=NULL; |
|
| 325 | + var $_img=NULL; |
|
| 326 | + var $_error=""; |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Constructor |
|
| 330 | + * |
|
| 331 | + * @param [String $imgFile] Image File Name |
|
| 332 | + * @return RESIZEIMAGE (Class Object) |
|
| 333 | + */ |
|
| 334 | + function __construct($imgFile=""){ |
|
| 335 | + if (!function_exists("imagecreate")){ |
|
| 336 | + $this->_error="Error: GD Library is not available."; |
|
| 337 | + return false; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + $this->type=Array(1 => 'GIF', 2 => 'JPG', 3 => 'PNG', 4 => 'SWF', 5 => 'PSD', 6 => 'BMP', 7 => 'TIFF', 8 => 'TIFF', 9 => 'JPC', 10 => 'JP2', 11 => 'JPX', 12 => 'JB2', 13 => 'SWC', 14 => 'IFF', 15 => 'WBMP', 16 => 'XBM'); |
|
| 341 | + if(!empty($imgFile)){ |
|
| 342 | + $this->setImage($imgFile); |
|
| 343 | + } |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Error occured while resizing the image. |
|
| 348 | + * |
|
| 349 | + * @return String |
|
| 350 | + */ |
|
| 351 | + function error(){ |
|
| 352 | + return $this->_error; |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * Set image file name |
|
| 357 | + * |
|
| 358 | + * @param String $imgFile |
|
| 359 | + * @return void |
|
| 360 | + */ |
|
| 361 | + function setImage($imgFile){ |
|
| 362 | + $this->imgFile=$imgFile; |
|
| 363 | + return $this->_createImage(); |
|
| 364 | + } |
|
| 365 | 365 | |
| 366 | 366 | /** |
| 367 | - * @return void |
|
| 368 | - */ |
|
| 369 | - function close(){ |
|
| 370 | - return @imagedestroy($this->_img); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * Resize a image to given width and height and keep it's current width and height ratio |
|
| 375 | - * |
|
| 376 | - * @param Number $imgwidth |
|
| 377 | - * @param Numnber $imgheight |
|
| 378 | - * @param String $newfile |
|
| 379 | - */ |
|
| 380 | - function resize_limitwh($imgwidth,$imgheight,$newfile=NULL){ |
|
| 381 | - $image_per = 100; |
|
| 382 | - list($width, $height, $type, $attr) = @getimagesize($this->imgFile); |
|
| 383 | - if($width > $imgwidth && $imgwidth > 0){ |
|
| 384 | - $image_per = (double)(($imgwidth * 100) / $width); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - if(floor(($height * $image_per)/100)>$imgheight && $imgheight > 0){ |
|
| 388 | - $image_per = (double)(($imgheight * 100) / $height); |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - $this->resize_percentage($image_per,$newfile); |
|
| 392 | - } |
|
| 367 | + * @return void |
|
| 368 | + */ |
|
| 369 | + function close(){ |
|
| 370 | + return @imagedestroy($this->_img); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * Resize a image to given width and height and keep it's current width and height ratio |
|
| 375 | + * |
|
| 376 | + * @param Number $imgwidth |
|
| 377 | + * @param Numnber $imgheight |
|
| 378 | + * @param String $newfile |
|
| 379 | + */ |
|
| 380 | + function resize_limitwh($imgwidth,$imgheight,$newfile=NULL){ |
|
| 381 | + $image_per = 100; |
|
| 382 | + list($width, $height, $type, $attr) = @getimagesize($this->imgFile); |
|
| 383 | + if($width > $imgwidth && $imgwidth > 0){ |
|
| 384 | + $image_per = (double)(($imgwidth * 100) / $width); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + if(floor(($height * $image_per)/100)>$imgheight && $imgheight > 0){ |
|
| 388 | + $image_per = (double)(($imgheight * 100) / $height); |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + $this->resize_percentage($image_per,$newfile); |
|
| 392 | + } |
|
| 393 | 393 | |
| 394 | 394 | /** |
| 395 | - * Resize an image to given percentage. |
|
| 396 | - * |
|
| 397 | - * @param Number $percent |
|
| 398 | - * @param String $newfile |
|
| 399 | - * @return Boolean |
|
| 400 | - */ |
|
| 401 | - function resize_percentage($percent=100,$newfile=NULL) { |
|
| 402 | - $newWidth=($this->imgWidth*$percent)/100; |
|
| 403 | - $newHeight=($this->imgHeight*$percent)/100; |
|
| 404 | - return $this->resize($newWidth,$newHeight,$newfile); |
|
| 405 | - } |
|
| 395 | + * Resize an image to given percentage. |
|
| 396 | + * |
|
| 397 | + * @param Number $percent |
|
| 398 | + * @param String $newfile |
|
| 399 | + * @return Boolean |
|
| 400 | + */ |
|
| 401 | + function resize_percentage($percent=100,$newfile=NULL) { |
|
| 402 | + $newWidth=($this->imgWidth*$percent)/100; |
|
| 403 | + $newHeight=($this->imgHeight*$percent)/100; |
|
| 404 | + return $this->resize($newWidth,$newHeight,$newfile); |
|
| 405 | + } |
|
| 406 | 406 | |
| 407 | 407 | /** |
| 408 | - * Resize an image to given X and Y percentage. |
|
| 409 | - * |
|
| 410 | - * @param Number $xpercent |
|
| 411 | - * @param Number $ypercent |
|
| 412 | - * @param String $newfile |
|
| 413 | - * @return Boolean |
|
| 414 | - */ |
|
| 415 | - function resize_xypercentage($xpercent=100,$ypercent=100,$newfile=NULL) { |
|
| 416 | - $newWidth=($this->imgWidth*$xpercent)/100; |
|
| 417 | - $newHeight=($this->imgHeight*$ypercent)/100; |
|
| 418 | - return $this->resize($newWidth,$newHeight,$newfile); |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - /** |
|
| 422 | - * Resize an image to given width and height |
|
| 423 | - * |
|
| 424 | - * @param Number $width |
|
| 425 | - * @param Number $height |
|
| 426 | - * @param String $newfile |
|
| 427 | - * @return Boolean |
|
| 428 | - */ |
|
| 429 | - function resize($width,$height,$newfile=NULL){ |
|
| 430 | - if(empty($this->imgFile)){ |
|
| 431 | - $this->_error="File name is not initialised."; |
|
| 432 | - return false; |
|
| 433 | - } |
|
| 434 | - if($this->imgWidth<=0 || $this->imgHeight<=0){ |
|
| 435 | - $this->_error="Could not resize given image"; |
|
| 436 | - return false; |
|
| 437 | - } |
|
| 438 | - if($width<=0) $width=$this->imgWidth; |
|
| 439 | - if($height<=0) $height=$this->imgHeight; |
|
| 440 | - |
|
| 441 | - return $this->_resize($width,$height,$newfile); |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - /** |
|
| 445 | - * Get the image attributes |
|
| 446 | - * @access Private |
|
| 447 | - * |
|
| 448 | - */ |
|
| 449 | - function _getImageInfo() |
|
| 450 | - { |
|
| 451 | - @list($this->imgWidth,$this->imgHeight,$type,$this->imgAttr)=@getimagesize($this->imgFile); |
|
| 452 | - $this->imgType=$this->type[$type]; |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Create the image resource |
|
| 457 | - * @access Private |
|
| 458 | - * @return Boolean |
|
| 459 | - */ |
|
| 460 | - function _createImage(){ |
|
| 461 | - $this->_getImageInfo(); |
|
| 462 | - if($this->imgType=='GIF'){ |
|
| 463 | - $this->_img=@imagecreatefromgif($this->imgFile); |
|
| 464 | - } |
|
| 465 | - elseif($this->imgType=='JPG'){ |
|
| 466 | - $this->_img=@imagecreatefromjpeg($this->imgFile); |
|
| 467 | - } |
|
| 468 | - elseif($this->imgType=='PNG'){ |
|
| 469 | - $this->_img=@imagecreatefrompng($this->imgFile); |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - if(!$this->_img || !@is_resource($this->_img)){ |
|
| 473 | - $this->_error="Error loading ".$this->imgFile; |
|
| 474 | - return false; |
|
| 475 | - } |
|
| 476 | - return true; |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - /** |
|
| 480 | - * Function is used to resize the image |
|
| 481 | - * |
|
| 482 | - * @access Private |
|
| 483 | - * @param Number $width |
|
| 484 | - * @param Number $height |
|
| 485 | - * @param String $newfile |
|
| 486 | - * @return Boolean |
|
| 487 | - */ |
|
| 488 | - function _resize($width,$height,$newfile=NULL){ |
|
| 489 | - if (!function_exists("imagecreate")){ |
|
| 490 | - $this->_error="Error: GD Library is not available."; |
|
| 491 | - return false; |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - $newimg=@imagecreatetruecolor($width,$height); |
|
| 495 | - //imagecolortransparent( $newimg, imagecolorat( $newimg, 0, 0 ) ); |
|
| 496 | - |
|
| 497 | - if($this->imgType=='GIF' || $this->imgType=='PNG') { |
|
| 498 | - /** Code to keep transparency of image **/ |
|
| 499 | - $colorcount = imagecolorstotal($this->_img); |
|
| 500 | - if ($colorcount == 0) $colorcount = 256; |
|
| 501 | - imagetruecolortopalette($newimg,true,$colorcount); |
|
| 502 | - imagepalettecopy($newimg,$this->_img); |
|
| 503 | - $transparentcolor = imagecolortransparent($this->_img); |
|
| 504 | - imagefill($newimg,0,0,$transparentcolor); |
|
| 505 | - imagecolortransparent($newimg,$transparentcolor); |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - @imagecopyresampled ( $newimg, $this->_img, 0,0,0,0, $width, $height, $this->imgWidth,$this->imgHeight); |
|
| 509 | - |
|
| 510 | - if($newfile===1) { |
|
| 511 | - if(@preg_match("/\..*+$/",@basename($this->imgFile),$matches)){ |
|
| 512 | - $newfile=@substr_replace($this->imgFile,"_har",-@strlen($matches[0]),0); |
|
| 408 | + * Resize an image to given X and Y percentage. |
|
| 409 | + * |
|
| 410 | + * @param Number $xpercent |
|
| 411 | + * @param Number $ypercent |
|
| 412 | + * @param String $newfile |
|
| 413 | + * @return Boolean |
|
| 414 | + */ |
|
| 415 | + function resize_xypercentage($xpercent=100,$ypercent=100,$newfile=NULL) { |
|
| 416 | + $newWidth=($this->imgWidth*$xpercent)/100; |
|
| 417 | + $newHeight=($this->imgHeight*$ypercent)/100; |
|
| 418 | + return $this->resize($newWidth,$newHeight,$newfile); |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + /** |
|
| 422 | + * Resize an image to given width and height |
|
| 423 | + * |
|
| 424 | + * @param Number $width |
|
| 425 | + * @param Number $height |
|
| 426 | + * @param String $newfile |
|
| 427 | + * @return Boolean |
|
| 428 | + */ |
|
| 429 | + function resize($width,$height,$newfile=NULL){ |
|
| 430 | + if(empty($this->imgFile)){ |
|
| 431 | + $this->_error="File name is not initialised."; |
|
| 432 | + return false; |
|
| 433 | + } |
|
| 434 | + if($this->imgWidth<=0 || $this->imgHeight<=0){ |
|
| 435 | + $this->_error="Could not resize given image"; |
|
| 436 | + return false; |
|
| 437 | + } |
|
| 438 | + if($width<=0) $width=$this->imgWidth; |
|
| 439 | + if($height<=0) $height=$this->imgHeight; |
|
| 440 | + |
|
| 441 | + return $this->_resize($width,$height,$newfile); |
|
| 513 | 442 | } |
| 514 | - } |
|
| 515 | - elseif(!empty($newfile)){ |
|
| 516 | - if(!@preg_match("/\..*+$/",@basename($newfile))){ |
|
| 517 | - if(@preg_match("/\..*+$/",@basename($this->imgFile),$matches)){ |
|
| 518 | - $newfile=$newfile.$matches[0]; |
|
| 519 | - } |
|
| 520 | - } |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - if($this->imgType=='GIF'){ |
|
| 524 | - if(!empty($newfile)){ |
|
| 525 | - @imagegif($newimg,$newfile); |
|
| 443 | + |
|
| 444 | + /** |
|
| 445 | + * Get the image attributes |
|
| 446 | + * @access Private |
|
| 447 | + * |
|
| 448 | + */ |
|
| 449 | + function _getImageInfo() |
|
| 450 | + { |
|
| 451 | + @list($this->imgWidth,$this->imgHeight,$type,$this->imgAttr)=@getimagesize($this->imgFile); |
|
| 452 | + $this->imgType=$this->type[$type]; |
|
| 526 | 453 | } |
| 527 | - else { |
|
| 528 | - @header("Content-type: image/gif"); |
|
| 529 | - @imagegif($newimg); |
|
| 530 | - } |
|
| 531 | - } |
|
| 532 | - elseif($this->imgType=='JPG'){ |
|
| 533 | - if(!empty($newfile)){ |
|
| 534 | - @imagejpeg($newimg,$newfile); |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Create the image resource |
|
| 457 | + * @access Private |
|
| 458 | + * @return Boolean |
|
| 459 | + */ |
|
| 460 | + function _createImage(){ |
|
| 461 | + $this->_getImageInfo(); |
|
| 462 | + if($this->imgType=='GIF'){ |
|
| 463 | + $this->_img=@imagecreatefromgif($this->imgFile); |
|
| 464 | + } |
|
| 465 | + elseif($this->imgType=='JPG'){ |
|
| 466 | + $this->_img=@imagecreatefromjpeg($this->imgFile); |
|
| 467 | + } |
|
| 468 | + elseif($this->imgType=='PNG'){ |
|
| 469 | + $this->_img=@imagecreatefrompng($this->imgFile); |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + if(!$this->_img || !@is_resource($this->_img)){ |
|
| 473 | + $this->_error="Error loading ".$this->imgFile; |
|
| 474 | + return false; |
|
| 475 | + } |
|
| 476 | + return true; |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + /** |
|
| 480 | + * Function is used to resize the image |
|
| 481 | + * |
|
| 482 | + * @access Private |
|
| 483 | + * @param Number $width |
|
| 484 | + * @param Number $height |
|
| 485 | + * @param String $newfile |
|
| 486 | + * @return Boolean |
|
| 487 | + */ |
|
| 488 | + function _resize($width,$height,$newfile=NULL){ |
|
| 489 | + if (!function_exists("imagecreate")){ |
|
| 490 | + $this->_error="Error: GD Library is not available."; |
|
| 491 | + return false; |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + $newimg=@imagecreatetruecolor($width,$height); |
|
| 495 | + //imagecolortransparent( $newimg, imagecolorat( $newimg, 0, 0 ) ); |
|
| 496 | + |
|
| 497 | + if($this->imgType=='GIF' || $this->imgType=='PNG') { |
|
| 498 | + /** Code to keep transparency of image **/ |
|
| 499 | + $colorcount = imagecolorstotal($this->_img); |
|
| 500 | + if ($colorcount == 0) $colorcount = 256; |
|
| 501 | + imagetruecolortopalette($newimg,true,$colorcount); |
|
| 502 | + imagepalettecopy($newimg,$this->_img); |
|
| 503 | + $transparentcolor = imagecolortransparent($this->_img); |
|
| 504 | + imagefill($newimg,0,0,$transparentcolor); |
|
| 505 | + imagecolortransparent($newimg,$transparentcolor); |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + @imagecopyresampled ( $newimg, $this->_img, 0,0,0,0, $width, $height, $this->imgWidth,$this->imgHeight); |
|
| 509 | + |
|
| 510 | + if($newfile===1) { |
|
| 511 | + if(@preg_match("/\..*+$/",@basename($this->imgFile),$matches)){ |
|
| 512 | + $newfile=@substr_replace($this->imgFile,"_har",-@strlen($matches[0]),0); |
|
| 535 | 513 | } |
| 536 | - else { |
|
| 537 | - @header("Content-type: image/jpeg"); |
|
| 538 | - @imagejpeg($newimg); |
|
| 539 | - } |
|
| 540 | - } |
|
| 541 | - elseif($this->imgType=='PNG'){ |
|
| 542 | - if(!empty($newfile)){ |
|
| 543 | - @imagepng($newimg,$newfile); |
|
| 514 | + } |
|
| 515 | + elseif(!empty($newfile)){ |
|
| 516 | + if(!@preg_match("/\..*+$/",@basename($newfile))){ |
|
| 517 | + if(@preg_match("/\..*+$/",@basename($this->imgFile),$matches)){ |
|
| 518 | + $newfile=$newfile.$matches[0]; |
|
| 519 | + } |
|
| 520 | + } |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + if($this->imgType=='GIF'){ |
|
| 524 | + if(!empty($newfile)){ |
|
| 525 | + @imagegif($newimg,$newfile); |
|
| 544 | 526 | } |
| 545 | - else{ |
|
| 546 | - @header("Content-type: image/png"); |
|
| 547 | - @imagepng($newimg); |
|
| 548 | - } |
|
| 549 | - } |
|
| 550 | - @imagedestroy($newimg); |
|
| 551 | - } |
|
| 552 | - } |
|
| 527 | + else { |
|
| 528 | + @header("Content-type: image/gif"); |
|
| 529 | + @imagegif($newimg); |
|
| 530 | + } |
|
| 531 | + } |
|
| 532 | + elseif($this->imgType=='JPG'){ |
|
| 533 | + if(!empty($newfile)){ |
|
| 534 | + @imagejpeg($newimg,$newfile); |
|
| 535 | + } |
|
| 536 | + else { |
|
| 537 | + @header("Content-type: image/jpeg"); |
|
| 538 | + @imagejpeg($newimg); |
|
| 539 | + } |
|
| 540 | + } |
|
| 541 | + elseif($this->imgType=='PNG'){ |
|
| 542 | + if(!empty($newfile)){ |
|
| 543 | + @imagepng($newimg,$newfile); |
|
| 544 | + } |
|
| 545 | + else{ |
|
| 546 | + @header("Content-type: image/png"); |
|
| 547 | + @imagepng($newimg); |
|
| 548 | + } |
|
| 549 | + } |
|
| 550 | + @imagedestroy($newimg); |
|
| 551 | + } |
|
| 552 | + } |
|
| 553 | 553 | ?> |
| 554 | 554 | \ No newline at end of file |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | * |
| 21 | 21 | */ |
| 22 | 22 | App::import('Config', 'FileUpload.file_upload_settings'); |
| 23 | -class FileUploadHelper extends AppHelper{ |
|
| 23 | +class FileUploadHelper extends AppHelper { |
|
| 24 | 24 | var $helpers = array('Html', 'Form'); |
| 25 | 25 | |
| 26 | 26 | /** |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | * Constructor, initiallizes the FileUpload Component |
| 65 | 65 | * and sets the default options. |
| 66 | 66 | */ |
| 67 | - function __construct(View $View, $settings = array()){ |
|
| 67 | + function __construct(View $View, $settings = array()) { |
|
| 68 | 68 | parent::__construct($View, $settings); |
| 69 | 69 | $this->FileUploadSettings = new FileUploadSettings; |
| 70 | 70 | |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | * @access public |
| 78 | 78 | * @return void |
| 79 | 79 | */ |
| 80 | - function reset(){ |
|
| 80 | + function reset() { |
|
| 81 | 81 | $this->fileName = null; |
| 82 | 82 | $this->options = array( |
| 83 | 83 | 'width' => 0, |
@@ -101,10 +101,10 @@ discard block |
||
| 101 | 101 | * @access public |
| 102 | 102 | * @return mixed html tag, url string, or false if unable to find image. |
| 103 | 103 | */ |
| 104 | - function image($name, $options = array()){ |
|
| 104 | + function image($name, $options = array()) { |
|
| 105 | 105 | $this->fileName = $name; |
| 106 | 106 | //options takes in a width as well |
| 107 | - if(is_int($options)){ |
|
| 107 | + if (is_int($options)) { |
|
| 108 | 108 | $width = $options; |
| 109 | 109 | $options = array(); |
| 110 | 110 | $options['width'] = $width; |
@@ -113,14 +113,14 @@ discard block |
||
| 113 | 113 | $this->settings = array_merge($this->settings, $options); |
| 114 | 114 | |
| 115 | 115 | $img = false; |
| 116 | - if(is_string($name)){ |
|
| 116 | + if (is_string($name)) { |
|
| 117 | 117 | $img = $this->_getImageByName(); |
| 118 | 118 | } |
| 119 | - elseif(is_int($name)){ |
|
| 119 | + elseif (is_int($name)) { |
|
| 120 | 120 | $img = $this->_getImageById(); |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | - if($img){ |
|
| 123 | + if ($img) { |
|
| 124 | 124 | return $img; |
| 125 | 125 | } |
| 126 | 126 | |
@@ -135,13 +135,13 @@ discard block |
||
| 135 | 135 | * @return String HTML form input element configured for the FileUploadComponent |
| 136 | 136 | * @access public |
| 137 | 137 | */ |
| 138 | - function input($options = array()){ |
|
| 138 | + function input($options = array()) { |
|
| 139 | 139 | $options = array_merge( |
| 140 | - array('var' => $this->settings['fileVar'],'model' => $this->settings['fileModel']), |
|
| 140 | + array('var' => $this->settings['fileVar'], 'model' => $this->settings['fileModel']), |
|
| 141 | 141 | $options |
| 142 | 142 | ); |
| 143 | 143 | $configs = $options; |
| 144 | - if($configs['model']){ |
|
| 144 | + if ($configs['model']) { |
|
| 145 | 145 | unset($options['model'], $options['var']); |
| 146 | 146 | |
| 147 | 147 | return $this->Form->input("{$configs['model']}.".$this->inputCount++.".{$configs['var']}", array_merge(array('type'=>'file'), $options)); |
@@ -154,20 +154,20 @@ discard block |
||
| 154 | 154 | /** |
| 155 | 155 | * @access protected |
| 156 | 156 | */ |
| 157 | - function _getImageById(){ |
|
| 157 | + function _getImageById() { |
|
| 158 | 158 | App::import('Component', 'FileUpload.FileUpload'); |
| 159 | 159 | $this->FileUpload = new FileUploadComponent; |
| 160 | 160 | |
| 161 | 161 | $id = $this->fileName; |
| 162 | 162 | $this->FileUpload->options['fileModel'] = $this->settings['fileModel']; |
| 163 | - $Model =& $this->FileUpload->getModel(); |
|
| 163 | + $Model = & $this->FileUpload->getModel(); |
|
| 164 | 164 | $Model->recursive = -1; |
| 165 | 165 | $upload = $Model->findById($id); |
| 166 | - if(!empty($upload)){ |
|
| 166 | + if (!empty($upload)) { |
|
| 167 | 167 | $this->fileName = $upload[$this->settings['fileModel']][$this->settings['fields']['name']]; |
| 168 | 168 | return $this->_getImageByName(); |
| 169 | 169 | } |
| 170 | - else{ |
|
| 170 | + else { |
|
| 171 | 171 | return false; |
| 172 | 172 | } |
| 173 | 173 | } |
@@ -177,12 +177,12 @@ discard block |
||
| 177 | 177 | * @access protected |
| 178 | 178 | * @return String full path of the file name |
| 179 | 179 | */ |
| 180 | - function _getFullPath(){ |
|
| 181 | - if($this->_isOutsideSource()){ |
|
| 180 | + function _getFullPath() { |
|
| 181 | + if ($this->_isOutsideSource()) { |
|
| 182 | 182 | return $this->fileName; |
| 183 | 183 | } |
| 184 | 184 | else { |
| 185 | - return WWW_ROOT . $this->_getUploadPath(); |
|
| 185 | + return WWW_ROOT.$this->_getUploadPath(); |
|
| 186 | 186 | } |
| 187 | 187 | } |
| 188 | 188 | |
@@ -191,12 +191,12 @@ discard block |
||
| 191 | 191 | * @access protected |
| 192 | 192 | * @return String full path of the file name |
| 193 | 193 | */ |
| 194 | - function _getImagePath(){ |
|
| 195 | - if($this->_isOutsideSource()){ |
|
| 194 | + function _getImagePath() { |
|
| 195 | + if ($this->_isOutsideSource()) { |
|
| 196 | 196 | return $this->fileName; |
| 197 | 197 | } |
| 198 | 198 | else { |
| 199 | - return '/' . $this->_getUploadPath(); |
|
| 199 | + return '/'.$this->_getUploadPath(); |
|
| 200 | 200 | } |
| 201 | 201 | } |
| 202 | 202 | |
@@ -205,8 +205,8 @@ discard block |
||
| 205 | 205 | * @access protected |
| 206 | 206 | * @return String upload path of all files |
| 207 | 207 | */ |
| 208 | - function _getUploadPath(){ |
|
| 209 | - return $this->settings['uploadDir'] . '/' . $this->fileName; |
|
| 208 | + function _getUploadPath() { |
|
| 209 | + return $this->settings['uploadDir'].'/'.$this->fileName; |
|
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | /** |
@@ -214,8 +214,8 @@ discard block |
||
| 214 | 214 | * @access protected |
| 215 | 215 | * @return String extension of filename |
| 216 | 216 | */ |
| 217 | - function _getExt(){ |
|
| 218 | - return strrchr($this->fileName,"."); |
|
| 217 | + function _getExt() { |
|
| 218 | + return strrchr($this->fileName, "."); |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | /** |
@@ -225,12 +225,12 @@ discard block |
||
| 225 | 225 | * if not, make it, save it, and return it. |
| 226 | 226 | * @return String HTML of resized or full image. |
| 227 | 227 | */ |
| 228 | - function _getImageByName(){ |
|
| 228 | + function _getImageByName() { |
|
| 229 | 229 | //only proceed if we actually have the file in question |
| 230 | - if(!$this->_isOutsideSource() && !file_exists($this->_getFullPath())) return false; |
|
| 230 | + if (!$this->_isOutsideSource() && !file_exists($this->_getFullPath())) return false; |
|
| 231 | 231 | |
| 232 | 232 | //resize if we have resize on, a width, and if it doesn't already exist. |
| 233 | - if($this->options['autoResize'] && $this->options['width'] > 0 && !file_exists($this->_getResizeNameOrPath($this->_getFullPath()))){ |
|
| 233 | + if ($this->options['autoResize'] && $this->options['width'] > 0 && !file_exists($this->_getResizeNameOrPath($this->_getFullPath()))) { |
|
| 234 | 234 | $this->_resizeImage(); |
| 235 | 235 | } |
| 236 | 236 | return $this->_htmlImage(); |
@@ -240,11 +240,11 @@ discard block |
||
| 240 | 240 | * @return String of the resizedpath of a filename or path. |
| 241 | 241 | * @access protected |
| 242 | 242 | */ |
| 243 | - function _getResizeNameOrPath($file_name_or_path){ |
|
| 243 | + function _getResizeNameOrPath($file_name_or_path) { |
|
| 244 | 244 | $file_name = basename($file_name_or_path); |
| 245 | 245 | $path = substr($file_name_or_path, 0, strlen($file_name_or_path) - strlen($file_name)); |
| 246 | - $temp_path = substr($file_name,0,strlen($file_name) - strlen($this->_getExt())) . "x" . $this->options['width'] . $this->_getExt(); |
|
| 247 | - $full_path = (strlen($this->options['resizedDir']) > 0) ? $path . $this->options['resizedDir'] . '/' . $temp_path : $path . $temp_path; |
|
| 246 | + $temp_path = substr($file_name, 0, strlen($file_name) - strlen($this->_getExt()))."x".$this->options['width'].$this->_getExt(); |
|
| 247 | + $full_path = (strlen($this->options['resizedDir']) > 0) ? $path.$this->options['resizedDir'].'/'.$temp_path : $path.$temp_path; |
|
| 248 | 248 | return $full_path; |
| 249 | 249 | } |
| 250 | 250 | |
@@ -253,9 +253,9 @@ discard block |
||
| 253 | 253 | * @access protected |
| 254 | 254 | * @return null |
| 255 | 255 | */ |
| 256 | - function _resizeImage(){ |
|
| 256 | + function _resizeImage() { |
|
| 257 | 257 | $this->newImage = new RResizeImage($this->_getFullPath()); |
| 258 | - if($this->newImage->imgWidth > $this->options['width']){ |
|
| 258 | + if ($this->newImage->imgWidth > $this->options['width']) { |
|
| 259 | 259 | $this->newImage->resize_limitwh($this->options['width'], 0, $this->_getResizeNameOrPath($this->_getFullPath())); |
| 260 | 260 | } |
| 261 | 261 | else { |
@@ -268,9 +268,9 @@ discard block |
||
| 268 | 268 | * @access protected |
| 269 | 269 | * @return String HTML image asked for |
| 270 | 270 | */ |
| 271 | - function _htmlImage(){ |
|
| 272 | - if(!$this->_isOutsideSource() && $this->options['autoResize'] && $this->options['width'] > 0){ |
|
| 273 | - if(isset($this->newImage) && $this->newImage->imgWidth && $this->newImage->imgWidth <= $this->options['width']){ |
|
| 271 | + function _htmlImage() { |
|
| 272 | + if (!$this->_isOutsideSource() && $this->options['autoResize'] && $this->options['width'] > 0) { |
|
| 273 | + if (isset($this->newImage) && $this->newImage->imgWidth && $this->newImage->imgWidth <= $this->options['width']) { |
|
| 274 | 274 | $image = $this->_getImagePath(); |
| 275 | 275 | } |
| 276 | 276 | else { |
@@ -285,10 +285,10 @@ discard block |
||
| 285 | 285 | //unset the default options |
| 286 | 286 | unset($options['resizedDir'], $options['uploadDir'], $options['imagePathOnly'], $options['autoResize'], $options['resizeThumbOnly']); |
| 287 | 287 | //unset width only if we're not an outsourced image, we have resize turned on, or we don't have a width to begin with. |
| 288 | - if(!$this->_isOutsideSource() && ($this->options['resizeThumbOnly'] || !$options['width'])) unset($options['width']); |
|
| 288 | + if (!$this->_isOutsideSource() && ($this->options['resizeThumbOnly'] || !$options['width'])) unset($options['width']); |
|
| 289 | 289 | |
| 290 | 290 | //return the impage path or image html |
| 291 | - if($this->options['imagePathOnly']){ |
|
| 291 | + if ($this->options['imagePathOnly']) { |
|
| 292 | 292 | return $image; |
| 293 | 293 | } |
| 294 | 294 | else { |
@@ -299,7 +299,7 @@ discard block |
||
| 299 | 299 | /** |
| 300 | 300 | * _isOutsideSource searches the fileName string for :// to determine if the image source is inside or outside our server |
| 301 | 301 | */ |
| 302 | - function _isOutsideSource(){ |
|
| 302 | + function _isOutsideSource() { |
|
| 303 | 303 | return !!strpos($this->fileName, '://'); |
| 304 | 304 | } |
| 305 | 305 | } |
@@ -315,15 +315,15 @@ discard block |
||
| 315 | 315 | * |
| 316 | 316 | */ |
| 317 | 317 | //define("HAR_AUTO_NAME",1); |
| 318 | - Class RResizeImage { |
|
| 319 | - var $imgFile=""; |
|
| 320 | - var $imgWidth=0; |
|
| 321 | - var $imgHeight=0; |
|
| 322 | - var $imgType=""; |
|
| 323 | - var $imgAttr=""; |
|
| 324 | - var $type=NULL; |
|
| 325 | - var $_img=NULL; |
|
| 326 | - var $_error=""; |
|
| 318 | + Class RResizeImage { |
|
| 319 | + var $imgFile = ""; |
|
| 320 | + var $imgWidth = 0; |
|
| 321 | + var $imgHeight = 0; |
|
| 322 | + var $imgType = ""; |
|
| 323 | + var $imgAttr = ""; |
|
| 324 | + var $type = NULL; |
|
| 325 | + var $_img = NULL; |
|
| 326 | + var $_error = ""; |
|
| 327 | 327 | |
| 328 | 328 | /** |
| 329 | 329 | * Constructor |
@@ -331,14 +331,14 @@ discard block |
||
| 331 | 331 | * @param [String $imgFile] Image File Name |
| 332 | 332 | * @return RESIZEIMAGE (Class Object) |
| 333 | 333 | */ |
| 334 | - function __construct($imgFile=""){ |
|
| 335 | - if (!function_exists("imagecreate")){ |
|
| 336 | - $this->_error="Error: GD Library is not available."; |
|
| 334 | + function __construct($imgFile = "") { |
|
| 335 | + if (!function_exists("imagecreate")) { |
|
| 336 | + $this->_error = "Error: GD Library is not available."; |
|
| 337 | 337 | return false; |
| 338 | 338 | } |
| 339 | 339 | |
| 340 | - $this->type=Array(1 => 'GIF', 2 => 'JPG', 3 => 'PNG', 4 => 'SWF', 5 => 'PSD', 6 => 'BMP', 7 => 'TIFF', 8 => 'TIFF', 9 => 'JPC', 10 => 'JP2', 11 => 'JPX', 12 => 'JB2', 13 => 'SWC', 14 => 'IFF', 15 => 'WBMP', 16 => 'XBM'); |
|
| 341 | - if(!empty($imgFile)){ |
|
| 340 | + $this->type = Array(1 => 'GIF', 2 => 'JPG', 3 => 'PNG', 4 => 'SWF', 5 => 'PSD', 6 => 'BMP', 7 => 'TIFF', 8 => 'TIFF', 9 => 'JPC', 10 => 'JP2', 11 => 'JPX', 12 => 'JB2', 13 => 'SWC', 14 => 'IFF', 15 => 'WBMP', 16 => 'XBM'); |
|
| 341 | + if (!empty($imgFile)) { |
|
| 342 | 342 | $this->setImage($imgFile); |
| 343 | 343 | } |
| 344 | 344 | } |
@@ -348,7 +348,7 @@ discard block |
||
| 348 | 348 | * |
| 349 | 349 | * @return String |
| 350 | 350 | */ |
| 351 | - function error(){ |
|
| 351 | + function error() { |
|
| 352 | 352 | return $this->_error; |
| 353 | 353 | } |
| 354 | 354 | |
@@ -358,15 +358,15 @@ discard block |
||
| 358 | 358 | * @param String $imgFile |
| 359 | 359 | * @return void |
| 360 | 360 | */ |
| 361 | - function setImage($imgFile){ |
|
| 362 | - $this->imgFile=$imgFile; |
|
| 361 | + function setImage($imgFile) { |
|
| 362 | + $this->imgFile = $imgFile; |
|
| 363 | 363 | return $this->_createImage(); |
| 364 | 364 | } |
| 365 | 365 | |
| 366 | 366 | /** |
| 367 | 367 | * @return void |
| 368 | 368 | */ |
| 369 | - function close(){ |
|
| 369 | + function close() { |
|
| 370 | 370 | return @imagedestroy($this->_img); |
| 371 | 371 | } |
| 372 | 372 | |
@@ -377,18 +377,18 @@ discard block |
||
| 377 | 377 | * @param Numnber $imgheight |
| 378 | 378 | * @param String $newfile |
| 379 | 379 | */ |
| 380 | - function resize_limitwh($imgwidth,$imgheight,$newfile=NULL){ |
|
| 380 | + function resize_limitwh($imgwidth, $imgheight, $newfile = NULL) { |
|
| 381 | 381 | $image_per = 100; |
| 382 | 382 | list($width, $height, $type, $attr) = @getimagesize($this->imgFile); |
| 383 | - if($width > $imgwidth && $imgwidth > 0){ |
|
| 384 | - $image_per = (double)(($imgwidth * 100) / $width); |
|
| 383 | + if ($width > $imgwidth && $imgwidth > 0) { |
|
| 384 | + $image_per = (double) (($imgwidth * 100) / $width); |
|
| 385 | 385 | } |
| 386 | 386 | |
| 387 | - if(floor(($height * $image_per)/100)>$imgheight && $imgheight > 0){ |
|
| 388 | - $image_per = (double)(($imgheight * 100) / $height); |
|
| 387 | + if (floor(($height * $image_per) / 100) > $imgheight && $imgheight > 0) { |
|
| 388 | + $image_per = (double) (($imgheight * 100) / $height); |
|
| 389 | 389 | } |
| 390 | 390 | |
| 391 | - $this->resize_percentage($image_per,$newfile); |
|
| 391 | + $this->resize_percentage($image_per, $newfile); |
|
| 392 | 392 | } |
| 393 | 393 | |
| 394 | 394 | /** |
@@ -398,10 +398,10 @@ discard block |
||
| 398 | 398 | * @param String $newfile |
| 399 | 399 | * @return Boolean |
| 400 | 400 | */ |
| 401 | - function resize_percentage($percent=100,$newfile=NULL) { |
|
| 402 | - $newWidth=($this->imgWidth*$percent)/100; |
|
| 403 | - $newHeight=($this->imgHeight*$percent)/100; |
|
| 404 | - return $this->resize($newWidth,$newHeight,$newfile); |
|
| 401 | + function resize_percentage($percent = 100, $newfile = NULL) { |
|
| 402 | + $newWidth = ($this->imgWidth * $percent) / 100; |
|
| 403 | + $newHeight = ($this->imgHeight * $percent) / 100; |
|
| 404 | + return $this->resize($newWidth, $newHeight, $newfile); |
|
| 405 | 405 | } |
| 406 | 406 | |
| 407 | 407 | /** |
@@ -412,10 +412,10 @@ discard block |
||
| 412 | 412 | * @param String $newfile |
| 413 | 413 | * @return Boolean |
| 414 | 414 | */ |
| 415 | - function resize_xypercentage($xpercent=100,$ypercent=100,$newfile=NULL) { |
|
| 416 | - $newWidth=($this->imgWidth*$xpercent)/100; |
|
| 417 | - $newHeight=($this->imgHeight*$ypercent)/100; |
|
| 418 | - return $this->resize($newWidth,$newHeight,$newfile); |
|
| 415 | + function resize_xypercentage($xpercent = 100, $ypercent = 100, $newfile = NULL) { |
|
| 416 | + $newWidth = ($this->imgWidth * $xpercent) / 100; |
|
| 417 | + $newHeight = ($this->imgHeight * $ypercent) / 100; |
|
| 418 | + return $this->resize($newWidth, $newHeight, $newfile); |
|
| 419 | 419 | } |
| 420 | 420 | |
| 421 | 421 | /** |
@@ -426,19 +426,19 @@ discard block |
||
| 426 | 426 | * @param String $newfile |
| 427 | 427 | * @return Boolean |
| 428 | 428 | */ |
| 429 | - function resize($width,$height,$newfile=NULL){ |
|
| 430 | - if(empty($this->imgFile)){ |
|
| 431 | - $this->_error="File name is not initialised."; |
|
| 429 | + function resize($width, $height, $newfile = NULL) { |
|
| 430 | + if (empty($this->imgFile)) { |
|
| 431 | + $this->_error = "File name is not initialised."; |
|
| 432 | 432 | return false; |
| 433 | 433 | } |
| 434 | - if($this->imgWidth<=0 || $this->imgHeight<=0){ |
|
| 435 | - $this->_error="Could not resize given image"; |
|
| 434 | + if ($this->imgWidth <= 0 || $this->imgHeight <= 0) { |
|
| 435 | + $this->_error = "Could not resize given image"; |
|
| 436 | 436 | return false; |
| 437 | 437 | } |
| 438 | - if($width<=0) $width=$this->imgWidth; |
|
| 439 | - if($height<=0) $height=$this->imgHeight; |
|
| 438 | + if ($width <= 0) $width = $this->imgWidth; |
|
| 439 | + if ($height <= 0) $height = $this->imgHeight; |
|
| 440 | 440 | |
| 441 | - return $this->_resize($width,$height,$newfile); |
|
| 441 | + return $this->_resize($width, $height, $newfile); |
|
| 442 | 442 | } |
| 443 | 443 | |
| 444 | 444 | /** |
@@ -448,8 +448,8 @@ discard block |
||
| 448 | 448 | */ |
| 449 | 449 | function _getImageInfo() |
| 450 | 450 | { |
| 451 | - @list($this->imgWidth,$this->imgHeight,$type,$this->imgAttr)=@getimagesize($this->imgFile); |
|
| 452 | - $this->imgType=$this->type[$type]; |
|
| 451 | + @list($this->imgWidth, $this->imgHeight, $type, $this->imgAttr) = @getimagesize($this->imgFile); |
|
| 452 | + $this->imgType = $this->type[$type]; |
|
| 453 | 453 | } |
| 454 | 454 | |
| 455 | 455 | /** |
@@ -457,20 +457,20 @@ discard block |
||
| 457 | 457 | * @access Private |
| 458 | 458 | * @return Boolean |
| 459 | 459 | */ |
| 460 | - function _createImage(){ |
|
| 460 | + function _createImage() { |
|
| 461 | 461 | $this->_getImageInfo(); |
| 462 | - if($this->imgType=='GIF'){ |
|
| 463 | - $this->_img=@imagecreatefromgif($this->imgFile); |
|
| 462 | + if ($this->imgType == 'GIF') { |
|
| 463 | + $this->_img = @imagecreatefromgif($this->imgFile); |
|
| 464 | 464 | } |
| 465 | - elseif($this->imgType=='JPG'){ |
|
| 466 | - $this->_img=@imagecreatefromjpeg($this->imgFile); |
|
| 465 | + elseif ($this->imgType == 'JPG') { |
|
| 466 | + $this->_img = @imagecreatefromjpeg($this->imgFile); |
|
| 467 | 467 | } |
| 468 | - elseif($this->imgType=='PNG'){ |
|
| 469 | - $this->_img=@imagecreatefrompng($this->imgFile); |
|
| 468 | + elseif ($this->imgType == 'PNG') { |
|
| 469 | + $this->_img = @imagecreatefrompng($this->imgFile); |
|
| 470 | 470 | } |
| 471 | 471 | |
| 472 | - if(!$this->_img || !@is_resource($this->_img)){ |
|
| 473 | - $this->_error="Error loading ".$this->imgFile; |
|
| 472 | + if (!$this->_img || !@is_resource($this->_img)) { |
|
| 473 | + $this->_error = "Error loading ".$this->imgFile; |
|
| 474 | 474 | return false; |
| 475 | 475 | } |
| 476 | 476 | return true; |
@@ -485,64 +485,64 @@ discard block |
||
| 485 | 485 | * @param String $newfile |
| 486 | 486 | * @return Boolean |
| 487 | 487 | */ |
| 488 | - function _resize($width,$height,$newfile=NULL){ |
|
| 489 | - if (!function_exists("imagecreate")){ |
|
| 490 | - $this->_error="Error: GD Library is not available."; |
|
| 488 | + function _resize($width, $height, $newfile = NULL) { |
|
| 489 | + if (!function_exists("imagecreate")) { |
|
| 490 | + $this->_error = "Error: GD Library is not available."; |
|
| 491 | 491 | return false; |
| 492 | 492 | } |
| 493 | 493 | |
| 494 | - $newimg=@imagecreatetruecolor($width,$height); |
|
| 494 | + $newimg = @imagecreatetruecolor($width, $height); |
|
| 495 | 495 | //imagecolortransparent( $newimg, imagecolorat( $newimg, 0, 0 ) ); |
| 496 | 496 | |
| 497 | - if($this->imgType=='GIF' || $this->imgType=='PNG') { |
|
| 497 | + if ($this->imgType == 'GIF' || $this->imgType == 'PNG') { |
|
| 498 | 498 | /** Code to keep transparency of image **/ |
| 499 | 499 | $colorcount = imagecolorstotal($this->_img); |
| 500 | 500 | if ($colorcount == 0) $colorcount = 256; |
| 501 | - imagetruecolortopalette($newimg,true,$colorcount); |
|
| 502 | - imagepalettecopy($newimg,$this->_img); |
|
| 501 | + imagetruecolortopalette($newimg, true, $colorcount); |
|
| 502 | + imagepalettecopy($newimg, $this->_img); |
|
| 503 | 503 | $transparentcolor = imagecolortransparent($this->_img); |
| 504 | - imagefill($newimg,0,0,$transparentcolor); |
|
| 505 | - imagecolortransparent($newimg,$transparentcolor); |
|
| 504 | + imagefill($newimg, 0, 0, $transparentcolor); |
|
| 505 | + imagecolortransparent($newimg, $transparentcolor); |
|
| 506 | 506 | } |
| 507 | 507 | |
| 508 | - @imagecopyresampled ( $newimg, $this->_img, 0,0,0,0, $width, $height, $this->imgWidth,$this->imgHeight); |
|
| 508 | + @imagecopyresampled($newimg, $this->_img, 0, 0, 0, 0, $width, $height, $this->imgWidth, $this->imgHeight); |
|
| 509 | 509 | |
| 510 | - if($newfile===1) { |
|
| 511 | - if(@preg_match("/\..*+$/",@basename($this->imgFile),$matches)){ |
|
| 512 | - $newfile=@substr_replace($this->imgFile,"_har",-@strlen($matches[0]),0); |
|
| 510 | + if ($newfile === 1) { |
|
| 511 | + if (@preg_match("/\..*+$/", @basename($this->imgFile), $matches)) { |
|
| 512 | + $newfile = @substr_replace($this->imgFile, "_har", -@strlen($matches[0]), 0); |
|
| 513 | 513 | } |
| 514 | 514 | } |
| 515 | - elseif(!empty($newfile)){ |
|
| 516 | - if(!@preg_match("/\..*+$/",@basename($newfile))){ |
|
| 517 | - if(@preg_match("/\..*+$/",@basename($this->imgFile),$matches)){ |
|
| 518 | - $newfile=$newfile.$matches[0]; |
|
| 515 | + elseif (!empty($newfile)) { |
|
| 516 | + if (!@preg_match("/\..*+$/", @basename($newfile))) { |
|
| 517 | + if (@preg_match("/\..*+$/", @basename($this->imgFile), $matches)) { |
|
| 518 | + $newfile = $newfile.$matches[0]; |
|
| 519 | 519 | } |
| 520 | 520 | } |
| 521 | 521 | } |
| 522 | 522 | |
| 523 | - if($this->imgType=='GIF'){ |
|
| 524 | - if(!empty($newfile)){ |
|
| 525 | - @imagegif($newimg,$newfile); |
|
| 523 | + if ($this->imgType == 'GIF') { |
|
| 524 | + if (!empty($newfile)) { |
|
| 525 | + @imagegif($newimg, $newfile); |
|
| 526 | 526 | } |
| 527 | 527 | else { |
| 528 | 528 | @header("Content-type: image/gif"); |
| 529 | 529 | @imagegif($newimg); |
| 530 | 530 | } |
| 531 | 531 | } |
| 532 | - elseif($this->imgType=='JPG'){ |
|
| 533 | - if(!empty($newfile)){ |
|
| 534 | - @imagejpeg($newimg,$newfile); |
|
| 532 | + elseif ($this->imgType == 'JPG') { |
|
| 533 | + if (!empty($newfile)) { |
|
| 534 | + @imagejpeg($newimg, $newfile); |
|
| 535 | 535 | } |
| 536 | - else { |
|
| 536 | + else { |
|
| 537 | 537 | @header("Content-type: image/jpeg"); |
| 538 | 538 | @imagejpeg($newimg); |
| 539 | 539 | } |
| 540 | 540 | } |
| 541 | - elseif($this->imgType=='PNG'){ |
|
| 542 | - if(!empty($newfile)){ |
|
| 543 | - @imagepng($newimg,$newfile); |
|
| 541 | + elseif ($this->imgType == 'PNG') { |
|
| 542 | + if (!empty($newfile)) { |
|
| 543 | + @imagepng($newimg, $newfile); |
|
| 544 | 544 | } |
| 545 | - else{ |
|
| 545 | + else { |
|
| 546 | 546 | @header("Content-type: image/png"); |
| 547 | 547 | @imagepng($newimg); |
| 548 | 548 | } |
@@ -115,8 +115,7 @@ discard block |
||
| 115 | 115 | $img = false; |
| 116 | 116 | if(is_string($name)){ |
| 117 | 117 | $img = $this->_getImageByName(); |
| 118 | - } |
|
| 119 | - elseif(is_int($name)){ |
|
| 118 | + } elseif(is_int($name)){ |
|
| 120 | 119 | $img = $this->_getImageById(); |
| 121 | 120 | } |
| 122 | 121 | |
@@ -145,8 +144,7 @@ discard block |
||
| 145 | 144 | unset($options['model'], $options['var']); |
| 146 | 145 | |
| 147 | 146 | return $this->Form->input("{$configs['model']}.".$this->inputCount++.".{$configs['var']}", array_merge(array('type'=>'file'), $options)); |
| 148 | - } |
|
| 149 | - else { |
|
| 147 | + } else { |
|
| 150 | 148 | return "<input type='file' name='data[{$configs['var']}][".$this->inputCount++."]' />"; |
| 151 | 149 | } |
| 152 | 150 | } |
@@ -166,8 +164,7 @@ discard block |
||
| 166 | 164 | if(!empty($upload)){ |
| 167 | 165 | $this->fileName = $upload[$this->settings['fileModel']][$this->settings['fields']['name']]; |
| 168 | 166 | return $this->_getImageByName(); |
| 169 | - } |
|
| 170 | - else{ |
|
| 167 | + } else{ |
|
| 171 | 168 | return false; |
| 172 | 169 | } |
| 173 | 170 | } |
@@ -180,8 +177,7 @@ discard block |
||
| 180 | 177 | function _getFullPath(){ |
| 181 | 178 | if($this->_isOutsideSource()){ |
| 182 | 179 | return $this->fileName; |
| 183 | - } |
|
| 184 | - else { |
|
| 180 | + } else { |
|
| 185 | 181 | return WWW_ROOT . $this->_getUploadPath(); |
| 186 | 182 | } |
| 187 | 183 | } |
@@ -194,8 +190,7 @@ discard block |
||
| 194 | 190 | function _getImagePath(){ |
| 195 | 191 | if($this->_isOutsideSource()){ |
| 196 | 192 | return $this->fileName; |
| 197 | - } |
|
| 198 | - else { |
|
| 193 | + } else { |
|
| 199 | 194 | return '/' . $this->_getUploadPath(); |
| 200 | 195 | } |
| 201 | 196 | } |
@@ -227,7 +222,9 @@ discard block |
||
| 227 | 222 | */ |
| 228 | 223 | function _getImageByName(){ |
| 229 | 224 | //only proceed if we actually have the file in question |
| 230 | - if(!$this->_isOutsideSource() && !file_exists($this->_getFullPath())) return false; |
|
| 225 | + if(!$this->_isOutsideSource() && !file_exists($this->_getFullPath())) { |
|
| 226 | + return false; |
|
| 227 | + } |
|
| 231 | 228 | |
| 232 | 229 | //resize if we have resize on, a width, and if it doesn't already exist. |
| 233 | 230 | if($this->options['autoResize'] && $this->options['width'] > 0 && !file_exists($this->_getResizeNameOrPath($this->_getFullPath()))){ |
@@ -257,8 +254,7 @@ discard block |
||
| 257 | 254 | $this->newImage = new RResizeImage($this->_getFullPath()); |
| 258 | 255 | if($this->newImage->imgWidth > $this->options['width']){ |
| 259 | 256 | $this->newImage->resize_limitwh($this->options['width'], 0, $this->_getResizeNameOrPath($this->_getFullPath())); |
| 260 | - } |
|
| 261 | - else { |
|
| 257 | + } else { |
|
| 262 | 258 | //$this->autoResize = false; |
| 263 | 259 | } |
| 264 | 260 | } |
@@ -272,12 +268,10 @@ discard block |
||
| 272 | 268 | if(!$this->_isOutsideSource() && $this->options['autoResize'] && $this->options['width'] > 0){ |
| 273 | 269 | if(isset($this->newImage) && $this->newImage->imgWidth && $this->newImage->imgWidth <= $this->options['width']){ |
| 274 | 270 | $image = $this->_getImagePath(); |
| 275 | - } |
|
| 276 | - else { |
|
| 271 | + } else { |
|
| 277 | 272 | $image = $this->_getResizeNameOrPath($this->_getImagePath()); |
| 278 | 273 | } |
| 279 | - } |
|
| 280 | - else { |
|
| 274 | + } else { |
|
| 281 | 275 | $image = $this->_getImagePath(); |
| 282 | 276 | } |
| 283 | 277 | |
@@ -285,13 +279,14 @@ discard block |
||
| 285 | 279 | //unset the default options |
| 286 | 280 | unset($options['resizedDir'], $options['uploadDir'], $options['imagePathOnly'], $options['autoResize'], $options['resizeThumbOnly']); |
| 287 | 281 | //unset width only if we're not an outsourced image, we have resize turned on, or we don't have a width to begin with. |
| 288 | - if(!$this->_isOutsideSource() && ($this->options['resizeThumbOnly'] || !$options['width'])) unset($options['width']); |
|
| 282 | + if(!$this->_isOutsideSource() && ($this->options['resizeThumbOnly'] || !$options['width'])) { |
|
| 283 | + unset($options['width']); |
|
| 284 | + } |
|
| 289 | 285 | |
| 290 | 286 | //return the impage path or image html |
| 291 | 287 | if($this->options['imagePathOnly']){ |
| 292 | 288 | return $image; |
| 293 | - } |
|
| 294 | - else { |
|
| 289 | + } else { |
|
| 295 | 290 | return $this->Html->image($image, $options); |
| 296 | 291 | } |
| 297 | 292 | } |
@@ -435,8 +430,12 @@ discard block |
||
| 435 | 430 | $this->_error="Could not resize given image"; |
| 436 | 431 | return false; |
| 437 | 432 | } |
| 438 | - if($width<=0) $width=$this->imgWidth; |
|
| 439 | - if($height<=0) $height=$this->imgHeight; |
|
| 433 | + if($width<=0) { |
|
| 434 | + $width=$this->imgWidth; |
|
| 435 | + } |
|
| 436 | + if($height<=0) { |
|
| 437 | + $height=$this->imgHeight; |
|
| 438 | + } |
|
| 440 | 439 | |
| 441 | 440 | return $this->_resize($width,$height,$newfile); |
| 442 | 441 | } |
@@ -461,11 +460,9 @@ discard block |
||
| 461 | 460 | $this->_getImageInfo(); |
| 462 | 461 | if($this->imgType=='GIF'){ |
| 463 | 462 | $this->_img=@imagecreatefromgif($this->imgFile); |
| 464 | - } |
|
| 465 | - elseif($this->imgType=='JPG'){ |
|
| 463 | + } elseif($this->imgType=='JPG'){ |
|
| 466 | 464 | $this->_img=@imagecreatefromjpeg($this->imgFile); |
| 467 | - } |
|
| 468 | - elseif($this->imgType=='PNG'){ |
|
| 465 | + } elseif($this->imgType=='PNG'){ |
|
| 469 | 466 | $this->_img=@imagecreatefrompng($this->imgFile); |
| 470 | 467 | } |
| 471 | 468 | |
@@ -497,7 +494,9 @@ discard block |
||
| 497 | 494 | if($this->imgType=='GIF' || $this->imgType=='PNG') { |
| 498 | 495 | /** Code to keep transparency of image **/ |
| 499 | 496 | $colorcount = imagecolorstotal($this->_img); |
| 500 | - if ($colorcount == 0) $colorcount = 256; |
|
| 497 | + if ($colorcount == 0) { |
|
| 498 | + $colorcount = 256; |
|
| 499 | + } |
|
| 501 | 500 | imagetruecolortopalette($newimg,true,$colorcount); |
| 502 | 501 | imagepalettecopy($newimg,$this->_img); |
| 503 | 502 | $transparentcolor = imagecolortransparent($this->_img); |
@@ -511,8 +510,7 @@ discard block |
||
| 511 | 510 | if(@preg_match("/\..*+$/",@basename($this->imgFile),$matches)){ |
| 512 | 511 | $newfile=@substr_replace($this->imgFile,"_har",-@strlen($matches[0]),0); |
| 513 | 512 | } |
| 514 | - } |
|
| 515 | - elseif(!empty($newfile)){ |
|
| 513 | + } elseif(!empty($newfile)){ |
|
| 516 | 514 | if(!@preg_match("/\..*+$/",@basename($newfile))){ |
| 517 | 515 | if(@preg_match("/\..*+$/",@basename($this->imgFile),$matches)){ |
| 518 | 516 | $newfile=$newfile.$matches[0]; |
@@ -523,26 +521,21 @@ discard block |
||
| 523 | 521 | if($this->imgType=='GIF'){ |
| 524 | 522 | if(!empty($newfile)){ |
| 525 | 523 | @imagegif($newimg,$newfile); |
| 526 | - } |
|
| 527 | - else { |
|
| 524 | + } else { |
|
| 528 | 525 | @header("Content-type: image/gif"); |
| 529 | 526 | @imagegif($newimg); |
| 530 | 527 | } |
| 531 | - } |
|
| 532 | - elseif($this->imgType=='JPG'){ |
|
| 528 | + } elseif($this->imgType=='JPG'){ |
|
| 533 | 529 | if(!empty($newfile)){ |
| 534 | 530 | @imagejpeg($newimg,$newfile); |
| 535 | - } |
|
| 536 | - else { |
|
| 531 | + } else { |
|
| 537 | 532 | @header("Content-type: image/jpeg"); |
| 538 | 533 | @imagejpeg($newimg); |
| 539 | 534 | } |
| 540 | - } |
|
| 541 | - elseif($this->imgType=='PNG'){ |
|
| 535 | + } elseif($this->imgType=='PNG'){ |
|
| 542 | 536 | if(!empty($newfile)){ |
| 543 | 537 | @imagepng($newimg,$newfile); |
| 544 | - } |
|
| 545 | - else{ |
|
| 538 | + } else{ |
|
| 546 | 539 | @header("Content-type: image/png"); |
| 547 | 540 | @imagepng($newimg); |
| 548 | 541 | } |
@@ -110,7 +110,7 @@ |
||
| 110 | 110 | * credentials or create the missing database |
| 111 | 111 | * Create the database file and insert the submitted details |
| 112 | 112 | * |
| 113 | - * @return void |
|
| 113 | + * @return null|CakeResponse |
|
| 114 | 114 | * @access public |
| 115 | 115 | */ |
| 116 | 116 | public function database() { |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | * @var string |
| 22 | 22 | * @access public |
| 23 | 23 | */ |
| 24 | - public $name = 'Install'; |
|
| 24 | + public $name = 'Install'; |
|
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | 27 | * No models required |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | * @var array |
| 30 | 30 | * @access public |
| 31 | 31 | */ |
| 32 | - public $uses = null; |
|
| 32 | + public $uses = null; |
|
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | 35 | * No components required |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | * @var array |
| 38 | 38 | * @access public |
| 39 | 39 | */ |
| 40 | - public $components = null; |
|
| 40 | + public $components = null; |
|
| 41 | 41 | |
| 42 | 42 | /** |
| 43 | 43 | * Default configuration |
@@ -45,19 +45,19 @@ discard block |
||
| 45 | 45 | * @var array |
| 46 | 46 | * @access public |
| 47 | 47 | */ |
| 48 | - public $defaultConfig = array( |
|
| 49 | - 'name' => 'default', |
|
| 50 | - 'datasource'=> 'Database/Mysql', |
|
| 51 | - 'persistent'=> false, |
|
| 52 | - 'host'=> 'localhost', |
|
| 53 | - 'login'=> 'root', |
|
| 54 | - 'password'=> '', |
|
| 55 | - 'database'=> 'croogo', |
|
| 56 | - 'schema'=> null, |
|
| 57 | - 'prefix'=> null, |
|
| 58 | - 'encoding' => 'utf8', |
|
| 59 | - 'port' => null, |
|
| 60 | - ); |
|
| 48 | + public $defaultConfig = array( |
|
| 49 | + 'name' => 'default', |
|
| 50 | + 'datasource'=> 'Database/Mysql', |
|
| 51 | + 'persistent'=> false, |
|
| 52 | + 'host'=> 'localhost', |
|
| 53 | + 'login'=> 'root', |
|
| 54 | + 'password'=> '', |
|
| 55 | + 'database'=> 'croogo', |
|
| 56 | + 'schema'=> null, |
|
| 57 | + 'prefix'=> null, |
|
| 58 | + 'encoding' => 'utf8', |
|
| 59 | + 'port' => null, |
|
| 60 | + ); |
|
| 61 | 61 | |
| 62 | 62 | /** |
| 63 | 63 | * beforeFilter |
@@ -65,30 +65,30 @@ discard block |
||
| 65 | 65 | * @return void |
| 66 | 66 | * @access public |
| 67 | 67 | */ |
| 68 | - public function beforeFilter() { |
|
| 69 | - parent::beforeFilter(); |
|
| 70 | - $this->_check(); |
|
| 68 | + public function beforeFilter() { |
|
| 69 | + parent::beforeFilter(); |
|
| 70 | + $this->_check(); |
|
| 71 | 71 | |
| 72 | - $this->layout = 'install'; |
|
| 73 | - App::import('Component', 'Session'); |
|
| 74 | - $this->Session = new SessionComponent($this->Components); |
|
| 72 | + $this->layout = 'install'; |
|
| 73 | + App::import('Component', 'Session'); |
|
| 74 | + $this->Session = new SessionComponent($this->Components); |
|
| 75 | 75 | |
| 76 | - Cache::clear(false, '_cake_core_'); |
|
| 77 | - Cache::clear(false, '_cake_model_'); |
|
| 78 | - } |
|
| 76 | + Cache::clear(false, '_cake_core_'); |
|
| 77 | + Cache::clear(false, '_cake_model_'); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | 80 | /** |
| 81 | 81 | * If settings.yml exists, app is already installed |
| 82 | 82 | * |
| 83 | 83 | * @return void |
| 84 | 84 | */ |
| 85 | - protected function _check() { |
|
| 86 | - if (file_exists(APP . 'Config' . DS . 'installed.txt')) { |
|
| 85 | + protected function _check() { |
|
| 86 | + if (file_exists(APP . 'Config' . DS . 'installed.txt')) { |
|
| 87 | 87 | // if (Configure::read('Install.installed') && Configure::read('Install.secured')) { |
| 88 | - $this->Session->setFlash('Already Installed'); |
|
| 89 | - $this->redirect('/'); |
|
| 90 | - } |
|
| 91 | - } |
|
| 88 | + $this->Session->setFlash('Already Installed'); |
|
| 89 | + $this->redirect('/'); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | 93 | /** |
| 94 | 94 | * Step 0: welcome |
@@ -98,10 +98,10 @@ discard block |
||
| 98 | 98 | * @return void |
| 99 | 99 | * @access public |
| 100 | 100 | */ |
| 101 | - public function index() { |
|
| 102 | - $this->_check(); |
|
| 103 | - $this->set('title_for_layout', __('Installation: Welcome')); |
|
| 104 | - } |
|
| 101 | + public function index() { |
|
| 102 | + $this->_check(); |
|
| 103 | + $this->set('title_for_layout', __('Installation: Welcome')); |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | 106 | /** |
| 107 | 107 | * Step 1: database |
@@ -113,63 +113,63 @@ discard block |
||
| 113 | 113 | * @return void |
| 114 | 114 | * @access public |
| 115 | 115 | */ |
| 116 | - public function database() { |
|
| 117 | - $this->_check(); |
|
| 118 | - $this->set('title_for_layout', __('Step 1: Database')); |
|
| 116 | + public function database() { |
|
| 117 | + $this->_check(); |
|
| 118 | + $this->set('title_for_layout', __('Step 1: Database')); |
|
| 119 | 119 | |
| 120 | 120 | |
| 121 | - if (empty($this->request->data)) { |
|
| 122 | - return; |
|
| 123 | - } |
|
| 121 | + if (empty($this->request->data)) { |
|
| 122 | + return; |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - @App::import('Model', 'ConnectionManager'); |
|
| 126 | - $config = $this->defaultConfig; |
|
| 127 | - foreach ($this->request->data['Install'] AS $key => $value) { |
|
| 128 | - if (isset($this->request->data['Install'][$key])) { |
|
| 129 | - $config[$key] = $value; |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - try { |
|
| 133 | - @ConnectionManager::create('default', $config); |
|
| 134 | - $db = ConnectionManager::getDataSource('default'); |
|
| 135 | - } |
|
| 136 | - catch (MissingConnectionException $e) { |
|
| 137 | - $this->Session->setFlash(__('Could not connect to database: %s', $e->getMessage()), 'default', array('class' => 'error')); |
|
| 138 | - $this->redirect(array('action' => 'database')); |
|
| 139 | - } |
|
| 140 | - if (!$db->isConnected()) { |
|
| 141 | - $this->Session->setFlash(__('Could not connect to database.'), 'default', array('class' => 'error')); |
|
| 142 | - return; |
|
| 143 | - } |
|
| 125 | + @App::import('Model', 'ConnectionManager'); |
|
| 126 | + $config = $this->defaultConfig; |
|
| 127 | + foreach ($this->request->data['Install'] AS $key => $value) { |
|
| 128 | + if (isset($this->request->data['Install'][$key])) { |
|
| 129 | + $config[$key] = $value; |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + try { |
|
| 133 | + @ConnectionManager::create('default', $config); |
|
| 134 | + $db = ConnectionManager::getDataSource('default'); |
|
| 135 | + } |
|
| 136 | + catch (MissingConnectionException $e) { |
|
| 137 | + $this->Session->setFlash(__('Could not connect to database: %s', $e->getMessage()), 'default', array('class' => 'error')); |
|
| 138 | + $this->redirect(array('action' => 'database')); |
|
| 139 | + } |
|
| 140 | + if (!$db->isConnected()) { |
|
| 141 | + $this->Session->setFlash(__('Could not connect to database.'), 'default', array('class' => 'error')); |
|
| 142 | + return; |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - copy(APP . 'Config' . DS.'database.php.install', APP . 'Config' . DS.'database.php'); |
|
| 146 | - App::uses('File', 'Utility'); |
|
| 147 | - $file = new File(APP . 'Config' . DS.'database.php', true); |
|
| 148 | - $content = $file->read(); |
|
| 145 | + copy(APP . 'Config' . DS.'database.php.install', APP . 'Config' . DS.'database.php'); |
|
| 146 | + App::uses('File', 'Utility'); |
|
| 147 | + $file = new File(APP . 'Config' . DS.'database.php', true); |
|
| 148 | + $content = $file->read(); |
|
| 149 | 149 | |
| 150 | - foreach ($config AS $configKey => $configValue) { |
|
| 151 | - $content = str_replace('{default_' . $configKey . '}', $configValue, $content); |
|
| 152 | - } |
|
| 150 | + foreach ($config AS $configKey => $configValue) { |
|
| 151 | + $content = str_replace('{default_' . $configKey . '}', $configValue, $content); |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | - if($file->write($content) ) { |
|
| 155 | - return $this->redirect(array('action' => 'data')); |
|
| 156 | - } else { |
|
| 157 | - $this->Session->setFlash(__('Could not write database.php file.'), 'default', array('class' => 'error')); |
|
| 158 | - } |
|
| 159 | - } |
|
| 154 | + if($file->write($content) ) { |
|
| 155 | + return $this->redirect(array('action' => 'data')); |
|
| 156 | + } else { |
|
| 157 | + $this->Session->setFlash(__('Could not write database.php file.'), 'default', array('class' => 'error')); |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | - /** |
|
| 162 | - * Fixes Postgres sequence |
|
| 163 | - */ |
|
| 164 | - protected function _fixSequence($model) { |
|
| 165 | - $db = $model->getDataSource(); |
|
| 166 | - $nextValue = $model->find('first', array( |
|
| 167 | - 'fields' => sprintf('MAX(%s.%s) as max', $model->alias, $model->primaryKey), |
|
| 168 | - )); |
|
| 169 | - $nextValue = empty($nextValue[0]['max']) ? 1 : $nextValue[0]['max'] + 1; |
|
| 170 | - $sql = sprintf('alter sequence %s restart with %d', $db->getSequence($model), $nextValue); |
|
| 171 | - $db->execute($sql); |
|
| 172 | - } |
|
| 161 | + /** |
|
| 162 | + * Fixes Postgres sequence |
|
| 163 | + */ |
|
| 164 | + protected function _fixSequence($model) { |
|
| 165 | + $db = $model->getDataSource(); |
|
| 166 | + $nextValue = $model->find('first', array( |
|
| 167 | + 'fields' => sprintf('MAX(%s.%s) as max', $model->alias, $model->primaryKey), |
|
| 168 | + )); |
|
| 169 | + $nextValue = empty($nextValue[0]['max']) ? 1 : $nextValue[0]['max'] + 1; |
|
| 170 | + $sql = sprintf('alter sequence %s restart with %d', $db->getSequence($model), $nextValue); |
|
| 171 | + $db->execute($sql); |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | 174 | /** |
| 175 | 175 | * Step 2: Run the initial sql scripts to create the db and seed it with data |
@@ -177,83 +177,83 @@ discard block |
||
| 177 | 177 | * @return void |
| 178 | 178 | * @access public |
| 179 | 179 | */ |
| 180 | - public function data() { |
|
| 181 | - $this->_check(); |
|
| 182 | - $this->set('title_for_layout', __('Step 2: Build database')); |
|
| 183 | - if (isset($this->params['named']['run'])) { |
|
| 184 | - App::uses('File', 'Utility'); |
|
| 185 | - App::import('Model', 'CakeSchema', false); |
|
| 186 | - App::import('Model', 'ConnectionManager'); |
|
| 180 | + public function data() { |
|
| 181 | + $this->_check(); |
|
| 182 | + $this->set('title_for_layout', __('Step 2: Build database')); |
|
| 183 | + if (isset($this->params['named']['run'])) { |
|
| 184 | + App::uses('File', 'Utility'); |
|
| 185 | + App::import('Model', 'CakeSchema', false); |
|
| 186 | + App::import('Model', 'ConnectionManager'); |
|
| 187 | 187 | |
| 188 | - $db = ConnectionManager::getDataSource('default'); |
|
| 189 | - $brokenSequence = $db instanceof Postgres; |
|
| 190 | - if(!$db->isConnected()) { |
|
| 191 | - $this->Session->setFlash(__('Could not connect to database.'), 'default', array('class' => 'error')); |
|
| 192 | - } else { |
|
| 193 | - $schema = new CakeSchema(array('name'=>'app')); |
|
| 194 | - $schema = $schema->load(); |
|
| 195 | - foreach($schema->tables as $table => $fields) { |
|
| 196 | - $create = $db->createSchema($schema, $table); |
|
| 197 | - try { |
|
| 198 | - $db->execute($create); |
|
| 199 | - } |
|
| 200 | - catch (PDOException $e) { |
|
| 201 | - $this->Session->setFlash(__('Could not create table: %s', $e->getMessage()), 'default', array('class' => 'error')); |
|
| 202 | - $this->redirect(array('action' => 'database')); |
|
| 203 | - } |
|
| 204 | - } |
|
| 188 | + $db = ConnectionManager::getDataSource('default'); |
|
| 189 | + $brokenSequence = $db instanceof Postgres; |
|
| 190 | + if(!$db->isConnected()) { |
|
| 191 | + $this->Session->setFlash(__('Could not connect to database.'), 'default', array('class' => 'error')); |
|
| 192 | + } else { |
|
| 193 | + $schema = new CakeSchema(array('name'=>'app')); |
|
| 194 | + $schema = $schema->load(); |
|
| 195 | + foreach($schema->tables as $table => $fields) { |
|
| 196 | + $create = $db->createSchema($schema, $table); |
|
| 197 | + try { |
|
| 198 | + $db->execute($create); |
|
| 199 | + } |
|
| 200 | + catch (PDOException $e) { |
|
| 201 | + $this->Session->setFlash(__('Could not create table: %s', $e->getMessage()), 'default', array('class' => 'error')); |
|
| 202 | + $this->redirect(array('action' => 'database')); |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | - $path = CakePlugin::path('Install') .DS. 'Config' .DS. 'Data' .DS; |
|
| 207 | - $dataObjects = App::objects('class', $path); |
|
| 208 | - foreach ($dataObjects as $data) { |
|
| 209 | - include($path . $data . '.php'); |
|
| 210 | - $classVars = get_class_vars($data); |
|
| 211 | - $modelAlias = substr($data, 0, -4); |
|
| 212 | - $table = $classVars['table']; |
|
| 213 | - $records = $classVars['records']; |
|
| 214 | - App::import('Model', 'Model', false); |
|
| 215 | - $modelObject = new Model(array( |
|
| 216 | - 'name' => $modelAlias, |
|
| 217 | - 'table' => $table, |
|
| 218 | - 'ds' => 'default', |
|
| 219 | - )); |
|
| 220 | - if (is_array($records) && count($records) > 0) { |
|
| 221 | - $i = 1; |
|
| 222 | - foreach($records as $record) { |
|
| 223 | - if (!isset($record['id'])) { |
|
| 224 | - $record['id'] = $i++; |
|
| 225 | - } |
|
| 226 | - $modelObject->create($record); |
|
| 227 | - $modelObject->save(); |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - if ($brokenSequence) { |
|
| 231 | - // $this->_fixSequence($modelObject); |
|
| 232 | - } |
|
| 233 | - } |
|
| 206 | + $path = CakePlugin::path('Install') .DS. 'Config' .DS. 'Data' .DS; |
|
| 207 | + $dataObjects = App::objects('class', $path); |
|
| 208 | + foreach ($dataObjects as $data) { |
|
| 209 | + include($path . $data . '.php'); |
|
| 210 | + $classVars = get_class_vars($data); |
|
| 211 | + $modelAlias = substr($data, 0, -4); |
|
| 212 | + $table = $classVars['table']; |
|
| 213 | + $records = $classVars['records']; |
|
| 214 | + App::import('Model', 'Model', false); |
|
| 215 | + $modelObject = new Model(array( |
|
| 216 | + 'name' => $modelAlias, |
|
| 217 | + 'table' => $table, |
|
| 218 | + 'ds' => 'default', |
|
| 219 | + )); |
|
| 220 | + if (is_array($records) && count($records) > 0) { |
|
| 221 | + $i = 1; |
|
| 222 | + foreach($records as $record) { |
|
| 223 | + if (!isset($record['id'])) { |
|
| 224 | + $record['id'] = $i++; |
|
| 225 | + } |
|
| 226 | + $modelObject->create($record); |
|
| 227 | + $modelObject->save(); |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + if ($brokenSequence) { |
|
| 231 | + // $this->_fixSequence($modelObject); |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | 234 | |
| 235 | - $this->redirect(array('action' => 'adminuser')); |
|
| 236 | - } |
|
| 237 | - } |
|
| 238 | - } |
|
| 235 | + $this->redirect(array('action' => 'adminuser')); |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | + } |
|
| 239 | 239 | |
| 240 | 240 | /** |
| 241 | 241 | * Step 3: get username and passwords for administrative user |
| 242 | 242 | */ |
| 243 | - public function adminuser() { |
|
| 243 | + public function adminuser() { |
|
| 244 | 244 | // if ($this->request->is('post')) { |
| 245 | - $this->loadModel('User'); |
|
| 245 | + $this->loadModel('User'); |
|
| 246 | 246 | // $this->User->set($this->request->data); |
| 247 | 247 | // if ($this->User->validates()) { |
| 248 | - $token = uniqid(); |
|
| 249 | - $this->Session->write('Install', array( |
|
| 250 | - 'token' => $token, |
|
| 248 | + $token = uniqid(); |
|
| 249 | + $this->Session->write('Install', array( |
|
| 250 | + 'token' => $token, |
|
| 251 | 251 | // 'User' => $this->request->data['User'], |
| 252 | - )); |
|
| 253 | - $this->redirect(array('action' => 'finish', $token)); |
|
| 252 | + )); |
|
| 253 | + $this->redirect(array('action' => 'finish', $token)); |
|
| 254 | 254 | // } |
| 255 | 255 | // } |
| 256 | - } |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | 258 | /** |
| 259 | 259 | * Step 4: finish |
@@ -263,33 +263,33 @@ discard block |
||
| 263 | 263 | * @return void |
| 264 | 264 | * @access public |
| 265 | 265 | */ |
| 266 | - public function finish($token = null) { |
|
| 267 | - $this->set('title_for_layout', __('Installation completed successfully')); |
|
| 268 | - $this->_check(); |
|
| 269 | - $this->loadModel('Install.Install'); |
|
| 270 | - $install = $this->Session->read('Install'); |
|
| 271 | - if ($install['token'] == $token) { |
|
| 272 | - unset($install['token']); |
|
| 273 | - if ($this->Install->finalize($install)) { |
|
| 274 | - $urlBlogAdd = Router::url(array( |
|
| 275 | - 'plugin' => false, 'admin' => true, |
|
| 276 | - 'controller' => 'nodes', 'action' => 'add', 'blog', |
|
| 277 | - )); |
|
| 278 | - $urlSettings = Router::url(array( |
|
| 279 | - 'plugin' => false, 'admin' => true, |
|
| 280 | - 'controller' => 'settings', 'action' => 'prefix', 'Site', |
|
| 281 | - )); |
|
| 282 | - $this->set('user', $install); |
|
| 283 | - $this->set(compact('urlBlogAdd', 'urlSettings')); |
|
| 284 | - } else { |
|
| 285 | - $this->Session->setFlash('Something went wrong during installation. Please check your server logs.'); |
|
| 286 | - $this->redirect(array('action' => 'adminuser')); |
|
| 287 | - } |
|
| 288 | - $this->Session->delete('Install'); |
|
| 289 | - } else { |
|
| 290 | - $this->redirect('/'); |
|
| 291 | - } |
|
| 292 | - } |
|
| 266 | + public function finish($token = null) { |
|
| 267 | + $this->set('title_for_layout', __('Installation completed successfully')); |
|
| 268 | + $this->_check(); |
|
| 269 | + $this->loadModel('Install.Install'); |
|
| 270 | + $install = $this->Session->read('Install'); |
|
| 271 | + if ($install['token'] == $token) { |
|
| 272 | + unset($install['token']); |
|
| 273 | + if ($this->Install->finalize($install)) { |
|
| 274 | + $urlBlogAdd = Router::url(array( |
|
| 275 | + 'plugin' => false, 'admin' => true, |
|
| 276 | + 'controller' => 'nodes', 'action' => 'add', 'blog', |
|
| 277 | + )); |
|
| 278 | + $urlSettings = Router::url(array( |
|
| 279 | + 'plugin' => false, 'admin' => true, |
|
| 280 | + 'controller' => 'settings', 'action' => 'prefix', 'Site', |
|
| 281 | + )); |
|
| 282 | + $this->set('user', $install); |
|
| 283 | + $this->set(compact('urlBlogAdd', 'urlSettings')); |
|
| 284 | + } else { |
|
| 285 | + $this->Session->setFlash('Something went wrong during installation. Please check your server logs.'); |
|
| 286 | + $this->redirect(array('action' => 'adminuser')); |
|
| 287 | + } |
|
| 288 | + $this->Session->delete('Install'); |
|
| 289 | + } else { |
|
| 290 | + $this->redirect('/'); |
|
| 291 | + } |
|
| 292 | + } |
|
| 293 | 293 | |
| 294 | 294 | } |
| 295 | 295 | ?> |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | * @return void |
| 84 | 84 | */ |
| 85 | 85 | protected function _check() { |
| 86 | - if (file_exists(APP . 'Config' . DS . 'installed.txt')) { |
|
| 86 | + if (file_exists(APP.'Config'.DS.'installed.txt')) { |
|
| 87 | 87 | // if (Configure::read('Install.installed') && Configure::read('Install.secured')) { |
| 88 | 88 | $this->Session->setFlash('Already Installed'); |
| 89 | 89 | $this->redirect('/'); |
@@ -142,16 +142,16 @@ discard block |
||
| 142 | 142 | return; |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | - copy(APP . 'Config' . DS.'database.php.install', APP . 'Config' . DS.'database.php'); |
|
| 145 | + copy(APP.'Config'.DS.'database.php.install', APP.'Config'.DS.'database.php'); |
|
| 146 | 146 | App::uses('File', 'Utility'); |
| 147 | - $file = new File(APP . 'Config' . DS.'database.php', true); |
|
| 147 | + $file = new File(APP.'Config'.DS.'database.php', true); |
|
| 148 | 148 | $content = $file->read(); |
| 149 | 149 | |
| 150 | 150 | foreach ($config AS $configKey => $configValue) { |
| 151 | - $content = str_replace('{default_' . $configKey . '}', $configValue, $content); |
|
| 151 | + $content = str_replace('{default_'.$configKey.'}', $configValue, $content); |
|
| 152 | 152 | } |
| 153 | 153 | |
| 154 | - if($file->write($content) ) { |
|
| 154 | + if ($file->write($content)) { |
|
| 155 | 155 | return $this->redirect(array('action' => 'data')); |
| 156 | 156 | } else { |
| 157 | 157 | $this->Session->setFlash(__('Could not write database.php file.'), 'default', array('class' => 'error')); |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | $nextValue = $model->find('first', array( |
| 167 | 167 | 'fields' => sprintf('MAX(%s.%s) as max', $model->alias, $model->primaryKey), |
| 168 | 168 | )); |
| 169 | - $nextValue = empty($nextValue[0]['max']) ? 1 : $nextValue[0]['max'] + 1; |
|
| 169 | + $nextValue = empty($nextValue[0]['max']) ? 1 : $nextValue[0]['max'] + 1; |
|
| 170 | 170 | $sql = sprintf('alter sequence %s restart with %d', $db->getSequence($model), $nextValue); |
| 171 | 171 | $db->execute($sql); |
| 172 | 172 | } |
@@ -187,12 +187,12 @@ discard block |
||
| 187 | 187 | |
| 188 | 188 | $db = ConnectionManager::getDataSource('default'); |
| 189 | 189 | $brokenSequence = $db instanceof Postgres; |
| 190 | - if(!$db->isConnected()) { |
|
| 190 | + if (!$db->isConnected()) { |
|
| 191 | 191 | $this->Session->setFlash(__('Could not connect to database.'), 'default', array('class' => 'error')); |
| 192 | 192 | } else { |
| 193 | 193 | $schema = new CakeSchema(array('name'=>'app')); |
| 194 | 194 | $schema = $schema->load(); |
| 195 | - foreach($schema->tables as $table => $fields) { |
|
| 195 | + foreach ($schema->tables as $table => $fields) { |
|
| 196 | 196 | $create = $db->createSchema($schema, $table); |
| 197 | 197 | try { |
| 198 | 198 | $db->execute($create); |
@@ -203,10 +203,10 @@ discard block |
||
| 203 | 203 | } |
| 204 | 204 | } |
| 205 | 205 | |
| 206 | - $path = CakePlugin::path('Install') .DS. 'Config' .DS. 'Data' .DS; |
|
| 206 | + $path = CakePlugin::path('Install').DS.'Config'.DS.'Data'.DS; |
|
| 207 | 207 | $dataObjects = App::objects('class', $path); |
| 208 | 208 | foreach ($dataObjects as $data) { |
| 209 | - include($path . $data . '.php'); |
|
| 209 | + include($path.$data.'.php'); |
|
| 210 | 210 | $classVars = get_class_vars($data); |
| 211 | 211 | $modelAlias = substr($data, 0, -4); |
| 212 | 212 | $table = $classVars['table']; |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | )); |
| 220 | 220 | if (is_array($records) && count($records) > 0) { |
| 221 | 221 | $i = 1; |
| 222 | - foreach($records as $record) { |
|
| 222 | + foreach ($records as $record) { |
|
| 223 | 223 | if (!isset($record['id'])) { |
| 224 | 224 | $record['id'] = $i++; |
| 225 | 225 | } |
@@ -132,8 +132,7 @@ discard block |
||
| 132 | 132 | try { |
| 133 | 133 | @ConnectionManager::create('default', $config); |
| 134 | 134 | $db = ConnectionManager::getDataSource('default'); |
| 135 | - } |
|
| 136 | - catch (MissingConnectionException $e) { |
|
| 135 | + } catch (MissingConnectionException $e) { |
|
| 137 | 136 | $this->Session->setFlash(__('Could not connect to database: %s', $e->getMessage()), 'default', array('class' => 'error')); |
| 138 | 137 | $this->redirect(array('action' => 'database')); |
| 139 | 138 | } |
@@ -196,8 +195,7 @@ discard block |
||
| 196 | 195 | $create = $db->createSchema($schema, $table); |
| 197 | 196 | try { |
| 198 | 197 | $db->execute($create); |
| 199 | - } |
|
| 200 | - catch (PDOException $e) { |
|
| 198 | + } catch (PDOException $e) { |
|
| 201 | 199 | $this->Session->setFlash(__('Could not create table: %s', $e->getMessage()), 'default', array('class' => 'error')); |
| 202 | 200 | $this->redirect(array('action' => 'database')); |
| 203 | 201 | } |