Profile-Modify.php 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489
  1. <?php
  2. /**
  3. * This file has the primary job of showing and editing people's profiles.
  4. * It also allows the user to change some of their or another's preferences,
  5. * and such things
  6. *
  7. * Simple Machines Forum (SMF)
  8. *
  9. * @package SMF
  10. * @author Simple Machines http://www.simplemachines.org
  11. * @copyright 2011 Simple Machines
  12. * @license http://www.simplemachines.org/about/smf/license.php BSD
  13. *
  14. * @version 2.1 Alpha 1
  15. */
  16. if (!defined('SMF'))
  17. die('Hacking attempt...');
  18. /**
  19. * This defines every profile field known to man.
  20. *
  21. * @param bool $force_reload = false
  22. */
  23. function loadProfileFields($force_reload = false)
  24. {
  25. global $context, $profile_fields, $txt, $scripturl, $modSettings, $user_info, $old_profile, $smcFunc, $cur_profile, $language;
  26. // Don't load this twice!
  27. if (!empty($profile_fields) && !$force_reload)
  28. return;
  29. /* This horrific array defines all the profile fields in the whole world!
  30. In general each "field" has one array - the key of which is the database column name associated with said field. Each item
  31. can have the following attributes:
  32. string $type: The type of field this is - valid types are:
  33. - callback: This is a field which has its own callback mechanism for templating.
  34. - check: A simple checkbox.
  35. - hidden: This doesn't have any visual aspects but may have some validity.
  36. - password: A password box.
  37. - select: A select box.
  38. - text: A string of some description.
  39. string $label: The label for this item - default will be $txt[$key] if this isn't set.
  40. string $subtext: The subtext (Small label) for this item.
  41. int $size: Optional size for a text area.
  42. array $input_attr: An array of text strings to be added to the input box for this item.
  43. string $value: The value of the item. If not set $cur_profile[$key] is assumed.
  44. string $permission: Permission required for this item (Excluded _any/_own subfix which is applied automatically).
  45. function $input_validate: A runtime function which validates the element before going to the database. It is passed
  46. the relevant $_POST element if it exists and should be treated like a reference.
  47. Return types:
  48. - true: Element can be stored.
  49. - false: Skip this element.
  50. - a text string: An error occured - this is the error message.
  51. function $preload: A function that is used to load data required for this element to be displayed. Must return
  52. true to be displayed at all.
  53. string $cast_type: If set casts the element to a certain type. Valid types (bool, int, float).
  54. string $save_key: If the index of this element isn't the database column name it can be overriden
  55. with this string.
  56. bool $is_dummy: If set then nothing is acted upon for this element.
  57. bool $enabled: A test to determine whether this is even available - if not is unset.
  58. string $link_with: Key which links this field to an overall set.
  59. Note that all elements that have a custom input_validate must ensure they set the value of $cur_profile correct to enable
  60. the changes to be displayed correctly on submit of the form.
  61. */
  62. $profile_fields = array(
  63. 'aim' => array(
  64. 'type' => 'text',
  65. 'label' => $txt['aim'],
  66. 'subtext' => $txt['your_aim'],
  67. 'size' => 24,
  68. 'value' => strtr(empty($cur_profile['aim']) ? '' : $cur_profile['aim'], '+', ' '),
  69. 'permission' => 'profile_extra',
  70. 'input_validate' => create_function('&$value', '
  71. $value = strtr($value, \' \', \'+\');
  72. return true;
  73. '),
  74. ),
  75. 'avatar_choice' => array(
  76. 'type' => 'callback',
  77. 'callback_func' => 'avatar_select',
  78. // This handles the permissions too.
  79. 'preload' => 'profileLoadAvatarData',
  80. 'input_validate' => 'profileSaveAvatarData',
  81. 'save_key' => 'avatar',
  82. ),
  83. 'bday1' => array(
  84. 'type' => 'callback',
  85. 'callback_func' => 'birthdate',
  86. 'permission' => 'profile_extra',
  87. 'preload' => create_function('', '
  88. global $cur_profile, $context;
  89. // Split up the birthdate....
  90. list ($uyear, $umonth, $uday) = explode(\'-\', empty($cur_profile[\'birthdate\']) || $cur_profile[\'birthdate\'] == \'0001-01-01\' ? \'0000-00-00\' : $cur_profile[\'birthdate\']);
  91. $context[\'member\'][\'birth_date\'] = array(
  92. \'year\' => $uyear == \'0004\' ? \'0000\' : $uyear,
  93. \'month\' => $umonth,
  94. \'day\' => $uday,
  95. );
  96. return true;
  97. '),
  98. 'input_validate' => create_function('&$value', '
  99. global $profile_vars, $cur_profile;
  100. if (isset($_POST[\'bday2\'], $_POST[\'bday3\']) && $value > 0 && $_POST[\'bday2\'] > 0)
  101. {
  102. // Set to blank?
  103. if ((int) $_POST[\'bday3\'] == 1 && (int) $_POST[\'bday2\'] == 1 && (int) $value == 1)
  104. $value = \'0001-01-01\';
  105. else
  106. $value = checkdate($value, $_POST[\'bday2\'], $_POST[\'bday3\'] < 4 ? 4 : $_POST[\'bday3\']) ? sprintf(\'%04d-%02d-%02d\', $_POST[\'bday3\'] < 4 ? 4 : $_POST[\'bday3\'], $_POST[\'bday1\'], $_POST[\'bday2\']) : \'0001-01-01\';
  107. }
  108. else
  109. $value = \'0001-01-01\';
  110. $profile_vars[\'birthdate\'] = $value;
  111. $cur_profile[\'birthdate\'] = $value;
  112. return false;
  113. '),
  114. ),
  115. // Setting the birthdate the old style way?
  116. 'birthdate' => array(
  117. 'type' => 'hidden',
  118. 'permission' => 'profile_extra',
  119. 'input_validate' => create_function('&$value', '
  120. global $cur_profile;
  121. // @todo Should we check for this year and tell them they made a mistake :P? (based on coppa at least?)
  122. if (preg_match(\'/(\d{4})[\-\., ](\d{2})[\-\., ](\d{2})/\', $value, $dates) === 1)
  123. {
  124. $value = checkdate($dates[2], $dates[3], $dates[1] < 4 ? 4 : $dates[1]) ? sprintf(\'%04d-%02d-%02d\', $dates[1] < 4 ? 4 : $dates[1], $dates[2], $dates[3]) : \'0001-01-01\';
  125. return true;
  126. }
  127. else
  128. {
  129. $value = empty($cur_profile[\'birthdate\']) ? \'0001-01-01\' : $cur_profile[\'birthdate\'];
  130. return false;
  131. }
  132. '),
  133. ),
  134. 'date_registered' => array(
  135. 'type' => 'text',
  136. 'value' => empty($cur_profile['date_registered']) ? $txt['not_applicable'] : strftime('%Y-%m-%d', $cur_profile['date_registered'] + ($user_info['time_offset'] + $modSettings['time_offset']) * 3600),
  137. 'label' => $txt['date_registered'],
  138. 'log_change' => true,
  139. 'permission' => 'moderate_forum',
  140. 'input_validate' => create_function('&$value', '
  141. global $txt, $user_info, $modSettings, $cur_profile, $context;
  142. // Bad date! Go try again - please?
  143. if (($value = strtotime($value)) === -1)
  144. {
  145. $value = $cur_profile[\'date_registered\'];
  146. return $txt[\'invalid_registration\'] . \' \' . strftime(\'%d %b %Y \' . (strpos($user_info[\'time_format\'], \'%H\') !== false ? \'%I:%M:%S %p\' : \'%H:%M:%S\'), forum_time(false));
  147. }
  148. // As long as it doesn\'t equal "N/A"...
  149. elseif ($value != $txt[\'not_applicable\'] && $value != strtotime(strftime(\'%Y-%m-%d\', $cur_profile[\'date_registered\'] + ($user_info[\'time_offset\'] + $modSettings[\'time_offset\']) * 3600)))
  150. $value = $value - ($user_info[\'time_offset\'] + $modSettings[\'time_offset\']) * 3600;
  151. else
  152. $value = $cur_profile[\'date_registered\'];
  153. return true;
  154. '),
  155. ),
  156. 'email_address' => array(
  157. 'type' => 'text',
  158. 'label' => $txt['email'],
  159. 'subtext' => $txt['valid_email'],
  160. 'log_change' => true,
  161. 'permission' => 'profile_identity',
  162. 'input_validate' => create_function('&$value', '
  163. global $context, $old_profile, $context, $profile_vars, $sourcedir, $modSettings;
  164. if (strtolower($value) == strtolower($old_profile[\'email_address\']))
  165. return false;
  166. $isValid = profileValidateEmail($value, $context[\'id_member\']);
  167. // Do they need to revalidate? If so schedule the function!
  168. if ($isValid === true && !empty($modSettings[\'send_validation_onChange\']) && !allowedTo(\'moderate_forum\'))
  169. {
  170. require_once($sourcedir . \'/Subs-Members.php\');
  171. $profile_vars[\'validation_code\'] = generateValidationCode();
  172. $profile_vars[\'is_activated\'] = 2;
  173. $context[\'profile_execute_on_save\'][] = \'profileSendActivation\';
  174. unset($context[\'profile_execute_on_save\'][\'reload_user\']);
  175. }
  176. return $isValid;
  177. '),
  178. ),
  179. 'gender' => array(
  180. 'type' => 'select',
  181. 'cast_type' => 'int',
  182. 'options' => 'return array(0 => \'\', 1 => $txt[\'male\'], 2 => $txt[\'female\']);',
  183. 'label' => $txt['gender'],
  184. 'permission' => 'profile_extra',
  185. ),
  186. 'hide_email' => array(
  187. 'type' => 'check',
  188. 'value' => empty($cur_profile['hide_email']) ? true : false,
  189. 'label' => $txt['allow_user_email'],
  190. 'permission' => 'profile_identity',
  191. 'input_validate' => create_function('&$value', '
  192. $value = $value == 0 ? 1 : 0;
  193. return true;
  194. '),
  195. ),
  196. 'icq' => array(
  197. 'type' => 'text',
  198. 'label' => $txt['icq'],
  199. 'subtext' => $txt['your_icq'],
  200. 'size' => 24,
  201. 'permission' => 'profile_extra',
  202. // Need to make sure ICQ doesn't equal 0.
  203. 'input_validate' => create_function('&$value', '
  204. if (empty($value))
  205. $value = \'\';
  206. else
  207. $value = (int) $value;
  208. return true;
  209. '),
  210. ),
  211. // Selecting group membership is a complicated one so we treat it separate!
  212. 'id_group' => array(
  213. 'type' => 'callback',
  214. 'callback_func' => 'group_manage',
  215. 'permission' => 'manage_membergroups',
  216. 'preload' => 'profileLoadGroups',
  217. 'log_change' => true,
  218. 'input_validate' => 'profileSaveGroups',
  219. ),
  220. 'id_theme' => array(
  221. 'type' => 'callback',
  222. 'callback_func' => 'theme_pick',
  223. 'permission' => 'profile_extra',
  224. 'enabled' => $modSettings['theme_allow'] || allowedTo('admin_forum'),
  225. 'preload' => create_function('', '
  226. global $smcFunc, $context, $cur_profile, $txt;
  227. $request = $smcFunc[\'db_query\'](\'\', \'
  228. SELECT value
  229. FROM {db_prefix}themes
  230. WHERE id_theme = {int:id_theme}
  231. AND variable = {string:variable}
  232. LIMIT 1\', array(
  233. \'id_theme\' => $cur_profile[\'id_theme\'],
  234. \'variable\' => \'name\',
  235. )
  236. );
  237. list ($name) = $smcFunc[\'db_fetch_row\']($request);
  238. $smcFunc[\'db_free_result\']($request);
  239. $context[\'member\'][\'theme\'] = array(
  240. \'id\' => $cur_profile[\'id_theme\'],
  241. \'name\' => empty($cur_profile[\'id_theme\']) ? $txt[\'theme_forum_default\'] : $name
  242. );
  243. return true;
  244. '),
  245. 'input_validate' => create_function('&$value', '
  246. $value = (int) $value;
  247. return true;
  248. '),
  249. ),
  250. 'karma_good' => array(
  251. 'type' => 'callback',
  252. 'callback_func' => 'karma_modify',
  253. 'permission' => 'admin_forum',
  254. // Set karma_bad too!
  255. 'input_validate' => create_function('&$value', '
  256. global $profile_vars, $cur_profile;
  257. $value = (int) $value;
  258. if (isset($_POST[\'karma_bad\']))
  259. {
  260. $profile_vars[\'karma_bad\'] = $_POST[\'karma_bad\'] != \'\' ? (int) $_POST[\'karma_bad\'] : 0;
  261. $cur_profile[\'karma_bad\'] = $_POST[\'karma_bad\'] != \'\' ? (int) $_POST[\'karma_bad\'] : 0;
  262. }
  263. return true;
  264. '),
  265. 'preload' => create_function('', '
  266. global $context, $cur_profile;
  267. $context[\'member\'][\'karma\'][\'good\'] = $cur_profile[\'karma_good\'];
  268. $context[\'member\'][\'karma\'][\'bad\'] = $cur_profile[\'karma_bad\'];
  269. return true;
  270. '),
  271. 'enabled' => !empty($modSettings['karmaMode']),
  272. ),
  273. 'lngfile' => array(
  274. 'type' => 'select',
  275. 'options' => 'return $context[\'profile_languages\'];',
  276. 'label' => $txt['preferred_language'],
  277. 'permission' => 'profile_identity',
  278. 'preload' => 'profileLoadLanguages',
  279. 'enabled' => !empty($modSettings['userLanguage']),
  280. 'value' => empty($cur_profile['lngfile']) ? $language : $cur_profile['lngfile'],
  281. 'input_validate' => create_function('&$value', '
  282. global $context, $cur_profile;
  283. // Load the languages.
  284. profileLoadLanguages();
  285. if (isset($context[\'profile_languages\'][$value]))
  286. {
  287. if ($context[\'user\'][\'is_owner\'] && empty($context[\'password_auth_failed\']))
  288. $_SESSION[\'language\'] = $value;
  289. return true;
  290. }
  291. else
  292. {
  293. $value = $cur_profile[\'lngfile\'];
  294. return false;
  295. }
  296. '),
  297. ),
  298. 'location' => array(
  299. 'type' => 'text',
  300. 'label' => $txt['location'],
  301. 'log_change' => true,
  302. 'size' => 50,
  303. 'permission' => 'profile_extra',
  304. ),
  305. // The username is not always editable - so adjust it as such.
  306. 'member_name' => array(
  307. 'type' => allowedTo('admin_forum') && isset($_GET['changeusername']) ? 'text' : 'label',
  308. 'label' => $txt['username'],
  309. 'subtext' => allowedTo('admin_forum') && !isset($_GET['changeusername']) ? '(<a href="' . $scripturl . '?action=profile;u=' . $context['id_member'] . ';area=account;changeusername" style="font-style: italic;">' . $txt['username_change'] . '</a>)' : '',
  310. 'log_change' => true,
  311. 'permission' => 'profile_identity',
  312. 'prehtml' => allowedTo('admin_forum') && isset($_GET['changeusername']) ? '<div class="alert">' . $txt['username_warning'] . '</div>' : '',
  313. 'input_validate' => create_function('&$value', '
  314. global $sourcedir, $context, $user_info, $cur_profile;
  315. if (allowedTo(\'admin_forum\'))
  316. {
  317. // We\'ll need this...
  318. require_once($sourcedir . \'/Subs-Auth.php\');
  319. // Maybe they are trying to change their password as well?
  320. $resetPassword = true;
  321. if (isset($_POST[\'passwrd1\']) && $_POST[\'passwrd1\'] != \'\' && isset($_POST[\'passwrd2\']) && $_POST[\'passwrd1\'] == $_POST[\'passwrd2\'] && validatePassword($_POST[\'passwrd1\'], $value, array($cur_profile[\'real_name\'], $user_info[\'username\'], $user_info[\'name\'], $user_info[\'email\'])) == null)
  322. $resetPassword = false;
  323. // Do the reset... this will send them an email too.
  324. if ($resetPassword)
  325. resetPassword($context[\'id_member\'], $value);
  326. elseif ($value !== null)
  327. {
  328. validateUsername($context[\'id_member\'], $value);
  329. updateMemberData($context[\'id_member\'], array(\'member_name\' => $value));
  330. }
  331. }
  332. return false;
  333. '),
  334. ),
  335. 'msn' => array(
  336. 'type' => 'text',
  337. 'label' => $txt['msn'],
  338. 'subtext' => $txt['msn_email_address'],
  339. 'size' => 24,
  340. 'permission' => 'profile_extra',
  341. 'input_validate' => create_function('&$value', '
  342. global $cur_profile;
  343. // Make sure the msn one is an email address, not something like \'none\' :P.
  344. if ($value != \'\' && preg_match(\'~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\\\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~\', $value) == 0)
  345. {
  346. $value = $cur_profile[\'msn\'];
  347. return false;
  348. }
  349. return true;
  350. '),
  351. ),
  352. 'passwrd1' => array(
  353. 'type' => 'password',
  354. 'label' => $txt['choose_pass'],
  355. 'subtext' => $txt['password_strength'],
  356. 'size' => 20,
  357. 'value' => '',
  358. 'enabled' => empty($cur_profile['openid_uri']),
  359. 'permission' => 'profile_identity',
  360. 'save_key' => 'passwd',
  361. // Note this will only work if passwrd2 also exists!
  362. 'input_validate' => create_function('&$value', '
  363. global $sourcedir, $user_info, $smcFunc, $cur_profile;
  364. // If we didn\'t try it then ignore it!
  365. if ($value == \'\')
  366. return false;
  367. // Do the two entries for the password even match?
  368. if (!isset($_POST[\'passwrd2\']) || $value != $_POST[\'passwrd2\'])
  369. return \'bad_new_password\';
  370. // Let\'s get the validation function into play...
  371. require_once($sourcedir . \'/Subs-Auth.php\');
  372. $passwordErrors = validatePassword($value, $cur_profile[\'member_name\'], array($cur_profile[\'real_name\'], $user_info[\'username\'], $user_info[\'name\'], $user_info[\'email\']));
  373. // Were there errors?
  374. if ($passwordErrors != null)
  375. return \'password_\' . $passwordErrors;
  376. // Set up the new password variable... ready for storage.
  377. $value = sha1(strtolower($cur_profile[\'member_name\']) . un_htmlspecialchars($value));
  378. return true;
  379. '),
  380. ),
  381. 'passwrd2' => array(
  382. 'type' => 'password',
  383. 'label' => $txt['verify_pass'],
  384. 'enabled' => empty($cur_profile['openid_uri']),
  385. 'size' => 20,
  386. 'value' => '',
  387. 'permission' => 'profile_identity',
  388. 'is_dummy' => true,
  389. ),
  390. 'personal_text' => array(
  391. 'type' => 'text',
  392. 'label' => $txt['personal_text'],
  393. 'log_change' => true,
  394. 'input_attr' => array('maxlength="50"'),
  395. 'size' => 50,
  396. 'permission' => 'profile_extra',
  397. ),
  398. // This does ALL the pm settings
  399. 'pm_prefs' => array(
  400. 'type' => 'callback',
  401. 'callback_func' => 'pm_settings',
  402. 'permission' => 'pm_read',
  403. 'preload' => create_function('', '
  404. global $context, $cur_profile;
  405. $context[\'display_mode\'] = $cur_profile[\'pm_prefs\'] & 3;
  406. $context[\'send_email\'] = $cur_profile[\'pm_email_notify\'];
  407. $context[\'receive_from\'] = !empty($cur_profile[\'pm_receive_from\']) ? $cur_profile[\'pm_receive_from\'] : 0;
  408. return true;
  409. '),
  410. 'input_validate' => create_function('&$value', '
  411. global $cur_profile, $profile_vars;
  412. // Simple validate and apply the two "sub settings"
  413. $value = max(min($value, 2), 0);
  414. $cur_profile[\'pm_email_notify\'] = $profile_vars[\'pm_email_notify\'] = max(min((int) $_POST[\'pm_email_notify\'], 2), 0);
  415. $cur_profile[\'pm_receive_from\'] = $profile_vars[\'pm_receive_from\'] = max(min((int) $_POST[\'pm_receive_from\'], 4), 0);
  416. return true;
  417. '),
  418. ),
  419. 'posts' => array(
  420. 'type' => 'int',
  421. 'label' => $txt['profile_posts'],
  422. 'log_change' => true,
  423. 'size' => 7,
  424. 'permission' => 'moderate_forum',
  425. 'input_validate' => create_function('&$value', '
  426. $value = $value != \'\' ? strtr($value, array(\',\' => \'\', \'.\' => \'\', \' \' => \'\')) : 0;
  427. return true;
  428. '),
  429. ),
  430. 'real_name' => array(
  431. 'type' => !empty($modSettings['allow_editDisplayName']) || allowedTo('moderate_forum') ? 'text' : 'label',
  432. 'label' => $txt['name'],
  433. 'subtext' => $txt['display_name_desc'],
  434. 'log_change' => true,
  435. 'input_attr' => array('maxlength="60"'),
  436. 'permission' => 'profile_identity',
  437. 'enabled' => !empty($modSettings['allow_editDisplayName']) || allowedTo('moderate_forum'),
  438. 'input_validate' => create_function('&$value', '
  439. global $context, $smcFunc, $sourcedir, $cur_profile;
  440. $value = trim(preg_replace(\'~[\s]~\' . ($context[\'utf8\'] ? \'u\' : \'\'), \' \', $value));
  441. if (trim($value) == \'\')
  442. return \'no_name\';
  443. elseif ($smcFunc[\'strlen\']($value) > 60)
  444. return \'name_too_long\';
  445. elseif ($cur_profile[\'real_name\'] != $value)
  446. {
  447. require_once($sourcedir . \'/Subs-Members.php\');
  448. if (isReservedName($value, $context[\'id_member\']))
  449. return \'name_taken\';
  450. }
  451. return true;
  452. '),
  453. ),
  454. 'secret_question' => array(
  455. 'type' => 'text',
  456. 'label' => $txt['secret_question'],
  457. 'subtext' => $txt['secret_desc'],
  458. 'size' => 50,
  459. 'permission' => 'profile_identity',
  460. ),
  461. 'secret_answer' => array(
  462. 'type' => 'text',
  463. 'label' => $txt['secret_answer'],
  464. 'subtext' => $txt['secret_desc2'],
  465. 'size' => 20,
  466. 'postinput' => '<span class="smalltext" style="margin-left: 4ex;"><a href="' . $scripturl . '?action=helpadmin;help=secret_why_blank" onclick="return reqWin(this.href);">' . $txt['secret_why_blank'] . '</a></span>',
  467. 'value' => '',
  468. 'permission' => 'profile_identity',
  469. 'input_validate' => create_function('&$value', '
  470. $value = $value != \'\' ? md5($value) : \'\';
  471. return true;
  472. '),
  473. ),
  474. 'signature' => array(
  475. 'type' => 'callback',
  476. 'callback_func' => 'signature_modify',
  477. 'permission' => 'profile_extra',
  478. 'enabled' => substr($modSettings['signature_settings'], 0, 1) == 1,
  479. 'preload' => 'profileLoadSignatureData',
  480. 'input_validate' => 'profileValidateSignature',
  481. ),
  482. 'show_online' => array(
  483. 'type' => 'check',
  484. 'label' => $txt['show_online'],
  485. 'permission' => 'profile_identity',
  486. 'enabled' => !empty($modSettings['allow_hideOnline']) || allowedTo('moderate_forum'),
  487. ),
  488. 'smiley_set' => array(
  489. 'type' => 'callback',
  490. 'callback_func' => 'smiley_pick',
  491. 'enabled' => !empty($modSettings['smiley_sets_enable']),
  492. 'permission' => 'profile_extra',
  493. 'preload' => create_function('', '
  494. global $modSettings, $context, $txt, $cur_profile;
  495. $context[\'member\'][\'smiley_set\'][\'id\'] = empty($cur_profile[\'smiley_set\']) ? \'\' : $cur_profile[\'smiley_set\'];
  496. $context[\'smiley_sets\'] = explode(\',\', \'none,,\' . $modSettings[\'smiley_sets_known\']);
  497. $set_names = explode("\n", $txt[\'smileys_none\'] . "\n" . $txt[\'smileys_forum_board_default\'] . "\n" . $modSettings[\'smiley_sets_names\']);
  498. foreach ($context[\'smiley_sets\'] as $i => $set)
  499. {
  500. $context[\'smiley_sets\'][$i] = array(
  501. \'id\' => htmlspecialchars($set),
  502. \'name\' => htmlspecialchars($set_names[$i]),
  503. \'selected\' => $set == $context[\'member\'][\'smiley_set\'][\'id\']
  504. );
  505. if ($context[\'smiley_sets\'][$i][\'selected\'])
  506. $context[\'member\'][\'smiley_set\'][\'name\'] = $set_names[$i];
  507. }
  508. return true;
  509. '),
  510. 'input_validate' => create_function('&$value', '
  511. global $modSettings;
  512. $smiley_sets = explode(\',\', $modSettings[\'smiley_sets_known\']);
  513. if (!in_array($value, $smiley_sets) && $value != \'none\')
  514. $value = \'\';
  515. return true;
  516. '),
  517. ),
  518. // Pretty much a dummy entry - it populates all the theme settings.
  519. 'theme_settings' => array(
  520. 'type' => 'callback',
  521. 'callback_func' => 'theme_settings',
  522. 'permission' => 'profile_extra',
  523. 'is_dummy' => true,
  524. 'preload' => create_function('', '
  525. global $context, $user_info;
  526. loadLanguage(\'Settings\');
  527. $context[\'allow_no_censored\'] = false;
  528. if ($user_info[\'is_admin\'] || $context[\'user\'][\'is_owner\'])
  529. $context[\'allow_no_censored\'] = allowedTo(\'disable_censor\');
  530. return true;
  531. '),
  532. ),
  533. 'time_format' => array(
  534. 'type' => 'callback',
  535. 'callback_func' => 'timeformat_modify',
  536. 'permission' => 'profile_extra',
  537. 'preload' => create_function('', '
  538. global $context, $user_info, $txt, $cur_profile, $modSettings;
  539. $context[\'easy_timeformats\'] = array(
  540. array(\'format\' => \'\', \'title\' => $txt[\'timeformat_default\']),
  541. array(\'format\' => \'%B %d, %Y, %I:%M:%S %p\', \'title\' => $txt[\'timeformat_easy1\']),
  542. array(\'format\' => \'%B %d, %Y, %H:%M:%S\', \'title\' => $txt[\'timeformat_easy2\']),
  543. array(\'format\' => \'%Y-%m-%d, %H:%M:%S\', \'title\' => $txt[\'timeformat_easy3\']),
  544. array(\'format\' => \'%d %B %Y, %H:%M:%S\', \'title\' => $txt[\'timeformat_easy4\']),
  545. array(\'format\' => \'%d-%m-%Y, %H:%M:%S\', \'title\' => $txt[\'timeformat_easy5\'])
  546. );
  547. $context[\'member\'][\'time_format\'] = $cur_profile[\'time_format\'];
  548. $context[\'current_forum_time\'] = timeformat(time() - $user_info[\'time_offset\'] * 3600, false);
  549. $context[\'current_forum_time_js\'] = strftime(\'%Y,\' . ((int) strftime(\'%m\', time() + $modSettings[\'time_offset\'] * 3600) - 1) . \',%d,%H,%M,%S\', time() + $modSettings[\'time_offset\'] * 3600);
  550. $context[\'current_forum_time_hour\'] = (int) strftime(\'%H\', forum_time(false));
  551. return true;
  552. '),
  553. ),
  554. 'time_offset' => array(
  555. 'type' => 'callback',
  556. 'callback_func' => 'timeoffset_modify',
  557. 'permission' => 'profile_extra',
  558. 'preload' => create_function('', '
  559. global $context, $cur_profile;
  560. $context[\'member\'][\'time_offset\'] = $cur_profile[\'time_offset\'];
  561. return true;
  562. '),
  563. 'input_validate' => create_function('&$value', '
  564. // Validate the time_offset...
  565. $value = (float) strtr($value, \',\', \'.\');
  566. if ($value < -23.5 || $value > 23.5)
  567. return \'bad_offset\';
  568. return true;
  569. '),
  570. ),
  571. 'usertitle' => array(
  572. 'type' => 'text',
  573. 'label' => $txt['custom_title'],
  574. 'log_change' => true,
  575. 'size' => 50,
  576. 'permission' => 'profile_title',
  577. 'enabled' => !empty($modSettings['titlesEnable']),
  578. ),
  579. 'website_title' => array(
  580. 'type' => 'text',
  581. 'label' => $txt['website_title'],
  582. 'subtext' => $txt['include_website_url'],
  583. 'size' => 50,
  584. 'permission' => 'profile_extra',
  585. 'link_with' => 'website',
  586. ),
  587. 'website_url' => array(
  588. 'type' => 'text',
  589. 'label' => $txt['website_url'],
  590. 'subtext' => $txt['complete_url'],
  591. 'size' => 50,
  592. 'permission' => 'profile_extra',
  593. // Fix the URL...
  594. 'input_validate' => create_function('&$value', '
  595. if (strlen(trim($value)) > 0 && strpos($value, \'://\') === false)
  596. $value = \'http://\' . $value;
  597. if (strlen($value) < 8 || (substr($value, 0, 7) !== \'http://\' && substr($value, 0, 8) !== \'https://\'))
  598. $value = \'\';
  599. return true;
  600. '),
  601. 'link_with' => 'website',
  602. ),
  603. 'yim' => array(
  604. 'type' => 'text',
  605. 'label' => $txt['yim'],
  606. 'subtext' => $txt['your_yim'],
  607. 'size' => 24,
  608. 'input_attr' => array('maxlength="32"'),
  609. 'permission' => 'profile_extra',
  610. ),
  611. );
  612. $disabled_fields = !empty($modSettings['disabled_profile_fields']) ? explode(',', $modSettings['disabled_profile_fields']) : array();
  613. // For each of the above let's take out the bits which don't apply - to save memory and security!
  614. foreach ($profile_fields as $key => $field)
  615. {
  616. // Do we have permission to do this?
  617. if (isset($field['permission']) && !allowedTo(($context['user']['is_owner'] ? array($field['permission'] . '_own', $field['permission'] . '_any') : $field['permission'] . '_any')) && !allowedTo($field['permission']))
  618. unset($profile_fields[$key]);
  619. // Is it enabled?
  620. if (isset($field['enabled']) && !$field['enabled'])
  621. unset($profile_fields[$key]);
  622. // Is it specifically disabled?
  623. if (in_array($key, $disabled_fields) || (isset($field['link_with']) && in_array($field['link_with'], $disabled_fields)))
  624. unset($profile_fields[$key]);
  625. }
  626. }
  627. /**
  628. * Setup the context for a page load!
  629. *
  630. * @param array $fields
  631. */
  632. function setupProfileContext($fields)
  633. {
  634. global $profile_fields, $context, $cur_profile, $smcFunc, $txt;
  635. // Make sure we have this!
  636. loadProfileFields(true);
  637. // First check for any linked sets.
  638. foreach ($profile_fields as $key => $field)
  639. if (isset($field['link_with']) && in_array($field['link_with'], $fields))
  640. $fields[] = $key;
  641. // Some default bits.
  642. $context['profile_prehtml'] = '';
  643. $context['profile_posthtml'] = '';
  644. $context['profile_javascript'] = '';
  645. $context['profile_onsubmit_javascript'] = '';
  646. $i = 0;
  647. $last_type = '';
  648. foreach ($fields as $key => $field)
  649. {
  650. if (isset($profile_fields[$field]))
  651. {
  652. // Shortcut.
  653. $cur_field = &$profile_fields[$field];
  654. // Does it have a preload and does that preload succeed?
  655. if (isset($cur_field['preload']) && !$cur_field['preload']())
  656. continue;
  657. // If this is anything but complex we need to do more cleaning!
  658. if ($cur_field['type'] != 'callback' && $cur_field['type'] != 'hidden')
  659. {
  660. if (!isset($cur_field['label']))
  661. $cur_field['label'] = isset($txt[$field]) ? $txt[$field] : $field;
  662. // Everything has a value!
  663. if (!isset($cur_field['value']))
  664. {
  665. $cur_field['value'] = isset($cur_profile[$field]) ? $cur_profile[$field] : '';
  666. }
  667. // Any input attributes?
  668. $cur_field['input_attr'] = !empty($cur_field['input_attr']) ? implode(',', $cur_field['input_attr']) : '';
  669. }
  670. // Was there an error with this field on posting?
  671. if (isset($context['profile_errors'][$field]))
  672. $cur_field['is_error'] = true;
  673. // Any javascript stuff?
  674. if (!empty($cur_field['js_submit']))
  675. $context['profile_onsubmit_javascript'] .= $cur_field['js_submit'];
  676. if (!empty($cur_field['js']))
  677. $context['profile_javascript'] .= $cur_field['js'];
  678. // Any template stuff?
  679. if (!empty($cur_field['prehtml']))
  680. $context['profile_prehtml'] .= $cur_field['prehtml'];
  681. if (!empty($cur_field['posthtml']))
  682. $context['profile_posthtml'] .= $cur_field['posthtml'];
  683. // Finally put it into context?
  684. if ($cur_field['type'] != 'hidden')
  685. {
  686. $last_type = $cur_field['type'];
  687. $context['profile_fields'][$field] = &$profile_fields[$field];
  688. }
  689. }
  690. // Bodge in a line break - without doing two in a row ;)
  691. elseif ($field == 'hr' && $last_type != 'hr' && $last_type != '')
  692. {
  693. $last_type = 'hr';
  694. $context['profile_fields'][$i++]['type'] = 'hr';
  695. }
  696. }
  697. // Free up some memory.
  698. unset($profile_fields);
  699. }
  700. /**
  701. * Save the profile changes.
  702. */
  703. function saveProfileFields()
  704. {
  705. global $profile_fields, $profile_vars, $context, $old_profile, $post_errors, $sourcedir, $modSettings, $cur_profile, $smcFunc;
  706. // Load them up.
  707. loadProfileFields();
  708. // This makes things easier...
  709. $old_profile = $cur_profile;
  710. // This allows variables to call activities when they save - by default just to reload their settings
  711. $context['profile_execute_on_save'] = array();
  712. if ($context['user']['is_owner'])
  713. $context['profile_execute_on_save']['reload_user'] = 'profileReloadUser';
  714. // Assume we log nothing.
  715. $context['log_changes'] = array();
  716. // Cycle through the profile fields working out what to do!
  717. foreach ($profile_fields as $key => $field)
  718. {
  719. if (!isset($_POST[$key]) || !empty($field['is_dummy']))
  720. continue;
  721. // What gets updated?
  722. $db_key = isset($field['save_key']) ? $field['save_key'] : $key;
  723. // Right - we have something that is enabled, we can act upon and has a value posted to it. Does it have a validation function?
  724. if (isset($field['input_validate']))
  725. {
  726. $is_valid = $field['input_validate']($_POST[$key]);
  727. // An error occured - set it as such!
  728. if ($is_valid !== true)
  729. {
  730. // Is this an actual error?
  731. if ($is_valid !== false)
  732. {
  733. $post_errors[$key] = $is_valid;
  734. $profile_fields[$key]['is_error'] = $is_valid;
  735. }
  736. // Retain the old value.
  737. $cur_profile[$key] = $_POST[$key];
  738. continue;
  739. }
  740. }
  741. // Are we doing a cast?
  742. $field['cast_type'] = empty($field['cast_type']) ? $field['type'] : $field['cast_type'];
  743. // Finally, clean up certain types.
  744. if ($field['cast_type'] == 'int')
  745. $_POST[$key] = (int) $_POST[$key];
  746. elseif ($field['cast_type'] == 'float')
  747. $_POST[$key] = (float) $_POST[$key];
  748. elseif ($field['cast_type'] == 'check')
  749. $_POST[$key] = !empty($_POST[$key]) ? 1 : 0;
  750. // If we got here we're doing OK.
  751. if ($field['type'] != 'hidden' && (!isset($old_profile[$key]) || $_POST[$key] != $old_profile[$key]))
  752. {
  753. // Set the save variable.
  754. $profile_vars[$db_key] = $_POST[$key];
  755. // And update the user profile.
  756. $cur_profile[$key] = $_POST[$key];
  757. // Are we logging it?
  758. if (!empty($field['log_change']) && isset($old_profile[$key]))
  759. $context['log_changes'][$key] = array(
  760. 'previous' => $old_profile[$key],
  761. 'new' => $_POST[$key],
  762. );
  763. }
  764. // Logging group changes are a bit different...
  765. if ($key == 'id_group' && $field['log_change'])
  766. {
  767. profileLoadGroups();
  768. // Any changes to primary group?
  769. if ($_POST['id_group'] != $old_profile['id_group'])
  770. {
  771. $context['log_changes']['id_group'] = array(
  772. 'previous' => !empty($old_profile[$key]) && isset($context['member_groups'][$old_profile[$key]]) ? $context['member_groups'][$old_profile[$key]]['name'] : '',
  773. 'new' => !empty($_POST[$key]) && isset($context['member_groups'][$_POST[$key]]) ? $context['member_groups'][$_POST[$key]]['name'] : '',
  774. );
  775. }
  776. // Prepare additional groups for comparison.
  777. $additional_groups = array(
  778. 'previous' => !empty($old_profile['additional_groups']) ? explode(',', $old_profile['additional_groups']) : array(),
  779. 'new' => !empty($_POST['additional_groups']) ? array_diff($_POST['additional_groups'], array(0)) : array(),
  780. );
  781. sort($additional_groups['previous']);
  782. sort($additional_groups['new']);
  783. // What about additional groups?
  784. if ($additional_groups['previous'] != $additional_groups['new'])
  785. {
  786. foreach ($additional_groups as $type => $groups)
  787. {
  788. foreach ($groups as $id => $group)
  789. {
  790. if (isset($context['member_groups'][$group]))
  791. $additional_groups[$type][$id] = $context['member_groups'][$group]['name'];
  792. else
  793. unset($additional_groups[$type][$id]);
  794. }
  795. $additional_groups[$type] = implode(', ', $additional_groups[$type]);
  796. }
  797. $context['log_changes']['additional_groups'] = $additional_groups;
  798. }
  799. }
  800. }
  801. // @todo Temporary
  802. if ($context['user']['is_owner'])
  803. $changeOther = allowedTo(array('profile_extra_any', 'profile_extra_own'));
  804. else
  805. $changeOther = allowedTo('profile_extra_any');
  806. if ($changeOther && empty($post_errors))
  807. {
  808. makeThemeChanges($context['id_member'], isset($_POST['id_theme']) ? (int) $_POST['id_theme'] : $old_profile['id_theme']);
  809. if (!empty($_REQUEST['sa']))
  810. makeCustomFieldChanges($context['id_member'], $_REQUEST['sa'], false);
  811. }
  812. // Free memory!
  813. unset($profile_fields);
  814. }
  815. /**
  816. * Save the profile changes
  817. *
  818. * @param array &$profile_variables
  819. * @param array &$post_errors
  820. * @param int $memID id_member
  821. */
  822. function saveProfileChanges(&$profile_vars, &$post_errors, $memID)
  823. {
  824. global $user_info, $txt, $modSettings, $user_profile;
  825. global $context, $settings, $sourcedir;
  826. global $smcFunc;
  827. // These make life easier....
  828. $old_profile = &$user_profile[$memID];
  829. // Permissions...
  830. if ($context['user']['is_owner'])
  831. {
  832. $changeIdentity = allowedTo(array('profile_identity_any', 'profile_identity_own'));
  833. $changeOther = allowedTo(array('profile_extra_any', 'profile_extra_own'));
  834. }
  835. else
  836. {
  837. $changeIdentity = allowedTo('profile_identity_any');
  838. $changeOther = allowedTo('profile_extra_any');
  839. }
  840. // Arrays of all the changes - makes things easier.
  841. $profile_bools = array(
  842. 'notify_announcements', 'notify_send_body',
  843. );
  844. $profile_ints = array(
  845. 'notify_regularity',
  846. 'notify_types',
  847. );
  848. $profile_floats = array(
  849. );
  850. $profile_strings = array(
  851. 'buddy_list',
  852. 'ignore_boards',
  853. );
  854. if (isset($_POST['sa']) && $_POST['sa'] == 'ignoreboards' && empty($_POST['ignore_brd']))
  855. $_POST['ignore_brd'] = array();
  856. unset($_POST['ignore_boards']); // Whatever it is set to is a dirty fithy thing. Kinda like our minds.
  857. if (isset($_POST['ignore_brd']))
  858. {
  859. if (!is_array($_POST['ignore_brd']))
  860. $_POST['ignore_brd'] = array ($_POST['ignore_brd']);
  861. foreach ($_POST['ignore_brd'] as $k => $d)
  862. {
  863. $d = (int) $d;
  864. if ($d != 0)
  865. $_POST['ignore_brd'][$k] = $d;
  866. else
  867. unset($_POST['ignore_brd'][$k]);
  868. }
  869. $_POST['ignore_boards'] = implode(',', $_POST['ignore_brd']);
  870. unset($_POST['ignore_brd']);
  871. }
  872. // Here's where we sort out all the 'other' values...
  873. if ($changeOther)
  874. {
  875. makeThemeChanges($memID, isset($_POST['id_theme']) ? (int) $_POST['id_theme'] : $old_profile['id_theme']);
  876. //makeAvatarChanges($memID, $post_errors);
  877. makeNotificationChanges($memID);
  878. if (!empty($_REQUEST['sa']))
  879. makeCustomFieldChanges($memID, $_REQUEST['sa'], false);
  880. foreach ($profile_bools as $var)
  881. if (isset($_POST[$var]))
  882. $profile_vars[$var] = empty($_POST[$var]) ? '0' : '1';
  883. foreach ($profile_ints as $var)
  884. if (isset($_POST[$var]))
  885. $profile_vars[$var] = $_POST[$var] != '' ? (int) $_POST[$var] : '';
  886. foreach ($profile_floats as $var)
  887. if (isset($_POST[$var]))
  888. $profile_vars[$var] = (float) $_POST[$var];
  889. foreach ($profile_strings as $var)
  890. if (isset($_POST[$var]))
  891. $profile_vars[$var] = $_POST[$var];
  892. }
  893. }
  894. /**
  895. * Make any theme changes that are sent with the profile.
  896. *
  897. * @param int $memID
  898. * @param int $id_theme
  899. */
  900. function makeThemeChanges($memID, $id_theme)
  901. {
  902. global $modSettings, $smcFunc, $context, $user_info;
  903. $reservedVars = array(
  904. 'actual_theme_url',
  905. 'actual_images_url',
  906. 'base_theme_dir',
  907. 'base_theme_url',
  908. 'default_images_url',
  909. 'default_theme_dir',
  910. 'default_theme_url',
  911. 'default_template',
  912. 'images_url',
  913. 'number_recent_posts',
  914. 'smiley_sets_default',
  915. 'theme_dir',
  916. 'theme_id',
  917. 'theme_layers',
  918. 'theme_templates',
  919. 'theme_url',
  920. );
  921. // Can't change reserved vars.
  922. if ((isset($_POST['options']) && count(array_intersect(array_keys($_POST['options']), $reservedVars)) != 0) || (isset($_POST['default_options']) && count(array_intersect(array_keys($_POST['default_options']), $reservedVars)) != 0))
  923. fatal_lang_error('no_access', false);
  924. // Don't allow any overriding of custom fields with default or non-default options.
  925. $request = $smcFunc['db_query']('', '
  926. SELECT col_name
  927. FROM {db_prefix}custom_fields
  928. WHERE active = {int:is_active}',
  929. array(
  930. 'is_active' => 1,
  931. )
  932. );
  933. $custom_fields = array();
  934. while ($row = $smcFunc['db_fetch_assoc']($request))
  935. $custom_fields[] = $row['col_name'];
  936. $smcFunc['db_free_result']($request);
  937. // These are the theme changes...
  938. $themeSetArray = array();
  939. if (isset($_POST['options']) && is_array($_POST['options']))
  940. {
  941. foreach ($_POST['options'] as $opt => $val)
  942. {
  943. if (in_array($opt, $custom_fields))
  944. continue;
  945. // These need to be controlled.
  946. if ($opt == 'topics_per_page' || $opt == 'messages_per_page')
  947. $val = max(0, min($val, 50));
  948. // We don't set this per theme anymore.
  949. elseif ($opt == 'allow_no_censored')
  950. continue;
  951. $themeSetArray[] = array($memID, $id_theme, $opt, is_array($val) ? implode(',', $val) : $val);
  952. }
  953. }
  954. $erase_options = array();
  955. if (isset($_POST['default_options']) && is_array($_POST['default_options']))
  956. foreach ($_POST['default_options'] as $opt => $val)
  957. {
  958. if (in_array($opt, $custom_fields))
  959. continue;
  960. // These need to be controlled.
  961. if ($opt == 'topics_per_page' || $opt == 'messages_per_page')
  962. $val = max(0, min($val, 50));
  963. // Only let admins and owners change the censor.
  964. elseif ($opt == 'allow_no_censored' && !$user_info['is_admin'] && !$context['user']['is_owner'])
  965. continue;
  966. $themeSetArray[] = array($memID, 1, $opt, is_array($val) ? implode(',', $val) : $val);
  967. $erase_options[] = $opt;
  968. }
  969. // If themeSetArray isn't still empty, send it to the database.
  970. if (empty($context['password_auth_failed']))
  971. {
  972. if (!empty($themeSetArray))
  973. {
  974. $smcFunc['db_insert']('replace',
  975. '{db_prefix}themes',
  976. array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  977. $themeSetArray,
  978. array('id_member', 'id_theme', 'variable')
  979. );
  980. }
  981. if (!empty($erase_options))
  982. {
  983. $smcFunc['db_query']('', '
  984. DELETE FROM {db_prefix}themes
  985. WHERE id_theme != {int:id_theme}
  986. AND variable IN ({array_string:erase_variables})
  987. AND id_member = {int:id_member}',
  988. array(
  989. 'id_theme' => 1,
  990. 'id_member' => $memID,
  991. 'erase_variables' => $erase_options
  992. )
  993. );
  994. }
  995. $themes = explode(',', $modSettings['knownThemes']);
  996. foreach ($themes as $t)
  997. cache_put_data('theme_settings-' . $t . ':' . $memID, null, 60);
  998. }
  999. }
  1000. /**
  1001. * Make any notification changes that need to be made.
  1002. *
  1003. * @param int $memID id_member
  1004. */
  1005. function makeNotificationChanges($memID)
  1006. {
  1007. global $smcFunc;
  1008. // Update the boards they are being notified on.
  1009. if (isset($_POST['edit_notify_boards']) && !empty($_POST['notify_boards']))
  1010. {
  1011. // Make sure only integers are deleted.
  1012. foreach ($_POST['notify_boards'] as $index => $id)
  1013. $_POST['notify_boards'][$index] = (int) $id;
  1014. // id_board = 0 is reserved for topic notifications.
  1015. $_POST['notify_boards'] = array_diff($_POST['notify_boards'], array(0));
  1016. $smcFunc['db_query']('', '
  1017. DELETE FROM {db_prefix}log_notify
  1018. WHERE id_board IN ({array_int:board_list})
  1019. AND id_member = {int:selected_member}',
  1020. array(
  1021. 'board_list' => $_POST['notify_boards'],
  1022. 'selected_member' => $memID,
  1023. )
  1024. );
  1025. }
  1026. // We are editing topic notifications......
  1027. elseif (isset($_POST['edit_notify_topics']) && !empty($_POST['notify_topics']))
  1028. {
  1029. foreach ($_POST['notify_topics'] as $index => $id)
  1030. $_POST['notify_topics'][$index] = (int) $id;
  1031. // Make sure there are no zeros left.
  1032. $_POST['notify_topics'] = array_diff($_POST['notify_topics'], array(0));
  1033. $smcFunc['db_query']('', '
  1034. DELETE FROM {db_prefix}log_notify
  1035. WHERE id_topic IN ({array_int:topic_list})
  1036. AND id_member = {int:selected_member}',
  1037. array(
  1038. 'topic_list' => $_POST['notify_topics'],
  1039. 'selected_member' => $memID,
  1040. )
  1041. );
  1042. }
  1043. }
  1044. /**
  1045. * Save any changes to the custom profile fields
  1046. *
  1047. * @param int $memID
  1048. * @param string $area
  1049. * @param bool $sanitize = true
  1050. */
  1051. function makeCustomFieldChanges($memID, $area, $sanitize = true)
  1052. {
  1053. global $context, $smcFunc, $user_profile, $user_info, $modSettings, $sourcedir;
  1054. if ($sanitize && isset($_POST['customfield']))
  1055. $_POST['customfield'] = htmlspecialchars__recursive($_POST['customfield']);
  1056. $where = $area == 'register' ? 'show_reg != 0' : 'show_profile = {string:area}';
  1057. // Load the fields we are saving too - make sure we save valid data (etc).
  1058. $request = $smcFunc['db_query']('', '
  1059. SELECT col_name, field_name, field_desc, field_type, field_length, field_options, default_value, show_reg, mask, private
  1060. FROM {db_prefix}custom_fields
  1061. WHERE ' . $where . '
  1062. AND active = {int:is_active}',
  1063. array(
  1064. 'is_active' => 1,
  1065. 'area' => $area,
  1066. )
  1067. );
  1068. $changes = array();
  1069. $log_changes = array();
  1070. while ($row = $smcFunc['db_fetch_assoc']($request))
  1071. {
  1072. /* This means don't save if:
  1073. - The user is NOT an admin.
  1074. - The data is not freely viewable and editable by users.
  1075. - The data is not invisible to users but editable by the owner (or if it is the user is not the owner)
  1076. - The area isn't registration, and if it is that the field is not suppossed to be shown there.
  1077. */
  1078. if ($row['private'] != 0 && !allowedTo('admin_forum') && ($memID != $user_info['id'] || $row['private'] != 2) && ($area != 'register' || $row['show_reg'] == 0))
  1079. continue;
  1080. // Validate the user data.
  1081. if ($row['field_type'] == 'check')
  1082. $value = isset($_POST['customfield'][$row['col_name']]) ? 1 : 0;
  1083. elseif ($row['field_type'] == 'select' || $row['field_type'] == 'radio')
  1084. {
  1085. $value = $row['default_value'];
  1086. foreach (explode(',', $row['field_options']) as $k => $v)
  1087. if (isset($_POST['customfield'][$row['col_name']]) && $_POST['customfield'][$row['col_name']] == $k)
  1088. $value = $v;
  1089. }
  1090. // Otherwise some form of text!
  1091. else
  1092. {
  1093. $value = isset($_POST['customfield'][$row['col_name']]) ? $_POST['customfield'][$row['col_name']] : '';
  1094. if ($row['field_length'])
  1095. $value = $smcFunc['substr']($value, 0, $row['field_length']);
  1096. // Any masks?
  1097. if ($row['field_type'] == 'text' && !empty($row['mask']) && $row['mask'] != 'none')
  1098. {
  1099. // @todo We never error on this - just ignore it at the moment...
  1100. if ($row['mask'] == 'email' && (preg_match('~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~', $value) === 0 || strlen($value) > 255))
  1101. $value = '';
  1102. elseif ($row['mask'] == 'number')
  1103. {
  1104. $value = (int) $value;
  1105. }
  1106. elseif (substr($row['mask'], 0, 5) == 'regex' && preg_match(substr($row['mask'], 5), $value) === 0)
  1107. $value = '';
  1108. }
  1109. }
  1110. // Did it change?
  1111. if (!isset($user_profile[$memID]['options'][$row['col_name']]) || $user_profile[$memID]['options'][$row['col_name']] != $value)
  1112. {
  1113. $log_changes[] = array(
  1114. 'action' => 'customfield_' . $row['col_name'],
  1115. 'log_type' => 'user',
  1116. 'extra' => array(
  1117. 'previous' => !empty($user_profile[$memID]['options'][$row['col_name']]) ? $user_profile[$memID]['options'][$row['col_name']] : '',
  1118. 'new' => $value,
  1119. 'applicator' => $user_info['id'],
  1120. 'member_affected' => $memID,
  1121. ),
  1122. );
  1123. $changes[] = array(1, $row['col_name'], $value, $memID);
  1124. $user_profile[$memID]['options'][$row['col_name']] = $value;
  1125. }
  1126. }
  1127. $smcFunc['db_free_result']($request);
  1128. // Make those changes!
  1129. if (!empty($changes) && empty($context['password_auth_failed']))
  1130. {
  1131. $smcFunc['db_insert']('replace',
  1132. '{db_prefix}themes',
  1133. array('id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534', 'id_member' => 'int'),
  1134. $changes,
  1135. array('id_theme', 'variable', 'id_member')
  1136. );
  1137. if (!empty($log_changes) && !empty($modSettings['modlog_enabled']))
  1138. {
  1139. require_once($sourcedir . '/Logging.php');
  1140. foreach ($log_changes as $log_change)
  1141. logAction($log_change['action'], $log_change['extra'], $log_change['log_type']);
  1142. }
  1143. }
  1144. }
  1145. /**
  1146. * Show all the users buddies, as well as a add/delete interface.
  1147. *
  1148. * @param int $memID id_member
  1149. */
  1150. function editBuddyIgnoreLists($memID)
  1151. {
  1152. global $sourcedir, $context, $txt, $scripturl, $modSettings, $user_profile;
  1153. // Do a quick check to ensure people aren't getting here illegally!
  1154. if (!$context['user']['is_owner'] || empty($modSettings['enable_buddylist']))
  1155. fatal_lang_error('no_access', false);
  1156. // Can we email the user direct?
  1157. $context['can_moderate_forum'] = allowedTo('moderate_forum');
  1158. $subActions = array(
  1159. 'buddies' => array('editBuddies', $txt['editBuddies']),
  1160. 'ignore' => array('editIgnoreList', $txt['editIgnoreList']),
  1161. );
  1162. $context['list_area'] = isset($_GET['sa']) && isset($subActions[$_GET['sa']]) ? $_GET['sa'] : 'buddies';
  1163. // Create the tabs for the template.
  1164. $context[$context['profile_menu_name']]['tab_data'] = array(
  1165. 'title' => $txt['editBuddyIgnoreLists'],
  1166. 'description' => $txt['buddy_ignore_desc'],
  1167. 'icon' => 'profile_sm.gif',
  1168. 'tabs' => array(
  1169. 'buddies' => array(),
  1170. 'ignore' => array(),
  1171. ),
  1172. );
  1173. // Pass on to the actual function.
  1174. $context['sub_template'] = $subActions[$context['list_area']][0];
  1175. $subActions[$context['list_area']][0]($memID);
  1176. }
  1177. /**
  1178. * Show all the users buddies, as well as a add/delete interface.
  1179. *
  1180. * @param int $memID id_member
  1181. */
  1182. function editBuddies($memID)
  1183. {
  1184. global $txt, $scripturl, $modSettings;
  1185. global $context, $user_profile, $memberContext, $smcFunc;
  1186. // For making changes!
  1187. $buddiesArray = explode(',', $user_profile[$memID]['buddy_list']);
  1188. foreach ($buddiesArray as $k => $dummy)
  1189. if ($dummy == '')
  1190. unset($buddiesArray[$k]);
  1191. // Removing a buddy?
  1192. if (isset($_GET['remove']))
  1193. {
  1194. checkSession('get');
  1195. // Heh, I'm lazy, do it the easy way...
  1196. foreach ($buddiesArray as $key => $buddy)
  1197. if ($buddy == (int) $_GET['remove'])
  1198. unset($buddiesArray[$key]);
  1199. // Make the changes.
  1200. $user_profile[$memID]['buddy_list'] = implode(',', $buddiesArray);
  1201. updateMemberData($memID, array('buddy_list' => $user_profile[$memID]['buddy_list']));
  1202. // Redirect off the page because we don't like all this ugly query stuff to stick in the history.
  1203. redirectexit('action=profile;area=lists;sa=buddies;u=' . $memID);
  1204. }
  1205. elseif (isset($_POST['new_buddy']))
  1206. {
  1207. checkSession();
  1208. // Prepare the string for extraction...
  1209. $_POST['new_buddy'] = strtr($smcFunc['htmlspecialchars']($_POST['new_buddy'], ENT_QUOTES), array('&quot;' => '"'));
  1210. preg_match_all('~"([^"]+)"~', $_POST['new_buddy'], $matches);
  1211. $new_buddies = array_unique(array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $_POST['new_buddy']))));
  1212. foreach ($new_buddies as $k => $dummy)
  1213. {
  1214. $new_buddies[$k] = strtr(trim($new_buddies[$k]), array('\'' => '&#039;'));
  1215. if (strlen($new_buddies[$k]) == 0 || in_array($new_buddies[$k], array($user_profile[$memID]['member_name'], $user_profile[$memID]['real_name'])))
  1216. unset($new_buddies[$k]);
  1217. }
  1218. if (!empty($new_buddies))
  1219. {
  1220. // Now find out the id_member of the buddy.
  1221. $request = $smcFunc['db_query']('', '
  1222. SELECT id_member
  1223. FROM {db_prefix}members
  1224. WHERE member_name IN ({array_string:new_buddies}) OR real_name IN ({array_string:new_buddies})
  1225. LIMIT {int:count_new_buddies}',
  1226. array(
  1227. 'new_buddies' => $new_buddies,
  1228. 'count_new_buddies' => count($new_buddies),
  1229. )
  1230. );
  1231. // Add the new member to the buddies array.
  1232. while ($row = $smcFunc['db_fetch_assoc']($request))
  1233. $buddiesArray[] = (int) $row['id_member'];
  1234. $smcFunc['db_free_result']($request);
  1235. // Now update the current users buddy list.
  1236. $user_profile[$memID]['buddy_list'] = implode(',', $buddiesArray);
  1237. updateMemberData($memID, array('buddy_list' => $user_profile[$memID]['buddy_list']));
  1238. }
  1239. // Back to the buddy list!
  1240. redirectexit('action=profile;area=lists;sa=buddies;u=' . $memID);
  1241. }
  1242. // Get all the users "buddies"...
  1243. $buddies = array();
  1244. if (!empty($buddiesArray))
  1245. {
  1246. $result = $smcFunc['db_query']('', '
  1247. SELECT id_member
  1248. FROM {db_prefix}members
  1249. WHERE id_member IN ({array_int:buddy_list})
  1250. ORDER BY real_name
  1251. LIMIT {int:buddy_list_count}',
  1252. array(
  1253. 'buddy_list' => $buddiesArray,
  1254. 'buddy_list_count' => substr_count($user_profile[$memID]['buddy_list'], ',') + 1,
  1255. )
  1256. );
  1257. while ($row = $smcFunc['db_fetch_assoc']($result))
  1258. $buddies[] = $row['id_member'];
  1259. $smcFunc['db_free_result']($result);
  1260. }
  1261. $context['buddy_count'] = count($buddies);
  1262. // Load all the members up.
  1263. loadMemberData($buddies, false, 'profile');
  1264. // Setup the context for each buddy.
  1265. $context['buddies'] = array();
  1266. foreach ($buddies as $buddy)
  1267. {
  1268. loadMemberContext($buddy);
  1269. $context['buddies'][$buddy] = $memberContext[$buddy];
  1270. }
  1271. }
  1272. /**
  1273. * Allows the user to view their ignore list, as well as the option to manage members on it.
  1274. *
  1275. * @param int $memID id_member
  1276. */
  1277. function editIgnoreList($memID)
  1278. {
  1279. global $txt, $scripturl, $modSettings;
  1280. global $context, $user_profile, $memberContext, $smcFunc;
  1281. // For making changes!
  1282. $ignoreArray = explode(',', $user_profile[$memID]['pm_ignore_list']);
  1283. foreach ($ignoreArray as $k => $dummy)
  1284. if ($dummy == '')
  1285. unset($ignoreArray[$k]);
  1286. // Removing a member from the ignore list?
  1287. if (isset($_GET['remove']))
  1288. {
  1289. checkSession('get');
  1290. // Heh, I'm lazy, do it the easy way...
  1291. foreach ($ignoreArray as $key => $id_remove)
  1292. if ($id_remove == (int) $_GET['remove'])
  1293. unset($ignoreArray[$key]);
  1294. // Make the changes.
  1295. $user_profile[$memID]['pm_ignore_list'] = implode(',', $ignoreArray);
  1296. updateMemberData($memID, array('pm_ignore_list' => $user_profile[$memID]['pm_ignore_list']));
  1297. // Redirect off the page because we don't like all this ugly query stuff to stick in the history.
  1298. redirectexit('action=profile;area=lists;sa=ignore;u=' . $memID);
  1299. }
  1300. elseif (isset($_POST['new_ignore']))
  1301. {
  1302. checkSession();
  1303. // Prepare the string for extraction...
  1304. $_POST['new_ignore'] = strtr($smcFunc['htmlspecialchars']($_POST['new_ignore'], ENT_QUOTES), array('&quot;' => '"'));
  1305. preg_match_all('~"([^"]+)"~', $_POST['new_ignore'], $matches);
  1306. $new_entries = array_unique(array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $_POST['new_ignore']))));
  1307. foreach ($new_entries as $k => $dummy)
  1308. {
  1309. $new_entries[$k] = strtr(trim($new_entries[$k]), array('\'' => '&#039;'));
  1310. if (strlen($new_entries[$k]) == 0 || in_array($new_entries[$k], array($user_profile[$memID]['member_name'], $user_profile[$memID]['real_name'])))
  1311. unset($new_entries[$k]);
  1312. }
  1313. if (!empty($new_entries))
  1314. {
  1315. // Now find out the id_member for the members in question.
  1316. $request = $smcFunc['db_query']('', '
  1317. SELECT id_member
  1318. FROM {db_prefix}members
  1319. WHERE member_name IN ({array_string:new_entries}) OR real_name IN ({array_string:new_entries})
  1320. LIMIT {int:count_new_entries}',
  1321. array(
  1322. 'new_entries' => $new_entries,
  1323. 'count_new_entries' => count($new_entries),
  1324. )
  1325. );
  1326. // Add the new member to the buddies array.
  1327. while ($row = $smcFunc['db_fetch_assoc']($request))
  1328. $ignoreArray[] = (int) $row['id_member'];
  1329. $smcFunc['db_free_result']($request);
  1330. // Now update the current users buddy list.
  1331. $user_profile[$memID]['pm_ignore_list'] = implode(',', $ignoreArray);
  1332. updateMemberData($memID, array('pm_ignore_list' => $user_profile[$memID]['pm_ignore_list']));
  1333. }
  1334. // Back to the list of pityful people!
  1335. redirectexit('action=profile;area=lists;sa=ignore;u=' . $memID);
  1336. }
  1337. // Initialise the list of members we're ignoring.
  1338. $ignored = array();
  1339. if (!empty($ignoreArray))
  1340. {
  1341. $result = $smcFunc['db_query']('', '
  1342. SELECT id_member
  1343. FROM {db_prefix}members
  1344. WHERE id_member IN ({array_int:ignore_list})
  1345. ORDER BY real_name
  1346. LIMIT {int:ignore_list_count}',
  1347. array(
  1348. 'ignore_list' => $ignoreArray,
  1349. 'ignore_list_count' => substr_count($user_profile[$memID]['pm_ignore_list'], ',') + 1,
  1350. )
  1351. );
  1352. while ($row = $smcFunc['db_fetch_assoc']($result))
  1353. $ignored[] = $row['id_member'];
  1354. $smcFunc['db_free_result']($result);
  1355. }
  1356. $context['ignore_count'] = count($ignored);
  1357. // Load all the members up.
  1358. loadMemberData($ignored, false, 'profile');
  1359. // Setup the context for each buddy.
  1360. $context['ignore_list'] = array();
  1361. foreach ($ignored as $ignore_member)
  1362. {
  1363. loadMemberContext($ignore_member);
  1364. $context['ignore_list'][$ignore_member] = $memberContext[$ignore_member];
  1365. }
  1366. }
  1367. /**
  1368. * @todo Needs a description
  1369. *
  1370. * @param int $memID id_member
  1371. */
  1372. function account($memID)
  1373. {
  1374. global $context, $txt;
  1375. loadThemeOptions($memID);
  1376. if (allowedTo(array('profile_identity_own', 'profile_identity_any')))
  1377. loadCustomFields($memID, 'account');
  1378. $context['sub_template'] = 'edit_options';
  1379. $context['page_desc'] = $txt['account_info'];
  1380. setupProfileContext(
  1381. array(
  1382. 'member_name', 'real_name', 'date_registered', 'posts', 'lngfile', 'hr',
  1383. 'id_group', 'hr',
  1384. 'email_address', 'hide_email', 'show_online', 'hr',
  1385. 'passwrd1', 'passwrd2', 'hr',
  1386. 'secret_question', 'secret_answer',
  1387. )
  1388. );
  1389. }
  1390. /**
  1391. * @todo Needs a description
  1392. *
  1393. * @param int $memID id_member
  1394. */
  1395. function forumProfile($memID)
  1396. {
  1397. global $context, $user_profile, $user_info, $txt, $modSettings;
  1398. loadThemeOptions($memID);
  1399. if (allowedTo(array('profile_extra_own', 'profile_extra_any')))
  1400. loadCustomFields($memID, 'forumprofile');
  1401. $context['sub_template'] = 'edit_options';
  1402. $context['page_desc'] = $txt['forumProfile_info'];
  1403. setupProfileContext(
  1404. array(
  1405. 'avatar_choice', 'hr', 'personal_text', 'hr',
  1406. 'bday1', 'location', 'gender', 'hr',
  1407. 'icq', 'aim', 'msn', 'yim', 'hr',
  1408. 'usertitle', 'signature', 'hr',
  1409. 'karma_good', 'hr',
  1410. 'website_title', 'website_url',
  1411. )
  1412. );
  1413. }
  1414. /**
  1415. * Allow the edit of *someone elses* personal message settings.
  1416. *
  1417. * @param int $memID id_member
  1418. */
  1419. function pmprefs($memID)
  1420. {
  1421. global $sourcedir, $context, $txt, $scripturl;
  1422. loadThemeOptions($memID);
  1423. loadCustomFields($memID, 'pmprefs');
  1424. $context['sub_template'] = 'edit_options';
  1425. $context['page_desc'] = $txt['pm_settings_desc'];
  1426. setupProfileContext(
  1427. array(
  1428. 'pm_prefs',
  1429. )
  1430. );
  1431. }
  1432. /**
  1433. * Recursive function to retrieve server-stored avatar files
  1434. *
  1435. * @param string $directory
  1436. * @param int $level
  1437. * @return array
  1438. */
  1439. function getAvatars($directory, $level)
  1440. {
  1441. global $context, $txt, $modSettings;
  1442. $result = array();
  1443. // Open the directory..
  1444. $dir = dir($modSettings['avatar_directory'] . (!empty($directory) ? '/' : '') . $directory);
  1445. $dirs = array();
  1446. $files = array();
  1447. if (!$dir)
  1448. return array();
  1449. while ($line = $dir->read())
  1450. {
  1451. if (in_array($line, array('.', '..', 'blank.gif', 'index.php')))
  1452. continue;
  1453. if (is_dir($modSettings['avatar_directory'] . '/' . $directory . (!empty($directory) ? '/' : '') . $line))
  1454. $dirs[] = $line;
  1455. else
  1456. $files[] = $line;
  1457. }
  1458. $dir->close();
  1459. // Sort the results...
  1460. natcasesort($dirs);
  1461. natcasesort($files);
  1462. if ($level == 0)
  1463. {
  1464. $result[] = array(
  1465. 'filename' => 'blank.gif',
  1466. 'checked' => in_array($context['member']['avatar']['server_pic'], array('', 'blank.gif')),
  1467. 'name' => $txt['no_pic'],
  1468. 'is_dir' => false
  1469. );
  1470. }
  1471. foreach ($dirs as $line)
  1472. {
  1473. $tmp = getAvatars($directory . (!empty($directory) ? '/' : '') . $line, $level + 1);
  1474. if (!empty($tmp))
  1475. $result[] = array(
  1476. 'filename' => htmlspecialchars($line),
  1477. 'checked' => strpos($context['member']['avatar']['server_pic'], $line . '/') !== false,
  1478. 'name' => '[' . htmlspecialchars(str_replace('_', ' ', $line)) . ']',
  1479. 'is_dir' => true,
  1480. 'files' => $tmp
  1481. );
  1482. unset($tmp);
  1483. }
  1484. foreach ($files as $line)
  1485. {
  1486. $filename = substr($line, 0, (strlen($line) - strlen(strrchr($line, '.'))));
  1487. $extension = substr(strrchr($line, '.'), 1);
  1488. // Make sure it is an image.
  1489. if (strcasecmp($extension, 'gif') != 0 && strcasecmp($extension, 'jpg') != 0 && strcasecmp($extension, 'jpeg') != 0 && strcasecmp($extension, 'png') != 0 && strcasecmp($extension, 'bmp') != 0)
  1490. continue;
  1491. $result[] = array(
  1492. 'filename' => htmlspecialchars($line),
  1493. 'checked' => $line == $context['member']['avatar']['server_pic'],
  1494. 'name' => htmlspecialchars(str_replace('_', ' ', $filename)),
  1495. 'is_dir' => false
  1496. );
  1497. if ($level == 1)
  1498. $context['avatar_list'][] = $directory . '/' . $line;
  1499. }
  1500. return $result;
  1501. }
  1502. /**
  1503. * @todo needs a description
  1504. *
  1505. * @param int $memID id_member
  1506. */
  1507. function theme($memID)
  1508. {
  1509. global $txt, $context, $user_profile, $modSettings, $settings, $user_info, $smcFunc;
  1510. loadThemeOptions($memID);
  1511. if (allowedTo(array('profile_extra_own', 'profile_extra_any')))
  1512. loadCustomFields($memID, 'theme');
  1513. $context['sub_template'] = 'edit_options';
  1514. $context['page_desc'] = $txt['theme_info'];
  1515. setupProfileContext(
  1516. array(
  1517. 'id_theme', 'smiley_set', 'hr',
  1518. 'time_format', 'time_offset', 'hr',
  1519. 'theme_settings',
  1520. )
  1521. );
  1522. }
  1523. /**
  1524. * Changing authentication method? Only appropriate for people using OpenID.
  1525. *
  1526. * @param int $memID id_member
  1527. * @param bool $saving = false
  1528. */
  1529. function authentication($memID, $saving = false)
  1530. {
  1531. global $context, $cur_profile, $sourcedir, $txt, $post_errors, $modSettings;
  1532. loadLanguage('Login');
  1533. // We are saving?
  1534. if ($saving)
  1535. {
  1536. // Moving to password passed authentication?
  1537. if ($_POST['authenticate'] == 'passwd')
  1538. {
  1539. // Didn't enter anything?
  1540. if ($_POST['passwrd1'] == '')
  1541. $post_errors[] = 'no_password';
  1542. // Do the two entries for the password even match?
  1543. elseif (!isset($_POST['passwrd2']) || $_POST['passwrd1'] != $_POST['passwrd2'])
  1544. $post_errors[] = 'bad_new_password';
  1545. // Is it valid?
  1546. else
  1547. {
  1548. require_once($sourcedir . '/Subs-Auth.php');
  1549. $passwordErrors = validatePassword($_POST['passwrd1'], $cur_profile['member_name'], array($cur_profile['real_name'], $cur_profile['email_address']));
  1550. // Were there errors?
  1551. if ($passwordErrors != null)
  1552. $post_errors[] = 'password_' . $passwordErrors;
  1553. }
  1554. if (empty($post_errors))
  1555. {
  1556. // Integration?
  1557. call_integration_hook('integrate_reset_pass', array($cur_profile['member_name'], $cur_profile['member_name'], $_POST['passwrd1']));
  1558. // Go then.
  1559. $passwd = sha1(strtolower($cur_profile['member_name']) . un_htmlspecialchars($_POST['passwrd1']));
  1560. // Do the important bits.
  1561. updateMemberData($memID, array('openid_uri' => '', 'passwd' => $passwd));
  1562. if ($context['user']['is_owner'])
  1563. setLoginCookie(60 * $modSettings['cookieTime'], $memID, sha1(sha1(strtolower($cur_profile['member_name']) . un_htmlspecialchars($_POST['passwrd2'])) . $cur_profile['password_salt']));
  1564. redirectexit('action=profile;u=' . $memID);
  1565. }
  1566. return true;
  1567. }
  1568. // Not right yet!
  1569. elseif ($_POST['authenticate'] == 'openid' && !empty($_POST['openid_identifier']))
  1570. {
  1571. require_once($sourcedir . '/Subs-OpenID.php');
  1572. $_POST['openid_identifier'] = smf_openID_canonize($_POST['openid_identifier']);
  1573. if (smf_openid_member_exists($_POST['openid_identifier']))
  1574. $post_errors[] = 'openid_in_use';
  1575. elseif (empty($post_errors))
  1576. {
  1577. // Authenticate using the new OpenID URI first to make sure they didn't make a mistake.
  1578. if ($context['user']['is_owner'])
  1579. {
  1580. $_SESSION['new_openid_uri'] = $_POST['openid_identifier'];
  1581. smf_openID_validate($_POST['openid_identifier'], false, null, 'change_uri');
  1582. }
  1583. else
  1584. updateMemberData($memID, array('openid_uri' => $_POST['openid_identifier']));
  1585. }
  1586. }
  1587. }
  1588. // Some stuff.
  1589. $context['member']['openid_uri'] = $cur_profile['openid_uri'];
  1590. $context['auth_method'] = empty($cur_profile['openid_uri']) ? 'password' : 'openid';
  1591. $context['sub_template'] = 'authentication_method';
  1592. }
  1593. /**
  1594. * Display the notifications and settings for changes.
  1595. *
  1596. * @param int $memID id_member
  1597. */
  1598. function notification($memID)
  1599. {
  1600. global $txt, $scripturl, $user_profile, $user_info, $context, $modSettings, $smcFunc, $sourcedir, $settings;
  1601. // Gonna want this for the list.
  1602. require_once($sourcedir . '/Subs-List.php');
  1603. // Fine, start with the board list.
  1604. $listOptions = array(
  1605. 'id' => 'board_notification_list',
  1606. 'width' => '100%',
  1607. 'no_items_label' => $txt['notifications_boards_none'] . '<br /><br />' . $txt['notifications_boards_howto'],
  1608. 'no_items_align' => 'left',
  1609. 'base_href' => $scripturl . '?action=profile;u=' . $memID . ';area=notification',
  1610. 'default_sort_col' => 'board_name',
  1611. 'get_items' => array(
  1612. 'function' => 'list_getBoardNotifications',
  1613. 'params' => array(
  1614. $memID,
  1615. ),
  1616. ),
  1617. 'columns' => array(
  1618. 'board_name' => array(
  1619. 'header' => array(
  1620. 'value' => $txt['notifications_boards'],
  1621. 'class' => 'lefttext first_th',
  1622. ),
  1623. 'data' => array(
  1624. 'function' => create_function('$board', '
  1625. global $settings, $txt;
  1626. $link = $board[\'link\'];
  1627. if ($board[\'new\'])
  1628. $link .= \' <a href="\' . $board[\'href\'] . \'"><img src="\' . $settings[\'lang_images_url\'] . \'/new.gif" alt="\' . $txt[\'new\'] . \'" /></a>\';
  1629. return $link;
  1630. '),
  1631. ),
  1632. 'sort' => array(
  1633. 'default' => 'name',
  1634. 'reverse' => 'name DESC',
  1635. ),
  1636. ),
  1637. 'delete' => array(
  1638. 'header' => array(
  1639. 'value' => '<input type="checkbox" class="input_check" onclick="invertAll(this, this.form);" />',
  1640. 'style' => 'width: 4%;',
  1641. ),
  1642. 'data' => array(
  1643. 'sprintf' => array(
  1644. 'format' => '<input type="checkbox" name="notify_boards[]" value="%1$d" class="input_check" />',
  1645. 'params' => array(
  1646. 'id' => false,
  1647. ),
  1648. ),
  1649. 'style' => 'text-align: center;',
  1650. ),
  1651. ),
  1652. ),
  1653. 'form' => array(
  1654. 'href' => $scripturl . '?action=profile;area=notification;save',
  1655. 'include_sort' => true,
  1656. 'include_start' => true,
  1657. 'hidden_fields' => array(
  1658. 'u' => $memID,
  1659. 'sa' => $context['menu_item_selected'],
  1660. $context['session_var'] => $context['session_id'],
  1661. ),
  1662. 'token' => $context['token_check'],
  1663. ),
  1664. 'additional_rows' => array(
  1665. array(
  1666. 'position' => 'bottom_of_list',
  1667. 'value' => '<input type="submit" name="edit_notify_boards" value="' . $txt['notifications_update'] . '" class="button_submit" />',
  1668. 'align' => 'right',
  1669. ),
  1670. ),
  1671. );
  1672. // Create the board notification list.
  1673. createList($listOptions);
  1674. // Now do the topic notifications.
  1675. $listOptions = array(
  1676. 'id' => 'topic_notification_list',
  1677. 'width' => '100%',
  1678. 'items_per_page' => $modSettings['defaultMaxMessages'],
  1679. 'no_items_label' => $txt['notifications_topics_none'] . '<br /><br />' . $txt['notifications_topics_howto'],
  1680. 'no_items_align' => 'left',
  1681. 'base_href' => $scripturl . '?action=profile;u=' . $memID . ';area=notification',
  1682. 'default_sort_col' => 'last_post',
  1683. 'get_items' => array(
  1684. 'function' => 'list_getTopicNotifications',
  1685. 'params' => array(
  1686. $memID,
  1687. ),
  1688. ),
  1689. 'get_count' => array(
  1690. 'function' => 'list_getTopicNotificationCount',
  1691. 'params' => array(
  1692. $memID,
  1693. ),
  1694. ),
  1695. 'columns' => array(
  1696. 'subject' => array(
  1697. 'header' => array(
  1698. 'value' => $txt['notifications_topics'],
  1699. 'class' => 'lefttext first_th',
  1700. ),
  1701. 'data' => array(
  1702. 'function' => create_function('$topic', '
  1703. global $settings, $txt;
  1704. $link = $topic[\'link\'];
  1705. if ($topic[\'new\'])
  1706. $link .= \' <a href="\' . $topic[\'new_href\'] . \'"><img src="\' . $settings[\'lang_images_url\'] . \'/new.gif" alt="\' . $txt[\'new\'] . \'" /></a>\';
  1707. $link .= \'<br /><span class="smalltext"><em>\' . $txt[\'in\'] . \' \' . $topic[\'board_link\'] . \'</em></span>\';
  1708. return $link;
  1709. '),
  1710. ),
  1711. 'sort' => array(
  1712. 'default' => 'ms.subject',
  1713. 'reverse' => 'ms.subject DESC',
  1714. ),
  1715. ),
  1716. 'started_by' => array(
  1717. 'header' => array(
  1718. 'value' => $txt['started_by'],
  1719. 'class' => 'lefttext',
  1720. ),
  1721. 'data' => array(
  1722. 'db' => 'poster_link',
  1723. ),
  1724. 'sort' => array(
  1725. 'default' => 'real_name_col',
  1726. 'reverse' => 'real_name_col DESC',
  1727. ),
  1728. ),
  1729. 'last_post' => array(
  1730. 'header' => array(
  1731. 'value' => $txt['last_post'],
  1732. 'class' => 'lefttext',
  1733. ),
  1734. 'data' => array(
  1735. 'sprintf' => array(
  1736. 'format' => '<span class="smalltext">%1$s<br />' . $txt['by'] . ' %2$s</span>',
  1737. 'params' => array(
  1738. 'updated' => false,
  1739. 'poster_updated_link' => false,
  1740. ),
  1741. ),
  1742. ),
  1743. 'sort' => array(
  1744. 'default' => 'ml.id_msg DESC',
  1745. 'reverse' => 'ml.id_msg',
  1746. ),
  1747. ),
  1748. 'delete' => array(
  1749. 'header' => array(
  1750. 'value' => '<input type="checkbox" class="input_check" onclick="invertAll(this, this.form);" />',
  1751. 'style' => 'width: 4%;',
  1752. ),
  1753. 'data' => array(
  1754. 'sprintf' => array(
  1755. 'format' => '<input type="checkbox" name="notify_topics[]" value="%1$d" class="input_check" />',
  1756. 'params' => array(
  1757. 'id' => false,
  1758. ),
  1759. ),
  1760. 'style' => 'text-align: center;',
  1761. ),
  1762. ),
  1763. ),
  1764. 'form' => array(
  1765. 'href' => $scripturl . '?action=profile;area=notification;save',
  1766. 'include_sort' => true,
  1767. 'include_start' => true,
  1768. 'hidden_fields' => array(
  1769. 'u' => $memID,
  1770. 'sa' => $context['menu_item_selected'],
  1771. $context['session_var'] => $context['session_id'],
  1772. ),
  1773. 'token' => $context['token_check'],
  1774. ),
  1775. 'additional_rows' => array(
  1776. array(
  1777. 'position' => 'bottom_of_list',
  1778. 'value' => '<input type="submit" name="edit_notify_topics" value="' . $txt['notifications_update'] . '" class="button_submit" />',
  1779. 'align' => 'right',
  1780. ),
  1781. ),
  1782. );
  1783. // Create the notification list.
  1784. createList($listOptions);
  1785. // What options are set?
  1786. $context['member'] += array(
  1787. 'notify_announcements' => $user_profile[$memID]['notify_announcements'],
  1788. 'notify_send_body' => $user_profile[$memID]['notify_send_body'],
  1789. 'notify_types' => $user_profile[$memID]['notify_types'],
  1790. 'notify_regularity' => $user_profile[$memID]['notify_regularity'],
  1791. );
  1792. loadThemeOptions($memID);
  1793. }
  1794. /**
  1795. * @todo needs a description
  1796. *
  1797. * @param int $memID id_member
  1798. * @return string
  1799. */
  1800. function list_getTopicNotificationCount($memID)
  1801. {
  1802. global $smcFunc, $user_info, $context, $modSettings;
  1803. $request = $smcFunc['db_query']('', '
  1804. SELECT COUNT(*)
  1805. FROM {db_prefix}log_notify AS ln' . (!$modSettings['postmod_active'] && $user_info['query_see_board'] === '1=1' ? '' : '
  1806. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = ln.id_topic)') . ($user_info['query_see_board'] === '1=1' ? '' : '
  1807. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)') . '
  1808. WHERE ln.id_member = {int:selected_member}' . ($user_info['query_see_board'] === '1=1' ? '' : '
  1809. AND {query_see_board}') . ($modSettings['postmod_active'] ? '
  1810. AND t.approved = {int:is_approved}' : ''),
  1811. array(
  1812. 'selected_member' => $memID,
  1813. 'is_approved' => 1,
  1814. )
  1815. );
  1816. list ($totalNotifications) = $smcFunc['db_fetch_row']($request);
  1817. $smcFunc['db_free_result']($request);
  1818. // @todo make this an integer before it gets returned
  1819. return $totalNotifications;
  1820. }
  1821. /**
  1822. * @todo Needs a description
  1823. *
  1824. * @param int $start
  1825. * @param int $items_per_page
  1826. * @param string $sort
  1827. * @param int $memID id_member
  1828. * @return array $notification_topics
  1829. */
  1830. function list_getTopicNotifications($start, $items_per_page, $sort, $memID)
  1831. {
  1832. global $smcFunc, $txt, $scripturl, $user_info, $context, $modSettings;
  1833. // All the topics with notification on...
  1834. $request = $smcFunc['db_query']('', '
  1835. SELECT
  1836. IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1 AS new_from, b.id_board, b.name,
  1837. t.id_topic, ms.subject, ms.id_member, IFNULL(mem.real_name, ms.poster_name) AS real_name_col,
  1838. ml.id_msg_modified, ml.poster_time, ml.id_member AS id_member_updated,
  1839. IFNULL(mem2.real_name, ml.poster_name) AS last_real_name
  1840. FROM {db_prefix}log_notify AS ln
  1841. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = ln.id_topic' . ($modSettings['postmod_active'] ? ' AND t.approved = {int:is_approved}' : '') . ')
  1842. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board AND {query_see_board})
  1843. INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)
  1844. INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
  1845. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ms.id_member)
  1846. LEFT JOIN {db_prefix}members AS mem2 ON (mem2.id_member = ml.id_member)
  1847. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
  1848. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = b.id_board AND lmr.id_member = {int:current_member})
  1849. WHERE ln.id_member = {int:selected_member}
  1850. ORDER BY {raw:sort}
  1851. LIMIT {int:offset}, {int:items_per_page}',
  1852. array(
  1853. 'current_member' => $user_info['id'],
  1854. 'is_approved' => 1,
  1855. 'selected_member' => $memID,
  1856. 'sort' => $sort,
  1857. 'offset' => $start,
  1858. 'items_per_page' => $items_per_page,
  1859. )
  1860. );
  1861. $notification_topics = array();
  1862. while ($row = $smcFunc['db_fetch_assoc']($request))
  1863. {
  1864. censorText($row['subject']);
  1865. $notification_topics[] = array(
  1866. 'id' => $row['id_topic'],
  1867. 'poster_link' => empty($row['id_member']) ? $row['real_name_col'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name_col'] . '</a>',
  1868. 'poster_updated_link' => empty($row['id_member_updated']) ? $row['last_real_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member_updated'] . '">' . $row['last_real_name'] . '</a>',
  1869. 'subject' => $row['subject'],
  1870. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  1871. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['subject'] . '</a>',
  1872. 'new' => $row['new_from'] <= $row['id_msg_modified'],
  1873. 'new_from' => $row['new_from'],
  1874. 'updated' => timeformat($row['poster_time']),
  1875. 'new_href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . '#new',
  1876. 'new_link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . '#new">' . $row['subject'] . '</a>',
  1877. 'board_link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>',
  1878. );
  1879. }
  1880. $smcFunc['db_free_result']($request);
  1881. return $notification_topics;
  1882. }
  1883. /**
  1884. * @todo needs a description
  1885. *
  1886. * @param int $start
  1887. * @param int $items_per_page
  1888. * @param string $sort
  1889. * @param int $memID id_member
  1890. * @return array
  1891. */
  1892. function list_getBoardNotifications($start, $items_per_page, $sort, $memID)
  1893. {
  1894. global $smcFunc, $txt, $scripturl, $user_info;
  1895. $request = $smcFunc['db_query']('', '
  1896. SELECT b.id_board, b.name, IFNULL(lb.id_msg, 0) AS board_read, b.id_msg_updated
  1897. FROM {db_prefix}log_notify AS ln
  1898. INNER JOIN {db_prefix}boards AS b ON (b.id_board = ln.id_board)
  1899. LEFT JOIN {db_prefix}log_boards AS lb ON (lb.id_board = b.id_board AND lb.id_member = {int:current_member})
  1900. WHERE ln.id_member = {int:selected_member}
  1901. AND {query_see_board}
  1902. ORDER BY ' . $sort,
  1903. array(
  1904. 'current_member' => $user_info['id'],
  1905. 'selected_member' => $memID,
  1906. )
  1907. );
  1908. $notification_boards = array();
  1909. while ($row = $smcFunc['db_fetch_assoc']($request))
  1910. $notification_boards[] = array(
  1911. 'id' => $row['id_board'],
  1912. 'name' => $row['name'],
  1913. 'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
  1914. 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>',
  1915. 'new' => $row['board_read'] < $row['id_msg_updated']
  1916. );
  1917. $smcFunc['db_free_result']($request);
  1918. return $notification_boards;
  1919. }
  1920. /**
  1921. * @todo needs a description
  1922. *
  1923. * @param int $memID id_member
  1924. */
  1925. function loadThemeOptions($memID)
  1926. {
  1927. global $context, $options, $cur_profile, $smcFunc;
  1928. if (isset($_POST['default_options']))
  1929. $_POST['options'] = isset($_POST['options']) ? $_POST['options'] + $_POST['default_options'] : $_POST['default_options'];
  1930. if ($context['user']['is_owner'])
  1931. {
  1932. $context['member']['options'] = $options;
  1933. if (isset($_POST['options']) && is_array($_POST['options']))
  1934. foreach ($_POST['options'] as $k => $v)
  1935. $context['member']['options'][$k] = $v;
  1936. }
  1937. else
  1938. {
  1939. $request = $smcFunc['db_query']('', '
  1940. SELECT id_member, variable, value
  1941. FROM {db_prefix}themes
  1942. WHERE id_theme IN (1, {int:member_theme})
  1943. AND id_member IN (-1, {int:selected_member})',
  1944. array(
  1945. 'member_theme' => (int) $cur_profile['id_theme'],
  1946. 'selected_member' => $memID,
  1947. )
  1948. );
  1949. $temp = array();
  1950. while ($row = $smcFunc['db_fetch_assoc']($request))
  1951. {
  1952. if ($row['id_member'] == -1)
  1953. {
  1954. $temp[$row['variable']] = $row['value'];
  1955. continue;
  1956. }
  1957. if (isset($_POST['options'][$row['variable']]))
  1958. $row['value'] = $_POST['options'][$row['variable']];
  1959. $context['member']['options'][$row['variable']] = $row['value'];
  1960. }
  1961. $smcFunc['db_free_result']($request);
  1962. // Load up the default theme options for any missing.
  1963. foreach ($temp as $k => $v)
  1964. {
  1965. if (!isset($context['member']['options'][$k]))
  1966. $context['member']['options'][$k] = $v;
  1967. }
  1968. }
  1969. }
  1970. /**
  1971. * @todo needs a description
  1972. *
  1973. * @param int $memID id_member
  1974. */
  1975. function ignoreboards($memID)
  1976. {
  1977. global $txt, $user_info, $context, $modSettings, $smcFunc, $cur_profile;
  1978. // Have the admins enabled this option?
  1979. if (empty($modSettings['allow_ignore_boards']))
  1980. fatal_lang_error('ignoreboards_disallowed', 'user');
  1981. // Find all the boards this user is allowed to see.
  1982. $request = $smcFunc['db_query']('order_by_board_order', '
  1983. SELECT b.id_cat, c.name AS cat_name, b.id_board, b.name, b.child_level,
  1984. '. (!empty($cur_profile['ignore_boards']) ? 'b.id_board IN ({array_int:ignore_boards})' : '0') . ' AS is_ignored
  1985. FROM {db_prefix}boards AS b
  1986. LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
  1987. WHERE {query_see_board}
  1988. AND redirect = {string:empty_string}',
  1989. array(
  1990. 'ignore_boards' => !empty($cur_profile['ignore_boards']) ? explode(',', $cur_profile['ignore_boards']) : array(),
  1991. 'empty_string' => '',
  1992. )
  1993. );
  1994. $context['num_boards'] = $smcFunc['db_num_rows']($request);
  1995. $context['categories'] = array();
  1996. while ($row = $smcFunc['db_fetch_assoc']($request))
  1997. {
  1998. // This category hasn't been set up yet..
  1999. if (!isset($context['categories'][$row['id_cat']]))
  2000. $context['categories'][$row['id_cat']] = array(
  2001. 'id' => $row['id_cat'],
  2002. 'name' => $row['cat_name'],
  2003. 'boards' => array()
  2004. );
  2005. // Set this board up, and let the template know when it's a child. (indent them..)
  2006. $context['categories'][$row['id_cat']]['boards'][$row['id_board']] = array(
  2007. 'id' => $row['id_board'],
  2008. 'name' => $row['name'],
  2009. 'child_level' => $row['child_level'],
  2010. 'selected' => $row['is_ignored'],
  2011. );
  2012. }
  2013. $smcFunc['db_free_result']($request);
  2014. // Now, let's sort the list of categories into the boards for templates that like that.
  2015. $temp_boards = array();
  2016. foreach ($context['categories'] as $category)
  2017. {
  2018. // Include a list of boards per category for easy toggling.
  2019. $context['categories'][$category['id']]['child_ids'] = array_keys($category['boards']);
  2020. $temp_boards[] = array(
  2021. 'name' => $category['name'],
  2022. 'child_ids' => array_keys($category['boards'])
  2023. );
  2024. $temp_boards = array_merge($temp_boards, array_values($category['boards']));
  2025. }
  2026. $max_boards = ceil(count($temp_boards) / 2);
  2027. if ($max_boards == 1)
  2028. $max_boards = 2;
  2029. // Now, alternate them so they can be shown left and right ;).
  2030. $context['board_columns'] = array();
  2031. for ($i = 0; $i < $max_boards; $i++)
  2032. {
  2033. $context['board_columns'][] = $temp_boards[$i];
  2034. if (isset($temp_boards[$i + $max_boards]))
  2035. $context['board_columns'][] = $temp_boards[$i + $max_boards];
  2036. else
  2037. $context['board_columns'][] = array();
  2038. }
  2039. loadThemeOptions($memID);
  2040. }
  2041. /**
  2042. * Load all the languages for the profile.
  2043. * @return bool
  2044. */
  2045. function profileLoadLanguages()
  2046. {
  2047. global $context, $modSettings, $settings, $cur_profile, $language, $smcFunc;
  2048. $context['profile_languages'] = array();
  2049. // Get our languages!
  2050. getLanguages(true, true);
  2051. // Setup our languages.
  2052. foreach ($context['languages'] as $lang)
  2053. {
  2054. $context['profile_languages'][$lang['filename']] = strtr($lang['name'], array('-utf8' => ''));
  2055. }
  2056. ksort($context['profile_languages']);
  2057. // Return whether we should proceed with this.
  2058. return count($context['profile_languages']) > 1 ? true : false;
  2059. }
  2060. /**
  2061. * @todo needs description
  2062. *
  2063. * @return true
  2064. */
  2065. function profileLoadGroups()
  2066. {
  2067. global $cur_profile, $txt, $context, $smcFunc, $user_settings;
  2068. $context['member_groups'] = array(
  2069. 0 => array(
  2070. 'id' => 0,
  2071. 'name' => $txt['no_primary_membergroup'],
  2072. 'is_primary' => $cur_profile['id_group'] == 0,
  2073. 'can_be_additional' => false,
  2074. 'can_be_primary' => true,
  2075. )
  2076. );
  2077. $curGroups = explode(',', $cur_profile['additional_groups']);
  2078. // Load membergroups, but only those groups the user can assign.
  2079. $request = $smcFunc['db_query']('', '
  2080. SELECT group_name, id_group, hidden
  2081. FROM {db_prefix}membergroups
  2082. WHERE id_group != {int:moderator_group}
  2083. AND min_posts = {int:min_posts}' . (allowedTo('admin_forum') ? '' : '
  2084. AND group_type != {int:is_protected}') . '
  2085. ORDER BY min_posts, CASE WHEN id_group < {int:newbie_group} THEN id_group ELSE 4 END, group_name',
  2086. array(
  2087. 'moderator_group' => 3,
  2088. 'min_posts' => -1,
  2089. 'is_protected' => 1,
  2090. 'newbie_group' => 4,
  2091. )
  2092. );
  2093. while ($row = $smcFunc['db_fetch_assoc']($request))
  2094. {
  2095. // We should skip the administrator group if they don't have the admin_forum permission!
  2096. if ($row['id_group'] == 1 && !allowedTo('admin_forum'))
  2097. continue;
  2098. $context['member_groups'][$row['id_group']] = array(
  2099. 'id' => $row['id_group'],
  2100. 'name' => $row['group_name'],
  2101. 'is_primary' => $cur_profile['id_group'] == $row['id_group'],
  2102. 'is_additional' => in_array($row['id_group'], $curGroups),
  2103. 'can_be_additional' => true,
  2104. 'can_be_primary' => $row['hidden'] != 2,
  2105. );
  2106. }
  2107. $smcFunc['db_free_result']($request);
  2108. $context['member']['group_id'] = $user_settings['id_group'];
  2109. return true;
  2110. }
  2111. /**
  2112. * Load key signature context data.
  2113. *
  2114. * @return true
  2115. */
  2116. function profileLoadSignatureData()
  2117. {
  2118. global $modSettings, $context, $txt, $cur_profile, $smcFunc;
  2119. // Signature limits.
  2120. list ($sig_limits, $sig_bbc) = explode(':', $modSettings['signature_settings']);
  2121. $sig_limits = explode(',', $sig_limits);
  2122. $context['signature_enabled'] = isset($sig_limits[0]) ? $sig_limits[0] : 0;
  2123. $context['signature_limits'] = array(
  2124. 'max_length' => isset($sig_limits[1]) ? $sig_limits[1] : 0,
  2125. 'max_lines' => isset($sig_limits[2]) ? $sig_limits[2] : 0,
  2126. 'max_images' => isset($sig_limits[3]) ? $sig_limits[3] : 0,
  2127. 'max_smileys' => isset($sig_limits[4]) ? $sig_limits[4] : 0,
  2128. 'max_image_width' => isset($sig_limits[5]) ? $sig_limits[5] : 0,
  2129. 'max_image_height' => isset($sig_limits[6]) ? $sig_limits[6] : 0,
  2130. 'max_font_size' => isset($sig_limits[7]) ? $sig_limits[7] : 0,
  2131. 'bbc' => !empty($sig_bbc) ? explode(',', $sig_bbc) : array(),
  2132. );
  2133. // Kept this line in for backwards compatibility!
  2134. $context['max_signature_length'] = $context['signature_limits']['max_length'];
  2135. // Warning message for signature image limits?
  2136. $context['signature_warning'] = '';
  2137. if ($context['signature_limits']['max_image_width'] && $context['signature_limits']['max_image_height'])
  2138. $context['signature_warning'] = sprintf($txt['profile_error_signature_max_image_size'], $context['signature_limits']['max_image_width'], $context['signature_limits']['max_image_height']);
  2139. elseif ($context['signature_limits']['max_image_width'] || $context['signature_limits']['max_image_height'])
  2140. $context['signature_warning'] = sprintf($txt['profile_error_signature_max_image_' . ($context['signature_limits']['max_image_width'] ? 'width' : 'height')], $context['signature_limits'][$context['signature_limits']['max_image_width'] ? 'max_image_width' : 'max_image_height']);
  2141. $context['show_spellchecking'] = !empty($modSettings['enableSpellChecking']) && function_exists('pspell_new');
  2142. $context['member']['signature'] = empty($cur_profile['signature']) ? '' : str_replace(array('<br />', '<', '>', '"', '\''), array("\n", '&lt;', '&gt;', '&quot;', '&#039;'), $cur_profile['signature']);
  2143. return true;
  2144. }
  2145. /**
  2146. * Load avatar context data.
  2147. *
  2148. * @return true
  2149. */
  2150. function profileLoadAvatarData()
  2151. {
  2152. global $context, $cur_profile, $modSettings, $scripturl;
  2153. $context['avatar_url'] = $modSettings['avatar_url'];
  2154. // Default context.
  2155. $context['member']['avatar'] += array(
  2156. 'custom' => stristr($cur_profile['avatar'], 'http://') ? $cur_profile['avatar'] : 'http://',
  2157. 'selection' => $cur_profile['avatar'] == '' || stristr($cur_profile['avatar'], 'http://') ? '' : $cur_profile['avatar'],
  2158. 'id_attach' => $cur_profile['id_attach'],
  2159. 'filename' => $cur_profile['filename'],
  2160. 'allow_server_stored' => allowedTo('profile_server_avatar') || (!$context['user']['is_owner'] && allowedTo('profile_extra_any')),
  2161. 'allow_upload' => allowedTo('profile_upload_avatar') || (!$context['user']['is_owner'] && allowedTo('profile_extra_any')),
  2162. 'allow_external' => allowedTo('profile_remote_avatar') || (!$context['user']['is_owner'] && allowedTo('profile_extra_any')),
  2163. );
  2164. if ($cur_profile['avatar'] == '' && $cur_profile['id_attach'] > 0 && $context['member']['avatar']['allow_upload'])
  2165. {
  2166. $context['member']['avatar'] += array(
  2167. 'choice' => 'upload',
  2168. 'server_pic' => 'blank.gif',
  2169. 'external' => 'http://'
  2170. );
  2171. $context['member']['avatar']['href'] = empty($cur_profile['attachment_type']) ? $scripturl . '?action=dlattach;attach=' . $cur_profile['id_attach'] . ';type=avatar' : $modSettings['custom_avatar_url'] . '/' . $cur_profile['filename'];
  2172. }
  2173. elseif (stristr($cur_profile['avatar'], 'http://') && $context['member']['avatar']['allow_external'])
  2174. $context['member']['avatar'] += array(
  2175. 'choice' => 'external',
  2176. 'server_pic' => 'blank.gif',
  2177. 'external' => $cur_profile['avatar']
  2178. );
  2179. elseif ($cur_profile['avatar'] != '' && file_exists($modSettings['avatar_directory'] . '/' . $cur_profile['avatar']) && $context['member']['avatar']['allow_server_stored'])
  2180. $context['member']['avatar'] += array(
  2181. 'choice' => 'server_stored',
  2182. 'server_pic' => $cur_profile['avatar'] == '' ? 'blank.gif' : $cur_profile['avatar'],
  2183. 'external' => 'http://'
  2184. );
  2185. else
  2186. $context['member']['avatar'] += array(
  2187. 'choice' => 'none',
  2188. 'server_pic' => 'blank.gif',
  2189. 'external' => 'http://'
  2190. );
  2191. // Get a list of all the avatars.
  2192. if ($context['member']['avatar']['allow_server_stored'])
  2193. {
  2194. $context['avatar_list'] = array();
  2195. $context['avatars'] = is_dir($modSettings['avatar_directory']) ? getAvatars('', 0) : array();
  2196. }
  2197. else
  2198. $context['avatars'] = array();
  2199. // Second level selected avatar...
  2200. $context['avatar_selected'] = substr(strrchr($context['member']['avatar']['server_pic'], '/'), 1);
  2201. return true;
  2202. }
  2203. /**
  2204. * Save a members group.
  2205. *
  2206. * @param int &$value
  2207. * @return true
  2208. */
  2209. function profileSaveGroups(&$value)
  2210. {
  2211. global $profile_vars, $old_profile, $context, $smcFunc, $cur_profile;
  2212. // Do we need to protect some groups?
  2213. if (!allowedTo('admin_forum'))
  2214. {
  2215. $request = $smcFunc['db_query']('', '
  2216. SELECT id_group
  2217. FROM {db_prefix}membergroups
  2218. WHERE group_type = {int:is_protected}',
  2219. array(
  2220. 'is_protected' => 1,
  2221. )
  2222. );
  2223. $protected_groups = array(1);
  2224. while ($row = $smcFunc['db_fetch_assoc']($request))
  2225. $protected_groups[] = $row['id_group'];
  2226. $smcFunc['db_free_result']($request);
  2227. $protected_groups = array_unique($protected_groups);
  2228. }
  2229. // The account page allows the change of your id_group - but not to a protected group!
  2230. if (empty($protected_groups) || count(array_intersect(array((int) $value, $old_profile['id_group']), $protected_groups)) == 0)
  2231. $value = (int) $value;
  2232. // ... otherwise it's the old group sir.
  2233. else
  2234. $value = $old_profile['id_group'];
  2235. // Find the additional membergroups (if any)
  2236. if (isset($_POST['additional_groups']) && is_array($_POST['additional_groups']))
  2237. {
  2238. $additional_groups = array();
  2239. foreach ($_POST['additional_groups'] as $group_id)
  2240. {
  2241. $group_id = (int) $group_id;
  2242. if (!empty($group_id) && (empty($protected_groups) || !in_array($group_id, $protected_groups)))
  2243. $additional_groups[] = $group_id;
  2244. }
  2245. // Put the protected groups back in there if you don't have permission to take them away.
  2246. $old_additional_groups = explode(',', $old_profile['additional_groups']);
  2247. foreach ($old_additional_groups as $group_id)
  2248. {
  2249. if (!empty($protected_groups) && in_array($group_id, $protected_groups))
  2250. $additional_groups[] = $group_id;
  2251. }
  2252. if (implode(',', $additional_groups) !== $old_profile['additional_groups'])
  2253. {
  2254. $profile_vars['additional_groups'] = implode(',', $additional_groups);
  2255. $cur_profile['additional_groups'] = implode(',', $additional_groups);
  2256. }
  2257. }
  2258. // Too often, people remove delete their own account, or something.
  2259. if (in_array(1, explode(',', $old_profile['additional_groups'])) || $old_profile['id_group'] == 1)
  2260. {
  2261. $stillAdmin = $value == 1 || (isset($additional_groups) && in_array(1, $additional_groups));
  2262. // If they would no longer be an admin, look for any other...
  2263. if (!$stillAdmin)
  2264. {
  2265. $request = $smcFunc['db_query']('', '
  2266. SELECT id_member
  2267. FROM {db_prefix}members
  2268. WHERE (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0)
  2269. AND id_member != {int:selected_member}
  2270. LIMIT 1',
  2271. array(
  2272. 'admin_group' => 1,
  2273. 'selected_member' => $context['id_member'],
  2274. )
  2275. );
  2276. list ($another) = $smcFunc['db_fetch_row']($request);
  2277. $smcFunc['db_free_result']($request);
  2278. if (empty($another))
  2279. fatal_lang_error('at_least_one_admin', 'critical');
  2280. }
  2281. }
  2282. // If we are changing group status, update permission cache as necessary.
  2283. if ($value != $old_profile['id_group'] || isset($profile_vars['additional_groups']))
  2284. {
  2285. if ($context['user']['is_owner'])
  2286. $_SESSION['mc']['time'] = 0;
  2287. else
  2288. updateSettings(array('settings_updated' => time()));
  2289. }
  2290. return true;
  2291. }
  2292. /**
  2293. * The avatar is incredibly complicated, what with the options... and what not.
  2294. * @todo argh, the avatar here. Take this out of here!
  2295. *
  2296. * @param array &$value
  2297. * @return mixed
  2298. */
  2299. function profileSaveAvatarData(&$value)
  2300. {
  2301. global $modSettings, $sourcedir, $smcFunc, $profile_vars, $cur_profile, $context;
  2302. $memID = $context['id_member'];
  2303. if (empty($memID) && !empty($context['password_auth_failed']))
  2304. return false;
  2305. require_once($sourcedir . '/ManageAttachments.php');
  2306. // We need to know where we're going to be putting it..
  2307. if (!empty($modSettings['custom_avatar_enabled']))
  2308. {
  2309. $uploadDir = $modSettings['custom_avatar_dir'];
  2310. $id_folder = 1;
  2311. }
  2312. elseif (!empty($modSettings['currentAttachmentUploadDir']))
  2313. {
  2314. if (!is_array($modSettings['attachmentUploadDir']))
  2315. $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']);
  2316. // Just use the current path for temp files.
  2317. $uploadDir = $modSettings['attachmentUploadDir'][$modSettings['currentAttachmentUploadDir']];
  2318. $id_folder = $modSettings['currentAttachmentUploadDir'];
  2319. }
  2320. else
  2321. {
  2322. $uploadDir = $modSettings['attachmentUploadDir'];
  2323. $id_folder = 1;
  2324. }
  2325. $downloadedExternalAvatar = false;
  2326. if ($value == 'external' && allowedTo('profile_remote_avatar') && stripos($_POST['userpicpersonal'], 'http://') === 0 && strlen($_POST['userpicpersonal']) > 7 && !empty($modSettings['avatar_download_external']))
  2327. {
  2328. if (!is_writable($uploadDir))
  2329. fatal_lang_error('attachments_no_write', 'critical');
  2330. require_once($sourcedir . '/Subs-Package.php');
  2331. $url = parse_url($_POST['userpicpersonal']);
  2332. $contents = fetch_web_data('http://' . $url['host'] . (empty($url['port']) ? '' : ':' . $url['port']) . str_replace(' ', '%20', trim($url['path'])));
  2333. if ($contents != false && $tmpAvatar = fopen($uploadDir . '/avatar_tmp_' . $memID, 'wb'))
  2334. {
  2335. fwrite($tmpAvatar, $contents);
  2336. fclose($tmpAvatar);
  2337. $downloadedExternalAvatar = true;
  2338. $_FILES['attachment']['tmp_name'] = $uploadDir . '/avatar_tmp_' . $memID;
  2339. }
  2340. }
  2341. if ($value == 'none')
  2342. {
  2343. $profile_vars['avatar'] = '';
  2344. // Reset the attach ID.
  2345. $cur_profile['id_attach'] = 0;
  2346. $cur_profile['attachment_type'] = 0;
  2347. $cur_profile['filename'] = '';
  2348. removeAttachments(array('id_member' => $memID));
  2349. }
  2350. elseif ($value == 'server_stored' && allowedTo('profile_server_avatar'))
  2351. {
  2352. $profile_vars['avatar'] = strtr(empty($_POST['file']) ? (empty($_POST['cat']) ? '' : $_POST['cat']) : $_POST['file'], array('&amp;' => '&'));
  2353. $profile_vars['avatar'] = preg_match('~^([\w _!@%*=\-#()\[\]&.,]+/)?[\w _!@%*=\-#()\[\]&.,]+$~', $profile_vars['avatar']) != 0 && preg_match('/\.\./', $profile_vars['avatar']) == 0 && file_exists($modSettings['avatar_directory'] . '/' . $profile_vars['avatar']) ? ($profile_vars['avatar'] == 'blank.gif' ? '' : $profile_vars['avatar']) : '';
  2354. // Clear current profile...
  2355. $cur_profile['id_attach'] = 0;
  2356. $cur_profile['attachment_type'] = 0;
  2357. $cur_profile['filename'] = '';
  2358. // Get rid of their old avatar. (if uploaded.)
  2359. removeAttachments(array('id_member' => $memID));
  2360. }
  2361. elseif ($value == 'external' && allowedTo('profile_remote_avatar') && stripos($_POST['userpicpersonal'], 'http://') === 0 && empty($modSettings['avatar_download_external']))
  2362. {
  2363. // We need these clean...
  2364. $cur_profile['id_attach'] = 0;
  2365. $cur_profile['attachment_type'] = 0;
  2366. $cur_profile['filename'] = '';
  2367. // Remove any attached avatar...
  2368. removeAttachments(array('id_member' => $memID));
  2369. // @todo http://www.simplemachines.org/community/index.php?topic=462089.msg3226650#msg3226650
  2370. $profile_vars['avatar'] = str_replace(' ', '%20', preg_replace('~action(?:=|%3d)(?!dlattach)~i', 'action-', $_POST['userpicpersonal']));
  2371. if ($profile_vars['avatar'] == 'http://' || $profile_vars['avatar'] == 'http:///')
  2372. $profile_vars['avatar'] = '';
  2373. // Trying to make us do something we'll regret?
  2374. elseif (substr($profile_vars['avatar'], 0, 7) != 'http://')
  2375. return 'bad_avatar';
  2376. // Should we check dimensions?
  2377. elseif (!empty($modSettings['avatar_max_height_external']) || !empty($modSettings['avatar_max_width_external']))
  2378. {
  2379. // Now let's validate the avatar.
  2380. $sizes = url_image_size($profile_vars['avatar']);
  2381. if (is_array($sizes) && (($sizes[0] > $modSettings['avatar_max_width_external'] && !empty($modSettings['avatar_max_width_external'])) || ($sizes[1] > $modSettings['avatar_max_height_external'] && !empty($modSettings['avatar_max_height_external']))))
  2382. {
  2383. // Houston, we have a problem. The avatar is too large!!
  2384. if ($modSettings['avatar_action_too_large'] == 'option_refuse')
  2385. return 'bad_avatar';
  2386. elseif ($modSettings['avatar_action_too_large'] == 'option_download_and_resize')
  2387. {
  2388. // @todo remove this if appropriate
  2389. require_once($sourcedir . '/Subs-Graphics.php');
  2390. if (downloadAvatar($profile_vars['avatar'], $memID, $modSettings['avatar_max_width_external'], $modSettings['avatar_max_height_external']))
  2391. {
  2392. $profile_vars['avatar'] = '';
  2393. $cur_profile['id_attach'] = $modSettings['new_avatar_data']['id'];
  2394. $cur_profile['filename'] = $modSettings['new_avatar_data']['filename'];
  2395. $cur_profile['attachment_type'] = $modSettings['new_avatar_data']['type'];
  2396. }
  2397. else
  2398. return 'bad_avatar';
  2399. }
  2400. }
  2401. }
  2402. }
  2403. elseif (($value == 'upload' && allowedTo('profile_upload_avatar')) || $downloadedExternalAvatar)
  2404. {
  2405. if ((isset($_FILES['attachment']['name']) && $_FILES['attachment']['name'] != '') || $downloadedExternalAvatar)
  2406. {
  2407. // Get the dimensions of the image.
  2408. if (!$downloadedExternalAvatar)
  2409. {
  2410. if (!is_writable($uploadDir))
  2411. fatal_lang_error('attachments_no_write', 'critical');
  2412. if (!move_uploaded_file($_FILES['attachment']['tmp_name'], $uploadDir . '/avatar_tmp_' . $memID))
  2413. fatal_lang_error('attach_timeout', 'critical');
  2414. $_FILES['attachment']['tmp_name'] = $uploadDir . '/avatar_tmp_' . $memID;
  2415. }
  2416. $sizes = @getimagesize($_FILES['attachment']['tmp_name']);
  2417. // No size, then it's probably not a valid pic.
  2418. if ($sizes === false)
  2419. return 'bad_avatar';
  2420. // Check whether the image is too large.
  2421. elseif ((!empty($modSettings['avatar_max_width_upload']) && $sizes[0] > $modSettings['avatar_max_width_upload']) || (!empty($modSettings['avatar_max_height_upload']) && $sizes[1] > $modSettings['avatar_max_height_upload']))
  2422. {
  2423. if (!empty($modSettings['avatar_resize_upload']))
  2424. {
  2425. // Attempt to chmod it.
  2426. @chmod($uploadDir . '/avatar_tmp_' . $memID, 0644);
  2427. // @todo remove this require when appropriate
  2428. require_once($sourcedir . '/Subs-Graphics.php');
  2429. if (!downloadAvatar($uploadDir . '/avatar_tmp_' . $memID, $memID, $modSettings['avatar_max_width_upload'], $modSettings['avatar_max_height_upload']))
  2430. return 'bad_avatar';
  2431. // Reset attachment avatar data.
  2432. $cur_profile['id_attach'] = $modSettings['new_avatar_data']['id'];
  2433. $cur_profile['filename'] = $modSettings['new_avatar_data']['filename'];
  2434. $cur_profile['attachment_type'] = $modSettings['new_avatar_data']['type'];
  2435. }
  2436. else
  2437. return 'bad_avatar';
  2438. }
  2439. elseif (is_array($sizes))
  2440. {
  2441. // Now try to find an infection.
  2442. require_once($sourcedir . '/Subs-Graphics.php');
  2443. if (!checkImageContents($_FILES['attachment']['tmp_name'], !empty($modSettings['avatar_paranoid'])))
  2444. {
  2445. // It's bad. Try to re-encode the contents?
  2446. if (empty($modSettings['avatar_reencode']) || (!reencodeImage($_FILES['attachment']['tmp_name'], $sizes[2])))
  2447. return 'bad_avatar';
  2448. // We were successful. However, at what price?
  2449. $sizes = @getimagesize($_FILES['attachment']['tmp_name']);
  2450. // Hard to believe this would happen, but can you bet?
  2451. if ($sizes === false)
  2452. return 'bad_avatar';
  2453. }
  2454. $extensions = array(
  2455. '1' => 'gif',
  2456. '2' => 'jpg',
  2457. '3' => 'png',
  2458. '6' => 'bmp'
  2459. );
  2460. $extension = isset($extensions[$sizes[2]]) ? $extensions[$sizes[2]] : 'bmp';
  2461. $mime_type = 'image/' . ($extension === 'jpg' ? 'jpeg' : ($extension === 'bmp' ? 'x-ms-bmp' : $extension));
  2462. $destName = 'avatar_' . $memID . '_' . time() . '.' . $extension;
  2463. list ($width, $height) = getimagesize($_FILES['attachment']['tmp_name']);
  2464. $file_hash = empty($modSettings['custom_avatar_enabled']) ? getAttachmentFilename($destName, false, null, true) : '';
  2465. // Remove previous attachments this member might have had.
  2466. removeAttachments(array('id_member' => $memID));
  2467. $smcFunc['db_insert']('',
  2468. '{db_prefix}attachments',
  2469. array(
  2470. 'id_member' => 'int', 'attachment_type' => 'int', 'filename' => 'string', 'file_hash' => 'string', 'fileext' => 'string', 'size' => 'int',
  2471. 'width' => 'int', 'height' => 'int', 'mime_type' => 'string', 'id_folder' => 'int',
  2472. ),
  2473. array(
  2474. $memID, (empty($modSettings['custom_avatar_enabled']) ? 0 : 1), $destName, $file_hash, $extension, filesize($_FILES['attachment']['tmp_name']),
  2475. (int) $width, (int) $height, $mime_type, $id_folder,
  2476. ),
  2477. array('id_attach')
  2478. );
  2479. $cur_profile['id_attach'] = $smcFunc['db_insert_id']('{db_prefix}attachments', 'id_attach');
  2480. $cur_profile['filename'] = $destName;
  2481. $cur_profile['attachment_type'] = empty($modSettings['custom_avatar_enabled']) ? 0 : 1;
  2482. $destinationPath = $uploadDir . '/' . (empty($file_hash) ? $destName : $cur_profile['id_attach'] . '_' . $file_hash);
  2483. if (!rename($_FILES['attachment']['tmp_name'], $destinationPath))
  2484. {
  2485. // I guess a man can try.
  2486. removeAttachments(array('id_member' => $memID));
  2487. fatal_lang_error('attach_timeout', 'critical');
  2488. }
  2489. // Attempt to chmod it.
  2490. @chmod($uploadDir . '/' . $destinationPath, 0644);
  2491. }
  2492. $profile_vars['avatar'] = '';
  2493. // Delete any temporary file.
  2494. if (file_exists($uploadDir . '/avatar_tmp_' . $memID))
  2495. @unlink($uploadDir . '/avatar_tmp_' . $memID);
  2496. }
  2497. // Selected the upload avatar option and had one already uploaded before or didn't upload one.
  2498. else
  2499. $profile_vars['avatar'] = '';
  2500. }
  2501. else
  2502. $profile_vars['avatar'] = '';
  2503. // Setup the profile variables so it shows things right on display!
  2504. $cur_profile['avatar'] = $profile_vars['avatar'];
  2505. return false;
  2506. }
  2507. /**
  2508. * Validate the signature
  2509. *
  2510. * @param mixed &$value
  2511. * @return bool|string
  2512. */
  2513. function profileValidateSignature(&$value)
  2514. {
  2515. global $sourcedir, $modSettings, $smcFunc, $txt;
  2516. require_once($sourcedir . '/Subs-Post.php');
  2517. // Admins can do whatever they hell they want!
  2518. if (!allowedTo('admin_forum'))
  2519. {
  2520. // Load all the signature limits.
  2521. list ($sig_limits, $sig_bbc) = explode(':', $modSettings['signature_settings']);
  2522. $sig_limits = explode(',', $sig_limits);
  2523. $disabledTags = !empty($sig_bbc) ? explode(',', $sig_bbc) : array();
  2524. $unparsed_signature = strtr(un_htmlspecialchars($value), array("\r" => '', '&#039' => '\''));
  2525. // Too long?
  2526. if (!empty($sig_limits[1]) && $smcFunc['strlen']($unparsed_signature) > $sig_limits[1])
  2527. {
  2528. $_POST['signature'] = trim(htmlspecialchars($smcFunc['substr']($unparsed_signature, 0, $sig_limits[1]), ENT_QUOTES));
  2529. $txt['profile_error_signature_max_length'] = sprintf($txt['profile_error_signature_max_length'], $sig_limits[1]);
  2530. return 'signature_max_length';
  2531. }
  2532. // Too many lines?
  2533. if (!empty($sig_limits[2]) && substr_count($unparsed_signature, "\n") >= $sig_limits[2])
  2534. {
  2535. $txt['profile_error_signature_max_lines'] = sprintf($txt['profile_error_signature_max_lines'], $sig_limits[2]);
  2536. return 'signature_max_lines';
  2537. }
  2538. // Too many images?!
  2539. if (!empty($sig_limits[3]) && (substr_count(strtolower($unparsed_signature), '[img') + substr_count(strtolower($unparsed_signature), '<img')) > $sig_limits[3])
  2540. {
  2541. $txt['profile_error_signature_max_image_count'] = sprintf($txt['profile_error_signature_max_image_count'], $sig_limits[3]);
  2542. return 'signature_max_image_count';
  2543. }
  2544. // What about too many smileys!
  2545. $smiley_parsed = $unparsed_signature;
  2546. parsesmileys($smiley_parsed);
  2547. $smiley_count = substr_count(strtolower($smiley_parsed), '<img') - substr_count(strtolower($unparsed_signature), '<img');
  2548. if (!empty($sig_limits[4]) && $sig_limits[4] == -1 && $smiley_count > 0)
  2549. return 'signature_allow_smileys';
  2550. elseif (!empty($sig_limits[4]) && $sig_limits[4] > 0 && $smiley_count > $sig_limits[4])
  2551. {
  2552. $txt['profile_error_signature_max_smileys'] = sprintf($txt['profile_error_signature_max_smileys'], $sig_limits[4]);
  2553. return 'signature_max_smileys';
  2554. }
  2555. // Maybe we are abusing font sizes?
  2556. if (!empty($sig_limits[7]) && preg_match_all('~\[size=([\d\.]+)?(px|pt|em|x-large|larger)~i', $unparsed_signature, $matches) !== false && isset($matches[2]))
  2557. {
  2558. foreach ($matches[1] as $ind => $size)
  2559. {
  2560. $limit_broke = 0;
  2561. // Attempt to allow all sizes of abuse, so to speak.
  2562. if ($matches[2][$ind] == 'px' && $size > $sig_limits[7])
  2563. $limit_broke = $sig_limits[7] . 'px';
  2564. elseif ($matches[2][$ind] == 'pt' && $size > ($sig_limits[7] * 0.75))
  2565. $limit_broke = ((int) $sig_limits[7] * 0.75) . 'pt';
  2566. elseif ($matches[2][$ind] == 'em' && $size > ((float) $sig_limits[7] / 16))
  2567. $limit_broke = ((float) $sig_limits[7] / 16) . 'em';
  2568. elseif ($matches[2][$ind] != 'px' && $matches[2][$ind] != 'pt' && $matches[2][$ind] != 'em' && $sig_limits[7] < 18)
  2569. $limit_broke = 'large';
  2570. if ($limit_broke)
  2571. {
  2572. $txt['profile_error_signature_max_font_size'] = sprintf($txt['profile_error_signature_max_font_size'], $limit_broke);
  2573. return 'signature_max_font_size';
  2574. }
  2575. }
  2576. }
  2577. // The difficult one - image sizes! Don't error on this - just fix it.
  2578. if ((!empty($sig_limits[5]) || !empty($sig_limits[6])))
  2579. {
  2580. // Get all BBC tags...
  2581. preg_match_all('~\[img(\s+width=([\d]+))?(\s+height=([\d]+))?(\s+width=([\d]+))?\s*\](?:<br />)*([^<">]+?)(?:<br />)*\[/img\]~i', $unparsed_signature, $matches);
  2582. // ... and all HTML ones.
  2583. preg_match_all('~<img\s+src=(?:")?((?:http://|ftp://|https://|ftps://).+?)(?:")?(?:\s+alt=(?:")?(.*?)(?:")?)?(?:\s?/)?>~i', $unparsed_signature, $matches2, PREG_PATTERN_ORDER);
  2584. // And stick the HTML in the BBC.
  2585. if (!empty($matches2))
  2586. {
  2587. foreach ($matches2[0] as $ind => $dummy)
  2588. {
  2589. $matches[0][] = $matches2[0][$ind];
  2590. $matches[1][] = '';
  2591. $matches[2][] = '';
  2592. $matches[3][] = '';
  2593. $matches[4][] = '';
  2594. $matches[5][] = '';
  2595. $matches[6][] = '';
  2596. $matches[7][] = $matches2[1][$ind];
  2597. }
  2598. }
  2599. $replaces = array();
  2600. // Try to find all the images!
  2601. if (!empty($matches))
  2602. {
  2603. foreach ($matches[0] as $key => $image)
  2604. {
  2605. $width = -1; $height = -1;
  2606. // Does it have predefined restraints? Width first.
  2607. if ($matches[6][$key])
  2608. $matches[2][$key] = $matches[6][$key];
  2609. if ($matches[2][$key] && $sig_limits[5] && $matches[2][$key] > $sig_limits[5])
  2610. {
  2611. $width = $sig_limits[5];
  2612. $matches[4][$key] = $matches[4][$key] * ($width / $matches[2][$key]);
  2613. }
  2614. elseif ($matches[2][$key])
  2615. $width = $matches[2][$key];
  2616. // ... and height.
  2617. if ($matches[4][$key] && $sig_limits[6] && $matches[4][$key] > $sig_limits[6])
  2618. {
  2619. $height = $sig_limits[6];
  2620. if ($width != -1)
  2621. $width = $width * ($height / $matches[4][$key]);
  2622. }
  2623. elseif ($matches[4][$key])
  2624. $height = $matches[4][$key];
  2625. // If the dimensions are still not fixed - we need to check the actual image.
  2626. if (($width == -1 && $sig_limits[5]) || ($height == -1 && $sig_limits[6]))
  2627. {
  2628. $sizes = url_image_size($matches[7][$key]);
  2629. if (is_array($sizes))
  2630. {
  2631. // Too wide?
  2632. if ($sizes[0] > $sig_limits[5] && $sig_limits[5])
  2633. {
  2634. $width = $sig_limits[5];
  2635. $sizes[1] = $sizes[1] * ($width / $sizes[0]);
  2636. }
  2637. // Too high?
  2638. if ($sizes[1] > $sig_limits[6] && $sig_limits[6])
  2639. {
  2640. $height = $sig_limits[6];
  2641. if ($width == -1)
  2642. $width = $sizes[0];
  2643. $width = $width * ($height / $sizes[1]);
  2644. }
  2645. elseif ($width != -1)
  2646. $height = $sizes[1];
  2647. }
  2648. }
  2649. // Did we come up with some changes? If so remake the string.
  2650. if ($width != -1 || $height != -1)
  2651. $replaces[$image] = '[img' . ($width != -1 ? ' width=' . round($width) : '') . ($height != -1 ? ' height=' . round($height) : '') . ']' . $matches[7][$key] . '[/img]';
  2652. }
  2653. if (!empty($replaces))
  2654. $value = str_replace(array_keys($replaces), array_values($replaces), $value);
  2655. }
  2656. }
  2657. // Any disabled BBC?
  2658. $disabledSigBBC = implode('|', $disabledTags);
  2659. if (!empty($disabledSigBBC))
  2660. {
  2661. if (preg_match('~\[(' . $disabledSigBBC . '[ =\]/])~i', $unparsed_signature, $matches) !== false && isset($matches[1]))
  2662. {
  2663. $disabledTags = array_unique($disabledTags);
  2664. $txt['profile_error_signature_disabled_bbc'] = sprintf($txt['profile_error_signature_disabled_bbc'], implode(', ', $disabledTags));
  2665. return 'signature_disabled_bbc';
  2666. }
  2667. }
  2668. }
  2669. preparsecode($value);
  2670. return true;
  2671. }
  2672. /**
  2673. * Validate an email address.
  2674. *
  2675. * @param string $email
  2676. * @param int $memID = 0
  2677. * @return bool|string
  2678. */
  2679. function profileValidateEmail($email, $memID = 0)
  2680. {
  2681. global $smcFunc, $context;
  2682. $email = strtr($email, array('&#039;' => '\''));
  2683. // Check the name and email for validity.
  2684. if (trim($email) == '')
  2685. return 'no_email';
  2686. if (preg_match('~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~', $email) == 0)
  2687. return 'bad_email';
  2688. // Email addresses should be and stay unique.
  2689. $request = $smcFunc['db_query']('', '
  2690. SELECT id_member
  2691. FROM {db_prefix}members
  2692. WHERE ' . ($memID != 0 ? 'id_member != {int:selected_member} AND ' : '') . '
  2693. email_address = {string:email_address}
  2694. LIMIT 1',
  2695. array(
  2696. 'selected_member' => $memID,
  2697. 'email_address' => $email,
  2698. )
  2699. );
  2700. if ($smcFunc['db_num_rows']($request) > 0)
  2701. return 'email_taken';
  2702. $smcFunc['db_free_result']($request);
  2703. return true;
  2704. }
  2705. /**
  2706. * Reload a users settings.
  2707. */
  2708. function profileReloadUser()
  2709. {
  2710. global $sourcedir, $modSettings, $context, $cur_profile, $smcFunc, $profile_vars;
  2711. // Log them back in - using the verify password as they must have matched and this one doesn't get changed by anyone!
  2712. if (isset($_POST['passwrd2']) && $_POST['passwrd2'] != '')
  2713. {
  2714. require_once($sourcedir . '/Subs-Auth.php');
  2715. setLoginCookie(60 * $modSettings['cookieTime'], $context['id_member'], sha1(sha1(strtolower($cur_profile['member_name']) . un_htmlspecialchars($_POST['passwrd2'])) . $cur_profile['password_salt']));
  2716. }
  2717. loadUserSettings();
  2718. writeLog();
  2719. }
  2720. /**
  2721. * Send the user a new activation email if they need to reactivate!
  2722. */
  2723. function profileSendActivation()
  2724. {
  2725. global $sourcedir, $profile_vars, $txt, $context, $scripturl, $smcFunc, $cookiename, $cur_profile, $language, $modSettings;
  2726. require_once($sourcedir . '/Subs-Post.php');
  2727. // Shouldn't happen but just in case.
  2728. if (empty($profile_vars['email_address']))
  2729. return;
  2730. $replacements = array(
  2731. 'ACTIVATIONLINK' => $scripturl . '?action=activate;u=' . $context['id_member'] . ';code=' . $profile_vars['validation_code'],
  2732. 'ACTIVATIONCODE' => $profile_vars['validation_code'],
  2733. 'ACTIVATIONLINKWITHOUTCODE' => $scripturl . '?action=activate;u=' . $context['id_member'],
  2734. );
  2735. // Send off the email.
  2736. $emaildata = loadEmailTemplate('activate_reactivate', $replacements, empty($cur_profile['lngfile']) || empty($modSettings['userLanguage']) ? $language : $cur_profile['lngfile']);
  2737. sendmail($profile_vars['email_address'], $emaildata['subject'], $emaildata['body'], null, null, false, 0);
  2738. // Log the user out.
  2739. $smcFunc['db_query']('', '
  2740. DELETE FROM {db_prefix}log_online
  2741. WHERE id_member = {int:selected_member}',
  2742. array(
  2743. 'selected_member' => $context['id_member'],
  2744. )
  2745. );
  2746. $_SESSION['log_time'] = 0;
  2747. $_SESSION['login_' . $cookiename] = serialize(array(0, '', 0));
  2748. if (isset($_COOKIE[$cookiename]))
  2749. $_COOKIE[$cookiename] = '';
  2750. loadUserSettings();
  2751. $context['user']['is_logged'] = false;
  2752. $context['user']['is_guest'] = true;
  2753. // Send them to the done-with-registration-login screen.
  2754. loadTemplate('Register');
  2755. $context['page_title'] = $txt['profile'];
  2756. $context['sub_template'] = 'after';
  2757. $context['title'] = $txt['activate_changed_email_title'];
  2758. $context['description'] = $txt['activate_changed_email_desc'];
  2759. // We're gone!
  2760. obExit();
  2761. }
  2762. /**
  2763. * Function to allow the user to choose group membership etc...
  2764. *
  2765. * @param int $memID id_member
  2766. */
  2767. function groupMembership($memID)
  2768. {
  2769. global $txt, $scripturl, $user_profile, $user_info, $context, $modSettings, $smcFunc;
  2770. $curMember = $user_profile[$memID];
  2771. $context['primary_group'] = $curMember['id_group'];
  2772. // Can they manage groups?
  2773. $context['can_manage_membergroups'] = allowedTo('manage_membergroups');
  2774. $context['can_manage_protected'] = allowedTo('admin_forum');
  2775. $context['can_edit_primary'] = $context['can_manage_protected'];
  2776. $context['update_message'] = isset($_GET['msg']) && isset($txt['group_membership_msg_' . $_GET['msg']]) ? $txt['group_membership_msg_' . $_GET['msg']] : '';
  2777. // Get all the groups this user is a member of.
  2778. $groups = explode(',', $curMember['additional_groups']);
  2779. $groups[] = $curMember['id_group'];
  2780. // Ensure the query doesn't croak!
  2781. if (empty($groups))
  2782. $groups = array(0);
  2783. // Just to be sure...
  2784. foreach ($groups as $k => $v)
  2785. $groups[$k] = (int) $v;
  2786. // Get all the membergroups they can join.
  2787. $request = $smcFunc['db_query']('', '
  2788. SELECT mg.id_group, mg.group_name, mg.description, mg.group_type, mg.online_color, mg.hidden,
  2789. IFNULL(lgr.id_member, 0) AS pending
  2790. FROM {db_prefix}membergroups AS mg
  2791. LEFT JOIN {db_prefix}log_group_requests AS lgr ON (lgr.id_member = {int:selected_member} AND lgr.id_group = mg.id_group)
  2792. WHERE (mg.id_group IN ({array_int:group_list})
  2793. OR mg.group_type > {int:nonjoin_group_id})
  2794. AND mg.min_posts = {int:min_posts}
  2795. AND mg.id_group != {int:moderator_group}
  2796. ORDER BY group_name',
  2797. array(
  2798. 'group_list' => $groups,
  2799. 'selected_member' => $memID,
  2800. 'nonjoin_group_id' => 1,
  2801. 'min_posts' => -1,
  2802. 'moderator_group' => 3,
  2803. )
  2804. );
  2805. // This beast will be our group holder.
  2806. $context['groups'] = array(
  2807. 'member' => array(),
  2808. 'available' => array()
  2809. );
  2810. while ($row = $smcFunc['db_fetch_assoc']($request))
  2811. {
  2812. // Can they edit their primary group?
  2813. if (($row['id_group'] == $context['primary_group'] && $row['group_type'] > 1) || ($row['hidden'] != 2 && $context['primary_group'] == 0 && in_array($row['id_group'], $groups)))
  2814. $context['can_edit_primary'] = true;
  2815. // If they can't manage (protected) groups, and it's not publically joinable or already assigned, they can't see it.
  2816. if (((!$context['can_manage_protected'] && $row['group_type'] == 1) || (!$context['can_manage_membergroups'] && $row['group_type'] == 0)) && $row['id_group'] != $context['primary_group'])
  2817. continue;
  2818. $context['groups'][in_array($row['id_group'], $groups) ? 'member' : 'available'][$row['id_group']] = array(
  2819. 'id' => $row['id_group'],
  2820. 'name' => $row['group_name'],
  2821. 'desc' => $row['description'],
  2822. 'color' => $row['online_color'],
  2823. 'type' => $row['group_type'],
  2824. 'pending' => $row['pending'],
  2825. 'is_primary' => $row['id_group'] == $context['primary_group'],
  2826. 'can_be_primary' => $row['hidden'] != 2,
  2827. // Anything more than this needs to be done through account settings for security.
  2828. 'can_leave' => $row['id_group'] != 1 && $row['group_type'] > 1 ? true : false,
  2829. );
  2830. }
  2831. $smcFunc['db_free_result']($request);
  2832. // Add registered members on the end.
  2833. $context['groups']['member'][0] = array(
  2834. 'id' => 0,
  2835. 'name' => $txt['regular_members'],
  2836. 'desc' => $txt['regular_members_desc'],
  2837. 'type' => 0,
  2838. 'is_primary' => $context['primary_group'] == 0 ? true : false,
  2839. 'can_be_primary' => true,
  2840. 'can_leave' => 0,
  2841. );
  2842. // No changing primary one unless you have enough groups!
  2843. if (count($context['groups']['member']) < 2)
  2844. $context['can_edit_primary'] = false;
  2845. // In the special case that someone is requesting membership of a group, setup some special context vars.
  2846. if (isset($_REQUEST['request']) && isset($context['groups']['available'][(int) $_REQUEST['request']]) && $context['groups']['available'][(int) $_REQUEST['request']]['type'] == 2)
  2847. $context['group_request'] = $context['groups']['available'][(int) $_REQUEST['request']];
  2848. }
  2849. /**
  2850. * This function actually makes all the group changes
  2851. *
  2852. * @param array $profile_vars
  2853. * @param array $post_errors
  2854. * @param int $memID id_member
  2855. * @return mixed
  2856. */
  2857. function groupMembership2($profile_vars, $post_errors, $memID)
  2858. {
  2859. global $user_info, $sourcedir, $context, $user_profile, $modSettings, $txt, $smcFunc, $scripturl, $language;
  2860. // Let's be extra cautious...
  2861. if (!$context['user']['is_owner'] || empty($modSettings['show_group_membership']))
  2862. isAllowedTo('manage_membergroups');
  2863. if (!isset($_REQUEST['gid']) && !isset($_POST['primary']))
  2864. fatal_lang_error('no_access', false);
  2865. checkSession(isset($_GET['gid']) ? 'get' : 'post');
  2866. $old_profile = &$user_profile[$memID];
  2867. $context['can_manage_membergroups'] = allowedTo('manage_membergroups');
  2868. $context['can_manage_protected'] = allowedTo('admin_forum');
  2869. // By default the new primary is the old one.
  2870. $newPrimary = $old_profile['id_group'];
  2871. $addGroups = array_flip(explode(',', $old_profile['additional_groups']));
  2872. $canChangePrimary = $old_profile['id_group'] == 0 ? 1 : 0;
  2873. $changeType = isset($_POST['primary']) ? 'primary' : (isset($_POST['req']) ? 'request' : 'free');
  2874. // One way or another, we have a target group in mind...
  2875. $group_id = isset($_REQUEST['gid']) ? (int) $_REQUEST['gid'] : (int) $_POST['primary'];
  2876. $foundTarget = $changeType == 'primary' && $group_id == 0 ? true : false;
  2877. // Sanity check!!
  2878. if ($group_id == 1)
  2879. isAllowedTo('admin_forum');
  2880. // Protected groups too!
  2881. else
  2882. {
  2883. $request = $smcFunc['db_query']('', '
  2884. SELECT group_type
  2885. FROM {db_prefix}membergroups
  2886. WHERE id_group = {int:current_group}
  2887. LIMIT {int:limit}',
  2888. array(
  2889. 'current_group' => $group_id,
  2890. 'limit' => 1,
  2891. )
  2892. );
  2893. list ($is_protected) = $smcFunc['db_fetch_row']($request);
  2894. $smcFunc['db_free_result']($request);
  2895. if ($is_protected == 1)
  2896. isAllowedTo('admin_forum');
  2897. }
  2898. // What ever we are doing, we need to determine if changing primary is possible!
  2899. $request = $smcFunc['db_query']('', '
  2900. SELECT id_group, group_type, hidden, group_name
  2901. FROM {db_prefix}membergroups
  2902. WHERE id_group IN ({int:group_list}, {int:current_group})',
  2903. array(
  2904. 'group_list' => $group_id,
  2905. 'current_group' => $old_profile['id_group'],
  2906. )
  2907. );
  2908. while ($row = $smcFunc['db_fetch_assoc']($request))
  2909. {
  2910. // Is this the new group?
  2911. if ($row['id_group'] == $group_id)
  2912. {
  2913. $foundTarget = true;
  2914. $group_name = $row['group_name'];
  2915. // Does the group type match what we're doing - are we trying to request a non-requestable group?
  2916. if ($changeType == 'request' && $row['group_type'] != 2)
  2917. fatal_lang_error('no_access', false);
  2918. // What about leaving a requestable group we are not a member of?
  2919. elseif ($changeType == 'free' && $row['group_type'] == 2 && $old_profile['id_group'] != $row['id_group'] && !isset($addGroups[$row['id_group']]))
  2920. fatal_lang_error('no_access', false);
  2921. elseif ($changeType == 'free' && $row['group_type'] != 3 && $row['group_type'] != 2)
  2922. fatal_lang_error('no_access', false);
  2923. // We can't change the primary group if this is hidden!
  2924. if ($row['hidden'] == 2)
  2925. $canChangePrimary = false;
  2926. }
  2927. // If this is their old primary, can we change it?
  2928. if ($row['id_group'] == $old_profile['id_group'] && ($row['group_type'] > 1 || $context['can_manage_membergroups']) && $canChangePrimary !== false)
  2929. $canChangePrimary = 1;
  2930. // If we are not doing a force primary move, don't do it automatically if current primary is not 0.
  2931. if ($changeType != 'primary' && $old_profile['id_group'] != 0)
  2932. $canChangePrimary = false;
  2933. // If this is the one we are acting on, can we even act?
  2934. if ((!$context['can_manage_protected'] && $row['group_type'] == 1) || (!$context['can_manage_membergroups'] && $row['group_type'] == 0))
  2935. $canChangePrimary = false;
  2936. }
  2937. $smcFunc['db_free_result']($request);
  2938. // Didn't find the target?
  2939. if (!$foundTarget)
  2940. fatal_lang_error('no_access', false);
  2941. // Final security check, don't allow users to promote themselves to admin.
  2942. if ($context['can_manage_membergroups'] && !allowedTo('admin_forum'))
  2943. {
  2944. $request = $smcFunc['db_query']('', '
  2945. SELECT COUNT(permission)
  2946. FROM {db_prefix}permissions
  2947. WHERE id_group = {int:selected_group}
  2948. AND permission = {string:admin_forum}
  2949. AND add_deny = {int:not_denied}',
  2950. array(
  2951. 'selected_group' => $group_id,
  2952. 'not_denied' => 1,
  2953. 'admin_forum' => 'admin_forum',
  2954. )
  2955. );
  2956. list ($disallow) = $smcFunc['db_fetch_row']($request);
  2957. $smcFunc['db_free_result']($request);
  2958. if ($disallow)
  2959. isAllowedTo('admin_forum');
  2960. }
  2961. // If we're requesting, add the note then return.
  2962. if ($changeType == 'request')
  2963. {
  2964. $request = $smcFunc['db_query']('', '
  2965. SELECT id_member
  2966. FROM {db_prefix}log_group_requests
  2967. WHERE id_member = {int:selected_member}
  2968. AND id_group = {int:selected_group}',
  2969. array(
  2970. 'selected_member' => $memID,
  2971. 'selected_group' => $group_id,
  2972. )
  2973. );
  2974. if ($smcFunc['db_num_rows']($request) != 0)
  2975. fatal_lang_error('profile_error_already_requested_group');
  2976. $smcFunc['db_free_result']($request);
  2977. // Log the request.
  2978. $smcFunc['db_insert']('',
  2979. '{db_prefix}log_group_requests',
  2980. array(
  2981. 'id_member' => 'int', 'id_group' => 'int', 'time_applied' => 'int', 'reason' => 'string-65534',
  2982. ),
  2983. array(
  2984. $memID, $group_id, time(), $_POST['reason'],
  2985. ),
  2986. array('id_request')
  2987. );
  2988. // Send an email to all group moderators etc.
  2989. require_once($sourcedir . '/Subs-Post.php');
  2990. // Do we have any group moderators?
  2991. $request = $smcFunc['db_query']('', '
  2992. SELECT id_member
  2993. FROM {db_prefix}group_moderators
  2994. WHERE id_group = {int:selected_group}',
  2995. array(
  2996. 'selected_group' => $group_id,
  2997. )
  2998. );
  2999. $moderators = array();
  3000. while ($row = $smcFunc['db_fetch_assoc']($request))
  3001. $moderators[] = $row['id_member'];
  3002. $smcFunc['db_free_result']($request);
  3003. // Otherwise this is the backup!
  3004. if (empty($moderators))
  3005. {
  3006. require_once($sourcedir . '/Subs-Members.php');
  3007. $moderators = membersAllowedTo('manage_membergroups');
  3008. }
  3009. if (!empty($moderators))
  3010. {
  3011. $request = $smcFunc['db_query']('', '
  3012. SELECT id_member, email_address, lngfile, member_name, mod_prefs
  3013. FROM {db_prefix}members
  3014. WHERE id_member IN ({array_int:moderator_list})
  3015. AND notify_types != {int:no_notifications}
  3016. ORDER BY lngfile',
  3017. array(
  3018. 'moderator_list' => $moderators,
  3019. 'no_notifications' => 4,
  3020. )
  3021. );
  3022. while ($row = $smcFunc['db_fetch_assoc']($request))
  3023. {
  3024. // Check whether they are interested.
  3025. if (!empty($row['mod_prefs']))
  3026. {
  3027. list(,, $pref_binary) = explode('|', $row['mod_prefs']);
  3028. if (!($pref_binary & 4))
  3029. continue;
  3030. }
  3031. $replacements = array(
  3032. 'RECPNAME' => $row['member_name'],
  3033. 'APPYNAME' => $old_profile['member_name'],
  3034. 'GROUPNAME' => $group_name,
  3035. 'REASON' => $_POST['reason'],
  3036. 'MODLINK' => $scripturl . '?action=moderate;area=groups;sa=requests',
  3037. );
  3038. $emaildata = loadEmailTemplate('request_membership', $replacements, empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile']);
  3039. sendmail($row['email_address'], $emaildata['subject'], $emaildata['body'], null, null, false, 2);
  3040. }
  3041. $smcFunc['db_free_result']($request);
  3042. }
  3043. return $changeType;
  3044. }
  3045. // Otherwise we are leaving/joining a group.
  3046. elseif ($changeType == 'free')
  3047. {
  3048. // Are we leaving?
  3049. if ($old_profile['id_group'] == $group_id || isset($addGroups[$group_id]))
  3050. {
  3051. if ($old_profile['id_group'] == $group_id)
  3052. $newPrimary = 0;
  3053. else
  3054. unset($addGroups[$group_id]);
  3055. }
  3056. // ... if not, must be joining.
  3057. else
  3058. {
  3059. // Can we change the primary, and do we want to?
  3060. if ($canChangePrimary)
  3061. {
  3062. if ($old_profile['id_group'] != 0)
  3063. $addGroups[$old_profile['id_group']] = -1;
  3064. $newPrimary = $group_id;
  3065. }
  3066. // Otherwise it's an additional group...
  3067. else
  3068. $addGroups[$group_id] = -1;
  3069. }
  3070. }
  3071. // Finally, we must be setting the primary.
  3072. elseif ($canChangePrimary)
  3073. {
  3074. if ($old_profile['id_group'] != 0)
  3075. $addGroups[$old_profile['id_group']] = -1;
  3076. if (isset($addGroups[$group_id]))
  3077. unset($addGroups[$group_id]);
  3078. $newPrimary = $group_id;
  3079. }
  3080. // Finally, we can make the changes!
  3081. foreach ($addGroups as $id => $dummy)
  3082. if (empty($id))
  3083. unset($addGroups[$id]);
  3084. $addGroups = implode(',', array_flip($addGroups));
  3085. // Ensure that we don't cache permissions if the group is changing.
  3086. if ($context['user']['is_owner'])
  3087. $_SESSION['mc']['time'] = 0;
  3088. else
  3089. updateSettings(array('settings_updated' => time()));
  3090. updateMemberData($memID, array('id_group' => $newPrimary, 'additional_groups' => $addGroups));
  3091. return $changeType;
  3092. }
  3093. ?>